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

Commit

Permalink
chore: refactor consts file to types (#758)
Browse files Browse the repository at this point in the history
* chore: remove ARCHIVE_FORMAT constants

* chore: use existing NodeBundlerName and RuntimeName types instead of consts

* chore: same for tests

* chore: improve typing for test_many helper

* chore: remove consts file

* chore: rename "runtime" to "runtimeName"

Co-authored-by: Netlify Team Account 1 <netlify-team-account-1@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
  • Loading branch information
4 people committed Oct 22, 2021
1 parent b63ff7e commit 2d7900f
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 84 deletions.
9 changes: 6 additions & 3 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { exit } from 'process'

import yargs from 'yargs'

import type { ArchiveFormat } from './archive'
import { zipFunctions } from './main'
import { ARCHIVE_FORMAT_NONE, ARCHIVE_FORMAT_ZIP } from './utils/consts'

// CLI entry point
const runCli = async function () {
Expand All @@ -31,11 +31,14 @@ const parseArgs = function () {
.parse()
}

const archiveFormats: ArchiveFormat[] = ['none', 'zip']
const defaultArchiveFormat: ArchiveFormat = 'zip'

const OPTIONS = {
'archive-format': {
string: true,
choices: [ARCHIVE_FORMAT_NONE, ARCHIVE_FORMAT_ZIP],
default: ARCHIVE_FORMAT_ZIP,
choices: archiveFormats,
default: defaultArchiveFormat,
describe: 'Format of the archive created for each function',
},
config: {
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mergeOptions from 'merge-options'
import minimatch from 'minimatch'

import { FunctionSource } from './function'
import type { NodeBundler } from './runtimes/node'
import type { NodeBundlerName } from './runtimes/node'

// eslint-disable-next-line no-magic-numbers
type SupportedVersionNumbers = 8 | 10 | 12 | 14
Expand All @@ -13,7 +13,7 @@ interface FunctionConfig {
includedFiles?: string[]
includedFilesBasePath?: string
ignoredNodeModules?: string[]
nodeBundler?: NodeBundler
nodeBundler?: NodeBundlerName
nodeSourcemap?: boolean
nodeVersion?: NodeVersion
processDynamicNodeImports?: boolean
Expand Down
27 changes: 16 additions & 11 deletions src/runtimes/detect_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import type { Buffer } from 'buffer'

import { detect, Runtime } from 'elf-cam'

import { RUNTIME_GO, RUNTIME_RUST } from '../utils/consts'
import { cachedReadFile, FsCache } from '../utils/fs'

import type { RuntimeName } from './runtime'

// Try to guess the runtime by inspecting the binary file.
const detectBinaryRuntime = async function ({ fsCache, path }: { fsCache: FsCache; path: string }) {
const detectBinaryRuntime = async function ({
fsCache,
path,
}: {
fsCache: FsCache
path: string
}): Promise<RuntimeName | undefined> {
try {
const buffer = await cachedReadFile(fsCache, path)

Expand All @@ -15,17 +22,15 @@ const detectBinaryRuntime = async function ({ fsCache, path }: { fsCache: FsCach
// Buffer in this case because we're not specifying an encoding.
const binaryType = detect(buffer as Buffer)

if (binaryType === undefined) {
return
switch (binaryType) {
case Runtime.Go:
return 'go'
case Runtime.Rust:
return 'rs'
default:
return undefined
}

return RUNTIMES[binaryType]
} catch (error) {}
}

const RUNTIMES = {
[Runtime.Go]: RUNTIME_GO,
[Runtime.Rust]: RUNTIME_RUST,
}

export { detectBinaryRuntime }
5 changes: 3 additions & 2 deletions src/runtimes/go/builder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { basename } from 'path'

import { RUNTIME_GO } from '../../utils/consts'
import { lstat } from '../../utils/fs'
import { runCommand } from '../../utils/shell'
import type { RuntimeName } from '../runtime'

const build = async ({ destPath, mainFile, srcDir }: { destPath: string; mainFile: string; srcDir: string }) => {
const functionName = basename(srcDir)
Expand All @@ -16,7 +16,8 @@ const build = async ({ destPath, mainFile, srcDir }: { destPath: string; mainFil
},
})
} catch (error) {
error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_GO } }
const runtime: RuntimeName = 'go'
error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime } }

console.error(`Could not compile Go function ${functionName}:\n`)

Expand Down
3 changes: 1 addition & 2 deletions src/runtimes/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { basename, dirname, extname, join } from 'path'
import cpFile from 'cp-file'

import { SourceFile } from '../../function'
import { RUNTIME_GO } from '../../utils/consts'
import { cachedLstat, cachedReaddir, FsCache } from '../../utils/fs'
import { nonNullable } from '../../utils/non_nullable'
import { detectBinaryRuntime } from '../detect_runtime'
Expand Down Expand Up @@ -38,7 +37,7 @@ const findFunctionsInPaths: FindFunctionsInPathsFunction = async function ({ fea
paths.map(async (path) => {
const runtime = await detectBinaryRuntime({ fsCache, path })

if (runtime === RUNTIME_GO) {
if (runtime === 'go') {
return processBinary({ fsCache, path })
}

Expand Down
7 changes: 5 additions & 2 deletions src/runtimes/node/bundlers/esbuild/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { basename, dirname, extname, resolve, join } from 'path'
import { build, Metafile } from '@netlify/esbuild'
import { tmpName } from 'tmp-promise'

import type { NodeBundlerName } from '../..'
import type { FunctionConfig } from '../../../../config'
import { JS_BUNDLER_ESBUILD, RUNTIME_JS } from '../../../../utils/consts'
import { getPathWithExtension, safeUnlink } from '../../../../utils/fs'
import type { RuntimeName } from '../../../runtime'

import { getBundlerTarget } from './bundler_target'
import { getDynamicImportsPlugin } from './plugin_dynamic_imports'
Expand Down Expand Up @@ -116,9 +117,11 @@ const bundleJsFile = async function ({
warnings,
}
} catch (error) {
const bundler: NodeBundlerName = 'esbuild'
const runtime: RuntimeName = 'js'
error.customErrorInfo = {
type: 'functionsBundling',
location: { bundler: JS_BUNDLER_ESBUILD, functionName: name, runtime: RUNTIME_JS },
location: { bundler, functionName: name, runtime },
}

throw error
Expand Down
12 changes: 6 additions & 6 deletions src/runtimes/node/bundlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Message } from '@netlify/esbuild'

import type { NodeBundlerName } from '..'
import { FunctionConfig } from '../../../config'
import { FeatureFlag } from '../../../feature_flags'
import { FunctionSource } from '../../../function'
import { JS_BUNDLER_ESBUILD, JS_BUNDLER_ESBUILD_ZISI, JS_BUNDLER_NFT, JS_BUNDLER_ZISI } from '../../../utils/consts'
import { GetSrcFilesFunction } from '../../runtime'

import esbuildBundler from './esbuild'
Expand Down Expand Up @@ -42,16 +42,16 @@ interface NodeBundler {
getSrcFiles: GetSrcFilesFunction
}

const getBundler = (name: string): NodeBundler => {
const getBundler = (name: NodeBundlerName): NodeBundler => {
switch (name) {
case JS_BUNDLER_ESBUILD:
case JS_BUNDLER_ESBUILD_ZISI:
case 'esbuild':
case 'esbuild_zisi':
return esbuildBundler

case JS_BUNDLER_NFT:
case 'nft':
return nftBundler

case JS_BUNDLER_ZISI:
case 'zisi':
return zisiBundler

default:
Expand Down
7 changes: 5 additions & 2 deletions src/runtimes/node/bundlers/zisi/list_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as esbuild from '@netlify/esbuild'
import isBuiltinModule from 'is-builtin-module'
import { tmpName } from 'tmp-promise'

import { JS_BUNDLER_ZISI, RUNTIME_JS } from '../../../../utils/consts'
import type { NodeBundlerName } from '../..'
import { safeUnlink } from '../../../../utils/fs'
import type { RuntimeName } from '../../../runtime'

// Maximum number of log messages that an esbuild instance will produce. This
// limit is important to avoid out-of-memory errors due to too much data being
Expand Down Expand Up @@ -51,9 +52,11 @@ const listImports = async ({ functionName, path }: { functionName: string; path:
target: 'esnext',
})
} catch (error) {
const bundler: NodeBundlerName = 'zisi'
const runtime: RuntimeName = 'js'
error.customErrorInfo = {
type: 'functionsBundling',
location: { bundler: JS_BUNDLER_ZISI, functionName, runtime: RUNTIME_JS },
location: { bundler, functionName, runtime },
}

throw error
Expand Down
4 changes: 2 additions & 2 deletions src/runtimes/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { findFunctionsInPaths } from './finder'
import { detectEsModule } from './utils/detect_es_module'
import { zipNodeJs } from './utils/zip'

export type NodeBundler = 'esbuild' | 'esbuild_zisi' | 'nft' | 'zisi'
export type NodeBundlerName = 'esbuild' | 'esbuild_zisi' | 'nft' | 'zisi'

// We use ZISI as the default bundler, except for certain extensions, for which
// esbuild is the only option.
Expand All @@ -22,7 +22,7 @@ const getDefaultBundler = async ({
extension: string
mainFile: string
featureFlags: FeatureFlags
}): Promise<NodeBundler> => {
}): Promise<NodeBundlerName> => {
if (['.mjs', '.ts'].includes(extension)) {
return 'esbuild'
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtimes/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FeatureFlags } from '../feature_flags'
import { FunctionSource, SourceFile } from '../function'
import { FsCache } from '../utils/fs'

import type { NodeBundler } from './node'
import type { NodeBundlerName } from './node'

type RuntimeName = 'go' | 'js' | 'rs'

Expand All @@ -25,7 +25,7 @@ type GetSrcFilesFunction = (
) => Promise<string[]>

interface ZipFunctionResult {
bundler?: NodeBundler
bundler?: NodeBundlerName
bundlerErrors?: object[]
bundlerWarnings?: object[]
config: FunctionConfig
Expand Down
8 changes: 5 additions & 3 deletions src/runtimes/rust/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import tmp from 'tmp-promise'
import toml from 'toml'

import { FunctionConfig } from '../../config'
import { RUNTIME_RUST } from '../../utils/consts'
import { lstat } from '../../utils/fs'
import { runCommand } from '../../utils/shell'
import type { RuntimeName } from '../runtime'

import { CargoManifest } from './cargo_manifest'
import { BUILD_TARGET, MANIFEST_NAME } from './constants'

const pReadFile = promisify(readFile)

const runtimeName: RuntimeName = 'rs'

const build = async ({ config, name, srcDir }: { config: FunctionConfig; name: string; srcDir: string }) => {
const functionName = basename(srcDir)

try {
await installToolchainOnce()
} catch (error) {
error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_RUST } }
error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: runtimeName } }

throw error
}
Expand Down Expand Up @@ -74,7 +76,7 @@ const cargoBuild = async ({
'There is no Rust toolchain installed. Visit https://ntl.fyi/missing-rust-toolchain for more information.'
}

error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_RUST } }
error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: runtimeName } }

throw error
}
Expand Down
3 changes: 1 addition & 2 deletions src/runtimes/rust/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { join, extname, dirname, basename } from 'path'

import { FeatureFlags } from '../../feature_flags'
import { SourceFile } from '../../function'
import { RUNTIME_RUST } from '../../utils/consts'
import { cachedLstat, cachedReaddir, FsCache } from '../../utils/fs'
import { nonNullable } from '../../utils/non_nullable'
import { zipBinary } from '../../zip_binary'
Expand Down Expand Up @@ -55,7 +54,7 @@ const findFunctionsInPaths: FindFunctionsInPathsFunction = async function ({
paths.map(async (path) => {
const runtime = await detectBinaryRuntime({ fsCache, path })

if (runtime === RUNTIME_RUST) {
if (runtime === 'rs') {
return processBinary({ fsCache, path })
}

Expand Down
9 changes: 0 additions & 9 deletions src/utils/consts.ts

This file was deleted.

3 changes: 1 addition & 2 deletions tests/helpers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { dir: getTmpDir } = require('tmp-promise')

const { zipFunctions } = require('../..')
const { listImports } = require('../../dist/runtimes/node/bundlers/zisi/list_imports')
const { ARCHIVE_FORMAT_ZIP } = require('../../dist/utils/consts')

const FIXTURES_DIR = join(__dirname, '..', 'fixtures')

Expand All @@ -19,7 +18,7 @@ const zipNode = async function (t, fixture, { length, fixtureDir, opts } = {}) {
})
const { archiveFormat } = opts || {}

if (archiveFormat === undefined || archiveFormat === ARCHIVE_FORMAT_ZIP) {
if (archiveFormat === undefined || archiveFormat === 'zip') {
await requireExtractedFiles(t, files)
}

Expand Down
6 changes: 3 additions & 3 deletions tests/helpers/test_many.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const getRateLimitedTestFunction = (originalTestFunction) => {
}

/**
* @template M, O
* @template M
* @param {import("ava")} test
* @param {Record<M, O>} matrix
* @returns {(name: string, matrix: M[], runner: (opts: O, t: import("ava").ExecutionContext) => any) => void}
* @param {Record<M, { config: import("../../src/config").Config }>} matrix
* @returns {(name: string, matrix: M[], runner: (opts: { config: import("../../src/config").Config }, t: import("ava").ExecutionContext) => any) => void}
*/
const makeTestMany = (test, matrix) => {
const filteredVariations = env.ZISI_FILTER_VARIATIONS ? env.ZISI_FILTER_VARIATIONS.split(',') : []
Expand Down

1 comment on commit 2d7900f

@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: 8.4s

largeDepsNft: 59.3s

largeDepsZisi: 1m 9.5s

Please sign in to comment.