@@ -6,11 +6,20 @@ import { dirname, join, relative, resolve, sep } from 'node:path'
66
77import { ListResponse } from './backend/list.ts'
88import { decodeMetadata , encodeMetadata , METADATA_HEADER_INTERNAL } from './metadata.ts'
9+ import { HTTPMethod } from './types.ts'
910import { isNodeError , Logger } from './util.ts'
1011
1112const API_URL_PATH = / \/ a p i \/ v 1 \/ s i t e s \/ (?< site_id > [ ^ / ] + ) \/ b l o b s \/ ? (?< key > [ ^ ? ] * ) /
1213const DEFAULT_STORE = 'production'
1314
15+ export enum Operation {
16+ DELETE = 'delete' ,
17+ GET = 'get' ,
18+ GET_METADATA = 'getMetadata' ,
19+ LIST = 'list' ,
20+ SET = 'set' ,
21+ }
22+
1423interface BlobsServerOptions {
1524 /**
1625 * Whether debug-level information should be logged, such as internal errors
@@ -28,6 +37,11 @@ interface BlobsServerOptions {
2837 */
2938 logger ?: Logger
3039
40+ /**
41+ * Callback function to be called on every request.
42+ */
43+ onRequest ?: ( parameters : { type : Operation } ) => void
44+
3145 /**
3246 * Port to run the server on. Defaults to a random port.
3347 */
@@ -45,16 +59,22 @@ export class BlobsServer {
4559 private debug : boolean
4660 private directory : string
4761 private logger : Logger
62+ private onRequest : ( parameters : { type : Operation } ) => void
4863 private port : number
4964 private server ?: http . Server
5065 private token ?: string
5166 private tokenHash : string
5267
53- constructor ( { debug, directory, logger, port, token } : BlobsServerOptions ) {
68+ constructor ( { debug, directory, logger, onRequest , port, token } : BlobsServerOptions ) {
5469 this . address = ''
5570 this . debug = debug === true
5671 this . directory = directory
5772 this . logger = logger ?? console . log
73+ this . onRequest =
74+ onRequest ??
75+ ( ( ) => {
76+ // no-op
77+ } )
5878 this . port = port || 0
5979 this . token = token
6080 this . tokenHash = createHmac ( 'sha256' , Math . random . toString ( ) )
@@ -124,6 +144,8 @@ export class BlobsServer {
124144 return this . list ( { dataPath, metadataPath, rootPath, req, res, url } )
125145 }
126146
147+ this . onRequest ( { type : Operation . GET } )
148+
127149 const headers : Record < string , string > = { }
128150
129151 try {
@@ -193,6 +215,8 @@ export class BlobsServer {
193215 res : http . ServerResponse
194216 url : URL
195217 } ) {
218+ this . onRequest ( { type : Operation . LIST } )
219+
196220 const { dataPath, rootPath, req, res, url } = options
197221 const directories = url . searchParams . get ( 'directories' ) === 'true'
198222 const prefix = url . searchParams . get ( 'prefix' ) ?? ''
@@ -295,17 +319,26 @@ export class BlobsServer {
295319 return this . sendResponse ( req , res , 403 )
296320 }
297321
298- switch ( req . method ) {
299- case 'DELETE' :
322+ switch ( req . method ?. toLowerCase ( ) ) {
323+ case HTTPMethod . DELETE : {
324+ this . onRequest ( { type : Operation . DELETE } )
325+
300326 return this . delete ( req , res )
327+ }
301328
302- case ' GET' :
329+ case HTTPMethod . GET : {
303330 return this . get ( req , res )
331+ }
332+
333+ case HTTPMethod . PUT : {
334+ this . onRequest ( { type : Operation . SET } )
304335
305- case 'PUT' :
306336 return this . put ( req , res )
337+ }
338+
339+ case HTTPMethod . HEAD : {
340+ this . onRequest ( { type : Operation . GET_METADATA } )
307341
308- case 'HEAD' : {
309342 return this . head ( req , res )
310343 }
311344
0 commit comments