| 1 | "use strict"; |
| 2 | // @flow |
| 3 | // exports.__esModule = true; |
| 4 | // first bit defines type |
| 5 | // last bit defines length (if 16 or 'f' than it is variable length - hence it handles up to 14) |
| 6 | // overflow has it's own key code (STR_40 || BUF_20) |
| 7 | // variable length structures will have a TERM(inal) code after words (ex. string or buffer of size 23) |
| 8 | // Lists and Dictionaries (structures) will also have a TERM(inal) codea fter words |
| 9 | var LIST = Uint8Array.from([0x01]), DICT = Uint8Array.from([0x02]), NUM = Uint8Array.from([0x03]), STR = Uint8Array.from([0x10]), STR_40 = Uint8Array.from([0x20]), STR_64 = Uint8Array.from([0x21]), STR_128 = Uint8Array.from([0x22]), STR_256 = Uint8Array.from([0x23]), STR_512 = Uint8Array.from([0x24]), TRUE = Uint8Array.from([0x30]), FALSE = Uint8Array.from([0x31]), NULL = Uint8Array.from([0x32]), BUF = Uint8Array.from([0x40]), BUF_20 = Uint8Array.from([0x50]), BUF_32 = Uint8Array.from([0x51]), BUF_64 = Uint8Array.from([0x52]), BUF_128 = Uint8Array.from([0x53]), BUF_256 = Uint8Array.from([0x54]), INT_8 = Uint8Array.from([0x60]), INT_16LE = Uint8Array.from([0x61]), INT_16BE = Uint8Array.from([0x62]), INT_32LE = Uint8Array.from([0x63]), INT_32BE = Uint8Array.from([0x64]), INT_64LE = Uint8Array.from([0x65]), INT_64BE = Uint8Array.from([0x66]), U_INT_8 = Uint8Array.from([0x67]), U_INT_16LE = Uint8Array.from([0x68]), U_INT_16BE = Uint8Array.from([0x69]), U_INT_32LE = Uint8Array.from([0x6a]), U_INT_32BE = Uint8Array.from([0x6b]), U_INT_64LE = Uint8Array.from([0x6c]), U_INT_64BE = Uint8Array.from([0x6d]), TERM = Uint8Array.from([0xFF]); |
| 10 | function encode(input) { |
| 11 | if (input === null || input === undefined) |
| 12 | return NULL; |
| 13 | else if (input === true) |
| 14 | return TRUE; |
| 15 | else if (input === false) |
| 16 | return FALSE; |
| 17 | else if (typeof input === 'number') |
| 18 | return _encodeNumber(input); |
| 19 | else if (typeof input === 'string') |
| 20 | return _encodeString(input); |
| 21 | else if (Array.isArray(input)) |
| 22 | return new Uint8Array([LIST, input.map(function (item) { return encode(item); }), TERM]); |
| 23 | else if (typeof input === 'object') |
| 24 | return new Uint8Array([DICT, _encodeObject(input), TERM]); |
| 25 | else if (Buffer.isBuffer(input)) |
| 26 | return _encodeBuffer(input); |
| 27 | else if (input instanceof Uint8Array) |
| 28 | return input; |
| 29 | else |
| 30 | throw "well, shucks"; |
| 31 | } |
| 32 | var rencode = Object(); |
| 33 | rencode.encode = encode; |
| 34 | function _encodeNumber(input) { |
| 35 | var b, t; |
| 36 | console.log("INPUT", input); |
| 37 | if (input < 0) { |
| 38 | // Signed |
| 39 | if (input >= (-127)) { |
| 40 | t = INT_8; |
| 41 | b = new Buffer(1); |
| 42 | b.writeInt8(input, 0); |
| 43 | } // 8 |
| 44 | else if (input >= (-32767)) { |
| 45 | t = INT_16BE; |
| 46 | b = new Buffer(2); |
| 47 | b.writeInt16BE(input, 0); |
| 48 | } // 16 |
| 49 | else if (input >= (-2147483647)) { |
| 50 | t = INT_32BE; |
| 51 | b = new Buffer(4); |
| 52 | b.writeInt32BE(input, 0); |
| 53 | } // 32 |
| 54 | // else {b = new Buffer(8); b.writeUInt64BE(input);} // 64 |
| 55 | } |
| 56 | else { |
| 57 | // Unsigned |
| 58 | if (input <= 255) { |
| 59 | t = U_INT_8; |
| 60 | b = new Buffer(1); |
| 61 | b.writeUInt8(input, 0); |
| 62 | } // 8 |
| 63 | else if (input <= 65535) { |
| 64 | t = U_INT_16BE; |
| 65 | b = new Buffer(2); |
| 66 | b.writeUInt16BE(input, 0); |
| 67 | } // 16 |
| 68 | else if (input <= 4294967295) { |
| 69 | t = U_INT_32BE; |
| 70 | b = new Buffer(4); |
| 71 | b.writeUInt32BE(input, 0); |
| 72 | } // 32 |
| 73 | // else b = new Buffer(8).writeInt8BE(input); // 64 |
| 74 | } |
| 75 | console.log("BBBBBUUUUUUFFFFFEEERRRR", b); |
| 76 | return _concatBuffers(t, b); |
| 77 | } |
| 78 | function _encodeBuffer(input) { |
| 79 | input = new Uint8Array(input); |
| 80 | if (input.length <= 14) |
| 81 | return new Uint8Array([input.length + 64, input]); |
| 82 | else if (input.length === 20) |
| 83 | return new Uint8Array([BUF_20, input]); |
| 84 | else if (input.length === 32) |
| 85 | return new Uint8Array([BUF_32, input]); |
| 86 | else if (input.length === 64) |
| 87 | return new Uint8Array([BUF_64, input]); |
| 88 | else if (input.length === 128) |
| 89 | return new Uint8Array([BUF_128, input]); |
| 90 | else if (input.length === 256) |
| 91 | return new Uint8Array([BUF_256, input]); |
| 92 | else |
| 93 | return new Uint8Array([BUF, input, TERM]); // TODO: buffer will have 255 in it.... |
| 94 | } |
| 95 | function _encodeString(input) { |
| 96 | console.log("ENCODE STRING"); |
| 97 | var b = Buffer.from(input); |
| 98 | b = new Uint8Array(b); |
| 99 | if (input.length <= 14) |
| 100 | return new Uint8Array([input.length + 16, b]); |
| 101 | else if (input.length === 40) |
| 102 | return new Uint8Array([STR_40, b]); |
| 103 | else if (input.length === 64) |
| 104 | return new Uint8Array([STR_64, b]); |
| 105 | else if (input.length === 128) |
| 106 | return new Uint8Array([STR_128, b]); |
| 107 | else if (input.length === 256) |
| 108 | return new Uint8Array([STR_256, b]); |
| 109 | else if (input.length === 512) |
| 110 | return new Uint8Array([STR_512, b]); |
| 111 | else |
| 112 | return new Uint8Array([STR, b, TERM]); |
| 113 | } |
| 114 | function _encodeObject(input) { |
| 115 | return new Uint8Array(Object.keys(input).map(function (key) { return [encode(key), encode(input[key])]; })); |
| 116 | } |
| 117 | function decode(input) { |
| 118 | if (input[0] === NULL) |
| 119 | return null; |
| 120 | else if (input[0] === TRUE) |
| 121 | return true; |
| 122 | else if (input[0] === FALSE) |
| 123 | return false; |
| 124 | else if (input[0] === LIST) |
| 125 | return _decodeList(input.slice(1)); |
| 126 | else if (input[0] === DICT) |
| 127 | return _decodeDict(input.slice(1)); |
| 128 | else if (input[0] >= 16 && input[0] <= 36) |
| 129 | return _decodeString(input); |
| 130 | else if (input[0] >= 64 && input[0] <= 84) |
| 131 | return _decodeBuffer(input); |
| 132 | else if (input[0] >= 96 && input[0] <= 109) |
| 133 | return _decodeNumber(input); |
| 134 | } |
| 135 | rencode.decode = decode; |
| 136 | function _decodeList(input) { |
| 137 | return []; |
| 138 | } |
| 139 | function _decodeDict(input) { |
| 140 | return {}; |
| 141 | } |
| 142 | function _decodeNumber(input) { |
| 143 | // INT_8 = Uint8Array.from([0x60]), |
| 144 | // INT_16LE = Uint8Array.from([0x61]), |
| 145 | // INT_16BE = Uint8Array.from([0x62]), |
| 146 | // INT_32LE = Uint8Array.from([0x63]), |
| 147 | // INT_32BE = Uint8Array.from([0x64]), |
| 148 | // INT_64LE = Uint8Array.from([0x65]), |
| 149 | // INT_64BE = Uint8Array.from([0x66]), |
| 150 | // U_INT_8 = Uint8Array.from([0x67]), |
| 151 | // U_INT_16LE = Uint8Array.from([0x68]), |
| 152 | // U_INT_16BE = Uint8Array.from([0x69]), |
| 153 | // U_INT_32LE = Uint8Array.from([0x6a]), |
| 154 | // U_INT_32BE = Uint8Array.from([0x6b]), |
| 155 | // U_INT_64LE = Uint8Array.from([0x6c]), |
| 156 | // U_INT_64BE = Uint8Array.from([0x6d]), |
| 157 | } |
| 158 | function _decodeBuffer(input) { |
| 159 | // BUF = Uint8Array.from([0x40]), |
| 160 | // BUF_20 = Uint8Array.from([0x50]), |
| 161 | // BUF_32 = Uint8Array.from([0x51]), |
| 162 | // BUF_64 = Uint8Array.from([0x52]), |
| 163 | // BUF_128 = Uint8Array.from([0x53]), |
| 164 | // BUF_256 = Uint8Array.from([0x54]), |
| 165 | } |
| 166 | function _decodeString(input) { |
| 167 | // STR = Uint8Array.from([0x10]), |
| 168 | // STR_40 = Uint8Array.from([0x20]), |
| 169 | // STR_64 = Uint8Array.from([0x21]), |
| 170 | // STR_128 = Uint8Array.from([0x22]), |
| 171 | // STR_256 = Uint8Array.from([0x23]), |
| 172 | // STR_512 = Uint8Array.from([0x24]), |
| 173 | } |
| 174 | function _concatBuffers(buffer1, buffer2) { |
| 175 | var uintArray = new Uint8Array(buffer1.byteLength + buffer2.byteLength); |
| 176 | uintArray.set(new Uint8Array(buffer1), 0); |
| 177 | uintArray.set(new Uint8Array(buffer2), buffer1.byteLength); |
| 178 | return uintArray; |
| 179 | } |
| 180 | ; |