UNPKG

@substrate-system/bencode

Version:
94 lines (93 loc) 3.29 kB
"use strict"; 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 encoding_length_exports = {}; __export(encoding_length_exports, { default: () => encoding_length_default, encodingLength: () => encodingLength }); module.exports = __toCommonJS(encoding_length_exports); var import_uint8_util = require("@substrate-system/uint8-util"); var import_util = require("./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 = (0, import_uint8_util.text2arr)(key).byteLength; length += (0, import_util.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 = (0, import_uint8_util.text2arr)(keys[i]).byteLength; length += (0, import_util.digitCount)(keyLength) + 1 + keyLength; length += encodingLength(value[keys[i]]); } return length; } __name(objectLength, "objectLength"); function stringLength(value) { const length = (0, import_uint8_util.text2arr)(value).byteLength; return (0, import_util.digitCount)(length) + 1 + length; } __name(stringLength, "stringLength"); function arrayBufferLength(value) { const length = value.byteLength - value.byteOffset; return (0, import_util.digitCount)(length) + 1 + length; } __name(arrayBufferLength, "arrayBufferLength"); function encodingLength(value) { const length = 0; if (value == null) return length; const type = (0, import_util.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 + (0, import_util.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; //# sourceMappingURL=encoding-length.cjs.map