@glodon-aiot/bot-client-sdk 3.26.0-alpha.8 → 3.26.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +108 -96
  2. package/dist/es/Config.mjs +63 -85
  3. package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
  4. package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
  5. package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
  6. package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
  7. package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
  8. package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
  9. package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
  10. package/dist/es/builtinTools/command.mjs +56 -0
  11. package/dist/es/builtinTools/constants.mjs +10 -0
  12. package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +244 -0
  13. package/dist/es/builtinTools/overview.mjs +77 -0
  14. package/dist/es/builtinTools/paths.mjs +63 -0
  15. package/dist/es/builtinTools/schemas.mjs +216 -0
  16. package/dist/es/builtinTools/tools.mjs +471 -0
  17. package/dist/es/index.mjs +49 -48
  18. package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +193 -103
  19. package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +117 -132
  20. package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +132 -97
  21. package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +32 -28
  22. package/dist/es/localFiles/constants.mjs +1 -1
  23. package/dist/es/localFiles/host/connectHostBridge.mjs +37 -34
  24. package/dist/es/localFiles/host/protocol.mjs +6 -5
  25. package/dist/es/localFiles/host/sandboxClient.mjs +324 -0
  26. package/dist/es/localFiles/host/sandboxFrames.mjs +21 -0
  27. package/dist/es/localFiles/schemas.mjs +45 -60
  28. package/dist/es/localFiles/validate.mjs +37 -37
  29. package/dist/es/localFiles/workspace/LocalWorkspace.mjs +71 -52
  30. package/dist/es/package.json.mjs +38 -29
  31. package/dist/lib/index.js +374 -10
  32. package/dist/src/Config.d.ts +2 -2
  33. package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
  34. package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
  35. package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
  36. package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
  37. package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
  38. package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
  39. package/dist/src/builtinTools/command.d.ts +28 -0
  40. package/dist/src/builtinTools/constants.d.ts +17 -0
  41. package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
  42. package/dist/src/builtinTools/overview.d.ts +5 -0
  43. package/dist/src/builtinTools/paths.d.ts +15 -0
  44. package/dist/src/builtinTools/schemas.d.ts +3 -0
  45. package/dist/src/builtinTools/tools.d.ts +60 -0
  46. package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +4 -1
  47. package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +28 -15
  48. package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +4 -1
  49. package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +14 -0
  50. package/dist/src/localFiles/adapters/fsaTypes.d.ts +2 -0
  51. package/dist/src/localFiles/constants.d.ts +2 -1
  52. package/dist/src/localFiles/host/protocol.d.ts +21 -14
  53. package/dist/src/localFiles/host/sandboxClient.d.ts +62 -0
  54. package/dist/src/localFiles/host/sandboxFrames.d.ts +9 -0
  55. package/dist/src/localFiles/index.d.ts +3 -2
  56. package/dist/src/localFiles/schemas.d.ts +2 -3
  57. package/dist/src/localFiles/types.d.ts +14 -1
  58. package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +12 -0
  59. package/package.json +13 -4
  60. package/dist/es/localFiles/createLocalPythonPlugin.mjs +0 -196
