-
Notifications
You must be signed in to change notification settings - Fork 170
♻️ [PANA-5982] Make the serialization code more configurable and testable #4185
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
Merged
sethfowler-datadog
merged 2 commits into
main
from
seth.fowler/PANA-5982-make-the-serialization-code-more-configurable-and-testable
Feb 19, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,19 @@ | ||
| import type { TimeStamp } from '@datadog/browser-core' | ||
| import { timeStampNow } from '@datadog/browser-core' | ||
| import type { BrowserIncrementalData, BrowserIncrementalSnapshotRecord } from '../../types' | ||
| import { RecordType } from '../../types' | ||
|
|
||
| export function assembleIncrementalSnapshot<Data extends BrowserIncrementalData>( | ||
| source: Data['source'], | ||
| data: Omit<Data, 'source'> | ||
| data: Omit<Data, 'source'>, | ||
| timestamp: TimeStamp = timeStampNow() | ||
| ): BrowserIncrementalSnapshotRecord { | ||
| return { | ||
| data: { | ||
| source, | ||
| ...data, | ||
| } as Data, | ||
| type: RecordType.IncrementalSnapshot, | ||
| timestamp: timeStampNow(), | ||
| timestamp, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
packages/rum/src/domain/record/serialization/serializeDocument.ts
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
packages/rum/src/domain/record/serialization/serializeFullSnapshot.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { getScrollX, getScrollY } from '@datadog/browser-rum-core' | ||
| import type { TimeStamp } from '@datadog/browser-core' | ||
| import type { BrowserFullSnapshotRecord } from '../../../types' | ||
| import { RecordType } from '../../../types' | ||
| import type { EmitRecordCallback, EmitStatsCallback } from '../record.types' | ||
| import type { RecordingScope } from '../recordingScope' | ||
| import { serializeNode } from './serializeNode' | ||
| import { serializeInTransaction } from './serializationTransaction' | ||
| import type { SerializationKind, SerializationTransaction } from './serializationTransaction' | ||
|
|
||
| export function serializeFullSnapshot( | ||
| timestamp: TimeStamp, | ||
| kind: SerializationKind, | ||
| document: Document, | ||
| emitRecord: EmitRecordCallback, | ||
| emitStats: EmitStatsCallback, | ||
| scope: RecordingScope | ||
| ): void { | ||
| serializeInTransaction(kind, emitRecord, emitStats, scope, (transaction: SerializationTransaction) => { | ||
| const defaultPrivacyLevel = transaction.scope.configuration.defaultPrivacyLevel | ||
|
|
||
| // We are sure that Documents are never ignored, so this function never returns null. | ||
| const node = serializeNode(document, defaultPrivacyLevel, transaction)! | ||
|
|
||
| const record: BrowserFullSnapshotRecord = { | ||
| data: { | ||
| node, | ||
| initialOffset: { | ||
| left: getScrollX(), | ||
| top: getScrollY(), | ||
| }, | ||
| }, | ||
| type: RecordType.FullSnapshot, | ||
| timestamp, | ||
| } | ||
| transaction.add(record) | ||
| }) | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
packages/rum/src/domain/record/serialization/serializeFullSnapshotAsChange.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { TimeStamp } from '@datadog/browser-core' | ||
| import type { EmitRecordCallback, EmitStatsCallback } from '../record.types' | ||
| import type { RecordingScope } from '../recordingScope' | ||
| import { serializeChangesInTransaction } from './serializationTransaction' | ||
| import type { ChangeSerializationTransaction, SerializationKind } from './serializationTransaction' | ||
| import { serializeNodeAsChange } from './serializeNodeAsChange' | ||
| import { createRootInsertionCursor } from './insertionCursor' | ||
|
|
||
| export function serializeFullSnapshotAsChange( | ||
| timestamp: TimeStamp, | ||
| kind: SerializationKind, | ||
| document: Document, | ||
| emitRecord: EmitRecordCallback, | ||
| emitStats: EmitStatsCallback, | ||
| scope: RecordingScope | ||
| ): void { | ||
| scope.resetIds() | ||
| serializeChangesInTransaction( | ||
| kind, | ||
| emitRecord, | ||
| emitStats, | ||
| scope, | ||
| timestamp, | ||
| (transaction: ChangeSerializationTransaction) => { | ||
| serializeNodeAsChange( | ||
| createRootInsertionCursor(scope.nodeIds), | ||
| document, | ||
| scope.configuration.defaultPrivacyLevel, | ||
| transaction | ||
| ) | ||
| } | ||
| ) | ||
| } |
113 changes: 113 additions & 0 deletions
113
packages/rum/src/domain/record/serialization/serializeMutations.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import type { RecordingScope } from '../recordingScope' | ||
| import { createRecordingScopeForTesting } from '../test/recordingScope.specHelper' | ||
| import { idsAreAssignedForNodeAndAncestors, sortAddedAndMovedNodes } from './serializeMutations' | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything in this file has been moved from |
||
|
|
||
| describe('idsAreAssignedForNodeAndAncestors', () => { | ||
| let scope: RecordingScope | ||
|
|
||
| beforeEach(() => { | ||
| scope = createRecordingScopeForTesting() | ||
| }) | ||
|
|
||
| it('returns false for DOM Nodes that have not been assigned an id', () => { | ||
| expect(idsAreAssignedForNodeAndAncestors(document.createElement('div'), scope.nodeIds)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns true for DOM Nodes that have been assigned an id', () => { | ||
| const node = document.createElement('div') | ||
| scope.nodeIds.getOrInsert(node) | ||
| expect(idsAreAssignedForNodeAndAncestors(node, scope.nodeIds)).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false for DOM Nodes when an ancestor has not been assigned an id', () => { | ||
| const node = document.createElement('div') | ||
| scope.nodeIds.getOrInsert(node) | ||
|
|
||
| const parent = document.createElement('div') | ||
| parent.appendChild(node) | ||
| scope.nodeIds.getOrInsert(parent) | ||
|
|
||
| const grandparent = document.createElement('div') | ||
| grandparent.appendChild(parent) | ||
|
|
||
| expect(idsAreAssignedForNodeAndAncestors(node, scope.nodeIds)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns true for DOM Nodes when all ancestors have been assigned an id', () => { | ||
| const node = document.createElement('div') | ||
| scope.nodeIds.getOrInsert(node) | ||
|
|
||
| const parent = document.createElement('div') | ||
| parent.appendChild(node) | ||
| scope.nodeIds.getOrInsert(parent) | ||
|
|
||
| const grandparent = document.createElement('div') | ||
| grandparent.appendChild(parent) | ||
| scope.nodeIds.getOrInsert(grandparent) | ||
|
|
||
| expect(idsAreAssignedForNodeAndAncestors(node, scope.nodeIds)).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true for DOM Nodes in shadow subtrees', () => { | ||
| const node = document.createElement('div') | ||
| scope.nodeIds.getOrInsert(node) | ||
|
|
||
| const parent = document.createElement('div') | ||
| parent.appendChild(node) | ||
| scope.nodeIds.getOrInsert(parent) | ||
|
|
||
| const grandparent = document.createElement('div') | ||
| const shadowRoot = grandparent.attachShadow({ mode: 'open' }) | ||
| shadowRoot.appendChild(parent) | ||
| scope.nodeIds.getOrInsert(grandparent) | ||
|
|
||
| expect(idsAreAssignedForNodeAndAncestors(node, scope.nodeIds)).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('sortAddedAndMovedNodes', () => { | ||
| let parent: Node | ||
| let a: Node | ||
| let aa: Node | ||
| let b: Node | ||
| let c: Node | ||
| let d: Node | ||
|
|
||
| beforeEach(() => { | ||
| // Create a tree like this: | ||
| // parent | ||
| // / | \ \ | ||
| // a b c d | ||
| // | | ||
| // aa | ||
| a = document.createElement('a') | ||
| aa = document.createElement('aa') | ||
| b = document.createElement('b') | ||
| c = document.createElement('c') | ||
| d = document.createElement('d') | ||
| parent = document.createElement('parent') | ||
| parent.appendChild(a) | ||
| a.appendChild(aa) | ||
| parent.appendChild(b) | ||
| parent.appendChild(c) | ||
| parent.appendChild(d) | ||
| }) | ||
|
|
||
| it('sorts siblings in reverse order', () => { | ||
| const nodes = [c, b, d, a] | ||
| sortAddedAndMovedNodes(nodes) | ||
| expect(nodes).toEqual([d, c, b, a]) | ||
| }) | ||
|
|
||
| it('sorts parents', () => { | ||
| const nodes = [a, parent, aa] | ||
| sortAddedAndMovedNodes(nodes) | ||
| expect(nodes).toEqual([parent, a, aa]) | ||
| }) | ||
|
|
||
| it('sorts parents first then siblings', () => { | ||
| const nodes = [c, aa, b, parent, d, a] | ||
| sortAddedAndMovedNodes(nodes) | ||
| expect(nodes).toEqual([parent, d, c, b, a, aa]) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just the old
serializeDocument()function, merged into the code block that called it instartFullSnapshots.ts. The boundary between "driver" code and serialization code is now at theserializeInTransaction()call, for both full snapshots and incremental snapshots.