hackmud-script-manager 0.19.0 → 0.19.1-07fc9cb

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.
Files changed (39) hide show
  1. package/README.md +0 -2
  2. package/bin/hsm.js +435 -1
  3. package/constants.js +3 -1
  4. package/generateTypeDeclaration.js +68 -1
  5. package/index.js +51 -1
  6. package/package.json +38 -39
  7. package/processScript/index.js +333 -1
  8. package/processScript/minify.js +444 -1
  9. package/processScript/postprocess.js +20 -1
  10. package/processScript/preprocess.js +105 -1
  11. package/processScript/shared.js +32 -1
  12. package/processScript/transform.js +668 -1
  13. package/pull.js +11 -1
  14. package/push.js +269 -1
  15. package/{bin → src/bin}/hsm.d.ts +0 -0
  16. package/src/generateTypeDeclaration.d.ts +2 -0
  17. package/src/processScript/index.d.ts +31 -0
  18. package/src/processScript/minify.d.ts +18 -0
  19. package/src/processScript/preprocess.d.ts +9 -0
  20. package/{processScript → src/processScript}/shared.d.ts +1 -1
  21. package/src/processScript/transform.d.ts +18 -0
  22. package/src/pull.d.ts +6 -0
  23. package/src/push.d.ts +29 -0
  24. package/{syncMacros.d.ts → src/syncMacros.d.ts} +1 -1
  25. package/src/watch.d.ts +15 -0
  26. package/syncMacros.js +43 -1
  27. package/watch.js +208 -1
  28. package/generateTypeDeclaration.d.ts +0 -2
  29. package/processScript/index.d.ts +0 -40
  30. package/processScript/minify.d.ts +0 -24
  31. package/processScript/preprocess.d.ts +0 -12
  32. package/processScript/transform.d.ts +0 -24
  33. package/pull.d.ts +0 -9
  34. package/push.d.ts +0 -37
  35. package/tsconfig.tsbuildinfo +0 -1
  36. package/watch.d.ts +0 -20
  37. /package/{constants.d.ts → src/constants.d.ts} +0 -0
  38. /package/{index.d.ts → src/index.d.ts} +0 -0
  39. /package/{processScript → src/processScript}/postprocess.d.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hackmud-script-manager",
3
- "version": "0.19.0",
3
+ "version": "0.19.1-07fc9cb",
4
4
  "description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
5
5
  "keywords": [
6
6
  "api",
@@ -11,8 +11,8 @@
11
11
  "minifier",
12
12
  "hackmd",
13
13
  "hackmud",
14
- "javascipt",
15
- "typescipt",
14
+ "javascript",
15
+ "typescript",
16
16
  "typescript-support",
17
17
  "golf",
18
18
  "golfer"
@@ -27,50 +27,47 @@
27
27
  "url": "https://github.com/samualtnorman/hackmud-script-manager.git"
28
28
  },
