Comparing version 0.0.8 to 0.0.10
331
index.js
@@ -13,6 +13,6 @@ /** | ||
this.offset = offset; | ||
this.get = function() { | ||
this.get = function () { | ||
return p.buf[offset]; | ||
} | ||
this.set = function(val) { | ||
this.set = function (val) { | ||
p.buf[offset] = val; | ||
@@ -26,5 +26,5 @@ } | ||
this.get = function() { | ||
return (p.buf[offset] > 0 ); | ||
return (p.buf[offset] > 0); | ||
} | ||
this.set = function(val) { | ||
this.set = function (val) { | ||
p.buf[offset] = val ? 1 : 0; | ||
@@ -37,3 +37,3 @@ } | ||
this.offset = offset; | ||
function bec(cb) { | ||
@@ -43,3 +43,3 @@ for (var i = 0; i < length; i++) | ||
} | ||
function lec(cb) { | ||
@@ -49,6 +49,6 @@ for (var i = 0; i < length; i++) | ||
} | ||
function getUVal(bor) { | ||
var val = 0; | ||
bor(function(i, o) { | ||
bor(function (i, o) { | ||
val += Math.pow(256, o) * p.buf[offset + i]; | ||
@@ -58,7 +58,7 @@ }) | ||
} | ||
function getSVal(bor) { | ||
var val = getUVal(bor); | ||
if ((p.buf[offset + ( le ? (length - 1) : 0)] & 0x80) == 0x80) { | ||
if ((p.buf[offset + (le ? (length - 1) : 0)] & 0x80) == 0x80) { | ||
val -= Math.pow(256, length); | ||
@@ -68,5 +68,5 @@ } | ||
} | ||
function setVal(bor, val) { | ||
bor(function(i, o) { | ||
bor(function (i, o) { | ||
p.buf[offset + i] = Math.floor(val / Math.pow(256, o)) & 0xff; | ||
@@ -77,21 +77,21 @@ }); | ||
var | ||
nativeSuff = (signed?'':'U') +'Int'+ (length*8)+ (le?'LE':'BE'), | ||
readMethod = Buffer.prototype['read' + nativeSuff], writeMethod = Buffer.prototype['write' + nativeSuff]; | ||
nativeSuff = (signed?'':'U') + 'Int' + (length * 8) + (le?'LE':'BE'), | ||
readMethod = Buffer.prototype['read' + nativeSuff], writeMethod = Buffer.prototype['write' + nativeSuff]; | ||
if (!readMethod) { | ||
this.get = function() { | ||
this.get = function () { | ||
var bor = le ? lec : bec; | ||
return ( signed ? getSVal(bor) : getUVal(bor)); | ||
return (signed ? getSVal(bor) : getUVal(bor)); | ||
} | ||
} | ||
else { | ||
this.get = function() { | ||
return readMethod.call(p.buf,offset); | ||
}; | ||
this.get = function () { | ||
return readMethod.call(p.buf, offset); | ||
}; | ||
} | ||
if (!writeMethod) { | ||
this.set = function(val) { | ||
this.set = function (val) { | ||
var bor = le ? lec : bec; | ||
@@ -102,4 +102,4 @@ setVal(bor, val); | ||
else { | ||
this.set = function(val){ | ||
writeMethod.call(p.buf,val,offset); | ||
this.set = function (val) { | ||
writeMethod.call(p.buf, val, offset); | ||
} | ||
@@ -110,3 +110,25 @@ } | ||
function charField(p, offset, length, encoding) { | ||
function floatField(p, offset, le) { | ||
this.length = 4; | ||
this.offset = offset; | ||
this.get = function () { | ||
return le ? p.buf.readFloatLE(offset) : p.buf.readFloatBE(offset); | ||
} | ||
this.set = function (val) { | ||
return le ? p.buf.writeFloatLE(val, offset) : p.buf.writeFloatBE(val, offset); | ||
} | ||
} | ||
function doubleField(p, offset, le) { | ||
this.length = 8; | ||
this.offset = offset; | ||
this.get = function () { | ||
return le ? p.buf.readDoubleLE(offset) : p.buf.readDoubleBE(offset); | ||
} | ||
this.set = function (val) { | ||
return le ? p.buf.writeDoubleLE(val, offset) : p.buf.writeDoubleBE(val, offset); | ||
} | ||
} | ||
function charField(p, offset, length, encoding, secure) { | ||
var self = this; | ||
@@ -116,6 +138,8 @@ self.length = length; | ||
self.encoding = encoding; | ||
self.get = function() { | ||
if (!length) return; | ||
self.secure = secure; | ||
self.get = function () { | ||
if (!length) | ||
return; | ||
var result = p.buf.toString(self.encoding, offset, offset + length); | ||
var result = p.buf.toString(self.encoding, offset, (offset + length)); | ||
var strlen = result.indexOf("\0"); | ||
@@ -128,15 +152,29 @@ if (strlen == -1) { | ||
} | ||
self.set = function(val) { | ||
if (!length) return; | ||
self.set = function (val) { | ||
if (!length) | ||
return; | ||
// Be string is terminated with the null char, else troncate it | ||
if (secure === true) { | ||
// Append \0 to the string | ||
val += "\0"; | ||
if (val.length >= length) { | ||
val = val.substring(0, length - 1); | ||
val += "\0"; | ||
} | ||
// Write to buffer | ||
p.buf.write(val, offset, val.length, self.encoding); | ||
// Fill rest of the buffer with \0 | ||
var remainSpace = (length - val.length); | ||
if (remainSpace > 0) { | ||
p.buf.fill(0, (offset + val.length), length); | ||
} | ||
/* | ||
// comment off these might be less rubust, but chars encoding | ||
// would be ok | ||
val += "\0"; | ||
if (val.length > length) | ||
val = val.substring(0, length); | ||
*/ | ||
// buf.write(string, [offset], [length], [encoding]) | ||
p.buf.write(val, offset, length, self.encoding); | ||
} else { | ||
// Trust Buffer class to write the string into the buffer | ||
p.buf.write(val, offset, length, self.encoding); | ||
} | ||
} | ||
@@ -148,9 +186,9 @@ } | ||
this.offset = offset; | ||
this.get = function() { | ||
this.get = function () { | ||
return struct; | ||
} | ||
this.set = function(val) { | ||
this.set = function (val) { | ||
struct.set(val); | ||
} | ||
this.allocate = function() { | ||
this.allocate = function () { | ||
struct._setBuff(p.buf.slice(offset, offset + struct.length())); | ||
@@ -165,5 +203,5 @@ } | ||
for (var i = 0; i < len; i++) { | ||
if ( type instanceof Struct) { | ||
if (type instanceof Struct) { | ||
as.struct(i, type.clone()); | ||
} else if ( type in as) { | ||
} else if (type in as) { | ||
args[0] = i; | ||
@@ -175,9 +213,9 @@ as[type].apply(as, args); | ||
this.offset = offset; | ||
this.allocate = function() { | ||
this.allocate = function () { | ||
as._setBuff(p.buf.slice(offset, offset + as.length())); | ||
} | ||
this.get = function() { | ||
this.get = function () { | ||
return as; | ||
} | ||
this.set = function(val) { | ||
this.set = function (val) { | ||
as.set(val); | ||
@@ -190,3 +228,3 @@ } | ||
return new Struct; | ||
var priv = { | ||
@@ -199,3 +237,3 @@ buf : {}, | ||
}, self = this; | ||
function checkAllocated() { | ||
@@ -205,18 +243,34 @@ if (priv.allocated) | ||
} | ||
this.word8 = function(key) { | ||
checkAllocated(); | ||
priv.closures.push(function(p) { | ||
p.fields[key] = new byteField(p, p.len); | ||
p.len++; | ||
}); | ||
return this; | ||
}; | ||
// Create handlers for various float Field Variants | ||
[true, false].forEach(function (le) { | ||
self['float' + (le ? 'le' : 'be')] = function (key) { | ||
checkAllocated(); | ||
priv.closures.push(function (p) { | ||
var n = 4; | ||
p.fields[key] = new floatField(p, p.len, le); | ||
p.len += n; | ||
}); | ||
return this; | ||
} | ||
}); | ||
// Create handlers for various double Field Variants | ||
[true, false].forEach(function (le) { | ||
self['double' + (le ? 'le' : 'be')] = function (key) { | ||
checkAllocated(); | ||
priv.closures.push(function (p) { | ||
var n = 8; | ||
p.fields[key] = new doubleField(p, p.len, le); | ||
p.len += n; | ||
}); | ||
return this; | ||
} | ||
}); | ||
// Create handlers for various Bool Field Variants | ||
[1, 2, 3, 4].forEach(function(n) { | ||
self['bool' + (n == 1 ? '' : n)] = function(key) { | ||
[1, 2, 3, 4].forEach(function (n) { | ||
self['bool' + (n == 1 ? '' : n)] = function (key) { | ||
checkAllocated(); | ||
priv.closures.push(function(p) { | ||
priv.closures.push(function (p) { | ||
p.fields[key] = new boolField(p, p.len, n); | ||
@@ -228,11 +282,11 @@ p.len += n; | ||
}); | ||
// Create handlers for various Integer Field Variants | ||
[1, 2, 3, 4, 6, 8].forEach(function(n) { | ||
[true, false].forEach(function(le) { | ||
[true, false].forEach(function(signed) { | ||
var name = 'word' + (n * 8) + ( signed ? 'S' : 'U') + ( le ? 'le' : 'be'); | ||
self[name] = function(key) { | ||
[1, 2, 3, 4, 6, 8].forEach(function (n) { | ||
[true, false].forEach(function (le) { | ||
[true, false].forEach(function (signed) { | ||
var name = 'word' + (n * 8) + (signed ? 'S' : 'U') + (le ? 'le' : 'be'); | ||
self[name] = function (key) { | ||
checkAllocated(); | ||
priv.closures.push(function(p) { | ||
priv.closures.push(function (p) { | ||
p.fields[key] = new intField(p, p.len, n, le, signed); | ||
@@ -246,15 +300,18 @@ p.len += n; | ||
}); | ||
this.word8 = this.word8Ule; | ||
['chars', 'charsnt'].forEach(function (c) { | ||
self[c] = function (key, length, encoding) { | ||
checkAllocated(); | ||
priv.closures.push(function (p) { | ||
p.fields[key] = new charField(p, p.len, length, encoding || 'ascii', (c == 'charsnt')); | ||
p.len += length; | ||
}); | ||
return this; | ||
} | ||
}); | ||
this.chars = function(key, length, encoding) { | ||
this.struct = function (key, struct) { | ||
checkAllocated(); | ||
priv.closures.push(function(p) { | ||
p.fields[key] = new charField(p, p.len, length, encoding || 'ascii'); | ||
p.len += length; | ||
}); | ||
return this; | ||
} | ||
this.struct = function(key, struct) { | ||
checkAllocated(); | ||
priv.closures.push(function(p) { | ||
priv.closures.push(function (p) { | ||
p.fields[key] = new structField(p, p.len, struct.clone()); | ||
@@ -269,10 +326,9 @@ p.len += p.fields[key].length; | ||
} | ||
F.prototype = constructor.prototype; | ||
return new F(); | ||
} | ||
this.array = function(key, length, type) { | ||
this.array = function (key, length, type) { | ||
checkAllocated(); | ||
@@ -282,3 +338,3 @@ var args = [].slice.call(arguments, 1); | ||
args.unshift(null); | ||
priv.closures.push(function(p) { | ||
priv.closures.push(function (p) { | ||
args[0] = p; | ||
@@ -289,11 +345,11 @@ args[1] = p.len; | ||
}); | ||
return this; | ||
} | ||
var beenHere = false; | ||
function applyClosures(p) { | ||
if (beenHere) | ||
return; | ||
p.closures.forEach(function(el) { | ||
p.closures.forEach(function (el) { | ||
el(p); | ||
@@ -303,3 +359,3 @@ }); | ||
} | ||
function allocateFields() { | ||
@@ -311,12 +367,21 @@ for (var key in priv.fields) { | ||
} | ||
this._setBuff = this.setBuffer = function(buff) { | ||
priv.buf = buff; | ||
this._setBuff = this.setBuffer = function (buff, buffLength) { | ||
applyClosures(priv); | ||
if (typeof (buffLength) === 'number') { | ||
if (buffLength > buff.length) { | ||
throw new Error('Invalid specified buffer size !'); | ||
} | ||
priv.buf = buff.slice(0, buffLength); | ||
} else { | ||
priv.buf = buff; | ||
} | ||
if (priv.buf.length < priv.len) { | ||
throw new Error('Buffer size too small for struct layout !'); | ||
} | ||
allocateFields(); | ||
priv.allocated = true; | ||
} | ||
this.allocate = function() { | ||
this.allocate = function () { | ||
applyClosures(priv); | ||
@@ -328,12 +393,12 @@ priv.buf = new Buffer(priv.len); | ||
} | ||
this._getPriv = function() { | ||
this._getPriv = function () { | ||
return priv; | ||
} | ||
this.getOffset = function(field){ | ||
if(priv.fields[field]) return priv.fields[field].offset; | ||
this.getOffset = function (field) { | ||
if (priv.fields[field]) return priv.fields[field].offset; | ||
} | ||
this.clone = function() { | ||
this.clone = function () { | ||
var c = new Struct; | ||
@@ -344,10 +409,10 @@ var p = c._getPriv(); | ||
} | ||
this.length = function() { | ||
this.length = function () { | ||
applyClosures(priv); | ||
return priv.len; | ||
} | ||
this.get = function(key) { | ||
if ( key in priv.fields) { | ||
this.get = function (key) { | ||
if (key in priv.fields) { | ||
return priv.fields[key].get(); | ||
@@ -357,6 +422,6 @@ } else | ||
} | ||
this.set = function(key, val) { | ||
this.set = function (key, val) { | ||
if (arguments.length == 2) { | ||
if ( key in priv.fields) { | ||
if (key in priv.fields) { | ||
priv.fields[key].set(val); | ||
@@ -373,3 +438,3 @@ } else | ||
} | ||
this.buffer = function() { | ||
this.buffer = function () { | ||
return priv.buf; | ||
@@ -381,17 +446,17 @@ } | ||
var fields = {}; | ||
Object.keys(priv.fields).forEach(function(key) { | ||
Object.keys(priv.fields).forEach(function (key) { | ||
var setFunc, getFunc; | ||
if(priv.fields[key] instanceof structField || | ||
priv.fields[key] instanceof arrayField) { | ||
getFunc = function(){ | ||
return priv.fields[key].get().fields; | ||
}; | ||
setFunc = function(newVal){ | ||
self.set(key, newVal); | ||
}; | ||
} | ||
else { | ||
getFunc = priv.fields[key].get; | ||
setFunc = priv.fields[key].set; | ||
}; | ||
if (priv.fields[key] instanceof structField || | ||
priv.fields[key] instanceof arrayField) { | ||
getFunc = function () { | ||
return priv.fields[key].get().fields; | ||
}; | ||
setFunc = function (newVal) { | ||
self.set(key, newVal); | ||
}; | ||
} | ||
else { | ||
getFunc = priv.fields[key].get; | ||
setFunc = priv.fields[key].set; | ||
}; | ||
@@ -406,9 +471,9 @@ Object.defineProperty(fields, key, { | ||
}; | ||
var _fields; | ||
Object.defineProperty(this, 'fields', { | ||
get : function() { | ||
get : function () { | ||
if (_fields) | ||
return _fields; | ||
return ( _fields = getFields()); | ||
return (_fields = getFields()); | ||
}, | ||
@@ -419,2 +484,2 @@ enumerable : true, | ||
} | ||
} |
{ "name" : "struct" | ||
, "version" : "v0.0.8" | ||
, "version" : "v0.0.10" | ||
, "description" : "Pack/Unpack multibyte binary values from/to buffers " | ||
@@ -4,0 +4,0 @@ , "author": "Denys Khanzhiyev" |
@@ -86,5 +86,14 @@ Struct | ||
### floatle(name),floatbe(name) | ||
define one float field | ||
### doublele(name),doublebe(name) | ||
define one double field | ||
### chars(name,length[,encoding]) | ||
defines array of chars with `encoding` ('ascii' by default) encoding, name - name of the field, length - length of array | ||
### charsnt(name,length[,encoding]) | ||
same as chars but ensure that string is null terminated and buffer remaining space is fill with \0 | ||
### array(name, length, type, ...) | ||
@@ -91,0 +100,0 @@ defines array of fields (internally it is Struct() object with field names set to 0,1,2,... ). |
var Struct = require('../index.js'); | ||
/******************************************************************/ | ||
// Create structs and automatically allocate an internal buffer | ||
/******************************************************************/ | ||
// Create 3 structures | ||
var Car = Struct() | ||
.charnt('constructor', 20) | ||
.charnt('model', 32) | ||
.word16Ule('year', 20) | ||
var Person = Struct() | ||
.chars('firstName',10) | ||
.chars('lastName',10) | ||
.array('items',3,'chars',10) | ||
.word16Sle('balance'), | ||
Persons = Struct() | ||
.word8('presentCount') | ||
.array('list',2,Person); | ||
.chars('firstName', 10) | ||
.chars('lastName', 10) | ||
.array('items', 3, 'chars', 10) | ||
.word16Sle('balance'), | ||
.struct('car', Car); | ||
Persons = Struct() | ||
.word8('presentCount') | ||
.array('list', 2, Person); | ||
// Allocate an internal buffer required to hold all struct fields | ||
Persons.allocate(); | ||
// Get total size (in bytes) of all fields | ||
console.log(Persons.length()); | ||
// Display Persons internal buffer values | ||
var buf = Persons.buffer(); | ||
for (var i = 0; i < buf.length ; i++) { | ||
buf[i] = 0; | ||
} | ||
buf.fill(0); | ||
console.log(buf); | ||
// Write directly through the struct field setter | ||
var proxy = Persons.fields; | ||
proxy.presentCount = 2; | ||
console.log(buf); | ||
proxy.list[0].firstName = 'John'; | ||
@@ -33,2 +43,4 @@ proxy.list[0].lastName = 'Johnson'; | ||
proxy.list[0].balance = -100; | ||
proxy.list[0].car.constructor = "honda"; | ||
proxy.list[0].car.model = "civic"; | ||
@@ -40,4 +52,34 @@ proxy.list[1].firstName = 'Bob'; | ||
proxy.list[1].balance = +100; | ||
proxy.list[1].car.constructor = "Very long long constructor name that will be truncated."; | ||
proxy.list[1].car.model = "We don't care."; | ||
// Display struct buffer internal content | ||
console.log(buf); | ||
console.log(proxy.list[1].items[1].length ); | ||
// Get length of a specific field | ||
console.log(proxy.list[1].items[1].length); | ||
/******************************************************************/ | ||
// Create a struct and assign it an existing buffer | ||
/******************************************************************/ | ||
// Create struct | ||
var Contact = Struct() | ||
.charsnt('name', 64) | ||
.charsnt('email', 64); | ||
// Create a buffer manually using the size in bytes of all fields of this struct | ||
var contactBuffer = new Buffer(Contact.length()); | ||
contactBuffer.fill(0); | ||
// Assign buffer to the struct | ||
// If the buffer does not have enough space an exception will be thrown | ||
Contact.setBuffer(contactBuffer); | ||
// Write directly through the buffer | ||
contactBuffer.write("Martin", 0) | ||
// Test if it work by displaying the Contact.name field | ||
console.log(Contact.name); | ||
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
22666
503
137