Skip to content
Open
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
15 changes: 15 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log
4 changes: 4 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Client


## Changelog
0.2.2

- Update js-binarypack to 0.0.9, removed unused check.

0.2.1

- Update js-binarypack to 0.0.7, fast utf8 support now on by default.
Expand Down
3 changes: 1 addition & 2 deletions bin/build.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ var starttagIF = '// if node'
*/

var base = [
'../deps/js-binarypack/lib/bufferbuilder.js'
, '../deps/js-binarypack/lib/binarypack.js'
'../deps/js-binarypack/dist/binarypack.js'
, '../deps/EventEmitter/EventEmitter.js'
, 'util.js'
, 'client/stream.js'
Expand Down
166 changes: 88 additions & 78 deletions dist/binary.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,79 +1,10 @@
/*! binary.js build:0.2.1, development. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */
/*! binary.js build:0.2.2, development. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */
(function(exports){
var binaryFeatures = {};
binaryFeatures.useBlobBuilder = (function(){
try {
new Blob([]);
return false;
} catch (e) {
return true;
}
})();
/*! binarypack.js build:0.0.9, production. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var BufferBuilder = require('./bufferbuilder').BufferBuilder;
var binaryFeatures = require('./bufferbuilder').binaryFeatures;

binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function(){
try {
return (new Blob([new Uint8Array([])])).size === 0;
} catch (e) {
return true;
}
})();
binaryFeatures.supportsBinaryWebsockets = (function(){
try {
var wstest = new WebSocket('ws://null');
wstest.onerror = function(){};
if (typeof(wstest.binaryType) !== "undefined") {
return true;
} else {
return false;
}
wstest.close();
wstest = null;
} catch (e) {
return false;
}
})();

exports.binaryFeatures = binaryFeatures;
exports.BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder;

function BufferBuilder(){
this._pieces = [];
this._parts = [];
}

BufferBuilder.prototype.append = function(data) {
if(typeof data === 'number') {
this._pieces.push(data);
} else {
this.flush();
this._parts.push(data);
}
};

BufferBuilder.prototype.flush = function() {
if (this._pieces.length > 0) {
var buf = new Uint8Array(this._pieces);
if(!binaryFeatures.useArrayBufferView) {
buf = buf.buffer;
}
this._parts.push(buf);
this._pieces = [];
}
};

BufferBuilder.prototype.getBuffer = function() {
this.flush();
if(binaryFeatures.useBlobBuilder) {
var builder = new BlobBuilder();
for(var i = 0, ii = this._parts.length; i < ii; i++) {
builder.append(this._parts[i]);
}
return builder.getBlob();
} else {
return new Blob(this._parts);
}
};
exports.BinaryPack = {
var BinaryPack = {
unpack: function(data){
var unpacker = new Unpacker(data);
return unpacker.unpack();
Expand All @@ -86,6 +17,8 @@ exports.BinaryPack = {
}
};

module.exports = BinaryPack;

function Unpacker (data){
// Data is ArrayBuffer
this.index = 0;
Expand All @@ -94,7 +27,6 @@ function Unpacker (data){
this.length = this.dataBuffer.byteLength;
}


Unpacker.prototype.unpack = function(){
var type = this.unpack_uint8();
if (type < 0x80){
Expand Down Expand Up @@ -400,7 +332,6 @@ Packer.prototype.pack_bin = function(blob){
this.pack_uint32(length);
} else{
throw new Error('Invalid length');
return;
}
this.bufferBuilder.append(blob);
}
Expand All @@ -418,7 +349,6 @@ Packer.prototype.pack_string = function(str){
this.pack_uint32(length);
} else{
throw new Error('Invalid length');
return;
}
this.bufferBuilder.append(str);
}
Expand Down Expand Up @@ -590,6 +520,82 @@ function utf8Length(str){
return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length;
}
}

},{"./bufferbuilder":2}],2:[function(require,module,exports){
var binaryFeatures = {};
binaryFeatures.useBlobBuilder = (function(){
try {
new Blob([]);
return false;
} catch (e) {
return true;
}
})();

binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function(){
try {
return (new Blob([new Uint8Array([])])).size === 0;
} catch (e) {
return true;
}
})();

module.exports.binaryFeatures = binaryFeatures;
var BlobBuilder = module.exports.BlobBuilder;
if (typeof window != 'undefined') {
BlobBuilder = module.exports.BlobBuilder = window.WebKitBlobBuilder ||
window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder;
}

function BufferBuilder(){
this._pieces = [];
this._parts = [];
}

BufferBuilder.prototype.append = function(data) {
if(typeof data === 'number') {
this._pieces.push(data);
} else {
this.flush();
this._parts.push(data);
}
};

BufferBuilder.prototype.flush = function() {
if (this._pieces.length > 0) {
var buf = new Uint8Array(this._pieces);
if(!binaryFeatures.useArrayBufferView) {
buf = buf.buffer;
}
this._parts.push(buf);
this._pieces = [];
}
};

BufferBuilder.prototype.getBuffer = function() {
this.flush();
if(binaryFeatures.useBlobBuilder) {
var builder = new BlobBuilder();
for(var i = 0, ii = this._parts.length; i < ii; i++) {
builder.append(this._parts[i]);
}
return builder.getBlob();
} else {
return new Blob(this._parts);
}
};

module.exports.BufferBuilder = BufferBuilder;

},{}],3:[function(require,module,exports){
var BufferBuilderExports = require('./bufferbuilder');

window.BufferBuilder = BufferBuilderExports.BufferBuilder;
window.binaryFeatures = BufferBuilderExports.binaryFeatures;
window.BlobBuilder = BufferBuilderExports.BlobBuilder;
window.BinaryPack = require('./binarypack');

},{"./binarypack":1,"./bufferbuilder":2}]},{},[3]);
/**
* Light EventEmitter. Ported from Node.js/events.js
* Eric Zhang
Expand Down Expand Up @@ -805,6 +811,7 @@ var util = {
}(this))
};

exports.util = util;


function Stream() {
Expand Down Expand Up @@ -897,6 +904,8 @@ Stream.prototype.pipe = function(dest, options) {
// Allow for unix-like usage: A.pipe(B).pipe(C)
return dest;
};

exports.Stream = Stream;
function BlobReadStream(source, options){
Stream.call(this);

Expand Down Expand Up @@ -1280,7 +1289,8 @@ BinaryStream.prototype.write = function(data) {
var out = this._write(2, data, this.id);
return !this.paused && out;
} else {
throw new Error('Stream is not writable');
this.emit('error', new Error('Stream is not writable'));
return false;
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/binary.min.js
100644 → 100755

Large diffs are not rendered by default.

43 changes: 41 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var Stream = require('stream').Stream;
var EventEmitter = require('events').EventEmitter;
var BufferReadStream = require('streamers').BufferReadStream;
var WebSocket = require('streamws');
var WebSocket = require('ws');

var util = require('./util');
var BinaryStream = require('./stream').BinaryStream;
Expand Down Expand Up @@ -36,6 +36,12 @@ function BinaryClient(socket, options) {
this._socket.addEventListener('open', function(){
self.emit('open');
});
this._socket.addEventListener('ping', function(data, flags){
self.emit('ping', data, flags);
});
this._socket.addEventListener('pong', function(data, flags){
self.emit('pong', data, flags);
});
// if node
this._socket.on('drain', function(){
var ids = Object.keys(self.streams);
Expand Down Expand Up @@ -90,7 +96,13 @@ function BinaryClient(socket, options) {
//
// Close
// [ 6 , null , streamId ]
//
//
// 7
// [ 7 , Notification , streamId ]
//
// 8
// [ 8 , Message ]
//

data = data.data;

Expand Down Expand Up @@ -162,6 +174,20 @@ function BinaryClient(socket, options) {
self.emit('error', new Error('Received `close` message for unknown stream: ' + streamId));
}
break;
case 7:
var streamId = data[2];
var binaryStream = self.streams[streamId];
if(binaryStream) {
var event = data[1];
binaryStream._onNotification(event);
} else {
self.emit('error', new Error('Received `notification` message for unknown stream: ' + streamId));
}
break;
case 8:
var message = data[1];
self.emit('message', message);
break;
default:
self.emit('error', new Error('Unrecognized message type received: ' + data[0]));
}
Expand All @@ -171,6 +197,19 @@ function BinaryClient(socket, options) {

util.inherits(BinaryClient, EventEmitter);

BinaryClient.prototype.ping = function(data){
return this._socket.ping(data, {}, true);
}

BinaryClient.prototype.pong = function(data){
return this._socket.pong(data, {}, true);
}

BinaryClient.prototype.sendMessage = function(message){
var data = util.pack([8, message, 0]);
return this._socket.send(data, {binary: true});
}

BinaryClient.prototype.send = function(data, meta){
var stream = this.createStream(meta);
if(data instanceof Stream) {
Expand Down
2 changes: 2 additions & 0 deletions lib/client/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,5 @@ Stream.prototype.pipe = function(dest, options) {
// Allow for unix-like usage: A.pipe(B).pipe(C)
return dest;
};

exports.Stream = Stream;
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var ws = require('streamws');
var ws = require('ws');
var EventEmitter = require('events').EventEmitter;
var util = require('./util');

Expand Down
13 changes: 11 additions & 2 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ BinaryStream.prototype._write = function(code, data, bonus) {
return false;
}
var message = util.pack([code, data, bonus]);
return this._socket.send(message) !== false;
return this._socket.send(message, {binary: true}) !== false;
};

BinaryStream.prototype.write = function(data) {
if(this.writable) {
var out = this._write(2, data, this.id);
return !this.paused && out;
} else {
throw new Error('Stream is not writable');
this.emit('error', new Error('Stream is not writable'));
return false;
}
};

Expand Down Expand Up @@ -113,6 +114,14 @@ BinaryStream.prototype._onData = function(data) {
this.emit('data', data);
};

BinaryStream.prototype._onNotification = function(event) {
this.emit('notify', event);
};

BinaryStream.prototype.sendNotification = function(event) {
this._write(7, event, this.id);
};

BinaryStream.prototype.pause = function() {
this._onPause();
this._write(3, null, this.id);
Expand Down
1 change: 1 addition & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var util = {
}(this))
};

exports.util = util;
// if node
module.exports = util;
// end node
Loading