From feb24ff4afe1277ccdc1ff0cd4039068150f2b4a Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Sun, 18 Dec 2016 23:50:34 +0100 Subject: [PATCH] refact --- build | 4 +- dist/{pluie.bin.js => pluie-bin.js} | 97 ++++++++++++++++------------- dist/pluie-bin.min.js | 1 + dist/pluie.bin.min.js | 1 - index.js | 7 ++- lib/bin.js | 56 ++++++++--------- lib/file.js | 28 +++++---- lib/header.js | 6 +- package.json | 4 +- 9 files changed, 109 insertions(+), 95 deletions(-) rename dist/{pluie.bin.js => pluie-bin.js} (79%) create mode 100644 dist/pluie-bin.min.js delete mode 100644 dist/pluie.bin.min.js diff --git a/build b/build index 2e967da..af60acc 100755 --- a/build +++ b/build @@ -1,4 +1,4 @@ #!/bin/bash -browserify index.js > dist/pluie.bin.js -uglifyjs dist/pluie.bin.js -c -m -o dist/pluie.bin.min.js +browserify index.js > dist/pluie-bin.js +uglifyjs dist/pluie-bin.js -c -m -o dist/pluie-bin.min.js diff --git a/dist/pluie.bin.js b/dist/pluie-bin.js similarity index 79% rename from dist/pluie.bin.js rename to dist/pluie-bin.js index 4da708f..fe35101 100644 --- a/dist/pluie.bin.js +++ b/dist/pluie-bin.js @@ -1,26 +1,29 @@ (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 0) { @@ -30,9 +33,9 @@ var bin = } } return value; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bytesFromInt : function(value, size) + ns.bytesFromInt = function(value, size) { var bytes = new Uint8Array(size); while (value) { @@ -40,9 +43,9 @@ var bin = value >>= 8; } return bytes; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - pack : function(size, type, value) + ns.pack = function(size, type, value) { var packed = null if (type == 'n' || type == 'N') { @@ -56,9 +59,9 @@ var bin = packed.set(this.enc.encode(value), 0); } return packed; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - unpack : function(size, type, bytes) + ns.unpack = function(size, type, bytes) { var unpacked = null if (type == 'n' || type == 'N') { @@ -69,9 +72,9 @@ var bin = unpacked = this.dec.decode(bytes.slice(0, size)).removeEndNullBytes(); } return unpacked; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - mergeBuffers : function(bufferArray) + ns.mergeBuffers = function(bufferArray) { var maxLength = 0; for(var i=0, lim=bufferArray.length; i < lim; i++) { @@ -87,24 +90,28 @@ var bin = } } return tmp; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - getBuffer : function(array, size) + ns.getBuffer = function(array, size) { var buffer = new Uint8Array(!size ? array.length : size); buffer.set(new Uint8Array(array), 0); return buffer; - }, + }; + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + require('./file')(ns); + return ns; } -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -require('./file')(bin); -module.exports = bin; },{"./file":3}],3:[function(require,module,exports){ -module.exports = function (bin) { - require('./header')(bin); +'use strict'; + +module.exports = function (ns) { + + require('./header')(ns); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.File = function(binary, header) { + ns.File = function(binary, header) { this.header = header; this.binary = binary; this.extractProperties = function(binary) { @@ -112,7 +119,7 @@ module.exports = function (bin) { if (binary != null && binary.length >= this.header.def.length) { this.header.def.map.forEach (function(prop) { 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; }); } @@ -121,16 +128,16 @@ module.exports = function (bin) { var data = []; this.header.buildProperties(properties); 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 = bin.mergeBuffers(data); + data = ns.mergeBuffers(data); }; this.extractProperties(binary); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.smpFile = function(binary) { - return new bin.File(binary, new bin.Header([ + ns.smpFile = function(binary) { + return new ns.File(binary, new ns.Header([ ['signature', 'a', 9, '∢SMP0.2'], ['size' , 'N', 1, ''], ['id' , 'N', 1, ''], @@ -143,8 +150,8 @@ module.exports = function (bin) { ])); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.smpsFile = function(binary) { - return new bin.File(binary, new bin.Header([ + ns.smpsFile = function(binary) { + return new ns.File(binary, new ns.Header([ ['signature', 'a', 13, 'ﷸSMPS0.2∢'], ['size' , 'N', 1, ''], ['nonce' , 'a', 12, ''], @@ -152,8 +159,8 @@ module.exports = function (bin) { ])); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.smpaFile = function(binary) { - return new bin.BaseFile(binary, new bin.Header([ + ns.smpaFile = function(binary) { + return new ns.BaseFile(binary, new ns.Header([ ['signature', 'a', 13, 'ﷸSMPA0.2∢'], ['size' , 'N', 1, ''], ['nonce' , 'a', 24, ''], @@ -165,8 +172,8 @@ module.exports = function (bin) { },{"./header":4}],4:[function(require,module,exports){ 'use strict'; -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -module.exports = function (bin) { +module.exports = function (ns) { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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.value = function(key) { return this.def.getProperty(key).value; diff --git a/dist/pluie-bin.min.js b/dist/pluie-bin.min.js new file mode 100644 index 0000000..baac164 --- /dev/null +++ b/dist/pluie-bin.min.js @@ -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;a0)for(var t=n=0,r=e.length;t>=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=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]); \ No newline at end of file diff --git a/dist/pluie.bin.min.js b/dist/pluie.bin.min.js deleted file mode 100644 index 0c3b7a0..0000000 --- a/dist/pluie.bin.min.js +++ /dev/null @@ -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;u0)for(var t=n=0,r=e.length;t>=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=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]); \ No newline at end of file diff --git a/index.js b/index.js index 0f36466..2f1481d 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,5 @@ -bin = require('./lib/bin'); -module.exports = bin; +if (typeof pluie == 'undefined') { + pluie = { bin : {}}; +} +require('./lib/bin')(pluie.bin); +module.exports = pluie; diff --git a/lib/bin.js b/lib/bin.js index 87f53a9..20e12c4 100644 --- a/lib/bin.js +++ b/lib/bin.js @@ -1,21 +1,21 @@ 'use strict'; -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -String.prototype.removeEndNullBytes = function() { - return this.replace(/\0+$/g, ''); -} -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -String.prototype.removeStartNullBytes = function() { - return this.replace(/^\0+/g, ''); -} +module.exports = function (ns) { -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -var bin = -{ - enc : new TextEncoder("utf-8"), - dec : new TextDecoder("utf-8", {fatal: true}), // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - intFromBytes : function(bytes) + String.prototype.removeEndNullBytes = function() { + return this.replace(/\0+$/g, ''); + } + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + String.prototype.removeStartNullBytes = function() { + return this.replace(/^\0+/g, ''); + } + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ns.enc = new TextEncoder("utf-8"); + ns.dec = new TextDecoder("utf-8", {fatal: true}); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ns.intFromBytes = function(bytes) { var value = null; if (bytes!=null && bytes.length > 0) { @@ -25,9 +25,9 @@ var bin = } } return value; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bytesFromInt : function(value, size) + ns.bytesFromInt = function(value, size) { var bytes = new Uint8Array(size); while (value) { @@ -35,9 +35,9 @@ var bin = value >>= 8; } return bytes; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - pack : function(size, type, value) + ns.pack = function(size, type, value) { var packed = null if (type == 'n' || type == 'N') { @@ -51,9 +51,9 @@ var bin = packed.set(this.enc.encode(value), 0); } return packed; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - unpack : function(size, type, bytes) + ns.unpack = function(size, type, bytes) { var unpacked = null if (type == 'n' || type == 'N') { @@ -64,9 +64,9 @@ var bin = unpacked = this.dec.decode(bytes.slice(0, size)).removeEndNullBytes(); } return unpacked; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - mergeBuffers : function(bufferArray) + ns.mergeBuffers = function(bufferArray) { var maxLength = 0; for(var i=0, lim=bufferArray.length; i < lim; i++) { @@ -82,15 +82,15 @@ var bin = } } return tmp; - }, + }; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - getBuffer : function(array, size) + ns.getBuffer = function(array, size) { var buffer = new Uint8Array(!size ? array.length : size); buffer.set(new Uint8Array(array), 0); return buffer; - }, + }; + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + require('./file')(ns); + return ns; } -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -require('./file')(bin); -module.exports = bin; diff --git a/lib/file.js b/lib/file.js index 6607c4f..7e9141b 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,7 +1,11 @@ -module.exports = function (bin) { - require('./header')(bin); +'use strict'; + +module.exports = function (ns) { + + require('./header')(ns); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.File = function(binary, header) { + ns.File = function(binary, header) { this.header = header; this.binary = binary; this.extractProperties = function(binary) { @@ -9,7 +13,7 @@ module.exports = function (bin) { if (binary != null && binary.length >= this.header.def.length) { this.header.def.map.forEach (function(prop) { 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; }); } @@ -18,16 +22,16 @@ module.exports = function (bin) { var data = []; this.header.buildProperties(properties); 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 = bin.mergeBuffers(data); + data = ns.mergeBuffers(data); }; this.extractProperties(binary); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.smpFile = function(binary) { - return new bin.File(binary, new bin.Header([ + ns.smpFile = function(binary) { + return new ns.File(binary, new ns.Header([ ['signature', 'a', 9, '∢SMP0.2'], ['size' , 'N', 1, ''], ['id' , 'N', 1, ''], @@ -40,8 +44,8 @@ module.exports = function (bin) { ])); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.smpsFile = function(binary) { - return new bin.File(binary, new bin.Header([ + ns.smpsFile = function(binary) { + return new ns.File(binary, new ns.Header([ ['signature', 'a', 13, 'ﷸSMPS0.2∢'], ['size' , 'N', 1, ''], ['nonce' , 'a', 12, ''], @@ -49,8 +53,8 @@ module.exports = function (bin) { ])); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bin.smpaFile = function(binary) { - return new bin.BaseFile(binary, new bin.Header([ + ns.smpaFile = function(binary) { + return new ns.BaseFile(binary, new ns.Header([ ['signature', 'a', 13, 'ﷸSMPA0.2∢'], ['size' , 'N', 1, ''], ['nonce' , 'a', 24, ''], diff --git a/lib/header.js b/lib/header.js index 355b4ae..a403bc8 100644 --- a/lib/header.js +++ b/lib/header.js @@ -1,7 +1,7 @@ 'use strict'; -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -module.exports = function (bin) { +module.exports = function (ns) { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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.value = function(key) { return this.def.getProperty(key).value; diff --git a/package.json b/package.json index 0cb9c72..262babd 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@pluie/bin", + "name": "pluie-bin", "version": "1.0.2", "description": "Node.js pluie small binary API for the browser", "main": "index.js", @@ -11,7 +11,7 @@ "license": "MIT", "repository": { "type": "git", - "url": "https://git.pluie.org/pluie/js-bin.git" + "url": "https://git.pluie.org/pluie/pluie-bin.git" }, "dependencies": { }