web3-core-requestmanager
Advanced tools
Comparing version 1.2.6 to 1.2.7-rc.0
{ | ||
"name": "web3-core-requestmanager", | ||
"version": "1.2.6", | ||
"version": "1.2.7-rc.0", | ||
"description": "Web3 module to handle requests to external providers.", | ||
@@ -13,8 +13,8 @@ "repository": "https://github.com/ethereum/web3.js/tree/1.x/packages/web3-core-requestmanager", | ||
"underscore": "1.9.1", | ||
"web3-core-helpers": "1.2.6", | ||
"web3-providers-http": "1.2.6", | ||
"web3-providers-ipc": "1.2.6", | ||
"web3-providers-ws": "1.2.6" | ||
"web3-core-helpers": "1.2.7-rc.0", | ||
"web3-providers-http": "1.2.7-rc.0", | ||
"web3-providers-ipc": "1.2.7-rc.0", | ||
"web3-providers-ws": "1.2.7-rc.0" | ||
}, | ||
"gitHead": "c20bcf09b04f773406ce3532e88fd105bb04e244" | ||
"gitHead": "598e53163890660670c46d84bb6a6cbee4693e41" | ||
} |
# web3-core-requestmanager | ||
This is a sub package of [web3.js][repo] | ||
[![NPM Package][npm-image]][npm-url] [![Dependency Status][deps-image]][deps-url] [![Dev Dependency Status][deps-dev-image]][deps-dev-url] | ||
The requestmanager package is used by most [web3.js][repo] packages. | ||
This is a sub-package of [web3.js][repo]. | ||
This requestmanager package is used by most [web3.js][repo] packages. | ||
Please read the [documentation][docs] for more. | ||
@@ -38,6 +41,10 @@ | ||
[docs]: http://web3js.readthedocs.io/en/1.0/ | ||
[repo]: https://github.com/ethereum/web3.js | ||
[npm-image]: https://img.shields.io/npm/v/web3-core-requestmanager.svg | ||
[npm-url]: https://npmjs.org/package/web3-core-requestmanager | ||
[deps-image]: https://david-dm.org/ethereum/web3.js/1.x/status.svg?path=packages/web3-core-requestmanager | ||
[deps-url]: https://david-dm.org/ethereum/web3.js/1.x?path=packages/web3-core-requestmanager | ||
[deps-dev-image]: https://david-dm.org/ethereum/web3.js/1.x/dev-status.svg?path=packages/web3-core-requestmanager | ||
[deps-dev-url]: https://david-dm.org/ethereum/web3.js/1.x?type=dev&path=packages/web3-core-requestmanager | ||
171
src/index.js
@@ -32,5 +32,3 @@ /* | ||
/** | ||
/** | ||
* It's responsible for passing messages to providers | ||
@@ -40,13 +38,17 @@ * It's also responsible for polling the ethereum node for incoming messages | ||
* Singleton | ||
* | ||
* @param {string|Object}provider | ||
* @param {Net.Socket} net | ||
* | ||
* @constructor | ||
*/ | ||
var RequestManager = function RequestManager(provider) { | ||
var RequestManager = function RequestManager(provider, net) { | ||
this.provider = null; | ||
this.providers = RequestManager.providers; | ||
this.setProvider(provider); | ||
this.subscriptions = {}; | ||
this.setProvider(provider, net); | ||
this.subscriptions = new Map(); | ||
}; | ||
RequestManager.givenProvider = givenProvider; | ||
@@ -61,3 +63,2 @@ | ||
/** | ||
@@ -67,27 +68,32 @@ * Should be used to set provider of request manager | ||
* @method setProvider | ||
* @param {Object} p | ||
* | ||
* @param {Object} provider | ||
* @param {net.Socket} net | ||
* | ||
* @returns void | ||
*/ | ||
RequestManager.prototype.setProvider = function (p, net) { | ||
RequestManager.prototype.setProvider = function (provider, net) { | ||
var _this = this; | ||
// autodetect provider | ||
if(p && typeof p === 'string' && this.providers) { | ||
if (provider && typeof provider === 'string' && this.providers) { | ||
// HTTP | ||
if(/^http(s)?:\/\//i.test(p)) { | ||
p = new this.providers.HttpProvider(p); | ||
if (/^http(s)?:\/\//i.test(provider)) { | ||
provider = new this.providers.HttpProvider(provider); | ||
// WS | ||
} else if(/^ws(s)?:\/\//i.test(p)) { | ||
p = new this.providers.WebsocketProvider(p); | ||
} else if (/^ws(s)?:\/\//i.test(provider)) { | ||
provider = new this.providers.WebsocketProvider(provider); | ||
// IPC | ||
} else if(p && typeof net === 'object' && typeof net.connect === 'function') { | ||
p = new this.providers.IpcProvider(p, net); | ||
} else if (provider && typeof net === 'object' && typeof net.connect === 'function') { | ||
provider = new this.providers.IpcProvider(provider, net); | ||
} else if(p) { | ||
throw new Error('Can\'t autodetect provider for "'+ p +'"'); | ||
} else if (provider) { | ||
throw new Error('Can\'t autodetect provider for "' + provider + '"'); | ||
} | ||
} | ||
// reset the old one before changing, if still connected | ||
@@ -97,27 +103,46 @@ if(this.provider && this.provider.connected) | ||
this.provider = provider || null; | ||
this.provider = p || null; | ||
// listen to incoming notifications | ||
if(this.provider && this.provider.on) { | ||
this.provider.on('data', function requestManagerNotification(result, deprecatedResult){ | ||
if (this.provider && this.provider.on) { | ||
this.provider.on('data', function data(result, deprecatedResult) { | ||
result = result || deprecatedResult; // this is for possible old providers, which may had the error first handler | ||
// check for result.method, to prevent old providers errors to pass as result | ||
if(result.method && _this.subscriptions[result.params.subscription] && _this.subscriptions[result.params.subscription].callback) { | ||
_this.subscriptions[result.params.subscription].callback(null, result.params.result); | ||
if (result.method && _this.subscriptions.has(result.params.subscription)) { | ||
_this.subscriptions.get(result.params.subscription).callback(null, result.params.result); | ||
} | ||
}); | ||
// TODO add error, end, timeout, connect?? | ||
// this.provider.on('error', function requestManagerNotification(result){ | ||
// Object.keys(_this.subscriptions).forEach(function(id){ | ||
// if(_this.subscriptions[id].callback) | ||
// _this.subscriptions[id].callback(err); | ||
// }); | ||
// } | ||
// resubscribe if the provider has reconnected | ||
this.provider.on('connect', function connect() { | ||
_this.subscriptions.forEach(function (subscription) { | ||
subscription.subscription.resubscribe(); | ||
}); | ||
}); | ||
// notify all subscriptions about the error condition | ||
this.provider.on('error', function error(error) { | ||
_this.subscriptions.forEach(function (subscription) { | ||
subscription.callback(error); | ||
}); | ||
}); | ||
// notify all subscriptions about bad close conditions | ||
this.provider.on('close', function close(event) { | ||
if (!_this._isCleanCloseEvent(event) || _this._isIpcCloseError(event)){ | ||
_this.subscriptions.forEach(function (subscription) { | ||
subscription.callback(errors.ConnectionCloseError(event)); | ||
_this.subscriptions.delete(subscription.subscription.id); | ||
}); | ||
} | ||
}); | ||
// TODO add end, timeout?? | ||
} | ||
}; | ||
/** | ||
* TODO: This method should be implemented with a Promise instead of a callback | ||
* | ||
* Should be used to asynchronously send request | ||
@@ -130,3 +155,4 @@ * | ||
RequestManager.prototype.send = function (data, callback) { | ||
callback = callback || function(){}; | ||
callback = callback || function () { | ||
}; | ||
@@ -158,6 +184,8 @@ if (!this.provider) { | ||
/** | ||
* TODO: This method should be implemented with a Promise instead of a callback | ||
* | ||
* Should be called to asynchronously send batch request | ||
* | ||
* @method sendBatch | ||
* @param {Array} batch data | ||
* @param {Array} data - array of payload objects | ||
* @param {Function} callback | ||
@@ -189,15 +217,15 @@ */ | ||
* @method addSubscription | ||
* @param {String} id the subscription id | ||
* @param {String} name the subscription name | ||
* @param {Subscription} subscription the subscription | ||
* @param {String} type the subscription namespace (eth, personal, etc) | ||
* @param {Function} callback the callback to call for incoming notifications | ||
*/ | ||
RequestManager.prototype.addSubscription = function (id, name, type, callback) { | ||
if(this.provider.on) { | ||
this.subscriptions[id] = { | ||
callback: callback, | ||
type: type, | ||
name: name | ||
}; | ||
RequestManager.prototype.addSubscription = function (subscription, callback) { | ||
if (this.provider.on) { | ||
this.subscriptions.set( | ||
subscription.id, | ||
{ | ||
callback: callback, | ||
subscription: subscription | ||
} | ||
); | ||
} else { | ||
@@ -216,14 +244,21 @@ throw new Error('The provider doesn\'t support subscriptions: '+ this.provider.constructor.name); | ||
RequestManager.prototype.removeSubscription = function (id, callback) { | ||
var _this = this; | ||
if (this.subscriptions.has(id)) { | ||
var type = this.subscriptions.get(id).subscription.options.type; | ||
if(this.subscriptions[id]) { | ||
// remove subscription first to avoid reentry | ||
this.subscriptions.delete(id); | ||
// then, try to actually unsubscribe | ||
this.send({ | ||
method: this.subscriptions[id].type + '_unsubscribe', | ||
method: type + '_unsubscribe', | ||
params: [id] | ||
}, callback); | ||
// remove subscription | ||
delete _this.subscriptions[id]; | ||
return; | ||
} | ||
if (typeof callback === 'function') { | ||
// call the callback if the subscription was already removed | ||
callback(null); | ||
} | ||
}; | ||
@@ -239,7 +274,6 @@ | ||
if (this.subscriptions) { | ||
// uninstall all subscriptions | ||
Object.keys(this.subscriptions).forEach(function(id){ | ||
if(!keepIsSyncing || _this.subscriptions[id].name !== 'syncing') | ||
// uninstall all subscriptions | ||
if (this.subscriptions.size > 0) { | ||
this.subscriptions.forEach(function (value, id) { | ||
if (!keepIsSyncing || value.name !== 'syncing') | ||
_this.removeSubscription(id); | ||
@@ -249,3 +283,2 @@ }); | ||
// reset notification callbacks etc. | ||
@@ -256,2 +289,28 @@ if(this.provider.reset) | ||
/** | ||
* Evaluates WS close event | ||
* | ||
* @method _isCleanClose | ||
* | ||
* @param {CloseEvent | boolean} event WS close event or exception flag | ||
* | ||
* @returns {boolean} | ||
*/ | ||
RequestManager.prototype._isCleanCloseEvent = function (event) { | ||
return typeof event === 'object' && ([1000].includes(event.code) || event.wasClean === true); | ||
}; | ||
/** | ||
* Detects Ipc close error. The node.net module emits ('close', isException) | ||
* | ||
* @method _isIpcCloseError | ||
* | ||
* @param {CloseEvent | boolean} event WS close event or exception flag | ||
* | ||
* @returns {boolean} | ||
*/ | ||
RequestManager.prototype._isIpcCloseError = function (event) { | ||
return typeof event === 'boolean' && event; | ||
}; | ||
module.exports = { | ||
@@ -258,0 +317,0 @@ Manager: RequestManager, |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
19740
465
50
2
1
+ Addedeventemitter3@4.0.7(transitive)
+ Addedweb3-core-helpers@1.2.7-rc.0(transitive)
+ Addedweb3-eth-iban@1.2.7-rc.0(transitive)
+ Addedweb3-providers-http@1.2.7-rc.0(transitive)
+ Addedweb3-providers-ipc@1.2.7-rc.0(transitive)
+ Addedweb3-providers-ws@1.2.7-rc.0(transitive)
+ Addedweb3-utils@1.2.7-rc.0(transitive)
- Removedweb3-core-helpers@1.2.6(transitive)
- Removedweb3-eth-iban@1.2.6(transitive)
- Removedweb3-providers-http@1.2.6(transitive)
- Removedweb3-providers-ipc@1.2.6(transitive)
- Removedweb3-providers-ws@1.2.6(transitive)
- Removedweb3-utils@1.2.6(transitive)
Updatedweb3-core-helpers@1.2.7-rc.0
Updatedweb3-providers-ws@1.2.7-rc.0