diff --git a/dist/osc-browser.js b/dist/osc-browser.js index fd0ff9d..3c853f6 100644 --- a/dist/osc-browser.js +++ b/dist/osc-browser.js @@ -164,30 +164,39 @@ var osc = osc || {}; idx = (idx + 3) & ~0x03; offsetState.idx = idx; - if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line - // Check for Buffer API (Node/Electron) - if (Buffer.from) { - // new Buffer() is now deprecated, so we use Buffer.from if available - return Buffer.from(charCodes).toString("utf-8"); - } else { - return new Buffer(charCodes).toString("utf-8"); - } - } else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line - // Check for TextDecoder API (Browser/WebKit-based) - return new TextDecoder("utf-8").decode(new Int8Array(charCodes)); - } else { - // If no Buffer or TextDecoder, resort to fromCharCode - // This does not properly decode multi-byte Unicode characters. + var decoder = osc.isBufferEnv ? osc.readString.withBuffer : + typeof TextDecoder !== "undefined" ? osc.readString.withTextDecoder : osc.readString.raw; - var str = ""; - var sliceSize = 10000; + return decoder(charCodes); + }; - // Processing the array in chunks so as not to exceed argument - // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 - for (var i = 0; i < charCodes.length; i += sliceSize) { + osc.readString.raw = function (charCodes) { + // If no Buffer or TextDecoder, resort to fromCharCode + // This does not properly decode multi-byte Unicode characters. + var str = ""; + var sliceSize = 10000; + + // Processing the array in chunks so as not to exceed argument + // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 + for (var i = 0; i < charCodes.length; i += sliceSize) { str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize)); - } - return str; + } + + return str; + }; + + osc.readString.withTextDecoder = function (charCodes) { + var data = new Int8Array(charCodes); + return new TextDecoder("utf-8").decode(data); + }; + + osc.readString.withBuffer = function (charCodes) { + // new Buffer() is now deprecated, + // so we use Buffer.from if available + if (Buffer.from) { + return Buffer.from(charCodes).toString("utf-8"); + } else { + return new Buffer(charCodes).toString("utf-8"); } }; diff --git a/dist/osc-browser.min.js b/dist/osc-browser.min.js index f4378b7..da66d3e 100644 --- a/dist/osc-browser.min.js +++ b/dist/osc-browser.min.js @@ -39,10 +39,15 @@ var osc = osc || {}; } r.push(i); } - if (n = n + 3 & -4, t.idx = n, ("undefined" != typeof global ? global : window).hasOwnProperty("Buffer")) return Buffer.from ? Buffer.from(r).toString("utf-8") : new Buffer(r).toString("utf-8"); - if (("undefined" != typeof global ? global : window).hasOwnProperty("TextDecoder")) return new TextDecoder("utf-8").decode(new Int8Array(r)); - for (var s = "", o = 0; o < r.length; o += 1e4) s += String.fromCharCode.apply(null, r.slice(o, o + 1e4)); - return s; + return n = n + 3 & -4, t.idx = n, (osc.isBufferEnv ? osc.readString.withBuffer : "undefined" != typeof TextDecoder ? osc.readString.withTextDecoder : osc.readString.raw)(r); + }, osc.readString.raw = function(e) { + for (var t = "", r = 0; r < e.length; r += 1e4) t += String.fromCharCode.apply(null, e.slice(r, r + 1e4)); + return t; + }, osc.readString.withTextDecoder = function(e) { + var t = new Int8Array(e); + return new TextDecoder("utf-8").decode(t); + }, osc.readString.withBuffer = function(e) { + return Buffer.from ? Buffer.from(e).toString("utf-8") : new Buffer(e).toString("utf-8"); }, osc.writeString = function(e) { for (var t = e + "\0", r = t.length, n = new Uint8Array(r + 3 & -4), i = 0; i < t.length; i++) { var s = t.charCodeAt(i); @@ -409,7 +414,7 @@ var osc = osc || {}; if (e < 0) return c; if (a <= e) return A; } else { - if (e <= -u) return P; + if (e <= -u) return B; if (u <= e + 1) return E; } return e < 0 ? l(-e, t).neg() : p(e % i | 0, e / i | 0, t); @@ -433,7 +438,7 @@ var osc = osc || {}; } return s.unsigned = t, s; } - function y(e, t) { + function v(e, t) { return "number" == typeof e ? l(e, t) : "string" == typeof e ? h(e, t) : p(e.low, e.high, "boolean" == typeof t ? t : e.unsigned); } e.exports = n; @@ -447,8 +452,8 @@ var osc = osc || {}; var s = {}, o = {}; n.fromInt = r, n.fromNumber = l, n.fromBits = p; var f = Math.pow; - n.fromString = h, n.fromValue = y; - var i = 4294967296, a = i * i, u = a / 2, v = r(1 << 24), m = r(0); + n.fromString = h, n.fromValue = v; + var i = 4294967296, a = i * i, u = a / 2, y = r(1 << 24), m = r(0); n.ZERO = m; var c = r(0, !0); n.UZERO = c; @@ -462,18 +467,18 @@ var osc = osc || {}; n.MAX_VALUE = E; var A = p(-1, -1, !0); n.MAX_UNSIGNED_VALUE = A; - var P = p(0, -2147483648, !1); - n.MIN_VALUE = P; - var B = n.prototype; - B.toInt = function() { + var B = p(0, -2147483648, !1); + n.MIN_VALUE = B; + var P = n.prototype; + P.toInt = function() { return this.unsigned ? this.low >>> 0 : this.low; - }, B.toNumber = function() { + }, P.toNumber = function() { return this.unsigned ? (this.high >>> 0) * i + (this.low >>> 0) : this.high * i + (this.low >>> 0); - }, B.toString = function(e) { + }, P.toString = function(e) { if ((e = e || 10) < 2 || 36 < e) throw RangeError("radix"); if (this.isZero()) return "0"; if (this.isNegative()) { - if (this.eq(P)) { + if (this.eq(B)) { var t = l(e), r = this.div(t), n = r.mul(t).sub(this); return r.toString(e) + n.toInt().toString(e); } @@ -485,68 +490,68 @@ var osc = osc || {}; for (;u.length < 6; ) u = "0" + u; o = "" + u + o; } - }, B.getHighBits = function() { + }, P.getHighBits = function() { return this.high; - }, B.getHighBitsUnsigned = function() { + }, P.getHighBitsUnsigned = function() { return this.high >>> 0; - }, B.getLowBits = function() { + }, P.getLowBits = function() { return this.low; - }, B.getLowBitsUnsigned = function() { + }, P.getLowBitsUnsigned = function() { return this.low >>> 0; - }, B.getNumBitsAbs = function() { - if (this.isNegative()) return this.eq(P) ? 64 : this.neg().getNumBitsAbs(); + }, P.getNumBitsAbs = function() { + if (this.isNegative()) return this.eq(B) ? 64 : this.neg().getNumBitsAbs(); for (var e = 0 != this.high ? this.high : this.low, t = 31; 0 < t && 0 == (e & 1 << t); t--) ; return 0 != this.high ? t + 33 : t + 1; - }, B.isZero = function() { + }, P.isZero = function() { return 0 === this.high && 0 === this.low; - }, B.eqz = B.isZero, B.isNegative = function() { + }, P.eqz = P.isZero, P.isNegative = function() { return !this.unsigned && this.high < 0; - }, B.isPositive = function() { + }, P.isPositive = function() { return this.unsigned || 0 <= this.high; - }, B.isOdd = function() { + }, P.isOdd = function() { return 1 == (1 & this.low); - }, B.isEven = function() { + }, P.isEven = function() { return 0 == (1 & this.low); - }, B.equals = function(e) { - return g(e) || (e = y(e)), (this.unsigned === e.unsigned || this.high >>> 31 != 1 || e.high >>> 31 != 1) && this.high === e.high && this.low === e.low; - }, B.eq = B.equals, B.notEquals = function(e) { + }, P.equals = function(e) { + return g(e) || (e = v(e)), (this.unsigned === e.unsigned || this.high >>> 31 != 1 || e.high >>> 31 != 1) && this.high === e.high && this.low === e.low; + }, P.eq = P.equals, P.notEquals = function(e) { return !this.eq(e); - }, B.neq = B.notEquals, B.ne = B.notEquals, B.lessThan = function(e) { + }, P.neq = P.notEquals, P.ne = P.notEquals, P.lessThan = function(e) { return this.comp(e) < 0; - }, B.lt = B.lessThan, B.lessThanOrEqual = function(e) { + }, P.lt = P.lessThan, P.lessThanOrEqual = function(e) { return this.comp(e) <= 0; - }, B.lte = B.lessThanOrEqual, B.le = B.lessThanOrEqual, B.greaterThan = function(e) { + }, P.lte = P.lessThanOrEqual, P.le = P.lessThanOrEqual, P.greaterThan = function(e) { return 0 < this.comp(e); - }, B.gt = B.greaterThan, B.greaterThanOrEqual = function(e) { + }, P.gt = P.greaterThan, P.greaterThanOrEqual = function(e) { return 0 <= this.comp(e); - }, B.gte = B.greaterThanOrEqual, B.ge = B.greaterThanOrEqual, B.compare = function(e) { - if (g(e) || (e = y(e)), this.eq(e)) return 0; + }, P.gte = P.greaterThanOrEqual, P.ge = P.greaterThanOrEqual, P.compare = function(e) { + if (g(e) || (e = v(e)), this.eq(e)) return 0; var t = this.isNegative(), r = e.isNegative(); return t && !r ? -1 : !t && r ? 1 : this.unsigned ? e.high >>> 0 > this.high >>> 0 || e.high === this.high && e.low >>> 0 > this.low >>> 0 ? -1 : 1 : this.sub(e).isNegative() ? -1 : 1; - }, B.comp = B.compare, B.negate = function() { - return !this.unsigned && this.eq(P) ? P : this.not().add(d); - }, B.neg = B.negate, B.add = function(e) { - g(e) || (e = y(e)); + }, P.comp = P.compare, P.negate = function() { + return !this.unsigned && this.eq(B) ? B : this.not().add(d); + }, P.neg = P.negate, P.add = function(e) { + g(e) || (e = v(e)); var t = this.high >>> 16, r = 65535 & this.high, n = this.low >>> 16, i = 65535 & this.low, s = e.high >>> 16, o = 65535 & e.high, a = e.low >>> 16, u = 0, c = 0, h = 0, f = 0; return h += (f += i + (65535 & e.low)) >>> 16, c += (h += n + a) >>> 16, u += (c += r + o) >>> 16, u += t + s, p((h &= 65535) << 16 | (f &= 65535), (u &= 65535) << 16 | (c &= 65535), this.unsigned); - }, B.subtract = function(e) { - return g(e) || (e = y(e)), this.add(e.neg()); - }, B.sub = B.subtract, B.multiply = function(e) { + }, P.subtract = function(e) { + return g(e) || (e = v(e)), this.add(e.neg()); + }, P.sub = P.subtract, P.multiply = function(e) { if (this.isZero()) return m; - if (g(e) || (e = y(e)), w) return p(w.mul(this.low, this.high, e.low, e.high), w.get_high(), this.unsigned); + if (g(e) || (e = v(e)), w) return p(w.mul(this.low, this.high, e.low, e.high), w.get_high(), this.unsigned); if (e.isZero()) return m; - if (this.eq(P)) return e.isOdd() ? P : m; - if (e.eq(P)) return this.isOdd() ? P : m; + if (this.eq(B)) return e.isOdd() ? B : m; + if (e.eq(B)) return this.isOdd() ? B : m; if (this.isNegative()) return e.isNegative() ? this.neg().mul(e.neg()) : this.neg().mul(e).neg(); if (e.isNegative()) return this.mul(e.neg()).neg(); - if (this.lt(v) && e.lt(v)) return l(this.toNumber() * e.toNumber(), this.unsigned); + if (this.lt(y) && e.lt(y)) return l(this.toNumber() * e.toNumber(), this.unsigned); var t = this.high >>> 16, r = 65535 & this.high, n = this.low >>> 16, i = 65535 & this.low, s = e.high >>> 16, o = 65535 & e.high, a = e.low >>> 16, u = 65535 & e.low, c = 0, h = 0, f = 0, d = 0; return f += (d += i * u) >>> 16, h += (f += n * u) >>> 16, f &= 65535, h += (f += i * a) >>> 16, c += (h += r * u) >>> 16, h &= 65535, c += (h += n * a) >>> 16, h &= 65535, c += (h += i * o) >>> 16, c += t * u + r * a + n * o + i * s, p((f &= 65535) << 16 | (d &= 65535), (c &= 65535) << 16 | (h &= 65535), this.unsigned); - }, B.mul = B.multiply, B.divide = function(e) { - if (g(e) || (e = y(e)), e.isZero()) throw Error("division by zero"); + }, P.mul = P.multiply, P.divide = function(e) { + if (g(e) || (e = v(e)), e.isZero()) throw Error("division by zero"); if (w) return this.unsigned || -2147483648 !== this.high || -1 !== e.low || -1 !== e.high ? p((this.unsigned ? w.div_u : w.div_s)(this.low, this.high, e.low, e.high), w.get_high(), this.unsigned) : this; if (this.isZero()) return this.unsigned ? c : m; var t, r, n; @@ -555,9 +560,9 @@ var osc = osc || {}; if (e.gt(this.shru(1))) return b; n = c; } else { - if (this.eq(P)) return e.eq(d) || e.eq(S) ? P : e.eq(P) ? d : (t = this.shr(1).div(e).shl(1)).eq(m) ? e.isNegative() ? d : S : (r = this.sub(e.mul(t)), + if (this.eq(B)) return e.eq(d) || e.eq(S) ? B : e.eq(B) ? d : (t = this.shr(1).div(e).shl(1)).eq(m) ? e.isNegative() ? d : S : (r = this.sub(e.mul(t)), n = t.add(r.div(e))); - if (e.eq(P)) return this.unsigned ? c : m; + if (e.eq(B)) return this.unsigned ? c : m; if (this.isNegative()) return e.isNegative() ? this.neg().div(e.neg()) : this.neg().div(e).neg(); if (e.isNegative()) return this.div(e.neg()).neg(); n = m; @@ -568,34 +573,34 @@ var osc = osc || {}; o.isZero() && (o = d), n = n.add(o), r = r.sub(a); } return n; - }, B.div = B.divide, B.modulo = function(e) { - return g(e) || (e = y(e)), w ? p((this.unsigned ? w.rem_u : w.rem_s)(this.low, this.high, e.low, e.high), w.get_high(), this.unsigned) : this.sub(this.div(e).mul(e)); - }, B.mod = B.modulo, B.rem = B.modulo, B.not = function() { + }, P.div = P.divide, P.modulo = function(e) { + return g(e) || (e = v(e)), w ? p((this.unsigned ? w.rem_u : w.rem_s)(this.low, this.high, e.low, e.high), w.get_high(), this.unsigned) : this.sub(this.div(e).mul(e)); + }, P.mod = P.modulo, P.rem = P.modulo, P.not = function() { return p(~this.low, ~this.high, this.unsigned); - }, B.and = function(e) { - return g(e) || (e = y(e)), p(this.low & e.low, this.high & e.high, this.unsigned); - }, B.or = function(e) { - return g(e) || (e = y(e)), p(this.low | e.low, this.high | e.high, this.unsigned); - }, B.xor = function(e) { - return g(e) || (e = y(e)), p(this.low ^ e.low, this.high ^ e.high, this.unsigned); - }, B.shiftLeft = function(e) { + }, P.and = function(e) { + return g(e) || (e = v(e)), p(this.low & e.low, this.high & e.high, this.unsigned); + }, P.or = function(e) { + return g(e) || (e = v(e)), p(this.low | e.low, this.high | e.high, this.unsigned); + }, P.xor = function(e) { + return g(e) || (e = v(e)), p(this.low ^ e.low, this.high ^ e.high, this.unsigned); + }, P.shiftLeft = function(e) { return g(e) && (e = e.toInt()), 0 == (e &= 63) ? this : e < 32 ? p(this.low << e, this.high << e | this.low >>> 32 - e, this.unsigned) : p(0, this.low << e - 32, this.unsigned); - }, B.shl = B.shiftLeft, B.shiftRight = function(e) { + }, P.shl = P.shiftLeft, P.shiftRight = function(e) { return g(e) && (e = e.toInt()), 0 == (e &= 63) ? this : e < 32 ? p(this.low >>> e | this.high << 32 - e, this.high >> e, this.unsigned) : p(this.high >> e - 32, 0 <= this.high ? 0 : -1, this.unsigned); - }, B.shr = B.shiftRight, B.shiftRightUnsigned = function(e) { + }, P.shr = P.shiftRight, P.shiftRightUnsigned = function(e) { if (g(e) && (e = e.toInt()), 0 == (e &= 63)) return this; var t = this.high; return e < 32 ? p(this.low >>> e | t << 32 - e, t >>> e, this.unsigned) : p(32 === e ? t : t >>> e - 32, 0, this.unsigned); - }, B.shru = B.shiftRightUnsigned, B.shr_u = B.shiftRightUnsigned, B.toSigned = function() { + }, P.shru = P.shiftRightUnsigned, P.shr_u = P.shiftRightUnsigned, P.toSigned = function() { return this.unsigned ? p(this.low, this.high, !1) : this; - }, B.toUnsigned = function() { + }, P.toUnsigned = function() { return this.unsigned ? this : p(this.low, this.high, !0); - }, B.toBytes = function(e) { + }, P.toBytes = function(e) { return e ? this.toBytesLE() : this.toBytesBE(); - }, B.toBytesLE = function() { + }, P.toBytesLE = function() { var e = this.high, t = this.low; return [ 255 & t, t >>> 8 & 255, t >>> 16 & 255, t >>> 24, 255 & e, e >>> 8 & 255, e >>> 16 & 255, e >>> 24 ]; - }, B.toBytesBE = function() { + }, P.toBytesBE = function() { var e = this.high, t = this.low; return [ e >>> 24, e >>> 16 & 255, e >>> 8 & 255, 255 & e, t >>> 24, t >>> 16 & 255, t >>> 8 & 255, 255 & t ]; }, n.fromBytes = function(e, t, r) { diff --git a/dist/osc-chromeapp.js b/dist/osc-chromeapp.js index 246897f..d5d7d77 100644 --- a/dist/osc-chromeapp.js +++ b/dist/osc-chromeapp.js @@ -164,30 +164,39 @@ var osc = osc || {}; idx = (idx + 3) & ~0x03; offsetState.idx = idx; - if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line - // Check for Buffer API (Node/Electron) - if (Buffer.from) { - // new Buffer() is now deprecated, so we use Buffer.from if available - return Buffer.from(charCodes).toString("utf-8"); - } else { - return new Buffer(charCodes).toString("utf-8"); - } - } else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line - // Check for TextDecoder API (Browser/WebKit-based) - return new TextDecoder("utf-8").decode(new Int8Array(charCodes)); - } else { - // If no Buffer or TextDecoder, resort to fromCharCode - // This does not properly decode multi-byte Unicode characters. + var decoder = osc.isBufferEnv ? osc.readString.withBuffer : + typeof TextDecoder !== "undefined" ? osc.readString.withTextDecoder : osc.readString.raw; - var str = ""; - var sliceSize = 10000; + return decoder(charCodes); + }; - // Processing the array in chunks so as not to exceed argument - // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 - for (var i = 0; i < charCodes.length; i += sliceSize) { + osc.readString.raw = function (charCodes) { + // If no Buffer or TextDecoder, resort to fromCharCode + // This does not properly decode multi-byte Unicode characters. + var str = ""; + var sliceSize = 10000; + + // Processing the array in chunks so as not to exceed argument + // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 + for (var i = 0; i < charCodes.length; i += sliceSize) { str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize)); - } - return str; + } + + return str; + }; + + osc.readString.withTextDecoder = function (charCodes) { + var data = new Int8Array(charCodes); + return new TextDecoder("utf-8").decode(data); + }; + + osc.readString.withBuffer = function (charCodes) { + // new Buffer() is now deprecated, + // so we use Buffer.from if available + if (Buffer.from) { + return Buffer.from(charCodes).toString("utf-8"); + } else { + return new Buffer(charCodes).toString("utf-8"); } }; diff --git a/dist/osc-chromeapp.min.js b/dist/osc-chromeapp.min.js index dfa3c5a..cf167fc 100644 --- a/dist/osc-chromeapp.min.js +++ b/dist/osc-chromeapp.min.js @@ -39,10 +39,15 @@ var osc = osc || {}; } r.push(i); } - if (n = n + 3 & -4, t.idx = n, ("undefined" != typeof global ? global : window).hasOwnProperty("Buffer")) return Buffer.from ? Buffer.from(r).toString("utf-8") : new Buffer(r).toString("utf-8"); - if (("undefined" != typeof global ? global : window).hasOwnProperty("TextDecoder")) return new TextDecoder("utf-8").decode(new Int8Array(r)); - for (var s = "", o = 0; o < r.length; o += 1e4) s += String.fromCharCode.apply(null, r.slice(o, o + 1e4)); - return s; + return n = n + 3 & -4, t.idx = n, (osc.isBufferEnv ? osc.readString.withBuffer : "undefined" != typeof TextDecoder ? osc.readString.withTextDecoder : osc.readString.raw)(r); + }, osc.readString.raw = function(e) { + for (var t = "", r = 0; r < e.length; r += 1e4) t += String.fromCharCode.apply(null, e.slice(r, r + 1e4)); + return t; + }, osc.readString.withTextDecoder = function(e) { + var t = new Int8Array(e); + return new TextDecoder("utf-8").decode(t); + }, osc.readString.withBuffer = function(e) { + return Buffer.from ? Buffer.from(e).toString("utf-8") : new Buffer(e).toString("utf-8"); }, osc.writeString = function(e) { for (var t = e + "\0", r = t.length, n = new Uint8Array(r + 3 & -4), i = 0; i < t.length; i++) { var s = t.charCodeAt(i); diff --git a/dist/osc-module.js b/dist/osc-module.js index 6617938..b07d1a4 100644 --- a/dist/osc-module.js +++ b/dist/osc-module.js @@ -182,30 +182,39 @@ var osc = osc || {}; idx = (idx + 3) & ~0x03; offsetState.idx = idx; - if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line - // Check for Buffer API (Node/Electron) - if (Buffer.from) { - // new Buffer() is now deprecated, so we use Buffer.from if available - return Buffer.from(charCodes).toString("utf-8"); - } else { - return new Buffer(charCodes).toString("utf-8"); - } - } else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line - // Check for TextDecoder API (Browser/WebKit-based) - return new TextDecoder("utf-8").decode(new Int8Array(charCodes)); - } else { - // If no Buffer or TextDecoder, resort to fromCharCode - // This does not properly decode multi-byte Unicode characters. + var decoder = osc.isBufferEnv ? osc.readString.withBuffer : + typeof TextDecoder !== "undefined" ? osc.readString.withTextDecoder : osc.readString.raw; - var str = ""; - var sliceSize = 10000; + return decoder(charCodes); + }; - // Processing the array in chunks so as not to exceed argument - // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 - for (var i = 0; i < charCodes.length; i += sliceSize) { + osc.readString.raw = function (charCodes) { + // If no Buffer or TextDecoder, resort to fromCharCode + // This does not properly decode multi-byte Unicode characters. + var str = ""; + var sliceSize = 10000; + + // Processing the array in chunks so as not to exceed argument + // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 + for (var i = 0; i < charCodes.length; i += sliceSize) { str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize)); - } - return str; + } + + return str; + }; + + osc.readString.withTextDecoder = function (charCodes) { + var data = new Int8Array(charCodes); + return new TextDecoder("utf-8").decode(data); + }; + + osc.readString.withBuffer = function (charCodes) { + // new Buffer() is now deprecated, + // so we use Buffer.from if available + if (Buffer.from) { + return Buffer.from(charCodes).toString("utf-8"); + } else { + return new Buffer(charCodes).toString("utf-8"); } }; diff --git a/dist/osc-module.min.js b/dist/osc-module.min.js index e548840..7a5ebd3 100644 --- a/dist/osc-module.min.js +++ b/dist/osc-module.min.js @@ -42,10 +42,15 @@ } r.push(i); } - if (n = n + 3 & -4, t.idx = n, ("undefined" != typeof global ? global : window).hasOwnProperty("Buffer")) return Buffer.from ? Buffer.from(r).toString("utf-8") : new Buffer(r).toString("utf-8"); - if (("undefined" != typeof global ? global : window).hasOwnProperty("TextDecoder")) return new TextDecoder("utf-8").decode(new Int8Array(r)); - for (var a = "", o = 0; o < r.length; o += 1e4) a += String.fromCharCode.apply(null, r.slice(o, o + 1e4)); - return a; + return n = n + 3 & -4, t.idx = n, (l.isBufferEnv ? l.readString.withBuffer : "undefined" != typeof TextDecoder ? l.readString.withTextDecoder : l.readString.raw)(r); + }, l.readString.raw = function(e) { + for (var t = "", r = 0; r < e.length; r += 1e4) t += String.fromCharCode.apply(null, e.slice(r, r + 1e4)); + return t; + }, l.readString.withTextDecoder = function(e) { + var t = new Int8Array(e); + return new TextDecoder("utf-8").decode(t); + }, l.readString.withBuffer = function(e) { + return Buffer.from ? Buffer.from(e).toString("utf-8") : new Buffer(e).toString("utf-8"); }, l.writeString = function(e) { for (var t = e + "\0", r = t.length, n = new Uint8Array(r + 3 & -4), i = 0; i < t.length; i++) { var a = t.charCodeAt(i); diff --git a/dist/osc.js b/dist/osc.js index 3811e8d..8227967 100644 --- a/dist/osc.js +++ b/dist/osc.js @@ -164,30 +164,39 @@ var osc = osc || {}; idx = (idx + 3) & ~0x03; offsetState.idx = idx; - if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line - // Check for Buffer API (Node/Electron) - if (Buffer.from) { - // new Buffer() is now deprecated, so we use Buffer.from if available - return Buffer.from(charCodes).toString("utf-8"); - } else { - return new Buffer(charCodes).toString("utf-8"); - } - } else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line - // Check for TextDecoder API (Browser/WebKit-based) - return new TextDecoder("utf-8").decode(new Int8Array(charCodes)); - } else { - // If no Buffer or TextDecoder, resort to fromCharCode - // This does not properly decode multi-byte Unicode characters. + var decoder = osc.isBufferEnv ? osc.readString.withBuffer : + typeof TextDecoder !== "undefined" ? osc.readString.withTextDecoder : osc.readString.raw; - var str = ""; - var sliceSize = 10000; + return decoder(charCodes); + }; - // Processing the array in chunks so as not to exceed argument - // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 - for (var i = 0; i < charCodes.length; i += sliceSize) { + osc.readString.raw = function (charCodes) { + // If no Buffer or TextDecoder, resort to fromCharCode + // This does not properly decode multi-byte Unicode characters. + var str = ""; + var sliceSize = 10000; + + // Processing the array in chunks so as not to exceed argument + // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 + for (var i = 0; i < charCodes.length; i += sliceSize) { str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize)); - } - return str; + } + + return str; + }; + + osc.readString.withTextDecoder = function (charCodes) { + var data = new Int8Array(charCodes); + return new TextDecoder("utf-8").decode(data); + }; + + osc.readString.withBuffer = function (charCodes) { + // new Buffer() is now deprecated, + // so we use Buffer.from if available + if (Buffer.from) { + return Buffer.from(charCodes).toString("utf-8"); + } else { + return new Buffer(charCodes).toString("utf-8"); } }; diff --git a/dist/osc.min.js b/dist/osc.min.js index 480ae00..7e383b7 100644 --- a/dist/osc.min.js +++ b/dist/osc.min.js @@ -39,10 +39,15 @@ var osc = osc || {}; } t.push(a); } - if (n = n + 3 & -4, e.idx = n, ("undefined" != typeof global ? global : window).hasOwnProperty("Buffer")) return Buffer.from ? Buffer.from(t).toString("utf-8") : new Buffer(t).toString("utf-8"); - if (("undefined" != typeof global ? global : window).hasOwnProperty("TextDecoder")) return new TextDecoder("utf-8").decode(new Int8Array(t)); - for (var o = "", i = 0; i < t.length; i += 1e4) o += String.fromCharCode.apply(null, t.slice(i, i + 1e4)); - return o; + return n = n + 3 & -4, e.idx = n, (osc.isBufferEnv ? osc.readString.withBuffer : "undefined" != typeof TextDecoder ? osc.readString.withTextDecoder : osc.readString.raw)(t); + }, osc.readString.raw = function(r) { + for (var e = "", t = 0; t < r.length; t += 1e4) e += String.fromCharCode.apply(null, r.slice(t, t + 1e4)); + return e; + }, osc.readString.withTextDecoder = function(r) { + var e = new Int8Array(r); + return new TextDecoder("utf-8").decode(e); + }, osc.readString.withBuffer = function(r) { + return Buffer.from ? Buffer.from(r).toString("utf-8") : new Buffer(r).toString("utf-8"); }, osc.writeString = function(r) { for (var e = r + "\0", t = e.length, n = new Uint8Array(t + 3 & -4), a = 0; a < e.length; a++) { var o = e.charCodeAt(a); diff --git a/src/osc.js b/src/osc.js index 50491b6..3bfddc1 100644 --- a/src/osc.js +++ b/src/osc.js @@ -162,30 +162,39 @@ var osc = osc || {}; idx = (idx + 3) & ~0x03; offsetState.idx = idx; - if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line - // Check for Buffer API (Node/Electron) - if (Buffer.from) { - // new Buffer() is now deprecated, so we use Buffer.from if available - return Buffer.from(charCodes).toString("utf-8"); - } else { - return new Buffer(charCodes).toString("utf-8"); - } - } else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line - // Check for TextDecoder API (Browser/WebKit-based) - return new TextDecoder("utf-8").decode(new Int8Array(charCodes)); - } else { - // If no Buffer or TextDecoder, resort to fromCharCode - // This does not properly decode multi-byte Unicode characters. + var decoder = osc.isBufferEnv ? osc.readString.withBuffer : + typeof TextDecoder !== "undefined" ? osc.readString.withTextDecoder : osc.readString.raw; - var str = ""; - var sliceSize = 10000; + return decoder(charCodes); + }; - // Processing the array in chunks so as not to exceed argument - // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 - for (var i = 0; i < charCodes.length; i += sliceSize) { + osc.readString.raw = function (charCodes) { + // If no Buffer or TextDecoder, resort to fromCharCode + // This does not properly decode multi-byte Unicode characters. + var str = ""; + var sliceSize = 10000; + + // Processing the array in chunks so as not to exceed argument + // limit, see https://bugs.webkit.org/show_bug.cgi?id=80797 + for (var i = 0; i < charCodes.length; i += sliceSize) { str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize)); - } - return str; + } + + return str; + }; + + osc.readString.withTextDecoder = function (charCodes) { + var data = new Int8Array(charCodes); + return new TextDecoder("utf-8").decode(data); + }; + + osc.readString.withBuffer = function (charCodes) { + // new Buffer() is now deprecated, + // so we use Buffer.from if available + if (Buffer.from) { + return Buffer.from(charCodes).toString("utf-8"); + } else { + return new Buffer(charCodes).toString("utf-8"); } };