Skip to content

Commit fd6ad77

Browse files
committed
Merge branch 'gh-92'
* gh-92: Updates pre-built files. Removes use of Buffer constructor now that Node 10 is LTS. Uses TextDecoder in Node.js when available. gh-92: Refactors for modularity and simpler environment detection. Updating dist files Updating dist files Adding tests Changing checks for Buffer/TextDecoder Fixing style Changing osc.readString to use Buffer.toString or TextDecoder if available
2 parents 388e526 + 0e699f9 commit fd6ad77

File tree

10 files changed

+864
-656
lines changed

10 files changed

+864
-656
lines changed

dist/osc-browser.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Licensed under the MIT and GPL 3 licenses.
88
*/
99

10-
/* global require, module, process, Buffer, Long */
10+
/* global require, module, process, Buffer, Long, util */
1111

1212
var osc = osc || {};
1313

@@ -55,6 +55,11 @@ var osc = osc || {};
5555
osc.Long = typeof Long !== "undefined" ? Long :
5656
osc.isNode ? require("long") : undefined;
5757

58+
// Unsupported, non-API member. Can be removed when supported versions
59+
// of Node.js expose TextDecoder as a global, as in the browser.
60+
osc.TextDecoder = typeof TextDecoder !== "undefined" ? TextDecoder :
61+
typeof util !== "undefined" && typeof (util.TextDecoder !== "undefined") ? util.TextDecoder : undefined;
62+
5863
/**
5964
* Wraps the specified object in a DataView.
6065
*
@@ -164,7 +169,34 @@ var osc = osc || {};
164169
idx = (idx + 3) & ~0x03;
165170
offsetState.idx = idx;
166171

167-
return String.fromCharCode.apply(null, charCodes);
172+
var decoder = osc.isBufferEnv ? osc.readString.withBuffer :
173+
osc.TextDecoder ? osc.readString.withTextDecoder : osc.readString.raw;
174+
175+
return decoder(charCodes);
176+
};
177+
178+
osc.readString.raw = function (charCodes) {
179+
// If no Buffer or TextDecoder, resort to fromCharCode
180+
// This does not properly decode multi-byte Unicode characters.
181+
var str = "";
182+
var sliceSize = 10000;
183+
184+
// Processing the array in chunks so as not to exceed argument
185+
// limit, see https://bugs.webkit.org/show_bug.cgi?id=80797
186+
for (var i = 0; i < charCodes.length; i += sliceSize) {
187+
str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
188+
}
189+
190+
return str;
191+
};
192+
193+
osc.readString.withTextDecoder = function (charCodes) {
194+
var data = new Int8Array(charCodes);
195+
return new osc.TextDecoder("utf-8").decode(data);
196+
};
197+
198+
osc.readString.withBuffer = function (charCodes) {
199+
return Buffer.from(charCodes).toString("utf-8");
168200
};
169201

170202
/**

dist/osc-browser.min.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ var osc = osc || {};
1717
}, osc.isBuffer = function(e) {
1818
return osc.isBufferEnv && e instanceof Buffer;
1919
}, osc.Long = "undefined" != typeof Long ? Long : osc.isNode ? require("long") : void 0,
20-
osc.dataView = function(e, t, r) {
20+
osc.TextDecoder = "undefined" != typeof TextDecoder ? TextDecoder : "undefined" != typeof util && (util.TextDecoder,
21+
1) ? util.TextDecoder : void 0, osc.dataView = function(e, t, r) {
2122
return e.buffer ? new DataView(e.buffer, t, r) : e instanceof ArrayBuffer ? new DataView(e, t, r) : new DataView(new Uint8Array(e), t, r);
2223
}, osc.byteArray = function(e) {
2324
if (e instanceof Uint8Array) return e;
@@ -39,7 +40,15 @@ var osc = osc || {};
3940
}
4041
r.push(i);
4142
}
42-
return n = n + 3 & -4, t.idx = n, String.fromCharCode.apply(null, r);
43+
return n = n + 3 & -4, t.idx = n, (osc.isBufferEnv ? osc.readString.withBuffer : osc.TextDecoder ? osc.readString.withTextDecoder : osc.readString.raw)(r);
44+
}, osc.readString.raw = function(e) {
45+
for (var t = "", r = 0; r < e.length; r += 1e4) t += String.fromCharCode.apply(null, e.slice(r, r + 1e4));
46+
return t;
47+
}, osc.readString.withTextDecoder = function(e) {
48+
var t = new Int8Array(e);
49+
return new osc.TextDecoder("utf-8").decode(t);
50+
}, osc.readString.withBuffer = function(e) {
51+
return Buffer.from(e).toString("utf-8");
4352
}, osc.writeString = function(e) {
4453
for (var t = e + "\0", r = t.length, n = new Uint8Array(r + 3 & -4), i = 0; i < t.length; i++) {
4554
var s = t.charCodeAt(i);
@@ -380,7 +389,7 @@ var osc = osc || {};
380389
if (a <= e) return A;
381390
} else {
382391
if (e <= -u) return P;
383-
if (u <= e + 1) return S;
392+
if (u <= e + 1) return E;
384393
}
385394
return e < 0 ? l(-e, t).neg() : p(e % i | 0, e / i | 0, t);
386395
}
@@ -403,21 +412,21 @@ var osc = osc || {};
403412
}
404413
return s.unsigned = t, s;
405414
}
406-
function y(e, t) {
415+
function v(e, t) {
407416
return "number" == typeof e ? l(e, t) : "string" == typeof e ? h(e, t) : p(e.low, e.high, "boolean" == typeof t ? t : e.unsigned);
408417
}
409418
e.exports = n;
410-
var v = null;
419+
var y = null;
411420
try {
412-
v = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([ 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11 ])), {}).exports;
421+
y = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([ 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11 ])), {}).exports;
413422
} catch (e) {}
414423
Object.defineProperty(n.prototype, "__isLong__", {
415424
value: !0
416425
}), n.isLong = g;
417426
var s = {}, o = {};
418427
n.fromInt = r, n.fromNumber = l, n.fromBits = p;
419428
var f = Math.pow;
420-
n.fromString = h, n.fromValue = y;
429+
n.fromString = h, n.fromValue = v;
421430
var i = 4294967296, a = i * i, u = a / 2, w = r(1 << 24), m = r(0);
422431
n.ZERO = m;
423432
var c = r(0, !0);
@@ -426,10 +435,10 @@ var osc = osc || {};
426435
n.ONE = d;
427436
var b = r(1, !0);
428437
n.UONE = b;
429-
var E = r(-1);
430-
n.NEG_ONE = E;
431-
var S = p(-1, 2147483647, !1);
432-
n.MAX_VALUE = S;
438+
var S = r(-1);
439+
n.NEG_ONE = S;
440+
var E = p(-1, 2147483647, !1);
441+
n.MAX_VALUE = E;
433442
var A = p(-1, -1, !0);
434443
n.MAX_UNSIGNED_VALUE = A;
435444
var P = p(0, -2147483648, !1);
@@ -478,7 +487,7 @@ var osc = osc || {};
478487
}, B.isEven = function() {
479488
return 0 == (1 & this.low);
480489
}, B.equals = function(e) {
481-
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;
490+
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;
482491
}, B.eq = B.equals, B.notEquals = function(e) {
483492
return !this.eq(e);
484493
}, B.neq = B.notEquals, B.ne = B.notEquals, B.lessThan = function(e) {
@@ -490,21 +499,21 @@ var osc = osc || {};
490499
}, B.gt = B.greaterThan, B.greaterThanOrEqual = function(e) {
491500
return 0 <= this.comp(e);
492501
}, B.gte = B.greaterThanOrEqual, B.ge = B.greaterThanOrEqual, B.compare = function(e) {
493-
if (g(e) || (e = y(e)), this.eq(e)) return 0;
502+
if (g(e) || (e = v(e)), this.eq(e)) return 0;
494503
var t = this.isNegative(), r = e.isNegative();
495504
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;
496505
}, B.comp = B.compare, B.negate = function() {
497506
return !this.unsigned && this.eq(P) ? P : this.not().add(d);
498507
}, B.neg = B.negate, B.add = function(e) {
499-
g(e) || (e = y(e));
508+
g(e) || (e = v(e));
500509
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;
501510
return h += (f += i + (65535 & e.low)) >>> 16, c += (h += n + a) >>> 16, u += (c += r + o) >>> 16,
502511
u += t + s, p((h &= 65535) << 16 | (f &= 65535), (u &= 65535) << 16 | (c &= 65535), this.unsigned);
503512
}, B.subtract = function(e) {
504-
return g(e) || (e = y(e)), this.add(e.neg());
513+
return g(e) || (e = v(e)), this.add(e.neg());
505514
}, B.sub = B.subtract, B.multiply = function(e) {
506515
if (this.isZero()) return m;
507-
if (g(e) || (e = y(e)), v) return p(v.mul(this.low, this.high, e.low, e.high), v.get_high(), this.unsigned);
516+
if (g(e) || (e = v(e)), y) return p(y.mul(this.low, this.high, e.low, e.high), y.get_high(), this.unsigned);
508517
if (e.isZero()) return m;
509518
if (this.eq(P)) return e.isOdd() ? P : m;
510519
if (e.eq(P)) return this.isOdd() ? P : m;
@@ -516,16 +525,16 @@ var osc = osc || {};
516525
c += (h += r * u) >>> 16, h &= 65535, c += (h += n * a) >>> 16, h &= 65535, c += (h += i * o) >>> 16,
517526
c += t * u + r * a + n * o + i * s, p((f &= 65535) << 16 | (d &= 65535), (c &= 65535) << 16 | (h &= 65535), this.unsigned);
518527
}, B.mul = B.multiply, B.divide = function(e) {
519-
if (g(e) || (e = y(e)), e.isZero()) throw Error("division by zero");
520-
if (v) return this.unsigned || -2147483648 !== this.high || -1 !== e.low || -1 !== e.high ? p((this.unsigned ? v.div_u : v.div_s)(this.low, this.high, e.low, e.high), v.get_high(), this.unsigned) : this;
528+
if (g(e) || (e = v(e)), e.isZero()) throw Error("division by zero");
529+
if (y) return this.unsigned || -2147483648 !== this.high || -1 !== e.low || -1 !== e.high ? p((this.unsigned ? y.div_u : y.div_s)(this.low, this.high, e.low, e.high), y.get_high(), this.unsigned) : this;
521530
if (this.isZero()) return this.unsigned ? c : m;
522531
var t, r, n;
523532
if (this.unsigned) {
524533
if (e.unsigned || (e = e.toUnsigned()), e.gt(this)) return c;
525534
if (e.gt(this.shru(1))) return b;
526535
n = c;
527536
} else {
528-
if (this.eq(P)) return e.eq(d) || e.eq(E) ? P : e.eq(P) ? d : (t = this.shr(1).div(e).shl(1)).eq(m) ? e.isNegative() ? d : E : (r = this.sub(e.mul(t)),
537+
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)),
529538
n = t.add(r.div(e)));
530539
if (e.eq(P)) return this.unsigned ? c : m;
531540
if (this.isNegative()) return e.isNegative() ? this.neg().div(e.neg()) : this.neg().div(e).neg();
@@ -539,15 +548,15 @@ var osc = osc || {};
539548
}
540549
return n;
541550
}, B.div = B.divide, B.modulo = function(e) {
542-
return g(e) || (e = y(e)), v ? p((this.unsigned ? v.rem_u : v.rem_s)(this.low, this.high, e.low, e.high), v.get_high(), this.unsigned) : this.sub(this.div(e).mul(e));
551+
return g(e) || (e = v(e)), y ? p((this.unsigned ? y.rem_u : y.rem_s)(this.low, this.high, e.low, e.high), y.get_high(), this.unsigned) : this.sub(this.div(e).mul(e));
543552
}, B.mod = B.modulo, B.rem = B.modulo, B.not = function() {
544553
return p(~this.low, ~this.high, this.unsigned);
545554
}, B.and = function(e) {
546-
return g(e) || (e = y(e)), p(this.low & e.low, this.high & e.high, this.unsigned);
555+
return g(e) || (e = v(e)), p(this.low & e.low, this.high & e.high, this.unsigned);
547556
}, B.or = function(e) {
548-
return g(e) || (e = y(e)), p(this.low | e.low, this.high | e.high, this.unsigned);
557+
return g(e) || (e = v(e)), p(this.low | e.low, this.high | e.high, this.unsigned);
549558
}, B.xor = function(e) {
550-
return g(e) || (e = y(e)), p(this.low ^ e.low, this.high ^ e.high, this.unsigned);
559+
return g(e) || (e = v(e)), p(this.low ^ e.low, this.high ^ e.high, this.unsigned);
551560
}, B.shiftLeft = function(e) {
552561
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);
553562
}, B.shl = B.shiftLeft, B.shiftRight = function(e) {

dist/osc-chromeapp.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Licensed under the MIT and GPL 3 licenses.
88
*/
99

