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

Commit

Permalink
feat: expose the api version in the results (#1460)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez authored Jun 13, 2023
1 parent 418a420 commit bae1ee5
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 214 deletions.
42 changes: 31 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,16 @@ The following properties are accepted:
- _Type_: `string`
- _Default value_: undefined

A name to use when displaying the function in the Netlify UI. Populates the `displayName` property in the functions manifest for the specified function.
A name to use when displaying the function in the Netlify UI. Populates the `displayName` property in the functions
manifest for the specified function.

- `generator`

- _Type_: `string`
- _Default value_: undefined

A field to use if the function has been autogenerated by a plugin or integration. A recommended format is `@netlify/fake-plugin@1.0.0`, where adding the version is highly appreciated.
A field to use if the function has been autogenerated by a plugin or integration. A recommended format is
`@netlify/fake-plugin@1.0.0`, where adding the version is highly appreciated.

#### `featureFlags`

Expand All @@ -172,8 +174,9 @@ See [feature flags](#feature-flags).
- _Type_: `string`
- _Default value_: `undefined`

Defines the full path, including the file name, to use for the manifest file that will be created with the functions bundling results. For example, `path/to/manifest.json`. This file is a
JSON-formatted string with the following properties:
Defines the full path, including the file name, to use for the manifest file that will be created with the functions
bundling results. For example, `path/to/manifest.json`. This file is a JSON-formatted string with the following
properties:

- `functions`: An array with the functions created, in the same format as returned by `zipFunctions`
- `system.arch`: The operating system CPU architecture, as returned by
Expand All @@ -195,7 +198,9 @@ Maximum number of functions to bundle at the same time.
- _Type_: `string`
- _Default value_: `undefined`

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.
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.

### Return value

Expand Down Expand Up @@ -224,11 +229,14 @@ properties.

- `displayName`: `string`

If there was a user-defined configuration object applied to the function, and it had a `name` defined. This will be returned here.
If there was a user-defined configuration object applied to the function, and it had a `name` defined. This will be
returned here.

- `generator`: `string`

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.
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.

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

Expand Down Expand Up @@ -275,6 +283,16 @@ Additionally, the following properties also exist for Node.js functions:
A list of Node modules that reference other files with a dynamic expression (e.g. `require(someFunction())` as opposed
to `require('./some-file')`). This is an array containing the module names.

- `runtimeAPIVersion` `number | undefined`

When a function with the new API will be detected this is set to `2`. Otherwise it is set to `1`. `undefined` is only
returned in case of an error.

- `runtimeVersion` `string | undefined`

Which exact runtime this function should be using. (e.g. `nodejs18.x`). This value is detected based on the input
`nodeVersion`.

## zipFunction(srcPath, destFolder, options?)

- `srcPath`: `string`
Expand Down Expand Up @@ -326,7 +344,8 @@ Each object has the following properties:

- `displayName`: `string`

If there was a user-defined configuration object applied to the function, and it had a `name` defined. This will be returned here.
If there was a user-defined configuration object applied to the function, and it had a `name` defined. This will be
returned here.

- `mainFile`: `string`

Expand All @@ -343,12 +362,13 @@ Each object has the following properties:

- `generator`: `string`

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 a user-defined configuration object applied to the function, and it had `generator` defined. This will be
returned here.

## listFunctionsFiles(srcFolders)

Like [`listFunctions()`](#listfunctionssrcfolders-options), except it returns not only the Functions main files, but also all
their required files. This is much slower.
Like [`listFunctions()`](#listfunctionssrcfolders-options), except it returns not only the Functions main files, but
also all their required files. This is much slower.

```js
import { listFunctionsFiles } from '@netlify/zip-it-and-ship-it'
Expand Down
16 changes: 9 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ListedFunction {
mainFile: string
runtime: RuntimeName
extension: string
runtimeAPIVersion?: number
schedule?: string
displayName?: string
generator?: string
Expand Down Expand Up @@ -129,21 +130,22 @@ export const listFunctionsFiles = async function (
}

const getListedFunction = function ({
runtime,
name,
mainFile,
extension,
config,
extension,
inSourceConfig,
mainFile,
name,
runtime,
}: AugmentedFunctionSource): ListedFunction {
return {
name,
displayName: config.name,
extension,
generator: config.generator,
mainFile,
name,
runtime: runtime.name,
extension,
runtimeAPIVersion: inSourceConfig ? inSourceConfig?.runtimeAPIVersion ?? 1 : undefined,
schedule: inSourceConfig?.schedule ?? config.schedule,
generator: config.generator,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtimes/node/in_source_config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const findISCDeclarations = (source: string, functionName: string, featur

const mergedExports: ISCValues = iscExports.reduce((acc, obj) => ({ ...acc, ...obj }), {})

return mergedExports
return { ...mergedExports, runtimeAPIVersion: 1 }
}

export type ISCHandlerArg = ArgumentPlaceholder | Expression | SpreadElement | JSXNamespacedName
Expand Down
2 changes: 2 additions & 0 deletions src/utils/format_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { removeUndefined } from './remove_undefined.js'
export type FunctionResult = Omit<FunctionArchive, 'runtime'> & {
runtime: RuntimeName
schedule?: string
runtimeAPIVersion?: number
}

// Takes the result of zipping a function and formats it for output.
Expand All @@ -15,6 +16,7 @@ export const formatZipResult = (archive: FunctionArchive) => {
inSourceConfig: undefined,
runtime: archive.runtime.name,
schedule: archive.inSourceConfig?.schedule ?? archive?.config?.schedule,
runtimeAPIVersion: archive.inSourceConfig?.runtimeAPIVersion,
}

return removeUndefined(functionResult)
Expand Down
15 changes: 13 additions & 2 deletions tests/__snapshots__/list_functions.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`listFunctions > Can list function main files from multiple source directories with listFunctions() 1`] = `
exports[`listFunctions > v1 > Can list function main files from multiple source directories with listFunctions() 1`] = `
[
{
"displayName": undefined,
Expand All @@ -9,6 +9,7 @@ exports[`listFunctions > Can list function main files from multiple source direc
"mainFile": ".netlify/internal-functions/function.js",
"name": "function",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -19,6 +20,7 @@ exports[`listFunctions > Can list function main files from multiple source direc
"mainFile": ".netlify/internal-functions/function_internal.js",
"name": "function_internal",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -29,6 +31,7 @@ exports[`listFunctions > Can list function main files from multiple source direc
"mainFile": "netlify/functions/function.js",
"name": "function",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -39,13 +42,14 @@ exports[`listFunctions > Can list function main files from multiple source direc
"mainFile": "netlify/functions/function_user.js",
"name": "function_user",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
]
`;

exports[`listFunctions > Can list function main files with listFunctions() 1`] = `
exports[`listFunctions > v1 > Can list function main files with listFunctions() 1`] = `
[
{
"displayName": undefined,
Expand All @@ -54,6 +58,7 @@ exports[`listFunctions > Can list function main files with listFunctions() 1`] =
"mainFile": "test.zip",
"name": "test",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -64,6 +69,7 @@ exports[`listFunctions > Can list function main files with listFunctions() 1`] =
"mainFile": "test.js",
"name": "test",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -74,6 +80,7 @@ exports[`listFunctions > Can list function main files with listFunctions() 1`] =
"mainFile": "five/index.ts",
"name": "five",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -84,6 +91,7 @@ exports[`listFunctions > Can list function main files with listFunctions() 1`] =
"mainFile": "four.js/four.js.js",
"name": "four",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -94,6 +102,7 @@ exports[`listFunctions > Can list function main files with listFunctions() 1`] =
"mainFile": "one/index.js",
"name": "one",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -104,6 +113,7 @@ exports[`listFunctions > Can list function main files with listFunctions() 1`] =
"mainFile": "two/two.js",
"name": "two",
"runtime": "js",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand All @@ -114,6 +124,7 @@ exports[`listFunctions > Can list function main files with listFunctions() 1`] =
"mainFile": "test",
"name": "test",
"runtime": "go",
"runtimeAPIVersion": undefined,
"schedule": undefined,
"srcFile": undefined,
},
Expand Down
Loading

1 comment on commit bae1ee5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⏱ Benchmark results

  • largeDepsEsbuild: 2.8s
  • largeDepsNft: 8.8s
  • largeDepsZisi: 17.8s

Please sign in to comment.