@substrate-system/bencode
Version:
Bencode de/encoder
150 lines (149 loc) • 4.52 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var decode_exports = {};
__export(decode_exports, {
default: () => decode_default
});
module.exports = __toCommonJS(decode_exports);
var import_uint8_util = require("@substrate-system/uint8-util");
const INTEGER_START = 105;
const STRING_DELIM = 58;
const DICTIONARY_START = 100;
const LIST_START = 108;
const END_OF_TYPE = 101;
const decode = /* @__PURE__ */ __name(function decode2(data, start, end, encoding) {
if (!data || data.length === 0) {
throw new Error("Missing data to decode.");
}
if (typeof start !== "number" && encoding == null) {
encoding = start;
start = void 0;
}
if (typeof end !== "number" && encoding == null) {
encoding = end;
end = void 0;
}
decode2.position = 0;
decode2.encoding = encoding || null;
decode2.data = !ArrayBuffer.isView(data) ? (0, import_uint8_util.text2arr)(data) : new Uint8Array(
data.slice(
start,
end
)
);
decode2.bytes = decode2.data.length;
return decode2.next();
}, "decode");
decode.bytes = 0;
decode.position = 0;
decode.data = null;
decode.encoding = null;
decode.next = function() {
switch (decode.data[decode.position]) {
case DICTIONARY_START:
return decode.dictionary();
case LIST_START:
return decode.list();
case INTEGER_START:
return decode.integer();
default:
return decode.buffer();
}
};
decode.find = function(chr) {
if (!decode.data?.length) return null;
let i = decode.position;
const c = decode.data.length;
const d = decode.data;
while (i < c) {
if (d[i] === chr) return i;
i++;
}
throw new Error(
'Invalid data: Missing delimiter "' + String.fromCharCode(chr) + '" [0x' + chr.toString(16) + "]"
);
};
decode.dictionary = function() {
if (!decode.data) return null;
decode.position++;
const dict = {};
while (decode.data[decode.position] !== END_OF_TYPE) {
const buffer = decode.buffer();
if (typeof buffer === "string") {
dict[buffer] = decode.next();
continue;
}
let key = (0, import_uint8_util.arr2text)(buffer);
if (key.includes("\uFFFD")) key = (0, import_uint8_util.arr2hex)(buffer);
dict[key] = decode.next();
}
decode.position++;
return dict;
};
decode.list = function() {
decode.position++;
const lst = [];
while (decode.data[decode.position] !== END_OF_TYPE) {
lst.push(decode.next());
}
decode.position++;
return lst;
};
decode.integer = function() {
const end = decode.find(END_OF_TYPE);
const number = getIntFromBuffer(decode.data, decode.position + 1, end);
if (!end) throw new Error("not end");
decode.position += end + 1 - decode.position;
return number;
};
decode.buffer = function() {
const sep = decode.find(STRING_DELIM);
const newIndex = (sep || 0) + 1;
const length = getIntFromBuffer(decode.data, decode.position, sep);
const end = newIndex + length;
decode.position = end;
return decode.encoding ? (0, import_uint8_util.arr2text)(decode.data.slice(newIndex, end)) : decode.data.slice(newIndex, end);
};
var decode_default = decode;
function getIntFromBuffer(buffer, start, end) {
let sum = 0;
let sign = 1;
for (let i = start; i < end; i++) {
const num = buffer[i];
if (num < 58 && num >= 48) {
sum = sum * 10 + (num - 48);
continue;
}
if (i === start && num === 43) {
continue;
}
if (i === start && num === 45) {
sign = -1;
continue;
}
if (num === 46) {
break;
}
throw new Error("not a number: buffer[" + i + "] = " + num);
}
return sum * sign;
}
__name(getIntFromBuffer, "getIntFromBuffer");
//# sourceMappingURL=decode.cjs.map
;