Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl sequential id plugins #819

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/rrweb/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ const baseConfigs = [
name: 'rrwebConsoleReplay',
pathFn: toPluginPath('console', 'replay'),
},
{
input: './src/plugins/sequential-id/record/index.ts',
name: 'rrwebSequentialIdRecord',
pathFn: toPluginPath('sequential-id', 'record'),
},
{
input: './src/plugins/sequential-id/replay/index.ts',
name: 'rrwebSequentialIdReplay',
pathFn: toPluginPath('sequential-id', 'replay'),
},
];

let configs = [];
Expand Down
31 changes: 31 additions & 0 deletions packages/rrweb/src/plugins/sequential-id/record/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { RecordPlugin } from '../../../types';

export type SequentialIdOptions = {
key: string;
};

const defaultOptions: SequentialIdOptions = {
key: '_sid',
};

export const PLUGIN_NAME = 'rrweb/sequential-id@1';

export const getRecordSequentialIdPlugin: (
options?: Partial<SequentialIdOptions>,
) => RecordPlugin = (options) => {
const _options = options
? Object.assign({}, defaultOptions, options)
: defaultOptions;
let id = 0;

return {
name: PLUGIN_NAME,
eventProcessor(event) {
Object.assign(event, {
[_options.key]: ++id,
});
return event;
},
options: _options,
};
};
39 changes: 39 additions & 0 deletions packages/rrweb/src/plugins/sequential-id/replay/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { SequentialIdOptions } from '../record';
import { ReplayPlugin, eventWithTime } from '../../../types';

type Options = SequentialIdOptions & {
warnOnMissingId: boolean;
};

const defaultOptions: Options = {
key: '_sid',
warnOnMissingId: true,
};

export const getReplaySequentialIdPlugin: (
options?: Partial<Options>,
) => ReplayPlugin = (options) => {
const { key, warnOnMissingId } = options
? Object.assign({}, defaultOptions, options)
: defaultOptions;
let currentId = 1;

return {
handler(event: eventWithTime) {
if (key in event) {
const id = ((event as unknown) as Record<string, number>)[key];
if (id !== currentId) {
console.error(
`[sequential-id-plugin]: expect to get an id with value "${currentId}", but got "${id}"`,
);
} else {
currentId++;
}
} else if (warnOnMissingId) {
console.warn(
`[sequential-id-plugin]: failed to get id in key: "${key}"`,
);
}
},
};
};
43 changes: 28 additions & 15 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ function record<T = eventWithTime>(

let lastFullSnapshotEvent: eventWithTime;
let incrementalSnapshotCount = 0;
const eventProcessor = (e: eventWithTime): T => {
for (const plugin of plugins || []) {
if (plugin.eventProcessor) {
e = plugin.eventProcessor(e);
}
}
if (packFn) {
e = (packFn(e) as unknown) as eventWithTime;
}
return (e as unknown) as T;
};
wrappedEmit = (e: eventWithTime, isCheckout?: boolean) => {
if (
mutationBuffers[0]?.isFrozen() &&
Expand All @@ -134,7 +145,7 @@ function record<T = eventWithTime>(
mutationBuffers.forEach((buf) => buf.unfreeze());
}

emit(((packFn ? packFn(e) : e) as unknown) as T, isCheckout);
emit(eventProcessor(e), isCheckout);
if (e.type === EventType.FullSnapshot) {
lastFullSnapshotEvent = e;
incrementalSnapshotCount = 0;
Expand Down Expand Up @@ -405,20 +416,22 @@ function record<T = eventWithTime>(
iframeManager,
shadowDomManager,
plugins:
plugins?.map((p) => ({
observer: p.observer,
options: p.options,
callback: (payload: object) =>
wrappedEmit(
wrapEvent({
type: EventType.Plugin,
data: {
plugin: p.name,
payload,
},
}),
),
})) || [],
plugins
?.filter((p) => p.observer)
?.map((p) => ({
observer: p.observer!,
options: p.options,
callback: (payload: object) =>
wrappedEmit(
wrapEvent({
type: EventType.Plugin,
data: {
plugin: p.name,
payload,
},
}),
),
})) || [],
},
hooks,
);
Expand Down
3 changes: 2 additions & 1 deletion packages/rrweb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export type SamplingStrategy = Partial<{

export type RecordPlugin<TOptions = unknown> = {
name: string;
observer: (cb: Function, win: IWindow, options: TOptions) => listenerHandler;
observer?: (cb: Function, win: IWindow, options: TOptions) => listenerHandler;
eventProcessor?: <TExtend>(event: eventWithTime) => eventWithTime & TExtend;
options: TOptions;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RecordPlugin } from '../../../types';
export declare type SequentialIdOptions = {
key: string;
};
export declare const PLUGIN_NAME = "rrweb/sequential-id@1";
export declare const getRecordSequentialIdPlugin: (options?: Partial<SequentialIdOptions>) => RecordPlugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { SequentialIdOptions } from '../record';
import { ReplayPlugin } from '../../../types';
declare type Options = SequentialIdOptions & {
warnOnMissingId: boolean;
};
export declare const getReplaySequentialIdPlugin: (options?: Partial<Options>) => ReplayPlugin;
export {};
4 changes: 3 additions & 1 deletion packages/rrweb/typings/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="css-font-loading-module" />
import { serializedNodeWithId, idNodeMap, INode, MaskInputOptions, SlimDOMOptions, MaskInputFn, MaskTextFn } from 'rrweb-snapshot';
import { PackFn, UnpackFn } from './packer/base';
import { IframeManager } from './record/iframe-manager';
Expand Down Expand Up @@ -126,7 +127,8 @@ export declare type SamplingStrategy = Partial<{
}>;
export declare type RecordPlugin<TOptions = unknown> = {
name: string;
observer: (cb: Function, win: IWindow, options: TOptions) => listenerHandler;
observer?: (cb: Function, win: IWindow, options: TOptions) => listenerHandler;
eventProcessor?: <TExtend>(event: eventWithTime) => eventWithTime & TExtend;
options: TOptions;
};
export declare type recordOptions<T> = {
Expand Down