Skip to content

Commit d10904e

Browse files
Copilotstreamich
andcommitted
refactor: address review comments - use native BigInt methods, simplify opaque handling, fix error messages
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent d4705ad commit d10904e

File tree

3 files changed

+10
-41
lines changed

3 files changed

+10
-41
lines changed

src/xdr/XdrEncoder.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ export class XdrEncoder implements BinaryJsonEncoder {
104104
writer.ensureCapacity(8);
105105

106106
if (typeof hyper === 'bigint') {
107-
// Convert bigint to two 32-bit values for big-endian encoding
108-
const high = Number((hyper >> BigInt(32)) & BigInt(0xffffffff));
109-
const low = Number(hyper & BigInt(0xffffffff));
110-
writer.view.setInt32(writer.x, high, false); // high 32 bits
111-
writer.view.setUint32(writer.x + 4, low, false); // low 32 bits
107+
writer.view.setBigInt64(writer.x, hyper, false); // big-endian
112108
} else {
113109
const truncated = Math.trunc(hyper);
114110
const high = Math.floor(truncated / 0x100000000);
@@ -127,11 +123,7 @@ export class XdrEncoder implements BinaryJsonEncoder {
127123
writer.ensureCapacity(8);
128124

129125
if (typeof uhyper === 'bigint') {
130-
// Convert bigint to two 32-bit values for big-endian encoding
131-
const high = Number((uhyper >> BigInt(32)) & BigInt(0xffffffff));
132-
const low = Number(uhyper & BigInt(0xffffffff));
133-
writer.view.setUint32(writer.x, high, false); // high 32 bits
134-
writer.view.setUint32(writer.x + 4, low, false); // low 32 bits
126+
writer.view.setBigUint64(writer.x, uhyper, false); // big-endian
135127
} else {
136128
const truncated = Math.trunc(Math.abs(uhyper));
137129
const high = Math.floor(truncated / 0x100000000);
@@ -174,11 +166,8 @@ export class XdrEncoder implements BinaryJsonEncoder {
174166
* Writes XDR opaque data with fixed length.
175167
* Data is padded to 4-byte boundary.
176168
*/
177-
public writeOpaque(data: Uint8Array, size: number): void {
178-
if (data.length !== size) {
179-
throw new Error(`Opaque data length ${data.length} does not match expected size ${size}`);
180-
}
181-
169+
public writeOpaque(data: Uint8Array): void {
170+
const size = data.length;
182171
const writer = this.writer;
183172
const paddedSize = Math.ceil(size / 4) * 4;
184173
writer.ensureCapacity(paddedSize);
@@ -199,19 +188,7 @@ export class XdrEncoder implements BinaryJsonEncoder {
199188
*/
200189
public writeVarlenOpaque(data: Uint8Array): void {
201190
this.writeUnsignedInt(data.length);
202-
203-
const writer = this.writer;
204-
const paddedSize = Math.ceil(data.length / 4) * 4;
205-
writer.ensureCapacity(paddedSize);
206-
207-
// Write data
208-
writer.buf(data, data.length);
209-
210-
// Write padding bytes
211-
const padding = paddedSize - data.length;
212-
for (let i = 0; i < padding; i++) {
213-
writer.u8(0);
214-
}
191+
this.writeOpaque(data);
215192
}
216193

217194
/**
@@ -241,11 +218,11 @@ export class XdrEncoder implements BinaryJsonEncoder {
241218
}
242219

243220
public writeArr(arr: unknown[]): void {
244-
throw new Error('writeArr not implemented in XDR encoder');
221+
throw new Error('not implemented');
245222
}
246223

247224
public writeObj(obj: Record<string, unknown>): void {
248-
throw new Error('writeObj not implemented in XDR encoder');
225+
throw new Error('not implemented');
249226
}
250227

251228
// BinaryJsonEncoder interface methods

src/xdr/XdrSchemaEncoder.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib';
22
import {XdrEncoder} from './XdrEncoder';
3-
import {XdrSchemaValidator} from './XdrSchemaValidator';
43
import {XdrUnion} from './XdrUnion';
54
import type {
65
XdrSchema,
@@ -16,11 +15,9 @@ import type {
1615

1716
export class XdrSchemaEncoder {
1817
private encoder: XdrEncoder;
19-
private validator: XdrSchemaValidator;
2018

2119
constructor(public readonly writer: IWriter & IWriterGrowable) {
2220
this.encoder = new XdrEncoder(writer);
23-
this.validator = new XdrSchemaValidator();
2421
}
2522

2623
public encode(value: unknown, schema: XdrSchema): Uint8Array {
@@ -104,7 +101,7 @@ export class XdrSchemaEncoder {
104101
throw new Error(`Opaque data length ${value.length} does not match schema size ${schema.size}`);
105102
}
106103

107-
this.encoder.writeOpaque(value, schema.size);
104+
this.encoder.writeOpaque(value);
108105
}
109106

110107
public writeVarlenOpaque(value: Uint8Array, schema: XdrVarlenOpaqueSchema): void {

src/xdr/__tests__/XdrEncoder.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,18 @@ describe('XdrEncoder', () => {
104104
describe('opaque data', () => {
105105
test('encodes fixed opaque data', () => {
106106
const data = new Uint8Array([1, 2, 3]);
107-
encoder.writeOpaque(data, 3);
107+
encoder.writeOpaque(data);
108108
const result = writer.flush();
109109
expect(result).toEqual(new Uint8Array([1, 2, 3, 0])); // padded to 4 bytes
110110
});
111111

112112
test('encodes fixed opaque data with exact 4-byte boundary', () => {
113113
const data = new Uint8Array([1, 2, 3, 4]);
114-
encoder.writeOpaque(data, 4);
114+
encoder.writeOpaque(data);
115115
const result = writer.flush();
116116
expect(result).toEqual(new Uint8Array([1, 2, 3, 4])); // no padding needed
117117
});
118118

119-
test('throws error for mismatched opaque size', () => {
120-
const data = new Uint8Array([1, 2, 3]);
121-
expect(() => encoder.writeOpaque(data, 5)).toThrow('Opaque data length 3 does not match expected size 5');
122-
});
123-
124119
test('encodes variable-length opaque data', () => {
125120
const data = new Uint8Array([1, 2, 3]);
126121
encoder.writeVarlenOpaque(data);

0 commit comments

Comments
 (0)