unpeek 1.0.1 → 1.2.0

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.
Files changed (102) hide show
  1. package/README.md +131 -60
  2. package/README.tr.md +317 -0
  3. package/dist/ast/index.cjs +802 -0
  4. package/dist/ast/index.cjs.map +1 -0
  5. package/dist/ast/index.d.cts +9 -0
  6. package/dist/ast/index.d.ts +9 -4
  7. package/dist/ast/index.js +762 -52
  8. package/dist/ast/index.js.map +1 -0
  9. package/dist/browser.cjs +816 -0
  10. package/dist/browser.cjs.map +1 -0
  11. package/dist/browser.d.cts +3 -0
  12. package/dist/browser.d.ts +3 -0
  13. package/dist/browser.js +773 -0
  14. package/dist/browser.js.map +1 -0
  15. package/dist/bundler/detector.cjs +180 -0
  16. package/dist/bundler/detector.cjs.map +1 -0
  17. package/dist/bundler/detector.d.cts +9 -0
  18. package/dist/bundler/detector.d.ts +6 -3
  19. package/dist/bundler/detector.js +130 -135
  20. package/dist/bundler/detector.js.map +1 -0
  21. package/dist/bundler/sourcemap-vault.cjs +210 -0
  22. package/dist/bundler/sourcemap-vault.cjs.map +1 -0
  23. package/dist/bundler/sourcemap-vault.d.cts +6 -0
  24. package/dist/bundler/sourcemap-vault.d.ts +6 -2
  25. package/dist/bundler/sourcemap-vault.js +161 -85
  26. package/dist/bundler/sourcemap-vault.js.map +1 -0
  27. package/dist/bundler/unplugin.cjs +848 -0
  28. package/dist/bundler/unplugin.cjs.map +1 -0
  29. package/dist/bundler/unplugin.d.cts +14 -0
  30. package/dist/bundler/unplugin.d.ts +14 -7
  31. package/dist/bundler/unplugin.js +805 -51
  32. package/dist/bundler/unplugin.js.map +1 -0
  33. package/dist/cli/index.cjs +1361 -0
  34. package/dist/cli/index.cjs.map +1 -0
  35. package/dist/cli/index.d.cts +3 -0
  36. package/dist/cli/index.d.ts +3 -1
  37. package/dist/cli/index.js +1309 -94
  38. package/dist/cli/index.js.map +1 -0
  39. package/dist/cli/init.cjs +200 -0
  40. package/dist/cli/init.cjs.map +1 -0
  41. package/dist/cli/init.d.cts +13 -0
  42. package/dist/cli/init.d.ts +10 -2
  43. package/dist/cli/init.js +162 -46
  44. package/dist/cli/init.js.map +1 -0
  45. package/dist/cli/protect.cjs +1147 -0
  46. package/dist/cli/protect.cjs.map +1 -0
  47. package/dist/cli/protect.d.cts +5 -0
  48. package/dist/cli/protect.d.ts +5 -2
  49. package/dist/cli/protect.js +1107 -83
  50. package/dist/cli/protect.js.map +1 -0
  51. package/dist/esbuild.cjs +844 -0
  52. package/dist/esbuild.cjs.map +1 -0
  53. package/dist/esbuild.d.cts +11 -0
  54. package/dist/esbuild.d.ts +11 -0
  55. package/dist/esbuild.js +807 -0
  56. package/dist/esbuild.js.map +1 -0
  57. package/dist/index.cjs +1453 -0
  58. package/dist/index.cjs.map +1 -0
  59. package/dist/index.d.cts +14 -0
  60. package/dist/index.d.ts +9 -4
  61. package/dist/index.js +1395 -8
  62. package/dist/index.js.map +1 -0
  63. package/dist/rollup.cjs +844 -0
  64. package/dist/rollup.cjs.map +1 -0
  65. package/dist/rollup.d.cts +11 -0
  66. package/dist/rollup.d.ts +11 -0
  67. package/dist/rollup.js +807 -0
  68. package/dist/rollup.js.map +1 -0
  69. package/dist/runtime/index.cjs +88 -0
  70. package/dist/runtime/index.cjs.map +1 -0
  71. package/dist/runtime/index.d.cts +23 -0
  72. package/dist/runtime/index.d.ts +23 -4
  73. package/dist/runtime/index.js +57 -15
  74. package/dist/runtime/index.js.map +1 -0
  75. package/dist/{types.d.ts → types-BTMkXmVE.d.cts} +14 -12
  76. package/dist/types-BTMkXmVE.d.ts +73 -0
  77. package/dist/vite.cjs +844 -0
  78. package/dist/vite.cjs.map +1 -0
  79. package/dist/vite.d.cts +11 -0
  80. package/dist/vite.d.ts +11 -0
  81. package/dist/vite.js +807 -0
  82. package/dist/vite.js.map +1 -0
  83. package/dist/webpack.cjs +844 -0
  84. package/dist/webpack.cjs.map +1 -0
  85. package/dist/webpack.d.cts +11 -0
  86. package/dist/webpack.d.ts +11 -0
  87. package/dist/webpack.js +807 -0
  88. package/dist/webpack.js.map +1 -0
  89. package/package.json +104 -7
  90. package/dist/ast/cold-flow-guard.d.ts +0 -2
  91. package/dist/ast/cold-flow-guard.js +0 -39
  92. package/dist/ast/hot-path-detector.d.ts +0 -10
  93. package/dist/ast/hot-path-detector.js +0 -235
  94. package/dist/ast/polymorphic-mangler.d.ts +0 -2
  95. package/dist/ast/polymorphic-mangler.js +0 -270
  96. package/dist/ast/string-vault.d.ts +0 -7
  97. package/dist/ast/string-vault.js +0 -139
  98. package/dist/runtime/devtools-deterrent.d.ts +0 -7
  99. package/dist/runtime/devtools-deterrent.js +0 -27
  100. package/dist/runtime/integrity-guard.d.ts +0 -11
  101. package/dist/runtime/integrity-guard.js +0 -25
  102. package/dist/types.js +0 -1
