Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/zip-it-and-ship-it/src/feature_flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export const defaultFlags = {
// Adds the `___netlify-telemetry.mjs` file to the function bundle.
zisi_add_instrumentation_loader: true,

// Adds a `___netlify-bootstrap-version` file to the function bundle.
zisi_add_version_file: false,
// Adds a `___netlify-metadata.json` file to the function bundle.
zisi_add_metadata_file: false,
} as const

export type FeatureFlags = Partial<Record<keyof typeof defaultFlags, boolean>>
Expand Down
2 changes: 2 additions & 0 deletions packages/zip-it-and-ship-it/src/runtimes/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const getSrcFilesWithBundler: GetSrcFilesFunction = async (parameters) => {
const zipFunction: ZipFunction = async function ({
archiveFormat,
basePath,
branch,
cache,
config = {},
destFolder,
Expand Down Expand Up @@ -113,6 +114,7 @@ const zipFunction: ZipFunction = async function ({
aliases,
archiveFormat,
basePath: finalBasePath,
branch,
cache,
destFolder,
extension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { normalizeFilePath } from './normalize_path.js'
export const ENTRY_FILE_NAME = '___netlify-entry-point'
export const BOOTSTRAP_FILE_NAME = '___netlify-bootstrap.mjs'
export const BOOTSTRAP_VERSION_FILE_NAME = '___netlify-bootstrap-version'
export const METADATA_FILE_NAME = '___netlify-metadata.json'
export const TELEMETRY_FILE_NAME = '___netlify-telemetry.mjs'

const require = createRequire(import.meta.url)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface MetadataFile {
bootstrap_version?: string
branch?: string
version: number
}

export const getMetadataFile = (bootstrapVersion?: string, branch?: string): MetadataFile => ({
bootstrap_version: bootstrapVersion,
branch,
version: 1,
})
12 changes: 7 additions & 5 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 @@ -23,13 +23,14 @@ import { cachedLstat, mkdirAndWriteFile } from '../../../utils/fs.js'

import {
BOOTSTRAP_FILE_NAME,
BOOTSTRAP_VERSION_FILE_NAME,
METADATA_FILE_NAME,
conflictsWithEntryFile,
EntryFile,
getEntryFile,
getTelemetryFile,
isNamedLikeEntryFile,
} from './entry_file.js'
import { getMetadataFile } from './metadata_file.js'
import { ModuleFormat } from './module_format.js'
import { normalizeFilePath } from './normalize_path.js'
import { getPackageJsonIfAvailable } from './package_json.js'
Expand All @@ -44,6 +45,7 @@ const DEFAULT_USER_SUBDIRECTORY = 'src'
interface ZipNodeParameters {
aliases?: Map<string, string>
basePath: string
branch?: string
cache: RuntimeCache
destFolder: string
extension: string
Expand Down Expand Up @@ -186,6 +188,7 @@ const createDirectory = async function ({
const createZipArchive = async function ({
aliases = new Map(),
basePath,
branch,
cache,
destFolder,
extension,
Expand Down Expand Up @@ -251,12 +254,11 @@ const createZipArchive = async function ({
if (runtimeAPIVersion === 2) {
const bootstrapPath = addBootstrapFile(srcFiles, aliases)

if (featureFlags.zisi_add_version_file === true) {
if (featureFlags.zisi_add_metadata_file === true) {
const { version: bootstrapVersion } = await getPackageJsonIfAvailable(bootstrapPath)
const payload = JSON.stringify(getMetadataFile(bootstrapVersion, branch))

if (bootstrapVersion) {
addZipContent(archive, bootstrapVersion, BOOTSTRAP_VERSION_FILE_NAME)
}
addZipContent(archive, payload, METADATA_FILE_NAME)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/zip-it-and-ship-it/src/runtimes/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type ZipFunction = (
args: {
archiveFormat: ArchiveFormat
basePath?: string
branch?: string
cache: RuntimeCache
config: FunctionConfig
destFolder: string
Expand Down
3 changes: 3 additions & 0 deletions packages/zip-it-and-ship-it/src/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { nonNullable } from './utils/non_nullable.js'
export interface ZipFunctionOptions {
archiveFormat?: ArchiveFormat
basePath?: string
branch?: string
config?: Config
featureFlags?: FeatureFlags
repositoryRoot?: string
Expand Down Expand Up @@ -53,6 +54,7 @@ export const zipFunctions = async function (
{
archiveFormat = ARCHIVE_FORMAT.ZIP,
basePath,
branch,
config = {},
configFileDirectories,
featureFlags: inputFeatureFlags,
Expand Down Expand Up @@ -94,6 +96,7 @@ export const zipFunctions = async function (
const zipResult = await func.runtime.zipFunction({
archiveFormat,
basePath,
branch,
cache,
config: func.config,
destFolder,
Expand Down
54 changes: 40 additions & 14 deletions packages/zip-it-and-ship-it/tests/v2api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,22 +709,48 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
expect(files[0].runtimeVersion).toBe('nodejs20.x')
})

test('Adds a file with the bootstrap version to the ZIP archive', async () => {
const fixtureName = 'v2-api'
const { files } = await zipFixture(fixtureName, {
fixtureDir: FIXTURES_ESM_DIR,
opts: {
featureFlags: {
zisi_add_version_file: true,
describe('Adds a file with metadata', () => {
test('Without a branch', async () => {
const fixtureName = 'v2-api'
const { files } = await zipFixture(fixtureName, {
fixtureDir: FIXTURES_ESM_DIR,
opts: {
featureFlags: {
zisi_add_metadata_file: true,
},
},
},
})
const [unzippedFunction] = await unzipFiles(files)
const bootstrapPath = getBootstrapPath()
const bootstrapPackageJson = await readFile(resolve(bootstrapPath, '..', '..', 'package.json'), 'utf8')
const { version: bootstrapVersion } = JSON.parse(bootstrapPackageJson)
const versionFileContents = await readFile(join(unzippedFunction.unzipPath, '___netlify-metadata.json'), 'utf8')

expect(JSON.parse(versionFileContents)).toEqual({ bootstrap_version: bootstrapVersion, version: 1 })
})
const [unzippedFunction] = await unzipFiles(files)
const bootstrapPath = getBootstrapPath()
const bootstrapPackageJson = await readFile(resolve(bootstrapPath, '..', '..', 'package.json'), 'utf8')
const { version: bootstrapVersion } = JSON.parse(bootstrapPackageJson)
const versionFileContents = await readFile(join(unzippedFunction.unzipPath, '___netlify-bootstrap-version'), 'utf8')

expect(versionFileContents).toBe(bootstrapVersion)
test('With a branch', async () => {
const fixtureName = 'v2-api'
const { files } = await zipFixture(fixtureName, {
fixtureDir: FIXTURES_ESM_DIR,
opts: {
branch: 'main',
featureFlags: {
zisi_add_metadata_file: true,
},
},
})
const [unzippedFunction] = await unzipFiles(files)
const bootstrapPath = getBootstrapPath()
const bootstrapPackageJson = await readFile(resolve(bootstrapPath, '..', '..', 'package.json'), 'utf8')
const { version: bootstrapVersion } = JSON.parse(bootstrapPackageJson)
const versionFileContents = await readFile(join(unzippedFunction.unzipPath, '___netlify-metadata.json'), 'utf8')

expect(JSON.parse(versionFileContents)).toEqual({
bootstrap_version: bootstrapVersion,
branch: 'main',
version: 1,
})
})
})
})
Loading