@@ -25,6 +25,8 @@ export enum Operation {
2525 SET = 'set' ,
2626}
2727
28+ export type OnRequestCallback = ( parameters : { type : Operation ; url : string } ) => void
29+
2830// TODO: Replace with `promises` import of `node:stream` once we can drop
2931// support for Node 14.
3032const pipeline = promisify ( stream . pipeline )
@@ -49,7 +51,7 @@ interface BlobsServerOptions {
4951 /**
5052 * Callback function to be called on every request.
5153 */
52- onRequest ?: ( parameters : { type : Operation } ) => void
54+ onRequest ?: OnRequestCallback
5355
5456 /**
5557 * Port to run the server on. Defaults to a random port.
@@ -68,7 +70,7 @@ export class BlobsServer {
6870 private debug : boolean
6971 private directory : string
7072 private logger : Logger
71- private onRequest : ( parameters : { type : Operation } ) => void
73+ private onRequest ?: OnRequestCallback
7274 private port : number
7375 private server ?: http . Server
7476 private token ?: string
@@ -79,18 +81,24 @@ export class BlobsServer {
7981 this . debug = debug === true
8082 this . directory = directory
8183 this . logger = logger ?? console . log
82- this . onRequest =
83- onRequest ??
84- ( ( ) => {
85- // no-op
86- } )
84+ this . onRequest = onRequest
8785 this . port = port || 0
8886 this . token = token
8987 this . tokenHash = createHmac ( 'sha256' , Math . random . toString ( ) )
9088 . update ( token ?? Math . random . toString ( ) )
9189 . digest ( 'hex' )
9290 }
9391
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+
94102 logDebug ( ...message : unknown [ ] ) {
95103 if ( ! this . debug ) {
96104 return
@@ -159,7 +167,7 @@ export class BlobsServer {
159167 return this . listBlobs ( { dataPath, metadataPath, rootPath, req, res, url } )
160168 }
161169
162- this . onRequest ( { type : Operation . GET } )
170+ this . dispatchOnRequestEvent ( Operation . GET , url )
163171
164172 const headers : Record < string , string > = { }
165173
@@ -230,8 +238,6 @@ export class BlobsServer {
230238 res : http . ServerResponse
231239 url : URL
232240 } ) {
233- this . onRequest ( { type : Operation . LIST } )
234-
235241 const { dataPath, rootPath, req, res, url } = options
236242 const directories = url . searchParams . get ( 'directories' ) === 'true'
237243 const prefix = url . searchParams . get ( 'prefix' ) ?? ''
@@ -240,6 +246,8 @@ export class BlobsServer {
240246 directories : [ ] ,
241247 }
242248
249+ this . dispatchOnRequestEvent ( Operation . LIST , url )
250+
243251 try {
244252 await BlobsServer . walk ( { directories, path : dataPath , prefix, rootPath, result } )
245253 } catch ( error ) {
@@ -356,7 +364,7 @@ export class BlobsServer {
356364
357365 switch ( req . method ?. toLowerCase ( ) ) {
358366 case HTTPMethod . DELETE : {
359- this . onRequest ( { type : Operation . DELETE } )
367+ this . dispatchOnRequestEvent ( Operation . DELETE , req . url )
360368
361369 return this . delete ( req , res )
362370 }
@@ -366,13 +374,13 @@ export class BlobsServer {
366374 }
367375
368376 case HTTPMethod . PUT : {
369- this . onRequest ( { type : Operation . SET } )
377+ this . dispatchOnRequestEvent ( Operation . SET , req . url )
370378
371379 return this . put ( req , res )
372380 }
373381
374382 case HTTPMethod . HEAD : {
375- this . onRequest ( { type : Operation . GET_METADATA } )
383+ this . dispatchOnRequestEvent ( Operation . GET_METADATA , req . url )
376384
377385 return this . head ( req , res )
378386 }
0 commit comments