From 85732a042e63f1a24fbfcf8a462fd1865aa31dfc Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Mon, 19 Dec 2016 00:39:08 +0100 Subject: [PATCH] add dependencies textencoder --- dist/pluie-bin.js | 154 +++++++++++++++++++++++++++++++++++++++--- dist/pluie-bin.min.js | 2 +- lib/bin.js | 8 +-- lib/encoder.js | 140 ++++++++++++++++++++++++++++++++++++++ lib/file.js | 2 - lib/header.js | 2 - 6 files changed, 291 insertions(+), 17 deletions(-) create mode 100644 lib/encoder.js diff --git a/dist/pluie-bin.js b/dist/pluie-bin.js index fe35101..b77e3e9 100644 --- a/dist/pluie-bin.js +++ b/dist/pluie-bin.js @@ -6,9 +6,9 @@ require('./lib/bin')(pluie.bin); module.exports = pluie; },{"./lib/bin":2}],2:[function(require,module,exports){ -'use strict'; - module.exports = function (ns) { + + require('./encoder')(ns); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ String.prototype.removeEndNullBytes = function() { @@ -20,8 +20,8 @@ module.exports = function (ns) { } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - ns.enc = new TextEncoder("utf-8"); - ns.dec = new TextDecoder("utf-8", {fatal: true}); + ns.enc = new ns.TextEncoder("utf-8"); + ns.dec = new ns.TextDecoder("utf-8", {fatal: true}); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ns.intFromBytes = function(bytes) { @@ -103,9 +103,149 @@ module.exports = function (ns) { return ns; } -},{"./file":3}],3:[function(require,module,exports){ +},{"./encoder":3,"./file":4}],3:[function(require,module,exports){ +function TextEncoderLite() { +} +function TextDecoderLite() { +} + +(function () { 'use strict'; +// Taken from https://github.com/feross/buffer/blob/master/index.js +// Thanks Feross et al! :-) + +function utf8ToBytes (string, units) { + units = units || Infinity + var codePoint + var length = string.length + var leadSurrogate = null + var bytes = [] + var i = 0 + + for (; i < length; i++) { + codePoint = string.charCodeAt(i) + + // is surrogate component + if (codePoint > 0xD7FF && codePoint < 0xE000) { + // last char was a lead + if (leadSurrogate) { + // 2 leads in a row + if (codePoint < 0xDC00) { + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + leadSurrogate = codePoint + continue + } else { + // valid surrogate pair + codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000 + leadSurrogate = null + } + } else { + // no lead yet + + if (codePoint > 0xDBFF) { + // unexpected trail + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } else if (i + 1 === length) { + // unpaired lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } else { + // valid lead + leadSurrogate = codePoint + continue + } + } + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + leadSurrogate = null + } + + // encode utf8 + if (codePoint < 0x80) { + if ((units -= 1) < 0) break + bytes.push(codePoint) + } else if (codePoint < 0x800) { + if ((units -= 2) < 0) break + bytes.push( + codePoint >> 0x6 | 0xC0, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x10000) { + if ((units -= 3) < 0) break + bytes.push( + codePoint >> 0xC | 0xE0, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x200000) { + if ((units -= 4) < 0) break + bytes.push( + codePoint >> 0x12 | 0xF0, + codePoint >> 0xC & 0x3F | 0x80, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else { + throw new Error('Invalid code point') + } + } + + return bytes +} + +function utf8Slice (buf, start, end) { + var res = '' + var tmp = '' + end = Math.min(buf.length, end || Infinity) + start = start || 0; + + for (var i = start; i < end; i++) { + if (buf[i] <= 0x7F) { + res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) + tmp = '' + } else { + tmp += '%' + buf[i].toString(16) + } + } + + return res + decodeUtf8Char(tmp) +} + +function decodeUtf8Char (str) { + try { + return decodeURIComponent(str) + } catch (err) { + return String.fromCharCode(0xFFFD) // UTF 8 invalid char + } +} + +TextEncoderLite.prototype.encode = function (str) { + var result; + + if ('undefined' === typeof Uint8Array) { + result = utf8ToBytes(str); + } else { + result = new Uint8Array(utf8ToBytes(str)); + } + + return result; +}; + +TextDecoderLite.prototype.decode = function (bytes) { + return utf8Slice(bytes, 0, bytes.length); +} + +}()); + +module.exports = function(ns) { + ns.TextEncoder = TextEncoderLite; + ns.TextDecoder = TextDecoderLite; +} + +},{}],4:[function(require,module,exports){ module.exports = function (ns) { require('./header')(ns); @@ -169,9 +309,7 @@ module.exports = function (ns) { } } -},{"./header":4}],4:[function(require,module,exports){ -'use strict'; - +},{"./header":5}],5:[function(require,module,exports){ module.exports = function (ns) { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/dist/pluie-bin.min.js b/dist/pluie-bin.min.js index baac164..7031b30 100644 --- a/dist/pluie-bin.min.js +++ b/dist/pluie-bin.min.js @@ -1 +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 +!function e(n,t,r){function i(u,f){if(!t[u]){if(!n[u]){var a="function"==typeof require&&require;if(!f&&a)return a(u,!0);if(o)return o(u,!0);var s=new Error("Cannot find module '"+u+"'");throw s.code="MODULE_NOT_FOUND",s}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 o="function"==typeof require&&require,u=0;u0)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;t55295&&t<57344){if(!i){if(t>56319){(n-=3)>-1&&o.push(239,191,189);continue}if(u+1===r){(n-=3)>-1&&o.push(239,191,189);continue}i=t;continue}if(t<56320){(n-=3)>-1&&o.push(239,191,189),i=t;continue}t=i-55296<<10|t-56320|65536,i=null}else i&&((n-=3)>-1&&o.push(239,191,189),i=null);if(t<128){if((n-=1)<0)break;o.push(t)}else if(t<2048){if((n-=2)<0)break;o.push(t>>6|192,63&t|128)}else if(t<65536){if((n-=3)<0)break;o.push(t>>12|224,t>>6&63|128,63&t|128)}else{if(!(t<2097152))throw new Error("Invalid code point");if((n-=4)<0)break;o.push(t>>18|240,t>>12&63|128,t>>6&63|128,63&t|128)}}return o}function n(e,n,r){var i="",o="";r=Math.min(e.length,r||1/0),n=n||0;for(var u=n;u=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":5}],5:[function(e,n,t){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={},o=0,u=0;this.map.forEach(function(e){e.offset=o,r[r.length]=e.name,o+=e.length,i[e.name]=u++}),this.indexes=i,this.keys=r,this.length=o,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/lib/bin.js b/lib/bin.js index 20e12c4..88ae068 100644 --- a/lib/bin.js +++ b/lib/bin.js @@ -1,6 +1,6 @@ -'use strict'; - module.exports = function (ns) { + + require('./encoder')(ns); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ String.prototype.removeEndNullBytes = function() { @@ -12,8 +12,8 @@ module.exports = function (ns) { } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - ns.enc = new TextEncoder("utf-8"); - ns.dec = new TextDecoder("utf-8", {fatal: true}); + ns.enc = new ns.TextEncoder("utf-8"); + ns.dec = new ns.TextDecoder("utf-8", {fatal: true}); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ns.intFromBytes = function(bytes) { diff --git a/lib/encoder.js b/lib/encoder.js new file mode 100644 index 0000000..48f9626 --- /dev/null +++ b/lib/encoder.js @@ -0,0 +1,140 @@ +function TextEncoderLite() { +} +function TextDecoderLite() { +} + +(function () { +'use strict'; + +// Taken from https://github.com/feross/buffer/blob/master/index.js +// Thanks Feross et al! :-) + +function utf8ToBytes (string, units) { + units = units || Infinity + var codePoint + var length = string.length + var leadSurrogate = null + var bytes = [] + var i = 0 + + for (; i < length; i++) { + codePoint = string.charCodeAt(i) + + // is surrogate component + if (codePoint > 0xD7FF && codePoint < 0xE000) { + // last char was a lead + if (leadSurrogate) { + // 2 leads in a row + if (codePoint < 0xDC00) { + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + leadSurrogate = codePoint + continue + } else { + // valid surrogate pair + codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000 + leadSurrogate = null + } + } else { + // no lead yet + + if (codePoint > 0xDBFF) { + // unexpected trail + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } else if (i + 1 === length) { + // unpaired lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } else { + // valid lead + leadSurrogate = codePoint + continue + } + } + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + leadSurrogate = null + } + + // encode utf8 + if (codePoint < 0x80) { + if ((units -= 1) < 0) break + bytes.push(codePoint) + } else if (codePoint < 0x800) { + if ((units -= 2) < 0) break + bytes.push( + codePoint >> 0x6 | 0xC0, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x10000) { + if ((units -= 3) < 0) break + bytes.push( + codePoint >> 0xC | 0xE0, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x200000) { + if ((units -= 4) < 0) break + bytes.push( + codePoint >> 0x12 | 0xF0, + codePoint >> 0xC & 0x3F | 0x80, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else { + throw new Error('Invalid code point') + } + } + + return bytes +} + +function utf8Slice (buf, start, end) { + var res = '' + var tmp = '' + end = Math.min(buf.length, end || Infinity) + start = start || 0; + + for (var i = start; i < end; i++) { + if (buf[i] <= 0x7F) { + res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) + tmp = '' + } else { + tmp += '%' + buf[i].toString(16) + } + } + + return res + decodeUtf8Char(tmp) +} + +function decodeUtf8Char (str) { + try { + return decodeURIComponent(str) + } catch (err) { + return String.fromCharCode(0xFFFD) // UTF 8 invalid char + } +} + +TextEncoderLite.prototype.encode = function (str) { + var result; + + if ('undefined' === typeof Uint8Array) { + result = utf8ToBytes(str); + } else { + result = new Uint8Array(utf8ToBytes(str)); + } + + return result; +}; + +TextDecoderLite.prototype.decode = function (bytes) { + return utf8Slice(bytes, 0, bytes.length); +} + +}()); + +module.exports = function(ns) { + ns.TextEncoder = TextEncoderLite; + ns.TextDecoder = TextDecoderLite; +} diff --git a/lib/file.js b/lib/file.js index 7e9141b..5c9ee8b 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = function (ns) { require('./header')(ns); diff --git a/lib/header.js b/lib/header.js index a403bc8..ed66767 100644 --- a/lib/header.js +++ b/lib/header.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = function (ns) { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~