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

chore(rrweb): fix the dist files to properly map to typescript checks #45

Merged
merged 3 commits into from
Oct 28, 2024
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
8 changes: 8 additions & 0 deletions .changeset/large-files-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@amplitude/rrweb-all": patch
"@amplitude/rrdom-nodejs": patch
"@amplitude/rrdom": patch
"@amplitude/rrweb": patch
---

chore(rrweb): fix the dist files to properly map to typescript checks
2 changes: 1 addition & 1 deletion packages/all/test/cross-origin-iframe-packer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as fs from 'fs';
import * as path from 'path';
import type * as puppeteer from 'puppeteer';
import type { recordOptions } from '@amplitude/rrweb';
import type {} from'@amplitude/rrweb-types';
import type {} from '@amplitude/rrweb-types';
import { EventType } from '@amplitude/rrweb-types';
import {
assertSnapshot,
Expand Down
2 changes: 1 addition & 1 deletion packages/rrdom-nodejs/src/document-nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,4 @@ interface RRElementTagNameMap {
}

type RRElementType<K extends keyof HTMLElementTagNameMap> =
K extends keyof RRElementTagNameMap ? RRElementTagNameMap[K] : RRElement;
K extends keyof RRElementTagNameMap ? RRElementTagNameMap[K] : RRElement;
2 changes: 1 addition & 1 deletion packages/rrdom-nodejs/test/document-nodejs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,4 @@ describe('RRDocument for nodejs environment', () => {
function getHtml(fileName: string) {
const filePath = path.resolve(__dirname, `../../rrdom/test/html/${fileName}`);
return fs.readFileSync(filePath, 'utf8');
}
}
28 changes: 14 additions & 14 deletions packages/rrdom/src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Mirror as NodeMirror,
NodeType as RRNodeType,
NodeType as NodeType_2,
type elementNode,
} from '@amplitude/rrweb-snapshot';
import type {
Expand Down Expand Up @@ -148,7 +148,7 @@ function diffBeforeUpdatingChildren(
oldTree = calibratedOldTree;
}
switch (newTree.RRNodeType) {
case RRNodeType.Document: {
case NodeType_2.Document: {
/**
* Special cases for updating the document node:
* Case 1: If the oldTree is the content document of an iframe element and its content (HTML, HEAD, and BODY) is automatically mounted by browsers, we need to remove them to avoid unexpected behaviors. e.g. Selector matches may be case insensitive.
Expand All @@ -166,7 +166,7 @@ function diffBeforeUpdatingChildren(
}
break;
}
case RRNodeType.Element: {
case NodeType_2.Element: {
const oldElement = oldTree as HTMLElement;
const newRRElement = newTree as IRRElement;
switch (newRRElement.tagName) {
Expand Down Expand Up @@ -219,12 +219,12 @@ function diffAfterUpdatingChildren(
replayer: ReplayerHandler,
) {
switch (newTree.RRNodeType) {
case RRNodeType.Document: {
case NodeType_2.Document: {
const scrollData = (newTree as RRDocument).scrollData;
scrollData && replayer.applyScroll(scrollData, true);
break;
}
case RRNodeType.Element: {
case NodeType_2.Element: {
const oldElement = oldTree as HTMLElement;
const newRRElement = newTree as RRElement;
newRRElement.scrollData &&
Expand Down Expand Up @@ -312,9 +312,9 @@ function diffAfterUpdatingChildren(
}
break;
}
case RRNodeType.Text:
case RRNodeType.Comment:
case RRNodeType.CDATA: {
case NodeType_2.Text:
case NodeType_2.Comment:
case NodeType_2.CDATA: {
if (
oldTree.textContent !==
(newTree as IRRText | IRRComment | IRRCDATASection).data
Expand Down Expand Up @@ -536,31 +536,31 @@ export function createOrGetNode(
if (nodeId > -1) node = domMirror.getNode(nodeId);
if (node !== null && sameNodeType(node, rrNode)) return node;
switch (rrNode.RRNodeType) {
case RRNodeType.Document:
case NodeType_2.Document:
node = new Document();
break;
case RRNodeType.DocumentType:
case NodeType_2.DocumentType:
node = document.implementation.createDocumentType(
(rrNode as IRRDocumentType).name,
(rrNode as IRRDocumentType).publicId,
(rrNode as IRRDocumentType).systemId,
);
break;
case RRNodeType.Element: {
case NodeType_2.Element: {
let tagName = (rrNode as IRRElement).tagName.toLowerCase();
tagName = SVGTagMap[tagName] || tagName;
if (sn && 'isSVG' in sn && sn?.isSVG) {
node = document.createElementNS(NAMESPACES['svg'], tagName);
} else node = document.createElement((rrNode as IRRElement).tagName);
break;
}
case RRNodeType.Text:
case NodeType_2.Text:
node = document.createTextNode((rrNode as IRRText).data);
break;
case RRNodeType.Comment:
case NodeType_2.Comment:
node = document.createComment((rrNode as IRRComment).data);
break;
case RRNodeType.CDATA:
case NodeType_2.CDATA:
node = document.createCDATASection((rrNode as IRRCDATASection).data);
break;
}
Expand Down
36 changes: 18 additions & 18 deletions packages/rrdom/src/document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeType as RRNodeType } from '@amplitude/rrweb-snapshot';
import { NodeType as NodeType_2 } from '@amplitude/rrweb-snapshot';
import { camelize, parseCSSText, toCSSText } from './style';
export interface IRRNode {
parentElement: IRRNode | null;
Expand All @@ -10,7 +10,7 @@ export interface IRRNode {
// corresponding nodeType value of standard HTML Node
readonly nodeType: number;
readonly nodeName: string; // https://dom.spec.whatwg.org/#dom-node-nodename
readonly RRNodeType: RRNodeType;
readonly RRNodeType: NodeType_2;

firstChild: IRRNode | null;

Expand Down Expand Up @@ -140,7 +140,7 @@ export abstract class BaseRRNode implements IRRNode {
// corresponding nodeType value of standard HTML Node
public readonly nodeType!: number;
public readonly nodeName!: string;
public readonly RRNodeType!: RRNodeType;
public readonly RRNodeType!: NodeType_2;

// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
constructor(..._args: any[]) {
Expand Down Expand Up @@ -199,7 +199,7 @@ export class BaseRRDocument extends BaseRRNode implements IRRDocument {
public readonly nodeType: number = NodeType.DOCUMENT_NODE;
public readonly nodeName = '#document' as const;
public readonly compatMode: 'BackCompat' | 'CSS1Compat' = 'CSS1Compat';
public readonly RRNodeType = RRNodeType.Document;
public readonly RRNodeType = NodeType_2.Document;
public textContent: string | null = null;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -212,7 +212,7 @@ export class BaseRRDocument extends BaseRRNode implements IRRDocument {
return (
(this.childNodes.find(
(node) =>
node.RRNodeType === RRNodeType.Element &&
node.RRNodeType === NodeType_2.Element &&
(node as IRRElement).tagName === 'HTML',
) as IRRElement) || null
);
Expand All @@ -222,7 +222,7 @@ export class BaseRRDocument extends BaseRRNode implements IRRDocument {
return (
(this.documentElement?.childNodes.find(
(node) =>
node.RRNodeType === RRNodeType.Element &&
node.RRNodeType === NodeType_2.Element &&
(node as IRRElement).tagName === 'BODY',
) as IRRElement) || null
);
Expand All @@ -232,7 +232,7 @@ export class BaseRRDocument extends BaseRRNode implements IRRDocument {
return (
(this.documentElement?.childNodes.find(
(node) =>
node.RRNodeType === RRNodeType.Element &&
node.RRNodeType === NodeType_2.Element &&
(node as IRRElement).tagName === 'HEAD',
) as IRRElement) || null
);
Expand All @@ -249,13 +249,13 @@ export class BaseRRDocument extends BaseRRNode implements IRRDocument {
public appendChild(newChild: IRRNode): IRRNode {
const nodeType = newChild.RRNodeType;
if (
nodeType === RRNodeType.Element ||
nodeType === RRNodeType.DocumentType
nodeType === NodeType_2.Element ||
nodeType === NodeType_2.DocumentType
) {
if (this.childNodes.some((s) => s.RRNodeType === nodeType)) {
throw new Error(
`RRDomException: Failed to execute 'appendChild' on 'RRNode': Only one ${
nodeType === RRNodeType.Element ? 'RRElement' : 'RRDoctype'
nodeType === NodeType_2.Element ? 'RRElement' : 'RRDoctype'
} on RRDocument allowed.`,
);
}
Expand All @@ -269,13 +269,13 @@ export class BaseRRDocument extends BaseRRNode implements IRRDocument {
public insertBefore(newChild: IRRNode, refChild: IRRNode | null): IRRNode {
const nodeType = newChild.RRNodeType;
if (
nodeType === RRNodeType.Element ||
nodeType === RRNodeType.DocumentType
nodeType === NodeType_2.Element ||
nodeType === NodeType_2.DocumentType
) {
if (this.childNodes.some((s) => s.RRNodeType === nodeType)) {
throw new Error(
`RRDomException: Failed to execute 'insertBefore' on 'RRNode': Only one ${
nodeType === RRNodeType.Element ? 'RRElement' : 'RRDoctype'
nodeType === NodeType_2.Element ? 'RRElement' : 'RRDoctype'
} on RRDocument allowed.`,
);
}
Expand Down Expand Up @@ -380,7 +380,7 @@ export class BaseRRDocument extends BaseRRNode implements IRRDocument {

export class BaseRRDocumentType extends BaseRRNode implements IRRDocumentType {
public readonly nodeType: number = NodeType.DOCUMENT_TYPE_NODE;
public readonly RRNodeType = RRNodeType.DocumentType;
public readonly RRNodeType = NodeType_2.DocumentType;
declare readonly nodeName: string;
public readonly name: string;
public readonly publicId: string;
Expand All @@ -402,7 +402,7 @@ export class BaseRRDocumentType extends BaseRRNode implements IRRDocumentType {

export class BaseRRElement extends BaseRRNode implements IRRElement {
public readonly nodeType: number = NodeType.ELEMENT_NODE;
public readonly RRNodeType = RRNodeType.Element;
public readonly RRNodeType = NodeType_2.Element;
declare readonly nodeName: string;
public tagName: string;
public attributes: Record<string, string> = {};
Expand Down Expand Up @@ -575,7 +575,7 @@ export class BaseRRDialogElement extends BaseRRElement {
export class BaseRRText extends BaseRRNode implements IRRText {
public readonly nodeType: number = NodeType.TEXT_NODE;
public readonly nodeName = '#text' as const;
public readonly RRNodeType = RRNodeType.Text;
public readonly RRNodeType = NodeType_2.Text;
public data: string;

constructor(data: string) {
Expand All @@ -599,7 +599,7 @@ export class BaseRRText extends BaseRRNode implements IRRText {
export class BaseRRComment extends BaseRRNode implements IRRComment {
public readonly nodeType: number = NodeType.COMMENT_NODE;
public readonly nodeName = '#comment' as const;
public readonly RRNodeType = RRNodeType.Comment;
public readonly RRNodeType = NodeType_2.Comment;
public data: string;

constructor(data: string) {
Expand All @@ -623,7 +623,7 @@ export class BaseRRComment extends BaseRRNode implements IRRComment {
export class BaseRRCDATASection extends BaseRRNode implements IRRCDATASection {
public readonly nodeName = '#cdata-section' as const;
public readonly nodeType: number = NodeType.CDATA_SECTION_NODE;
public readonly RRNodeType = RRNodeType.CDATA;
public readonly RRNodeType = NodeType_2.CDATA;
public data: string;

constructor(data: string) {
Expand Down
16 changes: 8 additions & 8 deletions packages/rrdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
serializedNodeWithId,
} from '@amplitude/rrweb-snapshot';
import {
NodeType as RRNodeType,
NodeType as NodeType_2,
createMirror as createNodeMirror,
} from '@amplitude/rrweb-snapshot';
import type {
Expand Down Expand Up @@ -415,13 +415,13 @@ export class Mirror implements IMirror<RRNode> {
*/
export function getDefaultSN(node: IRRNode, id: number): serializedNodeWithId {
switch (node.RRNodeType) {
case RRNodeType.Document:
case NodeType_2.Document:
return {
id,
type: node.RRNodeType,
childNodes: [],
};
case RRNodeType.DocumentType: {
case NodeType_2.DocumentType: {
const doctype = node as IRRDocumentType;
return {
id,
Expand All @@ -431,27 +431,27 @@ export function getDefaultSN(node: IRRNode, id: number): serializedNodeWithId {
systemId: doctype.systemId,
};
}
case RRNodeType.Element:
case NodeType_2.Element:
return {
id,
type: node.RRNodeType,
tagName: (node as IRRElement).tagName.toLowerCase(), // In rrweb data, all tagNames are lowercase.
attributes: {},
childNodes: [],
};
case RRNodeType.Text:
case NodeType_2.Text:
return {
id,
type: node.RRNodeType,
textContent: (node as IRRText).textContent || '',
};
case RRNodeType.Comment:
case NodeType_2.Comment:
return {
id,
type: node.RRNodeType,
textContent: (node as IRRComment).textContent || '',
};
case RRNodeType.CDATA:
case NodeType_2.CDATA:
return {
id,
type: node.RRNodeType,
Expand All @@ -471,7 +471,7 @@ export function printRRDom(rootNode: IRRNode, mirror: IMirror<IRRNode>) {
}
function walk(node: IRRNode, mirror: IMirror<IRRNode>, blankSpace: string) {
let printText = `${blankSpace}${mirror.getId(node)} ${node.toString()}\n`;
if (node.RRNodeType === RRNodeType.Element) {
if (node.RRNodeType === NodeType_2.Element) {
const element = node as IRRElement;
if (element.shadowRoot)
printText += walk(element.shadowRoot, mirror, blankSpace + ' ');
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}

const mirror = createMirror();
function record<T = eventWithTime>(
export function record<T = eventWithTime>(
options: recordOptions<T> = {},
): listenerHandler | undefined {
const {
Expand Down Expand Up @@ -557,7 +557,7 @@
plugins
?.filter((p) => p.observer)
?.map((p) => ({
observer: p.observer!,

Check warning on line 560 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 560 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 560 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/index.ts#L560

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
options: p.options,
callback: (payload: object) =>
wrappedEmit({
Expand All @@ -575,7 +575,7 @@

iframeManager.addLoadListener((iframeEl) => {
try {
handlers.push(observe(iframeEl.contentDocument!));

Check warning on line 578 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/index.ts#L578

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
} catch (error) {
// TODO: handle internal error
console.warn(error);
Expand Down
5 changes: 4 additions & 1 deletion packages/rrweb/src/replay/canvas/webgl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CanvasContext, type canvasMutationCommand } from '@amplitude/rrweb-types';
import {
CanvasContext,
type canvasMutationCommand,
} from '@amplitude/rrweb-types';
import type { Replayer } from '../';
import { deserializeArg, variableListFor } from './deserialize-args';

Expand All @@ -12,10 +15,10 @@
try {
if (type === CanvasContext.WebGL) {
return (
target.getContext('webgl')! || target.getContext('experimental-webgl')

Check warning on line 18 in packages/rrweb/src/replay/canvas/webgl.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/replay/canvas/webgl.ts#L18

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
);
}
return target.getContext('webgl2')!;

Check warning on line 21 in packages/rrweb/src/replay/canvas/webgl.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/replay/canvas/webgl.ts#L21

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
} catch (e) {
return null;
}
Expand All @@ -36,7 +39,7 @@

function saveToWebGLVarMap(
ctx: WebGLRenderingContext | WebGL2RenderingContext,
result: any,

Check warning on line 42 in packages/rrweb/src/replay/canvas/webgl.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/replay/canvas/webgl.ts#L42

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!result?.constructor) return; // probably null or undefined
Expand Down Expand Up @@ -75,7 +78,7 @@
if (mutation.setter) {
// skip some read-only type checks
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(ctx as any)[mutation.property] = mutation.args[0];

Check warning on line 81 in packages/rrweb/src/replay/canvas/webgl.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/replay/canvas/webgl.ts#L81

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
return;
}
const original = ctx[
Expand Down
6 changes: 5 additions & 1 deletion packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { RRNode, RRIFrameElement, BaseRRNode } from '@amplitude/rrdom';
import type { IMirror, Mirror, SlimDOMOptions } from '@amplitude/rrweb-snapshot';
import type {
IMirror,
Mirror,
SlimDOMOptions,
} from '@amplitude/rrweb-snapshot';
import {
IGNORED_NODE,
classMatchesRegex,
Expand Down Expand Up @@ -35,7 +39,7 @@
'now you can use replayer.getMirror() to access the mirror instance of a replayer,' +
'\r\n' +
'or you can use record.mirror to access the mirror instance during recording.';
/** @deprecated */

Check warning on line 42 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L42

[tsdoc/syntax] tsdoc-missing-deprecation-message: The @deprecated block must include a deprecation message, e.g. describing the recommended alternative
export let _mirror: DeprecatedMirror = {
map: {},
getId() {
Expand Down Expand Up @@ -119,7 +123,7 @@
set(value) {
// put hooked setter into event loop to avoid of set latency
setTimeout(() => {
d.set!.call(this, value);

Check warning on line 126 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L126

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}, 0);
if (original && original.set) {
original.set.call(this, value);
Expand All @@ -132,7 +136,7 @@

// copy from https://github.com/getsentry/sentry-javascript/blob/b2109071975af8bf0316d3b5b38f519bdaf5dc15/packages/utils/src/object.ts
export function patch(
source: { [key: string]: any },

Check warning on line 139 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L139

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
name: string,
replacement: (...args: unknown[]) => unknown,
): () => void {
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/test/replay/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ describe('replayer', function () {
});
});
});
});
});
2 changes: 1 addition & 1 deletion packages/rrweb/test/replay/video.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,4 @@ describe('video', () => {
`);
expect(time).toBe(8);
});
});
});
5 changes: 4 additions & 1 deletion packages/rrweb/test/rrdom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
import { EventType, IncrementalSource, Replayer, eventWithTime } from '../src';
import { vi, type MockInstance } from 'vitest';
import type { styleDeclarationData, styleSheetRuleData } from '@amplitude/rrweb-types';
import type {
styleDeclarationData,
styleSheetRuleData,
} from '@amplitude/rrweb-types';
import { createMirror, Mirror as NodeMirror } from '@amplitude/rrweb-snapshot';
import type { ReplayerHandler } from '@amplitude/rrdom';

Expand Down
Loading