@eyhn/msgpack-stream
Advanced tools
Comparing version 2.8.0 to 2.8.1
@@ -6,2 +6,8 @@ import type { ExtensionCodecType } from "./ExtensionCodec"; | ||
/** | ||
* The size of the stream buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
streamBufferSize: number; | ||
/** | ||
* Maximum string length. | ||
@@ -56,2 +62,2 @@ * | ||
export declare function decodeMulti<ContextType = undefined>(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions<SplitUndefined<ContextType>>): Generator<unknown, void, unknown>; | ||
export declare function decodeStream<ContextType = undefined>(read: (data: Uint8Array) => number, options?: DecodeOptions<SplitUndefined<ContextType>>): unknown; | ||
export declare function decodeStream<ContextType = undefined>(read: AsyncIterable<Uint8Array>, options?: DecodeOptions<SplitUndefined<ContextType>>): Promise<unknown>; |
@@ -18,2 +18,8 @@ import type { ExtensionCodecType } from "./ExtensionCodec"; | ||
/** | ||
* The size of the stream buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
streamBufferSize: number; | ||
/** | ||
* If `true`, the keys of an object is sorted. In other words, the encoded | ||
@@ -55,2 +61,2 @@ * binary is canonical and thus comparable to another encoded binary. | ||
export declare function encode<ContextType = undefined>(value: unknown, options?: EncodeOptions<SplitUndefined<ContextType>>): Uint8Array; | ||
export declare function encodeStream<ContextType = undefined>(value: unknown, write: (data: Uint8Array) => void, options?: EncodeOptions<SplitUndefined<ContextType>>): void; | ||
export declare function encodeStream<ContextType = undefined>(value: unknown, options?: EncodeOptions<SplitUndefined<ContextType>>): Iterable<Uint8Array>; |
@@ -18,7 +18,7 @@ "use strict"; | ||
exports.encode = encode; | ||
function encodeStream(value, write, options = defaultEncodeOptions) { | ||
const encoder = new StreamEncoder_1.StreamEncoder(write, options.extensionCodec, options.context, options.maxDepth, options.initialBufferSize, options.sortKeys, options.forceFloat32, options.ignoreUndefined, options.forceIntegerToFloat); | ||
encoder.encode(value); | ||
function encodeStream(value, options = defaultEncodeOptions) { | ||
const encoder = new StreamEncoder_1.StreamEncoder(options.extensionCodec, options.context, options.maxDepth, options.streamBufferSize, options.sortKeys, options.forceFloat32, options.ignoreUndefined, options.forceIntegerToFloat); | ||
return encoder.encode(value); | ||
} | ||
exports.encodeStream = encodeStream; | ||
//# sourceMappingURL=encode.js.map |
import { ExtensionCodecType } from "./ExtensionCodec"; | ||
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder"; | ||
import { KeyDecoder } from "./CachedKeyDecoder"; | ||
export declare const DataViewIndexOutOfBoundsError: typeof Error; | ||
export declare const DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
export declare const DEFAULT_BUFFER_SIZE = 2048; | ||
export declare class StreamDecoder<ContextType = undefined> { | ||
private readonly read; | ||
private readonly readIterable; | ||
private readonly extensionCodec; | ||
@@ -15,8 +15,8 @@ private readonly context; | ||
private readonly keyDecoder; | ||
private readonly initialBufferSize; | ||
private readonly bufferSize; | ||
private headByte; | ||
private view; | ||
private bytes; | ||
private readonly stack; | ||
constructor(read: (data: Uint8Array) => number, extensionCodec?: ExtensionCodecType<ContextType>, context?: ContextType, maxStrLength?: number, maxBinLength?: number, maxArrayLength?: number, maxMapLength?: number, maxExtLength?: number, keyDecoder?: KeyDecoder | null, initialBufferSize?: CachedKeyDecoder); | ||
private readonly readStream; | ||
private buffer; | ||
constructor(readIterable: AsyncIterable<Uint8Array>, extensionCodec?: ExtensionCodecType<ContextType>, context?: ContextType, maxStrLength?: number, maxBinLength?: number, maxArrayLength?: number, maxMapLength?: number, maxExtLength?: number, keyDecoder?: KeyDecoder | null, bufferSize?: number); | ||
private reinitializeState; | ||
@@ -28,10 +28,11 @@ private createExtraByteError; | ||
*/ | ||
decode(): unknown; | ||
decodeMulti(): Generator<unknown, void, unknown>; | ||
decode(): Promise<unknown>; | ||
decodeMulti(): AsyncGenerator<unknown>; | ||
private doDecodeSync; | ||
private readHeadByteFromBuffer; | ||
private readHeadByte; | ||
private complete; | ||
private readArraySize; | ||
private pushMapState; | ||
private pushArrayState; | ||
private decodeUtf8StringFromBuffer; | ||
private decodeUtf8String; | ||
@@ -51,5 +52,15 @@ private stateIsMapKey; | ||
private readF64; | ||
private readU8FromBuffer; | ||
private readI8FromBuffer; | ||
private readU16FromBuffer; | ||
private readI16FromBuffer; | ||
private readU32FromBuffer; | ||
private readI32FromBuffer; | ||
private readU64FromBuffer; | ||
private readI64FromBuffer; | ||
private readF32FromBuffer; | ||
private readF64FromBuffer; | ||
private readBytesFromBuffer; | ||
private readBytes; | ||
private ensureBufferSizeToWrite; | ||
private resizeBuffer; | ||
private readToBuffer; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StreamDecoder = exports.DEFAULT_INITIAL_BUFFER_SIZE = exports.DataViewIndexOutOfBoundsError = void 0; | ||
exports.StreamDecoder = exports.DEFAULT_BUFFER_SIZE = exports.DataViewIndexOutOfBoundsError = void 0; | ||
const prettyByte_1 = require("./utils/prettyByte"); | ||
@@ -32,8 +32,8 @@ const ExtensionCodec_1 = require("./ExtensionCodec"); | ||
})(); | ||
const sharedCachedKeyDecoder = new CachedKeyDecoder_1.CachedKeyDecoder(); | ||
const MORE_DATA = new exports.DataViewIndexOutOfBoundsError("Insufficient data"); | ||
const sharedCachedKeyDecoder = new CachedKeyDecoder_1.CachedKeyDecoder(); | ||
exports.DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
exports.DEFAULT_BUFFER_SIZE = 2048; | ||
class StreamDecoder { | ||
constructor(read, extensionCodec = ExtensionCodec_1.ExtensionCodec.defaultCodec, context = undefined, maxStrLength = int_1.UINT32_MAX, maxBinLength = int_1.UINT32_MAX, maxArrayLength = int_1.UINT32_MAX, maxMapLength = int_1.UINT32_MAX, maxExtLength = int_1.UINT32_MAX, keyDecoder = sharedCachedKeyDecoder, initialBufferSize = sharedCachedKeyDecoder) { | ||
this.read = read; | ||
constructor(readIterable, extensionCodec = ExtensionCodec_1.ExtensionCodec.defaultCodec, context = undefined, maxStrLength = int_1.UINT32_MAX, maxBinLength = int_1.UINT32_MAX, maxArrayLength = int_1.UINT32_MAX, maxMapLength = int_1.UINT32_MAX, maxExtLength = int_1.UINT32_MAX, keyDecoder = sharedCachedKeyDecoder, bufferSize = exports.DEFAULT_BUFFER_SIZE) { | ||
this.readIterable = readIterable; | ||
this.extensionCodec = extensionCodec; | ||
@@ -47,7 +47,7 @@ this.context = context; | ||
this.keyDecoder = keyDecoder; | ||
this.initialBufferSize = initialBufferSize; | ||
this.bufferSize = bufferSize; | ||
this.headByte = HEAD_BYTE_REQUIRED; | ||
this.view = new DataView(new ArrayBuffer(exports.DEFAULT_INITIAL_BUFFER_SIZE)); | ||
this.bytes = new Uint8Array(this.view.buffer); | ||
this.stack = []; | ||
this.buffer = null; | ||
this.readStream = readIterable[Symbol.asyncIterator](); | ||
} | ||
@@ -70,11 +70,12 @@ reinitializeState() { | ||
} | ||
*decodeMulti() { | ||
async *decodeMulti() { | ||
this.reinitializeState(); | ||
while (true) { | ||
yield this.doDecodeSync(); | ||
yield await this.doDecodeSync(); | ||
} | ||
} | ||
doDecodeSync() { | ||
async doDecodeSync() { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3; | ||
DECODE: while (true) { | ||
const headByte = this.readHeadByte(); | ||
const headByte = (_a = this.readHeadByteFromBuffer()) !== null && _a !== void 0 ? _a : (await this.readHeadByte()); | ||
let object; | ||
@@ -117,3 +118,3 @@ if (headByte >= 0xe0) { | ||
const byteLength = headByte - 0xa0; | ||
object = this.decodeUtf8String(byteLength); | ||
object = (_b = this.decodeUtf8StringFromBuffer(byteLength)) !== null && _b !== void 0 ? _b : (await this.decodeUtf8String(byteLength)); | ||
} | ||
@@ -135,58 +136,58 @@ } | ||
// float 32 | ||
object = this.readF32(); | ||
object = (_c = this.readF32FromBuffer()) !== null && _c !== void 0 ? _c : (await this.readF32()); | ||
} | ||
else if (headByte === 0xcb) { | ||
// float 64 | ||
object = this.readF64(); | ||
object = (_d = this.readF64FromBuffer()) !== null && _d !== void 0 ? _d : (await this.readF64()); | ||
} | ||
else if (headByte === 0xcc) { | ||
// uint 8 | ||
object = this.readU8(); | ||
object = (_e = this.readU8FromBuffer()) !== null && _e !== void 0 ? _e : (await this.readU8()); | ||
} | ||
else if (headByte === 0xcd) { | ||
// uint 16 | ||
object = this.readU16(); | ||
object = (_f = this.readU16FromBuffer()) !== null && _f !== void 0 ? _f : (await this.readU16()); | ||
} | ||
else if (headByte === 0xce) { | ||
// uint 32 | ||
object = this.readU32(); | ||
object = (_g = this.readU32FromBuffer()) !== null && _g !== void 0 ? _g : (await this.readU32()); | ||
} | ||
else if (headByte === 0xcf) { | ||
// uint 64 | ||
object = this.readU64(); | ||
object = (_h = this.readU64FromBuffer()) !== null && _h !== void 0 ? _h : (await this.readU64()); | ||
} | ||
else if (headByte === 0xd0) { | ||
// int 8 | ||
object = this.readI8(); | ||
object = (_j = this.readI8FromBuffer()) !== null && _j !== void 0 ? _j : (await this.readI8()); | ||
} | ||
else if (headByte === 0xd1) { | ||
// int 16 | ||
object = this.readI16(); | ||
object = (_k = this.readI16FromBuffer()) !== null && _k !== void 0 ? _k : (await this.readI16()); | ||
} | ||
else if (headByte === 0xd2) { | ||
// int 32 | ||
object = this.readI32(); | ||
object = (_l = this.readI32FromBuffer()) !== null && _l !== void 0 ? _l : (await this.readI32()); | ||
} | ||
else if (headByte === 0xd3) { | ||
// int 64 | ||
object = this.readI64(); | ||
object = (_m = this.readI64FromBuffer()) !== null && _m !== void 0 ? _m : (await this.readI64()); | ||
} | ||
else if (headByte === 0xd9) { | ||
// str 8 | ||
const byteLength = this.readU8(); | ||
object = this.decodeUtf8String(byteLength); | ||
const byteLength = (_o = this.readU8FromBuffer()) !== null && _o !== void 0 ? _o : (await this.readU8()); | ||
object = (_p = this.decodeUtf8StringFromBuffer(byteLength)) !== null && _p !== void 0 ? _p : (await this.decodeUtf8String(byteLength)); | ||
} | ||
else if (headByte === 0xda) { | ||
// str 16 | ||
const byteLength = this.readU16(); | ||
object = this.decodeUtf8String(byteLength); | ||
const byteLength = (_q = this.readU16FromBuffer()) !== null && _q !== void 0 ? _q : (await this.readU16()); | ||
object = (_r = this.decodeUtf8StringFromBuffer(byteLength)) !== null && _r !== void 0 ? _r : (await this.decodeUtf8String(byteLength)); | ||
} | ||
else if (headByte === 0xdb) { | ||
// str 32 | ||
const byteLength = this.readU32(); | ||
object = this.decodeUtf8String(byteLength); | ||
const byteLength = (_s = this.readU32FromBuffer()) !== null && _s !== void 0 ? _s : (await this.readU32()); | ||
object = (_t = this.decodeUtf8StringFromBuffer(byteLength)) !== null && _t !== void 0 ? _t : (await this.decodeUtf8String(byteLength)); | ||
} | ||
else if (headByte === 0xdc) { | ||
// array 16 | ||
const size = this.readU16(); | ||
const size = (_u = this.readU16FromBuffer()) !== null && _u !== void 0 ? _u : (await this.readU16()); | ||
if (size !== 0) { | ||
@@ -203,3 +204,3 @@ this.pushArrayState(size); | ||
// array 32 | ||
const size = this.readU32(); | ||
const size = (_v = this.readU32FromBuffer()) !== null && _v !== void 0 ? _v : (await this.readU32()); | ||
if (size !== 0) { | ||
@@ -216,3 +217,3 @@ this.pushArrayState(size); | ||
// map 16 | ||
const size = this.readU16(); | ||
const size = (_w = this.readU16FromBuffer()) !== null && _w !== void 0 ? _w : (await this.readU16()); | ||
if (size !== 0) { | ||
@@ -229,3 +230,3 @@ this.pushMapState(size); | ||
// map 32 | ||
const size = this.readU32(); | ||
const size = (_x = this.readU32FromBuffer()) !== null && _x !== void 0 ? _x : (await this.readU32()); | ||
if (size !== 0) { | ||
@@ -242,49 +243,49 @@ this.pushMapState(size); | ||
// bin 8 | ||
const size = this.readU8(); | ||
object = this.decodeBinary(size); | ||
const size = (_y = this.readU8FromBuffer()) !== null && _y !== void 0 ? _y : (await this.readU8()); | ||
object = await this.decodeBinary(size); | ||
} | ||
else if (headByte === 0xc5) { | ||
// bin 16 | ||
const size = this.readU16(); | ||
object = this.decodeBinary(size); | ||
const size = (_z = this.readU16FromBuffer()) !== null && _z !== void 0 ? _z : (await this.readU16()); | ||
object = await this.decodeBinary(size); | ||
} | ||
else if (headByte === 0xc6) { | ||
// bin 32 | ||
const size = this.readU32(); | ||
object = this.decodeBinary(size); | ||
const size = (_0 = this.readU32FromBuffer()) !== null && _0 !== void 0 ? _0 : (await this.readU32()); | ||
object = await this.decodeBinary(size); | ||
} | ||
else if (headByte === 0xd4) { | ||
// fixext 1 | ||
object = this.decodeExtension(1); | ||
object = await this.decodeExtension(1); | ||
} | ||
else if (headByte === 0xd5) { | ||
// fixext 2 | ||
object = this.decodeExtension(2); | ||
object = await this.decodeExtension(2); | ||
} | ||
else if (headByte === 0xd6) { | ||
// fixext 4 | ||
object = this.decodeExtension(4); | ||
object = await this.decodeExtension(4); | ||
} | ||
else if (headByte === 0xd7) { | ||
// fixext 8 | ||
object = this.decodeExtension(8); | ||
object = await this.decodeExtension(8); | ||
} | ||
else if (headByte === 0xd8) { | ||
// fixext 16 | ||
object = this.decodeExtension(16); | ||
object = await this.decodeExtension(16); | ||
} | ||
else if (headByte === 0xc7) { | ||
// ext 8 | ||
const size = this.readU8(); | ||
object = this.decodeExtension(size); | ||
const size = (_1 = this.readU8FromBuffer()) !== null && _1 !== void 0 ? _1 : (await this.readU8()); | ||
object = await this.decodeExtension(size); | ||
} | ||
else if (headByte === 0xc8) { | ||
// ext 16 | ||
const size = this.readU16(); | ||
object = this.decodeExtension(size); | ||
const size = (_2 = this.readU16FromBuffer()) !== null && _2 !== void 0 ? _2 : (await this.readU16()); | ||
object = await this.decodeExtension(size); | ||
} | ||
else if (headByte === 0xc9) { | ||
// ext 32 | ||
const size = this.readU32(); | ||
object = this.decodeExtension(size); | ||
const size = (_3 = this.readU32FromBuffer()) !== null && _3 !== void 0 ? _3 : (await this.readU32()); | ||
object = await this.decodeExtension(size); | ||
} | ||
@@ -339,29 +340,21 @@ else { | ||
} | ||
readHeadByte() { | ||
readHeadByteFromBuffer() { | ||
const b = this.readU8FromBuffer(); | ||
if (b === null) { | ||
return null; | ||
} | ||
if (this.headByte === HEAD_BYTE_REQUIRED) { | ||
this.headByte = this.readU8(); | ||
// console.log("headByte", prettyByte(this.headByte)); | ||
this.headByte = b; | ||
} | ||
return this.headByte; | ||
} | ||
async readHeadByte() { | ||
if (this.headByte === HEAD_BYTE_REQUIRED) { | ||
this.headByte = await this.readU8(); | ||
} | ||
return this.headByte; | ||
} | ||
complete() { | ||
this.headByte = HEAD_BYTE_REQUIRED; | ||
} | ||
readArraySize() { | ||
const headByte = this.readHeadByte(); | ||
switch (headByte) { | ||
case 0xdc: | ||
return this.readU16(); | ||
case 0xdd: | ||
return this.readU32(); | ||
default: { | ||
if (headByte < 0xa0) { | ||
return headByte - 0x90; | ||
} | ||
else { | ||
throw new DecodeError_1.DecodeError(`Unrecognized array type byte: ${(0, prettyByte_1.prettyByte)(headByte)}`); | ||
} | ||
} | ||
} | ||
} | ||
pushMapState(size) { | ||
@@ -390,3 +383,3 @@ if (size > this.maxMapLength) { | ||
} | ||
decodeUtf8String(byteLength) { | ||
decodeUtf8StringFromBuffer(byteLength) { | ||
var _a; | ||
@@ -396,3 +389,6 @@ if (byteLength > this.maxStrLength) { | ||
} | ||
const bytes = this.readBytes(byteLength); | ||
const bytes = this.readBytesFromBuffer(byteLength); | ||
if (bytes === null) { | ||
return null; | ||
} | ||
let object; | ||
@@ -410,2 +406,20 @@ if (this.stateIsMapKey() && ((_a = this.keyDecoder) === null || _a === void 0 ? void 0 : _a.canBeCached(byteLength))) { | ||
} | ||
async decodeUtf8String(byteLength) { | ||
var _a; | ||
if (byteLength > this.maxStrLength) { | ||
throw new DecodeError_1.DecodeError(`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`); | ||
} | ||
const bytes = await this.readBytes(byteLength); | ||
let object; | ||
if (this.stateIsMapKey() && ((_a = this.keyDecoder) === null || _a === void 0 ? void 0 : _a.canBeCached(byteLength))) { | ||
object = this.keyDecoder.decode(bytes, 0, byteLength); | ||
} | ||
else if (byteLength > utf8_1.TEXT_DECODER_THRESHOLD) { | ||
object = (0, utf8_1.utf8DecodeTD)(bytes, 0, byteLength); | ||
} | ||
else { | ||
object = (0, utf8_1.utf8DecodeJs)(bytes, 0, byteLength); | ||
} | ||
return object; | ||
} | ||
stateIsMapKey() { | ||
@@ -418,93 +432,215 @@ if (this.stack.length > 0) { | ||
} | ||
decodeBinary(byteLength) { | ||
async decodeBinary(byteLength) { | ||
if (byteLength > this.maxBinLength) { | ||
throw new DecodeError_1.DecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`); | ||
} | ||
const object = this.readBytes(byteLength); | ||
const object = await this.readBytes(byteLength); | ||
return object; | ||
} | ||
decodeExtension(size) { | ||
async decodeExtension(size) { | ||
if (size > this.maxExtLength) { | ||
throw new DecodeError_1.DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`); | ||
} | ||
const extType = this.readI8(); | ||
const data = this.decodeBinary(size); | ||
const extType = await this.readI8(); | ||
const data = await this.decodeBinary(size); | ||
return this.extensionCodec.decode(data, extType, this.context); | ||
} | ||
readU8() { | ||
this.readBytes(1); | ||
const value = this.view.getUint8(0); | ||
async readU8() { | ||
const data = await this.readBytes(1); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint8(0); | ||
return value; | ||
} | ||
readI8() { | ||
this.readBytes(1); | ||
const value = this.view.getInt8(0); | ||
async readI8() { | ||
const data = await this.readBytes(1); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt8(0); | ||
return value; | ||
} | ||
readU16() { | ||
this.readBytes(2); | ||
const value = this.view.getUint16(0); | ||
async readU16() { | ||
const data = await this.readBytes(2); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0); | ||
return value; | ||
} | ||
readI16() { | ||
this.readBytes(2); | ||
const value = this.view.getInt16(0); | ||
async readI16() { | ||
const data = await this.readBytes(2); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt16(0); | ||
return value; | ||
} | ||
readU32() { | ||
this.readBytes(4); | ||
const value = this.view.getUint32(0); | ||
async readU32() { | ||
const data = await this.readBytes(4); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint32(0); | ||
return value; | ||
} | ||
readI32() { | ||
this.readBytes(4); | ||
const value = this.view.getInt32(0); | ||
async readI32() { | ||
const data = await this.readBytes(4); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt32(0); | ||
return value; | ||
} | ||
readU64() { | ||
this.readBytes(8); | ||
const value = (0, int_1.getUint64)(this.view, 0); | ||
async readU64() { | ||
const data = await this.readBytes(8); | ||
const value = (0, int_1.getUint64)(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
readI64() { | ||
this.readBytes(8); | ||
const value = (0, int_1.getInt64)(this.view, 0); | ||
async readI64() { | ||
const data = await this.readBytes(8); | ||
const value = (0, int_1.getInt64)(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
readF32() { | ||
this.readBytes(4); | ||
const value = this.view.getFloat32(0); | ||
async readF32() { | ||
const data = await this.readBytes(4); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat32(0); | ||
return value; | ||
} | ||
readF64() { | ||
this.readBytes(8); | ||
const value = this.view.getFloat64(0); | ||
async readF64() { | ||
const data = await this.readBytes(8); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat64(0); | ||
return value; | ||
} | ||
readBytes(length) { | ||
this.ensureBufferSizeToWrite(length); | ||
let pos = 0; | ||
while (pos < length) { | ||
const read = this.read(this.bytes.subarray(pos, length)); | ||
if (read <= 0) { | ||
throw MORE_DATA; | ||
readU8FromBuffer() { | ||
const data = this.readBytesFromBuffer(1); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint8(0); | ||
return value; | ||
} | ||
readI8FromBuffer() { | ||
const data = this.readBytesFromBuffer(1); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt8(0); | ||
return value; | ||
} | ||
readU16FromBuffer() { | ||
const data = this.readBytesFromBuffer(2); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0); | ||
return value; | ||
} | ||
readI16FromBuffer() { | ||
const data = this.readBytesFromBuffer(2); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt16(0); | ||
return value; | ||
} | ||
readU32FromBuffer() { | ||
const data = this.readBytesFromBuffer(4); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint32(0); | ||
return value; | ||
} | ||
readI32FromBuffer() { | ||
const data = this.readBytesFromBuffer(4); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt32(0); | ||
return value; | ||
} | ||
readU64FromBuffer() { | ||
const data = this.readBytesFromBuffer(8); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = (0, int_1.getUint64)(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
readI64FromBuffer() { | ||
const data = this.readBytesFromBuffer(8); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = (0, int_1.getInt64)(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
readF32FromBuffer() { | ||
const data = this.readBytesFromBuffer(4); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat32(0); | ||
return value; | ||
} | ||
readF64FromBuffer() { | ||
const data = this.readBytesFromBuffer(8); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat64(0); | ||
return value; | ||
} | ||
readBytesFromBuffer(length) { | ||
if (this.buffer && this.buffer.length >= length) { | ||
const result = this.buffer.subarray(0, length); | ||
this.buffer = this.buffer.subarray(length); | ||
return result; | ||
} | ||
return null; | ||
} | ||
async readBytes(length) { | ||
var _a, _b; | ||
if (this.buffer && length <= this.buffer.length) { | ||
const result = this.buffer.subarray(0, length); | ||
this.buffer = this.buffer.subarray(length); | ||
return result; | ||
} | ||
else { | ||
const hasData = (_b = (_a = this.buffer) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0; | ||
const needData = length - hasData; | ||
const result = new Uint8Array(length); | ||
if (this.buffer) { | ||
result.set(this.buffer, 0); | ||
} | ||
pos += read; | ||
if (needData < this.bufferSize) { | ||
const next = await this.readToBuffer(this.bufferSize); | ||
if (next.length < needData) { | ||
throw MORE_DATA; | ||
} | ||
result.set(next.subarray(0, needData), hasData); | ||
this.buffer = this.buffer.subarray(needData); | ||
} | ||
else { | ||
const next = await this.readToBuffer(needData); | ||
if (next.length < needData) { | ||
throw MORE_DATA; | ||
} | ||
result.set(next, hasData); | ||
this.buffer = new Uint8Array(); | ||
} | ||
return result; | ||
} | ||
return this.bytes.subarray(0, length); | ||
} | ||
ensureBufferSizeToWrite(sizeToWrite) { | ||
if (this.view.byteLength < sizeToWrite) { | ||
this.resizeBuffer(this.view.byteLength * 2); | ||
async readToBuffer(length) { | ||
const bytes = []; | ||
let size = 0; | ||
while (size < length) { | ||
const result = await this.readStream.next(this.bufferSize); | ||
if (!result.done) { | ||
bytes.push(result.value); | ||
size += result.value.length; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
if (bytes.length == 1) { | ||
this.buffer = bytes[0]; | ||
return bytes[0]; | ||
} | ||
this.buffer = new Uint8Array(size); | ||
let pos = 0; | ||
for (const chunk of bytes) { | ||
this.buffer.set(chunk, pos); | ||
pos += chunk.length; | ||
} | ||
return this.buffer; | ||
} | ||
resizeBuffer(newSize) { | ||
const newBuffer = new ArrayBuffer(newSize); | ||
const newBytes = new Uint8Array(newBuffer); | ||
const newView = new DataView(newBuffer); | ||
this.view = newView; | ||
this.bytes = newBytes; | ||
} | ||
} | ||
exports.StreamDecoder = StreamDecoder; | ||
//# sourceMappingURL=StreamDecoder.js.map |
import { ExtensionCodecType } from "./ExtensionCodec"; | ||
export declare const DEFAULT_MAX_DEPTH = 100; | ||
export declare const DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
export declare const DEFAULT_BUFFER_SIZE = 2048; | ||
export declare class StreamEncoder<ContextType = undefined> { | ||
private readonly write; | ||
private readonly extensionCodec; | ||
private readonly context; | ||
private readonly maxDepth; | ||
private readonly initialBufferSize; | ||
private readonly bufferSize; | ||
private readonly sortKeys; | ||
@@ -16,7 +15,4 @@ private readonly forceFloat32; | ||
private bytes; | ||
constructor(write: (data: Uint8Array) => void, extensionCodec?: ExtensionCodecType<ContextType>, context?: ContextType, maxDepth?: number, initialBufferSize?: number, sortKeys?: boolean, forceFloat32?: boolean, ignoreUndefined?: boolean, forceIntegerToFloat?: boolean); | ||
/** | ||
* @returns Encodes the object and returns a copy of the encoder's internal buffer. | ||
*/ | ||
encode(object: unknown): void; | ||
constructor(extensionCodec?: ExtensionCodecType<ContextType>, context?: ContextType, maxDepth?: number, bufferSize?: number, sortKeys?: boolean, forceFloat32?: boolean, ignoreUndefined?: boolean, forceIntegerToFloat?: boolean); | ||
encode(object: unknown): Iterable<Uint8Array>; | ||
private doEncode; | ||
@@ -28,3 +24,3 @@ private ensureBufferSizeToWrite; | ||
private encodeNumber; | ||
private writeStringHeader; | ||
private buildStringHeader; | ||
private encodeString; | ||
@@ -48,3 +44,13 @@ private encodeObject; | ||
private writeI64; | ||
private writeU8U8; | ||
private writeU8U16; | ||
private writeU8U32; | ||
private writeU8U64; | ||
private writeU8I8; | ||
private writeU8I16; | ||
private writeU8I32; | ||
private writeU8I64; | ||
private writeU8F32; | ||
private writeU8F64; | ||
private writeBuffer; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StreamEncoder = exports.DEFAULT_INITIAL_BUFFER_SIZE = exports.DEFAULT_MAX_DEPTH = void 0; | ||
exports.StreamEncoder = exports.DEFAULT_BUFFER_SIZE = exports.DEFAULT_MAX_DEPTH = void 0; | ||
const utf8_1 = require("./utils/utf8"); | ||
@@ -9,10 +9,9 @@ const ExtensionCodec_1 = require("./ExtensionCodec"); | ||
exports.DEFAULT_MAX_DEPTH = 100; | ||
exports.DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
exports.DEFAULT_BUFFER_SIZE = 2048; | ||
class StreamEncoder { | ||
constructor(write, extensionCodec = ExtensionCodec_1.ExtensionCodec.defaultCodec, context = undefined, maxDepth = exports.DEFAULT_MAX_DEPTH, initialBufferSize = exports.DEFAULT_INITIAL_BUFFER_SIZE, sortKeys = false, forceFloat32 = false, ignoreUndefined = false, forceIntegerToFloat = false) { | ||
this.write = write; | ||
constructor(extensionCodec = ExtensionCodec_1.ExtensionCodec.defaultCodec, context = undefined, maxDepth = exports.DEFAULT_MAX_DEPTH, bufferSize = exports.DEFAULT_BUFFER_SIZE, sortKeys = false, forceFloat32 = false, ignoreUndefined = false, forceIntegerToFloat = false) { | ||
this.extensionCodec = extensionCodec; | ||
this.context = context; | ||
this.maxDepth = maxDepth; | ||
this.initialBufferSize = initialBufferSize; | ||
this.bufferSize = bufferSize; | ||
this.sortKeys = sortKeys; | ||
@@ -22,10 +21,33 @@ this.forceFloat32 = forceFloat32; | ||
this.forceIntegerToFloat = forceIntegerToFloat; | ||
this.view = new DataView(new ArrayBuffer(this.initialBufferSize)); | ||
this.view = new DataView(new ArrayBuffer(2048)); | ||
this.bytes = new Uint8Array(this.view.buffer); | ||
} | ||
/** | ||
* @returns Encodes the object and returns a copy of the encoder's internal buffer. | ||
*/ | ||
encode(object) { | ||
this.doEncode(object, 1); | ||
*encode(object) { | ||
const buffer = new Uint8Array(this.bufferSize); | ||
let pos = 0; | ||
for (let chunk of this.doEncode(object, 1)) { | ||
if (chunk.length >= buffer.length) { | ||
if (pos >= 0) { | ||
yield buffer.slice(0, pos); | ||
} | ||
pos = 0; | ||
yield chunk.slice(0, pos); | ||
} | ||
else { | ||
while (chunk.length > 0) { | ||
const readSize = Math.min(buffer.length - pos, chunk.length); | ||
const read = chunk.subarray(0, readSize); | ||
buffer.set(read, pos); | ||
pos += read.length; | ||
if (pos === buffer.length) { | ||
yield buffer.slice(); | ||
pos = 0; | ||
} | ||
chunk = chunk.subarray(readSize, chunk.length); | ||
} | ||
} | ||
} | ||
if (pos >= 0) { | ||
yield buffer.subarray(0, pos); | ||
} | ||
} | ||
@@ -37,15 +59,15 @@ doEncode(object, depth) { | ||
if (object == null) { | ||
this.encodeNil(); | ||
return this.encodeNil(); | ||
} | ||
else if (typeof object === "boolean") { | ||
this.encodeBoolean(object); | ||
return this.encodeBoolean(object); | ||
} | ||
else if (typeof object === "number") { | ||
this.encodeNumber(object); | ||
return this.encodeNumber(object); | ||
} | ||
else if (typeof object === "string") { | ||
this.encodeString(object); | ||
return this.encodeString(object); | ||
} | ||
else { | ||
this.encodeObject(object, depth); | ||
return this.encodeObject(object, depth); | ||
} | ||
@@ -66,10 +88,10 @@ } | ||
encodeNil() { | ||
this.writeU8(0xc0); | ||
return this.writeU8(0xc0); | ||
} | ||
encodeBoolean(object) { | ||
if (object === false) { | ||
this.writeU8(0xc2); | ||
return this.writeU8(0xc2); | ||
} | ||
else { | ||
this.writeU8(0xc3); | ||
return this.writeU8(0xc3); | ||
} | ||
@@ -82,23 +104,19 @@ } | ||
// positive fixint | ||
this.writeU8(object); | ||
return this.writeU8(object); | ||
} | ||
else if (object < 0x100) { | ||
// uint 8 | ||
this.writeU8(0xcc); | ||
this.writeU8(object); | ||
return this.writeU8U8(0xcc, object); | ||
} | ||
else if (object < 0x10000) { | ||
// uint 16 | ||
this.writeU8(0xcd); | ||
this.writeU16(object); | ||
return this.writeU8U16(0xcd, object); | ||
} | ||
else if (object < 0x100000000) { | ||
// uint 32 | ||
this.writeU8(0xce); | ||
this.writeU32(object); | ||
return this.writeU8U32(0xce, object); | ||
} | ||
else { | ||
// uint 64 | ||
this.writeU8(0xcf); | ||
this.writeU64(object); | ||
return this.writeU8U64(0xcf, object); | ||
} | ||
@@ -109,23 +127,19 @@ } | ||
// negative fixint | ||
this.writeU8(0xe0 | (object + 0x20)); | ||
return this.writeU8(0xe0 | (object + 0x20)); | ||
} | ||
else if (object >= -0x80) { | ||
// int 8 | ||
this.writeU8(0xd0); | ||
this.writeI8(object); | ||
return this.writeU8I8(0xd0, object); | ||
} | ||
else if (object >= -0x8000) { | ||
// int 16 | ||
this.writeU8(0xd1); | ||
this.writeI16(object); | ||
return this.writeU8I16(0xd1, object); | ||
} | ||
else if (object >= -0x80000000) { | ||
// int 32 | ||
this.writeU8(0xd2); | ||
this.writeI32(object); | ||
return this.writeU8I32(0xd2, object); | ||
} | ||
else { | ||
// int 64 | ||
this.writeU8(0xd3); | ||
this.writeI64(object); | ||
return this.writeU8I64(0xd3, object); | ||
} | ||
@@ -138,31 +152,32 @@ } | ||
// float 32 | ||
this.writeU8(0xca); | ||
this.writeF32(object); | ||
return this.writeU8F32(0xca, object); | ||
} | ||
else { | ||
// float 64 | ||
this.writeU8(0xcb); | ||
this.writeF64(object); | ||
return this.writeU8F64(0xcb, object); | ||
} | ||
} | ||
} | ||
writeStringHeader(byteLength) { | ||
buildStringHeader(byteLength) { | ||
if (byteLength < 32) { | ||
// fixstr | ||
this.writeU8(0xa0 + byteLength); | ||
return [0xa0 + byteLength]; | ||
} | ||
else if (byteLength < 0x100) { | ||
// str 8 | ||
this.writeU8(0xd9); | ||
this.writeU8(byteLength); | ||
return [0xd9, byteLength]; | ||
} | ||
else if (byteLength < 0x10000) { | ||
// str 16 | ||
this.writeU8(0xda); | ||
this.writeU16(byteLength); | ||
const bytes = new DataView(new ArrayBuffer(3)); | ||
bytes.setUint8(0, 0xda); | ||
bytes.setUint16(1, byteLength); | ||
return new Uint8Array(bytes.buffer); | ||
} | ||
else if (byteLength < 0x100000000) { | ||
// str 32 | ||
this.writeU8(0xdb); | ||
this.writeU32(byteLength); | ||
const bytes = new DataView(new ArrayBuffer(5)); | ||
bytes.setUint8(0, 0xdb); | ||
bytes.setUint32(1, byteLength); | ||
return new Uint8Array(bytes.buffer); | ||
} | ||
@@ -179,5 +194,6 @@ else { | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
(0, utf8_1.utf8EncodeTE)(object, this.bytes, 0); | ||
this.writeBuffer(byteLength); | ||
const header = this.buildStringHeader(byteLength); | ||
this.bytes.set(header, 0); | ||
(0, utf8_1.utf8EncodeTE)(object, this.bytes, header.length); | ||
return this.writeBuffer(byteLength + header.length); | ||
} | ||
@@ -187,5 +203,6 @@ else { | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
(0, utf8_1.utf8EncodeJs)(object, this.bytes, 0); | ||
this.writeBuffer(byteLength); | ||
const header = this.buildStringHeader(byteLength); | ||
this.bytes.set(header, 0); | ||
(0, utf8_1.utf8EncodeJs)(object, this.bytes, header.length); | ||
return this.writeBuffer(byteLength + header.length); | ||
} | ||
@@ -197,12 +214,12 @@ } | ||
if (ext != null) { | ||
this.encodeExtension(ext); | ||
return this.encodeExtension(ext); | ||
} | ||
else if (Array.isArray(object)) { | ||
this.encodeArray(object, depth); | ||
return this.encodeArray(object, depth); | ||
} | ||
else if (ArrayBuffer.isView(object)) { | ||
this.encodeBinary(object); | ||
return this.encodeBinary(object); | ||
} | ||
else if (typeof object === "object") { | ||
this.encodeMap(object, depth); | ||
return this.encodeMap(object, depth); | ||
} | ||
@@ -214,18 +231,15 @@ else { | ||
} | ||
encodeBinary(object) { | ||
*encodeBinary(object) { | ||
const size = object.byteLength; | ||
if (size < 0x100) { | ||
// bin 8 | ||
this.writeU8(0xc4); | ||
this.writeU8(size); | ||
yield* this.writeU8U8(0xc4, size); | ||
} | ||
else if (size < 0x10000) { | ||
// bin 16 | ||
this.writeU8(0xc5); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xc5, size); | ||
} | ||
else if (size < 0x100000000) { | ||
// bin 32 | ||
this.writeU8(0xc6); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xc6, size); | ||
} | ||
@@ -236,19 +250,17 @@ else { | ||
const bytes = (0, typedArrays_1.ensureUint8Array)(object); | ||
this.writeU8a(bytes); | ||
yield* this.writeU8a(bytes); | ||
} | ||
encodeArray(object, depth) { | ||
*encodeArray(object, depth) { | ||
const size = object.length; | ||
if (size < 16) { | ||
// fixarray | ||
this.writeU8(0x90 + size); | ||
yield* this.writeU8(0x90 + size); | ||
} | ||
else if (size < 0x10000) { | ||
// array 16 | ||
this.writeU8(0xdc); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xdc, size); | ||
} | ||
else if (size < 0x100000000) { | ||
// array 32 | ||
this.writeU8(0xdd); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xdd, size); | ||
} | ||
@@ -259,3 +271,3 @@ else { | ||
for (const item of object) { | ||
this.doEncode(item, depth + 1); | ||
yield* this.doEncode(item, depth + 1); | ||
} | ||
@@ -272,3 +284,3 @@ } | ||
} | ||
encodeMap(object, depth) { | ||
*encodeMap(object, depth) { | ||
const keys = Object.keys(object); | ||
@@ -281,13 +293,11 @@ if (this.sortKeys) { | ||
// fixmap | ||
this.writeU8(0x80 + size); | ||
yield* this.writeU8(0x80 + size); | ||
} | ||
else if (size < 0x10000) { | ||
// map 16 | ||
this.writeU8(0xde); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xde, size); | ||
} | ||
else if (size < 0x100000000) { | ||
// map 32 | ||
this.writeU8(0xdf); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xdf, size); | ||
} | ||
@@ -300,43 +310,40 @@ else { | ||
if (!(this.ignoreUndefined && value === undefined)) { | ||
this.encodeString(key); | ||
this.doEncode(value, depth + 1); | ||
yield* this.encodeString(key); | ||
yield* this.doEncode(value, depth + 1); | ||
} | ||
} | ||
} | ||
encodeExtension(ext) { | ||
*encodeExtension(ext) { | ||
const size = ext.data.length; | ||
if (size === 1) { | ||
// fixext 1 | ||
this.writeU8(0xd4); | ||
yield* this.writeU8(0xd4); | ||
} | ||
else if (size === 2) { | ||
// fixext 2 | ||
this.writeU8(0xd5); | ||
yield* this.writeU8(0xd5); | ||
} | ||
else if (size === 4) { | ||
// fixext 4 | ||
this.writeU8(0xd6); | ||
yield* this.writeU8(0xd6); | ||
} | ||
else if (size === 8) { | ||
// fixext 8 | ||
this.writeU8(0xd7); | ||
yield* this.writeU8(0xd7); | ||
} | ||
else if (size === 16) { | ||
// fixext 16 | ||
this.writeU8(0xd8); | ||
yield* this.writeU8(0xd8); | ||
} | ||
else if (size < 0x100) { | ||
// ext 8 | ||
this.writeU8(0xc7); | ||
this.writeU8(size); | ||
yield* this.writeU8U8(0xc7, size); | ||
} | ||
else if (size < 0x10000) { | ||
// ext 16 | ||
this.writeU8(0xc8); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xc8, size); | ||
} | ||
else if (size < 0x100000000) { | ||
// ext 32 | ||
this.writeU8(0xc9); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xc9, size); | ||
} | ||
@@ -346,8 +353,8 @@ else { | ||
} | ||
this.writeI8(ext.type); | ||
this.writeU8a(ext.data); | ||
yield* this.writeI8(ext.type); | ||
yield* this.writeU8a(ext.data); | ||
} | ||
writeU8(value) { | ||
this.view.setUint8(0, value); | ||
this.writeBuffer(1); | ||
return this.writeBuffer(1); | ||
} | ||
@@ -358,45 +365,95 @@ writeU8a(values) { | ||
this.bytes.set(values, 0); | ||
this.writeBuffer(size); | ||
return this.writeBuffer(size); | ||
} | ||
writeI8(value) { | ||
this.view.setInt8(0, value); | ||
this.writeBuffer(1); | ||
return this.writeBuffer(1); | ||
} | ||
writeU16(value) { | ||
this.view.setUint16(0, value); | ||
this.writeBuffer(2); | ||
return this.writeBuffer(2); | ||
} | ||
writeI16(value) { | ||
this.view.setInt16(0, value); | ||
this.writeBuffer(2); | ||
return this.writeBuffer(2); | ||
} | ||
writeU32(value) { | ||
this.view.setUint32(0, value); | ||
this.writeBuffer(4); | ||
return this.writeBuffer(4); | ||
} | ||
writeI32(value) { | ||
this.view.setInt32(0, value); | ||
this.writeBuffer(4); | ||
return this.writeBuffer(4); | ||
} | ||
writeF32(value) { | ||
this.view.setFloat32(0, value); | ||
this.writeBuffer(4); | ||
return this.writeBuffer(4); | ||
} | ||
writeF64(value) { | ||
this.view.setFloat64(0, value); | ||
this.writeBuffer(8); | ||
return this.writeBuffer(8); | ||
} | ||
writeU64(value) { | ||
(0, int_1.setUint64)(this.view, 0, value); | ||
this.writeBuffer(8); | ||
return this.writeBuffer(8); | ||
} | ||
writeI64(value) { | ||
(0, int_1.setInt64)(this.view, 0, value); | ||
this.writeBuffer(8); | ||
return this.writeBuffer(8); | ||
} | ||
writeBuffer(length) { | ||
this.write(this.bytes.subarray(0, length)); | ||
writeU8U8(value, value2) { | ||
this.view.setUint8(0, value); | ||
this.view.setUint8(1, value2); | ||
return this.writeBuffer(2); | ||
} | ||
writeU8U16(u8, u16) { | ||
this.view.setUint8(0, u8); | ||
this.view.setUint16(1, u16); | ||
return this.writeBuffer(3); | ||
} | ||
writeU8U32(u8, u32) { | ||
this.view.setUint8(0, u8); | ||
this.view.setUint32(1, u32); | ||
return this.writeBuffer(5); | ||
} | ||
writeU8U64(u8, u64) { | ||
this.view.setUint8(0, u8); | ||
(0, int_1.setUint64)(this.view, 1, u64); | ||
return this.writeBuffer(9); | ||
} | ||
writeU8I8(value, value2) { | ||
this.view.setUint8(0, value); | ||
this.view.setInt8(1, value2); | ||
return this.writeBuffer(2); | ||
} | ||
writeU8I16(u8, i16) { | ||
this.view.setUint8(0, u8); | ||
this.view.setInt16(1, i16); | ||
return this.writeBuffer(3); | ||
} | ||
writeU8I32(u8, i32) { | ||
this.view.setUint8(0, u8); | ||
this.view.setInt32(1, i32); | ||
return this.writeBuffer(5); | ||
} | ||
writeU8I64(u8, i64) { | ||
this.view.setUint8(0, u8); | ||
(0, int_1.setInt64)(this.view, 1, i64); | ||
return this.writeBuffer(9); | ||
} | ||
writeU8F32(u8, f32) { | ||
this.view.setUint8(0, u8); | ||
this.view.setFloat32(1, f32); | ||
return this.writeBuffer(5); | ||
} | ||
writeU8F64(u8, f64) { | ||
this.view.setUint8(0, u8); | ||
this.view.setFloat64(1, f64); | ||
return this.writeBuffer(9); | ||
} | ||
*writeBuffer(length) { | ||
yield this.bytes.subarray(0, length); | ||
} | ||
} | ||
exports.StreamEncoder = StreamEncoder; | ||
//# sourceMappingURL=StreamEncoder.js.map |
@@ -1,1 +0,1 @@ | ||
export * from "./dist.es5+esm/index.mjs"; | ||
export * from "./dist.es2015+esm/index.mjs"; |
{ | ||
"name": "@eyhn/msgpack-stream", | ||
"version": "2.8.0", | ||
"version": "2.8.1", | ||
"description": "MessagePack for ECMA-262/JavaScript/TypeScript", | ||
@@ -8,5 +8,5 @@ "author": "The MessagePack community", | ||
"main": "./dist/index.js", | ||
"module": "./dist.es5+esm/index.mjs", | ||
"cdn": "./dist.es5+umd/msgpack.min.js", | ||
"unpkg": "./dist.es5+umd/msgpack.min.js", | ||
"module": "./dist.es2015+esm/index.mjs", | ||
"cdn": "./dist.es2015+umd/msgpack.min.js", | ||
"unpkg": "./dist.es2015+umd/msgpack.min.js", | ||
"types": "./dist/index.d.ts", | ||
@@ -16,3 +16,3 @@ "sideEffects": false, | ||
"build": "npm publish --dry-run", | ||
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.es5+esm.json && ts-node tools/esmify.ts dist.es5+esm/*.js dist.es5+esm/*/*.js", | ||
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.es2015+esm.json && ts-node tools/esmify.ts dist.es2015+esm/*.js dist.es2015+esm/*/*.js", | ||
"prepublishOnly": "run-p 'test:dist:*' && npm run test:browser", | ||
@@ -23,3 +23,3 @@ "clean": "rimraf build dist dist.*", | ||
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.ts'", | ||
"test:dist:purejs": "TS_NODE_PROJECT=tsconfig.test-dist-es5-purejs.json npm run test:purejs -- --reporter=dot", | ||
"test:dist:purejs": "TS_NODE_PROJECT=tsconfig.test-dist-es2015-purejs.json npm run test:purejs -- --reporter=dot", | ||
"test:cover": "npm run cover:clean && npm-run-all 'test:cover:*' && npm run cover:report", | ||
@@ -100,5 +100,5 @@ "test:cover:purejs": "npx nyc --no-clean npm run test:purejs", | ||
"dist/**/*.*", | ||
"dist.es5+umd/**/*.*", | ||
"dist.es5+esm/**/*.*" | ||
"dist.es2015+umd/**/*.*", | ||
"dist.es2015+esm/**/*.*" | ||
] | ||
} |
# MessagePack for JavaScript/ECMA-262 <!-- omit in toc --> | ||
[![npm version](https://img.shields.io/npm/v/@msgpack/msgpack.svg)](https://www.npmjs.com/package/@msgpack/msgpack) ![CI](https://github.com/msgpack/msgpack-javascript/workflows/CI/badge.svg) [![codecov](https://codecov.io/gh/msgpack/msgpack-javascript/branch/master/graphs/badge.svg)](https://codecov.io/gh/msgpack/msgpack-javascript) [![minzip](https://badgen.net/bundlephobia/minzip/@msgpack/msgpack)](https://bundlephobia.com/result?p=@msgpack/msgpack) [![tree-shaking](https://badgen.net/bundlephobia/tree-shaking/@msgpack/msgpack)](https://bundlephobia.com/result?p=@msgpack/msgpack) | ||
[![npm version](https://img.shields.io/npm/v/@eyhn/msgpack-stream.svg)](https://www.npmjs.com/package/@eyhn/msgpack-stream) ![CI](https://github.com/msgpack/msgpack-javascript/workflows/CI/badge.svg) [![codecov](https://codecov.io/gh/msgpack/msgpack-javascript/branch/master/graphs/badge.svg)](https://codecov.io/gh/msgpack/msgpack-javascript) [![minzip](https://badgen.net/bundlephobia/minzip/@eyhn/msgpack-stream)](https://bundlephobia.com/result?p=@eyhn/msgpack-stream) [![tree-shaking](https://badgen.net/bundlephobia/tree-shaking/@eyhn/msgpack-stream)](https://bundlephobia.com/result?p=@eyhn/msgpack-stream) | ||
@@ -17,3 +17,3 @@ This is a JavaScript/ECMA-262 implementation of **MessagePack**, an efficient binary serilization format: | ||
import { deepStrictEqual } from "assert"; | ||
import { encode, decode } from "@msgpack/msgpack"; | ||
import { encode, decode } from "@eyhn/msgpack-stream"; | ||
@@ -76,6 +76,6 @@ const object = { | ||
This library is published to `npmjs.com` as [@msgpack/msgpack](https://www.npmjs.com/package/@msgpack/msgpack). | ||
This library is published to `npmjs.com` as [@eyhn/msgpack-stream](https://www.npmjs.com/package/@eyhn/msgpack-stream). | ||
```shell | ||
npm install @msgpack/msgpack | ||
npm install @eyhn/msgpack-stream | ||
``` | ||
@@ -92,3 +92,3 @@ | ||
```typescript | ||
import { encode } from "@msgpack/msgpack"; | ||
import { encode } from "@eyhn/msgpack-stream"; | ||
@@ -102,3 +102,3 @@ const encoded: Uint8Array = encode({ foo: "bar" }); | ||
```typescript | ||
import { encode } from "@msgpack/msgpack"; | ||
import { encode } from "@eyhn/msgpack-stream"; | ||
@@ -136,3 +136,3 @@ const encoded: Uint8Array = encode({ foo: "bar" }); | ||
```typescript | ||
import { decode } from "@msgpack/msgpack"; | ||
import { decode } from "@eyhn/msgpack-stream"; | ||
@@ -169,3 +169,3 @@ const encoded: Uint8Array; | ||
```typescript | ||
import { decode } from "@msgpack/msgpack"; | ||
import { decode } from "@eyhn/msgpack-stream"; | ||
@@ -190,3 +190,3 @@ const encoded: Uint8Array; | ||
```typescript | ||
import { decodeAsync } from "@msgpack/msgpack"; | ||
import { decodeAsync } from "@eyhn/msgpack-stream"; | ||
@@ -210,3 +210,3 @@ const MSGPACK_TYPE = "application/x-msgpack"; | ||
```typescript | ||
import { decodeArrayStream } from "@msgpack/msgpack"; | ||
import { decodeArrayStream } from "@eyhn/msgpack-stream"; | ||
@@ -230,3 +230,3 @@ const stream: AsyncIterator<Uint8Array>; | ||
```typescript | ||
import { decodeMultiStream } from "@msgpack/msgpack"; | ||
import { decodeMultiStream } from "@eyhn/msgpack-stream"; | ||
@@ -249,3 +249,3 @@ const stream: AsyncIterator<Uint8Array>; | ||
import { deepStrictEqual } from "assert"; | ||
import { Encoder, Decoder } from "@msgpack/msgpack"; | ||
import { Encoder, Decoder } from "@eyhn/msgpack-stream"; | ||
@@ -271,3 +271,3 @@ const encoder = new Encoder(); | ||
```typescript | ||
import { encode, decode, ExtensionCodec } from "@msgpack/msgpack"; | ||
import { encode, decode, ExtensionCodec } from "@eyhn/msgpack-stream"; | ||
@@ -321,3 +321,3 @@ const extensionCodec = new ExtensionCodec(); | ||
```typescript | ||
import { encode, decode, ExtensionCodec } from "@msgpack/msgpack"; | ||
import { encode, decode, ExtensionCodec } from "@eyhn/msgpack-stream"; | ||
@@ -353,3 +353,3 @@ class MyContext { | ||
// and later | ||
import { encode, decode } from "@msgpack/msgpack"; | ||
import { encode, decode } from "@eyhn/msgpack-stream"; | ||
@@ -368,3 +368,3 @@ const context = new MyContext(); | ||
import { deepStrictEqual } from "assert"; | ||
import { encode, decode, ExtensionCodec } from "@msgpack/msgpack"; | ||
import { encode, decode, ExtensionCodec } from "@eyhn/msgpack-stream"; | ||
@@ -414,3 +414,3 @@ const BIGINT_EXT_TYPE = 0; // Any in 0-127 | ||
decodeTimestampToTimeSpec, | ||
} from "@msgpack/msgpack"; | ||
} from "@eyhn/msgpack-stream"; | ||
@@ -519,3 +519,3 @@ const extensionCodec = new ExtensionCodec(); | ||
NodeJS before v10 will work by importing `@msgpack/msgpack/dist.es5+umd/msgpack`. | ||
NodeJS before v10 will work by importing `@eyhn/msgpack-stream/dist.es2015+umd/msgpack`. | ||
@@ -544,6 +544,6 @@ ### TypeScript Compiler / Type Definitions | ||
obj = require("msgpack-lite").decode(buf); | 246200 | 5001 | 49230 | ||
buf = require("@msgpack/msgpack").encode(obj); | 843300 | 5000 | 168660 | ||
obj = require("@msgpack/msgpack").decode(buf); | 489300 | 5000 | 97860 | ||
buf = /* @msgpack/msgpack */ encoder.encode(obj); | 1154200 | 5000 | 230840 | ||
obj = /* @msgpack/msgpack */ decoder.decode(buf); | 448900 | 5000 | 89780 | ||
buf = require("@eyhn/msgpack-stream").encode(obj); | 843300 | 5000 | 168660 | ||
obj = require("@eyhn/msgpack-stream").decode(buf); | 489300 | 5000 | 97860 | ||
buf = /* @eyhn/msgpack-stream */ encoder.encode(obj); | 1154200 | 5000 | 230840 | ||
obj = /* @eyhn/msgpack-stream */ decoder.decode(buf); | 448900 | 5000 | 89780 | ||
@@ -556,9 +556,9 @@ Note that `JSON` cases use `Buffer` to emulate I/O where a JavaScript string must be converted into a byte array encoded in UTF-8, whereas MessagePack modules deal with byte arrays. | ||
The NPM package distributed in npmjs.com includes both ES2015+ and ES5 files: | ||
The NPM package distributed in npmjs.com includes both ES2015+ and ES2015 files: | ||
* `dist/` is compiled into ES2019 with CommomJS, provided for NodeJS v10 | ||
* `dist.es5+umd/` is compiled into ES5 with UMD | ||
* `dist.es5+umd/msgpack.min.js` - the minified file | ||
* `dist.es5+umd/msgpack.js` - the non-minified file | ||
* `dist.es5+esm/` is compiled into ES5 with ES modules, provided for webpack-like bundlers and NodeJS's ESM-mode | ||
* `dist.es2015+umd/` is compiled into ES2015 with UMD | ||
* `dist.es2015+umd/msgpack.min.js` - the minified file | ||
* `dist.es2015+umd/msgpack.js` - the non-minified file | ||
* `dist.es2015+esm/` is compiled into ES2015 with ES modules, provided for webpack-like bundlers and NodeJS's ESM-mode | ||
@@ -572,3 +572,3 @@ If you use NodeJS and/or webpack, their module resolvers use the suitable one automatically. | ||
```html | ||
<script crossorigin src="https://unpkg.com/@msgpack/msgpack"></script> | ||
<script crossorigin src="https://unpkg.com/@eyhn/msgpack-stream"></script> | ||
``` | ||
@@ -575,0 +575,0 @@ |
@@ -11,2 +11,9 @@ import { Decoder } from "./Decoder"; | ||
/** | ||
* The size of the stream buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
streamBufferSize: number; | ||
/** | ||
* Maximum string length. | ||
@@ -96,5 +103,5 @@ * | ||
export function decodeStream<ContextType = undefined>( | ||
read: (data: Uint8Array) => number, | ||
read: AsyncIterable<Uint8Array>, | ||
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any, | ||
): unknown { | ||
): Promise<unknown> { | ||
const decoder = new StreamDecoder( | ||
@@ -101,0 +108,0 @@ read, |
@@ -25,2 +25,9 @@ import { Encoder } from "./Encoder"; | ||
/** | ||
* The size of the stream buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
streamBufferSize: number; | ||
/** | ||
* If `true`, the keys of an object is sorted. In other words, the encoded | ||
@@ -87,11 +94,9 @@ * binary is canonical and thus comparable to another encoded binary. | ||
value: unknown, | ||
write: (data: Uint8Array) => void, | ||
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any, | ||
): void { | ||
): Iterable<Uint8Array> { | ||
const encoder = new StreamEncoder( | ||
write, | ||
options.extensionCodec, | ||
(options as typeof options & { context: any }).context, | ||
options.maxDepth, | ||
options.initialBufferSize, | ||
options.streamBufferSize, | ||
options.sortKeys, | ||
@@ -102,3 +107,3 @@ options.forceFloat32, | ||
); | ||
encoder.encode(value); | ||
return encoder.encode(value); | ||
} |
@@ -54,16 +54,16 @@ import { prettyByte } from "./utils/prettyByte"; | ||
const sharedCachedKeyDecoder = new CachedKeyDecoder(); | ||
const MORE_DATA = new DataViewIndexOutOfBoundsError("Insufficient data"); | ||
const sharedCachedKeyDecoder = new CachedKeyDecoder(); | ||
export const DEFAULT_BUFFER_SIZE = 2048; | ||
export const DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
export class StreamDecoder<ContextType = undefined> { | ||
private headByte = HEAD_BYTE_REQUIRED; | ||
private view = new DataView(new ArrayBuffer(DEFAULT_INITIAL_BUFFER_SIZE)); | ||
private bytes = new Uint8Array(this.view.buffer); | ||
private readonly stack: Array<StackState> = []; | ||
private readonly readStream: AsyncIterator<Uint8Array, any, number | undefined>; | ||
private buffer: Uint8Array | null = null; | ||
public constructor( | ||
private readonly read: (data: Uint8Array) => number, | ||
private readonly readIterable: AsyncIterable<Uint8Array>, | ||
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any, | ||
@@ -77,4 +77,6 @@ private readonly context: ContextType = undefined as any, | ||
private readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder, | ||
private readonly initialBufferSize = sharedCachedKeyDecoder, | ||
) {} | ||
private readonly bufferSize: number = DEFAULT_BUFFER_SIZE, | ||
) { | ||
this.readStream = readIterable[Symbol.asyncIterator](); | ||
} | ||
@@ -96,3 +98,3 @@ private reinitializeState() { | ||
*/ | ||
public decode(): unknown { | ||
public decode(): Promise<unknown> { | ||
this.reinitializeState(); | ||
@@ -103,13 +105,13 @@ | ||
public *decodeMulti(): Generator<unknown, void, unknown> { | ||
public async *decodeMulti(): AsyncGenerator<unknown> { | ||
this.reinitializeState(); | ||
while (true) { | ||
yield this.doDecodeSync(); | ||
yield await this.doDecodeSync(); | ||
} | ||
} | ||
private doDecodeSync(): unknown { | ||
private async doDecodeSync() { | ||
DECODE: while (true) { | ||
const headByte = this.readHeadByte(); | ||
const headByte = this.readHeadByteFromBuffer() ?? (await this.readHeadByte()); | ||
let object: unknown; | ||
@@ -147,3 +149,3 @@ | ||
const byteLength = headByte - 0xa0; | ||
object = this.decodeUtf8String(byteLength); | ||
object = this.decodeUtf8StringFromBuffer(byteLength) ?? (await this.decodeUtf8String(byteLength)); | ||
} | ||
@@ -161,45 +163,45 @@ } else if (headByte === 0xc0) { | ||
// float 32 | ||
object = this.readF32(); | ||
object = this.readF32FromBuffer() ?? (await this.readF32()); | ||
} else if (headByte === 0xcb) { | ||
// float 64 | ||
object = this.readF64(); | ||
object = this.readF64FromBuffer() ?? (await this.readF64()); | ||
} else if (headByte === 0xcc) { | ||
// uint 8 | ||
object = this.readU8(); | ||
object = this.readU8FromBuffer() ?? (await this.readU8()); | ||
} else if (headByte === 0xcd) { | ||
// uint 16 | ||
object = this.readU16(); | ||
object = this.readU16FromBuffer() ?? (await this.readU16()); | ||
} else if (headByte === 0xce) { | ||
// uint 32 | ||
object = this.readU32(); | ||
object = this.readU32FromBuffer() ?? (await this.readU32()); | ||
} else if (headByte === 0xcf) { | ||
// uint 64 | ||
object = this.readU64(); | ||
object = this.readU64FromBuffer() ?? (await this.readU64()); | ||
} else if (headByte === 0xd0) { | ||
// int 8 | ||
object = this.readI8(); | ||
object = this.readI8FromBuffer() ?? (await this.readI8()); | ||
} else if (headByte === 0xd1) { | ||
// int 16 | ||
object = this.readI16(); | ||
object = this.readI16FromBuffer() ?? (await this.readI16()); | ||
} else if (headByte === 0xd2) { | ||
// int 32 | ||
object = this.readI32(); | ||
object = this.readI32FromBuffer() ?? (await this.readI32()); | ||
} else if (headByte === 0xd3) { | ||
// int 64 | ||
object = this.readI64(); | ||
object = this.readI64FromBuffer() ?? (await this.readI64()); | ||
} else if (headByte === 0xd9) { | ||
// str 8 | ||
const byteLength = this.readU8(); | ||
object = this.decodeUtf8String(byteLength); | ||
const byteLength = this.readU8FromBuffer() ?? (await this.readU8()); | ||
object = this.decodeUtf8StringFromBuffer(byteLength) ?? (await this.decodeUtf8String(byteLength)); | ||
} else if (headByte === 0xda) { | ||
// str 16 | ||
const byteLength = this.readU16(); | ||
object = this.decodeUtf8String(byteLength); | ||
const byteLength = this.readU16FromBuffer() ?? (await this.readU16()); | ||
object = this.decodeUtf8StringFromBuffer(byteLength) ?? (await this.decodeUtf8String(byteLength)); | ||
} else if (headByte === 0xdb) { | ||
// str 32 | ||
const byteLength = this.readU32(); | ||
object = this.decodeUtf8String(byteLength); | ||
const byteLength = this.readU32FromBuffer() ?? (await this.readU32()); | ||
object = this.decodeUtf8StringFromBuffer(byteLength) ?? (await this.decodeUtf8String(byteLength)); | ||
} else if (headByte === 0xdc) { | ||
// array 16 | ||
const size = this.readU16(); | ||
const size = this.readU16FromBuffer() ?? (await this.readU16()); | ||
if (size !== 0) { | ||
@@ -214,3 +216,3 @@ this.pushArrayState(size); | ||
// array 32 | ||
const size = this.readU32(); | ||
const size = this.readU32FromBuffer() ?? (await this.readU32()); | ||
if (size !== 0) { | ||
@@ -225,3 +227,3 @@ this.pushArrayState(size); | ||
// map 16 | ||
const size = this.readU16(); | ||
const size = this.readU16FromBuffer() ?? (await this.readU16()); | ||
if (size !== 0) { | ||
@@ -236,3 +238,3 @@ this.pushMapState(size); | ||
// map 32 | ||
const size = this.readU32(); | ||
const size = this.readU32FromBuffer() ?? (await this.readU32()); | ||
if (size !== 0) { | ||
@@ -247,39 +249,39 @@ this.pushMapState(size); | ||
// bin 8 | ||
const size = this.readU8(); | ||
object = this.decodeBinary(size); | ||
const size = this.readU8FromBuffer() ?? (await this.readU8()); | ||
object = await this.decodeBinary(size); | ||
} else if (headByte === 0xc5) { | ||
// bin 16 | ||
const size = this.readU16(); | ||
object = this.decodeBinary(size); | ||
const size = this.readU16FromBuffer() ?? (await this.readU16()); | ||
object = await this.decodeBinary(size); | ||
} else if (headByte === 0xc6) { | ||
// bin 32 | ||
const size = this.readU32(); | ||
object = this.decodeBinary(size); | ||
const size = this.readU32FromBuffer() ?? (await this.readU32()); | ||
object = await this.decodeBinary(size); | ||
} else if (headByte === 0xd4) { | ||
// fixext 1 | ||
object = this.decodeExtension(1); | ||
object = await this.decodeExtension(1); | ||
} else if (headByte === 0xd5) { | ||
// fixext 2 | ||
object = this.decodeExtension(2); | ||
object = await this.decodeExtension(2); | ||
} else if (headByte === 0xd6) { | ||
// fixext 4 | ||
object = this.decodeExtension(4); | ||
object = await this.decodeExtension(4); | ||
} else if (headByte === 0xd7) { | ||
// fixext 8 | ||
object = this.decodeExtension(8); | ||
object = await this.decodeExtension(8); | ||
} else if (headByte === 0xd8) { | ||
// fixext 16 | ||
object = this.decodeExtension(16); | ||
object = await this.decodeExtension(16); | ||
} else if (headByte === 0xc7) { | ||
// ext 8 | ||
const size = this.readU8(); | ||
object = this.decodeExtension(size); | ||
const size = this.readU8FromBuffer() ?? (await this.readU8()); | ||
object = await this.decodeExtension(size); | ||
} else if (headByte === 0xc8) { | ||
// ext 16 | ||
const size = this.readU16(); | ||
object = this.decodeExtension(size); | ||
const size = this.readU16FromBuffer() ?? (await this.readU16()); | ||
object = await this.decodeExtension(size); | ||
} else if (headByte === 0xc9) { | ||
// ext 32 | ||
const size = this.readU32(); | ||
object = this.decodeExtension(size); | ||
const size = this.readU32FromBuffer() ?? (await this.readU32()); | ||
object = await this.decodeExtension(size); | ||
} else { | ||
@@ -336,6 +338,10 @@ throw new DecodeError(`Unrecognized type byte: ${prettyByte(headByte)}`); | ||
private readHeadByte(): number { | ||
private readHeadByteFromBuffer() { | ||
const b = this.readU8FromBuffer(); | ||
if (b === null) { | ||
return null; | ||
} | ||
if (this.headByte === HEAD_BYTE_REQUIRED) { | ||
this.headByte = this.readU8(); | ||
// console.log("headByte", prettyByte(this.headByte)); | ||
this.headByte = b; | ||
} | ||
@@ -346,2 +352,10 @@ | ||
private async readHeadByte() { | ||
if (this.headByte === HEAD_BYTE_REQUIRED) { | ||
this.headByte = await this.readU8(); | ||
} | ||
return this.headByte; | ||
} | ||
private complete(): void { | ||
@@ -351,20 +365,2 @@ this.headByte = HEAD_BYTE_REQUIRED; | ||
private readArraySize(): number { | ||
const headByte = this.readHeadByte(); | ||
switch (headByte) { | ||
case 0xdc: | ||
return this.readU16(); | ||
case 0xdd: | ||
return this.readU32(); | ||
default: { | ||
if (headByte < 0xa0) { | ||
return headByte - 0x90; | ||
} else { | ||
throw new DecodeError(`Unrecognized array type byte: ${prettyByte(headByte)}`); | ||
} | ||
} | ||
} | ||
} | ||
private pushMapState(size: number) { | ||
@@ -397,3 +393,3 @@ if (size > this.maxMapLength) { | ||
private decodeUtf8String(byteLength: number): string { | ||
private decodeUtf8StringFromBuffer(byteLength: number) { | ||
if (byteLength > this.maxStrLength) { | ||
@@ -405,3 +401,6 @@ throw new DecodeError( | ||
const bytes = this.readBytes(byteLength); | ||
const bytes = this.readBytesFromBuffer(byteLength); | ||
if (bytes === null) { | ||
return null; | ||
} | ||
let object: string; | ||
@@ -418,2 +417,21 @@ if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) { | ||
private async decodeUtf8String(byteLength: number) { | ||
if (byteLength > this.maxStrLength) { | ||
throw new DecodeError( | ||
`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`, | ||
); | ||
} | ||
const bytes = await this.readBytes(byteLength); | ||
let object: string; | ||
if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) { | ||
object = this.keyDecoder.decode(bytes, 0, byteLength); | ||
} else if (byteLength > TEXT_DECODER_THRESHOLD) { | ||
object = utf8DecodeTD(bytes, 0, byteLength); | ||
} else { | ||
object = utf8DecodeJs(bytes, 0, byteLength); | ||
} | ||
return object; | ||
} | ||
private stateIsMapKey(): boolean { | ||
@@ -427,3 +445,3 @@ if (this.stack.length > 0) { | ||
private decodeBinary(byteLength: number): Uint8Array { | ||
private async decodeBinary(byteLength: number) { | ||
if (byteLength > this.maxBinLength) { | ||
@@ -433,7 +451,7 @@ throw new DecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`); | ||
const object = this.readBytes(byteLength); | ||
const object = await this.readBytes(byteLength); | ||
return object; | ||
} | ||
private decodeExtension(size: number): unknown { | ||
private async decodeExtension(size: number) { | ||
if (size > this.maxExtLength) { | ||
@@ -443,96 +461,221 @@ throw new DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`); | ||
const extType = this.readI8(); | ||
const data = this.decodeBinary(size); | ||
const extType = await this.readI8(); | ||
const data = await this.decodeBinary(size); | ||
return this.extensionCodec.decode(data, extType, this.context); | ||
} | ||
private readU8(): number { | ||
this.readBytes(1); | ||
const value = this.view.getUint8(0); | ||
private async readU8() { | ||
const data = await this.readBytes(1); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint8(0); | ||
return value; | ||
} | ||
private readI8(): number { | ||
this.readBytes(1); | ||
const value = this.view.getInt8(0); | ||
private async readI8() { | ||
const data = await this.readBytes(1); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt8(0); | ||
return value; | ||
} | ||
private readU16(): number { | ||
this.readBytes(2); | ||
const value = this.view.getUint16(0); | ||
private async readU16() { | ||
const data = await this.readBytes(2); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0); | ||
return value; | ||
} | ||
private readI16(): number { | ||
this.readBytes(2); | ||
const value = this.view.getInt16(0); | ||
private async readI16() { | ||
const data = await this.readBytes(2); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt16(0); | ||
return value; | ||
} | ||
private readU32(): number { | ||
this.readBytes(4); | ||
const value = this.view.getUint32(0); | ||
private async readU32() { | ||
const data = await this.readBytes(4); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint32(0); | ||
return value; | ||
} | ||
private readI32(): number { | ||
this.readBytes(4); | ||
const value = this.view.getInt32(0); | ||
private async readI32() { | ||
const data = await this.readBytes(4); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt32(0); | ||
return value; | ||
} | ||
private readU64(): number { | ||
this.readBytes(8); | ||
const value = getUint64(this.view, 0); | ||
private async readU64() { | ||
const data = await this.readBytes(8); | ||
const value = getUint64(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
private readI64(): number { | ||
this.readBytes(8); | ||
const value = getInt64(this.view, 0); | ||
private async readI64() { | ||
const data = await this.readBytes(8); | ||
const value = getInt64(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
private readF32() { | ||
this.readBytes(4); | ||
const value = this.view.getFloat32(0); | ||
private async readF32() { | ||
const data = await this.readBytes(4); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat32(0); | ||
return value; | ||
} | ||
private readF64() { | ||
this.readBytes(8); | ||
const value = this.view.getFloat64(0); | ||
private async readF64() { | ||
const data = await this.readBytes(8); | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat64(0); | ||
return value; | ||
} | ||
private readBytes(length: number) { | ||
this.ensureBufferSizeToWrite(length); | ||
let pos = 0; | ||
private readU8FromBuffer() { | ||
const data = this.readBytesFromBuffer(1); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint8(0); | ||
return value; | ||
} | ||
while (pos < length) { | ||
const read = this.read(this.bytes.subarray(pos, length)); | ||
if (read <= 0) { | ||
throw MORE_DATA; | ||
} | ||
pos += read; | ||
private readI8FromBuffer() { | ||
const data = this.readBytesFromBuffer(1); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt8(0); | ||
return value; | ||
} | ||
return this.bytes.subarray(0, length); | ||
private readU16FromBuffer() { | ||
const data = this.readBytesFromBuffer(2); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0); | ||
return value; | ||
} | ||
private ensureBufferSizeToWrite(sizeToWrite: number) { | ||
if (this.view.byteLength < sizeToWrite) { | ||
this.resizeBuffer(this.view.byteLength * 2); | ||
private readI16FromBuffer() { | ||
const data = this.readBytesFromBuffer(2); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt16(0); | ||
return value; | ||
} | ||
private resizeBuffer(newSize: number) { | ||
const newBuffer = new ArrayBuffer(newSize); | ||
const newBytes = new Uint8Array(newBuffer); | ||
const newView = new DataView(newBuffer); | ||
private readU32FromBuffer() { | ||
const data = this.readBytesFromBuffer(4); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getUint32(0); | ||
return value; | ||
} | ||
this.view = newView; | ||
this.bytes = newBytes; | ||
private readI32FromBuffer() { | ||
const data = this.readBytesFromBuffer(4); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getInt32(0); | ||
return value; | ||
} | ||
private readU64FromBuffer() { | ||
const data = this.readBytesFromBuffer(8); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = getUint64(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
private readI64FromBuffer() { | ||
const data = this.readBytesFromBuffer(8); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = getInt64(new DataView(data.buffer, data.byteOffset, data.byteLength), 0); | ||
return value; | ||
} | ||
private readF32FromBuffer() { | ||
const data = this.readBytesFromBuffer(4); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat32(0); | ||
return value; | ||
} | ||
private readF64FromBuffer() { | ||
const data = this.readBytesFromBuffer(8); | ||
if (!data) { | ||
return null; | ||
} | ||
const value = new DataView(data.buffer, data.byteOffset, data.byteLength).getFloat64(0); | ||
return value; | ||
} | ||
private readBytesFromBuffer(length: number): Uint8Array | null { | ||
if (this.buffer && this.buffer.length >= length) { | ||
const result = this.buffer.subarray(0, length); | ||
this.buffer = this.buffer.subarray(length); | ||
return result; | ||
} | ||
return null; | ||
} | ||
private async readBytes(length: number) { | ||
if (this.buffer && length <= this.buffer.length) { | ||
const result = this.buffer.subarray(0, length); | ||
this.buffer = this.buffer.subarray(length); | ||
return result; | ||
} else { | ||
const hasData = this.buffer?.length ?? 0; | ||
const needData = length - hasData; | ||
const result = new Uint8Array(length); | ||
if (this.buffer) { | ||
result.set(this.buffer, 0); | ||
} | ||
if (needData < this.bufferSize) { | ||
const next = await this.readToBuffer(this.bufferSize); | ||
if (next.length < needData) { | ||
throw MORE_DATA; | ||
} | ||
result.set(next.subarray(0, needData), hasData); | ||
this.buffer = this.buffer!.subarray(needData); | ||
} else { | ||
const next = await this.readToBuffer(needData); | ||
if (next.length < needData) { | ||
throw MORE_DATA; | ||
} | ||
result.set(next, hasData); | ||
this.buffer = new Uint8Array(); | ||
} | ||
return result; | ||
} | ||
} | ||
private async readToBuffer(length: number) { | ||
const bytes: Array<Uint8Array> = []; | ||
let size = 0; | ||
while (size < length) { | ||
const result = await this.readStream.next(this.bufferSize); | ||
if (!result.done) { | ||
bytes.push(result.value); | ||
size += result.value.length; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (bytes.length == 1) { | ||
this.buffer = bytes[0]!; | ||
return bytes[0]!; | ||
} | ||
this.buffer = new Uint8Array(size); | ||
let pos = 0; | ||
for (const chunk of bytes) { | ||
this.buffer.set(chunk, pos); | ||
pos += chunk.length; | ||
} | ||
return this.buffer; | ||
} | ||
} |
@@ -8,14 +8,13 @@ import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8"; | ||
export const DEFAULT_MAX_DEPTH = 100; | ||
export const DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
export const DEFAULT_BUFFER_SIZE = 2048; | ||
export class StreamEncoder<ContextType = undefined> { | ||
private view = new DataView(new ArrayBuffer(this.initialBufferSize)); | ||
private view = new DataView(new ArrayBuffer(2048)); | ||
private bytes = new Uint8Array(this.view.buffer); | ||
public constructor( | ||
private readonly write: (data: Uint8Array) => void, | ||
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any, | ||
private readonly context: ContextType = undefined as any, | ||
private readonly maxDepth = DEFAULT_MAX_DEPTH, | ||
private readonly initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE, | ||
private readonly bufferSize = DEFAULT_BUFFER_SIZE, | ||
private readonly sortKeys = false, | ||
@@ -27,10 +26,34 @@ private readonly forceFloat32 = false, | ||
/** | ||
* @returns Encodes the object and returns a copy of the encoder's internal buffer. | ||
*/ | ||
public encode(object: unknown) { | ||
this.doEncode(object, 1); | ||
public *encode(object: unknown): Iterable<Uint8Array> { | ||
const buffer = new Uint8Array(this.bufferSize); | ||
let pos = 0; | ||
for (let chunk of this.doEncode(object, 1)) { | ||
if (chunk.length >= buffer.length) { | ||
if (pos >= 0) { | ||
yield buffer.slice(0, pos); | ||
} | ||
pos = 0; | ||
yield chunk.slice(0, pos); | ||
} else { | ||
while (chunk.length > 0) { | ||
const readSize = Math.min(buffer.length - pos, chunk.length); | ||
const read = chunk.subarray(0, readSize); | ||
buffer.set(read, pos); | ||
pos += read.length; | ||
if (pos === buffer.length) { | ||
yield buffer.slice(); | ||
pos = 0; | ||
} | ||
chunk = chunk.subarray(readSize, chunk.length); | ||
} | ||
} | ||
} | ||
if (pos >= 0) { | ||
yield buffer.subarray(0, pos); | ||
} | ||
} | ||
private doEncode(object: unknown, depth: number): void { | ||
private doEncode(object: unknown, depth: number): Iterable<Uint8Array> { | ||
if (depth > this.maxDepth) { | ||
@@ -41,11 +64,11 @@ throw new Error(`Too deep objects in depth ${depth}`); | ||
if (object == null) { | ||
this.encodeNil(); | ||
return this.encodeNil(); | ||
} else if (typeof object === "boolean") { | ||
this.encodeBoolean(object); | ||
return this.encodeBoolean(object); | ||
} else if (typeof object === "number") { | ||
this.encodeNumber(object); | ||
return this.encodeNumber(object); | ||
} else if (typeof object === "string") { | ||
this.encodeString(object); | ||
return this.encodeString(object); | ||
} else { | ||
this.encodeObject(object, depth); | ||
return this.encodeObject(object, depth); | ||
} | ||
@@ -70,3 +93,3 @@ } | ||
private encodeNil() { | ||
this.writeU8(0xc0); | ||
return this.writeU8(0xc0); | ||
} | ||
@@ -76,7 +99,8 @@ | ||
if (object === false) { | ||
this.writeU8(0xc2); | ||
return this.writeU8(0xc2); | ||
} else { | ||
this.writeU8(0xc3); | ||
return this.writeU8(0xc3); | ||
} | ||
} | ||
private encodeNumber(object: number) { | ||
@@ -87,19 +111,15 @@ if (Number.isSafeInteger(object) && !this.forceIntegerToFloat) { | ||
// positive fixint | ||
this.writeU8(object); | ||
return this.writeU8(object); | ||
} else if (object < 0x100) { | ||
// uint 8 | ||
this.writeU8(0xcc); | ||
this.writeU8(object); | ||
return this.writeU8U8(0xcc, object); | ||
} else if (object < 0x10000) { | ||
// uint 16 | ||
this.writeU8(0xcd); | ||
this.writeU16(object); | ||
return this.writeU8U16(0xcd, object); | ||
} else if (object < 0x100000000) { | ||
// uint 32 | ||
this.writeU8(0xce); | ||
this.writeU32(object); | ||
return this.writeU8U32(0xce, object); | ||
} else { | ||
// uint 64 | ||
this.writeU8(0xcf); | ||
this.writeU64(object); | ||
return this.writeU8U64(0xcf, object); | ||
} | ||
@@ -109,19 +129,15 @@ } else { | ||
// negative fixint | ||
this.writeU8(0xe0 | (object + 0x20)); | ||
return this.writeU8(0xe0 | (object + 0x20)); | ||
} else if (object >= -0x80) { | ||
// int 8 | ||
this.writeU8(0xd0); | ||
this.writeI8(object); | ||
return this.writeU8I8(0xd0, object); | ||
} else if (object >= -0x8000) { | ||
// int 16 | ||
this.writeU8(0xd1); | ||
this.writeI16(object); | ||
return this.writeU8I16(0xd1, object); | ||
} else if (object >= -0x80000000) { | ||
// int 32 | ||
this.writeU8(0xd2); | ||
this.writeI32(object); | ||
return this.writeU8I32(0xd2, object); | ||
} else { | ||
// int 64 | ||
this.writeU8(0xd3); | ||
this.writeI64(object); | ||
return this.writeU8I64(0xd3, object); | ||
} | ||
@@ -133,8 +149,6 @@ } | ||
// float 32 | ||
this.writeU8(0xca); | ||
this.writeF32(object); | ||
return this.writeU8F32(0xca, object); | ||
} else { | ||
// float 64 | ||
this.writeU8(0xcb); | ||
this.writeF64(object); | ||
return this.writeU8F64(0xcb, object); | ||
} | ||
@@ -144,18 +158,21 @@ } | ||
private writeStringHeader(byteLength: number) { | ||
private buildStringHeader(byteLength: number) { | ||
if (byteLength < 32) { | ||
// fixstr | ||
this.writeU8(0xa0 + byteLength); | ||
return [0xa0 + byteLength]; | ||
} else if (byteLength < 0x100) { | ||
// str 8 | ||
this.writeU8(0xd9); | ||
this.writeU8(byteLength); | ||
return [0xd9, byteLength]; | ||
} else if (byteLength < 0x10000) { | ||
// str 16 | ||
this.writeU8(0xda); | ||
this.writeU16(byteLength); | ||
const bytes = new DataView(new ArrayBuffer(3)); | ||
bytes.setUint8(0, 0xda); | ||
bytes.setUint16(1, byteLength); | ||
return new Uint8Array(bytes.buffer); | ||
} else if (byteLength < 0x100000000) { | ||
// str 32 | ||
this.writeU8(0xdb); | ||
this.writeU32(byteLength); | ||
const bytes = new DataView(new ArrayBuffer(5)); | ||
bytes.setUint8(0, 0xdb); | ||
bytes.setUint32(1, byteLength); | ||
return new Uint8Array(bytes.buffer); | ||
} else { | ||
@@ -173,11 +190,13 @@ throw new Error(`Too long string: ${byteLength} bytes in UTF-8`); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
utf8EncodeTE(object, this.bytes, 0); | ||
this.writeBuffer(byteLength) | ||
const header = this.buildStringHeader(byteLength); | ||
this.bytes.set(header, 0); | ||
utf8EncodeTE(object, this.bytes, header.length); | ||
return this.writeBuffer(byteLength + header.length); | ||
} else { | ||
const byteLength = utf8Count(object); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
utf8EncodeJs(object, this.bytes, 0); | ||
this.writeBuffer(byteLength) | ||
const header = this.buildStringHeader(byteLength); | ||
this.bytes.set(header, 0); | ||
utf8EncodeJs(object, this.bytes, header.length); | ||
return this.writeBuffer(byteLength + header.length); | ||
} | ||
@@ -190,9 +209,9 @@ } | ||
if (ext != null) { | ||
this.encodeExtension(ext); | ||
return this.encodeExtension(ext); | ||
} else if (Array.isArray(object)) { | ||
this.encodeArray(object, depth); | ||
return this.encodeArray(object, depth); | ||
} else if (ArrayBuffer.isView(object)) { | ||
this.encodeBinary(object); | ||
return this.encodeBinary(object); | ||
} else if (typeof object === "object") { | ||
this.encodeMap(object as Record<string, unknown>, depth); | ||
return this.encodeMap(object as Record<string, unknown>, depth); | ||
} else { | ||
@@ -204,16 +223,13 @@ // symbol, function and other special object come here unless extensionCodec handles them. | ||
private encodeBinary(object: ArrayBufferView) { | ||
private *encodeBinary(object: ArrayBufferView) { | ||
const size = object.byteLength; | ||
if (size < 0x100) { | ||
// bin 8 | ||
this.writeU8(0xc4); | ||
this.writeU8(size); | ||
yield* this.writeU8U8(0xc4, size); | ||
} else if (size < 0x10000) { | ||
// bin 16 | ||
this.writeU8(0xc5); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xc5, size); | ||
} else if (size < 0x100000000) { | ||
// bin 32 | ||
this.writeU8(0xc6); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xc6, size); | ||
} else { | ||
@@ -223,18 +239,16 @@ throw new Error(`Too large binary: ${size}`); | ||
const bytes = ensureUint8Array(object); | ||
this.writeU8a(bytes); | ||
yield* this.writeU8a(bytes); | ||
} | ||
private encodeArray(object: Array<unknown>, depth: number) { | ||
private *encodeArray(object: Array<unknown>, depth: number) { | ||
const size = object.length; | ||
if (size < 16) { | ||
// fixarray | ||
this.writeU8(0x90 + size); | ||
yield* this.writeU8(0x90 + size); | ||
} else if (size < 0x10000) { | ||
// array 16 | ||
this.writeU8(0xdc); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xdc, size); | ||
} else if (size < 0x100000000) { | ||
// array 32 | ||
this.writeU8(0xdd); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xdd, size); | ||
} else { | ||
@@ -244,3 +258,3 @@ throw new Error(`Too large array: ${size}`); | ||
for (const item of object) { | ||
this.doEncode(item, depth + 1); | ||
yield* this.doEncode(item, depth + 1); | ||
} | ||
@@ -261,3 +275,3 @@ } | ||
private encodeMap(object: Record<string, unknown>, depth: number) { | ||
private *encodeMap(object: Record<string, unknown>, depth: number) { | ||
const keys = Object.keys(object); | ||
@@ -272,11 +286,9 @@ if (this.sortKeys) { | ||
// fixmap | ||
this.writeU8(0x80 + size); | ||
yield* this.writeU8(0x80 + size); | ||
} else if (size < 0x10000) { | ||
// map 16 | ||
this.writeU8(0xde); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xde, size); | ||
} else if (size < 0x100000000) { | ||
// map 32 | ||
this.writeU8(0xdf); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xdf, size); | ||
} else { | ||
@@ -290,4 +302,4 @@ throw new Error(`Too large map object: ${size}`); | ||
if (!(this.ignoreUndefined && value === undefined)) { | ||
this.encodeString(key); | ||
this.doEncode(value, depth + 1); | ||
yield* this.encodeString(key); | ||
yield* this.doEncode(value, depth + 1); | ||
} | ||
@@ -297,36 +309,33 @@ } | ||
private encodeExtension(ext: ExtData) { | ||
private *encodeExtension(ext: ExtData) { | ||
const size = ext.data.length; | ||
if (size === 1) { | ||
// fixext 1 | ||
this.writeU8(0xd4); | ||
yield* this.writeU8(0xd4); | ||
} else if (size === 2) { | ||
// fixext 2 | ||
this.writeU8(0xd5); | ||
yield* this.writeU8(0xd5); | ||
} else if (size === 4) { | ||
// fixext 4 | ||
this.writeU8(0xd6); | ||
yield* this.writeU8(0xd6); | ||
} else if (size === 8) { | ||
// fixext 8 | ||
this.writeU8(0xd7); | ||
yield* this.writeU8(0xd7); | ||
} else if (size === 16) { | ||
// fixext 16 | ||
this.writeU8(0xd8); | ||
yield* this.writeU8(0xd8); | ||
} else if (size < 0x100) { | ||
// ext 8 | ||
this.writeU8(0xc7); | ||
this.writeU8(size); | ||
yield* this.writeU8U8(0xc7, size); | ||
} else if (size < 0x10000) { | ||
// ext 16 | ||
this.writeU8(0xc8); | ||
this.writeU16(size); | ||
yield* this.writeU8U16(0xc8, size); | ||
} else if (size < 0x100000000) { | ||
// ext 32 | ||
this.writeU8(0xc9); | ||
this.writeU32(size); | ||
yield* this.writeU8U32(0xc9, size); | ||
} else { | ||
throw new Error(`Too large extension object: ${size}`); | ||
} | ||
this.writeI8(ext.type); | ||
this.writeU8a(ext.data); | ||
yield* this.writeI8(ext.type); | ||
yield* this.writeU8a(ext.data); | ||
} | ||
@@ -336,3 +345,3 @@ | ||
this.view.setUint8(0, value); | ||
this.writeBuffer(1) | ||
return this.writeBuffer(1); | ||
} | ||
@@ -345,3 +354,3 @@ | ||
this.bytes.set(values, 0); | ||
this.writeBuffer(size) | ||
return this.writeBuffer(size); | ||
} | ||
@@ -351,3 +360,3 @@ | ||
this.view.setInt8(0, value); | ||
this.writeBuffer(1) | ||
return this.writeBuffer(1); | ||
} | ||
@@ -357,3 +366,3 @@ | ||
this.view.setUint16(0, value); | ||
this.writeBuffer(2) | ||
return this.writeBuffer(2); | ||
} | ||
@@ -363,3 +372,3 @@ | ||
this.view.setInt16(0, value); | ||
this.writeBuffer(2) | ||
return this.writeBuffer(2); | ||
} | ||
@@ -369,3 +378,3 @@ | ||
this.view.setUint32(0, value); | ||
this.writeBuffer(4) | ||
return this.writeBuffer(4); | ||
} | ||
@@ -375,3 +384,3 @@ | ||
this.view.setInt32(0, value); | ||
this.writeBuffer(4) | ||
return this.writeBuffer(4); | ||
} | ||
@@ -381,3 +390,3 @@ | ||
this.view.setFloat32(0, value); | ||
this.writeBuffer(4) | ||
return this.writeBuffer(4); | ||
} | ||
@@ -387,3 +396,3 @@ | ||
this.view.setFloat64(0, value); | ||
this.writeBuffer(8) | ||
return this.writeBuffer(8); | ||
} | ||
@@ -393,3 +402,3 @@ | ||
setUint64(this.view, 0, value); | ||
this.writeBuffer(8) | ||
return this.writeBuffer(8); | ||
} | ||
@@ -399,8 +408,68 @@ | ||
setInt64(this.view, 0, value); | ||
this.writeBuffer(8) | ||
return this.writeBuffer(8); | ||
} | ||
private writeBuffer(length: number){ | ||
this.write(this.bytes.subarray(0, length)) | ||
private writeU8U8(value: number, value2: number) { | ||
this.view.setUint8(0, value); | ||
this.view.setUint8(1, value2); | ||
return this.writeBuffer(2); | ||
} | ||
private writeU8U16(u8: number, u16: number) { | ||
this.view.setUint8(0, u8); | ||
this.view.setUint16(1, u16); | ||
return this.writeBuffer(3); | ||
} | ||
private writeU8U32(u8: number, u32: number) { | ||
this.view.setUint8(0, u8); | ||
this.view.setUint32(1, u32); | ||
return this.writeBuffer(5); | ||
} | ||
private writeU8U64(u8: number, u64: number) { | ||
this.view.setUint8(0, u8); | ||
setUint64(this.view, 1, u64); | ||
return this.writeBuffer(9); | ||
} | ||
private writeU8I8(value: number, value2: number) { | ||
this.view.setUint8(0, value); | ||
this.view.setInt8(1, value2); | ||
return this.writeBuffer(2); | ||
} | ||
private writeU8I16(u8: number, i16: number) { | ||
this.view.setUint8(0, u8); | ||
this.view.setInt16(1, i16); | ||
return this.writeBuffer(3); | ||
} | ||
private writeU8I32(u8: number, i32: number) { | ||
this.view.setUint8(0, u8); | ||
this.view.setInt32(1, i32); | ||
return this.writeBuffer(5); | ||
} | ||
private writeU8I64(u8: number, i64: number) { | ||
this.view.setUint8(0, u8); | ||
setInt64(this.view, 1, i64); | ||
return this.writeBuffer(9); | ||
} | ||
private writeU8F32(u8: number, f32: number) { | ||
this.view.setUint8(0, u8); | ||
this.view.setFloat32(1, f32); | ||
return this.writeBuffer(5); | ||
} | ||
private writeU8F64(u8: number, f64: number) { | ||
this.view.setUint8(0, u8); | ||
this.view.setFloat64(1, f64); | ||
return this.writeBuffer(9); | ||
} | ||
private *writeBuffer(length: number) { | ||
yield this.bytes.subarray(0, length); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1062501
12760
1