Skip to content

Commit fe7d899

Browse files
feat: handle region: prefix in local server (#164)
Co-authored-by: Simon Knott <[email protected]>
1 parent bc59a81 commit fe7d899

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/server.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Buffer } from 'node:buffer'
12
import { promises as fs } from 'node:fs'
23
import { env, version as nodeVersion } from 'node:process'
34

@@ -429,3 +430,34 @@ test('Returns a signed URL or the blob directly based on the request parameters'
429430
await server.stop()
430431
await fs.rm(directory.path, { force: true, recursive: true })
431432
})
433+
434+
test('Accepts stores with `experimentalRegion`', async () => {
435+
const deployID = '655f77a1b48f470008e5879a'
436+
const directory = await tmp.dir()
437+
const server = new BlobsServer({
438+
directory: directory.path,
439+
token,
440+
})
441+
const { port } = await server.start()
442+
443+
const context = {
444+
deployID,
445+
edgeURL: `http://localhost:${port}`,
446+
primaryRegion: 'us-east-1',
447+
siteID,
448+
token,
449+
}
450+
451+
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
452+
453+
const store = getDeployStore({ experimentalRegion: 'context' })
454+
const key = 'my-key'
455+
const value = 'hello from a deploy store'
456+
457+
await store.set(key, value)
458+
459+
expect(await store.get(key)).toBe(value)
460+
461+
await server.stop()
462+
await fs.rm(directory.path, { force: true, recursive: true })
463+
})

src/server.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { isNodeError, Logger } from './util.ts'
1616
const API_URL_PATH = /\/api\/v1\/blobs\/(?<site_id>[^/]+)\/(?<store_name>[^/]+)\/?(?<key>[^?]*)/
1717
const LEGACY_API_URL_PATH = /\/api\/v1\/sites\/(?<site_id>[^/]+)\/blobs\/?(?<key>[^?]*)/
1818
const LEGACY_DEFAULT_STORE = 'production'
19+
const REGION_PREFIX = 'region:'
1920

2021
export enum Operation {
2122
DELETE = 'delete',
@@ -335,7 +336,13 @@ export class BlobsServer {
335336
return {}
336337
}
337338

338-
const [, siteID, rawStoreName, ...key] = url.pathname.split('/')
339+
let parts = url.pathname.split('/').slice(1)
340+
341+
if (parts[0].startsWith(REGION_PREFIX)) {
342+
parts = parts.slice(1)
343+
}
344+
345+
const [siteID, rawStoreName, ...key] = parts
339346

340347
if (!siteID) {
341348
return {}

0 commit comments

Comments
 (0)