@substrate-system/bencode
Version:
Bencode de/encoder
75 lines (74 loc) • 2.21 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { text2arr } from "@substrate-system/uint8-util";
import { digitCount, getType } from "./util.js";
function listLength(list) {
let length = 1 + 1;
for (const value of list) {
length += encodingLength(value);
}
return length;
}
__name(listLength, "listLength");
function mapLength(map) {
let length = 1 + 1;
for (const [key, value] of map) {
const keyLength = text2arr(key).byteLength;
length += digitCount(keyLength) + 1 + keyLength;
length += encodingLength(value);
}
return length;
}
__name(mapLength, "mapLength");
function objectLength(value) {
let length = 1 + 1;
const keys = Object.keys(value);
for (let i = 0; i < keys.length; i++) {
const keyLength = text2arr(keys[i]).byteLength;
length += digitCount(keyLength) + 1 + keyLength;
length += encodingLength(value[keys[i]]);
}
return length;
}
__name(objectLength, "objectLength");
function stringLength(value) {
const length = text2arr(value).byteLength;
return digitCount(length) + 1 + length;
}
__name(stringLength, "stringLength");
function arrayBufferLength(value) {
const length = value.byteLength - value.byteOffset;
return digitCount(length) + 1 + length;
}
__name(arrayBufferLength, "arrayBufferLength");
function encodingLength(value) {
const length = 0;
if (value == null) return length;
const type = getType(value);
switch (type) {
case "arraybufferview":
return arrayBufferLength(value);
case "string":
return stringLength(value);
case "array":
case "set":
return listLength(value);
case "number":
return 1 + digitCount(Math.floor(value)) + 1;
case "bigint":
return 1 + value.toString().length + 1;
case "object":
return objectLength(value);
case "map":
return mapLength(value);
default:
throw new TypeError(`Unsupported value of type "${type}"`);
}
}
__name(encodingLength, "encodingLength");
var encoding_length_default = encodingLength;
export {
encoding_length_default as default,
encodingLength
};
//# sourceMappingURL=encoding-length.js.map