first commit
This commit is contained in:
251
node_modules/binaryjs/lib/client.js
generated
vendored
Normal file
251
node_modules/binaryjs/lib/client.js
generated
vendored
Normal file
@ -0,0 +1,251 @@
|
||||
// if node
|
||||
var Stream = require('stream').Stream;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var BufferReadStream = require('streamers').BufferReadStream;
|
||||
var WebSocket = require('streamws');
|
||||
|
||||
var util = require('./util');
|
||||
var BinaryStream = require('./stream').BinaryStream;
|
||||
|
||||
// end node
|
||||
|
||||
function BinaryClient(socket, options) {
|
||||
if (!(this instanceof BinaryClient)) return new BinaryClient(socket, options);
|
||||
|
||||
EventEmitter.call(this);
|
||||
|
||||
var self = this;
|
||||
|
||||
this._options = util.extend({
|
||||
chunkSize: 40960
|
||||
}, options);
|
||||
|
||||
this.streams = {};
|
||||
|
||||
if(typeof socket === 'string') {
|
||||
this._nextId = 0;
|
||||
this._socket = new WebSocket(socket);
|
||||
} else {
|
||||
// Use odd numbered ids for server originated streams
|
||||
this._nextId = 1;
|
||||
this._socket = socket;
|
||||
}
|
||||
|
||||
this._socket.binaryType = 'arraybuffer';
|
||||
|
||||
this._socket.addEventListener('open', function(){
|
||||
self.emit('open');
|
||||
});
|
||||
// if node
|
||||
this._socket.on('drain', function(){
|
||||
var ids = Object.keys(self.streams);
|
||||
for (var i = 0, ii = ids.length; i < ii; i++) {
|
||||
self.streams[ids[i]]._onDrain();
|
||||
}
|
||||
});
|
||||
// end node
|
||||
this._socket.addEventListener('error', function(error){
|
||||
var ids = Object.keys(self.streams);
|
||||
for (var i = 0, ii = ids.length; i < ii; i++) {
|
||||
self.streams[ids[i]]._onError(error);
|
||||
}
|
||||
self.emit('error', error);
|
||||
});
|
||||
this._socket.addEventListener('close', function(code, message){
|
||||
var ids = Object.keys(self.streams);
|
||||
for (var i = 0, ii = ids.length; i < ii; i++) {
|
||||
self.streams[ids[i]]._onClose();
|
||||
}
|
||||
self.emit('close', code, message);
|
||||
});
|
||||
this._socket.addEventListener('message', function(data, flags){
|
||||
util.setZeroTimeout(function(){
|
||||
|
||||
// Message format
|
||||
// [type, payload, bonus ]
|
||||
//
|
||||
// Reserved
|
||||
// [ 0 , X , X ]
|
||||
//
|
||||
//
|
||||
// New stream
|
||||
// [ 1 , Meta , new streamId ]
|
||||
//
|
||||
//
|
||||
// Data
|
||||
// [ 2 , Data , streamId ]
|
||||
//
|
||||
//
|
||||
// Pause
|
||||
// [ 3 , null , streamId ]
|
||||
//
|
||||
//
|
||||
// Resume
|
||||
// [ 4 , null , streamId ]
|
||||
//
|
||||
//
|
||||
// End
|
||||
// [ 5 , null , streamId ]
|
||||
//
|
||||
//
|
||||
// Close
|
||||
// [ 6 , null , streamId ]
|
||||
//
|
||||
|
||||
data = data.data;
|
||||
|
||||
try {
|
||||
data = util.unpack(data);
|
||||
} catch (ex) {
|
||||
return self.emit('error', new Error('Received unparsable message: ' + ex));
|
||||
}
|
||||
if (!(data instanceof Array))
|
||||
return self.emit('error', new Error('Received non-array message'));
|
||||
if (data.length != 3)
|
||||
return self.emit('error', new Error('Received message with wrong part count: ' + data.length));
|
||||
if ('number' != typeof data[0])
|
||||
return self.emit('error', new Error('Received message with non-number type: ' + data[0]));
|
||||
|
||||
switch(data[0]) {
|
||||
case 0:
|
||||
// Reserved
|
||||
break;
|
||||
case 1:
|
||||
var meta = data[1];
|
||||
var streamId = data[2];
|
||||
var binaryStream = self._receiveStream(streamId);
|
||||
self.emit('stream', binaryStream, meta);
|
||||
break;
|
||||
case 2:
|
||||
var payload = data[1];
|
||||
var streamId = data[2];
|
||||
var binaryStream = self.streams[streamId];
|
||||
if(binaryStream) {
|
||||
binaryStream._onData(payload);
|
||||
} else {
|
||||
self.emit('error', new Error('Received `data` message for unknown stream: ' + streamId));
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
var streamId = data[2];
|
||||
var binaryStream = self.streams[streamId];
|
||||
if(binaryStream) {
|
||||
binaryStream._onPause();
|
||||
} else {
|
||||
self.emit('error', new Error('Received `pause` message for unknown stream: ' + streamId));
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
var streamId = data[2];
|
||||
var binaryStream = self.streams[streamId];
|
||||
if(binaryStream) {
|
||||
binaryStream._onResume();
|
||||
} else {
|
||||
self.emit('error', new Error('Received `resume` message for unknown stream: ' + streamId));
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
var streamId = data[2];
|
||||
var binaryStream = self.streams[streamId];
|
||||
if(binaryStream) {
|
||||
binaryStream._onEnd();
|
||||
} else {
|
||||
self.emit('error', new Error('Received `end` message for unknown stream: ' + streamId));
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
var streamId = data[2];
|
||||
var binaryStream = self.streams[streamId];
|
||||
if(binaryStream) {
|
||||
binaryStream._onClose();
|
||||
} else {
|
||||
self.emit('error', new Error('Received `close` message for unknown stream: ' + streamId));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
self.emit('error', new Error('Unrecognized message type received: ' + data[0]));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
util.inherits(BinaryClient, EventEmitter);
|
||||
|
||||
BinaryClient.prototype.send = function(data, meta){
|
||||
var stream = this.createStream(meta);
|
||||
if(data instanceof Stream) {
|
||||
data.pipe(stream);
|
||||
} else if (util.isNode === true) {
|
||||
if(Buffer.isBuffer(data)) {
|
||||
(new BufferReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream);
|
||||
} else {
|
||||
stream.write(data);
|
||||
}
|
||||
} else if (util.isNode !== true) {
|
||||
if(data.constructor == Blob || data.constructor == File) {
|
||||
(new BlobReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream);
|
||||
} else if (data.constructor == ArrayBuffer) {
|
||||
var blob;
|
||||
if(binaryFeatures.useArrayBufferView) {
|
||||
data = new Uint8Array(data);
|
||||
}
|
||||
if(binaryFeatures.useBlobBuilder) {
|
||||
var builder = new BlobBuilder();
|
||||
builder.append(data);
|
||||
blob = builder.getBlob()
|
||||
} else {
|
||||
blob = new Blob([data]);
|
||||
}
|
||||
(new BlobReadStream(blob, {chunkSize: this._options.chunkSize})).pipe(stream);
|
||||
} else if (typeof data === 'object' && 'BYTES_PER_ELEMENT' in data) {
|
||||
var blob;
|
||||
if(!binaryFeatures.useArrayBufferView) {
|
||||
// Warn
|
||||
data = data.buffer;
|
||||
}
|
||||
if(binaryFeatures.useBlobBuilder) {
|
||||
var builder = new BlobBuilder();
|
||||
builder.append(data);
|
||||
blob = builder.getBlob()
|
||||
} else {
|
||||
blob = new Blob([data]);
|
||||
}
|
||||
(new BlobReadStream(blob, {chunkSize: this._options.chunkSize})).pipe(stream);
|
||||
} else {
|
||||
stream.write(data);
|
||||
}
|
||||
}
|
||||
return stream;
|
||||
};
|
||||
|
||||
BinaryClient.prototype._receiveStream = function(streamId){
|
||||
var self = this;
|
||||
var binaryStream = new BinaryStream(this._socket, streamId, false);
|
||||
binaryStream.on('close', function(){
|
||||
delete self.streams[streamId];
|
||||
});
|
||||
this.streams[streamId] = binaryStream;
|
||||
return binaryStream;
|
||||
};
|
||||
|
||||
BinaryClient.prototype.createStream = function(meta){
|
||||
if(this._socket.readyState !== WebSocket.OPEN) {
|
||||
throw new Error('Client is not yet connected or has closed');
|
||||
return;
|
||||
}
|
||||
var self = this;
|
||||
var streamId = this._nextId;
|
||||
this._nextId += 2;
|
||||
var binaryStream = new BinaryStream(this._socket, streamId, true, meta);
|
||||
binaryStream.on('close', function(){
|
||||
delete self.streams[streamId];
|
||||
});
|
||||
this.streams[streamId] = binaryStream;
|
||||
return binaryStream;
|
||||
};
|
||||
|
||||
BinaryClient.prototype.close = BinaryClient.prototype.destroy = function() {
|
||||
this._socket.close();
|
||||
};
|
||||
|
||||
exports.BinaryClient = BinaryClient;
|
305
node_modules/binaryjs/lib/client/blob_stream.js
generated
vendored
Normal file
305
node_modules/binaryjs/lib/client/blob_stream.js
generated
vendored
Normal file
@ -0,0 +1,305 @@
|
||||
function BlobReadStream(source, options){
|
||||
Stream.call(this);
|
||||
|
||||
options = util.extend({
|
||||
readDelay: 0,
|
||||
paused: false
|
||||
}, options);
|
||||
|
||||
this._source = source;
|
||||
this._start = 0;
|
||||
this._readChunkSize = options.chunkSize || source.size;
|
||||
this._readDelay = options.readDelay;
|
||||
|
||||
this.readable = true;
|
||||
this.paused = options.paused;
|
||||
|
||||
this._read();
|
||||
}
|
||||
|
||||
util.inherits(BlobReadStream, Stream);
|
||||
|
||||
|
||||
BlobReadStream.prototype.pause = function(){
|
||||
this.paused = true;
|
||||
};
|
||||
|
||||
BlobReadStream.prototype.resume = function(){
|
||||
this.paused = false;
|
||||
this._read();
|
||||
};
|
||||
|
||||
BlobReadStream.prototype.destroy = function(){
|
||||
this.readable = false;
|
||||
clearTimeout(this._timeoutId);
|
||||
};
|
||||
|
||||
BlobReadStream.prototype._read = function(){
|
||||
|
||||
var self = this;
|
||||
|
||||
function emitReadChunk(){
|
||||
self._emitReadChunk();
|
||||
}
|
||||
|
||||
var readDelay = this._readDelay;
|
||||
if (readDelay !== 0){
|
||||
this._timeoutId = setTimeout(emitReadChunk, readDelay);
|
||||
} else {
|
||||
util.setZeroTimeout(emitReadChunk);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
BlobReadStream.prototype._emitReadChunk = function(){
|
||||
|
||||
if(this.paused || !this.readable) return;
|
||||
|
||||
var chunkSize = Math.min(this._source.size - this._start, this._readChunkSize);
|
||||
|
||||
if(chunkSize === 0){
|
||||
this.readable = false;
|
||||
this.emit("end");
|
||||
return;
|
||||
}
|
||||
|
||||
var sourceEnd = this._start + chunkSize;
|
||||
var chunk = (this._source.slice || this._source.webkitSlice || this._source.mozSlice).call(this._source, this._start, sourceEnd);
|
||||
|
||||
this._start = sourceEnd;
|
||||
this._read();
|
||||
|
||||
this.emit("data", chunk);
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
|
||||
|
||||
|
||||
function BlobWriteStream(options){
|
||||
|
||||
stream.Stream.call(this);
|
||||
|
||||
options = _.extend({
|
||||
onFull: onFull,
|
||||
onEnd: function(){},
|
||||
minBlockAllocSize: 0,
|
||||
drainDelay:0
|
||||
}, options);
|
||||
|
||||
this._onFull = options.onFull;
|
||||
this._onEnd = options.onEnd;
|
||||
this._onWrite = options.onWrite;
|
||||
|
||||
this._minBlockAllocSize = options.minBlockAllocSize;
|
||||
this._maxBlockAllocSize = options.maxBlockAllocSize;
|
||||
this._drainDelay = options.drainDelay;
|
||||
|
||||
this._buffer = new Buffer(options.minBlockAllocSize);
|
||||
this._destination = this._buffer;
|
||||
this._destinationPos = 0;
|
||||
|
||||
this._writeQueue = [];
|
||||
this._pendingOnFull = false;
|
||||
this._pendingQueueDrain = false;
|
||||
|
||||
this.writable = true;
|
||||
this.bytesWritten = 0;
|
||||
}
|
||||
|
||||
util.inherits(BlobWriteStream, stream.Stream);
|
||||
|
||||
BlobWriteStream.prototype.getBuffer = function(){
|
||||
return this._buffer;
|
||||
};
|
||||
|
||||
BlobWriteStream.prototype.write = function(data, encoding){
|
||||
|
||||
if(!this.writable){
|
||||
throw new Error("stream is not writable");
|
||||
}
|
||||
|
||||
if(!Buffer.isBuffer(data)){
|
||||
data = new Buffer(data, encoding);
|
||||
}
|
||||
|
||||
if(data.length){
|
||||
this._writeQueue.push(data);
|
||||
}
|
||||
|
||||
this._commit();
|
||||
|
||||
return this._writeQueue.length === 0;
|
||||
};
|
||||
|
||||
BlobWriteStream.prototype._commit = function(){
|
||||
|
||||
var self = this;
|
||||
|
||||
var destination = this._destination;
|
||||
var writeQueue = this._writeQueue;
|
||||
|
||||
var startDestinationPos = this._destinationPos;
|
||||
|
||||
while(writeQueue.length && destination.length){
|
||||
|
||||
var head = writeQueue[0];
|
||||
|
||||
var copySize = Math.min(destination.length, head.length);
|
||||
|
||||
head.copy(destination, 0, 0, copySize);
|
||||
|
||||
head = head.slice(copySize);
|
||||
destination = destination.slice(copySize);
|
||||
|
||||
this.bytesWritten += copySize;
|
||||
this._destinationPos += copySize;
|
||||
|
||||
if(head.length === 0){
|
||||
writeQueue.shift();
|
||||
}
|
||||
else{
|
||||
writeQueue[0] = head;
|
||||
}
|
||||
}
|
||||
|
||||
this._destination = destination;
|
||||
|
||||
bytesCommitted = this._destinationPos - startDestinationPos;
|
||||
if(bytesCommitted){
|
||||
if(this._onWrite){
|
||||
|
||||
if(writeQueue.length){
|
||||
this._pendingQueueDrain = true;
|
||||
}
|
||||
|
||||
// By locking destination the buffer is frozen and the onWrite
|
||||
// callback cannot miss any write commits
|
||||
this._destination = emptyBuffer;
|
||||
|
||||
var consumer = this._onWrite;
|
||||
this._onWrite = null;
|
||||
|
||||
consumer.call(this, function(nextCallback){
|
||||
util.setZeroTimeout(function(){
|
||||
self._destination = destination;
|
||||
self._onWrite = nextCallback;
|
||||
self._commit();
|
||||
});
|
||||
}, consumer);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(writeQueue.length){
|
||||
|
||||
this._pendingQueueDrain = true;
|
||||
this._growBuffer();
|
||||
}
|
||||
else if(this._pendingQueueDrain){
|
||||
|
||||
this._pendingQueueDrain = false;
|
||||
|
||||
if(this._drainDelay !== 0){
|
||||
setTimeout(function(){
|
||||
self.emit("drain");
|
||||
}, this._drainDelay);
|
||||
}
|
||||
else{
|
||||
util.setZeroTimeout(function(){
|
||||
self.emit("drain");
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BlobWriteStream.prototype._growBuffer = function(){
|
||||
|
||||
var self = this;
|
||||
var writeQueue = this._writeQueue;
|
||||
|
||||
var requestSize = this._minBlockAllocSize;
|
||||
|
||||
var maxBlockAllocSize = this._maxBlockAllocSize;
|
||||
var add = (maxBlockAllocSize === undefined ? function(a, b){return a + b;} : function(a, b){return Math.min(a + b, maxBlockAllocSize);});
|
||||
|
||||
for(var i = 0, queueLength = writeQueue.length; i < queueLength; i++){
|
||||
requestSize = add(requestSize, writeQueue[i].length);
|
||||
}
|
||||
|
||||
// Prevent concurrent onFull callbacks
|
||||
if(this._pendingOnFull){
|
||||
return;
|
||||
}
|
||||
this._pendingOnFull = true;
|
||||
|
||||
this._onFull(this._buffer, requestSize, function(buffer, destination){
|
||||
util.setZeroTimeout(function(){
|
||||
|
||||
self._pendingOnFull = false;
|
||||
|
||||
if(!destination){
|
||||
if(self.writable){
|
||||
self.emit("error", new Error("buffer is full"));
|
||||
}
|
||||
self.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
self._buffer = buffer;
|
||||
self._destination = destination;
|
||||
|
||||
self._commit();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
BlobWriteStream.prototype.end = function(data, encoding){
|
||||
|
||||
var self = this;
|
||||
|
||||
function _end(){
|
||||
self.writable = false;
|
||||
self._onEnd();
|
||||
}
|
||||
|
||||
if(data){
|
||||
if(this.write(data, encoding)){
|
||||
_end();
|
||||
}else{
|
||||
self.writable = false;
|
||||
this.once("drain", _end);
|
||||
}
|
||||
}
|
||||
else{
|
||||
_end();
|
||||
}
|
||||
};
|
||||
|
||||
BlobWriteStream.prototype.destroy = function(){
|
||||
this.writable = false;
|
||||
this._pendingQueueDrain = false;
|
||||
this._writeQueue = [];
|
||||
};
|
||||
|
||||
BlobWriteStream.prototype.consume = function(consume){
|
||||
|
||||
this._buffer = this._buffer.slice(consume);
|
||||
this._destinationPos -= consume;
|
||||
};
|
||||
|
||||
BlobWriteStream.prototype.getCommittedSlice = function(){
|
||||
return this._buffer.slice(0, this._destinationPos);
|
||||
};
|
||||
|
||||
function onFull(buffer, extraSize, callback){
|
||||
var newBuffer = new Buffer(buffer.length + extraSize);
|
||||
buffer.copy(newBuffer);
|
||||
callback(newBuffer, newBuffer.slice(buffer.length));
|
||||
}
|
||||
*/
|
||||
exports.BlobReadStream = BlobReadStream;
|
||||
exports.BlobWriteStream = BlobWriteStream;
|
92
node_modules/binaryjs/lib/client/stream.js
generated
vendored
Normal file
92
node_modules/binaryjs/lib/client/stream.js
generated
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
|
||||
|
||||
function Stream() {
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
|
||||
util.inherits(Stream, EventEmitter);
|
||||
|
||||
Stream.prototype.pipe = function(dest, options) {
|
||||
var source = this;
|
||||
|
||||
function ondata(chunk) {
|
||||
if (dest.writable) {
|
||||
if (false === dest.write(chunk) && source.pause) {
|
||||
source.pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source.on('data', ondata);
|
||||
|
||||
function ondrain() {
|
||||
if (source.readable && source.resume) {
|
||||
source.resume();
|
||||
}
|
||||
}
|
||||
|
||||
dest.on('drain', ondrain);
|
||||
|
||||
// If the 'end' option is not supplied, dest.end() will be called when
|
||||
// source gets the 'end' or 'close' events. Only dest.end() once.
|
||||
if (!dest._isStdio && (!options || options.end !== false)) {
|
||||
source.on('end', onend);
|
||||
source.on('close', onclose);
|
||||
}
|
||||
|
||||
var didOnEnd = false;
|
||||
function onend() {
|
||||
if (didOnEnd) return;
|
||||
didOnEnd = true;
|
||||
|
||||
dest.end();
|
||||
}
|
||||
|
||||
|
||||
function onclose() {
|
||||
if (didOnEnd) return;
|
||||
didOnEnd = true;
|
||||
|
||||
dest.destroy();
|
||||
}
|
||||
|
||||
// don't leave dangling pipes when there are errors.
|
||||
function onerror(er) {
|
||||
cleanup();
|
||||
if (this.listeners('error').length === 0) {
|
||||
throw er; // Unhandled stream error in pipe.
|
||||
}
|
||||
}
|
||||
|
||||
source.on('error', onerror);
|
||||
dest.on('error', onerror);
|
||||
|
||||
// remove all the event listeners that were added.
|
||||
function cleanup() {
|
||||
source.removeListener('data', ondata);
|
||||
dest.removeListener('drain', ondrain);
|
||||
|
||||
source.removeListener('end', onend);
|
||||
source.removeListener('close', onclose);
|
||||
|
||||
source.removeListener('error', onerror);
|
||||
dest.removeListener('error', onerror);
|
||||
|
||||
source.removeListener('end', cleanup);
|
||||
source.removeListener('close', cleanup);
|
||||
|
||||
dest.removeListener('end', cleanup);
|
||||
dest.removeListener('close', cleanup);
|
||||
}
|
||||
|
||||
source.on('end', cleanup);
|
||||
source.on('close', cleanup);
|
||||
|
||||
dest.on('end', cleanup);
|
||||
dest.on('close', cleanup);
|
||||
|
||||
dest.emit('pipe', source);
|
||||
|
||||
// Allow for unix-like usage: A.pipe(B).pipe(C)
|
||||
return dest;
|
||||
};
|
76
node_modules/binaryjs/lib/server.js
generated
vendored
Normal file
76
node_modules/binaryjs/lib/server.js
generated
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
var ws = require('streamws');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var util = require('./util');
|
||||
|
||||
var BinaryClient = require('./client').BinaryClient;
|
||||
|
||||
function BinaryServer(options) {
|
||||
if (!(this instanceof BinaryServer)) return new BinaryServer(options);
|
||||
|
||||
var self = this;
|
||||
|
||||
options = util.extend({
|
||||
host: '0.0.0.0',
|
||||
chunkSize: 40960
|
||||
}, options);
|
||||
|
||||
this.clients = {};
|
||||
this._clientCounter = 0;
|
||||
|
||||
|
||||
if (options.server && (options.server instanceof ws.Server)) {
|
||||
this._server = options.server;
|
||||
} else {
|
||||
this._server = new ws.Server(options);
|
||||
}
|
||||
|
||||
this._server.on('connection', function(socket){
|
||||
var clientId = self._clientCounter;
|
||||
var binaryClient = new BinaryClient(socket, options);
|
||||
binaryClient.id = clientId;
|
||||
self.clients[clientId] = binaryClient;
|
||||
self._clientCounter++;
|
||||
binaryClient.on('close', function(){
|
||||
delete self.clients[clientId];
|
||||
});
|
||||
self.emit('connection', binaryClient);
|
||||
});
|
||||
this._server.on('error', function(error){
|
||||
self.emit('error', error);
|
||||
});
|
||||
}
|
||||
|
||||
util.inherits(BinaryServer, EventEmitter);
|
||||
|
||||
BinaryServer.prototype.close = function(code, message){
|
||||
this._server.close(code, message);
|
||||
}
|
||||
|
||||
exports.BinaryClient = BinaryClient;
|
||||
exports.BinaryServer = BinaryServer;
|
||||
|
||||
// Expose a method similar to http.createServer
|
||||
//
|
||||
// usage:
|
||||
// var fs = require('fs');
|
||||
// require('binaryjs').createServer(function(c) {
|
||||
// fs.createReadStream('some.bin').pipe(c);
|
||||
// });
|
||||
//
|
||||
exports.createServer = function(fn) {
|
||||
var server;
|
||||
|
||||
return {
|
||||
listen : function(port, opts) {
|
||||
opts = opts || {};
|
||||
opts.port = port;
|
||||
|
||||
var server = new BinaryServer(opts);
|
||||
server.on('connection', function(conn) {
|
||||
fn && fn(conn.createStream());
|
||||
});
|
||||
|
||||
return server;
|
||||
}
|
||||
}
|
||||
};
|
128
node_modules/binaryjs/lib/stream.js
generated
vendored
Normal file
128
node_modules/binaryjs/lib/stream.js
generated
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
// if node
|
||||
var Stream = require('stream').Stream;
|
||||
var util = require('./util');
|
||||
// end node
|
||||
|
||||
function BinaryStream(socket, id, create, meta) {
|
||||
if (!(this instanceof BinaryStream)) return new BinaryStream(options);
|
||||
|
||||
var self = this;
|
||||
|
||||
Stream.call(this);
|
||||
|
||||
|
||||
this.id = id;
|
||||
this._socket = socket;
|
||||
|
||||
this.writable = true;
|
||||
this.readable = true;
|
||||
this.paused = false;
|
||||
|
||||
this._closed = false;
|
||||
this._ended = false;
|
||||
|
||||
if(create) {
|
||||
// This is a stream we are creating
|
||||
this._write(1, meta, this.id);
|
||||
}
|
||||
}
|
||||
|
||||
util.inherits(BinaryStream, Stream);
|
||||
|
||||
|
||||
BinaryStream.prototype._onDrain = function() {
|
||||
if(!this.paused) {
|
||||
this.emit('drain');
|
||||
}
|
||||
};
|
||||
|
||||
BinaryStream.prototype._onClose = function() {
|
||||
// Emit close event
|
||||
if (this._closed) {
|
||||
return;
|
||||
}
|
||||
this.readable = false;
|
||||
this.writable = false;
|
||||
this._closed = true;
|
||||
this.emit('close');
|
||||
};
|
||||
|
||||
BinaryStream.prototype._onError = function(error){
|
||||
this.readable = false;
|
||||
this.writable = false;
|
||||
this.emit('error', error);
|
||||
};
|
||||
|
||||
// Write stream
|
||||
|
||||
BinaryStream.prototype._onPause = function() {
|
||||
// Emit pause event
|
||||
this.paused = true;
|
||||
this.emit('pause');
|
||||
};
|
||||
|
||||
BinaryStream.prototype._onResume = function() {
|
||||
// Emit resume event
|
||||
this.paused = false;
|
||||
this.emit('resume');
|
||||
this.emit('drain');
|
||||
};
|
||||
|
||||
BinaryStream.prototype._write = function(code, data, bonus) {
|
||||
if (this._socket.readyState !== this._socket.constructor.OPEN) {
|
||||
return false;
|
||||
}
|
||||
var message = util.pack([code, data, bonus]);
|
||||
return this._socket.send(message) !== 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');
|
||||
}
|
||||
};
|
||||
|
||||
BinaryStream.prototype.end = function() {
|
||||
this._ended = true;
|
||||
this.readable = false;
|
||||
this._write(5, null, this.id);
|
||||
};
|
||||
|
||||
BinaryStream.prototype.destroy = BinaryStream.prototype.destroySoon = function() {
|
||||
this._onClose();
|
||||
this._write(6, null, this.id);
|
||||
};
|
||||
|
||||
|
||||
// Read stream
|
||||
|
||||
BinaryStream.prototype._onEnd = function() {
|
||||
if(this._ended) {
|
||||
return;
|
||||
}
|
||||
this._ended = true;
|
||||
this.readable = false;
|
||||
this.emit('end');
|
||||
};
|
||||
|
||||
BinaryStream.prototype._onData = function(data) {
|
||||
// Dispatch
|
||||
this.emit('data', data);
|
||||
};
|
||||
|
||||
BinaryStream.prototype.pause = function() {
|
||||
this._onPause();
|
||||
this._write(3, null, this.id);
|
||||
};
|
||||
|
||||
BinaryStream.prototype.resume = function() {
|
||||
this._onResume();
|
||||
this._write(4, null, this.id);
|
||||
};
|
||||
|
||||
// if node
|
||||
exports.BinaryStream = BinaryStream;
|
||||
// end node
|
68
node_modules/binaryjs/lib/util.js
generated
vendored
Normal file
68
node_modules/binaryjs/lib/util.js
generated
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
// if node
|
||||
var BinaryPack = require('binarypack');
|
||||
// end node
|
||||
|
||||
var util = {
|
||||
inherits: function(ctor, superCtor) {
|
||||
ctor.super_ = superCtor;
|
||||
ctor.prototype = Object.create(superCtor.prototype, {
|
||||
constructor: {
|
||||
value: ctor,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
},
|
||||
extend: function(dest, source) {
|
||||
for(var key in source) {
|
||||
if(source.hasOwnProperty(key)) {
|
||||
dest[key] = source[key];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
},
|
||||
pack: BinaryPack.pack,
|
||||
unpack: BinaryPack.unpack,
|
||||
// if node
|
||||
isNode: true,
|
||||
// end node
|
||||
setZeroTimeout: (function(global) {
|
||||
// if node
|
||||
|
||||
return process.nextTick;
|
||||
|
||||
// end node
|
||||
var timeouts = [];
|
||||
var messageName = 'zero-timeout-message';
|
||||
|
||||
// Like setTimeout, but only takes a function argument. There's
|
||||
// no time argument (always zero) and no arguments (you have to
|
||||
// use a closure).
|
||||
function setZeroTimeoutPostMessage(fn) {
|
||||
timeouts.push(fn);
|
||||
global.postMessage(messageName, '*');
|
||||
}
|
||||
|
||||
function handleMessage(event) {
|
||||
if (event.source == global && event.data == messageName) {
|
||||
if (event.stopPropagation) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
if (timeouts.length) {
|
||||
timeouts.shift()();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (global.addEventListener) {
|
||||
global.addEventListener('message', handleMessage, true);
|
||||
} else if (global.attachEvent) {
|
||||
global.attachEvent('onmessage', handleMessage);
|
||||
}
|
||||
return setZeroTimeoutPostMessage;
|
||||
}(this))
|
||||
};
|
||||
|
||||
// if node
|
||||
module.exports = util;
|
||||
// end node
|
Reference in New Issue
Block a user