Skip to content

Commit

Permalink
Merge pull request #488 from streamich/resp-2
Browse files Browse the repository at this point in the history
RESP3 encoder improvements
  • Loading branch information
streamich authored Dec 10, 2023
2 parents bf3e322 + 5d65600 commit f0a4501
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 109 deletions.
74 changes: 50 additions & 24 deletions src/json-pack/cbor/CborEncoderFast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ export class CborEncoderFast<W extends IWriter & IWriterGrowable = IWriter & IWr
else writer.u8u64(0x5b, length);
}

public writeStartBin(): void {
this.writer.u8(0x5f);
}

public writeStr(str: string): void {
const writer = this.writer;
const length = str.length;
Expand Down Expand Up @@ -222,10 +218,6 @@ export class CborEncoderFast<W extends IWriter & IWriterGrowable = IWriter & IWr
this.writer.ascii(str);
}

public writeStartStr(): void {
this.writer.u8(0x7f);
}

public writeArr(arr: unknown[]): void {
const length = arr.length;
this.writeArrHdr(length);
Expand All @@ -241,14 +233,6 @@ export class CborEncoderFast<W extends IWriter & IWriterGrowable = IWriter & IWr
else writer.u8u64(0x9b, length);
}

public writeStartArr(): void {
this.writer.u8(0x9f);
}

public writeEndArr(): void {
this.writer.u8(CONST.END);
}

public writeObj(obj: Record<string, unknown>): void {
const keys = Object.keys(obj);
const length = keys.length;
Expand Down Expand Up @@ -277,14 +261,6 @@ export class CborEncoderFast<W extends IWriter & IWriterGrowable = IWriter & IWr
this.writer.u8(0xbf);
}

public writeStartObj(): void {
this.writer.u8(0xbf);
}

public writeEndObj(): void {
this.writer.u8(CONST.END);
}

public writeTag(tag: number, value: unknown): void {
this.writeTagHdr(tag);
this.writeAny(value);
Expand All @@ -304,4 +280,54 @@ export class CborEncoderFast<W extends IWriter & IWriterGrowable = IWriter & IWr
if (value <= 23) writer.u8(MAJOR_OVERLAY.TKN + value);
else if (value <= 0xff) writer.u16((0xf8 << 8) + value);
}

// ------------------------------------------------------- Streaming encoding

public writeStartStr(): void {
this.writer.u8(0x7f);
}

public writeStrChunk(str: string): void {
throw new Error('Not implemented');
}

public writeEndStr(): void {
throw new Error('Not implemented');
}

public writeStartBin(): void {
this.writer.u8(0x5f);
}

public writeBinChunk(buf: Uint8Array): void {
throw new Error('Not implemented');
}

public writeEndBin(): void {
throw new Error('Not implemented');
}

public writeStartArr(): void {
this.writer.u8(0x9f);
}

public writeArrChunk(item: unknown): void {
throw new Error('Not implemented');
}

public writeEndArr(): void {
this.writer.u8(CONST.END);
}

public writeStartObj(): void {
this.writer.u8(0xbf);
}

public writeObjChunk(key: string, value: unknown): void {
throw new Error('Not implemented');
}

public writeEndObj(): void {
this.writer.u8(CONST.END);
}
}
67 changes: 50 additions & 17 deletions src/json-pack/json/JsonEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {toBase64Bin} from '../../util/base64/toBase64Bin';
import type {IWriter, IWriterGrowable} from '../../util/buffers';
import type {Slice} from '../../util/buffers/Slice';
import type {BinaryJsonEncoder, StreamingBinaryJsonEncoder} from '../types';

export class JsonEncoder implements BinaryJsonEncoder, StreamingBinaryJsonEncoder {
Expand Down Expand Up @@ -158,14 +157,6 @@ export class JsonEncoder implements BinaryJsonEncoder, StreamingBinaryJsonEncode
writer.u8(0x5d); // ]
}

public writeStartArr(): void {
this.writer.u8(0x5b); // [
}

public writeEndArr(): void {
this.writer.u8(0x5d); // ]
}

public writeArrSeparator(): void {
this.writer.u8(0x2c); // ,
}
Expand All @@ -187,19 +178,61 @@ export class JsonEncoder implements BinaryJsonEncoder, StreamingBinaryJsonEncode
writer.uint8[writer.x - 1] = 0x7d; // }
}

public writeStartObj(): void {
this.writer.u8(0x7b); // {
}

public writeEndObj(): void {
this.writer.u8(0x7d); // }
}

public writeObjSeparator(): void {
this.writer.u8(0x2c); // ,
}

public writeObjKeySeparator(): void {
this.writer.u8(0x3a); // :
}

// ------------------------------------------------------- Streaming encoding

public writeStartStr(): void {
throw new Error('Method not implemented.');
}

public writeStrChunk(str: string): void {
throw new Error('Method not implemented.');
}

public writeEndStr(): void {
throw new Error('Method not implemented.');
}

public writeStartBin(): void {
throw new Error('Method not implemented.');
}

public writeBinChunk(buf: Uint8Array): void {
throw new Error('Method not implemented.');
}

public writeEndBin(): void {
throw new Error('Method not implemented.');
}

public writeStartArr(): void {
this.writer.u8(0x5b); // [
}

public writeArrChunk(item: unknown): void {
throw new Error('Method not implemented.');
}

public writeEndArr(): void {
this.writer.u8(0x5d); // ]
}

public writeStartObj(): void {
this.writer.u8(0x7b); // {
}

public writeObjChunk(key: string, value: unknown): void {
throw new Error('Method not implemented.');
}

public writeEndObj(): void {
this.writer.u8(0x7d); // }
}
}
Loading

0 comments on commit f0a4501

Please sign in to comment.