@phpsandbox/sdk 0.0.2
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 +718 -0
- package/dist/auth.d.ts +14 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +12 -0
- package/dist/auth.js.map +1 -0
- package/dist/beacon/index.d.ts +169 -0
- package/dist/beacon/index.d.ts.map +1 -0
- package/dist/beacon/index.js +537 -0
- package/dist/beacon/index.js.map +1 -0
- package/dist/beacon/navigator.d.ts +141 -0
- package/dist/beacon/navigator.d.ts.map +1 -0
- package/dist/beacon/navigator.js +246 -0
- package/dist/beacon/navigator.js.map +1 -0
- package/dist/beacon/types.d.ts +230 -0
- package/dist/beacon/types.d.ts.map +1 -0
- package/dist/beacon/types.js +2 -0
- package/dist/beacon/types.js.map +1 -0
- package/dist/browser/phpsandbox-sdk.esm.js +4755 -0
- package/dist/browser/phpsandbox-sdk.esm.js.map +7 -0
- package/dist/browser/phpsandbox-sdk.esm.min.js +27 -0
- package/dist/browser/phpsandbox-sdk.esm.min.js.map +7 -0
- package/dist/browser/phpsandbox-sdk.iife.js +4766 -0
- package/dist/browser/phpsandbox-sdk.iife.js.map +7 -0
- package/dist/browser/phpsandbox-sdk.iife.min.js +27 -0
- package/dist/browser/phpsandbox-sdk.iife.min.js.map +7 -0
- package/dist/composer.d.ts +45 -0
- package/dist/composer.d.ts.map +1 -0
- package/dist/composer.js +30 -0
- package/dist/composer.js.map +1 -0
- package/dist/container.d.ts +66 -0
- package/dist/container.d.ts.map +1 -0
- package/dist/container.js +56 -0
- package/dist/container.js.map +1 -0
- package/dist/events/index.d.ts +23 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +46 -0
- package/dist/events/index.js.map +1 -0
- package/dist/filesystem.d.ts +483 -0
- package/dist/filesystem.d.ts.map +1 -0
- package/dist/filesystem.js +244 -0
- package/dist/filesystem.js.map +1 -0
- package/dist/git.d.ts +42 -0
- package/dist/git.d.ts.map +1 -0
- package/dist/git.js +18 -0
- package/dist/git.js.map +1 -0
- package/dist/index.d.ts +167 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +265 -0
- package/dist/index.js.map +1 -0
- package/dist/laravel.d.ts +23 -0
- package/dist/laravel.d.ts.map +1 -0
- package/dist/laravel.js +12 -0
- package/dist/laravel.js.map +1 -0
- package/dist/log.d.ts +13 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +12 -0
- package/dist/log.js.map +1 -0
- package/dist/lsp.d.ts +57 -0
- package/dist/lsp.d.ts.map +1 -0
- package/dist/lsp.js +69 -0
- package/dist/lsp.js.map +1 -0
- package/dist/repl.d.ts +41 -0
- package/dist/repl.d.ts.map +1 -0
- package/dist/repl.js +27 -0
- package/dist/repl.js.map +1 -0
- package/dist/shell.d.ts +29 -0
- package/dist/shell.d.ts.map +1 -0
- package/dist/shell.js +29 -0
- package/dist/shell.js.map +1 -0
- package/dist/socket/index.d.ts +229 -0
- package/dist/socket/index.d.ts.map +1 -0
- package/dist/socket/index.js +825 -0
- package/dist/socket/index.js.map +1 -0
- package/dist/terminal.d.ts +97 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +87 -0
- package/dist/terminal.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/disposable.d.ts +7 -0
- package/dist/utils/disposable.d.ts.map +1 -0
- package/dist/utils/disposable.js +20 -0
- package/dist/utils/disposable.js.map +1 -0
- package/dist/utils/promise.d.ts +13 -0
- package/dist/utils/promise.d.ts.map +1 -0
- package/dist/utils/promise.js +21 -0
- package/dist/utils/promise.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,4755 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __typeError = (msg) => {
|
|
8
|
+
throw TypeError(msg);
|
|
9
|
+
};
|
|
10
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
11
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
30
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
31
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
32
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
33
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
34
|
+
|
|
35
|
+
// node_modules/retry/lib/retry_operation.js
|
|
36
|
+
var require_retry_operation = __commonJS({
|
|
37
|
+
"node_modules/retry/lib/retry_operation.js"(exports, module) {
|
|
38
|
+
function RetryOperation(timeouts, options) {
|
|
39
|
+
if (typeof options === "boolean") {
|
|
40
|
+
options = { forever: options };
|
|
41
|
+
}
|
|
42
|
+
this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
|
|
43
|
+
this._timeouts = timeouts;
|
|
44
|
+
this._options = options || {};
|
|
45
|
+
this._maxRetryTime = options && options.maxRetryTime || Infinity;
|
|
46
|
+
this._fn = null;
|
|
47
|
+
this._errors = [];
|
|
48
|
+
this._attempts = 1;
|
|
49
|
+
this._operationTimeout = null;
|
|
50
|
+
this._operationTimeoutCb = null;
|
|
51
|
+
this._timeout = null;
|
|
52
|
+
this._operationStart = null;
|
|
53
|
+
this._timer = null;
|
|
54
|
+
if (this._options.forever) {
|
|
55
|
+
this._cachedTimeouts = this._timeouts.slice(0);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
module.exports = RetryOperation;
|
|
59
|
+
RetryOperation.prototype.reset = function() {
|
|
60
|
+
this._attempts = 1;
|
|
61
|
+
this._timeouts = this._originalTimeouts.slice(0);
|
|
62
|
+
};
|
|
63
|
+
RetryOperation.prototype.stop = function() {
|
|
64
|
+
if (this._timeout) {
|
|
65
|
+
clearTimeout(this._timeout);
|
|
66
|
+
}
|
|
67
|
+
if (this._timer) {
|
|
68
|
+
clearTimeout(this._timer);
|
|
69
|
+
}
|
|
70
|
+
this._timeouts = [];
|
|
71
|
+
this._cachedTimeouts = null;
|
|
72
|
+
};
|
|
73
|
+
RetryOperation.prototype.retry = function(err) {
|
|
74
|
+
if (this._timeout) {
|
|
75
|
+
clearTimeout(this._timeout);
|
|
76
|
+
}
|
|
77
|
+
if (!err) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
var currentTime = (/* @__PURE__ */ new Date()).getTime();
|
|
81
|
+
if (err && currentTime - this._operationStart >= this._maxRetryTime) {
|
|
82
|
+
this._errors.push(err);
|
|
83
|
+
this._errors.unshift(new Error("RetryOperation timeout occurred"));
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
this._errors.push(err);
|
|
87
|
+
var timeout2 = this._timeouts.shift();
|
|
88
|
+
if (timeout2 === void 0) {
|
|
89
|
+
if (this._cachedTimeouts) {
|
|
90
|
+
this._errors.splice(0, this._errors.length - 1);
|
|
91
|
+
timeout2 = this._cachedTimeouts.slice(-1);
|
|
92
|
+
} else {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
var self2 = this;
|
|
97
|
+
this._timer = setTimeout(function() {
|
|
98
|
+
self2._attempts++;
|
|
99
|
+
if (self2._operationTimeoutCb) {
|
|
100
|
+
self2._timeout = setTimeout(function() {
|
|
101
|
+
self2._operationTimeoutCb(self2._attempts);
|
|
102
|
+
}, self2._operationTimeout);
|
|
103
|
+
if (self2._options.unref) {
|
|
104
|
+
self2._timeout.unref();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
self2._fn(self2._attempts);
|
|
108
|
+
}, timeout2);
|
|
109
|
+
if (this._options.unref) {
|
|
110
|
+
this._timer.unref();
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
};
|
|
114
|
+
RetryOperation.prototype.attempt = function(fn, timeoutOps) {
|
|
115
|
+
this._fn = fn;
|
|
116
|
+
if (timeoutOps) {
|
|
117
|
+
if (timeoutOps.timeout) {
|
|
118
|
+
this._operationTimeout = timeoutOps.timeout;
|
|
119
|
+
}
|
|
120
|
+
if (timeoutOps.cb) {
|
|
121
|
+
this._operationTimeoutCb = timeoutOps.cb;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
var self2 = this;
|
|
125
|
+
if (this._operationTimeoutCb) {
|
|
126
|
+
this._timeout = setTimeout(function() {
|
|
127
|
+
self2._operationTimeoutCb();
|
|
128
|
+
}, self2._operationTimeout);
|
|
129
|
+
}
|
|
130
|
+
this._operationStart = (/* @__PURE__ */ new Date()).getTime();
|
|
131
|
+
this._fn(this._attempts);
|
|
132
|
+
};
|
|
133
|
+
RetryOperation.prototype.try = function(fn) {
|
|
134
|
+
console.log("Using RetryOperation.try() is deprecated");
|
|
135
|
+
this.attempt(fn);
|
|
136
|
+
};
|
|
137
|
+
RetryOperation.prototype.start = function(fn) {
|
|
138
|
+
console.log("Using RetryOperation.start() is deprecated");
|
|
139
|
+
this.attempt(fn);
|
|
140
|
+
};
|
|
141
|
+
RetryOperation.prototype.start = RetryOperation.prototype.try;
|
|
142
|
+
RetryOperation.prototype.errors = function() {
|
|
143
|
+
return this._errors;
|
|
144
|
+
};
|
|
145
|
+
RetryOperation.prototype.attempts = function() {
|
|
146
|
+
return this._attempts;
|
|
147
|
+
};
|
|
148
|
+
RetryOperation.prototype.mainError = function() {
|
|
149
|
+
if (this._errors.length === 0) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
var counts = {};
|
|
153
|
+
var mainError = null;
|
|
154
|
+
var mainErrorCount = 0;
|
|
155
|
+
for (var i = 0; i < this._errors.length; i++) {
|
|
156
|
+
var error = this._errors[i];
|
|
157
|
+
var message = error.message;
|
|
158
|
+
var count = (counts[message] || 0) + 1;
|
|
159
|
+
counts[message] = count;
|
|
160
|
+
if (count >= mainErrorCount) {
|
|
161
|
+
mainError = error;
|
|
162
|
+
mainErrorCount = count;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return mainError;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// node_modules/retry/lib/retry.js
|
|
171
|
+
var require_retry = __commonJS({
|
|
172
|
+
"node_modules/retry/lib/retry.js"(exports) {
|
|
173
|
+
var RetryOperation = require_retry_operation();
|
|
174
|
+
exports.operation = function(options) {
|
|
175
|
+
var timeouts = exports.timeouts(options);
|
|
176
|
+
return new RetryOperation(timeouts, {
|
|
177
|
+
forever: options && (options.forever || options.retries === Infinity),
|
|
178
|
+
unref: options && options.unref,
|
|
179
|
+
maxRetryTime: options && options.maxRetryTime
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
exports.timeouts = function(options) {
|
|
183
|
+
if (options instanceof Array) {
|
|
184
|
+
return [].concat(options);
|
|
185
|
+
}
|
|
186
|
+
var opts = {
|
|
187
|
+
retries: 10,
|
|
188
|
+
factor: 2,
|
|
189
|
+
minTimeout: 1 * 1e3,
|
|
190
|
+
maxTimeout: Infinity,
|
|
191
|
+
randomize: false
|
|
192
|
+
};
|
|
193
|
+
for (var key in options) {
|
|
194
|
+
opts[key] = options[key];
|
|
195
|
+
}
|
|
196
|
+
if (opts.minTimeout > opts.maxTimeout) {
|
|
197
|
+
throw new Error("minTimeout is greater than maxTimeout");
|
|
198
|
+
}
|
|
199
|
+
var timeouts = [];
|
|
200
|
+
for (var i = 0; i < opts.retries; i++) {
|
|
201
|
+
timeouts.push(this.createTimeout(i, opts));
|
|
202
|
+
}
|
|
203
|
+
if (options && options.forever && !timeouts.length) {
|
|
204
|
+
timeouts.push(this.createTimeout(i, opts));
|
|
205
|
+
}
|
|
206
|
+
timeouts.sort(function(a, b) {
|
|
207
|
+
return a - b;
|
|
208
|
+
});
|
|
209
|
+
return timeouts;
|
|
210
|
+
};
|
|
211
|
+
exports.createTimeout = function(attempt, opts) {
|
|
212
|
+
var random = opts.randomize ? Math.random() + 1 : 1;
|
|
213
|
+
var timeout2 = Math.round(random * Math.max(opts.minTimeout, 1) * Math.pow(opts.factor, attempt));
|
|
214
|
+
timeout2 = Math.min(timeout2, opts.maxTimeout);
|
|
215
|
+
return timeout2;
|
|
216
|
+
};
|
|
217
|
+
exports.wrap = function(obj, options, methods) {
|
|
218
|
+
if (options instanceof Array) {
|
|
219
|
+
methods = options;
|
|
220
|
+
options = null;
|
|
221
|
+
}
|
|
222
|
+
if (!methods) {
|
|
223
|
+
methods = [];
|
|
224
|
+
for (var key in obj) {
|
|
225
|
+
if (typeof obj[key] === "function") {
|
|
226
|
+
methods.push(key);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
for (var i = 0; i < methods.length; i++) {
|
|
231
|
+
var method = methods[i];
|
|
232
|
+
var original = obj[method];
|
|
233
|
+
obj[method] = function retryWrapper(original2) {
|
|
234
|
+
var op = exports.operation(options);
|
|
235
|
+
var args = Array.prototype.slice.call(arguments, 1);
|
|
236
|
+
var callback = args.pop();
|
|
237
|
+
args.push(function(err) {
|
|
238
|
+
if (op.retry(err)) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (err) {
|
|
242
|
+
arguments[0] = op.mainError();
|
|
243
|
+
}
|
|
244
|
+
callback.apply(this, arguments);
|
|
245
|
+
});
|
|
246
|
+
op.attempt(function() {
|
|
247
|
+
original2.apply(obj, args);
|
|
248
|
+
});
|
|
249
|
+
}.bind(obj, original);
|
|
250
|
+
obj[method].options = options;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// node_modules/retry/index.js
|
|
257
|
+
var require_retry2 = __commonJS({
|
|
258
|
+
"node_modules/retry/index.js"(exports, module) {
|
|
259
|
+
module.exports = require_retry();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// node_modules/async-retry/lib/index.js
|
|
264
|
+
var require_lib = __commonJS({
|
|
265
|
+
"node_modules/async-retry/lib/index.js"(exports, module) {
|
|
266
|
+
var retrier = require_retry2();
|
|
267
|
+
function retry3(fn, opts) {
|
|
268
|
+
function run(resolve, reject) {
|
|
269
|
+
var options = opts || {};
|
|
270
|
+
var op;
|
|
271
|
+
if (!("randomize" in options)) {
|
|
272
|
+
options.randomize = true;
|
|
273
|
+
}
|
|
274
|
+
op = retrier.operation(options);
|
|
275
|
+
function bail(err) {
|
|
276
|
+
reject(err || new Error("Aborted"));
|
|
277
|
+
}
|
|
278
|
+
function onError(err, num) {
|
|
279
|
+
if (err.bail) {
|
|
280
|
+
bail(err);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (!op.retry(err)) {
|
|
284
|
+
reject(op.mainError());
|
|
285
|
+
} else if (options.onRetry) {
|
|
286
|
+
options.onRetry(err, num);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function runAttempt(num) {
|
|
290
|
+
var val;
|
|
291
|
+
try {
|
|
292
|
+
val = fn(bail, num);
|
|
293
|
+
} catch (err) {
|
|
294
|
+
onError(err, num);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
Promise.resolve(val).then(resolve).catch(function catchIt(err) {
|
|
298
|
+
onError(err, num);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
op.attempt(runAttempt);
|
|
302
|
+
}
|
|
303
|
+
return new Promise(run);
|
|
304
|
+
}
|
|
305
|
+
module.exports = retry3;
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// src/types.ts
|
|
310
|
+
var ErrorEvent = class extends Error {
|
|
311
|
+
constructor(code, message, raw = {}) {
|
|
312
|
+
super(message);
|
|
313
|
+
this.code = code;
|
|
314
|
+
this.message = message;
|
|
315
|
+
this.raw = raw;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
var RateLimitError = class extends ErrorEvent {
|
|
319
|
+
constructor(message, raw = {}) {
|
|
320
|
+
super(429, message, raw);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
var PHPSandboxError = class extends ErrorEvent {
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// node_modules/nanoid/url-alphabet/index.js
|
|
327
|
+
var urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
328
|
+
|
|
329
|
+
// node_modules/nanoid/index.browser.js
|
|
330
|
+
var nanoid = (size = 21) => {
|
|
331
|
+
let id = "";
|
|
332
|
+
let bytes = crypto.getRandomValues(new Uint8Array(size |= 0));
|
|
333
|
+
while (size--) {
|
|
334
|
+
id += urlAlphabet[bytes[size] & 63];
|
|
335
|
+
}
|
|
336
|
+
return id;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
// src/filesystem.ts
|
|
340
|
+
var FileType = /* @__PURE__ */ ((FileType2) => {
|
|
341
|
+
FileType2[FileType2["Unknown"] = 0] = "Unknown";
|
|
342
|
+
FileType2[FileType2["File"] = 1] = "File";
|
|
343
|
+
FileType2[FileType2["Directory"] = 2] = "Directory";
|
|
344
|
+
FileType2[FileType2["SymbolicLink"] = 64] = "SymbolicLink";
|
|
345
|
+
return FileType2;
|
|
346
|
+
})(FileType || {});
|
|
347
|
+
var FileChangeType = /* @__PURE__ */ ((FileChangeType2) => {
|
|
348
|
+
FileChangeType2[FileChangeType2["UPDATED"] = 1] = "UPDATED";
|
|
349
|
+
FileChangeType2[FileChangeType2["ADDED"] = 2] = "ADDED";
|
|
350
|
+
FileChangeType2[FileChangeType2["DELETED"] = 3] = "DELETED";
|
|
351
|
+
return FileChangeType2;
|
|
352
|
+
})(FileChangeType || {});
|
|
353
|
+
var FilesystemErrorType = /* @__PURE__ */ ((FilesystemErrorType2) => {
|
|
354
|
+
FilesystemErrorType2["Unavailable"] = "Unavailable";
|
|
355
|
+
FilesystemErrorType2["NoPermissions"] = "NoPermissions";
|
|
356
|
+
FilesystemErrorType2["FileExists"] = "FileExists";
|
|
357
|
+
FilesystemErrorType2["FileNotFound"] = "FileNotFound";
|
|
358
|
+
FilesystemErrorType2["FileIsADirectory"] = "FileIsADirectory";
|
|
359
|
+
FilesystemErrorType2["FileNotADirectory"] = "FileNotADirectory";
|
|
360
|
+
return FilesystemErrorType2;
|
|
361
|
+
})(FilesystemErrorType || {});
|
|
362
|
+
var FilesystemError = class extends ErrorEvent {
|
|
363
|
+
constructor(inner, name) {
|
|
364
|
+
super(inner.code, inner.message);
|
|
365
|
+
this.name = name;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
var FilesystemSubscription = class {
|
|
369
|
+
constructor(path, okra, disposable) {
|
|
370
|
+
this.path = path;
|
|
371
|
+
this.okra = okra;
|
|
372
|
+
this.disposable = disposable;
|
|
373
|
+
}
|
|
374
|
+
dispose() {
|
|
375
|
+
this.okra.invoke("fs.unwatch", { path: this.path });
|
|
376
|
+
this.disposable.dispose();
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
var Filesystem = class {
|
|
380
|
+
constructor(okra) {
|
|
381
|
+
this.okra = okra;
|
|
382
|
+
this.watches = /* @__PURE__ */ new Map();
|
|
383
|
+
this.watchOkraConnection();
|
|
384
|
+
}
|
|
385
|
+
watchOkraConnection() {
|
|
386
|
+
this.okra.onDidConnect(() => {
|
|
387
|
+
for (const { path, options, onDidChange } of this.watches.values()) {
|
|
388
|
+
this.watch(path, options, onDidChange);
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
info(path) {
|
|
393
|
+
return this.okra.invoke("fs.info", { path });
|
|
394
|
+
}
|
|
395
|
+
write(path, contents) {
|
|
396
|
+
return this.okra.invoke("fs.write", { path, contents });
|
|
397
|
+
}
|
|
398
|
+
find(query, options = {
|
|
399
|
+
useIgnoreFiles: false,
|
|
400
|
+
followSymlinks: false,
|
|
401
|
+
useGlobalIgnoreFiles: false,
|
|
402
|
+
useParentIgnoreFiles: false
|
|
403
|
+
}) {
|
|
404
|
+
return this.okra.invoke("fs.find", {
|
|
405
|
+
query,
|
|
406
|
+
options: Object.assign(options, {
|
|
407
|
+
includes: [],
|
|
408
|
+
excludes: [
|
|
409
|
+
"**/storage",
|
|
410
|
+
"**/vendor",
|
|
411
|
+
"**/node_modules",
|
|
412
|
+
"**/.git",
|
|
413
|
+
"**/.svn",
|
|
414
|
+
"**/.hg",
|
|
415
|
+
"**/CVS",
|
|
416
|
+
"**/.DS_Store",
|
|
417
|
+
"**/Thumbs.db",
|
|
418
|
+
"**/*.crswap"
|
|
419
|
+
],
|
|
420
|
+
useIgnoreFiles: false,
|
|
421
|
+
followSymlinks: false,
|
|
422
|
+
useGlobalIgnoreFiles: false,
|
|
423
|
+
useParentIgnoreFiles: false
|
|
424
|
+
})
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
search(query, options, onMatch) {
|
|
428
|
+
if (!query.id) {
|
|
429
|
+
query.id = nanoid();
|
|
430
|
+
}
|
|
431
|
+
const defaultOptions = {
|
|
432
|
+
maxResults: 5,
|
|
433
|
+
afterContext: 2,
|
|
434
|
+
beforeContext: 2,
|
|
435
|
+
includes: [],
|
|
436
|
+
excludes: [
|
|
437
|
+
"**/.git",
|
|
438
|
+
"**/.svn",
|
|
439
|
+
"**/.hg",
|
|
440
|
+
"**/CVS",
|
|
441
|
+
"**/.DS_Store",
|
|
442
|
+
"**/Thumbs.db",
|
|
443
|
+
"**/*.crswap",
|
|
444
|
+
"**/node_modules",
|
|
445
|
+
"**/vendor",
|
|
446
|
+
"**/bower_components",
|
|
447
|
+
"**/*.code-search"
|
|
448
|
+
],
|
|
449
|
+
useIgnoreFiles: true,
|
|
450
|
+
followSymlinks: true,
|
|
451
|
+
useGlobalIgnoreFiles: true,
|
|
452
|
+
useParentIgnoreFiles: true,
|
|
453
|
+
encoding: "utf-8",
|
|
454
|
+
previewOptions: {
|
|
455
|
+
matchLines: 5,
|
|
456
|
+
charsPerLine: 1e3
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
const sid = query.id;
|
|
460
|
+
const localOnMatch = async (result) => {
|
|
461
|
+
if (result === false) {
|
|
462
|
+
disposable.dispose();
|
|
463
|
+
}
|
|
464
|
+
if (onMatch) {
|
|
465
|
+
const ret = await Promise.resolve(onMatch(result));
|
|
466
|
+
if (ret === false) {
|
|
467
|
+
disposable.dispose();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
const disposable = this.okra.listen(`fs.text.search.${sid}`, localOnMatch);
|
|
472
|
+
return this.okra.invoke("fs.textSearch", { query, options: { ...defaultOptions, ...options } }).finally(() => disposable.dispose());
|
|
473
|
+
}
|
|
474
|
+
mkdir(path) {
|
|
475
|
+
return this.okra.invoke("fs.mkdir", { path });
|
|
476
|
+
}
|
|
477
|
+
move(from, to) {
|
|
478
|
+
return this.okra.invoke("fs.move", { from, to });
|
|
479
|
+
}
|
|
480
|
+
remove(path, type) {
|
|
481
|
+
return this.okra.invoke("fs.remove", { path, type });
|
|
482
|
+
}
|
|
483
|
+
async readFile(path, lineRange) {
|
|
484
|
+
try {
|
|
485
|
+
const content = await this.okra.invoke("fs.readFile", { path, lineRange });
|
|
486
|
+
if (content instanceof Uint8Array) {
|
|
487
|
+
return content;
|
|
488
|
+
}
|
|
489
|
+
if (typeof content === "string") {
|
|
490
|
+
return new TextEncoder().encode(content);
|
|
491
|
+
}
|
|
492
|
+
return content;
|
|
493
|
+
} catch (e) {
|
|
494
|
+
this.handleError(e);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
tail(path, lines = 10) {
|
|
498
|
+
return this.okra.invoke("fs.tail", { path, lines }).catch((e) => this.handleError(e));
|
|
499
|
+
}
|
|
500
|
+
writeFile(path, contents, options) {
|
|
501
|
+
return this.okra.invoke("fs.writeFile", {
|
|
502
|
+
path,
|
|
503
|
+
contents,
|
|
504
|
+
options
|
|
505
|
+
}).catch((e) => this.handleError(e));
|
|
506
|
+
}
|
|
507
|
+
stat(path) {
|
|
508
|
+
return this.okra.invoke("fs.stat", { path }).catch((e) => this.handleError(e));
|
|
509
|
+
}
|
|
510
|
+
rename(from, to, options) {
|
|
511
|
+
return this.okra.invoke("fs.rename", { from, to, options }).catch((e) => this.handleError(e));
|
|
512
|
+
}
|
|
513
|
+
delete(path, options) {
|
|
514
|
+
return this.okra.invoke("fs.delete", { path, options }).catch((e) => this.handleError(e));
|
|
515
|
+
}
|
|
516
|
+
copy(source, destination, options) {
|
|
517
|
+
return this.okra.invoke("fs.copy", { source, destination, options }).catch((e) => this.handleError(e));
|
|
518
|
+
}
|
|
519
|
+
readDirectory(path, include = [], exclude = []) {
|
|
520
|
+
return this.okra.invoke("fs.readDirectory", { path, include, exclude }).catch((e) => this.handleError(e));
|
|
521
|
+
}
|
|
522
|
+
createDirectory(path) {
|
|
523
|
+
return this.okra.invoke("fs.createDirectory", { path }).catch((e) => this.handleError(e));
|
|
524
|
+
}
|
|
525
|
+
watch(path, options, onDidChange) {
|
|
526
|
+
const disposable = this.okra.listen(`fs.watch.${path}`, onDidChange);
|
|
527
|
+
const wrappedDisposable = {
|
|
528
|
+
dispose: () => {
|
|
529
|
+
disposable.dispose();
|
|
530
|
+
this.okra.invoke("fs.unwatch", { path });
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const subscription = new FilesystemSubscription(path, this.okra, wrappedDisposable);
|
|
534
|
+
this.watches.set(path, { options, path, onDidChange });
|
|
535
|
+
this.okra.invoke("fs.watch", { path, options });
|
|
536
|
+
return subscription;
|
|
537
|
+
}
|
|
538
|
+
exists(path) {
|
|
539
|
+
return this.stat(path).then(() => true).catch(() => false);
|
|
540
|
+
}
|
|
541
|
+
async download(chunk, exclude, include) {
|
|
542
|
+
const id = nanoid();
|
|
543
|
+
const stream = !!chunk;
|
|
544
|
+
const chunks = [];
|
|
545
|
+
this.okra.listen(
|
|
546
|
+
`fs.download.${id}`,
|
|
547
|
+
stream ? chunk : (data) => {
|
|
548
|
+
chunks.push(data);
|
|
549
|
+
}
|
|
550
|
+
);
|
|
551
|
+
return this.okra.invoke("fs.download", { id, exclude, include }).then(() => new Blob(chunks, { type: "application/octet-stream" }));
|
|
552
|
+
}
|
|
553
|
+
tree(path = "/") {
|
|
554
|
+
return this.okra.invoke("fs.tree", { path });
|
|
555
|
+
}
|
|
556
|
+
handleError(e) {
|
|
557
|
+
if (e instanceof ErrorEvent && "name" in e.raw) {
|
|
558
|
+
throw new FilesystemError(e, e.raw.name);
|
|
559
|
+
}
|
|
560
|
+
throw e;
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// src/terminal.ts
|
|
565
|
+
var Terminal = class {
|
|
566
|
+
constructor(okra) {
|
|
567
|
+
this.okra = okra;
|
|
568
|
+
}
|
|
569
|
+
list() {
|
|
570
|
+
return this.okra.invoke("terminal.list");
|
|
571
|
+
}
|
|
572
|
+
start() {
|
|
573
|
+
return this.okra.invoke("terminal.start");
|
|
574
|
+
}
|
|
575
|
+
create(input) {
|
|
576
|
+
return this.okra.invoke("terminal.create", input);
|
|
577
|
+
}
|
|
578
|
+
onStarted(handler) {
|
|
579
|
+
this.okra.listen("terminal.started", handler);
|
|
580
|
+
}
|
|
581
|
+
resize(id, size) {
|
|
582
|
+
return this.okra.invoke("terminal.resize", { id, width: size[0], height: size[1] });
|
|
583
|
+
}
|
|
584
|
+
onOutput(id, handler) {
|
|
585
|
+
this.okra.listen(`terminal.output.${id}`, handler);
|
|
586
|
+
}
|
|
587
|
+
input(id, input) {
|
|
588
|
+
return this.okra.invoke("terminal.input", { id, input });
|
|
589
|
+
}
|
|
590
|
+
listen(event, handler) {
|
|
591
|
+
return this.okra.listen(event, handler);
|
|
592
|
+
}
|
|
593
|
+
async spawn(command, args, opts) {
|
|
594
|
+
const id = opts?.id || nanoid();
|
|
595
|
+
const disposables = /* @__PURE__ */ new Set();
|
|
596
|
+
const dispose = () => {
|
|
597
|
+
for (const disposable of disposables) {
|
|
598
|
+
disposable.dispose();
|
|
599
|
+
}
|
|
600
|
+
disposables.clear();
|
|
601
|
+
};
|
|
602
|
+
const input = new WritableStream({
|
|
603
|
+
write: (chunk) => {
|
|
604
|
+
this.input(id, chunk);
|
|
605
|
+
},
|
|
606
|
+
close: dispose
|
|
607
|
+
});
|
|
608
|
+
const output = new ReadableStream({
|
|
609
|
+
start: (controller) => {
|
|
610
|
+
disposables.add(
|
|
611
|
+
this.listen(`terminal.output.${id}`, (data) => {
|
|
612
|
+
controller.enqueue(data.output);
|
|
613
|
+
})
|
|
614
|
+
);
|
|
615
|
+
},
|
|
616
|
+
cancel: dispose
|
|
617
|
+
});
|
|
618
|
+
const exit = new Promise((resolve) => {
|
|
619
|
+
disposables.add(
|
|
620
|
+
this.listen(`terminal.close.${id}`, (data) => {
|
|
621
|
+
dispose();
|
|
622
|
+
resolve(data.exitCode);
|
|
623
|
+
})
|
|
624
|
+
);
|
|
625
|
+
});
|
|
626
|
+
const kill = () => {
|
|
627
|
+
dispose();
|
|
628
|
+
return this.okra.invoke("terminal.close", { id });
|
|
629
|
+
};
|
|
630
|
+
const resize = (dimensions) => {
|
|
631
|
+
this.okra.invoke("terminal.resize", { id, width: dimensions.cols, height: dimensions.rows });
|
|
632
|
+
};
|
|
633
|
+
const { abortSignal, ...otherOpts } = opts ?? {};
|
|
634
|
+
if (abortSignal) {
|
|
635
|
+
abortSignal.addEventListener("abort", kill);
|
|
636
|
+
}
|
|
637
|
+
const result = await this.okra.invoke(
|
|
638
|
+
"terminal.spawn",
|
|
639
|
+
{
|
|
640
|
+
command: [command, ...args],
|
|
641
|
+
opts: {
|
|
642
|
+
id,
|
|
643
|
+
...otherOpts
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
{ abortSignal }
|
|
647
|
+
);
|
|
648
|
+
return {
|
|
649
|
+
exit,
|
|
650
|
+
input,
|
|
651
|
+
output,
|
|
652
|
+
kill,
|
|
653
|
+
resize,
|
|
654
|
+
...result
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
// src/container.ts
|
|
660
|
+
var NotebookState = /* @__PURE__ */ ((NotebookState2) => {
|
|
661
|
+
NotebookState2["RUNNING"] = "running";
|
|
662
|
+
NotebookState2["STOPPED"] = "stopped";
|
|
663
|
+
NotebookState2["STARTING"] = "starting";
|
|
664
|
+
NotebookState2["STOPPING"] = "stopping";
|
|
665
|
+
NotebookState2["KILLED"] = "killed";
|
|
666
|
+
NotebookState2["ERROR"] = "error";
|
|
667
|
+
NotebookState2["PROVISIONING"] = "provisioning";
|
|
668
|
+
return NotebookState2;
|
|
669
|
+
})(NotebookState || {});
|
|
670
|
+
var Container = class {
|
|
671
|
+
constructor(okra) {
|
|
672
|
+
this.okra = okra;
|
|
673
|
+
}
|
|
674
|
+
start() {
|
|
675
|
+
return this.okra.invoke("container.start");
|
|
676
|
+
}
|
|
677
|
+
stop() {
|
|
678
|
+
return this.okra.invoke("container.stop");
|
|
679
|
+
}
|
|
680
|
+
state() {
|
|
681
|
+
return this.okra.invoke("container.state");
|
|
682
|
+
}
|
|
683
|
+
openedPorts() {
|
|
684
|
+
return this.okra.invoke("container.opened-ports");
|
|
685
|
+
}
|
|
686
|
+
setPhp(version) {
|
|
687
|
+
return this.okra.invoke("container.set-php", { version });
|
|
688
|
+
}
|
|
689
|
+
enableTelemetry(features) {
|
|
690
|
+
return this.okra.invoke("container.stream-telemetry", { features: Array.from(features) });
|
|
691
|
+
}
|
|
692
|
+
stopTelemetry() {
|
|
693
|
+
return this.okra.invoke("container.stop-telemetry");
|
|
694
|
+
}
|
|
695
|
+
listen(event, handler) {
|
|
696
|
+
return this.okra.listen(event, handler);
|
|
697
|
+
}
|
|
698
|
+
onPort(handler) {
|
|
699
|
+
let ports = [];
|
|
700
|
+
return this.listen("container.ports", (newPorts) => {
|
|
701
|
+
for (const port of newPorts) {
|
|
702
|
+
if (!ports.some((p) => p.port === port.port)) {
|
|
703
|
+
handler(port, "open");
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
for (const port of ports) {
|
|
707
|
+
if (!newPorts.some((p) => p.port === port.port)) {
|
|
708
|
+
handler(port, "close");
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
ports = newPorts;
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
// src/auth.ts
|
|
717
|
+
var Auth = class {
|
|
718
|
+
constructor(okra) {
|
|
719
|
+
this.okra = okra;
|
|
720
|
+
}
|
|
721
|
+
logout() {
|
|
722
|
+
return this.okra.invoke("auth.logout");
|
|
723
|
+
}
|
|
724
|
+
login(newConnectionData) {
|
|
725
|
+
return this.okra.invoke("auth.login", { newConnectionData });
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
// src/lsp.ts
|
|
730
|
+
var Lsp = class {
|
|
731
|
+
constructor(okra) {
|
|
732
|
+
this.okra = okra;
|
|
733
|
+
}
|
|
734
|
+
message(id, message) {
|
|
735
|
+
return this.okra.invoke("lsp.message", { id, message });
|
|
736
|
+
}
|
|
737
|
+
listen(id, event, cb) {
|
|
738
|
+
this.okra.listen(`${event}.${id}`, (e) => cb(e));
|
|
739
|
+
}
|
|
740
|
+
onClose(id, cb) {
|
|
741
|
+
this.listen(id, "lsp.closed", (e) => cb(e.code, e.reason));
|
|
742
|
+
}
|
|
743
|
+
onError(id, cb) {
|
|
744
|
+
this.listen(id, "lsp.error", (e) => cb(e.message));
|
|
745
|
+
}
|
|
746
|
+
onClientDisconnect(cb) {
|
|
747
|
+
this.okra.onDidDisconnect(() => cb(1e3, "OKRA DISCONNECTED"));
|
|
748
|
+
}
|
|
749
|
+
onResponse(id, cb) {
|
|
750
|
+
this.listen(id, "lsp.response", (e) => cb(JSON.stringify(e)));
|
|
751
|
+
}
|
|
752
|
+
close(id) {
|
|
753
|
+
return this.okra.invoke("lsp.close", { id });
|
|
754
|
+
}
|
|
755
|
+
start(id) {
|
|
756
|
+
return this.okra.invoke("lsp.start", { id });
|
|
757
|
+
}
|
|
758
|
+
connection(id) {
|
|
759
|
+
return LspConnection.create(id, this);
|
|
760
|
+
}
|
|
761
|
+
whenSocketConnected() {
|
|
762
|
+
return this.okra.whenConnected();
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
var LspConnection = class _LspConnection {
|
|
766
|
+
constructor(id, lsp) {
|
|
767
|
+
this.id = id;
|
|
768
|
+
this.lsp = lsp;
|
|
769
|
+
}
|
|
770
|
+
send(content) {
|
|
771
|
+
return this.lsp.message(this.id, content);
|
|
772
|
+
}
|
|
773
|
+
onMessage(cb) {
|
|
774
|
+
this.lsp.onResponse(this.id, cb);
|
|
775
|
+
}
|
|
776
|
+
onError(cb) {
|
|
777
|
+
this.lsp.onError(this.id, cb);
|
|
778
|
+
}
|
|
779
|
+
onClose(cb) {
|
|
780
|
+
this.lsp.onClose(this.id, cb);
|
|
781
|
+
}
|
|
782
|
+
dispose() {
|
|
783
|
+
this.lsp.close(this.id);
|
|
784
|
+
}
|
|
785
|
+
start() {
|
|
786
|
+
this.lsp.start(this.id);
|
|
787
|
+
}
|
|
788
|
+
onClientDisconnect(cb) {
|
|
789
|
+
this.lsp.onClientDisconnect(cb);
|
|
790
|
+
}
|
|
791
|
+
whenSocketConnected() {
|
|
792
|
+
return this.lsp.whenSocketConnected();
|
|
793
|
+
}
|
|
794
|
+
static create(id, lsp) {
|
|
795
|
+
return new _LspConnection(id, lsp);
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
|
|
799
|
+
// src/composer.ts
|
|
800
|
+
var Composer = class {
|
|
801
|
+
constructor(okra) {
|
|
802
|
+
this.okra = okra;
|
|
803
|
+
}
|
|
804
|
+
invoke(command, args = {}, options = {}) {
|
|
805
|
+
return this.okra.invoke("composer.invoke", { command, args, options });
|
|
806
|
+
}
|
|
807
|
+
dumpAutoload() {
|
|
808
|
+
return this.okra.invoke("composer.dump-autoload");
|
|
809
|
+
}
|
|
810
|
+
install(options = {}) {
|
|
811
|
+
return this.invoke("install", {}, options);
|
|
812
|
+
}
|
|
813
|
+
update(args, options = {}) {
|
|
814
|
+
return this.invoke("update", args, options);
|
|
815
|
+
}
|
|
816
|
+
require(packages, options = {}) {
|
|
817
|
+
return this.invoke("require", packages, options);
|
|
818
|
+
}
|
|
819
|
+
remove(args, options = {}) {
|
|
820
|
+
return this.invoke("remove", args, options);
|
|
821
|
+
}
|
|
822
|
+
stream(fn) {
|
|
823
|
+
return this.okra.listen("composer.log", fn);
|
|
824
|
+
}
|
|
825
|
+
packages() {
|
|
826
|
+
return this.okra.invoke("composer.packages");
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
// src/log.ts
|
|
831
|
+
var Log = class {
|
|
832
|
+
constructor(okra) {
|
|
833
|
+
this.okra = okra;
|
|
834
|
+
}
|
|
835
|
+
stream(fn) {
|
|
836
|
+
return this.okra.listen("notebook.log", fn);
|
|
837
|
+
}
|
|
838
|
+
listen(event, handler) {
|
|
839
|
+
this.okra.listen(event, handler);
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
|
|
843
|
+
// src/laravel.ts
|
|
844
|
+
var Lravel = class {
|
|
845
|
+
constructor(okra) {
|
|
846
|
+
this.okra = okra;
|
|
847
|
+
}
|
|
848
|
+
maintenanceInfo() {
|
|
849
|
+
return this.okra.invoke("laravel.maintenance.info", {});
|
|
850
|
+
}
|
|
851
|
+
toggleMaintenance(down) {
|
|
852
|
+
return this.okra.invoke("laravel.maintenance.toggle", down);
|
|
853
|
+
}
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
// src/repl.ts
|
|
857
|
+
var Repl = class {
|
|
858
|
+
constructor(okra) {
|
|
859
|
+
this.okra = okra;
|
|
860
|
+
}
|
|
861
|
+
eval(code, args = "", repl = { prependWith: "", appendWith: "" }) {
|
|
862
|
+
return this.okra.invoke("repl.eval", { code, args, repl });
|
|
863
|
+
}
|
|
864
|
+
write(input) {
|
|
865
|
+
return this.okra.invoke("repl.write", { input });
|
|
866
|
+
}
|
|
867
|
+
stop() {
|
|
868
|
+
return this.okra.invoke("repl.stop");
|
|
869
|
+
}
|
|
870
|
+
start() {
|
|
871
|
+
return this.okra.invoke("repl.start");
|
|
872
|
+
}
|
|
873
|
+
resize(cols, rows) {
|
|
874
|
+
return this.okra.invoke("repl.resize", { cols, rows });
|
|
875
|
+
}
|
|
876
|
+
listen(event, handler) {
|
|
877
|
+
this.okra.listen(event, handler);
|
|
878
|
+
}
|
|
879
|
+
onOutput(handler) {
|
|
880
|
+
this.okra.listen("repl.output", handler);
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
// src/shell.ts
|
|
885
|
+
var CommandError = class extends Error {
|
|
886
|
+
constructor(output, exitCode) {
|
|
887
|
+
super(output);
|
|
888
|
+
this.output = output;
|
|
889
|
+
this.exitCode = exitCode;
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
var Result = class {
|
|
893
|
+
constructor(output, exitCode) {
|
|
894
|
+
this.output = output;
|
|
895
|
+
this.exitCode = exitCode;
|
|
896
|
+
}
|
|
897
|
+
throw() {
|
|
898
|
+
if (this.exitCode !== 0) {
|
|
899
|
+
throw new CommandError(this.output, this.exitCode);
|
|
900
|
+
}
|
|
901
|
+
return this;
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
var Shell = class {
|
|
905
|
+
constructor(okra) {
|
|
906
|
+
this.okra = okra;
|
|
907
|
+
}
|
|
908
|
+
async exec(command) {
|
|
909
|
+
const result = await this.okra.invoke("shell.exec", { command });
|
|
910
|
+
return new Result(result.output, result.exitCode);
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
// node_modules/@msgpack/msgpack/dist.esm/utils/utf8.mjs
|
|
915
|
+
function utf8Count(str) {
|
|
916
|
+
const strLength = str.length;
|
|
917
|
+
let byteLength = 0;
|
|
918
|
+
let pos = 0;
|
|
919
|
+
while (pos < strLength) {
|
|
920
|
+
let value = str.charCodeAt(pos++);
|
|
921
|
+
if ((value & 4294967168) === 0) {
|
|
922
|
+
byteLength++;
|
|
923
|
+
continue;
|
|
924
|
+
} else if ((value & 4294965248) === 0) {
|
|
925
|
+
byteLength += 2;
|
|
926
|
+
} else {
|
|
927
|
+
if (value >= 55296 && value <= 56319) {
|
|
928
|
+
if (pos < strLength) {
|
|
929
|
+
const extra = str.charCodeAt(pos);
|
|
930
|
+
if ((extra & 64512) === 56320) {
|
|
931
|
+
++pos;
|
|
932
|
+
value = ((value & 1023) << 10) + (extra & 1023) + 65536;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
if ((value & 4294901760) === 0) {
|
|
937
|
+
byteLength += 3;
|
|
938
|
+
} else {
|
|
939
|
+
byteLength += 4;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return byteLength;
|
|
944
|
+
}
|
|
945
|
+
function utf8EncodeJs(str, output, outputOffset) {
|
|
946
|
+
const strLength = str.length;
|
|
947
|
+
let offset = outputOffset;
|
|
948
|
+
let pos = 0;
|
|
949
|
+
while (pos < strLength) {
|
|
950
|
+
let value = str.charCodeAt(pos++);
|
|
951
|
+
if ((value & 4294967168) === 0) {
|
|
952
|
+
output[offset++] = value;
|
|
953
|
+
continue;
|
|
954
|
+
} else if ((value & 4294965248) === 0) {
|
|
955
|
+
output[offset++] = value >> 6 & 31 | 192;
|
|
956
|
+
} else {
|
|
957
|
+
if (value >= 55296 && value <= 56319) {
|
|
958
|
+
if (pos < strLength) {
|
|
959
|
+
const extra = str.charCodeAt(pos);
|
|
960
|
+
if ((extra & 64512) === 56320) {
|
|
961
|
+
++pos;
|
|
962
|
+
value = ((value & 1023) << 10) + (extra & 1023) + 65536;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
if ((value & 4294901760) === 0) {
|
|
967
|
+
output[offset++] = value >> 12 & 15 | 224;
|
|
968
|
+
output[offset++] = value >> 6 & 63 | 128;
|
|
969
|
+
} else {
|
|
970
|
+
output[offset++] = value >> 18 & 7 | 240;
|
|
971
|
+
output[offset++] = value >> 12 & 63 | 128;
|
|
972
|
+
output[offset++] = value >> 6 & 63 | 128;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
output[offset++] = value & 63 | 128;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
var sharedTextEncoder = new TextEncoder();
|
|
979
|
+
var TEXT_ENCODER_THRESHOLD = 50;
|
|
980
|
+
function utf8EncodeTE(str, output, outputOffset) {
|
|
981
|
+
sharedTextEncoder.encodeInto(str, output.subarray(outputOffset));
|
|
982
|
+
}
|
|
983
|
+
function utf8Encode(str, output, outputOffset) {
|
|
984
|
+
if (str.length > TEXT_ENCODER_THRESHOLD) {
|
|
985
|
+
utf8EncodeTE(str, output, outputOffset);
|
|
986
|
+
} else {
|
|
987
|
+
utf8EncodeJs(str, output, outputOffset);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
var CHUNK_SIZE = 4096;
|
|
991
|
+
function utf8DecodeJs(bytes, inputOffset, byteLength) {
|
|
992
|
+
let offset = inputOffset;
|
|
993
|
+
const end = offset + byteLength;
|
|
994
|
+
const units = [];
|
|
995
|
+
let result = "";
|
|
996
|
+
while (offset < end) {
|
|
997
|
+
const byte1 = bytes[offset++];
|
|
998
|
+
if ((byte1 & 128) === 0) {
|
|
999
|
+
units.push(byte1);
|
|
1000
|
+
} else if ((byte1 & 224) === 192) {
|
|
1001
|
+
const byte2 = bytes[offset++] & 63;
|
|
1002
|
+
units.push((byte1 & 31) << 6 | byte2);
|
|
1003
|
+
} else if ((byte1 & 240) === 224) {
|
|
1004
|
+
const byte2 = bytes[offset++] & 63;
|
|
1005
|
+
const byte3 = bytes[offset++] & 63;
|
|
1006
|
+
units.push((byte1 & 31) << 12 | byte2 << 6 | byte3);
|
|
1007
|
+
} else if ((byte1 & 248) === 240) {
|
|
1008
|
+
const byte2 = bytes[offset++] & 63;
|
|
1009
|
+
const byte3 = bytes[offset++] & 63;
|
|
1010
|
+
const byte4 = bytes[offset++] & 63;
|
|
1011
|
+
let unit = (byte1 & 7) << 18 | byte2 << 12 | byte3 << 6 | byte4;
|
|
1012
|
+
if (unit > 65535) {
|
|
1013
|
+
unit -= 65536;
|
|
1014
|
+
units.push(unit >>> 10 & 1023 | 55296);
|
|
1015
|
+
unit = 56320 | unit & 1023;
|
|
1016
|
+
}
|
|
1017
|
+
units.push(unit);
|
|
1018
|
+
} else {
|
|
1019
|
+
units.push(byte1);
|
|
1020
|
+
}
|
|
1021
|
+
if (units.length >= CHUNK_SIZE) {
|
|
1022
|
+
result += String.fromCharCode(...units);
|
|
1023
|
+
units.length = 0;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
if (units.length > 0) {
|
|
1027
|
+
result += String.fromCharCode(...units);
|
|
1028
|
+
}
|
|
1029
|
+
return result;
|
|
1030
|
+
}
|
|
1031
|
+
var sharedTextDecoder = new TextDecoder();
|
|
1032
|
+
var TEXT_DECODER_THRESHOLD = 200;
|
|
1033
|
+
function utf8DecodeTD(bytes, inputOffset, byteLength) {
|
|
1034
|
+
const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);
|
|
1035
|
+
return sharedTextDecoder.decode(stringBytes);
|
|
1036
|
+
}
|
|
1037
|
+
function utf8Decode(bytes, inputOffset, byteLength) {
|
|
1038
|
+
if (byteLength > TEXT_DECODER_THRESHOLD) {
|
|
1039
|
+
return utf8DecodeTD(bytes, inputOffset, byteLength);
|
|
1040
|
+
} else {
|
|
1041
|
+
return utf8DecodeJs(bytes, inputOffset, byteLength);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
// node_modules/@msgpack/msgpack/dist.esm/ExtData.mjs
|
|
1046
|
+
var ExtData = class {
|
|
1047
|
+
constructor(type, data) {
|
|
1048
|
+
this.type = type;
|
|
1049
|
+
this.data = data;
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
// node_modules/@msgpack/msgpack/dist.esm/DecodeError.mjs
|
|
1054
|
+
var DecodeError = class _DecodeError extends Error {
|
|
1055
|
+
constructor(message) {
|
|
1056
|
+
super(message);
|
|
1057
|
+
const proto = Object.create(_DecodeError.prototype);
|
|
1058
|
+
Object.setPrototypeOf(this, proto);
|
|
1059
|
+
Object.defineProperty(this, "name", {
|
|
1060
|
+
configurable: true,
|
|
1061
|
+
enumerable: false,
|
|
1062
|
+
value: _DecodeError.name
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
|
|
1067
|
+
// node_modules/@msgpack/msgpack/dist.esm/utils/int.mjs
|
|
1068
|
+
var UINT32_MAX = 4294967295;
|
|
1069
|
+
function setUint64(view, offset, value) {
|
|
1070
|
+
const high = value / 4294967296;
|
|
1071
|
+
const low = value;
|
|
1072
|
+
view.setUint32(offset, high);
|
|
1073
|
+
view.setUint32(offset + 4, low);
|
|
1074
|
+
}
|
|
1075
|
+
function setInt64(view, offset, value) {
|
|
1076
|
+
const high = Math.floor(value / 4294967296);
|
|
1077
|
+
const low = value;
|
|
1078
|
+
view.setUint32(offset, high);
|
|
1079
|
+
view.setUint32(offset + 4, low);
|
|
1080
|
+
}
|
|
1081
|
+
function getInt64(view, offset) {
|
|
1082
|
+
const high = view.getInt32(offset);
|
|
1083
|
+
const low = view.getUint32(offset + 4);
|
|
1084
|
+
return high * 4294967296 + low;
|
|
1085
|
+
}
|
|
1086
|
+
function getUint64(view, offset) {
|
|
1087
|
+
const high = view.getUint32(offset);
|
|
1088
|
+
const low = view.getUint32(offset + 4);
|
|
1089
|
+
return high * 4294967296 + low;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
// node_modules/@msgpack/msgpack/dist.esm/timestamp.mjs
|
|
1093
|
+
var EXT_TIMESTAMP = -1;
|
|
1094
|
+
var TIMESTAMP32_MAX_SEC = 4294967296 - 1;
|
|
1095
|
+
var TIMESTAMP64_MAX_SEC = 17179869184 - 1;
|
|
1096
|
+
function encodeTimeSpecToTimestamp({ sec, nsec }) {
|
|
1097
|
+
if (sec >= 0 && nsec >= 0 && sec <= TIMESTAMP64_MAX_SEC) {
|
|
1098
|
+
if (nsec === 0 && sec <= TIMESTAMP32_MAX_SEC) {
|
|
1099
|
+
const rv = new Uint8Array(4);
|
|
1100
|
+
const view = new DataView(rv.buffer);
|
|
1101
|
+
view.setUint32(0, sec);
|
|
1102
|
+
return rv;
|
|
1103
|
+
} else {
|
|
1104
|
+
const secHigh = sec / 4294967296;
|
|
1105
|
+
const secLow = sec & 4294967295;
|
|
1106
|
+
const rv = new Uint8Array(8);
|
|
1107
|
+
const view = new DataView(rv.buffer);
|
|
1108
|
+
view.setUint32(0, nsec << 2 | secHigh & 3);
|
|
1109
|
+
view.setUint32(4, secLow);
|
|
1110
|
+
return rv;
|
|
1111
|
+
}
|
|
1112
|
+
} else {
|
|
1113
|
+
const rv = new Uint8Array(12);
|
|
1114
|
+
const view = new DataView(rv.buffer);
|
|
1115
|
+
view.setUint32(0, nsec);
|
|
1116
|
+
setInt64(view, 4, sec);
|
|
1117
|
+
return rv;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
function encodeDateToTimeSpec(date) {
|
|
1121
|
+
const msec = date.getTime();
|
|
1122
|
+
const sec = Math.floor(msec / 1e3);
|
|
1123
|
+
const nsec = (msec - sec * 1e3) * 1e6;
|
|
1124
|
+
const nsecInSec = Math.floor(nsec / 1e9);
|
|
1125
|
+
return {
|
|
1126
|
+
sec: sec + nsecInSec,
|
|
1127
|
+
nsec: nsec - nsecInSec * 1e9
|
|
1128
|
+
};
|
|
1129
|
+
}
|
|
1130
|
+
function encodeTimestampExtension(object) {
|
|
1131
|
+
if (object instanceof Date) {
|
|
1132
|
+
const timeSpec = encodeDateToTimeSpec(object);
|
|
1133
|
+
return encodeTimeSpecToTimestamp(timeSpec);
|
|
1134
|
+
} else {
|
|
1135
|
+
return null;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
function decodeTimestampToTimeSpec(data) {
|
|
1139
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
1140
|
+
switch (data.byteLength) {
|
|
1141
|
+
case 4: {
|
|
1142
|
+
const sec = view.getUint32(0);
|
|
1143
|
+
const nsec = 0;
|
|
1144
|
+
return { sec, nsec };
|
|
1145
|
+
}
|
|
1146
|
+
case 8: {
|
|
1147
|
+
const nsec30AndSecHigh2 = view.getUint32(0);
|
|
1148
|
+
const secLow32 = view.getUint32(4);
|
|
1149
|
+
const sec = (nsec30AndSecHigh2 & 3) * 4294967296 + secLow32;
|
|
1150
|
+
const nsec = nsec30AndSecHigh2 >>> 2;
|
|
1151
|
+
return { sec, nsec };
|
|
1152
|
+
}
|
|
1153
|
+
case 12: {
|
|
1154
|
+
const sec = getInt64(view, 4);
|
|
1155
|
+
const nsec = view.getUint32(0);
|
|
1156
|
+
return { sec, nsec };
|
|
1157
|
+
}
|
|
1158
|
+
default:
|
|
1159
|
+
throw new DecodeError(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${data.length}`);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
function decodeTimestampExtension(data) {
|
|
1163
|
+
const timeSpec = decodeTimestampToTimeSpec(data);
|
|
1164
|
+
return new Date(timeSpec.sec * 1e3 + timeSpec.nsec / 1e6);
|
|
1165
|
+
}
|
|
1166
|
+
var timestampExtension = {
|
|
1167
|
+
type: EXT_TIMESTAMP,
|
|
1168
|
+
encode: encodeTimestampExtension,
|
|
1169
|
+
decode: decodeTimestampExtension
|
|
1170
|
+
};
|
|
1171
|
+
|
|
1172
|
+
// node_modules/@msgpack/msgpack/dist.esm/ExtensionCodec.mjs
|
|
1173
|
+
var ExtensionCodec = class {
|
|
1174
|
+
constructor() {
|
|
1175
|
+
this.builtInEncoders = [];
|
|
1176
|
+
this.builtInDecoders = [];
|
|
1177
|
+
this.encoders = [];
|
|
1178
|
+
this.decoders = [];
|
|
1179
|
+
this.register(timestampExtension);
|
|
1180
|
+
}
|
|
1181
|
+
register({ type, encode: encode2, decode: decode2 }) {
|
|
1182
|
+
if (type >= 0) {
|
|
1183
|
+
this.encoders[type] = encode2;
|
|
1184
|
+
this.decoders[type] = decode2;
|
|
1185
|
+
} else {
|
|
1186
|
+
const index = -1 - type;
|
|
1187
|
+
this.builtInEncoders[index] = encode2;
|
|
1188
|
+
this.builtInDecoders[index] = decode2;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
tryToEncode(object, context) {
|
|
1192
|
+
for (let i = 0; i < this.builtInEncoders.length; i++) {
|
|
1193
|
+
const encodeExt = this.builtInEncoders[i];
|
|
1194
|
+
if (encodeExt != null) {
|
|
1195
|
+
const data = encodeExt(object, context);
|
|
1196
|
+
if (data != null) {
|
|
1197
|
+
const type = -1 - i;
|
|
1198
|
+
return new ExtData(type, data);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
for (let i = 0; i < this.encoders.length; i++) {
|
|
1203
|
+
const encodeExt = this.encoders[i];
|
|
1204
|
+
if (encodeExt != null) {
|
|
1205
|
+
const data = encodeExt(object, context);
|
|
1206
|
+
if (data != null) {
|
|
1207
|
+
const type = i;
|
|
1208
|
+
return new ExtData(type, data);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
if (object instanceof ExtData) {
|
|
1213
|
+
return object;
|
|
1214
|
+
}
|
|
1215
|
+
return null;
|
|
1216
|
+
}
|
|
1217
|
+
decode(data, type, context) {
|
|
1218
|
+
const decodeExt = type < 0 ? this.builtInDecoders[-1 - type] : this.decoders[type];
|
|
1219
|
+
if (decodeExt) {
|
|
1220
|
+
return decodeExt(data, type, context);
|
|
1221
|
+
} else {
|
|
1222
|
+
return new ExtData(type, data);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
ExtensionCodec.defaultCodec = new ExtensionCodec();
|
|
1227
|
+
|
|
1228
|
+
// node_modules/@msgpack/msgpack/dist.esm/utils/typedArrays.mjs
|
|
1229
|
+
function isArrayBufferLike(buffer) {
|
|
1230
|
+
return buffer instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && buffer instanceof SharedArrayBuffer;
|
|
1231
|
+
}
|
|
1232
|
+
function ensureUint8Array(buffer) {
|
|
1233
|
+
if (buffer instanceof Uint8Array) {
|
|
1234
|
+
return buffer;
|
|
1235
|
+
} else if (ArrayBuffer.isView(buffer)) {
|
|
1236
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
1237
|
+
} else if (isArrayBufferLike(buffer)) {
|
|
1238
|
+
return new Uint8Array(buffer);
|
|
1239
|
+
} else {
|
|
1240
|
+
return Uint8Array.from(buffer);
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
// node_modules/@msgpack/msgpack/dist.esm/Encoder.mjs
|
|
1245
|
+
var DEFAULT_MAX_DEPTH = 100;
|
|
1246
|
+
var DEFAULT_INITIAL_BUFFER_SIZE = 2048;
|
|
1247
|
+
var Encoder = class _Encoder {
|
|
1248
|
+
constructor(options) {
|
|
1249
|
+
this.entered = false;
|
|
1250
|
+
this.extensionCodec = options?.extensionCodec ?? ExtensionCodec.defaultCodec;
|
|
1251
|
+
this.context = options?.context;
|
|
1252
|
+
this.useBigInt64 = options?.useBigInt64 ?? false;
|
|
1253
|
+
this.maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
1254
|
+
this.initialBufferSize = options?.initialBufferSize ?? DEFAULT_INITIAL_BUFFER_SIZE;
|
|
1255
|
+
this.sortKeys = options?.sortKeys ?? false;
|
|
1256
|
+
this.forceFloat32 = options?.forceFloat32 ?? false;
|
|
1257
|
+
this.ignoreUndefined = options?.ignoreUndefined ?? false;
|
|
1258
|
+
this.forceIntegerToFloat = options?.forceIntegerToFloat ?? false;
|
|
1259
|
+
this.pos = 0;
|
|
1260
|
+
this.view = new DataView(new ArrayBuffer(this.initialBufferSize));
|
|
1261
|
+
this.bytes = new Uint8Array(this.view.buffer);
|
|
1262
|
+
}
|
|
1263
|
+
clone() {
|
|
1264
|
+
return new _Encoder({
|
|
1265
|
+
extensionCodec: this.extensionCodec,
|
|
1266
|
+
context: this.context,
|
|
1267
|
+
useBigInt64: this.useBigInt64,
|
|
1268
|
+
maxDepth: this.maxDepth,
|
|
1269
|
+
initialBufferSize: this.initialBufferSize,
|
|
1270
|
+
sortKeys: this.sortKeys,
|
|
1271
|
+
forceFloat32: this.forceFloat32,
|
|
1272
|
+
ignoreUndefined: this.ignoreUndefined,
|
|
1273
|
+
forceIntegerToFloat: this.forceIntegerToFloat
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
reinitializeState() {
|
|
1277
|
+
this.pos = 0;
|
|
1278
|
+
}
|
|
1279
|
+
/**
|
|
1280
|
+
* This is almost equivalent to {@link Encoder#encode}, but it returns an reference of the encoder's internal buffer and thus much faster than {@link Encoder#encode}.
|
|
1281
|
+
*
|
|
1282
|
+
* @returns Encodes the object and returns a shared reference the encoder's internal buffer.
|
|
1283
|
+
*/
|
|
1284
|
+
encodeSharedRef(object) {
|
|
1285
|
+
if (this.entered) {
|
|
1286
|
+
const instance = this.clone();
|
|
1287
|
+
return instance.encodeSharedRef(object);
|
|
1288
|
+
}
|
|
1289
|
+
try {
|
|
1290
|
+
this.entered = true;
|
|
1291
|
+
this.reinitializeState();
|
|
1292
|
+
this.doEncode(object, 1);
|
|
1293
|
+
return this.bytes.subarray(0, this.pos);
|
|
1294
|
+
} finally {
|
|
1295
|
+
this.entered = false;
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* @returns Encodes the object and returns a copy of the encoder's internal buffer.
|
|
1300
|
+
*/
|
|
1301
|
+
encode(object) {
|
|
1302
|
+
if (this.entered) {
|
|
1303
|
+
const instance = this.clone();
|
|
1304
|
+
return instance.encode(object);
|
|
1305
|
+
}
|
|
1306
|
+
try {
|
|
1307
|
+
this.entered = true;
|
|
1308
|
+
this.reinitializeState();
|
|
1309
|
+
this.doEncode(object, 1);
|
|
1310
|
+
return this.bytes.slice(0, this.pos);
|
|
1311
|
+
} finally {
|
|
1312
|
+
this.entered = false;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
doEncode(object, depth) {
|
|
1316
|
+
if (depth > this.maxDepth) {
|
|
1317
|
+
throw new Error(`Too deep objects in depth ${depth}`);
|
|
1318
|
+
}
|
|
1319
|
+
if (object == null) {
|
|
1320
|
+
this.encodeNil();
|
|
1321
|
+
} else if (typeof object === "boolean") {
|
|
1322
|
+
this.encodeBoolean(object);
|
|
1323
|
+
} else if (typeof object === "number") {
|
|
1324
|
+
if (!this.forceIntegerToFloat) {
|
|
1325
|
+
this.encodeNumber(object);
|
|
1326
|
+
} else {
|
|
1327
|
+
this.encodeNumberAsFloat(object);
|
|
1328
|
+
}
|
|
1329
|
+
} else if (typeof object === "string") {
|
|
1330
|
+
this.encodeString(object);
|
|
1331
|
+
} else if (this.useBigInt64 && typeof object === "bigint") {
|
|
1332
|
+
this.encodeBigInt64(object);
|
|
1333
|
+
} else {
|
|
1334
|
+
this.encodeObject(object, depth);
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
ensureBufferSizeToWrite(sizeToWrite) {
|
|
1338
|
+
const requiredSize = this.pos + sizeToWrite;
|
|
1339
|
+
if (this.view.byteLength < requiredSize) {
|
|
1340
|
+
this.resizeBuffer(requiredSize * 2);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
resizeBuffer(newSize) {
|
|
1344
|
+
const newBuffer = new ArrayBuffer(newSize);
|
|
1345
|
+
const newBytes = new Uint8Array(newBuffer);
|
|
1346
|
+
const newView = new DataView(newBuffer);
|
|
1347
|
+
newBytes.set(this.bytes);
|
|
1348
|
+
this.view = newView;
|
|
1349
|
+
this.bytes = newBytes;
|
|
1350
|
+
}
|
|
1351
|
+
encodeNil() {
|
|
1352
|
+
this.writeU8(192);
|
|
1353
|
+
}
|
|
1354
|
+
encodeBoolean(object) {
|
|
1355
|
+
if (object === false) {
|
|
1356
|
+
this.writeU8(194);
|
|
1357
|
+
} else {
|
|
1358
|
+
this.writeU8(195);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
encodeNumber(object) {
|
|
1362
|
+
if (!this.forceIntegerToFloat && Number.isSafeInteger(object)) {
|
|
1363
|
+
if (object >= 0) {
|
|
1364
|
+
if (object < 128) {
|
|
1365
|
+
this.writeU8(object);
|
|
1366
|
+
} else if (object < 256) {
|
|
1367
|
+
this.writeU8(204);
|
|
1368
|
+
this.writeU8(object);
|
|
1369
|
+
} else if (object < 65536) {
|
|
1370
|
+
this.writeU8(205);
|
|
1371
|
+
this.writeU16(object);
|
|
1372
|
+
} else if (object < 4294967296) {
|
|
1373
|
+
this.writeU8(206);
|
|
1374
|
+
this.writeU32(object);
|
|
1375
|
+
} else if (!this.useBigInt64) {
|
|
1376
|
+
this.writeU8(207);
|
|
1377
|
+
this.writeU64(object);
|
|
1378
|
+
} else {
|
|
1379
|
+
this.encodeNumberAsFloat(object);
|
|
1380
|
+
}
|
|
1381
|
+
} else {
|
|
1382
|
+
if (object >= -32) {
|
|
1383
|
+
this.writeU8(224 | object + 32);
|
|
1384
|
+
} else if (object >= -128) {
|
|
1385
|
+
this.writeU8(208);
|
|
1386
|
+
this.writeI8(object);
|
|
1387
|
+
} else if (object >= -32768) {
|
|
1388
|
+
this.writeU8(209);
|
|
1389
|
+
this.writeI16(object);
|
|
1390
|
+
} else if (object >= -2147483648) {
|
|
1391
|
+
this.writeU8(210);
|
|
1392
|
+
this.writeI32(object);
|
|
1393
|
+
} else if (!this.useBigInt64) {
|
|
1394
|
+
this.writeU8(211);
|
|
1395
|
+
this.writeI64(object);
|
|
1396
|
+
} else {
|
|
1397
|
+
this.encodeNumberAsFloat(object);
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
} else {
|
|
1401
|
+
this.encodeNumberAsFloat(object);
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
encodeNumberAsFloat(object) {
|
|
1405
|
+
if (this.forceFloat32) {
|
|
1406
|
+
this.writeU8(202);
|
|
1407
|
+
this.writeF32(object);
|
|
1408
|
+
} else {
|
|
1409
|
+
this.writeU8(203);
|
|
1410
|
+
this.writeF64(object);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
encodeBigInt64(object) {
|
|
1414
|
+
if (object >= BigInt(0)) {
|
|
1415
|
+
this.writeU8(207);
|
|
1416
|
+
this.writeBigUint64(object);
|
|
1417
|
+
} else {
|
|
1418
|
+
this.writeU8(211);
|
|
1419
|
+
this.writeBigInt64(object);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
writeStringHeader(byteLength) {
|
|
1423
|
+
if (byteLength < 32) {
|
|
1424
|
+
this.writeU8(160 + byteLength);
|
|
1425
|
+
} else if (byteLength < 256) {
|
|
1426
|
+
this.writeU8(217);
|
|
1427
|
+
this.writeU8(byteLength);
|
|
1428
|
+
} else if (byteLength < 65536) {
|
|
1429
|
+
this.writeU8(218);
|
|
1430
|
+
this.writeU16(byteLength);
|
|
1431
|
+
} else if (byteLength < 4294967296) {
|
|
1432
|
+
this.writeU8(219);
|
|
1433
|
+
this.writeU32(byteLength);
|
|
1434
|
+
} else {
|
|
1435
|
+
throw new Error(`Too long string: ${byteLength} bytes in UTF-8`);
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
encodeString(object) {
|
|
1439
|
+
const maxHeaderSize = 1 + 4;
|
|
1440
|
+
const byteLength = utf8Count(object);
|
|
1441
|
+
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
|
|
1442
|
+
this.writeStringHeader(byteLength);
|
|
1443
|
+
utf8Encode(object, this.bytes, this.pos);
|
|
1444
|
+
this.pos += byteLength;
|
|
1445
|
+
}
|
|
1446
|
+
encodeObject(object, depth) {
|
|
1447
|
+
const ext = this.extensionCodec.tryToEncode(object, this.context);
|
|
1448
|
+
if (ext != null) {
|
|
1449
|
+
this.encodeExtension(ext);
|
|
1450
|
+
} else if (Array.isArray(object)) {
|
|
1451
|
+
this.encodeArray(object, depth);
|
|
1452
|
+
} else if (ArrayBuffer.isView(object)) {
|
|
1453
|
+
this.encodeBinary(object);
|
|
1454
|
+
} else if (typeof object === "object") {
|
|
1455
|
+
this.encodeMap(object, depth);
|
|
1456
|
+
} else {
|
|
1457
|
+
throw new Error(`Unrecognized object: ${Object.prototype.toString.apply(object)}`);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
encodeBinary(object) {
|
|
1461
|
+
const size = object.byteLength;
|
|
1462
|
+
if (size < 256) {
|
|
1463
|
+
this.writeU8(196);
|
|
1464
|
+
this.writeU8(size);
|
|
1465
|
+
} else if (size < 65536) {
|
|
1466
|
+
this.writeU8(197);
|
|
1467
|
+
this.writeU16(size);
|
|
1468
|
+
} else if (size < 4294967296) {
|
|
1469
|
+
this.writeU8(198);
|
|
1470
|
+
this.writeU32(size);
|
|
1471
|
+
} else {
|
|
1472
|
+
throw new Error(`Too large binary: ${size}`);
|
|
1473
|
+
}
|
|
1474
|
+
const bytes = ensureUint8Array(object);
|
|
1475
|
+
this.writeU8a(bytes);
|
|
1476
|
+
}
|
|
1477
|
+
encodeArray(object, depth) {
|
|
1478
|
+
const size = object.length;
|
|
1479
|
+
if (size < 16) {
|
|
1480
|
+
this.writeU8(144 + size);
|
|
1481
|
+
} else if (size < 65536) {
|
|
1482
|
+
this.writeU8(220);
|
|
1483
|
+
this.writeU16(size);
|
|
1484
|
+
} else if (size < 4294967296) {
|
|
1485
|
+
this.writeU8(221);
|
|
1486
|
+
this.writeU32(size);
|
|
1487
|
+
} else {
|
|
1488
|
+
throw new Error(`Too large array: ${size}`);
|
|
1489
|
+
}
|
|
1490
|
+
for (const item of object) {
|
|
1491
|
+
this.doEncode(item, depth + 1);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
countWithoutUndefined(object, keys) {
|
|
1495
|
+
let count = 0;
|
|
1496
|
+
for (const key of keys) {
|
|
1497
|
+
if (object[key] !== void 0) {
|
|
1498
|
+
count++;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
return count;
|
|
1502
|
+
}
|
|
1503
|
+
encodeMap(object, depth) {
|
|
1504
|
+
const keys = Object.keys(object);
|
|
1505
|
+
if (this.sortKeys) {
|
|
1506
|
+
keys.sort();
|
|
1507
|
+
}
|
|
1508
|
+
const size = this.ignoreUndefined ? this.countWithoutUndefined(object, keys) : keys.length;
|
|
1509
|
+
if (size < 16) {
|
|
1510
|
+
this.writeU8(128 + size);
|
|
1511
|
+
} else if (size < 65536) {
|
|
1512
|
+
this.writeU8(222);
|
|
1513
|
+
this.writeU16(size);
|
|
1514
|
+
} else if (size < 4294967296) {
|
|
1515
|
+
this.writeU8(223);
|
|
1516
|
+
this.writeU32(size);
|
|
1517
|
+
} else {
|
|
1518
|
+
throw new Error(`Too large map object: ${size}`);
|
|
1519
|
+
}
|
|
1520
|
+
for (const key of keys) {
|
|
1521
|
+
const value = object[key];
|
|
1522
|
+
if (!(this.ignoreUndefined && value === void 0)) {
|
|
1523
|
+
this.encodeString(key);
|
|
1524
|
+
this.doEncode(value, depth + 1);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
encodeExtension(ext) {
|
|
1529
|
+
if (typeof ext.data === "function") {
|
|
1530
|
+
const data = ext.data(this.pos + 6);
|
|
1531
|
+
const size2 = data.length;
|
|
1532
|
+
if (size2 >= 4294967296) {
|
|
1533
|
+
throw new Error(`Too large extension object: ${size2}`);
|
|
1534
|
+
}
|
|
1535
|
+
this.writeU8(201);
|
|
1536
|
+
this.writeU32(size2);
|
|
1537
|
+
this.writeI8(ext.type);
|
|
1538
|
+
this.writeU8a(data);
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
const size = ext.data.length;
|
|
1542
|
+
if (size === 1) {
|
|
1543
|
+
this.writeU8(212);
|
|
1544
|
+
} else if (size === 2) {
|
|
1545
|
+
this.writeU8(213);
|
|
1546
|
+
} else if (size === 4) {
|
|
1547
|
+
this.writeU8(214);
|
|
1548
|
+
} else if (size === 8) {
|
|
1549
|
+
this.writeU8(215);
|
|
1550
|
+
} else if (size === 16) {
|
|
1551
|
+
this.writeU8(216);
|
|
1552
|
+
} else if (size < 256) {
|
|
1553
|
+
this.writeU8(199);
|
|
1554
|
+
this.writeU8(size);
|
|
1555
|
+
} else if (size < 65536) {
|
|
1556
|
+
this.writeU8(200);
|
|
1557
|
+
this.writeU16(size);
|
|
1558
|
+
} else if (size < 4294967296) {
|
|
1559
|
+
this.writeU8(201);
|
|
1560
|
+
this.writeU32(size);
|
|
1561
|
+
} else {
|
|
1562
|
+
throw new Error(`Too large extension object: ${size}`);
|
|
1563
|
+
}
|
|
1564
|
+
this.writeI8(ext.type);
|
|
1565
|
+
this.writeU8a(ext.data);
|
|
1566
|
+
}
|
|
1567
|
+
writeU8(value) {
|
|
1568
|
+
this.ensureBufferSizeToWrite(1);
|
|
1569
|
+
this.view.setUint8(this.pos, value);
|
|
1570
|
+
this.pos++;
|
|
1571
|
+
}
|
|
1572
|
+
writeU8a(values) {
|
|
1573
|
+
const size = values.length;
|
|
1574
|
+
this.ensureBufferSizeToWrite(size);
|
|
1575
|
+
this.bytes.set(values, this.pos);
|
|
1576
|
+
this.pos += size;
|
|
1577
|
+
}
|
|
1578
|
+
writeI8(value) {
|
|
1579
|
+
this.ensureBufferSizeToWrite(1);
|
|
1580
|
+
this.view.setInt8(this.pos, value);
|
|
1581
|
+
this.pos++;
|
|
1582
|
+
}
|
|
1583
|
+
writeU16(value) {
|
|
1584
|
+
this.ensureBufferSizeToWrite(2);
|
|
1585
|
+
this.view.setUint16(this.pos, value);
|
|
1586
|
+
this.pos += 2;
|
|
1587
|
+
}
|
|
1588
|
+
writeI16(value) {
|
|
1589
|
+
this.ensureBufferSizeToWrite(2);
|
|
1590
|
+
this.view.setInt16(this.pos, value);
|
|
1591
|
+
this.pos += 2;
|
|
1592
|
+
}
|
|
1593
|
+
writeU32(value) {
|
|
1594
|
+
this.ensureBufferSizeToWrite(4);
|
|
1595
|
+
this.view.setUint32(this.pos, value);
|
|
1596
|
+
this.pos += 4;
|
|
1597
|
+
}
|
|
1598
|
+
writeI32(value) {
|
|
1599
|
+
this.ensureBufferSizeToWrite(4);
|
|
1600
|
+
this.view.setInt32(this.pos, value);
|
|
1601
|
+
this.pos += 4;
|
|
1602
|
+
}
|
|
1603
|
+
writeF32(value) {
|
|
1604
|
+
this.ensureBufferSizeToWrite(4);
|
|
1605
|
+
this.view.setFloat32(this.pos, value);
|
|
1606
|
+
this.pos += 4;
|
|
1607
|
+
}
|
|
1608
|
+
writeF64(value) {
|
|
1609
|
+
this.ensureBufferSizeToWrite(8);
|
|
1610
|
+
this.view.setFloat64(this.pos, value);
|
|
1611
|
+
this.pos += 8;
|
|
1612
|
+
}
|
|
1613
|
+
writeU64(value) {
|
|
1614
|
+
this.ensureBufferSizeToWrite(8);
|
|
1615
|
+
setUint64(this.view, this.pos, value);
|
|
1616
|
+
this.pos += 8;
|
|
1617
|
+
}
|
|
1618
|
+
writeI64(value) {
|
|
1619
|
+
this.ensureBufferSizeToWrite(8);
|
|
1620
|
+
setInt64(this.view, this.pos, value);
|
|
1621
|
+
this.pos += 8;
|
|
1622
|
+
}
|
|
1623
|
+
writeBigUint64(value) {
|
|
1624
|
+
this.ensureBufferSizeToWrite(8);
|
|
1625
|
+
this.view.setBigUint64(this.pos, value);
|
|
1626
|
+
this.pos += 8;
|
|
1627
|
+
}
|
|
1628
|
+
writeBigInt64(value) {
|
|
1629
|
+
this.ensureBufferSizeToWrite(8);
|
|
1630
|
+
this.view.setBigInt64(this.pos, value);
|
|
1631
|
+
this.pos += 8;
|
|
1632
|
+
}
|
|
1633
|
+
};
|
|
1634
|
+
|
|
1635
|
+
// node_modules/@msgpack/msgpack/dist.esm/encode.mjs
|
|
1636
|
+
function encode(value, options) {
|
|
1637
|
+
const encoder = new Encoder(options);
|
|
1638
|
+
return encoder.encodeSharedRef(value);
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
// node_modules/@msgpack/msgpack/dist.esm/utils/prettyByte.mjs
|
|
1642
|
+
function prettyByte(byte) {
|
|
1643
|
+
return `${byte < 0 ? "-" : ""}0x${Math.abs(byte).toString(16).padStart(2, "0")}`;
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
// node_modules/@msgpack/msgpack/dist.esm/CachedKeyDecoder.mjs
|
|
1647
|
+
var DEFAULT_MAX_KEY_LENGTH = 16;
|
|
1648
|
+
var DEFAULT_MAX_LENGTH_PER_KEY = 16;
|
|
1649
|
+
var CachedKeyDecoder = class {
|
|
1650
|
+
constructor(maxKeyLength = DEFAULT_MAX_KEY_LENGTH, maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY) {
|
|
1651
|
+
this.hit = 0;
|
|
1652
|
+
this.miss = 0;
|
|
1653
|
+
this.maxKeyLength = maxKeyLength;
|
|
1654
|
+
this.maxLengthPerKey = maxLengthPerKey;
|
|
1655
|
+
this.caches = [];
|
|
1656
|
+
for (let i = 0; i < this.maxKeyLength; i++) {
|
|
1657
|
+
this.caches.push([]);
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
canBeCached(byteLength) {
|
|
1661
|
+
return byteLength > 0 && byteLength <= this.maxKeyLength;
|
|
1662
|
+
}
|
|
1663
|
+
find(bytes, inputOffset, byteLength) {
|
|
1664
|
+
const records = this.caches[byteLength - 1];
|
|
1665
|
+
FIND_CHUNK: for (const record of records) {
|
|
1666
|
+
const recordBytes = record.bytes;
|
|
1667
|
+
for (let j = 0; j < byteLength; j++) {
|
|
1668
|
+
if (recordBytes[j] !== bytes[inputOffset + j]) {
|
|
1669
|
+
continue FIND_CHUNK;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
return record.str;
|
|
1673
|
+
}
|
|
1674
|
+
return null;
|
|
1675
|
+
}
|
|
1676
|
+
store(bytes, value) {
|
|
1677
|
+
const records = this.caches[bytes.length - 1];
|
|
1678
|
+
const record = { bytes, str: value };
|
|
1679
|
+
if (records.length >= this.maxLengthPerKey) {
|
|
1680
|
+
records[Math.random() * records.length | 0] = record;
|
|
1681
|
+
} else {
|
|
1682
|
+
records.push(record);
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
decode(bytes, inputOffset, byteLength) {
|
|
1686
|
+
const cachedValue = this.find(bytes, inputOffset, byteLength);
|
|
1687
|
+
if (cachedValue != null) {
|
|
1688
|
+
this.hit++;
|
|
1689
|
+
return cachedValue;
|
|
1690
|
+
}
|
|
1691
|
+
this.miss++;
|
|
1692
|
+
const str = utf8DecodeJs(bytes, inputOffset, byteLength);
|
|
1693
|
+
const slicedCopyOfBytes = Uint8Array.prototype.slice.call(bytes, inputOffset, inputOffset + byteLength);
|
|
1694
|
+
this.store(slicedCopyOfBytes, str);
|
|
1695
|
+
return str;
|
|
1696
|
+
}
|
|
1697
|
+
};
|
|
1698
|
+
|
|
1699
|
+
// node_modules/@msgpack/msgpack/dist.esm/Decoder.mjs
|
|
1700
|
+
var STATE_ARRAY = "array";
|
|
1701
|
+
var STATE_MAP_KEY = "map_key";
|
|
1702
|
+
var STATE_MAP_VALUE = "map_value";
|
|
1703
|
+
var mapKeyConverter = (key) => {
|
|
1704
|
+
if (typeof key === "string" || typeof key === "number") {
|
|
1705
|
+
return key;
|
|
1706
|
+
}
|
|
1707
|
+
throw new DecodeError("The type of key must be string or number but " + typeof key);
|
|
1708
|
+
};
|
|
1709
|
+
var StackPool = class {
|
|
1710
|
+
constructor() {
|
|
1711
|
+
this.stack = [];
|
|
1712
|
+
this.stackHeadPosition = -1;
|
|
1713
|
+
}
|
|
1714
|
+
get length() {
|
|
1715
|
+
return this.stackHeadPosition + 1;
|
|
1716
|
+
}
|
|
1717
|
+
top() {
|
|
1718
|
+
return this.stack[this.stackHeadPosition];
|
|
1719
|
+
}
|
|
1720
|
+
pushArrayState(size) {
|
|
1721
|
+
const state = this.getUninitializedStateFromPool();
|
|
1722
|
+
state.type = STATE_ARRAY;
|
|
1723
|
+
state.position = 0;
|
|
1724
|
+
state.size = size;
|
|
1725
|
+
state.array = new Array(size);
|
|
1726
|
+
}
|
|
1727
|
+
pushMapState(size) {
|
|
1728
|
+
const state = this.getUninitializedStateFromPool();
|
|
1729
|
+
state.type = STATE_MAP_KEY;
|
|
1730
|
+
state.readCount = 0;
|
|
1731
|
+
state.size = size;
|
|
1732
|
+
state.map = {};
|
|
1733
|
+
}
|
|
1734
|
+
getUninitializedStateFromPool() {
|
|
1735
|
+
this.stackHeadPosition++;
|
|
1736
|
+
if (this.stackHeadPosition === this.stack.length) {
|
|
1737
|
+
const partialState = {
|
|
1738
|
+
type: void 0,
|
|
1739
|
+
size: 0,
|
|
1740
|
+
array: void 0,
|
|
1741
|
+
position: 0,
|
|
1742
|
+
readCount: 0,
|
|
1743
|
+
map: void 0,
|
|
1744
|
+
key: null
|
|
1745
|
+
};
|
|
1746
|
+
this.stack.push(partialState);
|
|
1747
|
+
}
|
|
1748
|
+
return this.stack[this.stackHeadPosition];
|
|
1749
|
+
}
|
|
1750
|
+
release(state) {
|
|
1751
|
+
const topStackState = this.stack[this.stackHeadPosition];
|
|
1752
|
+
if (topStackState !== state) {
|
|
1753
|
+
throw new Error("Invalid stack state. Released state is not on top of the stack.");
|
|
1754
|
+
}
|
|
1755
|
+
if (state.type === STATE_ARRAY) {
|
|
1756
|
+
const partialState = state;
|
|
1757
|
+
partialState.size = 0;
|
|
1758
|
+
partialState.array = void 0;
|
|
1759
|
+
partialState.position = 0;
|
|
1760
|
+
partialState.type = void 0;
|
|
1761
|
+
}
|
|
1762
|
+
if (state.type === STATE_MAP_KEY || state.type === STATE_MAP_VALUE) {
|
|
1763
|
+
const partialState = state;
|
|
1764
|
+
partialState.size = 0;
|
|
1765
|
+
partialState.map = void 0;
|
|
1766
|
+
partialState.readCount = 0;
|
|
1767
|
+
partialState.type = void 0;
|
|
1768
|
+
}
|
|
1769
|
+
this.stackHeadPosition--;
|
|
1770
|
+
}
|
|
1771
|
+
reset() {
|
|
1772
|
+
this.stack.length = 0;
|
|
1773
|
+
this.stackHeadPosition = -1;
|
|
1774
|
+
}
|
|
1775
|
+
};
|
|
1776
|
+
var HEAD_BYTE_REQUIRED = -1;
|
|
1777
|
+
var EMPTY_VIEW = new DataView(new ArrayBuffer(0));
|
|
1778
|
+
var EMPTY_BYTES = new Uint8Array(EMPTY_VIEW.buffer);
|
|
1779
|
+
try {
|
|
1780
|
+
EMPTY_VIEW.getInt8(0);
|
|
1781
|
+
} catch (e) {
|
|
1782
|
+
if (!(e instanceof RangeError)) {
|
|
1783
|
+
throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access");
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
var MORE_DATA = new RangeError("Insufficient data");
|
|
1787
|
+
var sharedCachedKeyDecoder = new CachedKeyDecoder();
|
|
1788
|
+
var Decoder = class _Decoder {
|
|
1789
|
+
constructor(options) {
|
|
1790
|
+
this.totalPos = 0;
|
|
1791
|
+
this.pos = 0;
|
|
1792
|
+
this.view = EMPTY_VIEW;
|
|
1793
|
+
this.bytes = EMPTY_BYTES;
|
|
1794
|
+
this.headByte = HEAD_BYTE_REQUIRED;
|
|
1795
|
+
this.stack = new StackPool();
|
|
1796
|
+
this.entered = false;
|
|
1797
|
+
this.extensionCodec = options?.extensionCodec ?? ExtensionCodec.defaultCodec;
|
|
1798
|
+
this.context = options?.context;
|
|
1799
|
+
this.useBigInt64 = options?.useBigInt64 ?? false;
|
|
1800
|
+
this.rawStrings = options?.rawStrings ?? false;
|
|
1801
|
+
this.maxStrLength = options?.maxStrLength ?? UINT32_MAX;
|
|
1802
|
+
this.maxBinLength = options?.maxBinLength ?? UINT32_MAX;
|
|
1803
|
+
this.maxArrayLength = options?.maxArrayLength ?? UINT32_MAX;
|
|
1804
|
+
this.maxMapLength = options?.maxMapLength ?? UINT32_MAX;
|
|
1805
|
+
this.maxExtLength = options?.maxExtLength ?? UINT32_MAX;
|
|
1806
|
+
this.keyDecoder = options?.keyDecoder !== void 0 ? options.keyDecoder : sharedCachedKeyDecoder;
|
|
1807
|
+
this.mapKeyConverter = options?.mapKeyConverter ?? mapKeyConverter;
|
|
1808
|
+
}
|
|
1809
|
+
clone() {
|
|
1810
|
+
return new _Decoder({
|
|
1811
|
+
extensionCodec: this.extensionCodec,
|
|
1812
|
+
context: this.context,
|
|
1813
|
+
useBigInt64: this.useBigInt64,
|
|
1814
|
+
rawStrings: this.rawStrings,
|
|
1815
|
+
maxStrLength: this.maxStrLength,
|
|
1816
|
+
maxBinLength: this.maxBinLength,
|
|
1817
|
+
maxArrayLength: this.maxArrayLength,
|
|
1818
|
+
maxMapLength: this.maxMapLength,
|
|
1819
|
+
maxExtLength: this.maxExtLength,
|
|
1820
|
+
keyDecoder: this.keyDecoder
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
reinitializeState() {
|
|
1824
|
+
this.totalPos = 0;
|
|
1825
|
+
this.headByte = HEAD_BYTE_REQUIRED;
|
|
1826
|
+
this.stack.reset();
|
|
1827
|
+
}
|
|
1828
|
+
setBuffer(buffer) {
|
|
1829
|
+
const bytes = ensureUint8Array(buffer);
|
|
1830
|
+
this.bytes = bytes;
|
|
1831
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1832
|
+
this.pos = 0;
|
|
1833
|
+
}
|
|
1834
|
+
appendBuffer(buffer) {
|
|
1835
|
+
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining(1)) {
|
|
1836
|
+
this.setBuffer(buffer);
|
|
1837
|
+
} else {
|
|
1838
|
+
const remainingData = this.bytes.subarray(this.pos);
|
|
1839
|
+
const newData = ensureUint8Array(buffer);
|
|
1840
|
+
const newBuffer = new Uint8Array(remainingData.length + newData.length);
|
|
1841
|
+
newBuffer.set(remainingData);
|
|
1842
|
+
newBuffer.set(newData, remainingData.length);
|
|
1843
|
+
this.setBuffer(newBuffer);
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
hasRemaining(size) {
|
|
1847
|
+
return this.view.byteLength - this.pos >= size;
|
|
1848
|
+
}
|
|
1849
|
+
createExtraByteError(posToShow) {
|
|
1850
|
+
const { view, pos } = this;
|
|
1851
|
+
return new RangeError(`Extra ${view.byteLength - pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`);
|
|
1852
|
+
}
|
|
1853
|
+
/**
|
|
1854
|
+
* @throws {@link DecodeError}
|
|
1855
|
+
* @throws {@link RangeError}
|
|
1856
|
+
*/
|
|
1857
|
+
decode(buffer) {
|
|
1858
|
+
if (this.entered) {
|
|
1859
|
+
const instance = this.clone();
|
|
1860
|
+
return instance.decode(buffer);
|
|
1861
|
+
}
|
|
1862
|
+
try {
|
|
1863
|
+
this.entered = true;
|
|
1864
|
+
this.reinitializeState();
|
|
1865
|
+
this.setBuffer(buffer);
|
|
1866
|
+
const object = this.doDecodeSync();
|
|
1867
|
+
if (this.hasRemaining(1)) {
|
|
1868
|
+
throw this.createExtraByteError(this.pos);
|
|
1869
|
+
}
|
|
1870
|
+
return object;
|
|
1871
|
+
} finally {
|
|
1872
|
+
this.entered = false;
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
*decodeMulti(buffer) {
|
|
1876
|
+
if (this.entered) {
|
|
1877
|
+
const instance = this.clone();
|
|
1878
|
+
yield* instance.decodeMulti(buffer);
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
try {
|
|
1882
|
+
this.entered = true;
|
|
1883
|
+
this.reinitializeState();
|
|
1884
|
+
this.setBuffer(buffer);
|
|
1885
|
+
while (this.hasRemaining(1)) {
|
|
1886
|
+
yield this.doDecodeSync();
|
|
1887
|
+
}
|
|
1888
|
+
} finally {
|
|
1889
|
+
this.entered = false;
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
async decodeAsync(stream) {
|
|
1893
|
+
if (this.entered) {
|
|
1894
|
+
const instance = this.clone();
|
|
1895
|
+
return instance.decodeAsync(stream);
|
|
1896
|
+
}
|
|
1897
|
+
try {
|
|
1898
|
+
this.entered = true;
|
|
1899
|
+
let decoded = false;
|
|
1900
|
+
let object;
|
|
1901
|
+
for await (const buffer of stream) {
|
|
1902
|
+
if (decoded) {
|
|
1903
|
+
this.entered = false;
|
|
1904
|
+
throw this.createExtraByteError(this.totalPos);
|
|
1905
|
+
}
|
|
1906
|
+
this.appendBuffer(buffer);
|
|
1907
|
+
try {
|
|
1908
|
+
object = this.doDecodeSync();
|
|
1909
|
+
decoded = true;
|
|
1910
|
+
} catch (e) {
|
|
1911
|
+
if (!(e instanceof RangeError)) {
|
|
1912
|
+
throw e;
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
this.totalPos += this.pos;
|
|
1916
|
+
}
|
|
1917
|
+
if (decoded) {
|
|
1918
|
+
if (this.hasRemaining(1)) {
|
|
1919
|
+
throw this.createExtraByteError(this.totalPos);
|
|
1920
|
+
}
|
|
1921
|
+
return object;
|
|
1922
|
+
}
|
|
1923
|
+
const { headByte, pos, totalPos } = this;
|
|
1924
|
+
throw new RangeError(`Insufficient data in parsing ${prettyByte(headByte)} at ${totalPos} (${pos} in the current buffer)`);
|
|
1925
|
+
} finally {
|
|
1926
|
+
this.entered = false;
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
decodeArrayStream(stream) {
|
|
1930
|
+
return this.decodeMultiAsync(stream, true);
|
|
1931
|
+
}
|
|
1932
|
+
decodeStream(stream) {
|
|
1933
|
+
return this.decodeMultiAsync(stream, false);
|
|
1934
|
+
}
|
|
1935
|
+
async *decodeMultiAsync(stream, isArray) {
|
|
1936
|
+
if (this.entered) {
|
|
1937
|
+
const instance = this.clone();
|
|
1938
|
+
yield* instance.decodeMultiAsync(stream, isArray);
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
try {
|
|
1942
|
+
this.entered = true;
|
|
1943
|
+
let isArrayHeaderRequired = isArray;
|
|
1944
|
+
let arrayItemsLeft = -1;
|
|
1945
|
+
for await (const buffer of stream) {
|
|
1946
|
+
if (isArray && arrayItemsLeft === 0) {
|
|
1947
|
+
throw this.createExtraByteError(this.totalPos);
|
|
1948
|
+
}
|
|
1949
|
+
this.appendBuffer(buffer);
|
|
1950
|
+
if (isArrayHeaderRequired) {
|
|
1951
|
+
arrayItemsLeft = this.readArraySize();
|
|
1952
|
+
isArrayHeaderRequired = false;
|
|
1953
|
+
this.complete();
|
|
1954
|
+
}
|
|
1955
|
+
try {
|
|
1956
|
+
while (true) {
|
|
1957
|
+
yield this.doDecodeSync();
|
|
1958
|
+
if (--arrayItemsLeft === 0) {
|
|
1959
|
+
break;
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
} catch (e) {
|
|
1963
|
+
if (!(e instanceof RangeError)) {
|
|
1964
|
+
throw e;
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
this.totalPos += this.pos;
|
|
1968
|
+
}
|
|
1969
|
+
} finally {
|
|
1970
|
+
this.entered = false;
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
doDecodeSync() {
|
|
1974
|
+
DECODE: while (true) {
|
|
1975
|
+
const headByte = this.readHeadByte();
|
|
1976
|
+
let object;
|
|
1977
|
+
if (headByte >= 224) {
|
|
1978
|
+
object = headByte - 256;
|
|
1979
|
+
} else if (headByte < 192) {
|
|
1980
|
+
if (headByte < 128) {
|
|
1981
|
+
object = headByte;
|
|
1982
|
+
} else if (headByte < 144) {
|
|
1983
|
+
const size = headByte - 128;
|
|
1984
|
+
if (size !== 0) {
|
|
1985
|
+
this.pushMapState(size);
|
|
1986
|
+
this.complete();
|
|
1987
|
+
continue DECODE;
|
|
1988
|
+
} else {
|
|
1989
|
+
object = {};
|
|
1990
|
+
}
|
|
1991
|
+
} else if (headByte < 160) {
|
|
1992
|
+
const size = headByte - 144;
|
|
1993
|
+
if (size !== 0) {
|
|
1994
|
+
this.pushArrayState(size);
|
|
1995
|
+
this.complete();
|
|
1996
|
+
continue DECODE;
|
|
1997
|
+
} else {
|
|
1998
|
+
object = [];
|
|
1999
|
+
}
|
|
2000
|
+
} else {
|
|
2001
|
+
const byteLength = headByte - 160;
|
|
2002
|
+
object = this.decodeString(byteLength, 0);
|
|
2003
|
+
}
|
|
2004
|
+
} else if (headByte === 192) {
|
|
2005
|
+
object = null;
|
|
2006
|
+
} else if (headByte === 194) {
|
|
2007
|
+
object = false;
|
|
2008
|
+
} else if (headByte === 195) {
|
|
2009
|
+
object = true;
|
|
2010
|
+
} else if (headByte === 202) {
|
|
2011
|
+
object = this.readF32();
|
|
2012
|
+
} else if (headByte === 203) {
|
|
2013
|
+
object = this.readF64();
|
|
2014
|
+
} else if (headByte === 204) {
|
|
2015
|
+
object = this.readU8();
|
|
2016
|
+
} else if (headByte === 205) {
|
|
2017
|
+
object = this.readU16();
|
|
2018
|
+
} else if (headByte === 206) {
|
|
2019
|
+
object = this.readU32();
|
|
2020
|
+
} else if (headByte === 207) {
|
|
2021
|
+
if (this.useBigInt64) {
|
|
2022
|
+
object = this.readU64AsBigInt();
|
|
2023
|
+
} else {
|
|
2024
|
+
object = this.readU64();
|
|
2025
|
+
}
|
|
2026
|
+
} else if (headByte === 208) {
|
|
2027
|
+
object = this.readI8();
|
|
2028
|
+
} else if (headByte === 209) {
|
|
2029
|
+
object = this.readI16();
|
|
2030
|
+
} else if (headByte === 210) {
|
|
2031
|
+
object = this.readI32();
|
|
2032
|
+
} else if (headByte === 211) {
|
|
2033
|
+
if (this.useBigInt64) {
|
|
2034
|
+
object = this.readI64AsBigInt();
|
|
2035
|
+
} else {
|
|
2036
|
+
object = this.readI64();
|
|
2037
|
+
}
|
|
2038
|
+
} else if (headByte === 217) {
|
|
2039
|
+
const byteLength = this.lookU8();
|
|
2040
|
+
object = this.decodeString(byteLength, 1);
|
|
2041
|
+
} else if (headByte === 218) {
|
|
2042
|
+
const byteLength = this.lookU16();
|
|
2043
|
+
object = this.decodeString(byteLength, 2);
|
|
2044
|
+
} else if (headByte === 219) {
|
|
2045
|
+
const byteLength = this.lookU32();
|
|
2046
|
+
object = this.decodeString(byteLength, 4);
|
|
2047
|
+
} else if (headByte === 220) {
|
|
2048
|
+
const size = this.readU16();
|
|
2049
|
+
if (size !== 0) {
|
|
2050
|
+
this.pushArrayState(size);
|
|
2051
|
+
this.complete();
|
|
2052
|
+
continue DECODE;
|
|
2053
|
+
} else {
|
|
2054
|
+
object = [];
|
|
2055
|
+
}
|
|
2056
|
+
} else if (headByte === 221) {
|
|
2057
|
+
const size = this.readU32();
|
|
2058
|
+
if (size !== 0) {
|
|
2059
|
+
this.pushArrayState(size);
|
|
2060
|
+
this.complete();
|
|
2061
|
+
continue DECODE;
|
|
2062
|
+
} else {
|
|
2063
|
+
object = [];
|
|
2064
|
+
}
|
|
2065
|
+
} else if (headByte === 222) {
|
|
2066
|
+
const size = this.readU16();
|
|
2067
|
+
if (size !== 0) {
|
|
2068
|
+
this.pushMapState(size);
|
|
2069
|
+
this.complete();
|
|
2070
|
+
continue DECODE;
|
|
2071
|
+
} else {
|
|
2072
|
+
object = {};
|
|
2073
|
+
}
|
|
2074
|
+
} else if (headByte === 223) {
|
|
2075
|
+
const size = this.readU32();
|
|
2076
|
+
if (size !== 0) {
|
|
2077
|
+
this.pushMapState(size);
|
|
2078
|
+
this.complete();
|
|
2079
|
+
continue DECODE;
|
|
2080
|
+
} else {
|
|
2081
|
+
object = {};
|
|
2082
|
+
}
|
|
2083
|
+
} else if (headByte === 196) {
|
|
2084
|
+
const size = this.lookU8();
|
|
2085
|
+
object = this.decodeBinary(size, 1);
|
|
2086
|
+
} else if (headByte === 197) {
|
|
2087
|
+
const size = this.lookU16();
|
|
2088
|
+
object = this.decodeBinary(size, 2);
|
|
2089
|
+
} else if (headByte === 198) {
|
|
2090
|
+
const size = this.lookU32();
|
|
2091
|
+
object = this.decodeBinary(size, 4);
|
|
2092
|
+
} else if (headByte === 212) {
|
|
2093
|
+
object = this.decodeExtension(1, 0);
|
|
2094
|
+
} else if (headByte === 213) {
|
|
2095
|
+
object = this.decodeExtension(2, 0);
|
|
2096
|
+
} else if (headByte === 214) {
|
|
2097
|
+
object = this.decodeExtension(4, 0);
|
|
2098
|
+
} else if (headByte === 215) {
|
|
2099
|
+
object = this.decodeExtension(8, 0);
|
|
2100
|
+
} else if (headByte === 216) {
|
|
2101
|
+
object = this.decodeExtension(16, 0);
|
|
2102
|
+
} else if (headByte === 199) {
|
|
2103
|
+
const size = this.lookU8();
|
|
2104
|
+
object = this.decodeExtension(size, 1);
|
|
2105
|
+
} else if (headByte === 200) {
|
|
2106
|
+
const size = this.lookU16();
|
|
2107
|
+
object = this.decodeExtension(size, 2);
|
|
2108
|
+
} else if (headByte === 201) {
|
|
2109
|
+
const size = this.lookU32();
|
|
2110
|
+
object = this.decodeExtension(size, 4);
|
|
2111
|
+
} else {
|
|
2112
|
+
throw new DecodeError(`Unrecognized type byte: ${prettyByte(headByte)}`);
|
|
2113
|
+
}
|
|
2114
|
+
this.complete();
|
|
2115
|
+
const stack = this.stack;
|
|
2116
|
+
while (stack.length > 0) {
|
|
2117
|
+
const state = stack.top();
|
|
2118
|
+
if (state.type === STATE_ARRAY) {
|
|
2119
|
+
state.array[state.position] = object;
|
|
2120
|
+
state.position++;
|
|
2121
|
+
if (state.position === state.size) {
|
|
2122
|
+
object = state.array;
|
|
2123
|
+
stack.release(state);
|
|
2124
|
+
} else {
|
|
2125
|
+
continue DECODE;
|
|
2126
|
+
}
|
|
2127
|
+
} else if (state.type === STATE_MAP_KEY) {
|
|
2128
|
+
if (object === "__proto__") {
|
|
2129
|
+
throw new DecodeError("The key __proto__ is not allowed");
|
|
2130
|
+
}
|
|
2131
|
+
state.key = this.mapKeyConverter(object);
|
|
2132
|
+
state.type = STATE_MAP_VALUE;
|
|
2133
|
+
continue DECODE;
|
|
2134
|
+
} else {
|
|
2135
|
+
state.map[state.key] = object;
|
|
2136
|
+
state.readCount++;
|
|
2137
|
+
if (state.readCount === state.size) {
|
|
2138
|
+
object = state.map;
|
|
2139
|
+
stack.release(state);
|
|
2140
|
+
} else {
|
|
2141
|
+
state.key = null;
|
|
2142
|
+
state.type = STATE_MAP_KEY;
|
|
2143
|
+
continue DECODE;
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
return object;
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
readHeadByte() {
|
|
2151
|
+
if (this.headByte === HEAD_BYTE_REQUIRED) {
|
|
2152
|
+
this.headByte = this.readU8();
|
|
2153
|
+
}
|
|
2154
|
+
return this.headByte;
|
|
2155
|
+
}
|
|
2156
|
+
complete() {
|
|
2157
|
+
this.headByte = HEAD_BYTE_REQUIRED;
|
|
2158
|
+
}
|
|
2159
|
+
readArraySize() {
|
|
2160
|
+
const headByte = this.readHeadByte();
|
|
2161
|
+
switch (headByte) {
|
|
2162
|
+
case 220:
|
|
2163
|
+
return this.readU16();
|
|
2164
|
+
case 221:
|
|
2165
|
+
return this.readU32();
|
|
2166
|
+
default: {
|
|
2167
|
+
if (headByte < 160) {
|
|
2168
|
+
return headByte - 144;
|
|
2169
|
+
} else {
|
|
2170
|
+
throw new DecodeError(`Unrecognized array type byte: ${prettyByte(headByte)}`);
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
pushMapState(size) {
|
|
2176
|
+
if (size > this.maxMapLength) {
|
|
2177
|
+
throw new DecodeError(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
|
|
2178
|
+
}
|
|
2179
|
+
this.stack.pushMapState(size);
|
|
2180
|
+
}
|
|
2181
|
+
pushArrayState(size) {
|
|
2182
|
+
if (size > this.maxArrayLength) {
|
|
2183
|
+
throw new DecodeError(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
|
|
2184
|
+
}
|
|
2185
|
+
this.stack.pushArrayState(size);
|
|
2186
|
+
}
|
|
2187
|
+
decodeString(byteLength, headerOffset) {
|
|
2188
|
+
if (!this.rawStrings || this.stateIsMapKey()) {
|
|
2189
|
+
return this.decodeUtf8String(byteLength, headerOffset);
|
|
2190
|
+
}
|
|
2191
|
+
return this.decodeBinary(byteLength, headerOffset);
|
|
2192
|
+
}
|
|
2193
|
+
/**
|
|
2194
|
+
* @throws {@link RangeError}
|
|
2195
|
+
*/
|
|
2196
|
+
decodeUtf8String(byteLength, headerOffset) {
|
|
2197
|
+
if (byteLength > this.maxStrLength) {
|
|
2198
|
+
throw new DecodeError(`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`);
|
|
2199
|
+
}
|
|
2200
|
+
if (this.bytes.byteLength < this.pos + headerOffset + byteLength) {
|
|
2201
|
+
throw MORE_DATA;
|
|
2202
|
+
}
|
|
2203
|
+
const offset = this.pos + headerOffset;
|
|
2204
|
+
let object;
|
|
2205
|
+
if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) {
|
|
2206
|
+
object = this.keyDecoder.decode(this.bytes, offset, byteLength);
|
|
2207
|
+
} else {
|
|
2208
|
+
object = utf8Decode(this.bytes, offset, byteLength);
|
|
2209
|
+
}
|
|
2210
|
+
this.pos += headerOffset + byteLength;
|
|
2211
|
+
return object;
|
|
2212
|
+
}
|
|
2213
|
+
stateIsMapKey() {
|
|
2214
|
+
if (this.stack.length > 0) {
|
|
2215
|
+
const state = this.stack.top();
|
|
2216
|
+
return state.type === STATE_MAP_KEY;
|
|
2217
|
+
}
|
|
2218
|
+
return false;
|
|
2219
|
+
}
|
|
2220
|
+
/**
|
|
2221
|
+
* @throws {@link RangeError}
|
|
2222
|
+
*/
|
|
2223
|
+
decodeBinary(byteLength, headOffset) {
|
|
2224
|
+
if (byteLength > this.maxBinLength) {
|
|
2225
|
+
throw new DecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
|
|
2226
|
+
}
|
|
2227
|
+
if (!this.hasRemaining(byteLength + headOffset)) {
|
|
2228
|
+
throw MORE_DATA;
|
|
2229
|
+
}
|
|
2230
|
+
const offset = this.pos + headOffset;
|
|
2231
|
+
const object = this.bytes.subarray(offset, offset + byteLength);
|
|
2232
|
+
this.pos += headOffset + byteLength;
|
|
2233
|
+
return object;
|
|
2234
|
+
}
|
|
2235
|
+
decodeExtension(size, headOffset) {
|
|
2236
|
+
if (size > this.maxExtLength) {
|
|
2237
|
+
throw new DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
|
|
2238
|
+
}
|
|
2239
|
+
const extType = this.view.getInt8(this.pos + headOffset);
|
|
2240
|
+
const data = this.decodeBinary(
|
|
2241
|
+
size,
|
|
2242
|
+
headOffset + 1
|
|
2243
|
+
/* extType */
|
|
2244
|
+
);
|
|
2245
|
+
return this.extensionCodec.decode(data, extType, this.context);
|
|
2246
|
+
}
|
|
2247
|
+
lookU8() {
|
|
2248
|
+
return this.view.getUint8(this.pos);
|
|
2249
|
+
}
|
|
2250
|
+
lookU16() {
|
|
2251
|
+
return this.view.getUint16(this.pos);
|
|
2252
|
+
}
|
|
2253
|
+
lookU32() {
|
|
2254
|
+
return this.view.getUint32(this.pos);
|
|
2255
|
+
}
|
|
2256
|
+
readU8() {
|
|
2257
|
+
const value = this.view.getUint8(this.pos);
|
|
2258
|
+
this.pos++;
|
|
2259
|
+
return value;
|
|
2260
|
+
}
|
|
2261
|
+
readI8() {
|
|
2262
|
+
const value = this.view.getInt8(this.pos);
|
|
2263
|
+
this.pos++;
|
|
2264
|
+
return value;
|
|
2265
|
+
}
|
|
2266
|
+
readU16() {
|
|
2267
|
+
const value = this.view.getUint16(this.pos);
|
|
2268
|
+
this.pos += 2;
|
|
2269
|
+
return value;
|
|
2270
|
+
}
|
|
2271
|
+
readI16() {
|
|
2272
|
+
const value = this.view.getInt16(this.pos);
|
|
2273
|
+
this.pos += 2;
|
|
2274
|
+
return value;
|
|
2275
|
+
}
|
|
2276
|
+
readU32() {
|
|
2277
|
+
const value = this.view.getUint32(this.pos);
|
|
2278
|
+
this.pos += 4;
|
|
2279
|
+
return value;
|
|
2280
|
+
}
|
|
2281
|
+
readI32() {
|
|
2282
|
+
const value = this.view.getInt32(this.pos);
|
|
2283
|
+
this.pos += 4;
|
|
2284
|
+
return value;
|
|
2285
|
+
}
|
|
2286
|
+
readU64() {
|
|
2287
|
+
const value = getUint64(this.view, this.pos);
|
|
2288
|
+
this.pos += 8;
|
|
2289
|
+
return value;
|
|
2290
|
+
}
|
|
2291
|
+
readI64() {
|
|
2292
|
+
const value = getInt64(this.view, this.pos);
|
|
2293
|
+
this.pos += 8;
|
|
2294
|
+
return value;
|
|
2295
|
+
}
|
|
2296
|
+
readU64AsBigInt() {
|
|
2297
|
+
const value = this.view.getBigUint64(this.pos);
|
|
2298
|
+
this.pos += 8;
|
|
2299
|
+
return value;
|
|
2300
|
+
}
|
|
2301
|
+
readI64AsBigInt() {
|
|
2302
|
+
const value = this.view.getBigInt64(this.pos);
|
|
2303
|
+
this.pos += 8;
|
|
2304
|
+
return value;
|
|
2305
|
+
}
|
|
2306
|
+
readF32() {
|
|
2307
|
+
const value = this.view.getFloat32(this.pos);
|
|
2308
|
+
this.pos += 4;
|
|
2309
|
+
return value;
|
|
2310
|
+
}
|
|
2311
|
+
readF64() {
|
|
2312
|
+
const value = this.view.getFloat64(this.pos);
|
|
2313
|
+
this.pos += 8;
|
|
2314
|
+
return value;
|
|
2315
|
+
}
|
|
2316
|
+
};
|
|
2317
|
+
|
|
2318
|
+
// node_modules/@msgpack/msgpack/dist.esm/decode.mjs
|
|
2319
|
+
function decode(buffer, options) {
|
|
2320
|
+
const decoder = new Decoder(options);
|
|
2321
|
+
return decoder.decode(buffer);
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
// src/socket/index.ts
|
|
2325
|
+
var import_async_retry = __toESM(require_lib(), 1);
|
|
2326
|
+
|
|
2327
|
+
// node_modules/reconnecting-websocket/dist/reconnecting-websocket-mjs.js
|
|
2328
|
+
var extendStatics = function(d, b) {
|
|
2329
|
+
extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
|
|
2330
|
+
d2.__proto__ = b2;
|
|
2331
|
+
} || function(d2, b2) {
|
|
2332
|
+
for (var p in b2) if (b2.hasOwnProperty(p)) d2[p] = b2[p];
|
|
2333
|
+
};
|
|
2334
|
+
return extendStatics(d, b);
|
|
2335
|
+
};
|
|
2336
|
+
function __extends(d, b) {
|
|
2337
|
+
extendStatics(d, b);
|
|
2338
|
+
function __() {
|
|
2339
|
+
this.constructor = d;
|
|
2340
|
+
}
|
|
2341
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
2342
|
+
}
|
|
2343
|
+
function __values(o) {
|
|
2344
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
|
|
2345
|
+
if (m) return m.call(o);
|
|
2346
|
+
return {
|
|
2347
|
+
next: function() {
|
|
2348
|
+
if (o && i >= o.length) o = void 0;
|
|
2349
|
+
return { value: o && o[i++], done: !o };
|
|
2350
|
+
}
|
|
2351
|
+
};
|
|
2352
|
+
}
|
|
2353
|
+
function __read(o, n) {
|
|
2354
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
2355
|
+
if (!m) return o;
|
|
2356
|
+
var i = m.call(o), r, ar = [], e;
|
|
2357
|
+
try {
|
|
2358
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
2359
|
+
} catch (error) {
|
|
2360
|
+
e = { error };
|
|
2361
|
+
} finally {
|
|
2362
|
+
try {
|
|
2363
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
2364
|
+
} finally {
|
|
2365
|
+
if (e) throw e.error;
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
return ar;
|
|
2369
|
+
}
|
|
2370
|
+
function __spread() {
|
|
2371
|
+
for (var ar = [], i = 0; i < arguments.length; i++)
|
|
2372
|
+
ar = ar.concat(__read(arguments[i]));
|
|
2373
|
+
return ar;
|
|
2374
|
+
}
|
|
2375
|
+
var Event = (
|
|
2376
|
+
/** @class */
|
|
2377
|
+
/* @__PURE__ */ (function() {
|
|
2378
|
+
function Event2(type, target) {
|
|
2379
|
+
this.target = target;
|
|
2380
|
+
this.type = type;
|
|
2381
|
+
}
|
|
2382
|
+
return Event2;
|
|
2383
|
+
})()
|
|
2384
|
+
);
|
|
2385
|
+
var ErrorEvent2 = (
|
|
2386
|
+
/** @class */
|
|
2387
|
+
(function(_super) {
|
|
2388
|
+
__extends(ErrorEvent3, _super);
|
|
2389
|
+
function ErrorEvent3(error, target) {
|
|
2390
|
+
var _this = _super.call(this, "error", target) || this;
|
|
2391
|
+
_this.message = error.message;
|
|
2392
|
+
_this.error = error;
|
|
2393
|
+
return _this;
|
|
2394
|
+
}
|
|
2395
|
+
return ErrorEvent3;
|
|
2396
|
+
})(Event)
|
|
2397
|
+
);
|
|
2398
|
+
var CloseEvent = (
|
|
2399
|
+
/** @class */
|
|
2400
|
+
(function(_super) {
|
|
2401
|
+
__extends(CloseEvent2, _super);
|
|
2402
|
+
function CloseEvent2(code, reason, target) {
|
|
2403
|
+
if (code === void 0) {
|
|
2404
|
+
code = 1e3;
|
|
2405
|
+
}
|
|
2406
|
+
if (reason === void 0) {
|
|
2407
|
+
reason = "";
|
|
2408
|
+
}
|
|
2409
|
+
var _this = _super.call(this, "close", target) || this;
|
|
2410
|
+
_this.wasClean = true;
|
|
2411
|
+
_this.code = code;
|
|
2412
|
+
_this.reason = reason;
|
|
2413
|
+
return _this;
|
|
2414
|
+
}
|
|
2415
|
+
return CloseEvent2;
|
|
2416
|
+
})(Event)
|
|
2417
|
+
);
|
|
2418
|
+
var getGlobalWebSocket = function() {
|
|
2419
|
+
if (typeof WebSocket !== "undefined") {
|
|
2420
|
+
return WebSocket;
|
|
2421
|
+
}
|
|
2422
|
+
};
|
|
2423
|
+
var isWebSocket = function(w) {
|
|
2424
|
+
return typeof w !== "undefined" && !!w && w.CLOSING === 2;
|
|
2425
|
+
};
|
|
2426
|
+
var DEFAULT = {
|
|
2427
|
+
maxReconnectionDelay: 1e4,
|
|
2428
|
+
minReconnectionDelay: 1e3 + Math.random() * 4e3,
|
|
2429
|
+
minUptime: 5e3,
|
|
2430
|
+
reconnectionDelayGrowFactor: 1.3,
|
|
2431
|
+
connectionTimeout: 4e3,
|
|
2432
|
+
maxRetries: Infinity,
|
|
2433
|
+
maxEnqueuedMessages: Infinity,
|
|
2434
|
+
startClosed: false,
|
|
2435
|
+
debug: false
|
|
2436
|
+
};
|
|
2437
|
+
var ReconnectingWebSocket = (
|
|
2438
|
+
/** @class */
|
|
2439
|
+
(function() {
|
|
2440
|
+
function ReconnectingWebSocket2(url, protocols, options) {
|
|
2441
|
+
var _this = this;
|
|
2442
|
+
if (options === void 0) {
|
|
2443
|
+
options = {};
|
|
2444
|
+
}
|
|
2445
|
+
this._listeners = {
|
|
2446
|
+
error: [],
|
|
2447
|
+
message: [],
|
|
2448
|
+
open: [],
|
|
2449
|
+
close: []
|
|
2450
|
+
};
|
|
2451
|
+
this._retryCount = -1;
|
|
2452
|
+
this._shouldReconnect = true;
|
|
2453
|
+
this._connectLock = false;
|
|
2454
|
+
this._binaryType = "blob";
|
|
2455
|
+
this._closeCalled = false;
|
|
2456
|
+
this._messageQueue = [];
|
|
2457
|
+
this.onclose = null;
|
|
2458
|
+
this.onerror = null;
|
|
2459
|
+
this.onmessage = null;
|
|
2460
|
+
this.onopen = null;
|
|
2461
|
+
this._handleOpen = function(event) {
|
|
2462
|
+
_this._debug("open event");
|
|
2463
|
+
var _a = _this._options.minUptime, minUptime = _a === void 0 ? DEFAULT.minUptime : _a;
|
|
2464
|
+
clearTimeout(_this._connectTimeout);
|
|
2465
|
+
_this._uptimeTimeout = setTimeout(function() {
|
|
2466
|
+
return _this._acceptOpen();
|
|
2467
|
+
}, minUptime);
|
|
2468
|
+
_this._ws.binaryType = _this._binaryType;
|
|
2469
|
+
_this._messageQueue.forEach(function(message) {
|
|
2470
|
+
return _this._ws.send(message);
|
|
2471
|
+
});
|
|
2472
|
+
_this._messageQueue = [];
|
|
2473
|
+
if (_this.onopen) {
|
|
2474
|
+
_this.onopen(event);
|
|
2475
|
+
}
|
|
2476
|
+
_this._listeners.open.forEach(function(listener) {
|
|
2477
|
+
return _this._callEventListener(event, listener);
|
|
2478
|
+
});
|
|
2479
|
+
};
|
|
2480
|
+
this._handleMessage = function(event) {
|
|
2481
|
+
_this._debug("message event");
|
|
2482
|
+
if (_this.onmessage) {
|
|
2483
|
+
_this.onmessage(event);
|
|
2484
|
+
}
|
|
2485
|
+
_this._listeners.message.forEach(function(listener) {
|
|
2486
|
+
return _this._callEventListener(event, listener);
|
|
2487
|
+
});
|
|
2488
|
+
};
|
|
2489
|
+
this._handleError = function(event) {
|
|
2490
|
+
_this._debug("error event", event.message);
|
|
2491
|
+
_this._disconnect(void 0, event.message === "TIMEOUT" ? "timeout" : void 0);
|
|
2492
|
+
if (_this.onerror) {
|
|
2493
|
+
_this.onerror(event);
|
|
2494
|
+
}
|
|
2495
|
+
_this._debug("exec error listeners");
|
|
2496
|
+
_this._listeners.error.forEach(function(listener) {
|
|
2497
|
+
return _this._callEventListener(event, listener);
|
|
2498
|
+
});
|
|
2499
|
+
_this._connect();
|
|
2500
|
+
};
|
|
2501
|
+
this._handleClose = function(event) {
|
|
2502
|
+
_this._debug("close event");
|
|
2503
|
+
_this._clearTimeouts();
|
|
2504
|
+
if (_this._shouldReconnect) {
|
|
2505
|
+
_this._connect();
|
|
2506
|
+
}
|
|
2507
|
+
if (_this.onclose) {
|
|
2508
|
+
_this.onclose(event);
|
|
2509
|
+
}
|
|
2510
|
+
_this._listeners.close.forEach(function(listener) {
|
|
2511
|
+
return _this._callEventListener(event, listener);
|
|
2512
|
+
});
|
|
2513
|
+
};
|
|
2514
|
+
this._url = url;
|
|
2515
|
+
this._protocols = protocols;
|
|
2516
|
+
this._options = options;
|
|
2517
|
+
if (this._options.startClosed) {
|
|
2518
|
+
this._shouldReconnect = false;
|
|
2519
|
+
}
|
|
2520
|
+
this._connect();
|
|
2521
|
+
}
|
|
2522
|
+
Object.defineProperty(ReconnectingWebSocket2, "CONNECTING", {
|
|
2523
|
+
get: function() {
|
|
2524
|
+
return 0;
|
|
2525
|
+
},
|
|
2526
|
+
enumerable: true,
|
|
2527
|
+
configurable: true
|
|
2528
|
+
});
|
|
2529
|
+
Object.defineProperty(ReconnectingWebSocket2, "OPEN", {
|
|
2530
|
+
get: function() {
|
|
2531
|
+
return 1;
|
|
2532
|
+
},
|
|
2533
|
+
enumerable: true,
|
|
2534
|
+
configurable: true
|
|
2535
|
+
});
|
|
2536
|
+
Object.defineProperty(ReconnectingWebSocket2, "CLOSING", {
|
|
2537
|
+
get: function() {
|
|
2538
|
+
return 2;
|
|
2539
|
+
},
|
|
2540
|
+
enumerable: true,
|
|
2541
|
+
configurable: true
|
|
2542
|
+
});
|
|
2543
|
+
Object.defineProperty(ReconnectingWebSocket2, "CLOSED", {
|
|
2544
|
+
get: function() {
|
|
2545
|
+
return 3;
|
|
2546
|
+
},
|
|
2547
|
+
enumerable: true,
|
|
2548
|
+
configurable: true
|
|
2549
|
+
});
|
|
2550
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "CONNECTING", {
|
|
2551
|
+
get: function() {
|
|
2552
|
+
return ReconnectingWebSocket2.CONNECTING;
|
|
2553
|
+
},
|
|
2554
|
+
enumerable: true,
|
|
2555
|
+
configurable: true
|
|
2556
|
+
});
|
|
2557
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "OPEN", {
|
|
2558
|
+
get: function() {
|
|
2559
|
+
return ReconnectingWebSocket2.OPEN;
|
|
2560
|
+
},
|
|
2561
|
+
enumerable: true,
|
|
2562
|
+
configurable: true
|
|
2563
|
+
});
|
|
2564
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "CLOSING", {
|
|
2565
|
+
get: function() {
|
|
2566
|
+
return ReconnectingWebSocket2.CLOSING;
|
|
2567
|
+
},
|
|
2568
|
+
enumerable: true,
|
|
2569
|
+
configurable: true
|
|
2570
|
+
});
|
|
2571
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "CLOSED", {
|
|
2572
|
+
get: function() {
|
|
2573
|
+
return ReconnectingWebSocket2.CLOSED;
|
|
2574
|
+
},
|
|
2575
|
+
enumerable: true,
|
|
2576
|
+
configurable: true
|
|
2577
|
+
});
|
|
2578
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "binaryType", {
|
|
2579
|
+
get: function() {
|
|
2580
|
+
return this._ws ? this._ws.binaryType : this._binaryType;
|
|
2581
|
+
},
|
|
2582
|
+
set: function(value) {
|
|
2583
|
+
this._binaryType = value;
|
|
2584
|
+
if (this._ws) {
|
|
2585
|
+
this._ws.binaryType = value;
|
|
2586
|
+
}
|
|
2587
|
+
},
|
|
2588
|
+
enumerable: true,
|
|
2589
|
+
configurable: true
|
|
2590
|
+
});
|
|
2591
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "retryCount", {
|
|
2592
|
+
/**
|
|
2593
|
+
* Returns the number or connection retries
|
|
2594
|
+
*/
|
|
2595
|
+
get: function() {
|
|
2596
|
+
return Math.max(this._retryCount, 0);
|
|
2597
|
+
},
|
|
2598
|
+
enumerable: true,
|
|
2599
|
+
configurable: true
|
|
2600
|
+
});
|
|
2601
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "bufferedAmount", {
|
|
2602
|
+
/**
|
|
2603
|
+
* The number of bytes of data that have been queued using calls to send() but not yet
|
|
2604
|
+
* transmitted to the network. This value resets to zero once all queued data has been sent.
|
|
2605
|
+
* This value does not reset to zero when the connection is closed; if you keep calling send(),
|
|
2606
|
+
* this will continue to climb. Read only
|
|
2607
|
+
*/
|
|
2608
|
+
get: function() {
|
|
2609
|
+
var bytes = this._messageQueue.reduce(function(acc, message) {
|
|
2610
|
+
if (typeof message === "string") {
|
|
2611
|
+
acc += message.length;
|
|
2612
|
+
} else if (message instanceof Blob) {
|
|
2613
|
+
acc += message.size;
|
|
2614
|
+
} else {
|
|
2615
|
+
acc += message.byteLength;
|
|
2616
|
+
}
|
|
2617
|
+
return acc;
|
|
2618
|
+
}, 0);
|
|
2619
|
+
return bytes + (this._ws ? this._ws.bufferedAmount : 0);
|
|
2620
|
+
},
|
|
2621
|
+
enumerable: true,
|
|
2622
|
+
configurable: true
|
|
2623
|
+
});
|
|
2624
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "extensions", {
|
|
2625
|
+
/**
|
|
2626
|
+
* The extensions selected by the server. This is currently only the empty string or a list of
|
|
2627
|
+
* extensions as negotiated by the connection
|
|
2628
|
+
*/
|
|
2629
|
+
get: function() {
|
|
2630
|
+
return this._ws ? this._ws.extensions : "";
|
|
2631
|
+
},
|
|
2632
|
+
enumerable: true,
|
|
2633
|
+
configurable: true
|
|
2634
|
+
});
|
|
2635
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "protocol", {
|
|
2636
|
+
/**
|
|
2637
|
+
* A string indicating the name of the sub-protocol the server selected;
|
|
2638
|
+
* this will be one of the strings specified in the protocols parameter when creating the
|
|
2639
|
+
* WebSocket object
|
|
2640
|
+
*/
|
|
2641
|
+
get: function() {
|
|
2642
|
+
return this._ws ? this._ws.protocol : "";
|
|
2643
|
+
},
|
|
2644
|
+
enumerable: true,
|
|
2645
|
+
configurable: true
|
|
2646
|
+
});
|
|
2647
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "readyState", {
|
|
2648
|
+
/**
|
|
2649
|
+
* The current state of the connection; this is one of the Ready state constants
|
|
2650
|
+
*/
|
|
2651
|
+
get: function() {
|
|
2652
|
+
if (this._ws) {
|
|
2653
|
+
return this._ws.readyState;
|
|
2654
|
+
}
|
|
2655
|
+
return this._options.startClosed ? ReconnectingWebSocket2.CLOSED : ReconnectingWebSocket2.CONNECTING;
|
|
2656
|
+
},
|
|
2657
|
+
enumerable: true,
|
|
2658
|
+
configurable: true
|
|
2659
|
+
});
|
|
2660
|
+
Object.defineProperty(ReconnectingWebSocket2.prototype, "url", {
|
|
2661
|
+
/**
|
|
2662
|
+
* The URL as resolved by the constructor
|
|
2663
|
+
*/
|
|
2664
|
+
get: function() {
|
|
2665
|
+
return this._ws ? this._ws.url : "";
|
|
2666
|
+
},
|
|
2667
|
+
enumerable: true,
|
|
2668
|
+
configurable: true
|
|
2669
|
+
});
|
|
2670
|
+
ReconnectingWebSocket2.prototype.close = function(code, reason) {
|
|
2671
|
+
if (code === void 0) {
|
|
2672
|
+
code = 1e3;
|
|
2673
|
+
}
|
|
2674
|
+
this._closeCalled = true;
|
|
2675
|
+
this._shouldReconnect = false;
|
|
2676
|
+
this._clearTimeouts();
|
|
2677
|
+
if (!this._ws) {
|
|
2678
|
+
this._debug("close enqueued: no ws instance");
|
|
2679
|
+
return;
|
|
2680
|
+
}
|
|
2681
|
+
if (this._ws.readyState === this.CLOSED) {
|
|
2682
|
+
this._debug("close: already closed");
|
|
2683
|
+
return;
|
|
2684
|
+
}
|
|
2685
|
+
this._ws.close(code, reason);
|
|
2686
|
+
};
|
|
2687
|
+
ReconnectingWebSocket2.prototype.reconnect = function(code, reason) {
|
|
2688
|
+
this._shouldReconnect = true;
|
|
2689
|
+
this._closeCalled = false;
|
|
2690
|
+
this._retryCount = -1;
|
|
2691
|
+
if (!this._ws || this._ws.readyState === this.CLOSED) {
|
|
2692
|
+
this._connect();
|
|
2693
|
+
} else {
|
|
2694
|
+
this._disconnect(code, reason);
|
|
2695
|
+
this._connect();
|
|
2696
|
+
}
|
|
2697
|
+
};
|
|
2698
|
+
ReconnectingWebSocket2.prototype.send = function(data) {
|
|
2699
|
+
if (this._ws && this._ws.readyState === this.OPEN) {
|
|
2700
|
+
this._debug("send", data);
|
|
2701
|
+
this._ws.send(data);
|
|
2702
|
+
} else {
|
|
2703
|
+
var _a = this._options.maxEnqueuedMessages, maxEnqueuedMessages = _a === void 0 ? DEFAULT.maxEnqueuedMessages : _a;
|
|
2704
|
+
if (this._messageQueue.length < maxEnqueuedMessages) {
|
|
2705
|
+
this._debug("enqueue", data);
|
|
2706
|
+
this._messageQueue.push(data);
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
};
|
|
2710
|
+
ReconnectingWebSocket2.prototype.addEventListener = function(type, listener) {
|
|
2711
|
+
if (this._listeners[type]) {
|
|
2712
|
+
this._listeners[type].push(listener);
|
|
2713
|
+
}
|
|
2714
|
+
};
|
|
2715
|
+
ReconnectingWebSocket2.prototype.dispatchEvent = function(event) {
|
|
2716
|
+
var e_1, _a;
|
|
2717
|
+
var listeners = this._listeners[event.type];
|
|
2718
|
+
if (listeners) {
|
|
2719
|
+
try {
|
|
2720
|
+
for (var listeners_1 = __values(listeners), listeners_1_1 = listeners_1.next(); !listeners_1_1.done; listeners_1_1 = listeners_1.next()) {
|
|
2721
|
+
var listener = listeners_1_1.value;
|
|
2722
|
+
this._callEventListener(event, listener);
|
|
2723
|
+
}
|
|
2724
|
+
} catch (e_1_1) {
|
|
2725
|
+
e_1 = { error: e_1_1 };
|
|
2726
|
+
} finally {
|
|
2727
|
+
try {
|
|
2728
|
+
if (listeners_1_1 && !listeners_1_1.done && (_a = listeners_1.return)) _a.call(listeners_1);
|
|
2729
|
+
} finally {
|
|
2730
|
+
if (e_1) throw e_1.error;
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
}
|
|
2734
|
+
return true;
|
|
2735
|
+
};
|
|
2736
|
+
ReconnectingWebSocket2.prototype.removeEventListener = function(type, listener) {
|
|
2737
|
+
if (this._listeners[type]) {
|
|
2738
|
+
this._listeners[type] = this._listeners[type].filter(function(l) {
|
|
2739
|
+
return l !== listener;
|
|
2740
|
+
});
|
|
2741
|
+
}
|
|
2742
|
+
};
|
|
2743
|
+
ReconnectingWebSocket2.prototype._debug = function() {
|
|
2744
|
+
var args = [];
|
|
2745
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2746
|
+
args[_i] = arguments[_i];
|
|
2747
|
+
}
|
|
2748
|
+
if (this._options.debug) {
|
|
2749
|
+
console.log.apply(console, __spread(["RWS>"], args));
|
|
2750
|
+
}
|
|
2751
|
+
};
|
|
2752
|
+
ReconnectingWebSocket2.prototype._getNextDelay = function() {
|
|
2753
|
+
var _a = this._options, _b = _a.reconnectionDelayGrowFactor, reconnectionDelayGrowFactor = _b === void 0 ? DEFAULT.reconnectionDelayGrowFactor : _b, _c = _a.minReconnectionDelay, minReconnectionDelay = _c === void 0 ? DEFAULT.minReconnectionDelay : _c, _d = _a.maxReconnectionDelay, maxReconnectionDelay = _d === void 0 ? DEFAULT.maxReconnectionDelay : _d;
|
|
2754
|
+
var delay = 0;
|
|
2755
|
+
if (this._retryCount > 0) {
|
|
2756
|
+
delay = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1);
|
|
2757
|
+
if (delay > maxReconnectionDelay) {
|
|
2758
|
+
delay = maxReconnectionDelay;
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
this._debug("next delay", delay);
|
|
2762
|
+
return delay;
|
|
2763
|
+
};
|
|
2764
|
+
ReconnectingWebSocket2.prototype._wait = function() {
|
|
2765
|
+
var _this = this;
|
|
2766
|
+
return new Promise(function(resolve) {
|
|
2767
|
+
setTimeout(resolve, _this._getNextDelay());
|
|
2768
|
+
});
|
|
2769
|
+
};
|
|
2770
|
+
ReconnectingWebSocket2.prototype._getNextUrl = function(urlProvider) {
|
|
2771
|
+
if (typeof urlProvider === "string") {
|
|
2772
|
+
return Promise.resolve(urlProvider);
|
|
2773
|
+
}
|
|
2774
|
+
if (typeof urlProvider === "function") {
|
|
2775
|
+
var url = urlProvider();
|
|
2776
|
+
if (typeof url === "string") {
|
|
2777
|
+
return Promise.resolve(url);
|
|
2778
|
+
}
|
|
2779
|
+
if (!!url.then) {
|
|
2780
|
+
return url;
|
|
2781
|
+
}
|
|
2782
|
+
}
|
|
2783
|
+
throw Error("Invalid URL");
|
|
2784
|
+
};
|
|
2785
|
+
ReconnectingWebSocket2.prototype._connect = function() {
|
|
2786
|
+
var _this = this;
|
|
2787
|
+
if (this._connectLock || !this._shouldReconnect) {
|
|
2788
|
+
return;
|
|
2789
|
+
}
|
|
2790
|
+
this._connectLock = true;
|
|
2791
|
+
var _a = this._options, _b = _a.maxRetries, maxRetries = _b === void 0 ? DEFAULT.maxRetries : _b, _c = _a.connectionTimeout, connectionTimeout = _c === void 0 ? DEFAULT.connectionTimeout : _c, _d = _a.WebSocket, WebSocket2 = _d === void 0 ? getGlobalWebSocket() : _d;
|
|
2792
|
+
if (this._retryCount >= maxRetries) {
|
|
2793
|
+
this._debug("max retries reached", this._retryCount, ">=", maxRetries);
|
|
2794
|
+
return;
|
|
2795
|
+
}
|
|
2796
|
+
this._retryCount++;
|
|
2797
|
+
this._debug("connect", this._retryCount);
|
|
2798
|
+
this._removeListeners();
|
|
2799
|
+
if (!isWebSocket(WebSocket2)) {
|
|
2800
|
+
throw Error("No valid WebSocket class provided");
|
|
2801
|
+
}
|
|
2802
|
+
this._wait().then(function() {
|
|
2803
|
+
return _this._getNextUrl(_this._url);
|
|
2804
|
+
}).then(function(url) {
|
|
2805
|
+
if (_this._closeCalled) {
|
|
2806
|
+
return;
|
|
2807
|
+
}
|
|
2808
|
+
_this._debug("connect", { url, protocols: _this._protocols });
|
|
2809
|
+
_this._ws = _this._protocols ? new WebSocket2(url, _this._protocols) : new WebSocket2(url);
|
|
2810
|
+
_this._ws.binaryType = _this._binaryType;
|
|
2811
|
+
_this._connectLock = false;
|
|
2812
|
+
_this._addListeners();
|
|
2813
|
+
_this._connectTimeout = setTimeout(function() {
|
|
2814
|
+
return _this._handleTimeout();
|
|
2815
|
+
}, connectionTimeout);
|
|
2816
|
+
});
|
|
2817
|
+
};
|
|
2818
|
+
ReconnectingWebSocket2.prototype._handleTimeout = function() {
|
|
2819
|
+
this._debug("timeout event");
|
|
2820
|
+
this._handleError(new ErrorEvent2(Error("TIMEOUT"), this));
|
|
2821
|
+
};
|
|
2822
|
+
ReconnectingWebSocket2.prototype._disconnect = function(code, reason) {
|
|
2823
|
+
if (code === void 0) {
|
|
2824
|
+
code = 1e3;
|
|
2825
|
+
}
|
|
2826
|
+
this._clearTimeouts();
|
|
2827
|
+
if (!this._ws) {
|
|
2828
|
+
return;
|
|
2829
|
+
}
|
|
2830
|
+
this._removeListeners();
|
|
2831
|
+
try {
|
|
2832
|
+
this._ws.close(code, reason);
|
|
2833
|
+
this._handleClose(new CloseEvent(code, reason, this));
|
|
2834
|
+
} catch (error) {
|
|
2835
|
+
}
|
|
2836
|
+
};
|
|
2837
|
+
ReconnectingWebSocket2.prototype._acceptOpen = function() {
|
|
2838
|
+
this._debug("accept open");
|
|
2839
|
+
this._retryCount = 0;
|
|
2840
|
+
};
|
|
2841
|
+
ReconnectingWebSocket2.prototype._callEventListener = function(event, listener) {
|
|
2842
|
+
if ("handleEvent" in listener) {
|
|
2843
|
+
listener.handleEvent(event);
|
|
2844
|
+
} else {
|
|
2845
|
+
listener(event);
|
|
2846
|
+
}
|
|
2847
|
+
};
|
|
2848
|
+
ReconnectingWebSocket2.prototype._removeListeners = function() {
|
|
2849
|
+
if (!this._ws) {
|
|
2850
|
+
return;
|
|
2851
|
+
}
|
|
2852
|
+
this._debug("removeListeners");
|
|
2853
|
+
this._ws.removeEventListener("open", this._handleOpen);
|
|
2854
|
+
this._ws.removeEventListener("close", this._handleClose);
|
|
2855
|
+
this._ws.removeEventListener("message", this._handleMessage);
|
|
2856
|
+
this._ws.removeEventListener("error", this._handleError);
|
|
2857
|
+
};
|
|
2858
|
+
ReconnectingWebSocket2.prototype._addListeners = function() {
|
|
2859
|
+
if (!this._ws) {
|
|
2860
|
+
return;
|
|
2861
|
+
}
|
|
2862
|
+
this._debug("addListeners");
|
|
2863
|
+
this._ws.addEventListener("open", this._handleOpen);
|
|
2864
|
+
this._ws.addEventListener("close", this._handleClose);
|
|
2865
|
+
this._ws.addEventListener("message", this._handleMessage);
|
|
2866
|
+
this._ws.addEventListener("error", this._handleError);
|
|
2867
|
+
};
|
|
2868
|
+
ReconnectingWebSocket2.prototype._clearTimeouts = function() {
|
|
2869
|
+
clearTimeout(this._connectTimeout);
|
|
2870
|
+
clearTimeout(this._uptimeTimeout);
|
|
2871
|
+
};
|
|
2872
|
+
return ReconnectingWebSocket2;
|
|
2873
|
+
})()
|
|
2874
|
+
);
|
|
2875
|
+
var reconnecting_websocket_mjs_default = ReconnectingWebSocket;
|
|
2876
|
+
|
|
2877
|
+
// src/utils/promise.ts
|
|
2878
|
+
var PromiseTimeoutError = class extends Error {
|
|
2879
|
+
constructor(message, time) {
|
|
2880
|
+
super(message);
|
|
2881
|
+
this.time = time;
|
|
2882
|
+
}
|
|
2883
|
+
};
|
|
2884
|
+
var timeout = (prom, time) => {
|
|
2885
|
+
let timer;
|
|
2886
|
+
return Promise.race([
|
|
2887
|
+
prom,
|
|
2888
|
+
new Promise(
|
|
2889
|
+
(_r, rej) => timer = setTimeout(rej, time, new PromiseTimeoutError("Timeout before promise can resolve", time))
|
|
2890
|
+
)
|
|
2891
|
+
]).finally(() => clearTimeout(timer));
|
|
2892
|
+
};
|
|
2893
|
+
|
|
2894
|
+
// node_modules/isomorphic-ws/browser.js
|
|
2895
|
+
var ws = null;
|
|
2896
|
+
if (typeof WebSocket !== "undefined") {
|
|
2897
|
+
ws = WebSocket;
|
|
2898
|
+
} else if (typeof MozWebSocket !== "undefined") {
|
|
2899
|
+
ws = MozWebSocket;
|
|
2900
|
+
} else if (typeof global !== "undefined") {
|
|
2901
|
+
ws = global.WebSocket || global.MozWebSocket;
|
|
2902
|
+
} else if (typeof window !== "undefined") {
|
|
2903
|
+
ws = window.WebSocket || window.MozWebSocket;
|
|
2904
|
+
} else if (typeof self !== "undefined") {
|
|
2905
|
+
ws = self.WebSocket || self.MozWebSocket;
|
|
2906
|
+
}
|
|
2907
|
+
var browser_default = ws;
|
|
2908
|
+
|
|
2909
|
+
// src/utils/disposable.ts
|
|
2910
|
+
var NamedDisposable = class {
|
|
2911
|
+
constructor() {
|
|
2912
|
+
this.disposables = /* @__PURE__ */ new Map();
|
|
2913
|
+
}
|
|
2914
|
+
add(key, disposable) {
|
|
2915
|
+
if (this.disposables.has(key)) {
|
|
2916
|
+
console.warn(`Disposable with key "${key}" already exists. Disposing the old one.`);
|
|
2917
|
+
this.disposables.get(key)?.dispose();
|
|
2918
|
+
}
|
|
2919
|
+
if (typeof disposable === "function") {
|
|
2920
|
+
disposable = disposable();
|
|
2921
|
+
}
|
|
2922
|
+
this.disposables.set(key, disposable);
|
|
2923
|
+
}
|
|
2924
|
+
dispose() {
|
|
2925
|
+
this.disposables.forEach((disposable) => disposable.dispose());
|
|
2926
|
+
this.disposables.clear();
|
|
2927
|
+
}
|
|
2928
|
+
};
|
|
2929
|
+
|
|
2930
|
+
// src/socket/index.ts
|
|
2931
|
+
var InvalidMessageError = class extends Error {
|
|
2932
|
+
constructor(message, data) {
|
|
2933
|
+
super(message);
|
|
2934
|
+
this.data = data;
|
|
2935
|
+
this.name = "InvalidMessageError";
|
|
2936
|
+
}
|
|
2937
|
+
};
|
|
2938
|
+
var InvalidConfigurationError = class extends Error {
|
|
2939
|
+
constructor(message) {
|
|
2940
|
+
super(message);
|
|
2941
|
+
this.name = "InvalidConfigurationError";
|
|
2942
|
+
}
|
|
2943
|
+
};
|
|
2944
|
+
var _Transport_instances, connect_fn, startPeriodicPing_fn;
|
|
2945
|
+
var Transport = class {
|
|
2946
|
+
constructor(url, eventEmitter, options = {}) {
|
|
2947
|
+
this.eventEmitter = eventEmitter;
|
|
2948
|
+
this.options = options;
|
|
2949
|
+
__privateAdd(this, _Transport_instances);
|
|
2950
|
+
this.clientId = "";
|
|
2951
|
+
this.closed = false;
|
|
2952
|
+
this.disposables = new NamedDisposable();
|
|
2953
|
+
this.connectPromise = null;
|
|
2954
|
+
// Connection health monitoring
|
|
2955
|
+
this.connectionStats = {
|
|
2956
|
+
connectTime: 0,
|
|
2957
|
+
reconnectCount: 0,
|
|
2958
|
+
lastPingTime: 0,
|
|
2959
|
+
lastPongTime: 0,
|
|
2960
|
+
avgResponseTime: 0,
|
|
2961
|
+
totalMessages: 0,
|
|
2962
|
+
totalErrors: 0,
|
|
2963
|
+
connectionStartTime: 0
|
|
2964
|
+
};
|
|
2965
|
+
// Message queue for disconnection handling
|
|
2966
|
+
this.messageQueue = [];
|
|
2967
|
+
this.MAX_QUEUE_SIZE = 100;
|
|
2968
|
+
this.QUEUE_TIMEOUT = 3e4;
|
|
2969
|
+
// 30 seconds
|
|
2970
|
+
// Rate limiting
|
|
2971
|
+
this.rateLimiter = {
|
|
2972
|
+
requests: [],
|
|
2973
|
+
maxRequests: 50,
|
|
2974
|
+
windowMs: 1e3
|
|
2975
|
+
// 1 second
|
|
2976
|
+
};
|
|
2977
|
+
this.validateConfiguration(options);
|
|
2978
|
+
this.url = new URL(url);
|
|
2979
|
+
this.url.searchParams.set("sdk_version", "0.0.1");
|
|
2980
|
+
this.PING_INTERVAL = options.pingInterval ?? 3e4;
|
|
2981
|
+
this.connectionStats.connectionStartTime = Date.now();
|
|
2982
|
+
const startClosed = options.startClosed !== false;
|
|
2983
|
+
this.rws = new reconnecting_websocket_mjs_default(this.url.toString(), [], {
|
|
2984
|
+
WebSocket: globalThis.WebSocket ?? browser_default,
|
|
2985
|
+
connectionTimeout: options.connectionTimeout ?? 1e3,
|
|
2986
|
+
maxReconnectionDelay: 2e3,
|
|
2987
|
+
minReconnectionDelay: 200,
|
|
2988
|
+
maxEnqueuedMessages: 0,
|
|
2989
|
+
maxRetries: options.maxRetries ?? 50,
|
|
2990
|
+
startClosed
|
|
2991
|
+
});
|
|
2992
|
+
this.log("debug", "Transport initialized", {
|
|
2993
|
+
url: this.url.toString(),
|
|
2994
|
+
options
|
|
2995
|
+
});
|
|
2996
|
+
this.registerWatchers();
|
|
2997
|
+
this.setupConnectionHealthMonitoring();
|
|
2998
|
+
this.startPeriodicMaintenance();
|
|
2999
|
+
}
|
|
3000
|
+
/**
|
|
3001
|
+
* Validate configuration options
|
|
3002
|
+
*/
|
|
3003
|
+
validateConfiguration(options) {
|
|
3004
|
+
if (options.pingInterval !== void 0 && (options.pingInterval < 1e3 || options.pingInterval > 3e5)) {
|
|
3005
|
+
throw new InvalidConfigurationError("pingInterval must be between 1000ms and 300000ms");
|
|
3006
|
+
}
|
|
3007
|
+
if (options.connectionTimeout !== void 0 && (options.connectionTimeout < 100 || options.connectionTimeout > 3e4)) {
|
|
3008
|
+
throw new InvalidConfigurationError("connectionTimeout must be between 100ms and 30000ms");
|
|
3009
|
+
}
|
|
3010
|
+
if (options.maxRetries !== void 0 && (options.maxRetries < 0 || options.maxRetries > 100)) {
|
|
3011
|
+
throw new InvalidConfigurationError("maxRetries must be between 0 and 100");
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
3014
|
+
/**
|
|
3015
|
+
* Internal logging utility for debugging
|
|
3016
|
+
*/
|
|
3017
|
+
log(level, message, data) {
|
|
3018
|
+
if (this.options.debug) {
|
|
3019
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
3020
|
+
const logData = data ? JSON.stringify(data, null, 2) : "";
|
|
3021
|
+
console[level](`[Transport ${timestamp}] ${message}${logData ? "\n" + logData : ""}`);
|
|
3022
|
+
}
|
|
3023
|
+
}
|
|
3024
|
+
id() {
|
|
3025
|
+
return this.clientId;
|
|
3026
|
+
}
|
|
3027
|
+
async registerWatchers() {
|
|
3028
|
+
const onMessage = (ev) => {
|
|
3029
|
+
if (!(ev.data instanceof Blob)) {
|
|
3030
|
+
throw new Error("Unexpected message type: " + typeof ev.data);
|
|
3031
|
+
}
|
|
3032
|
+
ev.data.arrayBuffer().then((buffer) => {
|
|
3033
|
+
this.handleRawMessage(decode(buffer));
|
|
3034
|
+
});
|
|
3035
|
+
};
|
|
3036
|
+
this.rws.addEventListener("message", onMessage);
|
|
3037
|
+
this.disposables.add("message", {
|
|
3038
|
+
dispose: () => {
|
|
3039
|
+
this.rws.removeEventListener("message", onMessage);
|
|
3040
|
+
}
|
|
3041
|
+
});
|
|
3042
|
+
}
|
|
3043
|
+
async handleRawMessage(ev) {
|
|
3044
|
+
if (typeof ev !== "object" || ev === null) {
|
|
3045
|
+
this.log("debug", "Received invalid message format", { ev });
|
|
3046
|
+
return;
|
|
3047
|
+
}
|
|
3048
|
+
try {
|
|
3049
|
+
const { data, event, as } = ev;
|
|
3050
|
+
if (!event || typeof event !== "string") {
|
|
3051
|
+
throw new InvalidMessageError("Message missing event field", ev);
|
|
3052
|
+
}
|
|
3053
|
+
this.log("debug", "Processing message", { event, hasData: !!data });
|
|
3054
|
+
if (event === "App.Actions.GetClientId" /* ClientId */) {
|
|
3055
|
+
this.clientId = data.id;
|
|
3056
|
+
this.log("info", "Client ID received", { clientId: this.clientId });
|
|
3057
|
+
return;
|
|
3058
|
+
}
|
|
3059
|
+
if (event === "Events.BootError" /* BootError */) {
|
|
3060
|
+
this.log("error", "Boot error received", { data });
|
|
3061
|
+
return;
|
|
3062
|
+
}
|
|
3063
|
+
if (event === "response" /* Response */) {
|
|
3064
|
+
const { responseEvent, data: responseData } = data;
|
|
3065
|
+
if (!responseEvent) {
|
|
3066
|
+
throw new InvalidMessageError("Response message missing responseEvent", ev);
|
|
3067
|
+
}
|
|
3068
|
+
this.log("debug", "Response message received", { responseEvent });
|
|
3069
|
+
await this.handleMessage(responseEvent, responseData);
|
|
3070
|
+
return;
|
|
3071
|
+
}
|
|
3072
|
+
if (event === "error" /* Error */) {
|
|
3073
|
+
const { errorEvent, data: responseData } = data;
|
|
3074
|
+
if (!errorEvent) {
|
|
3075
|
+
throw new InvalidMessageError("Error message missing errorEvent", ev);
|
|
3076
|
+
}
|
|
3077
|
+
this.log("debug", "Error message received", {
|
|
3078
|
+
errorEvent,
|
|
3079
|
+
errorData: responseData
|
|
3080
|
+
});
|
|
3081
|
+
await this.handleMessage(errorEvent, responseData);
|
|
3082
|
+
return;
|
|
3083
|
+
}
|
|
3084
|
+
await this.handleMessage(event, data, as);
|
|
3085
|
+
} catch (e) {
|
|
3086
|
+
this.connectionStats.totalErrors++;
|
|
3087
|
+
if (e instanceof InvalidMessageError) {
|
|
3088
|
+
this.log("error", "Invalid message format", {
|
|
3089
|
+
error: e.message,
|
|
3090
|
+
data: e.data,
|
|
3091
|
+
totalErrors: this.connectionStats.totalErrors
|
|
3092
|
+
});
|
|
3093
|
+
} else {
|
|
3094
|
+
this.log("error", "Failed to parse message", {
|
|
3095
|
+
ev,
|
|
3096
|
+
error: e instanceof Error ? e.message : String(e),
|
|
3097
|
+
totalErrors: this.connectionStats.totalErrors
|
|
3098
|
+
});
|
|
3099
|
+
}
|
|
3100
|
+
this.eventEmitter.emit("transport.error", {
|
|
3101
|
+
type: "message_parse_error",
|
|
3102
|
+
error: e,
|
|
3103
|
+
rawMessage: ev,
|
|
3104
|
+
timestamp: Date.now()
|
|
3105
|
+
});
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
3108
|
+
async handleMessage(event, data, as) {
|
|
3109
|
+
if (event === "App.Actions.GetClientId" /* ClientId */) {
|
|
3110
|
+
this.clientId = data.id;
|
|
3111
|
+
this.eventEmitter.emit(event, data.id);
|
|
3112
|
+
return;
|
|
3113
|
+
}
|
|
3114
|
+
event && this.eventEmitter.emit(as || event, data);
|
|
3115
|
+
}
|
|
3116
|
+
listen(event, listener, _context) {
|
|
3117
|
+
return this.eventEmitter.listen(event, listener);
|
|
3118
|
+
}
|
|
3119
|
+
removeListener(event, listener) {
|
|
3120
|
+
this.eventEmitter.removeListener(event, listener);
|
|
3121
|
+
}
|
|
3122
|
+
listenOnce(event, listener, context) {
|
|
3123
|
+
this.eventEmitter.once(event, listener, context);
|
|
3124
|
+
}
|
|
3125
|
+
emit(event, ...data) {
|
|
3126
|
+
this.eventEmitter.emit(event, ...data);
|
|
3127
|
+
}
|
|
3128
|
+
get isConnected() {
|
|
3129
|
+
return this.status === "OPEN";
|
|
3130
|
+
}
|
|
3131
|
+
get isConnecting() {
|
|
3132
|
+
return this.status === "CONNECTING";
|
|
3133
|
+
}
|
|
3134
|
+
get isDisconnected() {
|
|
3135
|
+
return this.status === "CLOSED";
|
|
3136
|
+
}
|
|
3137
|
+
get isClosed() {
|
|
3138
|
+
return this.closed;
|
|
3139
|
+
}
|
|
3140
|
+
get status() {
|
|
3141
|
+
return {
|
|
3142
|
+
0: "CONNECTING",
|
|
3143
|
+
1: "OPEN",
|
|
3144
|
+
2: "CLOSING",
|
|
3145
|
+
3: "CLOSED"
|
|
3146
|
+
}[this.rws.readyState];
|
|
3147
|
+
}
|
|
3148
|
+
async call(action, data = {}, options = {}) {
|
|
3149
|
+
if (this.isRateLimited()) {
|
|
3150
|
+
throw new RateLimitError("Rate limit exceeded - too many requests");
|
|
3151
|
+
}
|
|
3152
|
+
this.clearOldQueuedMessages();
|
|
3153
|
+
const responseEvent = options.responseEvent || `${action}_${nanoid()}_response`;
|
|
3154
|
+
const errorEvent = `${responseEvent}_error`;
|
|
3155
|
+
let closeHandler;
|
|
3156
|
+
const removeListeners = () => {
|
|
3157
|
+
if (closeHandler) {
|
|
3158
|
+
this.rws.removeEventListener("close", closeHandler);
|
|
3159
|
+
this.rws.removeEventListener("error", closeHandler);
|
|
3160
|
+
}
|
|
3161
|
+
this.eventEmitter.removeListener(responseEvent);
|
|
3162
|
+
this.eventEmitter.removeListener(errorEvent);
|
|
3163
|
+
};
|
|
3164
|
+
const handler = async (resolve, reject) => {
|
|
3165
|
+
const abortError = new DOMException("Request aborted", "AbortError");
|
|
3166
|
+
if (options.abortSignal?.aborted) {
|
|
3167
|
+
reject(abortError);
|
|
3168
|
+
}
|
|
3169
|
+
if (options.abortSignal) {
|
|
3170
|
+
options.abortSignal.addEventListener("abort", () => {
|
|
3171
|
+
reject(abortError);
|
|
3172
|
+
});
|
|
3173
|
+
}
|
|
3174
|
+
if (!this.isConnected && !this.isClosed) {
|
|
3175
|
+
this.log("debug", "Connection not available, queuing message", {
|
|
3176
|
+
action
|
|
3177
|
+
});
|
|
3178
|
+
if (this.messageQueue.length >= this.MAX_QUEUE_SIZE) {
|
|
3179
|
+
const oldestMessage = this.messageQueue.shift();
|
|
3180
|
+
if (oldestMessage) {
|
|
3181
|
+
oldestMessage.reject(new Error("Message queue full, oldest message dropped"));
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
this.messageQueue.push({
|
|
3185
|
+
action,
|
|
3186
|
+
data,
|
|
3187
|
+
options,
|
|
3188
|
+
resolve,
|
|
3189
|
+
reject,
|
|
3190
|
+
timestamp: Date.now()
|
|
3191
|
+
});
|
|
3192
|
+
return;
|
|
3193
|
+
}
|
|
3194
|
+
const startTime = Date.now();
|
|
3195
|
+
this.listenOnce(responseEvent, (response) => {
|
|
3196
|
+
const responseTime = Date.now() - startTime;
|
|
3197
|
+
this.connectionStats.avgResponseTime = (this.connectionStats.avgResponseTime + responseTime) / 2;
|
|
3198
|
+
this.connectionStats.totalMessages++;
|
|
3199
|
+
this.log("debug", "Message response received", {
|
|
3200
|
+
action,
|
|
3201
|
+
responseTime,
|
|
3202
|
+
avgResponseTime: this.connectionStats.avgResponseTime
|
|
3203
|
+
});
|
|
3204
|
+
resolve(response);
|
|
3205
|
+
});
|
|
3206
|
+
this.listenOnce(errorEvent, (e) => {
|
|
3207
|
+
this.connectionStats.totalErrors++;
|
|
3208
|
+
this.log("error", "Message error received", { action, error: e });
|
|
3209
|
+
reject(new ErrorEvent(e.code, e.message, e));
|
|
3210
|
+
});
|
|
3211
|
+
closeHandler = (_ev) => {
|
|
3212
|
+
if (_ev.code === 1008 && (_ev.reason || "").includes("rate limit")) {
|
|
3213
|
+
reject(new RateLimitError(_ev.reason || "Rate limit exceeded", _ev));
|
|
3214
|
+
return;
|
|
3215
|
+
}
|
|
3216
|
+
reject(new Error(`Connection lost to the notebook during request: ${_ev.reason || "Unknown reason"}`));
|
|
3217
|
+
};
|
|
3218
|
+
this.rws.addEventListener("close", closeHandler);
|
|
3219
|
+
this.rws.addEventListener("error", closeHandler);
|
|
3220
|
+
try {
|
|
3221
|
+
this.rws.send(this.pack({ action, data, errorEvent, responseEvent }));
|
|
3222
|
+
this.log("debug", "Message sent", { action, data });
|
|
3223
|
+
} catch (error) {
|
|
3224
|
+
this.log("error", "Failed to send message", { action, error });
|
|
3225
|
+
throw error;
|
|
3226
|
+
}
|
|
3227
|
+
};
|
|
3228
|
+
const send = async () => {
|
|
3229
|
+
await __privateMethod(this, _Transport_instances, connect_fn).call(this);
|
|
3230
|
+
const promise = new Promise(handler).finally(removeListeners);
|
|
3231
|
+
if (!options.timeout) {
|
|
3232
|
+
return promise;
|
|
3233
|
+
}
|
|
3234
|
+
return timeout(promise, options.timeout).finally(removeListeners);
|
|
3235
|
+
};
|
|
3236
|
+
return this.sendWithRetry(async () => await send(), options.retries || 10);
|
|
3237
|
+
}
|
|
3238
|
+
pack(data) {
|
|
3239
|
+
return new Blob([encode(data)]);
|
|
3240
|
+
}
|
|
3241
|
+
sendWithRetry(sender, retries = 10) {
|
|
3242
|
+
return (0, import_async_retry.default)(
|
|
3243
|
+
async (bail, attempt) => {
|
|
3244
|
+
try {
|
|
3245
|
+
return await sender();
|
|
3246
|
+
} catch (e) {
|
|
3247
|
+
if (e instanceof ErrorEvent || e instanceof RateLimitError || e instanceof InvalidConfigurationError || e instanceof InvalidMessageError || e instanceof DOMException) {
|
|
3248
|
+
this.log("debug", "Non-retryable error, bailing", {
|
|
3249
|
+
error: e.message,
|
|
3250
|
+
attempt
|
|
3251
|
+
});
|
|
3252
|
+
bail(e);
|
|
3253
|
+
return;
|
|
3254
|
+
}
|
|
3255
|
+
this.log("debug", "Retrying send operation", {
|
|
3256
|
+
attempt,
|
|
3257
|
+
error: e instanceof Error ? e.message : String(e),
|
|
3258
|
+
nextDelay: this.getBackoffDelay(attempt - 1)
|
|
3259
|
+
});
|
|
3260
|
+
throw e;
|
|
3261
|
+
}
|
|
3262
|
+
},
|
|
3263
|
+
{
|
|
3264
|
+
retries,
|
|
3265
|
+
onRetry: (e, attempt) => {
|
|
3266
|
+
this.log("warn", "Send operation retry", {
|
|
3267
|
+
attempt,
|
|
3268
|
+
maxRetries: retries,
|
|
3269
|
+
error: e instanceof Error ? e.message : String(e)
|
|
3270
|
+
});
|
|
3271
|
+
},
|
|
3272
|
+
// Use exponential backoff with jitter
|
|
3273
|
+
minTimeout: 1e3,
|
|
3274
|
+
factor: 2,
|
|
3275
|
+
maxTimeout: 3e4,
|
|
3276
|
+
randomize: true
|
|
3277
|
+
}
|
|
3278
|
+
);
|
|
3279
|
+
}
|
|
3280
|
+
invoke(action, data = {}, options = {}) {
|
|
3281
|
+
if (!options.responseEvent) {
|
|
3282
|
+
options.responseEvent = `${action}_${nanoid()}`;
|
|
3283
|
+
}
|
|
3284
|
+
return this.call("invoke", { action, data }, options);
|
|
3285
|
+
}
|
|
3286
|
+
disconnect() {
|
|
3287
|
+
if (this.closed) {
|
|
3288
|
+
console.trace("Transport is already closed, cannot disconnect again");
|
|
3289
|
+
return;
|
|
3290
|
+
}
|
|
3291
|
+
this.close();
|
|
3292
|
+
}
|
|
3293
|
+
close(code, reason) {
|
|
3294
|
+
if (this.closed) {
|
|
3295
|
+
return;
|
|
3296
|
+
}
|
|
3297
|
+
this.log("info", "Closing transport connection", { code, reason });
|
|
3298
|
+
this.connectPromise = null;
|
|
3299
|
+
const queuedCount = this.messageQueue.length;
|
|
3300
|
+
this.messageQueue.forEach((msg) => {
|
|
3301
|
+
console.log("Rejecting queued message due to connection close");
|
|
3302
|
+
msg.reject(new Error("Connection closed while message was queued"));
|
|
3303
|
+
});
|
|
3304
|
+
this.messageQueue = [];
|
|
3305
|
+
if (queuedCount > 0) {
|
|
3306
|
+
this.log("debug", `Rejected ${queuedCount} queued messages due to connection close`);
|
|
3307
|
+
}
|
|
3308
|
+
this.rateLimiter.requests = [];
|
|
3309
|
+
this.disposables.dispose();
|
|
3310
|
+
try {
|
|
3311
|
+
this.rws.close(code, reason);
|
|
3312
|
+
} catch (error) {
|
|
3313
|
+
this.log("error", "Error closing WebSocket", {
|
|
3314
|
+
error: error instanceof Error ? error.message : String(error)
|
|
3315
|
+
});
|
|
3316
|
+
}
|
|
3317
|
+
this.eventEmitter.emit("transport.closed", {
|
|
3318
|
+
code,
|
|
3319
|
+
reason,
|
|
3320
|
+
metrics: this.getConnectionMetrics(),
|
|
3321
|
+
timestamp: Date.now()
|
|
3322
|
+
});
|
|
3323
|
+
this.eventEmitter.removeListener("*");
|
|
3324
|
+
this.closed = true;
|
|
3325
|
+
this.log("info", "Transport connection closed successfully");
|
|
3326
|
+
}
|
|
3327
|
+
onDidConnect(listener) {
|
|
3328
|
+
this.rws.addEventListener("open", listener);
|
|
3329
|
+
this.disposables.add("connect", {
|
|
3330
|
+
dispose: () => {
|
|
3331
|
+
this.rws.removeEventListener("open", listener);
|
|
3332
|
+
}
|
|
3333
|
+
});
|
|
3334
|
+
}
|
|
3335
|
+
onDidClose(listener) {
|
|
3336
|
+
this.rws.addEventListener("close", listener);
|
|
3337
|
+
this.disposables.add("close", {
|
|
3338
|
+
dispose: () => {
|
|
3339
|
+
this.rws.removeEventListener("close", listener);
|
|
3340
|
+
}
|
|
3341
|
+
});
|
|
3342
|
+
}
|
|
3343
|
+
/**
|
|
3344
|
+
* Setup connection health monitoring
|
|
3345
|
+
*/
|
|
3346
|
+
setupConnectionHealthMonitoring() {
|
|
3347
|
+
this.rws.addEventListener("open", () => {
|
|
3348
|
+
this.connectionStats.connectTime = Date.now();
|
|
3349
|
+
this.connectionStats.reconnectCount++;
|
|
3350
|
+
this.log("info", "Connection established", {
|
|
3351
|
+
reconnectCount: this.connectionStats.reconnectCount,
|
|
3352
|
+
timeSinceStart: Date.now() - this.connectionStats.connectionStartTime
|
|
3353
|
+
});
|
|
3354
|
+
this.processMessageQueue();
|
|
3355
|
+
});
|
|
3356
|
+
this.rws.addEventListener("close", (event) => {
|
|
3357
|
+
this.log("warn", "Connection closed", {
|
|
3358
|
+
code: event.code,
|
|
3359
|
+
reason: event.reason,
|
|
3360
|
+
wasClean: event.wasClean
|
|
3361
|
+
});
|
|
3362
|
+
this.handleConnectionClose(event.code);
|
|
3363
|
+
});
|
|
3364
|
+
this.rws.addEventListener("error", (event) => {
|
|
3365
|
+
this.connectionStats.totalErrors++;
|
|
3366
|
+
this.log("error", "Connection error", {
|
|
3367
|
+
error: event,
|
|
3368
|
+
totalErrors: this.connectionStats.totalErrors
|
|
3369
|
+
});
|
|
3370
|
+
});
|
|
3371
|
+
}
|
|
3372
|
+
/**
|
|
3373
|
+
* Handle different WebSocket close codes appropriately
|
|
3374
|
+
*/
|
|
3375
|
+
handleConnectionClose(code) {
|
|
3376
|
+
switch (code) {
|
|
3377
|
+
case 1e3:
|
|
3378
|
+
this.log("info", "Normal connection closure");
|
|
3379
|
+
return "stop";
|
|
3380
|
+
case 1001:
|
|
3381
|
+
this.log("info", "Connection going away, will reconnect");
|
|
3382
|
+
return "reconnect";
|
|
3383
|
+
case 1006:
|
|
3384
|
+
this.log("warn", "Abnormal connection closure, will retry");
|
|
3385
|
+
return "retry";
|
|
3386
|
+
case 1008:
|
|
3387
|
+
this.log("error", "Connection closed due to policy violation");
|
|
3388
|
+
this.clearOldQueuedMessages();
|
|
3389
|
+
return "stop";
|
|
3390
|
+
default:
|
|
3391
|
+
this.log("warn", `Unknown close code: ${code}, will reconnect`);
|
|
3392
|
+
return "reconnect";
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
/**
|
|
3396
|
+
* Process queued messages when connection is restored
|
|
3397
|
+
*/
|
|
3398
|
+
processMessageQueue() {
|
|
3399
|
+
if (this.messageQueue.length === 0) {
|
|
3400
|
+
return;
|
|
3401
|
+
}
|
|
3402
|
+
this.log("debug", `Processing ${this.messageQueue.length} queued messages`);
|
|
3403
|
+
const queue = [...this.messageQueue];
|
|
3404
|
+
this.messageQueue = [];
|
|
3405
|
+
for (const queuedMessage of queue) {
|
|
3406
|
+
if (Date.now() - queuedMessage.timestamp > this.QUEUE_TIMEOUT) {
|
|
3407
|
+
queuedMessage.reject(new Error("Queued message timed out"));
|
|
3408
|
+
continue;
|
|
3409
|
+
}
|
|
3410
|
+
this.call(queuedMessage.action, queuedMessage.data, queuedMessage.options).then(queuedMessage.resolve).catch(queuedMessage.reject);
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
/**
|
|
3414
|
+
* Clear old queued messages to prevent memory leaks
|
|
3415
|
+
*/
|
|
3416
|
+
clearOldQueuedMessages() {
|
|
3417
|
+
const now = Date.now();
|
|
3418
|
+
const originalLength = this.messageQueue.length;
|
|
3419
|
+
this.messageQueue = this.messageQueue.filter((msg) => {
|
|
3420
|
+
const isExpired = now - msg.timestamp > this.QUEUE_TIMEOUT;
|
|
3421
|
+
if (isExpired) {
|
|
3422
|
+
msg.reject(new Error("Queued message expired"));
|
|
3423
|
+
}
|
|
3424
|
+
return !isExpired;
|
|
3425
|
+
});
|
|
3426
|
+
if (originalLength !== this.messageQueue.length) {
|
|
3427
|
+
this.log("debug", `Cleared ${originalLength - this.messageQueue.length} expired messages`);
|
|
3428
|
+
}
|
|
3429
|
+
}
|
|
3430
|
+
/**
|
|
3431
|
+
* Rate limiting check
|
|
3432
|
+
*/
|
|
3433
|
+
isRateLimited() {
|
|
3434
|
+
const now = Date.now();
|
|
3435
|
+
this.rateLimiter.requests = this.rateLimiter.requests.filter((timestamp) => now - timestamp < this.rateLimiter.windowMs);
|
|
3436
|
+
if (this.rateLimiter.requests.length >= this.rateLimiter.maxRequests) {
|
|
3437
|
+
return true;
|
|
3438
|
+
}
|
|
3439
|
+
this.rateLimiter.requests.push(now);
|
|
3440
|
+
return false;
|
|
3441
|
+
}
|
|
3442
|
+
/**
|
|
3443
|
+
* Calculate exponential backoff delay
|
|
3444
|
+
*/
|
|
3445
|
+
getBackoffDelay(retryCount) {
|
|
3446
|
+
const baseDelay = 1e3;
|
|
3447
|
+
const maxDelay = 3e4;
|
|
3448
|
+
const delay = Math.min(baseDelay * Math.pow(2, retryCount), maxDelay);
|
|
3449
|
+
return delay + Math.random() * 1e3;
|
|
3450
|
+
}
|
|
3451
|
+
/**
|
|
3452
|
+
* Get comprehensive connection metrics
|
|
3453
|
+
*/
|
|
3454
|
+
getConnectionMetrics() {
|
|
3455
|
+
const now = Date.now();
|
|
3456
|
+
const connectionDuration = this.connectionStats.connectTime > 0 ? now - this.connectionStats.connectTime : 0;
|
|
3457
|
+
return {
|
|
3458
|
+
status: this.status,
|
|
3459
|
+
clientId: this.clientId,
|
|
3460
|
+
isConnected: this.isConnected,
|
|
3461
|
+
isConnecting: this.isConnecting,
|
|
3462
|
+
connectionStats: {
|
|
3463
|
+
...this.connectionStats,
|
|
3464
|
+
connectionDuration,
|
|
3465
|
+
uptime: now - this.connectionStats.connectionStartTime,
|
|
3466
|
+
messagesPerSecond: connectionDuration > 0 ? (this.connectionStats.totalMessages / (connectionDuration / 1e3)).toFixed(2) : "0",
|
|
3467
|
+
errorRate: this.connectionStats.totalMessages > 0 ? (this.connectionStats.totalErrors / this.connectionStats.totalMessages * 100).toFixed(2) + "%" : "0%",
|
|
3468
|
+
timeSinceLastPing: this.connectionStats.lastPingTime > 0 ? now - this.connectionStats.lastPingTime : 0,
|
|
3469
|
+
timeSinceLastPong: this.connectionStats.lastPongTime > 0 ? now - this.connectionStats.lastPongTime : 0
|
|
3470
|
+
},
|
|
3471
|
+
messageQueue: {
|
|
3472
|
+
length: this.messageQueue.length,
|
|
3473
|
+
maxSize: this.MAX_QUEUE_SIZE,
|
|
3474
|
+
oldestMessageAge: this.messageQueue.length > 0 ? now - Math.min(...this.messageQueue.map((m) => m.timestamp)) : 0
|
|
3475
|
+
},
|
|
3476
|
+
rateLimiter: {
|
|
3477
|
+
currentRequests: this.rateLimiter.requests.length,
|
|
3478
|
+
maxRequests: this.rateLimiter.maxRequests,
|
|
3479
|
+
windowMs: this.rateLimiter.windowMs,
|
|
3480
|
+
isLimited: this.isRateLimited()
|
|
3481
|
+
},
|
|
3482
|
+
config: {
|
|
3483
|
+
pingInterval: this.PING_INTERVAL,
|
|
3484
|
+
queueTimeout: this.QUEUE_TIMEOUT,
|
|
3485
|
+
url: this.url.toString()
|
|
3486
|
+
}
|
|
3487
|
+
};
|
|
3488
|
+
}
|
|
3489
|
+
/**
|
|
3490
|
+
* Get connection health status
|
|
3491
|
+
*/
|
|
3492
|
+
getHealthStatus() {
|
|
3493
|
+
const metrics = this.getConnectionMetrics();
|
|
3494
|
+
const stats = metrics.connectionStats;
|
|
3495
|
+
if (!this.isConnected || stats.timeSinceLastPong > this.PING_INTERVAL * 2 || Number.parseFloat(stats.errorRate) > 50) {
|
|
3496
|
+
return "unhealthy";
|
|
3497
|
+
}
|
|
3498
|
+
if (stats.avgResponseTime > 5e3 || Number.parseFloat(stats.errorRate) > 10 || stats.timeSinceLastPong > this.PING_INTERVAL * 1.5) {
|
|
3499
|
+
return "degraded";
|
|
3500
|
+
}
|
|
3501
|
+
return "healthy";
|
|
3502
|
+
}
|
|
3503
|
+
/**
|
|
3504
|
+
* Reset connection statistics
|
|
3505
|
+
*/
|
|
3506
|
+
resetStats() {
|
|
3507
|
+
this.connectionStats = {
|
|
3508
|
+
connectTime: Date.now(),
|
|
3509
|
+
reconnectCount: 0,
|
|
3510
|
+
lastPingTime: 0,
|
|
3511
|
+
lastPongTime: 0,
|
|
3512
|
+
avgResponseTime: 0,
|
|
3513
|
+
totalMessages: 0,
|
|
3514
|
+
totalErrors: 0,
|
|
3515
|
+
connectionStartTime: Date.now()
|
|
3516
|
+
};
|
|
3517
|
+
this.log("debug", "Connection statistics reset");
|
|
3518
|
+
}
|
|
3519
|
+
/**
|
|
3520
|
+
* Run connection diagnostics
|
|
3521
|
+
*/
|
|
3522
|
+
async runDiagnostics() {
|
|
3523
|
+
const metrics = this.getConnectionMetrics();
|
|
3524
|
+
const status = this.getHealthStatus();
|
|
3525
|
+
const issues = [];
|
|
3526
|
+
const recommendations = [];
|
|
3527
|
+
if (!this.isConnected) {
|
|
3528
|
+
issues.push("Connection is not established");
|
|
3529
|
+
recommendations.push("Check network connectivity and server availability");
|
|
3530
|
+
}
|
|
3531
|
+
if (metrics.connectionStats.timeSinceLastPong > this.PING_INTERVAL * 2) {
|
|
3532
|
+
issues.push("No pong received recently - connection may be stale");
|
|
3533
|
+
recommendations.push("Consider forcing a reconnection");
|
|
3534
|
+
}
|
|
3535
|
+
if (Number.parseFloat(metrics.connectionStats.errorRate) > 10) {
|
|
3536
|
+
issues.push(`High error rate: ${metrics.connectionStats.errorRate}`);
|
|
3537
|
+
recommendations.push("Check server logs and network stability");
|
|
3538
|
+
}
|
|
3539
|
+
if (metrics.messageQueue.length > this.MAX_QUEUE_SIZE * 0.8) {
|
|
3540
|
+
issues.push("Message queue is nearly full");
|
|
3541
|
+
recommendations.push("Check connection stability and consider reducing message frequency");
|
|
3542
|
+
}
|
|
3543
|
+
if (metrics.connectionStats.avgResponseTime > 5e3) {
|
|
3544
|
+
issues.push("High average response time");
|
|
3545
|
+
recommendations.push("Check network latency and server performance");
|
|
3546
|
+
}
|
|
3547
|
+
if (metrics.rateLimiter.isLimited) {
|
|
3548
|
+
issues.push("Rate limiting is active");
|
|
3549
|
+
recommendations.push("Reduce request frequency or increase rate limit");
|
|
3550
|
+
}
|
|
3551
|
+
this.log("info", "Connection diagnostics completed", {
|
|
3552
|
+
status,
|
|
3553
|
+
issueCount: issues.length,
|
|
3554
|
+
recommendationCount: recommendations.length
|
|
3555
|
+
});
|
|
3556
|
+
return {
|
|
3557
|
+
status,
|
|
3558
|
+
metrics,
|
|
3559
|
+
issues,
|
|
3560
|
+
recommendations
|
|
3561
|
+
};
|
|
3562
|
+
}
|
|
3563
|
+
/**
|
|
3564
|
+
* Periodic maintenance - clean up old data, run health checks
|
|
3565
|
+
*/
|
|
3566
|
+
startPeriodicMaintenance() {
|
|
3567
|
+
this.disposables.add("maintenance", () => {
|
|
3568
|
+
const maintenanceInterval = setInterval(
|
|
3569
|
+
() => {
|
|
3570
|
+
this.clearOldQueuedMessages();
|
|
3571
|
+
const now = Date.now();
|
|
3572
|
+
this.rateLimiter.requests = this.rateLimiter.requests.filter(
|
|
3573
|
+
(timestamp) => now - timestamp < this.rateLimiter.windowMs
|
|
3574
|
+
);
|
|
3575
|
+
if (this.options.debug) {
|
|
3576
|
+
const health = this.getHealthStatus();
|
|
3577
|
+
const metrics = this.getConnectionMetrics();
|
|
3578
|
+
this.log("debug", "Periodic health check", {
|
|
3579
|
+
health,
|
|
3580
|
+
messageCount: metrics.connectionStats.totalMessages,
|
|
3581
|
+
errorCount: metrics.connectionStats.totalErrors,
|
|
3582
|
+
queueLength: metrics.messageQueue.length
|
|
3583
|
+
});
|
|
3584
|
+
}
|
|
3585
|
+
},
|
|
3586
|
+
5 * 60 * 1e3
|
|
3587
|
+
);
|
|
3588
|
+
return {
|
|
3589
|
+
dispose: () => {
|
|
3590
|
+
clearInterval(maintenanceInterval);
|
|
3591
|
+
}
|
|
3592
|
+
};
|
|
3593
|
+
});
|
|
3594
|
+
}
|
|
3595
|
+
};
|
|
3596
|
+
_Transport_instances = new WeakSet();
|
|
3597
|
+
/**
|
|
3598
|
+
* Explicitly connect to the websocket if not already connected
|
|
3599
|
+
* Used for lazy initialization
|
|
3600
|
+
*
|
|
3601
|
+
* This method ensures only one connection attempt happens at a time
|
|
3602
|
+
* by caching the connection promise.
|
|
3603
|
+
*/
|
|
3604
|
+
connect_fn = function() {
|
|
3605
|
+
if (this.isConnected) {
|
|
3606
|
+
return Promise.resolve();
|
|
3607
|
+
}
|
|
3608
|
+
if (this.connectPromise) {
|
|
3609
|
+
return this.connectPromise;
|
|
3610
|
+
}
|
|
3611
|
+
this.connectPromise = new Promise((resolve, reject) => {
|
|
3612
|
+
if (this.isConnected) {
|
|
3613
|
+
resolve();
|
|
3614
|
+
__privateMethod(this, _Transport_instances, startPeriodicPing_fn).call(this);
|
|
3615
|
+
return;
|
|
3616
|
+
}
|
|
3617
|
+
if (this.rws.readyState === 3) {
|
|
3618
|
+
this.rws.reconnect();
|
|
3619
|
+
}
|
|
3620
|
+
let timeoutId;
|
|
3621
|
+
const openHandler = () => {
|
|
3622
|
+
this.rws.removeEventListener("open", openHandler);
|
|
3623
|
+
this.rws.removeEventListener("error", errorHandler);
|
|
3624
|
+
clearTimeout(timeoutId);
|
|
3625
|
+
this.connectPromise = null;
|
|
3626
|
+
resolve();
|
|
3627
|
+
__privateMethod(this, _Transport_instances, startPeriodicPing_fn).call(this);
|
|
3628
|
+
};
|
|
3629
|
+
const errorHandler = (error) => {
|
|
3630
|
+
this.rws.removeEventListener("open", openHandler);
|
|
3631
|
+
this.rws.removeEventListener("error", errorHandler);
|
|
3632
|
+
clearTimeout(timeoutId);
|
|
3633
|
+
this.connectPromise = null;
|
|
3634
|
+
reject(new Error(`WebSocket connection failed: ${error}`));
|
|
3635
|
+
};
|
|
3636
|
+
timeoutId = setTimeout(() => {
|
|
3637
|
+
this.rws.removeEventListener("open", openHandler);
|
|
3638
|
+
this.rws.removeEventListener("error", errorHandler);
|
|
3639
|
+
this.connectPromise = null;
|
|
3640
|
+
reject(new Error("WebSocket connection timeout"));
|
|
3641
|
+
}, 1e4);
|
|
3642
|
+
this.rws.addEventListener("open", openHandler);
|
|
3643
|
+
this.rws.addEventListener("error", errorHandler);
|
|
3644
|
+
});
|
|
3645
|
+
return this.connectPromise;
|
|
3646
|
+
};
|
|
3647
|
+
startPeriodicPing_fn = function() {
|
|
3648
|
+
this.disposables.add("pingInterval", () => {
|
|
3649
|
+
const interval = setInterval(async () => {
|
|
3650
|
+
try {
|
|
3651
|
+
this.connectionStats.lastPingTime = Date.now();
|
|
3652
|
+
this.log("debug", "Sending periodic ping");
|
|
3653
|
+
const startTime = Date.now();
|
|
3654
|
+
await this.invoke("ping");
|
|
3655
|
+
this.connectionStats.lastPongTime = Date.now();
|
|
3656
|
+
const pingTime = this.connectionStats.lastPongTime - startTime;
|
|
3657
|
+
this.log("debug", `Ping successful`, { pingTime });
|
|
3658
|
+
} catch (error) {
|
|
3659
|
+
this.log("error", "Ping failed", {
|
|
3660
|
+
error: error instanceof Error ? error.message : String(error)
|
|
3661
|
+
});
|
|
3662
|
+
if (Date.now() - this.connectionStats.lastPongTime > this.PING_INTERVAL * 3) {
|
|
3663
|
+
this.log("warn", "Connection appears dead, forcing reconnection");
|
|
3664
|
+
this.rws.reconnect();
|
|
3665
|
+
}
|
|
3666
|
+
}
|
|
3667
|
+
}, this.PING_INTERVAL);
|
|
3668
|
+
return {
|
|
3669
|
+
dispose: () => {
|
|
3670
|
+
clearInterval(interval);
|
|
3671
|
+
}
|
|
3672
|
+
};
|
|
3673
|
+
});
|
|
3674
|
+
};
|
|
3675
|
+
|
|
3676
|
+
// node_modules/mitt/dist/mitt.mjs
|
|
3677
|
+
function mitt_default(n) {
|
|
3678
|
+
return { all: n = n || /* @__PURE__ */ new Map(), on: function(t, e) {
|
|
3679
|
+
var i = n.get(t);
|
|
3680
|
+
i ? i.push(e) : n.set(t, [e]);
|
|
3681
|
+
}, off: function(t, e) {
|
|
3682
|
+
var i = n.get(t);
|
|
3683
|
+
i && (e ? i.splice(i.indexOf(e) >>> 0, 1) : n.set(t, []));
|
|
3684
|
+
}, emit: function(t, e) {
|
|
3685
|
+
var i = n.get(t);
|
|
3686
|
+
i && i.slice().map(function(n2) {
|
|
3687
|
+
n2(e);
|
|
3688
|
+
}), (i = n.get("*")) && i.slice().map(function(n2) {
|
|
3689
|
+
n2(t, e);
|
|
3690
|
+
});
|
|
3691
|
+
} };
|
|
3692
|
+
}
|
|
3693
|
+
|
|
3694
|
+
// src/events/index.ts
|
|
3695
|
+
function mittWithOnce(all) {
|
|
3696
|
+
const inst = mitt_default(all);
|
|
3697
|
+
inst.once = (type, fn) => {
|
|
3698
|
+
inst.on(type, fn);
|
|
3699
|
+
inst.on(type, inst.off.bind(inst, type, fn));
|
|
3700
|
+
};
|
|
3701
|
+
return inst;
|
|
3702
|
+
}
|
|
3703
|
+
var EE = mittWithOnce();
|
|
3704
|
+
var EventManager = class _EventManager {
|
|
3705
|
+
listen(event, callback) {
|
|
3706
|
+
const dispose = () => {
|
|
3707
|
+
this.removeListener(event, callback);
|
|
3708
|
+
};
|
|
3709
|
+
EE.on(event, callback);
|
|
3710
|
+
return { dispose };
|
|
3711
|
+
}
|
|
3712
|
+
emit(event, data) {
|
|
3713
|
+
EE.emit(event, data);
|
|
3714
|
+
}
|
|
3715
|
+
once(event, callback) {
|
|
3716
|
+
EE.once(event, callback);
|
|
3717
|
+
}
|
|
3718
|
+
static make() {
|
|
3719
|
+
return _EventManager.instance || (_EventManager.instance = new _EventManager());
|
|
3720
|
+
}
|
|
3721
|
+
static refresh() {
|
|
3722
|
+
EE.all.clear();
|
|
3723
|
+
EE = mittWithOnce();
|
|
3724
|
+
return _EventManager.instance = new _EventManager();
|
|
3725
|
+
}
|
|
3726
|
+
removeListener(event, callbackSignature) {
|
|
3727
|
+
EE.off(event, callbackSignature);
|
|
3728
|
+
}
|
|
3729
|
+
static createInstance() {
|
|
3730
|
+
EE = mittWithOnce();
|
|
3731
|
+
_EventManager.instance = new _EventManager();
|
|
3732
|
+
return _EventManager.instance;
|
|
3733
|
+
}
|
|
3734
|
+
inspect() {
|
|
3735
|
+
return EE.all;
|
|
3736
|
+
}
|
|
3737
|
+
};
|
|
3738
|
+
|
|
3739
|
+
// src/git.ts
|
|
3740
|
+
var Git = class {
|
|
3741
|
+
constructor(okra) {
|
|
3742
|
+
this.okra = okra;
|
|
3743
|
+
}
|
|
3744
|
+
checkpoint(author, message, branch = "main") {
|
|
3745
|
+
return this.okra.invoke("git.checkpoint", { author, message, branch });
|
|
3746
|
+
}
|
|
3747
|
+
sync(url, author, ref = "main", token, direction = "both", force = false) {
|
|
3748
|
+
return this.okra.invoke("git.sync", { url, author, ref, token, direction, force });
|
|
3749
|
+
}
|
|
3750
|
+
log(ref = "main") {
|
|
3751
|
+
return this.okra.invoke("git.log", { ref });
|
|
3752
|
+
}
|
|
3753
|
+
restore(ref) {
|
|
3754
|
+
return this.okra.invoke("git.restore", { ref });
|
|
3755
|
+
}
|
|
3756
|
+
};
|
|
3757
|
+
|
|
3758
|
+
// src/beacon/index.ts
|
|
3759
|
+
var import_async_retry2 = __toESM(require_lib(), 1);
|
|
3760
|
+
|
|
3761
|
+
// src/beacon/navigator.ts
|
|
3762
|
+
var Navigator = class {
|
|
3763
|
+
constructor(onEvent, onceEvent, beacon) {
|
|
3764
|
+
this.onEvent = onEvent;
|
|
3765
|
+
this.onceEvent = onceEvent;
|
|
3766
|
+
this.beacon = beacon;
|
|
3767
|
+
this.urlHistory = [];
|
|
3768
|
+
this.currentIndex = 0;
|
|
3769
|
+
this._canGoBack = false;
|
|
3770
|
+
this._canGoForward = false;
|
|
3771
|
+
this.beacon = beacon;
|
|
3772
|
+
this.setupUrlChangeListener();
|
|
3773
|
+
this.urlHistory.push(beacon.iframe.src);
|
|
3774
|
+
this.updateNavigationState();
|
|
3775
|
+
}
|
|
3776
|
+
/**
|
|
3777
|
+
* Navigate to a URL using internal history management
|
|
3778
|
+
*/
|
|
3779
|
+
visit(url) {
|
|
3780
|
+
if (!this.beacon) {
|
|
3781
|
+
throw new Error("Beacon reference not set");
|
|
3782
|
+
}
|
|
3783
|
+
this.beacon.navigate(url);
|
|
3784
|
+
if (this.currentIndex < this.urlHistory.length - 1) {
|
|
3785
|
+
this.urlHistory = this.urlHistory.slice(0, this.currentIndex + 1);
|
|
3786
|
+
}
|
|
3787
|
+
this.urlHistory.push(url);
|
|
3788
|
+
this.currentIndex = this.urlHistory.length - 1;
|
|
3789
|
+
this.updateNavigationState();
|
|
3790
|
+
this.emitHistoryChange(url, "push");
|
|
3791
|
+
}
|
|
3792
|
+
/**
|
|
3793
|
+
* Set up listener for URL change events from the beacon
|
|
3794
|
+
*/
|
|
3795
|
+
setupUrlChangeListener() {
|
|
3796
|
+
this.beacon.on("urlChange", (payload) => {
|
|
3797
|
+
this.addToHistory(payload.newUrl);
|
|
3798
|
+
});
|
|
3799
|
+
}
|
|
3800
|
+
/**
|
|
3801
|
+
* Add URL to internal history
|
|
3802
|
+
*/
|
|
3803
|
+
addToHistory(url) {
|
|
3804
|
+
if (this.currentIndex < this.urlHistory.length - 1) {
|
|
3805
|
+
this.urlHistory = this.urlHistory.slice(0, this.currentIndex + 1);
|
|
3806
|
+
}
|
|
3807
|
+
this.urlHistory.push(url);
|
|
3808
|
+
this.currentIndex = this.urlHistory.length - 1;
|
|
3809
|
+
this.updateNavigationState();
|
|
3810
|
+
}
|
|
3811
|
+
/**
|
|
3812
|
+
* Update internal navigation state and emit changes
|
|
3813
|
+
*/
|
|
3814
|
+
updateNavigationState() {
|
|
3815
|
+
const previousCanGoBack = this._canGoBack;
|
|
3816
|
+
const previousCanGoForward = this._canGoForward;
|
|
3817
|
+
this._canGoBack = this.currentIndex > 0;
|
|
3818
|
+
this._canGoForward = this.currentIndex < this.urlHistory.length - 1;
|
|
3819
|
+
if (previousCanGoBack !== this._canGoBack || previousCanGoForward !== this._canGoForward) {
|
|
3820
|
+
this.emitNavigationStateChange();
|
|
3821
|
+
}
|
|
3822
|
+
}
|
|
3823
|
+
/**
|
|
3824
|
+
* Emit navigation state change event
|
|
3825
|
+
*/
|
|
3826
|
+
emitNavigationStateChange() {
|
|
3827
|
+
if (this.beacon && this.beacon.emit) {
|
|
3828
|
+
this.beacon.emit("navigationStateChange", {
|
|
3829
|
+
canGoBack: this._canGoBack,
|
|
3830
|
+
canGoForward: this._canGoForward,
|
|
3831
|
+
currentIndex: this.currentIndex,
|
|
3832
|
+
historyLength: this.urlHistory.length,
|
|
3833
|
+
timestamp: Date.now()
|
|
3834
|
+
});
|
|
3835
|
+
}
|
|
3836
|
+
}
|
|
3837
|
+
get canGoBack() {
|
|
3838
|
+
return this._canGoBack;
|
|
3839
|
+
}
|
|
3840
|
+
get canGoForward() {
|
|
3841
|
+
return this._canGoForward;
|
|
3842
|
+
}
|
|
3843
|
+
async goBack() {
|
|
3844
|
+
if (!this.canGoBack) {
|
|
3845
|
+
return { success: false, error: "Cannot go back - no previous history" };
|
|
3846
|
+
}
|
|
3847
|
+
this.currentIndex--;
|
|
3848
|
+
this.updateNavigationState();
|
|
3849
|
+
const url = this.urlHistory[this.currentIndex];
|
|
3850
|
+
if (!this.beacon) {
|
|
3851
|
+
return { success: false, error: "Beacon reference not set" };
|
|
3852
|
+
}
|
|
3853
|
+
this.beacon.navigate(url);
|
|
3854
|
+
this.emitHistoryChange(url, "back");
|
|
3855
|
+
return { success: true, url };
|
|
3856
|
+
}
|
|
3857
|
+
/**
|
|
3858
|
+
* Navigate forward in internal history
|
|
3859
|
+
*/
|
|
3860
|
+
async goForward() {
|
|
3861
|
+
if (!this.canGoForward) {
|
|
3862
|
+
return { success: false, error: "Cannot go forward - no forward history" };
|
|
3863
|
+
}
|
|
3864
|
+
this.currentIndex++;
|
|
3865
|
+
this.updateNavigationState();
|
|
3866
|
+
const url = this.urlHistory[this.currentIndex];
|
|
3867
|
+
if (!this.beacon) {
|
|
3868
|
+
return { success: false, error: "Beacon reference not set" };
|
|
3869
|
+
}
|
|
3870
|
+
this.beacon.navigate(url);
|
|
3871
|
+
this.emitHistoryChange(url, "forward");
|
|
3872
|
+
return { success: true, url };
|
|
3873
|
+
}
|
|
3874
|
+
/**
|
|
3875
|
+
* Reload the current page
|
|
3876
|
+
*/
|
|
3877
|
+
async reload() {
|
|
3878
|
+
if (this.currentIndex >= 0 && this.currentIndex < this.urlHistory.length) {
|
|
3879
|
+
const currentUrl = this.urlHistory[this.currentIndex];
|
|
3880
|
+
if (!this.beacon) {
|
|
3881
|
+
throw new Error("Beacon reference not set");
|
|
3882
|
+
}
|
|
3883
|
+
this.beacon.navigate(currentUrl);
|
|
3884
|
+
this.emitHistoryChange(currentUrl, "reload");
|
|
3885
|
+
}
|
|
3886
|
+
}
|
|
3887
|
+
/**
|
|
3888
|
+
* Get current URL from internal history
|
|
3889
|
+
*/
|
|
3890
|
+
getCurrentUrl() {
|
|
3891
|
+
if (this.currentIndex >= 0 && this.currentIndex < this.urlHistory.length) {
|
|
3892
|
+
return new URL(this.urlHistory[this.currentIndex]);
|
|
3893
|
+
}
|
|
3894
|
+
return new URL("about:blank");
|
|
3895
|
+
}
|
|
3896
|
+
/**
|
|
3897
|
+
* Get the complete history array
|
|
3898
|
+
*/
|
|
3899
|
+
getHistory() {
|
|
3900
|
+
return [...this.urlHistory];
|
|
3901
|
+
}
|
|
3902
|
+
/**
|
|
3903
|
+
* Get current history index
|
|
3904
|
+
*/
|
|
3905
|
+
getCurrentIndex() {
|
|
3906
|
+
return this.currentIndex;
|
|
3907
|
+
}
|
|
3908
|
+
/**
|
|
3909
|
+
* Check if can go back synchronously
|
|
3910
|
+
*/
|
|
3911
|
+
canGoBackSync() {
|
|
3912
|
+
return this.currentIndex > 0;
|
|
3913
|
+
}
|
|
3914
|
+
/**
|
|
3915
|
+
* Check if can go forward synchronously
|
|
3916
|
+
*/
|
|
3917
|
+
canGoForwardSync() {
|
|
3918
|
+
return this.currentIndex < this.urlHistory.length - 1;
|
|
3919
|
+
}
|
|
3920
|
+
/**
|
|
3921
|
+
* Emit history change event
|
|
3922
|
+
*/
|
|
3923
|
+
emitHistoryChange(url, direction) {
|
|
3924
|
+
if (this.beacon && this.beacon.emit) {
|
|
3925
|
+
this.beacon.emit("historyChange", {
|
|
3926
|
+
url,
|
|
3927
|
+
state: null,
|
|
3928
|
+
direction,
|
|
3929
|
+
timestamp: Date.now()
|
|
3930
|
+
});
|
|
3931
|
+
}
|
|
3932
|
+
}
|
|
3933
|
+
/**
|
|
3934
|
+
* Push a new state to internal history (legacy method for compatibility)
|
|
3935
|
+
*/
|
|
3936
|
+
async pushState(url) {
|
|
3937
|
+
this.visit(url);
|
|
3938
|
+
}
|
|
3939
|
+
/**
|
|
3940
|
+
* Replace current state in internal history (legacy method for compatibility)
|
|
3941
|
+
*/
|
|
3942
|
+
async replaceState(url) {
|
|
3943
|
+
if (this.currentIndex >= 0 && this.currentIndex < this.urlHistory.length) {
|
|
3944
|
+
this.urlHistory[this.currentIndex] = url;
|
|
3945
|
+
if (!this.beacon) {
|
|
3946
|
+
throw new Error("Beacon reference not set");
|
|
3947
|
+
}
|
|
3948
|
+
this.beacon.navigate(url);
|
|
3949
|
+
this.emitHistoryChange(url, "replace");
|
|
3950
|
+
} else {
|
|
3951
|
+
this.visit(url);
|
|
3952
|
+
}
|
|
3953
|
+
}
|
|
3954
|
+
/**
|
|
3955
|
+
* Get the current history length
|
|
3956
|
+
*/
|
|
3957
|
+
async getHistoryLength() {
|
|
3958
|
+
return this.urlHistory.length;
|
|
3959
|
+
}
|
|
3960
|
+
/**
|
|
3961
|
+
* Get the current history state (always null since we don't store state)
|
|
3962
|
+
*/
|
|
3963
|
+
async getHistoryState() {
|
|
3964
|
+
return null;
|
|
3965
|
+
}
|
|
3966
|
+
/**
|
|
3967
|
+
* Get comprehensive history information
|
|
3968
|
+
*/
|
|
3969
|
+
async getHistoryInfo() {
|
|
3970
|
+
return {
|
|
3971
|
+
length: this.urlHistory.length,
|
|
3972
|
+
state: null,
|
|
3973
|
+
url: this.urlHistory[this.currentIndex] || "",
|
|
3974
|
+
canGoBack: this.canGoBack,
|
|
3975
|
+
canGoForward: this.canGoForward
|
|
3976
|
+
};
|
|
3977
|
+
}
|
|
3978
|
+
/**
|
|
3979
|
+
* Listen for navigator events
|
|
3980
|
+
*/
|
|
3981
|
+
on(event, handler) {
|
|
3982
|
+
return this.onEvent(event, handler);
|
|
3983
|
+
}
|
|
3984
|
+
/**
|
|
3985
|
+
* Listen for navigator events (once)
|
|
3986
|
+
*/
|
|
3987
|
+
once(event, handler) {
|
|
3988
|
+
this.onceEvent(event, handler);
|
|
3989
|
+
}
|
|
3990
|
+
};
|
|
3991
|
+
|
|
3992
|
+
// src/beacon/index.ts
|
|
3993
|
+
function ensureBrowserEnvironment() {
|
|
3994
|
+
if (typeof window === "undefined") {
|
|
3995
|
+
throw new Error("Beacon SDK can only be used in a browser environment");
|
|
3996
|
+
}
|
|
3997
|
+
}
|
|
3998
|
+
var BeaconError = class extends Error {
|
|
3999
|
+
constructor(message, code) {
|
|
4000
|
+
super(message);
|
|
4001
|
+
this.code = code;
|
|
4002
|
+
this.name = "BeaconError";
|
|
4003
|
+
}
|
|
4004
|
+
};
|
|
4005
|
+
var BeaconTimeoutError = class extends BeaconError {
|
|
4006
|
+
constructor(operation, timeoutMs) {
|
|
4007
|
+
super(`Beacon operation '${operation}' timed out after ${timeoutMs}ms`);
|
|
4008
|
+
this.code = "TIMEOUT";
|
|
4009
|
+
}
|
|
4010
|
+
};
|
|
4011
|
+
var BeaconConnectionError = class extends BeaconError {
|
|
4012
|
+
constructor(message) {
|
|
4013
|
+
super(`Beacon connection error: ${message}`);
|
|
4014
|
+
this.code = "CONNECTION_ERROR";
|
|
4015
|
+
}
|
|
4016
|
+
};
|
|
4017
|
+
var Beacon = class {
|
|
4018
|
+
constructor(iframe, options = {}) {
|
|
4019
|
+
this.messageHandlers = /* @__PURE__ */ new Map();
|
|
4020
|
+
this.isBeaconReady = false;
|
|
4021
|
+
this.readyPromise = null;
|
|
4022
|
+
this.isStarted = false;
|
|
4023
|
+
this.disposables = [];
|
|
4024
|
+
// MessagePort communication
|
|
4025
|
+
this.messageChannel = null;
|
|
4026
|
+
this.port = null;
|
|
4027
|
+
this.channelEstablished = false;
|
|
4028
|
+
ensureBrowserEnvironment();
|
|
4029
|
+
this.iframe = iframe;
|
|
4030
|
+
const defaultRetryOptions = {
|
|
4031
|
+
retries: 3,
|
|
4032
|
+
minTimeout: 1e3,
|
|
4033
|
+
maxTimeout: 5e3,
|
|
4034
|
+
factor: 2,
|
|
4035
|
+
randomize: true
|
|
4036
|
+
};
|
|
4037
|
+
this.options = {
|
|
4038
|
+
timeout: 1e4,
|
|
4039
|
+
targetOrigin: "*",
|
|
4040
|
+
debug: false,
|
|
4041
|
+
retry: {
|
|
4042
|
+
...defaultRetryOptions,
|
|
4043
|
+
...options.retry
|
|
4044
|
+
},
|
|
4045
|
+
...options
|
|
4046
|
+
};
|
|
4047
|
+
this.eventEmitter = EventManager.make();
|
|
4048
|
+
this.setupMessageChannel();
|
|
4049
|
+
this.navigator = new Navigator(
|
|
4050
|
+
this.eventEmitter.listen.bind(this.eventEmitter),
|
|
4051
|
+
this.eventEmitter.once.bind(this.eventEmitter),
|
|
4052
|
+
this
|
|
4053
|
+
);
|
|
4054
|
+
}
|
|
4055
|
+
/**
|
|
4056
|
+
* Setup communication channel using MessagePort
|
|
4057
|
+
*/
|
|
4058
|
+
setupMessageChannel() {
|
|
4059
|
+
const readyHandler = (event) => {
|
|
4060
|
+
try {
|
|
4061
|
+
if (event.source !== this.iframe.contentWindow) {
|
|
4062
|
+
return;
|
|
4063
|
+
}
|
|
4064
|
+
const message = event.data;
|
|
4065
|
+
if (!message || typeof message !== "object" || !message.type) {
|
|
4066
|
+
return;
|
|
4067
|
+
}
|
|
4068
|
+
if (message.type === "beacon:ready-for-channel") {
|
|
4069
|
+
this.establishChannel().catch((error) => {
|
|
4070
|
+
if (this.options.debug) {
|
|
4071
|
+
console.error("[Beacon SDK] Channel establishment failed:", error);
|
|
4072
|
+
}
|
|
4073
|
+
});
|
|
4074
|
+
}
|
|
4075
|
+
if (message.type === "beacon:channel-established") {
|
|
4076
|
+
this.channelEstablished = true;
|
|
4077
|
+
this.isBeaconReady = true;
|
|
4078
|
+
this.emit("ready", message.payload);
|
|
4079
|
+
if (this.options.debug) {
|
|
4080
|
+
console.log("[Beacon SDK] Channel established and beacon ready");
|
|
4081
|
+
}
|
|
4082
|
+
}
|
|
4083
|
+
} catch (error) {
|
|
4084
|
+
if (this.options.debug) {
|
|
4085
|
+
console.error("[Beacon SDK] Message parsing error:", error);
|
|
4086
|
+
}
|
|
4087
|
+
}
|
|
4088
|
+
};
|
|
4089
|
+
window.addEventListener("message", readyHandler);
|
|
4090
|
+
this.disposables.push({
|
|
4091
|
+
dispose: () => window.removeEventListener("message", readyHandler)
|
|
4092
|
+
});
|
|
4093
|
+
}
|
|
4094
|
+
/**
|
|
4095
|
+
* Establish MessagePort communication channel
|
|
4096
|
+
*/
|
|
4097
|
+
async establishChannel() {
|
|
4098
|
+
if (!this.iframe.contentWindow) {
|
|
4099
|
+
throw new BeaconConnectionError("Iframe contentWindow not available");
|
|
4100
|
+
}
|
|
4101
|
+
this.messageChannel = new MessageChannel();
|
|
4102
|
+
this.port = this.messageChannel.port1;
|
|
4103
|
+
this.port.onmessage = (event) => {
|
|
4104
|
+
this.handleBeaconMessage(event.data);
|
|
4105
|
+
};
|
|
4106
|
+
this.port.onmessageerror = (event) => {
|
|
4107
|
+
if (this.options.debug) {
|
|
4108
|
+
console.error("[Beacon SDK] Port message error:", event);
|
|
4109
|
+
}
|
|
4110
|
+
};
|
|
4111
|
+
this.port.start();
|
|
4112
|
+
const establishMessage = {
|
|
4113
|
+
type: "beacon:establish-channel",
|
|
4114
|
+
timestamp: Date.now(),
|
|
4115
|
+
id: this.generateId()
|
|
4116
|
+
};
|
|
4117
|
+
this.iframe.contentWindow.postMessage(
|
|
4118
|
+
establishMessage,
|
|
4119
|
+
"*",
|
|
4120
|
+
// Safe for channel establishment
|
|
4121
|
+
[this.messageChannel.port2]
|
|
4122
|
+
// Transfer the port
|
|
4123
|
+
);
|
|
4124
|
+
if (this.options.debug) {
|
|
4125
|
+
console.log("[Beacon SDK] Channel establishment message sent");
|
|
4126
|
+
}
|
|
4127
|
+
return new Promise((resolve, reject) => {
|
|
4128
|
+
const timeoutId = setTimeout(() => {
|
|
4129
|
+
reject(new BeaconConnectionError("Channel establishment timeout"));
|
|
4130
|
+
}, this.options.timeout);
|
|
4131
|
+
const checkReady = () => {
|
|
4132
|
+
if (this.channelEstablished && this.isBeaconReady) {
|
|
4133
|
+
clearTimeout(timeoutId);
|
|
4134
|
+
resolve();
|
|
4135
|
+
} else {
|
|
4136
|
+
setTimeout(checkReady, 100);
|
|
4137
|
+
}
|
|
4138
|
+
};
|
|
4139
|
+
checkReady();
|
|
4140
|
+
});
|
|
4141
|
+
}
|
|
4142
|
+
/**
|
|
4143
|
+
* Handle incoming beacon messages
|
|
4144
|
+
*/
|
|
4145
|
+
handleBeaconMessage(message) {
|
|
4146
|
+
if (this.options.debug) {
|
|
4147
|
+
console.log("[Beacon SDK] Received message:", message.type, message.payload);
|
|
4148
|
+
}
|
|
4149
|
+
const eventType = message.type.replace("beacon:", "");
|
|
4150
|
+
this.emit(eventType, message.payload);
|
|
4151
|
+
const handlers = this.messageHandlers.get(message.type);
|
|
4152
|
+
if (handlers) {
|
|
4153
|
+
handlers.forEach((handler) => {
|
|
4154
|
+
try {
|
|
4155
|
+
handler(message.payload);
|
|
4156
|
+
} catch (error) {
|
|
4157
|
+
if (this.options.debug) {
|
|
4158
|
+
console.error("[Beacon SDK] Message handler error:", error);
|
|
4159
|
+
}
|
|
4160
|
+
}
|
|
4161
|
+
});
|
|
4162
|
+
}
|
|
4163
|
+
}
|
|
4164
|
+
/**
|
|
4165
|
+
* Wait for beacon to be ready - simplified approach
|
|
4166
|
+
*/
|
|
4167
|
+
waitForBeaconReady() {
|
|
4168
|
+
return (0, import_async_retry2.default)(
|
|
4169
|
+
async () => {
|
|
4170
|
+
return new Promise((resolve, reject) => {
|
|
4171
|
+
if (this.isBeaconReady && this.channelEstablished) {
|
|
4172
|
+
resolve();
|
|
4173
|
+
return;
|
|
4174
|
+
}
|
|
4175
|
+
const timeoutMs = this.options.timeout;
|
|
4176
|
+
const timeoutId = setTimeout(() => {
|
|
4177
|
+
reject(new BeaconTimeoutError("beacon ready", timeoutMs));
|
|
4178
|
+
}, timeoutMs);
|
|
4179
|
+
const unsubscribe = this.on("ready", () => {
|
|
4180
|
+
clearTimeout(timeoutId);
|
|
4181
|
+
unsubscribe.dispose();
|
|
4182
|
+
resolve();
|
|
4183
|
+
});
|
|
4184
|
+
});
|
|
4185
|
+
},
|
|
4186
|
+
{
|
|
4187
|
+
retries: this.options.retry?.retries || 3,
|
|
4188
|
+
minTimeout: this.options.retry?.minTimeout || 1e3,
|
|
4189
|
+
maxTimeout: this.options.retry?.maxTimeout || 5e3,
|
|
4190
|
+
factor: this.options.retry?.factor || 2,
|
|
4191
|
+
randomize: this.options.retry?.randomize || true,
|
|
4192
|
+
onRetry: (error, attempt) => {
|
|
4193
|
+
if (this.options.debug) {
|
|
4194
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
4195
|
+
console.warn(`[Beacon SDK] Beacon ready retry attempt ${attempt}:`, errorMessage);
|
|
4196
|
+
}
|
|
4197
|
+
this.isBeaconReady = false;
|
|
4198
|
+
this.channelEstablished = false;
|
|
4199
|
+
}
|
|
4200
|
+
}
|
|
4201
|
+
);
|
|
4202
|
+
}
|
|
4203
|
+
/**
|
|
4204
|
+
* Send a message to the beacon via MessagePort
|
|
4205
|
+
*/
|
|
4206
|
+
sendMessage(type, payload = {}) {
|
|
4207
|
+
if (!this.port) {
|
|
4208
|
+
throw new BeaconConnectionError("MessagePort not available - channel not established");
|
|
4209
|
+
}
|
|
4210
|
+
const message = {
|
|
4211
|
+
type: `beacon:${type}`,
|
|
4212
|
+
payload,
|
|
4213
|
+
timestamp: Date.now(),
|
|
4214
|
+
source: "parent",
|
|
4215
|
+
id: this.generateId()
|
|
4216
|
+
};
|
|
4217
|
+
try {
|
|
4218
|
+
this.port.postMessage(message);
|
|
4219
|
+
if (this.options.debug) {
|
|
4220
|
+
console.log("[Beacon SDK] Sent message via port:", message.type, message.payload);
|
|
4221
|
+
}
|
|
4222
|
+
} catch (error) {
|
|
4223
|
+
throw new BeaconConnectionError(
|
|
4224
|
+
`Failed to send message via port: ${error instanceof Error ? error.message : String(error)}`
|
|
4225
|
+
);
|
|
4226
|
+
}
|
|
4227
|
+
}
|
|
4228
|
+
/**
|
|
4229
|
+
* Send a message and wait for a specific response with retry logic
|
|
4230
|
+
*/
|
|
4231
|
+
async sendAndWaitFor(sendType, responseType, payload = {}, timeoutMs) {
|
|
4232
|
+
await this.ready();
|
|
4233
|
+
return (0, import_async_retry2.default)(
|
|
4234
|
+
async (bail) => {
|
|
4235
|
+
return new Promise((resolve, reject) => {
|
|
4236
|
+
const actualTimeout = timeoutMs || this.options.timeout;
|
|
4237
|
+
const timeoutId = setTimeout(() => {
|
|
4238
|
+
unsubscribe.dispose();
|
|
4239
|
+
const error = new BeaconTimeoutError(sendType, actualTimeout);
|
|
4240
|
+
reject(error);
|
|
4241
|
+
}, actualTimeout);
|
|
4242
|
+
const unsubscribe = this.eventEmitter.listen(responseType, (data) => {
|
|
4243
|
+
clearTimeout(timeoutId);
|
|
4244
|
+
unsubscribe.dispose();
|
|
4245
|
+
resolve(data);
|
|
4246
|
+
});
|
|
4247
|
+
try {
|
|
4248
|
+
this.sendMessage(sendType, payload);
|
|
4249
|
+
} catch (error) {
|
|
4250
|
+
clearTimeout(timeoutId);
|
|
4251
|
+
unsubscribe.dispose();
|
|
4252
|
+
if (error instanceof BeaconConnectionError) {
|
|
4253
|
+
reject(error);
|
|
4254
|
+
} else {
|
|
4255
|
+
bail(error);
|
|
4256
|
+
}
|
|
4257
|
+
}
|
|
4258
|
+
});
|
|
4259
|
+
},
|
|
4260
|
+
{
|
|
4261
|
+
retries: this.options.retry?.retries || 3,
|
|
4262
|
+
minTimeout: this.options.retry?.minTimeout || 1e3,
|
|
4263
|
+
maxTimeout: this.options.retry?.maxTimeout || 5e3,
|
|
4264
|
+
factor: this.options.retry?.factor || 2,
|
|
4265
|
+
randomize: this.options.retry?.randomize || true,
|
|
4266
|
+
onRetry: (error, attempt) => {
|
|
4267
|
+
if (this.options.debug) {
|
|
4268
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
4269
|
+
console.warn(`[Beacon SDK] Retry attempt ${attempt} for ${sendType}:`, errorMessage);
|
|
4270
|
+
}
|
|
4271
|
+
}
|
|
4272
|
+
}
|
|
4273
|
+
);
|
|
4274
|
+
}
|
|
4275
|
+
/**
|
|
4276
|
+
* Generate a unique ID for messages
|
|
4277
|
+
*/
|
|
4278
|
+
generateId() {
|
|
4279
|
+
return `sdk_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
4280
|
+
}
|
|
4281
|
+
/**
|
|
4282
|
+
* Event emitter methods (both private and public for navigator access)
|
|
4283
|
+
*/
|
|
4284
|
+
emit(event, payload) {
|
|
4285
|
+
this.eventEmitter.emit(event, payload);
|
|
4286
|
+
}
|
|
4287
|
+
/**
|
|
4288
|
+
* Start the beacon initialization process
|
|
4289
|
+
*/
|
|
4290
|
+
start() {
|
|
4291
|
+
if (this.isStarted) {
|
|
4292
|
+
return this.readyPromise || Promise.resolve();
|
|
4293
|
+
}
|
|
4294
|
+
this.isStarted = true;
|
|
4295
|
+
this.readyPromise = this.waitForBeaconReady();
|
|
4296
|
+
return this.readyPromise;
|
|
4297
|
+
}
|
|
4298
|
+
/**
|
|
4299
|
+
* Wait for the beacon to be ready
|
|
4300
|
+
*/
|
|
4301
|
+
async ready() {
|
|
4302
|
+
if (!this.isStarted) {
|
|
4303
|
+
throw new BeaconError("Beacon has not been started. Call start() first.");
|
|
4304
|
+
}
|
|
4305
|
+
if (!this.readyPromise) {
|
|
4306
|
+
throw new BeaconError("Beacon ready promise is not available");
|
|
4307
|
+
}
|
|
4308
|
+
return timeout(this.readyPromise, this.options.timeout);
|
|
4309
|
+
}
|
|
4310
|
+
/**
|
|
4311
|
+
* Check if beacon is ready
|
|
4312
|
+
*/
|
|
4313
|
+
get isReady() {
|
|
4314
|
+
return this.isBeaconReady;
|
|
4315
|
+
}
|
|
4316
|
+
/**
|
|
4317
|
+
* Listen for beacon events
|
|
4318
|
+
*/
|
|
4319
|
+
on(event, handler) {
|
|
4320
|
+
return this.eventEmitter.listen(event, handler);
|
|
4321
|
+
}
|
|
4322
|
+
/**
|
|
4323
|
+
* Listen for beacon events (one-time)
|
|
4324
|
+
*/
|
|
4325
|
+
once(event, handler) {
|
|
4326
|
+
this.eventEmitter.once(event, handler);
|
|
4327
|
+
}
|
|
4328
|
+
/**
|
|
4329
|
+
* Ping the beacon to check if it's responsive
|
|
4330
|
+
*/
|
|
4331
|
+
async ping() {
|
|
4332
|
+
try {
|
|
4333
|
+
await this.sendAndWaitFor("ping", "pong", { timestamp: Date.now() });
|
|
4334
|
+
return true;
|
|
4335
|
+
} catch (error) {
|
|
4336
|
+
if (this.options.debug) {
|
|
4337
|
+
console.warn("[Beacon SDK] Ping failed:", error);
|
|
4338
|
+
}
|
|
4339
|
+
return false;
|
|
4340
|
+
}
|
|
4341
|
+
}
|
|
4342
|
+
/**
|
|
4343
|
+
* Get debug information from the beacon
|
|
4344
|
+
*/
|
|
4345
|
+
async getDebugInfo() {
|
|
4346
|
+
return this.sendAndWaitFor("getDebugInfo", "debugInfo");
|
|
4347
|
+
}
|
|
4348
|
+
/**
|
|
4349
|
+
* Get console events from the beacon
|
|
4350
|
+
*/
|
|
4351
|
+
async getConsoleEvents() {
|
|
4352
|
+
return this.sendAndWaitFor("getConsoleEvents", "consoleEvents");
|
|
4353
|
+
}
|
|
4354
|
+
/**
|
|
4355
|
+
* Get error events from the beacon
|
|
4356
|
+
*/
|
|
4357
|
+
async getErrorEvents() {
|
|
4358
|
+
return this.sendAndWaitFor("getErrorEvents", "errorEvents");
|
|
4359
|
+
}
|
|
4360
|
+
/**
|
|
4361
|
+
* Clear console events in the beacon
|
|
4362
|
+
*/
|
|
4363
|
+
async clearConsole() {
|
|
4364
|
+
await this.ready();
|
|
4365
|
+
this.sendMessage("clearConsole");
|
|
4366
|
+
}
|
|
4367
|
+
/**
|
|
4368
|
+
* Clear error events in the beacon
|
|
4369
|
+
*/
|
|
4370
|
+
async clearErrors() {
|
|
4371
|
+
await this.ready();
|
|
4372
|
+
this.sendMessage("clearErrors");
|
|
4373
|
+
}
|
|
4374
|
+
/**
|
|
4375
|
+
* Execute code in the beacon context
|
|
4376
|
+
*/
|
|
4377
|
+
async executeCode(code) {
|
|
4378
|
+
return this.sendAndWaitFor("executeCode", "codeExecutionResult", { code });
|
|
4379
|
+
}
|
|
4380
|
+
/**
|
|
4381
|
+
* Inspect an element in the beacon context
|
|
4382
|
+
*/
|
|
4383
|
+
async inspectElement(selector) {
|
|
4384
|
+
return this.sendAndWaitFor("inspectElement", "elementInspectionResult", { selector });
|
|
4385
|
+
}
|
|
4386
|
+
/**
|
|
4387
|
+
* Fetch a URL in the beacon context
|
|
4388
|
+
*/
|
|
4389
|
+
async fetch(request) {
|
|
4390
|
+
return this.sendAndWaitFor("fetch", "fetchResult", request);
|
|
4391
|
+
}
|
|
4392
|
+
/**
|
|
4393
|
+
* Debug a page (navigate and capture debug information). It loads
|
|
4394
|
+
* the page fresh in the IFRAME so that we can capture the latest state.
|
|
4395
|
+
*/
|
|
4396
|
+
async debug(request, waitSeconds) {
|
|
4397
|
+
const targetUrl = new URL(request.path, this.iframe.src || window.location.origin).href;
|
|
4398
|
+
if (this.iframe.src !== targetUrl) {
|
|
4399
|
+
this.navigator.visit(targetUrl);
|
|
4400
|
+
await this.ready();
|
|
4401
|
+
}
|
|
4402
|
+
waitSeconds && await this.wait(waitSeconds);
|
|
4403
|
+
const timeoutMs = (request.options?.timeout || 3e4) + 5e3;
|
|
4404
|
+
return this.sendAndWaitFor("debug", "debugResult", request, timeoutMs);
|
|
4405
|
+
}
|
|
4406
|
+
async wait(forSeconds) {
|
|
4407
|
+
return new Promise((resolve) => setTimeout(resolve, forSeconds * 1e3));
|
|
4408
|
+
}
|
|
4409
|
+
/**
|
|
4410
|
+
* Navigate the iframe to a new URL
|
|
4411
|
+
*/
|
|
4412
|
+
navigate(url) {
|
|
4413
|
+
if (!this.iframe) {
|
|
4414
|
+
throw new BeaconConnectionError("Iframe not available");
|
|
4415
|
+
}
|
|
4416
|
+
try {
|
|
4417
|
+
const targetUrl = new URL(url, this.iframe.src || window.location.origin);
|
|
4418
|
+
this.iframe.src = targetUrl.href;
|
|
4419
|
+
this.isBeaconReady = false;
|
|
4420
|
+
this.channelEstablished = false;
|
|
4421
|
+
if (this.port) {
|
|
4422
|
+
this.port.close();
|
|
4423
|
+
this.port = null;
|
|
4424
|
+
}
|
|
4425
|
+
if (this.messageChannel) {
|
|
4426
|
+
this.messageChannel = null;
|
|
4427
|
+
}
|
|
4428
|
+
if (this.isStarted) {
|
|
4429
|
+
this.readyPromise = this.waitForBeaconReady();
|
|
4430
|
+
}
|
|
4431
|
+
} catch (error) {
|
|
4432
|
+
throw new BeaconConnectionError(`Invalid URL: ${url}`);
|
|
4433
|
+
}
|
|
4434
|
+
}
|
|
4435
|
+
/**
|
|
4436
|
+
* Get current iframe URL
|
|
4437
|
+
*/
|
|
4438
|
+
get url() {
|
|
4439
|
+
return this.iframe.src;
|
|
4440
|
+
}
|
|
4441
|
+
/**
|
|
4442
|
+
* Get the iframe element
|
|
4443
|
+
*/
|
|
4444
|
+
get element() {
|
|
4445
|
+
return this.iframe;
|
|
4446
|
+
}
|
|
4447
|
+
/**
|
|
4448
|
+
* Dispose of all resources
|
|
4449
|
+
*/
|
|
4450
|
+
dispose() {
|
|
4451
|
+
if (this.port) {
|
|
4452
|
+
this.port.close();
|
|
4453
|
+
this.port = null;
|
|
4454
|
+
}
|
|
4455
|
+
if (this.messageChannel) {
|
|
4456
|
+
this.messageChannel = null;
|
|
4457
|
+
}
|
|
4458
|
+
this.disposables.forEach((disposable) => disposable.dispose());
|
|
4459
|
+
this.disposables = [];
|
|
4460
|
+
this.messageHandlers.clear();
|
|
4461
|
+
this.isBeaconReady = false;
|
|
4462
|
+
this.channelEstablished = false;
|
|
4463
|
+
this.isStarted = false;
|
|
4464
|
+
this.readyPromise = null;
|
|
4465
|
+
}
|
|
4466
|
+
};
|
|
4467
|
+
function createBeacon(iframe, options) {
|
|
4468
|
+
return new Beacon(iframe, options);
|
|
4469
|
+
}
|
|
4470
|
+
function isBeaconSupported() {
|
|
4471
|
+
try {
|
|
4472
|
+
ensureBrowserEnvironment();
|
|
4473
|
+
return true;
|
|
4474
|
+
} catch {
|
|
4475
|
+
return false;
|
|
4476
|
+
}
|
|
4477
|
+
}
|
|
4478
|
+
|
|
4479
|
+
// src/index.ts
|
|
4480
|
+
var NotebookInitError = class extends Error {
|
|
4481
|
+
constructor(message) {
|
|
4482
|
+
super(message);
|
|
4483
|
+
this.message = message;
|
|
4484
|
+
}
|
|
4485
|
+
};
|
|
4486
|
+
var ApiError = class extends Error {
|
|
4487
|
+
constructor(response, body) {
|
|
4488
|
+
super(`PHPSandbox API Error: ${response.status} ${response.statusText} - ${body}`);
|
|
4489
|
+
this.response = response;
|
|
4490
|
+
this.body = body;
|
|
4491
|
+
this.status = response.status;
|
|
4492
|
+
}
|
|
4493
|
+
};
|
|
4494
|
+
var NotebookApi = class {
|
|
4495
|
+
constructor(client) {
|
|
4496
|
+
this.client = client;
|
|
4497
|
+
}
|
|
4498
|
+
async create(template, input = {}, init = true) {
|
|
4499
|
+
const response = await this.client.post("/notebook", { template, ...input });
|
|
4500
|
+
const instance = new NotebookInstance(response.data, this.client);
|
|
4501
|
+
if (!init) {
|
|
4502
|
+
return instance;
|
|
4503
|
+
}
|
|
4504
|
+
return this.init(instance);
|
|
4505
|
+
}
|
|
4506
|
+
async get(id) {
|
|
4507
|
+
const response = await this.client.get(`/notebook/${id}`);
|
|
4508
|
+
return new NotebookInstance(response.data, this.client);
|
|
4509
|
+
}
|
|
4510
|
+
async fork(id) {
|
|
4511
|
+
const response = await this.client.post(`/notebook/${id}/fork`);
|
|
4512
|
+
return this.init(new NotebookInstance(response.data, this.client));
|
|
4513
|
+
}
|
|
4514
|
+
async open(id) {
|
|
4515
|
+
const response = await this.client.get(`/notebook/${id}`);
|
|
4516
|
+
return new NotebookInstance(response.data, this.client);
|
|
4517
|
+
}
|
|
4518
|
+
openFromData(data) {
|
|
4519
|
+
return this.init(new NotebookInstance(data, this.client));
|
|
4520
|
+
}
|
|
4521
|
+
async init(instance) {
|
|
4522
|
+
await instance.ready();
|
|
4523
|
+
return instance;
|
|
4524
|
+
}
|
|
4525
|
+
};
|
|
4526
|
+
var Client = class {
|
|
4527
|
+
constructor(token, url = "https://api.phpsandbox.io/v1", options = {}) {
|
|
4528
|
+
this.fetch = globalThis.fetch;
|
|
4529
|
+
this.notebook = new NotebookApi(this);
|
|
4530
|
+
this.options = Object.assign({ startClosed: true }, options);
|
|
4531
|
+
this.baseUrl = url;
|
|
4532
|
+
if (options.fetch) {
|
|
4533
|
+
this.fetch = options.fetch;
|
|
4534
|
+
}
|
|
4535
|
+
this.headers = {
|
|
4536
|
+
"Accept": "application/json",
|
|
4537
|
+
"Authorization": `Bearer ${token}`,
|
|
4538
|
+
"Content-Type": "application/json"
|
|
4539
|
+
};
|
|
4540
|
+
}
|
|
4541
|
+
get(path) {
|
|
4542
|
+
return this.makeRequest("GET", path);
|
|
4543
|
+
}
|
|
4544
|
+
post(path, body) {
|
|
4545
|
+
return this.makeRequest("POST", path, { body: body ? JSON.stringify(body) : void 0 });
|
|
4546
|
+
}
|
|
4547
|
+
async makeRequest(method, path, init) {
|
|
4548
|
+
const response = await this.fetch(
|
|
4549
|
+
new Request(new URL(`v1/${path.replace(/^\//, "")}`, this.baseUrl), {
|
|
4550
|
+
method,
|
|
4551
|
+
...init,
|
|
4552
|
+
headers: this.headers
|
|
4553
|
+
})
|
|
4554
|
+
);
|
|
4555
|
+
if (!response.ok) {
|
|
4556
|
+
throw new ApiError(response, await response.text());
|
|
4557
|
+
}
|
|
4558
|
+
return { data: await response.json() };
|
|
4559
|
+
}
|
|
4560
|
+
};
|
|
4561
|
+
var PHPSandbox = class extends Client {
|
|
4562
|
+
};
|
|
4563
|
+
var _initPromise, _NotebookInstance_instances, init_fn;
|
|
4564
|
+
var _NotebookInstance = class _NotebookInstance {
|
|
4565
|
+
constructor(data, client) {
|
|
4566
|
+
this.data = data;
|
|
4567
|
+
this.client = client;
|
|
4568
|
+
__privateAdd(this, _NotebookInstance_instances);
|
|
4569
|
+
this.initialized = false;
|
|
4570
|
+
__privateAdd(this, _initPromise);
|
|
4571
|
+
this.disposables = [];
|
|
4572
|
+
this.emitter = EventManager.createInstance();
|
|
4573
|
+
this.socket = new Transport(data.okraUrl, this.emitter, {
|
|
4574
|
+
debug: client.options.debug,
|
|
4575
|
+
startClosed: client.options.startClosed
|
|
4576
|
+
});
|
|
4577
|
+
this.watchConnection();
|
|
4578
|
+
__privateSet(this, _initPromise, __privateMethod(this, _NotebookInstance_instances, init_fn).call(this));
|
|
4579
|
+
this.file = new Filesystem(this);
|
|
4580
|
+
this.terminal = new Terminal(this);
|
|
4581
|
+
this.auth = new Auth(this);
|
|
4582
|
+
this.lsp = new Lsp(this);
|
|
4583
|
+
this.composer = new Composer(this);
|
|
4584
|
+
this.log = new Log(this);
|
|
4585
|
+
this.repl = new Repl(this);
|
|
4586
|
+
this.container = new Container(this);
|
|
4587
|
+
this.laravel = new Lravel(this);
|
|
4588
|
+
this.shell = new Shell(this);
|
|
4589
|
+
this.git = new Git(this);
|
|
4590
|
+
}
|
|
4591
|
+
async ready() {
|
|
4592
|
+
const ready = async () => {
|
|
4593
|
+
if (this.client.options.startClosed && !this.socket.isConnected) {
|
|
4594
|
+
await this.socket.invoke("ping");
|
|
4595
|
+
}
|
|
4596
|
+
return __privateGet(this, _initPromise);
|
|
4597
|
+
};
|
|
4598
|
+
return ready();
|
|
4599
|
+
}
|
|
4600
|
+
fork() {
|
|
4601
|
+
return this.client.notebook.fork(this.data.id);
|
|
4602
|
+
}
|
|
4603
|
+
stop() {
|
|
4604
|
+
return this.container.stop();
|
|
4605
|
+
}
|
|
4606
|
+
restart() {
|
|
4607
|
+
return this.container.start();
|
|
4608
|
+
}
|
|
4609
|
+
invoke(action, data = {}, options = {}) {
|
|
4610
|
+
return this.socket.invoke(action, data || {}, options);
|
|
4611
|
+
}
|
|
4612
|
+
ping() {
|
|
4613
|
+
return this.invoke("ping");
|
|
4614
|
+
}
|
|
4615
|
+
listen(event, handler) {
|
|
4616
|
+
return this.emitter.listen(event, handler);
|
|
4617
|
+
}
|
|
4618
|
+
dispose() {
|
|
4619
|
+
this.disposables.forEach((d) => d.dispose());
|
|
4620
|
+
!this.socket.isClosed && this.socket.disconnect();
|
|
4621
|
+
}
|
|
4622
|
+
connected() {
|
|
4623
|
+
if (this.socket.isConnected) {
|
|
4624
|
+
return Promise.resolve(this);
|
|
4625
|
+
}
|
|
4626
|
+
return new Promise((resolve, reject) => {
|
|
4627
|
+
try {
|
|
4628
|
+
this.socket.onDidConnect(() => resolve(this));
|
|
4629
|
+
this.socket.onDidClose(() => reject(new Error("Connection closed")));
|
|
4630
|
+
} catch (e) {
|
|
4631
|
+
reject(e);
|
|
4632
|
+
}
|
|
4633
|
+
});
|
|
4634
|
+
}
|
|
4635
|
+
whenConnected() {
|
|
4636
|
+
if (this.socket.isConnected) {
|
|
4637
|
+
return Promise.resolve(this);
|
|
4638
|
+
}
|
|
4639
|
+
return new Promise((resolve, reject) => {
|
|
4640
|
+
try {
|
|
4641
|
+
this.socket.onDidConnect(() => resolve(this));
|
|
4642
|
+
} catch (e) {
|
|
4643
|
+
reject(e);
|
|
4644
|
+
}
|
|
4645
|
+
});
|
|
4646
|
+
}
|
|
4647
|
+
watchConnection() {
|
|
4648
|
+
this.socket.onDidConnect(() => {
|
|
4649
|
+
this.socket.emit("okra.connected");
|
|
4650
|
+
if (this.client.options.telemetry) {
|
|
4651
|
+
this.container.enableTelemetry(this.client.options.telemetry);
|
|
4652
|
+
}
|
|
4653
|
+
});
|
|
4654
|
+
this.socket.onDidClose(() => {
|
|
4655
|
+
this.socket.emit("okra.disconnected");
|
|
4656
|
+
this.initialized = false;
|
|
4657
|
+
});
|
|
4658
|
+
}
|
|
4659
|
+
onDidConnect(handler) {
|
|
4660
|
+
this.socket.removeListener("okra.connected", handler);
|
|
4661
|
+
const disposable = this.socket.listen("okra.connected", handler);
|
|
4662
|
+
this.disposables.push(disposable);
|
|
4663
|
+
return disposable;
|
|
4664
|
+
}
|
|
4665
|
+
onDidDisconnect(handler) {
|
|
4666
|
+
const disposable = this.socket.listen("okra.disconnected", handler);
|
|
4667
|
+
this.disposables.push(disposable);
|
|
4668
|
+
return disposable;
|
|
4669
|
+
}
|
|
4670
|
+
update() {
|
|
4671
|
+
return this.invoke("notebook.update");
|
|
4672
|
+
}
|
|
4673
|
+
onDidInitialize(handler) {
|
|
4674
|
+
const disposable = this.listen("notebook.initialized", handler);
|
|
4675
|
+
this.disposables.push(disposable);
|
|
4676
|
+
return disposable;
|
|
4677
|
+
}
|
|
4678
|
+
reconnect() {
|
|
4679
|
+
this.dispose();
|
|
4680
|
+
return new _NotebookInstance(this.data, this.client);
|
|
4681
|
+
}
|
|
4682
|
+
async beacon(iframe, options) {
|
|
4683
|
+
const result = await this.ready();
|
|
4684
|
+
iframe.src = result.data.previewUrl;
|
|
4685
|
+
return createBeacon(iframe, options);
|
|
4686
|
+
}
|
|
4687
|
+
};
|
|
4688
|
+
_initPromise = new WeakMap();
|
|
4689
|
+
_NotebookInstance_instances = new WeakSet();
|
|
4690
|
+
init_fn = function() {
|
|
4691
|
+
return new Promise((resolve, reject) => {
|
|
4692
|
+
this.onDidInitialize((result) => {
|
|
4693
|
+
this.initialized = result;
|
|
4694
|
+
if (result.type === "error") {
|
|
4695
|
+
reject(new NotebookInitError(result.message));
|
|
4696
|
+
}
|
|
4697
|
+
resolve(result);
|
|
4698
|
+
});
|
|
4699
|
+
});
|
|
4700
|
+
};
|
|
4701
|
+
var NotebookInstance = _NotebookInstance;
|
|
4702
|
+
export {
|
|
4703
|
+
ApiError,
|
|
4704
|
+
Beacon,
|
|
4705
|
+
BeaconConnectionError,
|
|
4706
|
+
BeaconError,
|
|
4707
|
+
BeaconTimeoutError,
|
|
4708
|
+
Client,
|
|
4709
|
+
CommandError,
|
|
4710
|
+
ErrorEvent,
|
|
4711
|
+
FileChangeType,
|
|
4712
|
+
FileType,
|
|
4713
|
+
Filesystem,
|
|
4714
|
+
FilesystemError,
|
|
4715
|
+
FilesystemErrorType,
|
|
4716
|
+
LspConnection,
|
|
4717
|
+
Navigator,
|
|
4718
|
+
NotebookApi,
|
|
4719
|
+
NotebookInitError,
|
|
4720
|
+
NotebookInstance,
|
|
4721
|
+
NotebookState,
|
|
4722
|
+
PHPSandbox,
|
|
4723
|
+
PHPSandboxError,
|
|
4724
|
+
PromiseTimeoutError,
|
|
4725
|
+
RateLimitError,
|
|
4726
|
+
Transport,
|
|
4727
|
+
createBeacon,
|
|
4728
|
+
isBeaconSupported,
|
|
4729
|
+
timeout
|
|
4730
|
+
};
|
|
4731
|
+
/*! Bundled license information:
|
|
4732
|
+
|
|
4733
|
+
reconnecting-websocket/dist/reconnecting-websocket-mjs.js:
|
|
4734
|
+
(*! *****************************************************************************
|
|
4735
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4736
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
4737
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
4738
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
4739
|
+
|
|
4740
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
4741
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
4742
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
4743
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
4744
|
+
|
|
4745
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
4746
|
+
and limitations under the License.
|
|
4747
|
+
***************************************************************************** *)
|
|
4748
|
+
(*!
|
|
4749
|
+
* Reconnecting WebSocket
|
|
4750
|
+
* by Pedro Ladaria <pedro.ladaria@gmail.com>
|
|
4751
|
+
* https://github.com/pladaria/reconnecting-websocket
|
|
4752
|
+
* License MIT
|
|
4753
|
+
*)
|
|
4754
|
+
*/
|
|
4755
|
+
//# sourceMappingURL=phpsandbox-sdk.esm.js.map
|