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

it-ws

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

it-ws - npm Package Compare versions

Comparing version 2.1.0 to 3.0.0

8

client.js

@@ -8,7 +8,7 @@ 'use strict'

module.exports = function (addr, opts) {
module.exports = function (addr, opts = {}) {
const location = typeof window === 'undefined' ? {} : window.location
const url = wsurl(addr, location)
const socket = new WebSocket(url)
const socket = new WebSocket(url, opts.websocket)

@@ -21,2 +21,6 @@ const stream = duplex(socket, opts)

})
stream.destroy = () => {
socket.terminate()
}
stream.socket = socket

@@ -23,0 +27,0 @@ return stream

{
"name": "it-ws",
"version": "2.1.0",
"version": "3.0.0",
"description": "Simple async iterables for websocket client connections",
"main": "index.js",
"scripts": {
"test": "tap test/*.js"
"test": "tap --jobs=1 test/*.js"
},

@@ -27,13 +27,15 @@ "repository": {

"dependencies": {
"buffer": "^5.4.3",
"event-iterator": "^1.2.0",
"relative-url": "^1.0.2",
"safe-buffer": "^5.1.2",
"ws": "^6.2.1"
"ws": "^7.2.1"
},
"devDependencies": {
"it-goodbye": "^2.0.0",
"it-pipe": "^1.0.0",
"iterable-ndjson": "^1.1.0",
"mapleTree": "^0.5.1",
"standard": "^12.0.1",
"standard": "^14.3.1",
"streaming-iterables": "^4.0.2",
"tap": "^12.6.1",
"tap": "^14.10.6",
"tape": "^4.4.0",

@@ -40,0 +42,0 @@ "wsurl": "^1.0.0"

@@ -21,3 +21,3 @@ # it-ws

const connect = require('it-ws/client') // OR: require('it-ws').connect
const { pipeline } = require('streaming-iterables')
const pipe = require('it-pipe')

@@ -28,4 +28,3 @@ const stream = connect(WS_URL)

pipeline(() => source, stream.sink)
pipeline(() => stream.source, sink)
pipe(source, stream, sink)
```

@@ -37,2 +36,3 @@

const createServer = require('it-ws/server')
const pipe = require('it-pipe')

@@ -42,3 +42,3 @@ const server = createServer(stream => {

//eg, echo server
pipeline(() => stream.source, stream.sink)
pipe(stream, stream)
})

@@ -57,2 +57,4 @@

For adding options to the Websocket instance, as [websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options), you can provide an object with the `websocket` property into the connect options.
```js

@@ -59,0 +61,0 @@ const stream = connect(url)

@@ -13,3 +13,3 @@ module.exports = async socket => {

return new Promise((resolve, reject) => {
let remove = socket && (socket.removeEventListener || socket.removeListener)
const remove = socket && (socket.removeEventListener || socket.removeListener)

@@ -16,0 +16,0 @@ function cleanup () {

@@ -43,3 +43,15 @@ var ws = require('./')

var stream = ws(socket)
const addr = wsServer.address()
stream.localAddress = addr.address
stream.localPort = addr.port
stream.remoteAddress = req.socket.remoteAddress
stream.remotePort = req.socket.remotePort
stream.close = () => new Promise((resolve) => {
socket.addEventListener('close', resolve)
socket.close()
})
stream.destroy = () => socket.terminate()
stream.socket = socket
emitter.emit('connection', stream, req)

@@ -46,0 +58,0 @@ })

@@ -1,2 +0,2 @@

const { Buffer } = require('safe-buffer')
const { Buffer } = require('buffer')
const { EventIterator } = require('event-iterator')

@@ -30,3 +30,3 @@

for await (let { data } of messages) {
for await (const { data } of messages) {
yield isArrayBuffer(data) ? Buffer.from(data) : data

@@ -33,0 +33,0 @@ }

var test = require('tape')
var WebSocket = require('ws')
const { pipeline, tap, consume } = require('streaming-iterables')
const { tap, consume } = require('streaming-iterables')
const pipe = require('it-pipe')
var endpoint = require('./helpers/wsurl') + '/echo'

@@ -12,9 +13,9 @@ var ws = require('..')

pipeline(() => ws.source(socket), consume).then(() => {
pipe(ws.source(socket), consume).then(() => {
t.end()
})
pipeline(
() => ['x', 'y', 'z'],
ws.sink(socket, { closeOnEnd: true })
pipe(
['x', 'y', 'z'],
ws(socket, { closeOnEnd: true })
)

@@ -27,4 +28,4 @@ })

t.plan(3)
pipeline(
() => ws.source(socket),
pipe(
ws.source(socket),
tap(function (item) {

@@ -36,5 +37,5 @@ t.ok(item)

pipeline(
() => ['x', 'y', 'z'],
ws.sink(socket, { closeOnEnd: false })
pipe(
['x', 'y', 'z'],
ws(socket, { closeOnEnd: false })
)

@@ -41,0 +42,0 @@ })

var WS = require('../')
var tape = require('tape')
var ndjson = require('iterable-ndjson')
const { pipeline, map, collect } = require('streaming-iterables')
const { map, collect } = require('streaming-iterables')
const pipe = require('it-pipe')
tape('simple echo server', async function (t) {
var server = await WS.createServer(function (stream) {
pipeline(() => stream.source, stream.sink)
pipe(stream, stream)
}).listen(5678)
const ary = await pipeline(
() => [1, 2, 3],
const ary = await pipe(
[1, 2, 3],
// need a delay, because otherwise ws hangs up wrong.

@@ -17,7 +18,3 @@ // otherwise use pull-goodbye.

ndjson.stringify,
source => {
const stream = WS.connect('ws://localhost:5678')
stream.sink(source)
return stream.source
},
WS.connect('ws://localhost:5678'),
ndjson.parse,

@@ -24,0 +21,0 @@ collect

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

var url = require('./helpers/wsurl') + '/echo'
const { pipeline, tap, consume } = require('streaming-iterables')
const { tap, consume } = require('streaming-iterables')
const pipe = require('it-pipe')
const goodbye = require('it-goodbye')

@@ -16,4 +18,4 @@ var server = require('./server')()

pipeline(
() => ws.source(socket),
pipe(
ws.source(socket),
tap(function (value) {

@@ -26,4 +28,4 @@ console.log(value)

pipeline(
() => [].concat(expected),
pipe(
[].concat(expected),
ws.sink(socket, { closeOnEnd: false })

@@ -39,9 +41,5 @@ )

pipeline(
() => [].concat(expected),
source => {
const stream = ws(socket, { closeOnEnd: false })
stream.sink(source)
return stream.source
},
pipe(
[].concat(expected),
ws(socket, { closeOnEnd: false }),
tap(function (value) {

@@ -55,3 +53,3 @@ console.log('echo:', value)

test.skip('duplex with goodbye handshake', function (t) {
test('duplex with goodbye handshake', async function (t) {
var expected = ['x', 'y', 'z']

@@ -62,15 +60,16 @@ var socket = new WebSocket(url)

pipeline(
() => getIterator(pws),
await pipe(
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()
})
source: [].concat(expected),
sink: source => pipe(
source,
tap(value => t.equal(value.toString(), expected.shift())),
consume
)
}),
pws
)
t.end()
})

@@ -77,0 +76,0 @@

var test = require('tape')
var WebSocket = require('ws')
const { pipeline, getIterator, consume } = require('streaming-iterables')
const { consume } = require('streaming-iterables')
const pipe = require('it-pipe')
var ws = require('../')

@@ -11,35 +12,29 @@

test('test error', async function (t) {
let _err
try {
await pipeline(
() => ['x', 'y', 'z'],
// TODO: AS 2019-03-28 - is this actually what `pull` does? should this be
// in a `pipeline` function?
await pipe(
['x', 'y', 'z'],
source => {
const stream = ws(new WebSocket('ws://localhost:34897/' + Math.random()))
const dSinkAbort = (async function * () {
for await (const val of source) yield val
})()
const dSink = pipeline(() => dSinkAbort, stream.sink)
dSink.catch(err => {
if (dSource.throw) {
return Promise.resolve(dSource.throw(err)).catch(_ => {})
stream.sink(source).catch(err => {
if (_err) {
t.strictEqual(err.message, _err.message)
t.end()
}
throw err
_err = err
})
const dSource = (async function * () {
return stream.source
},
source => {
return (async function * () {
try {
for await (const val of stream.source) yield val
for await (const val of source) yield val
} catch (err) {
if (dSinkAbort.throw) {
await dSinkAbort.throw(err)
if (_err) {
t.strictEqual(err.message, _err.message)
t.end()
}
throw err
_err = err
}
})()
return dSource
},

@@ -65,4 +60,4 @@ consume

try {
await pipeline(
() => ws(new WebSocket('ws://localhost:34897/' + Math.random())).source,
await pipe(
ws(new WebSocket('ws://localhost:34897/' + Math.random())).source,
consume

@@ -69,0 +64,0 @@ )

@@ -1,2 +0,3 @@

const { pipeline, map, tap, consume } = require('streaming-iterables')
const { map, tap, consume } = require('streaming-iterables')
const pipe = require('it-pipe')

@@ -10,4 +11,4 @@ var WS = require('../')

var N = 0
await pipeline(
() => stream.source,
await pipe(
stream.source,
tap(val => {

@@ -23,4 +24,4 @@ if (!(N % 1000)) console.log(N)

pipeline(
() => Array.from(Array(10000), (_, i) => i),
pipe(
Array.from(Array(10000), (_, i) => i),
map(n => '?'),

@@ -27,0 +28,0 @@ WS.connect('ws://localhost:2134').sink

@@ -5,3 +5,4 @@

const ndjson = require('iterable-ndjson')
const { pipeline, map, collect } = require('streaming-iterables')
const { map, collect } = require('streaming-iterables')
const pipe = require('it-pipe')

@@ -12,3 +13,3 @@ tape('simple echo server', async function (t) {

var server = WS.createServer({ server: httpServer }, function (stream) {
pipeline(() => stream.source, stream.sink)
pipe(stream, stream)
})

@@ -20,4 +21,4 @@

const ary = await pipeline(
() => [1, 2, 3],
const ary = await pipe(
[1, 2, 3],
// need a delay, because otherwise ws hangs up wrong.

@@ -27,6 +28,3 @@ // otherwise use pull-goodbye.

ndjson.stringify,
source => {
stream.sink(source)
return stream.source
},
stream,
ndjson.parse,

@@ -33,0 +31,0 @@ collect

var test = require('tape')
var WebSocket = require('ws')
const { pipeline, collect } = require('streaming-iterables')
const { collect } = require('streaming-iterables')
const pipe = require('it-pipe')
var endpoint = require('./helpers/wsurl') + '/read'

@@ -19,8 +20,3 @@ var ws = require('../source')

t.plan(1)
const values = await pipeline(
() => ws(socket),
collect
)
const values = await pipe(ws(socket), collect)
t.deepEqual(values, ['a', 'b', 'c', 'd'])

@@ -32,7 +28,3 @@ })

const values = await pipeline(
() => ws(new WebSocket(endpoint)),
collect
)
const values = await pipe(ws(new WebSocket(endpoint)), collect)
t.deepEqual(values, ['a', 'b', 'c', 'd'])

@@ -39,0 +31,0 @@ })

var WS = require('../')
var tape = require('tape')
const ndjson = require('iterable-ndjson')
const { pipeline, map, collect } = require('streaming-iterables')
const { map, collect } = require('streaming-iterables')
const pipe = require('it-pipe')
tape('simple echo server', async t => {
const server = WS.createServer(async stream => {
await pipeline(() => stream.source, stream.sink)
await pipe(stream, stream)
})

@@ -15,4 +16,4 @@

const ary = await pipeline(
() => [1, 2, 3],
const ary = await pipe(
[1, 2, 3],
// need a delay, because otherwise ws hangs up wrong.

@@ -22,6 +23,3 @@ // otherwise use pull-goodbye.

ndjson.stringify,
source => {
stream.sink(source)
return stream.source
},
stream,
ndjson.parse,

@@ -28,0 +26,0 @@ collect

Sorry, the diff of this file is not supported yet

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