Skip to content

feat: support HEAD requests in local server #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
33 changes: 33 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {}

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
Expand Down Expand Up @@ -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)
}
Expand Down