hackmud-script-manager 0.19.0-f21e319 → 0.19.0-fa82f73
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -674
- package/bin/hsm.d.ts +1 -0
- package/bin/hsm.js +1 -687
- package/constants.js +1 -4
- package/generateTypeDeclaration.js +1 -94
- package/index.js +1 -47
- package/package.json +40 -39
- package/processScript/index.js +1 -310
- package/processScript/minify.js +1 -376
- package/processScript/postprocess.js +1 -5
- package/processScript/preprocess.js +1 -84
- package/processScript/shared.js +1 -18
- package/processScript/transform.js +1 -394
- package/pull.js +1 -17
- package/push.js +1 -251
- package/syncMacros.js +1 -53
- package/tsconfig.tsbuildinfo +1 -1
- package/watch.js +1 -228
package/constants.js
CHANGED
@@ -1,4 +1 @@
|
|
1
|
-
const
|
2
|
-
const validDBMethods = [`i`, `r`, `f`, `u`, `u1`, `us`, `ObjectId`];
|
3
|
-
|
4
|
-
export { supportedExtensions, validDBMethods };
|
1
|
+
const s=[".js",".ts"],t=["i","r","f","u","u1","us","ObjectId"];export{s as supportedExtensions,t as validDBMethods};
|
@@ -1,94 +1 @@
|
|
1
|
-
import
|
2
|
-
import { basename, resolve } from 'path';
|
3
|
-
|
4
|
-
const generateTypeDeclaration = async (sourceDirectory, hackmudPath) => {
|
5
|
-
const users = new Set();
|
6
|
-
if (hackmudPath) {
|
7
|
-
for (const dirent of await readdir(hackmudPath, {
|
8
|
-
withFileTypes: true
|
9
|
-
})) {
|
10
|
-
if (dirent.isFile() && dirent.name.endsWith(`.key`)) users.add(basename(dirent.name, `.key`));
|
11
|
-
}
|
12
|
-
}
|
13
|
-
const wildScripts = [];
|
14
|
-
const wildAnyScripts = [];
|
15
|
-
const allScripts = {};
|
16
|
-
const allAnyScripts = {};
|
17
|
-
await Promise.all((await readdir(sourceDirectory, {
|
18
|
-
withFileTypes: true
|
19
|
-
})).map(async dirent => {
|
20
|
-
if (dirent.isFile()) {
|
21
|
-
if (dirent.name.endsWith(`.ts`)) {
|
22
|
-
if (!dirent.name.endsWith(`.d.ts`)) wildScripts.push(basename(dirent.name, `.ts`));
|
23
|
-
} else if (dirent.name.endsWith(`.js`)) wildAnyScripts.push(basename(dirent.name, `.js`));
|
24
|
-
} else if (dirent.isDirectory()) {
|
25
|
-
const scripts = [];
|
26
|
-
const anyScripts = [];
|
27
|
-
allScripts[dirent.name] = scripts;
|
28
|
-
allAnyScripts[dirent.name] = anyScripts;
|
29
|
-
users.add(dirent.name);
|
30
|
-
for (const file of await readdir(resolve(sourceDirectory, dirent.name), {
|
31
|
-
withFileTypes: true
|
32
|
-
})) {
|
33
|
-
if (file.isFile()) {
|
34
|
-
if (file.name.endsWith(`.ts`)) {
|
35
|
-
if (!dirent.name.endsWith(`.d.ts`)) scripts.push(basename(file.name, `.ts`));
|
36
|
-
} else if (file.name.endsWith(`.js`)) anyScripts.push(basename(file.name, `.js`));
|
37
|
-
}
|
38
|
-
}
|
39
|
-
}
|
40
|
-
}));
|
41
|
-
sourceDirectory = resolve(sourceDirectory);
|
42
|
-
let o = ``;
|
43
|
-
for (const script of wildScripts) o += `type $${script}$ = typeof import("${sourceDirectory}/${script}").default\n`;
|
44
|
-
o += `\n`;
|
45
|
-
for (const user in allScripts) {
|
46
|
-
const scripts = allScripts[user];
|
47
|
-
for (const script of scripts) o += `type $${user}$${script}$ = typeof import("${sourceDirectory}/${user}/${script}").default\n`;
|
48
|
-
}
|
49
|
-
|
50
|
-
// TODO detect security level and generate apropriate code
|
51
|
-
|
52
|
-
// TODO accurate function signatures
|
53
|
-
// I lose the generic-ness of my functions when I wrap them
|
54
|
-
// regexing isn't enough and it looks like I'm going to need to parse the files in TypeScript to extract the signature
|
55
|
-
|
56
|
-
o += `
|
57
|
-
type ArrayRemoveFirst<A> = A extends [ infer FirstItem, ...infer Rest ] ? Rest : never
|
58
|
-
|
59
|
-
type Subscript<T extends (...args: any) => any> =
|
60
|
-
(...args: ArrayRemoveFirst<Parameters<T>>) => ReturnType<T> | ScriptFailure
|
61
|
-
|
62
|
-
type WildFullsec = Record<string, () => ScriptFailure> & {
|
63
|
-
`;
|
64
|
-
for (const script of wildScripts) o += `\t${script}: Subscript<$${script}$>\n`;
|
65
|
-
for (const script of wildAnyScripts) o += `\t${script}: (...args: any) => any\n`;
|
66
|
-
o += `}\n\ninterface PlayerFullsec {`;
|
67
|
-
let lastWasMultiLine = true;
|
68
|
-
for (const user of users) {
|
69
|
-
const scripts = allScripts[user];
|
70
|
-
const anyScripts = allAnyScripts[user];
|
71
|
-
if (scripts && scripts.length || anyScripts && anyScripts.length) {
|
72
|
-
lastWasMultiLine = true;
|
73
|
-
o += `\n\t${user}: WildFullsec & {\n`;
|
74
|
-
if (scripts) {
|
75
|
-
for (const script of scripts) o += `\t\t${script}: Subscript<$${user}$${script}$>\n`;
|
76
|
-
}
|
77
|
-
if (anyScripts) {
|
78
|
-
for (const script of anyScripts) o += `\t\t${script}: (...args: any) => any\n`;
|
79
|
-
}
|
80
|
-
o += `\t}`;
|
81
|
-
} else {
|
82
|
-
if (lastWasMultiLine) {
|
83
|
-
o += `\n`;
|
84
|
-
lastWasMultiLine = false;
|
85
|
-
}
|
86
|
-
o += `\t${user}: WildFullsec`;
|
87
|
-
}
|
88
|
-
o += `\n`;
|
89
|
-
}
|
90
|
-
o += `}\n`;
|
91
|
-
return o;
|
92
|
-
};
|
93
|
-
|
94
|
-
export { generateTypeDeclaration as default, generateTypeDeclaration };
|
1
|
+
import{readdir as e}from"fs/promises";import{basename as t,resolve as n}from"path";const generateTypeDeclaration=async(s,i)=>{const a=new Set;if(i)for(const n of await e(i,{withFileTypes:!0}))n.isFile()&&n.name.endsWith(".key")&&a.add(t(n.name,".key"));const r=[],o=[],f={},l={};await Promise.all((await e(s,{withFileTypes:!0})).map((async i=>{if(i.isFile())i.name.endsWith(".ts")?i.name.endsWith(".d.ts")||r.push(t(i.name,".ts")):i.name.endsWith(".js")&&o.push(t(i.name,".js"));else if(i.isDirectory()){const r=[],o=[];f[i.name]=r,l[i.name]=o,a.add(i.name);for(const a of await e(n(s,i.name),{withFileTypes:!0}))a.isFile()&&(a.name.endsWith(".ts")?i.name.endsWith(".d.ts")||r.push(t(a.name,".ts")):a.name.endsWith(".js")&&o.push(t(a.name,".js")))}}))),s=n(s);let c="";for(const e of r)c+=`type $${e}$ = typeof import("${s}/${e}").default\n`;c+="\n";for(const e in f){const t=f[e];for(const n of t)c+=`type $${e}$${n}$ = typeof import("${s}/${e}/${n}").default\n`}c+="\ntype ArrayRemoveFirst<A> = A extends [ infer FirstItem, ...infer Rest ] ? Rest : never\n\ntype Subscript<T extends (...args: any) => any> =\n\t(...args: ArrayRemoveFirst<Parameters<T>>) => ReturnType<T> | ScriptFailure\n\ntype WildFullsec = Record<string, () => ScriptFailure> & {\n";for(const e of r)c+=`\t${e}: Subscript<$${e}$>\n`;for(const e of o)c+=`\t${e}: (...args: any) => any\n`;c+="}\n\ninterface PlayerFullsec {";let m=!0;for(const e of a){const t=f[e],n=l[e];if(t&&t.length||n&&n.length){if(m=!0,c+=`\n\t${e}: WildFullsec & {\n`,t)for(const n of t)c+=`\t\t${n}: Subscript<$${e}$${n}$>\n`;if(n)for(const e of n)c+=`\t\t${e}: (...args: any) => any\n`;c+="\t}"}else m&&(c+="\n",m=!1),c+=`\t${e}: WildFullsec`;c+="\n"}return c+="}\n",c};export{generateTypeDeclaration as default,generateTypeDeclaration};
|
package/index.js
CHANGED
@@ -1,47 +1 @@
|
|
1
|
-
export
|
2
|
-
export { generateTypeDeclaration } from './generateTypeDeclaration.js';
|
3
|
-
export { processScript } from './processScript/index.js';
|
4
|
-
export { pull } from './pull.js';
|
5
|
-
export { push } from './push.js';
|
6
|
-
export { syncMacros } from './syncMacros.js';
|
7
|
-
export { watch } from './watch.js';
|
8
|
-
import 'fs/promises';
|
9
|
-
import 'path';
|
10
|
-
import '@babel/generator';
|
11
|
-
import '@babel/parser';
|
12
|
-
import '@babel/plugin-proposal-class-properties';
|
13
|
-
import '@babel/plugin-proposal-class-static-block';
|
14
|
-
import '@babel/plugin-proposal-decorators';
|
15
|
-
import '@babel/plugin-proposal-json-strings';
|
16
|
-
import '@babel/plugin-proposal-logical-assignment-operators';
|
17
|
-
import '@babel/plugin-proposal-nullish-coalescing-operator';
|
18
|
-
import '@babel/plugin-proposal-numeric-separator';
|
19
|
-
import '@babel/plugin-proposal-object-rest-spread';
|
20
|
-
import '@babel/plugin-proposal-optional-catch-binding';
|
21
|
-
import '@babel/plugin-proposal-optional-chaining';
|
22
|
-
import '@babel/plugin-proposal-private-property-in-object';
|
23
|
-
import '@babel/plugin-transform-exponentiation-operator';
|
24
|
-
import '@babel/traverse';
|
25
|
-
import '@babel/types';
|
26
|
-
import '@rollup/plugin-babel';
|
27
|
-
import '@rollup/plugin-commonjs';
|
28
|
-
import '@rollup/plugin-json';
|
29
|
-
import '@rollup/plugin-node-resolve';
|
30
|
-
import '@samual/lib/assert';
|
31
|
-
import 'prettier';
|
32
|
-
import 'rollup';
|
33
|
-
import './processScript/minify.js';
|
34
|
-
import '@samual/lib/countHackmudCharacters';
|
35
|
-
import '@samual/lib/spliceString';
|
36
|
-
import 'acorn';
|
37
|
-
import 'terser';
|
38
|
-
import './processScript/shared.js';
|
39
|
-
import './processScript/postprocess.js';
|
40
|
-
import './processScript/preprocess.js';
|
41
|
-
import 'import-meta-resolve';
|
42
|
-
import './processScript/transform.js';
|
43
|
-
import '@samual/lib/clearObject';
|
44
|
-
import '@samual/lib/copyFilePersistent';
|
45
|
-
import '@samual/lib/DynamicMap';
|
46
|
-
import '@samual/lib/writeFilePersistent';
|
47
|
-
import 'chokidar';
|
1
|
+
export{supportedExtensions}from"./constants.js";export{generateTypeDeclaration}from"./generateTypeDeclaration.js";export{processScript}from"./processScript/index.js";export{pull}from"./pull.js";export{push}from"./push.js";export{syncMacros}from"./syncMacros.js";export{watch}from"./watch.js";import"fs/promises";import"path";import"@babel/generator";import"@babel/parser";import"@babel/plugin-proposal-decorators";import"@babel/plugin-proposal-destructuring-private";import"@babel/plugin-proposal-explicit-resource-management";import"@babel/plugin-transform-class-properties";import"@babel/plugin-transform-class-static-block";import"@babel/plugin-transform-exponentiation-operator";import"@babel/plugin-transform-json-strings";import"@babel/plugin-transform-logical-assignment-operators";import"@babel/plugin-transform-nullish-coalescing-operator";import"@babel/plugin-transform-numeric-separator";import"@babel/plugin-transform-object-rest-spread";import"@babel/plugin-transform-optional-catch-binding";import"@babel/plugin-transform-optional-chaining";import"@babel/plugin-transform-private-property-in-object";import"@babel/plugin-transform-unicode-sets-regex";import"@babel/traverse";import"@babel/types";import"@rollup/plugin-babel";import"@rollup/plugin-commonjs";import"@rollup/plugin-json";import"@rollup/plugin-node-resolve";import"@samual/lib/assert";import"prettier";import"rollup";import"./processScript/minify.js";import"@samual/lib/countHackmudCharacters";import"@samual/lib/spliceString";import"acorn";import"terser";import"./processScript/shared.js";import"./processScript/postprocess.js";import"./processScript/preprocess.js";import"import-meta-resolve";import"./processScript/transform.js";import"@samual/lib/clearObject";import"@samual/lib/copyFilePersistent";import"@samual/lib/DynamicMap";import"@samual/lib/writeFilePersistent";import"chokidar";
|
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.0-fa82f73",
|
4
4
|
"description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
|
5
5
|
"keywords": [
|
6
6
|
"api",
|
@@ -19,7 +19,7 @@
|
|
19
19
|
],
|
20
20
|
"homepage": "https://github.com/samualtnorman/hackmud-script-manager#readme",
|
21
21
|
"bugs": "https://github.com/samualtnorman/hackmud-script-manager/issues",
|
22
|
-
"license": "
|
22
|
+
"license": "MIT",
|
23
23
|
"author": "Samual Norman",
|
24
24
|
"main": "index.js",
|
25
25
|
"repository": {
|
@@ -27,50 +27,51 @@
|
|
27
27
|
"url": "https://github.com/samualtnorman/hackmud-script-manager.git"
|
28
28
|
},
|
29
29
|
"dependencies": {
|
30
|
-
"@babel/
|
31
|
-
"@babel/
|
32
|
-
"@babel/
|
33
|
-
"@babel/plugin-proposal-
|
34
|
-
"@babel/plugin-proposal-
|
35
|
-
"@babel/plugin-proposal-
|
36
|
-
"@babel/plugin-proposal-
|
37
|
-
"@babel/plugin-proposal-function-
|
38
|
-
"@babel/plugin-proposal-
|
39
|
-
"@babel/plugin-proposal-
|
40
|
-
"@babel/plugin-proposal-
|
41
|
-
"@babel/plugin-proposal-
|
42
|
-
"@babel/plugin-
|
43
|
-
"@babel/plugin-
|
44
|
-
"@babel/plugin-
|
45
|
-
"@babel/plugin-
|
46
|
-
"@babel/plugin-
|
47
|
-
"@babel/plugin-
|
48
|
-
"@babel/plugin-
|
49
|
-
"@babel/plugin-
|
50
|
-
"@babel/plugin-
|
51
|
-
"@babel/plugin-transform-
|
52
|
-
"@babel/plugin-transform-
|
53
|
-
"@babel/
|
54
|
-
"@babel/
|
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",
|
55
57
|
"@bloomberg/record-tuple-polyfill": "^0.0.4",
|
56
|
-
"@rollup/plugin-babel": "^6.0.
|
57
|
-
"@rollup/plugin-commonjs": "^25.0.
|
58
|
-
"@rollup/plugin-json": "^6.
|
59
|
-
"@rollup/plugin-node-resolve": "^15.
|
60
|
-
"@samual/lib": "^0.
|
61
|
-
"acorn": "^8.
|
58
|
+
"@rollup/plugin-babel": "^6.0.4",
|
59
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
60
|
+
"@rollup/plugin-json": "^6.1.0",
|
61
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
62
|
+
"@samual/lib": "^0.9.1",
|
63
|
+
"acorn": "^8.11.3",
|
62
64
|
"chalk": "^5.3.0",
|
63
65
|
"chokidar": "^3.5.3",
|
64
|
-
"import-meta-resolve": "^
|
65
|
-
"prettier": "^3.
|
66
|
+
"import-meta-resolve": "^4.0.0",
|
67
|
+
"prettier": "^3.2.2",
|
66
68
|
"proxy-polyfill": "^0.3.2",
|
67
|
-
"rollup": "^
|
68
|
-
"terser": "^5.
|
69
|
+
"rollup": "^4.9.5",
|
70
|
+
"terser": "^5.26.0"
|
69
71
|
},
|
70
72
|
"engines": {
|
71
|
-
"node": ">=
|
73
|
+
"node": ">=20"
|
72
74
|
},
|
73
|
-
"types": "index.d.ts",
|
74
75
|
"type": "module",
|
75
76
|
"exports": {
|
76
77
|
"./*": "./*.js",
|
package/processScript/index.js
CHANGED
@@ -1,310 +1 @@
|
|
1
|
-
import
|
2
|
-
import { parse } from '@babel/parser';
|
3
|
-
import babelPluginProposalClassProperties from '@babel/plugin-proposal-class-properties';
|
4
|
-
import babelPluginProposalClassStaticBlock from '@babel/plugin-proposal-class-static-block';
|
5
|
-
import babelPluginProposalDecorators from '@babel/plugin-proposal-decorators';
|
6
|
-
import babelPluginProposalJSONStrings from '@babel/plugin-proposal-json-strings';
|
7
|
-
import babelPluginProposalLogicalAssignmentOperators from '@babel/plugin-proposal-logical-assignment-operators';
|
8
|
-
import babelPluginProposalNullishCoalescingOperator from '@babel/plugin-proposal-nullish-coalescing-operator';
|
9
|
-
import babelPluginProposalNumericSeparator from '@babel/plugin-proposal-numeric-separator';
|
10
|
-
import babelPluginProposalObjectRestSpread from '@babel/plugin-proposal-object-rest-spread';
|
11
|
-
import babelPluginProposalOptionalCatchBinding from '@babel/plugin-proposal-optional-catch-binding';
|
12
|
-
import babelPluginProposalOptionalChaining from '@babel/plugin-proposal-optional-chaining';
|
13
|
-
import babelPluginProposalPrivatePropertyInObject from '@babel/plugin-proposal-private-property-in-object';
|
14
|
-
import babelPluginTransformExponentiationOperator from '@babel/plugin-transform-exponentiation-operator';
|
15
|
-
import babelTraverse from '@babel/traverse';
|
16
|
-
import t from '@babel/types';
|
17
|
-
import { babel } from '@rollup/plugin-babel';
|
18
|
-
import rollupPluginCommonJS from '@rollup/plugin-commonjs';
|
19
|
-
import rollupPluginJSON from '@rollup/plugin-json';
|
20
|
-
import rollupPluginNodeResolve from '@rollup/plugin-node-resolve';
|
21
|
-
import { assert } from '@samual/lib/assert';
|
22
|
-
import { resolve } from 'path';
|
23
|
-
import prettier from 'prettier';
|
24
|
-
import { rollup } from 'rollup';
|
25
|
-
import { supportedExtensions } from '../constants.js';
|
26
|
-
import { minify } from './minify.js';
|
27
|
-
import { postprocess } from './postprocess.js';
|
28
|
-
import { preprocess } from './preprocess.js';
|
29
|
-
import { includesIllegalString, replaceUnsafeStrings } from './shared.js';
|
30
|
-
import { transform } from './transform.js';
|
31
|
-
import '@samual/lib/countHackmudCharacters';
|
32
|
-
import '@samual/lib/spliceString';
|
33
|
-
import 'acorn';
|
34
|
-
import 'terser';
|
35
|
-
import 'import-meta-resolve';
|
36
|
-
import '@samual/lib/clearObject';
|
37
|
-
|
38
|
-
const {
|
39
|
-
format
|
40
|
-
} = prettier;
|
41
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
42
|
-
const {
|
43
|
-
default: generate
|
44
|
-
} = babelGenerator;
|
45
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
46
|
-
const {
|
47
|
-
default: traverse
|
48
|
-
} = babelTraverse;
|
49
|
-
/**
|
50
|
-
* Minifies a given script
|
51
|
-
*
|
52
|
-
* @param code JavaScript or TypeScript code
|
53
|
-
* @param options {@link ProcessOptions details}
|
54
|
-
*/
|
55
|
-
const processScript = async (code, {
|
56
|
-
minify: shouldMinify = true,
|
57
|
-
uniqueID = Math.floor(Math.random() * 2 ** 52).toString(36).padStart(11, `0`),
|
58
|
-
scriptUser = `UNKNOWN`,
|
59
|
-
scriptName = `UNKNOWN`,
|
60
|
-
filePath,
|
61
|
-
mangleNames = false,
|
62
|
-
forceQuineCheats
|
63
|
-
} = {}) => {
|
64
|
-
assert(/^\w{11}$/.exec(uniqueID));
|
65
|
-
const sourceCode = code;
|
66
|
-
let autocomplete;
|
67
|
-
let statedSeclevel;
|
68
|
-
|
69
|
-
// TODO do seclevel detection and verification per module
|
70
|
-
|
71
|
-
const autocompleteMatch = /^function\s*\(.+\/\/(?<autocomplete>.+)/.exec(code);
|
72
|
-
if (autocompleteMatch) {
|
73
|
-
code = `export default ${code}`;
|
74
|
-
({
|
75
|
-
autocomplete
|
76
|
-
} = autocompleteMatch.groups);
|
77
|
-
} else {
|
78
|
-
for (const line of code.split(`\n`)) {
|
79
|
-
const comment = /^\s*\/\/(?<commentContent>.+)/.exec(line);
|
80
|
-
if (!comment) break;
|
81
|
-
const commentContent = comment.groups.commentContent.trim();
|
82
|
-
if (commentContent.startsWith(`@autocomplete `)) autocomplete = commentContent.slice(14).trimStart();else if (commentContent.startsWith(`@seclevel `)) {
|
83
|
-
const seclevelString = commentContent.slice(10).trimStart().toLowerCase();
|
84
|
-
switch (seclevelString) {
|
85
|
-
case `fullsec`:
|
86
|
-
case `full`:
|
87
|
-
case `fs`:
|
88
|
-
case `4s`:
|
89
|
-
case `f`:
|
90
|
-
case `4`:
|
91
|
-
{
|
92
|
-
statedSeclevel = 4;
|
93
|
-
}
|
94
|
-
break;
|
95
|
-
case `highsec`:
|
96
|
-
case `high`:
|
97
|
-
case `hs`:
|
98
|
-
case `3s`:
|
99
|
-
case `h`:
|
100
|
-
case `3`:
|
101
|
-
{
|
102
|
-
statedSeclevel = 3;
|
103
|
-
}
|
104
|
-
break;
|
105
|
-
case `midsec`:
|
106
|
-
case `mid`:
|
107
|
-
case `ms`:
|
108
|
-
case `2s`:
|
109
|
-
case `m`:
|
110
|
-
case `2`:
|
111
|
-
{
|
112
|
-
statedSeclevel = 2;
|
113
|
-
}
|
114
|
-
break;
|
115
|
-
case `lowsec`:
|
116
|
-
case `low`:
|
117
|
-
case `ls`:
|
118
|
-
case `1s`:
|
119
|
-
case `l`:
|
120
|
-
case `1`:
|
121
|
-
{
|
122
|
-
statedSeclevel = 1;
|
123
|
-
}
|
124
|
-
break;
|
125
|
-
case `nullsec`:
|
126
|
-
case `null`:
|
127
|
-
case `ns`:
|
128
|
-
case `0s`:
|
129
|
-
case `n`:
|
130
|
-
case `0`:
|
131
|
-
{
|
132
|
-
statedSeclevel = 0;
|
133
|
-
}
|
134
|
-
break;
|
135
|
-
default:
|
136
|
-
// TODO turn into warninig when I get round to those
|
137
|
-
throw new Error(`unrecognised seclevel "${seclevelString}"`);
|
138
|
-
}
|
139
|
-
}
|
140
|
-
}
|
141
|
-
}
|
142
|
-
assert(/^\w{11}$/.exec(uniqueID));
|
143
|
-
const plugins = [[babelPluginProposalDecorators.default, {
|
144
|
-
decoratorsBeforeExport: true
|
145
|
-
}], [babelPluginProposalClassProperties.default], [babelPluginProposalClassStaticBlock.default], [babelPluginProposalPrivatePropertyInObject.default], [babelPluginProposalLogicalAssignmentOperators.default], [babelPluginProposalNumericSeparator.default], [babelPluginProposalNullishCoalescingOperator.default], [babelPluginProposalOptionalChaining.default], [babelPluginProposalOptionalCatchBinding.default], [babelPluginProposalJSONStrings.default], [babelPluginProposalObjectRestSpread.default], [babelPluginTransformExponentiationOperator.default]];
|
146
|
-
let filePathResolved;
|
147
|
-
if (filePath) {
|
148
|
-
filePathResolved = resolve(filePath);
|
149
|
-
if (filePath.endsWith(`.ts`)) plugins.push([(await import('@babel/plugin-transform-typescript')).default, {
|
150
|
-
allowDeclareFields: true,
|
151
|
-
optimizeConstEnums: true
|
152
|
-
}]);else {
|
153
|
-
const [babelPluginProposalDoExpressions, babelPluginProposalFunctionBind, babelPluginProposalFunctionSent, babelPluginProposalPartialApplication, babelPluginProposalPipelineOperator, babelPluginProposalThrowExpressions, babelPluginProposalRecordAndTuple] = 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')]);
|
154
|
-
plugins.push([babelPluginProposalDoExpressions.default], [babelPluginProposalFunctionBind.default], [babelPluginProposalFunctionSent.default], [babelPluginProposalPartialApplication.default], [babelPluginProposalPipelineOperator.default, {
|
155
|
-
proposal: `hack`,
|
156
|
-
topicToken: `%`
|
157
|
-
}], [babelPluginProposalThrowExpressions.default], [babelPluginProposalRecordAndTuple.default, {
|
158
|
-
syntaxType: `hash`,
|
159
|
-
importPolyfill: true
|
160
|
-
}]);
|
161
|
-
}
|
162
|
-
} else {
|
163
|
-
filePathResolved = `${uniqueID}.ts`;
|
164
|
-
const [babelPluginTransformTypescript, babelPluginProposalDoExpressions, babelPluginProposalFunctionBind, babelPluginProposalFunctionSent, babelPluginProposalPartialApplication, babelPluginProposalPipelineOperator, babelPluginProposalThrowExpressions, babelPluginProposalRecordAndTuple] = 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')]);
|
165
|
-
plugins.push([babelPluginTransformTypescript.default, {
|
166
|
-
allowDeclareFields: true,
|
167
|
-
optimizeConstEnums: true
|
168
|
-
}], [babelPluginProposalDoExpressions.default], [babelPluginProposalFunctionBind.default], [babelPluginProposalFunctionSent.default], [babelPluginProposalPartialApplication.default], [babelPluginProposalPipelineOperator.default, {
|
169
|
-
proposal: `hack`,
|
170
|
-
topicToken: `%`
|
171
|
-
}], [babelPluginProposalThrowExpressions.default], [babelPluginProposalRecordAndTuple.default, {
|
172
|
-
syntaxType: `hash`,
|
173
|
-
importPolyfill: true
|
174
|
-
}]);
|
175
|
-
}
|
176
|
-
const bundle = await rollup({
|
177
|
-
input: filePathResolved,
|
178
|
-
plugins: [{
|
179
|
-
name: `hackmud-script-manager`,
|
180
|
-
transform: async code => (await preprocess(code, {
|
181
|
-
uniqueID
|
182
|
-
})).code
|
183
|
-
}, babel({
|
184
|
-
babelHelpers: `bundled`,
|
185
|
-
plugins,
|
186
|
-
configFile: false,
|
187
|
-
extensions: supportedExtensions
|
188
|
-
}), rollupPluginCommonJS(), rollupPluginNodeResolve({
|
189
|
-
extensions: supportedExtensions
|
190
|
-
}), rollupPluginJSON()],
|
191
|
-
treeshake: {
|
192
|
-
moduleSideEffects: false
|
193
|
-
}
|
194
|
-
});
|
195
|
-
const seclevelNames = [`NULLSEC`, `LOWSEC`, `MIDSEC`, `HIGHSEC`, `FULLSEC`];
|
196
|
-
code = (await bundle.generate({})).output[0].code;
|
197
|
-
const {
|
198
|
-
file,
|
199
|
-
seclevel
|
200
|
-
} = transform(parse(code, {
|
201
|
-
sourceType: `module`
|
202
|
-
}), sourceCode, {
|
203
|
-
uniqueID,
|
204
|
-
scriptUser,
|
205
|
-
scriptName
|
206
|
-
});
|
207
|
-
if (statedSeclevel != undefined && seclevel < statedSeclevel)
|
208
|
-
// TODO replace with a warning and build script anyway
|
209
|
-
throw new Error(`detected seclevel ${seclevelNames[seclevel]} is lower than stated seclevel ${seclevelNames[statedSeclevel]}`);
|
210
|
-
code = generate(file).code;
|
211
|
-
if (shouldMinify) code = await minify(file, {
|
212
|
-
uniqueID,
|
213
|
-
mangleNames,
|
214
|
-
forceQuineCheats,
|
215
|
-
autocomplete
|
216
|
-
});else {
|
217
|
-
traverse(file, {
|
218
|
-
MemberExpression({
|
219
|
-
node: memberExpression
|
220
|
-
}) {
|
221
|
-
if (memberExpression.computed) return;
|
222
|
-
assert(memberExpression.property.type == `Identifier`);
|
223
|
-
if (memberExpression.property.name == `prototype`) {
|
224
|
-
memberExpression.computed = true;
|
225
|
-
memberExpression.property = t.stringLiteral(`prototype`);
|
226
|
-
} else if (memberExpression.property.name == `__proto__`) {
|
227
|
-
memberExpression.computed = true;
|
228
|
-
memberExpression.property = t.stringLiteral(`__proto__`);
|
229
|
-
} else if (includesIllegalString(memberExpression.property.name)) {
|
230
|
-
memberExpression.computed = true;
|
231
|
-
memberExpression.property = t.stringLiteral(replaceUnsafeStrings(uniqueID, memberExpression.property.name));
|
232
|
-
}
|
233
|
-
},
|
234
|
-
VariableDeclarator(path) {
|
235
|
-
const renameVariables = lValue => {
|
236
|
-
switch (lValue.type) {
|
237
|
-
case `Identifier`:
|
238
|
-
{
|
239
|
-
if (includesIllegalString(lValue.name)) path.scope.rename(lValue.name, `$${Math.floor(Math.random() * 2 ** 52).toString(36).padStart(11, `0`)}`);
|
240
|
-
}
|
241
|
-
break;
|
242
|
-
case `ObjectPattern`:
|
243
|
-
{
|
244
|
-
for (const property of lValue.properties) {
|
245
|
-
assert(property.type == `ObjectProperty`);
|
246
|
-
renameVariables(property.value);
|
247
|
-
}
|
248
|
-
}
|
249
|
-
break;
|
250
|
-
case `ArrayPattern`:
|
251
|
-
{
|
252
|
-
for (const element of lValue.elements) {
|
253
|
-
if (element) renameVariables(element);
|
254
|
-
}
|
255
|
-
}
|
256
|
-
break;
|
257
|
-
default:
|
258
|
-
throw new Error(`unknown lValue type "${lValue.type}"`);
|
259
|
-
}
|
260
|
-
};
|
261
|
-
renameVariables(path.node.id);
|
262
|
-
},
|
263
|
-
ObjectProperty({
|
264
|
-
node: objectProperty
|
265
|
-
}) {
|
266
|
-
if (objectProperty.key.type == `Identifier` && includesIllegalString(objectProperty.key.name)) {
|
267
|
-
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(uniqueID, objectProperty.key.name));
|
268
|
-
objectProperty.shorthand = false;
|
269
|
-
}
|
270
|
-
},
|
271
|
-
StringLiteral({
|
272
|
-
node
|
273
|
-
}) {
|
274
|
-
node.value = replaceUnsafeStrings(uniqueID, node.value);
|
275
|
-
},
|
276
|
-
TemplateLiteral({
|
277
|
-
node
|
278
|
-
}) {
|
279
|
-
for (const templateElement of node.quasis) {
|
280
|
-
if (templateElement.value.cooked) {
|
281
|
-
templateElement.value.cooked = replaceUnsafeStrings(uniqueID, templateElement.value.cooked);
|
282
|
-
templateElement.value.raw = templateElement.value.cooked.replaceAll(`\\`, `\\\\`).replaceAll(`\``, `\\\``).replaceAll(`\${`, `$\\{`);
|
283
|
-
} else templateElement.value.raw = replaceUnsafeStrings(uniqueID, templateElement.value.raw);
|
284
|
-
}
|
285
|
-
},
|
286
|
-
RegExpLiteral(path) {
|
287
|
-
path.node.pattern = replaceUnsafeStrings(uniqueID, path.node.pattern);
|
288
|
-
delete path.node.extra;
|
289
|
-
}
|
290
|
-
});
|
291
|
-
|
292
|
-
// we can't have comments because they may contain illegal strings
|
293
|
-
code = await format(generate(file, {
|
294
|
-
comments: false
|
295
|
-
}).code, {
|
296
|
-
parser: `babel`,
|
297
|
-
arrowParens: `avoid`,
|
298
|
-
semi: false,
|
299
|
-
trailingComma: `none`
|
300
|
-
});
|
301
|
-
}
|
302
|
-
code = postprocess(code, seclevel, uniqueID);
|
303
|
-
if (includesIllegalString(code)) throw new Error(`you found a weird edge case where I wasn't able to replace illegal strings like "SC$", please report thx`);
|
304
|
-
return {
|
305
|
-
script: code,
|
306
|
-
warnings: []
|
307
|
-
};
|
308
|
-
};
|
309
|
-
|
310
|
-
export { processScript as default, minify, postprocess, preprocess, processScript, transform };
|
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};
|