@expo/event-log 0.0.1-init

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.
@@ -0,0 +1,983 @@
1
+ Object.defineProperty(exports, "__esModule", {
2
+ value: !0
3
+ });
4
+
5
+ var e = require("node:path");
6
+
7
+ var t = require("node:perf_hooks");
8
+
9
+ var i = require("./chunks/tap-chunk.js");
10
+
11
+ var n = require("node:buffer");
12
+
13
+ var s = require("node:events");
14
+
15
+ var r = require("node:fs");
16
+
17
+ var o = require("node:net");
18
+
19
+ var c = require("node:readline");
20
+
21
+ var h = require("node:worker_threads");
22
+
23
+ const l = Symbol.for(`@expo/event-log/state-${i.EVENT_LOG_STATE_VERSION}`);
24
+
25
+ const a = function getEventLogState() {
26
+ const e = globalThis;
27
+ return e[l] ??= {
28
+ logPath: process.cwd(),
29
+ eventLoggerInfo: null
30
+ };
31
+ }();
32
+
33
+ function setLogPath(e) {
34
+ a.logPath = e;
35
+ }
36
+
37
+ function setPrimarySink(e) {
38
+ a.primarySink = e;
39
+ }
40
+
41
+ function setEventLoggerInfo(e) {
42
+ a.eventLoggerInfo = e;
43
+ }
44
+
45
+ function writeRawLine(e) {
46
+ const t = e.endsWith("\n") ? e : `${e}\n`;
47
+ a.primarySink?._writeln(t);
48
+ }
49
+
50
+ const f = 16387;
51
+
52
+ const d = t.performance.timeOrigin;
53
+
54
+ let u;
55
+
56
+ let p = "";
57
+
58
+ function writeEvent(e, t, i, n, s) {
59
+ e._writeln(function serializeEvent(e, t, i, n, s) {
60
+ const r = getEventTimestamp();
61
+ if (!n?._w) {
62
+ const n = i ? JSON.stringify(i).slice(1) : "";
63
+ return n.length > 1 ? `{"_e":"${e}:${t}","_t":${r},${n}\n` : `{"_e":"${e}:${t}","_t":${r}}\n`;
64
+ }
65
+ let o = `{"_e":"${e}:${t}","_t":${r}`;
66
+ if (n?._w) {
67
+ o += `,"_w":${stringifyWorkerId(n._w)}`;
68
+ }
69
+ return `${appendPayload(o, i)}}\n`;
70
+ }(t, i, n, s));
71
+ }
72
+
73
+ function writeCompleteEvent(e, t, i, n, s, r, o) {
74
+ let c = `{"_e":"${t}:${i}","_t":${getEventTimestamp()},"_d":${r}`;
75
+ if (o?._w) {
76
+ c += `,"_w":${stringifyWorkerId(o._w)}`;
77
+ }
78
+ e._writeln(`${function appendMergedPayload(e, t, i) {
79
+ if (!t) {
80
+ return appendPayload(e, i);
81
+ }
82
+ if (!i) {
83
+ return appendPayload(e, t);
84
+ }
85
+ const n = new Set(Object.keys(i));
86
+ for (const i of Object.keys(t)) {
87
+ if (n.has(i)) {
88
+ continue;
89
+ }
90
+ const s = JSON.stringify(t[i]);
91
+ if (void 0 !== s) {
92
+ e += `,${JSON.stringify(i)}:${s}`;
93
+ }
94
+ }
95
+ return appendPayload(e, i);
96
+ }(c, n, s)}}\n`);
97
+ }
98
+
99
+ function getEventTimestamp() {
100
+ return d + t.performance.now();
101
+ }
102
+
103
+ function stringifyWorkerId(e) {
104
+ if (e !== u) {
105
+ u = e;
106
+ p = JSON.stringify(e);
107
+ }
108
+ return p;
109
+ }
110
+
111
+ function appendPayload(e, t) {
112
+ if (!t) {
113
+ return e;
114
+ }
115
+ const i = JSON.stringify(t);
116
+ return i && i.length > 2 ? `${e},${i.slice(1, -1)}` : e;
117
+ }
118
+
119
+ class LogStream extends s.EventEmitter {
120
+ #e=-1;
121
+ #t=null;
122
+ #i=null;
123
+ #n=!0;
124
+ #s=!1;
125
+ #r=!1;
126
+ #o=!1;
127
+ #c=!1;
128
+ #h=!1;
129
+ #l=!1;
130
+ #a="";
131
+ #f=0;
132
+ #d=[];
133
+ #u=0;
134
+ #p=0;
135
+ #g=(e, t) => this.#y(e, t);
136
+ constructor(e, t = {}) {
137
+ super();
138
+ this.#n = !1 !== t.closeFd;
139
+ if ("number" == typeof e) {
140
+ try {
141
+ r.fsyncSync(e);
142
+ } catch {}
143
+ this.#e = e;
144
+ process.nextTick(() => this.emit("ready"));
145
+ } else if ("string" == typeof e) {
146
+ this.#S(e);
147
+ }
148
+ }
149
+ get file() {
150
+ return this.#t;
151
+ }
152
+ get fd() {
153
+ return this.#e;
154
+ }
155
+ get writing() {
156
+ return this.#s;
157
+ }
158
+ get writable() {
159
+ return !this.#c && !this.#r;
160
+ }
161
+ reopen(e = this.#t) {
162
+ if (null == this.#t) {
163
+ throw new Error("Cannot reopen an fd-only LogStream");
164
+ }
165
+ if (this.#c || this.#r) {
166
+ return;
167
+ }
168
+ if (e) {
169
+ this.#t = e;
170
+ }
171
+ if (this.#h) {
172
+ this.once("ready", () => this.reopen(e));
173
+ return;
174
+ }
175
+ this.#l = !0;
176
+ if (!this.#s) {
177
+ this.#m();
178
+ }
179
+ }
180
+ #m() {
181
+ if (null == this.#t) {
182
+ return;
183
+ }
184
+ this.#l = !1;
185
+ this.#i = this.#e;
186
+ this.#S(this.#t);
187
+ }
188
+ #y(e, t) {
189
+ if (e) {
190
+ if ("EAGAIN" === e.code || "EBUSY" === e.code) {
191
+ setTimeout(() => this.#w(), 100);
192
+ } else {
193
+ this.#s = !1;
194
+ this.emit("error", e);
195
+ }
196
+ } else {
197
+ this.emit("write", t);
198
+ if (t === this.#a.length) {
199
+ this.#f -= this.#a.length;
200
+ this.#a = "";
201
+ if (this.#d.length - this.#u > this.#p) {
202
+ this.#w();
203
+ } else if (this.#l) {
204
+ this.#s = !1;
205
+ this.#m();
206
+ } else if (this.#r) {
207
+ this.#s = !1;
208
+ this.#v();
209
+ } else {
210
+ this.#s = !1;
211
+ if (this.#o) {
212
+ this.emit("drain");
213
+ }
214
+ }
215
+ } else {
216
+ if (n.Buffer.byteLength(this.#a) > t) {
217
+ const e = n.Buffer.from(this.#a).toString("utf8", t);
218
+ this.#f -= this.#a.length - e.length;
219
+ this.#a = e;
220
+ } else {
221
+ this.#f -= this.#a.length;
222
+ this.#a = "";
223
+ }
224
+ if (this.#a || this.#d.length - this.#u > this.#p) {
225
+ this.#w();
226
+ } else if (this.#l) {
227
+ this.#s = !1;
228
+ this.#m();
229
+ } else if (this.#r) {
230
+ this.#s = !1;
231
+ this.#v();
232
+ } else {
233
+ this.#s = !1;
234
+ if (this.#o) {
235
+ this.emit("drain");
236
+ }
237
+ }
238
+ }
239
+ }
240
+ }
241
+ #S(t) {
242
+ this.#h = !0;
243
+ this.#s = !0;
244
+ const onOpened = (e, i) => {
245
+ const n = this.#i;
246
+ this.#i = null;
247
+ if (e) {
248
+ this.#s = !1;
249
+ this.#h = !1;
250
+ this.emit("error", e);
251
+ } else {
252
+ this.#e = i;
253
+ this.#t = t;
254
+ this.#h = !1;
255
+ this.#s = !1;
256
+ this.emit("ready");
257
+ if (null != n && n !== this.#e && !isStdFd(n)) {
258
+ fsFsync(n, () => r.close(n, () => {}));
259
+ }
260
+ if (this.#c) {} else if (!this.writing && this.#d.length - this.#u > this.#p || this.#o) {
261
+ this.#w();
262
+ }
263
+ }
264
+ };
265
+ r.mkdir(e.dirname(t), {
266
+ recursive: !0
267
+ }, e => {
268
+ if (e) {
269
+ return onOpened(e);
270
+ }
271
+ r.open(t, "a", 438, onOpened);
272
+ });
273
+ }
274
+ #v() {
275
+ if (-1 === this.#e) {
276
+ this.once("ready", () => this.#v());
277
+ return;
278
+ }
279
+ this.#c = !0;
280
+ this.#p = 0;
281
+ this.#d.length = 0;
282
+ this.#u = 0;
283
+ const onClose = e => {
284
+ if (e) {
285
+ this.emit("error", e);
286
+ this.emit("close", e);
287
+ } else {
288
+ if (this.#r && !this.#s) {
289
+ this.emit("finish");
290
+ }
291
+ this.emit("close");
292
+ }
293
+ };
294
+ fsFsync(this.#e, e => {
295
+ if (!e && this.#n && !isStdFd(this.#e)) {
296
+ r.close(this.#e, onClose);
297
+ } else {
298
+ onClose();
299
+ }
300
+ });
301
+ }
302
+ #w() {
303
+ this.#s = !0;
304
+ if (!this.#a) {
305
+ const e = this.#d.length - this.#p;
306
+ if (e > this.#u) {
307
+ this.#a = this.#d[this.#u++] || "";
308
+ while (this.#u < e && this.#a.length < f) {
309
+ this.#a += this.#d[this.#u++];
310
+ }
311
+ if (this.#u === this.#d.length) {
312
+ this.#d.length = 0;
313
+ this.#u = 0;
314
+ }
315
+ }
316
+ }
317
+ r.write(this.#e, this.#a, this.#g);
318
+ }
319
+ _end() {
320
+ if (!this.#c && !this.#r) {
321
+ this.#r = !0;
322
+ if (this.#h) {
323
+ this.once("ready", () => this._end());
324
+ } else if (!this.#s && this.#e >= 0) {
325
+ if (this.#d.length - this.#u > this.#p) {
326
+ this.#w();
327
+ } else {
328
+ this.#v();
329
+ }
330
+ }
331
+ }
332
+ return this;
333
+ }
334
+ end(e, t, i) {
335
+ const n = i || t || e;
336
+ const s = "function" != typeof e ? e : void 0;
337
+ const r = "function" == typeof n ? n : void 0;
338
+ if ("string" == typeof s) {
339
+ this.write(s, "string" == typeof t ? t : "utf8");
340
+ } else if (null != s) {
341
+ this.write(s);
342
+ }
343
+ if (r) {
344
+ this.once("close", r);
345
+ }
346
+ return this._end();
347
+ }
348
+ destroy() {
349
+ if (!this.#c) {
350
+ this.#v();
351
+ }
352
+ }
353
+ flush(e) {
354
+ if (this.#c) {
355
+ e?.();
356
+ } else {
357
+ const onDrain = () => {
358
+ if (!this.#c) {
359
+ fsFsync(this.#e, t => {
360
+ this.#o = !1;
361
+ if ("EBADF" === t?.code) {
362
+ e?.();
363
+ } else {
364
+ e?.(t);
365
+ }
366
+ });
367
+ } else {
368
+ this.#o = !1;
369
+ e?.();
370
+ }
371
+ this.off("error", onError);
372
+ };
373
+ const onError = t => {
374
+ this.#o = !1;
375
+ this.off("drain", onDrain);
376
+ e?.(t);
377
+ };
378
+ this.#o = !0;
379
+ this.once("drain", onDrain);
380
+ this.once("error", onError);
381
+ if (!this.#s) {
382
+ if (this.#d.length - this.#u > this.#p || this.#a) {
383
+ this.#w();
384
+ } else {
385
+ process.nextTick(() => this.emit("drain"));
386
+ }
387
+ }
388
+ }
389
+ }
390
+ _writeln(e) {
391
+ this.#f += e.length;
392
+ if (!this.#s && this.#d.length === this.#u && !this.#a) {
393
+ this.#s = !0;
394
+ this.#a = e;
395
+ r.write(this.#e, e, this.#g);
396
+ } else {
397
+ this.#d.push(e);
398
+ if (!this.#s) {
399
+ this.#w();
400
+ }
401
+ }
402
+ return this.#f < f;
403
+ }
404
+ _write(e) {
405
+ if (this.#c) {
406
+ return !1;
407
+ }
408
+ if (0 === this.#p && 10 === e.charCodeAt(e.length - 1)) {
409
+ return this._writeln(e);
410
+ }
411
+ this.#f += e.length;
412
+ let t = 0;
413
+ let i = -1;
414
+ while ((i = e.indexOf("\n", t)) > -1) {
415
+ const n = e.slice(t, i + 1);
416
+ if (this.#p > 0) {
417
+ this.#d[this.#d.length - 1] += n;
418
+ } else {
419
+ this.#d.push(n);
420
+ }
421
+ this.#p = 0;
422
+ t = ++i;
423
+ }
424
+ if (t < e.length) {
425
+ const i = e.slice(t);
426
+ if (this.#p > 0) {
427
+ this.#d[this.#d.length - 1] += i;
428
+ } else {
429
+ this.#d.push(e.slice(t));
430
+ }
431
+ this.#p = 1;
432
+ }
433
+ if (!this.#s && this.#d.length - this.#u > this.#p) {
434
+ this.#w();
435
+ }
436
+ return this.#f < f;
437
+ }
438
+ write(e, t, i) {
439
+ const s = i || t;
440
+ const r = "string" == typeof t ? t : "utf8";
441
+ const o = "string" == typeof e ? e : n.Buffer.from(e).toString(r);
442
+ const c = "function" == typeof s ? s : void 0;
443
+ try {
444
+ return this._write(o);
445
+ } finally {
446
+ c?.();
447
+ }
448
+ }
449
+ [Symbol.dispose]() {
450
+ this.destroy();
451
+ }
452
+ }
453
+
454
+ class WritableStreamSink {
455
+ constructor(e) {
456
+ this.stream = e;
457
+ }
458
+ get writable() {
459
+ return !1 !== this.stream.writable;
460
+ }
461
+ _writeln(e) {
462
+ return this.stream.write(e);
463
+ }
464
+ end(e) {
465
+ this.stream.end(e);
466
+ return this;
467
+ }
468
+ destroy() {
469
+ const e = this.stream;
470
+ e.destroy?.();
471
+ }
472
+ }
473
+
474
+ const isStdFd = e => {
475
+ switch (e) {
476
+ case 1:
477
+ case 2:
478
+ case process.stdout.fd:
479
+ case process.stderr.fd:
480
+ return !0;
481
+
482
+ default:
483
+ return !1;
484
+ }
485
+ };
486
+
487
+ const fsFsync = (e, t) => {
488
+ try {
489
+ r.fsync(e, t);
490
+ } catch (e) {
491
+ t(e);
492
+ }
493
+ };
494
+
495
+ const NOOP_DONE = () => {};
496
+
497
+ function events(i) {
498
+ function log(e, t) {
499
+ if (a.primarySink) {
500
+ writeEvent(a.primarySink, i, e, t, a.eventMeta);
501
+ }
502
+ }
503
+ log.span = function span(e, n) {
504
+ if (!a.primarySink) {
505
+ return NOOP_DONE;
506
+ }
507
+ const s = t.performance.now();
508
+ return function done(e, r) {
509
+ if (!a.primarySink) {
510
+ return;
511
+ }
512
+ writeCompleteEvent(a.primarySink, i, e, n, r, t.performance.now() - s, a.eventMeta);
513
+ };
514
+ };
515
+ log.path = function relativePath(t) {
516
+ try {
517
+ return null != t && e.isAbsolute(t) ? e.relative(a.logPath, t).replace(/\\/g, "/") || "." : t ?? null;
518
+ } catch {
519
+ return t || null;
520
+ }
521
+ };
522
+ log.category = i;
523
+ return log;
524
+ }
525
+
526
+ const g = Symbol.for("@expo/event-log/process-origin");
527
+
528
+ function setLocalProcessOrigin(e) {
529
+ const t = globalThis;
530
+ if (e) {
531
+ t[g] = e;
532
+ } else {
533
+ delete t[g];
534
+ }
535
+ }
536
+
537
+ function getProcessOrigin() {
538
+ if (!h.isMainThread) {
539
+ return {
540
+ kind: "worker_thread",
541
+ id: String(h.threadId)
542
+ };
543
+ }
544
+ const e = globalThis[g];
545
+ const t = process.env[i.INTERNAL_PROCESS_ORIGIN_ENV];
546
+ if (t && t !== e) {
547
+ return {
548
+ kind: "event_log_child",
549
+ id: t
550
+ };
551
+ }
552
+ if (process.env.NODE_UNIQUE_ID) {
553
+ return {
554
+ kind: "child_process",
555
+ id: process.env.NODE_UNIQUE_ID
556
+ };
557
+ }
558
+ return null;
559
+ }
560
+
561
+ class IpcSink {
562
+ #_=null;
563
+ #E=!1;
564
+ #c=!1;
565
+ #L=!1;
566
+ #d=[];
567
+ #k=Date.now();
568
+ #I;
569
+ #N;
570
+ constructor(e, t = {}) {
571
+ this.socketPath = e;
572
+ this.#I = t.retryMs ?? 50;
573
+ this.#N = t.maxRetryMs ?? 750;
574
+ this.#P();
575
+ }
576
+ get writable() {
577
+ return !this.#c;
578
+ }
579
+ _writeln(e) {
580
+ if (this.#c) {
581
+ return !1;
582
+ }
583
+ if (this.#E && this.#_) {
584
+ return this.#_.write(e);
585
+ }
586
+ this.#d.push(e);
587
+ return !1;
588
+ }
589
+ end(e) {
590
+ this.destroy();
591
+ e?.();
592
+ return this;
593
+ }
594
+ destroy() {
595
+ this.#c = !0;
596
+ this.#d.length = 0;
597
+ this.#_?.destroy();
598
+ }
599
+ #P() {
600
+ if (this.#c || this.#E) {
601
+ return;
602
+ }
603
+ if (Date.now() - this.#k > this.#N) {
604
+ this.destroy();
605
+ return;
606
+ }
607
+ const e = o.connect(this.socketPath);
608
+ this.#_ = e;
609
+ e.once("connect", () => {
610
+ this.#L = !1;
611
+ this.#E = !0;
612
+ for (const t of this.#d.splice(0)) {
613
+ e.write(t);
614
+ }
615
+ });
616
+ e.once("error", () => {
617
+ if (this.#E) {
618
+ return;
619
+ }
620
+ e.destroy();
621
+ this.#O();
622
+ });
623
+ e.once("close", () => {
624
+ if (this.#c) {
625
+ return;
626
+ }
627
+ this.#E = !1;
628
+ if (this.#_ === e) {
629
+ this.#_ = null;
630
+ }
631
+ this.#O();
632
+ });
633
+ }
634
+ #O() {
635
+ if (this.#L || this.#c) {
636
+ return;
637
+ }
638
+ this.#L = !0;
639
+ setTimeout(() => {
640
+ this.#L = !1;
641
+ this.#P();
642
+ }, this.#I);
643
+ }
644
+ }
645
+
646
+ const y = Symbol.for("@expo/event-log/process-cleanup");
647
+
648
+ function getProcessCleanupState() {
649
+ const e = globalThis;
650
+ return e[y] ??= {
651
+ registered: !1,
652
+ callbacks: new Set
653
+ };
654
+ }
655
+
656
+ function runProcessCleanup() {
657
+ for (const e of getProcessCleanupState().callbacks) {
658
+ try {
659
+ e();
660
+ } catch {}
661
+ }
662
+ }
663
+
664
+ function listenUnixSocket(e, t) {
665
+ let i = !1;
666
+ let n = !1;
667
+ let s = !1;
668
+ const close = () => {
669
+ s = !0;
670
+ if (n && !i) {
671
+ e.close();
672
+ }
673
+ };
674
+ e.once("listening", () => {
675
+ n = !0;
676
+ if (s) {
677
+ close();
678
+ }
679
+ });
680
+ e.once("close", () => {
681
+ i = !0;
682
+ n = !1;
683
+ });
684
+ e.on("error", () => {
685
+ i = !0;
686
+ try {
687
+ e.close(() => {});
688
+ } catch {}
689
+ });
690
+ e.listen(t);
691
+ return close;
692
+ }
693
+
694
+ let S;
695
+
696
+ const m = events("root");
697
+
698
+ function parseLogTarget(t) {
699
+ if ("number" == typeof t) {
700
+ return Number.isSafeInteger(t) && t > 0 ? t : void 0;
701
+ }
702
+ if (!t) {
703
+ return;
704
+ }
705
+ const i = parseInt(t, 10);
706
+ if (`${i}` === t && i > 0 && Number.isSafeInteger(i)) {
707
+ return i;
708
+ }
709
+ try {
710
+ const i = e.parse(t);
711
+ const n = e.format(i);
712
+ setLogPath(i.dir || process.cwd());
713
+ return n;
714
+ } catch {
715
+ return;
716
+ }
717
+ }
718
+
719
+ function getInitMetadata(e) {
720
+ return {
721
+ format: "v0-jsonl",
722
+ formatVersion: i.EVENT_LOG_FORMAT_VERSION,
723
+ version: e ?? "UNVERSIONED",
724
+ processOrigin: getProcessOrigin() ?? void 0
725
+ };
726
+ }
727
+
728
+ function activateSink(e, t) {
729
+ setPrimarySink(e);
730
+ m("init", getInitMetadata(t));
731
+ }
732
+
733
+ function createSessionOrigin() {
734
+ const e = {
735
+ npmLifecycleEvent: process.env.npm_lifecycle_event,
736
+ npmExecPath: process.env.npm_execpath,
737
+ npmPackageName: process.env.npm_package_name
738
+ };
739
+ return {
740
+ argv: process.argv.slice(1),
741
+ execPath: process.execPath,
742
+ cwd: process.cwd(),
743
+ ppid: process.ppid || void 0,
744
+ env: Object.values(e).some(Boolean) ? e : void 0
745
+ };
746
+ }
747
+
748
+ exports.list = i.listSessions;
749
+
750
+ exports.tap = i.tap;
751
+
752
+ exports.events = events;
753
+
754
+ exports.installEventLogger = function installEventLogger(t) {
755
+ if (a.primarySink) {
756
+ return;
757
+ }
758
+ const n = parseLogTarget(process.env[i.LOG_EVENTS_ENV]) ?? parseLogTarget("string" == typeof t || "number" == typeof t ? t : void 0);
759
+ if (null != n) {
760
+ if ("number" == typeof n) {
761
+ i.redirectConsoleForFd(n);
762
+ }
763
+ setEventLoggerInfo(function getExplicitTargetInfo(e) {
764
+ if ("number" == typeof e) {
765
+ return {
766
+ destination: 1 === e ? "stdout" : 2 === e ? "stderr" : "fd",
767
+ isUserVisibleOutput: 1 === e || 2 === e,
768
+ fd: e
769
+ };
770
+ }
771
+ return {
772
+ destination: "file",
773
+ isUserVisibleOutput: !1,
774
+ file: e
775
+ };
776
+ }(n));
777
+ activateSink(new LogStream(n));
778
+ return;
779
+ }
780
+ const s = process.env[i.INTERNAL_IPC_ENV];
781
+ if (s) {
782
+ !function connectToParent(e) {
783
+ const t = function getProcessWorkerId() {
784
+ const e = getProcessOrigin();
785
+ return e ? `${e.kind}:${e.id}` : void 0;
786
+ }();
787
+ if (t) {
788
+ !function setEventMeta(e) {
789
+ a.eventMeta = e;
790
+ }({
791
+ _w: t
792
+ });
793
+ }
794
+ const i = new IpcSink(e);
795
+ setPrimarySink(i);
796
+ setEventLoggerInfo({
797
+ destination: "ipc",
798
+ isUserVisibleOutput: !1
799
+ });
800
+ m("init", getInitMetadata());
801
+ }(s);
802
+ return;
803
+ }
804
+ if (t && "object" == typeof t) {
805
+ S = function createSession(t) {
806
+ i.cleanStaleSessionsSync();
807
+ const n = i.getSessionBaseDir();
808
+ const s = e.join(n, String(process.pid));
809
+ r.rmSync(s, {
810
+ recursive: !0,
811
+ force: !0
812
+ });
813
+ r.mkdirSync(s, {
814
+ recursive: !0
815
+ });
816
+ const h = t.maxSegments ?? i.DEFAULT_SEGMENTS;
817
+ const l = t.maxSegmentSize ?? i.DEFAULT_SEGMENT_SIZE;
818
+ const a = e.join(s, i.SESSION_FILES.liveSocket);
819
+ const f = e.join(s, i.SESSION_FILES.ipcSocket);
820
+ const d = new LogStream(e.join(s, "0.jsonl"));
821
+ const u = new Set;
822
+ let p = 0;
823
+ let g = !1;
824
+ d.on("write", t => {
825
+ p += t;
826
+ if (p < l) {
827
+ return;
828
+ }
829
+ p = 0;
830
+ !function rotateSegments(t, i) {
831
+ r.rmSync(e.join(t, i - 1 + ".jsonl"), {
832
+ force: !0
833
+ });
834
+ for (let n = i - 1; n >= 1; n--) {
835
+ const i = e.join(t, n - 1 + ".jsonl");
836
+ const s = e.join(t, `${n}.jsonl`);
837
+ try {
838
+ r.renameSync(i, s);
839
+ } catch {}
840
+ }
841
+ }(s, h);
842
+ d.reopen();
843
+ });
844
+ const y = o.createServer(e => {
845
+ const t = function createSocketSink(e) {
846
+ const t = e._handle?.fd;
847
+ if ("number" == typeof t && t >= 0) {
848
+ return new LogStream(t, {
849
+ closeFd: !1
850
+ });
851
+ }
852
+ return new WritableStreamSink(e);
853
+ }(e);
854
+ u.add(t);
855
+ e.on("close", () => {
856
+ u.delete(t);
857
+ t.destroy();
858
+ });
859
+ e.on("error", () => {
860
+ u.delete(t);
861
+ t.destroy();
862
+ e.destroy();
863
+ });
864
+ });
865
+ const S = o.createServer(e => {
866
+ const i = c.createInterface({
867
+ input: e
868
+ });
869
+ i.on("line", t.onWorkerLine);
870
+ e.on("close", () => i.close());
871
+ e.on("error", () => i.close());
872
+ });
873
+ const m = listenUnixSocket(y, a);
874
+ const w = listenUnixSocket(S, f);
875
+ const v = String(process.pid);
876
+ process.env[i.INTERNAL_IPC_ENV] = f;
877
+ process.env[i.INTERNAL_PROCESS_ORIGIN_ENV] = v;
878
+ setLocalProcessOrigin(v);
879
+ const _ = {
880
+ pid: process.pid,
881
+ formatVersion: i.EVENT_LOG_FORMAT_VERSION,
882
+ startedAt: Date.now(),
883
+ command: t.command ?? process.argv.slice(1).join(" "),
884
+ cwd: process.cwd(),
885
+ maxSegments: h,
886
+ version: t.version,
887
+ socket: i.SESSION_FILES.liveSocket,
888
+ ipcSocket: i.SESSION_FILES.ipcSocket,
889
+ origin: createSessionOrigin()
890
+ };
891
+ !function writeJsonAtomic(e, t) {
892
+ const i = `${e}.${process.pid}.tmp`;
893
+ r.writeFileSync(i, JSON.stringify(t, null, 2));
894
+ r.renameSync(i, e);
895
+ }(e.join(s, i.SESSION_FILES.meta), _);
896
+ const E = {
897
+ get writable() {
898
+ return d.writable;
899
+ },
900
+ get file() {
901
+ return d.file;
902
+ },
903
+ _writeln(e) {
904
+ const t = d._writeln(e);
905
+ for (const t of u) {
906
+ t._writeln(e);
907
+ }
908
+ return t;
909
+ },
910
+ end(e) {
911
+ d.end(e);
912
+ for (const e of u) {
913
+ e.end();
914
+ }
915
+ return this;
916
+ },
917
+ destroy() {
918
+ d.destroy();
919
+ for (const e of u) {
920
+ e.destroy();
921
+ }
922
+ }
923
+ };
924
+ const destroy = () => {
925
+ if (g) {
926
+ return;
927
+ }
928
+ g = !0;
929
+ E.destroy();
930
+ m();
931
+ w();
932
+ r.rmSync(a, {
933
+ force: !0
934
+ });
935
+ r.rmSync(f, {
936
+ force: !0
937
+ });
938
+ if (process.env[i.INTERNAL_IPC_ENV] === f) {
939
+ delete process.env[i.INTERNAL_IPC_ENV];
940
+ }
941
+ if (process.env[i.INTERNAL_PROCESS_ORIGIN_ENV] === v) {
942
+ delete process.env[i.INTERNAL_PROCESS_ORIGIN_ENV];
943
+ }
944
+ setLocalProcessOrigin(void 0);
945
+ };
946
+ !function registerProcessCleanup(e) {
947
+ const t = getProcessCleanupState();
948
+ t.callbacks.add(e);
949
+ if (t.registered) {
950
+ return;
951
+ }
952
+ t.registered = !0;
953
+ process.once("exit", runProcessCleanup);
954
+ for (const e of [ "SIGINT", "SIGTERM", "SIGHUP" ]) {
955
+ process.once(e, () => {
956
+ runProcessCleanup();
957
+ process.kill(process.pid, e);
958
+ });
959
+ }
960
+ }(destroy);
961
+ return {
962
+ sessionDir: s,
963
+ meta: _,
964
+ sink: E,
965
+ destroy
966
+ };
967
+ }({
968
+ ...t,
969
+ onWorkerLine: writeRawLine
970
+ });
971
+ setLogPath(S.sessionDir);
972
+ setEventLoggerInfo({
973
+ destination: "session",
974
+ isUserVisibleOutput: !1,
975
+ sessionDir: S.sessionDir
976
+ });
977
+ activateSink(S.sink, t.version);
978
+ return;
979
+ }
980
+ };
981
+
982
+ exports.isEventLoggerActive = () => a.primarySink?.writable ? a.eventLoggerInfo : null;
983
+ //# sourceMappingURL=event-log.js.map