10-
/* global require, module, process, Buffer, Long */
10+
/* global require, module, process, Buffer, Long, util */
1111

1212
var osc = osc || {};
1313

@@ -55,6 +55,11 @@ var osc = osc || {};
5555
osc.Long = typeof Long !== "undefined" ? Long :
5656
osc.isNode ? require("long") : undefined;
5757

58+
// Unsupported, non-API member. Can be removed when supported versions
59+
// of Node.js expose TextDecoder as a global, as in the browser.
60+
osc.TextDecoder = typeof TextDecoder !== "undefined" ? TextDecoder :
61+
typeof util !== "undefined" && typeof (util.TextDecoder !== "undefined") ? util.TextDecoder : undefined;
62+
5863
/**
5964
* Wraps the specified object in a DataView.
6065
*
@@ -164,7 +169,34 @@ var osc = osc || {};
164169
idx = (idx + 3) & ~0x03;
165170
offsetState.idx = idx;
166171

167-
return String.fromCharCode.apply(null, charCodes);
172+
var decoder = osc.isBufferEnv ? osc.readString.withBuffer :
173+
osc.TextDecoder ? osc.readString.withTextDecoder : osc.readString.raw;
174+
175+
return decoder(charCodes);
176+
};
177+
178+
osc.readString.raw = function (charCodes) {
179+
// If no Buffer or TextDecoder, resort to fromCharCode
180+
// This does not properly decode multi-byte Unicode characters.
181+
var str = "";
182+
var sliceSize = 10000;
183+
184+
// Processing the array in chunks so as not to exceed argument
185+
// limit, see https://bugs.webkit.org/show_bug.cgi?id=80797
186+
for (var i = 0; i < charCodes.length; i += sliceSize) {
187+
str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
188+
}
189+
190+
return str;
191+
};
192+
193+
osc.readString.withTextDecoder = function (charCodes) {
194+
var data = new Int8Array(charCodes);
195+
return new osc.TextDecoder("utf-8").decode(data);
196+
};
197+
198+
osc.readString.withBuffer = function (charCodes) {
199+
return Buffer.from(charCodes).toString("utf-8");
168200
};
169201

170202
/**

0 commit comments

Comments
 (0)