Skip to content

Commit

Permalink
feat: adds getNodeVersion util (#1002)
Browse files Browse the repository at this point in the history
* feat: add `getNodeVersion` util

* chore: remove ESLint comment

* chore: add more unit tests
  • Loading branch information
eduardoboucas committed Feb 7, 2022
1 parent ccb1e4a commit b66177d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Expand Up @@ -11,13 +11,14 @@ module.exports = {
},
},
{
files: 'tests/*.js',
files: 'tests/**/*.js',
rules: {
'import/max-dependencies': 'off',
'import/no-dynamic-require': 'off',
'max-lines-per-function': 'off',
'max-statements': 'off',
'node/global-require': 'off',
'no-magic-numbers': 'off',
},
},
],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -112,7 +112,8 @@
},
"ava": {
"files": [
"tests/*"
"tests/*",
"tests/unit/**/*.js"
],
"verbose": true,
"timeout": "2m"
Expand Down
6 changes: 1 addition & 5 deletions src/config.ts
Expand Up @@ -2,11 +2,7 @@ import mergeOptions from 'merge-options'
import minimatch from 'minimatch'

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

// eslint-disable-next-line no-magic-numbers
type SupportedVersionNumbers = 8 | 10 | 12 | 14
type NodeVersion = `${SupportedVersionNumbers}.x` | `nodejs${SupportedVersionNumbers}.x`
import type { NodeBundlerName, NodeVersion } from './runtimes/node'

interface FunctionConfig {
externalNodeModules?: string[]
Expand Down
1 change: 1 addition & 0 deletions src/runtimes/node/index.ts
Expand Up @@ -13,6 +13,7 @@ import { createAliases as createPluginsModulesPathAliases, getPluginsModulesPath
import { zipNodeJs } from './utils/zip'

export type NodeBundlerName = 'esbuild' | 'esbuild_zisi' | 'nft' | 'zisi'
export { NodeVersion } from './utils/node_version'

// We use ZISI as the default bundler, except for certain extensions, for which
// esbuild is the only option.
Expand Down
33 changes: 33 additions & 0 deletions src/runtimes/node/utils/node_version.ts
@@ -0,0 +1,33 @@
// eslint-disable-next-line no-magic-numbers
type SupportedVersionNumbers = 8 | 10 | 12 | 14
type NodeVersion = `${SupportedVersionNumbers}.x` | `nodejs${SupportedVersionNumbers}.x`

// Must match the default version used in Bitballoon.
const DEFAULT_NODE_VERSION = 14
const VERSION_REGEX = /(nodejs)?(\d+)\.x/

const getNodeVersion = (configVersion?: string) => parseVersion(configVersion) ?? DEFAULT_NODE_VERSION

// Takes a string in the format defined by the `NodeVersion` type and returns
// the numeric major version (e.g. "nodejs14.x" => 14).
const parseVersion = (input: string | undefined) => {
if (input === undefined) {
return
}

const match = input.match(VERSION_REGEX)

if (match === null) {
return
}

const version = Number.parseInt(match[2])

if (Number.isNaN(version)) {
return
}

return version
}

export { DEFAULT_NODE_VERSION, getNodeVersion, parseVersion, NodeVersion }
1 change: 0 additions & 1 deletion tests/main.js
Expand Up @@ -2200,7 +2200,6 @@ test.serial('Builds Rust functions from source if the `buildRustSource` feature
})

t.is(files.length, 2)
// eslint-disable-next-line no-magic-numbers
t.is(shellUtilsStub.callCount, 4)

const { args: call1 } = shellUtilsStub.getCall(0)
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/runtimes/node/utils/node_version.js
@@ -0,0 +1,25 @@
const test = require('ava')

const {
DEFAULT_NODE_VERSION,
getNodeVersion,
parseVersion,
} = require('../../../../../dist/runtimes/node/utils/node_version')

test('getNodeVersion', (t) => {
t.is(getNodeVersion('nodejs12.x'), 12)
t.is(getNodeVersion('nodejs8.x'), 8)
t.is(getNodeVersion('12.x'), 12)
t.is(getNodeVersion('8.x'), 8)
t.is(getNodeVersion('node14'), DEFAULT_NODE_VERSION)
t.is(getNodeVersion(':shrug:'), DEFAULT_NODE_VERSION)
})

test('parseVersion', (t) => {
t.is(parseVersion('nodejs12.x'), 12)
t.is(parseVersion('nodejs8.x'), 8)
t.is(parseVersion('12.x'), 12)
t.is(parseVersion('8.x'), 8)
t.is(parseVersion('node14'), undefined)
t.is(parseVersion(':shrug:'), undefined)
})
@@ -1,6 +1,6 @@
const test = require('ava')

const { sanitisePackageJson } = require('../dist/runtimes/node/utils/package_json')
const { sanitisePackageJson } = require('../../../../../dist/runtimes/node/utils/package_json')

test('sanitisePackageJson', (t) => {
t.deepEqual(
Expand Down

1 comment on commit b66177d

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

largeDepsNft: 32.3s

largeDepsZisi: 58.4s

Please sign in to comment.