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