Skip to content

Commit

Permalink
fix: internal vs external metadata headers (#92)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo Bouças <[email protected]>
  • Loading branch information
cbosss and eduardoboucas authored Nov 7, 2023
1 parent 03a1c05 commit de41f9e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
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

0 comments on commit de41f9e

Please sign in to comment.