mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-19 19:55:51 +00:00
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:
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -127,7 +127,6 @@ import {
|
||||
export interface ClientOptions extends ConnectionUserOptions {
|
||||
feeCushion?: number
|
||||
maxFeeXRP?: string
|
||||
proxy?: string
|
||||
timeout?: number
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -95,7 +95,6 @@ function webpackForTest(testFileName) {
|
||||
resolve: {
|
||||
alias: {
|
||||
ws: './dist/npm/client/WSWrapper.js',
|
||||
'https-proxy-agent': false,
|
||||
},
|
||||
extensions: ['.ts', '.js', '.json'],
|
||||
fallback: {
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user