Skip to content

Commit de88016

Browse files
committed
Merge remote-tracking branch 'okofish/master' into gh-92
* okofish/master: 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 e774260 + 1cf77e2 commit de88016

File tree

10 files changed

+1166
-1022
lines changed

10 files changed

+1166
-1022
lines changed

dist/osc-browser.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,31 @@ var osc = osc || {};
164164
idx = (idx + 3) & ~0x03;
165165
offsetState.idx = idx;
166166

167-
return String.fromCharCode.apply(null, charCodes);
167+
if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line
168+
// Check for Buffer API (Node/Electron)
169+
if (Buffer.from) {
170+
// new Buffer() is now deprecated, so we use Buffer.from if available
171+
return Buffer.from(charCodes).toString("utf-8");
172+
} else {
173+
return new Buffer(charCodes).toString("utf-8");
174+
}
175+
} else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line
176+
// Check for TextDecoder API (Browser/WebKit-based)
177+
return new TextDecoder("utf-8").decode(new Int8Array(charCodes));
178+
} else {
179+
// If no Buffer or TextDecoder, resort to fromCharCode
180+
// This does not properly decode multi-byte Unicode characters.
181+
182+
var str = "";
183+
var sliceSize = 10000;
184+
185+
// Processing the array in chunks so as not to exceed argument
186+
// limit, see https://bugs.webkit.org/show_bug.cgi?id=80797
187+
for (var i = 0; i < charCodes.length; i += sliceSize) {
188+
str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
189+
}
190+
return str;
191+
}
168192
};
169193

