1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-22 01:36:56 +01:00

Merge pull request #312 from bigchaindb/fet-improve-requests

feat: improve requests
This commit is contained in:
getlarge 2022-02-17 06:03:53 +01:00 committed by GitHub
commit f9a4675726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 203 additions and 114 deletions

View File

@ -193,7 +193,7 @@ module.exports = {
],
},
],
'import/no-extraneous-dependencies': ['error', { 'devDependencies': true }],
/**
* ES6-specific Issues
* (http://eslint.org/docs/rules/#ecmascript-6)

View File

@ -45,6 +45,11 @@ jobs:
restore-keys: |
${{ runner.os }}-node-
- name: Run BigChainDB node
run: |
echo Building and starting up docker containers
docker-compose -f ./docker-compose.yml up -d
- name: Install dependencies
env:
HUSKY_SKIP_INSTALL: 'true'
@ -56,10 +61,8 @@ jobs:
- name: Build
run: npm run build
- name: Build docker
run: |
echo Building and starting up docker containers
docker-compose -f ./docker-compose.yml up
# ensure BCDB node is up and running
- run: sleep 20
- name: Test
run: npm run test

View File

@ -20,6 +20,7 @@
"sideEffects": false,
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"build": "npm run clean && npm run build:cjs && npm run build:dist",
"build:bundle": "webpack",
"build:cjs": "cross-env BABEL_ENV=cjs babel ./src -d dist/node",
@ -36,55 +37,54 @@
"doc": "documentation build src/index.js -f md -o API.md -g --markdown-toc"
},
"devDependencies": {
"@ava/babel": "^1.0.1",
"@babel/cli": "^7.13.0",
"@babel/core": "^7.13.8",
"@babel/eslint-parser": "^7.13.8",
"@babel/plugin-proposal-export-default-from": "^7.12.13",
"@babel/plugin-proposal-object-rest-spread": "^7.13.8",
"@ava/babel": "^2.0.0",
"@babel/cli": "^7.17.0",
"@babel/core": "^7.17.2",
"@babel/eslint-parser": "^7.17.0",
"@babel/plugin-proposal-export-default-from": "^7.16.7",
"@babel/plugin-proposal-object-rest-spread": "^7.16.7",
"@babel/plugin-syntax-async-generators": "^7.8.4",
"@babel/plugin-transform-async-to-generator": "^7.13.0",
"@babel/plugin-transform-object-assign": "^7.12.13",
"@babel/plugin-transform-regenerator": "^7.12.13",
"@babel/plugin-transform-runtime": "^7.13.9",
"@babel/preset-env": "^7.13.9",
"@babel/register": "^7.13.8",
"@babel/plugin-transform-async-to-generator": "^7.16.8",
"@babel/plugin-transform-object-assign": "^7.16.7",
"@babel/plugin-transform-regenerator": "^7.16.7",
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/preset-env": "^7.16.11",
"@babel/register": "^7.17.0",
"ava": "^3.15.0",
"babel-loader": "^8.2.2",
"buffer": "^6.0.3",
"codecov": "^3.8.1",
"cross-env": "^7.0.3",
"documentation": "^13.1.1",
"eslint": "^7.21.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"husky": "^5.1.3",
"lint-staged": "^10.5.4",
"documentation": "^13.2.5",
"eslint": "^8.9.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.25.4",
"husky": "^7.0.4",
"lint-staged": "^12.3.4",
"nyc": "^15.1.0",
"release-it": "^14.4.1",
"rewire": "^4.0.1",
"release-it": "^14.12.4",
"rewire": "^6.0.0",
"rimraf": "^3.0.2",
"sinon": "^7.3.2",
"terser-webpack-plugin": "^4.2.3",
"webpack": "^4.46.0",
"webpack-cli": "^4.5.0",
"webpack-concat-plugin": "^3.0.0",
"webpack-merge": "^5.7.3",
"webpack-sources": "^2.2.0"
"sinon": "^13.0.1",
"terser-webpack-plugin": "^5.3.1",
"webpack": "^5.68.0",
"webpack-cli": "^4.9.2",
"webpack-merge": "^5.8.0",
"webpack-sources": "^3.2.3"
},
"dependencies": {
"@babel/runtime-corejs3": "^7.13.9",
"browser-resolve": "^1.11.3",
"@babel/runtime-corejs3": "^7.17.2",
"abort-controller": "^3.0.0",
"bs58": "^4.0.1",
"buffer": "^6.0.3",
"clone": "^2.1.2",
"core-js": "^3.9.1",
"crypto-conditions": "2.1.2",
"core-js": "^3.21.0",
"crypto-conditions": "2.2.1",
"decamelize": "^5.0.0",
"es6-promise": "^4.2.8",
"fetch-ponyfill": "^7.1.0",
"js-sha3": "^0.8.0",
"json-stable-stringify": "^1.0.1",
"query-string": "^6.14.1",
"query-string": "^7.1.1",
"sprintf-js": "^1.1.2",
"tweetnacl": "^1.0.3"
},

View File

@ -2,6 +2,8 @@
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0
// TODO: remove abort-controller when using Node >=15
import AbortController from 'abort-controller'
import { Promise } from 'es6-promise'
import fetchPonyfill from 'fetch-ponyfill'
import { vsprintf } from 'sprintf-js'
@ -9,14 +11,14 @@ import { vsprintf } from 'sprintf-js'
import formatText from './format_text'
import stringifyAsQueryParam from './stringify_as_query_param'
const fetch = fetchPonyfill(Promise)
const fetch = fetchPonyfill({ Promise })
export function ResponseError(message, status, requestURI) {
this.name = 'ResponseError'
this.message = message
this.status = status
this.requestURI = requestURI
this.stack = (new Error()).stack
this.stack = new Error().stack
}
ResponseError.prototype = new Error()
@ -26,17 +28,27 @@ ResponseError.prototype = new Error()
* Timeout function following https://github.com/github/fetch/issues/175#issuecomment-284787564
* @param {integer} obj Source object
* @param {Promise} filter Array of key names to select or function to invoke per iteration
* @param {AbortController} controller AbortController instance bound to fetch
* @return {Object} TimeoutError if the time was consumed, otherwise the Promise will be resolved
*/
function timeout(ms, promise) {
function timeout(ms, promise, controller) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const nodeTimeout = setTimeout(() => {
controller.abort()
const errorObject = {
message: 'TimeoutError'
message: 'TimeoutError',
}
reject(new Error(errorObject))
}, ms)
promise.then(resolve, reject)
promise
.then((res) => {
clearTimeout(nodeTimeout)
resolve(res)
})
.catch((err) => {
clearTimeout(nodeTimeout)
reject(err)
})
})
}
@ -88,25 +100,30 @@ function handleResponse(res) {
* @return {Promise} If requestTimeout the timeout function will be called. Otherwise resolve the
* Promise with the handleResponse function
*/
export default function baseRequest(url, {
jsonBody,
query,
urlTemplateSpec,
...fetchConfig
} = {}, requestTimeout) {
export default function baseRequest(
url,
{
jsonBody, query, urlTemplateSpec, ...fetchConfig
} = {},
requestTimeout = 0
) {
let expandedUrl = url
if (urlTemplateSpec != null) {
if (Array.isArray(urlTemplateSpec) && urlTemplateSpec.length) {
// Use vsprintf for the array call signature
expandedUrl = vsprintf(url, urlTemplateSpec)
} else if (urlTemplateSpec &&
} else if (
urlTemplateSpec &&
typeof urlTemplateSpec === 'object' &&
Object.keys(urlTemplateSpec).length) {
Object.keys(urlTemplateSpec).length
) {
expandedUrl = formatText(url, urlTemplateSpec)
} else if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.warn('Supplied urlTemplateSpec was not an array or object. Ignoring...')
console.warn(
'Supplied urlTemplateSpec was not an array or object. Ignoring...'
)
}
}
@ -124,11 +141,17 @@ export default function baseRequest(url, {
if (jsonBody != null) {
fetchConfig.body = JSON.stringify(jsonBody)
}
if (requestTimeout) {
return timeout(requestTimeout, fetch.fetch(expandedUrl, fetchConfig))
const controller = new AbortController()
const { signal } = controller
return timeout(
requestTimeout,
fetch.fetch(expandedUrl, { ...fetchConfig, signal }),
controller
)
.then(handleResponse)
} else {
return fetch.fetch(expandedUrl, fetchConfig)
.then(handleResponse)
return fetch.fetch(expandedUrl, fetchConfig).then(handleResponse)
}
}

View File

@ -178,10 +178,11 @@ export default class Connection {
/**
* @param search
*/
searchAssets(search) {
searchAssets(search, limit = 10) {
return this._req(Connection.getApiUrls('assets'), {
query: {
search
search,
limit
}
})
}
@ -189,10 +190,11 @@ export default class Connection {
/**
* @param search
*/
searchMetadata(search) {
searchMetadata(search, limit = 10) {
return this._req(Connection.getApiUrls('metadata'), {
query: {
search
search,
limit
}
})
}

View File

@ -70,7 +70,7 @@ export default class Request {
const requestTimeout = timeout ? timeout - backoffTimedelta : timeout
return baseRequest(apiUrl, requestConfig, requestTimeout)
.then(async (res) => {
.then((res) => {
this.connectionError = null
return res.json()
})
@ -111,6 +111,8 @@ export default class Request {
}
static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
}

View File

@ -2,7 +2,6 @@
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0
import { Buffer } from 'buffer'
import stableStringify from 'json-stable-stringify'
import clone from 'clone'
import base58 from 'bs58'

View File

@ -225,7 +225,7 @@ test('Get asset for text', t => {
conn.searchAssets(search)
t.truthy(conn._req.calledWith(
expectedPath,
{ query: { search } }
{ query: { search, limit: 10 } }
))
})
@ -239,6 +239,6 @@ test('Get metadata for text', t => {
conn.searchMetadata(search)
t.truthy(conn._req.calledWith(
expectedPath,
{ query: { search } }
{ query: { search, limit: 10 } }
))
})

View File

@ -92,7 +92,8 @@ test('Fulfillment correctly formed', t => {
const msgHash = sha256Hash(msgUniqueFulfillment)
t.truthy(validateFulfillment(
txSigned.inputs[0].fulfillment, txCreate.outputs[0].condition.uri,
txSigned.inputs[0].fulfillment,
txCreate.outputs[0].condition.uri,
Buffer.from(msgHash, 'hex')
))
})

36
types/connection.d.ts vendored
View File

@ -19,6 +19,16 @@ export interface InputNode {
endpoint: string;
}
export type AssetResult = {
id: string;
data: Record<string, any>;
};
export type MetadataResult = {
id: string;
metadata: Record<string, any>;
};
export enum Endpoints {
blocks = 'blocks',
blocksDetail = 'blocksDetail',
@ -76,8 +86,8 @@ export interface EndpointsResponse<
[Endpoints.transactionsDetail]: O extends TransactionOperations.CREATE
? CreateTransaction<A, M>
: TransferTransaction<M>;
[Endpoints.assets]: { id: string; data: Record<string, any> }[];
[Endpoints.metadata]: { id: string; metadata: Record<string, any> }[];
[Endpoints.assets]: AssetResult[];
[Endpoints.metadata]: MetadataResult[];
}
export default class Connection {
@ -111,7 +121,9 @@ export default class Connection {
transactionId: string
): Promise<EndpointsResponse<O>[Endpoints.transactionsDetail]>;
listBlocks(transactionId: string): Promise<EndpointsResponse[Endpoints.blocks]>;
listBlocks(
transactionId: string
): Promise<EndpointsResponse[Endpoints.blocks]>;
listOutputs(
publicKey: string,
@ -124,7 +136,7 @@ export default class Connection {
): Promise<EndpointsResponse<typeof operation>[Endpoints.transactions]>;
postTransaction<
O = TransactionOperations.CREATE,
O extends TransactionOperations = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
@ -132,7 +144,7 @@ export default class Connection {
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsCommit]>;
postTransactionSync<
O = TransactionOperations.CREATE,
O extends TransactionOperations = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
@ -140,7 +152,7 @@ export default class Connection {
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsSync]>;
postTransactionAsync<
O = TransactionOperations.CREATE,
O extends TransactionOperations = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
@ -148,14 +160,20 @@ export default class Connection {
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsAsync]>;
postTransactionCommit<
O = TransactionOperations.CREATE,
O extends TransactionOperations = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
transaction: TransactionCommon<O>
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsCommit]>;
searchAssets(search: string): Promise<EndpointsResponse[Endpoints.assets]>;
searchAssets(
search: string,
limit?: number
): Promise<EndpointsResponse[Endpoints.assets]>;
searchMetadata(search: string): Promise<EndpointsResponse[Endpoints.metadata]>;
searchMetadata(
search: string,
limit?: number
): Promise<EndpointsResponse[Endpoints.metadata]>;
}

40
types/index.d.ts vendored
View File

@ -2,10 +2,38 @@
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0
import Ed25519Keypair from './Ed25519Keypair'
import Connection from './connection'
import Transaction from './transaction'
import ccJsonLoad from './utils/ccJsonLoad'
import ccJsonify from './utils/ccJsonify'
import Ed25519Keypair from './Ed25519Keypair';
import Connection, {
Endpoints,
EndpointsResponse,
EndpointsUrl,
} from './connection';
import Transaction, {
CreateTransaction,
TransactionCommon,
TransactionCommonSigned,
TransactionInput,
TransactionOutput,
TransferTransaction,
TransactionUnspentOutput,
TransactionOperations,
} from './transaction';
import ccJsonLoad from './utils/ccJsonLoad';
import ccJsonify from './utils/ccJsonify';
export { ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction }
export { ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction };
// Extras
export {
Endpoints,
EndpointsResponse,
EndpointsUrl,
CreateTransaction,
TransactionCommon,
TransactionCommonSigned,
TransactionInput,
TransactionOutput,
TransferTransaction,
TransactionUnspentOutput,
TransactionOperations,
};

6
types/sanitize.d.ts vendored
View File

@ -6,8 +6,8 @@ declare type FilterFn = (val: any, key?: string) => void;
declare function filterFromObject<I = Record<string, any>>(
obj: I,
filter: Array | FilterFn,
conf: { isInclusion?: boolean } = {}
filter: Array<any> | FilterFn,
conf: { isInclusion?: boolean }
): Partial<I>;
declare function applyFilterOnObject<I = Record<string, any>>(
@ -17,7 +17,7 @@ declare function applyFilterOnObject<I = Record<string, any>>(
declare function selectFromObject<I = Record<string, any>>(
obj: I,
filter: Array | FilterFn
filter: Array<any> | FilterFn
): Partial<I>;
export default function sanitize<I = Record<string, any>>(

View File

@ -33,9 +33,9 @@ export enum TransactionOperations {
}
export interface TransactionCommon<
O = TransactionOperations,
A = Record<string, unknown>,
M = Record<string, unknown>
O extends TransactionOperations = TransactionOperations.CREATE,
A extends Record<string, any> = Record<string, unknown>,
M extends Record<string, any> = Record<string, unknown>
> {
id?: string;
inputs: TransactionInput[];
@ -47,16 +47,16 @@ export interface TransactionCommon<
}
export interface TransactionCommonSigned<
O = TransactionOperations,
A = Record<string, unknown>,
M = Record<string, unknown>
O extends TransactionOperations = TransactionOperations.CREATE,
A extends Record<string, any> = Record<string, unknown>,
M extends Record<string, any> = Record<string, unknown>
> extends Omit<TransactionCommon<O, A, M>, 'id'> {
id: string;
}
export type TransactionAssetMap<
Operation,
A = Record<string, unknown>
A extends Record<string, any>
> = Operation extends TransactionOperations.CREATE
? {
data: A;
@ -66,18 +66,19 @@ export type TransactionAssetMap<
};
export interface CreateTransaction<
A = Record<string, unknown>,
M = Record<string, unknown>
A extends Record<string, any> = Record<string, unknown>,
M extends Record<string, any> = Record<string, unknown>
> extends TransactionCommon<TransactionOperations.CREATE, A, M> {
id: string;
asset: TransactionAssetMap<TransactionOperations.CREATE, A>;
operation: TransactionOperations.CREATE;
}
export interface TransferTransaction<M = Record<string, unknown>>
extends TransactionCommon<TransactionOperations.TRANSFER, any, M> {
export interface TransferTransaction<
M extends Record<string, any> = Record<string, unknown>
> extends TransactionCommon<TransactionOperations.TRANSFER, any, M> {
id: string;
asset: TransactionAssetMap<TransactionOperations.TRANSFER>;
asset: TransactionAssetMap<TransactionOperations.TRANSFER, { id: string }>;
operation: TransactionOperations.TRANSFER;
}
@ -96,22 +97,22 @@ interface TxTemplate {
version: '2.0';
}
declare type DelegateSignFunction = (
export type DelegateSignFunction = (
serializedTransaction: string,
input: TransactionInput,
index?: number
) => string;
declare type DelegateSignFunctionAsync = (
export type DelegateSignFunctionAsync = (
serializedTransaction: string,
input: TransactionInput,
index?: number
) => Promise<string>;
export default class Transaction {
static serializeTransactionIntoCanonicalString<O = TransactionOperations>(
transaction: TransactionCommon<O>
): string;
static serializeTransactionIntoCanonicalString<
O extends TransactionOperations = TransactionOperations
>(transaction: TransactionCommon<O>): string;
static serializeTransactionIntoCanonicalString(
transaction: CreateTransaction | TransferTransaction
@ -185,7 +186,7 @@ export default class Transaction {
static makeTransactionTemplate(): TxTemplate;
static makeTransaction<
O extends keyof TransactionOperations,
O extends TransactionOperations,
A = Record<string, any>,
M = Record<string, any>
>(
@ -212,18 +213,23 @@ export default class Transaction {
metadata: M
): TransferTransaction<M>;
static signTransaction<O = TransactionOperations.CREATE>(
static signTransaction<
O extends TransactionOperations = TransactionOperations.CREATE
>(
transaction: TransactionCommon<O>,
...privateKeys: string[]
): TransactionCommonSigned<O>;
static delegateSignTransaction<O = TransactionOperations.CREATE>(
static delegateSignTransaction<
O extends TransactionOperations = TransactionOperations.CREATE
>(
transaction: TransactionCommon<O>,
signFn: DelegateSignFunction
): TransactionCommonSigned<O>;
static delegateSignTransactionAsync<O = TransactionOperations.CREATE>(
static delegateSignTransactionAsync<
O extends TransactionOperations = TransactionOperations.CREATE
>(
transaction: TransactionCommon<O>,
signFn: DelegateSignFunctionAsync
): Promise<TransactionCommonSigned<O>>;

View File

@ -6,7 +6,8 @@
'use strict'
const { paths } = require('./webpack.parts.js')
const { ProvidePlugin } = require('webpack')
const { paths } = require('./webpack.parts')
module.exports = {
entry: paths.entry,
@ -24,10 +25,18 @@ module.exports = {
},
optimization: {
minimize: true,
noEmitOnErrors: true
emitOnErrors: false
},
resolve: {
extensions: ['.js'],
modules: ['node_modules'],
fallback: {
buffer: require.resolve('buffer/'),
}
},
plugins: [
new ProvidePlugin({
Buffer: ['buffer', 'Buffer']
})
]
}

View File

@ -8,9 +8,9 @@
const PRODUCTION = process.env.NODE_ENV === 'production'
const common = require('./webpack.common.js')
const common = require('./webpack.common')
const { outputs } = require('./webpack.parts.js')
const { outputs } = require('./webpack.parts')
// '[libraryTarget]': [file extension]
const OUTPUT_MAPPING = {

View File

@ -14,11 +14,9 @@ module.exports = {
minimizer: [
new TerserPlugin({
test: /vendor/,
sourceMap: false
}),
new TerserPlugin({
test: /^((?!(vendor)).)*.js$/,
sourceMap: false
})
],
splitChunks: {

View File

@ -9,8 +9,8 @@
const path = require('path')
const { merge } = require('webpack-merge')
const development = require('./webpack.development.js')
const production = require('./webpack.production.js')
const development = require('./webpack.development')
const production = require('./webpack.production')
const AddVendorsPlugin = require('./plugins/add-vendors-plugin')