Skip to content

Commit 344d864

Browse files
committed
feat: add url to server's onRequest callback
1 parent 90a0412 commit 344d864

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/server.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ afterEach(() => {
3030
const siteID = '9a003659-aaaa-0000-aaaa-63d3720d8621'
3131
const token = 'my-very-secret-token'
3232

33-
test('Reads and writes from the file system', async () => {
33+
test.only('Reads and writes from the file system', async () => {
3434
const metadata = {
3535
features: {
3636
blobs: true,
@@ -40,11 +40,11 @@ test('Reads and writes from the file system', async () => {
4040
}
4141

4242
// Store #1: Edge access
43-
const server1Ops: string[] = []
43+
const server1Ops: { type: string; url: string }[] = []
4444
const directory1 = await tmp.dir()
4545
const server1 = new BlobsServer({
4646
directory: directory1.path,
47-
onRequest: ({ type }) => server1Ops.push(type),
47+
onRequest: ({ type, url }) => server1Ops.push({ type, url }),
4848
token,
4949
})
5050

@@ -113,7 +113,13 @@ test('Reads and writes from the file system', async () => {
113113
expect(list3.directories).toEqual([])
114114
}
115115

116-
expect(server1Ops).toEqual([
116+
const urls = server1Ops.map(({ url }) => url)
117+
118+
expect(urls.every((url) => url.startsWith(`/${siteID}/`))).toBeTruthy()
119+
120+
const operations = server1Ops.map(({ type }) => type)
121+
122+
expect(operations).toEqual([
117123
'list',
118124
'set',
119125
'get',

src/server.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -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.
3032
const 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

Comments
 (0)