Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove semver as dependency #622

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"express": "^4.16.4",
"minimist": "^1.2.7",
"on-finished": "^2.3.0",
"read-pkg-up": "^7.0.1",
"semver": "^7.3.5"
"read-pkg-up": "^7.0.1"
},
"scripts": {
"test": "mocha build/test --recursive",
Expand Down
38 changes: 18 additions & 20 deletions src/async_local_storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as semver from 'semver';
import {Request, Response} from './functions';
import {NextFunction} from 'express';
import {requiredNodeJsVersionForLogExecutionID} from './options';
import {satisfiedRequiredNodeJsVersionForLogExecutionID} from './options';
import type {AsyncLocalStorage} from 'node:async_hooks';

export interface ExecutionContext {
Expand All @@ -17,28 +16,27 @@ export async function asyncLocalStorageMiddleware(
res: Response,
next: NextFunction
) {
if (
semver.lt(process.versions.node, requiredNodeJsVersionForLogExecutionID)
) {
if (satisfiedRequiredNodeJsVersionForLogExecutionID) {
if (!asyncLocalStorage) {
const asyncHooks = await import('node:async_hooks');
asyncLocalStorage = new asyncHooks.AsyncLocalStorage();
}

asyncLocalStorage.run(
{
executionId: req.executionId,
traceId: req.traceId,
spanId: req.spanId,
},
() => {
next();
}
);
} else {
// Skip for unsupported Node.js version.
next();
return;
}
if (!asyncLocalStorage) {
const asyncHooks = await import('node:async_hooks');
asyncLocalStorage = new asyncHooks.AsyncLocalStorage();
}

asyncLocalStorage.run(
{
executionId: req.executionId,
traceId: req.traceId,
spanId: req.spanId,
},
() => {
next();
}
);
}

export function getCurrentContext(): ExecutionContext | undefined {
Expand Down
9 changes: 7 additions & 2 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

import * as path from 'path';
import * as semver from 'semver';
import * as readPkgUp from 'read-pkg-up';
import {pathToFileURL} from 'url';
import {HandlerFunction} from './functions';
Expand All @@ -31,6 +30,12 @@ import {getRegisteredFunction} from './function_registry';
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility
// Exported for testing.
export const MIN_NODE_VERSION_ESMODULES = '13.2.0';
export const satisfiedMinNodeVersionESModules = (function (
nodeVersion = process.versions.node
): boolean {
const [major, minor] = nodeVersion.slice(1).split('.', 2).map(Number);
return major > 13 || (major === 13 && minor >= 2);
})();

/**
* Determines whether the given module is an ES module.
Expand Down Expand Up @@ -104,7 +109,7 @@ export async function getUserFunction(
let functionModule;
const esModule = await isEsModule(functionModulePath);
if (esModule) {
if (semver.lt(process.version, MIN_NODE_VERSION_ESMODULES)) {
if (!satisfiedMinNodeVersionESModules) {
console.error(
`Cannot load ES Module on Node.js ${process.version}. ` +
`Please upgrade to Node.js v${MIN_NODE_VERSION_ESMODULES} and up.`
Expand Down
14 changes: 8 additions & 6 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

import * as minimist from 'minimist';
import * as semver from 'semver';
import {resolve} from 'path';
import {SignatureType, isValidSignatureType} from './types';

Expand Down Expand Up @@ -132,20 +131,23 @@ const TimeoutOption = new ConfigurableOption(
);

export const requiredNodeJsVersionForLogExecutionID = '13.0.0';
export const satisfiedRequiredNodeJsVersionForLogExecutionID = (function (
nodeVersion = process.versions.node
) {
const [major] = nodeVersion.split('.', 1).map(Number);
return major >= 13;
})();

const ExecutionIdOption = new ConfigurableOption(
'log-execution-id',
'LOG_EXECUTION_ID',
false,
x => {
const nodeVersion = process.versions.node;
const isVersionSatisfied = semver.gte(
nodeVersion,
requiredNodeJsVersionForLogExecutionID
);
const isTrue =
(typeof x === 'boolean' && x) ||
(typeof x === 'string' && x.toLowerCase() === 'true');
if (isTrue && !isVersionSatisfied) {
if (isTrue && !satisfiedRequiredNodeJsVersionForLogExecutionID) {
console.warn(
`Execution id is only supported with Node.js versions
${requiredNodeJsVersionForLogExecutionID} and above. Your
Expand Down
11 changes: 5 additions & 6 deletions test/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import * as assert from 'assert';
import * as express from 'express';
import * as semver from 'semver';
import * as functions from '../src/functions';
import * as loader from '../src/loader';
import * as FunctionRegistry from '../src/function_registry';
Expand Down Expand Up @@ -80,16 +79,16 @@ describe('loading function', () => {
);
return loadedFunction?.userFunction as functions.HttpFunction;
};
if (semver.lt(process.version, loader.MIN_NODE_VERSION_ESMODULES)) {
it(`should fail to load function in an ES module ${test.name}`, async () => {
assert.rejects(loadFn);
});
} else {
if (loader.satisfiedMinNodeVersionESModules) {
it(`should load function in an ES module ${test.name}`, async () => {
const loadedFunction = await loadFn();
const returned = loadedFunction(express.request, express.response);
assert.strictEqual(returned, 'PASS');
});
} else {
it(`should fail to load function in an ES module ${test.name}`, async () => {
assert.rejects(loadFn);
});
}
}

Expand Down
14 changes: 5 additions & 9 deletions test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
// limitations under the License.

import * as assert from 'assert';
import * as semver from 'semver';
import {resolve} from 'path';
import {
parseOptions,
FrameworkOptions,
requiredNodeJsVersionForLogExecutionID,
satisfiedRequiredNodeJsVersionForLogExecutionID,
} from '../src/options';

describe('parseOptions', () => {
Expand Down Expand Up @@ -194,13 +193,10 @@ describe('parseOptions', () => {
executionIdTestData.forEach(testCase => {
it(testCase.name, () => {
const options = parseOptions(testCase.cliOpts, testCase.envVars);
if (
semver.lt(process.versions.node, requiredNodeJsVersionForLogExecutionID)
) {
assert.strictEqual(options.enableExecutionId, false);
} else {
assert.strictEqual(options.enableExecutionId, true);
}
assert.strictEqual(
options.enableExecutionId,
satisfiedRequiredNodeJsVersionForLogExecutionID
);
});
});

Expand Down