|
| 1 | +import {RESP} from './constants'; |
| 2 | +import {RespAttributes, RespPush, RespVerbatimString} from './extensions'; |
| 3 | +import {JsonPackExtension} from '../JsonPackExtension'; |
| 4 | +import {RespEncoder} from './RespEncoder'; |
| 5 | +import type {IWriter, IWriterGrowable} from '../../util/buffers'; |
| 6 | + |
| 7 | +const REG_RN = /[\r\n]/; |
| 8 | +const isSafeInteger = Number.isSafeInteger; |
| 9 | + |
| 10 | +/** |
| 11 | + * Implements RESP v2 encoding. |
| 12 | + */ |
| 13 | +export class RespEncoderLegacy<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> extends RespEncoder<W> { |
| 14 | + public writeAny(value: unknown): void { |
| 15 | + switch (typeof value) { |
| 16 | + case 'number': |
| 17 | + return this.writeNumber(value as number); |
| 18 | + case 'string': |
| 19 | + return this.writeStr(value); |
| 20 | + case 'boolean': |
| 21 | + return this.writeSimpleStr(value ? 'TRUE' : 'FALSE'); |
| 22 | + case 'object': { |
| 23 | + if (!value) return this.writeNull(); |
| 24 | + if (value instanceof Array) return this.writeArr(value); |
| 25 | + if (value instanceof Uint8Array) return this.writeBin(value); |
| 26 | + if (value instanceof Error) return this.writeErr(value.message); |
| 27 | + if (value instanceof Set) return this.writeSet(value); |
| 28 | + if (value instanceof JsonPackExtension) { |
| 29 | + if (value instanceof RespPush) return this.writeArr(value.val); |
| 30 | + if (value instanceof RespVerbatimString) return this.writeStr(value.val); |
| 31 | + if (value instanceof RespAttributes) return this.writeObj(value.val); |
| 32 | + } |
| 33 | + return this.writeObj(value as Record<string, unknown>); |
| 34 | + } |
| 35 | + case 'undefined': |
| 36 | + return this.writeUndef(); |
| 37 | + case 'bigint': |
| 38 | + return this.writeSimpleStrAscii(value + ''); |
| 39 | + default: |
| 40 | + return this.writeUnknown(value); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public writeNumber(num: number): void { |
| 45 | + if (isSafeInteger(num)) this.writeInteger(num); |
| 46 | + else this.writeSimpleStrAscii(num + ''); |
| 47 | + } |
| 48 | + |
| 49 | + public writeStr(str: string): void { |
| 50 | + const length = str.length; |
| 51 | + if (length < 64 && !REG_RN.test(str)) this.writeSimpleStr(str); |
| 52 | + else this.writeBulkStr(str); |
| 53 | + } |
| 54 | + |
| 55 | + public writeNull(): void { |
| 56 | + this.writeNullArr(); |
| 57 | + } |
| 58 | + |
| 59 | + public writeErr(str: string): void { |
| 60 | + if (str.length < 64 && !REG_RN.test(str)) this.writeSimpleErr(str); |
| 61 | + else this.writeBulkStr(str); |
| 62 | + } |
| 63 | + |
| 64 | + public writeSet(set: Set<unknown>): void { |
| 65 | + this.writeArr([...set]); |
| 66 | + } |
| 67 | + |
| 68 | + public writeArr(arr: unknown[]): void { |
| 69 | + const writer = this.writer; |
| 70 | + const length = arr.length; |
| 71 | + writer.u8(RESP.ARR); // * |
| 72 | + this.writeLength(length); |
| 73 | + writer.u16(RESP.RN); // \r\n |
| 74 | + for (let i = 0; i < length; i++) { |
| 75 | + const val = arr[i]; |
| 76 | + if (val === null) this.writeNullStr(); |
| 77 | + else this.writeAny(val); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public writeObj(obj: Record<string, unknown>): void { |
| 82 | + const writer = this.writer; |
| 83 | + const keys = Object.keys(obj); |
| 84 | + const length = keys.length; |
| 85 | + writer.u8(RESP.ARR); // % |
| 86 | + this.writeLength(length << 1); |
| 87 | + writer.u16(RESP.RN); // \r\n |
| 88 | + for (let i = 0; i < length; i++) { |
| 89 | + const key = keys[i]; |
| 90 | + this.writeStr(key); |
| 91 | + const val = obj[key]; |
| 92 | + if (val === null) this.writeNullStr(); |
| 93 | + else this.writeAny(val); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments