From 46de664d2815c430f7d1b45c8bd92d42a5cc4502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Mon, 21 Oct 2024 14:54:27 +0100 Subject: [PATCH] feat: add `bootstrapVersion` field to manifest (#5890) --- packages/zip-it-and-ship-it/src/manifest.ts | 13 ++++---- .../src/runtimes/node/index.ts | 7 +++-- .../src/runtimes/node/utils/zip.ts | 20 +++++++----- .../src/utils/format_result.ts | 1 + .../zip-it-and-ship-it/tests/v2api.test.ts | 31 +++++++++++++++++++ 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/packages/zip-it-and-ship-it/src/manifest.ts b/packages/zip-it-and-ship-it/src/manifest.ts index e6d54ce851..41a4481235 100644 --- a/packages/zip-it-and-ship-it/src/manifest.ts +++ b/packages/zip-it-and-ship-it/src/manifest.ts @@ -9,20 +9,20 @@ import type { ExtendedRoute, Route } from './utils/routes.js' interface ManifestFunction { buildData?: Record + bundler?: string + displayName?: string + excludedRoutes?: Route[] + generator?: string invocationMode?: InvocationMode mainFile: string name: string path: string + priority?: number routes?: ExtendedRoute[] - excludedRoutes?: Route[] runtime: string runtimeVersion?: string schedule?: string - displayName?: string - bundler?: string - generator?: string timeout?: number - priority?: number trafficRules?: TrafficRules } @@ -51,6 +51,7 @@ export const createManifest = async ({ functions, path }: { functions: FunctionR } const formatFunctionForManifest = ({ + bootstrapVersion, bundler, displayName, excludedRoutes, @@ -74,7 +75,7 @@ const formatFunctionForManifest = ({ generator, timeout, invocationMode, - buildData: { runtimeAPIVersion }, + buildData: { bootstrapVersion, runtimeAPIVersion }, mainFile, name, priority, diff --git a/packages/zip-it-and-ship-it/src/runtimes/node/index.ts b/packages/zip-it-and-ship-it/src/runtimes/node/index.ts index aedf137231..f488d55d55 100644 --- a/packages/zip-it-and-ship-it/src/runtimes/node/index.ts +++ b/packages/zip-it-and-ship-it/src/runtimes/node/index.ts @@ -110,7 +110,7 @@ const zipFunction: ZipFunction = async function ({ createPluginsModulesPathAliases(srcFiles, pluginsModulesPath, aliases, finalBasePath) const generator = mergedConfig?.generator || getInternalValue(isInternal) - const zipPath = await zipNodeJs({ + const zipResult = await zipNodeJs({ aliases, archiveFormat, basePath: finalBasePath, @@ -152,11 +152,12 @@ const zipFunction: ZipFunction = async function ({ const trafficRules = mergedConfig?.rateLimit ? getTrafficRulesConfig(mergedConfig.rateLimit) : undefined return { + bootstrapVersion: zipResult.bootstrapVersion, bundler: bundlerName, bundlerWarnings, config: mergedConfig, displayName: mergedConfig?.name, - entryFilename: zipPath.entryFilename, + entryFilename: zipResult.entryFilename, generator, timeout: mergedConfig?.timeout, inputs, @@ -165,7 +166,7 @@ const zipFunction: ZipFunction = async function ({ invocationMode, outputModuleFormat, nativeNodeModules, - path: zipPath.path, + path: zipResult.path, priority, trafficRules, runtimeVersion: diff --git a/packages/zip-it-and-ship-it/src/runtimes/node/utils/zip.ts b/packages/zip-it-and-ship-it/src/runtimes/node/utils/zip.ts index 5712460712..b079430083 100644 --- a/packages/zip-it-and-ship-it/src/runtimes/node/utils/zip.ts +++ b/packages/zip-it-and-ship-it/src/runtimes/node/utils/zip.ts @@ -229,6 +229,7 @@ const createZipArchive = async function ({ const userNamespace = hasEntryFileConflict ? DEFAULT_USER_SUBDIRECTORY : '' let entryFilename = `${basename(filename, extname(filename))}.js` + let bootstrapVersion: string | undefined if (needsEntryFile) { const entryFile = getEntryFile({ @@ -255,8 +256,10 @@ const createZipArchive = async function ({ const bootstrapPath = addBootstrapFile(srcFiles, aliases) if (featureFlags.zisi_add_metadata_file === true) { - const { version: bootstrapVersion } = await getPackageJsonIfAvailable(bootstrapPath) - const payload = JSON.stringify(getMetadataFile(bootstrapVersion, branch)) + const { version } = await getPackageJsonIfAvailable(bootstrapPath) + const payload = JSON.stringify(getMetadataFile(version, branch)) + + bootstrapVersion = version addZipContent(archive, payload, METADATA_FILE_NAME) } @@ -281,16 +284,19 @@ const createZipArchive = async function ({ await endZip(archive, output) - return { path: destPath, entryFilename } + return { path: destPath, entryFilename, bootstrapVersion } +} + +interface ZipNodeJsResult { + bootstrapVersion?: string + entryFilename: string + path: string } export const zipNodeJs = function ({ archiveFormat, ...options -}: ZipNodeParameters & { archiveFormat: ArchiveFormat }): Promise<{ - path: string - entryFilename: string -}> { +}: ZipNodeParameters & { archiveFormat: ArchiveFormat }): Promise { if (archiveFormat === ARCHIVE_FORMAT.ZIP) { return createZipArchive(options) } diff --git a/packages/zip-it-and-ship-it/src/utils/format_result.ts b/packages/zip-it-and-ship-it/src/utils/format_result.ts index e5e1d30417..3e2cbb3564 100644 --- a/packages/zip-it-and-ship-it/src/utils/format_result.ts +++ b/packages/zip-it-and-ship-it/src/utils/format_result.ts @@ -5,6 +5,7 @@ import { removeUndefined } from './remove_undefined.js' import type { ExtendedRoute, Route } from './routes.js' export type FunctionResult = Omit & { + bootstrapVersion?: string routes?: ExtendedRoute[] excludedRoutes?: Route[] runtime: RuntimeName diff --git a/packages/zip-it-and-ship-it/tests/v2api.test.ts b/packages/zip-it-and-ship-it/tests/v2api.test.ts index ff20297f21..a79c6348ac 100644 --- a/packages/zip-it-and-ship-it/tests/v2api.test.ts +++ b/packages/zip-it-and-ship-it/tests/v2api.test.ts @@ -753,4 +753,35 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => { }) }) }) + + test('Adds a `buildData` object to each function entry in the manifest file', async () => { + const bootstrapPath = getBootstrapPath() + const bootstrapPackageJson = await readFile(resolve(bootstrapPath, '..', '..', 'package.json'), 'utf8') + const { version: bootstrapVersion } = JSON.parse(bootstrapPackageJson) + + const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' }) + const manifestPath = join(tmpDir, 'manifest.json') + + const { files } = await zipFixture('v2-api', { + fixtureDir: FIXTURES_ESM_DIR, + opts: { + featureFlags: { + zisi_add_metadata_file: true, + }, + manifest: manifestPath, + }, + }) + + expect(files.length).toBe(1) + expect(files[0].name).toBe('function') + expect(files[0].bootstrapVersion).toBe(bootstrapVersion) + expect(files[0].runtimeAPIVersion).toBe(2) + + const manifestString = await readFile(manifestPath, { encoding: 'utf8' }) + const manifest = JSON.parse(manifestString) + + expect(manifest.functions.length).toBe(1) + expect(manifest.functions[0].name).toBe('function') + expect(manifest.functions[0].buildData).toEqual({ bootstrapVersion, runtimeAPIVersion: 2 }) + }) })