Skip to content

Commit 25ff62c

Browse files
feat: support HEAD requests in local server (#109)
1 parent bea8874 commit 25ff62c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/server.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ test('Reads and writes from the file system', async () => {
6464
const entry = await blobs.getWithMetadata('simple-key')
6565
expect(entry?.metadata).toEqual(metadata)
6666

67+
const entryMetadata = await blobs.getMetadata('simple-key')
68+
expect(entryMetadata?.metadata).toEqual(metadata)
69+
6770
await blobs.delete('simple-key')
6871
expect(await blobs.get('simple-key')).toBe(null)
6972

src/server.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,35 @@ export class BlobsServer {
134134
stream.pipe(res)
135135
}
136136

137+
async head(req: http.IncomingMessage, res: http.ServerResponse) {
138+
const url = new URL(req.url ?? '', this.address)
139+
const { dataPath, key, metadataPath } = this.getLocalPaths(url)
140+
141+
if (!dataPath || !metadataPath || !key) {
142+
return this.sendResponse(req, res, 400)
143+
}
144+
145+
const headers: Record<string, string> = {}
146+
147+
try {
148+
const rawData = await fs.readFile(metadataPath, 'utf8')
149+
const metadata = JSON.parse(rawData)
150+
const encodedMetadata = encodeMetadata(metadata)
151+
152+
if (encodedMetadata) {
153+
headers[METADATA_HEADER_INTERNAL] = encodedMetadata
154+
}
155+
} catch (error) {
156+
this.logDebug('Could not read metadata file:', error)
157+
}
158+
159+
for (const name in headers) {
160+
res.setHeader(name, headers[name])
161+
}
162+
163+
res.end()
164+
}
165+
137166
async list(options: {
138167
dataPath: string
139168
metadataPath: string
@@ -257,6 +286,10 @@ export class BlobsServer {
257286
case 'PUT':
258287
return this.put(req, res)
259288

289+
case 'HEAD': {
290+
return this.head(req, res)
291+
}
292+
260293
default:
261294
return this.sendResponse(req, res, 405)
262295
}

0 commit comments

Comments
 (0)