Socket
Socket
Sign inDemoInstall

faye-websocket

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

faye-websocket - npm Package Compare versions

Comparing version 0.7.3 to 0.8.0

examples/proxy_server.js

6

CHANGELOG.md

@@ -0,1 +1,7 @@

### 0.8.0 / 2014-11-08
* Support connections via HTTP proxies
* Close the connection cleanly if we're still waiting for a handshake response
### 0.7.3 / 2014-10-04

@@ -2,0 +8,0 @@

9

examples/autobahn_client.js

@@ -5,3 +5,3 @@ var WebSocket = require('../lib/faye/websocket'),

var host = 'ws://localhost:9001',
agent = 'Node ' + process.version,
agent = 'node-' + process.version,
cases = 0,

@@ -21,9 +21,10 @@ skip = [];

var runCase = function(n) {
progress.op();
if (n > cases) {
socket = new WebSocket.Client(host + '/updateReports?agent=' + encodeURIComponent(agent));
socket.onclose = process.exit;
return;
}
progress.op();
} else if (skip.indexOf(n) >= 0) {
if (skip.indexOf(n) >= 0) {
runCase(n + 1);

@@ -30,0 +31,0 @@

var WebSocket = require('../lib/faye/websocket'),
port = process.argv[2] || 7000,
secure = process.argv[3] === 'ssl',
scheme = secure ? 'wss' : 'ws',
url = scheme + '://localhost:' + port + '/',
headers = {Origin: 'http://faye.jcoglan.com'},
ws = new WebSocket.Client(url, null, {headers: headers});
fs = require('fs');
console.log('Connecting to ' + ws.url);
var url = process.argv[2],
headers = {Origin: 'http://faye.jcoglan.com'},
ca = fs.readFileSync(__dirname + '/../spec/server.crt'),
proxy = {origin: process.argv[3], headers: {'User-Agent': 'Echo'}, tls: {ca: ca}},
ws = new WebSocket.Client(url, null, {headers: headers, proxy: proxy, tls: {ca: ca}});
ws.onopen = function(event) {
console.log('open');
ws.send('Hello, WebSocket!');
ws.onopen = function() {
console.log('[open]', ws.headers);
ws.send('mic check');
};
ws.onmessage = function(event) {
console.log('message', event.data);
// ws.close(1002, 'Going away');
ws.onclose = function(close) {
console.log('[close]', close.code, close.reason);
};
ws.onclose = function(event) {
console.log('close', event.code, event.reason);
ws.onerror = function(error) {
console.log('[error]', error.message);
};
ws.onmessage = function(message) {
console.log('[message]', message.data);
};

@@ -7,7 +7,7 @@ var WebSocket = require('../lib/faye/websocket'),

var port = process.argv[2] || 7000,
secure = process.argv[3] === 'ssl';
secure = process.argv[3] === 'tls';
var upgradeHandler = function(request, socket, head) {
var ws = new WebSocket(request, socket, head, ['irc', 'xmpp'], {ping: 5});
console.log('open', ws.url, ws.version, ws.protocol);
console.log('[open]', ws.url, ws.version, ws.protocol, request.headers);

@@ -17,3 +17,3 @@ ws.pipe(ws);

ws.onclose = function(event) {
console.log('close', event.code, event.reason);
console.log('[close]', event.code, event.reason);
ws = null;

@@ -30,3 +30,3 @@ };

console.log('open', es.url, es.lastEventId);
console.log('[open]', es.url, es.lastEventId);

@@ -45,3 +45,3 @@ var loop = setInterval(function() {

clearInterval(loop);
console.log('close', es.url);
console.log('[close]', es.url);
es = null;

@@ -48,0 +48,0 @@ };

@@ -26,16 +26,2 @@ var Stream = require('stream').Stream,

this._stream.setTimeout(0);
this._stream.setNoDelay(true);
['close', 'end'].forEach(function(event) {
this._stream.on(event, function() { self._finalize('', 1006) });
}, this);
this._stream.on('error', function(error) {
var event = new Event('error', {message: 'Network error: ' + self.url + ': ' + error.message});
event.initEvent('error', false, false);
self.dispatchEvent(event);
self._finalize('', 1006);
});
this._driver.on('open', function(e) { self._open() });

@@ -49,2 +35,3 @@ this._driver.on('message', function(e) { self._receiveMessage(e.data) });

self.dispatchEvent(event);
self._finalize('', 1006);
});

@@ -63,4 +50,8 @@ this.on('error', function() {});

this._stream.pipe(this._driver.io);
this._driver.io.pipe(this._stream);
this._configureStream();
if (!this._proxy) {
this._stream.pipe(this._driver.io);
this._driver.io.pipe(this._stream);
}
};

@@ -108,2 +99,17 @@ util.inherits(API, Stream);

_configureStream: function() {
var self = this;
this._stream.setTimeout(0);
this._stream.setNoDelay(true);
['close', 'end'].forEach(function(event) {
this._stream.on(event, function() { self._finalize('', 1006) });
}, this);
this._stream.on('error', function(error) {
self._driver.emit('error', new Error('Network error: ' + self.url + ': ' + error.message));
});
},
_open: function() {

@@ -132,10 +138,16 @@ if (this.readyState !== API.CONNECTING) return;

if (this.readyState === API.CLOSED) return;
this.readyState = API.CLOSED;
if (this._pingTimer) clearInterval(this._pingTimer);
if (this._stream) this._stream.end();
var stream = this._stream;
if (stream) {
if (stream.unpipe) stream.unpipe(this._driver.io);
stream.on('data', function() { stream.destroy() });
stream.end();
}
if (this.readable) this.emit('end');
this.readable = this.writable = false;
this.readyState = API.CLOSED;
var event = new Event('close', {code: code || 1000, reason: reason || ''});

@@ -142,0 +154,0 @@ event.initEvent('close', false, false);

var util = require('util'),
net = require('net'),
tls = require('tls'),
crypto = require('crypto'),
url = require('url'),
driver = require('websocket-driver'),

@@ -8,8 +10,10 @@ API = require('./api'),

var Client = function(url, protocols, options) {
var DEFAULT_PORTS = {'http:': 80, 'https:': 443, 'ws:':80, 'wss:': 443},
SECURE_PROTOCOLS = ['https:', 'wss:'];
var Client = function(_url, protocols, options) {
options = options || {};
this.url = url;
this._uri = require('url').parse(url);
this._driver = driver.client(url, {maxLength: options.maxLength, protocols: protocols});
this.url = _url;
this._driver = driver.client(this.url, {maxLength: options.maxLength, protocols: protocols});

@@ -23,15 +27,18 @@ ['open', 'error'].forEach(function(event) {

var secure = (this._uri.protocol === 'wss:'),
onConnect = function() { self._driver.start() },
tlsOptions = {},
self = this;
var proxy = options.proxy || {},
endpoint = url.parse(proxy.origin || this.url),
port = endpoint.port || DEFAULT_PORTS[endpoint.protocol],
secure = SECURE_PROTOCOLS.indexOf(endpoint.protocol) >= 0,
onConnect = function() { self._onConnect() },
originTLS = options.tls || {},
socketTLS = proxy.origin ? (proxy.tls || {}) : originTLS,
self = this;
if (options.ca) tlsOptions.ca = options.ca;
originTLS.ca = originTLS.ca || options.ca;
var connection = secure
? tls.connect(this._uri.port || 443, this._uri.hostname, tlsOptions, onConnect)
: net.createConnection(this._uri.port || 80, this._uri.hostname);
this._stream = secure
? tls.connect(port, endpoint.hostname, socketTLS, onConnect)
: net.connect(port, endpoint.hostname, onConnect);
this._stream = connection;
if (!secure) this._stream.on('connect', onConnect);
if (proxy.origin) this._configureProxy(proxy, originTLS);

@@ -42,2 +49,39 @@ API.call(this, options);

Client.prototype._onConnect = function() {
var worker = this._proxy || this._driver;
worker.start();
};
Client.prototype._configureProxy = function(proxy, originTLS) {
var uri = url.parse(this.url),
secure = SECURE_PROTOCOLS.indexOf(uri.protocol) >= 0,
self = this,
name;
this._proxy = this._driver.proxy(proxy.origin);
if (proxy.headers) {
for (name in proxy.headers) this._proxy.setHeader(name, proxy.headers[name]);
}
this._proxy.pipe(this._stream, {end: false});
this._stream.pipe(this._proxy);
this._proxy.on('connect', function() {
if (secure) {
var options = {socket: self._stream};
for (name in originTLS) options[name] = originTLS[name];
self._stream = tls.connect(options);
self._configureStream();
}
self._driver.io.pipe(self._stream);
self._stream.pipe(self._driver.io);
self._driver.start();
});
this._proxy.on('error', function(error) {
self._driver.emit('error', error);
});
};
module.exports = Client;

@@ -8,6 +8,6 @@ { "name" : "faye-websocket"

, "version" : "0.7.3"
, "version" : "0.8.0"
, "engines" : {"node": ">=0.4.0"}
, "main" : "./lib/faye/websocket"
, "dependencies" : {"websocket-driver": ">=0.3.6"}
, "dependencies" : {"websocket-driver": ">=0.4.0"}
, "devDependencies" : {"jstest": "", "pace": ""}

@@ -14,0 +14,0 @@

@@ -120,3 +120,20 @@ # faye-websocket

To connect via a proxy, set the `proxy` option to the HTTP origin of the proxy,
including any authorization information, custom headers and TLS config you
require. Only the `origin` setting is required.
```js
var ws = new WebSocket.Client('ws://www.example.com/', null, {
proxy: {
origin: 'http://username:password@proxy.example.com',
headers: {'User-Agent': 'node'},
tls: {cert: fs.readFileSync('client.crt')}
}
});
```
The `tls` value is a Node 'TLS options' object that will be passed to
[`tls.connect()`](http://nodejs.org/api/tls.html#tls_tls_connect_options_callback).
## Subprotocol negotiation

@@ -164,3 +181,11 @@

The client accepts some additional options:
* `proxy` - settings for a proxy as described above
* `tls` - a Node 'TLS options' object containing TLS settings for the origin
server, this will be passed to
[`tls.connect()`](http://nodejs.org/api/tls.html#tls_tls_connect_options_callback)
* `ca` - (legacy) a shorthand for passing `{tls: {ca: value}}`
## WebSocket API

@@ -278,3 +303,3 @@

Copyright (c) 2010-2013 James Coglan
Copyright (c) 2010-2014 James Coglan

@@ -281,0 +306,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

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