170194
/**

dist/osc-browser.min.js

Lines changed: 191 additions & 188 deletions
Large diffs are not rendered by default.

dist/osc-chromeapp.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,31 @@ var osc = osc || {};
164164
idx = (idx + 3) & ~0x03;
165165
offsetState.idx = idx;
166166

167-
return String.fromCharCode.apply(null, charCodes);
167+
if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line
168+
// Check for Buffer API (Node/Electron)
169+
if (Buffer.from) {
170+
// new Buffer() is now deprecated, so we use Buffer.from if available
171+
return Buffer.from(charCodes).toString("utf-8");
172+
} else {
173+
return new Buffer(charCodes).toString("utf-8");
174+
}
175+
} else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line
176+
// Check for TextDecoder API (Browser/WebKit-based)
177+
return new TextDecoder("utf-8").decode(new Int8Array(charCodes));
178+
} else {
179+
// If no Buffer or TextDecoder, resort to fromCharCode
180+
// This does not properly decode multi-byte Unicode characters.
181+
182+
var str = "";
183+
var sliceSize = 10000;
184+
185+
// Processing the array in chunks so as not to exceed argument
186+
// limit, see https://bugs.webkit.org/show_bug.cgi?id=80797
187+
for (var i = 0; i < charCodes.length; i += sliceSize) {
188+
str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
189+
}
190+
return str;
191+
}
168192
};
169193

170194
/**

dist/osc-chromeapp.min.js

Lines changed: 573 additions & 570 deletions
Large diffs are not rendered by default.

dist/osc-module.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,31 @@ var osc = osc || {};
182182
idx = (idx + 3) & ~0x03;
183183
offsetState.idx = idx;
184184

185-
return String.fromCharCode.apply(null, charCodes);
185+
if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line
186+
// Check for Buffer API (Node/Electron)
187+
if (Buffer.from) {
188+
// new Buffer() is now deprecated, so we use Buffer.from if available
189+
return Buffer.from(charCodes).toString("utf-8");
190+
} else {
191+
return new Buffer(charCodes).toString("utf-8");
192+
}
193+
} else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line
194+
// Check for TextDecoder API (Browser/WebKit-based)
195+
return new TextDecoder("utf-8").decode(new Int8Array(charCodes));
196+
} else {
197+
// If no Buffer or TextDecoder, resort to fromCharCode
198+
// This does not properly decode multi-byte Unicode characters.
199+
200+
var str = "";
201+
var sliceSize = 10000;
202+
203+
// Processing the array in chunks so as not to exceed argument
204+
// limit, see https://bugs.webkit.org/show_bug.cgi?id=80797
205+
for (var i = 0; i < charCodes.length; i += sliceSize) {
206+
str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
207+
}
208+
return str;
209+
}
186210
};
187211

188212
/**

dist/osc-module.min.js

Lines changed: 234 additions & 231 deletions
Large diffs are not rendered by default.

dist/osc.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,31 @@ var osc = osc || {};
164164
idx = (idx + 3) & ~0x03;
165165
offsetState.idx = idx;
166166

167-
return String.fromCharCode.apply(null, charCodes);
167+
if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line
168+
// Check for Buffer API (Node/Electron)
169+
if (Buffer.from) {
170+
// new Buffer() is now deprecated, so we use Buffer.from if available
171+
return Buffer.from(charCodes).toString("utf-8");
172+
} else {
173+
return new Buffer(charCodes).toString("utf-8");
174+
}
175+
} else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line
176+
// Check for TextDecoder API (Browser/WebKit-based)
177+
return new TextDecoder("utf-8").decode(new Int8Array(charCodes));
178+
} else {
179+
// If no Buffer or TextDecoder, resort to fromCharCode
180+
// This does not properly decode multi-byte Unicode characters.
181+
182+
var str = "";
183+
var sliceSize = 10000;
184+
185+
// Processing the array in chunks so as not to exceed argument
186+
// limit, see https://bugs.webkit.org/show_bug.cgi?id=80797
187+
for (var i = 0; i < charCodes.length; i += sliceSize) {
188+
str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
189+
}
190+
return str;
191+
}
168192
};
169193

170194
/**

dist/osc.min.js

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ var osc = osc || {};
2727
}, osc.nativeBuffer = function(r) {
2828
return osc.isBufferEnv ? osc.isBuffer(r) ? r : new Buffer(r.buffer ? r : new Uint8Array(r)) : osc.isTypedArrayView(r) ? r : new Uint8Array(r);
2929
}, osc.copyByteArray = function(r, e, t) {
30-
if (osc.isTypedArrayView(r) && osc.isTypedArrayView(e)) e.set(r, t); else for (var n = void 0 === t ? 0 : t, a = Math.min(e.length - t, r.length), o = 0, s = n; o < a; o++,
31-
s++) e[s] = r[o];
30+
if (osc.isTypedArrayView(r) && osc.isTypedArrayView(e)) e.set(r, t); else for (var n = void 0 === t ? 0 : t, a = Math.min(e.length - t, r.length), o = 0, i = n; o < a; o++,
31+
i++) e[i] = r[o];
3232
return e;
3333
}, osc.readString = function(r, e) {
3434
for (var t = [], n = e.idx; n < r.byteLength; n++) {
@@ -39,7 +39,10 @@ var osc = osc || {};
3939
}
4040
t.push(a);
4141
}
42-
return n = n + 3 & -4, e.idx = n, String.fromCharCode.apply(null, t);
42+
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");
43+
if (("undefined" != typeof global ? global : window).hasOwnProperty("TextDecoder")) return new TextDecoder("utf-8").decode(new Int8Array(t));
44+
for (var o = "", i = 0; i < t.length; i += 1e4) o += String.fromCharCode.apply(null, t.slice(i, i + 1e4));
45+
return o;
4346
}, osc.writeString = function(r) {
4447
for (var e = r + "\0", t = e.length, n = new Uint8Array(t + 3 & -4), a = 0; a < e.length; a++) {
4548
var o = e.charCodeAt(a);
@@ -125,13 +128,13 @@ var osc = osc || {};
125128
return osc.writeInt32(e[0], n, 0), osc.writeInt32(e[1], n, 4), t;
126129
}, osc.timeTag = function(r, e) {
127130
r = r || 0;
128-
var t = (e = e || Date.now()) / 1e3, n = Math.floor(t), a = t - n, o = Math.floor(r), s = a + (r - o);
129-
if (s > 1) {
130-
var i = Math.floor(s);
131-
o += i, s = s - i;
131+
var t = (e = e || Date.now()) / 1e3, n = Math.floor(t), a = t - n, o = Math.floor(r), i = a + (r - o);
132+
if (1 < i) {
133+
var s = Math.floor(i);
134+
o += s, i = i - s;
132135
}
133136
return {
134-
raw: [ n + o + osc.SECS_70YRS, Math.round(osc.TWO_32 * s) ]
137+
raw: [ n + o + osc.SECS_70YRS, Math.round(osc.TWO_32 * i) ]
135138
};
136139
}, osc.ntpToJSTime = function(r, e) {
137140
return 1e3 * (r - osc.SECS_70YRS + e / osc.TWO_32);
@@ -146,21 +149,21 @@ var osc = osc || {};
146149
}, osc.readArgument = function(r, e, t, n, a) {
147150
var o = osc.argumentTypes[r];
148151
if (!o) throw new Error("'" + r + "' is not a valid OSC type tag. Type tag string was: " + e);
149-
var s = o.reader, i = osc[s](t, a);
150-
return n.metadata && (i = {
152+
var i = o.reader, s = osc[i](t, a);
153+
return n.metadata && (s = {
151154
type: r,
152-
value: i
153-
}), i;
155+
value: s
156+
}), s;
154157
}, osc.readArgumentsIntoArray = function(r, e, t, n, a, o) {
155-
for (var s = 0; s < e.length; ) {
156-
var i, c = e[s];
158+
for (var i = 0; i < e.length; ) {
159+
var s, c = e[i];
157160
if ("[" === c) {
158-
var u = e.slice(s + 1), d = u.indexOf("]");
159-
if (d < 0) throw new Error("Invalid argument type tag: an open array type tag ('[') was found without a matching close array tag ('[]'). Type tag was: " + t);
160-
var f = u.slice(0, d);
161-
i = osc.readArgumentsIntoArray([], f, t, n, a, o), s += d + 2;
162-
} else i = osc.readArgument(c, t, n, a, o), s++;
163-
r.push(i);
161+
var u = e.slice(i + 1), f = u.indexOf("]");
162+
if (f < 0) throw new Error("Invalid argument type tag: an open array type tag ('[') was found without a matching close array tag ('[]'). Type tag was: " + t);
163+
var d = u.slice(0, f);
164+
s = osc.readArgumentsIntoArray([], d, t, n, a, o), i += f + 2;
165+
} else s = osc.readArgument(c, t, n, a, o), i++;
166+
r.push(s);
164167
}
165168
return r;
166169
}, osc.writeArguments = function(r, e) {
@@ -194,11 +197,11 @@ var osc = osc || {};
194197
parts: []
195198
}, e.metadata || (r = osc.annotateArguments(r));
196199
for (var n = ",", a = t.parts.length, o = 0; o < r.length; o++) {
197-
var s = r[o];
198-
n += osc.writeArgument(s, t);
200+
var i = r[o];
201+
n += osc.writeArgument(i, t);
199202
}
200-
var i = osc.writeString(n);
201-
return t.byteLength += i.byteLength, t.parts.splice(a, 0, i), t;
203+
var s = osc.writeString(n);
204+
return t.byteLength += s.byteLength, t.parts.splice(a, 0, s), t;
202205
}, osc.readMessage = function(r, e, t) {
203206
e = e || osc.defaults;
204207
var n = osc.dataView(r, r.byteOffset, r.byteLength);
@@ -247,7 +250,7 @@ var osc = osc || {};
247250
return void 0 !== r.timeTag && void 0 !== r.packets;
248251
}, osc.readBundleContents = function(r, e, t, n) {
249252
for (var a = osc.readTimeTag(r, t), o = []; t.idx < n; ) {
250-
var s = osc.readInt32(r, t), i = t.idx + s, c = osc.readPacket(r, e, t, i);
253+
var i = osc.readInt32(r, t), s = t.idx + i, c = osc.readPacket(r, e, t, s);
251254
o.push(c);
252255
}
253256
return {
@@ -259,9 +262,9 @@ var osc = osc || {};
259262
n = void 0 === n ? a.byteLength : n, t = t || {
260263
idx: 0
261264
};
262-
var o = osc.readString(a, t), s = o[0];
263-
if ("#" === s) return osc.readBundleContents(a, e, t, n);
264-
if ("/" === s) return osc.readMessageContents(o, a, e, t);
265+
var o = osc.readString(a, t), i = o[0];
266+
if ("#" === i) return osc.readBundleContents(a, e, t, n);
267+
if ("/" === i) return osc.readMessageContents(o, a, e, t);
265268
throw new Error("The header of an OSC packet didn't contain an OSC address or a #bundle string. Header was: " + o);
266269
}, osc.writePacket = function(r, e) {
267270
if (osc.isValidMessage(r)) return osc.writeMessage(r, e);

src/osc.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,31 @@ var osc = osc || {};
162162
idx = (idx + 3) & ~0x03;
163163
offsetState.idx = idx;
164164

165-
return String.fromCharCode.apply(null, charCodes);
165+
if ((typeof global !== "undefined" ? global : window).hasOwnProperty("Buffer")) { // jshint ignore:line
166+
// Check for Buffer API (Node/Electron)
167+
if (Buffer.from) {
168+
// new Buffer() is now deprecated, so we use Buffer.from if available
169+
return Buffer.from(charCodes).toString("utf-8");
170+
} else {
171+
return new Buffer(charCodes).toString("utf-8");
172+
}
173+
} else if ((typeof global !== "undefined" ? global : window).hasOwnProperty("TextDecoder")) { // jshint ignore:line
174+
// Check for TextDecoder API (Browser/WebKit-based)
175+
return new TextDecoder("utf-8").decode(new Int8Array(charCodes));
176+
} else {
177+
// If no Buffer or TextDecoder, resort to fromCharCode
178+
// This does not properly decode multi-byte Unicode characters.
179+
180+
var str = "";
181+
var sliceSize = 10000;
182+
183+
// Processing the array in chunks so as not to exceed argument
184+
// limit, see https://bugs.webkit.org/show_bug.cgi?id=80797
185+
for (var i = 0; i < charCodes.length; i += sliceSize) {
186+
str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
187+
}
188+
return str;
189+
}
166190
};
167191

168192
/**

tests/osc-tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ var fluid = fluid || require("infusion"),
246246
});
247247
});
248248

249+
QUnit.test("readString very long string argument", function () {
250+
var expectedChars = new Array(400000);
251+
for (var i = 0; i < expectedChars.length; i++) {
252+
expectedChars[i] = "A";
253+
}
254+
var expected = expectedChars.join(""),
255+
dv = oscjsTests.stringToDataView(expected),
256+
actual = osc.readString(dv, {idx: 0});
257+
258+
QUnit.equal(actual, expected, "The string should have been read correctly.");
259+
});
260+
249261

250262
/***********
251263
* Numbers *

0 commit comments

Comments
 (0)