@liteguard/core 0.2.20260314 → 0.3.20260315
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/README.md +2 -2
- package/dist/index.d.mts +24 -16
- package/dist/index.d.ts +24 -16
- package/dist/index.js +66 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +66 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ This is an **internal package** consumed by `@liteguard/liteguard-node`, `@liteg
|
|
|
24
24
|
|
|
25
25
|
| Package | Description |
|
|
26
26
|
|---|---|
|
|
27
|
-
| [`@liteguard/liteguard`](https://www.npmjs.com/package/@liteguard/liteguard) | Umbrella package
|
|
27
|
+
| [`@liteguard/liteguard`](https://www.npmjs.com/package/@liteguard/liteguard) | Umbrella package. Automatic runtime resolution |
|
|
28
28
|
| [`@liteguard/liteguard-node`](https://www.npmjs.com/package/@liteguard/liteguard-node) | Node.js runtime |
|
|
29
29
|
| [`@liteguard/liteguard-browser`](https://www.npmjs.com/package/@liteguard/liteguard-browser) | Browser runtime |
|
|
30
30
|
| [`@liteguard/liteguard-react`](https://www.npmjs.com/package/@liteguard/liteguard-react) | React bindings |
|
|
@@ -33,4 +33,4 @@ This is an **internal package** consumed by `@liteguard/liteguard-node`, `@liteg
|
|
|
33
33
|
|
|
34
34
|
## License
|
|
35
35
|
|
|
36
|
-
Apache 2.0
|
|
36
|
+
Apache 2.0 see [LICENSE](https://github.com/liteguard/liteguard/blob/main/LICENSE).
|
package/dist/index.d.mts
CHANGED
|
@@ -39,7 +39,7 @@ interface ProtectedContext {
|
|
|
39
39
|
signature: string;
|
|
40
40
|
}
|
|
41
41
|
interface GetGuardsRequest {
|
|
42
|
-
|
|
42
|
+
projectClientToken: string;
|
|
43
43
|
environment: string;
|
|
44
44
|
protectedContext?: ProtectedContext;
|
|
45
45
|
}
|
|
@@ -91,10 +91,17 @@ interface Signal {
|
|
|
91
91
|
droppedSignalsSinceLast: number;
|
|
92
92
|
measurement?: SignalPerformance;
|
|
93
93
|
}
|
|
94
|
+
interface UnadoptedGuardObservation {
|
|
95
|
+
guardName: string;
|
|
96
|
+
firstSeenMs: number;
|
|
97
|
+
lastSeenMs: number;
|
|
98
|
+
checkCount: number;
|
|
99
|
+
estimatedChecksPerMinute: number;
|
|
100
|
+
}
|
|
94
101
|
interface SendUnadoptedGuardsRequest {
|
|
95
|
-
|
|
102
|
+
projectClientToken: string;
|
|
96
103
|
environment: string;
|
|
97
|
-
|
|
104
|
+
observations: UnadoptedGuardObservation[];
|
|
98
105
|
}
|
|
99
106
|
interface SendUnadoptedGuardsResponse {
|
|
100
107
|
accepted: boolean;
|
|
@@ -225,7 +232,7 @@ interface RuntimeAdapter {
|
|
|
225
232
|
clearInterval(handle: unknown): void;
|
|
226
233
|
/**
|
|
227
234
|
* Detach a timer handle so it does not prevent the process from exiting
|
|
228
|
-
* (e.g. call `handle.unref()` on Node.js). Optional
|
|
235
|
+
* (e.g. call `handle.unref()` on Node.js). Optional, no-op by default.
|
|
229
236
|
*/
|
|
230
237
|
detachTimer?(handle: unknown): void;
|
|
231
238
|
/**
|
|
@@ -278,7 +285,7 @@ type ScopeSnapshot = {
|
|
|
278
285
|
declare class LiteguardScope {
|
|
279
286
|
private readonly client;
|
|
280
287
|
private readonly snapshot;
|
|
281
|
-
/** @internal
|
|
288
|
+
/** @internal use {@link BaseLiteguardClient.createScope} instead. */
|
|
282
289
|
constructor(client: BaseLiteguardClient, snapshot: ScopeSnapshot);
|
|
283
290
|
/**
|
|
284
291
|
* Derive a new scope with the given properties merged on top of this
|
|
@@ -289,7 +296,7 @@ declare class LiteguardScope {
|
|
|
289
296
|
*/
|
|
290
297
|
withProperties(properties: Properties): LiteguardScope;
|
|
291
298
|
/**
|
|
292
|
-
* Alias for {@link withProperties}
|
|
299
|
+
* Alias for {@link withProperties} derives a new scope with additional properties.
|
|
293
300
|
*
|
|
294
301
|
* @param properties - Key/value pairs to add.
|
|
295
302
|
* @returns A new {@link LiteguardScope} with the merged properties.
|
|
@@ -407,14 +414,13 @@ declare class LiteguardScope {
|
|
|
407
414
|
* {@link RuntimeAdapter}.
|
|
408
415
|
*/
|
|
409
416
|
declare class BaseLiteguardClient {
|
|
410
|
-
private readonly
|
|
417
|
+
private readonly projectClientToken;
|
|
411
418
|
private readonly runtime;
|
|
412
419
|
private readonly options;
|
|
413
420
|
private readonly bundles;
|
|
414
421
|
private defaultScope;
|
|
415
422
|
private signalBuffer;
|
|
416
423
|
private droppedSignalsPending;
|
|
417
|
-
private readonly reportedUnadoptedGuards;
|
|
418
424
|
private readonly pendingUnadoptedGuards;
|
|
419
425
|
private refreshTimer;
|
|
420
426
|
private flushTimer;
|
|
@@ -429,10 +435,10 @@ declare class BaseLiteguardClient {
|
|
|
429
435
|
* and begin background timers.
|
|
430
436
|
*
|
|
431
437
|
* @param runtime - Platform adapter providing timers, fetch, and context storage.
|
|
432
|
-
* @param
|
|
438
|
+
* @param projectClientToken - Your project client token from the Liteguard dashboard.
|
|
433
439
|
* @param options - Optional SDK configuration overrides.
|
|
434
440
|
*/
|
|
435
|
-
constructor(runtime: RuntimeAdapter,
|
|
441
|
+
constructor(runtime: RuntimeAdapter, projectClientToken: string, options?: ClientOptions);
|
|
436
442
|
/**
|
|
437
443
|
* Fetch the initial public guard bundle and start background refresh and
|
|
438
444
|
* flush timers. Must be called once after construction before using the
|
|
@@ -452,7 +458,7 @@ declare class BaseLiteguardClient {
|
|
|
452
458
|
* Register a listener that is called whenever the guard bundle is refreshed.
|
|
453
459
|
*
|
|
454
460
|
* @param listener - Callback invoked on each guard bundle update.
|
|
455
|
-
* @returns An unsubscribe function
|
|
461
|
+
* @returns An unsubscribe function. Call it to remove the listener.
|
|
456
462
|
*
|
|
457
463
|
* @example
|
|
458
464
|
* ```ts
|
|
@@ -629,8 +635,8 @@ declare class BaseLiteguardClient {
|
|
|
629
635
|
*/
|
|
630
636
|
clearProtectedContext(): LiteguardScope;
|
|
631
637
|
/**
|
|
632
|
-
|
|
633
|
-
|
|
638
|
+
* Immediately transmit all buffered telemetry signals to the backend. Also
|
|
639
|
+
* flushes any pending unadopted-guard observations.
|
|
634
640
|
*
|
|
635
641
|
* Call this before process exit or page unload to avoid losing signals.
|
|
636
642
|
*
|
|
@@ -651,6 +657,8 @@ declare class BaseLiteguardClient {
|
|
|
651
657
|
private resolveScope;
|
|
652
658
|
/** Queue a one-time report for a guard that exists only in application code. */
|
|
653
659
|
private recordUnadoptedGuard;
|
|
660
|
+
private mergePendingUnadoptedObservation;
|
|
661
|
+
private toUnadoptedGuardObservation;
|
|
654
662
|
/** Schedule the next periodic guard refresh using the current refresh interval. */
|
|
655
663
|
private scheduleNextRefresh;
|
|
656
664
|
/** Refresh every cached bundle once, then reschedule the next cycle. */
|
|
@@ -705,8 +713,8 @@ declare class BaseLiteguardClient {
|
|
|
705
713
|
_getRefreshRateSeconds(): number;
|
|
706
714
|
/** @internal Return a cloned view of the buffered signals for tests and diagnostics. */
|
|
707
715
|
_getSignalBuffer(): Signal[];
|
|
708
|
-
/** @internal Return the sorted set of queued unadopted
|
|
709
|
-
_getPendingUnadoptedGuards():
|
|
716
|
+
/** @internal Return the sorted set of queued unadopted guard observations. */
|
|
717
|
+
_getPendingUnadoptedGuards(): UnadoptedGuardObservation[];
|
|
710
718
|
/** @internal Return the currently cached bundle keys. */
|
|
711
719
|
_getBundleKeys(): string[];
|
|
712
720
|
/** @internal Expose the request-scope store for tests. */
|
|
@@ -726,4 +734,4 @@ declare class BaseLiteguardClient {
|
|
|
726
734
|
*/
|
|
727
735
|
declare function evaluateGuard(guard: Guard, properties: Properties): boolean;
|
|
728
736
|
|
|
729
|
-
export { BaseLiteguardClient, type ClientOptions, type ExecutionAdapter, type ExecutionState, type FlushOptions, type GetGuardsRequest, type GetGuardsResponse, type Guard, type GuardCheckPerformance, type GuardExecutionPerformance, type LiteguardChangeListener, LiteguardScope, type MeasurementAdapter, type Operator, type Options, type Properties, type PropertyValue, type ProtectedContext, type RequestScopeAdapter, type RequestScopeStore, type Rule, type RuntimeAdapter, type ScopedOptions, type SendUnadoptedGuardsRequest, type SendUnadoptedGuardsResponse, type Signal, type SignalPerformance, type TraceContext, evaluateGuard };
|
|
737
|
+
export { BaseLiteguardClient, type ClientOptions, type ExecutionAdapter, type ExecutionState, type FlushOptions, type GetGuardsRequest, type GetGuardsResponse, type Guard, type GuardCheckPerformance, type GuardExecutionPerformance, type LiteguardChangeListener, LiteguardScope, type MeasurementAdapter, type Operator, type Options, type Properties, type PropertyValue, type ProtectedContext, type RequestScopeAdapter, type RequestScopeStore, type Rule, type RuntimeAdapter, type ScopedOptions, type SendUnadoptedGuardsRequest, type SendUnadoptedGuardsResponse, type Signal, type SignalPerformance, type TraceContext, type UnadoptedGuardObservation, evaluateGuard };
|
package/dist/index.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ interface ProtectedContext {
|
|
|
39
39
|
signature: string;
|
|
40
40
|
}
|
|
41
41
|
interface GetGuardsRequest {
|
|
42
|
-
|
|
42
|
+
projectClientToken: string;
|
|
43
43
|
environment: string;
|
|
44
44
|
protectedContext?: ProtectedContext;
|
|
45
45
|
}
|
|
@@ -91,10 +91,17 @@ interface Signal {
|
|
|
91
91
|
droppedSignalsSinceLast: number;
|
|
92
92
|
measurement?: SignalPerformance;
|
|
93
93
|
}
|
|
94
|
+
interface UnadoptedGuardObservation {
|
|
95
|
+
guardName: string;
|
|
96
|
+
firstSeenMs: number;
|
|
97
|
+
lastSeenMs: number;
|
|
98
|
+
checkCount: number;
|
|
99
|
+
estimatedChecksPerMinute: number;
|
|
100
|
+
}
|
|
94
101
|
interface SendUnadoptedGuardsRequest {
|
|
95
|
-
|
|
102
|
+
projectClientToken: string;
|
|
96
103
|
environment: string;
|
|
97
|
-
|
|
104
|
+
observations: UnadoptedGuardObservation[];
|
|
98
105
|
}
|
|
99
106
|
interface SendUnadoptedGuardsResponse {
|
|
100
107
|
accepted: boolean;
|
|
@@ -225,7 +232,7 @@ interface RuntimeAdapter {
|
|
|
225
232
|
clearInterval(handle: unknown): void;
|
|
226
233
|
/**
|
|
227
234
|
* Detach a timer handle so it does not prevent the process from exiting
|
|
228
|
-
* (e.g. call `handle.unref()` on Node.js). Optional
|
|
235
|
+
* (e.g. call `handle.unref()` on Node.js). Optional, no-op by default.
|
|
229
236
|
*/
|
|
230
237
|
detachTimer?(handle: unknown): void;
|
|
231
238
|
/**
|
|
@@ -278,7 +285,7 @@ type ScopeSnapshot = {
|
|
|
278
285
|
declare class LiteguardScope {
|
|
279
286
|
private readonly client;
|
|
280
287
|
private readonly snapshot;
|
|
281
|
-
/** @internal
|
|
288
|
+
/** @internal use {@link BaseLiteguardClient.createScope} instead. */
|
|
282
289
|
constructor(client: BaseLiteguardClient, snapshot: ScopeSnapshot);
|
|
283
290
|
/**
|
|
284
291
|
* Derive a new scope with the given properties merged on top of this
|
|
@@ -289,7 +296,7 @@ declare class LiteguardScope {
|
|
|
289
296
|
*/
|
|
290
297
|
withProperties(properties: Properties): LiteguardScope;
|
|
291
298
|
/**
|
|
292
|
-
* Alias for {@link withProperties}
|
|
299
|
+
* Alias for {@link withProperties} derives a new scope with additional properties.
|
|
293
300
|
*
|
|
294
301
|
* @param properties - Key/value pairs to add.
|
|
295
302
|
* @returns A new {@link LiteguardScope} with the merged properties.
|
|
@@ -407,14 +414,13 @@ declare class LiteguardScope {
|
|
|
407
414
|
* {@link RuntimeAdapter}.
|
|
408
415
|
*/
|
|
409
416
|
declare class BaseLiteguardClient {
|
|
410
|
-
private readonly
|
|
417
|
+
private readonly projectClientToken;
|
|
411
418
|
private readonly runtime;
|
|
412
419
|
private readonly options;
|
|
413
420
|
private readonly bundles;
|
|
414
421
|
private defaultScope;
|
|
415
422
|
private signalBuffer;
|
|
416
423
|
private droppedSignalsPending;
|
|
417
|
-
private readonly reportedUnadoptedGuards;
|
|
418
424
|
private readonly pendingUnadoptedGuards;
|
|
419
425
|
private refreshTimer;
|
|
420
426
|
private flushTimer;
|
|
@@ -429,10 +435,10 @@ declare class BaseLiteguardClient {
|
|
|
429
435
|
* and begin background timers.
|
|
430
436
|
*
|
|
431
437
|
* @param runtime - Platform adapter providing timers, fetch, and context storage.
|
|
432
|
-
* @param
|
|
438
|
+
* @param projectClientToken - Your project client token from the Liteguard dashboard.
|
|
433
439
|
* @param options - Optional SDK configuration overrides.
|
|
434
440
|
*/
|
|
435
|
-
constructor(runtime: RuntimeAdapter,
|
|
441
|
+
constructor(runtime: RuntimeAdapter, projectClientToken: string, options?: ClientOptions);
|
|
436
442
|
/**
|
|
437
443
|
* Fetch the initial public guard bundle and start background refresh and
|
|
438
444
|
* flush timers. Must be called once after construction before using the
|
|
@@ -452,7 +458,7 @@ declare class BaseLiteguardClient {
|
|
|
452
458
|
* Register a listener that is called whenever the guard bundle is refreshed.
|
|
453
459
|
*
|
|
454
460
|
* @param listener - Callback invoked on each guard bundle update.
|
|
455
|
-
* @returns An unsubscribe function
|
|
461
|
+
* @returns An unsubscribe function. Call it to remove the listener.
|
|
456
462
|
*
|
|
457
463
|
* @example
|
|
458
464
|
* ```ts
|
|
@@ -629,8 +635,8 @@ declare class BaseLiteguardClient {
|
|
|
629
635
|
*/
|
|
630
636
|
clearProtectedContext(): LiteguardScope;
|
|
631
637
|
/**
|
|
632
|
-
|
|
633
|
-
|
|
638
|
+
* Immediately transmit all buffered telemetry signals to the backend. Also
|
|
639
|
+
* flushes any pending unadopted-guard observations.
|
|
634
640
|
*
|
|
635
641
|
* Call this before process exit or page unload to avoid losing signals.
|
|
636
642
|
*
|
|
@@ -651,6 +657,8 @@ declare class BaseLiteguardClient {
|
|
|
651
657
|
private resolveScope;
|
|
652
658
|
/** Queue a one-time report for a guard that exists only in application code. */
|
|
653
659
|
private recordUnadoptedGuard;
|
|
660
|
+
private mergePendingUnadoptedObservation;
|
|
661
|
+
private toUnadoptedGuardObservation;
|
|
654
662
|
/** Schedule the next periodic guard refresh using the current refresh interval. */
|
|
655
663
|
private scheduleNextRefresh;
|
|
656
664
|
/** Refresh every cached bundle once, then reschedule the next cycle. */
|
|
@@ -705,8 +713,8 @@ declare class BaseLiteguardClient {
|
|
|
705
713
|
_getRefreshRateSeconds(): number;
|
|
706
714
|
/** @internal Return a cloned view of the buffered signals for tests and diagnostics. */
|
|
707
715
|
_getSignalBuffer(): Signal[];
|
|
708
|
-
/** @internal Return the sorted set of queued unadopted
|
|
709
|
-
_getPendingUnadoptedGuards():
|
|
716
|
+
/** @internal Return the sorted set of queued unadopted guard observations. */
|
|
717
|
+
_getPendingUnadoptedGuards(): UnadoptedGuardObservation[];
|
|
710
718
|
/** @internal Return the currently cached bundle keys. */
|
|
711
719
|
_getBundleKeys(): string[];
|
|
712
720
|
/** @internal Expose the request-scope store for tests. */
|
|
@@ -726,4 +734,4 @@ declare class BaseLiteguardClient {
|
|
|
726
734
|
*/
|
|
727
735
|
declare function evaluateGuard(guard: Guard, properties: Properties): boolean;
|
|
728
736
|
|
|
729
|
-
export { BaseLiteguardClient, type ClientOptions, type ExecutionAdapter, type ExecutionState, type FlushOptions, type GetGuardsRequest, type GetGuardsResponse, type Guard, type GuardCheckPerformance, type GuardExecutionPerformance, type LiteguardChangeListener, LiteguardScope, type MeasurementAdapter, type Operator, type Options, type Properties, type PropertyValue, type ProtectedContext, type RequestScopeAdapter, type RequestScopeStore, type Rule, type RuntimeAdapter, type ScopedOptions, type SendUnadoptedGuardsRequest, type SendUnadoptedGuardsResponse, type Signal, type SignalPerformance, type TraceContext, evaluateGuard };
|
|
737
|
+
export { BaseLiteguardClient, type ClientOptions, type ExecutionAdapter, type ExecutionState, type FlushOptions, type GetGuardsRequest, type GetGuardsResponse, type Guard, type GuardCheckPerformance, type GuardExecutionPerformance, type LiteguardChangeListener, LiteguardScope, type MeasurementAdapter, type Operator, type Options, type Properties, type PropertyValue, type ProtectedContext, type RequestScopeAdapter, type RequestScopeStore, type Rule, type RuntimeAdapter, type ScopedOptions, type SendUnadoptedGuardsRequest, type SendUnadoptedGuardsResponse, type Signal, type SignalPerformance, type TraceContext, type UnadoptedGuardObservation, evaluateGuard };
|
package/dist/index.js
CHANGED
|
@@ -156,7 +156,7 @@ var signalCounter = 0;
|
|
|
156
156
|
var LiteguardScope = class {
|
|
157
157
|
client;
|
|
158
158
|
snapshot;
|
|
159
|
-
/** @internal
|
|
159
|
+
/** @internal use {@link BaseLiteguardClient.createScope} instead. */
|
|
160
160
|
constructor(client, snapshot) {
|
|
161
161
|
this.client = client;
|
|
162
162
|
this.snapshot = {
|
|
@@ -181,7 +181,7 @@ var LiteguardScope = class {
|
|
|
181
181
|
});
|
|
182
182
|
}
|
|
183
183
|
/**
|
|
184
|
-
* Alias for {@link withProperties}
|
|
184
|
+
* Alias for {@link withProperties} derives a new scope with additional properties.
|
|
185
185
|
*
|
|
186
186
|
* @param properties - Key/value pairs to add.
|
|
187
187
|
* @returns A new {@link LiteguardScope} with the merged properties.
|
|
@@ -333,15 +333,14 @@ var LiteguardScope = class {
|
|
|
333
333
|
}
|
|
334
334
|
};
|
|
335
335
|
var BaseLiteguardClient = class {
|
|
336
|
-
|
|
336
|
+
projectClientToken;
|
|
337
337
|
runtime;
|
|
338
338
|
options;
|
|
339
339
|
bundles = /* @__PURE__ */ new Map();
|
|
340
340
|
defaultScope;
|
|
341
341
|
signalBuffer = [];
|
|
342
342
|
droppedSignalsPending = 0;
|
|
343
|
-
|
|
344
|
-
pendingUnadoptedGuards = /* @__PURE__ */ new Set();
|
|
343
|
+
pendingUnadoptedGuards = /* @__PURE__ */ new Map();
|
|
345
344
|
refreshTimer = null;
|
|
346
345
|
flushTimer = null;
|
|
347
346
|
lifecycleCleanup = null;
|
|
@@ -355,12 +354,12 @@ var BaseLiteguardClient = class {
|
|
|
355
354
|
* and begin background timers.
|
|
356
355
|
*
|
|
357
356
|
* @param runtime - Platform adapter providing timers, fetch, and context storage.
|
|
358
|
-
* @param
|
|
357
|
+
* @param projectClientToken - Your project client token from the Liteguard dashboard.
|
|
359
358
|
* @param options - Optional SDK configuration overrides.
|
|
360
359
|
*/
|
|
361
|
-
constructor(runtime,
|
|
360
|
+
constructor(runtime, projectClientToken, options = {}) {
|
|
362
361
|
this.runtime = runtime;
|
|
363
|
-
this.
|
|
362
|
+
this.projectClientToken = projectClientToken;
|
|
364
363
|
this.options = {
|
|
365
364
|
environment: options.environment ?? "",
|
|
366
365
|
fallback: options.fallback ?? false,
|
|
@@ -429,7 +428,7 @@ var BaseLiteguardClient = class {
|
|
|
429
428
|
* Register a listener that is called whenever the guard bundle is refreshed.
|
|
430
429
|
*
|
|
431
430
|
* @param listener - Callback invoked on each guard bundle update.
|
|
432
|
-
* @returns An unsubscribe function
|
|
431
|
+
* @returns An unsubscribe function. Call it to remove the listener.
|
|
433
432
|
*
|
|
434
433
|
* @example
|
|
435
434
|
* ```ts
|
|
@@ -879,8 +878,8 @@ var BaseLiteguardClient = class {
|
|
|
879
878
|
return this.replaceCurrentScope(nextScope);
|
|
880
879
|
}
|
|
881
880
|
/**
|
|
882
|
-
|
|
883
|
-
|
|
881
|
+
* Immediately transmit all buffered telemetry signals to the backend. Also
|
|
882
|
+
* flushes any pending unadopted-guard observations.
|
|
884
883
|
*
|
|
885
884
|
* Call this before process exit or page unload to avoid losing signals.
|
|
886
885
|
*
|
|
@@ -888,13 +887,13 @@ var BaseLiteguardClient = class {
|
|
|
888
887
|
*/
|
|
889
888
|
async flushSignals(options = {}) {
|
|
890
889
|
const signalBatch = this.signalBuffer.splice(0);
|
|
891
|
-
const
|
|
890
|
+
const unadoptedObservations = [...this.pendingUnadoptedGuards.values()].sort((a, b) => a.guardName.localeCompare(b.guardName)).map((observation) => this.toUnadoptedGuardObservation(observation));
|
|
892
891
|
this.pendingUnadoptedGuards.clear();
|
|
893
892
|
if (signalBatch.length > 0) {
|
|
894
893
|
try {
|
|
895
894
|
const url = new URL("/api/v1/signals", this.options.backendUrl);
|
|
896
895
|
const body = JSON.stringify({
|
|
897
|
-
|
|
896
|
+
projectClientToken: this.projectClientToken,
|
|
898
897
|
environment: this.options.environment,
|
|
899
898
|
signals: signalBatch
|
|
900
899
|
});
|
|
@@ -909,19 +908,19 @@ var BaseLiteguardClient = class {
|
|
|
909
908
|
}
|
|
910
909
|
}
|
|
911
910
|
}
|
|
912
|
-
if (
|
|
911
|
+
if (unadoptedObservations.length > 0) {
|
|
913
912
|
try {
|
|
914
913
|
const url = new URL("/api/v1/unadopted-guards", this.options.backendUrl);
|
|
915
914
|
const requestBody = {
|
|
916
|
-
|
|
915
|
+
projectClientToken: this.projectClientToken,
|
|
917
916
|
environment: this.options.environment,
|
|
918
|
-
|
|
917
|
+
observations: unadoptedObservations
|
|
919
918
|
};
|
|
920
919
|
await this.post(url, JSON.stringify(requestBody), options.keepalive);
|
|
921
920
|
} catch (error) {
|
|
922
921
|
this.log("[liteguard] Unadopted guard flush failed", error);
|
|
923
|
-
for (const
|
|
924
|
-
this.
|
|
922
|
+
for (const observation of unadoptedObservations) {
|
|
923
|
+
this.mergePendingUnadoptedObservation(observation);
|
|
925
924
|
}
|
|
926
925
|
}
|
|
927
926
|
}
|
|
@@ -987,10 +986,44 @@ var BaseLiteguardClient = class {
|
|
|
987
986
|
}
|
|
988
987
|
/** Queue a one-time report for a guard that exists only in application code. */
|
|
989
988
|
recordUnadoptedGuard(name) {
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
989
|
+
const now = Date.now();
|
|
990
|
+
const existing = this.pendingUnadoptedGuards.get(name);
|
|
991
|
+
if (existing) {
|
|
992
|
+
existing.lastSeenMs = now;
|
|
993
|
+
existing.checkCount += 1;
|
|
994
|
+
return;
|
|
993
995
|
}
|
|
996
|
+
this.pendingUnadoptedGuards.set(name, {
|
|
997
|
+
guardName: name,
|
|
998
|
+
firstSeenMs: now,
|
|
999
|
+
lastSeenMs: now,
|
|
1000
|
+
checkCount: 1
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
mergePendingUnadoptedObservation(observation) {
|
|
1004
|
+
const existing = this.pendingUnadoptedGuards.get(observation.guardName);
|
|
1005
|
+
if (!existing) {
|
|
1006
|
+
this.pendingUnadoptedGuards.set(observation.guardName, {
|
|
1007
|
+
guardName: observation.guardName,
|
|
1008
|
+
firstSeenMs: observation.firstSeenMs,
|
|
1009
|
+
lastSeenMs: observation.lastSeenMs,
|
|
1010
|
+
checkCount: observation.checkCount
|
|
1011
|
+
});
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
existing.firstSeenMs = Math.min(existing.firstSeenMs, observation.firstSeenMs);
|
|
1015
|
+
existing.lastSeenMs = Math.max(existing.lastSeenMs, observation.lastSeenMs);
|
|
1016
|
+
existing.checkCount += observation.checkCount;
|
|
1017
|
+
}
|
|
1018
|
+
toUnadoptedGuardObservation(observation) {
|
|
1019
|
+
return {
|
|
1020
|
+
...observation,
|
|
1021
|
+
estimatedChecksPerMinute: estimateChecksPerMinute(
|
|
1022
|
+
observation.firstSeenMs,
|
|
1023
|
+
observation.lastSeenMs,
|
|
1024
|
+
observation.checkCount
|
|
1025
|
+
)
|
|
1026
|
+
};
|
|
994
1027
|
}
|
|
995
1028
|
/** Schedule the next periodic guard refresh using the current refresh interval. */
|
|
996
1029
|
scheduleNextRefresh() {
|
|
@@ -1075,14 +1108,14 @@ var BaseLiteguardClient = class {
|
|
|
1075
1108
|
try {
|
|
1076
1109
|
const url = new URL("/api/v1/guards", this.options.backendUrl);
|
|
1077
1110
|
const headers = {
|
|
1078
|
-
Authorization: `Bearer ${this.
|
|
1111
|
+
Authorization: `Bearer ${this.projectClientToken}`,
|
|
1079
1112
|
"Content-Type": "application/json"
|
|
1080
1113
|
};
|
|
1081
1114
|
if (etag) {
|
|
1082
1115
|
headers["If-None-Match"] = etag;
|
|
1083
1116
|
}
|
|
1084
1117
|
const requestBody = {
|
|
1085
|
-
|
|
1118
|
+
projectClientToken: this.projectClientToken,
|
|
1086
1119
|
environment: this.options.environment,
|
|
1087
1120
|
...protectedContext ? { protectedContext } : {}
|
|
1088
1121
|
};
|
|
@@ -1148,7 +1181,7 @@ var BaseLiteguardClient = class {
|
|
|
1148
1181
|
const res = await this.runtime.fetch(url.toString(), {
|
|
1149
1182
|
method: "POST",
|
|
1150
1183
|
headers: {
|
|
1151
|
-
Authorization: `Bearer ${this.
|
|
1184
|
+
Authorization: `Bearer ${this.projectClientToken}`,
|
|
1152
1185
|
"Content-Type": "application/json",
|
|
1153
1186
|
...this.options.environment && { "X-Liteguard-Environment": this.options.environment }
|
|
1154
1187
|
},
|
|
@@ -1346,9 +1379,9 @@ var BaseLiteguardClient = class {
|
|
|
1346
1379
|
...signal.properties ? { properties: { ...signal.properties } } : {}
|
|
1347
1380
|
}));
|
|
1348
1381
|
}
|
|
1349
|
-
/** @internal Return the sorted set of queued unadopted
|
|
1382
|
+
/** @internal Return the sorted set of queued unadopted guard observations. */
|
|
1350
1383
|
_getPendingUnadoptedGuards() {
|
|
1351
|
-
return [...this.pendingUnadoptedGuards].sort();
|
|
1384
|
+
return [...this.pendingUnadoptedGuards.values()].sort((a, b) => a.guardName.localeCompare(b.guardName)).map((observation) => this.toUnadoptedGuardObservation(observation));
|
|
1352
1385
|
}
|
|
1353
1386
|
/** @internal Return the currently cached bundle keys. */
|
|
1354
1387
|
_getBundleKeys() {
|
|
@@ -1363,6 +1396,13 @@ function nextSignalId() {
|
|
|
1363
1396
|
signalCounter += 1;
|
|
1364
1397
|
return `${Date.now().toString(36)}-${signalCounter.toString(36)}`;
|
|
1365
1398
|
}
|
|
1399
|
+
function estimateChecksPerMinute(firstSeenMs, lastSeenMs, checkCount) {
|
|
1400
|
+
if (checkCount <= 0) {
|
|
1401
|
+
return 0;
|
|
1402
|
+
}
|
|
1403
|
+
const windowMs = Math.max(1e3, lastSeenMs - firstSeenMs);
|
|
1404
|
+
return checkCount * 6e4 / windowMs;
|
|
1405
|
+
}
|
|
1366
1406
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1367
1407
|
0 && (module.exports = {
|
|
1368
1408
|
BaseLiteguardClient,
|