Skip to content

fix: internal vs external metadata headers #92

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 5 commits into from
Nov 7, 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
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Client {
}

if (encodedMetadata) {
headers[METADATA_HEADER_EXTERNAL] = encodedMetadata
headers[METADATA_HEADER_INTERNAL] = encodedMetadata
}

const path = key ? `/${this.siteID}/${storeName}/${key}` : `/${this.siteID}/${storeName}`
Expand Down
2 changes: 1 addition & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ describe('setJSON', () => {
headers: {
authorization: `Bearer ${edgeToken}`,
'cache-control': 'max-age=0, stale-while-revalidate=60',
'netlify-blobs-metadata': encodedMetadata,
'x-amz-meta-user': encodedMetadata,
},
response: new Response(null),
url: `${edgeURL}/${siteID}/production/${key}`,
Expand Down
6 changes: 5 additions & 1 deletion src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ export const getMetadataFromResponse = (response: Response) => {
return {}
}

// If metadata is coming from the API, it will be under the external header.
// If it's coming from the edge, it will be under the internal header.
const value = response.headers.get(METADATA_HEADER_EXTERNAL) || response.headers.get(METADATA_HEADER_INTERNAL)

try {
return decodeMetadata(response.headers.get(METADATA_HEADER_INTERNAL))
return decodeMetadata(value)
} catch {
throw new Error(
'An internal error occurred while trying to retrieve the metadata for an entry. Please try updating to the latest version of the Netlify Blobs client.',
Expand Down
4 changes: 2 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { tmpdir } from 'node:os'
import { dirname, join, relative, resolve, sep } from 'node:path'

import { ListResponse } from './backend/list.ts'
import { decodeMetadata, encodeMetadata, METADATA_HEADER_EXTERNAL, METADATA_HEADER_INTERNAL } from './metadata.ts'
import { decodeMetadata, encodeMetadata, METADATA_HEADER_INTERNAL } from './metadata.ts'
import { isNodeError, Logger } from './util.ts'

interface BlobsServerOptions {
Expand Down Expand Up @@ -163,7 +163,7 @@ export class BlobsServer {
return this.sendResponse(req, res, 400)
}

const metadataHeader = req.headers[METADATA_HEADER_EXTERNAL]
const metadataHeader = req.headers[METADATA_HEADER_INTERNAL]
const metadata = decodeMetadata(Array.isArray(metadataHeader) ? metadataHeader[0] : metadataHeader ?? null)

try {
Expand Down