Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

socket.io-stream

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

socket.io-stream - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

25

lib/read-stream.js

@@ -22,19 +22,10 @@ var util = require('util')

this.id = null;
this.socket = null;
this._readable = false;
this.pushBuffer = [];
this.on('error', function(err) {
if (this.socket) {
// notify the error to the corresponding write-stream.
this.socket._readerror(this.id, err);
}
}.bind(this));
this.on('error', this._onerror.bind(this));
}
/**
* values to be set from Socket.
*/
ReadStream.prototype.id;
ReadStream.prototype.socket;
ReadStream.prototype._read = function(size) {

@@ -74,3 +65,3 @@ var push;

if (this.pushBuffer.length) {
// to flush buffer before the end.
// end after flushing buffer.
this.pushBuffer.push(this._onend.bind(this));

@@ -89,1 +80,9 @@ } else {

ReadStream.prototype._onerror = function(err) {
if (!this.socket) return;
// check if the error came from the corresponding stream.
if (err.remote) return;
// notify the error to the corresponding write-stream.
this.socket._readerror(this.id, err);
};

@@ -67,3 +67,10 @@ var util = require('util')

/**
* Sends a new stream request.
*
* @api private
*/
Socket.prototype._stream = function() {
debug('sending new streams');
var args = slice.call(arguments)

@@ -79,2 +86,6 @@ , type = args.shift()

if (stream.socket) {
throw new Error('stream has already been sent.');
}
// keep stream positions of args.

@@ -103,2 +114,7 @@ pos.push(i);

/**
* Notifies the read event.
*
* @api private
*/
Socket.prototype._read = function(id, size) {

@@ -108,5 +124,11 @@ this.sio.emit(exports.event + '-read', id, size);

/**
* Requests to write a chunk.
*
* @api private
*/
Socket.prototype._write = function(id, chunk, encoding, callback) {
this.sio.emit(exports.event + '-write',
id, chunk.toString(encoding), encoding, callback);
encoding = 'base64';
chunk = chunk.toString(encoding);
this.sio.emit(exports.event + '-write', id, chunk, encoding, callback);
};

@@ -126,3 +148,10 @@

/**
* Handles a new stream request.
*
* @api private
*/
Socket.prototype._onstream = function() {
debug('new streams');
var args = slice.call(arguments)

@@ -164,2 +193,4 @@ , pos = args.shift()

Socket.prototype._onread = function(id, size) {
debug('read: "%s"', id);
var writeStream = this.writeStreams[id];

@@ -174,2 +205,4 @@ if (writeStream) {

Socket.prototype._onwrite = function(id, chunk, encoding, callback) {
debug('write: "%s"', id);
var readStream = this.readStreams[id];

@@ -184,2 +217,4 @@ if (readStream) {

Socket.prototype._onend = function(id) {
debug('end: "%s"', id);
var readStream = this.readStreams[id];

@@ -194,5 +229,9 @@ if (readStream) {

Socket.prototype._onwriteerror = function(id, message) {
debug('write error: "%s", "%s"', id, message);
var readStream = this.readStreams[id];
if (readStream) {
readStream.emit('error', new Error(message));
var err = new Error(message);
err.remote = true;
readStream.emit('error', err);
} else {

@@ -204,5 +243,9 @@ debug('invalid read-stream id: "%s"', id);

Socket.prototype._onreaderror = function(id, message) {
debug('read error: "%s", "%s"', id, message);
var writeStream = this.writeStreams[id];
if (writeStream) {
writeStream.emit('error', new Error(message));
var err = new Error(message);
err.remote = true;
writeStream.emit('error', err);
} else {

@@ -209,0 +252,0 @@ debug('invalid write-stream id: "%s"', id);

@@ -22,2 +22,4 @@ var util = require('util')

this.id = null;
this.socket = null;
this._writable = false;

@@ -27,17 +29,5 @@ this.writeBuffer = [];

this.once('finish', this._onfinish.bind(this));
this.on('error', function(err) {
if (this.socket) {
// notify the error to the corresponding read-stream.
this.socket._writeerror(this.id, err);
}
}.bind(this));
this.on('error', this._onerror.bind(this));
}
/**
* values to be set from Socket.
*/
WriteStream.prototype.id;
WriteStream.prototype.socket;
WriteStream.prototype._write = function(chunk, encoding, callback) {

@@ -48,3 +38,3 @@ var self = this;

self._writable = false;
self.socket._write(self.id, chunk, 'base64', callback);
self.socket._write(self.id, chunk, encoding, callback);
}

@@ -70,1 +60,10 @@

WriteStream.prototype._onerror = function(err) {
if (!this.socket) return;
// check if the error came from the corresponding stream.
if (err.remote) return;
// notify the error to the corresponding read-stream.
this.socket._writeerror(this.id, err);
};
{
"name": "socket.io-stream",
"version": "0.1.0",
"version": "0.2.0",
"description": "stream for socket.io",

@@ -5,0 +5,0 @@ "author": "Naoyuki Kanezawa <naoyuki.kanezawa@gmail.com>",

@@ -1,13 +0,23 @@

# Socket.IO-Stream
# Socket.IO stream
[![Build Status](https://travis-ci.org/nkzawa/socket.io-stream.png?branch=master)](https://travis-ci.org/nkzawa/socket.io-stream)
[![NPM version](https://badge.fury.io/js/socket.io-stream.png)](http://badge.fury.io/js/socket.io-stream)
Bidirectional binary data transfer with Stream 2 API through [Socket.IO](https://github.com/LearnBoost/socket.io).
## Installation
$ npm install socket.io-stream
## Usage
To receive streams, you just wrap `socket` with `socket.io-stream`, then listen any events.
Server:
```js
var io = require('socket.io').listen(80);
var ss = require('socket.io-stream');
var path = require('path');
io.sockets.on('connection', function(socket) {
ss(socket).on('foo', function(stream, data) {
stream.pipe(fs.createWriteStream(data.name));
io.of('/user').on('connection', function(socket) {
ss(socket).on('profile-image', function(stream, data) {
var filename = path.basename(data.name);
stream.pipe(fs.createWriteStream(filename));
});

@@ -17,14 +27,19 @@ });

`createStream()` will creare a new writable stream which can be sent by `emit`.
Client:
```js
var io = require('socket.io-client');
var ss = require('socket.io-stream');
var socket = io.connect('http://localhost');
var socket = io.connect('http://example.com/user');
var stream = ss.createStream();
var filename = 'any.jpg';
var filename = 'profile.jpg';
ss(socket).emit('foo', stream, {name: filename});
ss(socket).emit('profile-image', stream, {name: filename});
fs.createReadStream(filename).pipe(stream);
```
## License
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc