hackmud-script-manager 0.19.1-d57be2a → 0.20.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/bin/hsm.js +1 -1
- package/env.d.ts +1657 -0
- package/package.json +7 -4
- package/processScript/index.d.ts +2 -2
- package/processScript/index.js +14 -14
- package/processScript/minify.d.ts +2 -2
- package/processScript/minify.js +42 -42
- package/processScript/postprocess.d.ts +1 -1
- package/processScript/postprocess.js +17 -17
- package/processScript/preprocess.d.ts +2 -2
- package/processScript/preprocess.js +4 -4
- package/processScript/shared.d.ts +1 -1
- package/processScript/shared.js +12 -12
- package/processScript/transform.d.ts +2 -2
- package/processScript/transform.js +46 -46
- package/push.js +4 -4
- package/watch.js +4 -4
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "hackmud-script-manager",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.20.0",
|
4
4
|
"description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
|
5
5
|
"keywords": [
|
6
6
|
"api",
|
@@ -59,15 +59,18 @@
|
|
59
59
|
"@rollup/plugin-commonjs": "^25.0.7",
|
60
60
|
"@rollup/plugin-json": "^6.1.0",
|
61
61
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
62
|
-
"@samual/lib": "0.
|
62
|
+
"@samual/lib": "0.11.0",
|
63
63
|
"acorn": "^8.11.3",
|
64
64
|
"chalk": "^5.3.0",
|
65
65
|
"chokidar": "^3.6.0",
|
66
66
|
"import-meta-resolve": "^4.0.0",
|
67
67
|
"prettier": "^3.2.5",
|
68
68
|
"proxy-polyfill": "^0.3.2",
|
69
|
-
"rollup": "^4.
|
70
|
-
"terser": "^5.30.
|
69
|
+
"rollup": "^4.16.4",
|
70
|
+
"terser": "^5.30.4"
|
71
|
+
},
|
72
|
+
"peerDependencies": {
|
73
|
+
"typescript": "5.4.5"
|
71
74
|
},
|
72
75
|
"type": "module",
|
73
76
|
"exports": {
|
package/processScript/index.d.ts
CHANGED
@@ -5,7 +5,7 @@ export { preprocess } from "./preprocess";
|
|
5
5
|
export { transform } from "./transform";
|
6
6
|
export type ProcessOptions = LaxPartial<{
|
7
7
|
/** whether to minify the given code */ minify: boolean;
|
8
|
-
/** 11 a-z 0-9 characters */
|
8
|
+
/** 11 a-z 0-9 characters */ uniqueId: string;
|
9
9
|
/** the user going to be hosting this script (or set to `true` if not yet known) */ scriptUser: string | true;
|
10
10
|
filePath: string;
|
11
11
|
/** whether to mangle function and class names (defaults to `false`) */ mangleNames: boolean;
|
@@ -22,7 +22,7 @@ export type ProcessOptions = LaxPartial<{
|
|
22
22
|
/** Minifies a given script
|
23
23
|
* @param code JavaScript or TypeScript code
|
24
24
|
* @param options {@link ProcessOptions details} */
|
25
|
-
export declare function processScript(code: string, { minify: shouldMinify,
|
25
|
+
export declare function processScript(code: string, { minify: shouldMinify, uniqueId, scriptUser, scriptName, filePath, mangleNames, forceQuineCheats }: ProcessOptions): Promise<{
|
26
26
|
script: string;
|
27
27
|
warnings: {
|
28
28
|
message: string;
|
package/processScript/index.js
CHANGED
@@ -44,7 +44,7 @@ async function processScript(
|
|
44
44
|
code,
|
45
45
|
{
|
46
46
|
minify: shouldMinify = !0,
|
47
|
-
|
47
|
+
uniqueId = Math.floor(Math.random() * 2 ** 52)
|
48
48
|
.toString(36)
|
49
49
|
.padStart(11, "0"),
|
50
50
|
scriptUser,
|
@@ -54,7 +54,7 @@ async function processScript(
|
|
54
54
|
forceQuineCheats
|
55
55
|
}
|
56
56
|
) {
|
57
|
-
assert(/^\w{11}$/.exec(
|
57
|
+
assert(/^\w{11}$/.exec(uniqueId), "src/processScript/index.ts:77:36")
|
58
58
|
const sourceCode = code
|
59
59
|
let autocomplete, statedSeclevel
|
60
60
|
const autocompleteMatch = /^function\s*\(.+\/\/(?<autocomplete>.+)/.exec(code)
|
@@ -115,7 +115,7 @@ async function processScript(
|
|
115
115
|
}
|
116
116
|
}
|
117
117
|
}
|
118
|
-
assert(/^\w{11}$/.exec(
|
118
|
+
assert(/^\w{11}$/.exec(uniqueId), "src/processScript/index.ts:158:36")
|
119
119
|
const plugins = [
|
120
120
|
[babelPluginProposalDecorators.default, { decoratorsBeforeExport: !0 }],
|
121
121
|
[babelPluginTransformClassProperties.default],
|
@@ -170,7 +170,7 @@ async function processScript(
|
|
170
170
|
)
|
171
171
|
}
|
172
172
|
} else {
|
173
|
-
filePathResolved =
|
173
|
+
filePathResolved = uniqueId + ".ts"
|
174
174
|
const [
|
175
175
|
babelPluginTransformTypescript,
|
176
176
|
babelPluginProposalDoExpressions,
|
@@ -207,7 +207,7 @@ async function processScript(
|
|
207
207
|
{
|
208
208
|
name: "hackmud-script-manager",
|
209
209
|
async transform(code, id) {
|
210
|
-
if (!id.includes("/node_modules/")) return (await preprocess(code, {
|
210
|
+
if (!id.includes("/node_modules/")) return (await preprocess(code, { uniqueId })).code
|
211
211
|
let program
|
212
212
|
traverse(parse(code, { sourceType: "module" }), {
|
213
213
|
Program(path) {
|
@@ -235,7 +235,7 @@ async function processScript(
|
|
235
235
|
seclevelNames = ["NULLSEC", "LOWSEC", "MIDSEC", "HIGHSEC", "FULLSEC"]
|
236
236
|
code = (await bundle.generate({})).output[0].code
|
237
237
|
const { file, seclevel } = transform(parse(code, { sourceType: "module" }), sourceCode, {
|
238
|
-
|
238
|
+
uniqueId,
|
239
239
|
scriptUser,
|
240
240
|
scriptName
|
241
241
|
})
|
@@ -244,7 +244,7 @@ async function processScript(
|
|
244
244
|
`detected seclevel ${seclevelNames[seclevel]} is lower than stated seclevel ${seclevelNames[statedSeclevel]}`
|
245
245
|
)
|
246
246
|
code = generate(file).code
|
247
|
-
if (shouldMinify) code = await minify(file, {
|
247
|
+
if (shouldMinify) code = await minify(file, { uniqueId, mangleNames, forceQuineCheats, autocomplete })
|
248
248
|
else {
|
249
249
|
traverse(file, {
|
250
250
|
MemberExpression({ node: memberExpression }) {
|
@@ -259,7 +259,7 @@ async function processScript(
|
|
259
259
|
} else if (includesIllegalString(memberExpression.property.name)) {
|
260
260
|
memberExpression.computed = !0
|
261
261
|
memberExpression.property = t.stringLiteral(
|
262
|
-
replaceUnsafeStrings(
|
262
|
+
replaceUnsafeStrings(uniqueId, memberExpression.property.name)
|
263
263
|
)
|
264
264
|
}
|
265
265
|
}
|
@@ -294,25 +294,25 @@ async function processScript(
|
|
294
294
|
},
|
295
295
|
ObjectProperty({ node: objectProperty }) {
|
296
296
|
if ("Identifier" == objectProperty.key.type && includesIllegalString(objectProperty.key.name)) {
|
297
|
-
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(
|
297
|
+
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(uniqueId, objectProperty.key.name))
|
298
298
|
objectProperty.shorthand = !1
|
299
299
|
}
|
300
300
|
},
|
301
301
|
StringLiteral({ node }) {
|
302
|
-
node.value = replaceUnsafeStrings(
|
302
|
+
node.value = replaceUnsafeStrings(uniqueId, node.value)
|
303
303
|
},
|
304
304
|
TemplateLiteral({ node }) {
|
305
305
|
for (const templateElement of node.quasis)
|
306
306
|
if (templateElement.value.cooked) {
|
307
|
-
templateElement.value.cooked = replaceUnsafeStrings(
|
307
|
+
templateElement.value.cooked = replaceUnsafeStrings(uniqueId, templateElement.value.cooked)
|
308
308
|
templateElement.value.raw = templateElement.value.cooked
|
309
309
|
.replaceAll("\\", "\\\\")
|
310
310
|
.replaceAll("`", "\\`")
|
311
311
|
.replaceAll("${", "$\\{")
|
312
|
-
} else templateElement.value.raw = replaceUnsafeStrings(
|
312
|
+
} else templateElement.value.raw = replaceUnsafeStrings(uniqueId, templateElement.value.raw)
|
313
313
|
},
|
314
314
|
RegExpLiteral(path) {
|
315
|
-
path.node.pattern = replaceUnsafeStrings(
|
315
|
+
path.node.pattern = replaceUnsafeStrings(uniqueId, path.node.pattern)
|
316
316
|
delete path.node.extra
|
317
317
|
}
|
318
318
|
})
|
@@ -323,7 +323,7 @@ async function processScript(
|
|
323
323
|
trailingComma: "none"
|
324
324
|
})
|
325
325
|
}
|
326
|
-
code = postprocess(code, seclevel,
|
326
|
+
code = postprocess(code, seclevel, uniqueId)
|
327
327
|
if (includesIllegalString(code))
|
328
328
|
throw Error(
|
329
329
|
'you found a weird edge case where I wasn\'t able to replace illegal strings like "SC$", please report thx'
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import type { File } from "@babel/types";
|
2
2
|
import type { LaxPartial } from "@samual/lib";
|
3
3
|
type MinifyOptions = LaxPartial<{
|
4
|
-
/** 11 a-z 0-9 characters */
|
4
|
+
/** 11 a-z 0-9 characters */ uniqueId: string;
|
5
5
|
/** whether to mangle function and class names (defaults to `false`) */ mangleNames: boolean;
|
6
6
|
/** when set to `true` forces use of quine cheats
|
7
7
|
*
|
@@ -14,5 +14,5 @@ type MinifyOptions = LaxPartial<{
|
|
14
14
|
}>;
|
15
15
|
/** @param file babel ast node representing a file containing transformed code
|
16
16
|
* @param options {@link MinifyOptions details} */
|
17
|
-
export declare function minify(file: File, {
|
17
|
+
export declare function minify(file: File, { uniqueId, mangleNames, forceQuineCheats, autocomplete }?: MinifyOptions): Promise<string>;
|
18
18
|
export {};
|
package/processScript/minify.js
CHANGED
@@ -11,8 +11,8 @@ const { default: generate } = babelGenerator,
|
|
11
11
|
{ default: traverse } = babelTraverse,
|
12
12
|
minifyNumber = async number =>
|
13
13
|
/\$\((?<number>.+)\)/.exec((await terser.minify(`$(${number})`, { ecma: 2015 })).code).groups.number
|
14
|
-
async function minify(file, {
|
15
|
-
assert(/^\w{11}$/.exec(
|
14
|
+
async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQuineCheats, autocomplete } = {}) {
|
15
|
+
assert(/^\w{11}$/.exec(uniqueId), "src/processScript/minify.ts:46:36")
|
16
16
|
let program
|
17
17
|
traverse(file, {
|
18
18
|
Program(path) {
|
@@ -32,23 +32,23 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
32
32
|
mainFunctionPath.node.params.pop()
|
33
33
|
}
|
34
34
|
for (const global in program.scope.globals) {
|
35
|
-
if ("arguments" == global || global.startsWith(`$${
|
35
|
+
if ("arguments" == global || global.startsWith(`$${uniqueId}$`)) continue
|
36
36
|
const referencePaths = getReferencePathsToGlobal(global, program)
|
37
37
|
if (!(5 + global.length + referencePaths.length >= global.length * referencePaths.length)) {
|
38
|
-
for (const path of referencePaths) path.replaceWith(t.identifier(`_${
|
38
|
+
for (const path of referencePaths) path.replaceWith(t.identifier(`_${uniqueId}_GLOBAL_${global}_`))
|
39
39
|
mainFunctionPath.node.body.body.unshift(
|
40
40
|
t.variableDeclaration("let", [
|
41
|
-
t.variableDeclarator(t.identifier(`_${
|
41
|
+
t.variableDeclarator(t.identifier(`_${uniqueId}_GLOBAL_${global}_`), t.identifier(global))
|
42
42
|
])
|
43
43
|
)
|
44
44
|
}
|
45
45
|
}
|
46
|
-
const hashGReferencePaths = getReferencePathsToGlobal(`$${
|
46
|
+
const hashGReferencePaths = getReferencePathsToGlobal(`$${uniqueId}$GLOBAL$`, program)
|
47
47
|
if (hashGReferencePaths.length > 3) {
|
48
|
-
for (const path of hashGReferencePaths) path.replaceWith(t.identifier(`_${
|
48
|
+
for (const path of hashGReferencePaths) path.replaceWith(t.identifier(`_${uniqueId}_G_`))
|
49
49
|
mainFunctionPath.node.body.body.unshift(
|
50
50
|
t.variableDeclaration("let", [
|
51
|
-
t.variableDeclarator(t.identifier(`_${
|
51
|
+
t.variableDeclarator(t.identifier(`_${uniqueId}_G_`), t.identifier(`$${uniqueId}$GLOBAL$`))
|
52
52
|
])
|
53
53
|
)
|
54
54
|
}
|
@@ -64,39 +64,39 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
64
64
|
assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:127:60")
|
65
65
|
if ("prototype" == memberExpression.property.name) {
|
66
66
|
memberExpression.computed = !0
|
67
|
-
memberExpression.property = t.identifier(`_${
|
67
|
+
memberExpression.property = t.identifier(`_${uniqueId}_PROTOTYPE_PROPERTY_`)
|
68
68
|
} else if ("__proto__" == memberExpression.property.name) {
|
69
69
|
memberExpression.computed = !0
|
70
|
-
memberExpression.property = t.identifier(`_${
|
70
|
+
memberExpression.property = t.identifier(`_${uniqueId}_PROTO_PROPERTY_`)
|
71
71
|
} else if (includesIllegalString(memberExpression.property.name)) {
|
72
72
|
memberExpression.computed = !0
|
73
73
|
memberExpression.property = t.stringLiteral(
|
74
|
-
replaceUnsafeStrings(
|
74
|
+
replaceUnsafeStrings(uniqueId, memberExpression.property.name)
|
75
75
|
)
|
76
76
|
}
|
77
77
|
}
|
78
78
|
},
|
79
79
|
ObjectProperty({ node: objectProperty }) {
|
80
80
|
if ("Identifier" == objectProperty.key.type && includesIllegalString(objectProperty.key.name)) {
|
81
|
-
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(
|
81
|
+
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(uniqueId, objectProperty.key.name))
|
82
82
|
objectProperty.shorthand = !1
|
83
83
|
}
|
84
84
|
},
|
85
85
|
StringLiteral({ node }) {
|
86
|
-
node.value = replaceUnsafeStrings(
|
86
|
+
node.value = replaceUnsafeStrings(uniqueId, node.value)
|
87
87
|
},
|
88
88
|
TemplateLiteral({ node }) {
|
89
89
|
for (const templateElement of node.quasis)
|
90
90
|
if (templateElement.value.cooked) {
|
91
|
-
templateElement.value.cooked = replaceUnsafeStrings(
|
91
|
+
templateElement.value.cooked = replaceUnsafeStrings(uniqueId, templateElement.value.cooked)
|
92
92
|
templateElement.value.raw = templateElement.value.cooked
|
93
93
|
.replaceAll("\\", "\\\\")
|
94
94
|
.replaceAll("`", "\\`")
|
95
95
|
.replaceAll("${", "$\\{")
|
96
|
-
} else templateElement.value.raw = replaceUnsafeStrings(
|
96
|
+
} else templateElement.value.raw = replaceUnsafeStrings(uniqueId, templateElement.value.raw)
|
97
97
|
},
|
98
98
|
RegExpLiteral(path) {
|
99
|
-
path.node.pattern = replaceUnsafeStrings(
|
99
|
+
path.node.pattern = replaceUnsafeStrings(uniqueId, path.node.pattern)
|
100
100
|
delete path.node.extra
|
101
101
|
}
|
102
102
|
})
|
@@ -120,8 +120,8 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
120
120
|
keep_fnames: !mangleNames
|
121
121
|
})
|
122
122
|
).code
|
123
|
-
.replace(RegExp(`_${
|
124
|
-
.replace(RegExp(`_${
|
123
|
+
.replace(RegExp(`_${uniqueId}_PROTOTYPE_PROPERTY_`, "g"), '"prototype"')
|
124
|
+
.replace(RegExp(`_${uniqueId}_PROTO_PROPERTY_`, "g"), '"__proto__"')
|
125
125
|
autocomplete &&
|
126
126
|
(scriptBeforeJSONValueReplacement = spliceString(
|
127
127
|
scriptBeforeJSONValueReplacement,
|
@@ -144,12 +144,12 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
144
144
|
ObjectExpression(path) {
|
145
145
|
const o = {}
|
146
146
|
parseObjectExpression(path.node, o) &&
|
147
|
-
path.replaceWith(t.identifier(`_${
|
147
|
+
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValues.push(o) - 1}_`))
|
148
148
|
},
|
149
149
|
ArrayExpression(path) {
|
150
150
|
const o = []
|
151
151
|
parseArrayExpression(path.node, o) &&
|
152
|
-
path.replaceWith(t.identifier(`_${
|
152
|
+
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValues.push(o) - 1}_`))
|
153
153
|
}
|
154
154
|
})
|
155
155
|
path.traverse({
|
@@ -182,7 +182,7 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
182
182
|
UnaryExpression(path) {
|
183
183
|
if ("void" == path.node.operator) {
|
184
184
|
if ("NumericLiteral" == path.node.argument.type && !path.node.argument.value) {
|
185
|
-
path.replaceWith(t.identifier(`_${
|
185
|
+
path.replaceWith(t.identifier(`_${uniqueId}_UNDEFINED_`))
|
186
186
|
undefinedIsReferenced = !0
|
187
187
|
}
|
188
188
|
} else if ("-" == path.node.operator && "NumericLiteral" == path.node.argument.type) {
|
@@ -195,7 +195,7 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
195
195
|
(path.parent.computed = !0)
|
196
196
|
let jsonValueIndex = jsonValues.indexOf(value)
|
197
197
|
;-1 == jsonValueIndex && (jsonValueIndex += jsonValues.push(value))
|
198
|
-
path.replaceWith(t.identifier(`_${
|
198
|
+
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
199
199
|
})()
|
200
200
|
)
|
201
201
|
path.skip()
|
@@ -204,7 +204,7 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
204
204
|
NullLiteral(path) {
|
205
205
|
let jsonValueIndex = jsonValues.indexOf(null)
|
206
206
|
;-1 == jsonValueIndex && (jsonValueIndex += jsonValues.push(null))
|
207
|
-
path.replaceWith(t.identifier(`_${
|
207
|
+
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
208
208
|
},
|
209
209
|
NumericLiteral(path) {
|
210
210
|
promises.push(
|
@@ -215,27 +215,27 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
215
215
|
(path.parent.computed = !0)
|
216
216
|
let jsonValueIndex = jsonValues.indexOf(path.node.value)
|
217
217
|
;-1 == jsonValueIndex && (jsonValueIndex += jsonValues.push(path.node.value))
|
218
|
-
path.replaceWith(t.identifier(`_${
|
218
|
+
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
219
219
|
})()
|
220
220
|
)
|
221
221
|
},
|
222
222
|
StringLiteral(path) {
|
223
|
-
path.node.value = replaceUnsafeStrings(
|
223
|
+
path.node.value = replaceUnsafeStrings(uniqueId, path.node.value)
|
224
224
|
if (JSON.stringify(path.node.value).includes("\\u00") || path.toString().length < 4) return
|
225
225
|
"key" == path.parentKey && "ObjectProperty" == path.parent.type && (path.parent.computed = !0)
|
226
226
|
let jsonValueIndex = jsonValues.indexOf(path.node.value)
|
227
227
|
;-1 == jsonValueIndex && (jsonValueIndex += jsonValues.push(path.node.value))
|
228
|
-
path.replaceWith(t.identifier(`_${
|
228
|
+
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
229
229
|
},
|
230
230
|
ObjectProperty({ node }) {
|
231
231
|
if (node.computed || "Identifier" != node.key.type || node.key.name.length < 4) return
|
232
232
|
let jsonValueIndex = jsonValues.indexOf(node.key.name)
|
233
233
|
;-1 == jsonValueIndex && (jsonValueIndex += jsonValues.push(node.key.name))
|
234
234
|
node.computed = !0
|
235
|
-
node.key = t.identifier(`_${
|
235
|
+
node.key = t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`)
|
236
236
|
},
|
237
237
|
RegExpLiteral(path) {
|
238
|
-
path.node.pattern = replaceUnsafeStrings(
|
238
|
+
path.node.pattern = replaceUnsafeStrings(uniqueId, path.node.pattern)
|
239
239
|
delete path.node.extra
|
240
240
|
}
|
241
241
|
})
|
@@ -251,17 +251,17 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
251
251
|
if ("string" != typeof jsonValues[0] || jsonValues[0].includes("\n") || jsonValues[0].includes("\t")) {
|
252
252
|
const variableDeclaration = t.variableDeclaration("let", [
|
253
253
|
t.variableDeclarator(
|
254
|
-
t.identifier(`_${
|
254
|
+
t.identifier(`_${uniqueId}_JSON_VALUE_0_`),
|
255
255
|
t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
|
256
256
|
t.memberExpression(
|
257
257
|
t.taggedTemplateExpression(
|
258
258
|
t.memberExpression(
|
259
|
-
t.callExpression(t.identifier(`$${
|
259
|
+
t.callExpression(t.identifier(`$${uniqueId}$SUBSCRIPT$scripts$quine$`), []),
|
260
260
|
t.identifier("split")
|
261
261
|
),
|
262
262
|
t.templateLiteral([t.templateElement({ raw: "\t", cooked: "\t" }, !0)], [])
|
263
263
|
),
|
264
|
-
t.identifier(`$${
|
264
|
+
t.identifier(`$${uniqueId}$SPLIT_INDEX$`),
|
265
265
|
!0
|
266
266
|
)
|
267
267
|
])
|
@@ -269,30 +269,30 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
269
269
|
])
|
270
270
|
undefinedIsReferenced &&
|
271
271
|
variableDeclaration.declarations.push(
|
272
|
-
t.variableDeclarator(t.identifier(`_${
|
272
|
+
t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`))
|
273
273
|
)
|
274
274
|
functionDeclaration.body.body.unshift(variableDeclaration)
|
275
275
|
comment = JSON.stringify(jsonValues[0])
|
276
276
|
} else {
|
277
277
|
const variableDeclaration = t.variableDeclaration("let", [
|
278
278
|
t.variableDeclarator(
|
279
|
-
t.identifier(`_${
|
279
|
+
t.identifier(`_${uniqueId}_JSON_VALUE_0_`),
|
280
280
|
t.memberExpression(
|
281
281
|
t.taggedTemplateExpression(
|
282
282
|
t.memberExpression(
|
283
|
-
t.callExpression(t.identifier(`$${
|
283
|
+
t.callExpression(t.identifier(`$${uniqueId}$SUBSCRIPT$scripts$quine$`), []),
|
284
284
|
t.identifier("split")
|
285
285
|
),
|
286
286
|
t.templateLiteral([t.templateElement({ raw: "\t", cooked: "\t" }, !0)], [])
|
287
287
|
),
|
288
|
-
t.identifier(`$${
|
288
|
+
t.identifier(`$${uniqueId}$SPLIT_INDEX$`),
|
289
289
|
!0
|
290
290
|
)
|
291
291
|
)
|
292
292
|
])
|
293
293
|
undefinedIsReferenced &&
|
294
294
|
variableDeclaration.declarations.push(
|
295
|
-
t.variableDeclarator(t.identifier(`_${
|
295
|
+
t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`))
|
296
296
|
)
|
297
297
|
functionDeclaration.body.body.unshift(variableDeclaration)
|
298
298
|
comment = jsonValues[0]
|
@@ -300,31 +300,31 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
300
300
|
else {
|
301
301
|
const variableDeclaration = t.variableDeclaration("let", [
|
302
302
|
t.variableDeclarator(
|
303
|
-
t.arrayPattern(jsonValues.map((_, index) => t.identifier(`_${
|
303
|
+
t.arrayPattern(jsonValues.map((_, index) => t.identifier(`_${uniqueId}_JSON_VALUE_${index}_`))),
|
304
304
|
t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
|
305
305
|
t.memberExpression(
|
306
306
|
t.taggedTemplateExpression(
|
307
307
|
t.memberExpression(
|
308
|
-
t.callExpression(t.identifier(`$${
|
308
|
+
t.callExpression(t.identifier(`$${uniqueId}$SUBSCRIPT$scripts$quine$`), []),
|
309
309
|
t.identifier("split")
|
310
310
|
),
|
311
311
|
t.templateLiteral([t.templateElement({ raw: "\t", cooked: "\t" }, !0)], [])
|
312
312
|
),
|
313
|
-
t.identifier(`$${
|
313
|
+
t.identifier(`$${uniqueId}$SPLIT_INDEX$`),
|
314
314
|
!0
|
315
315
|
)
|
316
316
|
])
|
317
317
|
)
|
318
318
|
])
|
319
319
|
undefinedIsReferenced &&
|
320
|
-
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${
|
320
|
+
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`)))
|
321
321
|
functionDeclaration.body.body.unshift(variableDeclaration)
|
322
322
|
comment = JSON.stringify(jsonValues)
|
323
323
|
}
|
324
324
|
} else
|
325
325
|
undefinedIsReferenced &&
|
326
326
|
functionDeclaration.body.body.unshift(
|
327
|
-
t.variableDeclaration("let", [t.variableDeclarator(t.identifier(`_${
|
327
|
+
t.variableDeclaration("let", [t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`))])
|
328
328
|
)
|
329
329
|
code = generate(file).code
|
330
330
|
}
|
@@ -356,7 +356,7 @@ async function minify(file, { uniqueID = "00000000000", mangleNames = !1, forceQ
|
|
356
356
|
getFunctionBodyStart(code) + 1
|
357
357
|
)
|
358
358
|
code = code.replace(
|
359
|
-
`$${
|
359
|
+
`$${uniqueId}$SPLIT_INDEX$`,
|
360
360
|
await minifyNumber(code.split("\t").findIndex(part => part == comment))
|
361
361
|
)
|
362
362
|
}
|
@@ -1 +1 @@
|
|
1
|
-
export declare const postprocess: (code: string, seclevel: number,
|
1
|
+
export declare const postprocess: (code: string, seclevel: number, uniqueId: string) => string;
|
@@ -1,20 +1,20 @@
|
|
1
|
-
const postprocess = (code, seclevel,
|
1
|
+
const postprocess = (code, seclevel, uniqueId) =>
|
2
2
|
code
|
3
3
|
.replace(/^function\s*\w+\(/, "function(")
|
4
|
-
.replace(RegExp(`\\$${
|
5
|
-
.replace(RegExp(`\\$${
|
6
|
-
.replace(RegExp(`\\$${
|
7
|
-
.replace(RegExp(`\\$${
|
8
|
-
.replace(RegExp(`\\$${
|
9
|
-
.replace(RegExp(`\\$${
|
10
|
-
.replace(RegExp(`\\$${
|
11
|
-
.replace(RegExp(`\\$${
|
12
|
-
.replace(RegExp(`\\$${
|
13
|
-
.replace(RegExp(`\\$${
|
14
|
-
.replace(RegExp(`\\$${
|
15
|
-
.replace(RegExp(`\\$${
|
16
|
-
.replace(RegExp(`\\$${
|
17
|
-
.replace(RegExp(`\\$${
|
18
|
-
.replace(RegExp(`\\$${
|
19
|
-
.replace(RegExp(`\\$${
|
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
20
|
export { postprocess }
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import type { LaxPartial } from "@samual/lib";
|
2
2
|
export type PreprocessOptions = LaxPartial<{
|
3
|
-
|
3
|
+
uniqueId: string;
|
4
4
|
}>;
|
5
5
|
/** @param code source code for preprocessing
|
6
6
|
* @param options {@link PreprocessOptions details} */
|
7
|
-
export declare function preprocess(code: string, {
|
7
|
+
export declare function preprocess(code: string, { uniqueId }?: PreprocessOptions): Promise<{
|
8
8
|
code: string;
|
9
9
|
}>;
|
@@ -7,8 +7,8 @@ import { spliceString } from "@samual/lib/spliceString"
|
|
7
7
|
import { resolve } from "import-meta-resolve"
|
8
8
|
const { default: traverse } = babelTraverse,
|
9
9
|
{ default: generate } = babelGenerator
|
10
|
-
async function preprocess(code, {
|
11
|
-
assert(/^\w{11}$/.test(
|
10
|
+
async function preprocess(code, { uniqueId = "00000000000" } = {}) {
|
11
|
+
assert(/^\w{11}$/.test(uniqueId), "src/processScript/preprocess.ts:22:36")
|
12
12
|
const sourceCode = code
|
13
13
|
let lengthBefore, file, program
|
14
14
|
do {
|
@@ -61,9 +61,9 @@ async function preprocess(code, { uniqueID = "00000000000" } = {}) {
|
|
61
61
|
else if ((match = /^#[0-4fhmln]?s\./.exec(codeSlice))) code = spliceString(code, "$", error.pos, 1)
|
62
62
|
else if ((match = /^#D[^\w$]/.exec(codeSlice))) code = spliceString(code, "$", error.pos, 1)
|
63
63
|
else if ((match = /^#FMCL/.exec(codeSlice)))
|
64
|
-
code = spliceString(code, `$${
|
64
|
+
code = spliceString(code, `$${uniqueId}$FMCL$`, error.pos, match[0].length)
|
65
65
|
else if ((match = /^#G/.exec(codeSlice)))
|
66
|
-
code = spliceString(code, `$${
|
66
|
+
code = spliceString(code, `$${uniqueId}$GLOBAL$`, error.pos, match[0].length)
|
67
67
|
else {
|
68
68
|
if (!(match = /^#db\./.exec(codeSlice))) throw error
|
69
69
|
code = spliceString(code, "$", error.pos, 1)
|
@@ -2,4 +2,4 @@ import type { NodePath } from "@babel/traverse";
|
|
2
2
|
import type { Identifier, Program } from "@babel/types";
|
3
3
|
export declare function getReferencePathsToGlobal(name: string, program: NodePath<Program>): NodePath<Identifier>[];
|
4
4
|
export declare const includesIllegalString: (toCheck: string) => boolean;
|
5
|
-
export declare const replaceUnsafeStrings: (
|
5
|
+
export declare const replaceUnsafeStrings: (uniqueId: string, toReplace: string) => string;
|
package/processScript/shared.js
CHANGED
@@ -16,17 +16,17 @@ const includesIllegalString = toCheck =>
|
|
16
16
|
toCheck.includes("__D_S") ||
|
17
17
|
toCheck.includes("__FMCL_") ||
|
18
18
|
toCheck.includes("__G_"),
|
19
|
-
replaceUnsafeStrings = (
|
19
|
+
replaceUnsafeStrings = (uniqueId, toReplace) =>
|
20
20
|
toReplace
|
21
|
-
.replaceAll("SC$", `$${
|
22
|
-
.replaceAll("DB$", `$${
|
23
|
-
.replaceAll("__D_S", `$${
|
24
|
-
.replaceAll("__FMCL_", `$${
|
25
|
-
.replaceAll("__G_", `$${
|
26
|
-
.replaceAll("//", `$${
|
27
|
-
.replaceAll(/#[0-4fhmln]?s(?:\.[_a-z][\d_a-z]{0,24}){2}\(/g, `$${
|
28
|
-
.replaceAll(/#db\.(?<methodName>[irfu]|u1|us|ObjectId)\(/g, `$${
|
29
|
-
.replaceAll("#D(", `$${
|
30
|
-
.replaceAll("#FMCL", `$${
|
31
|
-
.replaceAll("#G", `$${
|
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
32
|
export { getReferencePathsToGlobal, includesIllegalString, replaceUnsafeStrings }
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import type { File } from "@babel/types";
|
2
2
|
import type { LaxPartial } from "@samual/lib";
|
3
3
|
export type TransformOptions = LaxPartial<{
|
4
|
-
/** 11 a-z 0-9 characters */
|
4
|
+
/** 11 a-z 0-9 characters */ uniqueId: string;
|
5
5
|
/** the user going to be hosting this script (or set to `true` if not yet known) */ scriptUser: string | true;
|
6
6
|
seclevel: number;
|
7
7
|
}> & {
|
@@ -13,7 +13,7 @@ export type TransformOptions = LaxPartial<{
|
|
13
13
|
* @param file babel ast node representing a file containing preprocessed code
|
14
14
|
* @param sourceCode the original untouched source code
|
15
15
|
* @param options {@link TransformOptions details} */
|
16
|
-
export declare function transform(file: File, sourceCode: string, {
|
16
|
+
export declare function transform(file: File, sourceCode: string, { uniqueId, scriptUser, scriptName, seclevel }: TransformOptions): {
|
17
17
|
file: File;
|
18
18
|
seclevel: number;
|
19
19
|
};
|