* Returns serialized data. * @param {DeserializedType} data data * @param {Context} context context object * @param {{ leftOverBuffer: Buffer | null, allocationSize: number, increaseCounter: number }} allocationScope allocation scope * @returns {SerializedType} serialized data
(
data,
context,
allocationScope = {
allocationSize: 1024,
increaseCounter: 0,
leftOverBuffer: null
}
)
| 700 | * @returns {SerializedType} serialized data |
| 701 | */ |
| 702 | _serialize( |
| 703 | data, |
| 704 | context, |
| 705 | allocationScope = { |
| 706 | allocationSize: 1024, |
| 707 | increaseCounter: 0, |
| 708 | leftOverBuffer: null |
| 709 | } |
| 710 | ) { |
| 711 | /** @type {Buffer | null} */ |
| 712 | let leftOverBuffer = null; |
| 713 | /** @type {BufferSerializableType[]} */ |
| 714 | let buffers = []; |
| 715 | /** @type {Buffer | null} */ |
| 716 | let currentBuffer = allocationScope ? allocationScope.leftOverBuffer : null; |
| 717 | allocationScope.leftOverBuffer = null; |
| 718 | let currentPosition = 0; |
| 719 | if (currentBuffer === null) { |
| 720 | currentBuffer = Buffer.allocUnsafe(allocationScope.allocationSize); |
| 721 | } |
| 722 | /** |
| 723 | * Processes the provided bytes needed. |
| 724 | * @param {number} bytesNeeded bytes needed |
| 725 | */ |
| 726 | const allocate = (bytesNeeded) => { |
| 727 | if (currentBuffer !== null) { |
| 728 | if (currentBuffer.length - currentPosition >= bytesNeeded) return; |
| 729 | flush(); |
| 730 | } |
| 731 | if (leftOverBuffer && leftOverBuffer.length >= bytesNeeded) { |
| 732 | currentBuffer = leftOverBuffer; |
| 733 | leftOverBuffer = null; |
| 734 | } else { |
| 735 | currentBuffer = Buffer.allocUnsafe( |
| 736 | Math.max(bytesNeeded, allocationScope.allocationSize) |
| 737 | ); |
| 738 | if ( |
| 739 | !(allocationScope.increaseCounter = |
| 740 | (allocationScope.increaseCounter + 1) % 4) && |
| 741 | allocationScope.allocationSize < 16777216 |
| 742 | ) { |
| 743 | allocationScope.allocationSize <<= 1; |
| 744 | } |
| 745 | } |
| 746 | }; |
| 747 | const flush = () => { |
| 748 | if (currentBuffer !== null) { |
| 749 | if (currentPosition > 0) { |
| 750 | buffers.push( |
| 751 | Buffer.from( |
| 752 | currentBuffer.buffer, |
| 753 | currentBuffer.byteOffset, |
| 754 | currentPosition |
| 755 | ) |
| 756 | ); |
| 757 | } |
| 758 | if ( |
| 759 | !leftOverBuffer || |
no test coverage detected