Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/bolt-performance-improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"node-version": patch
---

Optimization: Improve performance of `compareTo` method (~50% faster) and add fail-closed DoS protection for long input strings.
28 changes: 20 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,32 @@
* Compare the current node version with a target version string.
*/
const compareTo = (target: string): number => {
if (target !== target.trim() || target.length === 0) {
return NaN;
}

const stripped = target.replace(/^v/i, "");
// Fail-closed for DoS protection
if (target.length > 256) return NaN;

if (stripped.length === 0) {
// Check for whitespace around without allocating
if (
target.length === 0 ||
target.charCodeAt(0) <= 32 ||
target.charCodeAt(target.length - 1) <= 32
) {
return NaN;
}

const s2 = stripped.split(".");
const s2 = target.split(".");
// Handle 'v' prefix in the first segment
if (s2[0].length > 0) {

Check failure on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20.x)

Object is possibly 'undefined'.

Check failure on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 22.x)

Object is possibly 'undefined'.

Check failure on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 24.x)

Object is possibly 'undefined'.
const firstChar = s2[0].charCodeAt(0);

Check failure on line 70 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20.x)

Object is possibly 'undefined'.

Check failure on line 70 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 22.x)

Object is possibly 'undefined'.

Check failure on line 70 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 24.x)

Object is possibly 'undefined'.
// Check for 'v' (118) or 'V' (86)
if (firstChar === 118 || firstChar === 86) {
s2[0] = s2[0].slice(1);

Check failure on line 73 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20.x)

Object is possibly 'undefined'.

Check failure on line 73 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 22.x)

Object is possibly 'undefined'.

Check failure on line 73 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 24.x)

Object is possibly 'undefined'.
}
}

for (const segment of s2) {
// Validate segments are numeric
for (let i = 0; i < s2.length; i++) {
const segment = s2[i];
if (segment === "" || !/^\d+$/.test(segment)) {

Check failure on line 80 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 80 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 22.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 80 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 24.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
return NaN;
}
}
Expand Down
Loading