refact
This commit is contained in:
parent
daf8bdf28a
commit
feb24ff4af
4
build
4
build
|
@ -1,4 +1,4 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
browserify index.js > dist/pluie.bin.js
|
browserify index.js > dist/pluie-bin.js
|
||||||
uglifyjs dist/pluie.bin.js -c -m -o dist/pluie.bin.min.js
|
uglifyjs dist/pluie-bin.js -c -m -o dist/pluie-bin.min.js
|
||||||
|
|
79
dist/pluie.bin.js → dist/pluie-bin.js
vendored
79
dist/pluie.bin.js → dist/pluie-bin.js
vendored
|
@ -1,10 +1,15 @@
|
||||||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
||||||
bin = require('./lib/bin');
|
if (typeof pluie == 'undefined') {
|
||||||
module.exports = bin;
|
pluie = { bin : {}};
|
||||||
|
}
|
||||||
|
require('./lib/bin')(pluie.bin);
|
||||||
|
module.exports = pluie;
|
||||||
|
|
||||||
},{"./lib/bin":2}],2:[function(require,module,exports){
|
},{"./lib/bin":2}],2:[function(require,module,exports){
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = function (ns) {
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
String.prototype.removeEndNullBytes = function() {
|
String.prototype.removeEndNullBytes = function() {
|
||||||
return this.replace(/\0+$/g, '');
|
return this.replace(/\0+$/g, '');
|
||||||
|
@ -15,12 +20,10 @@ String.prototype.removeStartNullBytes = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
var bin =
|
ns.enc = new TextEncoder("utf-8");
|
||||||
{
|
ns.dec = new TextDecoder("utf-8", {fatal: true});
|
||||||
enc : new TextEncoder("utf-8"),
|
|
||||||
dec : new TextDecoder("utf-8", {fatal: true}),
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
intFromBytes : function(bytes)
|
ns.intFromBytes = function(bytes)
|
||||||
{
|
{
|
||||||
var value = null;
|
var value = null;
|
||||||
if (bytes!=null && bytes.length > 0) {
|
if (bytes!=null && bytes.length > 0) {
|
||||||
|
@ -30,9 +33,9 @@ var bin =
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bytesFromInt : function(value, size)
|
ns.bytesFromInt = function(value, size)
|
||||||
{
|
{
|
||||||
var bytes = new Uint8Array(size);
|
var bytes = new Uint8Array(size);
|
||||||
while (value) {
|
while (value) {
|
||||||
|
@ -40,9 +43,9 @@ var bin =
|
||||||
value >>= 8;
|
value >>= 8;
|
||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
pack : function(size, type, value)
|
ns.pack = function(size, type, value)
|
||||||
{
|
{
|
||||||
var packed = null
|
var packed = null
|
||||||
if (type == 'n' || type == 'N') {
|
if (type == 'n' || type == 'N') {
|
||||||
|
@ -56,9 +59,9 @@ var bin =
|
||||||
packed.set(this.enc.encode(value), 0);
|
packed.set(this.enc.encode(value), 0);
|
||||||
}
|
}
|
||||||
return packed;
|
return packed;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
unpack : function(size, type, bytes)
|
ns.unpack = function(size, type, bytes)
|
||||||
{
|
{
|
||||||
var unpacked = null
|
var unpacked = null
|
||||||
if (type == 'n' || type == 'N') {
|
if (type == 'n' || type == 'N') {
|
||||||
|
@ -69,9 +72,9 @@ var bin =
|
||||||
unpacked = this.dec.decode(bytes.slice(0, size)).removeEndNullBytes();
|
unpacked = this.dec.decode(bytes.slice(0, size)).removeEndNullBytes();
|
||||||
}
|
}
|
||||||
return unpacked;
|
return unpacked;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
mergeBuffers : function(bufferArray)
|
ns.mergeBuffers = function(bufferArray)
|
||||||
{
|
{
|
||||||
var maxLength = 0;
|
var maxLength = 0;
|
||||||
for(var i=0, lim=bufferArray.length; i < lim; i++) {
|
for(var i=0, lim=bufferArray.length; i < lim; i++) {
|
||||||
|
@ -87,24 +90,28 @@ var bin =
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tmp;
|
return tmp;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
getBuffer : function(array, size)
|
ns.getBuffer = function(array, size)
|
||||||
{
|
{
|
||||||
var buffer = new Uint8Array(!size ? array.length : size);
|
var buffer = new Uint8Array(!size ? array.length : size);
|
||||||
buffer.set(new Uint8Array(array), 0);
|
buffer.set(new Uint8Array(array), 0);
|
||||||
return buffer;
|
return buffer;
|
||||||
},
|
};
|
||||||
}
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
require('./file')(bin);
|
require('./file')(ns);
|
||||||
module.exports = bin;
|
return ns;
|
||||||
|
}
|
||||||
|
|
||||||
},{"./file":3}],3:[function(require,module,exports){
|
},{"./file":3}],3:[function(require,module,exports){
|
||||||
module.exports = function (bin) {
|
'use strict';
|
||||||
require('./header')(bin);
|
|
||||||
|
module.exports = function (ns) {
|
||||||
|
|
||||||
|
require('./header')(ns);
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.File = function(binary, header) {
|
ns.File = function(binary, header) {
|
||||||
this.header = header;
|
this.header = header;
|
||||||
this.binary = binary;
|
this.binary = binary;
|
||||||
this.extractProperties = function(binary) {
|
this.extractProperties = function(binary) {
|
||||||
|
@ -112,7 +119,7 @@ module.exports = function (bin) {
|
||||||
if (binary != null && binary.length >= this.header.def.length) {
|
if (binary != null && binary.length >= this.header.def.length) {
|
||||||
this.header.def.map.forEach (function(prop) {
|
this.header.def.map.forEach (function(prop) {
|
||||||
var bytes = binary.slice(prop.offset, prop.offset+prop.length);
|
var bytes = binary.slice(prop.offset, prop.offset+prop.length);
|
||||||
var unpacked = bin.unpack(prop.size, prop.type, bytes);
|
var unpacked = ns.unpack(prop.size, prop.type, bytes);
|
||||||
prop.value = unpacked;
|
prop.value = unpacked;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -121,16 +128,16 @@ module.exports = function (bin) {
|
||||||
var data = [];
|
var data = [];
|
||||||
this.header.buildProperties(properties);
|
this.header.buildProperties(properties);
|
||||||
this.header.def.map.forEach (function(prop) {
|
this.header.def.map.forEach (function(prop) {
|
||||||
var packed = bin.pack(prop.size, prop.type, prop.value);
|
var packed = ns.pack(prop.size, prop.type, prop.value);
|
||||||
data.push(packed);
|
data.push(packed);
|
||||||
});
|
});
|
||||||
data = bin.mergeBuffers(data);
|
data = ns.mergeBuffers(data);
|
||||||
};
|
};
|
||||||
this.extractProperties(binary);
|
this.extractProperties(binary);
|
||||||
}
|
}
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.smpFile = function(binary) {
|
ns.smpFile = function(binary) {
|
||||||
return new bin.File(binary, new bin.Header([
|
return new ns.File(binary, new ns.Header([
|
||||||
['signature', 'a', 9, '∢SMP0.2'],
|
['signature', 'a', 9, '∢SMP0.2'],
|
||||||
['size' , 'N', 1, ''],
|
['size' , 'N', 1, ''],
|
||||||
['id' , 'N', 1, ''],
|
['id' , 'N', 1, ''],
|
||||||
|
@ -143,8 +150,8 @@ module.exports = function (bin) {
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.smpsFile = function(binary) {
|
ns.smpsFile = function(binary) {
|
||||||
return new bin.File(binary, new bin.Header([
|
return new ns.File(binary, new ns.Header([
|
||||||
['signature', 'a', 13, 'ﷸSMPS0.2∢'],
|
['signature', 'a', 13, 'ﷸSMPS0.2∢'],
|
||||||
['size' , 'N', 1, ''],
|
['size' , 'N', 1, ''],
|
||||||
['nonce' , 'a', 12, ''],
|
['nonce' , 'a', 12, ''],
|
||||||
|
@ -152,8 +159,8 @@ module.exports = function (bin) {
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.smpaFile = function(binary) {
|
ns.smpaFile = function(binary) {
|
||||||
return new bin.BaseFile(binary, new bin.Header([
|
return new ns.BaseFile(binary, new ns.Header([
|
||||||
['signature', 'a', 13, 'ﷸSMPA0.2∢'],
|
['signature', 'a', 13, 'ﷸSMPA0.2∢'],
|
||||||
['size' , 'N', 1, ''],
|
['size' , 'N', 1, ''],
|
||||||
['nonce' , 'a', 24, ''],
|
['nonce' , 'a', 24, ''],
|
||||||
|
@ -165,8 +172,8 @@ module.exports = function (bin) {
|
||||||
},{"./header":4}],4:[function(require,module,exports){
|
},{"./header":4}],4:[function(require,module,exports){
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
module.exports = function (ns) {
|
||||||
module.exports = function (bin) {
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
var HeaderProperty = function(name, type, size, defaultValue)
|
var HeaderProperty = function(name, type, size, defaultValue)
|
||||||
{
|
{
|
||||||
|
@ -202,7 +209,7 @@ module.exports = function (bin) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.Header = function(def) {
|
ns.Header = function(def) {
|
||||||
this.def = new HeaderDef(def);
|
this.def = new HeaderDef(def);
|
||||||
this.value = function(key) {
|
this.value = function(key) {
|
||||||
return this.def.getProperty(key).value;
|
return this.def.getProperty(key).value;
|
1
dist/pluie-bin.min.js
vendored
Normal file
1
dist/pluie-bin.min.js
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
!function e(n,t,r){function i(a,o){if(!t[a]){if(!n[a]){var s="function"==typeof require&&require;if(!o&&s)return s(a,!0);if(u)return u(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var c=t[a]={exports:{}};n[a][0].call(c.exports,function(e){var t=n[a][1][e];return i(t?t:e)},c,c.exports,e,n,t,r)}return t[a].exports}for(var u="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(e,n,t){"undefined"==typeof pluie&&(pluie={bin:{}}),e("./lib/bin")(pluie.bin),n.exports=pluie},{"./lib/bin":2}],2:[function(e,n,t){"use strict";n.exports=function(n){return String.prototype.removeEndNullBytes=function(){return this.replace(/\0+$/g,"")},String.prototype.removeStartNullBytes=function(){return this.replace(/^\0+/g,"")},n.enc=new TextEncoder("utf-8"),n.dec=new TextDecoder("utf-8",{fatal:!0}),n.intFromBytes=function(e){var n=null;if(null!=e&&e.length>0)for(var t=n=0,r=e.length;t<r;t++)n=256*n+e[t];return n},n.bytesFromInt=function(e,n){for(var t=new Uint8Array(n);e;)t.set([255&e],--n),e>>=8;return t},n.pack=function(e,n,t){var r=null;return"n"==n||"N"==n?t>=0&&t<=2147483647&&(r=this.bytesFromInt(t,"n"==n?2:4)):"a"==n&&(r=new Uint8Array(e),r.set(this.enc.encode(t),0)),r},n.unpack=function(e,n,t){var r=null;if("n"==n||"N"==n){var i=t.slice(0,e*("n"==n?2:4));r=this.intFromBytes(i)}else"a"==n&&(r=this.dec.decode(t.slice(0,e)).removeEndNullBytes());return r},n.mergeBuffers=function(e){for(var n=0,t=0,r=e.length;t<r;t++)null!=e[t]&&(n+=e[t].byteLength);for(var i=new Uint8Array(n),t=0,u=0,r=e.length;t<r;t++)null!=e[t]&&(i.set(new Uint8Array(e[t]),u),u+=e[t].byteLength);return i},n.getBuffer=function(e,n){var t=new Uint8Array(n?n:e.length);return t.set(new Uint8Array(e),0),t},e("./file")(n),n}},{"./file":3}],3:[function(e,n,t){"use strict";n.exports=function(n){e("./header")(n),n.File=function(e,t){this.header=t,this.binary=e,this.extractProperties=function(e){e||(e=this.binary),null!=e&&e.length>=this.header.def.length&&this.header.def.map.forEach(function(t){var r=e.slice(t.offset,t.offset+t.length),i=n.unpack(t.size,t.type,r);t.value=i})},this.buildProperties=function(e){var t=[];this.header.buildProperties(e),this.header.def.map.forEach(function(e){var r=n.pack(e.size,e.type,e.value);t.push(r)}),t=n.mergeBuffers(t)},this.extractProperties(e)},n.smpFile=function(e){return new n.File(e,new n.Header([["signature","a",9,"∢SMP0.2"],["size","N",1,""],["id","N",1,""],["from","a",32,""],["to","a",32,""],["channel","a",32,""],["command","n",1,""],["index","n",1,""],["split","n",1,""]]))},n.smpsFile=function(e){return new n.File(e,new n.Header([["signature","a",13,"ﷸSMPS0.2∢"],["size","N",1,""],["nonce","a",12,""],["cypher","a",135,""]]))},n.smpaFile=function(e){return new n.BaseFile(e,new n.Header([["signature","a",13,"ﷸSMPA0.2∢"],["size","N",1,""],["nonce","a",24,""],["cypher","a",199,""]]))}}},{"./header":4}],4:[function(e,n,t){"use strict";n.exports=function(e){var n=function(e,n,t,r){this.name=e,this.type=n,this.size=t,this.length="N"==n?4*t:"n"==n?2*t:t,this.value=r},t=function(e){var t=[];e.forEach(function(e){t.push(new n(e[0],e[1],e[2],e[3]))}),this.map=t;var r=[],i={},u=0,a=0;this.map.forEach(function(e){e.offset=u,r[r.length]=e.name,u+=e.length,i[e.name]=a++}),this.indexes=i,this.keys=r,this.length=u,this.getProperty=function(e){return this.map[this.indexes[e]]}};e.Header=function(e){this.def=new t(e),this.value=function(e){return this.def.getProperty(e).value},this.buildProperties=function(e){this.def.map.forEach(function(n){"signature"!=n.name&&"undefined"!=typeof e[n.name]&&(n.value=e[n.name])})}}}},{}]},{},[1]);
|
1
dist/pluie.bin.min.js
vendored
1
dist/pluie.bin.min.js
vendored
|
@ -1 +0,0 @@
|
||||||
!function e(n,t,r){function i(u,o){if(!t[u]){if(!n[u]){var s="function"==typeof require&&require;if(!o&&s)return s(u,!0);if(a)return a(u,!0);var f=new Error("Cannot find module '"+u+"'");throw f.code="MODULE_NOT_FOUND",f}var c=t[u]={exports:{}};n[u][0].call(c.exports,function(e){var t=n[u][1][e];return i(t?t:e)},c,c.exports,e,n,t,r)}return t[u].exports}for(var a="function"==typeof require&&require,u=0;u<r.length;u++)i(r[u]);return i}({1:[function(e,n,t){bin=e("./lib/bin"),n.exports=bin},{"./lib/bin":2}],2:[function(e,n,t){"use strict";String.prototype.removeEndNullBytes=function(){return this.replace(/\0+$/g,"")},String.prototype.removeStartNullBytes=function(){return this.replace(/^\0+/g,"")};var r={enc:new TextEncoder("utf-8"),dec:new TextDecoder("utf-8",{fatal:!0}),intFromBytes:function(e){var n=null;if(null!=e&&e.length>0)for(var t=n=0,r=e.length;t<r;t++)n=256*n+e[t];return n},bytesFromInt:function(e,n){for(var t=new Uint8Array(n);e;)t.set([255&e],--n),e>>=8;return t},pack:function(e,n,t){var r=null;return"n"==n||"N"==n?t>=0&&t<=2147483647&&(r=this.bytesFromInt(t,"n"==n?2:4)):"a"==n&&(r=new Uint8Array(e),r.set(this.enc.encode(t),0)),r},unpack:function(e,n,t){var r=null;if("n"==n||"N"==n){var i=t.slice(0,e*("n"==n?2:4));r=this.intFromBytes(i)}else"a"==n&&(r=this.dec.decode(t.slice(0,e)).removeEndNullBytes());return r},mergeBuffers:function(e){for(var n=0,t=0,r=e.length;t<r;t++)null!=e[t]&&(n+=e[t].byteLength);for(var i=new Uint8Array(n),t=0,a=0,r=e.length;t<r;t++)null!=e[t]&&(i.set(new Uint8Array(e[t]),a),a+=e[t].byteLength);return i},getBuffer:function(e,n){var t=new Uint8Array(n?n:e.length);return t.set(new Uint8Array(e),0),t}};e("./file")(r),n.exports=r},{"./file":3}],3:[function(e,n,t){n.exports=function(n){e("./header")(n),n.File=function(e,t){this.header=t,this.binary=e,this.extractProperties=function(e){e||(e=this.binary),null!=e&&e.length>=this.header.def.length&&this.header.def.map.forEach(function(t){var r=e.slice(t.offset,t.offset+t.length),i=n.unpack(t.size,t.type,r);t.value=i})},this.buildProperties=function(e){var t=[];this.header.buildProperties(e),this.header.def.map.forEach(function(e){var r=n.pack(e.size,e.type,e.value);t.push(r)}),t=n.mergeBuffers(t)},this.extractProperties(e)},n.smpFile=function(e){return new n.File(e,new n.Header([["signature","a",9,"∢SMP0.2"],["size","N",1,""],["id","N",1,""],["from","a",32,""],["to","a",32,""],["channel","a",32,""],["command","n",1,""],["index","n",1,""],["split","n",1,""]]))},n.smpsFile=function(e){return new n.File(e,new n.Header([["signature","a",13,"ﷸSMPS0.2∢"],["size","N",1,""],["nonce","a",12,""],["cypher","a",135,""]]))},n.smpaFile=function(e){return new n.BaseFile(e,new n.Header([["signature","a",13,"ﷸSMPA0.2∢"],["size","N",1,""],["nonce","a",24,""],["cypher","a",199,""]]))}}},{"./header":4}],4:[function(e,n,t){"use strict";n.exports=function(e){var n=function(e,n,t,r){this.name=e,this.type=n,this.size=t,this.length="N"==n?4*t:"n"==n?2*t:t,this.value=r},t=function(e){var t=[];e.forEach(function(e){t.push(new n(e[0],e[1],e[2],e[3]))}),this.map=t;var r=[],i={},a=0,u=0;this.map.forEach(function(e){e.offset=a,r[r.length]=e.name,a+=e.length,i[e.name]=u++}),this.indexes=i,this.keys=r,this.length=a,this.getProperty=function(e){return this.map[this.indexes[e]]}};e.Header=function(e){this.def=new t(e),this.value=function(e){return this.def.getProperty(e).value},this.buildProperties=function(e){this.def.map.forEach(function(n){"signature"!=n.name&&"undefined"!=typeof e[n.name]&&(n.value=e[n.name])})}}}},{}]},{},[1]);
|
|
7
index.js
7
index.js
|
@ -1,2 +1,5 @@
|
||||||
bin = require('./lib/bin');
|
if (typeof pluie == 'undefined') {
|
||||||
module.exports = bin;
|
pluie = { bin : {}};
|
||||||
|
}
|
||||||
|
require('./lib/bin')(pluie.bin);
|
||||||
|
module.exports = pluie;
|
||||||
|
|
38
lib/bin.js
38
lib/bin.js
|
@ -1,5 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = function (ns) {
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
String.prototype.removeEndNullBytes = function() {
|
String.prototype.removeEndNullBytes = function() {
|
||||||
return this.replace(/\0+$/g, '');
|
return this.replace(/\0+$/g, '');
|
||||||
|
@ -10,12 +12,10 @@ String.prototype.removeStartNullBytes = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
var bin =
|
ns.enc = new TextEncoder("utf-8");
|
||||||
{
|
ns.dec = new TextDecoder("utf-8", {fatal: true});
|
||||||
enc : new TextEncoder("utf-8"),
|
|
||||||
dec : new TextDecoder("utf-8", {fatal: true}),
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
intFromBytes : function(bytes)
|
ns.intFromBytes = function(bytes)
|
||||||
{
|
{
|
||||||
var value = null;
|
var value = null;
|
||||||
if (bytes!=null && bytes.length > 0) {
|
if (bytes!=null && bytes.length > 0) {
|
||||||
|
@ -25,9 +25,9 @@ var bin =
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bytesFromInt : function(value, size)
|
ns.bytesFromInt = function(value, size)
|
||||||
{
|
{
|
||||||
var bytes = new Uint8Array(size);
|
var bytes = new Uint8Array(size);
|
||||||
while (value) {
|
while (value) {
|
||||||
|
@ -35,9 +35,9 @@ var bin =
|
||||||
value >>= 8;
|
value >>= 8;
|
||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
pack : function(size, type, value)
|
ns.pack = function(size, type, value)
|
||||||
{
|
{
|
||||||
var packed = null
|
var packed = null
|
||||||
if (type == 'n' || type == 'N') {
|
if (type == 'n' || type == 'N') {
|
||||||
|
@ -51,9 +51,9 @@ var bin =
|
||||||
packed.set(this.enc.encode(value), 0);
|
packed.set(this.enc.encode(value), 0);
|
||||||
}
|
}
|
||||||
return packed;
|
return packed;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
unpack : function(size, type, bytes)
|
ns.unpack = function(size, type, bytes)
|
||||||
{
|
{
|
||||||
var unpacked = null
|
var unpacked = null
|
||||||
if (type == 'n' || type == 'N') {
|
if (type == 'n' || type == 'N') {
|
||||||
|
@ -64,9 +64,9 @@ var bin =
|
||||||
unpacked = this.dec.decode(bytes.slice(0, size)).removeEndNullBytes();
|
unpacked = this.dec.decode(bytes.slice(0, size)).removeEndNullBytes();
|
||||||
}
|
}
|
||||||
return unpacked;
|
return unpacked;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
mergeBuffers : function(bufferArray)
|
ns.mergeBuffers = function(bufferArray)
|
||||||
{
|
{
|
||||||
var maxLength = 0;
|
var maxLength = 0;
|
||||||
for(var i=0, lim=bufferArray.length; i < lim; i++) {
|
for(var i=0, lim=bufferArray.length; i < lim; i++) {
|
||||||
|
@ -82,15 +82,15 @@ var bin =
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tmp;
|
return tmp;
|
||||||
},
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
getBuffer : function(array, size)
|
ns.getBuffer = function(array, size)
|
||||||
{
|
{
|
||||||
var buffer = new Uint8Array(!size ? array.length : size);
|
var buffer = new Uint8Array(!size ? array.length : size);
|
||||||
buffer.set(new Uint8Array(array), 0);
|
buffer.set(new Uint8Array(array), 0);
|
||||||
return buffer;
|
return buffer;
|
||||||
},
|
};
|
||||||
}
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
require('./file')(bin);
|
require('./file')(ns);
|
||||||
module.exports = bin;
|
return ns;
|
||||||
|
}
|
||||||
|
|
28
lib/file.js
28
lib/file.js
|
@ -1,7 +1,11 @@
|
||||||
module.exports = function (bin) {
|
'use strict';
|
||||||
require('./header')(bin);
|
|
||||||
|
module.exports = function (ns) {
|
||||||
|
|
||||||
|
require('./header')(ns);
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.File = function(binary, header) {
|
ns.File = function(binary, header) {
|
||||||
this.header = header;
|
this.header = header;
|
||||||
this.binary = binary;
|
this.binary = binary;
|
||||||
this.extractProperties = function(binary) {
|
this.extractProperties = function(binary) {
|
||||||
|
@ -9,7 +13,7 @@ module.exports = function (bin) {
|
||||||
if (binary != null && binary.length >= this.header.def.length) {
|
if (binary != null && binary.length >= this.header.def.length) {
|
||||||
this.header.def.map.forEach (function(prop) {
|
this.header.def.map.forEach (function(prop) {
|
||||||
var bytes = binary.slice(prop.offset, prop.offset+prop.length);
|
var bytes = binary.slice(prop.offset, prop.offset+prop.length);
|
||||||
var unpacked = bin.unpack(prop.size, prop.type, bytes);
|
var unpacked = ns.unpack(prop.size, prop.type, bytes);
|
||||||
prop.value = unpacked;
|
prop.value = unpacked;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -18,16 +22,16 @@ module.exports = function (bin) {
|
||||||
var data = [];
|
var data = [];
|
||||||
this.header.buildProperties(properties);
|
this.header.buildProperties(properties);
|
||||||
this.header.def.map.forEach (function(prop) {
|
this.header.def.map.forEach (function(prop) {
|
||||||
var packed = bin.pack(prop.size, prop.type, prop.value);
|
var packed = ns.pack(prop.size, prop.type, prop.value);
|
||||||
data.push(packed);
|
data.push(packed);
|
||||||
});
|
});
|
||||||
data = bin.mergeBuffers(data);
|
data = ns.mergeBuffers(data);
|
||||||
};
|
};
|
||||||
this.extractProperties(binary);
|
this.extractProperties(binary);
|
||||||
}
|
}
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.smpFile = function(binary) {
|
ns.smpFile = function(binary) {
|
||||||
return new bin.File(binary, new bin.Header([
|
return new ns.File(binary, new ns.Header([
|
||||||
['signature', 'a', 9, '∢SMP0.2'],
|
['signature', 'a', 9, '∢SMP0.2'],
|
||||||
['size' , 'N', 1, ''],
|
['size' , 'N', 1, ''],
|
||||||
['id' , 'N', 1, ''],
|
['id' , 'N', 1, ''],
|
||||||
|
@ -40,8 +44,8 @@ module.exports = function (bin) {
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.smpsFile = function(binary) {
|
ns.smpsFile = function(binary) {
|
||||||
return new bin.File(binary, new bin.Header([
|
return new ns.File(binary, new ns.Header([
|
||||||
['signature', 'a', 13, 'ﷸSMPS0.2∢'],
|
['signature', 'a', 13, 'ﷸSMPS0.2∢'],
|
||||||
['size' , 'N', 1, ''],
|
['size' , 'N', 1, ''],
|
||||||
['nonce' , 'a', 12, ''],
|
['nonce' , 'a', 12, ''],
|
||||||
|
@ -49,8 +53,8 @@ module.exports = function (bin) {
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.smpaFile = function(binary) {
|
ns.smpaFile = function(binary) {
|
||||||
return new bin.BaseFile(binary, new bin.Header([
|
return new ns.BaseFile(binary, new ns.Header([
|
||||||
['signature', 'a', 13, 'ﷸSMPA0.2∢'],
|
['signature', 'a', 13, 'ﷸSMPA0.2∢'],
|
||||||
['size' , 'N', 1, ''],
|
['size' , 'N', 1, ''],
|
||||||
['nonce' , 'a', 24, ''],
|
['nonce' , 'a', 24, ''],
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
module.exports = function (ns) {
|
||||||
module.exports = function (bin) {
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
var HeaderProperty = function(name, type, size, defaultValue)
|
var HeaderProperty = function(name, type, size, defaultValue)
|
||||||
{
|
{
|
||||||
|
@ -37,7 +37,7 @@ module.exports = function (bin) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
bin.Header = function(def) {
|
ns.Header = function(def) {
|
||||||
this.def = new HeaderDef(def);
|
this.def = new HeaderDef(def);
|
||||||
this.value = function(key) {
|
this.value = function(key) {
|
||||||
return this.def.getProperty(key).value;
|
return this.def.getProperty(key).value;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "@pluie/bin",
|
"name": "pluie-bin",
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"description": "Node.js pluie small binary API for the browser",
|
"description": "Node.js pluie small binary API for the browser",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.pluie.org/pluie/js-bin.git"
|
"url": "https://git.pluie.org/pluie/pluie-bin.git"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user