UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

138 lines (137 loc) 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CrdtReader = void 0; const Reader_1 = require("@jsonjoy.com/util/lib/buffers/Reader"); /** @todo Rename file name. */ class CrdtReader extends Reader_1.Reader { id() { const byte = this.u8(); if (byte <= 127) return [byte >>> 4, byte & 0b1111]; this.x--; return [this.b1vu56()[1], this.vu57()]; } idSkip() { const byte = this.u8(); if (byte <= 127) return; this.x--; this.b1vu56(); this.vu57Skip(); } vu57() { const o1 = this.u8(); if (o1 <= 0b01111111) return o1; const o2 = this.u8(); if (o2 <= 0b01111111) return (o2 << 7) | (o1 & 0b01111111); const o3 = this.u8(); if (o3 <= 0b01111111) return (o3 << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b01111111); const o4 = this.u8(); if (o4 <= 0b01111111) return (o4 << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b01111111); const o5 = this.u8(); if (o5 <= 0b01111111) return (o5 * 0b10000000000000000000000000000 + (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 127))); const o6 = this.u8(); if (o6 <= 0b01111111) return (o6 * 0b100000000000000000000000000000000000 + ((o5 & 0b01111111) * 0b10000000000000000000000000000 + (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 127)))); const o7 = this.u8(); if (o7 <= 0b01111111) return (o7 * 0b1000000000000000000000000000000000000000000 + ((o6 & 0b01111111) * 0b100000000000000000000000000000000000 + ((o5 & 0b01111111) * 0b10000000000000000000000000000 + (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 127))))); const o8 = this.u8(); return (o8 * 0b10000000000000000000000000000000000000000000000000 + ((o7 & 0b01111111) * 0b1000000000000000000000000000000000000000000 + ((o6 & 0b01111111) * 0b100000000000000000000000000000000000 + ((o5 & 0b01111111) * 0b10000000000000000000000000000 + (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 127)))))); } vu57Skip() { const o1 = this.u8(); if (o1 <= 0b01111111) return; const o2 = this.u8(); if (o2 <= 0b01111111) return; const o3 = this.u8(); if (o3 <= 0b01111111) return; const o4 = this.u8(); if (o4 <= 0b01111111) return; const o5 = this.u8(); if (o5 <= 0b01111111) return; const o6 = this.u8(); if (o6 <= 0b01111111) return; const o7 = this.u8(); if (o7 <= 0b01111111) return; this.x++; } b1vu56() { const byte = this.u8(); const flag = byte & 0b10000000 ? 1 : 0; const o1 = 127 & byte; if (o1 <= 63) return [flag, o1]; const o2 = this.u8(); if (o2 <= 0b01111111) return [flag, (o2 << 6) | (o1 & 63)]; const o3 = this.u8(); if (o3 <= 0b01111111) return [flag, (o3 << 13) | ((o2 & 0b01111111) << 6) | (o1 & 63)]; const o4 = this.u8(); if (o4 <= 0b01111111) return [flag, (o4 << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 63)]; const o5 = this.u8(); if (o5 <= 0b01111111) return [ flag, o5 * 0b1000000000000000000000000000 + (((o4 & 0b01111111) << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 63)), ]; const o6 = this.u8(); if (o6 <= 0b01111111) return [ flag, o6 * 0b10000000000000000000000000000000000 + ((o5 & 0b01111111) * 0b1000000000000000000000000000 + (((o4 & 0b01111111) << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 63))), ]; const o7 = this.u8(); if (o7 <= 0b01111111) return [ flag, o7 * 0b100000000000000000000000000000000000000000 + ((o6 & 0b01111111) * 0b10000000000000000000000000000000000 + ((o5 & 0b01111111) * 0b1000000000000000000000000000 + (((o4 & 0b01111111) << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 63)))), ]; const o8 = this.u8(); return [ flag, o8 * 0b1000000000000000000000000000000000000000000000000 + ((o7 & 0b01111111) * 0b100000000000000000000000000000000000000000 + ((o6 & 0b01111111) * 0b10000000000000000000000000000000000 + ((o5 & 0b01111111) * 0b1000000000000000000000000000 + (((o4 & 0b01111111) << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 63))))), ]; } } exports.CrdtReader = CrdtReader;