@glodon-aiot/bot-client-sdk 3.26.0-alpha.5 → 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.
@@ -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
+ };
@@ -1,4 +1,4 @@
1
- const e = "@glodon-aiot/bot-client-sdk", s = "3.26.0-alpha.5", t = "glodoncv", i = "aiot bot client js sdk", n = "module", g = !1, o = [
1
+ const e = "@glodon-aiot/bot-client-sdk", s = "3.26.0-alpha.9", t = "glodoncv", i = "aiot bot client js sdk", n = "module", g = !1, o = [
2
2
  "aiot",
3
3
  "bot",
4
4
  "bot client",
@@ -68,7 +68,7 @@ const e = "@glodon-aiot/bot-client-sdk", s = "3.26.0-alpha.5", t = "glodoncv", i
68
68
  access: "public",
69
69
  registry: "https://registry.npmjs.org/"
70
70
  }, y = {
71
- "@glodon-aiot/apis": "^3.26.0-alpha.5",
71
+ "@glodon-aiot/apis": "^3.26.0-alpha.9",
72
72
  axios: "0.24.0",
73
73
  buffer: "^6.0.3",
74
74
  dayjs: "^1.11.13",