feat(deps): make https-proxy-agent optional (#2388)

Bump https-proxy-agent to v7 and a dev dependency. This makes
configuring bundlers easier to configure and removes a direct
dependency of `https-proxy-agent`.

BREAKING CHANGE: Configuring a proxy is done by specifying the
`agent` parameter on the ConnectionOptions config. This can use be
created by libraries such as `https-proxy-agent` or any that
implements `http.Agent`.
This commit is contained in:
Caleb Kniffen
2023-07-25 20:27:31 -05:00
parent 8caf2e45bb
commit 3b7dd4ea87
8 changed files with 76 additions and 91 deletions

View File

@@ -62,12 +62,7 @@ To use `xrpl.js` with React, you need to install shims for core NodeJS modules.
Buffer: ["buffer", "Buffer"],
}),
]);
// This is deprecated in webpack 5 but alias false does not seem to work
config.module.rules.push({
test: /node_modules[\\\/]https-proxy-agent[\\\/]/,
use: "null-loader",
});
return config;
};
```

63
package-lock.json generated
View File

@@ -67,7 +67,7 @@
},
"engines": {
"node": ">=16.0.0",
"npm": ">=8.0.0 < 10.0.0"
"npm": ">=7.10.0 < 10.0.0"
}
},
"node_modules/@aashutoshrathi/word-wrap": {
@@ -3598,6 +3598,7 @@
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz",
"integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==",
"dev": true,
"dependencies": {
"debug": "4"
},
@@ -5588,6 +5589,7 @@
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
"integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
"dev": true,
"dependencies": {
"ms": "2.1.2"
},
@@ -8272,6 +8274,7 @@
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz",
"integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==",
"dev": true,
"dependencies": {
"agent-base": "6",
"debug": "4"
@@ -11447,7 +11450,8 @@
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true
},
"node_modules/multimatch": {
"version": "5.0.0",
@@ -16528,6 +16532,7 @@
"browserify-fs": "^1.0.0",
"constants-browserify": "^1.0.0",
"https-browserify": "^1.0.0",
"https-proxy-agent": "^7.0.1",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",
"karma-jasmine": "^5.1.0",
@@ -16546,6 +16551,31 @@
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.38.tgz",
"integrity": "sha512-6sfo1qTulpVbkxECP+AVrHV9OoJqhzCsfTNp5NIG+enM4HyM3HvZCO798WShIXBN0+QtDIcutJCjsVYnQP5rIQ==",
"dev": true
},
"packages/xrpl/node_modules/agent-base": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.0.tgz",
"integrity": "sha512-o/zjMZRhJxny7OyEF+Op8X+efiELC7k7yOjMzgfzVqOzXqkBkWI79YoTdOtsuWd5BWhAGAuOY/Xa6xpiaWXiNg==",
"dev": true,
"dependencies": {
"debug": "^4.3.4"
},
"engines": {
"node": ">= 14"
}
},
"packages/xrpl/node_modules/https-proxy-agent": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.1.tgz",
"integrity": "sha512-Eun8zV0kcYS1g19r78osiQLEFIRspRUDd9tIfBCTBPBeMieF/EsJNL8VI3xOIdYRDEkjQnqOYPsZ2DsWsVsFwQ==",
"dev": true,
"dependencies": {
"agent-base": "^7.0.2",
"debug": "4"
},
"engines": {
"node": ">= 14"
}
}
},
"dependencies": {
@@ -19400,6 +19430,7 @@
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz",
"integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==",
"dev": true,
"requires": {
"debug": "4"
}
@@ -21006,6 +21037,7 @@
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
"integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
"dev": true,
"requires": {
"ms": "2.1.2"
}
@@ -23075,6 +23107,7 @@
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz",
"integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==",
"dev": true,
"requires": {
"agent-base": "6",
"debug": "4"
@@ -25594,7 +25627,8 @@
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true
},
"multimatch": {
"version": "5.0.0",
@@ -29364,7 +29398,7 @@
"version": "file:packages/xrpl",
"requires": {
"@geut/browser-node-core": "^2.0.13",
"@types/node": "16",
"@types/node": "^16.18.38",
"assert-browserify": "^2.0.0",
"bignumber.js": "^9.0.0",
"bip32": "^2.0.6",
@@ -29372,7 +29406,7 @@
"browserify-fs": "^1.0.0",
"constants-browserify": "^1.0.0",
"https-browserify": "^1.0.0",
"https-proxy-agent": "^5.0.0",
"https-proxy-agent": "^7.0.1",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",
"karma-jasmine": "^5.1.0",
@@ -29393,6 +29427,25 @@
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.38.tgz",
"integrity": "sha512-6sfo1qTulpVbkxECP+AVrHV9OoJqhzCsfTNp5NIG+enM4HyM3HvZCO798WShIXBN0+QtDIcutJCjsVYnQP5rIQ==",
"dev": true
},
"agent-base": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.0.tgz",
"integrity": "sha512-o/zjMZRhJxny7OyEF+Op8X+efiELC7k7yOjMzgfzVqOzXqkBkWI79YoTdOtsuWd5BWhAGAuOY/Xa6xpiaWXiNg==",
"dev": true,
"requires": {
"debug": "^4.3.4"
}
},
"https-proxy-agent": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.1.tgz",
"integrity": "sha512-Eun8zV0kcYS1g19r78osiQLEFIRspRUDd9tIfBCTBPBeMieF/EsJNL8VI3xOIdYRDEkjQnqOYPsZ2DsWsVsFwQ==",
"dev": true,
"requires": {
"agent-base": "^7.0.2",
"debug": "4"
}
}
}
},

View File

@@ -19,8 +19,7 @@
"test": "test"
},
"browser": {
"ws": "./dist/npm/client/WSWrapper.js",
"https-proxy-agent": false
"ws": "./dist/npm/client/WSWrapper.js"
},
"dependencies": {
"bignumber.js": "^9.0.0",
@@ -40,6 +39,7 @@
"browserify-fs": "^1.0.0",
"constants-browserify": "^1.0.0",
"https-browserify": "^1.0.0",
"https-proxy-agent": "^7.0.1",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",
"karma-jasmine": "^5.1.0",

View File

@@ -1,6 +1,6 @@
/* eslint-disable max-lines -- Connection is a large file w/ lots of imports/exports */
import { EventEmitter } from 'events'
import { Agent } from 'http'
import type { Agent } from 'http'
import WebSocket from 'ws'
@@ -11,7 +11,6 @@ import {
XrplError,
} from '../errors'
import { BaseRequest } from '../models/methods/baseMethod'
import { omitBy } from '../utils/collections'
import ConnectionManager from './ConnectionManager'
import ExponentialBackoff from './ExponentialBackoff'
@@ -26,17 +25,11 @@ const CONNECTION_TIMEOUT = 5
*/
interface ConnectionOptions {
trace?: boolean | ((id: string, message: string) => void)
proxy?: string
proxyAuthorization?: string
authorization?: string
trustedCertificates?: string[]
key?: string
passphrase?: string
certificate?: string
// request timeout
timeout: number
connectionTimeout: number
headers?: { [key: string]: string }
agent?: Agent
authorization?: string
connectionTimeout: number
timeout: number
}
/**
@@ -55,52 +48,6 @@ export const INTENTIONAL_DISCONNECT_CODE = 4000
type WebsocketState = 0 | 1 | 2 | 3
function getAgent(url: string, config: ConnectionOptions): Agent | undefined {
if (config.proxy == null) {
return undefined
}
const parsedURL = new URL(url)
const parsedProxyURL = new URL(config.proxy)
const proxyOptions = omitBy(
{
secureEndpoint: parsedURL.protocol === 'wss:',
secureProxy: parsedProxyURL.protocol === 'https:',
auth: config.proxyAuthorization,
ca: config.trustedCertificates,
key: config.key,
passphrase: config.passphrase,
cert: config.certificate,
href: parsedProxyURL.href,
origin: parsedProxyURL.origin,
protocol: parsedProxyURL.protocol,
username: parsedProxyURL.username,
password: parsedProxyURL.password,
host: parsedProxyURL.host,
hostname: parsedProxyURL.hostname,
port: parsedProxyURL.port,
pathname: parsedProxyURL.pathname,
search: parsedProxyURL.search,
hash: parsedProxyURL.hash,
},
(value) => value == null,
)
let HttpsProxyAgent: new (opt: typeof proxyOptions) => Agent
try {
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports,
node/global-require, global-require, -- Necessary for the `require` */
HttpsProxyAgent = require('https-proxy-agent')
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports,
node/global-require, global-require, */
} catch (_error) {
throw new Error('"proxy" option is not supported in the browser')
}
return new HttpsProxyAgent(proxyOptions)
}
/**
* Create a new websocket given your URL and optional proxy/certificate
* configuration.
@@ -113,8 +60,9 @@ function createWebSocket(
url: string,
config: ConnectionOptions,
): WebSocket | null {
const options: WebSocket.ClientOptions = {}
options.agent = getAgent(url, config)
const options: WebSocket.ClientOptions = {
agent: config.agent,
}
if (config.headers) {
options.headers = config.headers
}
@@ -125,16 +73,7 @@ function createWebSocket(
Authorization: `Basic ${base64}`,
}
}
const optionsOverrides = omitBy(
{
ca: config.trustedCertificates,
key: config.key,
passphrase: config.passphrase,
cert: config.certificate,
},
(value) => value == null,
)
const websocketOptions = { ...options, ...optionsOverrides }
const websocketOptions = { ...options }
const websocket = new WebSocket(url, websocketOptions)
/*
* we will have a listener for each outstanding request,
@@ -177,7 +116,7 @@ export class Connection extends EventEmitter {
private ws: WebSocket | null = null
// Typing necessary for Jest tests running in browser
private reconnectTimeoutID: null | ReturnType<typeof setTimeout> = null
// Typing necessary for Jest tetsts running in browser
// Typing necessary for Jest tests running in browser
private heartbeatIntervalID: null | ReturnType<typeof setTimeout> = null
private readonly retryConnectionBackoff = new ExponentialBackoff({
min: 100,

View File

@@ -127,7 +127,6 @@ import {
export interface ClientOptions extends ConnectionUserOptions {
feeCushion?: number
maxFeeXRP?: string
proxy?: string
timeout?: number
}

View File

@@ -3,6 +3,7 @@
import net from 'net'
import { assert } from 'chai'
import { HttpsProxyAgent } from 'https-proxy-agent'
import {
Client,
@@ -229,9 +230,9 @@ describe('Connection', function () {
const server = await createServer()
const port = (server.address() as net.AddressInfo).port
const options = {
proxy: `ws://127.0.0.1:${port}`,
authorization: 'authorization',
trustedCertificates: ['path/to/pem'],
agent: new HttpsProxyAgent<string>(`ws://127.0.0.1:${port}`, {
ca: ['path/to/pem'],
}),
}
const connection = new Connection(
// @ts-expect-error -- Testing private member
@@ -421,7 +422,7 @@ describe('Connection', function () {
spy = jest
// @ts-expect-error -- Testing private member
.spyOn(clientContext.client.connection.ws, 'send')
// @ts-expect-error -- Testing private member
// @ts-expect-error -- Typescript doesnt like the mock
.mockImplementation((_0, _1, _2) => {
return 0
})

View File

@@ -95,7 +95,6 @@ function webpackForTest(testFileName) {
resolve: {
alias: {
ws: './dist/npm/client/WSWrapper.js',
'https-proxy-agent': false,
},
extensions: ['.ts', '.js', '.json'],
fallback: {

View File

@@ -56,7 +56,6 @@ function getDefaultConfiguration() {
resolve: {
alias: {
ws: './dist/npm/client/WSWrapper.js',
'https-proxy-agent': false,
},
extensions: ['.js', '.json'],
// We don't want to webpack any of the local dependencies: