@@ -25,6 +25,8 @@ export enum Operation {
25
25
SET = 'set' ,
26
26
}
27
27
28
+ export type OnRequestCallback = ( parameters : { type : Operation ; url : string } ) => void
29
+
28
30
// TODO: Replace with `promises` import of `node:stream` once we can drop
29
31
// support for Node 14.
30
32
const pipeline = promisify ( stream . pipeline )
@@ -49,7 +51,7 @@ interface BlobsServerOptions {
49
51
/**
50
52
* Callback function to be called on every request.
51
53
*/
52
- onRequest ?: ( parameters : { type : Operation } ) => void
54
+ onRequest ?: OnRequestCallback
53
55
54
56
/**
55
57
* Port to run the server on. Defaults to a random port.
@@ -68,7 +70,7 @@ export class BlobsServer {
68
70
private debug : boolean
69
71
private directory : string
70
72
private logger : Logger
71
- private onRequest : ( parameters : { type : Operation } ) => void
73
+ private onRequest ?: OnRequestCallback
72
74
private port : number
73
75
private server ?: http . Server
74
76
private token ?: string
@@ -79,18 +81,24 @@ export class BlobsServer {
79
81
this . debug = debug === true
80
82
this . directory = directory
81
83
this . logger = logger ?? console . log
82
- this . onRequest =
83
- onRequest ??
84
- ( ( ) => {
85
- // no-op
86
- } )
84
+ this . onRequest = onRequest
87
85
this . port = port || 0
88
86
this . token = token
89
87
this . tokenHash = createHmac ( 'sha256' , Math . random . toString ( ) )
90
88
. update ( token ?? Math . random . toString ( ) )
91
89
. digest ( 'hex' )
92
90
}
93
91
92
+ private dispatchOnRequestEvent ( type : Operation , url : string | URL ) {
93
+ if ( ! this . onRequest ) {
94
+ return
95
+ }
96
+
97
+ const urlPath = url instanceof URL ? url . pathname + url . search : url
98
+
99
+ this . onRequest ( { type, url : urlPath } )
100
+ }
101
+
94
102
logDebug ( ...message : unknown [ ] ) {
95
103
if ( ! this . debug ) {
96
104
return
@@ -159,7 +167,7 @@ export class BlobsServer {
159
167
return this . listBlobs ( { dataPath, metadataPath, rootPath, req, res, url } )
160
168
}
161
169
162
- this . onRequest ( { type : Operation . GET } )
170
+ this . dispatchOnRequestEvent ( Operation . GET , url )
163
171
164
172
const headers : Record < string , string > = { }
165
173
@@ -230,8 +238,6 @@ export class BlobsServer {
230
238
res : http . ServerResponse
231
239
url : URL
232
240
} ) {
233
- this . onRequest ( { type : Operation . LIST } )
234
-
235
241
const { dataPath, rootPath, req, res, url } = options
236
242
const directories = url . searchParams . get ( 'directories' ) === 'true'
237
243
const prefix = url . searchParams . get ( 'prefix' ) ?? ''
@@ -240,6 +246,8 @@ export class BlobsServer {
240
246
directories : [ ] ,
241
247
}
242
248
249
+ this . dispatchOnRequestEvent ( Operation . LIST , url )
250
+
243
251
try {
244
252
await BlobsServer . walk ( { directories, path : dataPath , prefix, rootPath, result } )
245
253
} catch ( error ) {
@@ -356,7 +364,7 @@ export class BlobsServer {
356
364
357
365
switch ( req . method ?. toLowerCase ( ) ) {
358
366
case HTTPMethod . DELETE : {
359
- this . onRequest ( { type : Operation . DELETE } )
367
+ this . dispatchOnRequestEvent ( Operation . DELETE , req . url )
360
368
361
369
return this . delete ( req , res )
362
370
}
@@ -366,13 +374,13 @@ export class BlobsServer {
366
374
}
367
375
368
376
case HTTPMethod . PUT : {
369
- this . onRequest ( { type : Operation . SET } )
377
+ this . dispatchOnRequestEvent ( Operation . SET , req . url )
370
378
371
379
return this . put ( req , res )
372
380
}
373
381
374
382
case HTTPMethod . HEAD : {
375
- this . onRequest ( { type : Operation . GET_METADATA } )
383
+ this . dispatchOnRequestEvent ( Operation . GET_METADATA , req . url )
376
384
377
385
return this . head ( req , res )
378
386
}
0 commit comments