-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(session-replay-browser): WebWorker support for compression (#932)
* feat(session-replay-browser): webworkers first pass * test(session-replay-browser): webworkers additional coverage * fix(session-replay-browser): build fix due to typing issues * feat(session-replay-browser): adding options, fixing build, and adding test coverage * fix(session-replay-browser): pr fixes
- Loading branch information
1 parent
20adea2
commit 4ebe04a
Showing
12 changed files
with
233 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,6 +1,14 @@ | ||
import { iife, umd } from '../../scripts/build/rollup.config'; | ||
import { webWorkerPlugins } from '../session-replay-browser/rollup.config'; | ||
|
||
iife.input = umd.input; | ||
iife.output.name = 'sessionReplay'; | ||
|
||
export default [umd, iife]; | ||
export default async () => { | ||
const commonPlugins = await webWorkerPlugins(); | ||
|
||
iife.plugins = [...commonPlugins, ...iife.plugins]; | ||
umd.plugins = [...commonPlugins, ...umd.plugins]; | ||
|
||
return [iife, umd]; | ||
}; |
This file contains 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 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 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 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,6 +1,60 @@ | ||
import { iife, umd } from '../../scripts/build/rollup.config'; | ||
|
||
|
||
import resolve from '@rollup/plugin-node-resolve'; | ||
import replace from '@rollup/plugin-replace'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import typescript from '@rollup/plugin-typescript'; | ||
import { rollup } from 'rollup'; | ||
import path from 'node:path'; | ||
|
||
iife.input = umd.input; | ||
iife.output.name = 'sessionReplay'; | ||
|
||
export default [umd, iife]; | ||
async function buildWebWorker() { | ||
const input = path.join(path.dirname(new URL(import.meta.url).pathname), './src/worker/compression.ts'); | ||
const bundle = await rollup({ | ||
input, | ||
plugins: [ | ||
resolve({ | ||
browser: true, | ||
}), | ||
typescript({ | ||
tsconfig: 'tsconfig.json', | ||
// no need to output types | ||
declaration: false, | ||
declarationMap: false, | ||
}), | ||
terser(), | ||
], | ||
}); | ||
|
||
const { output } = await bundle.generate({ | ||
format: 'iife', | ||
name: 'WebWorker', | ||
inlineDynamicImports: true, | ||
}); | ||
const webWorkerCode = output[0].code; | ||
|
||
return webWorkerCode; | ||
} | ||
|
||
export async function webWorkerPlugins() { | ||
return [ | ||
replace({ | ||
preventAssignment: true, | ||
values: { | ||
'replace.COMPRESSION_WEBWORKER_BODY': JSON.stringify(await buildWebWorker()), | ||
}, | ||
}), | ||
]; | ||
} | ||
|
||
export default async () => { | ||
const commonPlugins = await webWorkerPlugins(); | ||
|
||
iife.plugins = [...commonPlugins, ...iife.plugins]; | ||
umd.plugins = [...commonPlugins, ...umd.plugins]; | ||
|
||
return [iife, umd]; | ||
}; |
This file contains 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 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 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 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 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,15 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import { pack } from '@amplitude/rrweb-packer'; | ||
|
||
onmessage = (e) => { | ||
const { event, sessionId } = e.data; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
const compressedEvent = JSON.stringify(pack(event)); | ||
|
||
postMessage({ compressedEvent, sessionId }); | ||
}; | ||
|
||
// added for testing | ||
export const compressionOnMessage = onmessage; |
This file contains 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
32 changes: 32 additions & 0 deletions
32
packages/session-replay-browser/test/worker/compression.test.ts
This file contains 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,32 @@ | ||
import { eventWithTime } from '@amplitude/rrweb'; | ||
import { compressionOnMessage } from '../../src/worker/compression'; | ||
import { pack } from '@amplitude/rrweb-packer'; | ||
|
||
describe('compression', () => { | ||
test('should compress event', async () => { | ||
global.postMessage = jest.fn(); | ||
|
||
const testEvent: eventWithTime = { | ||
timestamp: 1, | ||
type: 4, | ||
data: { | ||
height: 1, | ||
width: 1, | ||
href: 'http://localhost', | ||
}, | ||
}; | ||
|
||
// hack to make typescript not complain | ||
(compressionOnMessage as (_: unknown) => void)({ | ||
data: { | ||
event: testEvent, | ||
sessionId: 1234, | ||
}, | ||
}); | ||
|
||
expect(global.postMessage).toHaveBeenCalledWith({ | ||
sessionId: 1234, | ||
compressedEvent: JSON.stringify(pack(testEvent)), | ||
}); | ||
}); | ||
}); |