diff --git a/interceptors/lunar-ts-interceptor/README.md b/interceptors/lunar-ts-interceptor/README.md index 408e70b5..43ea5257 100644 --- a/interceptors/lunar-ts-interceptor/README.md +++ b/interceptors/lunar-ts-interceptor/README.md @@ -30,12 +30,8 @@ Our Interceptors are designed to run as close as possible to our user's code and 5. Lunar Proxy returns the response to Lunar Interceptor. 6. Lunar Interceptor returns the response to the application seamlessly as expected. - - -In case of an error while using Lunar Proxy, the Interceptor will use the original destination instead. - - - +- In case of an error while using Lunar Proxy, the Interceptor will use the original destination instead. + ## Failsafe Mechanism The Failsafe Mechanism is intended to minimize delays between the Interceptor and Proxy on the rare case when the Proxy is not available. diff --git a/interceptors/lunar-ts-interceptor/package-lock.json b/interceptors/lunar-ts-interceptor/package-lock.json index 9345fa09..c08db7c4 100644 --- a/interceptors/lunar-ts-interceptor/package-lock.json +++ b/interceptors/lunar-ts-interceptor/package-lock.json @@ -1,12 +1,13 @@ { "name": "lunar-interceptor", - "version": "1.1.2", + "version": "1.1.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "lunar-interceptor", - "version": "1.1.2", + "version": "1.1.3", + "license": "MIT", "dependencies": { "ipaddr.js": "^2.1.0", "winston": "^3.10.0" @@ -14,7 +15,7 @@ "devDependencies": { "@types/async": "^3.2.21", "@types/node": "^20.8.9", - "@typescript-eslint/eslint-plugin": "^6.11.0", + "@typescript-eslint/eslint-plugin": "^6.16.0", "@typescript-eslint/parser": "^6.11.0", "axios": "^1.5.0", "eslint": "^8.53.0", @@ -26,6 +27,9 @@ "eslint-plugin-promise": "^6.1.1", "prettier": "3.0.3", "typescript": "^5.2.2" + }, + "engines": { + "node": ">=16.0.0 || >=15.7.0 <16.0.0 || >=14.18.0 <15.0.0" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -227,9 +231,9 @@ } }, "node_modules/@types/semver": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.5.tgz", - "integrity": "sha512-+d+WYC1BxJ6yVOgUgzK8gWvp5qF8ssV5r4nsDcZWKRWcDQLQ619tvWAxJQYGgBrO1MnLJC7a5GtiYsAoQ47dJg==", + "version": "7.5.6", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.6.tgz", + "integrity": "sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==", "dev": true }, "node_modules/@types/triple-beam": { @@ -238,16 +242,16 @@ "integrity": "sha512-HlJjF3wxV4R2VQkFpKe0YqJLilYNgtRtsqqZtby7RkVsSs+i+vbyzjtUwpFEdUCKcrGzCiEJE7F/0mKjh0sunA==" }, "node_modules/@typescript-eslint/eslint-plugin": { - "version": "6.11.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.11.0.tgz", - "integrity": "sha512-uXnpZDc4VRjY4iuypDBKzW1rz9T5YBBK0snMn8MaTSNd2kMlj50LnLBABELjJiOL5YHk7ZD8hbSpI9ubzqYI0w==", + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.16.0.tgz", + "integrity": "sha512-O5f7Kv5o4dLWQtPX4ywPPa+v9G+1q1x8mz0Kr0pXUtKsevo+gIJHLkGc8RxaZWtP8RrhwhSNIWThnW42K9/0rQ==", "dev": true, "dependencies": { "@eslint-community/regexpp": "^4.5.1", - "@typescript-eslint/scope-manager": "6.11.0", - "@typescript-eslint/type-utils": "6.11.0", - "@typescript-eslint/utils": "6.11.0", - "@typescript-eslint/visitor-keys": "6.11.0", + "@typescript-eslint/scope-manager": "6.16.0", + "@typescript-eslint/type-utils": "6.16.0", + "@typescript-eslint/utils": "6.16.0", + "@typescript-eslint/visitor-keys": "6.16.0", "debug": "^4.3.4", "graphemer": "^1.4.0", "ignore": "^5.2.4", @@ -272,6 +276,53 @@ } } }, + "node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/scope-manager": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-6.16.0.tgz", + "integrity": "sha512-0N7Y9DSPdaBQ3sqSCwlrm9zJwkpOuc6HYm7LpzLAPqBL7dmzAUimr4M29dMkOP/tEwvOCC/Cxo//yOfJD3HUiw==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "6.16.0", + "@typescript-eslint/visitor-keys": "6.16.0" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/types": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.16.0.tgz", + "integrity": "sha512-hvDFpLEvTJoHutVl87+MG/c5C8I6LOgEx05zExTSJDEVU7hhR3jhV8M5zuggbdFCw98+HhZWPHZeKS97kS3JoQ==", + "dev": true, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/visitor-keys": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.16.0.tgz", + "integrity": "sha512-QSFQLruk7fhs91a/Ep/LqRdbJCZ1Rq03rqBdKT5Ky17Sz8zRLUksqIe9DW0pKtg/Z35/ztbLQ6qpOCN6rOC11A==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "6.16.0", + "eslint-visitor-keys": "^3.4.1" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, "node_modules/@typescript-eslint/parser": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-6.11.0.tgz", @@ -318,13 +369,13 @@ } }, "node_modules/@typescript-eslint/type-utils": { - "version": "6.11.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-6.11.0.tgz", - "integrity": "sha512-nA4IOXwZtqBjIoYrJcYxLRO+F9ri+leVGoJcMW1uqr4r1Hq7vW5cyWrA43lFbpRvQ9XgNrnfLpIkO3i1emDBIA==", + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-6.16.0.tgz", + "integrity": "sha512-ThmrEOcARmOnoyQfYkHw/DX2SEYBalVECmoldVuH6qagKROp/jMnfXpAU/pAIWub9c4YTxga+XwgAkoA0pxfmg==", "dev": true, "dependencies": { - "@typescript-eslint/typescript-estree": "6.11.0", - "@typescript-eslint/utils": "6.11.0", + "@typescript-eslint/typescript-estree": "6.16.0", + "@typescript-eslint/utils": "6.16.0", "debug": "^4.3.4", "ts-api-utils": "^1.0.1" }, @@ -344,6 +395,88 @@ } } }, + "node_modules/@typescript-eslint/type-utils/node_modules/@typescript-eslint/types": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.16.0.tgz", + "integrity": "sha512-hvDFpLEvTJoHutVl87+MG/c5C8I6LOgEx05zExTSJDEVU7hhR3jhV8M5zuggbdFCw98+HhZWPHZeKS97kS3JoQ==", + "dev": true, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/type-utils/node_modules/@typescript-eslint/typescript-estree": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-6.16.0.tgz", + "integrity": "sha512-VTWZuixh/vr7nih6CfrdpmFNLEnoVBF1skfjdyGnNwXOH1SLeHItGdZDHhhAIzd3ACazyY2Fg76zuzOVTaknGA==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "6.16.0", + "@typescript-eslint/visitor-keys": "6.16.0", + "debug": "^4.3.4", + "globby": "^11.1.0", + "is-glob": "^4.0.3", + "minimatch": "9.0.3", + "semver": "^7.5.4", + "ts-api-utils": "^1.0.1" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@typescript-eslint/type-utils/node_modules/@typescript-eslint/visitor-keys": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.16.0.tgz", + "integrity": "sha512-QSFQLruk7fhs91a/Ep/LqRdbJCZ1Rq03rqBdKT5Ky17Sz8zRLUksqIe9DW0pKtg/Z35/ztbLQ6qpOCN6rOC11A==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "6.16.0", + "eslint-visitor-keys": "^3.4.1" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/type-utils/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/@typescript-eslint/type-utils/node_modules/minimatch": { + "version": "9.0.3", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz", + "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/@typescript-eslint/types": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.11.0.tgz", @@ -385,17 +518,17 @@ } }, "node_modules/@typescript-eslint/utils": { - "version": "6.11.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-6.11.0.tgz", - "integrity": "sha512-p23ibf68fxoZy605dc0dQAEoUsoiNoP3MD9WQGiHLDuTSOuqoTsa4oAy+h3KDkTcxbbfOtUjb9h3Ta0gT4ug2g==", + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-6.16.0.tgz", + "integrity": "sha512-T83QPKrBm6n//q9mv7oiSvy/Xq/7Hyw9SzSEhMHJwznEmQayfBM87+oAlkNAMEO7/MjIwKyOHgBJbxB0s7gx2A==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.4.0", "@types/json-schema": "^7.0.12", "@types/semver": "^7.5.0", - "@typescript-eslint/scope-manager": "6.11.0", - "@typescript-eslint/types": "6.11.0", - "@typescript-eslint/typescript-estree": "6.11.0", + "@typescript-eslint/scope-manager": "6.16.0", + "@typescript-eslint/types": "6.16.0", + "@typescript-eslint/typescript-estree": "6.16.0", "semver": "^7.5.4" }, "engines": { @@ -409,6 +542,105 @@ "eslint": "^7.0.0 || ^8.0.0" } }, + "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/scope-manager": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-6.16.0.tgz", + "integrity": "sha512-0N7Y9DSPdaBQ3sqSCwlrm9zJwkpOuc6HYm7LpzLAPqBL7dmzAUimr4M29dMkOP/tEwvOCC/Cxo//yOfJD3HUiw==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "6.16.0", + "@typescript-eslint/visitor-keys": "6.16.0" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/types": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.16.0.tgz", + "integrity": "sha512-hvDFpLEvTJoHutVl87+MG/c5C8I6LOgEx05zExTSJDEVU7hhR3jhV8M5zuggbdFCw98+HhZWPHZeKS97kS3JoQ==", + "dev": true, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/typescript-estree": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-6.16.0.tgz", + "integrity": "sha512-VTWZuixh/vr7nih6CfrdpmFNLEnoVBF1skfjdyGnNwXOH1SLeHItGdZDHhhAIzd3ACazyY2Fg76zuzOVTaknGA==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "6.16.0", + "@typescript-eslint/visitor-keys": "6.16.0", + "debug": "^4.3.4", + "globby": "^11.1.0", + "is-glob": "^4.0.3", + "minimatch": "9.0.3", + "semver": "^7.5.4", + "ts-api-utils": "^1.0.1" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/visitor-keys": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.16.0.tgz", + "integrity": "sha512-QSFQLruk7fhs91a/Ep/LqRdbJCZ1Rq03rqBdKT5Ky17Sz8zRLUksqIe9DW0pKtg/Z35/ztbLQ6qpOCN6rOC11A==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "6.16.0", + "eslint-visitor-keys": "^3.4.1" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/utils/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/@typescript-eslint/utils/node_modules/minimatch": { + "version": "9.0.3", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz", + "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/@typescript-eslint/visitor-keys": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.11.0.tgz", diff --git a/interceptors/lunar-ts-interceptor/package.json b/interceptors/lunar-ts-interceptor/package.json index 732adbcf..8d4c6dfa 100644 --- a/interceptors/lunar-ts-interceptor/package.json +++ b/interceptors/lunar-ts-interceptor/package.json @@ -1,9 +1,12 @@ { "name": "lunar-interceptor", - "version": "1.1.2", + "version": "1.1.3", "description": "The Lunar interceptor. Used to manage outgoing HTTP/S traffic.", "main": "dist/index.js", "types": "dist/index.d.ts", + "engines": { + "node": ">=16.0.0 || >=15.7.0 <16.0.0 || >=14.18.0 <15.0.0" + }, "license": "MIT", "files": [ "/dist" @@ -23,7 +26,7 @@ "devDependencies": { "@types/async": "^3.2.21", "@types/node": "^20.8.9", - "@typescript-eslint/eslint-plugin": "^6.11.0", + "@typescript-eslint/eslint-plugin": "^6.16.0", "@typescript-eslint/parser": "^6.11.0", "axios": "^1.5.0", "eslint": "^8.53.0", @@ -40,4 +43,4 @@ "ipaddr.js": "^2.1.0", "winston": "^3.10.0" } -} \ No newline at end of file +} diff --git a/interceptors/lunar-ts-interceptor/src/engineVersion.ts b/interceptors/lunar-ts-interceptor/src/engineVersion.ts new file mode 100644 index 00000000..2c3c08e0 --- /dev/null +++ b/interceptors/lunar-ts-interceptor/src/engineVersion.ts @@ -0,0 +1,38 @@ +export class EngineVersion { + major: number; + minor: number; + fix: number; + + constructor(major: number, minor: number, fix: number) { + this.major = major; + this.minor = minor; + this.fix = fix; + } + + isEqualTo(other: EngineVersion): boolean { + return this.major === other.major && + this.minor === other.minor && + this.fix === other.fix; + } + + isLessThan(other: EngineVersion): boolean { + if (this.major < other.major) return true; + if (this.major > other.major) return false; + if (this.minor < other.minor) return true; + if (this.minor > other.minor) return false; + return this.fix < other.fix; + } + + isEqualOrGreaterThan(other: EngineVersion): boolean { + return !this.isLessThan(other) || this.isEqualTo(other); + } + + isGreaterThan(other: EngineVersion): boolean { + return !this.isLessThan(other) && !this.isEqualTo(other); + } + + inRange(from: EngineVersion, to: EngineVersion): boolean { + return this.isEqualOrGreaterThan(from) && + this.isLessThan(to); + } +} diff --git a/interceptors/lunar-ts-interceptor/src/failSafe.ts b/interceptors/lunar-ts-interceptor/src/failSafe.ts index 89a06514..36a0898d 100644 --- a/interceptors/lunar-ts-interceptor/src/failSafe.ts +++ b/interceptors/lunar-ts-interceptor/src/failSafe.ts @@ -54,4 +54,4 @@ export class FailSafe { return Object.prototype.hasOwnProperty.call(headers, HEADER_ERROR_KEY); } -} \ No newline at end of file +} diff --git a/interceptors/lunar-ts-interceptor/src/helper.ts b/interceptors/lunar-ts-interceptor/src/helper.ts index 00867dcb..003cace7 100644 --- a/interceptors/lunar-ts-interceptor/src/helper.ts +++ b/interceptors/lunar-ts-interceptor/src/helper.ts @@ -1,9 +1,9 @@ import { logger } from './logger' +import { EngineVersion } from './engineVersion' import http, { type OutgoingHttpHeader } from 'http' import { URL } from 'url' import https, { type RequestOptions } from 'https' -import type Module from 'module' const PROXY_HOST_KEY = "LUNAR_PROXY_HOST" @@ -12,7 +12,6 @@ const TENANT_ID_KEY = "LUNAR_TENANT_ID" const SUPPORT_TLS_KEY = "LUNAR_PROXY_SUPPORT_TLS" const INTERCEPTOR_ID = "lunar-ts-interceptor/1.0.0" const PROXY_DEFAULT_HEALTHCHECK_PORT = 8040 -const LEGACY_URL_MODULE_VERSION = 15 export interface ConnectionInformation { proxyScheme: string @@ -33,9 +32,10 @@ export function loadConnectionInformation(): ConnectionInformation { const proxyHostAndPort: string[] = proxyHost.split(':') if (proxyHostAndPort.length !== 2) { - logger.warn("Could not obtain the Port value of Lunar Proxy from environment variables," - + `please set ${PROXY_HOST_KEY} to the Lunar Proxy's host/IP and port in` - + "order to allow the interceptor to be loaded.") + logger.warn(` + Could not obtain the Port value of Lunar Proxy from environment variables, + please set ${PROXY_HOST_KEY} to the Lunar Proxy's host/IP and port in + order to allow the interceptor to be loaded.`) proxyHost = "null" } else { @@ -44,9 +44,8 @@ export function loadConnectionInformation(): ConnectionInformation { } } else { - logger.warn("Could not obtain the Host value of Lunar Proxy from environment variables," - + `please set ${PROXY_HOST_KEY} to the Lunar Proxy's host/IP and port in` - + "order to allow the interceptor to be loaded.") + logger.warn(`Could not obtain the Host value of Lunar Proxy from environment variables, + please set ${PROXY_HOST_KEY} to the Lunar Proxy's host/IP and port in order to allow the interceptor to be loaded.`) } const handShakePort: number = loadNumberFromEnv(HEALTH_CHECK_PORT_KEY, PROXY_DEFAULT_HEALTHCHECK_PORT) @@ -153,32 +152,35 @@ export async function makeProxyConnection(): Promise { return connectionInfo } -export function getUrlImport(): Module | null { +export function getEngineVersion(): EngineVersion | null { const version = process.versions.node.split('.') - - if (version.length === 0) { - logger.error("Could not determine the version of NodeJS, Lunar Interceptor is disabled.") + if (version.length <= 2) { return null } + const major = version[0] - let urlModule: Module | null = null + const minor = version[1] + const fix = version[2] - if (major !== undefined) { - const majorInt = parseInt(major) + if (major === undefined || minor === undefined || fix === undefined) { + return null + } - if (majorInt > LEGACY_URL_MODULE_VERSION) { - urlModule = require('node:url'); // New import - } else { - urlModule = require('url'); // Legacy import - } + return new EngineVersion(parseInt(major), parseInt(minor), parseInt(fix)) +} - // @ts-expect-error: TS2339 - if (typeof urlModule.urlToHttpOptions === 'undefined') { - logger.error("Could not find required `urlToHttpOptions` function, Lunar Interceptor is disabled.") - return null - } +export function isEngineVersionSupported(engineVersion: EngineVersion): boolean { + + if (engineVersion === null) { + logger.error("Could not determine the version of NodeJS, Lunar Interceptor is disabled.") + return false } - return urlModule + + if (engineVersion.isEqualOrGreaterThan(new EngineVersion(16, 0, 0))) return true + + return engineVersion.inRange(new EngineVersion(15, 7, 0), new EngineVersion(16, 0 ,0)) || + engineVersion.inRange(new EngineVersion(14, 18, 0), new EngineVersion(15, 0 ,0)) + } export function popHeaderValue(key: string, headers?: NodeJS.Dict): string | number | string[] | undefined { @@ -190,4 +192,4 @@ export function popHeaderValue(key: string, headers?: NodeJS.Dict void } @@ -286,7 +282,7 @@ class LunarInterceptor { private manipulateHeaders(options: RequestOptions, url: URL): void { if (options.headers == null) options.headers = {} - + options.headers.host = url.host options.headers['x-lunar-interceptor'] = this._proxyConnInfo.interceptorID options.headers['x-lunar-scheme'] = url.protocol.substring(0, url.protocol.length - 1) diff --git a/interceptors/lunar-ts-interceptor/src/logger.ts b/interceptors/lunar-ts-interceptor/src/logger.ts index 55248560..fa891a6c 100644 --- a/interceptors/lunar-ts-interceptor/src/logger.ts +++ b/interceptors/lunar-ts-interceptor/src/logger.ts @@ -5,7 +5,7 @@ export const logger = createLogger({ level: loadStrFromEnv("LUNAR_INTERCEPTOR_LOG_LEVEL", "info").toLowerCase(), format: format.combine( format.timestamp(), - format.printf(({ timestamp, level, message }) => { + format.printf(({ timestamp, level, message }) => { return `${timestamp} - lunar-interceptor - ${level.toUpperCase()}: ${message}`; }), ), diff --git a/interceptors/lunar-ts-interceptor/src/lunarObjects.ts b/interceptors/lunar-ts-interceptor/src/lunarObjects.ts index ae87f552..a81adfbb 100644 --- a/interceptors/lunar-ts-interceptor/src/lunarObjects.ts +++ b/interceptors/lunar-ts-interceptor/src/lunarObjects.ts @@ -1,6 +1,6 @@ import { type RequestOptions, type request as httpRequest, type get as httpGet, IncomingMessage, ClientRequest } from "http" import { type request as httpsRequest, type get as httpsGet } from 'https' -import { Socket } from 'node:net'; +import { Socket } from 'net'; export interface LunarOptions extends RequestOptions { href?: URL | null | undefined diff --git a/interceptors/lunar-ts-interceptor/test-client/http_client.js b/interceptors/lunar-ts-interceptor/test-client/http_client.js index a4df21ad..1846de66 100644 --- a/interceptors/lunar-ts-interceptor/test-client/http_client.js +++ b/interceptors/lunar-ts-interceptor/test-client/http_client.js @@ -2,6 +2,7 @@ const express = require('express'); require("../dist/index"); const http = require('http'); +const urlModule = require('url'); const { exit } = require('process'); const app = express(); app.use(express.json()) @@ -10,21 +11,6 @@ const HTTPBINMOCK_HOST = "httpbinmock" const HTTPBINMOCK_BASE_URL = `http://${HTTPBINMOCK_HOST}` const HEALTHCHECK_RESPONSE = { "status": "OK" } -function getUrlImport() { - const version = process.versions.node.split('.') - - if (version.length === 0) { - console.log("Could not determine the version of NodeJS") - exit(1) - } - - const major = version[0] - - if (major !== undefined) { - if (parseInt(major) > 15) return require('node:url'); - } - return require('url'); -} function _trigger(req, res) { console.log("_trigger") @@ -109,7 +95,7 @@ function _trigger_dynamic(req, res) { const method = req.query.method const url = req.query.url - const options = getUrlImport().urlToHttpOptions(url) + const options = urlModule.urlToHttpOptions(url) options.method = method var clientReq = http.request(options, (resp) => {