@tsrx/core 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -4
- package/src/index.js +2 -1
- package/src/parse/style.js +2 -2
- package/src/utils/hashing.js +30 -2
- package/src/utils.js +1 -1
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Core compiler infrastructure for TSRX syntax",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Dominic Gannaway",
|
|
6
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.10",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
@@ -32,14 +32,15 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@jridgewell/sourcemap-codec": "^1.5.5",
|
|
35
|
+
"@noble/hashes": "^2.2.0",
|
|
35
36
|
"@sveltejs/acorn-typescript": "^1.0.9",
|
|
37
|
+
"@types/estree-jsx": "^1.0.5",
|
|
38
|
+
"@types/estree": "^1.0.8",
|
|
36
39
|
"acorn": "^8.15.0",
|
|
37
40
|
"esrap": "^2.1.0",
|
|
38
41
|
"is-reference": "^3.0.3",
|
|
39
42
|
"magic-string": "^0.30.18",
|
|
40
|
-
"zimmerframe": "^1.1.2"
|
|
41
|
-
"@types/estree": "^1.0.8",
|
|
42
|
-
"@types/estree-jsx": "^1.0.5"
|
|
43
|
+
"zimmerframe": "^1.1.2"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@types/node": "^24.3.0",
|
package/src/index.js
CHANGED
package/src/parse/style.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** @import * as AST from 'estree' */
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { simple_hash } from '../utils/hashing.js';
|
|
4
4
|
|
|
5
5
|
const REGEX_MATCHER = /^[~^$*|]?=/;
|
|
6
6
|
const REGEX_ATTRIBUTE_FLAGS = /^[a-zA-Z]+/;
|
|
@@ -119,7 +119,7 @@ export function parse_style(content, options) {
|
|
|
119
119
|
|
|
120
120
|
return {
|
|
121
121
|
source: content,
|
|
122
|
-
hash: `tsrx-${
|
|
122
|
+
hash: `tsrx-${simple_hash(content)}`,
|
|
123
123
|
type: 'StyleSheet',
|
|
124
124
|
children: read_body(parser),
|
|
125
125
|
start: 0,
|
package/src/utils/hashing.js
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
2
|
+
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';
|
|
3
|
+
|
|
1
4
|
const regex_return_characters = /\r/g;
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
|
-
*
|
|
7
|
+
* Fast non-cryptographic string hash (djb2, base36).
|
|
8
|
+
*
|
|
9
|
+
* Cheap and small, producing 4–7 chars — good for high-volume identifiers like
|
|
10
|
+
* CSS class-name prefixes where the output multiplies across every scoped rule
|
|
11
|
+
* and DOM reference in the shipped bundle. Trivially reversible for short
|
|
12
|
+
* inputs, so never use this for hashes derived from server-only data that
|
|
13
|
+
* ships to the client (absolute file paths, function ids, etc.) — use
|
|
14
|
+
* {@link strong_hash} for those.
|
|
5
15
|
* @param {string} str
|
|
6
16
|
* @returns {string}
|
|
7
17
|
*/
|
|
8
|
-
export function
|
|
18
|
+
export function simple_hash(str) {
|
|
9
19
|
str = str.replace(regex_return_characters, '');
|
|
10
20
|
let hash = 5381;
|
|
11
21
|
let i = str.length;
|
|
@@ -13,3 +23,21 @@ export function hash(str) {
|
|
|
13
23
|
while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
|
|
14
24
|
return (hash >>> 0).toString(36);
|
|
15
25
|
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Cryptographic string hash — 8-char hex SHA-256 prefix.
|
|
29
|
+
*
|
|
30
|
+
* We use a pure-JS SHA-256 so this runs in browser workers (e.g. Monaco)
|
|
31
|
+
* without a `node:crypto` dependency.
|
|
32
|
+
*
|
|
33
|
+
* SHA-256 is pre-image-resistant, so a hash emitted into a client bundle (e.g.
|
|
34
|
+
* an RPC id derived from an absolute server-file path) can't be inverted to
|
|
35
|
+
* recover the original path. An attacker with a list of candidate paths could
|
|
36
|
+
* still confirm a guess by rehashing — the 8-char truncation keeps these ids
|
|
37
|
+
* short and is fine for identification, not for authentication.
|
|
38
|
+
* @param {string} str
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
export function strong_hash(str) {
|
|
42
|
+
return bytesToHex(sha256(utf8ToBytes(str.replace(regex_return_characters, '')))).slice(0, 8);
|
|
43
|
+
}
|