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

Commit

Permalink
feat: expose entryFilename (#1472)
Browse files Browse the repository at this point in the history
* feat: expose entryFilename

* fix: always populate entryFilename

* fix: tests
  • Loading branch information
Skn0tt authored Jun 23, 2023
1 parent c079d56 commit 0477b3e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/runtimes/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const zipFunction: ZipFunction = async function ({

await zipBinary({ ...zipOptions, srcPath: binary.path, stat: binary.stat })

return { config, path: zipPath }
return { config, path: zipPath, entryFilename: zipOptions.filename }
}

// We don't need to zip the binary, so we can just copy it to the right path.
Expand All @@ -158,6 +158,7 @@ const zipFunction: ZipFunction = async function ({
return {
config,
path: destPath,
entryFilename: '',
displayName: config?.name,
generator: config?.generator || getInternalValue(isInternal),
}
Expand Down
5 changes: 3 additions & 2 deletions src/runtimes/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const zipFunction: ZipFunction = async function ({

await copyFile(srcPath, destPath)

return { config, path: destPath }
return { config, path: destPath, entryFilename: '' }
}

const inSourceConfig = await findISCDeclarationsInPath(mainFile, name, featureFlags)
Expand Down Expand Up @@ -142,7 +142,8 @@ const zipFunction: ZipFunction = async function ({
invocationMode,
nativeNodeModules,
nodeModulesWithDynamicImports,
path: zipPath,
path: zipPath.path,
entryFilename: zipPath.entryFilename,
runtimeVersion: getNodeRuntime(config.nodeVersion),
displayName: config?.name,
generator: config?.generator || getInternalValue(isInternal),
Expand Down
12 changes: 8 additions & 4 deletions src/runtimes/node/utils/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Buffer } from 'buffer'
import { Stats } from 'fs'
import { mkdir, rm, writeFile } from 'fs/promises'
import os from 'os'
import { basename, join } from 'path'
import { basename, extname, join } from 'path'

import { getPath as getV2APIPath } from '@netlify/serverless-functions-api'
import { copyFile } from 'cp-file'
Expand Down Expand Up @@ -119,7 +119,7 @@ const createDirectory = async function ({
{ concurrency: COPY_FILE_CONCURRENCY },
)

return functionFolder
return { path: functionFolder, entryFilename }
}

const createZipArchive = async function ({
Expand Down Expand Up @@ -163,6 +163,8 @@ const createZipArchive = async function ({
// than the entry file) to its own sub-directory.
const userNamespace = hasEntryFileConflict ? DEFAULT_USER_SUBDIRECTORY : ''

let entryFilename = `${basename(filename, extname(filename))}.js`

if (needsEntryFile) {
const entryFile = getEntryFile({
commonPrefix: basePath,
Expand All @@ -174,6 +176,8 @@ const createZipArchive = async function ({
runtimeAPIVersion,
})

entryFilename = entryFile.filename

addEntryFileToZip(archive, entryFile)
}

Expand All @@ -199,13 +203,13 @@ const createZipArchive = async function ({

await endZip(archive, output)

return destPath
return { path: destPath, entryFilename }
}

export const zipNodeJs = function ({
archiveFormat,
...options
}: ZipNodeParameters & { archiveFormat: ArchiveFormat }): Promise<string> {
}: ZipNodeParameters & { archiveFormat: ArchiveFormat }): Promise<{ path: string; entryFilename: string }> {
if (archiveFormat === ARCHIVE_FORMAT.ZIP) {
return createZipArchive(options)
}
Expand Down
1 change: 1 addition & 0 deletions src/runtimes/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface ZipFunctionResult {
nodeModulesWithDynamicImports?: string[]
path: string
runtimeVersion?: string
entryFilename: string
}

export type ZipFunction = (
Expand Down
1 change: 1 addition & 0 deletions src/runtimes/rust/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const zipFunction: ZipFunction = async function ({
return {
config,
path: destPath,
entryFilename: '',
displayName: config?.name,
generator: config?.generator || getInternalValue(isInternal),
}
Expand Down
18 changes: 18 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1803,13 +1803,15 @@ describe('zip-it-and-ship-it', () => {
mainFile: join(FIXTURES_DIR, fixtureName, 'go-func-1', 'main.go'),
name: 'go-func-1',
path: expect.anything(),
entryFilename: '',
runtime: 'go',
},
{
config: expect.anything(),
mainFile: join(FIXTURES_DIR, fixtureName, 'go-func-2', 'go-func-2.go'),
name: 'go-func-2',
path: expect.anything(),
entryFilename: '',
runtime: 'go',
},
])
Expand Down Expand Up @@ -1906,6 +1908,7 @@ describe('zip-it-and-ship-it', () => {
mainFile: join(FIXTURES_DIR, fixtureName, 'rust-func-1', 'src', 'main.rs'),
name: 'rust-func-1',
path: expect.anything(),
entryFilename: '',
runtime: 'rs',
size: 278,
},
Expand All @@ -1914,6 +1917,7 @@ describe('zip-it-and-ship-it', () => {
mainFile: join(FIXTURES_DIR, fixtureName, 'rust-func-2', 'src', 'main.rs'),
name: 'rust-func-2',
path: expect.anything(),
entryFilename: '',
runtime: 'rs',
size: 278,
},
Expand Down Expand Up @@ -2688,4 +2692,18 @@ describe('zip-it-and-ship-it', () => {
)
},
)

testMany('Outputs correct entryFilename', ['bundler_esbuild', 'bundler_nft', 'bundler_default'], async (options) => {
const { files } = await zipFixture('node-mts-extension', {
length: 3,
opts: options,
})

const unzippedFunctions = await unzipFiles(files)

for (const { unzipPath, entryFilename } of unzippedFunctions) {
const { handler } = await importFunctionFile(`${unzipPath}/${entryFilename}`)
expect(handler()).toBe(true)
}
})
})
6 changes: 3 additions & 3 deletions tests/v2api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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'
import { zipFixture, unzipFiles, importFunctionFile } from './helpers/main.js'
Expand All @@ -27,7 +26,7 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
})
const unzippedFunctions = await unzipFiles(files)

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

Expand All @@ -48,7 +47,8 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
}),
})

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

Expand Down
1 change: 1 addition & 0 deletions tests/zip_function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ describe('zipFunction', () => {
expect(result.mainFile).toBe(mainFile)
expect(result.config).toEqual(bundler === undefined ? {} : expectedConfig)
expect(result.runtimeAPIVersion).toEqual(2)
expect(result.entryFilename).toEqual('___netlify-entry-point.mjs')
})
})
})

1 comment on commit 0477b3e

@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: 2.4s
  • largeDepsNft: 7.4s
  • largeDepsZisi: 14.9s

Please sign in to comment.