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

Commit

Permalink
fix: write bootstrap also when archive format is not zip (#1461)
Browse files Browse the repository at this point in the history
* fix: write bootstrap also when archive format is not zip

* chore: rename
  • Loading branch information
danez authored Jun 12, 2023
1 parent 47c0b97 commit 418a420
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/runtimes/node/utils/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ interface ZipNodeParameters {
srcFiles: string[]
}

const addBootstrapFile = function (srcFiles: string[], aliases: Map<string, string>) {
// This is the path to the file that contains all the code for the v2
// functions API. We add it to the list of source files and create an
// alias so that it's written as `BOOTSTRAP_FILE_NAME` in the ZIP/Directory.
const v2APIPath = getV2APIPath()

srcFiles.push(v2APIPath)
aliases.set(v2APIPath, BOOTSTRAP_FILE_NAME)
}

const createDirectory = async function ({
aliases = new Map(),
basePath,
Expand Down Expand Up @@ -84,6 +94,10 @@ const createDirectory = async function ({
// Writing entry file.
await writeFile(join(functionFolder, entryFilename), entryContents)

if (runtimeAPIVersion === 2) {
addBootstrapFile(srcFiles, aliases)
}

// Copying source files.
await pMap(
srcFiles,
Expand Down Expand Up @@ -164,13 +178,7 @@ const createZipArchive = async function ({
}

if (runtimeAPIVersion === 2) {
// This is the path to the file that contains all the code for the v2
// functions API. We add it to the list of source files and create an
// alias so that it's written as `BOOTSTRAP_FILE_NAME` in the ZIP.
const v2APIPath = getV2APIPath()

srcFiles.push(v2APIPath)
aliases.set(v2APIPath, BOOTSTRAP_FILE_NAME)
addBootstrapFile(srcFiles, aliases)
}

const srcFilesInfos = await Promise.all(srcFiles.map((file) => addStat(cache, file)))
Expand Down
22 changes: 22 additions & 0 deletions tests/v2api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import merge from 'deepmerge'
import semver from 'semver'
import { afterEach, describe, expect, vi } from 'vitest'

import { ARCHIVE_FORMAT } from '../src/archive.js'
import { ENTRY_FILE_NAME } from '../src/runtimes/node/utils/entry_file.js'

import { invokeLambda, readAsBuffer } from './helpers/lambda.js'
Expand Down Expand Up @@ -35,4 +36,25 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
expect(statusCode).toBe(200)
},
)

testMany(
'Handles a basic JavaScript function with archiveFormat set to `none`',
['bundler_default', 'todo:bundler_esbuild', 'todo:bundler_esbuild_zisi', 'bundler_default_nft', 'bundler_nft'],
async (options) => {
const { files, tmpDir } = await zipFixture('v2-api', {
opts: merge(options, {
archiveFormat: ARCHIVE_FORMAT.NONE,
featureFlags: { zisi_functions_api_v2: true },
}),
})

const func = await importFunctionFile(`${tmpDir}/${files[0].name}/${ENTRY_FILE_NAME}.mjs`)
const { body: bodyStream, headers = {}, statusCode } = await invokeLambda(func)
const body = await readAsBuffer(bodyStream)

expect(body).toBe('<h1>Hello world</h1>')
expect(headers['content-type']).toBe('text/html')
expect(statusCode).toBe(200)
},
)
})

1 comment on commit 418a420

@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: 3s
  • largeDepsNft: 9.6s
  • largeDepsZisi: 18.7s

Please sign in to comment.