Files
rrweb/packages/packer/src/unpack.ts
Justin Halsall a756a45aaf Improve development tooling (#1516)
- Running `yarn build` in a `packages/*/` directory will trigger build of all dependencies too, and cache them if possible.
- Fix for `yarn dev` breaking for `rrweb` package whenever changing files in `rrweb` package
- Update typescript, turbo, vite and vite-plugin-dts
- Require `workspaces-to-typescript-project-references` from `prepublish`
2024-06-21 18:13:53 +01:00

32 lines
866 B
TypeScript

import { strFromU8, strToU8, unzlibSync } from 'fflate';
import { type eventWithTimeAndPacker, MARK } from './base';
import type { UnpackFn, eventWithTime } from '@rrweb/types';
export const unpack: UnpackFn = (raw: string) => {
if (typeof raw !== 'string') {
return raw;
}
try {
const e: eventWithTime = JSON.parse(raw) as eventWithTime;
if (e.timestamp) {
return e;
}
} catch (error) {
// ignore and continue
}
try {
const e: eventWithTimeAndPacker = JSON.parse(
strFromU8(unzlibSync(strToU8(raw, true))),
) as eventWithTimeAndPacker;
if (e.v === MARK) {
return e;
}
throw new Error(
`These events were packed with packer ${e.v} which is incompatible with current packer ${MARK}.`,
);
} catch (error) {
console.error(error);
throw new Error('Unknown data format.');
}
};