Skip to content

Commit

Permalink
feat(deps): make https-proxy-agent optional (#2388)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
ckniffen committed Nov 30, 2023
1 parent 60e8c37 commit 9ab69b6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 90 deletions.
7 changes: 1 addition & 6 deletions UNIQUE_SETUPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
```
Expand Down
63 changes: 58 additions & 5 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions packages/xrpl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
79 changes: 9 additions & 70 deletions packages/xrpl/src/client/connection.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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'
Expand All @@ -26,17 +25,11 @@ const CONNECTION_TIMEOUT = 5
*/
interface ConnectionOptions {
trace?: boolean | ((id: string, message: string) => void)
proxy?: string
proxyAuthorization?: string
headers?: { [key: string]: string }
agent?: Agent
authorization?: string
trustedCertificates?: string[]
key?: string
passphrase?: string
certificate?: string
// request timeout
timeout: number
connectionTimeout: number
headers?: { [key: string]: string }
timeout: number
}

/**
Expand All @@ -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.
Expand All @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion packages/xrpl/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ import {
export interface ClientOptions extends ConnectionUserOptions {
feeCushion?: number
maxFeeXRP?: string
proxy?: string
timeout?: number
}

Expand Down
9 changes: 5 additions & 4 deletions packages/xrpl/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net from 'net'

import { assert } from 'chai'
import { HttpsProxyAgent } from 'https-proxy-agent'

import {
Client,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down
1 change: 0 additions & 1 deletion packages/xrpl/test/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ function webpackForTest(testFileName) {
resolve: {
alias: {
ws: './dist/npm/client/WSWrapper.js',
'https-proxy-agent': false,
},
extensions: ['.ts', '.js', '.json'],
fallback: {
Expand Down
1 change: 0 additions & 1 deletion packages/xrpl/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9ab69b6

Please sign in to comment.