diff --git a/src/server.test.ts b/src/server.test.ts index 83cd809..52e43bd 100644 --- a/src/server.test.ts +++ b/src/server.test.ts @@ -64,6 +64,9 @@ test('Reads and writes from the file system', async () => { const entry = await blobs.getWithMetadata('simple-key') expect(entry?.metadata).toEqual(metadata) + const entryMetadata = await blobs.getMetadata('simple-key') + expect(entryMetadata?.metadata).toEqual(metadata) + await blobs.delete('simple-key') expect(await blobs.get('simple-key')).toBe(null) diff --git a/src/server.ts b/src/server.ts index 095c919..69ff3cf 100644 --- a/src/server.ts +++ b/src/server.ts @@ -134,6 +134,35 @@ export class BlobsServer { stream.pipe(res) } + async head(req: http.IncomingMessage, res: http.ServerResponse) { + const url = new URL(req.url ?? '', this.address) + const { dataPath, key, metadataPath } = this.getLocalPaths(url) + + if (!dataPath || !metadataPath || !key) { + return this.sendResponse(req, res, 400) + } + + const headers: Record = {} + + try { + const rawData = await fs.readFile(metadataPath, 'utf8') + const metadata = JSON.parse(rawData) + const encodedMetadata = encodeMetadata(metadata) + + if (encodedMetadata) { + headers[METADATA_HEADER_INTERNAL] = encodedMetadata + } + } catch (error) { + this.logDebug('Could not read metadata file:', error) + } + + for (const name in headers) { + res.setHeader(name, headers[name]) + } + + res.end() + } + async list(options: { dataPath: string metadataPath: string @@ -257,6 +286,10 @@ export class BlobsServer { case 'PUT': return this.put(req, res) + case 'HEAD': { + return this.head(req, res) + } + default: return this.sendResponse(req, res, 405) }