@glodon-aiot/bot-client-sdk 3.26.0-alpha.4 → 3.26.0-alpha.5
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/README.md +105 -76
- package/dist/es/Config.mjs +63 -85
- package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
- package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
- package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
- package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
- package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
- package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
- package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
- package/dist/es/builtinTools/command.mjs +56 -0
- package/dist/es/builtinTools/constants.mjs +10 -0
- package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +236 -0
- package/dist/es/builtinTools/overview.mjs +77 -0
- package/dist/es/builtinTools/paths.mjs +63 -0
- package/dist/es/builtinTools/schemas.mjs +216 -0
- package/dist/es/builtinTools/tools.mjs +471 -0
- package/dist/es/index.mjs +49 -48
- package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +193 -103
- package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +138 -77
- package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +132 -97
- package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +32 -28
- package/dist/es/localFiles/constants.mjs +1 -1
- package/dist/es/localFiles/host/connectHostBridge.mjs +22 -18
- package/dist/es/localFiles/host/protocol.mjs +6 -5
- package/dist/es/localFiles/schemas.mjs +45 -60
- package/dist/es/localFiles/validate.mjs +37 -37
- package/dist/es/localFiles/workspace/LocalWorkspace.mjs +71 -52
- package/dist/es/package.json.mjs +33 -27
- package/dist/lib/index.js +372 -9
- package/dist/src/Config.d.ts +2 -2
- package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
- package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
- package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
- package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
- package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
- package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
- package/dist/src/builtinTools/command.d.ts +26 -0
- package/dist/src/builtinTools/constants.d.ts +17 -0
- package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
- package/dist/src/builtinTools/overview.d.ts +5 -0
- package/dist/src/builtinTools/paths.d.ts +15 -0
- package/dist/src/builtinTools/schemas.d.ts +3 -0
- package/dist/src/builtinTools/tools.d.ts +60 -0
- package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +4 -1
- package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +20 -1
- package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +4 -1
- package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +14 -0
- package/dist/src/localFiles/adapters/fsaTypes.d.ts +2 -0
- package/dist/src/localFiles/constants.d.ts +2 -1
- package/dist/src/localFiles/host/protocol.d.ts +13 -0
- package/dist/src/localFiles/index.d.ts +3 -2
- package/dist/src/localFiles/schemas.d.ts +2 -3
- package/dist/src/localFiles/types.d.ts +14 -1
- package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +12 -0
- package/package.json +8 -2
- package/dist/es/localFiles/createLocalPythonPlugin.mjs +0 -196
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
var k = Object.defineProperty;
|
|
2
|
+
var U = (r, t, e) => t in r ? k(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
|
+
var A = (r, t, e) => (U(r, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
var E = (r, t, e) => new Promise((n, i) => {
|
|
5
|
+
var l = (s) => {
|
|
6
|
+
try {
|
|
7
|
+
a(e.next(s));
|
|
8
|
+
} catch (o) {
|
|
9
|
+
i(o);
|
|
10
|
+
}
|
|
11
|
+
}, c = (s) => {
|
|
12
|
+
try {
|
|
13
|
+
a(e.throw(s));
|
|
14
|
+
} catch (o) {
|
|
15
|
+
i(o);
|
|
16
|
+
}
|
|
17
|
+
}, a = (s) => s.done ? n(s.value) : Promise.resolve(s.value).then(l, c);
|
|
18
|
+
a((e = e.apply(r, t)).next());
|
|
19
|
+
});
|
|
20
|
+
import { domError as y } from "../../localFiles/adapters/WorkspaceFileAdapter.mjs";
|
|
21
|
+
import { ToolError as x, ToolCode as W } from "../../localFiles/errors.mjs";
|
|
22
|
+
import { WORKSPACE_MOUNT as d } from "../constants.mjs";
|
|
23
|
+
import { isSyntheticCwd as L, mentionsSyntheticDir as O, SYNTHETIC_DIR_MESSAGE as T } from "../paths.mjs";
|
|
24
|
+
import { DEFAULT_BASH_MODULE_URL as B } from "./assets.mjs";
|
|
25
|
+
import { loadBash as F } from "./loadBash.mjs";
|
|
26
|
+
import { PythonRunner as M, NO_PIP_MESSAGE as R } from "./PythonRunner.mjs";
|
|
27
|
+
import { WorkspaceBashFs as N } from "./WorkspaceBashFs.mjs";
|
|
28
|
+
const D = `当前环境没有 Node,请改用 python3 或 shell 命令。
|
|
29
|
+
`, I = /* @__PURE__ */ new Set(["writeFile", "appendFile", "mkdir", "rm", "cp", "mv", "chmod", "symlink", "link", "utimes", "createExclusive"]), P = "dev|bin|usr|proc|etc|System|Library|private|var|opt|sbin|tmp|Applications|Volumes|cores|net|home", G = new RegExp(`(^|[\\s'"=(;|&<>\`:])/(?!${P}(?=$|[/\\s'"\`;|&)<>:]))(?!/)`, "g");
|
|
30
|
+
function H(r) {
|
|
31
|
+
let t = !1;
|
|
32
|
+
return {
|
|
33
|
+
fs: new Proxy(r, {
|
|
34
|
+
get(n, i, l) {
|
|
35
|
+
const c = Reflect.get(n, i, l);
|
|
36
|
+
return typeof c != "function" ? c : (...a) => {
|
|
37
|
+
var o;
|
|
38
|
+
const s = String((o = a[0]) != null ? o : "");
|
|
39
|
+
if (t && I.has(String(i)) && !s.startsWith("/dev/"))
|
|
40
|
+
throw Object.assign(new Error(`EROFS: 只能写入工作区下的路径, open '${s}'`), {
|
|
41
|
+
code: "EROFS"
|
|
42
|
+
});
|
|
43
|
+
return c.apply(n, a);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}),
|
|
47
|
+
lock: () => t = !0
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function j(r) {
|
|
51
|
+
return r instanceof Uint8Array ? r : Uint8Array.from(String(r != null ? r : ""), (t) => t.charCodeAt(0) & 255);
|
|
52
|
+
}
|
|
53
|
+
function K(r) {
|
|
54
|
+
return r instanceof DOMException && r.name === "AbortError";
|
|
55
|
+
}
|
|
56
|
+
function V(r) {
|
|
57
|
+
if (!r)
|
|
58
|
+
return;
|
|
59
|
+
if (r === d)
|
|
60
|
+
return ".";
|
|
61
|
+
if (!r.startsWith(`${d}/`))
|
|
62
|
+
return;
|
|
63
|
+
const t = r.slice(d.length + 1).replace(/\/+$/, "");
|
|
64
|
+
if (!t)
|
|
65
|
+
return ".";
|
|
66
|
+
const e = t.split("/");
|
|
67
|
+
if (!e.some((n) => !n || n === "." || n === ".."))
|
|
68
|
+
return e.join("/");
|
|
69
|
+
}
|
|
70
|
+
function _(r) {
|
|
71
|
+
return !r || r === "." ? d : r === d || r.startsWith(`${d}/`) ? r : `${d}/${r.replace(/^\/+/, "")}`;
|
|
72
|
+
}
|
|
73
|
+
function Y(r) {
|
|
74
|
+
return r.replace(G, (t, e) => `${e}${d}/`);
|
|
75
|
+
}
|
|
76
|
+
function C(r) {
|
|
77
|
+
return r.replace(/\/workspace(?![\p{L}\p{N}_.\-])/gu, (t, e, n) => n[e + t.length] === "/" ? "" : "/");
|
|
78
|
+
}
|
|
79
|
+
class nt {
|
|
80
|
+
constructor(t) {
|
|
81
|
+
A(this, "python");
|
|
82
|
+
this.options = t, this.python = new M({
|
|
83
|
+
adapter: t.adapter,
|
|
84
|
+
sensitive: t.sensitive,
|
|
85
|
+
indexURL: t.pyodideIndexURL,
|
|
86
|
+
createWorker: t.createWorker
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
load() {
|
|
90
|
+
return E(this, null, function* () {
|
|
91
|
+
const t = this.options.moduleURL || B;
|
|
92
|
+
try {
|
|
93
|
+
return yield F(t);
|
|
94
|
+
} catch (e) {
|
|
95
|
+
throw new x(W.NotImplemented, `命令运行时加载失败,请确认页面能访问 ${t}(CSP 需在 script-src 放行该地址)。`);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
create(t, e, n, i) {
|
|
100
|
+
const {
|
|
101
|
+
adapter: l,
|
|
102
|
+
sensitive: c
|
|
103
|
+
} = this.options, a = H(new t.InMemoryFs()), s = new t.MountableFs({
|
|
104
|
+
base: a.fs
|
|
105
|
+
});
|
|
106
|
+
s.mount(d, new N(l, "", c));
|
|
107
|
+
const o = (m, u, p) => t.defineCommand(m, () => E(this, null, function* () {
|
|
108
|
+
return {
|
|
109
|
+
stdout: "",
|
|
110
|
+
stderr: u,
|
|
111
|
+
exitCode: p
|
|
112
|
+
};
|
|
113
|
+
})), h = (m) => t.defineCommand(m, (u, p) => E(this, null, function* () {
|
|
114
|
+
var w;
|
|
115
|
+
try {
|
|
116
|
+
return yield this.python.run({
|
|
117
|
+
args: u,
|
|
118
|
+
cwd: p.cwd,
|
|
119
|
+
stdin: j(p.stdin),
|
|
120
|
+
signal: n
|
|
121
|
+
});
|
|
122
|
+
} catch (f) {
|
|
123
|
+
return f instanceof x || K(f) ? ((w = i.fatal) != null || (i.fatal = f), {
|
|
124
|
+
stdout: "",
|
|
125
|
+
stderr: "",
|
|
126
|
+
exitCode: 1
|
|
127
|
+
}) : {
|
|
128
|
+
stdout: "",
|
|
129
|
+
stderr: `${m}: ${f instanceof Error ? f.message : String(f)}
|
|
130
|
+
`,
|
|
131
|
+
exitCode: 1
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
})), g = new t.Bash({
|
|
135
|
+
fs: s,
|
|
136
|
+
cwd: e,
|
|
137
|
+
env: {
|
|
138
|
+
HOME: "/tmp",
|
|
139
|
+
LANG: "C.UTF-8"
|
|
140
|
+
},
|
|
141
|
+
customCommands: [o("node", D, 127), o("pip", R, 1), o("pip3", R, 1), h("python3"), h("python")],
|
|
142
|
+
// 默认开启时会在执行期间替换页面的 setTimeout、Function 等全局对象,影响宿主页面。
|
|
143
|
+
defenseInDepth: !1
|
|
144
|
+
});
|
|
145
|
+
return a.lock(), g;
|
|
146
|
+
}
|
|
147
|
+
exec(t) {
|
|
148
|
+
return E(this, null, function* () {
|
|
149
|
+
var h, g, m, u, p, w, f, b, v;
|
|
150
|
+
if (L(t.cwd) || O((h = t.command) != null ? h : "") || O((m = (g = t.argv) == null ? void 0 : g[1]) != null ? m : ""))
|
|
151
|
+
return {
|
|
152
|
+
exitCode: 1,
|
|
153
|
+
stdout: "",
|
|
154
|
+
stderr: `${T}
|
|
155
|
+
`
|
|
156
|
+
};
|
|
157
|
+
const e = yield this.load();
|
|
158
|
+
if ((u = t.signal) != null && u.aborted)
|
|
159
|
+
throw y("AbortError");
|
|
160
|
+
const n = _(t.cwd), i = {}, l = this.create(e, n, t.signal, i), [c, a, ...s] = (p = t.argv) != null ? p : [];
|
|
161
|
+
let o;
|
|
162
|
+
try {
|
|
163
|
+
o = t.argv ? yield l.exec(c, {
|
|
164
|
+
cwd: n,
|
|
165
|
+
signal: t.signal,
|
|
166
|
+
args: [_(a), ...s]
|
|
167
|
+
}) : yield l.exec(Y((w = t.command) != null ? w : ""), {
|
|
168
|
+
cwd: n,
|
|
169
|
+
signal: t.signal,
|
|
170
|
+
rawScript: !0
|
|
171
|
+
});
|
|
172
|
+
} catch (S) {
|
|
173
|
+
if (i.fatal)
|
|
174
|
+
throw i.fatal;
|
|
175
|
+
if ((f = t.signal) != null && f.aborted)
|
|
176
|
+
throw y("AbortError");
|
|
177
|
+
const $ = S instanceof Error ? S.message : String(S);
|
|
178
|
+
return {
|
|
179
|
+
exitCode: 1,
|
|
180
|
+
stdout: "",
|
|
181
|
+
stderr: `bash: ${C($)}
|
|
182
|
+
`
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (i.fatal)
|
|
186
|
+
throw i.fatal;
|
|
187
|
+
if ((b = t.signal) != null && b.aborted)
|
|
188
|
+
throw y("AbortError");
|
|
189
|
+
return {
|
|
190
|
+
exitCode: o.exitCode,
|
|
191
|
+
stdout: C(o.stdout),
|
|
192
|
+
stderr: C(o.stderr),
|
|
193
|
+
cwd: V((v = o.env) == null ? void 0 : v.PWD)
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
export {
|
|
199
|
+
nt as BrowserCommandRunner,
|
|
200
|
+
D as NO_NODE_MESSAGE
|
|
201
|
+
};
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
var D = Object.defineProperty;
|
|
2
|
+
var L = Object.getOwnPropertySymbols;
|
|
3
|
+
var j = Object.prototype.hasOwnProperty, M = Object.prototype.propertyIsEnumerable;
|
|
4
|
+
var $ = (r, t, e) => t in r ? D(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e, S = (r, t) => {
|
|
5
|
+
for (var e in t || (t = {}))
|
|
6
|
+
j.call(t, e) && $(r, e, t[e]);
|
|
7
|
+
if (L)
|
|
8
|
+
for (var e of L(t))
|
|
9
|
+
M.call(t, e) && $(r, e, t[e]);
|
|
10
|
+
return r;
|
|
11
|
+
};
|
|
12
|
+
var w = (r, t, e) => ($(r, typeof t != "symbol" ? t + "" : t, e), e);
|
|
13
|
+
var g = (r, t, e) => new Promise((a, s) => {
|
|
14
|
+
var o = (d) => {
|
|
15
|
+
try {
|
|
16
|
+
n(e.next(d));
|
|
17
|
+
} catch (i) {
|
|
18
|
+
s(i);
|
|
19
|
+
}
|
|
20
|
+
}, c = (d) => {
|
|
21
|
+
try {
|
|
22
|
+
n(e.throw(d));
|
|
23
|
+
} catch (i) {
|
|
24
|
+
s(i);
|
|
25
|
+
}
|
|
26
|
+
}, n = (d) => d.done ? a(d.value) : Promise.resolve(d.value).then(o, c);
|
|
27
|
+
n((e = e.apply(r, t)).next());
|
|
28
|
+
});
|
|
29
|
+
import { domError as x } from "../../localFiles/adapters/WorkspaceFileAdapter.mjs";
|
|
30
|
+
import { ToolError as O, ToolCode as R } from "../../localFiles/errors.mjs";
|
|
31
|
+
import { readBytes as W } from "../../localFiles/tools/runtime.mjs";
|
|
32
|
+
import { WORKSPACE_MOUNT as l } from "../constants.mjs";
|
|
33
|
+
import { DEFAULT_PYODIDE_INDEX_URL as A, PYODIDE_PACKAGES as U } from "./assets.mjs";
|
|
34
|
+
import N from "./pyodideRunner.py.mjs";
|
|
35
|
+
import T from "./pyodideWorker.mjs";
|
|
36
|
+
const B = `浏览器环境不能安装依赖。可以直接 import ${U.join("、")},需要其他包请改用宿主环境。
|
|
37
|
+
`, C = 10 * 1024 * 1024, I = 64 * 1024 * 1024;
|
|
38
|
+
function F(r) {
|
|
39
|
+
return !r || r === "." || r === "/" ? l : r === l || r.startsWith(`${l}/`) ? r : `${l}/${r.replace(/^\/+/, "")}`;
|
|
40
|
+
}
|
|
41
|
+
function v(r) {
|
|
42
|
+
if (r === l)
|
|
43
|
+
return "";
|
|
44
|
+
if (r.startsWith(`${l}/`))
|
|
45
|
+
return r.slice(l.length + 1);
|
|
46
|
+
}
|
|
47
|
+
const Y = /* @__PURE__ */ new Set(["-u", "-B", "-E", "-s", "-S", "-I", "-O", "-OO", "-q", "-b", "-bb"]);
|
|
48
|
+
function y(r, t, e = "") {
|
|
49
|
+
return {
|
|
50
|
+
exitCode: r,
|
|
51
|
+
stdout: e,
|
|
52
|
+
stderr: t
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function X(r, t) {
|
|
56
|
+
const e = [];
|
|
57
|
+
for (const a of (t.startsWith("/") ? t : `${r}/${t}`).split("/"))
|
|
58
|
+
!a || a === "." || (a === ".." ? e.pop() : e.push(a));
|
|
59
|
+
return `/${e.join("/")}`;
|
|
60
|
+
}
|
|
61
|
+
function z(r, t, e) {
|
|
62
|
+
const a = new Uint8Array(0);
|
|
63
|
+
for (let s = 0; s < r.length; s += 1) {
|
|
64
|
+
const o = r[s], c = r.slice(s + 1);
|
|
65
|
+
if (o === "-c" || o.startsWith("-c") && !o.startsWith("--")) {
|
|
66
|
+
const n = o === "-c" ? c.shift() : o.slice(2);
|
|
67
|
+
return n === void 0 ? y(2, `python3: -c 后面缺少代码
|
|
68
|
+
`) : {
|
|
69
|
+
mode: "c",
|
|
70
|
+
target: n,
|
|
71
|
+
argv: ["-c", ...c],
|
|
72
|
+
stdin: e
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (o === "-m") {
|
|
76
|
+
const n = c.shift();
|
|
77
|
+
return n ? n === "pip" || n === "ensurepip" ? y(1, B) : {
|
|
78
|
+
mode: "m",
|
|
79
|
+
target: n,
|
|
80
|
+
argv: [n, ...c],
|
|
81
|
+
stdin: e
|
|
82
|
+
} : y(2, `python3: -m 后面缺少模块名
|
|
83
|
+
`);
|
|
84
|
+
}
|
|
85
|
+
if (o === "-V" || o === "--version")
|
|
86
|
+
return {
|
|
87
|
+
mode: "c",
|
|
88
|
+
target: "import sys; print('Python ' + sys.version.split()[0])",
|
|
89
|
+
argv: ["-c"],
|
|
90
|
+
stdin: e
|
|
91
|
+
};
|
|
92
|
+
if (o === "-h" || o === "--help")
|
|
93
|
+
return y(0, "", `usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
|
|
94
|
+
`);
|
|
95
|
+
if (!(Y.has(o) || /^-[WX]./.test(o))) {
|
|
96
|
+
if (o === "-W" || o === "-X") {
|
|
97
|
+
s += 1;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
return o === "-" ? {
|
|
101
|
+
mode: "c",
|
|
102
|
+
target: new TextDecoder().decode(e),
|
|
103
|
+
argv: ["-", ...c],
|
|
104
|
+
stdin: a
|
|
105
|
+
} : o.startsWith("-") ? y(2, `python3: 浏览器环境不支持参数 ${o}
|
|
106
|
+
`) : {
|
|
107
|
+
mode: "file",
|
|
108
|
+
target: X(t, o),
|
|
109
|
+
argv: [o, ...c],
|
|
110
|
+
stdin: e
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return e.length > 0 ? {
|
|
115
|
+
mode: "c",
|
|
116
|
+
target: new TextDecoder().decode(e),
|
|
117
|
+
argv: [""],
|
|
118
|
+
stdin: a
|
|
119
|
+
} : y(2, `浏览器环境不支持交互式 Python,请用 python3 -c "…" 或脚本文件。
|
|
120
|
+
`);
|
|
121
|
+
}
|
|
122
|
+
function P(r) {
|
|
123
|
+
var t;
|
|
124
|
+
return r && typeof r == "object" ? String((t = r.name) != null ? t : "") : "";
|
|
125
|
+
}
|
|
126
|
+
function k(r) {
|
|
127
|
+
return r instanceof Error ? r.message : String(r);
|
|
128
|
+
}
|
|
129
|
+
function b(r, t) {
|
|
130
|
+
return new Promise((e, a) => {
|
|
131
|
+
if (!r)
|
|
132
|
+
return;
|
|
133
|
+
const s = () => {
|
|
134
|
+
t == null || t(), a(x("AbortError"));
|
|
135
|
+
};
|
|
136
|
+
r.aborted ? s() : r.addEventListener("abort", s, {
|
|
137
|
+
once: !0
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
function G(r) {
|
|
142
|
+
const t = URL.createObjectURL(new Blob([r], {
|
|
143
|
+
type: "text/javascript"
|
|
144
|
+
})), e = new Worker(t, {
|
|
145
|
+
type: "module"
|
|
146
|
+
}), a = () => URL.revokeObjectURL(t);
|
|
147
|
+
return e.addEventListener("message", a, {
|
|
148
|
+
once: !0
|
|
149
|
+
}), e.addEventListener("error", a, {
|
|
150
|
+
once: !0
|
|
151
|
+
}), e;
|
|
152
|
+
}
|
|
153
|
+
class et {
|
|
154
|
+
constructor(t) {
|
|
155
|
+
w(this, "state");
|
|
156
|
+
w(this, "queue", Promise.resolve());
|
|
157
|
+
w(this, "nextId", 0);
|
|
158
|
+
this.options = t;
|
|
159
|
+
}
|
|
160
|
+
get indexURL() {
|
|
161
|
+
return this.options.indexURL || A;
|
|
162
|
+
}
|
|
163
|
+
loadError(t) {
|
|
164
|
+
return new O(R.NotImplemented, `Python 运行环境加载失败,请确认页面能访问 ${this.indexURL}(CSP 需在 script-src、connect-src 放行该地址,worker-src 放行 blob:)。${t}`);
|
|
165
|
+
}
|
|
166
|
+
dispose(t, e) {
|
|
167
|
+
this.state === t && (this.state = void 0), t.worker.terminate(), t.pending.forEach(({
|
|
168
|
+
reject: a
|
|
169
|
+
}) => a(e)), t.pending.clear();
|
|
170
|
+
}
|
|
171
|
+
spawn() {
|
|
172
|
+
var o;
|
|
173
|
+
let t;
|
|
174
|
+
try {
|
|
175
|
+
t = ((o = this.options.createWorker) != null ? o : G)(T);
|
|
176
|
+
} catch (c) {
|
|
177
|
+
throw this.loadError(`(${k(c)})`);
|
|
178
|
+
}
|
|
179
|
+
let e;
|
|
180
|
+
const a = new Promise((c, n) => e = {
|
|
181
|
+
resolve: c,
|
|
182
|
+
reject: n
|
|
183
|
+
}), s = {
|
|
184
|
+
worker: t,
|
|
185
|
+
ready: a,
|
|
186
|
+
pending: /* @__PURE__ */ new Map()
|
|
187
|
+
};
|
|
188
|
+
return a.catch(() => {
|
|
189
|
+
}), t.onmessage = ({
|
|
190
|
+
data: c
|
|
191
|
+
}) => {
|
|
192
|
+
var d;
|
|
193
|
+
const n = c;
|
|
194
|
+
if (n.type === "ready")
|
|
195
|
+
e.resolve();
|
|
196
|
+
else if (n.type === "failed") {
|
|
197
|
+
const i = this.loadError(`(${n.message})`);
|
|
198
|
+
e.reject(i), this.dispose(s, i);
|
|
199
|
+
} else
|
|
200
|
+
n.type === "result" && ((d = s.pending.get(n.id)) == null || d.resolve(n), s.pending.delete(n.id));
|
|
201
|
+
}, t.onerror = (c) => {
|
|
202
|
+
var i;
|
|
203
|
+
const n = k((i = c == null ? void 0 : c.message) != null ? i : c), d = this.loadError(`(${n})`);
|
|
204
|
+
e.reject(d), this.dispose(s, new Error(`Python 运行环境意外退出:${n}`));
|
|
205
|
+
}, t.postMessage({
|
|
206
|
+
type: "init",
|
|
207
|
+
indexURL: this.indexURL,
|
|
208
|
+
packages: U,
|
|
209
|
+
runner: N
|
|
210
|
+
}), s;
|
|
211
|
+
}
|
|
212
|
+
run(t) {
|
|
213
|
+
const e = this.queue.then(() => this.runNow(t));
|
|
214
|
+
return this.queue = e.catch(() => {
|
|
215
|
+
}), e;
|
|
216
|
+
}
|
|
217
|
+
runNow(t) {
|
|
218
|
+
return g(this, null, function* () {
|
|
219
|
+
var p, h, m;
|
|
220
|
+
const e = F(t.cwd), a = z(t.args, e, (p = t.stdin) != null ? p : new Uint8Array(0));
|
|
221
|
+
if (!("mode" in a))
|
|
222
|
+
return a;
|
|
223
|
+
if ((h = t.signal) != null && h.aborted)
|
|
224
|
+
throw x("AbortError");
|
|
225
|
+
const s = (m = this.state) != null ? m : this.state = this.spawn();
|
|
226
|
+
yield Promise.race([s.ready, b(t.signal)]);
|
|
227
|
+
const o = yield this.collect(t.signal), c = this.nextId += 1, n = new Promise((u, E) => s.pending.set(c, {
|
|
228
|
+
resolve: u,
|
|
229
|
+
reject: E
|
|
230
|
+
}));
|
|
231
|
+
s.worker.postMessage(S({
|
|
232
|
+
type: "run",
|
|
233
|
+
id: c,
|
|
234
|
+
files: o.files,
|
|
235
|
+
dirs: o.dirs,
|
|
236
|
+
cwd: e
|
|
237
|
+
}, a), o.files.map((u) => u.data.buffer));
|
|
238
|
+
const d = yield Promise.race([n, b(t.signal, () => this.dispose(s, x("AbortError")))]), i = [];
|
|
239
|
+
if (o.skipped.length > 0) {
|
|
240
|
+
const u = o.skipped.slice(0, 5).map((_) => `/${_}`), E = o.skipped.length > u.length ? ` 等 ${o.skipped.length} 个` : "";
|
|
241
|
+
i.push(`提示:${u.join("、")}${E}文件超过同步上限(单个 10 MB、合计 64 MB),Python 里看不到。`);
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
exitCode: (yield this.writeBack(d, o, i)) && d.exitCode === 0 ? 1 : d.exitCode,
|
|
245
|
+
stdout: d.stdout,
|
|
246
|
+
stderr: d.stderr + i.map((u) => `${u}
|
|
247
|
+
`).join("")
|
|
248
|
+
};
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
/** 执行前把整个工作区读进 Pyodide。 */
|
|
252
|
+
collect(t) {
|
|
253
|
+
return g(this, null, function* () {
|
|
254
|
+
const {
|
|
255
|
+
adapter: e,
|
|
256
|
+
sensitive: a
|
|
257
|
+
} = this.options, s = {
|
|
258
|
+
files: [],
|
|
259
|
+
dirs: [],
|
|
260
|
+
synced: /* @__PURE__ */ new Set(),
|
|
261
|
+
skipped: []
|
|
262
|
+
};
|
|
263
|
+
let o = 0;
|
|
264
|
+
const c = (n) => g(this, null, function* () {
|
|
265
|
+
var i, f;
|
|
266
|
+
let d;
|
|
267
|
+
try {
|
|
268
|
+
d = yield e.listEntries(n, t);
|
|
269
|
+
} catch (p) {
|
|
270
|
+
if (P(p) === "NotFoundError" || P(p) === "TypeMismatchError")
|
|
271
|
+
return;
|
|
272
|
+
throw p;
|
|
273
|
+
}
|
|
274
|
+
s.dirs.push(n ? `${l}/${n}` : l);
|
|
275
|
+
for (const p of d.sort((h, m) => h.name.localeCompare(m.name))) {
|
|
276
|
+
const h = n ? `${n}/${p.name}` : p.name;
|
|
277
|
+
if (p.kind === "directory") {
|
|
278
|
+
yield c(h);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (a.match(h, "read"))
|
|
282
|
+
continue;
|
|
283
|
+
const m = (f = (i = p.size) != null ? i : (yield e.stat(h)).size) != null ? f : 0;
|
|
284
|
+
if (m > C || o + m > I) {
|
|
285
|
+
s.skipped.push(h);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
const u = yield W(yield e.readFile(h));
|
|
289
|
+
o += u.byteLength, s.files.push({
|
|
290
|
+
path: `${l}/${h}`,
|
|
291
|
+
data: u
|
|
292
|
+
}), s.synced.add(h);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
return yield c(""), s;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
exists(t) {
|
|
299
|
+
return g(this, null, function* () {
|
|
300
|
+
try {
|
|
301
|
+
return yield this.options.adapter.stat(t), !0;
|
|
302
|
+
} catch (e) {
|
|
303
|
+
return !1;
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/** 把 Python 里的改动写回工作区,返回是否有写回失败。 */
|
|
308
|
+
writeBack(t, e, a) {
|
|
309
|
+
return g(this, null, function* () {
|
|
310
|
+
const {
|
|
311
|
+
adapter: s,
|
|
312
|
+
sensitive: o
|
|
313
|
+
} = this.options;
|
|
314
|
+
let c = !1;
|
|
315
|
+
const n = (i, f) => {
|
|
316
|
+
c = !0, a.push(`写回 ${i} 失败:${k(f)}`);
|
|
317
|
+
};
|
|
318
|
+
for (const i of t.createdDirs.sort()) {
|
|
319
|
+
const f = v(i);
|
|
320
|
+
!f || !s.createDirectory || (yield s.createDirectory(f).catch((p) => n(i, p)));
|
|
321
|
+
}
|
|
322
|
+
for (const {
|
|
323
|
+
path: i,
|
|
324
|
+
data: f
|
|
325
|
+
} of t.written) {
|
|
326
|
+
const p = v(i);
|
|
327
|
+
p && (o.match(p, "write") ? a.push(`没有写回 ${i}:敏感文件不能由命令修改。`) : !e.synced.has(p) && (yield this.exists(p)) ? a.push(`没有写回 ${i}:工作区里已有这个文件,但它没有同步进 Python,为避免覆盖已跳过。`) : yield s.writeFile(p, f, {
|
|
328
|
+
createDirectories: !0
|
|
329
|
+
}).catch((h) => n(i, h)));
|
|
330
|
+
}
|
|
331
|
+
if (t.removed.length > 0 && !s.remove)
|
|
332
|
+
return a.push(`当前工作区不支持删除,${t.removed.join("、")} 仍然保留。`), c;
|
|
333
|
+
for (const i of t.removed) {
|
|
334
|
+
const f = v(i);
|
|
335
|
+
f && (yield s.remove(f).catch((p) => {
|
|
336
|
+
P(p) !== "NotFoundError" && n(i, p);
|
|
337
|
+
}));
|
|
338
|
+
}
|
|
339
|
+
const d = t.removedDirs.sort((i, f) => f.split("/").length - i.split("/").length);
|
|
340
|
+
for (const i of d) {
|
|
341
|
+
const f = v(i);
|
|
342
|
+
f && (yield s.remove(f).catch(() => {
|
|
343
|
+
}));
|
|
344
|
+
}
|
|
345
|
+
return c;
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
export {
|
|
350
|
+
C as MAX_SYNC_FILE_BYTES,
|
|
351
|
+
I as MAX_SYNC_TOTAL_BYTES,
|
|
352
|
+
B as NO_PIP_MESSAGE,
|
|
353
|
+
et as PythonRunner,
|
|
354
|
+
z as parsePythonArgs,
|
|
355
|
+
F as toMount
|
|
356
|
+
};
|