Skip to content

Commit

Permalink
feat: pass site information to edge function context in dev command (#…
Browse files Browse the repository at this point in the history
…4857)

* feat: pass site information to edge function context in dev mode

* fix: remove console.log

* test: add tests for context site info and use id instead of site_id

* change withSiteBuilder passed name for site info test

Co-authored-by: Eduardo Bouças <[email protected]>

* test: rename duplicate site names for sitebuilder

* test: replace btoa witg Buffer.from

Co-authored-by: Eduardo Bouças <[email protected]>
  • Loading branch information
khendrikse and eduardoboucas authored Jul 27, 2022
1 parent 91ba4fe commit 9ab5603
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"Joshua T. (http://localhost:8080)",
"João Antunes <[email protected]> (https://twitter.com/jgantunes)",
"Karagulamos (https://twitter.com/karagulamos)",
"Karin Hendrikse (khendrikse.netlify.app)",
"Karin Hendrikse <khendrikse.netlify.app>",
"Kaspar Emanuel <[email protected]> (https://twitter.com/kaspar_e)",
"Keiko Oda (https://keikooda.net/)",
"Krasimir Nedelchev (https://github.com/kaykayehnn)",
Expand Down
5 changes: 4 additions & 1 deletion src/commands/dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ const FRAMEWORK_PORT_TIMEOUT = 6e5
* @param {*} params.settings
* @param {boolean} params.offline
* @param {*} params.site
* @param {*} params.siteInfo
* @param {import('../../utils/state-config').StateConfig} params.state
* @returns
*/
Expand All @@ -253,6 +254,7 @@ const startProxyServer = async ({
offline,
settings,
site,
siteInfo,
state,
}) => {
const url = await startProxy({
Expand All @@ -267,8 +269,8 @@ const startProxyServer = async ({
projectDir: site.root,
settings,
state,
siteInfo,
})

if (!url) {
log(NETLIFYDEVERR, `Unable to start proxy server on port '${settings.port}'`)
exit(1)
Expand Down Expand Up @@ -484,6 +486,7 @@ const dev = async (options, command) => {
offline: options.offline,
settings,
site,
siteInfo,
state,
})

Expand Down
1 change: 1 addition & 0 deletions src/lib/edge-functions/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = {
Passthrough: 'x-deno-pass',
RequestID: 'X-NF-Request-ID',
IP: 'x-nf-client-connection-ip',
Site: 'X-NF-Site-Info',
}
7 changes: 6 additions & 1 deletion src/lib/edge-functions/proxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
const { Buffer } = require('buffer')
const { relative } = require('path')
const { cwd, env } = require('process')

Expand Down Expand Up @@ -55,6 +56,7 @@ const initializeProxy = async ({
offline,
projectDir,
settings,
siteInfo,
state,
}) => {
const { functions: internalFunctions, importMap, path: internalFunctionsPath } = await getInternalFunctions()
Expand Down Expand Up @@ -91,8 +93,11 @@ const initializeProxy = async ({

if (!registry) return

// Setting header with geolocation.
// Setting header with geolocation and site info.
req.headers[headers.Geo] = JSON.stringify(geoLocation)
req.headers[headers.Site] = Buffer.from(
JSON.stringify({ id: siteInfo.id, name: siteInfo.name, url: siteInfo.url }),
).toString('base64')

await registry.initialize()

Expand Down
2 changes: 2 additions & 0 deletions src/utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ const startProxy = async function ({
offline,
projectDir,
settings,
siteInfo,
state,
}) {
const functionsServer = settings.functionsPort ? `http://127.0.0.1:${settings.functionsPort}` : null
Expand All @@ -485,6 +486,7 @@ const startProxy = async function ({
offline,
projectDir,
settings,
siteInfo,
state,
})
const proxy = await initializeProxy({
Expand Down
70 changes: 67 additions & 3 deletions tests/integration/100.command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { Response } = require('node-fetch')

const { withDevServer } = require('./utils/dev-server')
const got = require('./utils/got')
const { withMockApi } = require('./utils/mock-api')
const { pause } = require('./utils/pause')
const { withSiteBuilder } = require('./utils/site-builder')

Expand Down Expand Up @@ -173,7 +174,7 @@ test('should enforce role based redirects with custom secret and role path', asy
})

test('Serves an Edge Function that terminates a response', async (t) => {
await withSiteBuilder('site-with-fully-qualified-redirect-rule', async (builder) => {
await withSiteBuilder('site-with-edge-function-that-terminates-response', async (builder) => {
const publicDir = 'public'
builder
.withNetlifyToml({
Expand Down Expand Up @@ -213,7 +214,7 @@ test('Serves an Edge Function that terminates a response', async (t) => {
})

test('Serves an Edge Function with a rewrite', async (t) => {
await withSiteBuilder('site-with-fully-qualified-redirect-rule', async (builder) => {
await withSiteBuilder('site-with-edge-function-that-rewrites', async (builder) => {
const publicDir = 'public'
builder
.withNetlifyToml({
Expand Down Expand Up @@ -252,8 +253,71 @@ test('Serves an Edge Function with a rewrite', async (t) => {
})
})

test('Serves an Edge Function that includes context with site information', async (t) => {
await withSiteBuilder('site-with-edge-function-printing-site-info', async (builder) => {
const publicDir = 'public'
builder
.withNetlifyToml({
config: {
build: {
publish: publicDir,
edge_functions: 'netlify/edge-functions',
},
edge_functions: [
{
function: 'siteContext',
path: '/*',
},
],
},
})
.withEdgeFunction({
handler: async (_, context) => new Response(JSON.stringify(context.site)),
name: 'siteContext',
})

await builder.buildAsync()

const siteInfo = {
account_slug: 'test-account',
id: 'site_id',
name: 'site-name',
url: 'site-url',
}

const routes = [
{ path: 'sites/site_id', response: siteInfo },
{ path: 'sites/site_id/service-instances', response: [] },
{
path: 'accounts',
response: [{ slug: siteInfo.account_slug }],
},
]

await withMockApi(routes, async ({ apiUrl }) => {
await withDevServer(
{
cwd: builder.directory,
offline: false,
env: {
NETLIFY_API_URL: apiUrl,
NETLIFY_SITE_ID: 'site_id',
NETLIFY_AUTH_TOKEN: 'fake-token',
},
},
async (server) => {
const response = await got(`${server.url}`)

t.is(response.statusCode, 200)
t.is(response.body, '{"id":"site_id","name":"site-name","url":"site-url"}')
},
)
})
})
})

test('Serves an Edge Function that transforms the response', async (t) => {
await withSiteBuilder('site-with-fully-qualified-redirect-rule', async (builder) => {
await withSiteBuilder('site-with-edge-function-that-transforms-response', async (builder) => {
const publicDir = 'public'
builder
.withNetlifyToml({
Expand Down

1 comment on commit 9ab5603

@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

Package size: 221 MB

Please sign in to comment.