Skip to content

Commit

Permalink
feat: add bootstrapVersion field to manifest (#5890)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Oct 21, 2024
1 parent a80f13c commit 46de664
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
13 changes: 7 additions & 6 deletions packages/zip-it-and-ship-it/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import type { ExtendedRoute, Route } from './utils/routes.js'

interface ManifestFunction {
buildData?: Record<string, unknown>
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
}

Expand Down Expand Up @@ -51,6 +51,7 @@ export const createManifest = async ({ functions, path }: { functions: FunctionR
}

const formatFunctionForManifest = ({
bootstrapVersion,
bundler,
displayName,
excludedRoutes,
Expand All @@ -74,7 +75,7 @@ const formatFunctionForManifest = ({
generator,
timeout,
invocationMode,
buildData: { runtimeAPIVersion },
buildData: { bootstrapVersion, runtimeAPIVersion },
mainFile,
name,
priority,
Expand Down
7 changes: 4 additions & 3 deletions packages/zip-it-and-ship-it/src/runtimes/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -165,7 +166,7 @@ const zipFunction: ZipFunction = async function ({
invocationMode,
outputModuleFormat,
nativeNodeModules,
path: zipPath.path,
path: zipResult.path,
priority,
trafficRules,
runtimeVersion:
Expand Down
20 changes: 13 additions & 7 deletions packages/zip-it-and-ship-it/src/runtimes/node/utils/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
}
Expand All @@ -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<ZipNodeJsResult> {
if (archiveFormat === ARCHIVE_FORMAT.ZIP) {
return createZipArchive(options)
}
Expand Down
1 change: 1 addition & 0 deletions packages/zip-it-and-ship-it/src/utils/format_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { removeUndefined } from './remove_undefined.js'
import type { ExtendedRoute, Route } from './routes.js'

export type FunctionResult = Omit<FunctionArchive, 'runtime'> & {
bootstrapVersion?: string
routes?: ExtendedRoute[]
excludedRoutes?: Route[]
runtime: RuntimeName
Expand Down
31 changes: 31 additions & 0 deletions packages/zip-it-and-ship-it/tests/v2api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
})

0 comments on commit 46de664

Please sign in to comment.