|
| 1 | +import {utf8Size} from '../../util/strings/utf8'; |
| 2 | +import {sort} from '../../util/sort/insertion'; |
| 3 | +import type {IWriter, IWriterGrowable} from '../../util/buffers'; |
| 4 | +import type {BinaryJsonEncoder} from '../types'; |
| 5 | + |
| 6 | +export class BencodeEncoder implements BinaryJsonEncoder { |
| 7 | + constructor(public readonly writer: IWriter & IWriterGrowable) {} |
| 8 | + |
| 9 | + public encode(value: unknown): Uint8Array { |
| 10 | + const writer = this.writer; |
| 11 | + writer.reset(); |
| 12 | + this.writeAny(value); |
| 13 | + return writer.flush(); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Called when the encoder encounters a value that it does not know how to encode. |
| 18 | + * |
| 19 | + * @param value Some JavaScript value. |
| 20 | + */ |
| 21 | + public writeUnknown(value: unknown): void { |
| 22 | + this.writeNull(); |
| 23 | + } |
| 24 | + |
| 25 | + public writeAny(value: unknown): void { |
| 26 | + switch (typeof value) { |
| 27 | + case 'boolean': |
| 28 | + return this.writeBoolean(value); |
| 29 | + case 'number': |
| 30 | + return this.writeNumber(value as number); |
| 31 | + case 'string': |
| 32 | + return this.writeStr(value); |
| 33 | + case 'object': { |
| 34 | + if (value === null) return this.writeNull(); |
| 35 | + const constructor = value.constructor; |
| 36 | + switch (constructor) { |
| 37 | + case Object: |
| 38 | + return this.writeObj(value as Record<string, unknown>); |
| 39 | + case Array: |
| 40 | + return this.writeArr(value as unknown[]); |
| 41 | + case Uint8Array: |
| 42 | + return this.writeBin(value as Uint8Array); |
| 43 | + case Map: |
| 44 | + return this.writeMap(value as Map<unknown, unknown>); |
| 45 | + case Set: |
| 46 | + return this.writeSet(value as Set<unknown>); |
| 47 | + default: |
| 48 | + return this.writeUnknown(value); |
| 49 | + } |
| 50 | + } |
| 51 | + case 'bigint': { |
| 52 | + return this.writeBigint(value); |
| 53 | + } |
| 54 | + case 'undefined': { |
| 55 | + return this.writeUndef(); |
| 56 | + } |
| 57 | + default: |
| 58 | + return this.writeUnknown(value); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public writeNull(): void { |
| 63 | + this.writer.u8(110); // 'n' |
| 64 | + } |
| 65 | + |
| 66 | + public writeUndef(): void { |
| 67 | + this.writer.u8(117); // 'u' |
| 68 | + } |
| 69 | + |
| 70 | + public writeBoolean(bool: boolean): void { |
| 71 | + this.writer.u8(bool ? 0x74 : 0x66); // 't' or 'f' |
| 72 | + } |
| 73 | + |
| 74 | + public writeNumber(num: number): void { |
| 75 | + const writer = this.writer; |
| 76 | + writer.u8(0x69); // 'i' |
| 77 | + writer.ascii(Math.round(num) + ''); |
| 78 | + writer.u8(0x65); // 'e' |
| 79 | + } |
| 80 | + |
| 81 | + public writeInteger(int: number): void { |
| 82 | + const writer = this.writer; |
| 83 | + writer.u8(0x69); // 'i' |
| 84 | + writer.ascii(int + ''); |
| 85 | + writer.u8(0x65); // 'e' |
| 86 | + } |
| 87 | + |
| 88 | + public writeUInteger(uint: number): void { |
| 89 | + this.writeInteger(uint); |
| 90 | + } |
| 91 | + |
| 92 | + public writeFloat(float: number): void { |
| 93 | + this.writeNumber(float); |
| 94 | + } |
| 95 | + |
| 96 | + public writeBigint(int: bigint): void { |
| 97 | + const writer = this.writer; |
| 98 | + writer.u8(0x69); // 'i' |
| 99 | + writer.ascii(int + ''); |
| 100 | + writer.u8(0x65); // 'e' |
| 101 | + } |
| 102 | + |
| 103 | + public writeBin(buf: Uint8Array): void { |
| 104 | + const writer = this.writer; |
| 105 | + const length = buf.length; |
| 106 | + writer.ascii(length + ''); |
| 107 | + writer.u8(0x3a); // ':' |
| 108 | + writer.buf(buf, length); |
| 109 | + } |
| 110 | + |
| 111 | + public writeStr(str: string): void { |
| 112 | + const writer = this.writer; |
| 113 | + const length = utf8Size(str); |
| 114 | + writer.ascii(length + ''); |
| 115 | + writer.u8(0x3a); // ':' |
| 116 | + writer.ensureCapacity(length); |
| 117 | + writer.utf8(str); |
| 118 | + } |
| 119 | + |
| 120 | + public writeAsciiStr(str: string): void { |
| 121 | + const writer = this.writer; |
| 122 | + writer.ascii(str.length + ''); |
| 123 | + writer.u8(0x3a); // ':' |
| 124 | + writer.ascii(str); |
| 125 | + } |
| 126 | + |
| 127 | + public writeArr(arr: unknown[]): void { |
| 128 | + const writer = this.writer; |
| 129 | + writer.u8(0x6c); // 'l' |
| 130 | + const length = arr.length; |
| 131 | + for (let i = 0; i < length; i++) this.writeAny(arr[i]); |
| 132 | + writer.u8(0x65); // 'e' |
| 133 | + } |
| 134 | + |
| 135 | + public writeObj(obj: Record<string, unknown>): void { |
| 136 | + const writer = this.writer; |
| 137 | + writer.u8(0x64); // 'd' |
| 138 | + const keys = sort(Object.keys(obj)); |
| 139 | + const length = keys.length; |
| 140 | + for (let i = 0; i < length; i++) { |
| 141 | + const key = keys[i]; |
| 142 | + this.writeStr(key); |
| 143 | + this.writeAny(obj[key]); |
| 144 | + } |
| 145 | + writer.u8(0x65); // 'e' |
| 146 | + } |
| 147 | + |
| 148 | + public writeMap(obj: Map<unknown, unknown>): void { |
| 149 | + const writer = this.writer; |
| 150 | + writer.u8(0x64); // 'd' |
| 151 | + const keys = sort([...obj.keys()]); |
| 152 | + const length = keys.length; |
| 153 | + for (let i = 0; i < length; i++) { |
| 154 | + const key = keys[i]; |
| 155 | + this.writeStr(key + ''); |
| 156 | + this.writeAny(obj.get(key)); |
| 157 | + } |
| 158 | + writer.u8(0x65); // 'e' |
| 159 | + } |
| 160 | + |
| 161 | + public writeSet(set: Set<unknown>): void { |
| 162 | + this.writeArr([...set.values()]); |
| 163 | + } |
| 164 | +} |
0 commit comments