Skip to content

Commit a4a4026

Browse files
authoredJul 5, 2024··
feat: expose deploy context to functions and edge functions (#6747)
1 parent f218e20 commit a4a4026

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed
 

‎package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"@netlify/build-info": "7.14.0",
7878
"@netlify/config": "20.16.0",
7979
"@netlify/edge-bundler": "12.2.0",
80-
"@netlify/edge-functions": "2.8.1",
80+
"@netlify/edge-functions": "2.9.0",
8181
"@netlify/local-functions-proxy": "1.1.1",
8282
"@netlify/zip-it-and-ship-it": "9.37.5",
8383
"@octokit/rest": "20.1.1",

‎src/lib/edge-functions/headers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Buffer } from 'buffer'
33
export const headers = {
44
BlobsInfo: 'x-nf-blobs-info',
55
DeployID: 'x-nf-deploy-id',
6+
DeployContext: 'x-nf-deploy-context',
67
FeatureFlags: 'x-nf-feature-flags',
78
ForwardedHost: 'x-forwarded-host',
89
ForwardedProtocol: 'x-forwarded-proto',

‎src/lib/edge-functions/proxy.ts

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export const initializeProxy = async ({
151151
// Setting header with geolocation and site info.
152152
req.headers[headers.Geo] = Buffer.from(JSON.stringify(geoLocation)).toString('base64')
153153
req.headers[headers.DeployID] = '0'
154+
req.headers[headers.DeployContext] = 'dev'
154155
req.headers[headers.Site] = createSiteInfoHeader(siteInfo, `${protocol}://localhost:${mainPort}`)
155156
req.headers[headers.Account] = createAccountInfoHeader({ id: accountId })
156157

‎tests/integration/commands/dev/dev-miscellaneous.test.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,29 @@ describe.concurrent('commands/dev-miscellaneous', () => {
238238
},
239239
])
240240
.withEdgeFunction({
241-
handler: (req) => new Response(req.headers.get('x-nf-request-id')),
241+
handler: (req, context) =>
242+
Response.json({
243+
requestID: req.headers.get('x-nf-request-id'),
244+
deploy: context.deploy,
245+
}),
242246
name: 'hello',
243247
})
244248

245249
await builder.build()
246250

247251
await withDevServer({ cwd: builder.directory }, async (server) => {
248252
const response = await fetch(`${server.url}/edge-function`)
249-
const responseBody = await response.text()
253+
const responseBody = await response.json()
250254

251255
t.expect(response.status).toBe(200)
252-
t.expect(responseBody.length).toBe(26)
253-
t.expect(responseBody).toEqual(response.headers.get('x-nf-request-id'))
256+
t.expect(responseBody).toEqual({
257+
requestID: response.headers.get('x-nf-request-id'),
258+
deploy: {
259+
context: 'dev',
260+
id: '0',
261+
published: false,
262+
},
263+
})
254264
})
255265
})
256266
})
@@ -423,7 +433,7 @@ describe.concurrent('commands/dev-miscellaneous', () => {
423433

424434
t.expect(response.status).toBe(200)
425435
t.expect(JSON.parse(await response.text())).toStrictEqual({
426-
deploy: { id: '0' },
436+
deploy: { context: 'dev', id: '0', published: false },
427437
site: { id: 'site_id', name: 'site-name', url: server.url },
428438
})
429439
},

‎tests/integration/commands/dev/edge-functions.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe.skipIf(isWindows)('edge functions', () => {
7777
const { deploy, geo, ip, params, requestId, server, site } = await response.json()
7878
expect(geo.city).toEqual('Mock City')
7979
expect(geo.country.code).toEqual('DE')
80-
expect(deploy).toEqual({ id: '0' })
80+
expect(deploy).toEqual({ context: 'dev', id: '0', published: false })
8181
expectTypeOf(ip).toBeString()
8282
expect(params).toEqual({})
8383
expectTypeOf(requestId).toBeString()

‎tests/integration/commands/dev/v2-api.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ describe.runIf(gte(version, '18.13.0')).concurrent('v2 api', () => {
6868
expect(context.server.region).toEqual('dev')
6969
expect(context.ip).toEqual('::1')
7070
expect(context.geo.city).toEqual('Mock City')
71-
7271
expect(context.cookies).toEqual({ foo: 'bar' })
73-
7472
expect(context.account.id).toEqual('mock-account-id')
73+
expect(context.deploy.context).toEqual('dev')
74+
expect(context.deploy.id).toEqual('0')
75+
expect(context.deploy.published).toEqual(false)
7576
})
7677

7778
test<FixtureTestContext>('logging works', async ({ devServer }) => {

2 commit comments

Comments
 (2)

github-actions[bot] commented on Jul 5, 2024

@github-actions[bot]

📊 Benchmark results

  • Dependency count: 1,213
  • Package size: 313 MB
  • Number of ts-expect-error directives: 976

github-actions[bot] commented on Jul 5, 2024

@github-actions[bot]

📊 Benchmark results

  • Dependency count: 1,213
  • Package size: 313 MB
  • Number of ts-expect-error directives: 976
Please sign in to comment.