From 42cc5b740c42f429790de099f52ce9b85e094f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 3 Apr 2024 09:52:30 +0100 Subject: [PATCH 1/3] feat: handle `region:` prefix in local server --- src/server.test.ts | 31 +++++++++++++++++++++++++++++++ src/server.ts | 9 ++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/server.test.ts b/src/server.test.ts index cb4ad1a..32c6373 100644 --- a/src/server.test.ts +++ b/src/server.test.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'node:buffer' import { promises as fs } from 'node:fs' import { env, version as nodeVersion } from 'node:process' @@ -429,3 +430,33 @@ test('Returns a signed URL or the blob directly based on the request parameters' await server.stop() await fs.rm(directory.path, { force: true, recursive: true }) }) + +test('Accepts stores with `experimentalRegion: "context"`', async () => { + const deployID = '655f77a1b48f470008e5879a' + const directory = await tmp.dir() + const server = new BlobsServer({ + directory: directory.path, + token, + }) + const { port } = await server.start() + + const context = { + deployID, + edgeURL: `http://localhost:${port}`, + primaryRegion: 'us-east-1', + siteID, + token, + } + + env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64') + + const store = getDeployStore({ experimentalRegion: 'context' }) + const key = 'my-key' + + await store.set(key, 'hello from a deploy store') + + expect(await store.get(key)).toBe('hello from a deploy store') + + await server.stop() + await fs.rm(directory.path, { force: true, recursive: true }) +}) diff --git a/src/server.ts b/src/server.ts index 87e9ec6..bc87fcf 100644 --- a/src/server.ts +++ b/src/server.ts @@ -16,6 +16,7 @@ import { isNodeError, Logger } from './util.ts' const API_URL_PATH = /\/api\/v1\/blobs\/(?[^/]+)\/(?[^/]+)\/?(?[^?]*)/ const LEGACY_API_URL_PATH = /\/api\/v1\/sites\/(?[^/]+)\/blobs\/?(?[^?]*)/ const LEGACY_DEFAULT_STORE = 'production' +const REGION_PREFIX = 'region:' export enum Operation { DELETE = 'delete', @@ -335,7 +336,13 @@ export class BlobsServer { return {} } - const [, siteID, rawStoreName, ...key] = url.pathname.split('/') + let parts = url.pathname.split('/').slice(1) + + if (parts[0].startsWith(REGION_PREFIX)) { + parts = parts.slice(1) + } + + const [siteID, rawStoreName, ...key] = parts if (!siteID) { return {} From 55e8bff4440ab5c7bbc7222c5b09098b2d425382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 3 Apr 2024 09:54:37 +0100 Subject: [PATCH 2/3] chore: update test --- src/server.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/server.test.ts b/src/server.test.ts index 32c6373..89b6fe4 100644 --- a/src/server.test.ts +++ b/src/server.test.ts @@ -452,10 +452,11 @@ test('Accepts stores with `experimentalRegion: "context"`', async () => { const store = getDeployStore({ experimentalRegion: 'context' }) const key = 'my-key' + const value = 'hello from a deploy store' - await store.set(key, 'hello from a deploy store') + await store.set(key, value) - expect(await store.get(key)).toBe('hello from a deploy store') + expect(await store.get(key)).toBe(value) await server.stop() await fs.rm(directory.path, { force: true, recursive: true }) From bafca5e1ae80804202ec5393e62233350de934b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 3 Apr 2024 10:28:57 +0100 Subject: [PATCH 3/3] chore: update test name Co-authored-by: Simon Knott --- src/server.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.test.ts b/src/server.test.ts index 89b6fe4..f91d44e 100644 --- a/src/server.test.ts +++ b/src/server.test.ts @@ -431,7 +431,7 @@ test('Returns a signed URL or the blob directly based on the request parameters' await fs.rm(directory.path, { force: true, recursive: true }) }) -test('Accepts stores with `experimentalRegion: "context"`', async () => { +test('Accepts stores with `experimentalRegion`', async () => { const deployID = '655f77a1b48f470008e5879a' const directory = await tmp.dir() const server = new BlobsServer({