@intuned/runtime-dev 1.3.24-pipe.0 → 1.3.24-pipe.10
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.
|
@@ -357,9 +357,14 @@ 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) {
|
|
365
|
+
if (this.socket.closed) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
363
368
|
const dataToSend = JSON.stringify(data);
|
|
364
369
|
const length = Buffer.byteLength(dataToSend);
|
|
365
370
|
const buffer = Buffer.alloc(_SocketClient.LENGTH_HEADER_LENGTH + length);
|
|
@@ -369,21 +374,30 @@ var require_interfaceClient = __commonJS({
|
|
|
369
374
|
}
|
|
370
375
|
async *receiveJSON() {
|
|
371
376
|
let buffer = Buffer.alloc(0);
|
|
377
|
+
let endListener;
|
|
378
|
+
let rejectListener;
|
|
372
379
|
const endPromise = new Promise((resolve, reject) => {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
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());
|
|
377
387
|
});
|
|
378
388
|
while (true) {
|
|
379
389
|
const chunk = await Promise.race([
|
|
380
|
-
new Promise((resolve) =>
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
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
|
|
387
401
|
]);
|
|
388
402
|
if (!(chunk instanceof Buffer)) {
|
|
389
403
|
break;
|
|
@@ -404,8 +418,44 @@ var require_interfaceClient = __commonJS({
|
|
|
404
418
|
yield JSON.parse(data.toString());
|
|
405
419
|
}
|
|
406
420
|
}
|
|
407
|
-
async close() {
|
|
408
|
-
|
|
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
|
+
console.log(`[${process.pid}] Waiting for socket to close gracefully for 3 seconds...`);
|
|
435
|
+
const closeResult = await Promise.race([
|
|
436
|
+
new Promise((resolve) => this.socket.once("close", () => resolve("closed"))),
|
|
437
|
+
new Promise((resolve) => this.socket.once("error", () => resolve("error"))),
|
|
438
|
+
(0, promises_1.setTimeout)(3e3, "timeout")
|
|
439
|
+
]);
|
|
440
|
+
console.log(`[${process.pid}] Socket close result: ${closeResult}`);
|
|
441
|
+
if (closeResult === "timeout") {
|
|
442
|
+
console.warn(`[${process.pid}] Socket did not close within timeout, destroying socket`);
|
|
443
|
+
this.socket.destroy();
|
|
444
|
+
}
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
console.log(`[${process.pid}] Waiting for socket to end gracefully for 3 seconds...`);
|
|
448
|
+
const receiveEndResult = await Promise.race([
|
|
449
|
+
new Promise((resolve) => this.socket.once("end", () => resolve("end"))),
|
|
450
|
+
new Promise((resolve) => this.socket.once("error", () => resolve("error"))),
|
|
451
|
+
(0, promises_1.setTimeout)(3e3, "timeout")
|
|
452
|
+
]);
|
|
453
|
+
console.log(`[${process.pid}] Socket receive end result: ${receiveEndResult}`);
|
|
454
|
+
if (receiveEndResult === "end") {
|
|
455
|
+
console.log(`[${process.pid}] Socket ended gracefully`);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
console.log(`[${process.pid}] Socket did not end gracefully, destroying socket`);
|
|
409
459
|
this.socket.destroy();
|
|
410
460
|
}
|
|
411
461
|
get closed() {
|