dismantle 0.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.
- package/LICENSE +7 -0
- package/README.md +40 -0
- package/dist/cjs/development/index.cjs +1484 -0
- package/dist/cjs/development/index.cjs.map +7 -0
- package/dist/cjs/development/runtime.cjs +1484 -0
- package/dist/cjs/development/runtime.cjs.map +7 -0
- package/dist/cjs/production/index.cjs +1 -0
- package/dist/cjs/production/runtime.cjs +1 -0
- package/dist/esm/development/index.mjs +1453 -0
- package/dist/esm/development/index.mjs.map +7 -0
- package/dist/esm/development/runtime.mjs +1453 -0
- package/dist/esm/development/runtime.mjs.map +7 -0
- package/dist/esm/production/index.mjs +1 -0
- package/dist/esm/production/runtime.mjs +1 -0
- package/dist/types/hidden-imports.d.ts +4 -0
- package/dist/types/hidden-imports.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/plugin.d.ts +8 -0
- package/dist/types/plugin.d.ts.map +1 -0
- package/dist/types/split.d.ts +7 -0
- package/dist/types/split.d.ts.map +1 -0
- package/dist/types/transform-block.d.ts +5 -0
- package/dist/types/transform-block.d.ts.map +1 -0
- package/dist/types/transform-call.d.ts +5 -0
- package/dist/types/transform-call.d.ts.map +1 -0
- package/dist/types/types.d.ts +62 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils/assert.d.ts +2 -0
- package/dist/types/utils/assert.d.ts.map +1 -0
- package/dist/types/utils/generator-shim.d.ts +4 -0
- package/dist/types/utils/generator-shim.d.ts.map +1 -0
- package/dist/types/utils/get-descriptive-name.d.ts +3 -0
- package/dist/types/utils/get-descriptive-name.d.ts.map +1 -0
- package/dist/types/utils/get-foreign-bindings.d.ts +3 -0
- package/dist/types/utils/get-foreign-bindings.d.ts.map +1 -0
- package/dist/types/utils/get-identifiers-from-lval.d.ts +3 -0
- package/dist/types/utils/get-identifiers-from-lval.d.ts.map +1 -0
- package/dist/types/utils/get-import-identifier.d.ts +5 -0
- package/dist/types/utils/get-import-identifier.d.ts.map +1 -0
- package/dist/types/utils/get-import-specifier-name.d.ts +3 -0
- package/dist/types/utils/get-import-specifier-name.d.ts.map +1 -0
- package/dist/types/utils/get-module-definition.d.ts +4 -0
- package/dist/types/utils/get-module-definition.d.ts.map +1 -0
- package/dist/types/utils/get-root-statement-path.d.ts +3 -0
- package/dist/types/utils/get-root-statement-path.d.ts.map +1 -0
- package/dist/types/utils/register-import-specifiers.d.ts +5 -0
- package/dist/types/utils/register-import-specifiers.d.ts.map +1 -0
- package/dist/types/utils/unwrap.d.ts +11 -0
- package/dist/types/utils/unwrap.d.ts.map +1 -0
- package/dist/types/utils/xxhash32.d.ts +7 -0
- package/dist/types/utils/xxhash32.d.ts.map +1 -0
- package/package.json +90 -0
- package/src/hidden-imports.ts +13 -0
- package/src/index.ts +90 -0
- package/src/plugin.ts +26 -0
- package/src/split.ts +1074 -0
- package/src/transform-block.ts +115 -0
- package/src/transform-call.ts +100 -0
- package/src/types.ts +73 -0
- package/src/utils/assert.ts +5 -0
- package/src/utils/errors.ts +21 -0
- package/src/utils/generator-shim.ts +22 -0
- package/src/utils/get-descriptive-name.ts +42 -0
- package/src/utils/get-foreign-bindings.ts +60 -0
- package/src/utils/get-identifiers-from-lval.ts +44 -0
- package/src/utils/get-import-identifier.ts +23 -0
- package/src/utils/get-import-specifier-name.ts +10 -0
- package/src/utils/get-module-definition.ts +53 -0
- package/src/utils/get-root-statement-path.ts +14 -0
- package/src/utils/register-import-specifiers.ts +77 -0
- package/src/utils/unwrap.ts +63 -0
- package/src/utils/xxhash32.ts +214 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type * as babel from '@babel/core';
|
|
2
|
+
import type * as t from '@babel/types';
|
|
3
|
+
|
|
4
|
+
type TypeFilter<V extends t.Node> = (node: t.Node) => node is V;
|
|
5
|
+
|
|
6
|
+
export function isPathValid<V extends t.Node>(
|
|
7
|
+
path: unknown,
|
|
8
|
+
key: TypeFilter<V>,
|
|
9
|
+
): path is babel.NodePath<V> {
|
|
10
|
+
return key((path as babel.NodePath).node);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type NestedExpression =
|
|
14
|
+
| t.ParenthesizedExpression
|
|
15
|
+
| t.TypeCastExpression
|
|
16
|
+
| t.TSAsExpression
|
|
17
|
+
| t.TSSatisfiesExpression
|
|
18
|
+
| t.TSNonNullExpression
|
|
19
|
+
| t.TSInstantiationExpression
|
|
20
|
+
| t.TSTypeAssertion;
|
|
21
|
+
|
|
22
|
+
export function isNestedExpression(node: t.Node): node is NestedExpression {
|
|
23
|
+
switch (node.type) {
|
|
24
|
+
case 'ParenthesizedExpression':
|
|
25
|
+
case 'TypeCastExpression':
|
|
26
|
+
case 'TSAsExpression':
|
|
27
|
+
case 'TSSatisfiesExpression':
|
|
28
|
+
case 'TSNonNullExpression':
|
|
29
|
+
case 'TSTypeAssertion':
|
|
30
|
+
case 'TSInstantiationExpression':
|
|
31
|
+
return true;
|
|
32
|
+
default:
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type TypeCheck<K> = K extends TypeFilter<infer U> ? U : never;
|
|
38
|
+
|
|
39
|
+
export function unwrapNode<K extends (value: t.Node) => boolean>(
|
|
40
|
+
node: t.Node,
|
|
41
|
+
key: K,
|
|
42
|
+
): TypeCheck<K> | undefined {
|
|
43
|
+
if (key(node)) {
|
|
44
|
+
return node as TypeCheck<K>;
|
|
45
|
+
}
|
|
46
|
+
if (isNestedExpression(node)) {
|
|
47
|
+
return unwrapNode(node.expression, key);
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function unwrapPath<V extends t.Node>(
|
|
53
|
+
path: unknown,
|
|
54
|
+
key: TypeFilter<V>,
|
|
55
|
+
): babel.NodePath<V> | undefined {
|
|
56
|
+
if (isPathValid(path, key)) {
|
|
57
|
+
return path;
|
|
58
|
+
}
|
|
59
|
+
if (isPathValid(path, isNestedExpression)) {
|
|
60
|
+
return unwrapPath(path.get('expression'), key);
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-shadow */
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2019 Jason Dent
|
|
4
|
+
* https://github.com/Jason3S/xxhash
|
|
5
|
+
*/
|
|
6
|
+
const PRIME32_1 = 2654435761;
|
|
7
|
+
const PRIME32_2 = 2246822519;
|
|
8
|
+
const PRIME32_3 = 3266489917;
|
|
9
|
+
const PRIME32_4 = 668265263;
|
|
10
|
+
const PRIME32_5 = 374761393;
|
|
11
|
+
|
|
12
|
+
function toUtf8(text: string): Uint8Array {
|
|
13
|
+
const bytes: number[] = [];
|
|
14
|
+
for (let i = 0, n = text.length; i < n; ++i) {
|
|
15
|
+
const c = text.charCodeAt(i);
|
|
16
|
+
if (c < 0x80) {
|
|
17
|
+
bytes.push(c);
|
|
18
|
+
} else if (c < 0x800) {
|
|
19
|
+
bytes.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
|
|
20
|
+
} else if (c < 0xd800 || c >= 0xe000) {
|
|
21
|
+
bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
|
|
22
|
+
} else {
|
|
23
|
+
const cp =
|
|
24
|
+
0x10000 + (((c & 0x3ff) << 10) | (text.charCodeAt(++i) & 0x3ff));
|
|
25
|
+
bytes.push(
|
|
26
|
+
0xf0 | ((cp >> 18) & 0x7),
|
|
27
|
+
0x80 | ((cp >> 12) & 0x3f),
|
|
28
|
+
0x80 | ((cp >> 6) & 0x3f),
|
|
29
|
+
0x80 | (cp & 0x3f),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return new Uint8Array(bytes);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @param buffer - byte array or string
|
|
38
|
+
* @param seed - optional seed (32-bit unsigned);
|
|
39
|
+
*/
|
|
40
|
+
export default function xxHash32(
|
|
41
|
+
buffer: Uint8Array | string,
|
|
42
|
+
seed = 0,
|
|
43
|
+
): number {
|
|
44
|
+
buffer = typeof buffer === 'string' ? toUtf8(buffer) : buffer;
|
|
45
|
+
const b = buffer;
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
Step 1. Initialize internal accumulators
|
|
49
|
+
Each accumulator gets an initial value based on optional seed input.
|
|
50
|
+
Since the seed is optional, it can be 0.
|
|
51
|
+
```
|
|
52
|
+
u32 acc1 = seed + PRIME32_1 + PRIME32_2;
|
|
53
|
+
u32 acc2 = seed + PRIME32_2;
|
|
54
|
+
u32 acc3 = seed + 0;
|
|
55
|
+
u32 acc4 = seed - PRIME32_1;
|
|
56
|
+
```
|
|
57
|
+
Special case : input is less than 16 bytes
|
|
58
|
+
When input is too small (< 16 bytes), the algorithm will not process any stripe.
|
|
59
|
+
Consequently, it will not make use of parallel accumulators.
|
|
60
|
+
In which case, a simplified initialization is performed, using a single accumulator :
|
|
61
|
+
u32 acc = seed + PRIME32_5;
|
|
62
|
+
The algorithm then proceeds directly to step 4.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
let acc = (seed + PRIME32_5) & 0xffffffff;
|
|
66
|
+
let offset = 0;
|
|
67
|
+
|
|
68
|
+
if (b.length >= 16) {
|
|
69
|
+
const accN = [
|
|
70
|
+
(seed + PRIME32_1 + PRIME32_2) & 0xffffffff,
|
|
71
|
+
(seed + PRIME32_2) & 0xffffffff,
|
|
72
|
+
(seed + 0) & 0xffffffff,
|
|
73
|
+
(seed - PRIME32_1) & 0xffffffff,
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
Step 2. Process stripes
|
|
78
|
+
A stripe is a contiguous segment of 16 bytes. It is evenly divided into 4 lanes,
|
|
79
|
+
of 4 bytes each. The first lane is used to update accumulator 1, the second lane
|
|
80
|
+
is used to update accumulator 2, and so on. Each lane read its associated 32-bit
|
|
81
|
+
value using little-endian convention. For each {lane, accumulator}, the update
|
|
82
|
+
process is called a round, and applies the following formula :
|
|
83
|
+
```
|
|
84
|
+
accN = accN + (laneN * PRIME32_2);
|
|
85
|
+
accN = accN <<< 13;
|
|
86
|
+
accN = accN * PRIME32_1;
|
|
87
|
+
```
|
|
88
|
+
This shuffles the bits so that any bit from input lane impacts several bits in
|
|
89
|
+
output accumulator. All operations are performed modulo 2^32.
|
|
90
|
+
Input is consumed one full stripe at a time. Step 2 is looped as many times as
|
|
91
|
+
necessary to consume the whole input, except the last remaining bytes which cannot
|
|
92
|
+
form a stripe (< 16 bytes). When that happens, move to step 3.
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
const b = buffer;
|
|
96
|
+
const limit = b.length - 16;
|
|
97
|
+
let lane = 0;
|
|
98
|
+
for (offset = 0; (offset & 0xfffffff0) <= limit; offset += 4) {
|
|
99
|
+
const i = offset;
|
|
100
|
+
const laneN0 = b[i + 0] + (b[i + 1] << 8);
|
|
101
|
+
const laneN1 = b[i + 2] + (b[i + 3] << 8);
|
|
102
|
+
const laneNP = laneN0 * PRIME32_2 + ((laneN1 * PRIME32_2) << 16);
|
|
103
|
+
let acc = (accN[lane] + laneNP) & 0xffffffff;
|
|
104
|
+
acc = (acc << 13) | (acc >>> 19);
|
|
105
|
+
const acc0 = acc & 0xffff;
|
|
106
|
+
const acc1 = acc >>> 16;
|
|
107
|
+
accN[lane] = (acc0 * PRIME32_1 + ((acc1 * PRIME32_1) << 16)) & 0xffffffff;
|
|
108
|
+
lane = (lane + 1) & 0x3;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/*
|
|
112
|
+
Step 3. Accumulator convergence
|
|
113
|
+
All 4 lane accumulators from previous steps are merged to produce a
|
|
114
|
+
single remaining accumulator
|
|
115
|
+
of same width (32-bit). The associated formula is as follows :
|
|
116
|
+
```
|
|
117
|
+
acc = (acc1 <<< 1) + (acc2 <<< 7) + (acc3 <<< 12) + (acc4 <<< 18);
|
|
118
|
+
```
|
|
119
|
+
*/
|
|
120
|
+
acc =
|
|
121
|
+
(((accN[0] << 1) | (accN[0] >>> 31)) +
|
|
122
|
+
((accN[1] << 7) | (accN[1] >>> 25)) +
|
|
123
|
+
((accN[2] << 12) | (accN[2] >>> 20)) +
|
|
124
|
+
((accN[3] << 18) | (accN[3] >>> 14))) &
|
|
125
|
+
0xffffffff;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/*
|
|
129
|
+
Step 4. Add input length
|
|
130
|
+
The input total length is presumed known at this stage.
|
|
131
|
+
This step is just about adding the length to
|
|
132
|
+
accumulator, so that it participates to final mixing.
|
|
133
|
+
```
|
|
134
|
+
acc = acc + (u32)inputLength;
|
|
135
|
+
```
|
|
136
|
+
*/
|
|
137
|
+
acc = (acc + buffer.length) & 0xffffffff;
|
|
138
|
+
|
|
139
|
+
/*
|
|
140
|
+
Step 5. Consume remaining input
|
|
141
|
+
There may be up to 15 bytes remaining to consume from the input.
|
|
142
|
+
The final stage will digest them according
|
|
143
|
+
to following pseudo-code :
|
|
144
|
+
```
|
|
145
|
+
while (remainingLength >= 4) {
|
|
146
|
+
lane = read_32bit_little_endian(input_ptr);
|
|
147
|
+
acc = acc + lane * PRIME32_3;
|
|
148
|
+
acc = (acc <<< 17) * PRIME32_4;
|
|
149
|
+
input_ptr += 4; remainingLength -= 4;
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
This process ensures that all input bytes are present in the final mix.
|
|
153
|
+
*/
|
|
154
|
+
|
|
155
|
+
const limit = buffer.length - 4;
|
|
156
|
+
for (; offset <= limit; offset += 4) {
|
|
157
|
+
const i = offset;
|
|
158
|
+
const laneN0 = b[i + 0] + (b[i + 1] << 8);
|
|
159
|
+
const laneN1 = b[i + 2] + (b[i + 3] << 8);
|
|
160
|
+
const laneP = laneN0 * PRIME32_3 + ((laneN1 * PRIME32_3) << 16);
|
|
161
|
+
acc = (acc + laneP) & 0xffffffff;
|
|
162
|
+
acc = (acc << 17) | (acc >>> 15);
|
|
163
|
+
acc =
|
|
164
|
+
((acc & 0xffff) * PRIME32_4 + (((acc >>> 16) * PRIME32_4) << 16)) &
|
|
165
|
+
0xffffffff;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/*
|
|
169
|
+
```
|
|
170
|
+
while (remainingLength >= 1) {
|
|
171
|
+
lane = read_byte(input_ptr);
|
|
172
|
+
acc = acc + lane * PRIME32_5;
|
|
173
|
+
acc = (acc <<< 11) * PRIME32_1;
|
|
174
|
+
input_ptr += 1; remainingLength -= 1;
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
for (; offset < b.length; ++offset) {
|
|
180
|
+
const lane = b[offset];
|
|
181
|
+
acc += lane * PRIME32_5;
|
|
182
|
+
acc = (acc << 11) | (acc >>> 21);
|
|
183
|
+
acc =
|
|
184
|
+
((acc & 0xffff) * PRIME32_1 + (((acc >>> 16) * PRIME32_1) << 16)) &
|
|
185
|
+
0xffffffff;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/*
|
|
189
|
+
Step 6. Final mix (avalanche)
|
|
190
|
+
The final mix ensures that all input bits have a chance to impact any bit in
|
|
191
|
+
the output digest, resulting in an unbiased distribution. This is also called
|
|
192
|
+
avalanche effect.
|
|
193
|
+
```
|
|
194
|
+
acc = acc xor (acc >> 15);
|
|
195
|
+
acc = acc * PRIME32_2;
|
|
196
|
+
acc = acc xor (acc >> 13);
|
|
197
|
+
acc = acc * PRIME32_3;
|
|
198
|
+
acc = acc xor (acc >> 16);
|
|
199
|
+
```
|
|
200
|
+
*/
|
|
201
|
+
|
|
202
|
+
acc ^= acc >>> 15;
|
|
203
|
+
acc =
|
|
204
|
+
(((acc & 0xffff) * PRIME32_2) & 0xffffffff) +
|
|
205
|
+
(((acc >>> 16) * PRIME32_2) << 16);
|
|
206
|
+
acc ^= acc >>> 13;
|
|
207
|
+
acc =
|
|
208
|
+
(((acc & 0xffff) * PRIME32_3) & 0xffffffff) +
|
|
209
|
+
(((acc >>> 16) * PRIME32_3) << 16);
|
|
210
|
+
acc ^= acc >>> 16;
|
|
211
|
+
|
|
212
|
+
// turn any negatives back into a positive number;
|
|
213
|
+
return acc < 0 ? acc + 4294967296 : acc;
|
|
214
|
+
}
|