Skip to content

Commit 025f38e

Browse files
committed
feat: 🎸 use Reader in RpcOpaqueAuth body
1 parent 1610d30 commit 025f38e

File tree

9 files changed

+47
-49
lines changed

9 files changed

+47
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
},
6565
"dependencies": {
6666
"@jsonjoy.com/base64": "^1.1.2",
67-
"@jsonjoy.com/buffers": "^1.1.0",
67+
"@jsonjoy.com/buffers": "^1.2.0",
6868
"@jsonjoy.com/codegen": "^1.0.0",
6969
"@jsonjoy.com/json-pointer": "^1.0.2",
7070
"@jsonjoy.com/util": "^1.9.0",

src/rpc/RpcMessageDecoder.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ export class RpcMessageDecoder {
1414
public decodeMessage(reader: Reader): RpcMessage | undefined {
1515
const startPos = reader.x;
1616
try {
17-
if (reader.size() < 8) {
18-
reader.x = startPos;
19-
return undefined;
20-
}
17+
if (reader.size() < 8) return undefined;
2118
const xid = reader.u32();
2219
const msgType = reader.u32();
2320
let message: RpcMessage | undefined;
@@ -123,7 +120,7 @@ export class RpcMessageDecoder {
123120
}
124121
const paddedLength = (length + 3) & ~3;
125122
if (reader.size() < paddedLength) return undefined;
126-
const body = length > 0 ? reader.buf(length) : new Uint8Array(0);
123+
const body = length > 0 ? reader.cut(length) : new Reader(new Uint8Array(0));
127124
const padding = paddedLength - length;
128125
if (padding > 0) {
129126
reader.skip(padding);

src/rpc/RpcMessageEncoder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ export class RpcMessageEncoder<W extends IWriter & IWriterGrowable = IWriter & I
149149
private writeOpaqueAuth(auth: RpcOpaqueAuth): void {
150150
const writer = this.writer;
151151
writer.u32(auth.flavor);
152-
const length = auth.body.length;
152+
const body = auth.body;
153+
const length = body.size();
153154
if (length > 400) throw new RpcEncodingError('Auth body too large');
154155
writer.u32(length);
155156
if (length > 0) {
156-
writer.buf(auth.body, length);
157+
writer.buf(body.subarray(0, length), length);
157158
const padding = (4 - (length % 4)) % 4;
158159
for (let i = 0; i < padding; i++) {
159160
writer.u8(0);

src/rpc/__tests__/decoder.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ describe('RpcMessageDecoder', () => {
6060
expect(call.vers).toBe(2);
6161
expect(call.proc).toBe(0);
6262
expect(call.cred.flavor).toBe(RpcAuthFlavor.AUTH_NULL);
63-
expect(call.cred.body.length).toBe(0);
63+
expect(call.cred.body.size()).toBe(0);
6464
expect(call.verf.flavor).toBe(RpcAuthFlavor.AUTH_NULL);
65-
expect(call.verf.body.length).toBe(0);
65+
expect(call.verf.body.size()).toBe(0);
6666
});
6767

6868
test('can decode CALL message with opaque auth data', () => {
@@ -123,7 +123,7 @@ describe('RpcMessageDecoder', () => {
123123
expect(msg.xid).toBe(10);
124124
const call = msg as RpcCallMessage;
125125
expect(call.cred.flavor).toBe(RpcAuthFlavor.AUTH_UNIX);
126-
expect(call.cred.body).toEqual(new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05]));
126+
expect(call.cred.body.buf()).toEqual(new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05]));
127127
});
128128

129129
test('returns undefined when not enough data', () => {

src/rpc/__tests__/encoder.spec.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ describe('RpcMessageEncoder', () => {
88
describe('CALL messages', () => {
99
test('can encode a simple CALL message with AUTH_NULL', () => {
1010
const encoder = new RpcMessageEncoder();
11-
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
12-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
11+
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
12+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
1313
const encoded = encoder.encodeCall(1, 100, 1, 0, cred, verf);
1414
const decoder = new RpcMessageDecoder();
1515
const reader = new Reader(encoded);
@@ -28,9 +28,9 @@ describe('RpcMessageEncoder', () => {
2828

2929
test('can encode CALL message with opaque auth data', () => {
3030
const encoder = new RpcMessageEncoder();
31-
const credBody = new Uint8Array([1, 2, 3, 4, 5]);
31+
const credBody = new Reader(new Uint8Array([1, 2, 3, 4, 5]));
3232
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_UNIX, credBody);
33-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
33+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
3434
const encoded = encoder.encodeCall(10, 200, 2, 5, cred, verf);
3535
const decoder = new RpcMessageDecoder();
3636
const reader = new Reader(encoded);
@@ -42,13 +42,13 @@ describe('RpcMessageEncoder', () => {
4242
expect(call.vers).toBe(2);
4343
expect(call.proc).toBe(5);
4444
expect(call.cred.flavor).toBe(RpcAuthFlavor.AUTH_UNIX);
45-
expect(call.cred.body).toEqual(credBody);
45+
expect(call.cred.body.buf()).toEqual(new Uint8Array([1, 2, 3, 4, 5]));
4646
});
4747

4848
test('can encode CALL message with parameters', () => {
4949
const encoder = new RpcMessageEncoder();
50-
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
51-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
50+
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
51+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
5252
const params = new Uint8Array([0, 0, 0, 42]);
5353
const encoded = encoder.encodeCall(15, 300, 1, 3, cred, verf, params);
5454
expect(encoded.length).toBeGreaterThan(40);
@@ -61,8 +61,8 @@ describe('RpcMessageEncoder', () => {
6161

6262
test('can encode CALL with RpcMessage object', () => {
6363
const encoder = new RpcMessageEncoder();
64-
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
65-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
64+
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
65+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
6666
const msg = new RpcCallMessage(20, RPC_VERSION, 100, 1, 0, cred, verf);
6767
const encoded = encoder.encodeMessage(msg);
6868
const decoder = new RpcMessageDecoder();
@@ -77,7 +77,7 @@ describe('RpcMessageEncoder', () => {
7777
describe('REPLY messages - MSG_ACCEPTED', () => {
7878
test('can encode SUCCESS reply', () => {
7979
const encoder = new RpcMessageEncoder();
80-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
80+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
8181
const results = new Uint8Array([0, 0, 0, 42]);
8282
const encoded = encoder.encodeAcceptedReply(1, verf, RpcAcceptStat.SUCCESS, undefined, results);
8383
const decoder = new RpcMessageDecoder();
@@ -92,7 +92,7 @@ describe('RpcMessageEncoder', () => {
9292

9393
test('can encode PROG_UNAVAIL reply', () => {
9494
const encoder = new RpcMessageEncoder();
95-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
95+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
9696
const encoded = encoder.encodeAcceptedReply(2, verf, RpcAcceptStat.PROG_UNAVAIL);
9797
const decoder = new RpcMessageDecoder();
9898
const reader = new Reader(encoded);
@@ -105,7 +105,7 @@ describe('RpcMessageEncoder', () => {
105105

106106
test('can encode PROG_MISMATCH reply', () => {
107107
const encoder = new RpcMessageEncoder();
108-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
108+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
109109
const mismatchInfo = {low: 1, high: 3};
110110
const encoded = encoder.encodeAcceptedReply(3, verf, RpcAcceptStat.PROG_MISMATCH, mismatchInfo);
111111
const decoder = new RpcMessageDecoder();
@@ -122,7 +122,7 @@ describe('RpcMessageEncoder', () => {
122122

123123
test('can encode PROC_UNAVAIL reply', () => {
124124
const encoder = new RpcMessageEncoder();
125-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
125+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
126126
const encoded = encoder.encodeAcceptedReply(4, verf, RpcAcceptStat.PROC_UNAVAIL);
127127
const decoder = new RpcMessageDecoder();
128128
const reader = new Reader(encoded);
@@ -135,7 +135,7 @@ describe('RpcMessageEncoder', () => {
135135

136136
test('can encode GARBAGE_ARGS reply', () => {
137137
const encoder = new RpcMessageEncoder();
138-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
138+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
139139
const encoded = encoder.encodeAcceptedReply(5, verf, RpcAcceptStat.GARBAGE_ARGS);
140140
const decoder = new RpcMessageDecoder();
141141
const reader = new Reader(encoded);
@@ -148,7 +148,7 @@ describe('RpcMessageEncoder', () => {
148148

149149
test('can encode AcceptedReply with RpcMessage object', () => {
150150
const encoder = new RpcMessageEncoder();
151-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
151+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
152152
const msg = new RpcAcceptedReplyMessage(25, verf, RpcAcceptStat.SUCCESS);
153153
const encoded = encoder.encodeMessage(msg);
154154
const decoder = new RpcMessageDecoder();
@@ -210,8 +210,8 @@ describe('RpcMessageEncoder', () => {
210210
test('multiple messages can be encoded and decoded', () => {
211211
const encoder = new RpcMessageEncoder();
212212
const decoder = new RpcMessageDecoder();
213-
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
214-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
213+
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
214+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
215215
const encoded1 = encoder.encodeCall(100, 1000, 1, 0, cred, verf);
216216
const encoded2 = encoder.encodeCall(101, 1001, 1, 1, cred, verf);
217217
const encoded3 = encoder.encodeAcceptedReply(100, verf, RpcAcceptStat.SUCCESS);
@@ -236,14 +236,14 @@ describe('RpcMessageEncoder', () => {
236236
const credBody2 = new Uint8Array([1, 2]);
237237
const credBody3 = new Uint8Array([1, 2, 3]);
238238
const credBody4 = new Uint8Array([1, 2, 3, 4]);
239-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
239+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
240240
const testCred = (body: Uint8Array, xid: number) => {
241-
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_UNIX, body);
241+
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_UNIX, new Reader(body));
242242
const encoded = encoder.encodeCall(xid, 100, 1, 0, cred, verf);
243243
const reader = new Reader(encoded);
244244
const msg = decoder.decodeMessage(reader)!;
245245
expect(msg.xid).toBe(xid);
246-
expect((msg as RpcCallMessage).cred.body).toEqual(body);
246+
expect((msg as RpcCallMessage).cred.body.buf()).toEqual(body);
247247
};
248248
testCred(credBody1, 1);
249249
testCred(credBody2, 2);
@@ -256,8 +256,8 @@ describe('RpcMessageEncoder', () => {
256256
test('encodes CALL with procedure parameters', () => {
257257
const encoder = new RpcMessageEncoder();
258258
const decoder = new RpcMessageDecoder();
259-
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
260-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
259+
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
260+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
261261
const params = new Uint8Array([0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x45]);
262262
const encoded = encoder.encodeCall(1, 100, 1, 1, cred, verf, params);
263263
const reader = new Reader(encoded);
@@ -271,7 +271,7 @@ describe('RpcMessageEncoder', () => {
271271
test('encodes REPLY with result data', () => {
272272
const encoder = new RpcMessageEncoder();
273273
const decoder = new RpcMessageDecoder();
274-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
274+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
275275
const results = new Uint8Array([0x00, 0x00, 0x00, 0x7b]);
276276
const encoded = encoder.encodeAcceptedReply(1, verf, RpcAcceptStat.SUCCESS, undefined, results);
277277
const reader = new Reader(encoded);
@@ -285,8 +285,8 @@ describe('RpcMessageEncoder', () => {
285285
test('encodes RpcCallMessage with params field via encodeMessage', () => {
286286
const encoder = new RpcMessageEncoder();
287287
const decoder = new RpcMessageDecoder();
288-
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
289-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
288+
const cred = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
289+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
290290
const params = new Uint8Array([0x12, 0x34, 0x56, 0x78]);
291291
const msg = new RpcCallMessage(1, RPC_VERSION, 100, 1, 1, cred, verf, new Reader(params));
292292
const encoded = encoder.encodeMessage(msg);
@@ -300,7 +300,7 @@ describe('RpcMessageEncoder', () => {
300300
test('encodes RpcAcceptedReplyMessage with results field via encodeMessage', () => {
301301
const encoder = new RpcMessageEncoder();
302302
const decoder = new RpcMessageDecoder();
303-
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Uint8Array(0));
303+
const verf = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NULL, new Reader(new Uint8Array(0)));
304304
const results = new Uint8Array([0x00, 0x00, 0x01, 0x00]);
305305
const msg = new RpcAcceptedReplyMessage(1, verf, RpcAcceptStat.SUCCESS, undefined, new Reader(results));
306306
const encoded = encoder.encodeMessage(msg);

src/rpc/__tests__/fixtures.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('RPC Real-world Fixtures', () => {
3535
expect(call.verf.flavor).toBe(fixture.expected.verfFlavor);
3636
}
3737
if (fixture.expected.credBodyLength !== undefined) {
38-
expect(call.cred.body.length).toBe(fixture.expected.credBodyLength);
38+
expect(call.cred.body.buf().length).toBe(fixture.expected.credBodyLength);
3939
}
4040
} else if (fixture.expected.type === 'REPLY') {
4141
if (fixture.expected.replyStat === 'MSG_ACCEPTED') {
@@ -94,9 +94,9 @@ describe('RPC Real-world Fixtures', () => {
9494
expect(call2.vers).toBe(call1.vers);
9595
expect(call2.proc).toBe(call1.proc);
9696
expect(call2.cred.flavor).toBe(call1.cred.flavor);
97-
expect(call2.cred.body).toEqual(call1.cred.body);
97+
expect(call2.cred.body.subarray()).toEqual(call1.cred.body.subarray());
9898
expect(call2.verf.flavor).toBe(call1.verf.flavor);
99-
expect(call2.verf.body).toEqual(call1.verf.body);
99+
expect(call2.verf.body.subarray()).toEqual(call1.verf.body.subarray());
100100
} else if (msg1 instanceof RpcAcceptedReplyMessage) {
101101
expect(msg2).toBeInstanceOf(RpcAcceptedReplyMessage);
102102
const reply1 = msg1 as RpcAcceptedReplyMessage;
@@ -189,7 +189,7 @@ describe('RPC Real-world Fixtures', () => {
189189
const msg = decoder.decodeMessage(reader)!;
190190
expect(msg).toBeDefined();
191191
const call = msg as RpcCallMessage;
192-
expect(call.cred.body.length).toBe(fixture.expected.credBodyLength);
192+
expect(call.cred.body.buf().length).toBe(fixture.expected.credBodyLength);
193193
const encoder = new RpcMessageEncoder();
194194
const encoded = encoder.encodeMessage(msg);
195195
expect(encoded.length % 4).toBe(0);
@@ -336,8 +336,8 @@ describe('RPC Real-world Fixtures', () => {
336336
const call = msg as RpcCallMessage;
337337
expect(call.prog).toBe(100003);
338338
expect(call.proc).toBe(0);
339-
expect(call.cred.body.length).toBe(0);
340-
expect(call.verf.body.length).toBe(0);
339+
expect(call.cred.body.size()).toBe(0);
340+
expect(call.verf.body.size()).toBe(0);
341341
});
342342

343343
test('GETPORT response format is valid', () => {

src/rpc/__tests__/real-traces.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RmRecordDecoder } from "../../rm";
2-
import { RpcCallMessage, RpcMessage } from "../messages";
2+
import { RpcMessage } from "../messages";
33
import { RpcMessageDecoder } from "../RpcMessageDecoder";
44

55
const rmDecoder = new RmRecordDecoder();

src/rpc/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export {RpcMsgType, RpcReplyStat, RpcAcceptStat, RpcRejectStat, RpcAuthStat, Rpc
66
export class RpcOpaqueAuth {
77
constructor(
88
public readonly flavor: RpcAuthFlavor,
9-
public readonly body: Uint8Array,
9+
public readonly body: Reader,
1010
) {}
1111
}
1212

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,10 @@
605605
resolved "https://registry.yarnpkg.com/@jsonjoy.com/buffers/-/buffers-1.0.0.tgz#ade6895b7d3883d70f87b5743efaa12c71dfef7a"
606606
integrity sha512-NDigYR3PHqCnQLXYyoLbnEdzMMvzeiCWo1KOut7Q0CoIqg9tUAPKJ1iq/2nFhc5kZtexzutNY0LFjdwWL3Dw3Q==
607607

608-
"@jsonjoy.com/buffers@^1.1.0":
609-
version "1.1.0"
610-
resolved "https://registry.yarnpkg.com/@jsonjoy.com/buffers/-/buffers-1.1.0.tgz#0be6938bedd791d29acc79d2d2bb05c6e1302077"
611-
integrity sha512-QOOUerWq5DfN9PNKVn3+Pj7Aje+vtsi2qlSshnTF3ElWrVbtN4Mp4afo60xmqd7MfeRyvR/KLIxhVpJ9IxSzxg==
608+
"@jsonjoy.com/buffers@^1.2.0":
609+
version "1.2.0"
610+
resolved "https://registry.yarnpkg.com/@jsonjoy.com/buffers/-/buffers-1.2.0.tgz#57b9bbc509055de80f22cf6b696ac7efd7554046"
611+
integrity sha512-6RX+W5a+ZUY/c/7J5s5jK9UinLfJo5oWKh84fb4X0yK2q4WXEWUWZWuEMjvCb1YNUQhEAhUfr5scEGOH7jC4YQ==
612612

613613
"@jsonjoy.com/codegen@^1.0.0":
614614
version "1.0.0"

0 commit comments

Comments
 (0)