Implement XDR encoder with separated schema validation and comprehensive tests#54
Implement XDR encoder with separated schema validation and comprehensive tests#54
Conversation
…tests Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
|
…n, add XdrUnion, fix quadruple Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Implemented all requested changes: XdrEncoder changes:
XdrSchemaEncoder changes:
Additional: |
| /** | ||
| * Writes an XDR signed 64-bit integer (hyper) in big-endian format. | ||
| */ | ||
| public writeHyper(hyper: number | bigint): void { |
There was a problem hiding this comment.
Use DataView.prototype.setBigInt64() here.
There was a problem hiding this comment.
Updated to use DataView.prototype.setBigInt64() for cleaner bigint encoding (d10904e)
| /** | ||
| * Writes an XDR unsigned 64-bit integer (unsigned hyper) in big-endian format. | ||
| */ | ||
| public writeUnsignedHyper(uhyper: number | bigint): void { |
There was a problem hiding this comment.
Use DataView.prototype.setBigUint64() here.
There was a problem hiding this comment.
Updated to use DataView.prototype.setBigUint64() for cleaner unsigned bigint encoding (d10904e)
src/xdr/XdrEncoder.ts
Outdated
| 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}`); | ||
| } |
There was a problem hiding this comment.
Remove this check and size property.
| 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.
Removed size check and parameter from writeOpaque() - now takes only data with automatic length detection (d10904e)
src/xdr/XdrEncoder.ts
Outdated
|
|
||
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
Simplified writeVarlenOpaque() to reuse writeOpaque() method (d10904e)
src/xdr/XdrEncoder.ts
Outdated
| } | ||
|
|
||
| public writeArr(arr: unknown[]): void { | ||
| throw new Error('writeArr not implemented in XDR encoder'); |
There was a problem hiding this comment.
| throw new Error('writeArr not implemented in XDR encoder'); | |
| throw new Error('not implemented'); |
There was a problem hiding this comment.
Fixed error message to be consistent: 'not implemented' (d10904e)
src/xdr/XdrEncoder.ts
Outdated
| } | ||
|
|
||
| public writeObj(obj: Record<string, unknown>): void { | ||
| throw new Error('writeObj not implemented in XDR encoder'); |
There was a problem hiding this comment.
| throw new Error('writeObj not implemented in XDR encoder'); | |
| throw new Error('not implemented'); |
There was a problem hiding this comment.
Fixed error message to be consistent: 'not implemented' (d10904e)
src/xdr/XdrSchemaEncoder.ts
Outdated
|
|
||
| export class XdrSchemaEncoder { | ||
| private encoder: XdrEncoder; | ||
| private validator: XdrSchemaValidator; |
There was a problem hiding this comment.
Remove validator from this encoder, assume that the schema is valid.
| private validator: XdrSchemaValidator; |
There was a problem hiding this comment.
Removed validator from XdrSchemaEncoder - now assumes schema is valid for better performance (d10904e)
…fy opaque handling, fix error messages Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Addressed all review comments, tests pass, and ran prettier (d10904e): ✅ Used native |
|
🎉 This PR is included in version 1.13.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Addressed review comments from @streamich:
Changes Made:
XdrEncoder improvements:
DataView.prototype.setBigInt64()for hyper encoding instead of manual bit manipulationDataView.prototype.setBigUint64()for unsigned hyper encodingwriteOpaque()method - now takes only datawriteOpaque()inwriteVarlenOpaque()for code simplificationwriteArr()andwriteObj()to be consistent:'not implemented'XdrSchemaEncoder improvements:
writeOpaque()call to match new signature without size parameterTest updates:
writeOpaque()callsQuality assurance:
The implementation now uses native DataView BigInt methods for better performance and has cleaner, more maintainable code with the validator properly separated.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.