Skip to content

Commit

Permalink
feat(json-pack): 🎸 add ability to encode streaming blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 10, 2023
1 parent f378f1c commit 773a217
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/json-pack/resp/RespEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,29 @@ export class RespEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriter
}

public writeStartBin(): void {
throw new Error('Not implemented');
this.writer.u32(
36 * 0x1000000 + // $
(63 << 16) + // ?
RESP.RN, // \r\n
);
}

public writeBinChunk(buf: Uint8Array): void {
throw new Error('Not implemented');
const writer = this.writer;
const length = buf.length;
writer.u8(59); // ;
writer.ascii(length + '');
writer.u16(RESP.RN); // \r\n
writer.buf(buf, length);
writer.u16(RESP.RN); // \r\n
}

public writeEndBin(): void {
throw new Error('Not implemented');
this.writer.u32(
59 * 0x1000000 + // ;
(48 << 16) + // 0
RESP.RN, // \r\n
);
}

public writeStartArr(): void {
Expand Down
12 changes: 12 additions & 0 deletions src/json-pack/resp/__tests__/RespEncoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,16 @@ describe('streaming data', () => {
expect(toStr(encoded)).toBe('$?\r\n;3\r\nabc\r\n;3\r\ndef\r\n;0\r\n');
});
});

describe('binary', () => {
test('can write a streaming binary', () => {
const encoder = new RespEncoder();
encoder.writeStartBin();
encoder.writeBinChunk(new Uint8Array([65]));
encoder.writeBinChunk(new Uint8Array([66]));
encoder.writeEndBin();
const encoded = encoder.writer.flush();
expect(toStr(encoded)).toBe('$?\r\n;1\r\nA\r\n;1\r\nB\r\n;0\r\n');
});
});
});

0 comments on commit 773a217

Please sign in to comment.