Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit 7b03070

Browse files
authoredMay 7, 2024··
fix: add name and generator support to ISC (#1732)
* feat: add support to ISC parser * feat: use parsed values * chore: remove unneeded import * chore: prettier * fix: flip precedence and extract to own test * fix: fmt * fix: switch back to logical or
1 parent 01f1c33 commit 7b03070

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed
 

‎src/runtimes/node/in_source_config/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export type ISCValues = {
2323
schedule?: string
2424
methods?: string[]
2525
trafficRules?: TrafficRules
26+
name?: string
27+
generator?: string
2628
}
2729

2830
export interface StaticAnalysisResult extends ISCValues {
@@ -177,6 +179,14 @@ export const parseSource = (source: string, { functionName }: FindISCDeclaration
177179
result.schedule = configExport.schedule
178180
}
179181

182+
if (typeof configExport.name === 'string') {
183+
result.name = configExport.name
184+
}
185+
186+
if (typeof configExport.generator === 'string') {
187+
result.generator = configExport.generator
188+
}
189+
180190
if (configExport.method !== undefined) {
181191
result.methods = normalizeMethods(configExport.method, functionName)
182192
}

‎src/runtimes/node/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const zipFunction: ZipFunction = async function ({
141141
invocationMode = INVOCATION_MODE.Background
142142
}
143143

144-
const { trafficRules } = staticAnalysisResult
144+
const { trafficRules, generator: staticAnalysisGenerator, name: staticAnalysisName } = staticAnalysisResult
145145

146146
const outputModuleFormat =
147147
extname(finalMainFile) === MODULE_FILE_EXTENSION.MJS ? MODULE_FORMAT.ESM : MODULE_FORMAT.COMMONJS
@@ -151,9 +151,9 @@ const zipFunction: ZipFunction = async function ({
151151
bundler: bundlerName,
152152
bundlerWarnings,
153153
config,
154-
displayName: config?.name,
154+
displayName: staticAnalysisName || config?.name,
155155
entryFilename: zipPath.entryFilename,
156-
generator: config?.generator || getInternalValue(isInternal),
156+
generator: staticAnalysisGenerator || config?.generator || getInternalValue(isInternal),
157157
inputs,
158158
includedFiles,
159159
staticAnalysisResult,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"config": {
3+
"name": "should not be respected, ISC takes precedence",
4+
"generator": "should not be respected, ISC takes precedence"
5+
},
6+
"version": 1
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default () => new Response('hello world')
2+
3+
export const config = {
4+
name: 'SSR Function',
5+
generator: 'next-runtime@1.2.3',
6+
}

‎tests/unit/runtimes/node/in_source_config.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -703,4 +703,19 @@ describe('V2 API', () => {
703703
expect(routes).toEqual([{ pattern: '/products', literal: '/products', methods: [], prefer_static: true }])
704704
})
705705
})
706+
707+
test('Understands name and generator', () => {
708+
const source = `
709+
export default async () => new Response("Hello!")
710+
export const config = { name: "foo", generator: "bar@1.2.3" }`
711+
712+
const isc = parseSource(source, options)
713+
expect(isc).toEqual({
714+
inputModuleFormat: 'esm',
715+
routes: [],
716+
runtimeAPIVersion: 2,
717+
name: 'foo',
718+
generator: 'bar@1.2.3',
719+
})
720+
})
706721
})

‎tests/v2api.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,26 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
528528
expect(body).toBe('foo!bar')
529529
})
530530
})
531+
532+
test('Name and Generator are taken from ISC and take precedence over deploy config', async () => {
533+
const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
534+
const manifestPath = join(tmpDir, 'manifest.json')
535+
536+
const fixtureName = 'v2-api-name-generator'
537+
const pathInternal = join(fixtureName, '.netlify', 'functions-internal')
538+
539+
const {
540+
files: [func],
541+
} = await zipFixture(pathInternal, {
542+
fixtureDir: FIXTURES_ESM_DIR,
543+
length: 1,
544+
opts: {
545+
manifest: manifestPath,
546+
configFileDirectories: [join(FIXTURES_ESM_DIR, fixtureName)],
547+
},
548+
})
549+
550+
expect(func.displayName).toBe('SSR Function')
551+
expect(func.generator).toBe('next-runtime@1.2.3')
552+
})
531553
})

1 commit comments

Comments
 (1)

github-actions[bot] commented on May 7, 2024

@github-actions[bot]
Contributor

⏱ Benchmark results

  • largeDepsEsbuild: 1.2s
  • largeDepsNft: 5s
  • largeDepsZisi: 9.2s
This repository has been archived.