Skip to content

Commit

Permalink
fix: set Blobs region in serve command (#6597)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed May 14, 2024
1 parent 33d2c2c commit 3bdf075
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/lib/blobs/blobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getPathInProject } from '../settings.js'
interface BaseBlobsContext {
deployID: string
siteID: string
primaryRegion?: string
token: string
}

Expand All @@ -27,6 +28,7 @@ export type BlobsContext = BlobsContextWithAPIAccess | BlobsContextWithEdgeAcces
let hasPrintedLocalBlobsNotice = false

export const BLOBS_CONTEXT_VARIABLE = 'NETLIFY_BLOBS_CONTEXT'
const DEV_REGION = 'dev'

const printLocalBlobsNotice = () => {
if (hasPrintedLocalBlobsNotice) {
Expand Down Expand Up @@ -76,6 +78,7 @@ export const getBlobsContextWithAPIAccess = async ({ debug, projectRoot, siteID
const context: BlobsContextWithAPIAccess = {
apiURL: url,
deployID: '0',
primaryRegion: DEV_REGION,
siteID,
token,
}
Expand All @@ -95,11 +98,23 @@ export const getBlobsContextWithEdgeAccess = async ({ debug, projectRoot, siteID
siteID,
token,
uncachedEdgeURL: url,
primaryRegion: DEV_REGION,
}

return context
}

/**
* Returns the Blobs metadata that should be added to the Lambda event when
* invoking a serverless function.
*/
export const getBlobsEventProperty = (context: BlobsContextWithEdgeAccess) => ({
primary_region: context.primaryRegion,
url: context.edgeURL,
url_uncached: context.edgeURL,
token: context.token,
})

/**
* Returns a Base-64, JSON-encoded representation of the Blobs context. This is
* the format that the `@netlify/blobs` package expects to find the context in.
Expand Down
8 changes: 2 additions & 6 deletions src/lib/functions/netlify-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import semver from 'semver'

import { error as errorExit } from '../../utils/command-helpers.js'
import { BACKGROUND } from '../../utils/functions/get-functions.js'
import type { BlobsContextWithEdgeAccess } from '../blobs/blobs.js'
import { BlobsContextWithEdgeAccess, getBlobsEventProperty } from '../blobs/blobs.js'

const TYPESCRIPT_EXTENSIONS = new Set(['.cts', '.mts', '.ts'])
const V2_MIN_NODE_VERSION = '18.14.0'
Expand Down Expand Up @@ -237,11 +237,7 @@ export default class NetlifyFunction {
const environment = {}

if (this.blobsContext) {
const payload = JSON.stringify({
url: this.blobsContext.edgeURL,
url_uncached: this.blobsContext.edgeURL,
token: this.blobsContext.token,
})
const payload = JSON.stringify(getBlobsEventProperty(this.blobsContext))

// @ts-expect-error TS(2339) FIXME: Property 'blobs' does not exist on type '{}'.
event.blobs = Buffer.from(payload).toString('base64')
Expand Down

2 comments on commit 3bdf075

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

  • Dependency count: 1,343
  • Package size: 311 MB
  • Number of ts-expect-error directives: 993

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

  • Dependency count: 1,343
  • Package size: 311 MB
  • Number of ts-expect-error directives: 993

Please sign in to comment.