From 504afda7a018f63ecb23cbb64a2936eb015e5c5d Mon Sep 17 00:00:00 2001 From: Pedro Branco Date: Fri, 27 Sep 2019 12:18:38 +0100 Subject: [PATCH] Release 3.0.0 --- CHANGELOG.md | 12 +++- dist/src/index.js | 151 +++++++++++++++++++-------------------------- dist/src/parser.js | 24 +++---- package.json | 2 +- 4 files changed, 88 insertions(+), 101 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4306cf2..d909d03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,16 @@ # Changelog -## [2.3.0](https://github.com/ruimarinho/bitcoin-core/tree/2.3.0) (2019-09-27) +## [3.0.0](https://github.com/ruimarinho/bitcoin-core/tree/3.0.0) (2019-09-27) -[Full Changelog](https://github.com/ruimarinho/bitcoin-core/compare/v2.2.1...2.3.0) +[Full Changelog](https://github.com/ruimarinho/bitcoin-core/compare/v2.3.0...3.0.0) + +**Merged pull requests:** + +- Remove bluebird dependency and callbacks support [\#87](https://github.com/ruimarinho/bitcoin-core/pull/87) ([pedrobranco](https://github.com/pedrobranco)) + +## [v2.3.0](https://github.com/ruimarinho/bitcoin-core/tree/v2.3.0) (2019-09-27) + +[Full Changelog](https://github.com/ruimarinho/bitcoin-core/compare/v2.2.1...v2.3.0) **Merged pull requests:** diff --git a/dist/src/index.js b/dist/src/index.js index eb2a8e1..7a1f3b0 100755 --- a/dist/src/index.js +++ b/dist/src/index.js @@ -7,8 +7,6 @@ exports.default = void 0; var _parser = _interopRequireDefault(require("./parser")); -var _bluebird = _interopRequireDefault(require("bluebird")); - var _requester = _interopRequireDefault(require("./requester")); var _lodash = _interopRequireDefault(require("lodash")); @@ -27,35 +25,33 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de * Module dependencies. */ -/** - * Source arguments to find out if a callback has been passed. - */ -function source(...args) { - const last = _lodash.default.last(args); - - let callback; - - if (_lodash.default.isFunction(last)) { - callback = last; - args = _lodash.default.dropRight(args); - } - - return [args, callback]; -} /** * List of networks and their default port mapping. */ - - const networks = { mainnet: 8332, regtest: 18332, testnet: 18332 }; +/** + * Promisify helper. + */ + +const promisify = fn => (...args) => new Promise((resolve, reject) => { + fn(...args, (error, value) => { + if (error) { + reject(error); + return; + } + + resolve(value); + }); +}); /** * Constructor. */ + class Client { constructor({ agentOptions, @@ -121,14 +117,14 @@ class Client { }; }, {}); const request = (0, _requestLogger.default)(logger); - this.request = _bluebird.default.promisifyAll(request.defaults({ + this.request = request.defaults({ agentOptions: this.agentOptions, baseUrl: `${this.ssl.enabled ? 'https' : 'http'}://${this.host}:${this.port}`, strictSSL: this.ssl.strict, timeout: this.timeout - }), { - multiArgs: true }); + this.request.getAsync = promisify(this.request.get); + this.request.postAsync = promisify(this.request.post); this.requester = new _requester.default({ methods: this.methods, version @@ -142,21 +138,13 @@ class Client { */ - command(...args) { + async command(...args) { let body; - let callback; let multiwallet; let [input, ...parameters] = args; // eslint-disable-line prefer-const - const lastArg = _lodash.default.last(parameters); - const isBatch = Array.isArray(input); - if (_lodash.default.isFunction(lastArg)) { - callback = lastArg; - parameters = _lodash.default.dropRight(parameters); - } - if (isBatch) { multiwallet = _lodash.default.some(input, command => { return _lodash.default.get(this.methods[command.method], 'features.multiwallet.supported', false) === true; @@ -178,29 +166,24 @@ class Client { }); } - return _bluebird.default.try(() => { - return this.request.postAsync({ - auth: _lodash.default.pickBy(this.auth, _lodash.default.identity), - body: JSON.stringify(body), - uri: `${multiwallet && this.wallet ? `/wallet/${this.wallet}` : '/'}` - }).bind(this).then(this.parser.rpc); - }).asCallback(callback); + return this.parser.rpc((await this.request.postAsync({ + auth: _lodash.default.pickBy(this.auth, _lodash.default.identity), + body: JSON.stringify(body), + uri: `${multiwallet && this.wallet ? `/wallet/${this.wallet}` : '/'}` + }))); } /** * Given a transaction hash, returns a transaction in binary, hex-encoded binary, or JSON formats. */ - getTransactionByHash(...args) { - const [[hash, { - extension = 'json' - } = {}], callback] = source(...args); - return _bluebird.default.try(() => { - return this.request.getAsync({ - encoding: extension === 'bin' ? null : undefined, - url: `/rest/tx/${hash}.${extension}` - }).bind(this).then(_lodash.default.partial(this.parser.rest, extension)); - }).asCallback(callback); + async getTransactionByHash(hash, { + extension = 'json' + } = {}) { + return this.parser.rest(extension, (await this.request.getAsync({ + encoding: extension === 'bin' ? null : undefined, + url: `/rest/tx/${hash}.${extension}` + }))); } /** * Given a block hash, returns a block, in binary, hex-encoded binary or JSON formats. @@ -209,33 +192,31 @@ class Client { */ - getBlockByHash(...args) { - const [[hash, { - summary = false, - extension = 'json' - } = {}], callback] = source(...args); - return _bluebird.default.try(() => { - return this.request.getAsync({ - encoding: extension === 'bin' ? null : undefined, - url: `/rest/block${summary ? '/notxdetails/' : '/'}${hash}.${extension}` - }).bind(this).then(_lodash.default.partial(this.parser.rest, extension)); - }).asCallback(callback); + async getBlockByHash(hash, { + summary = false, + extension = 'json' + } = {}) { + const encoding = extension === 'bin' ? null : undefined; + const url = `/rest/block${summary ? '/notxdetails/' : '/'}${hash}.${extension}`; + return this.parser.rest(extension, (await this.request.getAsync({ + encoding, + url + }))); } /** * Given a block hash, returns amount of blockheaders in upward direction. */ - getBlockHeadersByHash(...args) { - const [[hash, count, { - extension = 'json' - } = {}], callback] = source(...args); - return _bluebird.default.try(() => { - return this.request.getAsync({ - encoding: extension === 'bin' ? null : undefined, - url: `/rest/headers/${count}/${hash}.${extension}` - }).bind(this).then(_lodash.default.partial(this.parser.rest, extension)); - }).asCallback(callback); + async getBlockHeadersByHash(hash, count, { + extension = 'json' + } = {}) { + const encoding = extension === 'bin' ? null : undefined; + const url = `/rest/headers/${count}/${hash}.${extension}`; + return this.parser.rest(extension, (await this.request.getAsync({ + encoding, + url + }))); } /** * Returns various state info regarding block chain processing. @@ -243,9 +224,8 @@ class Client { */ - getBlockchainInformation(...args) { - const [, callback] = source(...args); - return this.request.getAsync(`/rest/chaininfo.json`).bind(this).then(_lodash.default.partial(this.parser.rest, 'json')).asCallback(callback); + async getBlockchainInformation() { + return this.parser.rest('json', (await this.request.getAsync(`/rest/chaininfo.json`))); } /** * Query unspent transaction outputs for a given set of outpoints. @@ -254,19 +234,20 @@ class Client { */ - getUnspentTransactionOutputs(...args) { - const [[outpoints, { - extension = 'json' - } = {}], callback] = source(...args); + async getUnspentTransactionOutputs(outpoints, { + extension = 'json' + } = {}) { + const encoding = extension === 'bin' ? null : undefined; const sets = _lodash.default.flatten([outpoints]).map(outpoint => { return `${outpoint.id}-${outpoint.index}`; }).join('/'); - return this.request.getAsync({ - encoding: extension === 'bin' ? null : undefined, - url: `/rest/getutxos/checkmempool/${sets}.${extension}` - }).bind(this).then(_lodash.default.partial(this.parser.rest, extension)).asCallback(callback); + const url = `/rest/getutxos/checkmempool/${sets}.${extension}`; + return this.parser.rest(extension, (await this.request.getAsync({ + encoding, + url + }))); } /** * Returns transactions in the transaction memory pool. @@ -274,9 +255,8 @@ class Client { */ - getMemoryPoolContent(...args) { - const [, callback] = source(...args); - return this.request.getAsync('/rest/mempool/contents.json').bind(this).then(_lodash.default.partial(this.parser.rest, 'json')).asCallback(callback); + async getMemoryPoolContent() { + return this.parser.rest('json', (await this.request.getAsync('/rest/mempool/contents.json'))); } /** * Returns various information about the transaction memory pool. @@ -288,9 +268,8 @@ class Client { */ - getMemoryPoolInformation(...args) { - const [, callback] = source(...args); - return this.request.getAsync('/rest/mempool/info.json').bind(this).then(_lodash.default.partial(this.parser.rest, 'json')).asCallback(callback); + async getMemoryPoolInformation() { + return this.parser.rest('json', (await this.request.getAsync('/rest/mempool/info.json'))); } } diff --git a/dist/src/parser.js b/dist/src/parser.js index 6d402d9..e3ac0ec 100644 --- a/dist/src/parser.js +++ b/dist/src/parser.js @@ -66,17 +66,17 @@ class Parser { */ - rpc([response, body]) { + rpc(response) { // The RPC api returns a `text/html; charset=ISO-8859-1` encoded response with an empty string as the body // when an error occurs. - if (typeof body === 'string' && response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) { + if (typeof response.body === 'string' && response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) { throw new _rpcError.default(response.statusCode, response.statusMessage, { - body + body: response.body }); } // Parsing the body with custom parser to support BigNumbers. - body = parse(body); + const body = parse(response.body); if (!Array.isArray(body)) { return getRpcResult(body, { @@ -104,31 +104,31 @@ class Parser { return batch; } - rest(extension, [response, body]) { + rest(extension, response) { // The REST api returns a `text/plain` encoded response with the error line and the control // characters \r\n. For readability and debuggability, the error message is set to this content. // When requesting a binary response, the body will be returned as a Buffer representation of // this error string. if (response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) { - if (body instanceof Buffer) { - body = body.toString('utf-8'); + if (response.body instanceof Buffer) { + response.body = response.body.toString('utf-8'); } - throw new _rpcError.default(response.statusCode, body.replace('\r\n', ''), { - body + throw new _rpcError.default(response.statusCode, response.body.replace('\r\n', ''), { + body: response.body }); } // Parsing the body with custom parser to support BigNumbers. if (extension === 'json') { - body = parse(body); + response.body = parse(response.body); } if (this.headers) { - return [body, response.headers]; + return [response.body, response.headers]; } - return body; + return response.body; } } diff --git a/package.json b/package.json index 799aadd..e1a697a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitcoin-core", - "version": "2.3.0", + "version": "3.0.0", "description": "A modern Bitcoin Core REST and RPC client.", "keywords": [ "bitcoin",