UNPKG

@stricahq/cbors

Version:

cbor encoder and decoder with annotation

35 lines (34 loc) 1.53 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.utf8Decoder = exports.addSpanBytesToObject = exports.getBigNum = exports.MAX_BIG_NUM_INT64 = exports.MAX_BIG_NUM_INT32 = exports.MAX_BIG_NUM_INT = exports.POW_2_53 = exports.POW_2_32 = exports.POW_2_24 = exports.SHIFT32 = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const MAX_SAFE_HIGH = 0x1fffff; exports.SHIFT32 = 0x100000000; exports.POW_2_24 = 5.960464477539063e-8; exports.POW_2_32 = 4294967296; exports.POW_2_53 = 9007199254740992; exports.MAX_BIG_NUM_INT = new bignumber_js_1.default('0x20000000000000'); exports.MAX_BIG_NUM_INT32 = new bignumber_js_1.default('0xffffffff'); exports.MAX_BIG_NUM_INT64 = new bignumber_js_1.default('0xffffffffffffffff'); const getBigNum = (f, g) => { if (f > MAX_SAFE_HIGH) { return new bignumber_js_1.default(f).times(exports.SHIFT32).plus(g); } return f * exports.SHIFT32 + g; }; exports.getBigNum = getBigNum; const addSpanBytesToObject = (obj, span) => { const spanObj = obj; spanObj.byteSpan = span; spanObj.getByteSpan = function () { return this.byteSpan; }; return spanObj; }; exports.addSpanBytesToObject = addSpanBytesToObject; const td = new TextDecoder('utf8', { fatal: true, ignoreBOM: true }); const utf8Decoder = (buf) => td.decode(buf); exports.utf8Decoder = utf8Decoder;