@@ -1,50 +1,54 @@
1
- var p = (n, e) => {
1
+ var a = (n, e) => {
2
2
  if (e = Symbol[n])
3
3
  return e;
4
4
  throw Error("Symbol." + n + " is not defined");
5
5
  };
6
- var a = function(n, e) {
6
+ var x = function(n, e) {
7
7
  this[0] = n, this[1] = e;
8
- }, s = (n, e, l) => {
9
- var d = (t, f, r, c) => {
8
+ }, E = (n, e, y) => {
9
+ var d = (t, f, o, l) => {
10
10
  try {
11
- var x = l[t](f), E = (f = x.value) instanceof a, g = x.done;
12
- Promise.resolve(E ? f[0] : f).then((y) => E ? d(t === "return" ? t : "next", f[1] ? { done: y.done, value: y.value } : y, r, c) : r({ value: y, done: g })).catch((y) => d("throw", y, r, c));
13
- } catch (y) {
14
- c(y);
11
+ var c = y[t](f), p = (f = c.value) instanceof x, u = c.done;
12
+ Promise.resolve(p ? f[0] : f).then((s) => p ? d(t === "return" ? t : "next", f[1] ? { done: s.done, value: s.value } : s, o, l) : o({ value: s, done: u })).catch((s) => d("throw", s, o, l));
13
+ } catch (s) {
14
+ l(s);
15
15
  }
16
- }, o = (t) => i[t] = (f) => new Promise((r, c) => d(t, f, r, c)), i = {};
17
- return l = l.apply(n, e), i[Symbol.asyncIterator] = () => i, o("next"), o("throw"), o("return"), i;
18
- }, w = (n) => {
19
- var e = n[p("asyncIterator")], l = !1, d, o = {};
20
- return e == null ? (e = n[p("iterator")](), d = (i) => o[i] = (t) => e[i](t)) : (e = e.call(n), d = (i) => o[i] = (t) => {
21
- if (l) {
22
- if (l = !1, i === "throw")
16
+ }, r = (t) => i[t] = (f) => new Promise((o, l) => d(t, f, o, l)), i = {};
17
+ return y = y.apply(n, e), i[Symbol.asyncIterator] = () => i, r("next"), r("throw"), r("return"), i;
18
+ }, h = (n) => {
19
+ var e = n[a("asyncIterator")], y = !1, d, r = {};
20
+ return e == null ? (e = n[a("iterator")](), d = (i) => r[i] = (t) => e[i](t)) : (e = e.call(n), d = (i) => r[i] = (t) => {
21
+ if (y) {
22
+ if (y = !1, i === "throw")
23
23
  throw t;
24
24
  return t;
25
25
  }
26
- return l = !0, {
26
+ return y = !0, {
27
27
  done: !1,
28
- value: new a(new Promise((f) => {
29
- var r = e[i](t);
30
- if (!(r instanceof Object))
28
+ value: new x(new Promise((f) => {
29
+ var o = e[i](t);
30
+ if (!(o instanceof Object))
31
31
  throw TypeError("Object expected");
32
- f(r);
32
+ f(o);
33
33
  }), 1)
34
34
  };
35
- }), o[p("iterator")] = () => o, d("next"), "throw" in e ? d("throw") : o.throw = (i) => {
35
+ }), r[a("iterator")] = () => r, d("next"), "throw" in e ? d("throw") : r.throw = (i) => {
36
36
  throw i;
37
- }, "return" in e && d("return"), o;
37
+ }, "return" in e && d("return"), r;
38
38
  };
39
- function k(n, e = n) {
39
+ function O(n, e) {
40
+ return e === n || e.startsWith(`${n}/`);
41
+ }
42
+ function g(n, e = n) {
40
43
  return new DOMException(e, n);
41
44
  }
42
- function A(n) {
43
- return s(this, null, function* () {
44
- typeof n == "string" ? yield new TextEncoder().encode(n) : n instanceof Uint8Array ? yield n : yield* w(n);
45
+ function k(n) {
46
+ return E(this, null, function* () {
47
+ typeof n == "string" ? yield new TextEncoder().encode(n) : n instanceof Uint8Array ? yield n : yield* h(n);
45
48
  });
46
49
  }
47
50
  export {
48
- k as domError,
49
- A as toChunks
51
+ g as domError,
52
+ O as isSameOrInside,
53
+ k as toChunks
50
54
  };
@@ -1,4 +1,4 @@
1
- const s = ["list_directory", "read_file", "stat_file", "search_files", "search_text", "apply_patch", "write_file", "upload_file", "download_file"], _ = "buildin_tools", t = {
1
+ const s = ["list_directory", "read_file", "stat_file", "search_files", "search_text", "apply_patch", "write_file", "upload_file", "download_file"], _ = "builtin_tools", t = {
2
2
  dev: _,
3
3
  test: _,
4
4
  prod: _,
@@ -1,50 +1,53 @@
1
1
  import { HostBridgeAdapter as f } from "../adapters/HostBridgeAdapter.mjs";
2
- import { getHostWebView as m, HOST_CHANNEL as g, parseHostMessage as y } from "./protocol.mjs";
3
- let t;
4
- function c(s) {
5
- t == null || t(), t = void 0;
6
- const i = m();
7
- if (!i)
2
+ import { getHostWebView as g, HOST_CHANNEL as l, parseHostMessage as y } from "./protocol.mjs";
3
+ function m(o) {
4
+ if (!o || typeof o != "object")
5
+ return;
6
+ const e = o;
7
+ return {
8
+ state: e.state,
9
+ url: typeof e.url == "string" ? e.url : void 0,
10
+ token: typeof e.token == "string" ? e.token : void 0,
11
+ protocol: typeof e.protocol == "number" ? e.protocol : void 0,
12
+ exitCode: typeof e.exitCode == "number" ? e.exitCode : void 0,
13
+ detail: typeof e.detail == "string" ? e.detail : void 0,
14
+ message: typeof e.message == "string" ? e.message : void 0,
15
+ reason: typeof e.reason == "string" ? e.reason : void 0
16
+ };
17
+ }
18
+ let s;
19
+ function v(o) {
20
+ s == null || s(), s = void 0;
21
+ const e = g();
22
+ if (!e)
8
23
  return () => {
9
24
  };
10
- const n = new f(i), d = (p) => {
11
- const e = y(p.data);
12
- if (e) {
13
- if (e.type === "workspace") {
14
- const r = e.granted === !0, o = typeof e.displayName == "string" ? e.displayName : "";
15
- r ? (n.displayName = o || "工程目录", (s.status !== "granted" || s.displayName !== n.displayName) && s.attachAdapter(n, n.displayName)) : s.status === "granted" && s.clear();
25
+ const i = new f(e), d = (p) => {
26
+ const t = y(p.data);
27
+ if (t) {
28
+ if (t.type === "workspace") {
29
+ const n = t.granted === !0, r = typeof t.displayName == "string" ? t.displayName : "";
30
+ i.setCapabilities(t.capabilities, {
31
+ version: t.sandboxVersion
32
+ }), i.applySandbox(m(t.sandbox)), n ? (i.displayName = r || "工程目录", (o.status !== "granted" || o.displayName !== i.displayName) && o.attachAdapter(i, i.displayName)) : o.status === "granted" && o.clear();
16
33
  return;
17
34
  }
18
- if (e.type === "fs-changed") {
19
- const r = Array.isArray(e.paths) ? e.paths.filter((o) => typeof o == "string") : [];
20
- s.notifyFilesChanged(r);
35
+ if (t.type === "fs-changed") {
36
+ const n = Array.isArray(t.paths) ? t.paths.filter((r) => typeof r == "string") : [];
37
+ o.notifyFilesChanged(n);
21
38
  return;
22
39
  }
23
- typeof e.id == "string" && n.settle({
24
- channel: g,
25
- id: e.id,
26
- ok: e.ok !== !1,
27
- name: typeof e.name == "string" ? e.name : void 0,
28
- entries: e.entries,
29
- entry: e.entry,
30
- contentBase64: typeof e.contentBase64 == "string" ? e.contentBase64 : void 0,
31
- exitCode: typeof e.exitCode == "number" ? e.exitCode : void 0,
32
- timedOut: e.timedOut === !0,
33
- stdout: typeof e.stdout == "string" ? e.stdout : void 0,
34
- stderr: typeof e.stderr == "string" ? e.stderr : void 0,
35
- message: typeof e.message == "string" ? e.message : void 0
36
- });
37
40
  }
38
41
  };
39
- i.addEventListener("message", d), i.postMessage({
40
- channel: g,
42
+ e.addEventListener("message", d), e.postMessage({
43
+ channel: l,
41
44
  type: "hello"
42
45
  });
43
46
  const a = () => {
44
- i.removeEventListener("message", d), t === a && (t = void 0);
47
+ e.removeEventListener("message", d), s === a && (s = void 0);
45
48
  };
46
- return t = a, a;
49
+ return s = a, a;
47
50
  }
48
51
  export {
49
- c as connectHostBridge
52
+ v as connectHostBridge
50
53
  };
@@ -1,5 +1,5 @@
1
- const n = "glodon-workspace";
2
- function i(t) {
1
+ const n = "glodon-workspace", i = 2;
2
+ function s(t) {
3
3
  let e = t;
4
4
  if (typeof e == "string")
5
5
  try {
@@ -13,7 +13,7 @@ function i(t) {
13
13
  if (o.channel === n)
14
14
  return o;
15
15
  }
16
- function s() {
16
+ function c() {
17
17
  var e;
18
18
  const t = (e = globalThis.chrome) == null ? void 0 : e.webview;
19
19
  if (!(!t || typeof t.postMessage != "function" || typeof t.addEventListener != "function"))
@@ -21,6 +21,7 @@ function s() {
21
21
  }
22
22
  export {
23
23
  n as HOST_CHANNEL,
24
- s as getHostWebView,
25
- i as parseHostMessage
24
+ i as SANDBOX_PROTOCOL,
25
+ c as getHostWebView,
26
+ s as parseHostMessage
26
27
  };
@@ -0,0 +1,324 @@
1
+ var N = Object.defineProperty;
2
+ var b = Object.getOwnPropertySymbols;
3
+ var C = Object.prototype.hasOwnProperty, M = Object.prototype.propertyIsEnumerable;
4
+ var A = (i, t, e) => t in i ? N(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e, S = (i, t) => {
5
+ for (var e in t || (t = {}))
6
+ C.call(t, e) && A(i, e, t[e]);
7
+ if (b)
8
+ for (var e of b(t))
9
+ M.call(t, e) && A(i, e, t[e]);
10
+ return i;
11
+ };
12
+ var m = (i, t, e) => (A(i, typeof t != "symbol" ? t + "" : t, e), e);
13
+ var g = (i, t, e) => new Promise((r, s) => {
14
+ var o = (a) => {
15
+ try {
16
+ c(e.next(a));
17
+ } catch (h) {
18
+ s(h);
19
+ }
20
+ }, n = (a) => {
21
+ try {
22
+ c(e.throw(a));
23
+ } catch (h) {
24
+ s(h);
25
+ }
26
+ }, c = (a) => a.done ? r(a.value) : Promise.resolve(a.value).then(o, n);
27
+ c((e = e.apply(i, t)).next());
28
+ });
29
+ import { MAX_COMMAND_OUTPUT_CHARS as w } from "../../builtinTools/command.mjs";
30
+ import { domError as d } from "../adapters/WorkspaceFileAdapter.mjs";
31
+ import { SANDBOX_PROTOCOL as k } from "./protocol.mjs";
32
+ import { decodeBinaryFrame as v, MAX_BINARY_PAYLOAD as O, encodeBinaryFrame as P } from "./sandboxFrames.mjs";
33
+ const y = "沙箱已退出。";
34
+ function E(i) {
35
+ if (i instanceof Uint8Array)
36
+ return i;
37
+ if (i instanceof ArrayBuffer)
38
+ return new Uint8Array(i);
39
+ }
40
+ function x(i, t, e, r) {
41
+ const s = i.length > w ? i.slice(i.length - w) : i;
42
+ if (!t && !e && !r && i.length <= w)
43
+ return s;
44
+ let o = `
45
+ …输出已截断`;
46
+ return e && (o += `,完整输出见 ${e}`), r && (o += ",日志已截断"), s + o;
47
+ }
48
+ class D {
49
+ constructor(t = (e) => new WebSocket(e)) {
50
+ m(this, "notice");
51
+ m(this, "socket");
52
+ m(this, "generation", 0);
53
+ m(this, "authed", !1);
54
+ m(this, "seq", 0);
55
+ m(this, "calls", /* @__PURE__ */ new Map());
56
+ m(this, "outgoing", []);
57
+ this.openSocket = t;
58
+ }
59
+ get ready() {
60
+ const t = this.notice;
61
+ return (t == null ? void 0 : t.state) === "ready" && t.protocol === k && !!(t.url && t.token);
62
+ }
63
+ unavailableMessage() {
64
+ var t, e, r, s;
65
+ return ((t = this.notice) == null ? void 0 : t.state) === "starting" ? "沙箱正在启动。" : (e = this.notice) != null && e.message ? this.notice.message : ((r = this.notice) == null ? void 0 : r.state) === "exited" || ((s = this.notice) == null ? void 0 : s.state) === "failed" ? y : "沙箱没有就绪。";
66
+ }
67
+ apply(t) {
68
+ const e = this.notice;
69
+ if (this.notice = t, !this.ready || !(t != null && t.url) || !t.token) {
70
+ this.failAll(d("NotAllowedError", this.unavailableMessage())), this.stop();
71
+ return;
72
+ }
73
+ (e == null ? void 0 : e.url) === t.url && e.token === t.token && this.socket && this.socket.readyState <= 1 || (this.failAll(d("AbortError", y)), this.stop(), this.connect(t.url, t.token));
74
+ }
75
+ stat(t) {
76
+ return g(this, null, function* () {
77
+ return this.invoke({
78
+ op: "stat",
79
+ path: t
80
+ });
81
+ });
82
+ }
83
+ list(t, e) {
84
+ return g(this, null, function* () {
85
+ return this.invoke({
86
+ op: "list",
87
+ path: t
88
+ }, e);
89
+ });
90
+ }
91
+ read(o) {
92
+ return g(this, arguments, function* (t, e = 0, r = 50 * 1024 * 1024, s) {
93
+ const n = this.begin({
94
+ op: "read",
95
+ path: t,
96
+ offset: e,
97
+ maxBytes: r
98
+ }, s), c = [];
99
+ let a;
100
+ for (; !a; ) {
101
+ const u = yield this.next(n), p = E(u);
102
+ if (p) {
103
+ c.push(v(p).payload);
104
+ continue;
105
+ }
106
+ a = u;
107
+ }
108
+ if (this.calls.delete(n), a.ok === !1)
109
+ throw d(String(a.name || "NotAllowedError"), String(a.message || "读取失败。"));
110
+ const h = c.reduce((u, p) => u + p.length, 0), l = new Uint8Array(h);
111
+ let f = 0;
112
+ return c.forEach((u) => {
113
+ l.set(u, f), f += u.length;
114
+ }), l;
115
+ });
116
+ }
117
+ write(t, e, r, s) {
118
+ return g(this, null, function* () {
119
+ const o = this.begin({
120
+ op: "write",
121
+ path: t,
122
+ size: e.length,
123
+ createDirectories: r
124
+ }, s);
125
+ let n = 0;
126
+ for (; n < e.length; ) {
127
+ const a = e.subarray(n, n + O), h = n + a.length;
128
+ this.send(P(o, n, h === e.length, a)), n = h;
129
+ const f = yield this.next(o);
130
+ if (f.ok === !1)
131
+ throw this.calls.delete(o), d(String(f.name || "NotAllowedError"), String(f.message || "写入失败。"));
132
+ if (f.op !== "ack" && f.ok === !0) {
133
+ this.calls.delete(o);
134
+ return;
135
+ }
136
+ }
137
+ const c = yield this.next(o);
138
+ if (this.calls.delete(o), c.ok === !1)
139
+ throw d(String(c.name || "NotAllowedError"), String(c.message || "写入失败。"));
140
+ });
141
+ }
142
+ mkdir(t) {
143
+ return g(this, null, function* () {
144
+ yield this.invoke({
145
+ op: "mkdir",
146
+ path: t
147
+ });
148
+ });
149
+ }
150
+ remove(t, e) {
151
+ return g(this, null, function* () {
152
+ yield this.invoke({
153
+ op: "remove",
154
+ path: t,
155
+ recursive: e
156
+ });
157
+ });
158
+ }
159
+ move(t, e) {
160
+ return g(this, null, function* () {
161
+ yield this.invoke({
162
+ op: "move",
163
+ from: t,
164
+ to: e
165
+ });
166
+ });
167
+ }
168
+ exec(t, e, r) {
169
+ return g(this, null, function* () {
170
+ var h;
171
+ const s = this.begin(S({}, t), e, () => {
172
+ this.send({
173
+ protocol: k,
174
+ id: s,
175
+ op: "cancel"
176
+ });
177
+ });
178
+ let o = "", n = "", c = !1, a = !1;
179
+ for (; this.calls.has(s); ) {
180
+ const l = yield this.next(s);
181
+ if (l.op === "stdout" || l.op === "stderr") {
182
+ const p = String((h = l.text) != null ? h : "");
183
+ r == null || r(l.op, p), l.op === "stdout" ? (o += p, o.length > w && (c = !0), o = o.slice(-w)) : (n += p, n.length > w && (a = !0), n = n.slice(-w));
184
+ continue;
185
+ }
186
+ if (this.calls.delete(s), l.ok === !1)
187
+ throw d(String(l.name || "NotAllowedError"), String(l.message || "命令失败。"));
188
+ const f = typeof l.logPath == "string" ? l.logPath : void 0, u = l.logTruncated === !0;
189
+ return {
190
+ exitCode: typeof l.exitCode == "number" ? l.exitCode : 1,
191
+ timedOut: l.timedOut === !0,
192
+ stdout: x(o, c, f, u),
193
+ stderr: x(n, a, f, u),
194
+ cwd: typeof l.cwd == "string" ? l.cwd : void 0
195
+ };
196
+ }
197
+ throw d("AbortError", y);
198
+ });
199
+ }
200
+ invoke(t, e) {
201
+ const r = this.begin(t, e);
202
+ return this.next(r).then((s) => {
203
+ this.calls.delete(r);
204
+ const o = s;
205
+ if (o.ok === !1)
206
+ throw d(String(o.name || "NotAllowedError"), String(o.message || "请求失败。"));
207
+ return o;
208
+ });
209
+ }
210
+ begin(t, e, r) {
211
+ if (!this.ready)
212
+ throw d("NotAllowedError", this.unavailableMessage());
213
+ if (e != null && e.aborted)
214
+ throw d("AbortError");
215
+ const s = `sbx-${++this.seq}`;
216
+ let o = () => {
217
+ };
218
+ const n = {
219
+ messages: [],
220
+ waiters: [],
221
+ reject: (a) => o(a)
222
+ };
223
+ this.calls.set(s, n);
224
+ const c = () => {
225
+ r == null || r(), n.reject(d("AbortError"));
226
+ };
227
+ return e == null || e.addEventListener("abort", c, {
228
+ once: !0
229
+ }), o = (a) => {
230
+ e == null || e.removeEventListener("abort", c), this.calls.delete(s);
231
+ const h = n.waiters.shift();
232
+ h && h(Promise.reject(a));
233
+ }, this.send(S({
234
+ protocol: k,
235
+ id: s
236
+ }, t)), s;
237
+ }
238
+ next(t) {
239
+ const e = this.calls.get(t);
240
+ if (!e)
241
+ return Promise.reject(d("AbortError", y));
242
+ const r = e.messages.shift();
243
+ return r !== void 0 ? Promise.resolve(r) : new Promise((s, o) => {
244
+ e.waiters.push(s), e.reject = (n) => {
245
+ this.calls.delete(t), o(n);
246
+ };
247
+ });
248
+ }
249
+ send(t) {
250
+ var r;
251
+ const e = () => {
252
+ var o;
253
+ const s = t instanceof Uint8Array ? t : JSON.stringify(t);
254
+ (o = this.socket) == null || o.send(s);
255
+ };
256
+ this.authed && ((r = this.socket) == null ? void 0 : r.readyState) === 1 ? e() : this.outgoing.push(e);
257
+ }
258
+ connect(t, e) {
259
+ const r = ++this.generation, s = this.openSocket(t);
260
+ this.socket = s, this.authed = !1, s.binaryType = "arraybuffer", s.onopen = () => {
261
+ r === this.generation && s.send(JSON.stringify({
262
+ protocol: k,
263
+ op: "auth",
264
+ token: e
265
+ }));
266
+ }, s.onmessage = (o) => {
267
+ r === this.generation && this.onFrame(o.data);
268
+ }, s.onclose = () => {
269
+ r === this.generation && (this.authed = !1, this.failAll(d("AbortError", y)));
270
+ }, s.onerror = () => {
271
+ };
272
+ }
273
+ onFrame(t) {
274
+ const e = E(t);
275
+ if (e) {
276
+ const s = v(e);
277
+ this.push(s.id, e);
278
+ return;
279
+ }
280
+ let r;
281
+ try {
282
+ r = JSON.parse(String(t));
283
+ } catch (s) {
284
+ return;
285
+ }
286
+ if (r.op === "auth") {
287
+ if (r.ok === !0) {
288
+ this.authed = !0;
289
+ const s = this.outgoing;
290
+ this.outgoing = [], s.forEach((o) => o());
291
+ }
292
+ return;
293
+ }
294
+ if (r.op === "bye") {
295
+ this.failAll(d("AbortError", y)), this.stop();
296
+ return;
297
+ }
298
+ typeof r.id == "string" && this.push(r.id, r);
299
+ }
300
+ push(t, e) {
301
+ const r = this.calls.get(t);
302
+ if (!r)
303
+ return;
304
+ const s = r.waiters.shift();
305
+ s ? s(e) : r.messages.push(e);
306
+ }
307
+ failAll(t) {
308
+ for (const e of this.calls.values())
309
+ e.reject(t);
310
+ this.calls.clear();
311
+ }
312
+ stop() {
313
+ this.generation += 1, this.authed = !1, this.outgoing = [];
314
+ const t = this.socket;
315
+ this.socket = void 0;
316
+ try {
317
+ t == null || t.close();
318
+ } catch (e) {
319
+ }
320
+ }
321
+ }
322
+ export {
323
+ D as SandboxClient
324
+ };
@@ -0,0 +1,21 @@
1
+ const g = 1048576;
2
+ function f(e, o, c, n) {
3
+ const s = new TextEncoder().encode(e), t = new Uint8Array(4 + s.length + 8), i = new DataView(t.buffer);
4
+ i.setUint8(0, 1), i.setUint8(1, c ? 1 : 0), i.setUint16(2, s.length), t.set(s, 4), i.setBigUint64(4 + s.length, BigInt(o));
5
+ const r = new Uint8Array(t.length + n.length);
6
+ return r.set(t, 0), r.set(n, t.length), r;
7
+ }
8
+ function h(e) {
9
+ const o = new DataView(e.buffer, e.byteOffset, e.byteLength), n = 4 + o.getUint16(2), s = new TextDecoder().decode(e.subarray(4, n)), t = Number(o.getBigUint64(n)), i = (o.getUint8(1) & 1) === 1;
10
+ return {
11
+ id: s,
12
+ offset: t,
13
+ eof: i,
14
+ payload: e.subarray(n + 8)
15
+ };
16
+ }
17
+ export {
18
+ g as MAX_BINARY_PAYLOAD,
19
+ h as decodeBinaryFrame,
20
+ f as encodeBinaryFrame
21
+ };