Skip to content

Commit 93a4771

Browse files
authored
Merge pull request #350 from NodeSecure/sec-literal-ts
refactor: migrate sec-literal to TypeScript
2 parents bf3d68c + b92c262 commit 93a4771

24 files changed

Lines changed: 193 additions & 128 deletions

.github/workflows/estree-ast-utils.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323

2424
strategy:
2525
matrix:
26-
node-version: [20.x, 22.x]
26+
node-version: [22.x, 24.x]
2727
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2828

2929
steps:

.github/workflows/node.js.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
node-version: [20.x, 22.x]
16+
node-version: [22.x, 24.x]
1717
fail-fast: false
1818
steps:
1919
- name: Harden Runner
@@ -28,6 +28,8 @@ jobs:
2828
node-version: ${{ matrix.node-version }}
2929
- name: Install dependencies
3030
run: npm install
31+
- name: Build typescript sources
32+
run: npm run build
3133
- name: Run tests
3234
run: npm run test
3335
- name: Send coverage report to Codecov

.github/workflows/sec-literal.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
node-version: [20.x, 22.x]
18+
node-version: [22.x, 24.x]
1919
fail-fast: false
2020
steps:
2121
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -24,4 +24,5 @@ jobs:
2424
with:
2525
node-version: ${{ matrix.node-version }}
2626
- run: npm install
27+
- run: npm run build
2728
- run: npm run test --workspace=workspaces/sec-literal

.github/workflows/ts-source-parser.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323

2424
strategy:
2525
matrix:
26-
node-version: [20.x, 22.x]
26+
node-version: [22.x, 24.x]
2727
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2828

2929
steps:
@@ -38,4 +38,5 @@ jobs:
3838
with:
3939
node-version: ${{ matrix.node-version }}
4040
- run: npm install
41+
- run: npm run build
4142
- run: npm run test --workspace=workspaces/ts-source-parser

base.tsconfig.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"node": ">=20.0.0"
1919
},
2020
"scripts": {
21+
"build": "tsc -b",
2122
"lint": "eslint src workspaces test",
23+
"test-all": "npm run test --ws --if-present",
2224
"test-only": "glob -c \"node --test-reporter=spec --test\" \"./test/**/*.spec.js\"",
2325
"test": "c8 --all --src ./src -r html npm run test-only",
2426
"check": "npm run lint && npm run test-only",

tsconfig.base.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@openally/config.typescript"
3+
}

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{
5+
"path": "./workspaces/sec-literal"
6+
},
7+
{
8+
"path": "./workspaces/ts-source-parser"
9+
}
10+
]
11+
}

workspaces/sec-literal/package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"name": "@nodesecure/sec-literal",
33
"version": "1.2.0",
44
"description": "Package created to analyze JavaScript literals",
5-
"exports": "./src/index.js",
5+
"exports": "./dist/index.js",
66
"private": false,
77
"type": "module",
88
"scripts": {
9-
"lint": "eslint src",
10-
"test-only": "node --test",
11-
"test": "npm run lint && npm run test-only"
9+
"prepublishOnly": "npm run build",
10+
"build": "tsc",
11+
"test-only": "tsx --test-reporter=spec --test \"./test/**/*.spec.ts\"",
12+
"test": "npm run test-only"
1213
},
1314
"repository": {
1415
"type": "git",
@@ -22,7 +23,7 @@
2223
"scanner"
2324
],
2425
"files": [
25-
"src"
26+
"dist"
2627
],
2728
"author": "GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>",
2829
"license": "MIT",
@@ -35,5 +36,8 @@
3536
"is-base64": "^1.1.0",
3637
"is-svg": "^6.0.0",
3738
"string-width": "^7.0.0"
39+
},
40+
"devDependencies": {
41+
"@types/is-base64": "^1.1.3"
3842
}
3943
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Import Internal Dependencies
2-
import * as Literal from "./literal.js";
2+
import { toRaw, toValue, type ESTreeLiteral } from "./literal.js";
33
import * as Utils from "./utils.js";
44

5+
// CONSTANTS
56
const kUnsafeHexValues = new Set([
67
"require",
78
"length"
89
].map((value) => Buffer.from(value).toString("hex")));
9-
10-
// CONSTANTS
1110
const kSafeHexValues = new Set([
1211
"0123456789",
1312
"123456789",
@@ -24,30 +23,36 @@ export const CONSTANTS = Object.freeze({
2423

2524
/**
2625
* @description detect if the given string is an Hexadecimal value
27-
* @param {SecLiteral.Literal | string} anyValue
28-
* @returns {boolean}
2926
*/
30-
export function isHex(anyValue) {
31-
const value = Literal.toValue(anyValue);
27+
export function isHex(
28+
anyValue: ESTreeLiteral | string
29+
): boolean {
30+
const value = toValue(anyValue);
3231

3332
return typeof value === "string" && /^[0-9A-Fa-f]{4,}$/g.test(value);
3433
}
3534

3635
/**
3736
* @description detect if the given string is a safe Hexadecimal value
38-
* @param {SecLiteral.Literal | string} anyValue
39-
* @returns {boolean}
4037
*/
41-
export function isSafe(anyValue) {
42-
const rawValue = Literal.toRaw(anyValue);
43-
if (kUnsafeHexValues.has(rawValue)) {
38+
export function isSafe(
39+
anyValue: ESTreeLiteral | string
40+
): boolean {
41+
const rawValue = toRaw(anyValue);
42+
if (typeof rawValue === "undefined" || kUnsafeHexValues.has(rawValue)) {
4443
return false;
4544
}
4645

4746
const charCount = Utils.stringCharDiversity(rawValue);
48-
if (/^([0-9]+|[a-z]+|[A-Z]+)$/g.test(rawValue) || rawValue.length <= 5 || charCount <= 2) {
47+
if (
48+
/^([0-9]+|[a-z]+|[A-Z]+)$/g.test(rawValue)
49+
|| rawValue.length <= 5
50+
|| charCount <= 2
51+
) {
4952
return true;
5053
}
5154

52-
return [...kSafeHexValues].some((value) => rawValue.toLowerCase().startsWith(value));
55+
return [...kSafeHexValues].some(
56+
(value) => rawValue.toLowerCase().startsWith(value)
57+
);
5358
}

0 commit comments

Comments
 (0)