Skip to content

Commit 62930d8

Browse files
fix: always look for internal edge functions (#5302)
1 parent 869c5a8 commit 62930d8

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/lib/edge-functions/internal.cjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
const { promises: fs } = require('fs')
3-
const path = require('path')
3+
const { dirname, join, resolve } = require('path')
44
const { cwd } = require('process')
55
const { pathToFileURL } = require('url')
66

@@ -30,16 +30,16 @@ const getImportMap = async (importMapPath) => {
3030
}
3131

3232
const getInternalFunctions = async () => {
33-
const internalPath = path.join(cwd(), getPathInProject([INTERNAL_EDGE_FUNCTIONS_FOLDER]))
33+
const path = join(cwd(), getPathInProject([INTERNAL_EDGE_FUNCTIONS_FOLDER]))
3434

3535
try {
36-
const stats = await fs.stat(internalPath)
36+
const stats = await fs.stat(path)
3737

3838
if (!stats.isDirectory()) {
3939
throw new Error('Path is not a directory')
4040
}
4141

42-
const manifestPath = path.join(internalPath, 'manifest.json')
42+
const manifestPath = join(path, 'manifest.json')
4343
// eslint-disable-next-line import/no-dynamic-require, n/global-require
4444
const manifest = require(manifestPath)
4545

@@ -49,11 +49,11 @@ const getInternalFunctions = async () => {
4949

5050
const data = {
5151
functions: manifest.functions,
52-
path: internalPath,
52+
path,
5353
}
5454

5555
if (manifest.import_map) {
56-
const importMapPath = path.resolve(path.dirname(manifestPath), manifest.import_map)
56+
const importMapPath = resolve(dirname(manifestPath), manifest.import_map)
5757
const importMap = await getImportMap(importMapPath)
5858

5959
if (importMap !== null) {
@@ -71,7 +71,7 @@ const getInternalFunctions = async () => {
7171
} catch {
7272
return {
7373
functions: [],
74-
path: null,
74+
path,
7575
}
7676
}
7777
}

tests/integration/100.command.dev.test.cjs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,12 @@ test('should respect in-source configuration from edge functions', async (t) =>
751751
handler: () => new Response('Hello world'),
752752
name: 'hello',
753753
})
754+
.withEdgeFunction({
755+
config: () => ({ path: '/internal-1' }),
756+
handler: () => new Response('Hello from an internal function'),
757+
internal: true,
758+
name: 'internal',
759+
})
754760

755761
await builder.buildAsync()
756762

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

769+
const res2 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })
770+
771+
t.is(res2.statusCode, 200)
772+
t.is(res2.body, 'Hello from an internal function')
773+
763774
// wait for file watcher to be up and running, which might take a little
764775
// if we do not wait, the next file change will not be picked up
765776
await pause(500)
@@ -770,18 +781,33 @@ test('should respect in-source configuration from edge functions', async (t) =>
770781
handler: () => new Response('Hello world'),
771782
name: 'hello',
772783
})
784+
.withEdgeFunction({
785+
config: () => ({ path: '/internal-2' }),
786+
handler: () => new Response('Hello from an internal function'),
787+
internal: true,
788+
name: 'internal',
789+
})
773790
.buildAsync()
774791

775792
await waitForLogMatching('Reloaded edge function')
776793

777-
const res2 = await got(`http://localhost:${port}/hello-1`, { throwHttpErrors: false })
794+
const res3 = await got(`http://localhost:${port}/hello-1`, { throwHttpErrors: false })
795+
796+
t.is(res3.statusCode, 404)
797+
798+
const res4 = await got(`http://localhost:${port}/hello-2`, { throwHttpErrors: false })
799+
800+
t.is(res4.statusCode, 200)
801+
t.is(res4.body, 'Hello world')
802+
803+
const res5 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })
778804

779-
t.is(res2.statusCode, 404)
805+
t.is(res5.statusCode, 404)
780806

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

783-
t.is(res3.statusCode, 200)
784-
t.is(res3.body, 'Hello world')
809+
t.is(res6.statusCode, 200)
810+
t.is(res6.body, 'Hello from an internal function')
785811
})
786812
})
787813
})

0 commit comments

Comments
 (0)