From fe7d89953f369fbe90c911c527d118b655b30b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 3 Apr 2024 10:32:39 +0100 Subject: [PATCH] feat: handle `region:` prefix in local server (#164) Co-authored-by: Simon Knott --- src/server.test.ts | 32 ++++++++++++++++++++++++++++++++ src/server.ts | 9 ++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/server.test.ts b/src/server.test.ts index cb4ad1a..f91d44e 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,34 @@ 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`', 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' + const value = 'hello from a deploy store' + + await store.set(key, value) + + expect(await store.get(key)).toBe(value) + + 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 {}