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

fix: handles esi Args that are strings but have leading integers #271

Merged
merged 1 commit into from
Jun 11, 2024
Merged
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
17 changes: 4 additions & 13 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -110,5 +110,8 @@
},
"dependencies": {
"worktop": "0.7.3"
},
"overrides": {
"@types/node": "20.8.3"
}
}
17 changes: 13 additions & 4 deletions src/processConditionals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test/esi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1911,3 +1911,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(`<esi:choose>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(2405)(?:,|$)/'">first-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1732)(?:,|$)/'">second-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1918)(?:,|$)/'">third-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1922)(?:,|$)/'">forth-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1926)(?:,|$)/'">fith-lineage</esi:when>
</esi:choose>`);
});
const res = await makeRequest(`${url}?esi_lineage=${check.arg}`);
expect(res.ok).toBeTruthy();
expect(await res.text()).toEqual(check.result);
expect(checkSurrogate(res)).toBeTruthy();
});
});
});
Loading