Commit ddd0d177815aa14350555ac08b857b7d8b8832a4
supporting rpc messages longer than 4096 bytes
Reto Gmür committed on 7/5/2021, 8:28:55 PMParent: c6d42e2e0402517cfddba9ff7766e5d0ffeb86f5
Files changed
RPCConnection.ts | changed |
SsbHost.ts | changed |
play.ts | changed |
RPCConnection.ts | ||
---|---|---|
@@ -41,9 +41,9 @@ | ||
41 | 41 … | if (headerBytes.length !== 9) { |
42 | 42 … | throw new Error("expected 9 headerBytes bytes, got " + headerBytes); |
43 | 43 … | } |
44 | 44 … | const header = parseHeader(headerBytes); |
45 | - const body = await boxConnection.read(); //readBytes(boxConnection,9); | |
45 … | + const body = await boxConnection.readTill(header.bodyLength); //readBytes(boxConnection,9); | |
46 | 46 … | if (body.length !== header.bodyLength) { |
47 | 47 … | throw new Error( |
48 | 48 … | `expected a body of length ${header.bodyLength} but got ${body.length}`, |
49 | 49 … | ); |
SsbHost.ts | ||
---|---|---|
@@ -6,8 +6,9 @@ | ||
6 | 6 … | await sodium.ready; |
7 | 7 … | |
8 | 8 … | interface BoxConnection { |
9 | 9 … | read: () => Promise<Uint8Array>; |
10 … | + readTill: (length: number) => Promise<Uint8Array>; | |
10 | 11 … | [Symbol.asyncIterator]: () => AsyncGenerator<Uint8Array>; |
11 | 12 … | write: (message: Uint8Array) => Promise<void>; |
12 | 13 … | close: () => void; |
13 | 14 … | } |
@@ -218,8 +219,26 @@ | ||
218 | 219 … | yield nextValue; |
219 | 220 … | } |
220 | 221 … | } |
221 | 222 … | }, |
223 … | + async readTill(length: number) { | |
224 … | + const chunks: Uint8Array[] = []; | |
225 … | + while ( | |
226 … | + chunks.reduce((previous, chunk) => previous + chunk.length, 0) < | |
227 … | + length | |
228 … | + ) { | |
229 … | + chunks.push(await this.read()); | |
230 … | + } | |
231 … | + if ( | |
232 … | + chunks.reduce((previous, chunk) => previous + chunk.length, 0) > | |
233 … | + length | |
234 … | + ) { | |
235 … | + throw new Error( | |
236 … | + `Requested number of bytes doesn't match the received chunks`, | |
237 … | + ); | |
238 … | + } | |
239 … | + return concat(...chunks); | |
240 … | + }, | |
222 | 241 … | async read() { |
223 | 242 … | const headerBox = await readBytes(conn, 34); |
224 | 243 … | const header = sodium.crypto_box_open_easy_afternm( |
225 | 244 … | headerBox, |
play.ts | ||
---|---|---|
@@ -47,9 +47,9 @@ | ||
47 | 47 … | "args": [{ "id": `@${address.key}.ed25519` }], |
48 | 48 … | }); |
49 | 49 … | while (true) { |
50 | 50 … | const msg = await historyStream.read(); |
51 | - console.log(decoder.decode(msg)); | |
51 … | + console.log(JSON.stringify(JSON.parse(decoder.decode(msg)), undefined, 2)); | |
52 | 52 … | } |
53 | 53 … | /* |
54 | 54 … | const waitForInactivity = async () => { |
55 | 55 … | if (Date.now() - lastActivity > 5000) { |
Built with git-ssb-web