Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const { promisify } = require('util');
// Inline promisify shim — avoids requiring Node's `util` module so this file
// can be bundled for browsers without a polyfill. If the codegen template
// is regenerated, restore this shim.
function promisify(fn) {
return (...args) => new Promise((resolve, reject) => {
fn(...args, (err, result) => (err ? reject(err) : resolve(result)));
});
}
const GrpcError = require('@dashevo/grpc-common/lib/server/error/GrpcError');

const { CoreClient } = require('./core_pb_service');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const { PlatformClient } = require('./platform_pb_service');
const { promisify } = require('util');

// Inline promisify shim — avoids requiring Node's `util` module so this file
// can be bundled for browsers without a polyfill. If the codegen template
// is regenerated, restore this shim.
function promisify(fn) {
return (...args) => new Promise((resolve, reject) => {
fn(...args, (err, result) => (err ? reject(err) : resolve(result)));
});
}

class PlatformPromiseClient {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sample = require('lodash/sample');
const sample = (arr) => arr[Math.floor(Math.random() * arr.length)];
const networks = require('@dashevo/dashcore-lib/lib/networks');

class ListDAPIAddressProvider {
Expand Down
2 changes: 0 additions & 2 deletions packages/js-dapi-client/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require('../polyfills/fetch-polyfill');

const DAPIClient = require('./DAPIClient');

const NotFoundError = require('./transport/GrpcTransport/errors/NotFoundError');
Expand Down
114 changes: 38 additions & 76 deletions packages/js-dapi-client/lib/logger/index.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,42 @@
const util = require('util');
const winston = require('winston');
const LOG_LEVEL = (typeof process !== 'undefined' && process.env && process.env.LOG_LEVEL) || 'silent';

// TODO: Refactor to use params instead on envs

const LOG_LEVEL = process.env.LOG_LEVEL || 'silent';
const LOG_TO_FILE = process.env.LOG_WALLET_TO_FILE || 'false';

// Log levels:
// error 0
// warn 1
// info 2 (default)
// verbose 3
// debug 4
// silly 5

const loggers = {};

const createLogger = (formats = [], id = '') => {
const format = winston.format.combine(
{
transform: (info) => {
const args = info[Symbol.for('splat')];
const result = { ...info };
if (args) {
result.message = util.format(info.message, ...args);
}
return result;
},
},
...formats,
winston.format.colorize(),
winston.format.printf(({
level, message,
}) => `${level}: ${message}`),
);

const transports = [
new winston.transports.Console({
format,
silent: LOG_LEVEL === 'silent',
}),
];

if (LOG_TO_FILE === 'true' && typeof window === 'undefined') {
transports.push(
new winston.transports.File({
filename: `wallet${id !== '' ? `_${id}` : ''}`,
format,
silent: LOG_LEVEL === 'silent',
}),
);
}

return winston.createLogger({
level: LOG_LEVEL,
transports,
});
const LEVELS = {
silent: -1, error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5,
};

const logger = createLogger();

logger.getForId = (id) => {
if (!loggers[id]) {
const format = {
transform: (info) => {
const message = `[DAPIClient: ${id}] ${info.message}`;
return { ...info, message };
},
};

loggers[id] = createLogger([format], id);
}

return loggers[id];
};

logger.verbose(`Logger uses "${LOG_LEVEL}" level`, { level: LOG_LEVEL });
const cache = {};

function build(level = LOG_LEVEL, prefix = '') {
const threshold = LEVELS[level] != null ? LEVELS[level] : LEVELS.silent;
const noop = () => {};
// Preserve printf-style interpolation (%s/%d/%o/...): when there is a
// prefix and the first argument is the format string, merge the prefix
// into it. Otherwise hand the prefix to console.* as a leading argument.
const fmt = prefix
? (...a) => {
if (a.length === 0) return [prefix];

Check warning on line 17 in packages/js-dapi-client/lib/logger/index.js

View workflow job for this annotation

GitHub Actions / JS packages (@dashevo/dapi-client) / Linting

Expected { after 'if' condition
const [first, ...rest] = a;
return typeof first === 'string' ? [`${prefix} ${first}`, ...rest] : [prefix, first, ...rest];
}
: (...a) => a;

const logger = {
error: threshold >= 0 ? (...a) => console.error(...fmt(...a)) : noop,

Check warning on line 24 in packages/js-dapi-client/lib/logger/index.js

View workflow job for this annotation

GitHub Actions / JS packages (@dashevo/dapi-client) / Linting

Unexpected console statement
warn: threshold >= 1 ? (...a) => console.warn(...fmt(...a)) : noop,

Check warning on line 25 in packages/js-dapi-client/lib/logger/index.js

View workflow job for this annotation

GitHub Actions / JS packages (@dashevo/dapi-client) / Linting

Unexpected console statement
info: threshold >= 2 ? (...a) => console.info(...fmt(...a)) : noop,

Check warning on line 26 in packages/js-dapi-client/lib/logger/index.js

View workflow job for this annotation

GitHub Actions / JS packages (@dashevo/dapi-client) / Linting

Unexpected console statement
verbose: threshold >= 3 ? (...a) => console.debug(...fmt(...a)) : noop,

Check warning on line 27 in packages/js-dapi-client/lib/logger/index.js

View workflow job for this annotation

GitHub Actions / JS packages (@dashevo/dapi-client) / Linting

Unexpected console statement
debug: threshold >= 4 ? (...a) => console.debug(...fmt(...a)) : noop,

Check warning on line 28 in packages/js-dapi-client/lib/logger/index.js

View workflow job for this annotation

GitHub Actions / JS packages (@dashevo/dapi-client) / Linting

Unexpected console statement
silly: threshold >= 5 ? (...a) => console.debug(...fmt(...a)) : noop,

Check warning on line 29 in packages/js-dapi-client/lib/logger/index.js

View workflow job for this annotation

GitHub Actions / JS packages (@dashevo/dapi-client) / Linting

Unexpected console statement
getForId(id, overrideLevel) {
const effective = overrideLevel || level;
const key = `${id}\0${effective}`;
if (!cache[key]) {
cache[key] = build(effective, `[DAPIClient: ${id}]`);
}
return cache[key];
},
};
return logger;
}

module.exports = logger;
module.exports = build();
3 changes: 0 additions & 3 deletions packages/js-dapi-client/lib/test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require('../../polyfills/fetch-polyfill');
require('setimmediate');

const { expect, use } = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
Expand Down
3 changes: 0 additions & 3 deletions packages/js-dapi-client/lib/test/karma/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require('../../../polyfills/fetch-polyfill');
require('setimmediate');

const { expect, use } = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const https = require('https');
const JsonRpcError = require('./errors/JsonRpcError');
const WrongHttpCodeError = require('./errors/WrongHttpCodeError');

// Lazily-created undici Agent that disables TLS verification, shared across
// all self-signed requests. A per-request Agent would leak its socket pool
// since nothing destroys it after the fetch completes.
let sharedSelfSignedAgent;
/**
* @typedef {requestJsonRpc}
* @param {string} protocol
Expand Down Expand Up @@ -47,17 +51,24 @@ async function requestJsonRpc(protocol, host, port, selfSigned, method, params,
Object.assign(requestOptions, { signal: controller.signal });
}

// For NodeJS Client
// Self-signed HTTPS: Node 18+ built-in fetch is backed by undici, which
// accepts a `dispatcher` for per-request TLS settings. Browsers can't
// bypass TLS verification, so the flag is a no-op there. eval('require')
// hides undici from bundler static analysis so it isn't pulled into
// browser bundles.
if (typeof process !== 'undefined'
&& process.versions != null
&& process.versions.node != null
&& protocol === 'https'
&& selfSigned) {
requestOptions.agent = new https.Agent({
rejectUnauthorized: false,
});
if (!sharedSelfSignedAgent) {
// eslint-disable-next-line no-eval, global-require
const { Agent } = eval('require')('undici');
sharedSelfSignedAgent = new Agent({ connect: { rejectUnauthorized: false } });
}
requestOptions.dispatcher = sharedSelfSignedAgent;
}
// eslint-disable-next-line

const response = await fetch(url, requestOptions);

if (typeof requestTimeoutId !== 'undefined') {
Expand Down
13 changes: 5 additions & 8 deletions packages/js-dapi-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@
"@dashevo/dashcore-lib": "~0.22.0",
"@dashevo/grpc-common": "workspace:*",
"@dashevo/wasm-dpp": "workspace:*",
"bs58": "^4.0.1",
"cbor": "^8.0.0",
"google-protobuf": "^3.12.2",
"lodash": "^4.17.23",
"node-fetch": "^2.6.7",
"node-inspect-extracted": "^1.0.8",
"wasm-x11-hash": "~0.0.2",
"winston": "^3.2.1"
"undici": "^6.0.0",
"wasm-x11-hash": "~0.0.2"
},
"devDependencies": {
"@babel/core": "^7.26.10",
Expand Down Expand Up @@ -66,7 +62,6 @@
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"setimmediate": "^1.0.5",
"sinon": "^18.0.1",
"sinon-chai": "^3.7.0",
"stream-browserify": "^3.0.0",
Expand All @@ -76,10 +71,12 @@
"webpack": "^5.104.0",
"webpack-cli": "^4.9.1"
},
"engines": {
"node": ">=18.18"
},
"files": [
"docs",
"lib",
"polyfills",
"dist"
],
"scripts": {
Expand Down
10 changes: 0 additions & 10 deletions packages/js-dapi-client/polyfills/fetch-polyfill.js

This file was deleted.

Loading
Loading