hackmud-script-manager 0.19.0-43367ba → 0.19.0-c12fd7c
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/hsm.d.ts +0 -1
- package/bin/hsm.js +687 -1
- package/constants.d.ts +2 -0
- package/constants.js +4 -0
- package/generateTypeDeclaration.js +94 -1
- package/index.d.ts +2 -2
- package/index.js +47 -1
- package/package.json +36 -78
- package/processScript/index.d.ts +2 -2
- package/processScript/index.js +310 -1
- package/processScript/minify.d.ts +3 -3
- package/processScript/minify.js +376 -1
- package/processScript/postprocess.js +5 -1
- package/processScript/preprocess.d.ts +1 -1
- package/processScript/preprocess.js +84 -1
- package/processScript/shared.d.ts +3 -3
- package/processScript/shared.js +18 -1
- package/processScript/transform.d.ts +3 -3
- package/processScript/transform.js +394 -1
- package/pull.js +17 -1
- package/push.d.ts +3 -3
- package/push.js +251 -1
- package/syncMacros.js +53 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/watch.d.ts +3 -3
- package/watch.js +228 -1
- package/assert-22a7ef8a.js +0 -1
- package/constants-9bb78688.js +0 -1
- package/countHackmudCharacters-a08a265f.js +0 -1
- package/spliceString-0e6b5d6d.js +0 -1
- package/writeFilePersistent-ee9c9bfd.js +0 -1
package/constants.d.ts
ADDED
package/constants.js
ADDED
@@ -1 +1,94 @@
|
|
1
|
-
import{readdir
|
1
|
+
import { readdir } from 'fs/promises';
|
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 };
|
package/index.d.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
export { supportedExtensions } from "./constants
|
1
|
+
export { supportedExtensions } from "./constants";
|
2
2
|
export { generateTypeDeclaration } from "./generateTypeDeclaration";
|
3
3
|
export { processScript } from "./processScript";
|
4
4
|
export { pull } from "./pull";
|
5
5
|
export { push } from "./push";
|
6
6
|
export { syncMacros } from "./syncMacros";
|
7
7
|
export { watch } from "./watch";
|
8
|
-
export
|
8
|
+
export type Info = {
|
9
9
|
file: string;
|
10
10
|
users: string[];
|
11
11
|
minLength: number;
|
package/index.js
CHANGED
@@ -1 +1,47 @@
|
|
1
|
-
export{
|
1
|
+
export { supportedExtensions } from './constants.js';
|
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';
|
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-c12fd7c",
|
4
4
|
"description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
|
5
5
|
"keywords": [
|
6
6
|
"api",
|
@@ -27,98 +27,56 @@
|
|
27
27
|
"url": "https://github.com/samualtnorman/hackmud-script-manager.git"
|
28
28
|
},
|
29
29
|
"dependencies": {
|
30
|
-
"@babel/
|
31
|
-
"@babel/
|
32
|
-
"@babel/parser": "^7.18.9",
|
30
|
+
"@babel/generator": "^7.22.9",
|
31
|
+
"@babel/parser": "^7.22.7",
|
33
32
|
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
34
|
-
"@babel/plugin-proposal-class-static-block": "^7.
|
35
|
-
"@babel/plugin-proposal-decorators": "^7.
|
36
|
-
"@babel/plugin-proposal-do-expressions": "^7.
|
37
|
-
"@babel/plugin-proposal-function-bind": "^7.
|
38
|
-
"@babel/plugin-proposal-function-sent": "^7.
|
33
|
+
"@babel/plugin-proposal-class-static-block": "^7.21.0",
|
34
|
+
"@babel/plugin-proposal-decorators": "^7.22.7",
|
35
|
+
"@babel/plugin-proposal-do-expressions": "^7.22.5",
|
36
|
+
"@babel/plugin-proposal-function-bind": "^7.22.5",
|
37
|
+
"@babel/plugin-proposal-function-sent": "^7.22.5",
|
39
38
|
"@babel/plugin-proposal-json-strings": "^7.18.6",
|
40
|
-
"@babel/plugin-proposal-logical-assignment-operators": "^7.
|
39
|
+
"@babel/plugin-proposal-logical-assignment-operators": "^7.20.7",
|
41
40
|
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
|
42
41
|
"@babel/plugin-proposal-numeric-separator": "^7.18.6",
|
43
|
-
"@babel/plugin-proposal-object-rest-spread": "^7.
|
42
|
+
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
|
44
43
|
"@babel/plugin-proposal-optional-catch-binding": "^7.18.6",
|
45
|
-
"@babel/plugin-proposal-optional-chaining": "^7.
|
46
|
-
"@babel/plugin-proposal-partial-application": "^7.
|
47
|
-
"@babel/plugin-proposal-pipeline-operator": "^7.
|
48
|
-
"@babel/plugin-proposal-private-property-in-object": "^7.
|
49
|
-
"@babel/plugin-proposal-record-and-tuple": "^7.
|
50
|
-
"@babel/plugin-proposal-throw-expressions": "^7.
|
51
|
-
"@babel/plugin-transform-exponentiation-operator": "^7.
|
52
|
-
"@babel/plugin-transform-typescript": "^7.
|
53
|
-
"@babel/traverse": "^7.
|
54
|
-
"@babel/types": "^7.
|
44
|
+
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
|
45
|
+
"@babel/plugin-proposal-partial-application": "^7.22.5",
|
46
|
+
"@babel/plugin-proposal-pipeline-operator": "^7.22.5",
|
47
|
+
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
|
48
|
+
"@babel/plugin-proposal-record-and-tuple": "^7.22.5",
|
49
|
+
"@babel/plugin-proposal-throw-expressions": "^7.22.5",
|
50
|
+
"@babel/plugin-transform-exponentiation-operator": "^7.22.5",
|
51
|
+
"@babel/plugin-transform-typescript": "^7.22.9",
|
52
|
+
"@babel/traverse": "^7.22.8",
|
53
|
+
"@babel/types": "^7.22.5",
|
55
54
|
"@bloomberg/record-tuple-polyfill": "^0.0.4",
|
56
|
-
"@rollup/plugin-babel": "^
|
57
|
-
"@rollup/plugin-commonjs": "^
|
58
|
-
"@rollup/plugin-json": "^
|
59
|
-
"@rollup/plugin-node-resolve": "^
|
60
|
-
"
|
61
|
-
"
|
55
|
+
"@rollup/plugin-babel": "^6.0.3",
|
56
|
+
"@rollup/plugin-commonjs": "^25.0.2",
|
57
|
+
"@rollup/plugin-json": "^6.0.0",
|
58
|
+
"@rollup/plugin-node-resolve": "^15.1.0",
|
59
|
+
"@samual/lib": "^0.8.1",
|
60
|
+
"acorn": "^8.10.0",
|
61
|
+
"chalk": "^5.3.0",
|
62
62
|
"chokidar": "^3.5.3",
|
63
|
-
"import-meta-resolve": "^
|
64
|
-
"prettier": "^
|
63
|
+
"import-meta-resolve": "^3.0.0",
|
64
|
+
"prettier": "^3.0.0",
|
65
65
|
"proxy-polyfill": "^0.3.2",
|
66
|
-
"rollup": "^
|
67
|
-
"terser": "^5.
|
68
|
-
},
|
69
|
-
"devDependencies": {
|
70
|
-
"@babel/preset-env": "^7.18.9",
|
71
|
-
"@babel/preset-typescript": "^7.18.6",
|
72
|
-
"@samual/lib": "^0.3.4",
|
73
|
-
"@types/babel__core": "^7.1.19",
|
74
|
-
"@types/babel__generator": "^7.6.4",
|
75
|
-
"@types/babel__traverse": "^7.17.1",
|
76
|
-
"@types/node": "^14.18.22",
|
77
|
-
"@types/prettier": "^2.6.3",
|
78
|
-
"@types/semver": "^7.3.10",
|
79
|
-
"@typescript-eslint/eslint-plugin": "^5.30.7",
|
80
|
-
"@typescript-eslint/parser": "^5.30.7",
|
81
|
-
"eslint": "^8.20.0",
|
82
|
-
"eslint-plugin-array-func": "^3.1.7",
|
83
|
-
"eslint-plugin-eslint-comments": "^3.2.0",
|
84
|
-
"eslint-plugin-regexp": "^1.7.0",
|
85
|
-
"eslint-plugin-unicorn": "^43.0.2",
|
86
|
-
"latest-version": "^7.0.0",
|
87
|
-
"rollup-plugin-preserve-shebang": "^1.0.1",
|
88
|
-
"rollup-plugin-terser": "^7.0.2",
|
89
|
-
"semver": "^7.3.7",
|
90
|
-
"typescript": "^4.7.4"
|
91
|
-
},
|
92
|
-
"optionalDependencies": {
|
93
|
-
"deasync": "^0.1.27"
|
66
|
+
"rollup": "^3.26.2",
|
67
|
+
"terser": "^5.19.0"
|
94
68
|
},
|
95
69
|
"engines": {
|
96
|
-
"node": ">=
|
70
|
+
"node": ">=16"
|
97
71
|
},
|
98
72
|
"types": "index.d.ts",
|
99
73
|
"type": "module",
|
100
74
|
"exports": {
|
101
|
-
"
|
102
|
-
|
103
|
-
"require": "./index.cjs"
|
104
|
-
},
|
105
|
-
"./*": "./*",
|
106
|
-
"./generateTypeDeclaration": "./generateTypeDeclaration.js",
|
107
|
-
"./index": "./index.js",
|
108
|
-
"./pull": "./pull.js",
|
109
|
-
"./push": "./push.js",
|
110
|
-
"./syncMacros": "./syncMacros.js",
|
111
|
-
"./watch": "./watch.js",
|
112
|
-
"./bin/hsm": "./bin/hsm.js",
|
113
|
-
"./processScript/index": "./processScript/index.js",
|
114
|
-
"./processScript": "./processScript/index.js",
|
115
|
-
"./processScript/minify": "./processScript/minify.js",
|
116
|
-
"./processScript/postprocess": "./processScript/postprocess.js",
|
117
|
-
"./processScript/preprocess": "./processScript/preprocess.js",
|
118
|
-
"./processScript/shared": "./processScript/shared.js",
|
119
|
-
"./processScript/transform": "./processScript/transform.js"
|
75
|
+
"./*": "./*.js",
|
76
|
+
"./*.js": "./*.js"
|
120
77
|
},
|
121
78
|
"bin": {
|
122
|
-
"hsm": "
|
79
|
+
"hsm.d": "bin/hsm.d.ts",
|
80
|
+
"hsm": "bin/hsm.js"
|
123
81
|
}
|
124
82
|
}
|
package/processScript/index.d.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import { LaxPartial } from "@samual/lib";
|
1
|
+
import type { LaxPartial } from "@samual/lib";
|
2
2
|
export { minify } from "./minify";
|
3
3
|
export { postprocess } from "./postprocess";
|
4
4
|
export { preprocess } from "./preprocess";
|
5
5
|
export { transform } from "./transform";
|
6
|
-
export
|
6
|
+
export type ProcessOptions = {
|
7
7
|
/** whether to minify the given code */
|
8
8
|
minify: boolean;
|
9
9
|
/** 11 a-z 0-9 characters */
|
package/processScript/index.js
CHANGED
@@ -1 +1,310 @@
|
|
1
|
-
import
|
1
|
+
import babelGenerator from '@babel/generator';
|
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 };
|