Skip to content

Commit fe0ad90

Browse files
committed
Update/drop/inline assorted minor deps to clean things up
1 parent 5b61354 commit fe0ad90

File tree

15 files changed

+71
-74
lines changed

15 files changed

+71
-74
lines changed

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function(config) {
2828
"http-proxy-agent": require.resolve('./test/empty-stub.js'),
2929
"https-proxy-agent": require.resolve('./test/empty-stub.js'),
3030
"request-promise-native": require.resolve('./test/empty-stub.js'),
31-
"portfinder": require.resolve('./test/empty-stub.js'),
31+
"get-port": require.resolve('./test/empty-stub.js'),
3232
"dns2": require.resolve('./test/empty-stub.js'),
3333
"ws": require.resolve('./test/empty-stub.js'),
3434
"tmp-promise": require.resolve('./test/empty-stub.js'),

package.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,36 +169,33 @@
169169
"@types/cors": "^2.8.6",
170170
"@types/node": "*",
171171
"async-mutex": "^0.5.0",
172-
"base64-arraybuffer": "^0.1.5",
172+
"base64-arraybuffer": "^1.0.2",
173173
"cacheable-lookup": "^6.0.0",
174174
"common-tags": "^1.8.0",
175175
"connect": "^3.7.0",
176176
"cors": "^2.8.4",
177-
"cors-gate": "^1.1.3",
178177
"destroyable-server": "^1.1.1",
179178
"express": "^5.2.1",
180179
"fast-json-patch": "^3.1.1",
180+
"get-port": "^7.2.0",
181181
"graphql": "^14.0.2 || ^15.5 || ^16.0.0",
182182
"graphql-http": "^1.22.0",
183183
"graphql-subscriptions": "^2.0.0",
184184
"graphql-tag": "^2.12.6",
185185
"http-encoding": "^2.0.1",
186186
"http2-wrapper": "^2.2.1",
187-
"https-proxy-agent": "^5.0.1",
187+
"https-proxy-agent": "^7.0.6",
188188
"isomorphic-ws": "^4.0.1",
189189
"lodash": "^4.16.4",
190-
"lru-cache": "^7.14.0",
190+
"lru-cache": "^11.2.7",
191191
"milliparsec": "^5.1.1",
192192
"native-duplexpair": "^1.0.0",
193193
"pac-proxy-agent": "^7.0.0",
194194
"parse-multipart-data": "^1.4.0",
195-
"performance-now": "^2.1.0",
196-
"portfinder": "^1.0.32",
197195
"read-tls-client-hello": "^2.0.0",
198196
"semver": "^7.5.3",
199-
"socks-proxy-agent": "^7.0.0",
200-
"typed-error": "^3.0.2",
201-
"urlpattern-polyfill": "^8.0.0",
197+
"socks-proxy-agent": "^8.0.5",
198+
"urlpattern-polyfill": "^10.1.0",
202199
"ws": "^8.20.0"
203200
}
204201
}

src/admin/admin-server.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as net from 'net';
55
import * as _ from 'lodash';
66
import * as express from 'express';
77
import * as cors from 'cors';
8-
import corsGate = require('cors-gate');
98
import { json as jsonParser } from 'milliparsec';
109
import * as Ws from 'ws';
1110

