Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@harmoniclabs/cbor

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@harmoniclabs/cbor - npm Package Compare versions

Comparing version 1.1.3-dev0 to 1.1.3-dev3

dist/LazyCborObj/index.d.ts

6

dist/Cbor/index.d.ts
import type { CborObj } from "../CborObj/index.js";
import { CborString } from "../CborString/index.js";
import { LazyCborObj } from "../LazyCborObj/LazyCborObj.js";
/**

@@ -16,2 +17,7 @@ * static class that allows CBOR encoding and decoding

};
static parseLazy(cbor: CborString | Uint8Array | string): LazyCborObj;
static parseLazyWithOffset(cbor: CborString | Uint8Array | string): {
parsed: LazyCborObj;
offset: number;
};
}

322

dist/Cbor/index.js

@@ -18,2 +18,5 @@ "use strict";

var assert_1 = require("../utils/assert.js");
var LazyCborArray_1 = require("../LazyCborObj/LazyCborArray.js");
var LazyCborMap_1 = require("../LazyCborObj/LazyCborMap.js");
var LazyCborTag_1 = require("../LazyCborObj/LazyCborTag.js");
/**

@@ -390,3 +393,3 @@ * @private to the module; not needed elsewhere

}
return new CborArray_1.CborArray(arrOfCbors);
return new CborArray_1.CborArray(arrOfCbors, { indefinite: length < 0 });
case Constants_1.MajorType.map:

@@ -410,3 +413,3 @@ var entries = [];

}
return new CborMap_1.CborMap(entries);
return new CborMap_1.CborMap(entries, { indefinite: length < 0 });
case Constants_1.MajorType.tag:

@@ -433,4 +436,319 @@ return new CborTag_1.CborTag(Number(length), parseCborObj());

};
Cbor.parseLazy = function (cbor) {
return Cbor.parseLazyWithOffset(cbor).parsed;
};
Cbor.parseLazyWithOffset = function (cbor) {
if (typeof cbor === "string")
cbor = (0, uint8array_utils_1.fromHex)(cbor);
(0, assert_1.assert)((cbor instanceof Uint8Array) || CborString_1.CborString.isStrictInstance(cbor), "in 'Cbor.parse' expected an instance of 'CborString' or a 'Uint8Array' as input; got: " + cbor);
var bytes = cbor instanceof CborString_1.CborString ?
cbor.toBuffer() :
cbor;
/**
* number of bytes red
* */
var offset = 0;
function incrementOffsetBy(l) {
offset += l;
}
function getBytesOfLength(l) {
incrementOffsetBy(l);
return bytes.slice(offset - l, // offset has been incremented prior reading
offset);
}
function getUInt8() {
incrementOffsetBy(1);
return (0, uint8array_utils_1.readUInt8)(bytes, offset - 1 // offset has been incremented prior reading
);
}
;
function getUInt16() {
incrementOffsetBy(2);
return (0, uint8array_utils_1.readUInt16BE)(bytes, offset - 2 // offset has been incremented prior reading
);
}
;
function getUInt32() {
incrementOffsetBy(4);
return (0, uint8array_utils_1.readUInt32BE)(bytes, offset - 4 // offset has been incremented prior reading
);
}
;
function getUInt64() {
incrementOffsetBy(8);
return (0, uint8array_utils_1.readBigUInt64BE)(bytes, offset - 8 // offset has been incremented prior reading
);
}
;
function getFloat16() {
// increments the offset here
var floatBits = getUInt16();
var tempArrayBuffer = new ArrayBuffer(4);
var tempDataView = new DataView(tempArrayBuffer);
var sign = floatBits & 32768;
var exponent = floatBits & 31744;
var fraction = floatBits & 1023;
if (exponent === 0x7c00)
exponent = 0xff << 10;
else if (exponent !== 0)
exponent += (127 - 15) << 10;
else if (fraction !== 0)
return new CborSimple_1.CborSimple((sign !== 0 ? -1 : 1) * fraction * 5.960464477539063e-8, "float");
tempDataView.setUint32(0, sign << 16 | exponent << 13 | fraction << 13);
return new CborSimple_1.CborSimple(tempDataView.getFloat32(0), "float");
}
function getFloat32() {
incrementOffsetBy(4);
return new CborSimple_1.CborSimple((0, uint8array_utils_1.readFloat32BE)(bytes, offset - 4 // offset has been incremented prior reading
), "float");
}
function getFloat64() {
incrementOffsetBy(8);
return new CborSimple_1.CborSimple((0, uint8array_utils_1.readFloat64BE)(bytes, offset - 8 // offset has been incremented prior reading
), "float");
}
function incrementIfBreak() {
if ((0, uint8array_utils_1.readUInt8)(bytes, offset) !== 0xff)
return false;
incrementOffsetBy(1);
return true;
}
function getLength(addInfos) {
if (addInfos < 24)
return BigInt(addInfos);
if (addInfos === 24)
return BigInt(getUInt8());
if (addInfos === 25)
return BigInt(getUInt16());
if (addInfos === 26)
return BigInt(getUInt32());
if (addInfos === 27)
return getUInt64();
if (addInfos === 31)
return BigInt(-1); // indefinite length element follows
throw new BaseCborError_1.BaseCborError("Invalid length encoding while parsing CBOR");
}
function getIndefiniteElemLengthOfType(majorType) {
var headerByte = getUInt8();
if (headerByte === 0xff) // break indefinite
return BigInt(-1);
var elemLength = getLength(headerByte & 31);
if (elemLength < 0 || (headerByte >> 5 !== majorType))
throw new BaseCborError_1.BaseCborError("unexpected nested indefinite length element");
return elemLength;
}
function getTextOfLength(l) {
// increments offset while getting the bytes
return (0, uint8array_utils_1.toUtf8)(getBytesOfLength(l));
}
function getNextElemBytes() {
var elemStart = offset;
var headerByte = getUInt8();
var major = headerByte >> 5;
var addInfos = headerByte & 31;
if (major === Constants_1.MajorType.float_or_simple) {
if (addInfos === 25)
return bytes.slice(elemStart, elemStart + 2);
if (addInfos === 26)
return bytes.slice(elemStart, elemStart + 3);
if (addInfos === 27)
return bytes.slice(elemStart, elemStart + 5);
}
var preLenOffset = offset;
var length = getLength(addInfos);
var postLenOffset = offset;
if (length < 0 &&
(major < 2 || major > 6)) {
throw new BaseCborError_1.BaseCborError("unexpected indefinite length element while parsing CBOR");
}
switch (major) {
case Constants_1.MajorType.unsigned: return bytes.slice(elemStart, postLenOffset);
case Constants_1.MajorType.negative: return bytes.slice(elemStart, postLenOffset);
case Constants_1.MajorType.bytes:
if (length < 0) // data in UPLC v1.*.* serializes as indefinite length
{
var elementLength = void 0;
while ((elementLength = getIndefiniteElemLengthOfType(major)) >= 0) {
// this operation is done when calling
// `getBytesOfLength( Number( elementLength )`
/// in the non-lazy verision
incrementOffsetBy(Number(elementLength));
}
return bytes.slice(elemStart, offset);
}
// definite length
// void getBytesOfLength( Number( length ) )
incrementOffsetBy(Number(length)); // this is the only part we need of the above function
return bytes.slice(elemStart, offset);
case Constants_1.MajorType.text:
if (length < 0) // indefinite length
{
var str = "";
var l = 0;
while ((l = Number(getIndefiniteElemLengthOfType(Constants_1.MajorType.text))) >= 0) {
// str += getTextOfLength( l );
incrementOffsetBy(l);
}
return bytes.slice(elemStart, offset);
}
// void getTextOfLength( Number( length ) );
incrementOffsetBy(Number(length)); // this is the only part we need of the above function
return bytes.slice(elemStart, offset);
case Constants_1.MajorType.array:
if (length < 0) {
while (!incrementIfBreak()) {
void getNextElemBytes();
}
}
else {
for (var i = 0; i < length; i++) {
void getNextElemBytes();
}
}
return bytes.slice(elemStart, offset);
case Constants_1.MajorType.map:
if (length < 0) {
while (!incrementIfBreak()) {
void getNextElemBytes();
void getNextElemBytes();
}
}
else {
for (var i = 0; i < length; i++) {
void getNextElemBytes();
void getNextElemBytes();
}
}
return bytes.slice(elemStart, offset);
case Constants_1.MajorType.tag:
void getNextElemBytes();
return bytes.slice(elemStart, offset);
case Constants_1.MajorType.float_or_simple:
var nLen = Number(length);
if (nLen === 20)
return bytes.slice(elemStart, offset); // 0xf4
if (nLen === 21)
return bytes.slice(elemStart, offset); // 0xf5
if (nLen === 22)
return bytes.slice(elemStart, offset); // 0xf6
if (nLen === 23)
return bytes.slice(elemStart, offset); // 0xf7
// flaots handled at the beginning of the function
// since length isn't required
throw new BaseCborError_1.BaseCborError("unrecognized simple value");
default:
throw new BaseCborError_1.BaseCborError("unrecognized majorType: " + major);
}
}
function parseCborObj() {
var headerByte = getUInt8();
var major = headerByte >> 5;
var addInfos = headerByte & 31;
if (major === Constants_1.MajorType.float_or_simple) {
if (addInfos === 25)
return getFloat16();
if (addInfos === 26)
return getFloat32();
if (addInfos === 27)
return getFloat64();
}
var length = getLength(addInfos);
if (length < 0 &&
(major < 2 || major > 6)) {
throw new BaseCborError_1.BaseCborError("unexpected indefinite length element while parsing CBOR");
}
switch (major) {
case Constants_1.MajorType.unsigned: return new CborUInt_1.CborUInt(length);
case Constants_1.MajorType.negative: return new CborNegInt_1.CborNegInt(-BigInt(1) - length);
case Constants_1.MajorType.bytes:
if (length < 0) // data in UPLC v1.*.* serializes as indefinite length
{
var chunks = [];
var fullUint8ArrayLength = 0;
var elementLength = void 0;
while ((elementLength = getIndefiniteElemLengthOfType(major)) >= 0) {
fullUint8ArrayLength += Number(elementLength);
chunks.push(getBytesOfLength(// increments offset
Number(elementLength)));
}
var fullUint8Array = new Uint8Array(fullUint8ArrayLength);
var fullUint8ArrayOffset = 0;
for (var i = 0; i < chunks.length; ++i) {
fullUint8Array.set(chunks[i], fullUint8ArrayOffset);
fullUint8ArrayOffset += chunks[i].length;
}
return new CborBytes_1.CborBytes(Uint8Array.from(fullUint8Array));
}
// definite length
return new CborBytes_1.CborBytes(getBytesOfLength(Number(length)));
case Constants_1.MajorType.text:
if (length < 0) // indefinite length
{
var str = "";
var l = 0;
while ((l = Number(getIndefiniteElemLengthOfType(Constants_1.MajorType.text))) >= 0) {
str += getTextOfLength(l);
}
return new CborText_1.CborText(str);
}
return new CborText_1.CborText(getTextOfLength(Number(length)));
case Constants_1.MajorType.array:
if (length < 0) {
var arr = [];
while (!incrementIfBreak()) {
arr.push(getNextElemBytes());
}
return new LazyCborArray_1.LazyCborArray(arr, { indefinite: true });
}
else {
var arr = new Array(Number(length));
for (var i = 0; i < length; i++) {
arr[i] = getNextElemBytes();
}
return new LazyCborArray_1.LazyCborArray(arr, { indefinite: false });
}
case Constants_1.MajorType.map:
if (length < 0) {
var entries = [];
while (!incrementIfBreak()) {
entries.push({
k: getNextElemBytes(),
v: getNextElemBytes()
});
}
return new LazyCborMap_1.LazyCborMap(entries, { indefinite: true });
}
else {
var entries = new Array(Number(length));
for (var i = 0; i < length; i++) {
entries[i] = {
k: getNextElemBytes(),
v: getNextElemBytes()
};
}
return new LazyCborMap_1.LazyCborMap(entries, { indefinite: true });
}
case Constants_1.MajorType.tag:
return new LazyCborTag_1.LazyCborTag(Number(length), parseCborObj());
case Constants_1.MajorType.float_or_simple:
var nLen = Number(length);
if (nLen === 20)
return new CborSimple_1.CborSimple(false); // 0xf4
if (nLen === 21)
return new CborSimple_1.CborSimple(true); // 0xf5
if (nLen === 22)
return new CborSimple_1.CborSimple(null); // 0xf6
if (nLen === 23)
return new CborSimple_1.CborSimple(undefined); // 0xf7
// flaots handled at the beginning of the function
// since length isn't required
throw new BaseCborError_1.BaseCborError("unrecognized simple value");
default:
throw new BaseCborError_1.BaseCborError("unrecognized majorType: " + major);
}
}
return { parsed: parseCborObj(), offset: offset };
};
return Cbor;
}());
exports.Cbor = Cbor;

