Skip to content

Commit

Permalink
feat: Support rawUrl and rawQuery in local lambda executions (#3310)
Browse files Browse the repository at this point in the history
* feat: Support rawUrl and rawQuery in local lambda executions

* refactor: use URLSearchParams

* chore: add test

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 13, 2021
1 parent a3b787a commit a4701e5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib/functions/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ const createHandler = function ({ functionsRegistry }) {
(prev, [key, value]) => ({ ...prev, [key]: Array.isArray(value) ? value : [value] }),
{},
)

const host = request.get('host') || 'localhost'
const rawUrl = `${request.protocol}://${host}${request.originalUrl}`
const rawQuery = new URLSearchParams(request.query).toString()
const event = {
path: requestPath,
httpMethod: request.method,
Expand All @@ -86,6 +88,8 @@ const createHandler = function ({ functionsRegistry }) {
multiValueHeaders: headers,
body,
isBase64Encoded,
rawUrl,
rawQuery,
}

const clientContext = buildClientContext(request.headers) || {}
Expand Down
33 changes: 33 additions & 0 deletions tests/serving-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,4 +734,37 @@ test('Uses sourcemaps to show correct paths and locations in stack trace', async
})
})
})

test('Populates the `event` argument', async (t) => {
await withSiteBuilder('function-event', async (builder) => {
await builder
.withFunction({
path: 'hello.js',
handler: async (event) => ({
statusCode: 200,
body: JSON.stringify(event),
}),
})
.withNetlifyToml({
config: {
build: { publish: 'public' },
functions: { directory: 'functions' },
},
})
.buildAsync()

await withDevServer({ cwd: builder.directory }, async ({ port, outputBuffer }) => {
await tryAndLogOutput(async () => {
const { httpMethod, path, rawQuery, rawUrl } = await got(
`http://localhost:${port}/.netlify/functions/hello?net=lify&jam=stack`,
).json()

t.is(httpMethod, 'GET')
t.is(path, '/.netlify/functions/hello')
t.is(rawQuery, 'net=lify&jam=stack')
t.is(rawUrl, `http://localhost:${port}/.netlify/functions/hello?net=lify&jam=stack`)
}, outputBuffer)
})
})
})
/* eslint-enable require-await */

1 comment on commit a4701e5

@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: 329 MB

Please sign in to comment.