simple-websocket
Advanced tools
Comparing version 8.1.1 to 9.0.0
118
index.js
@@ -1,2 +0,3 @@ | ||
/* global WebSocket, DOMException */ | ||
/*! simple-websocket. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */ | ||
/* global WebSocket */ | ||
@@ -58,3 +59,6 @@ const debug = require('debug')('simple-websocket') | ||
// `ws` package accepts options | ||
this._ws = new _WebSocket(opts.url, opts) | ||
this._ws = new _WebSocket(opts.url, null, { | ||
...opts, | ||
encoding: undefined // encoding option breaks ws internals | ||
}) | ||
} else { | ||
@@ -70,19 +74,15 @@ this._ws = new _WebSocket(opts.url) | ||
this._ws.binaryType = 'arraybuffer' | ||
this._ws.onopen = () => { | ||
this._onOpen() | ||
if (opts.socket && this.connected) { | ||
queueMicrotask(() => this._handleOpen()) | ||
} else { | ||
this._ws.onopen = () => this._handleOpen() | ||
} | ||
this._ws.onmessage = event => { | ||
this._onMessage(event) | ||
} | ||
this._ws.onclose = () => { | ||
this._onClose() | ||
} | ||
this._ws.onerror = () => { | ||
this.destroy(new Error('connection error to ' + this.url)) | ||
} | ||
this._onFinishBound = () => { | ||
this._onFinish() | ||
} | ||
this.once('finish', this._onFinishBound) | ||
this._ws.onmessage = event => this._handleMessage(event) | ||
this._ws.onclose = () => this._handleClose() | ||
this._ws.onerror = err => this._handleError(err) | ||
this._handleFinishBound = () => this._handleFinish() | ||
this.once('finish', this._handleFinishBound) | ||
} | ||
@@ -122,4 +122,6 @@ | ||
if (this._onFinishBound) this.removeListener('finish', this._onFinishBound) | ||
this._onFinishBound = null | ||
if (this._handleFinishBound) { | ||
this.removeListener('finish', this._handleFinishBound) | ||
} | ||
this._handleFinishBound = null | ||
@@ -148,11 +150,3 @@ if (this._ws) { | ||
if (err) { | ||
if (typeof DOMException !== 'undefined' && err instanceof DOMException) { | ||
// Convert Edge DOMException object to Error object | ||
const code = err.code | ||
err = new Error(err.message) | ||
err.code = code | ||
} | ||
this.emit('error', err) | ||
} | ||
if (err) this.emit('error', err) | ||
this.emit('close') | ||
@@ -186,28 +180,3 @@ cb() | ||
// When stream finishes writing, close socket. Half open connections are not | ||
// supported. | ||
_onFinish () { | ||
if (this.destroyed) return | ||
// Wait a bit before destroying so the socket flushes. | ||
// TODO: is there a more reliable way to accomplish this? | ||
const destroySoon = () => { | ||
setTimeout(() => this.destroy(), 1000) | ||
} | ||
if (this.connected) { | ||
destroySoon() | ||
} else { | ||
this.once('connect', destroySoon) | ||
} | ||
} | ||
_onMessage (event) { | ||
if (this.destroyed) return | ||
let data = event.data | ||
if (data instanceof ArrayBuffer) data = Buffer.from(data) | ||
this.push(data) | ||
} | ||
_onOpen () { | ||
_handleOpen () { | ||
if (this.connected || this.destroyed) return | ||
@@ -241,2 +210,37 @@ this.connected = true | ||
_handleMessage (event) { | ||
if (this.destroyed) return | ||
let data = event.data | ||
if (data instanceof ArrayBuffer) data = Buffer.from(data) | ||
this.push(data) | ||
} | ||
_handleClose () { | ||
if (this.destroyed) return | ||
this._debug('on close') | ||
this.destroy() | ||
} | ||
_handleError (err) { | ||
this.destroy(new Error(`Error connecting to ${this.url} (${err})`)) | ||
} | ||
// When stream finishes writing, close socket. Half open connections are not | ||
// supported. | ||
_handleFinish () { | ||
if (this.destroyed) return | ||
// Wait a bit before destroying so the socket flushes. | ||
// TODO: is there a more reliable way to accomplish this? | ||
const destroySoon = () => { | ||
setTimeout(() => this.destroy(), 1000) | ||
} | ||
if (this.connected) { | ||
destroySoon() | ||
} else { | ||
this.once('connect', destroySoon) | ||
} | ||
} | ||
_onInterval () { | ||
@@ -252,8 +256,2 @@ if (!this._cb || !this._ws || this._ws.bufferedAmount > MAX_BUFFERED_AMOUNT) { | ||
_onClose () { | ||
if (this.destroyed) return | ||
this._debug('on close') | ||
this.destroy() | ||
} | ||
_debug () { | ||
@@ -260,0 +258,0 @@ const args = [].slice.call(arguments) |
{ | ||
"name": "simple-websocket", | ||
"description": "Simple, EventEmitter API for WebSockets (browser)", | ||
"version": "8.1.1", | ||
"version": "9.0.0", | ||
"author": { | ||
"name": "Feross Aboukhadijeh", | ||
"email": "feross@feross.org", | ||
"url": "http://feross.org/" | ||
"url": "https://feross.org" | ||
}, | ||
@@ -24,3 +24,4 @@ "browser": { | ||
"devDependencies": { | ||
"airtap": "^2.0.3", | ||
"airtap": "^3.0.0", | ||
"babel-eslint": "^10.1.0", | ||
"babel-minify": "^0.5.1", | ||
@@ -31,3 +32,3 @@ "browserify": "^16.1.0", | ||
"standard": "*", | ||
"tape": "^4.0.0" | ||
"tape": "^5.0.0" | ||
}, | ||
@@ -56,3 +57,20 @@ "homepage": "https://github.com/feross/simple-websocket", | ||
"test-node": "tape test/*.js test/node/*.js" | ||
} | ||
}, | ||
"standard": { | ||
"parser": "babel-eslint" | ||
}, | ||
"funding": [ | ||
{ | ||
"type": "github", | ||
"url": "https://github.com/sponsors/feross" | ||
}, | ||
{ | ||
"type": "patreon", | ||
"url": "https://www.patreon.com/feross" | ||
}, | ||
{ | ||
"type": "consulting", | ||
"url": "https://feross.org/support" | ||
} | ||
] | ||
} |
@@ -7,21 +7,16 @@ const events = require('events') | ||
constructor (opts) { | ||
opts = Object.assign({ | ||
super() | ||
this.opts = { | ||
clientTracking: false, | ||
perMessageDeflate: false | ||
}, opts) | ||
perMessageDeflate: false, | ||
...opts | ||
} | ||
super() | ||
this.destroyed = false | ||
this._server = new WebSocketServer(opts) | ||
this._onListeningBound = () => this._onListening() | ||
this._server.on('listening', this._onListeningBound) | ||
this._onConnectionBound = conn => this._onConnection(conn) | ||
this._server.on('connection', this._onConnectionBound) | ||
this._onErrorBound = err => this._onError(err) | ||
this._server.once('error', this._onErrorBound) | ||
this._server = new WebSocketServer(this.opts) | ||
this._server.on('listening', this._handleListening) | ||
this._server.on('connection', this._handleConnection) | ||
this._server.once('error', this._handleError) | ||
} | ||
@@ -34,27 +29,29 @@ | ||
close (cb) { | ||
if (this.destroyed) return cb(new Error('server is closed')) | ||
if (this.destroyed) { | ||
if (cb) cb(new Error('server is closed')) | ||
return | ||
} | ||
this.destroyed = true | ||
if (cb) this.once('close', cb) | ||
this._server.removeListener('listening', this._onListeningBound) | ||
this._server.removeListener('connection', this._onConnectionBound) | ||
this._server.removeListener('error', this._onErrorBound) | ||
this._server.removeListener('listening', this._handleListening) | ||
this._server.removeListener('connection', this._handleConnection) | ||
this._server.removeListener('error', this._handleError) | ||
this._server.on('error', () => {}) // suppress future errors | ||
this._server.close(() => this.emit('close')) | ||
this._server = null | ||
} | ||
_onListening () { | ||
_handleListening = () => { | ||
this.emit('listening') | ||
} | ||
_onConnection (conn) { | ||
const socket = new Socket({ socket: conn }) | ||
socket._onOpen() | ||
socket.upgradeReq = conn.upgradeReq | ||
this.emit('connection', socket) | ||
this.once('close', () => { | ||
socket.upgradeReq = null | ||
}) | ||
_handleConnection = (conn, req) => { | ||
const socket = new Socket({ ...this.opts, socket: conn }) | ||
this.emit('connection', socket, req) | ||
} | ||
_onError (err) { | ||
_handleError = (err) => { | ||
this.emit('error', err) | ||
@@ -61,0 +58,0 @@ this.close() |
Sorry, the diff of this file is too big to display
99630
8
275