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

jpacks

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jpacks - npm Package Compare versions

Comparing version 0.3.25 to 0.4.1

182

jpacks.dev.js

@@ -8,4 +8,4 @@ (function (exportName) {

* zswang (http://weibo.com/zswang)
* @version 0.3.25
* @date 2015-11-06
* @version 0.4.1
* @date 2015-11-07
*/

@@ -309,5 +309,5 @@ function createSchema() {

function scan(obj) {
// if (obj === null) {
// return 'null';
// }
if (obj === null) {
return 'null';
}
if (!obj) {

@@ -484,3 +484,3 @@ return obj;

*
* @param {string|Schema} itemSchema 元素类型
* @param {string|Schema} item 元素类型
* @param {string|Schema|number=} count 下标类型或个数

@@ -528,18 +528,74 @@ * @return {Schema|Function} 返回数据结构

```
* @example arrayCreator():auto size int8
```js
var _ = jpacks;
var _schema = _.array('int8', null);
console.log(_.stringify(_schema))
// > array('int8',null)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7]);
console.log(buffer.join(' '));
// > 0 1 2 3 4 5 6 7
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,3,4,5,6,7]
```
* @example arrayCreator():auto size int16 littleEndian = true
```js
var _ = jpacks;
var _schema = _.array('int16', null);
var options = {
littleEndian: true
};
console.log(_.stringify(_schema))
// > array('int16',null)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7], options);
console.log(buffer.join(' '));
// > 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0
console.log(JSON.stringify(_.unpack(_schema, buffer, options)));
// > [0,1,2,3,4,5,6,7]
```
* @example arrayCreator():auto size int16 littleEndian = false
```js
var _ = jpacks;
var _schema = _.array('int16', null);
var options = {
littleEndian: false
};
console.log(_.stringify(_schema))
// > array('int16',null)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7], options);
console.log(buffer.join(' '));
// > 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7
console.log(JSON.stringify(_.unpack(_schema, buffer, options)));
// > [0,1,2,3,4,5,6,7]
```
* @example arrayCreator():size fault tolerant
```js
var _ = jpacks;
var _schema = _.array('int8', 4);
console.log(_.stringify(_schema))
// > array('int8',4)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7]);
console.log(buffer.join(' '));
// > 0 1 2 3
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,3]
var buffer = _.pack(_schema, [0, 1, 2]);
console.log(buffer.join(' '));
// > 0 1 2 0
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,0]
```
'''</example>'''
*/
function arrayCreator(itemSchema, count) {
function arrayCreator(item, count) {
/*<safe>*/
if (typeof itemSchema === 'undefined') {
throw new Error('Parameter "itemSchema" is undefined.');
if (typeof item === 'undefined') {
throw new Error('Parameter "item" is undefined.');
}
/*</safe>*/
/*<debug>
console.log('arrayCreator()', Schema.stringify(itemSchema, count));
console.log('arrayCreator()', Schema.stringify(item, count));
//</debug>*/
var size;
var countSchema;
if (typeof count === 'number') {
size = itemSchema.size * count;
} else {
if (count !== 'number' && count !== null) { // static size && auto size
countSchema = Schema.from(count);

@@ -549,16 +605,37 @@ }

unpack: function _unpack(buffer, options, offsets) {
var length = count;
var length;
if (countSchema) {
length = Schema.unpack(countSchema, buffer, options, offsets);
} else {
length = count;
}
if (itemSchema.array && options.littleEndian) {
size = countSchema.size * length;
if (length === 0) {
return [];
}
var result = [];
var itemSchema = Schema.from(item);
if (itemSchema.array && (options.littleEndian || itemSchema.size === 1)) {
var size = length === null ? buffer.byteLength : itemSchema.size * length;
/* TypeArray littleEndian is true */
var offset = offsets[0];
var arrayBuffer = new ArrayBuffer(size);
var typeArray = new itemSchema.array(arrayBuffer);
var uint8Array = new Uint8Array(arrayBuffer);
uint8Array.set(
new Uint8Array(buffer, offset, Math.min(size, buffer.byteLength))
);
[].push.apply(result, typeArray);
offsets[0] += size;
return [].slice.apply(new itemSchema.array(buffer, offset, length));
return result;
}
var result = [];
for (var i = 0; i < length; i++) {
result.push(Schema.unpack(itemSchema, buffer, options, offsets));
if (length === null) { // auto size
var laseOffset;
while (offsets[0] < buffer.byteLength && laseOffset !== offsets[0]) {
laseOffset = offsets[0];
result.push(Schema.unpack(itemSchema, buffer, options, offsets));
}
} else {
for (var i = 0; i < length; i++) {
result.push(Schema.unpack(itemSchema, buffer, options, offsets));
}
}

@@ -568,15 +645,25 @@ return result;

pack: function _pack(value, options, buffer) {
var length = count;
var itemSchema = Schema.from(item);
if (!value && !countSchema) { // 数据不变
return;
}
var length;
if (countSchema) {
length = value ? value.length : 0;
Schema.pack(countSchema, length, options, buffer);
} else {
length = count === null ? (value || []).length : count;
}
if (itemSchema.array && options.littleEndian) {
size = itemSchema.size * length;
if (!value || !length) {
return;
}
if (itemSchema.array && (options.littleEndian || itemSchema.size === 1)) {
var size = itemSchema.size * length;
/* TypeArray littleEndian is true */
var arrayBuffer = new ArrayBuffer(size);
var typeArray = new itemSchema.array(arrayBuffer);
typeArray.set(value);
typeArray.set(value.slice(0, length));
var uint8Array = new Uint8Array(arrayBuffer);
[].push.apply(buffer, uint8Array);
return;
}

@@ -589,6 +676,6 @@ for (var i = 0; i < length; i++) {

args: arguments,
size: size
size: count === 'number' ? itemSchema.size * count : undefined
});
}
var array = Schema.together(arrayCreator, function (fn, args) {
var array = Schema.together(arrayCreator, function(fn, args) {
fn.namespace = 'array';

@@ -629,2 +716,15 @@ fn.args = args;

```
* @example bytes() auto size
```js
var _ = jpacks;
var _schema = jpacks.bytes(null);
console.log(String(_schema));
// > array('uint8',null)
var value = [0, 1, 2, 3, 4, 5];
var buffer = jpacks.pack(_schema, value);
console.log(buffer.join(' '));
// > 0 1 2 3 4 5
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,3,4,5]
```
'''</example>'''

@@ -1104,2 +1204,14 @@ */

```
* @example cstringCreator():auto size
```js
var _ = jpacks;
var _schema = _.cstring(null);
console.log(_.stringify(_schema));
// > cstring(null)
var buffer = _.pack(_schema, 'Hello 你好!');
console.log(buffer.join(' '));
// > 72 101 108 108 111 32 228 189 160 229 165 189 239 188 129 0
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > "Hello 你好!"
```
* @example cstringCreator():pchar

@@ -1110,3 +1222,3 @@ ```js

console.log(_.stringify(_schema));
// > array(cstring(true),'uint8')
// > array(cstring(null),'uint8')
var buffer = _.pack(_schema, ['abc', 'defghijk', 'g']);

@@ -1124,3 +1236,3 @@ console.log(buffer.join(' '));

var bytes;
if (size === true) { // 自动大小
if (size === null) { // 自动大小
bytes = new Uint8Array(buffer, offsets[0]);

@@ -1135,3 +1247,3 @@ } else {

var result = Schema.unpack(Schema.string(byteSize), bytes, options);
if (size === true) {
if (size === null) {
offsets[0] += byteSize + 1;

@@ -1143,8 +1255,4 @@ }

var bytes = [0];
[].unshift.apply(bytes, Schema.stringBytes(value, options));
if (size === true) { // 自动大小
Schema.pack(Schema.bytes(bytes.length), bytes, options, buffer);
} else {
Schema.pack(Schema.bytes(size), bytes, options, buffer);
}
[].unshift.apply(bytes, Schema.stringBytes(value, options)); // 追加零字符
Schema.pack(Schema.bytes(size), bytes, options, buffer);
},

@@ -1160,3 +1268,3 @@ namespace: 'cstring',

Schema.register('cstring', cstring);
Schema.register('pchar', cstring(true));
Schema.register('pchar', cstring(null));
/**

@@ -1163,0 +1271,0 @@ * 创建条件类型

@@ -8,4 +8,4 @@ (function (exportName) {

* zswang (http://weibo.com/zswang)
* @version 0.3.25
* @date 2015-11-06
* @version 0.4.1
* @date 2015-11-07
*/

@@ -278,5 +278,5 @@ function createSchema() {

function scan(obj) {
// if (obj === null) {
// return 'null';
// }
if (obj === null) {
return 'null';
}
if (!obj) {

@@ -453,3 +453,3 @@ return obj;

*
* @param {string|Schema} itemSchema 元素类型
* @param {string|Schema} item 元素类型
* @param {string|Schema|number=} count 下标类型或个数

@@ -497,10 +497,66 @@ * @return {Schema|Function} 返回数据结构

```
* @example arrayCreator():auto size int8
```js
var _ = jpacks;
var _schema = _.array('int8', null);
console.log(_.stringify(_schema))
// > array('int8',null)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7]);
console.log(buffer.join(' '));
// > 0 1 2 3 4 5 6 7
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,3,4,5,6,7]
```
* @example arrayCreator():auto size int16 littleEndian = true
```js
var _ = jpacks;
var _schema = _.array('int16', null);
var options = {
littleEndian: true
};
console.log(_.stringify(_schema))
// > array('int16',null)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7], options);
console.log(buffer.join(' '));
// > 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0
console.log(JSON.stringify(_.unpack(_schema, buffer, options)));
// > [0,1,2,3,4,5,6,7]
```
* @example arrayCreator():auto size int16 littleEndian = false
```js
var _ = jpacks;
var _schema = _.array('int16', null);
var options = {
littleEndian: false
};
console.log(_.stringify(_schema))
// > array('int16',null)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7], options);
console.log(buffer.join(' '));
// > 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7
console.log(JSON.stringify(_.unpack(_schema, buffer, options)));
// > [0,1,2,3,4,5,6,7]
```
* @example arrayCreator():size fault tolerant
```js
var _ = jpacks;
var _schema = _.array('int8', 4);
console.log(_.stringify(_schema))
// > array('int8',4)
var buffer = _.pack(_schema, [0, 1, 2, 3, 4, 5, 6, 7]);
console.log(buffer.join(' '));
// > 0 1 2 3
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,3]
var buffer = _.pack(_schema, [0, 1, 2]);
console.log(buffer.join(' '));
// > 0 1 2 0
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,0]
```
'''</example>'''
*/
function arrayCreator(itemSchema, count) {
var size;
function arrayCreator(item, count) {
var countSchema;
if (typeof count === 'number') {
size = itemSchema.size * count;
} else {
if (count !== 'number' && count !== null) { // static size && auto size
countSchema = Schema.from(count);

@@ -510,16 +566,37 @@ }

unpack: function _unpack(buffer, options, offsets) {
var length = count;
var length;
if (countSchema) {
length = Schema.unpack(countSchema, buffer, options, offsets);
} else {
length = count;
}
if (itemSchema.array && options.littleEndian) {
size = countSchema.size * length;
if (length === 0) {
return [];
}
var result = [];
var itemSchema = Schema.from(item);
if (itemSchema.array && (options.littleEndian || itemSchema.size === 1)) {
var size = length === null ? buffer.byteLength : itemSchema.size * length;
/* TypeArray littleEndian is true */
var offset = offsets[0];
var arrayBuffer = new ArrayBuffer(size);
var typeArray = new itemSchema.array(arrayBuffer);
var uint8Array = new Uint8Array(arrayBuffer);
uint8Array.set(
new Uint8Array(buffer, offset, Math.min(size, buffer.byteLength))
);
[].push.apply(result, typeArray);
offsets[0] += size;
return [].slice.apply(new itemSchema.array(buffer, offset, length));
return result;
}
var result = [];
for (var i = 0; i < length; i++) {
result.push(Schema.unpack(itemSchema, buffer, options, offsets));
if (length === null) { // auto size
var laseOffset;
while (offsets[0] < buffer.byteLength && laseOffset !== offsets[0]) {
laseOffset = offsets[0];
result.push(Schema.unpack(itemSchema, buffer, options, offsets));
}
} else {
for (var i = 0; i < length; i++) {
result.push(Schema.unpack(itemSchema, buffer, options, offsets));
}
}

@@ -529,15 +606,25 @@ return result;

pack: function _pack(value, options, buffer) {
var length = count;
var itemSchema = Schema.from(item);
if (!value && !countSchema) { // 数据不变
return;
}
var length;
if (countSchema) {
length = value ? value.length : 0;
Schema.pack(countSchema, length, options, buffer);
} else {
length = count === null ? (value || []).length : count;
}
if (itemSchema.array && options.littleEndian) {
size = itemSchema.size * length;
if (!value || !length) {
return;
}
if (itemSchema.array && (options.littleEndian || itemSchema.size === 1)) {
var size = itemSchema.size * length;
/* TypeArray littleEndian is true */
var arrayBuffer = new ArrayBuffer(size);
var typeArray = new itemSchema.array(arrayBuffer);
typeArray.set(value);
typeArray.set(value.slice(0, length));
var uint8Array = new Uint8Array(arrayBuffer);
[].push.apply(buffer, uint8Array);
return;
}

@@ -550,6 +637,6 @@ for (var i = 0; i < length; i++) {

args: arguments,
size: size
size: count === 'number' ? itemSchema.size * count : undefined
});
}
var array = Schema.together(arrayCreator, function (fn, args) {
var array = Schema.together(arrayCreator, function(fn, args) {
fn.namespace = 'array';

@@ -590,2 +677,15 @@ fn.args = args;

```
* @example bytes() auto size
```js
var _ = jpacks;
var _schema = jpacks.bytes(null);
console.log(String(_schema));
// > array('uint8',null)
var value = [0, 1, 2, 3, 4, 5];
var buffer = jpacks.pack(_schema, value);
console.log(buffer.join(' '));
// > 0 1 2 3 4 5
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [0,1,2,3,4,5]
```
'''</example>'''

@@ -1041,2 +1141,14 @@ */

```
* @example cstringCreator():auto size
```js
var _ = jpacks;
var _schema = _.cstring(null);
console.log(_.stringify(_schema));
// > cstring(null)
var buffer = _.pack(_schema, 'Hello 你好!');
console.log(buffer.join(' '));
// > 72 101 108 108 111 32 228 189 160 229 165 189 239 188 129 0
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > "Hello 你好!"
```
* @example cstringCreator():pchar

@@ -1047,3 +1159,3 @@ ```js

console.log(_.stringify(_schema));
// > array(cstring(true),'uint8')
// > array(cstring(null),'uint8')
var buffer = _.pack(_schema, ['abc', 'defghijk', 'g']);

@@ -1061,3 +1173,3 @@ console.log(buffer.join(' '));

var bytes;
if (size === true) { // 自动大小
if (size === null) { // 自动大小
bytes = new Uint8Array(buffer, offsets[0]);

@@ -1072,3 +1184,3 @@ } else {

var result = Schema.unpack(Schema.string(byteSize), bytes, options);
if (size === true) {
if (size === null) {
offsets[0] += byteSize + 1;

@@ -1080,8 +1192,4 @@ }

var bytes = [0];
[].unshift.apply(bytes, Schema.stringBytes(value, options));
if (size === true) { // 自动大小
Schema.pack(Schema.bytes(bytes.length), bytes, options, buffer);
} else {
Schema.pack(Schema.bytes(size), bytes, options, buffer);
}
[].unshift.apply(bytes, Schema.stringBytes(value, options)); // 追加零字符
Schema.pack(Schema.bytes(size), bytes, options, buffer);
},

@@ -1097,3 +1205,3 @@ namespace: 'cstring',

Schema.register('cstring', cstring);
Schema.register('pchar', cstring(true));
Schema.register('pchar', cstring(null));
/**

@@ -1100,0 +1208,0 @@ * 创建条件类型

@@ -5,3 +5,3 @@ {

"description": "Binary data packing and unpacking.",
"version": "0.3.25",
"version": "0.4.1",
"homepage": "http://github.com/zswang/jpacks",

@@ -31,3 +31,3 @@ "main": "jpacks.js",

"protobufjs": "^4.1.2",
"jints": "^0.0.4"
"long": "^3.0.1"
},

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

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

var jints = require('jints');
var long = require('long');

@@ -6,3 +6,3 @@ module.exports = function(Schema) {

function bytes2Hex(bytes) {
return bytes.map(function (byte) {
return bytes.map(function(byte) {
return (0x100 + byte).toString(16).slice(1);

@@ -107,10 +107,2 @@ }).join('');

// > "-2"
var buffer = _.pack(_schema, "-0xff11233ff", { littleEndian: false });
console.log(buffer.join(' '));
// > 255 255 255 240 14 237 204 1
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false })));
// > "-68469011455"
```

@@ -122,50 +114,26 @@ '''</example>'''

unpack: function(buffer, options, offsets) {
var bytes = Schema.unpack(Schema.bytes(8), buffer, options, offsets);
var bigint = Schema.unpack({
high: 'uint32',
low: 'uint32'
}, buffer, options, offsets);
bigint.unsigned = unsigned;
if (options.littleEndian) {
bytes.reverse();
var temp = bigint.low;
bigint.low = bigint.high;
bigint.high = temp;
}
var signed = '';
if (!unsigned && bytes[0] & (1 << 7)) {
signed = '-';
bytes = bytes.map(function (item) {
return ~item & 0xff;
});
hex = bytes2Hex(bytes);
hex = jints.add(hex, '1', 16);
hex = jints.fullZero(hex, 16).replace(/^.+(.{16})$/, '$1'); // 避免位数过高
bytes = hex2bytes(hex);
}
hex = bytes2Hex(bytes);
return signed + jints.digit(hex, 16, 10);
return long.fromValue(bigint).toString();
},
pack: function _pack(value, options, buffer) {
if (typeof value === 'number') {
value = String(parseInt(value));
} else {
value = String(value);
}
var signed = value.indexOf('-') >= 0;
var hex;
if (/^[-+]?0x/.test(value)) {
hex = value.replace(/^[-+]?0x/, ''); // 清除符号和前缀
} else {
hex = jints.digit(value, 10, 16);
}
console.log('value: %j', hex);
hex = jints.fullZero(hex, 16).replace(/^.+(.{16})$/, '$1'); // 避免位数过高
var bytes = hex2bytes(hex);
if (signed) {
bytes = bytes.map(function (item) {
return ~item & 0xff;
});
hex = bytes2Hex(bytes);
hex = jints.add(hex, '1', 16);
hex = jints.fullZero(hex, 16).replace(/^.+(.{16})$/, '$1'); // 避免位数过高
bytes = hex2bytes(hex);
}
var bigint = long.fromString(String(value), unsigned);
var bytes = [];
if (options.littleEndian) {
bytes.reverse();
var temp = bigint.low;
bigint.low = bigint.high;
bigint.high = temp;
}
Schema.pack(Schema.bytes(8), bytes, options, buffer);
Schema.pack({
high: 'uint32',
low: 'uint32'
}, bigint, options, buffer);
},

@@ -172,0 +140,0 @@ size: 8,

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