All files / src/snapshot binary.ts

100% Statements 23/23
100% Branches 0/0
100% Functions 4/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 341x 1x 1x 1x 1x       1x 1x   1x 3x 3x     1x 1x 1x     1x 3x 3x     1x       1x 1x    
import { CborEncoder } from '@jsonjoy.com/json-pack/lib/cbor/CborEncoder';
import { CborDecoder } from '@jsonjoy.com/json-pack/lib/cbor/CborDecoder';
import { fromSnapshotSync, toSnapshotSync } from './sync';
import { fromSnapshot, toSnapshot } from './async';
import { writer } from './shared';
import type { CborUint8Array } from '@jsonjoy.com/json-pack/lib/cbor/types';
import type { AsyncSnapshotOptions, SnapshotNode, SnapshotOptions } from './types';
 
const encoder = new CborEncoder(writer);
const decoder = new CborDecoder();
 
export const toBinarySnapshotSync = (options: SnapshotOptions): CborUint8Array<SnapshotNode> => {
  const snapshot = toSnapshotSync(options);
  return encoder.encode(snapshot) as CborUint8Array<SnapshotNode>;
};
 
export const fromBinarySnapshotSync = (uint8: CborUint8Array<SnapshotNode>, options: SnapshotOptions): void => {
  const snapshot = decoder.decode(uint8) as SnapshotNode;
  fromSnapshotSync(snapshot, options);
};
 
export const toBinarySnapshot = async (options: AsyncSnapshotOptions): Promise<CborUint8Array<SnapshotNode>> => {
  const snapshot = await toSnapshot(options);
  return encoder.encode(snapshot) as CborUint8Array<SnapshotNode>;
};
 
export const fromBinarySnapshot = async (
  uint8: CborUint8Array<SnapshotNode>,
  options: AsyncSnapshotOptions,
): Promise<void> => {
  const snapshot = decoder.decode(uint8) as SnapshotNode;
  await fromSnapshot(snapshot, options);
};