Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165import { DataReader, TagType, CrdtId, LwwValue, UnexpectedBlockError, EOFError, EMPTY, copyBytes, decodeUtf8 } from './tagged-block-common.js';
export { UnexpectedBlockError, EOFError };
export class BlockOverflowError extends Error { constructor (msg) { super(msg); this.name = 'BlockOverflowError'; }}
export class BlockInfo { constructor (offset, size) { this.offset = offset; this.size = size; this.extraData = EMPTY; }}
export class MainBlockInfo extends BlockInfo { constructor (offset, size, blockType, minVersion, currentVersion) { super(offset, size); this.blockType = blockType; this.minVersion = minVersion; this.currentVersion = currentVersion; }}
export class SubBlockInfo extends BlockInfo {}
function warn (msg) { const stderr = globalThis.process?.stderr; if (stderr?.write) stderr.write(`${msg}\n`); else console.warn(msg);}
export class TaggedBlockReader { constructor (buf) { this.data = buf instanceof DataReader ? buf : new DataReader(buf); this.currentBlock = null; this._warnedAboutExtraData = false; }
readHeader () { this.data.readHeader(); }
beginBlock () { if (this.currentBlock !== null) throw new UnexpectedBlockError('Already in a block'); let blockLength; try { blockLength = this.data.readUint32(); } catch (e) { if (e instanceof EOFError) return null; throw e; } const unknown = this.data.readUint8(); // eslint-disable-line no-unused-vars const minVersion = this.data.readUint8(); const currentVersion = this.data.readUint8(); const blockType = this.data.readUint8(); const i0 = this.data.tell(); this.currentBlock = new MainBlockInfo(i0, blockLength, blockType, minVersion, currentVersion); return this.currentBlock; }
endBlock () { if (!this.currentBlock) throw new Error('Not in a block'); this._checkPosition(this.currentBlock); this.currentBlock = null; }
bytesRemainingInBlock () { if (!this.currentBlock) throw new Error('Not in a block'); return this.currentBlock.offset + this.currentBlock.size - this.data.tell(); }
readSubblock (index, fn) { this.data.readTag(index, TagType.Length4); const subblockLength = this.data.readUint32(); const i0 = this.data.tell(); const subblock = new SubBlockInfo(i0, subblockLength); const result = fn(subblock); this._checkPosition(subblock); return result; }
hasSubblock (index) { if (this.currentBlock && this.bytesRemainingInBlock() <= 0) return false; return this.data.checkTag(index, TagType.Length4); }
_checkPosition (blockInfo) { const i0 = blockInfo.offset, length = blockInfo.size, i1 = this.data.tell(); if (i1 > i0 + length) throw new BlockOverflowError( `${blockInfo.constructor.name} starting at ${i0}, length ${length}, read up to ${i1} (overflow by ${i1 - (i0 + length)})` ); if (i1 < i0 + length) { if (!this._warnedAboutExtraData) { warn('Warning: Some data has not been read. The data may have been written using a newer format.'); this._warnedAboutExtraData = true; } const remaining = i0 + length - i1; blockInfo.extraData = copyBytes(this.data.readBytes(remaining)); } }
readId (index) { this.data.readTag(index, TagType.ID); return this.data.readCrdtId(); } readBool (index) { this.data.readTag(index, TagType.Byte1); return this.data.readBool(); } readByte (index) { this.data.readTag(index, TagType.Byte1); return this.data.readUint8(); } readInt (index) { this.data.readTag(index, TagType.Byte4); return this.data.readUint32(); } readFloat (index) { this.data.readTag(index, TagType.Byte4); return this.data.readFloat32(); } readDouble (index) { this.data.readTag(index, TagType.Byte8); return this.data.readFloat64(); }
_readOptional (fn, defaultValue = null) { try { return fn(); } catch (e) { if (e instanceof UnexpectedBlockError || e instanceof EOFError) return defaultValue; throw e; } }
readIdOptional (index, def = null) { return this._readOptional(() => this.readId(index), def); } readBoolOptional (index, def = null) { return this._readOptional(() => this.readBool(index), def); } readByteOptional (index, def = null) { return this._readOptional(() => this.readByte(index), def); } readIntOptional (index, def = null) { return this._readOptional(() => this.readInt(index), def); } readFloatOptional (index, def = null) { return this._readOptional(() => this.readFloat(index), def); } readDoubleOptional (index, def = null) { return this._readOptional(() => this.readDouble(index), def); }
readColorOptional (index) { if (!this.data.checkTag(index, TagType.Byte4)) return null; const packed = this.readInt(index); return [(packed >> 16) & 0xFF, (packed >> 8) & 0xFF, packed & 0xFF, (packed >>> 24) & 0xFF]; }
readLwwBool (index) { return this.readSubblock(index, () => new LwwValue(this.readId(1), this.readBool(2))); } readLwwByte (index) { return this.readSubblock(index, () => new LwwValue(this.readId(1), this.readByte(2))); } readLwwFloat (index) { return this.readSubblock(index, () => new LwwValue(this.readId(1), this.readFloat(2))); } readLwwId (index) { return this.readSubblock(index, () => new LwwValue(this.readId(1), this.readId(2))); } readLwwString (index) { return this.readSubblock(index, () => new LwwValue(this.readId(1), this.readString(2))); }
readString (index) { return this.readSubblock(index, () => { const stringLength = this.data.readVarint(); this.data.readBool(); // isAscii flag return decodeUtf8(this.data.readBytes(stringLength)); }); }
readStringWithFormat (index) { return this.readSubblock(index, () => { const stringLength = this.data.readVarint(); this.data.readBool(); // isAscii flag const str = decodeUtf8(this.data.readBytes(stringLength)); const fmt = this.data.checkTag(2, TagType.Byte4) ? this.readInt(2) : null; return [str, fmt]; }); }
readIntPair (index) { return this.readSubblock(index, () => [this.data.readUint32(), this.data.readUint32()]); }}