29
29
  "dependencies": {
30
- "@babel/generator": "^7.23.6",
31
- "@babel/parser": "^7.23.6",
32
- "@babel/plugin-proposal-decorators": "^7.23.7",
33
- "@babel/plugin-proposal-destructuring-private": "^7.23.3",
34
- "@babel/plugin-proposal-do-expressions": "^7.23.3",
35
- "@babel/plugin-proposal-explicit-resource-management": "^7.23.3",
36
- "@babel/plugin-proposal-function-bind": "^7.23.3",
37
- "@babel/plugin-proposal-function-sent": "^7.23.3",
38
- "@babel/plugin-proposal-partial-application": "^7.23.3",
39
- "@babel/plugin-proposal-pipeline-operator": "^7.23.3",
40
- "@babel/plugin-proposal-record-and-tuple": "^7.23.3",
41
- "@babel/plugin-proposal-throw-expressions": "^7.23.3",
42
- "@babel/plugin-transform-class-properties": "^7.23.3",
43
- "@babel/plugin-transform-class-static-block": "^7.23.4",
44
- "@babel/plugin-transform-exponentiation-operator": "^7.23.3",
45
- "@babel/plugin-transform-json-strings": "^7.23.4",
46
- "@babel/plugin-transform-logical-assignment-operators": "^7.23.4",
47
- "@babel/plugin-transform-nullish-coalescing-operator": "^7.23.4",
48
- "@babel/plugin-transform-numeric-separator": "^7.23.4",
49
- "@babel/plugin-transform-object-rest-spread": "^7.23.4",
50
- "@babel/plugin-transform-optional-catch-binding": "^7.23.4",
51
- "@babel/plugin-transform-optional-chaining": "^7.23.4",
52
- "@babel/plugin-transform-private-property-in-object": "^7.23.4",
53
- "@babel/plugin-transform-typescript": "^7.23.6",
54
- "@babel/plugin-transform-unicode-sets-regex": "^7.23.3",
55
- "@babel/traverse": "^7.23.7",
56
- "@babel/types": "^7.23.6",
30
+ "@babel/generator": "^7.24.4",
31
+ "@babel/parser": "^7.24.4",
32
+ "@babel/plugin-proposal-decorators": "^7.24.1",
33
+ "@babel/plugin-proposal-destructuring-private": "^7.24.1",
34
+ "@babel/plugin-proposal-do-expressions": "^7.24.1",
35
+ "@babel/plugin-proposal-explicit-resource-management": "^7.24.1",
36
+ "@babel/plugin-proposal-function-bind": "^7.24.1",
37
+ "@babel/plugin-proposal-function-sent": "^7.24.1",
38
+ "@babel/plugin-proposal-partial-application": "^7.24.1",
39
+ "@babel/plugin-proposal-pipeline-operator": "^7.24.1",
40
+ "@babel/plugin-proposal-record-and-tuple": "^7.24.1",
41
+ "@babel/plugin-proposal-throw-expressions": "^7.24.1",
42
+ "@babel/plugin-transform-class-properties": "^7.24.1",
43
+ "@babel/plugin-transform-class-static-block": "^7.24.4",
44
+ "@babel/plugin-transform-exponentiation-operator": "^7.24.1",
45
+ "@babel/plugin-transform-json-strings": "^7.24.1",
46
+ "@babel/plugin-transform-logical-assignment-operators": "^7.24.1",
47
+ "@babel/plugin-transform-nullish-coalescing-operator": "^7.24.1",
48
+ "@babel/plugin-transform-numeric-separator": "^7.24.1",
49
+ "@babel/plugin-transform-object-rest-spread": "^7.24.1",
50
+ "@babel/plugin-transform-optional-catch-binding": "^7.24.1",
51
+ "@babel/plugin-transform-optional-chaining": "^7.24.1",
52
+ "@babel/plugin-transform-private-property-in-object": "^7.24.1",
53
+ "@babel/plugin-transform-typescript": "^7.24.4",
54
+ "@babel/plugin-transform-unicode-sets-regex": "^7.24.1",
55
+ "@babel/traverse": "^7.24.1",
56
+ "@babel/types": "^7.24.0",
57
57
  "@bloomberg/record-tuple-polyfill": "^0.0.4",
58
58
  "@rollup/plugin-babel": "^6.0.4",
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.9.1",
62
+ "@samual/lib": "0.10.2-e64c5bc",
63
63
  "acorn": "^8.11.3",
64
64
  "chalk": "^5.3.0",
65
- "chokidar": "^3.5.3",
65
+ "chokidar": "^3.6.0",
66
66
  "import-meta-resolve": "^4.0.0",
67
- "prettier": "^3.2.2",
67
+ "prettier": "^3.2.5",
68
68
  "proxy-polyfill": "^0.3.2",
69
- "rollup": "^4.9.5",
70
- "terser": "^5.26.0"
71
- },
72
- "engines": {
73
- "node": ">=20"
69
+ "rollup": "^4.14.2",
70
+ "terser": "^5.30.3"
74
71
  },
75
72
  "type": "module",
76
73
  "exports": {
@@ -78,7 +75,9 @@
78
75
  "./*.js": "./*.js"
79
76
  },
80
77
  "bin": {
81
- "hsm.d": "bin/hsm.d.ts",
82
78
  "hsm": "bin/hsm.js"
79
+ },
80
+ "engines": {
81
+ "node": "^18 || >=20"
83
82
  }
