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