@@ -167,11 +166,18 @@ export class AdminServer<Plugins extends { [key: string]: AdminPlugin<any, any>
167166
options.corsOptions.origin;
168167

169168
if (this.requiredOrigin) {
170-
this.app.use(corsGate({
171-
strict: true, // MUST send an allowed origin
172-
allowSafe: false, // Even for HEAD/GET requests (should be none anyway)
173-
origin: '' // No base origin - we accept *no* same-origin requests
174-
}));
169+
// Reject requests without a valid CORS origin. The upstream cors middleware
170+
// sets access-control-allow-origin for allowed origins, so we just check that.
171+
this.app.use((req, res, next) => {
172+
const origin = req.headers.origin?.toLowerCase().trim();
173+
if (!origin) { res.statusCode = 403; res.end(); return; }
174+
175+
const allowed = res.getHeader('access-control-allow-origin')?.toString().toLowerCase().trim();
176+
if (allowed === '*' || origin === allowed) return next();
177+
178+
res.statusCode = 403;
179+
res.end();
180+
});
175181
}
176182

177183
this.app.use(jsonParser({ payloadLimit: 50 * 1024 * 1024 }));

src/client/admin-client.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import _ = require('lodash');
22
import { EventEmitter } from 'events';
33
import { Duplex } from 'stream';
44
import DuplexPair = require('native-duplexpair');
5-
import { TypedError } from 'typed-error';
65
import * as WebSocket from 'isomorphic-ws';
76
import connectWebSocketStream = require('@httptoolkit/websocket-stream');
87
import { SubscriptionClient } from '@httptoolkit/subscriptions-transport-ws';
9-
import { ErrorLike, MaybePromise, getDeferred } from '@httptoolkit/util';
8+
import { CustomError, ErrorLike, MaybePromise, getDeferred } from '@httptoolkit/util';
109
import { print } from 'graphql';
1110

1211
import { DEFAULT_ADMIN_SERVER_PORT } from "../types";
@@ -20,7 +19,7 @@ import { AdminPlugin, PluginClientResponsesMap, PluginStartParamsMap } from '../
2019
import { SchemaIntrospector } from './schema-introspection';
2120
import { AdminQuery, getSingleSelectedFieldName } from './admin-query';
2221

23-
export class ConnectionError extends TypedError { }
22+
export class ConnectionError extends CustomError { }
2423

2524
// The Response type requires lib.dom. We include an empty placeholder here to
2625
// avoid the types breaking if you don't have that available. Once day TS will
@@ -29,7 +28,7 @@ declare global {
2928
interface Response {}
3029
}
3130

32-
export class RequestError extends TypedError {
31+
export class RequestError extends CustomError {
3332
constructor(
3433
message: string,
3534
public response: Response

src/rules/http-agents.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import * as url from 'url';
33
import * as http from 'http';
44
import * as https from 'https';
55

6-
import * as LRU from 'lru-cache';
6+
import { LRUCache } from 'lru-cache';
77

8-
import getHttpsProxyAgent = require('https-proxy-agent');
8+
import { HttpsProxyAgent } from 'https-proxy-agent';
99
import { PacProxyAgent } from 'pac-proxy-agent';
1010
import { SocksProxyAgent } from 'socks-proxy-agent';
11-
const getSocksProxyAgent = (opts: any) => new SocksProxyAgent(opts);
1211

1312
import { isNode } from "../util/util";
1413
import { getProxySetting, matchesNoProxy, ProxySettingSource } from './proxy-config';
@@ -24,25 +23,27 @@ const KeepAliveAgents = isNode
2423
})
2524
} : {};
2625

26+
const buildHttpsProxyAgent = (href: string, opts?: any) => new HttpsProxyAgent(href, opts);
27+
const buildSocksProxyAgent = (href: string, opts?: any) => new SocksProxyAgent(href, opts);
28+
2729
const ProxyAgentFactoryMap = {
28-
'http:': getHttpsProxyAgent, // HTTPS here really means 'CONNECT-tunnelled' - it can do either
29-
'https:': getHttpsProxyAgent,
30+
'http:': buildHttpsProxyAgent, // HTTPS here really means 'CONNECT-tunnelled' - it can do either
31+
'https:': buildHttpsProxyAgent,
3032

3133
'pac+http:': (...args: any) => new PacProxyAgent(...args),
3234
'pac+https:': (...args: any) => new PacProxyAgent(...args),
3335

34-
'socks:': getSocksProxyAgent,
35-
'socks4:': getSocksProxyAgent,
36-
'socks4a:': getSocksProxyAgent,
37-
'socks5:': getSocksProxyAgent,
38-
'socks5h:': getSocksProxyAgent
36+
'socks:': buildSocksProxyAgent,
37+
'socks4:': buildSocksProxyAgent,
38+
'socks4a:': buildSocksProxyAgent,
39+
'socks5:': buildSocksProxyAgent,
40+
'socks5h:': buildSocksProxyAgent
3941
} as const;
4042

41-
const proxyAgentCache = new LRU<string, http.Agent>({
43+
const proxyAgentCache = new LRUCache<string, http.Agent>({
4244
max: 20,
4345

4446
ttl: 1000 * 60 * 5, // Drop refs to unused agents after 5 minutes
45-
ttlResolution: 1000 * 60, // Check for expiry once every minute maximum
4647
ttlAutopurge: true, // Actively drop expired agents
4748
updateAgeOnGet: true // Don't drop agents while they're in use
4849
});
@@ -87,18 +88,10 @@ export async function getAgent({
8788
proxySetting.additionalTrustedCAs
8889
);
8990

90-
proxyAgentCache.set(cacheKey, buildProxyAgent({
91-
href,
92-
protocol,
93-
auth,
94-
hostname,
95-
port,
96-
97-
...(trustedCerts
98-
? { ca: trustedCerts }
99-
: {}
100-
)
101-
}));
91+
proxyAgentCache.set(cacheKey, buildProxyAgent(
92+
href!,
93+
trustedCerts ? { ca: trustedCerts } : {}
94+
));
10295
}
10396

10497
return proxyAgentCache.get(cacheKey);

src/rules/requests/request-step-impls.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import * as h2Client from 'http2-wrapper';
1313
import { decode as decodeBase64 } from 'base64-arraybuffer';
1414
import { Transform } from 'stream';
1515
import { stripIndent, oneLine } from 'common-tags';
16-
import { TypedError } from 'typed-error';
1716
import { applyPatch as applyJsonPatch } from 'fast-json-patch';
1817

1918
import {
@@ -23,7 +22,7 @@ import {
2322
OngoingResponse
2423
} from "../../types";
2524

26-
import { MaybePromise, ErrorLike, isErrorLike, delay } from '@httptoolkit/util';
25+
import { CustomError, MaybePromise, ErrorLike, isErrorLike, delay } from '@httptoolkit/util';
2726
import { isAbsoluteUrl, getEffectivePort } from '../../util/url';
2827
import {
2928
waitForCompletedRequest,
@@ -139,7 +138,7 @@ export {
139138

140139
// An error that indicates that the step is aborting the request.
141140
// This could be intentional, or an upstream server aborting the request.
142-
export class AbortError extends TypedError {
141+
export class AbortError extends CustomError {
143142

144143
constructor(
145144
message: string,

src/server/http-combo-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import _ = require('lodash');
2-
import now = require('performance-now');
32
import { Writable } from 'stream';
43
import * as net from 'net';
54
import * as tls from 'tls';
65
import * as http from 'http';
76
import * as http2 from 'http2';
87

98
import * as semver from 'semver';
9+
10+
const now = () => performance.now();
1011
import { makeDestroyable, DestroyableServer } from 'destroyable-server';
1112
import * as httpolyglot from '@httptoolkit/httpolyglot';
1213
import { CustomError, delay, unreachableCheck } from '@httptoolkit/util';

src/server/mockttp-server.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import * as http2 from "http2";
88

99
import * as _ from "lodash";
1010
import { EventEmitter } from 'events';
11-
import portfinder = require("portfinder");
11+
import getPort, { portNumbers } from 'get-port';
1212
import connect = require("connect");
1313
import cors = require("cors");
14-
import now = require("performance-now");
1514
import WebSocket = require("ws");
15+
16+
const now = () => performance.now();
1617
import { Mutex } from 'async-mutex';
1718
import { ErrorLike, isErrorLike, UnreachableCheck } from '@httptoolkit/util';
1819

@@ -182,11 +183,10 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
182183

183184
// We use a mutex here to avoid contention on ports with parallel setup
184185
await serverPortCheckMutex.runExclusive(async () => {
185-
const port = _.isNumber(portParam)
186+
const port = typeof portParam === 'number'
186187
? portParam
187-
: await portfinder.getPortPromise({
188-
port: portParam.startPort,
189-
stopPort: portParam.endPort
188+
: await getPort({
189+
port: portNumbers(portParam.startPort, portParam.endPort)
190190
});
191191

192192
if (this.debug) console.log(`Starting mock server on port ${port}`);
@@ -212,7 +212,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
212212
// Although we try to pick a free port, we may have race conditions, if something else
213213
// takes the same port at the same time. If you haven't explicitly picked a port, and
214214
// we do have a collision, simply try again.
215-
if (e.code === 'EADDRINUSE' && !_.isNumber(portParam)) {
215+
if (e.code === 'EADDRINUSE' && typeof portParam !== 'number') {
216216
if (this.debug) console.log('Address in use, retrying...');
217217

218218
// Destroy just in case there is something that needs cleanup here. Catch because most

src/util/request-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import * as http2 from 'http2';
99

1010
import * as _ from 'lodash';
1111
import * as multipart from 'parse-multipart-data';
12-
import now = require("performance-now");
1312
import type { SUPPORTED_ENCODING } from 'http-encoding';
1413
import { MaybePromise } from '@httptoolkit/util';
1514

15+
const now = () => performance.now();
16+
1617
import {
1718
Headers,
1819
OngoingRequest,

src/util/socket-util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import * as _ from 'lodash';
2-
import now = require("performance-now");
32
import * as os from 'os';
43
import * as net from 'net';
54
import * as tls from 'tls';
65
import * as http2 from 'http2';
76

87
import { isNode } from './util';
8+
9+
const now = () => performance.now();
910
import {
1011
OngoingRequest,
1112
RawPassthroughEvent,

0 commit comments

Comments
 (0)