hackmud-script-manager 0.19.0-7c69a3b → 0.19.0-b5e2c0b
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/hsm.js +1 -682
- package/constants.js +1 -4
- package/generateTypeDeclaration.js +1 -94
- package/index.js +1 -50
- package/package.json +1 -1
- package/processScript/index.js +1 -313
- package/processScript/minify.js +1 -376
- package/processScript/postprocess.js +1 -5
- package/processScript/preprocess.js +1 -83
- package/processScript/shared.js +1 -18
- package/processScript/transform.js +1 -393
- package/pull.js +1 -17
- package/push.js +1 -254
- package/syncMacros.js +1 -53
- package/tsconfig.tsbuildinfo +1 -1
- package/watch.js +1 -231
package/processScript/minify.js
CHANGED
@@ -1,376 +1 @@
|
|
1
|
-
import
|
2
|
-
import babelTraverse from '@babel/traverse';
|
3
|
-
import t from '@babel/types';
|
4
|
-
import { assert } from '@samual/lib/assert';
|
5
|
-
import { countHackmudCharacters } from '@samual/lib/countHackmudCharacters';
|
6
|
-
import { spliceString } from '@samual/lib/spliceString';
|
7
|
-
import { tokenizer, tokTypes } from 'acorn';
|
8
|
-
import * as terser from 'terser';
|
9
|
-
import { getReferencePathsToGlobal, includesIllegalString, replaceUnsafeStrings } from './shared.js';
|
10
|
-
|
11
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
12
|
-
const {
|
13
|
-
default: generate
|
14
|
-
} = babelGenerator;
|
15
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
16
|
-
const {
|
17
|
-
default: traverse
|
18
|
-
} = babelTraverse;
|
19
|
-
// TODO move autocomplete code outside this function
|
20
|
-
// TODO replace references to `arguments`
|
21
|
-
|
22
|
-
/**
|
23
|
-
* @param file babel ast node representing a file containing transformed code
|
24
|
-
* @param options {@link MinifyOptions details}
|
25
|
-
*/
|
26
|
-
const minify = async (file, {
|
27
|
-
uniqueID = `00000000000`,
|
28
|
-
mangleNames = false,
|
29
|
-
forceQuineCheats,
|
30
|
-
autocomplete
|
31
|
-
} = {}) => {
|
32
|
-
assert(/^\w{11}$/.exec(uniqueID));
|
33
|
-
let program;
|
34
|
-
traverse(file, {
|
35
|
-
Program(path) {
|
36
|
-
program = path;
|
37
|
-
path.skip();
|
38
|
-
}
|
39
|
-
});
|
40
|
-
if (program.scope.hasGlobal(`_START`)) {
|
41
|
-
for (const referencePath of getReferencePathsToGlobal(`_START`, program)) referencePath.replaceWith(t.identifier(`_ST`));
|
42
|
-
}
|
43
|
-
if (program.scope.hasGlobal(`_TIMEOUT`)) {
|
44
|
-
for (const referencePath of getReferencePathsToGlobal(`_TIMEOUT`, program)) referencePath.replaceWith(t.identifier(`_TO`));
|
45
|
-
}
|
46
|
-
|
47
|
-
// typescript does not like NodePath#get() and becomes slow so I have to dance around it
|
48
|
-
const mainFunctionPath = program.get(`body.0`);
|
49
|
-
for (const parameter of [...mainFunctionPath.node.params].reverse()) {
|
50
|
-
if (parameter.type == `Identifier`) {
|
51
|
-
const binding = mainFunctionPath.scope.getBinding(parameter.name);
|
52
|
-
if (!binding.referenced) {
|
53
|
-
mainFunctionPath.node.params.pop();
|
54
|
-
continue;
|
55
|
-
}
|
56
|
-
}
|
57
|
-
break;
|
58
|
-
}
|
59
|
-
for (const global in program.scope.globals) {
|
60
|
-
if (global == `arguments` || global.startsWith(`$${uniqueID}$`)) continue;
|
61
|
-
const referencePaths = getReferencePathsToGlobal(global, program);
|
62
|
-
if (5 + global.length + referencePaths.length >= global.length * referencePaths.length) continue;
|
63
|
-
for (const path of referencePaths) path.replaceWith(t.identifier(`_${uniqueID}_GLOBAL_${global}_`));
|
64
|
-
mainFunctionPath.node.body.body.unshift(t.variableDeclaration(`let`, [t.variableDeclarator(t.identifier(`_${uniqueID}_GLOBAL_${global}_`), t.identifier(global))]));
|
65
|
-
}
|
66
|
-
const hashGReferencePaths = getReferencePathsToGlobal(`$${uniqueID}$GLOBAL$`, program);
|
67
|
-
if (hashGReferencePaths.length > 3) {
|
68
|
-
for (const path of hashGReferencePaths) path.replaceWith(t.identifier(`_${uniqueID}_G_`));
|
69
|
-
mainFunctionPath.node.body.body.unshift(t.variableDeclaration(`let`, [t.variableDeclarator(t.identifier(`_${uniqueID}_G_`), t.identifier(`$${uniqueID}$GLOBAL$`))]));
|
70
|
-
}
|
71
|
-
const jsonValues = [];
|
72
|
-
// this needs `as boolean` because typescript is dumb
|
73
|
-
let undefinedIsReferenced = false;
|
74
|
-
let scriptBeforeJSONValueReplacement;
|
75
|
-
if (forceQuineCheats != true) {
|
76
|
-
const fileBeforeJSONValueReplacement = t.cloneNode(file);
|
77
|
-
traverse(fileBeforeJSONValueReplacement, {
|
78
|
-
MemberExpression({
|
79
|
-
node: memberExpression
|
80
|
-
}) {
|
81
|
-
if (memberExpression.computed) return;
|
82
|
-
assert(memberExpression.property.type == `Identifier`);
|
83
|
-
if (memberExpression.property.name == `prototype`) {
|
84
|
-
memberExpression.computed = true;
|
85
|
-
memberExpression.property = t.identifier(`_${uniqueID}_PROTOTYPE_PROPERTY_`);
|
86
|
-
} else if (memberExpression.property.name == `__proto__`) {
|
87
|
-
memberExpression.computed = true;
|
88
|
-
memberExpression.property = t.identifier(`_${uniqueID}_PROTO_PROPERTY_`);
|
89
|
-
} else if (includesIllegalString(memberExpression.property.name)) {
|
90
|
-
memberExpression.computed = true;
|
91
|
-
memberExpression.property = t.stringLiteral(replaceUnsafeStrings(uniqueID, memberExpression.property.name));
|
92
|
-
}
|
93
|
-
},
|
94
|
-
ObjectProperty({
|
95
|
-
node: objectProperty
|
96
|
-
}) {
|
97
|
-
if (objectProperty.key.type == `Identifier` && includesIllegalString(objectProperty.key.name)) {
|
98
|
-
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(uniqueID, objectProperty.key.name));
|
99
|
-
objectProperty.shorthand = false;
|
100
|
-
}
|
101
|
-
},
|
102
|
-
StringLiteral({
|
103
|
-
node
|
104
|
-
}) {
|
105
|
-
node.value = replaceUnsafeStrings(uniqueID, node.value);
|
106
|
-
},
|
107
|
-
TemplateLiteral({
|
108
|
-
node
|
109
|
-
}) {
|
110
|
-
for (const templateElement of node.quasis) {
|
111
|
-
if (templateElement.value.cooked) {
|
112
|
-
templateElement.value.cooked = replaceUnsafeStrings(uniqueID, templateElement.value.cooked);
|
113
|
-
templateElement.value.raw = templateElement.value.cooked.replaceAll(`\\`, `\\\\`).replaceAll(`\``, `\\\``).replaceAll(`\${`, `$\\{`);
|
114
|
-
} else templateElement.value.raw = replaceUnsafeStrings(uniqueID, templateElement.value.raw);
|
115
|
-
}
|
116
|
-
},
|
117
|
-
RegExpLiteral(path) {
|
118
|
-
path.node.pattern = replaceUnsafeStrings(uniqueID, path.node.pattern);
|
119
|
-
delete path.node.extra;
|
120
|
-
}
|
121
|
-
});
|
122
|
-
scriptBeforeJSONValueReplacement = (await terser.minify(generate(fileBeforeJSONValueReplacement).code, {
|
123
|
-
ecma: 2015,
|
124
|
-
compress: {
|
125
|
-
passes: Infinity,
|
126
|
-
unsafe: true,
|
127
|
-
unsafe_arrows: true,
|
128
|
-
unsafe_comps: true,
|
129
|
-
unsafe_symbols: true,
|
130
|
-
unsafe_methods: true,
|
131
|
-
unsafe_proto: true,
|
132
|
-
unsafe_regexp: true,
|
133
|
-
unsafe_undefined: true,
|
134
|
-
sequences: false
|
135
|
-
},
|
136
|
-
format: {
|
137
|
-
semicolons: false
|
138
|
-
},
|
139
|
-
keep_classnames: !mangleNames,
|
140
|
-
keep_fnames: !mangleNames
|
141
|
-
})).code.replace(new RegExp(`_${uniqueID}_PROTOTYPE_PROPERTY_`, `g`), `"prototype"`).replace(new RegExp(`_${uniqueID}_PROTO_PROPERTY_`, `g`), `"__proto__"`);
|
142
|
-
if (autocomplete) scriptBeforeJSONValueReplacement = spliceString(scriptBeforeJSONValueReplacement, `//${autocomplete}\n`, getFunctionBodyStart(scriptBeforeJSONValueReplacement) + 1);
|
143
|
-
if (forceQuineCheats == false) return scriptBeforeJSONValueReplacement;
|
144
|
-
}
|
145
|
-
let comment;
|
146
|
-
let hasComment = false;
|
147
|
-
let code;
|
148
|
-
{
|
149
|
-
const promises = [];
|
150
|
-
traverse(file, {
|
151
|
-
FunctionDeclaration(path) {
|
152
|
-
path.traverse({
|
153
|
-
Function(path) {
|
154
|
-
if (path.parent.type != `CallExpression` && path.parentKey != `callee`) path.skip();
|
155
|
-
},
|
156
|
-
Loop(path) {
|
157
|
-
path.skip();
|
158
|
-
},
|
159
|
-
ObjectExpression(path) {
|
160
|
-
const o = {};
|
161
|
-
if (parseObjectExpression(path.node, o)) path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValues.push(o) - 1}_`));
|
162
|
-
},
|
163
|
-
ArrayExpression(path) {
|
164
|
-
const o = [];
|
165
|
-
if (parseArrayExpression(path.node, o)) path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValues.push(o) - 1}_`));
|
166
|
-
}
|
167
|
-
});
|
168
|
-
path.traverse({
|
169
|
-
TemplateLiteral(path) {
|
170
|
-
if (path.parent.type == `TaggedTemplateExpression`) return;
|
171
|
-
const templateLiteral = path.node;
|
172
|
-
let replacement = t.stringLiteral(templateLiteral.quasis[0].value.cooked);
|
173
|
-
for (let index = 0; index < templateLiteral.expressions.length; index++) {
|
174
|
-
const expression = templateLiteral.expressions[index];
|
175
|
-
const templateElement = templateLiteral.quasis[index + 1];
|
176
|
-
replacement = t.binaryExpression(`+`, replacement, expression);
|
177
|
-
if (!templateElement.value.cooked) continue;
|
178
|
-
replacement = t.binaryExpression(`+`, replacement, t.stringLiteral(templateElement.value.cooked));
|
179
|
-
}
|
180
|
-
path.replaceWith(replacement);
|
181
|
-
},
|
182
|
-
MemberExpression({
|
183
|
-
node: memberExpression
|
184
|
-
}) {
|
185
|
-
if (memberExpression.computed) return;
|
186
|
-
assert(memberExpression.property.type == `Identifier`);
|
187
|
-
if (memberExpression.property.name.length < 3) return;
|
188
|
-
memberExpression.computed = true;
|
189
|
-
memberExpression.property = t.stringLiteral(memberExpression.property.name);
|
190
|
-
},
|
191
|
-
UnaryExpression(path) {
|
192
|
-
if (path.node.operator == `void`) {
|
193
|
-
if (path.node.argument.type == `NumericLiteral` && !path.node.argument.value) {
|
194
|
-
path.replaceWith(t.identifier(`_${uniqueID}_UNDEFINED_`));
|
195
|
-
undefinedIsReferenced = true;
|
196
|
-
}
|
197
|
-
} else if (path.node.operator == `-` && path.node.argument.type == `NumericLiteral`) {
|
198
|
-
const value = -path.node.argument.value;
|
199
|
-
promises.push((async () => {
|
200
|
-
if ((await minifyNumber(value)).length <= 3) return;
|
201
|
-
if (path.parentKey == `key` && path.parent.type == `ObjectProperty`) path.parent.computed = true;
|
202
|
-
let jsonValueIndex = jsonValues.indexOf(value);
|
203
|
-
if (jsonValueIndex == -1) jsonValueIndex += jsonValues.push(value);
|
204
|
-
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`));
|
205
|
-
})());
|
206
|
-
path.skip();
|
207
|
-
}
|
208
|
-
},
|
209
|
-
NullLiteral(path) {
|
210
|
-
/* eslint-disable unicorn/no-null */
|
211
|
-
let jsonValueIndex = jsonValues.indexOf(null);
|
212
|
-
if (jsonValueIndex == -1) jsonValueIndex += jsonValues.push(null);
|
213
|
-
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`));
|
214
|
-
/* eslint-enable unicorn/no-null */
|
215
|
-
},
|
216
|
-
NumericLiteral(path) {
|
217
|
-
promises.push((async () => {
|
218
|
-
if ((await minifyNumber(path.node.value)).length <= 3) return;
|
219
|
-
if (path.parentKey == `key` && path.parent.type == `ObjectProperty`) path.parent.computed = true;
|
220
|
-
let jsonValueIndex = jsonValues.indexOf(path.node.value);
|
221
|
-
if (jsonValueIndex == -1) jsonValueIndex += jsonValues.push(path.node.value);
|
222
|
-
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`));
|
223
|
-
})());
|
224
|
-
},
|
225
|
-
StringLiteral(path) {
|
226
|
-
path.node.value = replaceUnsafeStrings(uniqueID, path.node.value);
|
227
|
-
|
228
|
-
// eslint-disable-next-line @typescript-eslint/no-base-to-string -- the `NodePath`'s `.toString()` method compiles and returns the contained `Node`
|
229
|
-
if (JSON.stringify(path.node.value).includes(`\\u00`) || path.toString().length < 4) return;
|
230
|
-
if (path.parentKey == `key` && path.parent.type == `ObjectProperty`) path.parent.computed = true;
|
231
|
-
let jsonValueIndex = jsonValues.indexOf(path.node.value);
|
232
|
-
if (jsonValueIndex == -1) jsonValueIndex += jsonValues.push(path.node.value);
|
233
|
-
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`));
|
234
|
-
},
|
235
|
-
ObjectProperty({
|
236
|
-
node
|
237
|
-
}) {
|
238
|
-
if (node.computed || node.key.type != `Identifier` || node.key.name.length < 4) return;
|
239
|
-
let jsonValueIndex = jsonValues.indexOf(node.key.name);
|
240
|
-
if (jsonValueIndex == -1) jsonValueIndex += jsonValues.push(node.key.name);
|
241
|
-
node.computed = true;
|
242
|
-
node.key = t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`);
|
243
|
-
},
|
244
|
-
RegExpLiteral(path) {
|
245
|
-
path.node.pattern = replaceUnsafeStrings(uniqueID, path.node.pattern);
|
246
|
-
delete path.node.extra;
|
247
|
-
}
|
248
|
-
});
|
249
|
-
path.skip();
|
250
|
-
}
|
251
|
-
});
|
252
|
-
await Promise.all(promises);
|
253
|
-
const functionDeclaration = file.program.body[0];
|
254
|
-
assert(functionDeclaration.type == `FunctionDeclaration`);
|
255
|
-
if (jsonValues.length) {
|
256
|
-
hasComment = true;
|
257
|
-
if (jsonValues.length == 1) {
|
258
|
-
if (typeof jsonValues[0] == `string` && !jsonValues[0].includes(`\n`) && !jsonValues[0].includes(`\t`)) {
|
259
|
-
const variableDeclaration = t.variableDeclaration(`let`, [t.variableDeclarator(t.identifier(`_${uniqueID}_JSON_VALUE_0_`), t.memberExpression(t.taggedTemplateExpression(t.memberExpression(t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine$`), []), t.identifier(`split`)), t.templateLiteral([t.templateElement({
|
260
|
-
raw: `\t`,
|
261
|
-
cooked: `\t`
|
262
|
-
}, true)], [])), t.identifier(`$${uniqueID}$SPLIT_INDEX$`), true))]);
|
263
|
-
if (undefinedIsReferenced) variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`)));
|
264
|
-
functionDeclaration.body.body.unshift(variableDeclaration);
|
265
|
-
comment = jsonValues[0];
|
266
|
-
} else {
|
267
|
-
const variableDeclaration = t.variableDeclaration(`let`, [t.variableDeclarator(t.identifier(`_${uniqueID}_JSON_VALUE_0_`), t.callExpression(t.memberExpression(t.identifier(`JSON`), t.identifier(`parse`)), [t.memberExpression(t.taggedTemplateExpression(t.memberExpression(t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine$`), []), t.identifier(`split`)), t.templateLiteral([t.templateElement({
|
268
|
-
raw: `\t`,
|
269
|
-
cooked: `\t`
|
270
|
-
}, true)], [])), t.identifier(`$${uniqueID}$SPLIT_INDEX$`), true)]))]);
|
271
|
-
if (undefinedIsReferenced) variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`)));
|
272
|
-
functionDeclaration.body.body.unshift(variableDeclaration);
|
273
|
-
comment = JSON.stringify(jsonValues[0]);
|
274
|
-
}
|
275
|
-
} else {
|
276
|
-
const variableDeclaration = t.variableDeclaration(`let`, [t.variableDeclarator(t.arrayPattern(jsonValues.map((_, index) => t.identifier(`_${uniqueID}_JSON_VALUE_${index}_`))), t.callExpression(t.memberExpression(t.identifier(`JSON`), t.identifier(`parse`)), [t.memberExpression(t.taggedTemplateExpression(t.memberExpression(t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine$`), []), t.identifier(`split`)), t.templateLiteral([t.templateElement({
|
277
|
-
raw: `\t`,
|
278
|
-
cooked: `\t`
|
279
|
-
}, true)], [])), t.identifier(`$${uniqueID}$SPLIT_INDEX$`), true)]))]);
|
280
|
-
if (undefinedIsReferenced) variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`)));
|
281
|
-
functionDeclaration.body.body.unshift(variableDeclaration);
|
282
|
-
comment = JSON.stringify(jsonValues);
|
283
|
-
}
|
284
|
-
} else if (undefinedIsReferenced) {
|
285
|
-
functionDeclaration.body.body.unshift(t.variableDeclaration(`let`, [t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`))]));
|
286
|
-
}
|
287
|
-
code = generate(file).code;
|
288
|
-
}
|
289
|
-
code = (await terser.minify(code, {
|
290
|
-
ecma: 2015,
|
291
|
-
compress: {
|
292
|
-
passes: Infinity,
|
293
|
-
unsafe: true,
|
294
|
-
unsafe_arrows: true,
|
295
|
-
unsafe_comps: true,
|
296
|
-
unsafe_symbols: true,
|
297
|
-
unsafe_methods: true,
|
298
|
-
unsafe_proto: true,
|
299
|
-
unsafe_regexp: true,
|
300
|
-
unsafe_undefined: true,
|
301
|
-
sequences: false
|
302
|
-
},
|
303
|
-
format: {
|
304
|
-
semicolons: false
|
305
|
-
},
|
306
|
-
keep_classnames: !mangleNames,
|
307
|
-
keep_fnames: !mangleNames
|
308
|
-
})).code || ``;
|
309
|
-
|
310
|
-
// this step affects the character count and can't happen after the count comparison
|
311
|
-
if (comment != undefined) {
|
312
|
-
code = spliceString(code, `${autocomplete ? `//${autocomplete}\n` : ``}\n//\t${comment}\t\n`, getFunctionBodyStart(code) + 1);
|
313
|
-
code = code.replace(`$${uniqueID}$SPLIT_INDEX$`, await minifyNumber(code.split(`\t`).findIndex(part => part == comment)));
|
314
|
-
}
|
315
|
-
if (forceQuineCheats == true) return code;
|
316
|
-
assert(scriptBeforeJSONValueReplacement);
|
317
|
-
|
318
|
-
// if the script has a comment, it's gonna contain `SC$scripts$quine()`
|
319
|
-
// which is gonna compile to `#fs.scripts.quine()` which contains
|
320
|
-
// an extra character so we have to account for that
|
321
|
-
if (countHackmudCharacters(scriptBeforeJSONValueReplacement) <= countHackmudCharacters(code) + Number(hasComment)) return scriptBeforeJSONValueReplacement;
|
322
|
-
return code;
|
323
|
-
};
|
324
|
-
const parseObjectExpression = (node, o) => {
|
325
|
-
if (!node.properties.length) return false;
|
326
|
-
for (const property of node.properties) {
|
327
|
-
if (property.type != `ObjectProperty` || property.computed) return false;
|
328
|
-
assert(property.key.type == `Identifier` || property.key.type == `NumericLiteral` || property.key.type == `StringLiteral`);
|
329
|
-
if (property.value.type == `ArrayExpression`) {
|
330
|
-
const childArray = [];
|
331
|
-
if (parseArrayExpression(property.value, childArray)) o[property.key.type == `Identifier` ? property.key.name : property.key.value] = childArray;else return false;
|
332
|
-
} else if (property.value.type == `ObjectExpression`) {
|
333
|
-
const childObject = {};
|
334
|
-
if (parseObjectExpression(property.value, childObject)) o[property.key.type == `Identifier` ? property.key.name : property.key.value] = childObject;else return false;
|
335
|
-
} else if (property.value.type == `NullLiteral`)
|
336
|
-
// eslint-disable-next-line unicorn/no-null
|
337
|
-
o[property.key.type == `Identifier` ? property.key.name : property.key.value] = null;else if (property.value.type == `BooleanLiteral` || property.value.type == `NumericLiteral` || property.value.type == `StringLiteral`) o[property.key.type == `Identifier` ? property.key.name : property.key.value] = property.value.value;else if (property.value.type == `TemplateLiteral` && !property.value.expressions.length) o[property.key.type == `Identifier` ? property.key.name : property.key.value] = property.value.quasis[0].value.cooked;else return false;
|
338
|
-
}
|
339
|
-
return true;
|
340
|
-
};
|
341
|
-
const parseArrayExpression = (node, o) => {
|
342
|
-
if (!node.elements.length) return false;
|
343
|
-
for (const element of node.elements) {
|
344
|
-
if (!element) return false;
|
345
|
-
if (element.type == `ArrayExpression`) {
|
346
|
-
const childArray = [];
|
347
|
-
if (parseArrayExpression(element, childArray)) childArray.push(childArray);else return false;
|
348
|
-
} else if (element.type == `ObjectExpression`) {
|
349
|
-
const childObject = {};
|
350
|
-
if (parseObjectExpression(element, childObject)) o.push(childObject);else return false;
|
351
|
-
} else if (element.type == `NullLiteral`)
|
352
|
-
// eslint-disable-next-line unicorn/no-null
|
353
|
-
o.push(null);else if (element.type == `BooleanLiteral` || element.type == `NumericLiteral` || element.type == `StringLiteral`) o.push(element.value);else if (element.type == `TemplateLiteral` && !element.expressions.length) o.push(element.quasis[0].value.cooked);else return false;
|
354
|
-
}
|
355
|
-
return true;
|
356
|
-
};
|
357
|
-
const minifyNumber = async number => /\$\((?<number>.+)\)/.exec((await terser.minify(`$(${number})`, {
|
358
|
-
ecma: 2015
|
359
|
-
})).code).groups.number;
|
360
|
-
const getFunctionBodyStart = code => {
|
361
|
-
const tokens = tokenizer(code, {
|
362
|
-
ecmaVersion: 2015
|
363
|
-
});
|
364
|
-
tokens.getToken(); // function
|
365
|
-
tokens.getToken(); // name
|
366
|
-
tokens.getToken(); // (
|
367
|
-
|
368
|
-
let nests = 1;
|
369
|
-
while (nests) {
|
370
|
-
const token = tokens.getToken();
|
371
|
-
if (token.type == tokTypes.parenL) nests++;else if (token.type == tokTypes.parenR) nests--;
|
372
|
-
}
|
373
|
-
return tokens.getToken().start; // {
|
374
|
-
};
|
375
|
-
|
376
|
-
export { minify as default, minify };
|
1
|
+
import e from"@babel/generator";import t from"@babel/traverse";import r from"@babel/types";import{assert as i}from"@samual/lib/assert";import{countHackmudCharacters as n}from"@samual/lib/countHackmudCharacters";import{spliceString as a}from"@samual/lib/spliceString";import{tokenizer as o,tokTypes as s}from"acorn";import*as l from"terser";import{getReferencePathsToGlobal as p,includesIllegalString as u,replaceUnsafeStrings as c}from"./shared.js";const{default:d}=e,{default:f}=t,minify=async(e,{uniqueID:t="00000000000",mangleNames:o=!1,forceQuineCheats:s,autocomplete:m}={})=>{let _;if(i(/^\w{11}$/.exec(t)),f(e,{Program(e){_=e,e.skip()}}),_.scope.hasGlobal("_START"))for(const e of p("_START",_))e.replaceWith(r.identifier("_ST"));if(_.scope.hasGlobal("_TIMEOUT"))for(const e of p("_TIMEOUT",_))e.replaceWith(r.identifier("_TO"));const y=_.get("body.0");for(const e of[...y.node.params].reverse()){if("Identifier"!=e.type||y.scope.getBinding(e.name).referenced)break;y.node.params.pop()}for(const e in _.scope.globals){if("arguments"==e||e.startsWith(`$${t}$`))continue;const i=p(e,_);if(!(5+e.length+i.length>=e.length*i.length)){for(const n of i)n.replaceWith(r.identifier(`_${t}_GLOBAL_${e}_`));y.node.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_GLOBAL_${e}_`),r.identifier(e))]))}}const $=p(`$${t}$GLOBAL$`,_);if($.length>3){for(const e of $)e.replaceWith(r.identifier(`_${t}_G_`));y.node.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_G_`),r.identifier(`$${t}$GLOBAL$`))]))}const b=[];let g,E,v=!1;if(1!=s){const n=r.cloneNode(e);if(f(n,{MemberExpression({node:e}){e.computed||(i("Identifier"==e.property.type),"prototype"==e.property.name?(e.computed=!0,e.property=r.identifier(`_${t}_PROTOTYPE_PROPERTY_`)):"__proto__"==e.property.name?(e.computed=!0,e.property=r.identifier(`_${t}_PROTO_PROPERTY_`)):u(e.property.name)&&(e.computed=!0,e.property=r.stringLiteral(c(t,e.property.name))))},ObjectProperty({node:e}){"Identifier"==e.key.type&&u(e.key.name)&&(e.key=r.stringLiteral(c(t,e.key.name)),e.shorthand=!1)},StringLiteral({node:e}){e.value=c(t,e.value)},TemplateLiteral({node:e}){for(const r of e.quasis)r.value.cooked?(r.value.cooked=c(t,r.value.cooked),r.value.raw=r.value.cooked.replaceAll("\\","\\\\").replaceAll("`","\\`").replaceAll("${","$\\{")):r.value.raw=c(t,r.value.raw)},RegExpLiteral(e){e.node.pattern=c(t,e.node.pattern),delete e.node.extra}}),g=(await l.minify(d(n).code,{ecma:2015,compress:{passes:1/0,unsafe:!0,unsafe_arrows:!0,unsafe_comps:!0,unsafe_symbols:!0,unsafe_methods:!0,unsafe_proto:!0,unsafe_regexp:!0,unsafe_undefined:!0,sequences:!1},format:{semicolons:!1},keep_classnames:!o,keep_fnames:!o})).code.replace(new RegExp(`_${t}_PROTOTYPE_PROPERTY_`,"g"),'"prototype"').replace(new RegExp(`_${t}_PROTO_PROPERTY_`,"g"),'"__proto__"'),m&&(g=a(g,`//${m}\n`,getFunctionBodyStart(g)+1)),0==s)return g}let h,k=!1;{const n=[];f(e,{FunctionDeclaration(e){e.traverse({Function(e){"CallExpression"!=e.parent.type&&"callee"!=e.parentKey&&e.skip()},Loop(e){e.skip()},ObjectExpression(e){const i={};parseObjectExpression(e.node,i)&&e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${b.push(i)-1}_`))},ArrayExpression(e){const i=[];parseArrayExpression(e.node,i)&&e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${b.push(i)-1}_`))}}),e.traverse({TemplateLiteral(e){if("TaggedTemplateExpression"==e.parent.type)return;const t=e.node;let i=r.stringLiteral(t.quasis[0].value.cooked);for(let e=0;e<t.expressions.length;e++){const n=t.expressions[e],a=t.quasis[e+1];i=r.binaryExpression("+",i,n),a.value.cooked&&(i=r.binaryExpression("+",i,r.stringLiteral(a.value.cooked)))}e.replaceWith(i)},MemberExpression({node:e}){e.computed||(i("Identifier"==e.property.type),e.property.name.length<3||(e.computed=!0,e.property=r.stringLiteral(e.property.name)))},UnaryExpression(e){if("void"==e.node.operator)"NumericLiteral"!=e.node.argument.type||e.node.argument.value||(e.replaceWith(r.identifier(`_${t}_UNDEFINED_`)),v=!0);else if("-"==e.node.operator&&"NumericLiteral"==e.node.argument.type){const i=-e.node.argument.value;n.push((async()=>{if((await minifyNumber(i)).length<=3)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let n=b.indexOf(i);-1==n&&(n+=b.push(i)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${n}_`))})()),e.skip()}},NullLiteral(e){let i=b.indexOf(null);-1==i&&(i+=b.push(null)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))},NumericLiteral(e){n.push((async()=>{if((await minifyNumber(e.node.value)).length<=3)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let i=b.indexOf(e.node.value);-1==i&&(i+=b.push(e.node.value)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))})())},StringLiteral(e){if(e.node.value=c(t,e.node.value),JSON.stringify(e.node.value).includes("\\u00")||e.toString().length<4)return;"key"==e.parentKey&&"ObjectProperty"==e.parent.type&&(e.parent.computed=!0);let i=b.indexOf(e.node.value);-1==i&&(i+=b.push(e.node.value)),e.replaceWith(r.identifier(`_${t}_JSON_VALUE_${i}_`))},ObjectProperty({node:e}){if(e.computed||"Identifier"!=e.key.type||e.key.name.length<4)return;let i=b.indexOf(e.key.name);-1==i&&(i+=b.push(e.key.name)),e.computed=!0,e.key=r.identifier(`_${t}_JSON_VALUE_${i}_`)},RegExpLiteral(e){e.node.pattern=c(t,e.node.pattern),delete e.node.extra}}),e.skip()}}),await Promise.all(n);const a=e.program.body[0];if(i("FunctionDeclaration"==a.type),b.length)if(k=!0,1==b.length)if("string"!=typeof b[0]||b[0].includes("\n")||b[0].includes("\t")){const e=r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_JSON_VALUE_0_`),r.callExpression(r.memberExpression(r.identifier("JSON"),r.identifier("parse")),[r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${t}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${t}$SPLIT_INDEX$`),!0)]))]);v&&e.declarations.push(r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))),a.body.body.unshift(e),E=JSON.stringify(b[0])}else{const e=r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_JSON_VALUE_0_`),r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${t}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${t}$SPLIT_INDEX$`),!0))]);v&&e.declarations.push(r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))),a.body.body.unshift(e),E=b[0]}else{const e=r.variableDeclaration("let",[r.variableDeclarator(r.arrayPattern(b.map(((e,i)=>r.identifier(`_${t}_JSON_VALUE_${i}_`)))),r.callExpression(r.memberExpression(r.identifier("JSON"),r.identifier("parse")),[r.memberExpression(r.taggedTemplateExpression(r.memberExpression(r.callExpression(r.identifier(`$${t}$SUBSCRIPT$scripts$quine$`),[]),r.identifier("split")),r.templateLiteral([r.templateElement({raw:"\t",cooked:"\t"},!0)],[])),r.identifier(`$${t}$SPLIT_INDEX$`),!0)]))]);v&&e.declarations.push(r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))),a.body.body.unshift(e),E=JSON.stringify(b)}else v&&a.body.body.unshift(r.variableDeclaration("let",[r.variableDeclarator(r.identifier(`_${t}_UNDEFINED_`))]));h=d(e).code}return h=(await l.minify(h,{ecma:2015,compress:{passes:1/0,unsafe:!0,unsafe_arrows:!0,unsafe_comps:!0,unsafe_symbols:!0,unsafe_methods:!0,unsafe_proto:!0,unsafe_regexp:!0,unsafe_undefined:!0,sequences:!1},format:{semicolons:!1},keep_classnames:!o,keep_fnames:!o})).code||"",null!=E&&(h=a(h,`${m?`//${m}\n`:""}\n//\t${E}\t\n`,getFunctionBodyStart(h)+1),h=h.replace(`$${t}$SPLIT_INDEX$`,await minifyNumber(h.split("\t").findIndex((e=>e==E))))),1==s?h:(i(g),n(g)<=n(h)+Number(k)?g:h)},parseObjectExpression=(e,t)=>{if(!e.properties.length)return!1;for(const r of e.properties){if("ObjectProperty"!=r.type||r.computed)return!1;if(i("Identifier"==r.key.type||"NumericLiteral"==r.key.type||"StringLiteral"==r.key.type),"ArrayExpression"==r.value.type){const e=[];if(!parseArrayExpression(r.value,e))return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=e}else if("ObjectExpression"==r.value.type){const e={};if(!parseObjectExpression(r.value,e))return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=e}else if("NullLiteral"==r.value.type)t["Identifier"==r.key.type?r.key.name:r.key.value]=null;else if("BooleanLiteral"==r.value.type||"NumericLiteral"==r.value.type||"StringLiteral"==r.value.type)t["Identifier"==r.key.type?r.key.name:r.key.value]=r.value.value;else{if("TemplateLiteral"!=r.value.type||r.value.expressions.length)return!1;t["Identifier"==r.key.type?r.key.name:r.key.value]=r.value.quasis[0].value.cooked}}return!0},parseArrayExpression=(e,t)=>{if(!e.elements.length)return!1;for(const r of e.elements){if(!r)return!1;if("ArrayExpression"==r.type){const e=[];if(!parseArrayExpression(r,e))return!1;e.push(e)}else if("ObjectExpression"==r.type){const e={};if(!parseObjectExpression(r,e))return!1;t.push(e)}else if("NullLiteral"==r.type)t.push(null);else if("BooleanLiteral"==r.type||"NumericLiteral"==r.type||"StringLiteral"==r.type)t.push(r.value);else{if("TemplateLiteral"!=r.type||r.expressions.length)return!1;t.push(r.quasis[0].value.cooked)}}return!0},minifyNumber=async e=>/\$\((?<number>.+)\)/.exec((await l.minify(`$(${e})`,{ecma:2015})).code).groups.number,getFunctionBodyStart=e=>{const t=o(e,{ecmaVersion:2015});t.getToken(),t.getToken(),t.getToken();let r=1;for(;r;){const e=t.getToken();e.type==s.parenL?r++:e.type==s.parenR&&r--}return t.getToken().start};export{minify as default,minify};
|
@@ -1,5 +1 @@
|
|
1
|
-
const postprocess
|
2
|
-
return code.replace(/^function\s*\w+\(/, `function(`).replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$SC_DOLLAR\\$`, `g`), `S\\C$`).replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$DB_DOLLAR\\$`, `g`), `D\\B$`).replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$D\\$`, `g`), `_\\_D_S`).replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$FMCL\\$`, `g`), `_\\_FMCL_`).replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$G\\$`, `g`), `_\\_G_`).replace(new RegExp(`\\$${uniqueID}\\$SUBSCRIPT\\$(\\w+)\\$(\\w+)\\$`, `g`), `#${`nlmhf`[seclevel]}s.$1.$2`).replace(new RegExp(`\\$${uniqueID}\\$DEBUG\\$`, `g`), `#D`).replace(new RegExp(`\\$${uniqueID}\\$FMCL\\$`, `g`), `#FMCL`).replace(new RegExp(`\\$${uniqueID}\\$GLOBAL\\$`, `g`), `#G`).replace(new RegExp(`\\$${uniqueID}\\$DB\\$(\\w+)\\$`, `g`), `#db.$1`).replace(new RegExp(`\\$${uniqueID}\\$SLASH_SLASH\\$`, `g`), `/\\/`).replace(new RegExp(`\\$${uniqueID}\\$NOT_A_SUBSCRIPT\\$(#[\\w\\.]+)\\(\\$`, `g`), `$1\\(`).replace(new RegExp(`\\$${uniqueID}\\$NOT_A_DB_CALL\\$(\\w+)\\$`, `g`), `#db.$1\\(`).replace(new RegExp(`\\$${uniqueID}\\$NOT_A_DEBUG_CALL\\$`, `g`), `#D\\(`).replace(new RegExp(`\\$${uniqueID}\\$NOT_FMCL\\$`, `g`), `#\\FMCL`).replace(new RegExp(`\\$${uniqueID}\\$NOT_G\\$`, `g`), `#\\G`);
|
3
|
-
};
|
4
|
-
|
5
|
-
export { postprocess as default, postprocess };
|
1
|
+
const postprocess=($,e,p)=>$.replace(/^function\s*\w+\(/,"function(").replace(new RegExp(`\\$${p}\\$\\\\(?:\\\\)?\\$SC_DOLLAR\\$`,"g"),"S\\C$").replace(new RegExp(`\\$${p}\\$\\\\(?:\\\\)?\\$DB_DOLLAR\\$`,"g"),"D\\B$").replace(new RegExp(`\\$${p}\\$\\\\(?:\\\\)?\\$D\\$`,"g"),"_\\_D_S").replace(new RegExp(`\\$${p}\\$\\\\(?:\\\\)?\\$FMCL\\$`,"g"),"_\\_FMCL_").replace(new RegExp(`\\$${p}\\$\\\\(?:\\\\)?\\$G\\$`,"g"),"_\\_G_").replace(new RegExp(`\\$${p}\\$SUBSCRIPT\\$(\\w+)\\$(\\w+)\\$`,"g"),`#${"nlmhf"[e]}s.$1.$2`).replace(new RegExp(`\\$${p}\\$DEBUG\\$`,"g"),"#D").replace(new RegExp(`\\$${p}\\$FMCL\\$`,"g"),"#FMCL").replace(new RegExp(`\\$${p}\\$GLOBAL\\$`,"g"),"#G").replace(new RegExp(`\\$${p}\\$DB\\$(\\w+)\\$`,"g"),"#db.$1").replace(new RegExp(`\\$${p}\\$SLASH_SLASH\\$`,"g"),"/\\/").replace(new RegExp(`\\$${p}\\$NOT_A_SUBSCRIPT\\$(#[\\w\\.]+)\\(\\$`,"g"),"$1\\(").replace(new RegExp(`\\$${p}\\$NOT_A_DB_CALL\\$(\\w+)\\$`,"g"),"#db.$1\\(").replace(new RegExp(`\\$${p}\\$NOT_A_DEBUG_CALL\\$`,"g"),"#D\\(").replace(new RegExp(`\\$${p}\\$NOT_FMCL\\$`,"g"),"#\\FMCL").replace(new RegExp(`\\$${p}\\$NOT_G\\$`,"g"),"#\\G");export{postprocess as default,postprocess};
|
@@ -1,83 +1 @@
|
|
1
|
-
import
|
2
|
-
import { parse } from '@babel/parser';
|
3
|
-
import babelTraverse from '@babel/traverse';
|
4
|
-
import t from '@babel/types';
|
5
|
-
import { assert } from '@samual/lib/assert';
|
6
|
-
import { spliceString } from '@samual/lib/spliceString';
|
7
|
-
import { resolve } from 'import-meta-resolve';
|
8
|
-
|
9
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
10
|
-
const {
|
11
|
-
default: traverse
|
12
|
-
} = babelTraverse;
|
13
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
14
|
-
const {
|
15
|
-
default: generate
|
16
|
-
} = babelGenerator;
|
17
|
-
/**
|
18
|
-
* @param code source code for preprocessing
|
19
|
-
* @param options {@link PreprocessOptions details}
|
20
|
-
*/
|
21
|
-
const preprocess = async (code, {
|
22
|
-
uniqueID = `00000000000`
|
23
|
-
} = {}) => {
|
24
|
-
assert(/^\w{11}$/.test(uniqueID));
|
25
|
-
const sourceCode = code;
|
26
|
-
let lengthBefore;
|
27
|
-
do {
|
28
|
-
lengthBefore = code.length;
|
29
|
-
code = code.replace(/^\s+/, ``).replace(/^\/\/.*/, ``).replace(/^\/\*[\s\S]*?\*\//, ``);
|
30
|
-
} while (code.length != lengthBefore);
|
31
|
-
code = code.replace(/^function\s*\(/, `export default function (`);
|
32
|
-
let file;
|
33
|
-
while (true) {
|
34
|
-
let error;
|
35
|
-
try {
|
36
|
-
file = parse(code, {
|
37
|
-
plugins: [`typescript`, [`decorators`, {
|
38
|
-
decoratorsBeforeExport: true
|
39
|
-
}], `doExpressions`, `functionBind`, `functionSent`, `partialApplication`, [`pipelineOperator`, {
|
40
|
-
proposal: `hack`,
|
41
|
-
topicToken: `%`
|
42
|
-
}], `throwExpressions`, [`recordAndTuple`, {
|
43
|
-
syntaxType: `hash`
|
44
|
-
}], `classProperties`, `classPrivateProperties`, `classPrivateMethods`, `logicalAssignment`, `numericSeparator`, `nullishCoalescingOperator`, `optionalChaining`, `optionalCatchBinding`, `objectRestSpread`],
|
45
|
-
sourceType: `module`
|
46
|
-
});
|
47
|
-
break;
|
48
|
-
} catch (error_) {
|
49
|
-
assert(error_ instanceof SyntaxError);
|
50
|
-
error = error_;
|
51
|
-
}
|
52
|
-
if (error.code != `BABEL_PARSER_SYNTAX_ERROR` || error.reasonCode != `PrivateInExpectedIn`) {
|
53
|
-
console.log(/.+/.exec(code.slice(error.pos))?.[0]);
|
54
|
-
throw error;
|
55
|
-
}
|
56
|
-
const codeSlice = code.slice(error.pos);
|
57
|
-
let match;
|
58
|
-
if (match = /^#[0-4fhmln]s\.scripts\.quine\(\)/.exec(codeSlice)) code = spliceString(code, JSON.stringify(sourceCode), error.pos, match[0].length);else if (match = /^#[0-4fhmln]?s\./.exec(codeSlice)) code = spliceString(code, `$`, error.pos, 1);else if (match = /^#D[^\w$]/.exec(codeSlice)) code = spliceString(code, `$`, error.pos, 1);else if (match = /^#FMCL/.exec(codeSlice)) code = spliceString(code, `$${uniqueID}$FMCL$`, error.pos, match[0].length);else if (match = /^#G/.exec(codeSlice)) code = spliceString(code, `$${uniqueID}$GLOBAL$`, error.pos, match[0].length);else if (match = /^#db\./.exec(codeSlice)) code = spliceString(code, `$`, error.pos, 1);else throw error;
|
59
|
-
}
|
60
|
-
let program;
|
61
|
-
traverse(file, {
|
62
|
-
Program(path) {
|
63
|
-
program = path;
|
64
|
-
path.skip();
|
65
|
-
}
|
66
|
-
});
|
67
|
-
const needRecord = program.scope.hasGlobal(`Record`);
|
68
|
-
const needTuple = program.scope.hasGlobal(`Tuple`);
|
69
|
-
if (needRecord || needTuple) {
|
70
|
-
file.program.body.unshift(t.importDeclaration(needRecord ? needTuple ? [t.importSpecifier(t.identifier(`Record`), t.identifier(`Record`)), t.importSpecifier(t.identifier(`Tuple`), t.identifier(`Tuple`))] : [t.importSpecifier(t.identifier(`Record`), t.identifier(`Record`))] : [t.importSpecifier(t.identifier(`Tuple`), t.identifier(`Tuple`))], t.stringLiteral(`@bloomberg/record-tuple-polyfill`)));
|
71
|
-
}
|
72
|
-
if (program.scope.hasGlobal(`Proxy`)) {
|
73
|
-
file.program.body.unshift(t.importDeclaration([t.importDefaultSpecifier(t.identifier(`Proxy`))], t.stringLiteral((await resolve(`proxy-polyfill/src/proxy.js`, import.meta.url)).slice(7))));
|
74
|
-
}
|
75
|
-
if (program.node.body.length == 1 && program.node.body[0].type == `FunctionDeclaration`) return {
|
76
|
-
code: `export default ${generate(file).code}`
|
77
|
-
};
|
78
|
-
return {
|
79
|
-
code: generate(file).code
|
80
|
-
};
|
81
|
-
};
|
82
|
-
|
83
|
-
export { preprocess as default, preprocess };
|
1
|
+
import e from"@babel/generator";import{parse as o}from"@babel/parser";import r from"@babel/traverse";import i from"@babel/types";import{assert as t}from"@samual/lib/assert";import{spliceString as l}from"@samual/lib/spliceString";import{resolve as s}from"import-meta-resolve";const{default:p}=r,{default:a}=e,preprocess=async(e,{uniqueID:r="00000000000"}={})=>{t(/^\w{11}$/.test(r));const n=e;let c,f,d;do{c=e.length,e=e.replace(/^\s+/,"").replace(/^\/\/.*/,"").replace(/^\/\*[\s\S]*?\*\//,"")}while(e.length!=c);for(e=e.replace(/^function\s*\(/,"export default function (");;){let i;try{f=o(e,{plugins:["typescript",["decorators",{decoratorsBeforeExport:!0}],"doExpressions","functionBind","functionSent","partialApplication",["pipelineOperator",{proposal:"hack",topicToken:"%"}],"throwExpressions",["recordAndTuple",{syntaxType:"hash"}],"classProperties","classPrivateProperties","classPrivateMethods","logicalAssignment","numericSeparator","nullishCoalescingOperator","optionalChaining","optionalCatchBinding","objectRestSpread"],sourceType:"module"});break}catch(e){t(e instanceof SyntaxError),i=e}if("BABEL_PARSER_SYNTAX_ERROR"!=i.code||"PrivateInExpectedIn"!=i.reasonCode)throw console.log(/.+/.exec(e.slice(i.pos))?.[0]),i;const s=e.slice(i.pos);let p;if(p=/^#[0-4fhmln]s\.scripts\.quine\(\)/.exec(s))e=l(e,JSON.stringify(n),i.pos,p[0].length);else if(p=/^#[0-4fhmln]?s\./.exec(s))e=l(e,"$",i.pos,1);else if(p=/^#D[^\w$]/.exec(s))e=l(e,"$",i.pos,1);else if(p=/^#FMCL/.exec(s))e=l(e,`$${r}$FMCL$`,i.pos,p[0].length);else if(p=/^#G/.exec(s))e=l(e,`$${r}$GLOBAL$`,i.pos,p[0].length);else{if(!(p=/^#db\./.exec(s)))throw i;e=l(e,"$",i.pos,1)}}p(f,{Program(e){d=e,e.skip()}});const m=d.scope.hasGlobal("Record"),u=d.scope.hasGlobal("Tuple");return(m||u)&&f.program.body.unshift(i.importDeclaration(m?u?[i.importSpecifier(i.identifier("Record"),i.identifier("Record")),i.importSpecifier(i.identifier("Tuple"),i.identifier("Tuple"))]:[i.importSpecifier(i.identifier("Record"),i.identifier("Record"))]:[i.importSpecifier(i.identifier("Tuple"),i.identifier("Tuple"))],i.stringLiteral("@bloomberg/record-tuple-polyfill"))),d.scope.hasGlobal("Proxy")&&f.program.body.unshift(i.importDeclaration([i.importDefaultSpecifier(i.identifier("Proxy"))],i.stringLiteral((await s("proxy-polyfill/src/proxy.js",import.meta.url)).slice(7)))),1==d.node.body.length&&"FunctionDeclaration"==d.node.body[0].type?{code:`export default ${a(f).code}`}:{code:a(f).code}};export{preprocess as default,preprocess};
|
package/processScript/shared.js
CHANGED
@@ -1,18 +1 @@
|
|
1
|
-
import
|
2
|
-
import { ensure } from '@samual/lib/assert';
|
3
|
-
|
4
|
-
const getReferencePathsToGlobal = (name, program) => {
|
5
|
-
const [variableDeclaration] = program.unshiftContainer(`body`, t.variableDeclaration(`let`, [t.variableDeclarator(t.identifier(name))]));
|
6
|
-
program.scope.crawl();
|
7
|
-
const binding = ensure(program.scope.getBinding(name));
|
8
|
-
variableDeclaration.remove();
|
9
|
-
return binding.referencePaths;
|
10
|
-
};
|
11
|
-
const includesIllegalString = toCheck => {
|
12
|
-
return toCheck.includes(`SC$`) || toCheck.includes(`DB$`) || toCheck.includes(`__D_S`) || toCheck.includes(`__FMCL_`) || toCheck.includes(`__G_`);
|
13
|
-
};
|
14
|
-
const replaceUnsafeStrings = (uniqueID, toReplace) => {
|
15
|
-
return toReplace.replaceAll(`SC$`, `$${uniqueID}$\\$SC_DOLLAR$`).replaceAll(`DB$`, `$${uniqueID}$\\$DB_DOLLAR$`).replaceAll(`__D_S`, `$${uniqueID}$\\$D$`).replaceAll(`__FMCL_`, `$${uniqueID}$\\$FMCL$`).replaceAll(`__G_`, `$${uniqueID}$\\$G$`).replaceAll(`//`, `$${uniqueID}$SLASH_SLASH$`).replaceAll(/#[0-4fhmln]?s(?:\.[_a-z][\d_a-z]{0,24}){2}\(/g, `$${uniqueID}$NOT_A_SUBSCRIPT$$$&$`).replaceAll(/#db\.(?<methodName>[irfu]|u1|us|ObjectId)\(/g, `$${uniqueID}$NOT_A_DB_CALL$$$1$`).replaceAll(`#D(`, `$${uniqueID}$NOT_A_DEBUG_CALL$`).replaceAll(`#FMCL`, `$${uniqueID}$NOT_FMCL$`).replaceAll(`#G`, `$${uniqueID}$NOT_G$`);
|
16
|
-
};
|
17
|
-
|
18
|
-
export { getReferencePathsToGlobal, includesIllegalString, replaceUnsafeStrings };
|
1
|
+
import $ from"@babel/types";import{ensure as e}from"@samual/lib/assert";const getReferencePathsToGlobal=(l,_)=>{const[r]=_.unshiftContainer("body",$.variableDeclaration("let",[$.variableDeclarator($.identifier(l))]));_.scope.crawl();const a=e(_.scope.getBinding(l));return r.remove(),a.referencePaths},includesIllegalString=$=>$.includes("SC$")||$.includes("DB$")||$.includes("__D_S")||$.includes("__FMCL_")||$.includes("__G_"),replaceUnsafeStrings=($,e)=>e.replaceAll("SC$",`$${$}$\\$SC_DOLLAR$`).replaceAll("DB$",`$${$}$\\$DB_DOLLAR$`).replaceAll("__D_S",`$${$}$\\$D$`).replaceAll("__FMCL_",`$${$}$\\$FMCL$`).replaceAll("__G_",`$${$}$\\$G$`).replaceAll("//",`$${$}$SLASH_SLASH$`).replaceAll(/#[0-4fhmln]?s(?:\.[_a-z][\d_a-z]{0,24}){2}\(/g,`$${$}$NOT_A_SUBSCRIPT$$$&$`).replaceAll(/#db\.(?<methodName>[irfu]|u1|us|ObjectId)\(/g,`$${$}$NOT_A_DB_CALL$$$1$`).replaceAll("#D(",`$${$}$NOT_A_DEBUG_CALL$`).replaceAll("#FMCL",`$${$}$NOT_FMCL$`).replaceAll("#G",`$${$}$NOT_G$`);export{getReferencePathsToGlobal,includesIllegalString,replaceUnsafeStrings};
|