socket.io-stream
Advanced tools
var Socket = require('./socket') | ||
, WriteStream = require('./write-stream') | ||
, IOStream = require('./iostream') | ||
, BlobReadStream = require('./blob-read-stream'); | ||
@@ -33,7 +33,7 @@ | ||
* @param {Object} options | ||
* @return {WriteStream} writable stream | ||
* @return {IOStream} writable stream | ||
* @api public | ||
*/ | ||
exports.createStream = function(options) { | ||
return new WriteStream(options); | ||
return new IOStream(options); | ||
}; | ||
@@ -40,0 +40,0 @@ |
var util = require('util') | ||
, EventEmitter = require('events').EventEmitter | ||
, ReadStream = require('./read-stream') | ||
, WriteStream = require('./write-stream') | ||
, IOStream = require('./iostream') | ||
, debug = require('debug')('socket.io-stream:socket') | ||
, emit = EventEmitter.prototype.emit | ||
, slice = Array.prototype.slice; | ||
, slice = Array.prototype.slice | ||
, uuid = require('./uuid'); | ||
@@ -39,5 +39,3 @@ | ||
this.sio = sio; | ||
this.ids = 0; | ||
this.readStreams = {}; | ||
this.writeStreams = {}; | ||
this.streams = {}; | ||
@@ -49,4 +47,3 @@ var eventName = exports.event; | ||
sio.on(eventName + '-end', this._onend.bind(this)); | ||
sio.on(eventName + '-writeerror', this._onwriteerror.bind(this)); | ||
sio.on(eventName + '-readerror', this._onreaderror.bind(this)); | ||
sio.on(eventName + '-error', this._onerror.bind(this)); | ||
sio.on('error', this.emit.bind(this, 'error')); | ||
@@ -83,3 +80,3 @@ } | ||
args = args.map(function(stream, i) { | ||
if (!(stream instanceof WriteStream)) { | ||
if (!(stream instanceof IOStream)) { | ||
return stream; | ||
@@ -95,4 +92,5 @@ } | ||
var id = this.ids++; | ||
this.writeStreams[id] = stream; | ||
// Generate | ||
var id = uuid(); | ||
this.streams[id] = stream; | ||
stream.id = id; | ||
@@ -104,3 +102,3 @@ stream.socket = this; | ||
stream.on(type, function() { | ||
delete this.writeStreams[id]; | ||
delete this.streams[id]; | ||
}.bind(this)); | ||
@@ -141,10 +139,6 @@ }, this); | ||
Socket.prototype._writeerror = function(id, err) { | ||
this.sio.emit(exports.event + '-writeerror', id, err.message || err); | ||
Socket.prototype._error = function(id, err) { | ||
this.sio.emit(exports.event + '-error', id, err.message || err); | ||
}; | ||
Socket.prototype._readerror = function(id, err) { | ||
this.sio.emit(exports.event + '-readerror', id, err.message || err); | ||
}; | ||
/** | ||
@@ -169,4 +163,4 @@ * Handles a new stream request. | ||
if (this.readStreams[id]) { | ||
this._readerror(id, 'id already exists'); | ||
if (this.streams[id]) { | ||
this._error(id, 'id already exists'); | ||
return; | ||
@@ -176,3 +170,3 @@ } | ||
// TODO: enable to set options. | ||
var stream = this.readStreams[id] = new ReadStream(/*options*/); | ||
var stream = this.streams[id] = new IOStream(/*options*/); | ||
stream.id = id; | ||
@@ -184,3 +178,3 @@ stream.socket = this; | ||
stream.on(type, function() { | ||
delete this.readStreams[id]; | ||
delete this.streams[id]; | ||
}.bind(this)); | ||
@@ -199,7 +193,7 @@ }, this); | ||
var writeStream = this.writeStreams[id]; | ||
if (writeStream) { | ||
writeStream._read(size); | ||
var stream = this.streams[id]; | ||
if (stream) { | ||
stream._onread(size); | ||
} else { | ||
this._writeerror(id, 'invalid stream id'); | ||
this._error(id, 'invalid stream id'); | ||
} | ||
@@ -211,7 +205,7 @@ }; | ||
var readStream = this.readStreams[id]; | ||
if (readStream) { | ||
readStream._write(chunk, encoding, callback); | ||
var stream = this.streams[id]; | ||
if (stream) { | ||
stream._onwrite(chunk, encoding, callback); | ||
} else { | ||
this._readerror(id, 'invalid stream id'); | ||
this._error(id, 'invalid stream id'); | ||
} | ||
@@ -223,34 +217,21 @@ }; | ||
var readStream = this.readStreams[id]; | ||
if (readStream) { | ||
readStream._end(); | ||
var stream = this.streams[id]; | ||
if (stream) { | ||
stream._end(); | ||
} else { | ||
this._readerror(id, 'invalid stream id'); | ||
this._error(id, 'invalid stream id'); | ||
} | ||
}; | ||
Socket.prototype._onwriteerror = function(id, message) { | ||
debug('write error: "%s", "%s"', id, message); | ||
Socket.prototype._onerror = function(id, message) { | ||
debug('error: "%s", "%s"', id, message); | ||
var readStream = this.readStreams[id]; | ||
if (readStream) { | ||
var stream = this.streams[id]; | ||
if (stream) { | ||
var err = new Error(message); | ||
err.remote = true; | ||
readStream.emit('error', err); | ||
stream.emit('error', err); | ||
} else { | ||
debug('invalid read-stream id: "%s"', id); | ||
debug('invalid stream id: "%s"', id); | ||
} | ||
}; | ||
Socket.prototype._onreaderror = function(id, message) { | ||
debug('read error: "%s", "%s"', id, message); | ||
var writeStream = this.writeStreams[id]; | ||
if (writeStream) { | ||
var err = new Error(message); | ||
err.remote = true; | ||
writeStream.emit('error', err); | ||
} else { | ||
debug('invalid write-stream id: "%s"', id); | ||
} | ||
}; |
{ | ||
"name": "socket.io-stream", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "stream for socket.io", | ||
"author": "Naoyuki Kanezawa <naoyuki.kanezawa@gmail.com>", | ||
"contributors": [ | ||
{ | ||
"name": "Naoyuki Kanezawa", | ||
"email": "naoyuki.kanezawa@gmail.com" | ||
}, | ||
{ | ||
"name": "Aaron O'Mullan", | ||
"email": "aaron.omullan@friendco.de" | ||
} | ||
], | ||
"keywords": [ | ||
"stream", "socket.io", "binary", "file", "upload" | ||
"stream", "socket.io", "binary", "file", "upload", "download" | ||
], | ||
@@ -14,3 +24,3 @@ "repository": { | ||
"scripts": { | ||
"test": "make test" | ||
"test": "make test-all" | ||
}, | ||
@@ -24,3 +34,3 @@ "dependencies": { | ||
"socket.io-client": "0.9.16", | ||
"mocha": "1.9.0", | ||
"mocha": "*", | ||
"chai": "*", | ||
@@ -27,0 +37,0 @@ "async": "*", |
@@ -28,3 +28,3 @@ # Socket.IO stream | ||
`createStream()` will return a new writable stream which can be sent by `emit`. | ||
`createStream()` will return a new stream which can be sent by `emit`. | ||
@@ -91,3 +91,3 @@ Client: | ||
### ss.createStream([options]) | ||
Create a new writable stream. See [the docs](http://nodejs.org/api/stream.html) for the details of stream and `options`. | ||
Create a new duplex stream. See [the docs](http://nodejs.org/api/stream.html) for the details of stream and `options`. | ||
@@ -94,0 +94,0 @@ ### ss.createBlobReadStream(blob, [options]) |
Sorry, the diff of this file is too big to display
932
4.02%156170
-3.98%