@intuned/runtime-dev 1.3.24-pipe.1 → 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,34 +357,47 @@ 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);
|
|
366
371
|
buffer.writeUInt32BE(length, 0);
|
|
367
372
|
buffer.write(dataToSend, _SocketClient.LENGTH_HEADER_LENGTH);
|
|
368
|
-
console.log(`Sending ${dataToSend.length} bytes`);
|
|
369
373
|
await (0, util_1.promisify)((cb) => this.socket.write(buffer, cb))();
|
|
370
374
|
}
|
|
371
375
|
async *receiveJSON() {
|
|
372
376
|
let buffer = Buffer.alloc(0);
|
|
377
|
+
let endListener;
|
|
378
|
+
let rejectListener;
|
|
373
379
|
const endPromise = new Promise((resolve, reject) => {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
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());
|
|
378
387
|
});
|
|
379
388
|
while (true) {
|
|
380
389
|
const chunk = await Promise.race([
|
|
381
|
-
new Promise((resolve) =>
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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
|
|
388
401
|
]);
|
|
389
402
|
if (!(chunk instanceof Buffer)) {
|
|
390
403
|
break;
|
|
@@ -405,8 +418,44 @@ var require_interfaceClient = __commonJS({
|
|
|
405
418
|
yield JSON.parse(data.toString());
|
|
406
419
|
}
|
|
407
420
|
}
|
|
408
|
-
async close() {
|
|
409
|
-
|
|
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`);
|
|
410
459
|
this.socket.destroy();
|
|
411
460
|
}
|
|
412
461
|
get closed() {
|