basant307/AI_Governance_Project
048
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.BencodeEncoder = void 0;4const utf8_1 = require("@jsonjoy.com/util/lib/strings/utf8");5const insertion_1 = require("@jsonjoy.com/util/lib/sort/insertion");6class BencodeEncoder {7 constructor(writer) {8 this.writer = writer;9 }10 encode(value) {11 const writer = this.writer;12 writer.reset();13 this.writeAny(value);14 return writer.flush();15 }16 writeUnknown(value) {17 this.writeNull();18 }19 writeAny(value) {20 switch (typeof value) {21 case 'boolean':22 return this.writeBoolean(value);23 case 'number':24 return this.writeNumber(value);25 case 'string':26 return this.writeStr(value);27 case 'object': {28 if (value === null)29 return this.writeNull();30 const constructor = value.constructor;31 switch (constructor) {32 case Object:33 return this.writeObj(value);34 case Array:35 return this.writeArr(value);36 case Uint8Array:37 return this.writeBin(value);38 case Map:39 return this.writeMap(value);40 case Set:41 return this.writeSet(value);42 default:43 return this.writeUnknown(value);44 }45 }46 case 'bigint': {47 return this.writeBigint(value);48 }49 case 'undefined': {50 return this.writeUndef();51 }52 default:53 return this.writeUnknown(value);54 }55 }56 writeNull() {57 this.writer.u8(110);58 }59 writeUndef() {60 this.writer.u8(117);61 }62 writeBoolean(bool) {63 this.writer.u8(bool ? 0x74 : 0x66);64 }65 writeNumber(num) {66 const writer = this.writer;67 writer.u8(0x69);68 writer.ascii(Math.round(num) + '');69 writer.u8(0x65);70 }71 writeInteger(int) {72 const writer = this.writer;73 writer.u8(0x69);74 writer.ascii(int + '');75 writer.u8(0x65);76 }77 writeUInteger(uint) {78 this.writeInteger(uint);79 }80 writeFloat(float) {81 this.writeNumber(float);82 }83 writeBigint(int) {84 const writer = this.writer;85 writer.u8(0x69);86 writer.ascii(int + '');87 writer.u8(0x65);88 }89 writeBin(buf) {90 const writer = this.writer;91 const length = buf.length;92 writer.ascii(length + '');93 writer.u8(0x3a);94 writer.buf(buf, length);95 }96 writeStr(str) {97 const writer = this.writer;98 const length = (0, utf8_1.utf8Size)(str);99 writer.ascii(length + '');100 writer.u8(0x3a);101 writer.ensureCapacity(str.length * 4);102 writer.utf8(str);103 }104 writeAsciiStr(str) {105 const writer = this.writer;106 writer.ascii(str.length + '');107 writer.u8(0x3a);108 writer.ascii(str);109 }110 writeArr(arr) {111 const writer = this.writer;112 writer.u8(0x6c);113 const length = arr.length;114 for (let i = 0; i < length; i++)115 this.writeAny(arr[i]);116 writer.u8(0x65);117 }118 writeObj(obj) {119 const writer = this.writer;120 writer.u8(0x64);121 const keys = (0, insertion_1.sort)(Object.keys(obj));122 const length = keys.length;123 for (let i = 0; i < length; i++) {124 const key = keys[i];125 this.writeStr(key);126 this.writeAny(obj[key]);127 }128 writer.u8(0x65);129 }130 writeMap(obj) {131 const writer = this.writer;132 writer.u8(0x64);133 const keys = (0, insertion_1.sort)([...obj.keys()]);134 const length = keys.length;135 for (let i = 0; i < length; i++) {136 const key = keys[i];137 this.writeStr(key + '');138 this.writeAny(obj.get(key));139 }140 writer.u8(0x65);141 }142 writeSet(set) {143 this.writeArr([...set.values()]);144 }145}146exports.BencodeEncoder = BencodeEncoder;147//# sourceMappingURL=BencodeEncoder.js.map