Skip to content

feat: handle region: prefix in local server #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're discarding the region parameter, which means that in local dev, stores with different experimentalRegion parameters all share the same data. Is that exepcted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's fine for now. We will likely need to rethink the whole namespacing we use locally once we make region selection a customer-facing thing, but I think just ignoring regions for now is an okay first step.

}

const [siteID, rawStoreName, ...key] = parts

if (!siteID) {
return {}
Expand Down
Loading