| | |
| | | /** @typedef {typeof MEASURE_END_OPERATION} MEASURE_END_OPERATION_TYPE */ |
| | | |
| | | /** |
| | | * Returns type of number for serialization. |
| | | * @param {number} n number |
| | | * @returns {0 | 1 | 2} type of number for serialization |
| | | */ |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Returns type of bigint for serialization. |
| | | * @param {bigint} n bigint |
| | | * @returns {0 | 1 | 2} type of bigint for serialization |
| | | */ |
| | |
| | | /** @typedef {{ retainedBuffer?: (x: Buffer) => Buffer }} Context} */ |
| | | |
| | | /** |
| | | * Defines the lazy function type used by this module. |
| | | * @template LazyInputValue |
| | | * @template LazyOutputValue |
| | | * @typedef {import("./SerializerMiddleware").LazyFunction<LazyInputValue, LazyOutputValue, BinaryMiddleware, undefined>} LazyFunction |
| | | */ |
| | | |
| | | /** |
| | | * Represents BinaryMiddleware. |
| | | * @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>} |
| | | */ |
| | | class BinaryMiddleware extends SerializerMiddleware { |
| | | /** |
| | | * Serializes this instance into the provided serializer context. |
| | | * @param {DeserializedType} data data |
| | | * @param {Context} context context object |
| | | * @returns {SerializedType | Promise<SerializedType> | null} serialized data |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns new lazy. |
| | | * @param {LazyFunction<DeserializedType, SerializedType>} fn lazy function |
| | | * @param {Context} context serialize function |
| | | * @returns {LazyFunction<SerializedType, DeserializedType>} new lazy |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns serialized data. |
| | | * @param {DeserializedType} data data |
| | | * @param {Context} context context object |
| | | * @param {{ leftOverBuffer: Buffer | null, allocationSize: number, increaseCounter: number }} allocationScope allocation scope |
| | |
| | | currentBuffer = Buffer.allocUnsafe(allocationScope.allocationSize); |
| | | } |
| | | /** |
| | | * Processes the provided bytes needed. |
| | | * @param {number} bytesNeeded bytes needed |
| | | */ |
| | | const allocate = (bytesNeeded) => { |
| | |
| | | } |
| | | }; |
| | | /** |
| | | * Processes the provided byte. |
| | | * @param {number} byte byte |
| | | */ |
| | | const writeU8 = (byte) => { |
| | |
| | | (currentBuffer).writeUInt8(byte, currentPosition++); |
| | | }; |
| | | /** |
| | | * Processes the provided ui32. |
| | | * @param {number} ui32 ui32 |
| | | */ |
| | | const writeU32 = (ui32) => { |
| | |
| | | measureStack.push(buffers.length, currentPosition); |
| | | }; |
| | | /** |
| | | * Returns size. |
| | | * @returns {number} size |
| | | */ |
| | | const measureEnd = () => { |
| | |
| | | /** @type {number[]} */ |
| | | const lengths = []; |
| | | for (const item of serializedData) { |
| | | /** @type {undefined | number} */ |
| | | let last; |
| | | if (typeof item === "function") { |
| | | lengths.push(0); |
| | |
| | | } |
| | | case "boolean": { |
| | | let lastByte = thing === true ? 1 : 0; |
| | | /** @type {number[]} */ |
| | | const bytes = []; |
| | | let count = 1; |
| | | /** @type {undefined | number} */ |
| | | let n; |
| | | for (n = 1; n < 0xffffffff && i + n < data.length; n++) { |
| | | const item = data[i + n]; |
| | |
| | | } |
| | | case "object": { |
| | | if (thing === null) { |
| | | /** @type {number} */ |
| | | let n; |
| | | for (n = 1; n < 0x100000104 && i + n < data.length; n++) { |
| | | const item = data[i + n]; |
| | |
| | | } |
| | | |
| | | /** |
| | | * Restores this instance from the provided deserializer context. |
| | | * @param {SerializedType} data data |
| | | * @param {Context} context context object |
| | | * @returns {DeserializedType | Promise<DeserializedType>} deserialized data |
| | |
| | | } |
| | | |
| | | /** |
| | | * Create lazy deserialized. |
| | | * @private |
| | | * @param {SerializedType} content content |
| | | * @param {Context} context context object |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns new lazy. |
| | | * @private |
| | | * @param {LazyFunction<SerializedType, DeserializedType>} fn lazy function |
| | | * @param {Context} context context object |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns deserialized data. |
| | | * @param {SerializedType} data data |
| | | * @param {Context} context context object |
| | | * @returns {DeserializedType} deserialized data |
| | |
| | | } |
| | | }; |
| | | /** |
| | | * Checks whether this binary middleware is in current buffer. |
| | | * @param {number} n n |
| | | * @returns {boolean} true when in current buffer, otherwise false |
| | | */ |
| | |
| | | } |
| | | }; |
| | | /** |
| | | * Reads n bytes |
| | | * Returns buffer with bytes. |
| | | * @param {number} n amount of bytes to read |
| | | * @returns {Buffer} buffer with bytes |
| | | */ |
| | |
| | | return res; |
| | | }; |
| | | /** |
| | | * Returns u8. |
| | | * @returns {number} U8 |
| | | */ |
| | | const readU8 = () => { |
| | |
| | | return byte; |
| | | }; |
| | | /** |
| | | * Returns u32. |
| | | * @returns {number} U32 |
| | | */ |
| | | const readU32 = () => read(I32_SIZE).readUInt32LE(0); |
| | | /** |
| | | * Processes the provided data. |
| | | * @param {number} data data |
| | | * @param {number} n n |
| | | */ |