Skip to content

Commit

Permalink
feat: handle region: prefix in local server (#164)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Knott <info@simonknott.de>
  • Loading branch information
eduardoboucas and Skn0tt committed Apr 3, 2024
1 parent bc59a81 commit fe7d899
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'node:buffer'
import { promises as fs } from 'node:fs'
import { env, version as nodeVersion } from 'node:process'

Expand Down Expand Up @@ -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 })
})
9 changes: 8 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isNodeError, Logger } from './util.ts'
const API_URL_PATH = /\/api\/v1\/blobs\/(?<site_id>[^/]+)\/(?<store_name>[^/]+)\/?(?<key>[^?]*)/
const LEGACY_API_URL_PATH = /\/api\/v1\/sites\/(?<site_id>[^/]+)\/blobs\/?(?<key>[^?]*)/
const LEGACY_DEFAULT_STORE = 'production'
const REGION_PREFIX = 'region:'

export enum Operation {
DELETE = 'delete',
Expand Down Expand Up @@ -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 {}
Expand Down

0 comments on commit fe7d899

Please sign in to comment.