Skip to content

Conversation

@pieh
Copy link
Contributor

@pieh pieh commented Jul 21, 2025

Quite similar problem to #364, just fix being a little less nice having to rely on hardcoded array of bodyless status codes instead of being able to just check if body exists

},
test: {
include: ['src/**/*.test.ts'],
include: ['(src|dev)/**/*.test.ts'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

packages/functions/dev/main.test.ts was not running, with this added to config, other than added failing test (expected), it seems like some other tests in this module are failing/timing out :/ https://github.com/netlify/primitives/actions/runs/16414390123/job/46376826143?pr=373#step:11:301

settings: {},
timeouts: {},
userFunctionsPath: 'netlify/functions',
watch: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test was timing out here

await fixture.writeFile('netlify/functions/hello.mjs', `export default async () => new Response("Goodbye world")`)
await events.waitFor((event) => event.name === 'FunctionLoadedEvent' && !(event as FunctionLoadedEvent).firstLoad)
because watcher was not enabled so editing source of a function was not doing anything.

Same situation for similar addition in 'Returns a `preferStatic` property' test

@pieh pieh changed the title Test/function 304 fix: handle 304 responses from functions Jul 22, 2025
@pieh pieh marked this pull request as ready for review July 22, 2025 08:08
@pieh pieh requested a review from a team as a code owner July 22, 2025 08:08
Comment on lines 62 to 66
// For streaming responses, lambda-local always return a result with body stream.
// We need to discard this if response status code does not allow to have a body.
const shouldHaveBodyStream = !nullBodyStatus.includes(invocationResult.statusCode)

if (shouldHaveBodyStream) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note, that we have no such handling in CLI, but difference is that we convert to Response here, while CLI is using http.ServerResponse. In http.ServerResponse case it seems like body is just silently discarded, while Response will actively throw

eduardoboucas
eduardoboucas previously approved these changes Jul 22, 2025
import sourceMapSupport from 'source-map-support'

// https://github.com/nodejs/undici/blob/a36e299d544863c5ade17d4090181be894366024/lib/web/fetch/constants.js#L6
const nullBodyStatus = [101, 204, 205, 304]
Copy link
Member

Choose a reason for hiding this comment

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

nit: Maybe make this a Set for a nicer lookup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

// We need to discard this if response status code does not allow to have a body.
const shouldHaveBodyStream = !nullBodyStatus.includes(invocationResult.statusCode)

if (shouldHaveBodyStream) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: If we could invert this check and early-return, we'd avoid an extra level of nesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed a change. Had to do slightly larger change, because there were no functions in this worker module to begin with so couldn't really early-return without it, but I think it's slightly nicer now (tho I might have invited opportunity for more NITs doing that :) )

@eduardoboucas eduardoboucas merged commit 1094683 into main Jul 22, 2025
17 checks passed
@eduardoboucas eduardoboucas deleted the test/function-304 branch July 22, 2025 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants