Files
rrweb/typings/types.d.ts
Lucky Feng 9d2db86d5a feat: enable rrweb to record and replay log messages in console (#424)
* wip: working on rrweb logger

* wip: can record and replay some simple log

* wip: can record and replay log's stack

* wip: try to serialize object

* wip: record and replay console logger

hijack all of the console functions.
add listener to thrown errors

* wip: record and replay console logger
add limit to the max number of log records

* feat: enable rrweb to record and replay log messages in console

this is the implementation of new feature request(issue #234)

here are a few points of description.
1. users need to set recordLog option in rrweb.record's parameter to record log messages.  The log recorder is off by default.
2. support recording and replaying all kinds of console functions. But the reliability of them should be tested more
3. the stringify function in  stringify.ts needs improvement. e.g. robustness, handler for cyclical structures and better support for more kinds of object
4. we can replay the log messages in a simulated html console like LogRocket by implementing the interface "ReplayLogger" in the future

* improve: the stringify function

1. handle cyclical structures
2. add stringify option to limit the length of result
3. handle function type

* refactor: simplify the type definition of ReplayLogger
2026-04-01 12:00:00 +08:00

421 lines
13 KiB
TypeScript

import { serializedNodeWithId, idNodeMap, INode, MaskInputOptions, SlimDOMOptions } from 'rrweb-snapshot';
import { PackFn, UnpackFn } from './packer/base';
import { FontFaceDescriptors } from 'css-font-loading-module';
export declare enum EventType {
DomContentLoaded = 0,
Load = 1,
FullSnapshot = 2,
IncrementalSnapshot = 3,
Meta = 4,
Custom = 5
}
export declare type domContentLoadedEvent = {
type: EventType.DomContentLoaded;
data: {};
};
export declare type loadedEvent = {
type: EventType.Load;
data: {};
};
export declare type fullSnapshotEvent = {
type: EventType.FullSnapshot;
data: {
node: serializedNodeWithId;
initialOffset: {
top: number;
left: number;
};
};
};
export declare type incrementalSnapshotEvent = {
type: EventType.IncrementalSnapshot;
data: incrementalData;
};
export declare type metaEvent = {
type: EventType.Meta;
data: {
href: string;
width: number;
height: number;
};
};
export declare type logEvent = {
type: EventType.IncrementalSnapshot;
data: incrementalData;
};
export declare type customEvent<T = unknown> = {
type: EventType.Custom;
data: {
tag: string;
payload: T;
};
};
export declare type styleSheetEvent = {};
export declare enum IncrementalSource {
Mutation = 0,
MouseMove = 1,
MouseInteraction = 2,
Scroll = 3,
ViewportResize = 4,
Input = 5,
TouchMove = 6,
MediaInteraction = 7,
StyleSheetRule = 8,
CanvasMutation = 9,
Font = 10,
Log = 11
}
export declare type mutationData = {
source: IncrementalSource.Mutation;
} & mutationCallbackParam;
export declare type mousemoveData = {
source: IncrementalSource.MouseMove | IncrementalSource.TouchMove;
positions: mousePosition[];
};
export declare type mouseInteractionData = {
source: IncrementalSource.MouseInteraction;
} & mouseInteractionParam;
export declare type scrollData = {
source: IncrementalSource.Scroll;
} & scrollPosition;
export declare type viewportResizeData = {
source: IncrementalSource.ViewportResize;
} & viewportResizeDimention;
export declare type inputData = {
source: IncrementalSource.Input;
id: number;
} & inputValue;
export declare type mediaInteractionData = {
source: IncrementalSource.MediaInteraction;
} & mediaInteractionParam;
export declare type styleSheetRuleData = {
source: IncrementalSource.StyleSheetRule;
} & styleSheetRuleParam;
export declare type canvasMutationData = {
source: IncrementalSource.CanvasMutation;
} & canvasMutationParam;
export declare type fontData = {
source: IncrementalSource.Font;
} & fontParam;
export declare type logData = {
source: IncrementalSource.Log;
} & LogParam;
export declare type incrementalData = mutationData | mousemoveData | mouseInteractionData | scrollData | viewportResizeData | inputData | mediaInteractionData | styleSheetRuleData | canvasMutationData | fontData | logData;
export declare type event = domContentLoadedEvent | loadedEvent | fullSnapshotEvent | incrementalSnapshotEvent | metaEvent | logEvent | customEvent;
export declare type eventWithTime = event & {
timestamp: number;
delay?: number;
};
export declare type blockClass = string | RegExp;
export declare type SamplingStrategy = Partial<{
mousemove: boolean | number;
mouseInteraction: boolean | Record<string, boolean | undefined>;
scroll: number;
input: 'all' | 'last';
}>;
export declare type recordOptions<T> = {
emit?: (e: T, isCheckout?: boolean) => void;
checkoutEveryNth?: number;
checkoutEveryNms?: number;
blockClass?: blockClass;
blockSelector?: string;
ignoreClass?: string;
maskAllInputs?: boolean;
maskInputOptions?: MaskInputOptions;
maskInputFn?: MaskInputFn;
slimDOMOptions?: SlimDOMOptions | 'all' | true;
inlineStylesheet?: boolean;
hooks?: hooksParam;
packFn?: PackFn;
sampling?: SamplingStrategy;
recordCanvas?: boolean;
collectFonts?: boolean;
mousemoveWait?: number;
recordLog?: boolean | LogRecordOptions;
};
export declare type observerParam = {
mutationCb: mutationCallBack;
mousemoveCb: mousemoveCallBack;
mouseInteractionCb: mouseInteractionCallBack;
scrollCb: scrollCallback;
viewportResizeCb: viewportResizeCallback;
inputCb: inputCallback;
mediaInteractionCb: mediaInteractionCallback;
blockClass: blockClass;
blockSelector: string | null;
ignoreClass: string;
maskInputOptions: MaskInputOptions;
maskInputFn?: MaskInputFn;
inlineStylesheet: boolean;
styleSheetRuleCb: styleSheetRuleCallback;
canvasMutationCb: canvasMutationCallback;
fontCb: fontCallback;
logCb: logCallback;
logOptions: LogRecordOptions;
sampling: SamplingStrategy;
recordCanvas: boolean;
collectFonts: boolean;
slimDOMOptions: SlimDOMOptions;
};
export declare type hooksParam = {
mutation?: mutationCallBack;
mousemove?: mousemoveCallBack;
mouseInteraction?: mouseInteractionCallBack;
scroll?: scrollCallback;
viewportResize?: viewportResizeCallback;
input?: inputCallback;
mediaInteaction?: mediaInteractionCallback;
styleSheetRule?: styleSheetRuleCallback;
canvasMutation?: canvasMutationCallback;
font?: fontCallback;
log?: logCallback;
};
export declare type mutationRecord = {
type: string;
target: Node;
oldValue: string | null;
addedNodes: NodeList;
removedNodes: NodeList;
attributeName: string | null;
};
export declare type textCursor = {
node: Node;
value: string | null;
};
export declare type textMutation = {
id: number;
value: string | null;
};
export declare type attributeCursor = {
node: Node;
attributes: {
[key: string]: string | null;
};
};
export declare type attributeMutation = {
id: number;
attributes: {
[key: string]: string | null;
};
};
export declare type removedNodeMutation = {
parentId: number;
id: number;
};
export declare type addedNodeMutation = {
parentId: number;
previousId?: number | null;
nextId: number | null;
node: serializedNodeWithId;
};
declare type mutationCallbackParam = {
texts: textMutation[];
attributes: attributeMutation[];
removes: removedNodeMutation[];
adds: addedNodeMutation[];
};
export declare type mutationCallBack = (m: mutationCallbackParam) => void;
export declare type mousemoveCallBack = (p: mousePosition[], source: IncrementalSource.MouseMove | IncrementalSource.TouchMove) => void;
export declare type mousePosition = {
x: number;
y: number;
id: number;
timeOffset: number;
};
export declare enum MouseInteractions {
MouseUp = 0,
MouseDown = 1,
Click = 2,
ContextMenu = 3,
DblClick = 4,
Focus = 5,
Blur = 6,
TouchStart = 7,
TouchMove_Departed = 8,
TouchEnd = 9
}
declare type mouseInteractionParam = {
type: MouseInteractions;
id: number;
x: number;
y: number;
};
export declare type mouseInteractionCallBack = (d: mouseInteractionParam) => void;
export declare type scrollPosition = {
id: number;
x: number;
y: number;
};
export declare type scrollCallback = (p: scrollPosition) => void;
export declare type styleSheetAddRule = {
rule: string;
index?: number;
};
export declare type styleSheetDeleteRule = {
index: number;
};
export declare type styleSheetRuleParam = {
id: number;
removes?: styleSheetDeleteRule[];
adds?: styleSheetAddRule[];
};
export declare type styleSheetRuleCallback = (s: styleSheetRuleParam) => void;
export declare type canvasMutationCallback = (p: canvasMutationParam) => void;
export declare type canvasMutationParam = {
id: number;
property: string;
args: Array<unknown>;
setter?: true;
};
export declare type fontParam = {
family: string;
fontSource: string;
buffer: boolean;
descriptors?: FontFaceDescriptors;
};
export declare type LogLevel = 'assert' | 'clear' | 'count' | 'countReset' | 'debug' | 'dir' | 'dirxml' | 'error' | 'group' | 'groupCollapsed' | 'groupEnd' | 'info' | 'log' | 'table' | 'time' | 'timeEnd' | 'timeLog' | 'trace' | 'warn';
export declare type Logger = {
assert?: (value: any, message?: string, ...optionalParams: any[]) => void;
clear?: () => void;
count?: (label?: string) => void;
countReset?: (label?: string) => void;
debug?: (message?: any, ...optionalParams: any[]) => void;
dir?: (obj: any, options?: NodeJS.InspectOptions) => void;
dirxml?: (...data: any[]) => void;
error?: (message?: any, ...optionalParams: any[]) => void;
group?: (...label: any[]) => void;
groupCollapsed?: (label?: any[]) => void;
groupEnd?: () => void;
info?: (message?: any, ...optionalParams: any[]) => void;
log?: (message?: any, ...optionalParams: any[]) => void;
table?: (tabularData: any, properties?: ReadonlyArray<string>) => void;
time?: (label?: string) => void;
timeEnd?: (label?: string) => void;
timeLog?: (label?: string, ...data: any[]) => void;
trace?: (message?: any, ...optionalParams: any[]) => void;
warn?: (message?: any, ...optionalParams: any[]) => void;
};
export declare type ReplayLogger = Partial<Record<LogLevel, (data: logData) => void>>;
export declare type LogParam = {
level: LogLevel;
trace: Array<string>;
payload: Array<string>;
};
export declare type fontCallback = (p: fontParam) => void;
export declare type logCallback = (p: LogParam) => void;
export declare type viewportResizeDimention = {
width: number;
height: number;
};
export declare type viewportResizeCallback = (d: viewportResizeDimention) => void;
export declare type inputValue = {
text: string;
isChecked: boolean;
};
export declare type inputCallback = (v: inputValue & {
id: number;
}) => void;
export declare const enum MediaInteractions {
Play = 0,
Pause = 1
}
export declare type mediaInteractionParam = {
type: MediaInteractions;
id: number;
};
export declare type mediaInteractionCallback = (p: mediaInteractionParam) => void;
export declare type Mirror = {
map: idNodeMap;
getId: (n: INode) => number;
getNode: (id: number) => INode | null;
removeNodeFromMap: (n: INode) => void;
has: (id: number) => boolean;
};
export declare type throttleOptions = {
leading?: boolean;
trailing?: boolean;
};
export declare type listenerHandler = () => void;
export declare type hookResetter = () => void;
export declare type playerConfig = {
speed: number;
root: Element;
loadTimeout: number;
skipInactive: boolean;
showWarning: boolean;
showDebug: boolean;
blockClass: string;
liveMode: boolean;
insertStyleRules: string[];
triggerFocus: boolean;
UNSAFE_replayCanvas: boolean;
pauseAnimation?: boolean;
mouseTail: boolean | {
duration?: number;
lineCap?: string;
lineWidth?: number;
strokeStyle?: string;
};
unpackFn?: UnpackFn;
logConfig: LogReplayConfig;
};
export declare type LogReplayConfig = {
level?: Array<LogLevel> | undefined;
replayLogger: ReplayLogger | undefined;
};
export declare type playerMetaData = {
startTime: number;
endTime: number;
totalTime: number;
};
export declare type missingNode = {
node: Node;
mutation: addedNodeMutation;
};
export declare type missingNodeMap = {
[id: number]: missingNode;
};
export declare type actionWithDelay = {
doAction: () => void;
delay: number;
};
export declare type Handler = (event?: unknown) => void;
export declare type Emitter = {
on(type: string, handler: Handler): void;
emit(type: string, event?: unknown): void;
off(type: string, handler: Handler): void;
};
export declare type Arguments<T> = T extends (...payload: infer U) => unknown ? U : unknown;
export declare enum ReplayerEvents {
Start = "start",
Pause = "pause",
Resume = "resume",
Resize = "resize",
Finish = "finish",
FullsnapshotRebuilded = "fullsnapshot-rebuilded",
LoadStylesheetStart = "load-stylesheet-start",
LoadStylesheetEnd = "load-stylesheet-end",
SkipStart = "skip-start",
SkipEnd = "skip-end",
MouseInteraction = "mouse-interaction",
EventCast = "event-cast",
CustomEvent = "custom-event",
Flush = "flush",
StateChange = "state-change"
}
export declare type MaskInputFn = (text: string) => string;
export declare type ElementState = {
scroll?: [number, number];
};
export declare type StringifyOptions = {
stringLengthLimit?: number;
numOfKeysLimit: number;
};
export declare type LogRecordOptions = {
level?: Array<LogLevel> | undefined;
lengthThreshold?: number;
stringifyOptions?: StringifyOptions;
logger?: Logger;
};
export {};