All files / json-crdt-patch/codec/binary shared.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 2/2
100% Lines 10/10

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 34174x 174x   174x     174x     174x               174x 7865x       174x               174x 52505x    
import {Encoder} from './Encoder';
import {Decoder} from './Decoder';
import type {Patch} from '../../Patch';
import {CrdtWriter} from '../../util/binary/CrdtWriter';
 
/** A shared instance of the {@link CrdtWriter} class. */
const writer = new CrdtWriter(1024 * 4);
 
/** A shared instance of the {@link Encoder} class. */
export const encoder = new Encoder(writer);
 
/**
 * Encodes a JSON CRDT Patch into a binary {@link Uint8Array} blob.
 *
 * @param patch A {@link Patch} to encode.
 * @returns A Uint8Array containing the encoded patch.
 */
export const encode = (patch: Patch): Uint8Array => {
  return encoder.encode(patch);
};
 
/** A shared instance of the {@link Decoder} class. */
export const decoder = new Decoder();
 
/**
 * Decodes a binary {@link Uint8Array} blob into a JSON CRDT Patch.
 *
 * @param buf Binary blob to decode.
 * @returns A {@link Patch} decoded from the binary blob.
 */
export const decode = (buf: Uint8Array): Patch => {
  return decoder.decode(buf);
};