@utk09/ui-observability 0.7.0 → 0.9.0
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/CHANGELOG.md +79 -1
- package/README.md +170 -41
- package/dist/bus/bus.d.ts +75 -11
- package/dist/bus/links.d.ts +6 -3
- package/dist/bus-CtJWf9QN.cjs +2 -0
- package/dist/bus-CtJWf9QN.cjs.map +1 -0
- package/dist/{bus-DfAbg_pg.js → bus-D8kH58tb.js} +176 -73
- package/dist/bus-D8kH58tb.js.map +1 -0
- package/dist/capture/console.d.ts +37 -0
- package/dist/capture/errors.d.ts +2 -4
- package/dist/capture/interactions.d.ts +7 -6
- package/dist/capture/long-frames.d.ts +31 -0
- package/dist/capture/network.d.ts +23 -9
- package/dist/capture/storm.d.ts +17 -0
- package/dist/capture/types.d.ts +5 -3
- package/dist/console-BpHfWstv.cjs +2 -0
- package/dist/console-BpHfWstv.cjs.map +1 -0
- package/dist/console-DjLbryED.js +82 -0
- package/dist/console-DjLbryED.js.map +1 -0
- package/dist/constants-Br3b89Wg.cjs +2 -0
- package/dist/constants-Br3b89Wg.cjs.map +1 -0
- package/dist/constants-DM94FjVX.js +96 -0
- package/dist/constants-DM94FjVX.js.map +1 -0
- package/dist/constants.d.ts +51 -10
- package/dist/core/breadcrumbs.d.ts +1 -1
- package/dist/core/diagnostics.d.ts +2 -2
- package/dist/core/journey.d.ts +16 -4
- package/dist/core/logger.d.ts +6 -2
- package/dist/core/pipeline.d.ts +20 -11
- package/dist/core/record.d.ts +14 -6
- package/dist/core/remote-rules.d.ts +17 -0
- package/dist/core/remote-sampling.d.ts +65 -0
- package/dist/core/runtime.d.ts +52 -18
- package/dist/core/sampling.d.ts +5 -2
- package/dist/core/sender.d.ts +18 -10
- package/dist/deadline-B5DINjD5.js +111 -0
- package/dist/deadline-B5DINjD5.js.map +1 -0
- package/dist/deadline-Cbg2cbUw.cjs +2 -0
- package/dist/deadline-Cbg2cbUw.cjs.map +1 -0
- package/dist/error-like-Ywo5uRHo.js +19 -0
- package/dist/error-like-Ywo5uRHo.js.map +1 -0
- package/dist/error-like-gGRx2s36.cjs +2 -0
- package/dist/error-like-gGRx2s36.cjs.map +1 -0
- package/dist/errors-CpGRhhIf.cjs +3 -0
- package/dist/errors-CpGRhhIf.cjs.map +1 -0
- package/dist/{errors-4MQyjZVj.js → errors-DH4QJS0u.js} +38 -20
- package/dist/errors-DH4QJS0u.js.map +1 -0
- package/dist/global-scope-BN-Io_yv.cjs +2 -0
- package/dist/global-scope-BN-Io_yv.cjs.map +1 -0
- package/dist/global-scope-dp2jADRl.js +19 -0
- package/dist/global-scope-dp2jADRl.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +13 -5
- package/dist/index.js +633 -463
- package/dist/index.js.map +1 -1
- package/dist/indexeddb-storage-DNdf1fmL.cjs +2 -0
- package/dist/indexeddb-storage-DNdf1fmL.cjs.map +1 -0
- package/dist/{indexeddb-storage-0LtUALBg.js → indexeddb-storage-n1qkFBGA.js} +17 -17
- package/dist/indexeddb-storage-n1qkFBGA.js.map +1 -0
- package/dist/interactions-31eHGwEQ.cjs +2 -0
- package/dist/interactions-31eHGwEQ.cjs.map +1 -0
- package/dist/{interactions-BaCmoGLB.js → interactions-OU94MPXs.js} +22 -19
- package/dist/interactions-OU94MPXs.js.map +1 -0
- package/dist/journey-CE2EcwHW.cjs +2 -0
- package/dist/journey-CE2EcwHW.cjs.map +1 -0
- package/dist/journey-CeJ1QoT2.js +162 -0
- package/dist/journey-CeJ1QoT2.js.map +1 -0
- package/dist/library-request-C9uJFlbq.cjs +2 -0
- package/dist/library-request-C9uJFlbq.cjs.map +1 -0
- package/dist/library-request-DvhuplAB.js +14 -0
- package/dist/library-request-DvhuplAB.js.map +1 -0
- package/dist/long-frames-B8VdA_ny.cjs +2 -0
- package/dist/long-frames-B8VdA_ny.cjs.map +1 -0
- package/dist/long-frames-C_7OutLs.js +65 -0
- package/dist/long-frames-C_7OutLs.js.map +1 -0
- package/dist/models/batch.d.ts +1 -1
- package/dist/models/bus.d.ts +23 -4
- package/dist/models/config.d.ts +132 -11
- package/dist/models/log-record.d.ts +12 -6
- package/dist/network-BAV7ipyn.cjs +2 -0
- package/dist/network-BAV7ipyn.cjs.map +1 -0
- package/dist/network-D_lloS5t.js +174 -0
- package/dist/network-D_lloS5t.js.map +1 -0
- package/dist/patch-BX8KrPCT.cjs +2 -0
- package/dist/patch-BX8KrPCT.cjs.map +1 -0
- package/dist/patch-CCWfuEY7.js +9 -0
- package/dist/patch-CCWfuEY7.js.map +1 -0
- package/dist/{platform-ByQllE_W.cjs → platform-D4wu1eIF.cjs} +2 -2
- package/dist/platform-D4wu1eIF.cjs.map +1 -0
- package/dist/{platform-YuByH8A5.js → platform-IHBZGqUe.js} +9 -9
- package/dist/platform-IHBZGqUe.js.map +1 -0
- package/dist/remote-rules-Bt1XsdEQ.js +86 -0
- package/dist/remote-rules-Bt1XsdEQ.js.map +1 -0
- package/dist/remote-rules-kuDf5gbe.cjs +2 -0
- package/dist/remote-rules-kuDf5gbe.cjs.map +1 -0
- package/dist/sampling-BI15ds9D.cjs +2 -0
- package/dist/sampling-BI15ds9D.cjs.map +1 -0
- package/dist/sampling-o0VY1mqQ.js +63 -0
- package/dist/sampling-o0VY1mqQ.js.map +1 -0
- package/dist/sanitize-B9UheVKc.js +123 -0
- package/dist/sanitize-B9UheVKc.js.map +1 -0
- package/dist/sanitize-DSIP7amZ.cjs +2 -0
- package/dist/sanitize-DSIP7amZ.cjs.map +1 -0
- package/dist/{sender-BIob8eZz.js → sender-BnNj9bOX.js} +415 -272
- package/dist/sender-BnNj9bOX.js.map +1 -0
- package/dist/sender-D0OsyB1c.cjs +3 -0
- package/dist/sender-D0OsyB1c.cjs.map +1 -0
- package/dist/storage/emergency-queue.d.ts +1 -1
- package/dist/storage/factory.d.ts +1 -1
- package/dist/storage/idb-driver.d.ts +1 -1
- package/dist/storage/local-storage.d.ts +3 -3
- package/dist/storage/memory-storage.d.ts +3 -3
- package/dist/storm-BTw0J6eE.js +10 -0
- package/dist/storm-BTw0J6eE.js.map +1 -0
- package/dist/storm-Y-yuBhvv.cjs +2 -0
- package/dist/storm-Y-yuBhvv.cjs.map +1 -0
- package/dist/transport/compression.d.ts +1 -1
- package/dist/transport/custom-transport.d.ts +20 -0
- package/dist/transport/errors.d.ts +2 -1
- package/dist/transport/exit-flush.d.ts +32 -13
- package/dist/transport/http-transport.d.ts +57 -25
- package/dist/transport/memory-transport.d.ts +30 -0
- package/dist/transport/retry-engine.d.ts +7 -7
- package/dist/transport/serializers/select.d.ts +12 -1
- package/dist/transport/status-transport.d.ts +74 -0
- package/dist/utils/console.d.ts +1 -1
- package/dist/utils/deadline.d.ts +7 -0
- package/dist/utils/error-like.d.ts +25 -0
- package/dist/utils/global-scope.d.ts +20 -0
- package/dist/utils/identity.d.ts +1 -1
- package/dist/utils/library-console.d.ts +11 -0
- package/dist/utils/library-request.d.ts +12 -0
- package/dist/utils/patch.d.ts +9 -0
- package/dist/utils/platform.d.ts +3 -3
- package/dist/utils/sequence.d.ts +0 -5
- package/dist/utils/timer.d.ts +1 -1
- package/dist/utils/tracing.d.ts +3 -3
- package/dist/utils/unref.d.ts +2 -2
- package/dist/{web-vitals-DHZ0yA_H.js → web-vitals-Bflr5jSA.js} +4 -2
- package/dist/web-vitals-Bflr5jSA.js.map +1 -0
- package/dist/web-vitals-DFuoTQbZ.cjs +2 -0
- package/dist/web-vitals-DFuoTQbZ.cjs.map +1 -0
- package/package.json +2 -2
- package/dist/bus-DeZzr2bx.cjs +0 -2
- package/dist/bus-DeZzr2bx.cjs.map +0 -1
- package/dist/bus-DfAbg_pg.js.map +0 -1
- package/dist/constants-C8nZQBmQ.js +0 -86
- package/dist/constants-C8nZQBmQ.js.map +0 -1
- package/dist/constants-IBL_jd5R.cjs +0 -2
- package/dist/constants-IBL_jd5R.cjs.map +0 -1
- package/dist/errors-4MQyjZVj.js.map +0 -1
- package/dist/errors-CUdShXe7.cjs +0 -3
- package/dist/errors-CUdShXe7.cjs.map +0 -1
- package/dist/indexeddb-storage-0LtUALBg.js.map +0 -1
- package/dist/indexeddb-storage-CcEJlq2l.cjs +0 -2
- package/dist/indexeddb-storage-CcEJlq2l.cjs.map +0 -1
- package/dist/interactions-BMNPQ3WH.cjs +0 -2
- package/dist/interactions-BMNPQ3WH.cjs.map +0 -1
- package/dist/interactions-BaCmoGLB.js.map +0 -1
- package/dist/network-DBNvxCE3.cjs +0 -2
- package/dist/network-DBNvxCE3.cjs.map +0 -1
- package/dist/network-jQ2t8rU4.js +0 -111
- package/dist/network-jQ2t8rU4.js.map +0 -1
- package/dist/platform-ByQllE_W.cjs.map +0 -1
- package/dist/platform-YuByH8A5.js.map +0 -1
- package/dist/sampling-CbYySsZ1.js +0 -251
- package/dist/sampling-CbYySsZ1.js.map +0 -1
- package/dist/sampling-Cs50xsZl.cjs +0 -2
- package/dist/sampling-Cs50xsZl.cjs.map +0 -1
- package/dist/sender-BIob8eZz.js.map +0 -1
- package/dist/sender-iOjzcueM.cjs +0 -3
- package/dist/sender-iOjzcueM.cjs.map +0 -1
- package/dist/unref-16maCyKA.js +0 -8
- package/dist/unref-16maCyKA.js.map +0 -1
- package/dist/unref-CU9Nyz_9.cjs +0 -2
- package/dist/unref-CU9Nyz_9.cjs.map +0 -1
- package/dist/web-vitals-B2FYsEAU.cjs +0 -2
- package/dist/web-vitals-B2FYsEAU.cjs.map +0 -1
- package/dist/web-vitals-DHZ0yA_H.js.map +0 -1
package/dist/core/pipeline.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { LogBatch } from "../models/batch";
|
|
2
2
|
import { type LogRecord } from "../models/log-record";
|
|
3
3
|
import type { StorageAdapter } from "../models/storage";
|
|
4
|
-
import type {
|
|
4
|
+
import type { BatchTransport } from "../transport/status-transport";
|
|
5
5
|
import type { RetryEngine } from "../transport/retry-engine";
|
|
6
6
|
import type { Diagnostics } from "./diagnostics";
|
|
7
7
|
/** Name of a pipeline stream. */
|
|
@@ -18,7 +18,7 @@ export interface StreamOptions {
|
|
|
18
18
|
* Metrics have a higher volume, and no process waits for them.
|
|
19
19
|
*/
|
|
20
20
|
export declare const STREAM_OPTIONS: Record<StreamName, StreamOptions>;
|
|
21
|
-
/** Buffers log and metric streams
|
|
21
|
+
/** Buffers the log and metric streams. It makes batches. It sends them over HTTP. */
|
|
22
22
|
export declare class LogPipeline {
|
|
23
23
|
private readonly transport;
|
|
24
24
|
private readonly storage;
|
|
@@ -30,13 +30,15 @@ export declare class LogPipeline {
|
|
|
30
30
|
private readonly unconfirmed;
|
|
31
31
|
/** Closed batches waiting for a free dispatch slot. */
|
|
32
32
|
private readonly queue;
|
|
33
|
+
/** Each batch in the queue or in flight. flushPending waits for them. destroy() resolves them. */
|
|
34
|
+
private readonly open;
|
|
33
35
|
/** Ceiling on the ERROR and FATAL records that bypass sampling. */
|
|
34
36
|
private readonly ceiling;
|
|
35
|
-
/**
|
|
37
|
+
/** Number of dispatches in flight. MAX_CONCURRENT_REQUESTS is the limit. */
|
|
36
38
|
private inFlight;
|
|
37
39
|
/** It is true when a pump is in the queue. One microtask then drains a burst. */
|
|
38
40
|
private pumpScheduled;
|
|
39
|
-
/**
|
|
41
|
+
/** True after destroy() stops the pipeline. */
|
|
40
42
|
private stopped;
|
|
41
43
|
/**
|
|
42
44
|
* @param transport HTTP transport for live batch delivery.
|
|
@@ -45,7 +47,7 @@ export declare class LogPipeline {
|
|
|
45
47
|
* @param diagnostics Diagnostics reporter.
|
|
46
48
|
* @param streamOptions Batching policy per stream.
|
|
47
49
|
*/
|
|
48
|
-
constructor(transport:
|
|
50
|
+
constructor(transport: BatchTransport, storage: StorageAdapter, retry: RetryEngine, diagnostics: Diagnostics, streamOptions?: Record<StreamName, StreamOptions>);
|
|
49
51
|
/**
|
|
50
52
|
* Sets the observed timestamp and adds a record to the pipeline.
|
|
51
53
|
* @param record Record to push.
|
|
@@ -56,7 +58,7 @@ export declare class LogPipeline {
|
|
|
56
58
|
* An in-flight batch has its own ID and its send is not complete. If this method
|
|
57
59
|
* takes it, the pipeline sends each record again under a new ID. The server cannot
|
|
58
60
|
* deduplicate that ID.
|
|
59
|
-
* @returns Combined LogBatch or null if no records
|
|
61
|
+
* @returns Combined LogBatch, or null if the buffers hold no records.
|
|
60
62
|
*/
|
|
61
63
|
drainPending(): LogBatch | null;
|
|
62
64
|
/**
|
|
@@ -67,12 +69,19 @@ export declare class LogPipeline {
|
|
|
67
69
|
* @returns Combined LogBatch or null if no records are pending.
|
|
68
70
|
*/
|
|
69
71
|
drainForExit(): LogBatch | null;
|
|
72
|
+
/**
|
|
73
|
+
* Sends the buffered records through the normal dispatch path. Then it waits for each
|
|
74
|
+
* batch in the queue and in flight. The throttle check and the concurrency limit apply.
|
|
75
|
+
* A direct send goes around both.
|
|
76
|
+
* @returns Promise that resolves when the pipeline sends or stores each batch, or on destroy.
|
|
77
|
+
*/
|
|
78
|
+
flushPending(): Promise<void>;
|
|
70
79
|
/** Stops the flush timers and refuses further records. */
|
|
71
80
|
destroy(): void;
|
|
72
81
|
/**
|
|
73
82
|
* Wraps drained records in a batch.
|
|
74
83
|
* @param records Records taken from the buffers, in delivery order.
|
|
75
|
-
* @returns New LogBatch, or null when
|
|
84
|
+
* @returns New LogBatch, or null when the drain takes nothing.
|
|
76
85
|
*/
|
|
77
86
|
private toBatch;
|
|
78
87
|
/**
|
|
@@ -93,14 +102,14 @@ export declare class LogPipeline {
|
|
|
93
102
|
/**
|
|
94
103
|
* Queues a batch and schedules the pump on a microtask. An inline dispatch starts
|
|
95
104
|
* a fetch inside the log call of the application.
|
|
96
|
-
* @param batch Batch ready for delivery.
|
|
105
|
+
* @param batch Batch ready for delivery, or null when the drain found no records.
|
|
97
106
|
*/
|
|
98
107
|
private enqueue;
|
|
99
108
|
/** Starts queued dispatches up to the concurrency limit. */
|
|
100
109
|
private pump;
|
|
101
110
|
/**
|
|
102
|
-
* Delivers one batch
|
|
103
|
-
* @param
|
|
111
|
+
* Delivers one batch. It then frees the concurrency slot. It resolves the promise of the batch.
|
|
112
|
+
* @param pending Batch to deliver.
|
|
104
113
|
*/
|
|
105
114
|
private run;
|
|
106
115
|
/**
|
|
@@ -117,7 +126,7 @@ export declare class LogPipeline {
|
|
|
117
126
|
*/
|
|
118
127
|
private storeFailed;
|
|
119
128
|
/**
|
|
120
|
-
* Stores an undelivered batch
|
|
129
|
+
* Stores an undelivered batch. Then it starts the retry engine. A deadline limits the
|
|
121
130
|
* wait. A storage call that does not settle holds this dispatch slot. Two such calls
|
|
122
131
|
* stop the pipeline.
|
|
123
132
|
* @param batch Batch to persist.
|
package/dist/core/record.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { TraceEngine } from "../utils/tracing";
|
|
|
8
8
|
import type { ContextStore } from "./context";
|
|
9
9
|
import type { Diagnostics } from "./diagnostics";
|
|
10
10
|
import type { JourneyEngine } from "./journey";
|
|
11
|
+
import type { RemoteSlot } from "./remote-sampling";
|
|
11
12
|
/** Injected dependencies required by RecordBuilder. */
|
|
12
13
|
export interface RecordBuilderDeps {
|
|
13
14
|
/** Active configuration object reference. */
|
|
@@ -28,6 +29,8 @@ export interface RecordBuilderDeps {
|
|
|
28
29
|
identity: Identity;
|
|
29
30
|
/** Detected host platform metadata. */
|
|
30
31
|
platform: PlatformMetadata;
|
|
32
|
+
/** Slot of the sampling rules from the ingest server. */
|
|
33
|
+
remote: RemoteSlot;
|
|
31
34
|
}
|
|
32
35
|
/** Size limits for each record. The builder applies them after sanitize and after redact. */
|
|
33
36
|
export interface RecordLimits extends SanitizeLimits {
|
|
@@ -36,11 +39,14 @@ export interface RecordLimits extends SanitizeLimits {
|
|
|
36
39
|
/** Max total attribute bytes per record. */
|
|
37
40
|
maxRecordBytes: number;
|
|
38
41
|
}
|
|
39
|
-
/**
|
|
40
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Reason a record was not built. "malformed" means the redact hook broke the record shape
|
|
44
|
+
* or set an unknown severityText.
|
|
45
|
+
*/
|
|
46
|
+
export type DropReason = "level" | "sampling" | "redact" | "malformed";
|
|
41
47
|
/** Outcome of one build call. */
|
|
42
48
|
export interface BuildResult {
|
|
43
|
-
/** Completed record, or null when
|
|
49
|
+
/** Completed record, or null when the builder drops it. */
|
|
44
50
|
record: LogRecord | null;
|
|
45
51
|
/** Reason for the drop. It is absent when the builder built a record. */
|
|
46
52
|
dropped?: DropReason;
|
|
@@ -73,14 +79,16 @@ export declare class RecordBuilder {
|
|
|
73
79
|
invalidateResource(): void;
|
|
74
80
|
/**
|
|
75
81
|
* Checks if logging is on and the level is at or above minLevel.
|
|
82
|
+
* minLevel does not apply to an action or a metric.
|
|
76
83
|
* @param level Target log severity level.
|
|
77
|
-
* @
|
|
84
|
+
* @param type Record type. Without it, the check applies minLevel.
|
|
85
|
+
* @returns True if the library builds a record of this level and type.
|
|
78
86
|
*/
|
|
79
|
-
isEnabled(level: LogLevel): boolean;
|
|
87
|
+
isEnabled(level: LogLevel, type?: LogType): boolean;
|
|
80
88
|
/**
|
|
81
89
|
* Builds a structured LogRecord from the input. It sanitizes attributes and applies size limits.
|
|
82
90
|
* @param input Logger call input parameters.
|
|
83
|
-
* @returns The record, or null with the reason
|
|
91
|
+
* @returns The record, or null with the reason of the drop.
|
|
84
92
|
*/
|
|
85
93
|
build(input: BuildInput): BuildResult;
|
|
86
94
|
/**
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ResolvedConfig } from "../models/config";
|
|
2
|
+
import type { Diagnostics } from "./diagnostics";
|
|
3
|
+
import { type RemoteSlot, type RemoteUpdate } from "./remote-sampling";
|
|
4
|
+
/**
|
|
5
|
+
* Validates sampling rules from the server. A rule that is not valid rejects all the rules.
|
|
6
|
+
* @param raw Header text, or a parsed object from a custom transport or the bus.
|
|
7
|
+
* @param diagnostics Diagnostics reporter.
|
|
8
|
+
* @returns The rules and their TTL, or null if they are not valid.
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseRemoteRules(raw: unknown, diagnostics: Diagnostics): RemoteUpdate | null;
|
|
11
|
+
/**
|
|
12
|
+
* Puts rules in force in the slot of the runtime. It makes the overlay for the first rules.
|
|
13
|
+
* @param slot Slot of the runtime.
|
|
14
|
+
* @param config Active configuration instance.
|
|
15
|
+
* @param update Validated rules, or null when the upstream has no rules.
|
|
16
|
+
*/
|
|
17
|
+
export declare function applyRemote(slot: RemoteSlot, config: ResolvedConfig, update: RemoteUpdate | null): void;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { REMOTE_SAMPLING_VERSION } from "../constants";
|
|
2
|
+
import type { ResolvedConfig } from "../models/config";
|
|
3
|
+
import type { LogLevel } from "../models/log-record";
|
|
4
|
+
/** Sampling rules from the ingest server, after validation. */
|
|
5
|
+
export interface RemoteRules {
|
|
6
|
+
/** Rate when no namespace rate matches. Without it, the config rate applies. */
|
|
7
|
+
defaultRate?: number;
|
|
8
|
+
/** Rate for each namespace prefix. They apply over the config rates. */
|
|
9
|
+
rates: Record<string, number>;
|
|
10
|
+
/** Lowest level that the library sends. It can only lower `minLevel`. */
|
|
11
|
+
minLevel?: LogLevel;
|
|
12
|
+
}
|
|
13
|
+
/** Rules and their TTL, after validation. */
|
|
14
|
+
export interface RemoteUpdate {
|
|
15
|
+
/** Validated rules. */
|
|
16
|
+
rules: RemoteRules;
|
|
17
|
+
/** Time that the rules stay in force, in milliseconds. */
|
|
18
|
+
ttlMs: number;
|
|
19
|
+
}
|
|
20
|
+
/** Rules in the shape of the header. A context sends them to a forwarder over the bus. */
|
|
21
|
+
export interface RemoteWire extends RemoteRules {
|
|
22
|
+
/** Rules version. */
|
|
23
|
+
v: typeof REMOTE_SAMPLING_VERSION;
|
|
24
|
+
/** Time that the rules stay in force, in milliseconds. */
|
|
25
|
+
ttlMs: number;
|
|
26
|
+
}
|
|
27
|
+
/** Slot of the runtime for the overlay. The sender or the bus fills it when the first rules arrive. */
|
|
28
|
+
export interface RemoteSlot {
|
|
29
|
+
/** Overlay, or null before the first rules. */
|
|
30
|
+
sampling: RemoteSampling | null;
|
|
31
|
+
}
|
|
32
|
+
/** Remote sampling rules in force for this context. */
|
|
33
|
+
export declare class RemoteSampling {
|
|
34
|
+
private readonly config;
|
|
35
|
+
/** Rules in force, or null. */
|
|
36
|
+
private rules;
|
|
37
|
+
/** Time that the rules arrived, in epoch milliseconds. */
|
|
38
|
+
private receivedAt;
|
|
39
|
+
/** Time that the rules end, in epoch milliseconds. */
|
|
40
|
+
private until;
|
|
41
|
+
/**
|
|
42
|
+
* @param config Active configuration instance. A reconfigure changes it in place.
|
|
43
|
+
*/
|
|
44
|
+
constructor(config: ResolvedConfig);
|
|
45
|
+
/**
|
|
46
|
+
* Puts new rules in force. They replace the rules in force.
|
|
47
|
+
* @param update Validated rules and their TTL, or null when the upstream has no rules.
|
|
48
|
+
*/
|
|
49
|
+
apply(update: RemoteUpdate | null): void;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the rules in force.
|
|
52
|
+
* @returns The rules, or null after the TTL or when `sampling.remote` is off.
|
|
53
|
+
*/
|
|
54
|
+
current(): RemoteRules | null;
|
|
55
|
+
/**
|
|
56
|
+
* Returns the lowest level that the library sends.
|
|
57
|
+
* @returns The remote level if it is lower than `minLevel`. If not, `minLevel`.
|
|
58
|
+
*/
|
|
59
|
+
minLevel(): LogLevel;
|
|
60
|
+
/**
|
|
61
|
+
* Returns the rules in force in the shape of the header, with the remaining TTL.
|
|
62
|
+
* @returns The rules, or null if no rules are in force.
|
|
63
|
+
*/
|
|
64
|
+
wire(): RemoteWire | null;
|
|
65
|
+
}
|
package/dist/core/runtime.d.ts
CHANGED
|
@@ -14,6 +14,17 @@ import { JourneyEngine } from "./journey";
|
|
|
14
14
|
import { RecordBuilder } from "./record";
|
|
15
15
|
/** Manages subsystem lifecycles, bus coordination and record delivery. */
|
|
16
16
|
export declare class ObservabilityRuntime {
|
|
17
|
+
private readonly degraded;
|
|
18
|
+
/**
|
|
19
|
+
* Runtime of this copy of the library when another version owns the page runtime.
|
|
20
|
+
* It stays off the global key.
|
|
21
|
+
*/
|
|
22
|
+
private static local;
|
|
23
|
+
/**
|
|
24
|
+
* Library version of this runtime. Another copy reads it from the global key.
|
|
25
|
+
* Two versions with one runtime call methods that the other version does not have.
|
|
26
|
+
*/
|
|
27
|
+
readonly sdkVersion: string;
|
|
17
28
|
/** Diagnostics reporter for internal faults. */
|
|
18
29
|
readonly diagnostics: Diagnostics;
|
|
19
30
|
/** Active configuration instance. A reconfigure changes it in place. */
|
|
@@ -40,6 +51,8 @@ export declare class ObservabilityRuntime {
|
|
|
40
51
|
readonly journey: JourneyEngine;
|
|
41
52
|
/** Record builder assembling structured log records. */
|
|
42
53
|
readonly builder: RecordBuilder;
|
|
54
|
+
/** Slot of the remote sampling rules. The sender or the bus fills it. */
|
|
55
|
+
private readonly remote;
|
|
43
56
|
/** Console sink for local developer output. */
|
|
44
57
|
readonly console: ConsoleSink;
|
|
45
58
|
/**
|
|
@@ -69,18 +82,22 @@ export declare class ObservabilityRuntime {
|
|
|
69
82
|
private bootBuffer;
|
|
70
83
|
/** Batch buffer staging records for microtask forwarding. */
|
|
71
84
|
private forwardBuffer;
|
|
72
|
-
/**
|
|
85
|
+
/** True if a microtask drain of the forward buffer is pending. */
|
|
73
86
|
private forwardScheduled;
|
|
74
|
-
/**
|
|
87
|
+
/** True if startup has decided the bus role. A sender load waits for it. */
|
|
75
88
|
private roleResolved;
|
|
76
|
-
/**
|
|
89
|
+
/** Resolves when startup releases the boot records. flush() waits for it. init() sets it. */
|
|
90
|
+
private started;
|
|
91
|
+
/** True if asynchronous initialization is complete. */
|
|
77
92
|
private ready;
|
|
78
|
-
/**
|
|
93
|
+
/** True if shutdown stopped the runtime. */
|
|
79
94
|
private destroyed;
|
|
80
95
|
/** Timestamp of the last reported storage gap warning in epoch milliseconds. */
|
|
81
96
|
private lastGapAt;
|
|
82
97
|
/**
|
|
83
98
|
* @param input Initial consumer configuration options.
|
|
99
|
+
* @param degraded True for a console-only runtime next to another version. It installs
|
|
100
|
+
* no capture.
|
|
84
101
|
*/
|
|
85
102
|
private constructor();
|
|
86
103
|
/**
|
|
@@ -90,17 +107,24 @@ export declare class ObservabilityRuntime {
|
|
|
90
107
|
*/
|
|
91
108
|
readonly busAccept: (message: BusMessage, reply: Receive) => void;
|
|
92
109
|
/**
|
|
93
|
-
* Returns the
|
|
110
|
+
* Returns the runtime on the global key of this realm. Another copy of the library can own it.
|
|
111
|
+
* @returns Pinned runtime instance or null.
|
|
112
|
+
*/
|
|
113
|
+
private static pinned;
|
|
114
|
+
/**
|
|
115
|
+
* Returns the local runtime of this copy. Without one, it returns the pinned runtime.
|
|
116
|
+
* A pinned runtime of another version gives null. A log call then starts the local runtime.
|
|
94
117
|
* @returns Active runtime instance or null.
|
|
95
118
|
*/
|
|
96
119
|
static current(): ObservabilityRuntime | null;
|
|
97
120
|
/**
|
|
98
121
|
* Returns the runtime that shutdown() stopped, until the next configure().
|
|
122
|
+
* A stopped runtime of another version gives null.
|
|
99
123
|
* @returns Stopped runtime instance or null.
|
|
100
124
|
*/
|
|
101
125
|
static stopped(): ObservabilityRuntime | null;
|
|
102
126
|
/**
|
|
103
|
-
* Tests
|
|
127
|
+
* Tests if shutdown() stopped this runtime.
|
|
104
128
|
* @returns True after destroy() starts.
|
|
105
129
|
*/
|
|
106
130
|
isDestroyed(): boolean;
|
|
@@ -110,6 +134,14 @@ export declare class ObservabilityRuntime {
|
|
|
110
134
|
* @returns Active runtime instance.
|
|
111
135
|
*/
|
|
112
136
|
static configure(input: Partial<ObservabilityConfig>): ObservabilityRuntime;
|
|
137
|
+
/**
|
|
138
|
+
* Starts a console-only runtime next to the runtime of another version. It sends nothing,
|
|
139
|
+
* joins no bus and installs no capture. The other version keeps the page runtime.
|
|
140
|
+
* @param input Consumer configuration options.
|
|
141
|
+
* @param found sdkVersion of the pinned runtime. A runtime before 0.9.0 has none.
|
|
142
|
+
* @returns The local runtime.
|
|
143
|
+
*/
|
|
144
|
+
private static startDegraded;
|
|
113
145
|
/**
|
|
114
146
|
* Updates runtime configuration and refreshes affected subsystems.
|
|
115
147
|
* @param input Partial configuration to merge.
|
|
@@ -122,12 +154,12 @@ export declare class ObservabilityRuntime {
|
|
|
122
154
|
*/
|
|
123
155
|
private keepBuiltStorage;
|
|
124
156
|
/**
|
|
125
|
-
* Refuses
|
|
126
|
-
* the retry engine removes each stored batch as delivered.
|
|
157
|
+
* Refuses a change that leaves the built delivery stack with no destination.
|
|
158
|
+
* Without a destination, the retry engine removes each stored batch as delivered.
|
|
127
159
|
* @param input Partial configuration to merge.
|
|
128
|
-
* @returns Input
|
|
160
|
+
* @returns Input without the refused changes.
|
|
129
161
|
*/
|
|
130
|
-
private
|
|
162
|
+
private keepDestination;
|
|
131
163
|
/**
|
|
132
164
|
* Restores the identity strategy from startup. The ids exist from startup.
|
|
133
165
|
* A later change cannot move them to another store.
|
|
@@ -135,19 +167,21 @@ export declare class ObservabilityRuntime {
|
|
|
135
167
|
private keepBuiltIdentity;
|
|
136
168
|
/** Initializes asynchronous subsystems and flushes the boot buffer. */
|
|
137
169
|
private init;
|
|
170
|
+
/** Resolves the bus role, loads the delivery stack and releases the boot records. It never rejects. */
|
|
171
|
+
private boot;
|
|
138
172
|
/**
|
|
139
173
|
* Starts the load of the bus one time.
|
|
140
|
-
* @returns Promise
|
|
174
|
+
* @returns Promise that resolves to the started bus, or null if it did not load.
|
|
141
175
|
*/
|
|
142
176
|
private ensureBus;
|
|
143
177
|
/**
|
|
144
178
|
* Loads and constructs the bus one time. It does not call start().
|
|
145
|
-
* @returns Promise
|
|
179
|
+
* @returns Promise that resolves to the constructed bus, or null if it did not load.
|
|
146
180
|
*/
|
|
147
181
|
private constructBus;
|
|
148
182
|
/**
|
|
149
183
|
* Starts the constructed bus. It never rejects.
|
|
150
|
-
* @returns Promise
|
|
184
|
+
* @returns Promise that resolves to the started bus, or null if it did not load or start.
|
|
151
185
|
*/
|
|
152
186
|
private startBus;
|
|
153
187
|
/** Imports and constructs the bus. It never rejects. */
|
|
@@ -159,12 +193,12 @@ export declare class ObservabilityRuntime {
|
|
|
159
193
|
private role;
|
|
160
194
|
/**
|
|
161
195
|
* Loads the delivery stack if this context is the sender and has an endpoint.
|
|
162
|
-
* @returns Promise
|
|
196
|
+
* @returns Promise that resolves when the load completes, or immediately when nothing loads.
|
|
163
197
|
*/
|
|
164
198
|
private syncSender;
|
|
165
199
|
/**
|
|
166
200
|
* Starts the load of the delivery stack one time.
|
|
167
|
-
* @returns Promise
|
|
201
|
+
* @returns Promise that resolves when the load completes or fails.
|
|
168
202
|
*/
|
|
169
203
|
private loadSender;
|
|
170
204
|
/** Imports and builds the delivery stack. It then moves the held records into it. It never rejects. */
|
|
@@ -176,7 +210,7 @@ export declare class ObservabilityRuntime {
|
|
|
176
210
|
/**
|
|
177
211
|
* Imports one capture module. A failed import reports and skips that module only.
|
|
178
212
|
* @param name Capture module name.
|
|
179
|
-
* @returns Promise
|
|
213
|
+
* @returns Promise that resolves to the class, or undefined if the import failed.
|
|
180
214
|
*/
|
|
181
215
|
private loadCapture;
|
|
182
216
|
/**
|
|
@@ -232,13 +266,13 @@ export declare class ObservabilityRuntime {
|
|
|
232
266
|
private regenerateTabId;
|
|
233
267
|
/**
|
|
234
268
|
* Counts the batches held in storage after a failed or deferred delivery.
|
|
235
|
-
* @returns Promise
|
|
269
|
+
* @returns Promise that resolves to the number of stored batches. A forwarder stores
|
|
236
270
|
* nothing and reports 0.
|
|
237
271
|
*/
|
|
238
272
|
queueDepth(): Promise<number>;
|
|
239
273
|
/**
|
|
240
274
|
* Flushes all pending records in the pipeline or forward buffer.
|
|
241
|
-
* @returns Promise
|
|
275
|
+
* @returns Promise that resolves when the library sends or hands off the queued records.
|
|
242
276
|
*/
|
|
243
277
|
flush(): Promise<void>;
|
|
244
278
|
}
|
package/dist/core/sampling.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ResolvedConfig } from "../models/config";
|
|
2
2
|
import type { LogRecord } from "../models/log-record";
|
|
3
|
+
import type { RemoteRules } from "./remote-sampling";
|
|
3
4
|
/** Fields that sampling reads. The caller knows all of them before it builds a record. */
|
|
4
5
|
export interface SampleInput {
|
|
5
6
|
/** Severity level of the entry. */
|
|
@@ -18,17 +19,19 @@ export interface SampleInput {
|
|
|
18
19
|
* This lets the caller drop an entry before the attribute walk runs.
|
|
19
20
|
* @param input Fields the sampling decision reads.
|
|
20
21
|
* @param config Active configuration instance.
|
|
22
|
+
* @param remote Remote rules in force, or null.
|
|
21
23
|
* @returns True if the runtime keeps and sends the entry.
|
|
22
24
|
*/
|
|
23
|
-
export declare function shouldSampleInput(input: SampleInput, config: ResolvedConfig): boolean;
|
|
25
|
+
export declare function shouldSampleInput(input: SampleInput, config: ResolvedConfig, remote: RemoteRules | null): boolean;
|
|
24
26
|
/**
|
|
25
27
|
* Decides if sampling keeps a built log record. The runtime uses it for records
|
|
26
28
|
* that a child context sends over the bus.
|
|
27
29
|
* @param record Record to evaluate.
|
|
28
30
|
* @param config Active configuration instance.
|
|
31
|
+
* @param remote Remote rules in force, or null.
|
|
29
32
|
* @returns True if the runtime keeps and sends the record.
|
|
30
33
|
*/
|
|
31
|
-
export declare function shouldSample(record: LogRecord, config: ResolvedConfig): boolean;
|
|
34
|
+
export declare function shouldSample(record: LogRecord, config: ResolvedConfig, remote?: RemoteRules | null): boolean;
|
|
32
35
|
/**
|
|
33
36
|
* Sets a fixed-window limit on the ERROR and FATAL records that bypass sampling.
|
|
34
37
|
*
|
package/dist/core/sender.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { ResolvedConfig } from "../models/config";
|
|
|
2
2
|
import type { LogRecord } from "../models/log-record";
|
|
3
3
|
import type { GapReporter } from "../models/storage";
|
|
4
4
|
import type { Diagnostics } from "./diagnostics";
|
|
5
|
+
import type { RemoteSlot } from "./remote-sampling";
|
|
5
6
|
/** Inputs to build the delivery stack. */
|
|
6
7
|
export interface SenderDeps {
|
|
7
8
|
/** Active configuration instance. */
|
|
@@ -10,18 +11,18 @@ export interface SenderDeps {
|
|
|
10
11
|
diagnostics: Diagnostics;
|
|
11
12
|
/** Receives a report when storage drops batches before delivery. */
|
|
12
13
|
onGap: GapReporter;
|
|
14
|
+
/** Slot of the runtime for the sampling rules from the ingest server. */
|
|
15
|
+
remote: RemoteSlot;
|
|
13
16
|
}
|
|
14
17
|
/** Delivery stack for one sender runtime. */
|
|
15
18
|
export declare class Sender {
|
|
16
19
|
private readonly storage;
|
|
17
|
-
private readonly transport;
|
|
18
20
|
private readonly retry;
|
|
19
21
|
private readonly pipeline;
|
|
20
22
|
private readonly exitFlush;
|
|
21
23
|
private readonly diagnostics;
|
|
22
24
|
/**
|
|
23
25
|
* @param storage Storage adapter for undelivered batches.
|
|
24
|
-
* @param transport HTTP transport for live delivery.
|
|
25
26
|
* @param retry Retry engine for stored batches.
|
|
26
27
|
* @param pipeline Pipeline that batches records.
|
|
27
28
|
* @param exitFlush Handler that sends pending records on unload.
|
|
@@ -29,9 +30,10 @@ export declare class Sender {
|
|
|
29
30
|
*/
|
|
30
31
|
private constructor();
|
|
31
32
|
/**
|
|
32
|
-
* Builds the delivery stack
|
|
33
|
+
* Builds the delivery stack and starts the retry loop. The emergency queue import runs
|
|
34
|
+
* after the start. A slow IndexedDB open then does not hold the boot buffer.
|
|
33
35
|
* @param deps Configuration, diagnostics and gap reporter.
|
|
34
|
-
* @returns Promise
|
|
36
|
+
* @returns Promise that resolves to the started sender.
|
|
35
37
|
*/
|
|
36
38
|
static create(deps: SenderDeps): Promise<Sender>;
|
|
37
39
|
/**
|
|
@@ -40,19 +42,25 @@ export declare class Sender {
|
|
|
40
42
|
*/
|
|
41
43
|
push(record: LogRecord): void;
|
|
42
44
|
/**
|
|
43
|
-
* Sends the records
|
|
44
|
-
* @returns Promise
|
|
45
|
+
* Sends the records in the stream buffers. Then it waits for each batch in flight.
|
|
46
|
+
* @returns Promise that resolves when the pipeline sends or stores each batch.
|
|
45
47
|
*/
|
|
46
48
|
flush(): Promise<void>;
|
|
47
49
|
/**
|
|
48
50
|
* Counts the batches held in storage.
|
|
49
|
-
* @returns Promise
|
|
51
|
+
* @returns Promise that resolves to the batch count, or 0 if storage fails.
|
|
50
52
|
*/
|
|
51
53
|
queueDepth(): Promise<number>;
|
|
52
54
|
/**
|
|
53
|
-
* Sends pending records
|
|
54
|
-
*
|
|
55
|
-
*
|
|
55
|
+
* Sends pending records with a normal flush, then with the exit flush. Then it stops all
|
|
56
|
+
* timers and listeners. Then it closes storage. The normal flush keeps the headers, gzip
|
|
57
|
+
* and the retry. The exit flush takes the records that the normal flush did not confirm.
|
|
58
|
+
* @returns Promise that resolves when storage closes.
|
|
56
59
|
*/
|
|
57
60
|
destroy(): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Moves the batches of the emergency queue into storage, then wakes the retry engine.
|
|
63
|
+
* The previous page load parked them during its unload.
|
|
64
|
+
*/
|
|
65
|
+
private importEmergency;
|
|
58
66
|
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Qt as e, un as t } from "./constants-DM94FjVX.js";
|
|
2
|
+
//#region src/utils/identity.ts
|
|
3
|
+
var n = {
|
|
4
|
+
local: () => localStorage,
|
|
5
|
+
session: () => sessionStorage
|
|
6
|
+
};
|
|
7
|
+
function r() {
|
|
8
|
+
let e = (e) => ({
|
|
9
|
+
getItem: (t) => e.get(t) ?? null,
|
|
10
|
+
setItem: (t, n) => {
|
|
11
|
+
e.set(t, n);
|
|
12
|
+
},
|
|
13
|
+
removeItem: (t) => {
|
|
14
|
+
e.delete(t);
|
|
15
|
+
}
|
|
16
|
+
}), t = e(/* @__PURE__ */ new Map()), n = e(/* @__PURE__ */ new Map());
|
|
17
|
+
return {
|
|
18
|
+
local: () => t,
|
|
19
|
+
session: () => n
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function i(e) {
|
|
23
|
+
let t = new Uint8Array(e);
|
|
24
|
+
if (typeof crypto < "u" && typeof crypto.getRandomValues == "function") crypto.getRandomValues(t);
|
|
25
|
+
else for (let n = 0; n < e; n++) t[n] = Math.floor(Math.random() * 256);
|
|
26
|
+
return Array.from(t, (e) => e.toString(16).padStart(2, "0")).join("");
|
|
27
|
+
}
|
|
28
|
+
function a(e) {
|
|
29
|
+
try {
|
|
30
|
+
if (typeof crypto < "u" && typeof crypto.randomUUID == "function") return crypto.randomUUID();
|
|
31
|
+
} catch {
|
|
32
|
+
e.count("platform.api_blocked");
|
|
33
|
+
}
|
|
34
|
+
return i(16);
|
|
35
|
+
}
|
|
36
|
+
function o(e, t, n, r) {
|
|
37
|
+
return r.guard("storage.unavailable", `The library could not read ${t}Storage.`, () => e[t]().getItem(n)) ?? null;
|
|
38
|
+
}
|
|
39
|
+
function s(e, t, n, r, i) {
|
|
40
|
+
i.guard("storage.unavailable", `The library could not write to ${t}Storage.`, () => {
|
|
41
|
+
e[t]().setItem(n, r);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function c(r, i = n) {
|
|
45
|
+
let c = a(r), l = Date.now(), u = a(r), d = o(i, "local", e, r);
|
|
46
|
+
if (d) try {
|
|
47
|
+
let e = JSON.parse(d);
|
|
48
|
+
e.id && l - (e.lastSeenAt ?? 0) < 18e5 && (u = e.id);
|
|
49
|
+
} catch {
|
|
50
|
+
r.report("storage.degraded", "The session record is not valid JSON. The library starts a new session.");
|
|
51
|
+
}
|
|
52
|
+
let f = JSON.stringify({
|
|
53
|
+
id: u,
|
|
54
|
+
lastSeenAt: l
|
|
55
|
+
});
|
|
56
|
+
s(i, "local", e, f, r);
|
|
57
|
+
let p = o(i, "session", "ui-observability.tab", r) ?? "";
|
|
58
|
+
return p || (p = a(r), s(i, "session", t, p, r)), {
|
|
59
|
+
sessionId: u,
|
|
60
|
+
tabId: p,
|
|
61
|
+
contextId: c
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function l(e, n, r) {
|
|
65
|
+
s(r, "session", t, e, n);
|
|
66
|
+
}
|
|
67
|
+
var u = class {
|
|
68
|
+
diagnostics;
|
|
69
|
+
stores;
|
|
70
|
+
lastTouchAt = 0;
|
|
71
|
+
constructor(e, t = n) {
|
|
72
|
+
this.diagnostics = e, this.stores = t;
|
|
73
|
+
}
|
|
74
|
+
touch(t) {
|
|
75
|
+
let n = Date.now();
|
|
76
|
+
if (n - this.lastTouchAt < 6e4) return;
|
|
77
|
+
this.lastTouchAt = n;
|
|
78
|
+
let r = JSON.stringify({
|
|
79
|
+
id: t,
|
|
80
|
+
lastSeenAt: n
|
|
81
|
+
});
|
|
82
|
+
s(this.stores, "local", e, r, this.diagnostics);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/utils/unref.ts
|
|
87
|
+
function d(e) {
|
|
88
|
+
typeof e != "number" && e.unref?.();
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/utils/deadline.ts
|
|
92
|
+
var f = class extends Error {
|
|
93
|
+
constructor(e) {
|
|
94
|
+
super(`The operation took longer than ${String(e)} ms.`), this.name = "DeadlineError";
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
function p(e, t) {
|
|
98
|
+
let n, r = new Promise((e, r) => {
|
|
99
|
+
let i = setTimeout(() => {
|
|
100
|
+
r(new f(t));
|
|
101
|
+
}, t);
|
|
102
|
+
n = i, d(i);
|
|
103
|
+
});
|
|
104
|
+
return Promise.race([e, r]).finally(() => {
|
|
105
|
+
clearTimeout(n);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
export { u as a, l as c, n as i, i as l, p as n, r as o, d as r, a as s, f as t, c as u };
|
|
110
|
+
|
|
111
|
+
//# sourceMappingURL=deadline-B5DINjD5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deadline-B5DINjD5.js","names":["SESSION_ID_KEY","TAB_ID_KEY","BROWSER_STORES","memoryStores","wrap","map","key","value","localStore","sessionStore","randomHex","bytes","byteLength","i","b","newId","safeRead","diagnostics","store","stores","safeWrite","resolveIdentity","contextId","now","sessionId","rawSession","parsed","session","tabId","persistTabId","SessionTouch","unrefTimer","timer","DeadlineError","ms","withDeadline","expire","deadline","work"],"sources":["../src/utils/identity.ts","../src/utils/unref.ts","../src/utils/deadline.ts"],"sourcesContent":["// src/utils/identity.ts\n//\n// Makes and keeps the session, tab, and context correlation identifiers. In a\n// sandboxed iframe and in private mode, Storage throws instead of returning null.\n// An unguarded read breaks the library at import.\n\nimport {\n BYTE_VALUE_COUNT,\n ID_BYTE_LENGTH,\n SESSION_ID_KEY,\n SESSION_IDLE_MS,\n SESSION_TOUCH_MS,\n TAB_ID_KEY,\n} from \"../constants\";\nimport type { Diagnostics } from \"../core/diagnostics\";\n\n/** Correlation identifiers attached to log records across session, tab, and realm scopes. */\nexport interface Identity {\n /** Origin-wide user session identifier surviving across tabs until idle expiration. */\n sessionId: string;\n /** Browser tab or desktop window identifier surviving document reloads. */\n tabId: string;\n /** Unique execution realm identifier regenerated on every document load. */\n contextId: string;\n}\n\n/** The three Storage methods that identity and journey code use. */\nexport interface KeyValueStore {\n /**\n * Reads one value.\n * @param key Storage key.\n * @returns Stored value, or null.\n */\n getItem(key: string): string | null;\n /**\n * Writes one value.\n * @param key Storage key.\n * @param value Value to write.\n */\n setItem(key: string, value: string): void;\n /**\n * Removes one value.\n * @param key Storage key.\n */\n removeItem(key: string): void;\n}\n\n/** Stores for identity and journey keys. A getter can throw in a sandboxed frame. */\nexport interface IdentityStores {\n /** Store that lasts across tabs and reloads. */\n local(): KeyValueStore;\n /** Store that lasts for one tab. */\n session(): KeyValueStore;\n}\n\n/** Stores that write to the browser. The `identity: \"storage\"` option uses them. */\nexport const BROWSER_STORES: IdentityStores = {\n local: () => localStorage,\n session: () => sessionStorage,\n};\n\n/**\n * Creates stores that keep values in memory only. The `identity: \"memory\"` option uses them.\n * @returns Stores that write nothing to the browser.\n */\nexport function memoryStores(): IdentityStores {\n const wrap = (map: Map<string, string>): KeyValueStore => ({\n getItem: (key) => map.get(key) ?? null,\n setItem: (key, value) => {\n map.set(key, value);\n },\n removeItem: (key) => {\n map.delete(key);\n },\n });\n\n const localStore = wrap(new Map());\n const sessionStore = wrap(new Map());\n\n return { local: () => localStore, session: () => sessionStore };\n}\n\n/**\n * Generates a random lowercase hexadecimal string of the specified byte length.\n * It uses crypto.getRandomValues when it is available. If not, it uses Math.random.\n * @param byteLength Number of random bytes to generate.\n * @returns Hexadecimal string of length byteLength * 2.\n */\nexport function randomHex(byteLength: number): string {\n const bytes = new Uint8Array(byteLength);\n if (typeof crypto !== \"undefined\" && typeof crypto.getRandomValues === \"function\") {\n crypto.getRandomValues(bytes);\n } else {\n for (let i = 0; i < byteLength; i++) {\n bytes[i] = Math.floor(Math.random() * BYTE_VALUE_COUNT);\n }\n }\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\n/**\n * Generates a unique identifier string using crypto.randomUUID or randomHex fallback.\n * @param diagnostics Diagnostics reporter. It counts a blocked crypto.randomUUID call.\n * @returns UUID or random hex identifier.\n */\nexport function newId(diagnostics: Diagnostics): string {\n try {\n if (typeof crypto !== \"undefined\" && typeof crypto.randomUUID === \"function\") {\n return crypto.randomUUID();\n }\n } catch {\n // A sandbox can block crypto.randomUUID with a SecurityError. The method exists,\n // but the call throws. Count the fault. A report on each id is noise.\n diagnostics.count(\"platform.api_blocked\");\n }\n return randomHex(ID_BYTE_LENGTH);\n}\n\n/**\n * Reads a value from the local or session store. Reports a storage error to diagnostics.\n * @param stores Identity stores.\n * @param store Storage target (\"local\" or \"session\").\n * @param key Storage key.\n * @param diagnostics Diagnostics reporter.\n * @returns Stored string value or null if unreadable or absent.\n */\nfunction safeRead(\n stores: IdentityStores,\n store: \"local\" | \"session\",\n key: string,\n diagnostics: Diagnostics,\n): string | null {\n return (\n diagnostics.guard(\"storage.unavailable\", `The library could not read ${store}Storage.`, () =>\n stores[store]().getItem(key),\n ) ?? null\n );\n}\n\n/**\n * Writes a key-value pair to the local or session store. Reports a storage error to diagnostics.\n * @param stores Identity stores.\n * @param store Storage target (\"local\" or \"session\").\n * @param key Storage key.\n * @param value String value to store.\n * @param diagnostics Diagnostics reporter.\n */\nfunction safeWrite(\n stores: IdentityStores,\n store: \"local\" | \"session\",\n key: string,\n value: string,\n diagnostics: Diagnostics,\n): void {\n diagnostics.guard(\n \"storage.unavailable\",\n `The library could not write to ${store}Storage.`,\n () => {\n stores[store]().setItem(key, value);\n },\n );\n}\n\n/**\n * Resolves or initializes session, tab, and context identifiers for the active realm.\n * @param diagnostics Diagnostics reporter.\n * @param stores Identity stores.\n * @returns Populated Identity object.\n */\nexport function resolveIdentity(\n diagnostics: Diagnostics,\n stores: IdentityStores = BROWSER_STORES,\n): Identity {\n const contextId = newId(diagnostics);\n const now = Date.now();\n\n let sessionId = newId(diagnostics);\n const rawSession = safeRead(stores, \"local\", SESSION_ID_KEY, diagnostics);\n if (rawSession) {\n try {\n const parsed = JSON.parse(rawSession) as {\n id?: string;\n lastSeenAt?: number;\n };\n if (parsed.id && now - (parsed.lastSeenAt ?? 0) < SESSION_IDLE_MS) {\n sessionId = parsed.id;\n }\n } catch {\n diagnostics.report(\n \"storage.degraded\",\n \"The session record is not valid JSON. The library starts a new session.\",\n );\n }\n }\n const session = JSON.stringify({ id: sessionId, lastSeenAt: now });\n safeWrite(stores, \"local\", SESSION_ID_KEY, session, diagnostics);\n\n let tabId = safeRead(stores, \"session\", TAB_ID_KEY, diagnostics) ?? \"\";\n if (!tabId) {\n tabId = newId(diagnostics);\n safeWrite(stores, \"session\", TAB_ID_KEY, tabId, diagnostics);\n }\n\n return { sessionId, tabId, contextId };\n}\n\n/**\n * Writes a new tab id to the session store.\n * @param tabId New tab identifier.\n * @param diagnostics Diagnostics reporter.\n * @param stores Identity stores.\n */\nexport function persistTabId(\n tabId: string,\n diagnostics: Diagnostics,\n stores: IdentityStores,\n): void {\n safeWrite(stores, \"session\", TAB_ID_KEY, tabId, diagnostics);\n}\n\n/**\n * Writes the last-seen timestamp of the session. `SESSION_TOUCH_MS` sets the throttle.\n * The instance keeps the throttle state, not the module. Two runtimes in one document\n * must not block the session writes of each other.\n */\nexport class SessionTouch {\n /** Epoch milliseconds of the last write this instance made. */\n private lastTouchAt = 0;\n\n /**\n * @param diagnostics Diagnostics reporter.\n * @param stores Identity stores.\n */\n constructor(\n private diagnostics: Diagnostics,\n private readonly stores: IdentityStores = BROWSER_STORES,\n ) {}\n\n /**\n * Writes the session activity time to the local store. It writes one time in each throttle window.\n * @param sessionId Active session identifier.\n */\n touch(sessionId: string): void {\n const now = Date.now();\n\n if (now - this.lastTouchAt < SESSION_TOUCH_MS) {\n return;\n }\n\n this.lastTouchAt = now;\n const session = JSON.stringify({ id: sessionId, lastSeenAt: now });\n safeWrite(this.stores, \"local\", SESSION_ID_KEY, session, this.diagnostics);\n }\n}\n","// src/utils/unref.ts\n//\n// Unreferences timer handles in Node and browser environments. A pending timer\n// keeps a Node process alive under SSR and in tests.\n\n/** Timer handle. It is a number in a browser and a Timeout object in Node. */\nexport type TimerHandle = number | { unref?: () => void };\n\n/**\n * Unreferences a timer handle. A pending timer then does not keep a Node.js process alive.\n * @param timer Timer handle returned by setTimeout.\n */\nexport function unrefTimer(timer: TimerHandle): void {\n if (typeof timer !== \"number\") {\n timer.unref?.();\n }\n}\n","// src/utils/deadline.ts\n//\n// Sets a deadline for a promise that can stay pending. A pending promise raises no\n// diagnostics event. Without a deadline, its caller waits forever and keeps its slot.\n\nimport { unrefTimer } from \"./unref\";\n\n/** Error of a promise that did not settle before its deadline. */\nexport class DeadlineError extends Error {\n /**\n * @param ms Deadline in milliseconds.\n */\n constructor(ms: number) {\n super(`The operation took longer than ${String(ms)} ms.`);\n this.name = \"DeadlineError\";\n }\n}\n\n/**\n * Rejects when a promise does not settle within a deadline.\n * @param work Promise to bound.\n * @param ms Deadline in milliseconds.\n * @returns The value of the promise, or a rejection after the deadline.\n */\nexport function withDeadline<T>(work: Promise<T>, ms: number): Promise<T> {\n let expire: ReturnType<typeof setTimeout> | undefined;\n\n const deadline = new Promise<never>((_resolve, reject) => {\n const timer = setTimeout(() => {\n reject(new DeadlineError(ms));\n }, ms);\n\n expire = timer;\n unrefTimer(timer);\n });\n\n // Clear the timer on success and on failure. If not, each storage call leaves an armed timer.\n return Promise.race([work, deadline]).finally(() => {\n clearTimeout(expire);\n });\n}\n"],"mappings":"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"}
|