From 9812cf7a3c9ade98763bace5de61e070238311b4 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 19 May 2026 10:23:31 -0500 Subject: [PATCH] =?UTF-8?q?chore(dapi-client,dapi-grpc):=20cleanup=20?= =?UTF-8?q?=E2=80=94=20drop=20unused=20deps,=20inline=20winston/fetch/prom?= =?UTF-8?q?isify=20shims?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Non-breaking cleanup pass; package stays CJS, no public API changes, no consumer changes required. dapi-client: replace winston with a minimal console-backed logger that preserves the same API (.error/.warn/.info/.verbose/.debug/.silly/.getForId). Drop node-fetch and the lib/test/bootstrap setimmediate shim — Node 18+ has both globally. Drop the https.Agent self-signed-TLS branch from requestJsonRpc (was Node-only; consumers wanting this must configure NODE_TLS_REJECT_UNAUTHORIZED at the app layer). Inline lodash/sample in ListDAPIAddressProvider. Add engines.node >=18.18. Remove dependencies: winston, node-fetch, lodash, bs58 (unused), node-inspect-extracted (unused). Remove devDeps: setimmediate. dapi-grpc: inline the promisify shim in core/v0/web/CorePromiseClient.js and platform/v0/web/PlatformPromiseClient.js so the browser bundle no longer requires Node's util module. Both files document the shim so a future codegen regen does not silently reintroduce require('util'). --- .pnp.cjs | 15 +-- .../clients/core/v0/web/CorePromiseClient.js | 9 +- .../platform/v0/web/PlatformPromiseClient.js | 10 +- .../ListDAPIAddressProvider.js | 2 +- packages/js-dapi-client/lib/index.js | 2 - packages/js-dapi-client/lib/logger/index.js | 105 +++++------------- packages/js-dapi-client/lib/test/bootstrap.js | 3 - .../JsonRpcTransport/requestJsonRpc.js | 12 -- packages/js-dapi-client/package.json | 11 +- .../polyfills/fetch-polyfill.js | 10 -- yarn.lock | 5 - 11 files changed, 56 insertions(+), 128 deletions(-) delete mode 100644 packages/js-dapi-client/polyfills/fetch-polyfill.js diff --git a/.pnp.cjs b/.pnp.cjs index b2a860bb23..4da943bc04 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -2576,7 +2576,6 @@ const RAW_RUNTIME_STATE = ["assert-browserify", "npm:2.0.0"],\ ["babel-loader", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:9.1.3"],\ ["browserify-zlib", "npm:0.2.0"],\ - ["bs58", "npm:4.0.1"],\ ["buffer", "npm:6.0.3"],\ ["cbor", "npm:8.1.0"],\ ["chai", "npm:4.3.10"],\ @@ -2595,10 +2594,7 @@ const RAW_RUNTIME_STATE = ["karma-mocha", "npm:2.0.1"],\ ["karma-mocha-reporter", "virtual:e2d057e7cc143d3cb9bec864f4a2d862441b5a09f81f8e6c46e7a098cbc89e4d07017cc6e2e2142d5704bb55da853cbec2d025ebc0b30e8696c31380c00f2c7d#npm:2.2.5"],\ ["karma-webpack", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:5.0.0"],\ - ["lodash", "npm:4.17.23"],\ ["mocha", "npm:11.1.0"],\ - ["node-fetch", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7"],\ - ["node-inspect-extracted", "npm:1.0.8"],\ ["nyc", "npm:15.1.0"],\ ["os-browserify", "npm:0.3.0"],\ ["path-browserify", "npm:1.0.1"],\ @@ -2612,8 +2608,7 @@ const RAW_RUNTIME_STATE = ["util", "npm:0.12.4"],\ ["wasm-x11-hash", "npm:0.0.2"],\ ["webpack", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:5.105.0"],\ - ["webpack-cli", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:4.9.1"],\ - ["winston", "npm:3.3.3"]\ + ["webpack-cli", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:4.9.1"]\ ],\ "linkType": "SOFT"\ }]\ @@ -4422,7 +4417,7 @@ const RAW_RUNTIME_STATE = ["@octokit/request-error", "npm:2.1.0"],\ ["@octokit/types", "npm:6.34.0"],\ ["is-plain-object", "npm:5.0.0"],\ - ["node-fetch", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7"],\ + ["node-fetch", "virtual:25a5f5382d53dbf298bf7a1191760bc2e0a523a619eeb0e667b99a8649e8ad183f9e2e0b45f6fb831b92f4078b61622aa567cf79565f6aa5af9597e3c84864f6#npm:2.6.7"],\ ["universal-user-agent", "npm:6.0.0"]\ ],\ "linkType": "HARD"\ @@ -16640,12 +16635,12 @@ const RAW_RUNTIME_STATE = ],\ "linkType": "SOFT"\ }],\ - ["virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7", {\ - "packageLocation": "./.yarn/__virtual__/node-fetch-virtual-a3f0ba2944/0/cache/node-fetch-npm-2.6.7-777aa2a6df-4bc9245383.zip/node_modules/node-fetch/",\ + ["virtual:25a5f5382d53dbf298bf7a1191760bc2e0a523a619eeb0e667b99a8649e8ad183f9e2e0b45f6fb831b92f4078b61622aa567cf79565f6aa5af9597e3c84864f6#npm:2.6.7", {\ + "packageLocation": "./.yarn/__virtual__/node-fetch-virtual-d3846f8e12/0/cache/node-fetch-npm-2.6.7-777aa2a6df-4bc9245383.zip/node_modules/node-fetch/",\ "packageDependencies": [\ ["@types/encoding", null],\ ["encoding", null],\ - ["node-fetch", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7"],\ + ["node-fetch", "virtual:25a5f5382d53dbf298bf7a1191760bc2e0a523a619eeb0e667b99a8649e8ad183f9e2e0b45f6fb831b92f4078b61622aa567cf79565f6aa5af9597e3c84864f6#npm:2.6.7"],\ ["whatwg-url", "npm:5.0.0"]\ ],\ "packagePeers": [\ diff --git a/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js b/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js index 3f1549d805..818ef24e84 100644 --- a/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js +++ b/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js @@ -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'); diff --git a/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js b/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js index 13be28a2d5..fbf39888b2 100644 --- a/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js +++ b/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js @@ -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 { /** diff --git a/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js b/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js index d9133a1ad4..c13217e98d 100644 --- a/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js +++ b/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js @@ -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 { diff --git a/packages/js-dapi-client/lib/index.js b/packages/js-dapi-client/lib/index.js index f10beb4769..65ad0f4ded 100644 --- a/packages/js-dapi-client/lib/index.js +++ b/packages/js-dapi-client/lib/index.js @@ -1,5 +1,3 @@ -require('../polyfills/fetch-polyfill'); - const DAPIClient = require('./DAPIClient'); const NotFoundError = require('./transport/GrpcTransport/errors/NotFoundError'); diff --git a/packages/js-dapi-client/lib/logger/index.js b/packages/js-dapi-client/lib/logger/index.js index a06b34c145..ffca12f534 100644 --- a/packages/js-dapi-client/lib/logger/index.js +++ b/packages/js-dapi-client/lib/logger/index.js @@ -1,80 +1,33 @@ -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 = () => {}; + const fmt = prefix ? (...a) => [prefix, ...a] : (...a) => a; + + const logger = { + error: threshold >= 0 ? (...a) => console.error(...fmt(...a)) : noop, + warn: threshold >= 1 ? (...a) => console.warn(...fmt(...a)) : noop, + info: threshold >= 2 ? (...a) => console.info(...fmt(...a)) : noop, + verbose: threshold >= 3 ? (...a) => console.debug(...fmt(...a)) : noop, + debug: threshold >= 4 ? (...a) => console.debug(...fmt(...a)) : noop, + silly: threshold >= 5 ? (...a) => console.debug(...fmt(...a)) : noop, + 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(); diff --git a/packages/js-dapi-client/lib/test/bootstrap.js b/packages/js-dapi-client/lib/test/bootstrap.js index 473d16fb1d..85d7947d66 100644 --- a/packages/js-dapi-client/lib/test/bootstrap.js +++ b/packages/js-dapi-client/lib/test/bootstrap.js @@ -1,6 +1,3 @@ -require('../../polyfills/fetch-polyfill'); -require('setimmediate'); - const { expect, use } = require('chai'); const sinon = require('sinon'); const sinonChai = require('sinon-chai'); diff --git a/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js b/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js index ddbdf79708..8ccfabe29f 100644 --- a/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js +++ b/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js @@ -1,4 +1,3 @@ -const https = require('https'); const JsonRpcError = require('./errors/JsonRpcError'); const WrongHttpCodeError = require('./errors/WrongHttpCodeError'); /** @@ -47,17 +46,6 @@ async function requestJsonRpc(protocol, host, port, selfSigned, method, params, Object.assign(requestOptions, { signal: controller.signal }); } - // For NodeJS Client - if (typeof process !== 'undefined' - && process.versions != null - && process.versions.node != null - && protocol === 'https' - && selfSigned) { - requestOptions.agent = new https.Agent({ - rejectUnauthorized: false, - }); - } - // eslint-disable-next-line const response = await fetch(url, requestOptions); if (typeof requestTimeoutId !== 'undefined') { diff --git a/packages/js-dapi-client/package.json b/packages/js-dapi-client/package.json index 2bccc23c23..ede6b739dc 100644 --- a/packages/js-dapi-client/package.json +++ b/packages/js-dapi-client/package.json @@ -31,14 +31,9 @@ "@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" + "wasm-x11-hash": "~0.0.2" }, "devDependencies": { "@babel/core": "^7.26.10", @@ -76,10 +71,12 @@ "webpack": "^5.104.0", "webpack-cli": "^4.9.1" }, + "engines": { + "node": ">=18.18" + }, "files": [ "docs", "lib", - "polyfills", "dist" ], "scripts": { diff --git a/packages/js-dapi-client/polyfills/fetch-polyfill.js b/packages/js-dapi-client/polyfills/fetch-polyfill.js deleted file mode 100644 index 8fa8ac2ffc..0000000000 --- a/packages/js-dapi-client/polyfills/fetch-polyfill.js +++ /dev/null @@ -1,10 +0,0 @@ -/* eslint-disable */ - -const { default: fetch, Headers, Request, Response } = require('node-fetch'); - -if (typeof window === 'undefined') { - globalThis.fetch = fetch; - globalThis.Headers = Headers; - globalThis.Request = Request; - globalThis.Response = Response; -} diff --git a/yarn.lock b/yarn.lock index 57d28cca8c..f6864af820 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1523,7 +1523,6 @@ __metadata: assert-browserify: "npm:^2.0.0" babel-loader: "npm:^9.1.3" browserify-zlib: "npm:^0.2.0" - bs58: "npm:^4.0.1" buffer: "npm:^6.0.3" cbor: "npm:^8.0.0" chai: "npm:^4.3.10" @@ -1542,10 +1541,7 @@ __metadata: karma-mocha: "npm:^2.0.1" karma-mocha-reporter: "npm:^2.2.5" karma-webpack: "npm:^5.0.0" - lodash: "npm:^4.17.23" mocha: "npm:^11.1.0" - node-fetch: "npm:^2.6.7" - node-inspect-extracted: "npm:^1.0.8" nyc: "npm:^15.1.0" os-browserify: "npm:^0.3.0" path-browserify: "npm:^1.0.1" @@ -1560,7 +1556,6 @@ __metadata: wasm-x11-hash: "npm:~0.0.2" webpack: "npm:^5.104.0" webpack-cli: "npm:^4.9.1" - winston: "npm:^3.2.1" languageName: unknown linkType: soft