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

cbor-x

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cbor-x - npm Package Compare versions

Comparing version 0.3.2 to 0.3.3

48

decode.js

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

let dataView
let needsBufferCopy
let defaultOptions = {

@@ -39,3 +38,3 @@ useRecords: false,

if (src) {
// re-entrant execution, save the state and restore it after we do this unpack
// re-entrant execution, save the state and restore it after we do this decode
return saveState(() => {

@@ -212,3 +211,3 @@ src = null

if (currentExtensions[token])
return currentExtensions[token]()
return currentExtensions[token](read())
else

@@ -514,3 +513,3 @@ throw new Error('Unknown extension ' + token)

function readBin(length) {
return (currentDecoder.copyBuffers || needsBufferCopy) ?
return currentDecoder.copyBuffers ?
// specifically use the copying slice (not the node one)

@@ -549,11 +548,8 @@ Uint8Array.prototype.slice.call(src, position, position += length) :

currentExtensions[8] = () => {
let data = read()
currentExtensions[8] = (data) => {
return (glbl[data[0]] || Error)(data[1])
}
currentExtensions[9] = (data) => {
currentExtensions[9] = (id) => {
// id extension (for structured clones)
let id = dataView.getUint32(++position)
position += 4
if (!referenceMap)

@@ -565,3 +561,3 @@ referenceMap = new Map()

// ahead past references to record structure definitions
if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
if ((token >> 5) == 4)
target = []

@@ -580,6 +576,4 @@ else

currentExtensions[10] = () => {
currentExtensions[10] = (id) => {
// pointer extension (for structured clones)
let id = dataView.getUint32(++position)
position += 4
let refEntry = referenceMap.get(id)

@@ -590,21 +584,15 @@ refEntry.used = true

currentExtensions[11] = () => new Set(read())
currentExtensions[11] = (array) => new Set(array)
const typedArrays = ['Int8','Uint8 ','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array')
currentExtensions[12] = () => {
needsBufferCopy = true
try {
let [ typeCode, buffer ] = read()
let typedArrayName = typedArrays[typeCode]
if (!typedArrayName)
throw new Error('Could not find typed array for code ' + typeCode)
// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
return new glbl[typedArrayName](buffer.buffer)
} finally {
needsBufferCopy = false
}
currentExtensions[12] = (data) => {
let [ typeCode, buffer ] = data
let typedArrayName = typedArrays[typeCode]
if (!typedArrayName)
throw new Error('Could not find typed array for code ' + typeCode)
// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
return new glbl[typedArrayName](Uint8Array.prototype.slice.call(buffer, 0).buffer)
}
currentExtensions[13] = () => {
let data = read()
currentExtensions[13] = (data) => {
return new RegExp(data[0], data[1])

@@ -615,3 +603,3 @@ }

// 32-bit date extension
return new Date(read() * 1000)
return new Date(data * 1000)
}

@@ -651,3 +639,3 @@ function saveState(callback) {

exports.addExtension = function(extension) {
currentExtensions[extension.type] = extension.unpack
currentExtensions[extension.tag] = extension.decode
}

@@ -654,0 +642,0 @@

@@ -342,30 +342,18 @@ "use strict"

let extension = extensions[i]
let currentTarget = target
let currentTargetView = targetView
let currentPosition = position
target = null
let result
try {
result = extension.encode.call(this, value, (size) => {
target = currentTarget
currentTarget = null
position += size
if (position > safeEnd)
makeRoom(position)
return {
target, targetView, position: position - size
}
}, encode)
} finally {
// restore current target information (unless already restored)
if (currentTarget) {
target = currentTarget
targetView = currentTargetView
position = currentPosition
safeEnd = target.length - 10
}
}
if (result) {
position = writeExtensionData(result, target, position, extension.type)
}
let tag = extension.tag
if (tag < 0x18) {
target[position++] = 0xc0 | tag
} else if (tag < 0x100) {
target[position++] = 0xd8
target[position++] = tag
} else if (tag < 0x10000) {
target[position++] = 0xd9
target[position++] = tag >> 8
target[position++] = tag & 0xff
} else if (tag > -1) {
target[position++] = 0xba
targetView.setUint32(position, tag)
position += 4
} // else undefined, don't write tag
extension.encode.call(this, value, encode)
return

@@ -553,83 +541,64 @@ }

extensions = [{
encode(date, allocateForWrite) {
tag: 1,
encode(date, encode) {
let seconds = date.getTime() / 1000
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
// Timestamp 32
let { target, targetView, position} = allocateForWrite(6)
target[position++] = 0xc1
target[position++] = 0x1a
targetView.setUint32(position, seconds)
position += 4
} else {
// Timestamp float64
let { target, targetView, position} = allocateForWrite(10)
target[position++] = 0xc1
target[position++] = 0xfb
targetView.setFloat64(position, seconds)
position += 8
}
}
}, {
encode(set, allocateForWrite, encode) {
tag: 11,
encode(set, encode) {
let array = Array.from(set)
if (this.structuredClone) {
let { target, position} = allocateForWrite(1)
target[position++] = 0xcb // tag 11
}
encode(array)
}
}, {
encode(error, allocateForWrite, encode) {
if (this.structuredClone) {
let { target, position} = allocateForWrite(1)
target[position++] = 0xc8 // tag 8
}
tag: 8,
encode(error, encode) {
encode([ error.name, error.message ])
}
}, {
encode(regex, allocateForWrite, encode) {
if (this.structuredClone) {
let { target, position} = allocateForWrite(1)
target[position++] = 0xcd
}
tag: 13,
encode(regex, encode) {
encode([ regex.source, regex.flags ])
}
}, {
encode(arrayBuffer, allocateForWrite, encode) {
encode(arrayBuffer, encode) {
if (this.structuredClone)
writeExtBuffer(arrayBuffer, 0x10, allocateForWrite, encode)
writeExtBuffer(arrayBuffer, 0x10, encode)
else
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite)
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer))
}
}, {
encode(typedArray, allocateForWrite, encode) {
encode(typedArray, encode) {
let constructor = typedArray.constructor
if (constructor !== ByteArray && this.structuredClone)
writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite, encode)
writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), encode)
else
writeBuffer(typedArray, allocateForWrite)
writeBuffer(typedArray)
}
}, {
encode(c1, allocateForWrite) { // specific 0xC1 object
let { target, position} = allocateForWrite(1)
target[position] = 0xc1
}
}]
function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
function writeExtBuffer(typedArray, type, encode) {
target[position++] = 0xcc // tag 12
let length = typedArray.byteLength
let offset = typedArray.byteOffset || 0
let buffer = typedArray.buffer || typedArray
let { target, position, targetView } = allocateForWrite(1)
target[position++] = 0xcc
encode([type, hasNodeBuffer ? Buffer.from(buffer, offset, length) :
new Uint8Array(buffer, offset, length)])
}
function writeBuffer(buffer, allocateForWrite) {
function writeBuffer(buffer) {
let length = buffer.byteLength
var target, position
if (length < 0x100) {
var { target, position } = allocateForWrite(length + 2)
target[position++] = 0x58
target[position++] = length
} else if (length < 0x10000) {
var { target, position } = allocateForWrite(length + 3)
target[position++] = 0x59

@@ -639,3 +608,2 @@ target[position++] = length >> 8

} else {
var { target, position, targetView } = allocateForWrite(length + 5)
target[position++] = 0x5a

@@ -649,2 +617,3 @@ targetView.setUint32(position, length)

copyBinary(buffer, target, position, 0, length)
position += length
}

@@ -651,0 +620,0 @@

@@ -1,2 +0,2 @@

declare module 'msgpackr' {
declare module 'cbor-x' {
interface Options {

@@ -14,22 +14,17 @@ useFloat32?: 0 | typeof ALWAYS | typeof DECIMAL_ROUND | typeof DECIMAL_FIT

}
export class Unpackr {
export class Decoder {
constructor(options?: Options)
unpack(messagePack: Buffer): any
decode(messagePack: Buffer): any
}
export class Decoder extends Unpackr {}
export class Packr extends Unpackr {
pack(value: any): Buffer
export class Encoder extends Decoder {
encode(value: any): Buffer
resetMemory(): void
}
export class Encoder extends Packr {}
interface Extension {
Class: Function
type: number
pack(value: any): Buffer
unpack(messagePack: Buffer): any
encode(value: any): Buffer
decode(messagePack: Buffer): any
}
export function unpack(messagePack: Buffer): any
export function pack(value: any): Buffer
export function decode(messagePack: Buffer): any

@@ -41,5 +36,5 @@ export function encode(value: any): Buffer

export const DECIMAL_FIT = 4
export class UnpackrStream {
export class DecoderStream {
}
export class PackrStream {
export class EncoderStream {
write(value)

@@ -46,0 +41,0 @@ end(value?)

{
"name": "cbor-x",
"author": "Kris Zyp",
"version": "0.3.2",
"version": "0.3.3",
"license": "MIT",

@@ -29,3 +29,3 @@ "types": "./index.d.ts",

"optionalDependencies": {
"cbor-extract": "^0.1.0"
"cbor-extract": "^0.2.0"
},

@@ -32,0 +32,0 @@ "devDependencies": {

@@ -186,3 +186,3 @@ # cbor-x

## Custom Extensions
You can add your own custom extensions, which can be used to encode specific types/classes in certain ways. This is done by using the `addExtension` function, and specifying the class, extension type code (should be a number from 1-100, reserving negatives for CBOR, 101-127 for cbor-x), and your encode and decode functions (or just the one you need). You can use cbor-x encoding and decoding within your extensions, but if you do so, you must create a separate Encoder instance, otherwise you could override data in the same encoding buffer:
You can add your own custom extensions, which can be used to encode specific types/classes in certain ways. This is done by using the `addExtension` function, and specifying the class, extension type code (should be a number greater than 256, all others are reserved for CBOR or cbor-x), and your encode and decode functions (or just the one you need). You can use cbor-x encoding and decoding within your extensions:
```

@@ -196,11 +196,11 @@ import { addExtension, Encoder } from 'cbor-x';

Class: MyCustomClass,
type: 11, // register our own extension code (a type code from 1-100)
encode(instance) {
tag: 311, // register our own extension code (a tag code > 255)
encode(instance, encode) {
// define how your custom class should be encoded
return extEncoder.encode(instance.myData); // return a buffer
encode(instance.myData); // return a buffer
}
decode(buffer) {
decode(data) {
// define how your custom class should be decoded
let instance = new MyCustomClass();
instance.myData = extEncoder.decode(buffer);
instance.myData = data
return instance; // decoded value from buffer

@@ -207,0 +207,0 @@ }

var inspector = require('inspector')
inspector.open(9330, null, true)
//inspector.open(9330, null, true)

@@ -125,6 +125,5 @@ function tryRequire(module) {

Class: Extended,
type: 30,
decode: function(buffer) {
tag: 300,
decode: function(data) {
let e = new Extended()
let data = encoder.decode(buffer)
e.value = data[0]

@@ -279,3 +278,3 @@ e.string = data[1]

test('buffers', function(){
test('buffers', function() {
var data = {

@@ -282,0 +281,0 @@ buffer1: new Uint8Array([2,3,4]),

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