Skip to content

Commit 33a3446

Browse files
committed
mark public or private for Encoder / Decoder
1 parent bc71d5c commit 33a3446

2 files changed

Lines changed: 85 additions & 85 deletions

File tree

src/Decoder.ts

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,37 @@ const DEFAULT_MAX_LENGTH = 0xffff_ffff; // uint32_max
6161
const sharedCachedKeyDecoder = new CachedKeyDecoder();
6262

6363
export class Decoder<ContextType> {
64-
totalPos = 0;
65-
pos = 0;
66-
67-
view = EMPTY_VIEW;
68-
bytes = EMPTY_BYTES;
69-
headByte = HEAD_BYTE_REQUIRED;
70-
readonly stack: Array<StackState> = [];
71-
72-
constructor(
73-
readonly context: ContextType,
74-
readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
75-
readonly maxStrLength = DEFAULT_MAX_LENGTH,
76-
readonly maxBinLength = DEFAULT_MAX_LENGTH,
77-
readonly maxArrayLength = DEFAULT_MAX_LENGTH,
78-
readonly maxMapLength = DEFAULT_MAX_LENGTH,
79-
readonly maxExtLength = DEFAULT_MAX_LENGTH,
80-
readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder,
64+
private totalPos = 0;
65+
private pos = 0;
66+
67+
private view = EMPTY_VIEW;
68+
private bytes = EMPTY_BYTES;
69+
private headByte = HEAD_BYTE_REQUIRED;
70+
private readonly stack: Array<StackState> = [];
71+
72+
public constructor(
73+
private readonly context: ContextType,
74+
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
75+
private readonly maxStrLength = DEFAULT_MAX_LENGTH,
76+
private readonly maxBinLength = DEFAULT_MAX_LENGTH,
77+
private readonly maxArrayLength = DEFAULT_MAX_LENGTH,
78+
private readonly maxMapLength = DEFAULT_MAX_LENGTH,
79+
private readonly maxExtLength = DEFAULT_MAX_LENGTH,
80+
private readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder,
8181
) {}
8282

8383
private reinitializeState() {
8484
this.totalPos = 0;
8585
this.headByte = HEAD_BYTE_REQUIRED;
8686
}
8787

88-
setBuffer(buffer: ArrayLike<number> | ArrayBuffer): void {
88+
private setBuffer(buffer: ArrayLike<number> | ArrayBuffer): void {
8989
this.bytes = ensureUint8Array(buffer);
9090
this.view = createDataView(this.bytes);
9191
this.pos = 0;
9292
}
9393

94-
appendBuffer(buffer: ArrayLike<number>) {
94+
public appendBuffer(buffer: ArrayLike<number>) {
9595
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining()) {
9696
this.setBuffer(buffer);
9797
} else {
@@ -105,11 +105,11 @@ export class Decoder<ContextType> {
105105
}
106106
}
107107

108-
hasRemaining(size = 1) {
108+
private hasRemaining(size = 1) {
109109
return this.view.byteLength - this.pos >= size;
110110
}
111111

112-
createNoExtraBytesError(posToShow: number): Error {
112+
private createNoExtraBytesError(posToShow: number): Error {
113113
const { view, pos } = this;
114114
return new RangeError(`Extra ${view.byteLength - pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`);
115115
}
@@ -118,7 +118,7 @@ export class Decoder<ContextType> {
118118
* A synchronous interface to decode a byte buffer. It mutates the decoder instance.
119119
* @param buffer A byte buffer encoded in MessagePack.
120120
*/
121-
decodeSync(buffer: ArrayLike<number> | ArrayBuffer): unknown {
121+
public decodeSync(buffer: ArrayLike<number> | ArrayBuffer): unknown {
122122
this.reinitializeState();
123123
this.setBuffer(buffer);
124124
return this.doDecodeSingleSync();
@@ -132,7 +132,7 @@ export class Decoder<ContextType> {
132132
return object;
133133
}
134134

135-
async decodeSingleAsync(stream: AsyncIterable<ArrayLike<number>>): Promise<unknown> {
135+
public async decodeSingleAsync(stream: AsyncIterable<ArrayLike<number>>): Promise<unknown> {
136136
let decoded = false;
137137
let object: unknown;
138138
for await (const buffer of stream) {
@@ -167,11 +167,11 @@ export class Decoder<ContextType> {
167167
);
168168
}
169169

170-
decodeArrayStream(stream: AsyncIterable<ArrayLike<number>>) {
170+
public decodeArrayStream(stream: AsyncIterable<ArrayLike<number>>) {
171171
return this.decodeMultiAsync(stream, true);
172172
}
173173

174-
decodeStream(stream: AsyncIterable<ArrayLike<number>>) {
174+
public decodeStream(stream: AsyncIterable<ArrayLike<number>>) {
175175
return this.decodeMultiAsync(stream, false);
176176
}
177177

@@ -425,7 +425,7 @@ export class Decoder<ContextType> {
425425
}
426426
}
427427

428-
readHeadByte(): number {
428+
private readHeadByte(): number {
429429
if (this.headByte === HEAD_BYTE_REQUIRED) {
430430
this.headByte = this.readU8();
431431
// console.log("headByte", prettyByte(this.headByte));
@@ -434,11 +434,11 @@ export class Decoder<ContextType> {
434434
return this.headByte;
435435
}
436436

437-
complete(): void {
437+
private complete(): void {
438438
this.headByte = HEAD_BYTE_REQUIRED;
439439
}
440440

441-
readArraySize(): number {
441+
private readArraySize(): number {
442442
const headByte = this.readHeadByte();
443443

444444
switch (headByte) {
@@ -456,7 +456,7 @@ export class Decoder<ContextType> {
456456
}
457457
}
458458

459-
pushMapState(size: number) {
459+
private pushMapState(size: number) {
460460
if (size > this.maxMapLength) {
461461
throw new Error(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
462462
}
@@ -470,7 +470,7 @@ export class Decoder<ContextType> {
470470
});
471471
}
472472

473-
pushArrayState(size: number) {
473+
private pushArrayState(size: number) {
474474
if (size > this.maxArrayLength) {
475475
throw new Error(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
476476
}
@@ -483,7 +483,7 @@ export class Decoder<ContextType> {
483483
});
484484
}
485485

486-
decodeUtf8String(byteLength: number, headerOffset: number): string {
486+
private decodeUtf8String(byteLength: number, headerOffset: number): string {
487487
if (byteLength > this.maxStrLength) {
488488
throw new Error(`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`);
489489
}
@@ -505,15 +505,15 @@ export class Decoder<ContextType> {
505505
return object;
506506
}
507507

508-
stateIsMapKey(): boolean {
508+
private stateIsMapKey(): boolean {
509509
if (this.stack.length > 0) {
510510
const state = this.stack[this.stack.length - 1];
511511
return state.type === State.MAP_KEY;
512512
}
513513
return false;
514514
}
515515

516-
decodeBinary(byteLength: number, headOffset: number): Uint8Array {
516+
private decodeBinary(byteLength: number, headOffset: number): Uint8Array {
517517
if (byteLength > this.maxBinLength) {
518518
throw new Error(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
519519
}
@@ -528,7 +528,7 @@ export class Decoder<ContextType> {
528528
return object;
529529
}
530530

531-
decodeExtension(size: number, headOffset: number): unknown {
531+
private decodeExtension(size: number, headOffset: number): unknown {
532532
if (size > this.maxExtLength) {
533533
throw new Error(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
534534
}
@@ -538,73 +538,73 @@ export class Decoder<ContextType> {
538538
return this.extensionCodec.decode(data, extType, this.context);
539539
}
540540

541-
lookU8() {
541+
private lookU8() {
542542
return this.view.getUint8(this.pos);
543543
}
544544

545-
lookU16() {
545+
private lookU16() {
546546
return this.view.getUint16(this.pos);
547547
}
548548

549-
lookU32() {
549+
private lookU32() {
550550
return this.view.getUint32(this.pos);
551551
}
552552

553-
readU8(): number {
553+
private readU8(): number {
554554
const value = this.view.getUint8(this.pos);
555555
this.pos++;
556556
return value;
557557
}
558558

559-
readI8(): number {
559+
private readI8(): number {
560560
const value = this.view.getInt8(this.pos);
561561
this.pos++;
562562
return value;
563563
}
564564

565-
readU16(): number {
565+
private readU16(): number {
566566
const value = this.view.getUint16(this.pos);
567567
this.pos += 2;
568568
return value;
569569
}
570570

571-
readI16(): number {
571+
private readI16(): number {
572572
const value = this.view.getInt16(this.pos);
573573
this.pos += 2;
574574
return value;
575575
}
576576

577-
readU32(): number {
577+
private readU32(): number {
578578
const value = this.view.getUint32(this.pos);
579579
this.pos += 4;
580580
return value;
581581
}
582582

583-
readI32(): number {
583+
private readI32(): number {
584584
const value = this.view.getInt32(this.pos);
585585
this.pos += 4;
586586
return value;
587587
}
588588

589-
readU64(): number {
589+
private readU64(): number {
590590
const value = getUint64(this.view, this.pos);
591591
this.pos += 8;
592592
return value;
593593
}
594594

595-
readI64(): number {
595+
private readI64(): number {
596596
const value = getInt64(this.view, this.pos);
597597
this.pos += 8;
598598
return value;
599599
}
600600

601-
readF32() {
601+
private readF32() {
602602
const value = this.view.getFloat32(this.pos);
603603
this.pos += 4;
604604
return value;
605605
}
606606

607-
readF64() {
607+
private readF64() {
608608
const value = this.view.getFloat64(this.pos);
609609
this.pos += 8;
610610
return value;

0 commit comments

Comments
 (0)