##// END OF EJS Templates
Fixed bug in alias; wrote new alias test.
Fixed bug in alias; wrote new alias test.

File last commit:

r20829:916efd3e
r20897:3d0defaa
Show More
serialize.js
126 lines | 4.2 KiB | application/javascript | JavascriptLexer
MinRK
use utf8.js...
r18331 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
MinRK
cleanup serialize per review
r18416 'underscore',
], function (_) {
MinRK
use utf8.js...
r18331 "use strict";
MinRK
support binary message from javascript
r18332
var _deserialize_array_buffer = function (buf) {
var data = new DataView(buf);
// read the header: 1 + nbufs 32b integers
MinRK
unsigned ints for offsets...
r18338 var nbufs = data.getUint32(0);
MinRK
support binary message from javascript
r18332 var offsets = [];
var i;
for (i = 1; i <= nbufs; i++) {
MinRK
unsigned ints for offsets...
r18338 offsets.push(data.getUint32(i * 4));
MinRK
support binary message from javascript
r18332 }
MinRK
use TextEncoding for string<->ArrayBuffer...
r18333 var json_bytes = new Uint8Array(buf.slice(offsets[0], offsets[1]));
MinRK
cleanup serialize per review
r18416 var msg = JSON.parse(
MinRK
use TextEncoding for string<->ArrayBuffer...
r18333 (new TextDecoder('utf8')).decode(json_bytes)
MinRK
support binary message from javascript
r18332 );
// the remaining chunks are stored as DataViews in msg.buffers
msg.buffers = [];
var start, stop;
for (i = 1; i < nbufs; i++) {
start = offsets[i];
stop = offsets[i+1] || buf.byteLength;
msg.buffers.push(new DataView(buf.slice(start, stop)));
}
return msg;
};
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 var _deserialize_binary = function(data) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* deserialize the binary message format
* callback will be called with a message whose buffers attribute
* will be an array of DataViews.
*/
MinRK
support binary message from javascript
r18332 if (data instanceof Blob) {
// data is Blob, have to deserialize from ArrayBuffer in reader callback
var reader = new FileReader();
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 var promise = new Promise(function(resolve, reject) {
reader.onload = function () {
var msg = _deserialize_array_buffer(this.result);
resolve(msg);
};
});
MinRK
support binary message from javascript
r18332 reader.readAsArrayBuffer(data);
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 return promise;
MinRK
support binary message from javascript
r18332 } else {
// data is ArrayBuffer, can deserialize directly
var msg = _deserialize_array_buffer(data);
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 return msg;
MinRK
support binary message from javascript
r18332 }
MinRK
use utf8.js...
r18331 };
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 var deserialize = function (data) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 * deserialize a message and return a promise for the unpacked message
Jonathan Frederic
Ran function comment conversion tool
r19176 */
MinRK
use utf8.js...
r18331 if (typeof data === "string") {
// text JSON message
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 return Promise.resolve(JSON.parse(data));
MinRK
use utf8.js...
r18331 } else {
// binary message
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 return Promise.resolve(_deserialize_binary(data));
MinRK
use utf8.js...
r18331 }
};
MinRK
support binary message from javascript
r18332 var _serialize_binary = function (msg) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* implement the binary serialization protocol
* serializes JSON message to ArrayBuffer
*/
MinRK
cleanup serialize per review
r18416 msg = _.clone(msg);
MinRK
support binary message from javascript
r18332 var offsets = [];
MinRK
test binary websocket messages...
r18337 var buffers = [];
Jason Grout
Make it possible for msg.buffers in javascript to be either ArrayBuffers or views
r20824 var i;
for (i = 0; i < msg.buffers.length; i++) {
// msg.buffers elements could be either views or ArrayBuffers
// buffers elements are ArrayBuffers
Jason Grout
Typecheck the .buffer attribute before assuming it is an ArrayBuffer
r20829 var b = msg.buffers[i];
buffers.push(b.buffer instanceof ArrayBuffer ? b.buffer : b);
Jason Grout
Make it possible for msg.buffers in javascript to be either ArrayBuffers or views
r20824 }
MinRK
support binary message from javascript
r18332 delete msg.buffers;
MinRK
use TextEncoding for string<->ArrayBuffer...
r18333 var json_utf8 = (new TextEncoder('utf8')).encode(JSON.stringify(msg));
MinRK
support binary message from javascript
r18332 buffers.unshift(json_utf8);
var nbufs = buffers.length;
offsets.push(4 * (nbufs + 1));
for (i = 0; i + 1 < buffers.length; i++) {
offsets.push(offsets[offsets.length-1] + buffers[i].byteLength);
}
var msg_buf = new Uint8Array(
offsets[offsets.length-1] + buffers[buffers.length-1].byteLength
);
MinRK
unsigned ints for offsets...
r18338 // use DataView.setUint32 for network byte-order
MinRK
support binary message from javascript
r18332 var view = new DataView(msg_buf.buffer);
// write nbufs to first 4 bytes
MinRK
unsigned ints for offsets...
r18338 view.setUint32(0, nbufs);
MinRK
support binary message from javascript
r18332 // write offsets to next 4 * nbufs bytes
for (i = 0; i < offsets.length; i++) {
MinRK
unsigned ints for offsets...
r18338 view.setUint32(4 * (i+1), offsets[i]);
MinRK
support binary message from javascript
r18332 }
// write all the buffers at their respective offsets
for (i = 0; i < buffers.length; i++) {
Jason Grout
Make it possible for msg.buffers in javascript to be either ArrayBuffers or views
r20824 msg_buf.set(new Uint8Array(buffers[i]), offsets[i]);
MinRK
support binary message from javascript
r18332 }
// return raw ArrayBuffer
return msg_buf.buffer;
};
var serialize = function (msg) {
if (msg.buffers && msg.buffers.length) {
return _serialize_binary(msg);
} else {
return JSON.stringify(msg);
}
};
var exports = {
deserialize : deserialize,
serialize: serialize
MinRK
use utf8.js...
r18331 };
MinRK
support binary message from javascript
r18332 return exports;
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 });