Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make osc.readString not fail on very large strings #92

Merged
merged 8 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion dist/osc-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,31 @@ var osc = osc || {};
idx = (idx + 3) & ~0x03;
offsetState.idx = idx;

return String.fromCharCode.apply(null, charCodes);
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 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;
}
};

/**
Expand Down
377 changes: 190 additions & 187 deletions dist/osc-browser.min.js

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion dist/osc-chromeapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,31 @@ var osc = osc || {};
idx = (idx + 3) & ~0x03;
offsetState.idx = idx;

return String.fromCharCode.apply(null, charCodes);
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 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;
}
};

/**
Expand Down
1,143 changes: 573 additions & 570 deletions dist/osc-chromeapp.min.js

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion dist/osc-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,31 @@ var osc = osc || {};
idx = (idx + 3) & ~0x03;
offsetState.idx = idx;

return String.fromCharCode.apply(null, charCodes);
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 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;
}
};

/**
Expand Down
465 changes: 234 additions & 231 deletions dist/osc-module.min.js

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion dist/osc.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,31 @@ var osc = osc || {};
idx = (idx + 3) & ~0x03;
offsetState.idx = idx;

return String.fromCharCode.apply(null, charCodes);
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 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;
}
};

/**
Expand Down
59 changes: 31 additions & 28 deletions dist/osc.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ var osc = osc || {};
}, osc.nativeBuffer = function(r) {
return osc.isBufferEnv ? osc.isBuffer(r) ? r : new Buffer(r.buffer ? r : new Uint8Array(r)) : osc.isTypedArrayView(r) ? r : new Uint8Array(r);
}, osc.copyByteArray = function(r, e, t) {
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++,
s++) e[s] = r[o];
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++,
i++) e[i] = r[o];
return e;
}, osc.readString = function(r, e) {
for (var t = [], n = e.idx; n < r.byteLength; n++) {
Expand All @@ -39,7 +39,10 @@ var osc = osc || {};
}
t.push(a);
}
return n = n + 3 & -4, e.idx = n, String.fromCharCode.apply(null, t);
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;
}, 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);
Expand Down Expand Up @@ -125,13 +128,13 @@ var osc = osc || {};
return osc.writeInt32(e[0], n, 0), osc.writeInt32(e[1], n, 4), t;
}, osc.timeTag = function(r, e) {
r = r || 0;
var t = (e = e || Date.now()) / 1e3, n = Math.floor(t), a = t - n, o = Math.floor(r), s = a + (r - o);
if (s > 1) {
var i = Math.floor(s);
o += i, s = s - i;
var t = (e = e || Date.now()) / 1e3, n = Math.floor(t), a = t - n, o = Math.floor(r), i = a + (r - o);
if (1 < i) {
var s = Math.floor(i);
o += s, i = i - s;
}
return {
raw: [ n + o + osc.SECS_70YRS, Math.round(osc.TWO_32 * s) ]
raw: [ n + o + osc.SECS_70YRS, Math.round(osc.TWO_32 * i) ]
};
}, osc.ntpToJSTime = function(r, e) {
return 1e3 * (r - osc.SECS_70YRS + e / osc.TWO_32);
Expand All @@ -146,21 +149,21 @@ var osc = osc || {};
}, osc.readArgument = function(r, e, t, n, a) {
var o = osc.argumentTypes[r];
if (!o) throw new Error("'" + r + "' is not a valid OSC type tag. Type tag string was: " + e);
var s = o.reader, i = osc[s](t, a);
return n.metadata && (i = {
var i = o.reader, s = osc[i](t, a);
return n.metadata && (s = {
type: r,
value: i
}), i;
value: s
}), s;
}, osc.readArgumentsIntoArray = function(r, e, t, n, a, o) {
for (var s = 0; s < e.length; ) {
var i, c = e[s];
for (var i = 0; i < e.length; ) {
var s, c = e[i];
if ("[" === c) {
var u = e.slice(s + 1), d = u.indexOf("]");
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);
var f = u.slice(0, d);
i = osc.readArgumentsIntoArray([], f, t, n, a, o), s += d + 2;
} else i = osc.readArgument(c, t, n, a, o), s++;
r.push(i);
var u = e.slice(i + 1), f = u.indexOf("]");
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);
var d = u.slice(0, f);
s = osc.readArgumentsIntoArray([], d, t, n, a, o), i += f + 2;
} else s = osc.readArgument(c, t, n, a, o), i++;
r.push(s);
}
return r;
}, osc.writeArguments = function(r, e) {
Expand Down Expand Up @@ -194,11 +197,11 @@ var osc = osc || {};
parts: []
}, e.metadata || (r = osc.annotateArguments(r));
for (var n = ",", a = t.parts.length, o = 0; o < r.length; o++) {
var s = r[o];
n += osc.writeArgument(s, t);
var i = r[o];
n += osc.writeArgument(i, t);
}
var i = osc.writeString(n);
return t.byteLength += i.byteLength, t.parts.splice(a, 0, i), t;
var s = osc.writeString(n);
return t.byteLength += s.byteLength, t.parts.splice(a, 0, s), t;
}, osc.readMessage = function(r, e, t) {
e = e || osc.defaults;
var n = osc.dataView(r, r.byteOffset, r.byteLength);
Expand Down Expand Up @@ -247,7 +250,7 @@ var osc = osc || {};
return void 0 !== r.timeTag && void 0 !== r.packets;
}, osc.readBundleContents = function(r, e, t, n) {
for (var a = osc.readTimeTag(r, t), o = []; t.idx < n; ) {
var s = osc.readInt32(r, t), i = t.idx + s, c = osc.readPacket(r, e, t, i);
var i = osc.readInt32(r, t), s = t.idx + i, c = osc.readPacket(r, e, t, s);
o.push(c);
}
return {
Expand All @@ -259,9 +262,9 @@ var osc = osc || {};
n = void 0 === n ? a.byteLength : n, t = t || {
idx: 0
};
var o = osc.readString(a, t), s = o[0];
if ("#" === s) return osc.readBundleContents(a, e, t, n);
if ("/" === s) return osc.readMessageContents(o, a, e, t);
var o = osc.readString(a, t), i = o[0];
if ("#" === i) return osc.readBundleContents(a, e, t, n);
if ("/" === i) return osc.readMessageContents(o, a, e, t);
throw new Error("The header of an OSC packet didn't contain an OSC address or a #bundle string. Header was: " + o);
}, osc.writePacket = function(r, e) {
if (osc.isValidMessage(r)) return osc.writeMessage(r, e);
Expand Down
26 changes: 25 additions & 1 deletion src/osc.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,31 @@ var osc = osc || {};
idx = (idx + 3) & ~0x03;
offsetState.idx = idx;

return String.fromCharCode.apply(null, charCodes);
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 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;
}
};

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/osc-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ var fluid = fluid || require("infusion"),
});
});

QUnit.test("readString very long string argument", function () {
var expectedChars = new Array(400000);
for (var i = 0; i < expectedChars.length; i++) {
expectedChars[i] = "A";
}
var expected = expectedChars.join(""),
dv = oscjsTests.stringToDataView(expected),
actual = osc.readString(dv, {idx: 0});

QUnit.equal(actual, expected, "The string should have been read correctly.");
});


/***********
* Numbers *
Expand Down