hackmud-script-manager 0.19.1-a81047f → 0.19.1-b720a68

Sign up to get free protection for your applications and to get access to all the features.
package/bin/hsm.js CHANGED
@@ -12,7 +12,7 @@ import { pull } from "../pull.js"
12
12
  import { syncMacros } from "../syncMacros.js"
13
13
  import "@samual/lib/readDirectoryWithStats"
14
14
  import "@samual/lib/copyFilePersistent"
15
- const version = "0.19.1-a81047f",
15
+ const version = "0.19.1-b720a68",
16
16
  options = new Map(),
17
17
  commands = [],
18
18
  userColours = new Cache(user => {
@@ -280,7 +280,7 @@ switch (commands[0]) {
280
280
  scriptUser =
281
281
  "scripts" == basename(resolve(target, "..")) && "hackmud" == basename(resolve(target, "../../..")) ?
282
282
  basename(resolve(target, "../.."))
283
- : "UNKNOWN",
283
+ : void 0,
284
284
  optionsHasNoMinify = options.has("no-minify")
285
285
  if ((optionsHasNoMinify || options.has("skip-minify")) && options.has("mangle-names")) {
286
286
  logError(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hackmud-script-manager",
3
- "version": "0.19.1-a81047f",
3
+ "version": "0.19.1-b720a68",
4
4
  "description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
5
5
  "keywords": [
6
6
  "api",
@@ -47,7 +47,7 @@ async function processScript(
47
47
  uniqueID = Math.floor(Math.random() * 2 ** 52)
48
48
  .toString(36)
49
49
  .padStart(11, "0"),
50
- scriptUser = "UNKNOWN",
50
+ scriptUser,
51
51
  scriptName = "UNKNOWN",
52
52
  filePath,
53
53
  mangleNames = !1,
@@ -1,4 +1,5 @@
1
1
  import type { File } from "@babel/types";
2
+ import type { LaxPartial } from "@samual/lib";
2
3
  export type TransformOptions = {
3
4
  /** 11 a-z 0-9 characters */ uniqueID: string;
4
5
  /** the user going to be hosting this script (or set to `true` if not yet known) */ scriptUser: string | true;
@@ -11,7 +12,7 @@ export type TransformOptions = {
11
12
  * @param file babel ast node representing a file containing preprocessed code
12
13
  * @param sourceCode the original untouched source code
13
14
  * @param options {@link TransformOptions details} */
14
- export declare function transform(file: File, sourceCode: string, { uniqueID, scriptUser, scriptName, seclevel }?: Partial<TransformOptions>): {
15
+ export declare function transform(file: File, sourceCode: string, { uniqueID, scriptUser, scriptName, seclevel }?: LaxPartial<TransformOptions>): {
15
16
  file: File;
16
17
  seclevel: number;
17
18
  };
@@ -24,7 +24,7 @@ const { default: traverse } = babelTraverse,
24
24
  function transform(
25
25
  file,
26
26
  sourceCode,
27
- { uniqueID = "00000000000", scriptUser = "UNKNOWN", scriptName = "UNKNOWN", seclevel = 4 } = {}
27
+ { uniqueID = "00000000000", scriptUser, scriptName = "UNKNOWN", seclevel = 4 } = {}
28
28
  ) {
29
29
  const topFunctionName = `_${uniqueID}_SCRIPT_`,
30
30
  exports = new Map(),
@@ -42,9 +42,14 @@ function transform(
42
42
  if (program.scope.hasGlobal("_BUILD_DATE"))
43
43
  for (const referencePath of getReferencePathsToGlobal("_BUILD_DATE", program))
44
44
  referencePath.replaceWith(t.numericLiteral(Date.now()))
45
+ let uniqueIdScriptUserNeeded = !1
45
46
  if (program.scope.hasGlobal("_SCRIPT_USER"))
46
47
  for (const referencePath of getReferencePathsToGlobal("_SCRIPT_USER", program))
47
- referencePath.replaceWith(t.stringLiteral(1 == scriptUser ? `$${uniqueID}$SCRIPT_USER$` : scriptUser))
48
+ if (null == scriptUser) {
49
+ uniqueIdScriptUserNeeded = !0
50
+ referencePath.replaceWith(t.identifier(`_${uniqueID}_SCRIPT_USER_`))
51
+ } else
52
+ referencePath.replaceWith(t.stringLiteral(1 == scriptUser ? `$${uniqueID}$SCRIPT_USER$` : scriptUser))
48
53
  if (program.scope.hasGlobal("_SCRIPT_NAME"))
49
54
  for (const referencePath of getReferencePathsToGlobal("_SCRIPT_NAME", program))
50
55
  referencePath.replaceWith(t.stringLiteral(1 == scriptName ? `$${uniqueID}$SCRIPT_NAME$` : scriptName))
@@ -64,30 +69,30 @@ function transform(
64
69
  const referencePath = FunctionReferencePaths[0]
65
70
  assert(
66
71
  "MemberExpression" == referencePath.parent.type,
67
- "src/processScript/transform.ts:89:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
72
+ "src/processScript/transform.ts:99:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
68
73
  )
69
74
  assert(
70
75
  "Identifier" == referencePath.parent.property.type,
71
- "src/processScript/transform.ts:94:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
76
+ "src/processScript/transform.ts:104:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
72
77
  )
73
78
  assert(
74
79
  "prototype" == referencePath.parent.property.name,
75
- "src/processScript/transform.ts:99:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
80
+ "src/processScript/transform.ts:109:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
76
81
  )
77
82
  referencePath.parentPath.replaceWith(createGetFunctionPrototypeNode())
78
83
  } else {
79
84
  for (const referencePath of FunctionReferencePaths) {
80
85
  assert(
81
86
  "MemberExpression" == referencePath.parent.type,
82
- "src/processScript/transform.ts:107:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
87
+ "src/processScript/transform.ts:117:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
83
88
  )
84
89
  assert(
85
90
  "Identifier" == referencePath.parent.property.type,
86
- "src/processScript/transform.ts:112:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
91
+ "src/processScript/transform.ts:122:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
87
92
  )
88
93
  assert(
89
94
  "prototype" == referencePath.parent.property.name,
90
- "src/processScript/transform.ts:117:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
95
+ "src/processScript/transform.ts:127:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
91
96
  )
92
97
  functionDotPrototypeIsReferencedMultipleTimes = !0
93
98
  referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`))
@@ -123,12 +128,12 @@ function transform(
123
128
  const neededDbMethodLets = new Set()
124
129
  if (program.scope.hasGlobal("$db"))
125
130
  for (const referencePath of getReferencePathsToGlobal("$db", program)) {
126
- assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:171:69")
127
- assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:172:72")
131
+ assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:181:69")
132
+ assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:182:72")
128
133
  const databaseOpMethodName = referencePath.parentPath.node.property.name
129
134
  assert(
130
135
  validDBMethods.includes(databaseOpMethodName),
131
- `src/processScript/transform.ts:178:8 invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
136
+ `src/processScript/transform.ts:188:8 invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
132
137
  )
133
138
  if ("CallExpression" == referencePath.parentPath.parentPath?.type)
134
139
  referencePath.parentPath.replaceWith(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`))
@@ -161,7 +166,7 @@ function transform(
161
166
  if (program.scope.hasGlobal("Object"))
162
167
  for (const referencePath of getReferencePathsToGlobal("Object", program))
163
168
  if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
164
- assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:225:64")
169
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:235:64")
165
170
  if ("getPrototypeOf" == referencePath.parent.property.name) {
166
171
  referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`))
167
172
  needGetPrototypeOf = !0
@@ -171,7 +176,7 @@ function transform(
171
176
  if (program.scope.hasGlobal("console"))
172
177
  for (const referencePath of getReferencePathsToGlobal("console", program))
173
178
  if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
174
- assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:240:64")
179
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:250:64")
175
180
  referencePath.parentPath.replaceWith(
176
181
  t.identifier(`_${uniqueID}_CONSOLE_METHOD_${referencePath.parent.property.name}_`)
177
182
  )
@@ -179,13 +184,13 @@ function transform(
179
184
  }
180
185
  const lastStatement = program.node.body.at(-1)
181
186
  let exportDefaultName
182
- assert(lastStatement, "src/processScript/transform.ts:254:27 program is empty")
187
+ assert(lastStatement, "src/processScript/transform.ts:264:27 program is empty")
183
188
  if ("ExportNamedDeclaration" == lastStatement.type) {
184
189
  program.node.body.pop()
185
190
  for (const specifier of lastStatement.specifiers) {
186
191
  assert(
187
192
  "ExportSpecifier" == specifier.type,
188
- `src/processScript/transform.ts:260:51 ${specifier.type} is currently unsupported`
193
+ `src/processScript/transform.ts:270:51 ${specifier.type} is currently unsupported`
189
194
  )
190
195
  const exportedName =
191
196
  "Identifier" == specifier.exported.type ? specifier.exported.name : specifier.exported.value
@@ -261,6 +266,28 @@ function transform(
261
266
  [t.identifier("context"), t.identifier("args")],
262
267
  t.blockStatement([])
263
268
  )
269
+ if (uniqueIdScriptUserNeeded) {
270
+ const mainFunctionParams = mainFunction.params
271
+ mainFunction.params = [t.restElement(t.identifier(`_${uniqueID}_PARAMS_`))]
272
+ mainFunction.body.body.unshift(
273
+ t.variableDeclaration("let", [
274
+ t.variableDeclarator(t.arrayPattern(mainFunctionParams), t.identifier(`_${uniqueID}_PARAMS_`)),
275
+ t.variableDeclarator(
276
+ t.arrayPattern([t.identifier(`_${uniqueID}_SCRIPT_USER_`)]),
277
+ t.callExpression(
278
+ t.memberExpression(
279
+ t.memberExpression(
280
+ t.memberExpression(t.identifier(`_${uniqueID}_PARAMS_`), t.numericLiteral(0), !0),
281
+ t.identifier("this_script")
282
+ ),
283
+ t.identifier("split")
284
+ ),
285
+ [t.stringLiteral(".")]
286
+ )
287
+ )
288
+ ])
289
+ )
290
+ }
264
291
  program.node.body = [mainFunction]
265
292
  if (globalBlock.body.length) {
266
293
  ;(exports.size || liveExports.size) &&
@@ -286,11 +313,11 @@ function transform(
286
313
  let hoistedGlobalBlockFunctions = 0
287
314
  for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
288
315
  if ("VariableDeclaration" == globalBlockStatement.type) {
289
- assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:370:59")
316
+ assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:404:59")
290
317
  const declarator = globalBlockStatement.declarations[0]
291
318
  assert(
292
319
  "Identifier" == declarator.id.type,
293
- `src/processScript/transform.ts:374:51 declarator.id.type was "${declarator.id.type}"`
320
+ `src/processScript/transform.ts:408:51 declarator.id.type was "${declarator.id.type}"`
294
321
  )
295
322
  program.scope.crawl()
296
323
  if (program.scope.hasGlobal(declarator.id.name)) {
@@ -305,9 +332,9 @@ function transform(
305
332
  Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
306
333
  ) {
307
334
  const binding = program.scope.getBinding(declarator.id.name)
308
- assert(binding, "src/processScript/transform.ts:393:23")
335
+ assert(binding, "src/processScript/transform.ts:427:23")
309
336
  for (const referencePath of binding.referencePaths) {
310
- assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:396:56")
337
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:430:56")
311
338
  referencePath.replaceWith(
312
339
  t.memberExpression(
313
340
  t.identifier(`$${uniqueID}$GLOBAL$`),
@@ -351,16 +378,16 @@ function transform(
351
378
  } else globalBlockVariables.add(declarator.id.name)
352
379
  } else if ("ClassDeclaration" == globalBlockStatement.type) {
353
380
  program.scope.crawl()
354
- assert(globalBlockStatement.id, "src/processScript/transform.ts:447:37")
381
+ assert(globalBlockStatement.id, "src/processScript/transform.ts:481:37")
355
382
  if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
356
383
  globalBlock.body.splice(globalBlockIndex, 1)
357
384
  const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
358
385
  [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
359
386
  program.scope.crawl()
360
387
  const binding = program.scope.getBinding(globalBlockStatement.id.name)
361
- assert(binding, "src/processScript/transform.ts:459:22")
388
+ assert(binding, "src/processScript/transform.ts:493:22")
362
389
  for (const referencePath of binding.referencePaths) {
363
- assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:462:55")
390
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:496:55")
364
391
  referencePath.replaceWith(
365
392
  t.memberExpression(
366
393
  t.identifier(`$${uniqueID}$GLOBAL$`),
@@ -527,7 +554,7 @@ function transform(
527
554
  }
528
555
  },
529
556
  ClassBody({ node: classBody, scope, parent }) {
530
- assert(t.isClass(parent), "src/processScript/transform.ts:629:30")
557
+ assert(t.isClass(parent), "src/processScript/transform.ts:663:30")
531
558
  let thisIsReferenced = !1
532
559
  for (const classMethod of classBody.body) {
533
560
  if ("ClassMethod" != classMethod.type) continue
@@ -633,23 +660,23 @@ function transform(
633
660
  }
634
661
  function processFakeSubscriptObject(fakeSubscriptObjectName) {
635
662
  for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
636
- assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:743:60")
663
+ assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:777:60")
637
664
  assert("Identifier" == referencePath.parent.property.type)
638
665
  assert(
639
666
  "MemberExpression" == referencePath.parentPath.parentPath?.node.type,
640
- "src/processScript/transform.ts:745:81"
667
+ "src/processScript/transform.ts:779:81"
641
668
  )
642
669
  assert(
643
670
  "Identifier" == referencePath.parentPath.parentPath.node.property.type,
644
- "src/processScript/transform.ts:746:83"
671
+ "src/processScript/transform.ts:780:83"
645
672
  )
646
673
  assert(
647
674
  /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
648
- `src/processScript/transform.ts:750:8 invalid user "${referencePath.parent.property.name}" in subscript`
675
+ `src/processScript/transform.ts:784:8 invalid user "${referencePath.parent.property.name}" in subscript`
649
676
  )
650
677
  assert(
651
678
  /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
652
- `src/processScript/transform.ts:755:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
679
+ `src/processScript/transform.ts:789:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
653
680
  )
654
681
  if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
655
682
  referencePath.parentPath.parentPath.replaceWith(