84
83
  }
@@ -1 +1,333 @@
1
- import e from"@babel/generator";import{parse as r}from"@babel/parser";import o from"@babel/plugin-proposal-decorators";import t from"@babel/plugin-proposal-destructuring-private";import a from"@babel/plugin-proposal-explicit-resource-management";import l from"@babel/plugin-transform-class-properties";import p from"@babel/plugin-transform-class-static-block";import s from"@babel/plugin-transform-exponentiation-operator";import i from"@babel/plugin-transform-json-strings";import n from"@babel/plugin-transform-logical-assignment-operators";import m from"@babel/plugin-transform-nullish-coalescing-operator";import c from"@babel/plugin-transform-numeric-separator";import u from"@babel/plugin-transform-object-rest-spread";import f from"@babel/plugin-transform-optional-catch-binding";import b from"@babel/plugin-transform-optional-chaining";import d from"@babel/plugin-transform-private-property-in-object";import g from"@babel/plugin-transform-unicode-sets-regex";import h from"@babel/traverse";import y from"@babel/types";import{babel as w}from"@rollup/plugin-babel";import k from"@rollup/plugin-commonjs";import x from"@rollup/plugin-json";import v from"@rollup/plugin-node-resolve";import{assert as j}from"@samual/lib/assert";import{resolve as C}from"path";import E from"prettier";import{rollup as S}from"rollup";import{supportedExtensions as L}from"../constants.js";import{minify as $}from"./minify.js";import{postprocess as N}from"./postprocess.js";import{preprocess as I}from"./preprocess.js";import{includesIllegalString as P,replaceUnsafeStrings as D}from"./shared.js";import{transform as _}from"./transform.js";import"@samual/lib/countHackmudCharacters";import"@samual/lib/spliceString";import"acorn";import"terser";import"import-meta-resolve";import"@samual/lib/clearObject";const{format:O}=E,{default:M}=e,{default:T}=h,processScript=async(e,{minify:h=!0,uniqueID:E=Math.floor(Math.random()*2**52).toString(36).padStart(11,"0"),scriptUser:U="UNKNOWN",scriptName:W="UNKNOWN",filePath:q,mangleNames:A=!1,forceQuineCheats:F}={})=>{j(/^\w{11}$/.exec(E));const H=e;let z,K;const Q=/^function\s*\(.+\/\/(?<autocomplete>.+)/.exec(e);if(Q)e=`export default ${e}`,({autocomplete:z}=Q.groups);else for(const r of e.split("\n")){const e=/^\s*\/\/(?<commentContent>.+)/.exec(r);if(!e)break;const o=e.groups.commentContent.trim();if(o.startsWith("@autocomplete "))z=o.slice(14).trimStart();else if(o.startsWith("@seclevel ")){const e=o.slice(10).trimStart().toLowerCase();switch(e){case"fullsec":case"full":case"fs":case"4s":case"f":case"4":K=4;break;case"highsec":case"high":case"hs":case"3s":case"h":case"3":K=3;break;case"midsec":case"mid":case"ms":case"2s":case"m":case"2":K=2;break;case"lowsec":case"low":case"ls":case"1s":case"l":case"1":K=1;break;case"nullsec":case"null":case"ns":case"0s":case"n":case"0":K=0;break;default:throw new Error(`unrecognised seclevel "${e}"`)}}}j(/^\w{11}$/.exec(E));const V=[[o.default,{decoratorsBeforeExport:!0}],[l.default],[p.default],[d.default],[n.default],[c.default],[m.default],[b.default],[f.default],[i.default],[u.default],[s.default],[g.default],[t.default],[a.default]];let B;if(q)if(B=C(q),q.endsWith(".ts"))V.push([(await import("@babel/plugin-transform-typescript")).default,{allowDeclareFields:!0,optimizeConstEnums:!0}]);else{const[e,r,o,t,a,l,p]=await Promise.all([import("@babel/plugin-proposal-do-expressions"),import("@babel/plugin-proposal-function-bind"),import("@babel/plugin-proposal-function-sent"),import("@babel/plugin-proposal-partial-application"),import("@babel/plugin-proposal-pipeline-operator"),import("@babel/plugin-proposal-throw-expressions"),import("@babel/plugin-proposal-record-and-tuple")]);V.push([e.default],[r.default],[o.default],[t.default],[a.default,{proposal:"hack",topicToken:"%"}],[l.default],[p.default,{syntaxType:"hash",importPolyfill:!0}])}else{B=`${E}.ts`;const[e,r,o,t,a,l,p,s]=await Promise.all([import("@babel/plugin-transform-typescript"),import("@babel/plugin-proposal-do-expressions"),import("@babel/plugin-proposal-function-bind"),import("@babel/plugin-proposal-function-sent"),import("@babel/plugin-proposal-partial-application"),import("@babel/plugin-proposal-pipeline-operator"),import("@babel/plugin-proposal-throw-expressions"),import("@babel/plugin-proposal-record-and-tuple")]);V.push([e.default,{allowDeclareFields:!0,optimizeConstEnums:!0}],[r.default],[o.default],[t.default],[a.default],[l.default,{proposal:"hack",topicToken:"%"}],[p.default],[s.default,{syntaxType:"hash",importPolyfill:!0}])}const G=await S({input:B,plugins:[{name:"hackmud-script-manager",transform:async e=>(await I(e,{uniqueID:E})).code},w({babelHelpers:"bundled",plugins:V,configFile:!1,extensions:L}),k(),v({extensions:L}),x()],treeshake:{moduleSideEffects:!1}}),R=["NULLSEC","LOWSEC","MIDSEC","HIGHSEC","FULLSEC"];e=(await G.generate({})).output[0].code;const{file:J,seclevel:X}=_(r(e,{sourceType:"module"}),H,{uniqueID:E,scriptUser:U,scriptName:W});if(null!=K&&X<K)throw new Error(`detected seclevel ${R[X]} is lower than stated seclevel ${R[K]}`);if(e=M(J).code,h?e=await $(J,{uniqueID:E,mangleNames:A,forceQuineCheats:F,autocomplete:z}):(T(J,{MemberExpression({node:e}){e.computed||(j("Identifier"==e.property.type),"prototype"==e.property.name?(e.computed=!0,e.property=y.stringLiteral("prototype")):"__proto__"==e.property.name?(e.computed=!0,e.property=y.stringLiteral("__proto__")):P(e.property.name)&&(e.computed=!0,e.property=y.stringLiteral(D(E,e.property.name))))},VariableDeclarator(e){const renameVariables=r=>{switch(r.type){case"Identifier":P(r.name)&&e.scope.rename(r.name,`$${Math.floor(Math.random()*2**52).toString(36).padStart(11,"0")}`);break;case"ObjectPattern":for(const e of r.properties)j("ObjectProperty"==e.type),renameVariables(e.value);break;case"ArrayPattern":for(const e of r.elements)e&&renameVariables(e);break;default:throw new Error(`unknown lValue type "${r.type}"`)}};renameVariables(e.node.id)},ObjectProperty({node:e}){"Identifier"==e.key.type&&P(e.key.name)&&(e.key=y.stringLiteral(D(E,e.key.name)),e.shorthand=!1)},StringLiteral({node:e}){e.value=D(E,e.value)},TemplateLiteral({node:e}){for(const r of e.quasis)r.value.cooked?(r.value.cooked=D(E,r.value.cooked),r.value.raw=r.value.cooked.replaceAll("\\","\\\\").replaceAll("`","\\`").replaceAll("${","$\\{")):r.value.raw=D(E,r.value.raw)},RegExpLiteral(e){e.node.pattern=D(E,e.node.pattern),delete e.node.extra}}),e=await O(M(J,{comments:!1}).code,{parser:"babel",arrowParens:"avoid",semi:!1,trailingComma:"none"})),e=N(e,X,E),P(e))throw new Error('you found a weird edge case where I wasn\'t able to replace illegal strings like "SC$", please report thx');return{script:e,warnings:[]}};export{processScript as default,$ as minify,N as postprocess,I as preprocess,processScript,_ as transform};
1
+ import babelGenerator from "@babel/generator"
2
+ import { parse } from "@babel/parser"
3
+ import babelPluginProposalDecorators from "@babel/plugin-proposal-decorators"
4
+ import babelPluginProposalDestructuringPrivate from "@babel/plugin-proposal-destructuring-private"
5
+ import babelPluginProposalExplicitResourceManagement from "@babel/plugin-proposal-explicit-resource-management"
6
+ import babelPluginTransformClassProperties from "@babel/plugin-transform-class-properties"
7
+ import babelPluginTransformClassStaticBlock from "@babel/plugin-transform-class-static-block"
8
+ import babelPluginTransformExponentiationOperator from "@babel/plugin-transform-exponentiation-operator"
9
+ import babelPluginTransformJsonStrings from "@babel/plugin-transform-json-strings"
10
+ import babelPluginTransformLogicalAssignmentOperators from "@babel/plugin-transform-logical-assignment-operators"
11
+ import babelPluginTransformNullishCoalescingOperator from "@babel/plugin-transform-nullish-coalescing-operator"
12
+ import babelPluginTransformNumericSeparator from "@babel/plugin-transform-numeric-separator"
13
+ import babelPluginTransformObjectRestSpread from "@babel/plugin-transform-object-rest-spread"
14
+ import babelPluginTransformOptionalCatchBinding from "@babel/plugin-transform-optional-catch-binding"
15
+ import babelPluginTransformOptionalChaining from "@babel/plugin-transform-optional-chaining"
16
+ import babelPluginTransformPrivatePropertyInObject from "@babel/plugin-transform-private-property-in-object"
17
+ import babelPluginTransformUnicodeSetsRegex from "@babel/plugin-transform-unicode-sets-regex"
18
+ import babelTraverse from "@babel/traverse"
19
+ import t from "@babel/types"
20
+ import { babel } from "@rollup/plugin-babel"
21
+ import rollupPluginCommonJS from "@rollup/plugin-commonjs"
22
+ import rollupPluginJSON from "@rollup/plugin-json"
23
+ import rollupPluginNodeResolve from "@rollup/plugin-node-resolve"
24
+ import { assert } from "@samual/lib/assert"
25
+ import { resolve } from "path"
26
+ import prettier from "prettier"
27
+ import { rollup } from "rollup"
28
+ import { supportedExtensions } from "../constants.js"
29
+ import { minify } from "./minify.js"
30
+ import { postprocess } from "./postprocess.js"
31
+ import { preprocess } from "./preprocess.js"
32
+ import { getReferencePathsToGlobal, includesIllegalString, replaceUnsafeStrings } from "./shared.js"
33
+ import { transform } from "./transform.js"
34
+ import "@samual/lib/countHackmudCharacters"
35
+ import "@samual/lib/spliceString"
36
+ import "acorn"
37
+ import "terser"
38
+ import "import-meta-resolve"
39
+ import "@samual/lib/clearObject"
40
+ const { format } = prettier,
41
+ { default: generate } = babelGenerator,
42
+ { default: traverse } = babelTraverse
43
+ async function processScript(
44
+ code,
45
+ {
46
+ minify: shouldMinify = !0,
47
+ uniqueID = Math.floor(Math.random() * 2 ** 52)
48
+ .toString(36)
49
+ .padStart(11, "0"),
50
+ scriptUser = "UNKNOWN",
51
+ scriptName = "UNKNOWN",
52
+ filePath,
53
+ mangleNames = !1,
54
+ forceQuineCheats
55
+ } = {}
56
+ ) {
57
+ assert(/^\w{11}$/.exec(uniqueID), "src/processScript/index.ts:78:36")
58
+ const sourceCode = code
59
+ let autocomplete, statedSeclevel
60
+ const autocompleteMatch = /^function\s*\(.+\/\/(?<autocomplete>.+)/.exec(code)
61
+ if (autocompleteMatch) {
62
+ code = "export default " + code
63
+ ;({ autocomplete } = autocompleteMatch.groups)
64
+ } else
65
+ for (const line of code.split("\n")) {
66
+ const comment = /^\s*\/\/(?<commentContent>.+)/.exec(line)
67
+ if (!comment) break
68
+ const commentContent = comment.groups.commentContent.trim()
69
+ if (commentContent.startsWith("@autocomplete ")) autocomplete = commentContent.slice(14).trimStart()
70
+ else if (commentContent.startsWith("@seclevel ")) {
71
+ const seclevelString = commentContent.slice(10).trimStart().toLowerCase()
72
+ switch (seclevelString) {
73
+ case "fullsec":
74
+ case "full":
75
+ case "fs":
76
+ case "4s":
77
+ case "f":
78
+ case "4":
79
+ statedSeclevel = 4
80
+ break
81
+ case "highsec":
82
+ case "high":
83
+ case "hs":
84
+ case "3s":
85
+ case "h":
86
+ case "3":
87
+ statedSeclevel = 3
88
+ break
89
+ case "midsec":
90
+ case "mid":
91
+ case "ms":
92
+ case "2s":
93
+ case "m":
94
+ case "2":
95
+ statedSeclevel = 2
96
+ break
97
+ case "lowsec":
98
+ case "low":
99
+ case "ls":
100
+ case "1s":
101
+ case "l":
102
+ case "1":
103
+ statedSeclevel = 1
104
+ break
105
+ case "nullsec":
106
+ case "null":
107
+ case "ns":
108
+ case "0s":
109
+ case "n":
110
+ case "0":
111
+ statedSeclevel = 0
112
+ break
113
+ default:
114
+ throw Error(`unrecognised seclevel "${seclevelString}"`)
115
+ }
116
+ }
117
+ }
118
+ assert(/^\w{11}$/.exec(uniqueID), "src/processScript/index.ts:159:36")
119
+ const plugins = [
120
+ [babelPluginProposalDecorators.default, { decoratorsBeforeExport: !0 }],
121
+ [babelPluginTransformClassProperties.default],
122
+ [babelPluginTransformClassStaticBlock.default],
123
+ [babelPluginTransformPrivatePropertyInObject.default],
124
+ [babelPluginTransformLogicalAssignmentOperators.default],
125
+ [babelPluginTransformNumericSeparator.default],
126
+ [babelPluginTransformNullishCoalescingOperator.default],
127
+ [babelPluginTransformOptionalChaining.default],
128
+ [babelPluginTransformOptionalCatchBinding.default],
129
+ [babelPluginTransformJsonStrings.default],
130
+ [babelPluginTransformObjectRestSpread.default],
131
+ [babelPluginTransformExponentiationOperator.default],
132
+ [babelPluginTransformUnicodeSetsRegex.default],
133
+ [babelPluginProposalDestructuringPrivate.default],
134
+ [babelPluginProposalExplicitResourceManagement.default]
135
+ ]
136
+ let filePathResolved
137
+ if (filePath) {
138
+ filePathResolved = resolve(filePath)
139
+ if (filePath.endsWith(".ts"))
140
+ plugins.push([
141
+ (await import("@babel/plugin-transform-typescript")).default,
142
+ { allowDeclareFields: !0, optimizeConstEnums: !0 }
143
+ ])
144
+ else {
145
+ const [
146
+ babelPluginProposalDoExpressions,
147
+ babelPluginProposalFunctionBind,
148
+ babelPluginProposalFunctionSent,
149
+ babelPluginProposalPartialApplication,
150
+ babelPluginProposalPipelineOperator,
151
+ babelPluginProposalThrowExpressions,
152
+ babelPluginProposalRecordAndTuple
153
+ ] = await Promise.all([
154
+ import("@babel/plugin-proposal-do-expressions"),
155
+ import("@babel/plugin-proposal-function-bind"),
156
+ import("@babel/plugin-proposal-function-sent"),
157
+ import("@babel/plugin-proposal-partial-application"),
158
+ import("@babel/plugin-proposal-pipeline-operator"),
159
+ import("@babel/plugin-proposal-throw-expressions"),
160
+ import("@babel/plugin-proposal-record-and-tuple")
161
+ ])
162
+ plugins.push(
163
+ [babelPluginProposalDoExpressions.default],
164
+ [babelPluginProposalFunctionBind.default],
165
+ [babelPluginProposalFunctionSent.default],
166
+ [babelPluginProposalPartialApplication.default],
167
+ [babelPluginProposalPipelineOperator.default, { proposal: "hack", topicToken: "%" }],
168
+ [babelPluginProposalThrowExpressions.default],
169
+ [babelPluginProposalRecordAndTuple.default, { syntaxType: "hash", importPolyfill: !0 }]
170
+ )
171
+ }
172
+ } else {
173
+ filePathResolved = uniqueID + ".ts"
174
+ const [
175
+ babelPluginTransformTypescript,
176
+ babelPluginProposalDoExpressions,
177
+ babelPluginProposalFunctionBind,
178
+ babelPluginProposalFunctionSent,
179
+ babelPluginProposalPartialApplication,
180
+ babelPluginProposalPipelineOperator,
181
+ babelPluginProposalThrowExpressions,
182
+ babelPluginProposalRecordAndTuple
183
+ ] = await Promise.all([
184
+ import("@babel/plugin-transform-typescript"),
185
+ import("@babel/plugin-proposal-do-expressions"),
186
+ import("@babel/plugin-proposal-function-bind"),
187
+ import("@babel/plugin-proposal-function-sent"),
188
+ import("@babel/plugin-proposal-partial-application"),
189
+ import("@babel/plugin-proposal-pipeline-operator"),
190
+ import("@babel/plugin-proposal-throw-expressions"),
191
+ import("@babel/plugin-proposal-record-and-tuple")
192
+ ])
193
+ plugins.push(
194
+ [babelPluginTransformTypescript.default, { allowDeclareFields: !0, optimizeConstEnums: !0 }],
195
+ [babelPluginProposalDoExpressions.default],
196
+ [babelPluginProposalFunctionBind.default],
197
+ [babelPluginProposalFunctionSent.default],
198
+ [babelPluginProposalPartialApplication.default],
199
+ [babelPluginProposalPipelineOperator.default, { proposal: "hack", topicToken: "%" }],
200
+ [babelPluginProposalThrowExpressions.default],
201
+ [babelPluginProposalRecordAndTuple.default, { syntaxType: "hash", importPolyfill: !0 }]
202
+ )
203
+ }
204
+ const bundle = await rollup({
205
+ input: filePathResolved,
206
+ plugins: [
207
+ {
208
+ name: "hackmud-script-manager",
209
+ async transform(code, id) {
210
+ if (!id.includes("/node_modules/")) return (await preprocess(code, { uniqueID })).code
211
+ let program
212
+ traverse(parse(code, { sourceType: "module" }), {
213
+ Program(path) {
214
+ program = path
215
+ path.skip()
216
+ }
217
+ })
218
+ for (const referencePath of getReferencePathsToGlobal("JSON", program))
219
+ "MemberExpression" == referencePath.parentPath.node.type &&
220
+ "Identifier" == referencePath.parentPath.node.property.type &&
221
+ ("parse" == referencePath.parentPath.node.property.name ?
222
+ (referencePath.parentPath.node.property.name = "oparse")
223
+ : "stringify" == referencePath.parentPath.node.property.name &&
224
+ (referencePath.parentPath.node.property.name = "ostringify"))
225
+ return generate(program.node).code
226
+ }
227
+ },
228
+ babel({ babelHelpers: "bundled", plugins, configFile: !1, extensions: supportedExtensions }),
229
+ rollupPluginCommonJS(),
230
+ rollupPluginNodeResolve({ extensions: supportedExtensions }),
231
+ rollupPluginJSON()
232
+ ],
233
+ treeshake: { moduleSideEffects: !1 }
234
+ }),
235
+ seclevelNames = ["NULLSEC", "LOWSEC", "MIDSEC", "HIGHSEC", "FULLSEC"]
236
+ code = (await bundle.generate({})).output[0].code
237
+ const { file, seclevel } = transform(parse(code, { sourceType: "module" }), sourceCode, {
238
+ uniqueID,
239
+ scriptUser,
240
+ scriptName
241
+ })
242
+ if (null != statedSeclevel && seclevel < statedSeclevel)
243
+ throw Error(
244
+ `detected seclevel ${seclevelNames[seclevel]} is lower than stated seclevel ${seclevelNames[statedSeclevel]}`
245
+ )
246
+ code = generate(file).code
247
+ if (shouldMinify) code = await minify(file, { uniqueID, mangleNames, forceQuineCheats, autocomplete })
248
+ else {
249
+ traverse(file, {
250
+ MemberExpression({ node: memberExpression }) {
251
+ if (!memberExpression.computed) {
252
+ assert("Identifier" == memberExpression.property.type, "src/processScript/index.ts:322:60")
253
+ if ("prototype" == memberExpression.property.name) {
254
+ memberExpression.computed = !0
255
+ memberExpression.property = t.stringLiteral("prototype")
256
+ } else if ("__proto__" == memberExpression.property.name) {
257
+ memberExpression.computed = !0
258
+ memberExpression.property = t.stringLiteral("__proto__")
259
+ } else if (includesIllegalString(memberExpression.property.name)) {
260
+ memberExpression.computed = !0
261
+ memberExpression.property = t.stringLiteral(
262
+ replaceUnsafeStrings(uniqueID, memberExpression.property.name)
263
+ )
264
+ }
265
+ }
266
+ },
267
+ VariableDeclarator(path) {
268
+ const renameVariables = lValue => {
269
+ switch (lValue.type) {
270
+ case "Identifier":
271
+ includesIllegalString(lValue.name) &&
272
+ path.scope.rename(
273
+ lValue.name,
274
+ "$" +
275
+ Math.floor(Math.random() * 2 ** 52)
276
+ .toString(36)
277
+ .padStart(11, "0")
278
+ )
279
+ break
280
+ case "ObjectPattern":
281
+ for (const property of lValue.properties) {
282
+ assert("ObjectProperty" == property.type, "src/processScript/index.ts:352:51")
283
+ renameVariables(property.value)
284
+ }
285
+ break
286
+ case "ArrayPattern":
287
+ for (const element of lValue.elements) element && renameVariables(element)
288
+ break
289
+ default:
290
+ throw Error(`unknown lValue type "${lValue.type}"`)
291
+ }
292
+ }
293
+ renameVariables(path.node.id)
294
+ },
295
+ ObjectProperty({ node: objectProperty }) {
296
+ if ("Identifier" == objectProperty.key.type && includesIllegalString(objectProperty.key.name)) {
297
+ objectProperty.key = t.stringLiteral(replaceUnsafeStrings(uniqueID, objectProperty.key.name))
298
+ objectProperty.shorthand = !1
299
+ }
300
+ },
301
+ StringLiteral({ node }) {
302
+ node.value = replaceUnsafeStrings(uniqueID, node.value)
303
+ },
304
+ TemplateLiteral({ node }) {
305
+ for (const templateElement of node.quasis)
306
+ if (templateElement.value.cooked) {
307
+ templateElement.value.cooked = replaceUnsafeStrings(uniqueID, templateElement.value.cooked)
308
+ templateElement.value.raw = templateElement.value.cooked
309
+ .replaceAll("\\", "\\\\")
310
+ .replaceAll("`", "\\`")
311
+ .replaceAll("${", "$\\{")
312
+ } else templateElement.value.raw = replaceUnsafeStrings(uniqueID, templateElement.value.raw)
313
+ },
314
+ RegExpLiteral(path) {
315
+ path.node.pattern = replaceUnsafeStrings(uniqueID, path.node.pattern)
316
+ delete path.node.extra
317
+ }
318
+ })
319
+ code = await format(generate(file, { comments: !1 }).code, {
320
+ parser: "babel",
321
+ arrowParens: "avoid",
322
+ semi: !1,
323
+ trailingComma: "none"
324
+ })
325
+ }
326
+ code = postprocess(code, seclevel, uniqueID)
327
+ if (includesIllegalString(code))
328
+ throw Error(
329
+ 'you found a weird edge case where I wasn\'t able to replace illegal strings like "SC$", please report thx'
330
+ )
331
+ return { script: code, warnings: [] }
332
+ }
333
+ export { processScript as default, minify, postprocess, preprocess, processScript, transform }