@@ -1,270 +0,0 @@
1
- import * as walk from 'acorn-walk';
2
- import { computeFnv1aHash } from './string-vault.js';
3
- const RESERVED_IDENTIFIERS = new Set([
4
- 'arguments',
5
- 'undefined',
6
- 'NaN',
7
- 'Infinity',
8
- 'window',
9
- 'document',
10
- 'globalThis',
11
- 'self',
12
- 'console',
13
- 'Math',
14
- 'JSON',
15
- 'Object',
16
- 'Array',
17
- 'String',
18
- 'Number',
19
- 'Boolean',
20
- 'Symbol',
21
- 'BigInt',
22
- 'Promise',
23
- 'Map',
24
- 'Set',
25
- 'WeakMap',
26
- 'WeakSet',
27
- 'Error',
28
- 'TypeError',
29
- 'RangeError',
30
- 'SyntaxError',
31
- 'RegExp',
32
- 'Date',
33
- 'Reflect',
34
- 'Proxy',
35
- 'Intl',
36
- 'eval',
37
- 'process',
38
- 'Buffer',
39
- 'require',
40
- 'module',
41
- 'exports',
42
- 'React',
43
- ]);
44
- const FUNCTION_TYPES = new Set([
45
- 'FunctionDeclaration',
46
- 'FunctionExpression',
47
- 'ArrowFunctionExpression',
48
- ]);
49
- function collectPatternIdentifiers(pattern, out) {
50
- if (!pattern)
51
- return;
52
- switch (pattern.type) {
53
- case 'Identifier':
54
- out.push(pattern);
55
- break;
56
- case 'AssignmentPattern':
57
- collectPatternIdentifiers(pattern.left, out);
58
- break;
59
- case 'RestElement':
60
- collectPatternIdentifiers(pattern.argument, out);
61
- break;
62
- case 'ArrayPattern':
63
- for (const el of pattern.elements || []) {
64
- if (el)
65
- collectPatternIdentifiers(el, out);
66
- }
67
- break;
68
- case 'ObjectPattern':
69
- for (const prop of pattern.properties || []) {
70
- if (prop.type === 'Property') {
71
- if (prop.shorthand) {
72
- prop.shorthand = false;
73
- prop.key = { ...prop.key };
74
- }
75
- collectPatternIdentifiers(prop.value, out);
76
- }
77
- else if (prop.type === 'RestElement') {
78
- collectPatternIdentifiers(prop.argument, out);
79
- }
80
- }
81
- break;
82
- }
83
- }
84
- export function applyPolymorphicMangler(ast, seed = 0x9e3779b9) {
85
- const scopeMap = new Map();
86
- const scopeDepthMap = new Map();
87
- let counter = 0;
88
- function makeScopedMangledName(scopeDepth, localIndex, usedValues) {
89
- counter++;
90
- let salt = 0;
91
- while (true) {
92
- const h = computeFnv1aHash(`s${scopeDepth}:i${localIndex}:${salt}`, seed);
93
- const candidate = `_0x${((h & 0x1f) + localIndex + salt).toString(36)}`;
94
- if (!usedValues.has(candidate)) {
95
- return candidate;
96
- }
97
- salt++;
98
- }
99
- }
100
- function countFunctionDepth(ancestors) {
101
- let depth = 0;
102
- for (let i = 0; i < ancestors.length; i++) {
103
- if (FUNCTION_TYPES.has(ancestors[i].type)) {
104
- depth++;
105
- }
106
- }
107
- return Math.max(1, depth);
108
- }
109
- function findEnclosingFunction(ancestors) {
110
- for (let i = ancestors.length - 1; i >= 0; i--) {
111
- if (FUNCTION_TYPES.has(ancestors[i].type)) {
112
- return ancestors[i];
113
- }
114
- }
115
- return null;
116
- }
117
- function registerBindingAndMutateDeclSite(idNode, bindings, scopeDepth) {
118
- const origName = idNode.name;
119
- if (RESERVED_IDENTIFIERS.has(origName) || origName.startsWith('_u') || origName.startsWith('_0x')) {
120
- return;
121
- }
122
- let mangled = bindings.get(origName);
123
- if (!mangled) {
124
- const usedValues = new Set(bindings.values());
125
- mangled = makeScopedMangledName(scopeDepth, bindings.size, usedValues);
126
- bindings.set(origName, mangled);
127
- }
128
- idNode.name = mangled;
129
- }
130
- function registerFunctionParams(fnNode, ancestors) {
131
- let bindings = scopeMap.get(fnNode);
132
- const depth = countFunctionDepth(ancestors);
133
- scopeDepthMap.set(fnNode, depth);
134
- if (!bindings) {
135
- bindings = new Map();
136
- scopeMap.set(fnNode, bindings);
137
- }
138
- for (const param of fnNode.params || []) {
139
- const idNodes = [];
140
- collectPatternIdentifiers(param, idNodes);
141
- for (const idNode of idNodes) {
142
- registerBindingAndMutateDeclSite(idNode, bindings, depth);
143
- }
144
- }
145
- }
146
- // Pass 1: Collect and rename local function parameters and local variable declarations inside functions
147
- walk.ancestor(ast, {
148
- FunctionDeclaration(node, _state, ancestors) {
149
- registerFunctionParams(node, ancestors);
150
- },
151
- FunctionExpression(node, _state, ancestors) {
152
- registerFunctionParams(node, ancestors);
153
- },
154
- ArrowFunctionExpression(node, _state, ancestors) {
155
- registerFunctionParams(node, ancestors);
156
- },
157
- VariableDeclaration(node, _state, ancestors) {
158
- const fnScope = findEnclosingFunction(ancestors);
159
- if (!fnScope)
160
- return; // Never mangle top-level / module-scope variables
161
- let bindings = scopeMap.get(fnScope);
162
- const depth = scopeDepthMap.get(fnScope) ?? countFunctionDepth(ancestors);
163
- if (!bindings) {
164
- bindings = new Map();
165
- scopeMap.set(fnScope, bindings);
166
- }
167
- for (const decl of node.declarations || []) {
168
- const idNodes = [];
169
- collectPatternIdentifiers(decl.id, idNodes);
170
- for (const idNode of idNodes) {
171
- registerBindingAndMutateDeclSite(idNode, bindings, depth);
172
- }
173
- }
174
- },
175
- });
176
- if (counter === 0)
177
- return 0;
178
- // Pass 2: Rename all matching identifier references and assignment LHS patterns within their lexical function scope
179
- walk.ancestor(ast, {
180
- AssignmentExpression(node, _state, ancestors) {
181
- const idNodes = [];
182
- collectPatternIdentifiers(node.left, idNodes);
183
- for (const idNode of idNodes) {
184
- const resolved = resolveMangledName(idNode.name, ancestors, scopeMap);
185
- if (resolved) {
186
- idNode.name = resolved;
187
- }
188
- }
189
- },
190
- ForInStatement(node, _state, ancestors) {
191
- if (node.left && node.left.type !== 'VariableDeclaration') {
192
- const idNodes = [];
193
- collectPatternIdentifiers(node.left, idNodes);
194
- for (const idNode of idNodes) {
195
- const resolved = resolveMangledName(idNode.name, ancestors, scopeMap);
196
- if (resolved)
197
- idNode.name = resolved;
198
- }
199
- }
200
- },
201
- ForOfStatement(node, _state, ancestors) {
202
- if (node.left && node.left.type !== 'VariableDeclaration') {
203
- const idNodes = [];
204
- collectPatternIdentifiers(node.left, idNodes);
205
- for (const idNode of idNodes) {
206
- const resolved = resolveMangledName(idNode.name, ancestors, scopeMap);
207
- if (resolved)
208
- idNode.name = resolved;
209
- }
210
- }
211
- },
212
- Property(node, _state, ancestors) {
213
- if (node.shorthand && node.value?.type === 'Identifier') {
214
- const resolved = resolveMangledName(node.value.name, ancestors, scopeMap);
215
- if (resolved) {
216
- node.shorthand = false;
217
- node.key = { type: 'Identifier', name: node.value.name };
218
- }
219
- }
220
- },
221
- Identifier(node, _state, ancestors) {
222
- const parent = ancestors[ancestors.length - 2];
223
- if (!parent)
224
- return;
225
- if ((parent.type === 'FunctionDeclaration' ||
226
- parent.type === 'FunctionExpression' ||
227
- parent.type === 'ClassDeclaration' ||
228
- parent.type === 'ClassExpression') &&
229
- parent.id === node) {
230
- return;
231
- }
232
- if (parent.type === 'MemberExpression' &&
233
- parent.property === node &&
234
- !parent.computed) {
235
- return;
236
- }
237
- if ((parent.type === 'Property' ||
238
- parent.type === 'MethodDefinition' ||
239
- parent.type === 'PropertyDefinition') &&
240
- parent.key === node &&
241
- !parent.computed) {
242
- return;
243
- }
244
- if (parent.type === 'LabeledStatement' ||
245
- parent.type === 'BreakStatement' ||
246
- parent.type === 'ContinueStatement' ||
247
- parent.type === 'ImportSpecifier' ||
248
- parent.type === 'ImportDefaultSpecifier' ||
249
- parent.type === 'ImportNamespaceSpecifier' ||
250
- parent.type === 'ExportSpecifier') {
251
- return;
252
- }
253
- const resolved = resolveMangledName(node.name, ancestors, scopeMap);
254
- if (resolved) {
255
- node.name = resolved;
256
- }
257
- },
258
- });
259
- return counter;
260
- }
261
- function resolveMangledName(name, ancestors, scopeMap) {
262
- for (let i = ancestors.length - 1; i >= 0; i--) {
263
- const anc = ancestors[i];
264
- const bindings = scopeMap.get(anc);
265
- if (bindings && bindings.has(name)) {
266
- return bindings.get(name);
267
- }
268
- }
269
- return null;
270
- }
@@ -1,7 +0,0 @@
1
- import * as acorn from 'acorn';
2
- export declare function computeFnv1aHash(input: string, seed?: number): number;
3
- export interface StringVaultResult {
4
- encryptedStringsCount: number;
5
- integrityToken: number;
6
- }
7
- export declare function applyStringVault(ast: acorn.Program, seed?: number, minStringLength?: number): StringVaultResult;
@@ -1,139 +0,0 @@
1
- import * as acorn from 'acorn';
2
- import * as walk from 'acorn-walk';
3
- const DIRECTIVE_STRINGS = new Set([
4
- 'use strict',
5
- 'use client',
6
- 'use server',
7
- 'use asm',
8
- ]);
9
- export function computeFnv1aHash(input, seed = 0x811c9dc5) {
10
- let hash = seed >>> 0;
11
- for (let i = 0; i < input.length; i++) {
12
- hash ^= input.charCodeAt(i);
13
- hash = Math.imul(hash, 0x01000193) >>> 0;
14
- }
15
- return hash >>> 0;
16
- }
17
- function encodeStringWithSeed(str, mask, step) {
18
- let out = '';
19
- for (let i = 0; i < str.length; i++) {
20
- const code = str.charCodeAt(i);
21
- // Period-2 seed-polymorphic XOR scrambles all characters while preserving LZ77/Gzip compressibility
22
- const k = ((mask ^ ((i & 1) * step)) & 0x1e) | 1;
23
- out += String.fromCharCode(code ^ k);
24
- }
25
- return out;
26
- }
27
- export function applyStringVault(ast, seed = 0x9e3779b9, minStringLength = 4) {
28
- const suffix = ((seed >>> 0) % 0xff).toString(36);
29
- const cacheVarName = `_uC${suffix}`;
30
- const mask = ((seed >>> 0) & 0x1e) | 3;
31
- const step = (((seed >>> 5) & 7) * 2) + 3;
32
- const stringToIndex = new Map();
33
- const encryptedPayloads = [];
34
- const targetsToReplace = [];
35
- walk.ancestor(ast, {
36
- Literal(node, _state, ancestors) {
37
- if (typeof node.value !== 'string')
38
- return;
39
- const val = node.value;
40
- if (val.length < minStringLength)
41
- return;
42
- if (DIRECTIVE_STRINGS.has(val))
43
- return;
44
- const parent = ancestors[ancestors.length - 2];
45
- if (!parent)
46
- return;
47
- // Skip directives at top of program or function body
48
- if (parent.type === 'ExpressionStatement' && parent.directive)
49
- return;
50
- // Skip import/export source specifiers
51
- if (parent.type === 'ImportDeclaration' ||
52
- parent.type === 'ExportNamedDeclaration' ||
53
- parent.type === 'ExportAllDeclaration' ||
54
- parent.type === 'ImportExpression') {
55
- if (parent.source === node)
56
- return;
57
- }
58
- // Skip object/class property keys and import/export attributes
59
- if ((parent.type === 'Property' ||
60
- parent.type === 'MethodDefinition' ||
61
- parent.type === 'PropertyDefinition') &&
62
- parent.key === node &&
63
- !parent.computed) {
64
- return;
65
- }
66
- if (parent.type === 'ImportAttribute' ||
67
- parent.type === 'ImportSpecifier' ||
68
- parent.type === 'ExportSpecifier') {
69
- return;
70
- }
71
- let idx = stringToIndex.get(val);
72
- if (idx === undefined) {
73
- idx = encryptedPayloads.length;
74
- stringToIndex.set(val, idx);
75
- encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
76
- }
77
- targetsToReplace.push({ node });
78
- },
79
- });
80
- if (targetsToReplace.length === 0 || encryptedPayloads.length === 0) {
81
- return {
82
- encryptedStringsCount: 0,
83
- integrityToken: computeFnv1aHash('empty', seed),
84
- };
85
- }
86
- for (const { node } of targetsToReplace) {
87
- const idx = stringToIndex.get(node.value);
88
- node.type = 'MemberExpression';
89
- node.object = {
90
- type: 'Identifier',
91
- name: cacheVarName,
92
- };
93
- node.property = {
94
- type: 'Literal',
95
- value: idx,
96
- raw: String(idx),
97
- };
98
- node.computed = true;
99
- node.optional = false;
100
- delete node.value;
101
- delete node.raw;
102
- }
103
- const serializedTable = JSON.stringify(encryptedPayloads);
104
- const integrityToken = computeFnv1aHash(serializedTable, seed);
105
- // Decrypt once into const _uC array at module initialization so V8 marks the ContextCell kConst
106
- const helperSource = `
107
- const ${cacheVarName} = ${serializedTable}.map(function(a) {
108
- var s = "";
109
- for (var j = 0; j < a.length; j++) {
110
- s += String.fromCharCode(a.charCodeAt(j) ^ (((${mask} ^ ((j & 1) * ${step})) & 30) | 1));
111
- }
112
- return s;
113
- });
114
- `.trim();
115
- const helperAst = acorn.parse(helperSource, {
116
- ecmaVersion: 'latest',
117
- sourceType: 'script',
118
- });
119
- let insertIdx = 0;
120
- while (insertIdx < ast.body.length) {
121
- const stmt = ast.body[insertIdx];
122
- const isDirective = stmt.type === 'ExpressionStatement' &&
123
- (stmt.directive ||
124
- (stmt.expression?.type === 'Literal' &&
125
- typeof stmt.expression.value === 'string' &&
126
- DIRECTIVE_STRINGS.has(stmt.expression.value)));
127
- if (isDirective || stmt.type === 'ImportDeclaration') {
128
- insertIdx++;
129
- }
130
- else {
131
- break;
132
- }
133
- }
134
- ast.body.splice(insertIdx, 0, ...helperAst.body);
135
- return {
136
- encryptedStringsCount: encryptedPayloads.length,
137
- integrityToken,
138
- };
139
- }
@@ -1,7 +0,0 @@
1
- import type { ShieldConfig } from '../types.js';
2
- export declare function deriveIntegrityFnName(seed: number): string;
3
- /**
4
- * Generates a single-line, self-executing (IIFE), polymorphic, SSR-safe (<1.5 KB)
5
- * anti-tamper deterrent snippet without false-positive window dimension heuristics.
6
- */
7
- export declare function createDeterrentSnippet(config: ShieldConfig): string;
@@ -1,27 +0,0 @@
1
- import { computeIntegrityHash } from './integrity-guard.js';
2
- export function deriveIntegrityFnName(seed) {
3
- const hash = computeIntegrityHash(`unpeek:fn:${seed}`, seed);
4
- return `_u$${hash.toString(36)}`;
5
- }
6
- function deriveLocalVar(label, seed) {
7
- const hash = computeIntegrityHash(`unpeek:var:${label}:${seed}`, seed) & 0xfffff;
8
- return `_${label}${hash.toString(36)}`;
9
- }
10
- /**
11
- * Generates a single-line, self-executing (IIFE), polymorphic, SSR-safe (<1.5 KB)
12
- * anti-tamper deterrent snippet without false-positive window dimension heuristics.
13
- */
14
- export function createDeterrentSnippet(config) {
15
- const seed = config.seed >>> 0;
16
- const fnName = deriveIntegrityFnName(seed);
17
- const strVar = deriveLocalVar('s', seed);
18
- const tamperVar = deriveLocalVar('t', seed);
19
- const noopVar = deriveLocalVar('n', seed);
20
- const methodsVar = deriveLocalVar('m', seed);
21
- const idxVar = deriveLocalVar('i', seed);
22
- const errVar = deriveLocalVar('e', seed);
23
- const gradualConsoleSilence = config.deterrentMode === 'gradual'
24
- ? `if(typeof console!=='undefined'){var ${noopVar}=function(){},${methodsVar}=['log','debug','info','table','dir'];for(var ${idxVar}=0;${idxVar}<${methodsVar}.length;${idxVar}++){try{console[${methodsVar}[${idxVar}]]=${noopVar};}catch(${errVar}){}}}`
25
- : '';
26
- return `(function ${fnName}(){if(typeof window!=='undefined'){try{var ${strVar}=Function.prototype.toString,${tamperVar}=${strVar}.call(${strVar}).indexOf('[native code]')===-1||${strVar}.call(${fnName}).indexOf('\\n')!==-1||${strVar}.call(${fnName}).indexOf('\\r')!==-1;if(${tamperVar}){window.__unpeek_tampered=!0;${gradualConsoleSilence}}}catch(${errVar}){}}})();`;
27
- }
@@ -1,11 +0,0 @@
1
- /**
2
- * Computes a 32-bit unsigned FNV-1a hash over the un-normalized source string.
3
- * Any formatting change (such as DevTools Pretty-Print adding newlines or spaces)
4
- * or monkeypatching alters the resulting hash.
5
- */
6
- export declare function computeIntegrityHash(source: string, seed?: number): number;
7
- /**
8
- * Verifies that a function or source string has not been pretty-printed or modified
9
- * by comparing its un-normalized Function.prototype.toString() FNV-1a hash.
10
- */
11
- export declare function verifyCodeIntegrity(fnOrSource: Function | string, expectedHash: number, seed?: number): boolean;
@@ -1,25 +0,0 @@
1
- const FNV_OFFSET_BASIS = 0x811c9dc5;
2
- const FNV_PRIME = 0x01000193;
3
- /**
4
- * Computes a 32-bit unsigned FNV-1a hash over the un-normalized source string.
5
- * Any formatting change (such as DevTools Pretty-Print adding newlines or spaces)
6
- * or monkeypatching alters the resulting hash.
7
- */
8
- export function computeIntegrityHash(source, seed = 0) {
9
- let hash = (FNV_OFFSET_BASIS ^ (seed >>> 0)) >>> 0;
10
- for (let i = 0; i < source.length; i++) {
11
- hash ^= source.charCodeAt(i);
12
- hash = Math.imul(hash, FNV_PRIME) >>> 0;
13
- }
14
- return hash >>> 0;
15
- }
16
- /**
17
- * Verifies that a function or source string has not been pretty-printed or modified
18
- * by comparing its un-normalized Function.prototype.toString() FNV-1a hash.
19
- */
20
- export function verifyCodeIntegrity(fnOrSource, expectedHash, seed = 0) {
21
- const raw = typeof fnOrSource === 'function'
22
- ? Function.prototype.toString.call(fnOrSource)
23
- : String(fnOrSource);
24
- return computeIntegrityHash(raw, seed) === (expectedHash >>> 0);
25
- }
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};