New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

pull-ws

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pull-ws - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

17

index.js

@@ -1,12 +0,1 @@

/**
# pull-ws
A simple (but effective) implementation of a
[`pull-stream`](https://github.com/dominictarr/pull-stream) `Source` and `Sink`
that is compatible both with native browser WebSockets and
[`ws`](https://github.com/einaros/ws) created clients.
## Reference
**/
exports = module.exports = duplex;

@@ -17,7 +6,7 @@

function duplex (ws) {
function duplex (ws, opts) {
return {
sink: exports.sink(ws),
source: exports.source(ws)
source: exports.source(ws),
sink: exports.sink(ws, opts)
};
};

8

package.json
{
"name": "pull-ws",
"version": "1.1.0",
"version": "2.0.0",
"description": "Simple pull-streams for websocket client connections",

@@ -9,4 +9,3 @@ "main": "index.js",

"test": "node test/node.js",
"browser": "testling",
"gendocs": "gendocs > README.md"
"browser": "testling"
},

@@ -34,3 +33,4 @@ "repository": {

"ws": "^0.4.2",
"wsurl": "^1.0.0"
"wsurl": "^1.0.0",
"pull-goodbye": "~0.0.1"
},

@@ -37,0 +37,0 @@ "dependencies": {

@@ -15,6 +15,26 @@ # pull-ws

### `sink(socket, opts?)`
### `pws(socket, opts?)`
turn a websocket into a duplex pull stream.
If provided, `opts` is passed to `pws.sink(socket, opts)`.
Websockets do not support half open mode.
[see allowHalfOpen option in net module](
http://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener)
If you have a protocol that assumes halfOpen connections, but are using
a networking protocol like websockets that does not support it, I suggest
using [pull-goodbye](https://github.com/dominictarr/pull-goodbye) with your
protocol.
### `pws.sink(socket, opts?)`
Create a pull-stream `Sink` that will write data to the `socket`.
`opts` may be `{closeOnEnd: true, onClose: onClose}`.
`onClose` will be called when the sink ends. If `closeOnEnd=false`
the stream will not close, it will just stop emitting data.
(by default `closeOnEnd` is true)
If `opts` is a function, then `onClose = opts; opts.closeOnEnd = true`.
```js

@@ -47,3 +67,3 @@ var pull = require('pull-stream');

### `source(socket)`
### `pws.source(socket)`

@@ -50,0 +70,0 @@ Create a pull-stream `Source` that will read data from the `socket`.

@@ -13,3 +13,5 @@ var pull = require('pull-core');

module.exports = pull.Sink(function(read, socket, opts) {
var closeOnEnd = (opts || {}).closeOnEnd;
opts = opts || {}
var closeOnEnd = opts.closeOnEnd !== false;
var onClose = 'function' === typeof opts ? opts : opts.onClose;

@@ -20,2 +22,12 @@ function next(end, data) {

if (closeOnEnd && socket.readyState <= 1) {
if(onClose)
socket.addEventListener('close', function (ev) {
if(ev.wasClean) onClose()
else {
var err = new Error('ws error')
err.event = ev
onClose(err)
}
});
socket.close();

@@ -22,0 +34,0 @@ }

@@ -15,2 +15,3 @@ var test = require('tape');

pull.collect(function(err) {
console.log('END')
t.ifError(err, 'closed without error');

@@ -25,1 +26,40 @@ })

});
test('sink has callback for end of stream', function(t) {
var socket = new WebSocket(endpoint);
t.plan(2);
pull(
ws.source(socket),
pull.collect(function(err) {
t.ifError(err, 'closed without error');
})
);
pull(
pull.values(['x', 'y', 'z']),
ws.sink(socket, function (err) {
t.ifError(err, 'closed without error - sink');
})
);
});
test('closeOnEnd=false, stream doesn\'t close', function(t) {
var socket = new WebSocket(endpoint);
t.plan(3);
pull(
ws.source(socket),
pull.drain(function (item) {
t.ok(item)
})
);
pull(
pull.values(['x', 'y', 'z']),
ws.sink(socket, { closeOnEnd: false })
);
});

@@ -5,3 +5,4 @@ var test = require('tape');

var ws = require('..');
var url = require('./helpers/wsurl') + '/echo'
var url = require('./helpers/wsurl') + '/echo';
var goodbye = require('pull-goodbye');

@@ -17,2 +18,3 @@ test('setup echo reading and writing', function(t) {

pull.drain(function(value) {
console.log(value)
t.equal(value, expected.shift());

@@ -24,3 +26,3 @@ })

pull.values([].concat(expected)),
ws.sink(socket)
ws.sink(socket, {closeOnEnd: false})
);

@@ -39,4 +41,5 @@

pull.values([].concat(expected)),
ws(socket),
ws(socket, {closeOnEnd: false}),
pull.drain(function(value) {
console.log('echo:', value)
t.equal(value, expected.shift());

@@ -48,1 +51,25 @@ })

test('duplex with goodbye handshake', function (t) {
var expected = ['x', 'y', 'z'];
var socket = new WebSocket(url);
var pws = ws(socket)
pull(
pws,
goodbye({
source: pull.values([].concat(expected)),
sink: pull.drain(function(value) {
t.equal(value, expected.shift());
}, function (err) {
t.equal(expected.length, 0)
t.end()
})
}),
pws
);
})
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