diff --git a/package.json b/package.json index ca40f1e..234c07f 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "scripts": { "build": "tsc", "build-worker": "node worker/build.js", - "test": "npm run-script build-worker && node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose --collectCoverage --coverageProvider=v8", - "test-debug": "npm run-script build-worker && node --experimental-vm-modules --inspect-brk node_modules/jest/bin/jest.js --runInBand", + "test": "npm run-script build-worker && node --no-experimental-fetch --experimental-vm-modules node_modules/jest/bin/jest.js --verbose --collectCoverage --coverageProvider=v8", + "test-debug": "npm run-script build-worker && node --no-experimental-fetch --experimental-vm-modules --inspect-brk node_modules/jest/bin/jest.js --runInBand", "format": "prettier --write '*.{json,js}' 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'", "lint": "eslint --max-warnings=0 src && prettier --check '*.{json,js}' 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'", "prepack": "npm run-script test && npm run-script lint && npm run-script build", @@ -111,4 +111,4 @@ "dependencies": { "worktop": "0.7.3" } -} +} \ No newline at end of file diff --git a/src/processConditionals.ts b/src/processConditionals.ts index 0a381d1..2a8beaa 100644 --- a/src/processConditionals.ts +++ b/src/processConditionals.ts @@ -125,16 +125,25 @@ function esi_eval_var_in_when_tag( match: [String: string, ...args: string[]], ): string { const varInTag = esi_eval_var(eventData, match); - - const number = parseInt(varInTag, 10); - if (number) { - return number.toString(); + // we have to check varInTag is *actually* a number and doesn't just have leading numbers in it + if (strIsNumber(varInTag)) { + return parseInt(varInTag, 10).toString(); } else { // Change to replaceAll once upgraded node return "'" + varInTag.replace(/'/g, "\\'") + "'"; } } +/** + * Evaluates str and returns if it *actually* is a number not just a str with leading numbers + * + * @param {string} str string to check + * @returns {boolean} check result + */ +function strIsNumber(str: string): boolean { + return !isNaN(Number(str)) +} + /** * Takes a condition string and splits it into its two sides and operator * passes that to the tester and returns the result diff --git a/test/esi.spec.ts b/test/esi.spec.ts index f0d97c5..9c4ce7b 100644 --- a/test/esi.spec.ts +++ b/test/esi.spec.ts @@ -579,6 +579,7 @@ test("TEST 9e: List variable syntax (accept-language) with multiple headers", as ); const res = await makeRequest(`${url}?t=1`, { + // @ts-ignore headers: { // @ts-ignore "Accept-Language": ["da, en-gb", "fr"], @@ -1063,6 +1064,7 @@ test("TEST 20b: Test we advertise Surrogate-Capability (with COLO Data)", async res.writeHead(200, esiHead); res.end(req.headers["surrogate-capability"]); }); + // @ts-ignore const res = await makeRequest(url, { cf: { colo: "DFW" } }); expect(res.ok).toBeTruthy(); expect(checkSurrogate(res)).toBeTruthy(); @@ -1911,3 +1913,33 @@ test("TEST 50: Multiple ESI Args make it all the way through", async () => { `esi_args1=1&esi_args2=2&esi_args3=3&esi_args4=4`, ); }); + +describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () => { + const checks = [ + { arg: "1719,2405", result: "first-lineage" }, + { arg: "1719,1732", result: "second-lineage" }, + { arg: "1719,1918", result: "third-lineage" }, + { arg: "1719,1922", result: "forth-lineage" }, + { arg: "1719,1926", result: "fith-lineage" }, + ]; + + const url = `/esi/test-51`; + checks.forEach(function (check) { + it(check.result, async () => { + routeHandler.add(url, function (req, res) { + res.writeHead(200, esiHead); + res.end(` + first-lineage + second-lineage + third-lineage + forth-lineage + fith-lineage +`); + }); + const res = await makeRequest(`${url}?esi_lineage=${check.arg}`); + expect(res.ok).toBeTruthy(); + expect(await res.text()).toEqual(check.result); + expect(checkSurrogate(res)).toBeTruthy(); + }); + }); +});