Skip to content

Commit

Permalink
fix: allow for in-source-config internal edge functions in proxy (#5311)
Browse files Browse the repository at this point in the history
  • Loading branch information
khendrikse authored Dec 14, 2022
1 parent 6214768 commit acdcbb6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
9 changes: 8 additions & 1 deletion src/lib/edge-functions/internal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ export const getInternalFunctions = async () => {
const stats = await stat(path)

if (!stats.isDirectory()) {
throw new Error('Path is not a directory')
throw new Error('Internal edge functions directory expected')
}
} catch {
return {
functions: [],
path: null,
}
}

try {
const manifestPath = join(path, 'manifest.json')
const manifest = JSON.parse(await readFile(manifestPath))

Expand Down
2 changes: 1 addition & 1 deletion src/lib/edge-functions/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const initializeProxy = async ({
port: isolatePort,
projectDir,
})
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctions.length !== 0
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctionsPath

return async (req) => {
if (req.headers[headers.Passthrough] !== undefined || !hasEdgeFunctions) {
Expand Down
77 changes: 52 additions & 25 deletions tests/integration/100.command.dev.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -751,12 +751,6 @@ test('should respect in-source configuration from edge functions', async (t) =>
handler: () => new Response('Hello world'),
name: 'hello',
})
.withEdgeFunction({
config: () => ({ path: '/internal-1' }),
handler: () => new Response('Hello from an internal function'),
internal: true,
name: 'internal',
})

await builder.buildAsync()

Expand All @@ -766,11 +760,6 @@ test('should respect in-source configuration from edge functions', async (t) =>
t.is(res1.statusCode, 200)
t.is(res1.body, 'Hello world')

const res2 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })

t.is(res2.statusCode, 200)
t.is(res2.body, 'Hello from an internal function')

// wait for file watcher to be up and running, which might take a little
// if we do not wait, the next file change will not be picked up
await pause(500)
Expand All @@ -781,6 +770,53 @@ test('should respect in-source configuration from edge functions', async (t) =>
handler: () => new Response('Hello world'),
name: 'hello',
})
.buildAsync()

await waitForLogMatching('Reloaded edge function')

const res2 = await got(`http://localhost:${port}/hello-1`, { throwHttpErrors: false })

t.is(res2.statusCode, 404)

const res3 = await got(`http://localhost:${port}/hello-2`, { throwHttpErrors: false })

t.is(res3.statusCode, 200)
t.is(res3.body, 'Hello world')
})
})
})

test('should respect in-source configuration from internal edge functions', async (t) => {
await withSiteBuilder('site-with-internal-edge-functions', async (builder) => {
const publicDir = 'public'
await builder
.withNetlifyToml({
config: {
build: {
publish: publicDir,
},
},
})
.withEdgeFunction({
config: () => ({ path: '/internal-1' }),
handler: () => new Response('Hello from an internal function'),
internal: true,
name: 'internal',
})

await builder.buildAsync()

await withDevServer({ cwd: builder.directory }, async ({ port, waitForLogMatching }) => {
const res1 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })

t.is(res1.statusCode, 200)
t.is(res1.body, 'Hello from an internal function')

// wait for file watcher to be up and running, which might take a little
// if we do not wait, the next file change will not be picked up
await pause(500)

await builder
.withEdgeFunction({
config: () => ({ path: '/internal-2' }),
handler: () => new Response('Hello from an internal function'),
Expand All @@ -791,23 +827,14 @@ test('should respect in-source configuration from edge functions', async (t) =>

await waitForLogMatching('Reloaded edge function')

const res3 = await got(`http://localhost:${port}/hello-1`, { throwHttpErrors: false })

t.is(res3.statusCode, 404)

const res4 = await got(`http://localhost:${port}/hello-2`, { throwHttpErrors: false })

t.is(res4.statusCode, 200)
t.is(res4.body, 'Hello world')

const res5 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })
const res2 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })

t.is(res5.statusCode, 404)
t.is(res2.statusCode, 404)

const res6 = await got(`http://localhost:${port}/internal-2`, { throwHttpErrors: false })
const res3 = await got(`http://localhost:${port}/internal-2`, { throwHttpErrors: false })

t.is(res6.statusCode, 200)
t.is(res6.body, 'Hello from an internal function')
t.is(res3.statusCode, 200)
t.is(res3.body, 'Hello from an internal function')
})
})
})
Expand Down

1 comment on commit acdcbb6

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

Please sign in to comment.