Skip to content

Commit 7c5551d

Browse files
Uzlopaktaeold
andauthored
fix: remove semver dependency (#700)
Co-authored-by: Daniel Lee <danielylee@google.com>
1 parent e0a7ff8 commit 7c5551d

File tree

9 files changed

+24
-36
lines changed

9 files changed

+24
-36
lines changed

package-lock.json

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
"express": "^5.0.0",
2626
"minimist": "^1.2.8",
2727
"on-finished": "^2.3.0",
28-
"read-package-up": "^11.0.0",
29-
"semver": "^7.7.1"
28+
"read-package-up": "^11.0.0"
3029
},
3130
"scripts": {
3231
"test": "mocha build/test --recursive",
@@ -58,7 +57,6 @@
5857
"@types/mocha": "^10.0.0",
5958
"@types/node": "^24.0.0",
6059
"@types/on-finished": "2.3.5",
61-
"@types/semver": "^7.7.0",
6260
"@types/sinon": "^17.0.4",
6361
"@types/supertest": "6.0.3",
6462
"gts": "6.0.2",

src/async_local_storage.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import * as semver from 'semver';
21
import {Request, Response} from './functions';
32
import {NextFunction} from 'express';
4-
import {requiredNodeJsVersionForLogExecutionID} from './options';
53
import type {AsyncLocalStorage} from 'node:async_hooks';
64

75
export interface ExecutionContext {
@@ -16,13 +14,6 @@ export async function asyncLocalStorageMiddleware(
1614
res: Response,
1715
next: NextFunction,
1816
) {
19-
if (
20-
semver.lt(process.versions.node, requiredNodeJsVersionForLogExecutionID)
21-
) {
22-
// Skip for unsupported Node.js version.
23-
next();
24-
return;
25-
}
2617
if (!asyncLocalStorage) {
2718
const asyncHooks = await import('node:async_hooks');
2819
asyncLocalStorage = new asyncHooks.AsyncLocalStorage();

src/loader.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020

2121
import * as path from 'path';
22-
import * as semver from 'semver';
2322
import {pathToFileURL} from 'url';
2423
import {HandlerFunction} from './functions';
2524
import {SignatureType} from './types';
@@ -31,6 +30,11 @@ import {getRegisteredFunction} from './function_registry';
3130
// Exported for testing.
3231
export const MIN_NODE_VERSION_ESMODULES = '13.2.0';
3332

33+
export const esModuleSupported = (() => {
34+
const [major, minor] = process.versions.node.split('.', 2).map(Number);
35+
return major > 13 || (major === 13 && minor >= 2);
36+
})();
37+
3438
/**
3539
* Determines whether the given module is an ES module.
3640
*
@@ -104,7 +108,7 @@ export async function getUserFunction(
104108
let functionModule;
105109
const esModule = await isEsModule(functionModulePath);
106110
if (esModule) {
107-
if (semver.lt(process.version, MIN_NODE_VERSION_ESMODULES)) {
111+
if (esModuleSupported === false) {
108112
console.error(
109113
`Cannot load ES Module on Node.js ${process.version}. ` +
110114
`Please upgrade to Node.js v${MIN_NODE_VERSION_ESMODULES} and up.`,

src/options.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
import * as minimist from 'minimist';
16-
import * as semver from 'semver';
1716
import {resolve} from 'path';
1817
import {SignatureType, isValidSignatureType} from './types';
1918

@@ -141,24 +140,23 @@ const IgnoredRoutesOption = new ConfigurableOption<string | null>(
141140
);
142141

143142
export const requiredNodeJsVersionForLogExecutionID = '13.0.0';
143+
144+
export const logExecutionIdSupported =
145+
Number(process.versions.node.split('.', 1)[0]) >= 13;
146+
144147
const ExecutionIdOption = new ConfigurableOption(
145148
'log-execution-id',
146149
'LOG_EXECUTION_ID',
147150
false,
148151
x => {
149-
const nodeVersion = process.versions.node;
150-
const isVersionSatisfied = semver.gte(
151-
nodeVersion,
152-
requiredNodeJsVersionForLogExecutionID,
153-
);
154152
const isTrue =
155153
(typeof x === 'boolean' && x) ||
156154
(typeof x === 'string' && x.toLowerCase() === 'true');
157-
if (isTrue && !isVersionSatisfied) {
155+
if (isTrue && !logExecutionIdSupported) {
158156
console.warn(
159157
`Execution id is only supported with Node.js versions
160158
${requiredNodeJsVersionForLogExecutionID} and above. Your
161-
current version is ${nodeVersion}. Please upgrade.`,
159+
current version is ${process.versions.node}. Please upgrade.`,
162160
);
163161
console.warn('Proceeding with execution id support disabled...');
164162
return false;

src/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {timeoutMiddleware} from './middleware/timeout';
2525
import {wrapUserFunction} from './function_wrappers';
2626
import {asyncLocalStorageMiddleware} from './async_local_storage';
2727
import {executionContextMiddleware} from './execution_context';
28-
import {FrameworkOptions} from './options';
28+
import {FrameworkOptions, logExecutionIdSupported} from './options';
2929

3030
/**
3131
* Creates and configures an Express application and returns an HTTP server
@@ -106,8 +106,11 @@ export function getServer(
106106

107107
// Get execution context.
108108
app.use(executionContextMiddleware);
109+
109110
// Store execution context to async local storge.
110-
app.use(asyncLocalStorageMiddleware);
111+
if (logExecutionIdSupported) {
112+
app.use(asyncLocalStorageMiddleware);
113+
}
111114

112115
if (
113116
options.signatureType === 'event' ||

test/async_local_storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
import {Request, Response} from '../src/functions';
77
import {NextFunction} from 'express';
88
import * as assert from 'assert';
9-
import * as semver from 'semver';
9+
import {logExecutionIdSupported} from '../src/options';
1010

11-
const runOrSkip = semver.lt(process.versions.node, '13.0.0') ? it.skip : it;
11+
const runOrSkip = logExecutionIdSupported ? it : it.skip;
1212

1313
describe('asyncLocalStorageMiddleware', () => {
1414
runOrSkip('async local storage', async () => {

test/loader.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import * as assert from 'assert';
1616
import * as express from 'express';
17-
import * as semver from 'semver';
1817
import * as functions from '../src/functions';
1918
import * as loader from '../src/loader';
2019
import * as FunctionRegistry from '../src/function_registry';
@@ -80,7 +79,7 @@ describe('loading function', () => {
8079
);
8180
return loadedFunction?.userFunction as functions.HttpFunction;
8281
};
83-
if (semver.lt(process.version, loader.MIN_NODE_VERSION_ESMODULES)) {
82+
if (loader.esModuleSupported === false) {
8483
it(`should fail to load function in an ES module ${test.name}`, async () => {
8584
void assert.rejects(loadFn);
8685
});

test/options.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
// limitations under the License.
1414

1515
import * as assert from 'assert';
16-
import * as semver from 'semver';
1716
import {resolve} from 'path';
1817
import {
1918
parseOptions,
2019
FrameworkOptions,
21-
requiredNodeJsVersionForLogExecutionID,
20+
logExecutionIdSupported,
2221
} from '../src/options';
2322

2423
describe('parseOptions', () => {
@@ -202,9 +201,7 @@ describe('parseOptions', () => {
202201
executionIdTestData.forEach(testCase => {
203202
it(testCase.name, () => {
204203
const options = parseOptions(testCase.cliOpts, testCase.envVars);
205-
if (
206-
semver.lt(process.versions.node, requiredNodeJsVersionForLogExecutionID)
207-
) {
204+
if (logExecutionIdSupported === false) {
208205
assert.strictEqual(options.enableExecutionId, false);
209206
} else {
210207
assert.strictEqual(options.enableExecutionId, true);

0 commit comments

Comments
 (0)