Comparing version 1.2.3 to 1.3.0
187
index.js
@@ -6,7 +6,7 @@ var varint = require('varint') | ||
var INT = 2 // 101 //32bit int | ||
var DOUBLE = 3 // 010 //use next 8 bytes to encode 64bit float | ||
var INT = 2 // 010 //32bit int | ||
var DOUBLE = 3 // 011 //use next 8 bytes to encode 64bit float | ||
var ARRAY = 4 // 011 | ||
var OBJECT = 5 // 100 | ||
var ARRAY = 4 // 100 | ||
var OBJECT = 5 // 101 | ||
@@ -93,67 +93,64 @@ var BOOLNULL = 6 // 110 //and use the rest of the byte as true/false/null | ||
var decoders = [ | ||
function string (buffer, start, length) { | ||
return buffer.toString('utf8', start, start+length) | ||
}, | ||
function buffer (buffer, start, length) { | ||
return buffer.slice(start, start+length) | ||
}, | ||
function integer (buffer, start, length) { | ||
return buffer.readInt32LE(start) //TODO: encode in minimum bytes | ||
}, | ||
function double (buffer, start, length) { | ||
return buffer.readDoubleLE(start) //TODO: encode in minimum bytes | ||
}, | ||
function array (buffer, start, length) { | ||
var a = [], i = 0 | ||
for(var c = 0; c < length;) { | ||
var tag = varint.decode(buffer, start+c) | ||
var type = tag & TAG_MASK | ||
if(type === 7) throw new Error('reserved type') | ||
var len = tag >> TAG_SIZE | ||
c += varint.decode.bytes | ||
var value = decoders[type](buffer, start+c, len) | ||
a.push(value) | ||
c += len | ||
} | ||
return a | ||
}, | ||
function object (buffer, start, length) { | ||
var o = {} | ||
for(var c = 0; c < length;) { | ||
var tag = varint.decode(buffer, start+c) | ||
var type = tag & TAG_MASK | ||
var len = tag >> TAG_SIZE | ||
c += varint.decode.bytes | ||
//TODO: positive integers keys are always in order! | ||
//floats or negative numbers encoded as strings. or may not be keys? | ||
if(type === 7) throw new Error('reserved type:key') | ||
var key = decoders[type](buffer, start+c, len) | ||
c += len | ||
function decode_string (buffer, start, length) { | ||
return buffer.toString('utf8', start, start+length) | ||
} | ||
function decode_buffer (buffer, start, length) { | ||
return buffer.slice(start, start+length) | ||
} | ||
function decode_integer (buffer, start, length) { | ||
return buffer.readInt32LE(start) //TODO: encode in minimum bytes | ||
} | ||
function decode_double (buffer, start, length) { | ||
return buffer.readDoubleLE(start) //TODO: encode in minimum bytes | ||
} | ||
function decode_array (buffer, start, length) { | ||
var a = [], i = 0 | ||
for(var c = 0; c < length;) { | ||
var tag = varint.decode(buffer, start+c) | ||
var type = tag & TAG_MASK | ||
if(type === 7) throw new Error('reserved type') | ||
var len = tag >> TAG_SIZE | ||
c += varint.decode.bytes | ||
var value = decode_type(type, buffer, start+c, len) | ||
a.push(value) | ||
c += len | ||
} | ||
return a | ||
} | ||
function decode_object (buffer, start, length) { | ||
var o = {} | ||
for(var c = 0; c < length;) { | ||
var tag = varint.decode(buffer, start+c) | ||
var type = tag & TAG_MASK | ||
var len = tag >> TAG_SIZE | ||
c += varint.decode.bytes | ||
//TODO: positive integers keys are always in order! | ||
//floats or negative numbers encoded as strings. or may not be keys? | ||
if(type === 7) throw new Error('reserved type:key') | ||
var key = decode_type(type, buffer, start+c, len) | ||
c += len | ||
var tag2 = varint.decode(buffer, start+c) | ||
var type2 = tag2 & TAG_MASK | ||
if(type2 === 7) throw new Error('reserved type:value') | ||
var len2 = tag2 >> TAG_SIZE | ||
c += varint.decode.bytes | ||
var value = decoders[type2](buffer, start+c, len2) | ||
var tag2 = varint.decode(buffer, start+c) | ||
var type2 = tag2 & TAG_MASK | ||
if(type2 === 7) throw new Error('reserved type:value') | ||
var len2 = tag2 >> TAG_SIZE | ||
c += varint.decode.bytes | ||
var value = decode_type(type2, buffer, start+c, len2) | ||
c+= len2 | ||
o[key] = value | ||
} | ||
return o | ||
c+= len2 | ||
o[key] = value | ||
} | ||
return o | ||
} | ||
function decode_boolnull (buffer, start, length) { | ||
if(length === 0) return null | ||
if(buffer[start] > 2) throw new Error('invalid boolnull') | ||
if(length > 1) throw new Error('invalid boolnull, length must = 1') | ||
return ( | ||
buffer[start] === 0 ? false | ||
: buffer[start] === 1 ? true | ||
: undefined | ||
) | ||
} | ||
}, | ||
function boolnull (buffer, start, length) { | ||
if(length === 0) return null | ||
if(buffer[start] > 2) throw new Error('invalid boolnull') | ||
if(length > 1) throw new Error('invalid boolnull, length must = 1') | ||
return ( | ||
buffer[start] === 0 ? false | ||
: buffer[start] === 1 ? true | ||
: undefined | ||
) | ||
} | ||
] | ||
function getType (value) { | ||
@@ -182,3 +179,17 @@ if('string' === typeof value || value instanceof Date) | ||
function encode (value, buffer, start) { | ||
function slice(buffer, start) { | ||
var tag_value = varint.decode(buffer, start) | ||
var length = tag_value >> TAG_SIZE | ||
return buffer.slice(start+varint.decode.bytes, start+varint.decode.bytes + length) | ||
} | ||
function getEncodedLength(buffer, start) { | ||
return varint.decode(buffer, start) >> TAG_SIZE | ||
} | ||
function getEncodedType(buffer, start) { | ||
return varint.decode(buffer, start) & TAG_MASK | ||
} | ||
function encode (value, buffer, start, _len) { | ||
start = start | 0 | ||
@@ -188,5 +199,5 @@ var type = getType(value) | ||
throw new Error('unknown type:'+type+', '+JSON.stringify(value)) | ||
var len = encodingLengthers[type](value) | ||
if(!buffer) | ||
buffer = Buffer.allocUnsafe(len) | ||
var len = _len === undefined ? encodingLengthers[type](value) : _len | ||
// if(!buffer) | ||
// buffer = Buffer.allocUnsafe(len) | ||
//throw new Error('buffer must be provided') | ||
@@ -199,2 +210,23 @@ if(type === 7) throw new Error('reserved type') | ||
function allocAndEncode(value) { | ||
var len = encodingLength(value) | ||
var buffer = Buffer.allocUnsafe(len) | ||
encode(value, buffer, 0) | ||
return buffer | ||
} | ||
function decode_type(type, buffer, start, len) { | ||
return (type < ARRAY) ? ( | ||
type === STRING ? decode_string (buffer, start, len) | ||
: type === BUFFER ? decode_buffer (buffer, start, len) | ||
: type === INT ? decode_integer (buffer, start, len) | ||
: decode_double (buffer, start, len) | ||
) : ( | ||
type === ARRAY ? decode_array (buffer, start, len) | ||
: type === OBJECT ? decode_object (buffer, start, len) | ||
: type === BOOLNULL ? decode_boolnull (buffer, start, len) | ||
: decode_reserved (buffer, start, len) | ||
) | ||
} | ||
function decode (buffer, start) { | ||
@@ -207,3 +239,3 @@ start = start | 0 | ||
start += bytes | ||
var value = decoders[type](buffer, start, len) | ||
var value = decode_type(type, buffer, start, len) | ||
decode.bytes = len + bytes | ||
@@ -380,5 +412,6 @@ return value | ||
c += (key_tag >> TAG_SIZE) | ||
var value_tag = varint.decode(buffer, start+c) | ||
var value_start = start+c | ||
var value_tag = varint.decode(buffer, value_start) | ||
var next_start = varint.decode.bytes + (value_tag >> TAG_SIZE) | ||
iter(buffer, start+c, decode(buffer, key_start)) | ||
iter(buffer, value_start, key_start) | ||
c += next_start | ||
@@ -417,8 +450,9 @@ } | ||
decode: decode, | ||
allocAndEncode: allocAndEncode, | ||
encodingLength: encodingLength, | ||
buffer: true, | ||
slice: slice, | ||
getValueType: getType, | ||
getEncodedType: function (buffer, start) { | ||
return varint.decode(buffer, start) | ||
}, | ||
getEncodedLength: getEncodedLength, | ||
getEncodedType: getEncodedType, | ||
seekKey: seekKey, | ||
@@ -443,2 +477,1 @@ seekKey2: seekKey2, | ||
} | ||
{ | ||
"name": "bipf", | ||
"description": "binary in-place format", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"homepage": "https://github.com/dominictarr/binary", | ||
@@ -6,0 +6,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60069
14
759
1
3