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

Commit 0087f6d

Browse files
khendriksedanez
andauthoredFeb 28, 2023
feat: populate generator field if function is in the internal folder (#1357)
* feat: populate generator field if function is in the internal folder * Update README.md Co-authored-by: Daniel Tschinder <231804+danez@users.noreply.github.com> --------- Co-authored-by: Daniel Tschinder <231804+danez@users.noreply.github.com>
1 parent fe2c4a2 commit 0087f6d

File tree

8 files changed

+47
-7
lines changed

8 files changed

+47
-7
lines changed
 

‎README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ JSON-formatted string with the following properties:
191191

192192
Maximum number of functions to bundle at the same time.
193193

194+
#### `internalSrcFolder`
195+
196+
- _Type_: `string`
197+
- _Default value_: `undefined`
198+
199+
Defines the path to the folder with internal functions. Used to populate a function's `generator` property if `generator` is not configured in the function's config itself, if its path is within this specified internal functions folder.
200+
194201
### Return value
195202

196203
This returns a `Promise` resolving to an array of objects describing each archive. Every object has the following
@@ -222,7 +229,7 @@ properties.
222229

223230
- `generator`: `string`
224231

225-
If there was a user-defined configuration object applied to the function, and it had `generator` defined. This will be returned here.
232+
If there was a user-defined configuration object applied to the function, and it had `generator` defined. This will be returned here. If there was nothing defined, but an `internalSrcFolder` was passed and the function was defined in there, it will return a string to specify it was an internal function.
226233

227234
Additionally, the following properties also exist for Node.js functions:
228235

‎src/runtimes/go/index.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { copyFile } from 'cp-file'
66
import { SourceFile } from '../../function.js'
77
import type { RuntimeCache } from '../../utils/cache.js'
88
import { cachedLstat, cachedReaddir } from '../../utils/fs.js'
9+
import getInternalValue from '../../utils/get_internal_value.js'
910
import { nonNullable } from '../../utils/non_nullable.js'
1011
import { zipBinary } from '../../zip_binary.js'
1112
import { detectBinaryRuntime } from '../detect_runtime.js'
@@ -109,7 +110,16 @@ const processSource = async ({
109110
}
110111
}
111112

112-
const zipFunction: ZipFunction = async function ({ config, destFolder, filename, mainFile, srcDir, srcPath, stat }) {
113+
const zipFunction: ZipFunction = async function ({
114+
config,
115+
destFolder,
116+
filename,
117+
mainFile,
118+
srcDir,
119+
srcPath,
120+
stat,
121+
isInternal,
122+
}) {
113123
const destPath = join(destFolder, filename)
114124
const isSource = extname(mainFile) === '.go'
115125

@@ -155,7 +165,7 @@ const zipFunction: ZipFunction = async function ({ config, destFolder, filename,
155165
config,
156166
path: destPath,
157167
displayName: config?.name,
158-
generator: config?.generator,
168+
generator: config?.generator || getInternalValue(isInternal),
159169
}
160170
}
161171

‎src/runtimes/node/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { join } from 'path'
22

33
import { copyFile } from 'cp-file'
44

5+
import getInternalValue from '../../utils/get_internal_value.js'
56
import { GetSrcFilesFunction, Runtime, RuntimeType, ZipFunction } from '../runtime.js'
67

78
import { getBundler, getBundlerName } from './bundlers/index.js'
@@ -45,6 +46,7 @@ const zipFunction: ZipFunction = async function ({
4546
srcDir,
4647
srcPath,
4748
stat,
49+
isInternal,
4850
}) {
4951
const pluginsModulesPath = await getPluginsModulesPath(srcDir)
5052
const bundlerName = await getBundlerName({
@@ -127,7 +129,7 @@ const zipFunction: ZipFunction = async function ({
127129
nodeModulesWithDynamicImports,
128130
path: zipPath,
129131
displayName: config?.name,
130-
generator: config?.generator,
132+
generator: config?.generator || getInternalValue(isInternal),
131133
}
132134
}
133135

‎src/runtimes/runtime.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export type ZipFunction = (
5959
featureFlags: FeatureFlags
6060
repositoryRoot?: string
6161
generator?: string
62+
isInternal: boolean
6263
} & FunctionSource,
6364
) => Promise<ZipFunctionResult>
6465

‎src/runtimes/rust/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FeatureFlags } from '../../feature_flags.js'
55
import { SourceFile } from '../../function.js'
66
import type { RuntimeCache } from '../../utils/cache.js'
77
import { cachedLstat, cachedReaddir } from '../../utils/fs.js'
8+
import getInternalValue from '../../utils/get_internal_value.js'
89
import { nonNullable } from '../../utils/non_nullable.js'
910
import { zipBinary } from '../../zip_binary.js'
1011
import { detectBinaryRuntime } from '../detect_runtime.js'
@@ -137,6 +138,7 @@ const zipFunction: ZipFunction = async function ({
137138
srcDir,
138139
srcPath,
139140
stat,
141+
isInternal,
140142
}) {
141143
const destPath = join(destFolder, `${filename}.zip`)
142144
const isSource = extname(mainFile) === '.rs'
@@ -161,7 +163,7 @@ const zipFunction: ZipFunction = async function ({
161163
config,
162164
path: destPath,
163165
displayName: config?.name,
164-
generator: config?.generator,
166+
generator: config?.generator || getInternalValue(isInternal),
165167
}
166168
}
167169

‎src/utils/get_internal_value.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const getInternalValue = (isInternal: boolean) => (isInternal ? 'internalFunc' : undefined)
2+
3+
export default getInternalValue

‎src/zip.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { promises as fs } from 'fs'
22
import { resolve } from 'path'
33

4+
import isPathInside from 'is-path-inside'
45
import pMap from 'p-map'
56

67
import { ArchiveFormat } from './archive.js'
@@ -27,12 +28,14 @@ interface ZipFunctionOptions {
2728
zipGo?: boolean
2829
systemLog?: LogFunction
2930
debug?: boolean
31+
internalSrcFolder?: string
3032
}
3133

3234
export type ZipFunctionsOptions = ZipFunctionOptions & {
3335
configFileDirectories?: string[]
3436
manifest?: string
3537
parallelLimit?: number
38+
internalSrcFolder?: string
3639
}
3740

3841
const DEFAULT_PARALLEL_LIMIT = 5
@@ -60,6 +63,7 @@ export const zipFunctions = async function (
6063
repositoryRoot = basePath,
6164
systemLog,
6265
debug,
66+
internalSrcFolder,
6367
}: ZipFunctionsOptions = {},
6468
) {
6569
validateArchiveFormat(archiveFormat)
@@ -68,6 +72,7 @@ export const zipFunctions = async function (
6872
const cache = new RuntimeCache()
6973
const featureFlags = getFlags(inputFeatureFlags)
7074
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
75+
const internalFunctionsPath = internalSrcFolder && resolve(internalSrcFolder)
7176

7277
const [paths] = await Promise.all([listFunctionsDirectories(srcFolders), fs.mkdir(destFolder, { recursive: true })])
7378
const functions = await getFunctionsFromPaths(paths, {
@@ -105,6 +110,7 @@ export const zipFunctions = async function (
105110
srcDir: func.srcDir,
106111
srcPath: func.srcPath,
107112
stat: func.stat,
113+
isInternal: Boolean(internalFunctionsPath && isPathInside(func.srcPath, internalFunctionsPath)),
108114
})
109115
const durationNs = endTimer(startIntervalTime)
110116
const logObject = {
@@ -149,6 +155,7 @@ export const zipFunction = async function (
149155
repositoryRoot = basePath,
150156
systemLog,
151157
debug,
158+
internalSrcFolder,
152159
}: ZipFunctionOptions = {},
153160
) {
154161
validateArchiveFormat(archiveFormat)
@@ -158,6 +165,7 @@ export const zipFunction = async function (
158165
const srcPath = resolve(relativeSrcPath)
159166
const cache = new RuntimeCache()
160167
const functions = await getFunctionsFromPaths([srcPath], { cache, config: inputConfig, dedupe: true, featureFlags })
168+
const internalFunctionsPath = internalSrcFolder && resolve(internalSrcFolder)
161169

162170
if (functions.size === 0) {
163171
return
@@ -200,6 +208,7 @@ export const zipFunction = async function (
200208
srcDir,
201209
srcPath,
202210
stat: stats,
211+
isInternal: Boolean(internalFunctionsPath && isPathInside(srcPath, internalFunctionsPath)),
203212
})
204213
const durationNs = endTimer(startIntervalTime)
205214
const logObject = {

‎tests/main.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ describe('zip-it-and-ship-it', () => {
7878
length: 2,
7979
opts: {
8080
...options,
81+
internalSrcFolder: join(FIXTURES_DIR, fixtureName),
8182
config: { 'function-1': { name: 'Function One', generator: '@netlify/mock-plugin@1.0.0' } },
8283
},
8384
})
8485
expect(files).toHaveLength(2)
8586
expect(files[0].displayName).toBe('Function One')
8687
expect(files[0].generator).toBe('@netlify/mock-plugin@1.0.0')
8788
expect(files[1].displayName).toBeUndefined()
89+
expect(files[1].generator).toBe('internalFunc')
8890
},
8991
)
9092

@@ -1785,7 +1787,7 @@ describe('zip-it-and-ship-it', () => {
17851787
expect(mockSource).toBe(unzippedBinaryContents)
17861788
})
17871789

1788-
test('Builds Go functions from an internal functions dir with a configured fields', async () => {
1790+
test('Builds Go functions from an internal functions dir with configured fields', async () => {
17891791
vi.mocked(shellUtils.runCommand).mockImplementation(async (...args) => {
17901792
await writeFile(args[1][2], '')
17911793

@@ -1796,6 +1798,7 @@ describe('zip-it-and-ship-it', () => {
17961798
const { files } = await zipFixture(fixtureName, {
17971799
length: 2,
17981800
opts: {
1801+
internalSrcFolder: join(FIXTURES_DIR, fixtureName),
17991802
config: {
18001803
'go-func-1': {
18011804
name: 'Go Function One',
@@ -1809,6 +1812,7 @@ describe('zip-it-and-ship-it', () => {
18091812
expect(files[0].displayName).toBe('Go Function One')
18101813
expect(files[0].generator).toBe('@netlify/mock-plugin@1.0.0')
18111814
expect(files[1].displayName).toBeUndefined()
1815+
expect(files[1].generator).toBe('internalFunc')
18121816
})
18131817

18141818
test('Builds Go functions from source', async () => {
@@ -1985,6 +1989,7 @@ describe('zip-it-and-ship-it', () => {
19851989
const { files } = await zipFixture(fixtureName, {
19861990
length: 2,
19871991
opts: {
1992+
internalSrcFolder: join(FIXTURES_DIR, fixtureName),
19881993
config: {
19891994
'rust-func-1': {
19901995
name: 'Rust Function Two',
@@ -2001,6 +2006,7 @@ describe('zip-it-and-ship-it', () => {
20012006
expect(files[0].displayName).toBe('Rust Function Two')
20022007
expect(files[0].generator).toBe('@netlify/mock-plugin@1.0.0')
20032008
expect(files[1].displayName).toBeUndefined()
2009+
expect(files[1].generator).toBe('internalFunc')
20042010
})
20052011

20062012
test('Adds `type: "functionsBundling"` to errors resulting from compiling Rust binaries', async () => {
@@ -2082,7 +2088,6 @@ describe('zip-it-and-ship-it', () => {
20822088
)
20832089
})
20842090

2085-
// manifest stuff
20862091
test('Creates a manifest file with the list of created functions if the `manifest` property is supplied', async () => {
20872092
const FUNCTIONS_COUNT = 6
20882093
const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
@@ -2316,6 +2321,7 @@ describe('zip-it-and-ship-it', () => {
23162321
expect(func1Entry?.generator).toBe('@netlify/mock-plugin@1.0.0')
23172322
expect(func1Entry?.config.includedFiles).toEqual(['blog/*.md'])
23182323
expect(func2Entry?.config.includedFiles).toEqual(['blog/*.md'])
2324+
expect(func2Entry?.generator).toBeUndefined()
23192325
expect(func3Entry?.config.includedFiles).toBe(undefined)
23202326
expect(func4Entry?.config.includedFiles).toBe(undefined)
23212327

1 commit comments

Comments
 (1)

github-actions[bot] commented on Feb 28, 2023

@github-actions[bot]
Contributor

⏱ Benchmark results

  • largeDepsEsbuild: 2.5s
  • largeDepsNft: 9.9s
  • largeDepsZisi: 18.9s
This repository has been archived.