2g 0.4.2 → 0.4.3

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/2g.js CHANGED
@@ -8,64 +8,66 @@ var t = require("node:perf_hooks");
8
8
 
9
9
  var i = require("./chunks/sessionSockets-chunk.js");
10
10
 
11
- var s = require("node:fs");
11
+ var n = require("node:fs");
12
12
 
13
- var n = require("node:net");
13
+ var s = require("node:net");
14
14
 
15
15
  var r = require("./chunks/pretty-chunk.js");
16
16
 
17
- var o = require("node:buffer");
17
+ var o = require("node:os");
18
18
 
19
- var c = require("node:events");
19
+ var c = require("node:buffer");
20
20
 
21
- var l = require("node:worker_threads");
21
+ var l = require("node:events");
22
22
 
23
- const h = Symbol.for(`2g/state-${i.EVENT_LOG_STATE_VERSION}`);
23
+ var h = require("node:worker_threads");
24
+
25
+ const u = Symbol.for(`2g/state-${i.EVENT_LOG_STATE_VERSION}`);
24
26
 
25
27
  const a = function getEventLogState() {
26
28
  const e = globalThis;
27
- return e[h] ??= {
29
+ return e[u] ??= {
28
30
  logPath: process.cwd(),
29
31
  eventLoggerInfo: null,
30
32
  debug: !1
31
33
  };
32
34
  }();
33
35
 
34
- const u = t.performance.timeOrigin;
36
+ const d = t.performance.timeOrigin;
35
37
 
36
- const d = Array.from({
38
+ const f = Array.from({
37
39
  length: 1e3
38
40
  }, (e, t) => `.${`${t}`.padStart(3, "0")}`);
39
41
 
40
- let f = -1;
42
+ let p = -1;
41
43
 
42
44
  let g = "";
43
45
 
44
- let p;
46
+ let y;
45
47
 
46
- let y = "";
48
+ let m = "";
47
49
 
48
- function writeEvent(e, t, i, s, n, r) {
50
+ function writeEvent(e, t, i, n, s, r) {
49
51
  try {
50
- e._writeln(function serializeEvent(e, t, i, s, n, r) {
52
+ e._writeln(function serializeEvent(e, t, i, n, s, r) {
51
53
  const o = stringifyTimestamp();
52
- if (!s?._w && !r) {
53
- const s = i ? JSON.stringify(i).slice(1) : "";
54
- return s.length > 1 ? `{"_e":"${e}:${t}","_t":${o},${s}\n` : `{"_e":"${e}:${t}","_t":${o}}\n`;
54
+ if (!n?._w && !r) {
55
+ const n = i ? JSON.stringify(i).slice(1) : "";
56
+ return n.length > 1 ? `{"_e":"${e}:${t}","_t":${o},${n}\n` : `{"_e":"${e}:${t}","_t":${o}}\n`;
55
57
  }
56
58
  let c = `{"_e":"${e}:${t}","_t":${o}`;
57
59
  if (r) {
58
60
  c += `,"_l":${r}`;
59
61
  }
60
- if (s?._w) {
61
- c += `,"_w":${stringifyWorkerId(s._w)}`;
62
+ if (n?._w) {
63
+ c += `,"_w":${stringifyWorkerId(n._w)}`;
62
64
  }
63
65
  return `${appendPayload(c, i)}}\n`;
64
- }(t, i, s, n, 0, r));
66
+ }(t, i, n, s, 0, r));
65
67
  } catch {}
66
68
  }
67
69
 
68
- function writeCompleteEvent(e, t, i, s, n, r, o, c) {
70
+ function writeCompleteEvent(e, t, i, n, s, r, o, c) {
69
71
  try {
70
72
  let l = `{"_e":"${t}:${i}","_t":${stringifyTimestamp()},"_d":${r}`;
71
73
  if (c) {
@@ -79,26 +81,26 @@ function writeCompleteEvent(e, t, i, s, n, r, o, c) {
79
81
  return appendPayload(e, i);
80
82
  }
81
83
  return appendPayload(e, t);
82
- }(l, s, n)}}\n`);
84
+ }(l, n, s)}}\n`);
83
85
  } catch {}
84
86
  }
85
87
 
86
88
  function stringifyTimestamp() {
87
- const e = u + t.performance.now();
89
+ const e = d + t.performance.now();
88
90
  const i = Math.floor(e);
89
- if (i !== f) {
90
- f = i;
91
+ if (i !== p) {
92
+ p = i;
91
93
  g = `${i}`;
92
94
  }
93
- return g + d[1e3 * (e - i) | 0];
95
+ return g + f[1e3 * (e - i) | 0];
94
96
  }
95
97
 
96
98
  function stringifyWorkerId(e) {
97
- if (e !== p) {
98
- p = e;
99
- y = JSON.stringify(e);
99
+ if (e !== y) {
100
+ y = e;
101
+ m = JSON.stringify(e);
100
102
  }
101
- return y;
103
+ return m;
102
104
  }
103
105
 
104
106
  function appendPayload(e, t) {
@@ -122,13 +124,13 @@ function serializeError(e, t = 0) {
122
124
  name: e.name,
123
125
  message: e.message
124
126
  };
125
- const s = e.code;
126
- if ("string" == typeof s) {
127
- i.code = s;
127
+ const n = e.code;
128
+ if ("string" == typeof n) {
129
+ i.code = n;
128
130
  }
129
- const n = e.stack;
130
- if (n) {
131
- i.stack = n;
131
+ const s = e.stack;
132
+ if (s) {
133
+ i.stack = s;
132
134
  }
133
135
  if (null != e.cause && t < 4) {
134
136
  i.cause = serializeError(e.cause, t + 1);
@@ -136,22 +138,22 @@ function serializeError(e, t = 0) {
136
138
  return i;
137
139
  }
138
140
 
139
- function createEventLogger(i, s) {
141
+ function createEventLogger(i, n) {
140
142
  function log(e, t) {
141
- if (a.primarySink && (!s || a.debug)) {
142
- writeEvent(a.primarySink, i, e, t, a.eventMeta, s);
143
+ if (a.primarySink && (!n || a.debug)) {
144
+ writeEvent(a.primarySink, i, e, t, a.eventMeta, n);
143
145
  }
144
146
  }
145
147
  log.span = function span() {
146
- if (!a.primarySink || s && !a.debug) {
148
+ if (!a.primarySink || n && !a.debug) {
147
149
  return NOOP_DONE;
148
150
  }
149
151
  const e = t.performance.now();
150
- return function done(n, r) {
152
+ return function done(s, r) {
151
153
  if (!a.primarySink) {
152
154
  return;
153
155
  }
154
- writeCompleteEvent(a.primarySink, i, n, r, void 0, t.performance.now() - e, a.eventMeta, s);
156
+ writeCompleteEvent(a.primarySink, i, s, r, void 0, t.performance.now() - e, a.eventMeta, n);
155
157
  };
156
158
  };
157
159
  log.path = function relativePath(t) {
@@ -193,17 +195,17 @@ function createDebugSink(e = process.env[i.LOG_DEBUG_ENV]) {
193
195
  if (!t) {
194
196
  return null;
195
197
  }
196
- const s = r.shouldColorizeStream(process.stderr);
198
+ const n = r.shouldColorizeStream(process.stderr);
197
199
  return {
198
200
  get writable() {
199
201
  return !0;
200
202
  },
201
203
  _writeln(e) {
202
- let n = 0;
204
+ let s = 0;
203
205
  let o;
204
- while (-1 !== (o = e.indexOf("\n", n))) {
205
- const c = e.slice(n, o);
206
- n = o + 1;
206
+ while (-1 !== (o = e.indexOf("\n", s))) {
207
+ const c = e.slice(s, o);
208
+ s = o + 1;
207
209
  if (!c) {
208
210
  continue;
209
211
  }
@@ -216,7 +218,7 @@ function createDebugSink(e = process.env[i.LOG_DEBUG_ENV]) {
216
218
  continue;
217
219
  }
218
220
  process.stderr.write(`${r.formatPrettyEvent(l, {
219
- colorize: s,
221
+ colorize: n,
220
222
  stream: process.stderr
221
223
  })}\n`);
222
224
  }
@@ -230,49 +232,202 @@ function createDebugSink(e = process.env[i.LOG_DEBUG_ENV]) {
230
232
  };
231
233
  }
232
234
 
233
- class LogStream extends c.EventEmitter {
235
+ const S = Symbol.for("2g/process-cleanup");
236
+
237
+ function registerProcessCleanup(e) {
238
+ const t = getProcessCleanupState();
239
+ t.callbacks.add(e);
240
+ if (t.registered) {
241
+ return;
242
+ }
243
+ t.registered = !0;
244
+ process.once("exit", runProcessCleanup);
245
+ for (const e of [ "SIGINT", "SIGTERM", "SIGHUP" ]) {
246
+ process.once(e, () => {
247
+ if (process.listenerCount(e) > 0) {
248
+ return;
249
+ }
250
+ runProcessCleanup();
251
+ process.kill(process.pid, e);
252
+ });
253
+ }
254
+ }
255
+
256
+ function getProcessCleanupState() {
257
+ const e = globalThis;
258
+ return e[S] ??= {
259
+ registered: !1,
260
+ callbacks: new Set
261
+ };
262
+ }
263
+
264
+ function runProcessCleanup() {
265
+ for (const e of getProcessCleanupState().callbacks) {
266
+ try {
267
+ e();
268
+ } catch {}
269
+ }
270
+ }
271
+
272
+ function listenIpcSink(e, t) {
273
+ const n = s.createServer(t => ingestIpcSocket(t, e));
274
+ const r = i.listenSocket(n, t);
275
+ const o = function publishIpcPath(e) {
276
+ process.env[i.INTERNAL_IPC_ENV] = e;
277
+ return () => {
278
+ if (process.env[i.INTERNAL_IPC_ENV] === e) {
279
+ delete process.env[i.INTERNAL_IPC_ENV];
280
+ }
281
+ };
282
+ }(t);
283
+ const c = a.debug;
284
+ if (c) {
285
+ process.env[i.INTERNAL_DEBUG_ENV] = "1";
286
+ }
287
+ delete process.env[i.LOG_DEBUG_ENV];
288
+ delete process.env[i.LOG_EVENTS_ENV];
289
+ return () => {
290
+ r();
291
+ o();
292
+ if (c) {
293
+ delete process.env[i.INTERNAL_DEBUG_ENV];
294
+ }
295
+ };
296
+ }
297
+
298
+ function publishTempIpcSink(t) {
299
+ const n = function createTempIpcPath() {
300
+ const t = Date.now().toString(36);
301
+ const i = Math.random().toString(36).slice(2, 10);
302
+ const n = `${process.pid}-${t}-${i}`;
303
+ return "win32" === process.platform ? `\\\\.\\pipe\\event-log-${n}-ipc` : e.join(o.tmpdir(), `event-log-${n}.sock`);
304
+ }();
305
+ const s = listenIpcSink(t, n);
306
+ let r = !1;
307
+ const cleanup = () => {
308
+ if (r) {
309
+ return;
310
+ }
311
+ r = !0;
312
+ s();
313
+ i.removeSocket(n);
314
+ };
315
+ const c = t.destroy.bind(t);
316
+ t.destroy = () => {
317
+ cleanup();
318
+ c();
319
+ };
320
+ t.once("close", cleanup);
321
+ registerProcessCleanup(cleanup);
322
+ }
323
+
324
+ function openIpc(e, t = {}) {
325
+ const i = t.retryMs ?? 50;
326
+ const n = t.maxRetryMs ?? 750;
327
+ return (t, r) => {
328
+ const o = Date.now();
329
+ const attempt = () => {
330
+ const c = s.connect(e);
331
+ let l = !1;
332
+ c.once("connect", () => {
333
+ const e = c._handle?.fd;
334
+ l = !0;
335
+ c.unref();
336
+ c.once("close", () => r.destroy());
337
+ const drain = (e, t) => {
338
+ c.ref();
339
+ c.write(e, e => {
340
+ c.unref();
341
+ t(e);
342
+ });
343
+ };
344
+ if ("number" == typeof e && e >= 0) {
345
+ t(null, e, drain);
346
+ } else {
347
+ t(null, null, drain);
348
+ }
349
+ });
350
+ c.once("error", e => {
351
+ if (l) {
352
+ return;
353
+ }
354
+ c.destroy();
355
+ ((e, s) => {
356
+ if (Date.now() - o > n) {
357
+ t(e);
358
+ } else {
359
+ setTimeout(s, i);
360
+ }
361
+ })(e, attempt);
362
+ });
363
+ };
364
+ attempt();
365
+ };
366
+ }
367
+
368
+ function ingestIpcSocket(e, t) {
369
+ let i = "";
370
+ e.unref();
371
+ e.setEncoding("utf8");
372
+ e.on("data", e => {
373
+ const n = i ? i + e : e;
374
+ const s = n.lastIndexOf("\n");
375
+ if (s === n.length - 1) {
376
+ i = "";
377
+ t._writeln(n);
378
+ } else if (-1 === s) {
379
+ i = n;
380
+ } else {
381
+ i = n.slice(s + 1);
382
+ t._writeln(n.slice(0, s + 1));
383
+ }
384
+ });
385
+ e.on("error", () => {});
386
+ }
387
+
388
+ class LogStream extends l.EventEmitter {
234
389
  #e=-1;
235
390
  #t=null;
236
391
  #i=null;
237
- #s=!0;
238
- #n=!1;
392
+ #n=!0;
393
+ #s=!1;
239
394
  #r=!1;
240
395
  #o=!1;
241
396
  #c=!1;
242
397
  #l=!1;
243
398
  #h=!1;
244
- #a=!1;
245
- #u="";
399
+ #u=!1;
400
+ #a="";
246
401
  #d=0;
247
402
  #f=[];
248
- #g=0;
249
403
  #p=0;
404
+ #g=0;
250
405
  #y=0;
251
406
  #m=!1;
252
407
  #S=!1;
253
- #v;
254
- #E=(e, t) => {
255
- this.#w(e, t);
408
+ #E;
409
+ #v=(e, t) => {
410
+ this.#_(e, t);
256
411
  };
257
- #_=e => {
412
+ #k=e => {
258
413
  if (null != e) {
259
- this.#k(e);
414
+ this.#w(e);
260
415
  } else {
261
- this.#w(null, o.Buffer.byteLength(this.#u));
416
+ this.#_(null, c.Buffer.byteLength(this.#a));
262
417
  }
263
418
  };
264
419
  #L=() => {
265
420
  this.#S = !1;
266
- if (!this.#n && !this.#l && (this.#f.length - this.#g > this.#p || this.#u)) {
421
+ if (!this.#s && !this.#l && (this.#f.length - this.#p > this.#g || this.#a)) {
267
422
  this.#b();
268
423
  }
269
424
  };
270
425
  constructor(e, t = {}) {
271
426
  super();
272
- this.#s = !1 !== t.closeFd;
427
+ this.#n = !1 !== t.closeFd;
273
428
  if ("number" == typeof e) {
274
429
  try {
275
- s.fsyncSync(e);
430
+ n.fsyncSync(e);
276
431
  } catch {}
277
432
  this.#e = e;
278
433
  process.nextTick(() => this.emit("ready"));
@@ -289,7 +444,7 @@ class LogStream extends c.EventEmitter {
289
444
  return this.#e;
290
445
  }
291
446
  get writing() {
292
- return this.#n;
447
+ return this.#s;
293
448
  }
294
449
  get buffered() {
295
450
  return this.#d;
@@ -311,23 +466,23 @@ class LogStream extends c.EventEmitter {
311
466
  this.once("ready", () => this.reopen(e));
312
467
  return;
313
468
  }
314
- this.#a = !0;
315
- if (!this.#n) {
316
- this.#O();
469
+ this.#u = !0;
470
+ if (!this.#s) {
471
+ this.#P();
317
472
  }
318
473
  }
319
- #O() {
474
+ #P() {
320
475
  if (null == this.#t) {
321
476
  return;
322
477
  }
323
- this.#a = !1;
478
+ this.#u = !1;
324
479
  this.#i = this.#e;
325
480
  this.#I(this.#t);
326
481
  }
327
- #P(e) {
328
- if ("EAGAIN" === e.code && null != this.#v) {
482
+ #O(e) {
483
+ if ("EAGAIN" === e.code && null != this.#E) {
329
484
  this.#m = !0;
330
- this.#v(this.#u, this.#_);
485
+ this.#E(this.#a, this.#k);
331
486
  } else if (0 === this.#y) {
332
487
  this.#y = 1;
333
488
  setImmediate(() => this.#b());
@@ -336,45 +491,45 @@ class LogStream extends c.EventEmitter {
336
491
  setTimeout(() => this.#b(), this.#y);
337
492
  }
338
493
  }
339
- #w(e, t) {
494
+ #_(e, t) {
340
495
  if (e) {
341
496
  if ("EAGAIN" === e.code || "EBUSY" === e.code) {
342
- this.#P(e);
497
+ this.#O(e);
343
498
  } else {
344
- this.#k(e);
499
+ this.#w(e);
345
500
  }
346
501
  return;
347
502
  }
348
503
  this.#y = 0;
349
504
  this.emit("write", t);
350
- if (t === this.#u.length) {
351
- this.#d -= this.#u.length;
352
- this.#u = "";
505
+ if (t === this.#a.length) {
506
+ this.#d -= this.#a.length;
507
+ this.#a = "";
353
508
  } else {
354
- if (o.Buffer.byteLength(this.#u) > t) {
355
- const e = o.Buffer.from(this.#u).toString("utf8", t);
356
- this.#d -= this.#u.length - e.length;
357
- this.#u = e;
509
+ if (c.Buffer.byteLength(this.#a) > t) {
510
+ const e = c.Buffer.from(this.#a).toString("utf8", t);
511
+ this.#d -= this.#a.length - e.length;
512
+ this.#a = e;
358
513
  } else {
359
- this.#d -= this.#u.length;
360
- this.#u = "";
514
+ this.#d -= this.#a.length;
515
+ this.#a = "";
361
516
  }
362
517
  }
363
- if (this.#u) {
518
+ if (this.#a) {
364
519
  this.#b();
365
520
  } else if (this.#o && !this.#r) {
366
- this.#n = !1;
521
+ this.#s = !1;
367
522
  this.#T();
368
- } else if (this.#f.length - this.#g > this.#p) {
523
+ } else if (this.#f.length - this.#p > this.#g) {
369
524
  this.#b();
370
- } else if (this.#a) {
371
- this.#n = !1;
372
- this.#O();
525
+ } else if (this.#u) {
526
+ this.#s = !1;
527
+ this.#P();
373
528
  } else if (this.#r) {
374
- this.#n = !1;
529
+ this.#s = !1;
375
530
  this.#T();
376
531
  } else {
377
- this.#n = !1;
532
+ this.#s = !1;
378
533
  this.#m = !1;
379
534
  if (this.#c) {
380
535
  this.emit("drain");
@@ -383,15 +538,15 @@ class LogStream extends c.EventEmitter {
383
538
  }
384
539
  #I(t) {
385
540
  this.#N(i => {
386
- s.mkdir(e.dirname(t), {
541
+ n.mkdir(e.dirname(t), {
387
542
  recursive: !0
388
543
  }, () => {
389
- s.open(t, "a", 384, (e, t) => {
544
+ n.open(t, "a", 384, (e, t) => {
390
545
  if (e || null == t) {
391
546
  i(e);
392
547
  return;
393
548
  }
394
- s.fchmod(t, 384, () => {
549
+ n.fchmod(t, 384, () => {
395
550
  i(null, t);
396
551
  });
397
552
  });
@@ -400,29 +555,29 @@ class LogStream extends c.EventEmitter {
400
555
  }
401
556
  #N(e, t) {
402
557
  this.#h = !0;
403
- this.#n = !0;
404
- e((e, i, n) => {
558
+ this.#s = !0;
559
+ e((e, i, s) => {
405
560
  const r = this.#i;
406
561
  this.#i = null;
407
562
  if (e) {
408
563
  this.#h = !1;
409
- this.#k(e);
564
+ this.#w(e);
410
565
  } else {
411
566
  this.#e = i ?? -1;
412
567
  this.#t = t;
413
- this.#v = n;
568
+ this.#E = s;
414
569
  this.#h = !1;
415
- this.#n = !1;
570
+ this.#s = !1;
416
571
  this.emit("ready");
417
572
  if (null != r && r !== this.#e && !isStdFd(r)) {
418
- fsFsync(r, () => s.close(r, () => {}));
573
+ fsFsync(r, () => n.close(r, () => {}));
419
574
  }
420
575
  if (this.#l) {
421
576
  return;
422
577
  }
423
578
  if (this.#o) {
424
579
  this.#T();
425
- } else if (!this.#n && this.#f.length - this.#g > this.#p || this.#c) {
580
+ } else if (!this.#s && this.#f.length - this.#p > this.#g || this.#c) {
426
581
  this.#b();
427
582
  }
428
583
  }
@@ -434,15 +589,15 @@ class LogStream extends c.EventEmitter {
434
589
  return;
435
590
  }
436
591
  this.#l = !0;
437
- this.#p = 0;
438
- this.#f.length = 0;
439
592
  this.#g = 0;
593
+ this.#f.length = 0;
594
+ this.#p = 0;
440
595
  const onClose = e => {
441
596
  if (e) {
442
597
  this.emit("error", e);
443
598
  this.emit("close", e);
444
599
  } else {
445
- if (this.#r && !this.#n) {
600
+ if (this.#r && !this.#s) {
446
601
  this.emit("finish");
447
602
  }
448
603
  this.emit("close");
@@ -453,15 +608,15 @@ class LogStream extends c.EventEmitter {
453
608
  return;
454
609
  }
455
610
  fsFsync(this.#e, e => {
456
- if (!e && this.#s && !isStdFd(this.#e)) {
457
- s.close(this.#e, onClose);
611
+ if (!e && this.#n && !isStdFd(this.#e)) {
612
+ n.close(this.#e, onClose);
458
613
  } else {
459
614
  onClose();
460
615
  }
461
616
  });
462
617
  }
463
- #k(e) {
464
- this.#n = !1;
618
+ #w(e) {
619
+ this.#s = !1;
465
620
  if (this.listenerCount("error")) {
466
621
  this.emit("error", e);
467
622
  }
@@ -474,24 +629,24 @@ class LogStream extends c.EventEmitter {
474
629
  }
475
630
  }
476
631
  #b() {
477
- this.#n = !0;
478
- if (!this.#u) {
479
- const e = this.#f.length - this.#p;
480
- if (e > this.#g) {
481
- this.#u = this.#f[this.#g++] || "";
482
- while (this.#g < e && this.#u.length < 65536) {
483
- this.#u += this.#f[this.#g++];
632
+ this.#s = !0;
633
+ if (!this.#a) {
634
+ const e = this.#f.length - this.#g;
635
+ if (e > this.#p) {
636
+ this.#a = this.#f[this.#p++] || "";
637
+ while (this.#p < e && this.#a.length < 65536) {
638
+ this.#a += this.#f[this.#p++];
484
639
  }
485
- if (this.#g === this.#f.length) {
640
+ if (this.#p === this.#f.length) {
486
641
  this.#f.length = 0;
487
- this.#g = 0;
642
+ this.#p = 0;
488
643
  }
489
644
  }
490
645
  }
491
- if (null != this.#v && (this.#m || this.#e < 0)) {
492
- this.#v(this.#u, this.#_);
646
+ if (null != this.#E && (this.#m || this.#e < 0)) {
647
+ this.#E(this.#a, this.#k);
493
648
  } else {
494
- s.write(this.#e, this.#u, this.#E);
649
+ n.write(this.#e, this.#a, this.#v);
495
650
  }
496
651
  }
497
652
  _end() {
@@ -499,8 +654,8 @@ class LogStream extends c.EventEmitter {
499
654
  this.#r = !0;
500
655
  if (this.#h) {
501
656
  this.once("ready", () => this._end());
502
- } else if (!this.#n && this.#e >= 0) {
503
- if (this.#f.length - this.#g > this.#p) {
657
+ } else if (!this.#s && this.#e >= 0) {
658
+ if (this.#f.length - this.#p > this.#g) {
504
659
  this.#b();
505
660
  } else {
506
661
  this.#T();
@@ -510,13 +665,13 @@ class LogStream extends c.EventEmitter {
510
665
  return this;
511
666
  }
512
667
  end(e, t, i) {
513
- const s = i || t || e;
514
- const n = "function" != typeof e ? e : void 0;
515
- const r = "function" == typeof s ? s : void 0;
516
- if ("string" == typeof n) {
517
- this.write(n, "string" == typeof t ? t : "utf8");
518
- } else if (null != n) {
519
- this.write(n);
668
+ const n = i || t || e;
669
+ const s = "function" != typeof e ? e : void 0;
670
+ const r = "function" == typeof n ? n : void 0;
671
+ if ("string" == typeof s) {
672
+ this.write(s, "string" == typeof t ? t : "utf8");
673
+ } else if (null != s) {
674
+ this.write(s);
520
675
  }
521
676
  if (r) {
522
677
  this.once("close", r);
@@ -527,9 +682,9 @@ class LogStream extends c.EventEmitter {
527
682
  if (this.#l || this.#o) {
528
683
  return;
529
684
  }
530
- if (this.#n || this.#h) {
685
+ if (this.#s || this.#h) {
531
686
  this.#o = !0;
532
- } else if (this.#f.length - this.#g > this.#p) {
687
+ } else if (this.#f.length - this.#p > this.#g) {
533
688
  this.#o = !0;
534
689
  this.#b();
535
690
  } else {
@@ -564,8 +719,8 @@ class LogStream extends c.EventEmitter {
564
719
  this.#c = !0;
565
720
  this.once("drain", onDrain);
566
721
  this.once("error", onError);
567
- if (!this.#n) {
568
- if (this.#f.length - this.#g > this.#p || this.#u) {
722
+ if (!this.#s) {
723
+ if (this.#f.length - this.#p > this.#g || this.#a) {
569
724
  this.#b();
570
725
  } else {
571
726
  process.nextTick(() => this.emit("drain"));
@@ -579,7 +734,7 @@ class LogStream extends c.EventEmitter {
579
734
  }
580
735
  this.#d += e.length;
581
736
  this.#f.push(e);
582
- if (!this.#n) {
737
+ if (!this.#s) {
583
738
  this.#$();
584
739
  }
585
740
  return this.#d < 16384;
@@ -588,45 +743,45 @@ class LogStream extends c.EventEmitter {
588
743
  if (this.#l) {
589
744
  return !1;
590
745
  }
591
- if (0 === this.#p && 10 === e.charCodeAt(e.length - 1)) {
746
+ if (0 === this.#g && 10 === e.charCodeAt(e.length - 1)) {
592
747
  return this._writeln(e);
593
748
  }
594
749
  this.#d += e.length;
595
750
  let t = 0;
596
751
  let i = -1;
597
752
  while ((i = e.indexOf("\n", t)) > -1) {
598
- const s = e.slice(t, i + 1);
599
- if (this.#p > 0) {
600
- this.#f[this.#f.length - 1] += s;
753
+ const n = e.slice(t, i + 1);
754
+ if (this.#g > 0) {
755
+ this.#f[this.#f.length - 1] += n;
601
756
  } else {
602
- this.#f.push(s);
757
+ this.#f.push(n);
603
758
  }
604
- this.#p = 0;
759
+ this.#g = 0;
605
760
  t = ++i;
606
761
  }
607
762
  if (t < e.length) {
608
763
  const i = e.slice(t);
609
- if (this.#p > 0) {
764
+ if (this.#g > 0) {
610
765
  this.#f[this.#f.length - 1] += i;
611
766
  } else {
612
767
  this.#f.push(e.slice(t));
613
768
  }
614
- this.#p = 1;
769
+ this.#g = 1;
615
770
  }
616
- if (!this.#n && this.#f.length - this.#g > this.#p) {
771
+ if (!this.#s && this.#f.length - this.#p > this.#g) {
617
772
  this.#$();
618
773
  }
619
774
  return this.#d < 16384;
620
775
  }
621
776
  write(e, t, i) {
622
- const s = i || t;
623
- const n = "string" == typeof t ? t : "utf8";
624
- const r = "string" == typeof e ? e : o.Buffer.from(e).toString(n);
625
- const c = "function" == typeof s ? s : void 0;
777
+ const n = i || t;
778
+ const s = "string" == typeof t ? t : "utf8";
779
+ const r = "string" == typeof e ? e : c.Buffer.from(e).toString(s);
780
+ const o = "function" == typeof n ? n : void 0;
626
781
  try {
627
782
  return this._write(r);
628
783
  } finally {
629
- c?.();
784
+ o?.();
630
785
  }
631
786
  }
632
787
  [Symbol.dispose]() {
@@ -649,7 +804,7 @@ const isStdFd = e => {
649
804
 
650
805
  const fsFsync = (e, t) => {
651
806
  try {
652
- s.fsync(e, t);
807
+ n.fsync(e, t);
653
808
  } catch (e) {
654
809
  t(e);
655
810
  }
@@ -680,12 +835,12 @@ class SocketSink {
680
835
  }
681
836
 
682
837
  class BroadcastChannel {
683
- #C;
684
- #D=[];
838
+ #D;
839
+ #V=[];
685
840
  #l=!1;
686
841
  constructor(e) {
687
- this.#C = e;
688
- this.#D.push({
842
+ this.#D = e;
843
+ this.#V.push({
689
844
  sink: e,
690
845
  dropOnBackpressure: !1,
691
846
  destroy: () => e.destroy()
@@ -695,8 +850,8 @@ class BroadcastChannel {
695
850
  if (this.#l) {
696
851
  return !1;
697
852
  }
698
- for (let e = 0; e < this.#D.length; e++) {
699
- if (this.#D[e].sink.writable) {
853
+ for (let e = 0; e < this.#V.length; e++) {
854
+ if (this.#V[e].sink.writable) {
700
855
  return !0;
701
856
  }
702
857
  }
@@ -709,7 +864,7 @@ class BroadcastChannel {
709
864
  if (this.#l) {
710
865
  e.destroy();
711
866
  } else {
712
- this.#D.push({
867
+ this.#V.push({
713
868
  sink: e,
714
869
  dropOnBackpressure: !1,
715
870
  destroy: () => e.destroy()
@@ -741,11 +896,11 @@ class BroadcastChannel {
741
896
  e.destroy();
742
897
  }
743
898
  };
744
- this.#D.push(i);
899
+ this.#V.push(i);
745
900
  const detach = () => {
746
- const e = this.#D.indexOf(i);
901
+ const e = this.#V.indexOf(i);
747
902
  if (e > -1) {
748
- this.#D.splice(e, 1);
903
+ this.#V.splice(e, 1);
749
904
  }
750
905
  i.destroy();
751
906
  };
@@ -753,123 +908,63 @@ class BroadcastChannel {
753
908
  e.on("error", detach);
754
909
  }
755
910
  ingest(e) {
756
- let t = "";
757
- e.unref();
758
- e.setEncoding("utf8");
759
- e.on("data", e => {
760
- const i = t ? t + e : e;
761
- const s = i.lastIndexOf("\n");
762
- if (s === i.length - 1) {
763
- t = "";
764
- this._writeln(i);
765
- } else if (-1 === s) {
766
- t = i;
767
- } else {
768
- t = i.slice(s + 1);
769
- this._writeln(i.slice(0, s + 1));
770
- }
771
- });
772
- e.on("error", () => {});
911
+ ingestIpcSocket(e, this);
773
912
  }
774
913
  _writeln(e) {
775
- const t = this.#D;
914
+ const t = this.#V;
776
915
  let i = !1;
777
- for (let s = t.length - 1; s >= 0; s--) {
778
- const n = t[s];
779
- const r = n.sink._writeln(e);
780
- if (!r && n.dropOnBackpressure && (n.sink.buffered ?? 1 / 0) > 4194304) {
781
- t.splice(s, 1);
782
- n.destroy();
916
+ for (let n = t.length - 1; n >= 0; n--) {
917
+ const s = t[n];
918
+ const r = s.sink._writeln(e);
919
+ if (!r && s.dropOnBackpressure && (s.sink.buffered ?? 1 / 0) > 4194304) {
920
+ t.splice(n, 1);
921
+ s.destroy();
783
922
  }
784
923
  i = r || i;
785
924
  }
786
925
  return i;
787
926
  }
788
927
  flush(e) {
789
- if (this.#C.flush) {
790
- this.#C.flush(e);
928
+ if (this.#D.flush) {
929
+ this.#D.flush(e);
791
930
  } else {
792
931
  e();
793
932
  }
794
933
  }
795
934
  end(e) {
796
- for (const t of this.#D) {
797
- t.sink.end(t.sink === this.#C ? e : void 0);
935
+ for (const t of this.#V) {
936
+ t.sink.end(t.sink === this.#D ? e : void 0);
798
937
  }
799
938
  return this;
800
939
  }
801
940
  destroy() {
802
941
  this.#l = !0;
803
- for (const e of this.#D) {
942
+ for (const e of this.#V) {
804
943
  e.destroy();
805
944
  }
806
- this.#D.length = 0;
945
+ this.#V.length = 0;
807
946
  }
808
947
  }
809
948
 
810
- function openIpc(e, t = {}) {
811
- const i = t.retryMs ?? 50;
812
- const s = t.maxRetryMs ?? 750;
813
- return (t, r) => {
814
- const o = Date.now();
815
- const attempt = () => {
816
- const c = n.connect(e);
817
- let l = !1;
818
- c.once("connect", () => {
819
- const e = c._handle?.fd;
820
- l = !0;
821
- c.unref();
822
- c.once("close", () => r.destroy());
823
- const drain = (e, t) => {
824
- c.ref();
825
- c.write(e, e => {
826
- c.unref();
827
- t(e);
828
- });
829
- };
830
- if ("number" == typeof e && e >= 0) {
831
- t(null, e, drain);
832
- } else {
833
- t(null, null, drain);
834
- }
835
- });
836
- c.once("error", e => {
837
- if (l) {
838
- return;
839
- }
840
- c.destroy();
841
- ((e, n) => {
842
- if (Date.now() - o > s) {
843
- t(e);
844
- } else {
845
- setTimeout(n, i);
846
- }
847
- })(e, attempt);
848
- });
849
- };
850
- attempt();
851
- };
852
- }
853
-
854
- const m = Symbol.for("2g/process-origin");
949
+ const E = Symbol.for("2g/process-origin");
855
950
 
856
951
  function setLocalProcessOrigin(e) {
857
952
  const t = globalThis;
858
953
  if (e) {
859
- t[m] = e;
954
+ t[E] = e;
860
955
  } else {
861
- delete t[m];
956
+ delete t[E];
862
957
  }
863
958
  }
864
959
 
865
960
  function getProcessOrigin() {
866
- if (!l.isMainThread) {
961
+ if (!h.isMainThread) {
867
962
  return {
868
963
  kind: "worker_thread",
869
- id: String(l.threadId)
964
+ id: String(h.threadId)
870
965
  };
871
966
  }
872
- const e = globalThis[m];
967
+ const e = globalThis[E];
873
968
  const t = process.env[i.INTERNAL_PROCESS_ORIGIN_ENV];
874
969
  if (t && t !== e) {
875
970
  return {
@@ -886,44 +981,26 @@ function getProcessOrigin() {
886
981
  return null;
887
982
  }
888
983
 
889
- const S = Symbol.for("2g/process-cleanup");
890
-
891
- function getProcessCleanupState() {
892
- const e = globalThis;
893
- return e[S] ??= {
894
- registered: !1,
895
- callbacks: new Set
896
- };
897
- }
898
-
899
- function runProcessCleanup() {
900
- for (const e of getProcessCleanupState().callbacks) {
901
- try {
902
- e();
903
- } catch {}
904
- }
905
- }
906
-
907
984
  function createSession(t) {
908
985
  const r = i.getSessionBaseDir();
909
986
  const o = Date.now();
910
987
  const c = `${process.pid}-${o.toString(36)}-${Math.random().toString(36).slice(2, 8).padEnd(6, "0")}`;
911
988
  const l = e.join(r, c);
912
989
  try {
913
- s.mkdirSync(l, {
990
+ n.mkdirSync(l, {
914
991
  recursive: !0,
915
992
  mode: 448
916
993
  });
917
- s.chmodSync(r, 448);
994
+ n.chmodSync(r, 448);
918
995
  } catch {}
919
996
  const h = t.maxSegments ?? (a.debug ? i.DEBUG_SEGMENTS : i.DEFAULT_SEGMENTS);
920
997
  const u = t.maxSegmentSize ?? (a.debug ? i.DEBUG_SEGMENT_SIZE : i.DEFAULT_SEGMENT_SIZE);
921
998
  const d = i.createSocketAddress(l, c, "live");
922
999
  const f = i.createSocketAddress(l, c, "ipc");
923
- const g = new LogStream(e.join(l, "0.jsonl"));
924
- const p = new BroadcastChannel(g);
925
- p.add(createDebugSink());
926
- !function trackSegmentRotation(t, i, n, r) {
1000
+ const p = new LogStream(e.join(l, "0.jsonl"));
1001
+ const g = new BroadcastChannel(p);
1002
+ g.add(createDebugSink());
1003
+ !function trackSegmentRotation(t, i, s, r) {
927
1004
  let o = 0;
928
1005
  t.on("write", c => {
929
1006
  o += c;
@@ -932,33 +1009,24 @@ function createSession(t) {
932
1009
  }
933
1010
  o = 0;
934
1011
  !function rotateSegments(t, i) {
935
- s.rmSync(e.join(t, i - 1 + ".jsonl"), {
1012
+ n.rmSync(e.join(t, i - 1 + ".jsonl"), {
936
1013
  force: !0
937
1014
  });
938
- for (let n = i - 1; n >= 1; n--) {
939
- const i = e.join(t, n - 1 + ".jsonl");
940
- const r = e.join(t, `${n}.jsonl`);
1015
+ for (let s = i - 1; s >= 1; s--) {
1016
+ const i = e.join(t, s - 1 + ".jsonl");
1017
+ const r = e.join(t, `${s}.jsonl`);
941
1018
  try {
942
- s.renameSync(i, r);
1019
+ n.renameSync(i, r);
943
1020
  } catch {}
944
1021
  }
945
- }(i, n);
1022
+ }(i, s);
946
1023
  t.reopen();
947
1024
  });
948
- }(g, l, h, u);
949
- const y = n.createServer(e => p.attach(e));
950
- const m = n.createServer(e => p.ingest(e));
951
- const S = i.listenSocket(y, d.path);
952
- const v = i.listenSocket(m, f.path);
953
- const E = function publishIpcPath(e) {
954
- process.env[i.INTERNAL_IPC_ENV] = e;
955
- return () => {
956
- if (process.env[i.INTERNAL_IPC_ENV] === e) {
957
- delete process.env[i.INTERNAL_IPC_ENV];
958
- }
959
- };
960
- }(f.path);
961
- const w = function publishProcessOrigin() {
1025
+ }(p, l, h, u);
1026
+ const y = s.createServer(e => g.attach(e));
1027
+ const m = i.listenSocket(y, d.path);
1028
+ const S = listenIpcSink(g, f.path);
1029
+ const E = function publishProcessOrigin() {
962
1030
  const e = String(process.pid);
963
1031
  process.env[i.INTERNAL_PROCESS_ORIGIN_ENV] = e;
964
1032
  setLocalProcessOrigin(e);
@@ -969,7 +1037,7 @@ function createSession(t) {
969
1037
  setLocalProcessOrigin(void 0);
970
1038
  };
971
1039
  }();
972
- const _ = {
1040
+ const v = {
973
1041
  pid: process.pid,
974
1042
  formatVersion: i.EVENT_LOG_FORMAT_VERSION,
975
1043
  startedAt: o,
@@ -984,44 +1052,26 @@ function createSession(t) {
984
1052
  try {
985
1053
  !function writeJsonAtomic(e, t) {
986
1054
  const i = `${e}.${process.pid}.tmp`;
987
- s.writeFileSync(i, JSON.stringify(t, null, 2), {
1055
+ n.writeFileSync(i, JSON.stringify(t, null, 2), {
988
1056
  mode: 384
989
1057
  });
990
- s.renameSync(i, e);
991
- }(e.join(l, i.SESSION_FILES.meta), _);
1058
+ n.renameSync(i, e);
1059
+ }(e.join(l, i.SESSION_FILES.meta), v);
992
1060
  } catch {}
993
- let k = !1;
1061
+ let _ = !1;
994
1062
  const destroy = () => {
995
- if (k) {
1063
+ if (_) {
996
1064
  return;
997
1065
  }
998
- k = !0;
999
- p.destroy();
1066
+ _ = !0;
1067
+ g.destroy();
1068
+ m();
1000
1069
  S();
1001
- v();
1002
1070
  E();
1003
- w();
1004
1071
  i.removeSocket(d.path);
1005
1072
  i.removeSocket(f.path);
1006
1073
  };
1007
- !function registerProcessCleanup(e) {
1008
- const t = getProcessCleanupState();
1009
- t.callbacks.add(e);
1010
- if (t.registered) {
1011
- return;
1012
- }
1013
- t.registered = !0;
1014
- process.once("exit", runProcessCleanup);
1015
- for (const e of [ "SIGINT", "SIGTERM", "SIGHUP" ]) {
1016
- process.once(e, () => {
1017
- if (process.listenerCount(e) > 0) {
1018
- return;
1019
- }
1020
- runProcessCleanup();
1021
- process.kill(process.pid, e);
1022
- });
1023
- }
1024
- }(destroy);
1074
+ registerProcessCleanup(destroy);
1025
1075
  setImmediate(() => {
1026
1076
  try {
1027
1077
  i.cleanStaleSessionsSync();
@@ -1029,8 +1079,8 @@ function createSession(t) {
1029
1079
  });
1030
1080
  return {
1031
1081
  sessionDir: l,
1032
- meta: _,
1033
- sink: p,
1082
+ meta: v,
1083
+ sink: g,
1034
1084
  destroy
1035
1085
  };
1036
1086
  }
@@ -1065,21 +1115,24 @@ function installChildEventLogger(e) {
1065
1115
  _w: t
1066
1116
  };
1067
1117
  }
1068
- const s = function getParentIpcPath() {
1118
+ const n = function getParentIpcPath() {
1069
1119
  return process.env[i.INTERNAL_IPC_ENV];
1070
1120
  }();
1071
- if (s) {
1072
- a.debug = e?.debug ?? !!process.env[i.LOG_DEBUG_ENV];
1121
+ if (n) {
1122
+ a.debug = e?.debug ?? (function isParentDebugEnabled() {
1123
+ return "1" === process.env[i.INTERNAL_DEBUG_ENV];
1124
+ }() || !!process.env[i.LOG_DEBUG_ENV]);
1073
1125
  !function connectToParent(e) {
1074
1126
  a.primarySink = createPrimarySink(openIpc(e), {
1075
1127
  closeFd: !1
1076
1128
  });
1077
1129
  a.eventLoggerInfo = {
1078
1130
  destination: "ipc",
1079
- isUserVisibleOutput: !1
1131
+ isUserVisibleOutput: !1,
1132
+ debug: a.debug
1080
1133
  };
1081
1134
  v("init", getInitMetadata());
1082
- }(s);
1135
+ }(n);
1083
1136
  return !0;
1084
1137
  } else {
1085
1138
  return !1;
@@ -1099,9 +1152,9 @@ function parseLogTarget(t) {
1099
1152
  }
1100
1153
  try {
1101
1154
  const i = e.parse(t);
1102
- const s = e.format(i);
1155
+ const n = e.format(i);
1103
1156
  a.logPath = i.dir || process.cwd();
1104
- return s;
1157
+ return n;
1105
1158
  } catch {
1106
1159
  return;
1107
1160
  }
@@ -1154,10 +1207,10 @@ exports.installEventLogger = function installEventLogger(e) {
1154
1207
  if (installChildEventLogger(t)) {
1155
1208
  return;
1156
1209
  }
1157
- const s = parseLogTarget(process.env[i.LOG_EVENTS_ENV]) ?? parseLogTarget("string" == typeof e || "number" == typeof e ? e : void 0);
1158
- if (null != s) {
1159
- if ("number" == typeof s) {
1160
- r.redirectConsoleForFd(s);
1210
+ const n = parseLogTarget(process.env[i.LOG_EVENTS_ENV]) ?? parseLogTarget("string" == typeof e || "number" == typeof e ? e : void 0);
1211
+ if (null != n) {
1212
+ if ("number" == typeof n) {
1213
+ r.redirectConsoleForFd(n);
1161
1214
  }
1162
1215
  a.debug = t?.debug ?? !0;
1163
1216
  a.eventLoggerInfo = function getExplicitTargetInfo(e) {
@@ -1165,16 +1218,20 @@ exports.installEventLogger = function installEventLogger(e) {
1165
1218
  return {
1166
1219
  destination: 1 === e ? "stdout" : 2 === e ? "stderr" : "fd",
1167
1220
  isUserVisibleOutput: 1 === e || 2 === e,
1221
+ debug: a.debug,
1168
1222
  fd: e
1169
1223
  };
1170
1224
  }
1171
1225
  return {
1172
1226
  destination: "file",
1173
1227
  isUserVisibleOutput: !1,
1228
+ debug: a.debug,
1174
1229
  file: e
1175
1230
  };
1176
- }(s);
1177
- activateSink(createPrimarySink(s));
1231
+ }(n);
1232
+ const e = createPrimarySink(n);
1233
+ publishTempIpcSink(e);
1234
+ activateSink(e);
1178
1235
  return;
1179
1236
  }
1180
1237
  if (t && !1 !== t.session) {
@@ -1184,6 +1241,7 @@ exports.installEventLogger = function installEventLogger(e) {
1184
1241
  a.eventLoggerInfo = {
1185
1242
  destination: "session",
1186
1243
  isUserVisibleOutput: !1,
1244
+ debug: a.debug,
1187
1245
  sessionDir: e.sessionDir
1188
1246
  };
1189
1247
  activateSink(e.sink, t.version);