git ssb

0+

farewellutopia-dev / deno-ssb-experiments



Commit ddd0d177815aa14350555ac08b857b7d8b8832a4

supporting rpc messages longer than 4096 bytes

Reto Gmür committed on 7/5/2021, 8:28:55 PM
Parent: c6d42e2e0402517cfddba9ff7766e5d0ffeb86f5

Files changed

RPCConnection.tschanged
SsbHost.tschanged
play.tschanged
RPCConnection.tsView
@@ -41,9 +41,9 @@
4141 if (headerBytes.length !== 9) {
4242 throw new Error("expected 9 headerBytes bytes, got " + headerBytes);
4343 }
4444 const header = parseHeader(headerBytes);
45- const body = await boxConnection.read(); //readBytes(boxConnection,9);
45 + const body = await boxConnection.readTill(header.bodyLength); //readBytes(boxConnection,9);
4646 if (body.length !== header.bodyLength) {
4747 throw new Error(
4848 `expected a body of length ${header.bodyLength} but got ${body.length}`,
4949 );
SsbHost.tsView
@@ -6,8 +6,9 @@
66 await sodium.ready;
77
88 interface BoxConnection {
99 read: () => Promise<Uint8Array>;
10 + readTill: (length: number) => Promise<Uint8Array>;
1011 [Symbol.asyncIterator]: () => AsyncGenerator<Uint8Array>;
1112 write: (message: Uint8Array) => Promise<void>;
1213 close: () => void;
1314 }
@@ -218,8 +219,26 @@
218219 yield nextValue;
219220 }
220221 }
221222 },
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 + },
222241 async read() {
223242 const headerBox = await readBytes(conn, 34);
224243 const header = sodium.crypto_box_open_easy_afternm(
225244 headerBox,
play.tsView
@@ -47,9 +47,9 @@
4747 "args": [{ "id": `@${address.key}.ed25519` }],
4848 });
4949 while (true) {
5050 const msg = await historyStream.read();
51- console.log(decoder.decode(msg));
51 + console.log(JSON.stringify(JSON.parse(decoder.decode(msg)), undefined, 2));
5252 }
5353 /*
5454 const waitForInactivity = async () => {
5555 if (Date.now() - lastActivity > 5000) {

Built with git-ssb-web