-
Notifications
You must be signed in to change notification settings - Fork 2
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
Binary event #9
Comments
msgpack-lite looks the best as MessagePack JavaScript implementation. It is available on browser and Node.js. But, it's too big (37KB minified, 11KB gziped) to include it to cettia.js so those who want to handle binary on browser should include msgpack-lite manually and it should be noted in documentation. var msgpack = require('msgpack-lite');
var obj = {
text: "Donghwan Kim",
binary: new Buffer("Donghwan Kim")
};
var bytes = msgpack.encode(obj);
console.log('[' + new Int8Array(bytes).join(', ') + ']');
console.log(msgpack.decode(bytes));
Note that the first line from the result, |
On Node, to traverse the given object to check if it contains binary, we can use traverse. var traverse = require("traverse");
var hasBinary = data => traverse(data).reduce((hasBuffer, e) => {
return hasBuffer || Buffer.isBuffer(e);
}, false);
console.log(hasBinary("Donghwan Kim"));
console.log(hasBinary(new Buffer("Donghwan Kim")));
Unfortunately, traverse module is somewhat big to embed in cettia.js. As a trick on browser, using JSON.stringify's replacer option, it's possible to detect if the given object has binary without importing an external module. var hasBinary = data => {
var ArrayBuffer = window.ArrayBuffer;
if (!ArrayBuffer) {
return false;
}
var hasView = false;
JSON.stringify(data, (key, value) => {
hasView = hasView || ArrayBuffer.isView(value);
return value;
});
return hasView;
};
console.log(hasBinary("Donghwan Kim"));
console.log(hasBinary(new TextEncoder().encode("Donghwan Kim")));
Anyway, if it turns out that it has binary, |
|
|
Modified example - http://jsbin.com/xakovoguqe/edit?html,js,console |
In d8576e7 commit, a condition to determine if the current environment is Node.js, |
JSBin updated to load |
An explanation about |
The example is updated http://jsbin.com/votuzakaro/edit?html,js,console |
Derived from cettia/cettia-protocol#9
The text was updated successfully, but these errors were encountered: