@ohos-ports/bun-repl 0.0.0-stage → 2.1.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.json +89 -0
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/dist/cli.js +62 -0
- package/dist/colors.js +68 -0
- package/dist/module/repl.js +83 -0
- package/dist/repl.js +945 -0
- package/dist/shims/bun.js +126 -0
- package/dist/shims/inspector-server.js +518 -0
- package/dist/swcrc.js +66 -0
- package/dist/transpiler.js +86 -0
- package/dist/utl/index.d.ts +30 -0
- package/dist/utl/index.js +2760 -0
- package/dist/utl/primordials.js +216 -0
- package/dist/utl/proxy.js +30 -0
- package/package.json +50 -4
- package/scripts/build.mjs +98 -0
- package/src/cli.ts +54 -0
- package/src/colors.ts +73 -0
- package/src/module/repl.ts +77 -0
- package/src/repl.ts +947 -0
- package/src/shims/bun.js +126 -0
- package/src/shims/inspector-server.js +518 -0
- package/src/swcrc.ts +70 -0
- package/src/transpiler.ts +96 -0
- package/src/utl/index.d.ts +117 -0
- package/src/utl/index.js +2760 -0
- package/src/utl/primordials.js +216 -0
- package/src/utl/proxy.js +30 -0
- package/tsconfig.json +99 -0
package/src/swcrc.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import swc from '@ohos-ports/swc-core';
|
|
2
|
+
|
|
3
|
+
const REPL_FILENAME = '$bun$repl.ts' as const;
|
|
4
|
+
|
|
5
|
+
const swcrc: swc.Options = {
|
|
6
|
+
inlineSourcesContent: false,
|
|
7
|
+
sourceMaps: true,
|
|
8
|
+
isModule: !process.argv.includes('--sloppy'),
|
|
9
|
+
minify: false,
|
|
10
|
+
swcrc: false,
|
|
11
|
+
filename: REPL_FILENAME,
|
|
12
|
+
module: {
|
|
13
|
+
type: 'commonjs',
|
|
14
|
+
lazy: false,
|
|
15
|
+
strict: true,
|
|
16
|
+
strictMode: false,
|
|
17
|
+
ignoreDynamic: true,
|
|
18
|
+
importInterop: 'none',
|
|
19
|
+
},
|
|
20
|
+
jsc: {
|
|
21
|
+
target: 'esnext',
|
|
22
|
+
parser: {
|
|
23
|
+
syntax: 'typescript',
|
|
24
|
+
dynamicImport: true,
|
|
25
|
+
decorators: true,
|
|
26
|
+
},
|
|
27
|
+
transform: {
|
|
28
|
+
useDefineForClassFields: false,
|
|
29
|
+
treatConstEnumAsEnum: false,
|
|
30
|
+
optimizer: {
|
|
31
|
+
simplify: false,
|
|
32
|
+
globals: {
|
|
33
|
+
vars: {
|
|
34
|
+
'import.meta': /*js*/`((cwd) => {
|
|
35
|
+
const {
|
|
36
|
+
join, dirname, basename, resolve, getcwd,
|
|
37
|
+
pathToFileURL, fileURLToPath,
|
|
38
|
+
StringStartsWith, ObjectFreeze
|
|
39
|
+
} = (0, eval)('this')['@@bunReplRuntimeHelpers'];
|
|
40
|
+
cwd = join(getcwd(), '${REPL_FILENAME}');
|
|
41
|
+
return ObjectFreeze({
|
|
42
|
+
url: pathToFileURL(cwd).href,
|
|
43
|
+
main: true,
|
|
44
|
+
path: cwd,
|
|
45
|
+
dir: dirname(cwd),
|
|
46
|
+
file: basename(cwd),
|
|
47
|
+
require: require,
|
|
48
|
+
async resolve(id, parent) {
|
|
49
|
+
return this.resolveSync(id, parent);
|
|
50
|
+
},
|
|
51
|
+
resolveSync(id, parent) {
|
|
52
|
+
return require.resolve(id, {
|
|
53
|
+
paths: typeof parent === "string" ? [
|
|
54
|
+
resolve(StringStartsWith(parent, 'file://') ? fileURLToPath(parent) : parent, '.')
|
|
55
|
+
] : void 0
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
})()`,
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
preserveAllComments: true,
|
|
65
|
+
keepClassNames: true,
|
|
66
|
+
loose: false,
|
|
67
|
+
}
|
|
68
|
+
} as const;
|
|
69
|
+
|
|
70
|
+
export default swcrc;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import swc from '@ohos-ports/swc-core';
|
|
2
|
+
import swcDefaultConfig from './swcrc.ts';
|
|
3
|
+
|
|
4
|
+
type replTranspiledImportInfo = {
|
|
5
|
+
varname?: string,
|
|
6
|
+
destructuredVars?: string[],
|
|
7
|
+
moduleIdentifier: string,
|
|
8
|
+
wildcard: boolean,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const StringReplaceAll = Function.prototype.call.bind(String.prototype.replaceAll);
|
|
12
|
+
const randomUUID = crypto.randomUUID.bind(crypto);
|
|
13
|
+
const safeUUID = () => StringReplaceAll(randomUUID(), '-', '') as string;
|
|
14
|
+
const SLOPPY_MODE = process.argv.includes('--sloppy');
|
|
15
|
+
|
|
16
|
+
export default class Transpiler extends swc.Compiler {
|
|
17
|
+
constructor(config: swc.Options = {}) {
|
|
18
|
+
super();
|
|
19
|
+
this.#config = Object.assign(swcDefaultConfig, config);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
transpile(code: string): string {
|
|
23
|
+
return super.transformSync(code, this.#config).code;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// REPL-specific adjustments needed for the code to work in a REPL context. (Ran before transpile)
|
|
27
|
+
preprocess(code: string): string {
|
|
28
|
+
return code
|
|
29
|
+
.replaceAll(/import(?:(?:(?:[ \n\t]+([^ *\n\t{},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*\{(?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+\})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t{}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/g,
|
|
30
|
+
($0, defaultVar?: string, destructuredVars?: string, wildcardVar?: string, moduleIdentifier: string = '') => {
|
|
31
|
+
let str = `${$0};/*$replTranspiledImport:`;
|
|
32
|
+
let info = { moduleIdentifier } as replTranspiledImportInfo;
|
|
33
|
+
if (defaultVar) info.varname = defaultVar.trim();
|
|
34
|
+
if (wildcardVar) info.varname = wildcardVar.trim();
|
|
35
|
+
if (destructuredVars) {
|
|
36
|
+
info.destructuredVars = destructuredVars.trim().slice(1, -1).trim().split(',').map(s => s.trim());
|
|
37
|
+
}
|
|
38
|
+
info.wildcard = !!wildcardVar;
|
|
39
|
+
str += JSON.stringify(info) + '*/';
|
|
40
|
+
if (info.varname) str += `void ${info.varname};`;
|
|
41
|
+
if (info.destructuredVars) info.destructuredVars.forEach(variable => {
|
|
42
|
+
const match = variable.match(/[^ \n\t]+[ \n\t]+as[ \n\t]+([^ \n\t]+)/);
|
|
43
|
+
str += `void ${match ? match[1] : variable};`;
|
|
44
|
+
});
|
|
45
|
+
return str;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// REPL-specific adjustments needed for the code to work in a REPL context. (Ran after transpile)
|
|
50
|
+
postprocess(code: string): string {
|
|
51
|
+
let importsData = [] as ({ requireVar: string, requireStr: string, info: replTranspiledImportInfo, uuid: string })[];
|
|
52
|
+
code = (SLOPPY_MODE ? '' : '"use strict";void 0;\n') + code
|
|
53
|
+
.replaceAll(/(?:var|let|const) (_.+?) = require\("(.+?)"\);[ \t\n;]*\/\*\$replTranspiledImport:({.+?})\*\//g,
|
|
54
|
+
($0, requireVar: string, requireStr: string, infoStr: string) => {
|
|
55
|
+
const info = JSON.parse(infoStr) as replTranspiledImportInfo;
|
|
56
|
+
const uuid = safeUUID();
|
|
57
|
+
importsData.push({ requireVar, requireStr, info, uuid });
|
|
58
|
+
let str = `const ${requireVar}${uuid} = require("${requireStr}");`;
|
|
59
|
+
if (info.varname) {
|
|
60
|
+
str += `const ${info.varname}=${requireVar}${uuid}`;
|
|
61
|
+
if (!info.wildcard) str += `.default??${requireVar}${uuid};`;
|
|
62
|
+
str += ';';
|
|
63
|
+
}
|
|
64
|
+
if (info.destructuredVars) {
|
|
65
|
+
let ifstr = 'true.valueOf["@@replTemp"]={};if(!(';
|
|
66
|
+
let delstr = ')){';
|
|
67
|
+
info.destructuredVars.forEach(variable => {
|
|
68
|
+
let exportStr = variable;
|
|
69
|
+
const match = variable.match(/([^ \n\t]+)[ \n\t]+as[ \n\t]+([^ \n\t]+)/);
|
|
70
|
+
if (match) {
|
|
71
|
+
variable = match[2];
|
|
72
|
+
exportStr = match[1];
|
|
73
|
+
str += `const{${exportStr}:${variable}}=${requireVar}${uuid};`;
|
|
74
|
+
} else str += `const{${variable}}=${requireVar}${uuid};`;
|
|
75
|
+
ifstr += `(true.valueOf['@@replTemp'].$v="${exportStr}") in ${requireVar}${uuid}&&`;
|
|
76
|
+
delstr += `delete true.valueOf['@@replTemp']["${variable}"];`;
|
|
77
|
+
});
|
|
78
|
+
const errmsg = `Import named '\${true.valueOf["@@replTemp"].$v}' not found in module '${requireStr}'.`;
|
|
79
|
+
delstr += `{const $err=new SyntaxError(\`${errmsg}\`);$err.stack='';throw $err;}};`;
|
|
80
|
+
ifstr = ifstr.slice(0, -2);
|
|
81
|
+
str += ifstr + delstr;
|
|
82
|
+
}
|
|
83
|
+
return str + '\n';
|
|
84
|
+
});
|
|
85
|
+
for (const importData of importsData) {
|
|
86
|
+
// node port: rewrite every remaining reference to the renamed require
|
|
87
|
+
// variable — the @ohos-ports/swc-core (1.16) emit uses namespace-style
|
|
88
|
+
// usages like `(0, _mod.fn)(...)`, not only the `void _mod` markers.
|
|
89
|
+
// \b keeps the already-renamed `${requireVar}${uuid}` intact.
|
|
90
|
+
code = code.replaceAll(new RegExp(`\\b${importData.requireVar}\\b`, 'g'), `${importData.requireVar}${importData.uuid}`);
|
|
91
|
+
}
|
|
92
|
+
return code;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
readonly #config: swc.Options;
|
|
96
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@license
|
|
3
|
+
Extracted from https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
4
|
+
|
|
5
|
+
This project is licensed under the MIT license.
|
|
6
|
+
Copyrights are respective of each contributor listed at the beginning of each definition file.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
9
|
+
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
10
|
+
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
16
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
17
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* The `util` module supports the needs of Node.js internal APIs. Many of the
|
|
21
|
+
* utilities are useful for application and module developers as well.
|
|
22
|
+
* @see [source](https://github.com/nodejs/node/blob/v17.6.0/lib/util.js)
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export interface InspectOptions {
|
|
26
|
+
/**If set to `true`, getters are going to be inspected as well.
|
|
27
|
+
* If set to `'get'` only getters without setter are going to be inspected.
|
|
28
|
+
* If set to `'set'` only getters having a corresponding setter are going to be inspected.
|
|
29
|
+
* This might cause side effects depending on the getter function.
|
|
30
|
+
* @default false */
|
|
31
|
+
getters?: "get" | "set" | boolean | undefined;
|
|
32
|
+
showHidden?: boolean | undefined;
|
|
33
|
+
/** @default 2 */
|
|
34
|
+
depth?: number | null | undefined;
|
|
35
|
+
colors?: boolean | undefined;
|
|
36
|
+
customInspect?: boolean | undefined;
|
|
37
|
+
showProxy?: boolean | undefined;
|
|
38
|
+
maxArrayLength?: number | null | undefined;
|
|
39
|
+
/**Specifies the maximum number of characters to include when formatting.
|
|
40
|
+
* Set to `null` or `Infinity` to show all elements.
|
|
41
|
+
* Set to `0` or negative to show no characters.
|
|
42
|
+
* @default 10000 */
|
|
43
|
+
maxStringLength?: number | null | undefined;
|
|
44
|
+
breakLength?: number | undefined;
|
|
45
|
+
/**Setting this to `false` causes each object key to be displayed on a new line.
|
|
46
|
+
* It will also add new lines to text that is longer than `breakLength`.
|
|
47
|
+
* If set to a number, the most `n` inner elements are united on a single line as long as all properties fit into `breakLength`.
|
|
48
|
+
* Short array elements are also grouped together. Note that no text will be reduced below 16 characters, no matter the `breakLength` size.
|
|
49
|
+
* @default 3 */
|
|
50
|
+
compact?: boolean | number | undefined;
|
|
51
|
+
sorted?: boolean | ((a: string, b: string) => number) | undefined;
|
|
52
|
+
numericSeparator?: boolean | undefined;
|
|
53
|
+
}
|
|
54
|
+
export type Style = "special" | "number" | "bigint" | "boolean" | "undefined" | "null" | "string" | "symbol" | "date" | "regexp" | "module";
|
|
55
|
+
export type CustomInspectFunction = (depth: number, options: InspectOptionsStylized) => string;
|
|
56
|
+
export interface InspectOptionsStylized extends InspectOptions {
|
|
57
|
+
/** Write your own function for adding color to the output, or use one of the built-in stylize* functions. */
|
|
58
|
+
stylize(text: string, styleType: Style): string;
|
|
59
|
+
}
|
|
60
|
+
/**The `util.format()` method returns a formatted string using the first argument
|
|
61
|
+
* as a `printf`\-like format string which can contain zero or more format specifiers.
|
|
62
|
+
* Each specifier is replaced with the converted value from the corresponding argument.
|
|
63
|
+
*
|
|
64
|
+
* `util.format()` is a synchronous method that is intended as a debugging tool. Some input values can have a
|
|
65
|
+
* significant performance overhead that can block the event loop. Use this function with care and never in a hot code path.
|
|
66
|
+
* @since v0.5.3
|
|
67
|
+
* @param format A `printf`-like format string. */
|
|
68
|
+
export function format(format?: any, ...param: any[]): string;
|
|
69
|
+
/**
|
|
70
|
+
* This function is identical to {@link format}, except in that it takes
|
|
71
|
+
* an `inspectOptions` argument which specifies options that are passed along to {@link inspect}.
|
|
72
|
+
* ```js
|
|
73
|
+
* util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });
|
|
74
|
+
* // Returns 'See object { foo: 42 }', where `42` is colored as a number
|
|
75
|
+
* // when printed to a terminal.
|
|
76
|
+
* ```
|
|
77
|
+
* @since v10.0.0*/
|
|
78
|
+
export function formatWithOptions(inspectOptions: InspectOptions, format?: any, ...param: any[]): string;
|
|
79
|
+
/**The `util.inspect()` method returns a string representation of `object` that is intended for debugging.
|
|
80
|
+
* The output of `util.inspect` may change at any time and should not be depended upon programmatically.
|
|
81
|
+
* Additional `options` may be passed that alter the result.
|
|
82
|
+
*
|
|
83
|
+
* `util.inspect()` is a synchronous method intended for debugging. Its maximum output length is approximately 128 MB.
|
|
84
|
+
* Inputs that result in longer output will be truncated.
|
|
85
|
+
* @since v0.3.0
|
|
86
|
+
* @param object Any JavaScript primitive or `Object`.
|
|
87
|
+
* @return The representation of `object`. */
|
|
88
|
+
export function inspect(object: any, showHidden?: boolean, depth?: number | null, color?: boolean): string;
|
|
89
|
+
export function inspect(object: any, options?: InspectOptions): string;
|
|
90
|
+
export namespace inspect {
|
|
91
|
+
let colors: { [key: number]: number; };
|
|
92
|
+
let styles: { [K in Style]: string; };
|
|
93
|
+
let defaultOptions: InspectOptions;
|
|
94
|
+
/** Allows changing inspect settings from the repl. */
|
|
95
|
+
let replDefaults: InspectOptions;
|
|
96
|
+
/** That can be used to declare custom inspect functions. */
|
|
97
|
+
const custom: unique symbol;
|
|
98
|
+
}
|
|
99
|
+
/**Returns `str` with any ANSI escape codes removed.
|
|
100
|
+
* ```js
|
|
101
|
+
* console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'));
|
|
102
|
+
* // Prints "value"
|
|
103
|
+
* ```
|
|
104
|
+
* @since v16.11.0 */
|
|
105
|
+
export function stripVTControlCharacters(str: string): string;
|
|
106
|
+
/**Colorize `text` with ANSI escapes according to the styleType. Mostly used in inspect() options.
|
|
107
|
+
* ```typescript
|
|
108
|
+
* inspect({ a: 'b' }, { stylize: stylizeWithColor });
|
|
109
|
+
* ``` */
|
|
110
|
+
export function stylizeWithColor(text: string, styleType: Style): string;
|
|
111
|
+
/**Colorize `text` using HTML span tags and style. Mostly used in inspect() options.
|
|
112
|
+
* ```typescript
|
|
113
|
+
* inspect({ a: 'b' }, { stylize: stylizeWithHTML });
|
|
114
|
+
* ``` */
|
|
115
|
+
export function stylizeWithHTML(text: string, styleType: Style): string;
|
|
116
|
+
|
|
117
|
+
export const $Proxy: ProxyConstructor; //! modified
|