Skip to content

Commit

Permalink
feat: expose deploy context to functions and edge functions (#6747)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Jul 5, 2024
1 parent f218e20 commit a4a4026
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@netlify/build-info": "7.14.0",
"@netlify/config": "20.16.0",
"@netlify/edge-bundler": "12.2.0",
"@netlify/edge-functions": "2.8.1",
"@netlify/edge-functions": "2.9.0",
"@netlify/local-functions-proxy": "1.1.1",
"@netlify/zip-it-and-ship-it": "9.37.5",
"@octokit/rest": "20.1.1",
Expand Down
1 change: 1 addition & 0 deletions src/lib/edge-functions/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Buffer } from 'buffer'
export const headers = {
BlobsInfo: 'x-nf-blobs-info',
DeployID: 'x-nf-deploy-id',
DeployContext: 'x-nf-deploy-context',
FeatureFlags: 'x-nf-feature-flags',
ForwardedHost: 'x-forwarded-host',
ForwardedProtocol: 'x-forwarded-proto',
Expand Down
1 change: 1 addition & 0 deletions src/lib/edge-functions/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export const initializeProxy = async ({
// Setting header with geolocation and site info.
req.headers[headers.Geo] = Buffer.from(JSON.stringify(geoLocation)).toString('base64')
req.headers[headers.DeployID] = '0'
req.headers[headers.DeployContext] = 'dev'
req.headers[headers.Site] = createSiteInfoHeader(siteInfo, `${protocol}://localhost:${mainPort}`)
req.headers[headers.Account] = createAccountInfoHeader({ id: accountId })

Expand Down
20 changes: 15 additions & 5 deletions tests/integration/commands/dev/dev-miscellaneous.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,29 @@ describe.concurrent('commands/dev-miscellaneous', () => {
},
])
.withEdgeFunction({
handler: (req) => new Response(req.headers.get('x-nf-request-id')),
handler: (req, context) =>
Response.json({
requestID: req.headers.get('x-nf-request-id'),
deploy: context.deploy,
}),
name: 'hello',
})

await builder.build()

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

t.expect(response.status).toBe(200)
t.expect(responseBody.length).toBe(26)
t.expect(responseBody).toEqual(response.headers.get('x-nf-request-id'))
t.expect(responseBody).toEqual({
requestID: response.headers.get('x-nf-request-id'),
deploy: {
context: 'dev',
id: '0',
published: false,
},
})
})
})
})
Expand Down Expand Up @@ -423,7 +433,7 @@ describe.concurrent('commands/dev-miscellaneous', () => {

t.expect(response.status).toBe(200)
t.expect(JSON.parse(await response.text())).toStrictEqual({
deploy: { id: '0' },
deploy: { context: 'dev', id: '0', published: false },
site: { id: 'site_id', name: 'site-name', url: server.url },
})
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/commands/dev/edge-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe.skipIf(isWindows)('edge functions', () => {
const { deploy, geo, ip, params, requestId, server, site } = await response.json()
expect(geo.city).toEqual('Mock City')
expect(geo.country.code).toEqual('DE')
expect(deploy).toEqual({ id: '0' })
expect(deploy).toEqual({ context: 'dev', id: '0', published: false })
expectTypeOf(ip).toBeString()
expect(params).toEqual({})
expectTypeOf(requestId).toBeString()
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/commands/dev/v2-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ describe.runIf(gte(version, '18.13.0')).concurrent('v2 api', () => {
expect(context.server.region).toEqual('dev')
expect(context.ip).toEqual('::1')
expect(context.geo.city).toEqual('Mock City')

expect(context.cookies).toEqual({ foo: 'bar' })

expect(context.account.id).toEqual('mock-account-id')
expect(context.deploy.context).toEqual('dev')
expect(context.deploy.id).toEqual('0')
expect(context.deploy.published).toEqual(false)
})

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

2 comments on commit a4a4026

@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,213
  • Package size: 313 MB
  • Number of ts-expect-error directives: 976

@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,213
  • Package size: 313 MB
  • Number of ts-expect-error directives: 976

Please sign in to comment.