Skip to content

Commit 0886925

Browse files
committed
feat: write metadata file
1 parent 40bcebe commit 0886925

File tree

8 files changed

+67
-21
lines changed

8 files changed

+67
-21
lines changed

packages/zip-it-and-ship-it/src/feature_flags.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export const defaultFlags = {
3333
// Adds the `___netlify-telemetry.mjs` file to the function bundle.
3434
zisi_add_instrumentation_loader: true,
3535

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

4040
export type FeatureFlags = Partial<Record<keyof typeof defaultFlags, boolean>>

packages/zip-it-and-ship-it/src/runtimes/node/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const getSrcFilesWithBundler: GetSrcFilesFunction = async (parameters) => {
3737
const zipFunction: ZipFunction = async function ({
3838
archiveFormat,
3939
basePath,
40+
branch,
4041
cache,
4142
config = {},
4243
destFolder,
@@ -113,6 +114,7 @@ const zipFunction: ZipFunction = async function ({
113114
aliases,
114115
archiveFormat,
115116
basePath: finalBasePath,
117+
branch,
116118
cache,
117119
destFolder,
118120
extension,

packages/zip-it-and-ship-it/src/runtimes/node/utils/entry_file.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { normalizeFilePath } from './normalize_path.js'
1818
export const ENTRY_FILE_NAME = '___netlify-entry-point'
1919
export const BOOTSTRAP_FILE_NAME = '___netlify-bootstrap.mjs'
2020
export const BOOTSTRAP_VERSION_FILE_NAME = '___netlify-bootstrap-version'
21+
export const METADATA_FILE_NAME = '___netlify-metadata.json'
2122
export const TELEMETRY_FILE_NAME = '___netlify-telemetry.mjs'
2223

2324
const require = createRequire(import.meta.url)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface MetadataFile {
2+
bootstrap_version?: string
3+
branch?: string
4+
version: number
5+
}
6+
7+
export const getMetadataFile = (bootstrapVersion?: string, branch?: string): MetadataFile => ({
8+
bootstrap_version: bootstrapVersion,
9+
branch,
10+
version: 1,
11+
})

packages/zip-it-and-ship-it/src/runtimes/node/utils/zip.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ import { cachedLstat, mkdirAndWriteFile } from '../../../utils/fs.js'
2323

2424
import {
2525
BOOTSTRAP_FILE_NAME,
26-
BOOTSTRAP_VERSION_FILE_NAME,
26+
METADATA_FILE_NAME,
2727
conflictsWithEntryFile,
2828
EntryFile,
2929
getEntryFile,
3030
getTelemetryFile,
3131
isNamedLikeEntryFile,
3232
} from './entry_file.js'
33+
import { getMetadataFile } from './metadata_file.js'
3334
import { ModuleFormat } from './module_format.js'
3435
import { normalizeFilePath } from './normalize_path.js'
3536
import { getPackageJsonIfAvailable } from './package_json.js'
@@ -44,6 +45,7 @@ const DEFAULT_USER_SUBDIRECTORY = 'src'
4445
interface ZipNodeParameters {
4546
aliases?: Map<string, string>
4647
basePath: string
48+
branch?: string
4749
cache: RuntimeCache
4850
destFolder: string
4951
extension: string
@@ -186,6 +188,7 @@ const createDirectory = async function ({
186188
const createZipArchive = async function ({
187189
aliases = new Map(),
188190
basePath,
191+
branch,
189192
cache,
190193
destFolder,
191194
extension,
@@ -251,12 +254,11 @@ const createZipArchive = async function ({
251254
if (runtimeAPIVersion === 2) {
252255
const bootstrapPath = addBootstrapFile(srcFiles, aliases)
253256

254-
if (featureFlags.zisi_add_version_file === true) {
257+
if (featureFlags.zisi_add_metadata_file === true) {
255258
const { version: bootstrapVersion } = await getPackageJsonIfAvailable(bootstrapPath)
259+
const payload = JSON.stringify(getMetadataFile(bootstrapVersion, branch))
256260

257-
if (bootstrapVersion) {
258-
addZipContent(archive, bootstrapVersion, BOOTSTRAP_VERSION_FILE_NAME)
259-
}
261+
addZipContent(archive, payload, METADATA_FILE_NAME)
260262
}
261263
}
262264

packages/zip-it-and-ship-it/src/runtimes/runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export type ZipFunction = (
6666
args: {
6767
archiveFormat: ArchiveFormat
6868
basePath?: string
69+
branch?: string
6970
cache: RuntimeCache
7071
config: FunctionConfig
7172
destFolder: string

packages/zip-it-and-ship-it/src/zip.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { nonNullable } from './utils/non_nullable.js'
2121
export interface ZipFunctionOptions {
2222
archiveFormat?: ArchiveFormat
2323
basePath?: string
24+
branch?: string
2425
config?: Config
2526
featureFlags?: FeatureFlags
2627
repositoryRoot?: string
@@ -53,6 +54,7 @@ export const zipFunctions = async function (
5354
{
5455
archiveFormat = ARCHIVE_FORMAT.ZIP,
5556
basePath,
57+
branch,
5658
config = {},
5759
configFileDirectories,
5860
featureFlags: inputFeatureFlags,
@@ -94,6 +96,7 @@ export const zipFunctions = async function (
9496
const zipResult = await func.runtime.zipFunction({
9597
archiveFormat,
9698
basePath,
99+
branch,
97100
cache,
98101
config: func.config,
99102
destFolder,

packages/zip-it-and-ship-it/tests/v2api.test.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -709,22 +709,48 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
709709
expect(files[0].runtimeVersion).toBe('nodejs20.x')
710710
})
711711

712-
test('Adds a file with the bootstrap version to the ZIP archive', async () => {
713-
const fixtureName = 'v2-api'
714-
const { files } = await zipFixture(fixtureName, {
715-
fixtureDir: FIXTURES_ESM_DIR,
716-
opts: {
717-
featureFlags: {
718-
zisi_add_version_file: true,
712+
describe('Adds a file with metadata', () => {
713+
test('Without a branch', async () => {
714+
const fixtureName = 'v2-api'
715+
const { files } = await zipFixture(fixtureName, {
716+
fixtureDir: FIXTURES_ESM_DIR,
717+
opts: {
718+
featureFlags: {
719+
zisi_add_metadata_file: true,
720+
},
719721
},
720-
},
722+
})
723+
const [unzippedFunction] = await unzipFiles(files)
724+
const bootstrapPath = getBootstrapPath()
725+
const bootstrapPackageJson = await readFile(resolve(bootstrapPath, '..', '..', 'package.json'), 'utf8')
726+
const { version: bootstrapVersion } = JSON.parse(bootstrapPackageJson)
727+
const versionFileContents = await readFile(join(unzippedFunction.unzipPath, '___netlify-metadata.json'), 'utf8')
728+
729+
expect(JSON.parse(versionFileContents)).toEqual({ bootstrap_version: bootstrapVersion, version: 1 })
721730
})
722-
const [unzippedFunction] = await unzipFiles(files)
723-
const bootstrapPath = getBootstrapPath()
724-
const bootstrapPackageJson = await readFile(resolve(bootstrapPath, '..', '..', 'package.json'), 'utf8')
725-
const { version: bootstrapVersion } = JSON.parse(bootstrapPackageJson)
726-
const versionFileContents = await readFile(join(unzippedFunction.unzipPath, '___netlify-bootstrap-version'), 'utf8')
727731

728-
expect(versionFileContents).toBe(bootstrapVersion)
732+
test('With a branch', async () => {
733+
const fixtureName = 'v2-api'
734+
const { files } = await zipFixture(fixtureName, {
735+
fixtureDir: FIXTURES_ESM_DIR,
736+
opts: {
737+
branch: 'main',
738+
featureFlags: {
739+
zisi_add_metadata_file: true,
740+
},
741+
},
742+
})
743+
const [unzippedFunction] = await unzipFiles(files)
744+
const bootstrapPath = getBootstrapPath()
745+
const bootstrapPackageJson = await readFile(resolve(bootstrapPath, '..', '..', 'package.json'), 'utf8')
746+
const { version: bootstrapVersion } = JSON.parse(bootstrapPackageJson)
747+
const versionFileContents = await readFile(join(unzippedFunction.unzipPath, '___netlify-metadata.json'), 'utf8')
748+
749+
expect(JSON.parse(versionFileContents)).toEqual({
750+
bootstrap_version: bootstrapVersion,
751+
branch: 'main',
752+
version: 1,
753+
})
754+
})
729755
})
730756
})

0 commit comments

Comments
 (0)