socket.io-stream
Advanced tools
Comparing version 0.5.3 to 0.6.0
@@ -1,3 +0,4 @@ | ||
var util = require('util') | ||
, Readable = require('readable-stream').Readable; | ||
var util = require('util'); | ||
var Readable = require('stream').Readable; | ||
var bind = require('component-bind'); | ||
@@ -27,13 +28,13 @@ | ||
var fileReader = this.fileReader = new FileReader(); | ||
fileReader.onload = this._onload.bind(this); | ||
fileReader.onerror = this._onerror.bind(this); | ||
fileReader.onload = bind(this, '_onload'); | ||
fileReader.onerror = bind(this, '_onerror'); | ||
} | ||
BlobReadStream.prototype._read = function(size) { | ||
var start = this.start | ||
, end = this.start = this.start + size | ||
, chunk = this.slice.call(this.blob, start, end); | ||
var start = this.start; | ||
var end = this.start = this.start + size; | ||
var chunk = this.slice.call(this.blob, start, end); | ||
if (chunk.size) { | ||
this.fileReader.readAsDataURL(chunk); | ||
this.fileReader.readAsArrayBuffer(chunk); | ||
} else { | ||
@@ -45,5 +46,4 @@ this.push(null); | ||
BlobReadStream.prototype._onload = function(e) { | ||
// parse data from DataURL. | ||
var chunk = e.target.result.match(/,(.*)$/)[1]; | ||
this.push(chunk, 'base64'); | ||
var chunk = new Buffer(new Uint8Array(e.target.result)); | ||
this.push(chunk); | ||
}; | ||
@@ -50,0 +50,0 @@ |
@@ -1,4 +0,4 @@ | ||
var Socket = require('./socket') | ||
, IOStream = require('./iostream') | ||
, BlobReadStream = require('./blob-read-stream'); | ||
var Socket = require('./socket'); | ||
var IOStream = require('./iostream'); | ||
var BlobReadStream = require('./blob-read-stream'); | ||
@@ -16,11 +16,24 @@ | ||
/** | ||
* Forces base 64 encoding when emitting. Must be set to true for Socket.IO v0.9 or lower. | ||
* | ||
* @api public | ||
*/ | ||
exports.forceBase64 = false; | ||
/** | ||
* Look up an existing Socket. | ||
* | ||
* @param {socket.io#Socket} socket.io | ||
* @param {Object} options | ||
* @return {Socket} Socket instance | ||
* @api public | ||
*/ | ||
function lookup(sio) { | ||
function lookup(sio, options) { | ||
options = options || {}; | ||
if (null == options.forceBase64) { | ||
options.forceBase64 = exports.forceBase64; | ||
} | ||
if (!sio._streamSocket) { | ||
sio._streamSocket = new Socket(sio); | ||
sio._streamSocket = new Socket(sio, options); | ||
} | ||
@@ -27,0 +40,0 @@ return sio._streamSocket; |
@@ -1,4 +0,5 @@ | ||
var util = require('util') | ||
, Duplex = require('readable-stream').Duplex | ||
, debug = require('debug')('socket.io-stream:iostream'); | ||
var util = require('util'); | ||
var Duplex = require('stream').Duplex; | ||
var bind = require('component-bind'); | ||
var debug = require('debug')('socket.io-stream:iostream'); | ||
@@ -166,3 +167,3 @@ | ||
// end after flushing buffer. | ||
this.pushBuffer.push(this._done.bind(this)); | ||
this.pushBuffer.push(bind(this, '_done')); | ||
} else { | ||
@@ -169,0 +170,0 @@ this._done(); |
@@ -1,9 +0,10 @@ | ||
var util = require('util') | ||
, EventEmitter = require('events').EventEmitter | ||
, IOStream = require('./iostream') | ||
, uuid = require('./uuid') | ||
, debug = require('debug')('socket.io-stream:socket') | ||
, emit = EventEmitter.prototype.emit | ||
, on = EventEmitter.prototype.on | ||
, slice = Array.prototype.slice; | ||
var util = require('util'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var IOStream = require('./iostream'); | ||
var uuid = require('./uuid'); | ||
var bind = require('component-bind'); | ||
var debug = require('debug')('socket.io-stream:socket'); | ||
var emit = EventEmitter.prototype.emit; | ||
var on = EventEmitter.prototype.on; | ||
var slice = Array.prototype.slice; | ||
@@ -18,6 +19,8 @@ | ||
*/ | ||
exports.event = 'stream'; | ||
exports.event = '$stream'; | ||
exports.events = [ | ||
'error' | ||
'error', | ||
'newListener', | ||
'removeListener' | ||
]; | ||
@@ -33,5 +36,5 @@ | ||
*/ | ||
function Socket(sio) { | ||
function Socket(sio, options) { | ||
if (!(this instanceof Socket)) { | ||
return new Socket(sio); | ||
return new Socket(sio, options); | ||
} | ||
@@ -41,13 +44,16 @@ | ||
options = options || {}; | ||
this.sio = sio; | ||
this.forceBase64 = !!options.forceBase64; | ||
this.streams = {}; | ||
var eventName = exports.event; | ||
sio.on(eventName, emit.bind(this)); | ||
sio.on(eventName + '-read', this._onread.bind(this)); | ||
sio.on(eventName + '-write', this._onwrite.bind(this)); | ||
sio.on(eventName + '-end', this._onend.bind(this)); | ||
sio.on(eventName + '-error', this._onerror.bind(this)); | ||
sio.on('error', emit.bind(this, 'error')); | ||
sio.on('disconnect', this._ondisconnect.bind(this)); | ||
sio.on(eventName, bind(this, emit)); | ||
sio.on(eventName + '-read', bind(this, '_onread')); | ||
sio.on(eventName + '-write', bind(this, '_onwrite')); | ||
sio.on(eventName + '-end', bind(this, '_onend')); | ||
sio.on(eventName + '-error', bind(this, '_onerror')); | ||
sio.on('error', bind(this, emit, 'error')); | ||
sio.on('disconnect', bind(this, '_ondisconnect')); | ||
} | ||
@@ -142,4 +148,9 @@ | ||
Socket.prototype._write = function(id, chunk, encoding, callback) { | ||
encoding = 'base64'; | ||
chunk = chunk.toString(encoding); | ||
if (this.forceBase64) { | ||
encoding = 'base64'; | ||
chunk = chunk.toString(encoding); | ||
} else if (Buffer.isBuffer(chunk) && !global.Buffer) { | ||
// socket.io can't handle Buffer when using browserify. | ||
chunk = chunk.toArrayBuffer(); | ||
} | ||
this.sio.emit(exports.event + '-write', id, chunk, encoding, callback); | ||
@@ -215,7 +226,12 @@ }; | ||
var stream = this.streams[id]; | ||
if (stream) { | ||
stream._onwrite(chunk, encoding, callback); | ||
} else { | ||
if (!stream) { | ||
this._error(id, 'invalid stream id'); | ||
return; | ||
} | ||
if (global.ArrayBuffer && chunk instanceof ArrayBuffer) { | ||
// make sure that chunk is a buffer for stream | ||
chunk = new Buffer(new Uint8Array(chunk)); | ||
} | ||
stream._onwrite(chunk, encoding, callback); | ||
}; | ||
@@ -227,7 +243,8 @@ | ||
var stream = this.streams[id]; | ||
if (stream) { | ||
stream._end(); | ||
} else { | ||
if (!stream) { | ||
this._error(id, 'invalid stream id'); | ||
return; | ||
} | ||
stream._end(); | ||
}; | ||
@@ -239,9 +256,10 @@ | ||
var stream = this.streams[id]; | ||
if (stream) { | ||
var err = new Error(message); | ||
err.remote = true; | ||
stream.emit('error', err); | ||
} else { | ||
if (!stream) { | ||
debug('invalid stream id: "%s"', id); | ||
return; | ||
} | ||
var err = new Error(message); | ||
err.remote = true; | ||
stream.emit('error', err); | ||
}; | ||
@@ -248,0 +266,0 @@ |
{ | ||
"name": "socket.io-stream", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"description": "stream for socket.io", | ||
@@ -17,3 +17,8 @@ "author": "Naoyuki Kanezawa <naoyuki.kanezawa@gmail.com>", | ||
"keywords": [ | ||
"stream", "socket.io", "binary", "file", "upload", "download" | ||
"stream", | ||
"socket.io", | ||
"binary", | ||
"file", | ||
"upload", | ||
"download" | ||
], | ||
@@ -25,21 +30,17 @@ "repository": { | ||
"scripts": { | ||
"test": "make test-all" | ||
"test": "make test" | ||
}, | ||
"dependencies": { | ||
"readable-stream": "1.1.9", | ||
"debug": "*" | ||
"component-bind": "~1.0.0", | ||
"debug": "~2.1.0" | ||
}, | ||
"devDependencies": { | ||
"socket.io": "0.9.16", | ||
"socket.io-client": "0.9.16", | ||
"mocha": "*", | ||
"chai": "*", | ||
"async": "*", | ||
"checksum": "*", | ||
"browserify": "2.36.1", | ||
"uglify-js": "*", | ||
"phantomjs": "*", | ||
"mocha-phantomjs": "*", | ||
"node-static": "*" | ||
"blob": "0.0.4", | ||
"browserify": "~6.1.0", | ||
"expect.js": "~0.3.1", | ||
"mocha": "~1.21.5", | ||
"socket.io": "~1.1.0", | ||
"socket.io-client": "~1.1.0", | ||
"zuul": "~1.11.2" | ||
} | ||
} |
@@ -11,2 +11,5 @@ # Socket.IO stream | ||
## Usage | ||
If you are not familiar with Stream API, be sure to check out [the docs](http://nodejs.org/api/stream.html). | ||
I also recommend checking out the awesome [Stream Handbook](https://github.com/substack/stream-handbook): | ||
For streaming between servers and clients, you must send stream instances first. | ||
@@ -46,2 +49,13 @@ To receive streams, you just wrap `socket` with `socket.io-stream`, then listen any events as usual. | ||
```js | ||
// send data | ||
ss(socket).on('file', function(stream) { | ||
fs.createReadStream('/path/to/file').pipe(stream); | ||
}); | ||
// receive data | ||
ss(socket).emit('file', stream); | ||
stream.pipe(fs.createWriteStream('file.txt')); | ||
``` | ||
### Browser | ||
@@ -90,3 +104,3 @@ This module can be used on the browser. To do so, just copy a file to a public directory. | ||
console.log(Math.floor(size / file.size * 100) + '%'); | ||
// e.g. '42%' | ||
// -> e.g. '42%' | ||
}); | ||
@@ -97,21 +111,75 @@ | ||
### Supporting Socket.IO 0.9 | ||
You have set `forceBase64` option `true` when using on socket.io v0.9.x. | ||
```js | ||
ss.forceBase64 = true; | ||
``` | ||
## Documentation | ||
### ss(sio) | ||
- sio `socket.io Socket` A socket of Socket.IO, both for client and server | ||
- return `Socket` | ||
Look up an existing `Socket` instance based on `sio` (a socket of Socket.IO), or create one if it doesn't exist. | ||
### socket.emit(event, ...) | ||
### socket.emit(event, [arg1], [arg2], [...]) | ||
- event `String` The event name | ||
Emit an `event` with variable args including at least a stream. | ||
### socket.on(event, [options], [listener]) | ||
```js | ||
ss(socket).emit('myevent', stream, {name: 'thefilename'}, function() { ... }); | ||
// send some streams at a time. | ||
ss(socket).emit('multiple-streams', stream1, stream2); | ||
``` | ||
### socket.on(event, [options], listener) | ||
- event `String` The event name | ||
- options `Object` options for received Streams | ||
- highWaterMark `Number` | ||
- encoding `String` | ||
- decodeStrings `Boolean` | ||
- objectMode `Boolean` | ||
- listener `Function` The event handler function | ||
Add a `listener` for `event`. `listener` will take streams with any data as arguments. `options` is an object for streams. | ||
```js | ||
ss(socket).on('myevent', function(stream, data, callback) { ... }); | ||
// with options | ||
ss(socket).on('any', {highWaterMark: 64 * 1024}, function(stream) { ... }); | ||
``` | ||
### ss.createStream([options]) | ||
- options `Object` | ||
- highWaterMark `Number` | ||
- encoding `String` | ||
- decodeStrings `Boolean` | ||
- objectMode `Boolean` | ||
- return `Duplex Stream` | ||
Create a new duplex stream. See [the docs](http://nodejs.org/api/stream.html) for the details of stream and `options`. | ||
```js | ||
var stream = ss.createStream(); | ||
``` | ||
### ss.createBlobReadStream(blob, [options]) | ||
Create a new readable stream for [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and [File](https://developer.mozilla.org/en-US/docs/Web/API/File). See [the docs](http://nodejs.org/api/stream.html) for the details of stream and `options`. | ||
- options `Object` | ||
- highWaterMark `Number` | ||
- encoding `String` | ||
- objectMode `Boolean` | ||
- return `Readable Stream` | ||
Create a new readable stream for [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and [File](https://developer.mozilla.org/en-US/docs/Web/API/File) on browser. See [the docs](http://nodejs.org/api/stream.html) for the details of stream and `options`. | ||
```js | ||
var stream = ss.createBlobReadStream(new Blob([1, 2, 3])); | ||
``` | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
178031
7
13
5358
2
0
182
4
1
+ Addedcomponent-bind@~1.0.0
+ Addedcomponent-bind@1.0.0(transitive)
+ Addeddebug@2.1.3(transitive)
+ Addedms@0.7.0(transitive)
- Removedreadable-stream@1.1.9
- Removedcore-util-is@1.0.3(transitive)
- Removeddebug@4.4.0(transitive)
- Removeddebuglog@0.0.2(transitive)
- Removedms@2.1.3(transitive)
- Removedreadable-stream@1.1.9(transitive)
Updateddebug@~2.1.0