-
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement XDR encoder with separated schema validation and comprehensive tests #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
c8249f8
55c1c4a
d4705ad
d10904e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,295 @@ | ||||||||||||||||||||||||||||||||||
| import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib'; | ||||||||||||||||||||||||||||||||||
| import type {BinaryJsonEncoder} from '../types'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * XDR (External Data Representation) binary encoder for basic value encoding. | ||||||||||||||||||||||||||||||||||
| * Implements XDR binary encoding according to RFC 4506. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * Key XDR encoding principles: | ||||||||||||||||||||||||||||||||||
| * - All data types are aligned to 4-byte boundaries | ||||||||||||||||||||||||||||||||||
| * - Multi-byte quantities are transmitted in big-endian byte order | ||||||||||||||||||||||||||||||||||
| * - Strings and opaque data are padded to 4-byte boundaries | ||||||||||||||||||||||||||||||||||
| * - Variable-length arrays and strings are preceded by their length | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| export class XdrEncoder implements BinaryJsonEncoder { | ||||||||||||||||||||||||||||||||||
| constructor(public readonly writer: IWriter & IWriterGrowable) {} | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public encode(value: unknown): Uint8Array { | ||||||||||||||||||||||||||||||||||
| const writer = this.writer; | ||||||||||||||||||||||||||||||||||
| writer.reset(); | ||||||||||||||||||||||||||||||||||
| this.writeAny(value); | ||||||||||||||||||||||||||||||||||
| return writer.flush(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Called when the encoder encounters a value that it does not know how to encode. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeUnknown(value: unknown): void { | ||||||||||||||||||||||||||||||||||
| this.writeVoid(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public writeAny(value: unknown): void { | ||||||||||||||||||||||||||||||||||
| switch (typeof value) { | ||||||||||||||||||||||||||||||||||
| case 'boolean': | ||||||||||||||||||||||||||||||||||
| return this.writeBoolean(value); | ||||||||||||||||||||||||||||||||||
| case 'number': | ||||||||||||||||||||||||||||||||||
| return this.writeNumber(value); | ||||||||||||||||||||||||||||||||||
| case 'string': | ||||||||||||||||||||||||||||||||||
| return this.writeStr(value); | ||||||||||||||||||||||||||||||||||
| case 'object': { | ||||||||||||||||||||||||||||||||||
| if (value === null) return this.writeVoid(); | ||||||||||||||||||||||||||||||||||
| const constructor = value.constructor; | ||||||||||||||||||||||||||||||||||
| switch (constructor) { | ||||||||||||||||||||||||||||||||||
| case Uint8Array: | ||||||||||||||||||||||||||||||||||
| return this.writeBin(value as Uint8Array); | ||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||
| return this.writeUnknown(value); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| case 'bigint': | ||||||||||||||||||||||||||||||||||
| return this.writeHyper(value); | ||||||||||||||||||||||||||||||||||
| case 'undefined': | ||||||||||||||||||||||||||||||||||
| return this.writeVoid(); | ||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||
| return this.writeUnknown(value); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR void value (no data is actually written). | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeVoid(): void { | ||||||||||||||||||||||||||||||||||
| // Void values are encoded as no data | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR null value (for interface compatibility). | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeNull(): void { | ||||||||||||||||||||||||||||||||||
| this.writeVoid(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR boolean value as a 4-byte integer. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeBoolean(bool: boolean): void { | ||||||||||||||||||||||||||||||||||
| this.writeInt(bool ? 1 : 0); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR signed 32-bit integer in big-endian format. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeInt(int: number): void { | ||||||||||||||||||||||||||||||||||
| const writer = this.writer; | ||||||||||||||||||||||||||||||||||
| writer.ensureCapacity(4); | ||||||||||||||||||||||||||||||||||
| writer.view.setInt32(writer.x, Math.trunc(int), false); // big-endian | ||||||||||||||||||||||||||||||||||
| writer.move(4); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR unsigned 32-bit integer in big-endian format. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeUnsignedInt(uint: number): void { | ||||||||||||||||||||||||||||||||||
| const writer = this.writer; | ||||||||||||||||||||||||||||||||||
| writer.ensureCapacity(4); | ||||||||||||||||||||||||||||||||||
| writer.view.setUint32(writer.x, Math.trunc(uint) >>> 0, false); // big-endian | ||||||||||||||||||||||||||||||||||
| writer.move(4); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR signed 64-bit integer (hyper) in big-endian format. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeHyper(hyper: number | bigint): void { | ||||||||||||||||||||||||||||||||||
| const writer = this.writer; | ||||||||||||||||||||||||||||||||||
| writer.ensureCapacity(8); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (typeof hyper === 'bigint') { | ||||||||||||||||||||||||||||||||||
| // Convert bigint to two 32-bit values for big-endian encoding | ||||||||||||||||||||||||||||||||||
| const high = Number((hyper >> BigInt(32)) & BigInt(0xffffffff)); | ||||||||||||||||||||||||||||||||||
| const low = Number(hyper & BigInt(0xffffffff)); | ||||||||||||||||||||||||||||||||||
| writer.view.setInt32(writer.x, high, false); // high 32 bits | ||||||||||||||||||||||||||||||||||
| writer.view.setUint32(writer.x + 4, low, false); // low 32 bits | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| const truncated = Math.trunc(hyper); | ||||||||||||||||||||||||||||||||||
| const high = Math.floor(truncated / 0x100000000); | ||||||||||||||||||||||||||||||||||
| const low = truncated >>> 0; | ||||||||||||||||||||||||||||||||||
| writer.view.setInt32(writer.x, high, false); // high 32 bits | ||||||||||||||||||||||||||||||||||
| writer.view.setUint32(writer.x + 4, low, false); // low 32 bits | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| writer.move(8); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR unsigned 64-bit integer (unsigned hyper) in big-endian format. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeUnsignedHyper(uhyper: number | bigint): void { | ||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to use |
||||||||||||||||||||||||||||||||||
| const writer = this.writer; | ||||||||||||||||||||||||||||||||||
| writer.ensureCapacity(8); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (typeof uhyper === 'bigint') { | ||||||||||||||||||||||||||||||||||
| // Convert bigint to two 32-bit values for big-endian encoding | ||||||||||||||||||||||||||||||||||
| const high = Number((uhyper >> BigInt(32)) & BigInt(0xffffffff)); | ||||||||||||||||||||||||||||||||||
| const low = Number(uhyper & BigInt(0xffffffff)); | ||||||||||||||||||||||||||||||||||
| writer.view.setUint32(writer.x, high, false); // high 32 bits | ||||||||||||||||||||||||||||||||||
| writer.view.setUint32(writer.x + 4, low, false); // low 32 bits | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| const truncated = Math.trunc(Math.abs(uhyper)); | ||||||||||||||||||||||||||||||||||
| const high = Math.floor(truncated / 0x100000000); | ||||||||||||||||||||||||||||||||||
| const low = truncated >>> 0; | ||||||||||||||||||||||||||||||||||
| writer.view.setUint32(writer.x, high, false); // high 32 bits | ||||||||||||||||||||||||||||||||||
| writer.view.setUint32(writer.x + 4, low, false); // low 32 bits | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| writer.move(8); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR float value using IEEE 754 single-precision in big-endian format. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeFloat(float: number): void { | ||||||||||||||||||||||||||||||||||
| const writer = this.writer; | ||||||||||||||||||||||||||||||||||
| writer.ensureCapacity(4); | ||||||||||||||||||||||||||||||||||
| writer.view.setFloat32(writer.x, float, false); // big-endian | ||||||||||||||||||||||||||||||||||
| writer.move(4); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR double value using IEEE 754 double-precision in big-endian format. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeDouble(double: number): void { | ||||||||||||||||||||||||||||||||||
| const writer = this.writer; | ||||||||||||||||||||||||||||||||||
| writer.ensureCapacity(8); | ||||||||||||||||||||||||||||||||||
| writer.view.setFloat64(writer.x, double, false); // big-endian | ||||||||||||||||||||||||||||||||||
| writer.move(8); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes an XDR quadruple value (128-bit float). | ||||||||||||||||||||||||||||||||||
| * Note: JavaScript doesn't have native 128-bit float support. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeQuadruple(quad: number): void { | ||||||||||||||||||||||||||||||||||
| throw new Error('not implemented'); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Writes XDR opaque data with fixed length. | ||||||||||||||||||||||||||||||||||
| * Data is padded to 4-byte boundary. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public writeOpaque(data: Uint8Array, size: number): void { | ||||||||||||||||||||||||||||||||||
| if (data.length !== size) { | ||||||||||||||||||||||||||||||||||
| throw new Error(`Opaque data length ${data.length} does not match expected size ${size}`); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| public writeOpaque(data: Uint8Array, size: number): void { | |
| if (data.length !== size) { | |
| throw new Error(`Opaque data length ${data.length} does not match expected size ${size}`); | |
| } | |
| public writeOpaque(data: Uint8Array): void { | |
| const size = data.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed size check and parameter from writeOpaque() - now takes only data with automatic length detection (d10904e)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reuse .writeOpaque() here if possible:
| const writer = this.writer; | |
| const paddedSize = Math.ceil(data.length / 4) * 4; | |
| writer.ensureCapacity(paddedSize); | |
| // Write data | |
| writer.buf(data, data.length); | |
| // Write padding bytes | |
| const padding = paddedSize - data.length; | |
| for (let i = 0; i < padding; i++) { | |
| writer.u8(0); | |
| } | |
| } | |
| this.writeOpaque(data); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified writeVarlenOpaque() to reuse writeOpaque() method (d10904e)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| throw new Error('writeArr not implemented in XDR encoder'); | |
| throw new Error('not implemented'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed error message to be consistent: 'not implemented' (d10904e)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| throw new Error('writeObj not implemented in XDR encoder'); | |
| throw new Error('not implemented'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed error message to be consistent: 'not implemented' (d10904e)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
DataView.prototype.setBigInt64()here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use
DataView.prototype.setBigInt64()for cleaner bigint encoding (d10904e)