@intuned/runtime-dev 1.3.24-pipe.6 → 1.3.24-pipe.8
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/dist/vendor/runtime-interface.js +59 -13
- package/package.json +1 -1
|
@@ -357,7 +357,9 @@ var require_interfaceClient = __commonJS({
|
|
|
357
357
|
var util_1 = require("util");
|
|
358
358
|
var SocketClient = class _SocketClient {
|
|
359
359
|
constructor(socket) {
|
|
360
|
+
this.dataListener = null;
|
|
360
361
|
this.socket = socket;
|
|
362
|
+
this.closeAbortController = new AbortController();
|
|
361
363
|
}
|
|
362
364
|
async sendJSON(data) {
|
|
363
365
|
if (this.socket.closed) {
|
|
@@ -372,21 +374,30 @@ var require_interfaceClient = __commonJS({
|
|
|
372
374
|
}
|
|
373
375
|
async *receiveJSON() {
|
|
374
376
|
let buffer = Buffer.alloc(0);
|
|
377
|
+
let endListener;
|
|
378
|
+
let rejectListener;
|
|
375
379
|
const endPromise = new Promise((resolve, reject) => {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
this.socket.once("error",
|
|
380
|
+
endListener = () => resolve();
|
|
381
|
+
this.socket.once("end", endListener);
|
|
382
|
+
rejectListener = (e) => reject(e);
|
|
383
|
+
this.socket.once("error", rejectListener);
|
|
384
|
+
});
|
|
385
|
+
const closePromise = new Promise((resolve) => {
|
|
386
|
+
this.closeAbortController.signal.addEventListener("abort", () => resolve());
|
|
380
387
|
});
|
|
381
388
|
while (true) {
|
|
382
389
|
const chunk = await Promise.race([
|
|
383
|
-
new Promise((resolve) =>
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
+
new Promise((resolve) => {
|
|
391
|
+
this.dataListener = (data2) => {
|
|
392
|
+
if (typeof data2 === "string") {
|
|
393
|
+
return resolve(Buffer.from(data2));
|
|
394
|
+
}
|
|
395
|
+
resolve(data2);
|
|
396
|
+
};
|
|
397
|
+
this.socket.once("data", this.dataListener);
|
|
398
|
+
}),
|
|
399
|
+
endPromise,
|
|
400
|
+
closePromise
|
|
390
401
|
]);
|
|
391
402
|
if (!(chunk instanceof Buffer)) {
|
|
392
403
|
break;
|
|
@@ -407,8 +418,43 @@ var require_interfaceClient = __commonJS({
|
|
|
407
418
|
yield JSON.parse(data.toString());
|
|
408
419
|
}
|
|
409
420
|
}
|
|
410
|
-
async close() {
|
|
411
|
-
|
|
421
|
+
async close(mode = "client") {
|
|
422
|
+
console.log(`[${process.pid}] Closing socket client`);
|
|
423
|
+
if (this.dataListener) {
|
|
424
|
+
this.socket.off("data", this.dataListener);
|
|
425
|
+
console.log(`[${process.pid}] Removed data listener`);
|
|
426
|
+
} else {
|
|
427
|
+
console.log(`[${process.pid}] No data listener to remove`);
|
|
428
|
+
}
|
|
429
|
+
this.closeAbortController.abort();
|
|
430
|
+
console.log(`[${process.pid}] Called abort on closeAbortController`);
|
|
431
|
+
if (mode === "client") {
|
|
432
|
+
console.log(`[${process.pid}] Sending FIN to socket`);
|
|
433
|
+
await (0, util_1.promisify)((cb) => this.socket.end(cb))();
|
|
434
|
+
const closeResult = await Promise.race([
|
|
435
|
+
new Promise((resolve) => this.socket.once("close", () => resolve("closed"))),
|
|
436
|
+
new Promise((resolve) => this.socket.once("error", () => resolve("error"))),
|
|
437
|
+
(0, promises_1.setTimeout)(3e3, "timeout")
|
|
438
|
+
]);
|
|
439
|
+
console.log(`[${process.pid}] Socket close result: ${closeResult}`);
|
|
440
|
+
if (closeResult === "timeout") {
|
|
441
|
+
console.warn(`[${process.pid}] Socket did not close within timeout, destroying socket`);
|
|
442
|
+
this.socket.destroy();
|
|
443
|
+
}
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const receiveEndResult = await Promise.race([
|
|
447
|
+
new Promise((resolve) => this.socket.once("end", () => resolve("end"))),
|
|
448
|
+
new Promise((resolve) => this.socket.once("error", () => resolve("error"))),
|
|
449
|
+
(0, promises_1.setTimeout)(3e3, "timeout")
|
|
450
|
+
]);
|
|
451
|
+
console.log(`[${process.pid}] Socket receive end result: ${receiveEndResult}`);
|
|
452
|
+
if (receiveEndResult === "end") {
|
|
453
|
+
console.log(`[${process.pid}] Socket ended gracefully`);
|
|
454
|
+
await (0, util_1.promisify)((cb) => this.socket.end(cb))();
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
console.log(`[${process.pid}] Socket did not end gracefully, destroying socket`);
|
|
412
458
|
this.socket.destroy();
|
|
413
459
|
}
|
|
414
460
|
get closed() {
|