Skip to content
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

fix: b64-encode geo location in local dev #5873

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
778 changes: 696 additions & 82 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"@netlify/framework-info": "9.8.10",
"@netlify/local-functions-proxy": "1.1.1",
"@netlify/serverless-functions-api": "1.5.1",
"@netlify/zip-it-and-ship-it": "9.13.0",
"@netlify/zip-it-and-ship-it": "9.13.1",
Copy link
Member

Choose a reason for hiding this comment

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

Did you mean to include a ZISI update as part of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, because that contains the updated @netlify/serverless-functions-api

"@octokit/rest": "19.0.13",
"@skn0tt/lambda-local": "2.0.3",
"ansi-escapes": "6.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/edge-functions/bootstrap.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { env } from 'process'

const latestBootstrapURL = 'https://6494585a67d46e0008867e60--edge.netlify.com/bootstrap/index-combined.ts'
const latestBootstrapURL = 'https://64ae60d920fd0f000865bcfc--edge.netlify.com/bootstrap/index-combined.ts'

export const getBootstrapURL = () => env.NETLIFY_EDGE_BOOTSTRAP || latestBootstrapURL
2 changes: 1 addition & 1 deletion src/lib/edge-functions/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const initializeProxy = async ({
if (!registry) return

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

Expand Down
5 changes: 3 additions & 2 deletions src/lib/functions/server.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-check
import { Buffer } from 'buffer'

import express from 'express'
import expressLogging from 'express-logging'
import jwtDecode from 'jwt-decode'
Expand Down Expand Up @@ -47,7 +49,6 @@ const hasBody = (req) =>
// eslint-disable-next-line unicorn/prefer-number-properties
(req.header('transfer-encoding') !== undefined || !isNaN(req.header('content-length'))) &&
// we expect a string or a buffer, because we use the two bodyParsers(text, raw) from express
// eslint-disable-next-line n/prefer-global/buffer
(typeof req.body === 'string' || Buffer.isBuffer(req.body))

export const createHandler = function (options) {
Expand Down Expand Up @@ -114,7 +115,7 @@ export const createHandler = function (options) {
'client-ip': [remoteAddress],
'x-nf-client-connection-ip': [remoteAddress],
'x-nf-account-id': [options.accountId],
[efHeaders.Geo]: JSON.stringify(geoLocation),
[efHeaders.Geo]: Buffer.from(JSON.stringify(geoLocation)).toString('base64'),
}).reduce((prev, [key, value]) => ({ ...prev, [key]: Array.isArray(value) ? value : [value] }), {})
const rawQuery = new URLSearchParams(requestQuery).toString()
const protocol = options.config?.dev?.https ? 'https' : 'http'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Config, Context } from 'https://edge.netlify.com'

export default (_, context: Context) => Response.json(context)

export const config: Config = {
path: '/context',
}
11 changes: 11 additions & 0 deletions tests/integration/commands/dev/edge-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ describe('edge functions', () => {
expect(response.statusCode).toBe(200)
expect(response.body).toMatchSnapshot()
})

test<FixtureTestContext>('should provide geo location', async ({ devServer }) => {
const response = await got(`http://localhost:${devServer.port}/context`, {
throwHttpErrors: false,
retry: { limit: 0 },
})

const { geo } = JSON.parse(response.body)
expect(geo.city).toEqual('Mock City')
expect(geo.country.code).toEqual('DE')
})
})

setupFixtureTests('dev-server-with-edge-functions', { devServer: true }, () => {
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/utils/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ export async function setupFixtureTests(
if (options.mockApi) mockApi = await startMockApi(options.mockApi)
fixture = await Fixture.create(fixturePath, { apiUrl: mockApi?.apiUrl })

if (options.devServer) devServer = await startDevServer({ cwd: fixture.directory, args: ['--offline'] })
if (options.devServer)
devServer = await startDevServer({ cwd: fixture.directory, args: ['--offline', '--country', 'DE'] })

await options.setup?.({ devServer, fixture, mockApi })
}, HOOK_TIMEOUT)
Expand Down