11

dist/CborObj/CborArray.js

@@ -13,10 +13,7 @@ "use strict";

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CborArray = exports.isRawCborArray = void 0;
var obj_utils_1 = require("@harmoniclabs/obj-utils");
var JsRuntime_1 = __importDefault(require("../utils/JsRuntime/index.js"));
var CborObj_1 = require("./CborObj.js");
var assert_1 = require("../utils/assert.js");
function isRawCborArray(arr) {

@@ -36,3 +33,3 @@ if (typeof arr !== "object")

function CborArray(array, options) {
JsRuntime_1.default.assert(Array.isArray(array) &&
(0, assert_1.assert)(Array.isArray(array) &&
array.every(CborObj_1.isCborObj), "in 'CborArray' constructor: invalid input; got: " + array);

@@ -62,5 +59,3 @@ var indefinite = __assign(__assign({}, defaultOpts), options).indefinite;

CborArray.prototype.clone = function () {
return new CborArray(this.array, {
indefinite: this.indefinite
});
return new CborArray(this.array, { indefinite: this.indefinite });
};

@@ -67,0 +62,0 @@ return CborArray;

@@ -71,5 +71,3 @@ "use strict";

CborMap.prototype.clone = function () {
return new CborMap(this.map, {
indefinite: this.indefinite
});
return new CborMap(this.map, { indefinite: this.indefinite });
};

@@ -76,0 +74,0 @@ return CborMap;

export * from "./Cbor/index.js";
export * from "./CanBeCborString.js";
export * from "./CborObj/index.js";
export * from "./LazyCborObj/index.js";
export * from "./CborString/index.js";

@@ -5,0 +6,0 @@ export * from "./errors/index.js";

@@ -20,2 +20,3 @@ "use strict";

__exportStar(require("./CborObj/index.js"), exports);
__exportStar(require("./LazyCborObj/index.js"), exports);
__exportStar(require("./CborString/index.js"), exports);

@@ -22,0 +23,0 @@ __exportStar(require("./errors/index.js"), exports);

{
"name": "@harmoniclabs/cbor",
"version": "1.1.3-dev0",
"version": "1.1.3-dev3",
"description": "cbor library for easy cbor parsing, manipulation and conversion",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc