@descope/debug-logs 0.1.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.
@@ -0,0 +1,5 @@
1
+ import { TelemetryManager } from './telemetryManager';
2
+ export default TelemetryManager;
3
+ export type { Logger } from './telemetryManager';
4
+ export type { TelemetryConfig, TelemetryContext, NetworkCaptureConfig, } from './types';
5
+ export type { ConsoleLevel } from './plugins/consolePlugin';
@@ -0,0 +1,23 @@
1
+ import type { Plugin, PluginContext } from 'aws-rum-web';
2
+ export type ConsoleLevel = 'log' | 'info' | 'warn' | 'error' | 'debug';
3
+ export interface ConsolePluginConfig {
4
+ levels?: ConsoleLevel[];
5
+ maxMessageLength?: number;
6
+ }
7
+ /**
8
+ * Console Plugin for AWS RUM
9
+ * Records console.log/info/warn/error/debug calls as custom events
10
+ */
11
+ export declare class ConsolePlugin implements Plugin {
12
+ private context;
13
+ private enabled;
14
+ private readonly id;
15
+ private readonly levels;
16
+ private readonly maxMessageLength;
17
+ private originalMethods;
18
+ constructor(config?: ConsolePluginConfig);
19
+ load(context: PluginContext): void;
20
+ enable(): void;
21
+ disable(): void;
22
+ getPluginId(): string;
23
+ }
@@ -0,0 +1,53 @@
1
+ import type { Plugin, PluginContext } from 'aws-rum-web';
2
+ export interface DomMutationPluginConfig {
3
+ rootElement?: string | HTMLElement;
4
+ throttleMs?: number;
5
+ maxHtmlLength?: number;
6
+ /**
7
+ * When false (DEFAULT), the DOM snapshot strips all text nodes — only tag
8
+ * hierarchy + attributes are recorded. Prevents rendered content leaking
9
+ * into RUM (e.g. an error screen echoing an email, one-time recovery codes,
10
+ * OTP flow copy). Structure is enough to debug most UI issues.
11
+ *
12
+ * Set to true only when you need to see rendered text for a specific
13
+ * support session, and confirm no sensitive text lives in the observed
14
+ * root at that time.
15
+ */
16
+ includeText?: boolean;
17
+ }
18
+ /**
19
+ * DOM Mutation Plugin for AWS RUM
20
+ * Records aggregated DOM mutations using MutationObserver
21
+ */
22
+ export declare class DomMutationPlugin implements Plugin {
23
+ private context;
24
+ private enabled;
25
+ private readonly id;
26
+ private observer;
27
+ private pendingMutations;
28
+ private flushTimeout;
29
+ private throttleMs;
30
+ private maxHtmlLength;
31
+ private includeText;
32
+ private config;
33
+ private rootElement;
34
+ constructor(config?: DomMutationPluginConfig);
35
+ load(context: PluginContext): void;
36
+ enable(): void;
37
+ /**
38
+ * Serialize the observed root's HTML for a snapshot: strips text nodes unless
39
+ * includeText is set, then truncates to maxHtmlLength. Shared by the initial
40
+ * baseline snapshot and the structural-mutation path so the truncation math
41
+ * can't drift between the two.
42
+ */
43
+ private snapshotHTML;
44
+ disable(): void;
45
+ getPluginId(): string;
46
+ private scheduleMutationFlush;
47
+ private flushMutations;
48
+ /**
49
+ * Records mutations.
50
+ * @param mutations - Mutations to record. If not provided, uses this.pendingMutations
51
+ */
52
+ private recordPendingMutations;
53
+ }
@@ -0,0 +1,19 @@
1
+ import type { Plugin, PluginContext } from 'aws-rum-web';
2
+ /**
3
+ * Navigation Plugin for AWS RUM
4
+ * Records SPA navigation events (pushState, replaceState, popstate, hashchange)
5
+ */
6
+ export declare class NavigationPlugin implements Plugin {
7
+ private context;
8
+ private enabled;
9
+ private readonly id;
10
+ private originalPushState;
11
+ private originalReplaceState;
12
+ private currentUrl;
13
+ private eventHandlers;
14
+ load(context: PluginContext): void;
15
+ enable(): void;
16
+ disable(): void;
17
+ getPluginId(): string;
18
+ private recordNavigation;
19
+ }
@@ -0,0 +1,29 @@
1
+ import type { Plugin, PluginContext } from 'aws-rum-web';
2
+ import type { NetworkCaptureConfig } from '../../types';
3
+ export declare class NetworkPlugin implements Plugin {
4
+ private context;
5
+ private enabled;
6
+ private readonly id;
7
+ private readonly urlFilters?;
8
+ private readonly maxHeaderLength;
9
+ private readonly truncate;
10
+ private originalFetch?;
11
+ private originalXhrOpen?;
12
+ private originalXhrSend?;
13
+ private originalXhrSetRequestHeader?;
14
+ constructor(config?: NetworkCaptureConfig);
15
+ load(context: PluginContext): void;
16
+ enable(): void;
17
+ disable(): void;
18
+ getPluginId(): string;
19
+ private patchFetch;
20
+ private restoreFetch;
21
+ private patchXhr;
22
+ private restoreXhr;
23
+ private handleFetch;
24
+ private recordXhrSuccess;
25
+ private recordXhrError;
26
+ private recordNetworkSuccess;
27
+ private recordNetworkError;
28
+ private getXhrMeta;
29
+ }
@@ -0,0 +1,54 @@
1
+ import type { HeadersMap } from './types';
2
+ export declare const DEFAULT_MAX_HEADER_LENGTH = 2048;
3
+ /**
4
+ * Normalizes filter input to an array of RegExp or undefined
5
+ */
6
+ export declare function normalizeFilters(filter?: RegExp | RegExp[]): RegExp[] | undefined;
7
+ /**
8
+ * Type guard to check if input is a Request object
9
+ */
10
+ export declare function isRequest(input: RequestInfo | URL): input is Request;
11
+ /**
12
+ * Converts various header formats to a normalized HeadersMap
13
+ */
14
+ export declare function toHeadersMap(headers?: Headers | HeadersInit, truncate?: (value: string) => string): HeadersMap;
15
+ /**
16
+ * Parses raw HTTP header string into a HeadersMap
17
+ */
18
+ export declare function parseResponseHeaders(rawHeaders: string, truncate: (value: string) => string): HeadersMap;
19
+ /**
20
+ * Truncates a header value to a maximum length
21
+ */
22
+ export declare function truncateHeader(value: string, maxLength: number): string;
23
+ /**
24
+ * Normalizes a URL string to an absolute URL
25
+ */
26
+ export declare function normalizeUrl(url: string): string;
27
+ /**
28
+ * Tests if a URL matches any of the provided filters
29
+ */
30
+ export declare function shouldCaptureUrl(url: string, urlFilters?: RegExp[]): boolean;
31
+ /**
32
+ * Extracts HTTP method from fetch input
33
+ */
34
+ export declare function extractMethod(input: RequestInfo | URL, init?: RequestInit): string;
35
+ /**
36
+ * Extracts URL string from fetch input
37
+ */
38
+ export declare function extractUrl(input: RequestInfo | URL): string;
39
+ /**
40
+ * Extracts and normalizes request headers from fetch input
41
+ */
42
+ export declare function extractRequestHeaders(input: RequestInfo | URL, init: RequestInit | undefined, truncate: (value: string) => string): HeadersMap;
43
+ /**
44
+ * Extracts and normalizes response headers
45
+ */
46
+ export declare function extractResponseHeaders(response: Response, truncate: (value: string) => string): HeadersMap;
47
+ /**
48
+ * Safely logs debug messages with error handling
49
+ */
50
+ export declare function safeDebugLog(message: string, error: unknown): void;
51
+ /**
52
+ * Calculates duration from start time
53
+ */
54
+ export declare function calculateDuration(startTime: number): number;
@@ -0,0 +1,4 @@
1
+ export { NetworkPlugin } from './NetworkPlugin';
2
+ export type { HeadersMap, XhrMetadata, XhrWithMeta } from './types';
3
+ export { XHR_METADATA } from './types';
4
+ export { extractMethod, extractUrl, extractRequestHeaders, extractResponseHeaders, normalizeUrl, shouldCaptureUrl, } from './helpers';
@@ -0,0 +1,11 @@
1
+ export type HeadersMap = Record<string, string>;
2
+ export declare const XHR_METADATA: unique symbol;
3
+ export interface XhrMetadata {
4
+ method?: string;
5
+ url?: string;
6
+ startTime?: number;
7
+ requestHeaders: HeadersMap;
8
+ }
9
+ export type XhrWithMeta = XMLHttpRequest & {
10
+ [XHR_METADATA]?: XhrMetadata;
11
+ };
@@ -0,0 +1,59 @@
1
+ import { AwsRum } from 'aws-rum-web';
2
+ import type { TelemetryConfig, TelemetryContext } from './types';
3
+ export interface Logger {
4
+ debug: (...args: any[]) => void;
5
+ info: (...args: any[]) => void;
6
+ error: (...args: any[]) => void;
7
+ }
8
+ /**
9
+ * TelemetryManager - Class-based telemetry management for AWS CloudWatch RUM
10
+ *
11
+ * Manages the lifecycle of AWS RUM client and custom plugins for telemetry collection.
12
+ *
13
+ * Note: Initialization happens in the constructor. Once shutdown() is called,
14
+ * the instance cannot be reused - create a new instance instead.
15
+ */
16
+ export declare class TelemetryManager {
17
+ private rumClient;
18
+ private plugins;
19
+ private config;
20
+ private context;
21
+ private logger;
22
+ private isShutdown;
23
+ private initializationFailed;
24
+ constructor(config: TelemetryConfig, context: TelemetryContext, logger?: Logger);
25
+ /**
26
+ * Initialize the RUM client and plugins
27
+ */
28
+ private initializeRumClient;
29
+ /**
30
+ * Build HTTP telemetry configuration
31
+ */
32
+ private buildHttpTelemetry;
33
+ /**
34
+ * Build all custom event plugins
35
+ */
36
+ private buildCustomPlugins;
37
+ /**
38
+ * Create AWS RUM client instance
39
+ */
40
+ private createRumClient;
41
+ /**
42
+ * Shutdown the telemetry manager and cleanup resources.
43
+ * After calling shutdown, this instance cannot be reused.
44
+ */
45
+ shutdown(): void;
46
+ enable(): void;
47
+ disable(): void;
48
+ updateContext(context: Partial<Record<string, string | number | boolean>>): void;
49
+ isReady(): boolean;
50
+ getRumClient(): AwsRum | null;
51
+ private assertMutable;
52
+ private safe;
53
+ /**
54
+ * aws-rum-web does not expose a public getSessionId(); the session lives
55
+ * inside the private eventCache. Reach in for debug logging only — keep
56
+ * this strictly best-effort.
57
+ */
58
+ private tryGetSessionId;
59
+ }
@@ -0,0 +1,32 @@
1
+ import type { ConsoleLevel } from './plugins/consolePlugin';
2
+ export interface NetworkCaptureConfig {
3
+ urlFilter?: RegExp | RegExp[];
4
+ maxHeaderLength?: number;
5
+ }
6
+ export interface TelemetryConfig {
7
+ enabled: boolean;
8
+ rumConfig: {
9
+ sessionSampleRate: number;
10
+ applicationId: string;
11
+ identityPoolId: string;
12
+ guestRoleArn?: string;
13
+ region: string;
14
+ endpoint?: string;
15
+ };
16
+ capture?: {
17
+ console?: boolean | {
18
+ levels?: ConsoleLevel[];
19
+ };
20
+ network?: boolean | NetworkCaptureConfig;
21
+ navigation?: boolean;
22
+ dom?: boolean | {
23
+ rootElement?: string | HTMLElement;
24
+ throttleMs?: number;
25
+ };
26
+ };
27
+ }
28
+ export interface TelemetryContext {
29
+ projectId: string;
30
+ flowId: string;
31
+ version?: string;
32
+ }
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@descope/debug-logs",
3
+ "version": "0.1.0",
4
+ "author": "Descope Team <info@descope.com>",
5
+ "description": "CloudWatch RUM-based telemetry collector for Descope SDK",
6
+ "main": "dist/cjs/index.cjs.js",
7
+ "module": "dist/index.esm.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ "require": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/cjs/index.cjs.js"
13
+ },
14
+ "import": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.esm.js"
17
+ }
18
+ },
19
+ "type": "module",
20
+ "license": "MIT",
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "keywords": [
25
+ "descope",
26
+ "telemetry",
27
+ "cloudwatch",
28
+ "rum"
29
+ ],
30
+ "dependencies": {
31
+ "aws-rum-web": "^1.25.0"
32
+ },
33
+ "devDependencies": {
34
+ "@rollup/plugin-commonjs": "28.0.2",
35
+ "@rollup/plugin-node-resolve": "^15.0.0",
36
+ "@rollup/plugin-replace": "^5.0.2",
37
+ "@rollup/plugin-terser": "^0.4.0",
38
+ "@rollup/plugin-typescript": "^11.0.0",
39
+ "@types/jest": "^29.0.0",
40
+ "@types/node": "20.17.13",
41
+ "@typescript-eslint/parser": "^7.0.0",
42
+ "dotenv": "^16.4.5",
43
+ "eslint": "8.57.1",
44
+ "eslint-config-prettier": "9.1.0",
45
+ "eslint-import-resolver-typescript": "3.6.1",
46
+ "eslint-plugin-import": "2.31.0",
47
+ "eslint-plugin-jest-dom": "5.4.0",
48
+ "eslint-plugin-no-only-tests": "3.3.0",
49
+ "jest": "^29.0.0",
50
+ "rimraf": "^6.0.0",
51
+ "rollup": "^4.0.0",
52
+ "rollup-plugin-auto-external": "^2.0.0",
53
+ "rollup-plugin-serve": "^2.0.3",
54
+ "typescript": "^5.0.0"
55
+ },
56
+ "scripts": {
57
+ "build": "rimraf dist && rollup -c",
58
+ "dev": "rollup -c rollup.config.dev.mjs --watch",
59
+ "test": "jest",
60
+ "lint": "eslint '+(src|test)/**/*.ts'"
61
+ }
62
+ }