Current Folder
This commit is contained in:
1 parent
8b3382289b
commit
cfcada880d
4379 files changed
No files matched your search
+31
@@ -0,0 +1,31 @@
|
||||
var isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
|
||||
var isRN = typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
|
||||
var _btoa = null;
|
||||
var helpers = null;
|
||||
if (isNode || isRN) {
|
||||
_btoa = function (str) {
|
||||
return Buffer.from(str).toString('base64');
|
||||
};
|
||||
var url = require('url');
|
||||
if (url.URL) {
|
||||
// Use the new Node 6+ API for parsing URLs that supports username/password
|
||||
var newURL = url.URL;
|
||||
helpers = function (url) {
|
||||
return new newURL(url);
|
||||
};
|
||||
}
|
||||
else {
|
||||
// Web3 supports Node.js 5, so fall back to the legacy URL API if necessary
|
||||
helpers = require('url').parse;
|
||||
}
|
||||
}
|
||||
else {
|
||||
_btoa = btoa.bind(window);
|
||||
helpers = function (url) {
|
||||
return new URL(url);
|
||||
};
|
||||
}
|
||||
module.exports = {
|
||||
parseURL: helpers,
|
||||
btoa: _btoa
|
||||
};
|
||||
+355
@@ -0,0 +1,355 @@
|
||||
/*
|
||||
This file is part of web3.js.
|
||||
|
||||
web3.js is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
web3.js is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* @file WebsocketProvider.js
|
||||
* @authors: Samuel Furter <samuel@ethereum.org>, Fabian Vogelsteller <fabian@ethereum.org>
|
||||
* @date 2019
|
||||
*/
|
||||
'use strict';
|
||||
var EventEmitter = require('eventemitter3');
|
||||
var helpers = require('./helpers.js');
|
||||
var errors = require('web3-core-helpers').errors;
|
||||
var Ws = require('websocket').w3cwebsocket;
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {Object} options
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
var WebsocketProvider = function WebsocketProvider(url, options) {
|
||||
EventEmitter.call(this);
|
||||
options = options || {};
|
||||
this.url = url;
|
||||
this._customTimeout = options.timeout || 1000 * 15;
|
||||
this.headers = options.headers || {};
|
||||
this.protocol = options.protocol || undefined;
|
||||
this.reconnectOptions = Object.assign({
|
||||
auto: false,
|
||||
delay: 5000,
|
||||
maxAttempts: false,
|
||||
onTimeout: false
|
||||
}, options.reconnect);
|
||||
this.clientConfig = options.clientConfig || undefined; // Allow a custom client configuration
|
||||
this.requestOptions = options.requestOptions || undefined; // Allow a custom request options (https://github.com/theturtle32/WebSocket-Node/blob/master/docs/WebSocketClient.md#connectrequesturl-requestedprotocols-origin-headers-requestoptions)
|
||||
this.DATA = 'data';
|
||||
this.CLOSE = 'close';
|
||||
this.ERROR = 'error';
|
||||
this.CONNECT = 'connect';
|
||||
this.RECONNECT = 'reconnect';
|
||||
this.connection = null;
|
||||
this.requestQueue = new Map();
|
||||
this.responseQueue = new Map();
|
||||
this.reconnectAttempts = 0;
|
||||
this.reconnecting = false;
|
||||
// The w3cwebsocket implementation does not support Basic Auth
|
||||
// username/password in the URL. So generate the basic auth header, and
|
||||
// pass through with any additional headers supplied in constructor
|
||||
var parsedURL = helpers.parseURL(url);
|
||||
if (parsedURL.username && parsedURL.password) {
|
||||
this.headers.authorization = 'Basic ' + helpers.btoa(parsedURL.username + ':' + parsedURL.password);
|
||||
}
|
||||
// When all node core implementations that do not have the
|
||||
// WHATWG compatible URL parser go out of service this line can be removed.
|
||||
if (parsedURL.auth) {
|
||||
this.headers.authorization = 'Basic ' + helpers.btoa(parsedURL.auth);
|
||||
}
|
||||
// make property `connected` which will return the current connection status
|
||||
Object.defineProperty(this, 'connected', {
|
||||
get: function () {
|
||||
return this.connection && this.connection.readyState === this.connection.OPEN;
|
||||
},
|
||||
enumerable: true
|
||||
});
|
||||
this.connect();
|
||||
};
|
||||
// Inherit from EventEmitter
|
||||
WebsocketProvider.prototype = Object.create(EventEmitter.prototype);
|
||||
WebsocketProvider.prototype.constructor = WebsocketProvider;
|
||||
/**
|
||||
* Connects to the configured node
|
||||
*
|
||||
* @method connect
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.connect = function () {
|
||||
this.connection = new Ws(this.url, this.protocol, undefined, this.headers, this.requestOptions, this.clientConfig);
|
||||
this._addSocketListeners();
|
||||
};
|
||||
/**
|
||||
* Listener for the `data` event of the underlying WebSocket object
|
||||
*
|
||||
* @method _onMessage
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._onMessage = function (e) {
|
||||
var _this = this;
|
||||
this._parseResponse((typeof e.data === 'string') ? e.data : '').forEach(function (result) {
|
||||
if (result.method && result.method.indexOf('_subscription') !== -1) {
|
||||
_this.emit(_this.DATA, result);
|
||||
return;
|
||||
}
|
||||
var id = result.id;
|
||||
// get the id which matches the returned id
|
||||
if (Array.isArray(result)) {
|
||||
id = result[0].id;
|
||||
}
|
||||
if (_this.responseQueue.has(id)) {
|
||||
if (_this.responseQueue.get(id).callback !== undefined) {
|
||||
_this.responseQueue.get(id).callback(false, result);
|
||||
}
|
||||
_this.responseQueue.delete(id);
|
||||
}
|
||||
});
|
||||
};
|
||||
/**
|
||||
* Listener for the `open` event of the underlying WebSocket object
|
||||
*
|
||||
* @method _onConnect
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._onConnect = function () {
|
||||
this.emit(this.CONNECT);
|
||||
this.reconnectAttempts = 0;
|
||||
this.reconnecting = false;
|
||||
if (this.requestQueue.size > 0) {
|
||||
var _this = this;
|
||||
this.requestQueue.forEach(function (request, key) {
|
||||
_this.send(request.payload, request.callback);
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Listener for the `close` event of the underlying WebSocket object
|
||||
*
|
||||
* @method _onClose
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._onClose = function (event) {
|
||||
var _this = this;
|
||||
if (this.reconnectOptions.auto && (![1000, 1001].includes(event.code) || event.wasClean === false)) {
|
||||
this.reconnect();
|
||||
return;
|
||||
}
|
||||
this.emit(this.CLOSE, event);
|
||||
if (this.requestQueue.size > 0) {
|
||||
this.requestQueue.forEach(function (request, key) {
|
||||
request.callback(errors.ConnectionNotOpenError(event));
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
if (this.responseQueue.size > 0) {
|
||||
this.responseQueue.forEach(function (request, key) {
|
||||
request.callback(errors.InvalidConnection('on WS', event));
|
||||
_this.responseQueue.delete(key);
|
||||
});
|
||||
}
|
||||
this._removeSocketListeners();
|
||||
this.removeAllListeners();
|
||||
};
|
||||
/**
|
||||
* Will add the required socket listeners
|
||||
*
|
||||
* @method _addSocketListeners
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._addSocketListeners = function () {
|
||||
this.connection.addEventListener('message', this._onMessage.bind(this));
|
||||
this.connection.addEventListener('open', this._onConnect.bind(this));
|
||||
this.connection.addEventListener('close', this._onClose.bind(this));
|
||||
};
|
||||
/**
|
||||
* Will remove all socket listeners
|
||||
*
|
||||
* @method _removeSocketListeners
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._removeSocketListeners = function () {
|
||||
this.connection.removeEventListener('message', this._onMessage);
|
||||
this.connection.removeEventListener('open', this._onConnect);
|
||||
this.connection.removeEventListener('close', this._onClose);
|
||||
};
|
||||
/**
|
||||
* Will parse the response and make an array out of it.
|
||||
*
|
||||
* @method _parseResponse
|
||||
*
|
||||
* @param {String} data
|
||||
*
|
||||
* @returns {Array}
|
||||
*/
|
||||
WebsocketProvider.prototype._parseResponse = function (data) {
|
||||
var _this = this, returnValues = [];
|
||||
// DE-CHUNKER
|
||||
var dechunkedData = data
|
||||
.replace(/\}[\n\r]?\{/g, '}|--|{') // }{
|
||||
.replace(/\}\][\n\r]?\[\{/g, '}]|--|[{') // }][{
|
||||
.replace(/\}[\n\r]?\[\{/g, '}|--|[{') // }[{
|
||||
.replace(/\}\][\n\r]?\{/g, '}]|--|{') // }]{
|
||||
.split('|--|');
|
||||
dechunkedData.forEach(function (data) {
|
||||
// prepend the last chunk
|
||||
if (_this.lastChunk)
|
||||
data = _this.lastChunk + data;
|
||||
var result = null;
|
||||
try {
|
||||
result = JSON.parse(data);
|
||||
}
|
||||
catch (e) {
|
||||
_this.lastChunk = data;
|
||||
// start timeout to cancel all requests
|
||||
clearTimeout(_this.lastChunkTimeout);
|
||||
_this.lastChunkTimeout = setTimeout(function () {
|
||||
if (_this.reconnectOptions.auto && _this.reconnectOptions.onTimeout) {
|
||||
_this.reconnect();
|
||||
return;
|
||||
}
|
||||
_this.emit(_this.ERROR, errors.ConnectionTimeout(_this._customTimeout));
|
||||
if (_this.requestQueue.size > 0) {
|
||||
_this.requestQueue.forEach(function (request, key) {
|
||||
request.callback(errors.ConnectionTimeout(_this._customTimeout));
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
}, _this._customTimeout);
|
||||
return;
|
||||
}
|
||||
// cancel timeout and set chunk to null
|
||||
clearTimeout(_this.lastChunkTimeout);
|
||||
_this.lastChunk = null;
|
||||
if (result)
|
||||
returnValues.push(result);
|
||||
});
|
||||
return returnValues;
|
||||
};
|
||||
/**
|
||||
* Does check if the provider is connecting and will add it to the queue or will send it directly
|
||||
*
|
||||
* @method send
|
||||
*
|
||||
* @param {Object} payload
|
||||
* @param {Function} callback
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.send = function (payload, callback) {
|
||||
var _this = this;
|
||||
var id = payload.id;
|
||||
var request = { payload: payload, callback: callback };
|
||||
if (Array.isArray(payload)) {
|
||||
id = payload[0].id;
|
||||
}
|
||||
if (this.connection.readyState === this.connection.CONNECTING || this.reconnecting) {
|
||||
this.requestQueue.set(id, request);
|
||||
return;
|
||||
}
|
||||
if (this.connection.readyState !== this.connection.OPEN) {
|
||||
this.requestQueue.delete(id);
|
||||
this.emit(this.ERROR, errors.ConnectionNotOpenError());
|
||||
request.callback(errors.ConnectionNotOpenError());
|
||||
return;
|
||||
}
|
||||
this.responseQueue.set(id, request);
|
||||
this.requestQueue.delete(id);
|
||||
try {
|
||||
this.connection.send(JSON.stringify(request.payload));
|
||||
}
|
||||
catch (error) {
|
||||
request.callback(error);
|
||||
_this.responseQueue.delete(id);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Resets the providers, clears all callbacks
|
||||
*
|
||||
* @method reset
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.reset = function () {
|
||||
this.responseQueue.clear();
|
||||
this.requestQueue.clear();
|
||||
this.removeAllListeners();
|
||||
this._removeSocketListeners();
|
||||
this._addSocketListeners();
|
||||
};
|
||||
/**
|
||||
* Closes the current connection with the given code and reason arguments
|
||||
*
|
||||
* @method disconnect
|
||||
*
|
||||
* @param {number} code
|
||||
* @param {string} reason
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.disconnect = function (code, reason) {
|
||||
this._removeSocketListeners();
|
||||
this.connection.close(code || 1000, reason);
|
||||
};
|
||||
/**
|
||||
* Returns the desired boolean.
|
||||
*
|
||||
* @method supportsSubscriptions
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
WebsocketProvider.prototype.supportsSubscriptions = function () {
|
||||
return true;
|
||||
};
|
||||
/**
|
||||
* Removes the listeners and reconnects to the socket.
|
||||
*
|
||||
* @method reconnect
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.reconnect = function () {
|
||||
var _this = this;
|
||||
this.reconnecting = true;
|
||||
if (this.responseQueue.size > 0) {
|
||||
this.responseQueue.forEach(function (request, key) {
|
||||
request.callback(errors.PendingRequestsOnReconnectingError());
|
||||
_this.responseQueue.delete(key);
|
||||
});
|
||||
}
|
||||
if (!this.reconnectOptions.maxAttempts ||
|
||||
this.reconnectAttempts < this.reconnectOptions.maxAttempts) {
|
||||
setTimeout(function () {
|
||||
_this.reconnectAttempts++;
|
||||
_this._removeSocketListeners();
|
||||
_this.emit(_this.RECONNECT, _this.reconnectAttempts);
|
||||
_this.connect();
|
||||
}, this.reconnectOptions.delay);
|
||||
return;
|
||||
}
|
||||
this.emit(this.ERROR, errors.MaxAttemptsReachedOnReconnectingError());
|
||||
this.reconnecting = false;
|
||||
if (this.requestQueue.size > 0) {
|
||||
this.requestQueue.forEach(function (request, key) {
|
||||
request.callback(errors.MaxAttemptsReachedOnReconnectingError());
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
};
|
||||
module.exports = WebsocketProvider;
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"_from": "web3-providers-ws@1.3.0",
|
||||
"_id": "web3-providers-ws@1.3.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-Im5MthhJnJst8nSoq0TgbyOdaiFQFa5r6sHPOVllhgIgViDqzbnlAFW9sNzQ0Q8VXPNfPIQKi9cOrHlSRNPjRw==",
|
||||
"_location": "/web3-providers-ws",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "web3-providers-ws@1.3.0",
|
||||
"name": "web3-providers-ws",
|
||||
"escapedName": "web3-providers-ws",
|
||||
"rawSpec": "1.3.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.3.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/web3-core-requestmanager"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/web3-providers-ws/-/web3-providers-ws-1.3.0.tgz",
|
||||
"_shasum": "84adeff65acd4624d7f5bb43c5b2b22d8f0f63a4",
|
||||
"_spec": "web3-providers-ws@1.3.0",
|
||||
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\web3-core-requestmanager",
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"eventemitter3": "4.0.4",
|
||||
"underscore": "1.9.1",
|
||||
"web3-core-helpers": "1.3.0",
|
||||
"websocket": "^1.0.32"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Module to handle web3 RPC connections over WebSockets.",
|
||||
"devDependencies": {
|
||||
"dtslint": "^3.4.1",
|
||||
"typescript": "^3.9.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
},
|
||||
"gitHead": "a88944fff1f62b2b6f00e3de52446d24d9e92cc6",
|
||||
"license": "LGPL-3.0",
|
||||
"main": "lib/index.js",
|
||||
"name": "web3-providers-ws",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/ethereum/web3.js/tree/1.x/packages/web3-providers-ws"
|
||||
},
|
||||
"scripts": {
|
||||
"dtslint": "dtslint --localTs ../../node_modules/typescript/lib types",
|
||||
"tsc": "tsc -b tsconfig.json"
|
||||
},
|
||||
"types": "types/index.d.ts",
|
||||
"version": "1.3.0"
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
var isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
|
||||
var isRN = typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
|
||||
|
||||
var _btoa = null;
|
||||
var helpers = null;
|
||||
if (isNode || isRN) {
|
||||
_btoa = function(str) {
|
||||
return Buffer.from(str).toString('base64');
|
||||
};
|
||||
var url = require('url');
|
||||
if (url.URL) {
|
||||
// Use the new Node 6+ API for parsing URLs that supports username/password
|
||||
var newURL = url.URL;
|
||||
helpers = function(url) {
|
||||
return new newURL(url);
|
||||
};
|
||||
} else {
|
||||
// Web3 supports Node.js 5, so fall back to the legacy URL API if necessary
|
||||
helpers = require('url').parse;
|
||||
}
|
||||
} else {
|
||||
_btoa = btoa.bind(window);
|
||||
helpers = function(url) {
|
||||
return new URL(url);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseURL: helpers,
|
||||
btoa: _btoa
|
||||
};
|
||||
+425
@@ -0,0 +1,425 @@
|
||||
/*
|
||||
This file is part of web3.js.
|
||||
|
||||
web3.js is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
web3.js is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* @file WebsocketProvider.js
|
||||
* @authors: Samuel Furter <samuel@ethereum.org>, Fabian Vogelsteller <fabian@ethereum.org>
|
||||
* @date 2019
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var EventEmitter = require('eventemitter3');
|
||||
var helpers = require('./helpers.js');
|
||||
var errors = require('web3-core-helpers').errors;
|
||||
var Ws = require('websocket').w3cwebsocket;
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {Object} options
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
var WebsocketProvider = function WebsocketProvider(url, options) {
|
||||
EventEmitter.call(this);
|
||||
|
||||
options = options || {};
|
||||
this.url = url;
|
||||
this._customTimeout = options.timeout || 1000 * 15;
|
||||
this.headers = options.headers || {};
|
||||
this.protocol = options.protocol || undefined;
|
||||
this.reconnectOptions = Object.assign({
|
||||
auto: false,
|
||||
delay: 5000,
|
||||
maxAttempts: false,
|
||||
onTimeout: false
|
||||
},
|
||||
options.reconnect
|
||||
);
|
||||
this.clientConfig = options.clientConfig || undefined; // Allow a custom client configuration
|
||||
this.requestOptions = options.requestOptions || undefined; // Allow a custom request options (https://github.com/theturtle32/WebSocket-Node/blob/master/docs/WebSocketClient.md#connectrequesturl-requestedprotocols-origin-headers-requestoptions)
|
||||
|
||||
this.DATA = 'data';
|
||||
this.CLOSE = 'close';
|
||||
this.ERROR = 'error';
|
||||
this.CONNECT = 'connect';
|
||||
this.RECONNECT = 'reconnect';
|
||||
|
||||
this.connection = null;
|
||||
this.requestQueue = new Map();
|
||||
this.responseQueue = new Map();
|
||||
this.reconnectAttempts = 0;
|
||||
this.reconnecting = false;
|
||||
|
||||
// The w3cwebsocket implementation does not support Basic Auth
|
||||
// username/password in the URL. So generate the basic auth header, and
|
||||
// pass through with any additional headers supplied in constructor
|
||||
var parsedURL = helpers.parseURL(url);
|
||||
if (parsedURL.username && parsedURL.password) {
|
||||
this.headers.authorization = 'Basic ' + helpers.btoa(parsedURL.username + ':' + parsedURL.password);
|
||||
}
|
||||
|
||||
// When all node core implementations that do not have the
|
||||
// WHATWG compatible URL parser go out of service this line can be removed.
|
||||
if (parsedURL.auth) {
|
||||
this.headers.authorization = 'Basic ' + helpers.btoa(parsedURL.auth);
|
||||
}
|
||||
|
||||
// make property `connected` which will return the current connection status
|
||||
Object.defineProperty(this, 'connected', {
|
||||
get: function () {
|
||||
return this.connection && this.connection.readyState === this.connection.OPEN;
|
||||
},
|
||||
enumerable: true
|
||||
});
|
||||
|
||||
this.connect();
|
||||
};
|
||||
|
||||
// Inherit from EventEmitter
|
||||
WebsocketProvider.prototype = Object.create(EventEmitter.prototype);
|
||||
WebsocketProvider.prototype.constructor = WebsocketProvider;
|
||||
|
||||
/**
|
||||
* Connects to the configured node
|
||||
*
|
||||
* @method connect
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.connect = function () {
|
||||
this.connection = new Ws(this.url, this.protocol, undefined, this.headers, this.requestOptions, this.clientConfig);
|
||||
this._addSocketListeners();
|
||||
};
|
||||
|
||||
/**
|
||||
* Listener for the `data` event of the underlying WebSocket object
|
||||
*
|
||||
* @method _onMessage
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._onMessage = function (e) {
|
||||
var _this = this;
|
||||
|
||||
this._parseResponse((typeof e.data === 'string') ? e.data : '').forEach(function (result) {
|
||||
if (result.method && result.method.indexOf('_subscription') !== -1) {
|
||||
_this.emit(_this.DATA, result);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var id = result.id;
|
||||
|
||||
// get the id which matches the returned id
|
||||
if (Array.isArray(result)) {
|
||||
id = result[0].id;
|
||||
}
|
||||
|
||||
if (_this.responseQueue.has(id)) {
|
||||
if(_this.responseQueue.get(id).callback !== undefined) {
|
||||
_this.responseQueue.get(id).callback(false, result);
|
||||
}
|
||||
_this.responseQueue.delete(id);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Listener for the `open` event of the underlying WebSocket object
|
||||
*
|
||||
* @method _onConnect
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._onConnect = function () {
|
||||
this.emit(this.CONNECT);
|
||||
this.reconnectAttempts = 0;
|
||||
this.reconnecting = false;
|
||||
|
||||
if (this.requestQueue.size > 0) {
|
||||
var _this = this;
|
||||
|
||||
this.requestQueue.forEach(function (request, key) {
|
||||
_this.send(request.payload, request.callback);
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Listener for the `close` event of the underlying WebSocket object
|
||||
*
|
||||
* @method _onClose
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._onClose = function (event) {
|
||||
var _this = this;
|
||||
|
||||
if (this.reconnectOptions.auto && (![1000, 1001].includes(event.code) || event.wasClean === false)) {
|
||||
this.reconnect();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit(this.CLOSE, event);
|
||||
|
||||
if (this.requestQueue.size > 0) {
|
||||
this.requestQueue.forEach(function (request, key) {
|
||||
request.callback(errors.ConnectionNotOpenError(event));
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
|
||||
if (this.responseQueue.size > 0) {
|
||||
this.responseQueue.forEach(function (request, key) {
|
||||
request.callback(errors.InvalidConnection('on WS', event));
|
||||
_this.responseQueue.delete(key);
|
||||
});
|
||||
}
|
||||
|
||||
this._removeSocketListeners();
|
||||
this.removeAllListeners();
|
||||
};
|
||||
|
||||
/**
|
||||
* Will add the required socket listeners
|
||||
*
|
||||
* @method _addSocketListeners
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._addSocketListeners = function () {
|
||||
this.connection.addEventListener('message', this._onMessage.bind(this));
|
||||
this.connection.addEventListener('open', this._onConnect.bind(this));
|
||||
this.connection.addEventListener('close', this._onClose.bind(this));
|
||||
};
|
||||
|
||||
/**
|
||||
* Will remove all socket listeners
|
||||
*
|
||||
* @method _removeSocketListeners
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype._removeSocketListeners = function () {
|
||||
this.connection.removeEventListener('message', this._onMessage);
|
||||
this.connection.removeEventListener('open', this._onConnect);
|
||||
this.connection.removeEventListener('close', this._onClose);
|
||||
};
|
||||
|
||||
/**
|
||||
* Will parse the response and make an array out of it.
|
||||
*
|
||||
* @method _parseResponse
|
||||
*
|
||||
* @param {String} data
|
||||
*
|
||||
* @returns {Array}
|
||||
*/
|
||||
WebsocketProvider.prototype._parseResponse = function (data) {
|
||||
var _this = this,
|
||||
returnValues = [];
|
||||
|
||||
// DE-CHUNKER
|
||||
var dechunkedData = data
|
||||
.replace(/\}[\n\r]?\{/g, '}|--|{') // }{
|
||||
.replace(/\}\][\n\r]?\[\{/g, '}]|--|[{') // }][{
|
||||
.replace(/\}[\n\r]?\[\{/g, '}|--|[{') // }[{
|
||||
.replace(/\}\][\n\r]?\{/g, '}]|--|{') // }]{
|
||||
.split('|--|');
|
||||
|
||||
dechunkedData.forEach(function (data) {
|
||||
|
||||
// prepend the last chunk
|
||||
if (_this.lastChunk)
|
||||
data = _this.lastChunk + data;
|
||||
|
||||
var result = null;
|
||||
|
||||
try {
|
||||
result = JSON.parse(data);
|
||||
} catch (e) {
|
||||
|
||||
_this.lastChunk = data;
|
||||
|
||||
// start timeout to cancel all requests
|
||||
clearTimeout(_this.lastChunkTimeout);
|
||||
_this.lastChunkTimeout = setTimeout(function () {
|
||||
if (_this.reconnectOptions.auto && _this.reconnectOptions.onTimeout) {
|
||||
_this.reconnect();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
_this.emit(_this.ERROR, errors.ConnectionTimeout(_this._customTimeout));
|
||||
|
||||
if (_this.requestQueue.size > 0) {
|
||||
_this.requestQueue.forEach(function (request, key) {
|
||||
request.callback(errors.ConnectionTimeout(_this._customTimeout));
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
}, _this._customTimeout);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// cancel timeout and set chunk to null
|
||||
clearTimeout(_this.lastChunkTimeout);
|
||||
_this.lastChunk = null;
|
||||
|
||||
if (result)
|
||||
returnValues.push(result);
|
||||
});
|
||||
|
||||
return returnValues;
|
||||
};
|
||||
|
||||
/**
|
||||
* Does check if the provider is connecting and will add it to the queue or will send it directly
|
||||
*
|
||||
* @method send
|
||||
*
|
||||
* @param {Object} payload
|
||||
* @param {Function} callback
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.send = function (payload, callback) {
|
||||
var _this = this;
|
||||
var id = payload.id;
|
||||
var request = {payload: payload, callback: callback};
|
||||
|
||||
if (Array.isArray(payload)) {
|
||||
id = payload[0].id;
|
||||
}
|
||||
|
||||
if (this.connection.readyState === this.connection.CONNECTING || this.reconnecting) {
|
||||
this.requestQueue.set(id, request);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.connection.readyState !== this.connection.OPEN) {
|
||||
this.requestQueue.delete(id);
|
||||
|
||||
this.emit(this.ERROR, errors.ConnectionNotOpenError());
|
||||
request.callback(errors.ConnectionNotOpenError());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.responseQueue.set(id, request);
|
||||
this.requestQueue.delete(id);
|
||||
|
||||
try {
|
||||
this.connection.send(JSON.stringify(request.payload));
|
||||
} catch (error) {
|
||||
request.callback(error);
|
||||
_this.responseQueue.delete(id);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Resets the providers, clears all callbacks
|
||||
*
|
||||
* @method reset
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.reset = function () {
|
||||
this.responseQueue.clear();
|
||||
this.requestQueue.clear();
|
||||
|
||||
this.removeAllListeners();
|
||||
|
||||
this._removeSocketListeners();
|
||||
this._addSocketListeners();
|
||||
};
|
||||
|
||||
/**
|
||||
* Closes the current connection with the given code and reason arguments
|
||||
*
|
||||
* @method disconnect
|
||||
*
|
||||
* @param {number} code
|
||||
* @param {string} reason
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.disconnect = function (code, reason) {
|
||||
this._removeSocketListeners();
|
||||
this.connection.close(code || 1000, reason);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the desired boolean.
|
||||
*
|
||||
* @method supportsSubscriptions
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
WebsocketProvider.prototype.supportsSubscriptions = function () {
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the listeners and reconnects to the socket.
|
||||
*
|
||||
* @method reconnect
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
WebsocketProvider.prototype.reconnect = function () {
|
||||
var _this = this;
|
||||
this.reconnecting = true;
|
||||
|
||||
if (this.responseQueue.size > 0) {
|
||||
this.responseQueue.forEach(function (request, key) {
|
||||
request.callback(errors.PendingRequestsOnReconnectingError());
|
||||
_this.responseQueue.delete(key);
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
!this.reconnectOptions.maxAttempts ||
|
||||
this.reconnectAttempts < this.reconnectOptions.maxAttempts
|
||||
) {
|
||||
setTimeout(function () {
|
||||
_this.reconnectAttempts++;
|
||||
_this._removeSocketListeners();
|
||||
_this.emit(_this.RECONNECT, _this.reconnectAttempts);
|
||||
_this.connect();
|
||||
}, this.reconnectOptions.delay);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit(this.ERROR, errors.MaxAttemptsReachedOnReconnectingError());
|
||||
this.reconnecting = false;
|
||||
|
||||
if (this.requestQueue.size > 0) {
|
||||
this.requestQueue.forEach(function (request, key) {
|
||||
request.callback(errors.MaxAttemptsReachedOnReconnectingError());
|
||||
_this.requestQueue.delete(key);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = WebsocketProvider;
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./lib"
|
||||
},
|
||||
"include": [
|
||||
"./src"
|
||||
]
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": ["es6"],
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noEmit": true,
|
||||
"allowSyntheticDefaultImports": false,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"web3-providers": ["."]
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "dtslint/dtslint.json",
|
||||
"rules": {
|
||||
"semicolon": false,
|
||||
"no-import-default-of-export-equals": false,
|
||||
"file-name-casing": [true, "kebab-case"],
|
||||
"whitespace": false,
|
||||
"no-unnecessary-class": false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user