Skip to content

Commit 21a1a1f

Browse files
committed
Drop lots of very old backward compatibility & deprecated features:
* Lots of internal API compat for old clients/servers, which shouldn't affect any external usage at all unless you hit the internal GraphQL remote client API manually. * Dropped some type aliases exposed for backward compat: TlsRequest, HttpsOptions, HttpsPathOptions (now TlsHandshakeFailure, CertDataOptions, CertPathOptions) * Dropped ECONNRESET-only passthrough error simulation. Whether upstream connection errors are simulated downstream is now controlled exclusively by the simulateConnectionErrors passthrough rule option. * Dropped 'status' option on callback results - use statusCode instead. * Dropped deprecated 'trustAdditionalCAs' alias for 'additionalTrustedCAs' * Dropped deprecated string format in proxySetting.trustedCAs array. Everything dropped has been deprecated/backward-compat-only for 3+ years now, so this is unlikely to affect most users in practice.
1 parent f9b4e72 commit 21a1a1f

18 files changed

+74
-230
lines changed

src/admin/admin-server.ts

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,7 @@ export class AdminServer<Plugins extends { [key: string]: AdminPlugin<any, any>
181181
this.app.post('/start', async (req, res) => {
182182
try {
183183
const rawConfig = req.body;
184-
185-
// New clients send: "{ plugins: { http: {...}, webrtc: {...} } }" etc. Old clients just send
186-
// the HTTP options bare with no wrapper, so we wrap them for backward compat.
187-
const isPluginAwareClient = ('plugins' in rawConfig);
188-
189-
const providedPluginStartParams = (!isPluginAwareClient
190-
? { // Backward compat: this means the client is not plugin-aware, and so all options are Mockttp options
191-
http: {
192-
options: _.cloneDeep(rawConfig),
193-
port: (typeof req.query.port === 'string')
194-
? JSON.parse(req.query.port)
195-
: undefined
196-
}
197-
}
198-
: rawConfig.plugins
199-
) as PluginStartParamsMap<Plugins>;
184+
const providedPluginStartParams = rawConfig.plugins as PluginStartParamsMap<Plugins>;
200185

201186
// For each plugin that was specified, we pull default params into their start params.
202187
const pluginStartParams = _.mapValues((providedPluginStartParams), (params, pluginId) => {
@@ -205,15 +190,6 @@ export class AdminServer<Plugins extends { [key: string]: AdminPlugin<any, any>
205190

206191
if (this.debug) console.log('Admin server starting mock session with config', pluginStartParams);
207192

208-
// Backward compat: do an explicit check for HTTP port conflicts
209-
const httpPort = (pluginStartParams as { http?: { port: number } }).http?.port;
210-
if (_.isNumber(httpPort) && this.sessions[httpPort] != null) {
211-
res.status(409).json({
212-
error: `Cannot start: mock server is already running on port ${httpPort}`
213-
});
214-
return;
215-
}
216-
217193
const missingPluginId = Object.keys(pluginStartParams).find(pluginId => !(pluginId in this.adminPlugins));
218194
if (missingPluginId) {
219195
res.status(400).json({
@@ -233,28 +209,15 @@ export class AdminServer<Plugins extends { [key: string]: AdminPlugin<any, any>
233209
)
234210
);
235211

236-
// More backward compat: old clients assume that the port is also the management id.
237-
const sessionId = isPluginAwareClient
238-
? uuid()
239-
: (sessionPlugins as any as {
240-
'http': MockttpAdminPlugin
241-
}).http.getMockServer().port.toString();
242-
212+
const sessionId = uuid();
243213
await this.startSessionManagementAPI(sessionId, sessionPlugins);
244214

245-
if (isPluginAwareClient) {
246-
res.json({
247-
id: sessionId,
248-
pluginData: _.mapValues(pluginStartResults, (r: unknown) =>
249-
r ?? {} // Always return _something_, even if the plugin returns null/undefined.
250-
)
251-
});
252-
} else {
253-
res.json({
254-
id: sessionId,
255-
...(pluginStartResults['http']!)
256-
});
257-
}
215+
res.json({
216+
id: sessionId,
217+
pluginData: _.mapValues(pluginStartResults, (r: unknown) =>
218+
r ?? {} // Always return _something_, even if the plugin returns null/undefined.
219+
)
220+
});
258221
} catch (e) {
259222
res.status(500).json({ error: `Failed to start mock session: ${
260223
(isErrorLike(e) && e.message) || e
@@ -290,7 +253,6 @@ export class AdminServer<Plugins extends { [key: string]: AdminPlugin<any, any>
290253
}
291254

292255
this.app.use('/session/:id/', sessionRequest);
293-
this.app.use('/server/:id/', sessionRequest); // Old URL for backward compat
294256
}
295257

296258
async resetAdminServer() {

src/admin/mockttp-admin-model.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,6 @@ export function buildAdminServerModel(
103103
deserializeRuleData(rule, stream, ruleParameters)
104104
));
105105
},
106-
setFallbackRule: async (__: any, { input }: { input: Serialized<RequestRuleData> }) => {
107-
// Deprecated endpoint, but preserved for API backward compat
108-
const ruleData = deserializeRuleData(input, stream, ruleParameters);
109-
return mockServer.addRequestRules({
110-
...ruleData,
111-
priority: 0
112-
}).then((rules) => rules[0]);
113-
},
114106

115107
addWebSocketRule: async (__: any, { input }: { input: Serialized<WebSocketRuleData> }) => {
116108
return mockServer.addWebSocketRule(deserializeWebSocketRuleData(input, stream, ruleParameters));

src/admin/mockttp-schema.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const MockttpSchema = gql`
1111
addRule(input: MockRule!): MockedEndpoint!
1212
addRules(input: [MockRule!]!): [MockedEndpoint!]!
1313
setRules(input: [MockRule!]!): [MockedEndpoint!]!
14-
setFallbackRule(input: MockRule!): MockedEndpoint!
1514
1615
addWebSocketRule(input: WebSocketMockRule!): MockedEndpoint!
1716
addWebSocketRules(input: [WebSocketMockRule!]!): [MockedEndpoint!]!
@@ -85,17 +84,6 @@ export const MockttpSchema = gql`
8584
tlsMetadata: Json!
8685
}
8786
88-
# Old name for TlsHandshakeFailure, kept for backward compat
89-
type TlsRequest {
90-
failureCause: String!
91-
92-
hostname: String
93-
remoteIpAddress: String
94-
remotePort: Int
95-
tags: [String!]!
96-
timingEvents: Json!
97-
}
98-
9987
type ClientError {
10088
errorCode: String
10189
request: ClientErrorRequest!

src/client/admin-client.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async function requestFromAdminServer<T>(serverUrl: string, path: string, option
149149
jsonBody = JSON.parse(body);
150150
} catch (e) { }
151151

152-
if (jsonBody && jsonBody.error) {
152+
if (jsonBody?.error) {
153153
throw new RequestError(
154154
jsonBody.error,
155155
response
@@ -442,8 +442,7 @@ export class AdminClient<Plugins extends { [key: string]: AdminPlugin<any, any>
442442

443443
const path = portConfig ? `/start?port=${JSON.stringify(portConfig)}` : '/start';
444444
const adminServerResponse = await requestFromAdminServer<
445-
| { port: number, mockRoot: string } // Backward compat for old servers
446-
| { id: string, pluginData: PluginClientResponsesMap<Plugins> } // New plugin-aware servers
445+
{ id: string, pluginData: PluginClientResponsesMap<Plugins> } // New plugin-aware servers
447446
>(
448447
this.adminClientOptions.adminServerUrl,
449448
path,
@@ -453,23 +452,13 @@ export class AdminClient<Plugins extends { [key: string]: AdminPlugin<any, any>
453452
'Content-Type': 'application/json'
454453
}),
455454
body: JSON.stringify({
456-
plugins: pluginStartParams,
457-
// Include all the Mockttp params at the root too, for backward compat with old admin servers:
458-
...(pluginStartParams.http?.options as MockttpOptions | undefined)
455+
plugins: pluginStartParams
459456
})
460457
}, this.adminClientOptions.requestOptions)
461458
);
462459

463-
// Backward compat for old servers
464-
const isPluginAwareServer = 'id' in adminServerResponse;
465-
466-
const sessionId = isPluginAwareServer
467-
? adminServerResponse.id
468-
: adminServerResponse.port.toString();
469-
470-
const adminSessionBaseUrl = `${this.adminClientOptions.adminServerUrl}/${
471-
isPluginAwareServer ? 'session' : 'server'
472-
}/${sessionId}`
460+
const sessionId = adminServerResponse.id;
461+
const adminSessionBaseUrl = `${this.adminClientOptions.adminServerUrl}/session/${sessionId}`
473462

474463
// Also open a stream connection, for 2-way communication we might need later.
475464
const adminServerStream = await this.openStreamToMockServer(adminSessionBaseUrl);
@@ -496,14 +485,8 @@ export class AdminClient<Plugins extends { [key: string]: AdminPlugin<any, any>
496485

497486
if (this.debug) console.log('Started remote mock server');
498487

499-
const serverMetadata =
500-
this.adminServerMetadata = // Set field before we resolve the promise
501-
'pluginData' in adminServerResponse
502-
? adminServerResponse.pluginData
503-
: {
504-
// Backward compat - convert old always-HTTP data into per-plugin format:
505-
http: adminServerResponse
506-
} as unknown as PluginClientResponsesMap<Plugins>;
488+
// Set field before we resolve the promise:
489+
const serverMetadata = this.adminServerMetadata = adminServerResponse.pluginData;
507490

508491
startPromise.resolve(true);
509492
return serverMetadata;

src/client/mockttp-admin-request-builder.ts

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ function normalizeHttpMessage(message: any, event?: SubscribableEvent) {
3232
// We use raw headers where possible to derive headers, instead of using any pre-derived
3333
// header data, for maximum accuracy (and to avoid any need to query for both).
3434
message.headers = rawHeadersToObject(message.rawHeaders);
35-
} else if (message.headers) {
36-
// Backward compat for older servers:
37-
message.headers = JSON.parse(message.headers);
38-
message.rawHeaders = objectHeadersToRaw(message.headers);
3935
}
4036

4137
if (message.rawTrailers) {
@@ -236,19 +232,16 @@ export class MockttpAdminRequestBuilder {
236232
237233
${this.schema.asOptionalField('InitiatedRequest', 'destination', 'destination { hostname, port }')}
238234
239-
${this.schema.typeHasField('InitiatedRequest', 'rawHeaders')
240-
? 'rawHeaders'
241-
: 'headers'
242-
}
235+
rawHeaders
243236
timingEvents
244237
httpVersion
245-
${this.schema.asOptionalField('InitiatedRequest', 'tags')}
238+
tags
246239
}
247240
}`,
248241
request: gql`subscription OnRequest {
249242
requestReceived {
250243
id
251-
${this.schema.asOptionalField('Request', 'matchedRuleId')}
244+
matchedRuleId
252245
protocol
253246
method
254247
url
@@ -258,17 +251,13 @@ export class MockttpAdminRequestBuilder {
258251
259252
${this.schema.asOptionalField('Request', 'destination', 'destination { hostname, port }')}
260253
261-
${this.schema.typeHasField('Request', 'rawHeaders')
262-
? 'rawHeaders'
263-
: 'headers'
264-
}
265-
254+
rawHeaders
266255
body
267256
${this.schema.asOptionalField('Request', 'rawTrailers')}
268257
269-
${this.schema.asOptionalField('Request', 'timingEvents')}
270-
${this.schema.asOptionalField('Request', 'httpVersion')}
271-
${this.schema.asOptionalField('Request', 'tags')}
258+
timingEvents
259+
httpVersion
260+
tags
272261
}
273262
}`,
274263
response: gql`subscription OnResponse {
@@ -277,16 +266,12 @@ export class MockttpAdminRequestBuilder {
277266
statusCode
278267
statusMessage
279268
280-
${this.schema.typeHasField('Response', 'rawHeaders')
281-
? 'rawHeaders'
282-
: 'headers'
283-
}
284-
269+
rawHeaders
285270
body
286271
${this.schema.asOptionalField('Response', 'rawTrailers')}
287272
288-
${this.schema.asOptionalField('Response', 'timingEvents')}
289-
${this.schema.asOptionalField('Response', 'tags')}
273+
timingEvents
274+
tags
290275
}
291276
}`,
292277
'websocket-request': gql`subscription OnWebSocketRequest {
@@ -370,14 +355,12 @@ export class MockttpAdminRequestBuilder {
370355
371356
${this.schema.asOptionalField('AbortedRequest', 'destination', 'destination { hostname, port }')}
372357
373-
${this.schema.typeHasField('Request', 'rawHeaders')
374-
? 'rawHeaders'
375-
: 'headers'
376-
}
358+
rawHeaders
359+
360+
timingEvents
361+
tags
377362
378-
${this.schema.asOptionalField('Request', 'timingEvents')}
379-
${this.schema.asOptionalField('Request', 'tags')}
380-
${this.schema.asOptionalField('AbortedRequest', 'error')}
363+
error
381364
}
382365
}`,
383366
'tls-passthrough-opened': gql`subscription OnTlsPassthroughOpened {
@@ -397,7 +380,7 @@ export class MockttpAdminRequestBuilder {
397380
remotePort
398381
tags
399382
timingEvents
400-
${this.schema.asOptionalField('TlsPassthroughEvent', 'tlsMetadata')}
383+
tlsMetadata
401384
}
402385
}`,
403386
'tls-passthrough-closed': gql`subscription OnTlsPassthroughClosed {
@@ -417,18 +400,18 @@ export class MockttpAdminRequestBuilder {
417400
remotePort
418401
tags
419402
timingEvents
420-
${this.schema.asOptionalField('TlsPassthroughEvent', 'tlsMetadata')}
403+
tlsMetadata
421404
}
422405
}`,
423406
'tls-client-error': gql`subscription OnTlsClientError {
424407
failedTlsRequest {
425408
failureCause
426409
hostname
427410
remoteIpAddress
428-
${this.schema.asOptionalField(['TlsHandshakeFailure', 'TlsRequest'], 'remotePort')}
429-
${this.schema.asOptionalField(['TlsHandshakeFailure', 'TlsRequest'], 'tags')}
430-
${this.schema.asOptionalField(['TlsHandshakeFailure', 'TlsRequest'], 'timingEvents')}
431-
${this.schema.asOptionalField(['TlsHandshakeFailure', 'TlsRequest'], 'tlsMetadata')}
411+
remotePort
412+
tags
413+
timingEvents
414+
tlsMetadata
432415
}
433416
}`,
434417
'client-error': gql`subscription OnClientError {
@@ -444,10 +427,7 @@ export class MockttpAdminRequestBuilder {
444427
url
445428
path
446429
447-
${this.schema.typeHasField('ClientErrorRequest', 'rawHeaders')
448-
? 'rawHeaders'
449-
: 'headers'
450-
}
430+
rawHeaders
451431
452432
${this.schema.asOptionalField('ClientErrorRequest', 'remoteIpAddress')}
453433
${this.schema.asOptionalField('ClientErrorRequest', 'remotePort')}
@@ -460,10 +440,7 @@ export class MockttpAdminRequestBuilder {
460440
statusCode
461441
statusMessage
462442
463-
${this.schema.typeHasField('Response', 'rawHeaders')
464-
? 'rawHeaders'
465-
: 'headers'
466-
}
443+
rawHeaders
467444
468445
body
469446
${this.schema.asOptionalField('Response', 'rawTrailers')}
@@ -567,16 +544,13 @@ export class MockttpAdminRequestBuilder {
567544
path,
568545
hostname
569546
570-
${this.schema.typeHasField('Request', 'rawHeaders')
571-
? 'rawHeaders'
572-
: 'headers'
573-
}
547+
rawHeaders
574548
575549
body,
576-
${this.schema.asOptionalField('Request', 'timingEvents')}
577-
${this.schema.asOptionalField('Request', 'httpVersion')}
550+
timingEvents
551+
httpVersion
578552
}
579-
${this.schema.asOptionalField('MockedEndpoint', 'isPending')}
553+
isPending
580554
}
581555
}
582556
`,

src/main.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ export type {
2424
SocksServerOptions
2525
};
2626

27-
// Export now-renamed types with the old aliases to provide backward compat and
28-
// avoid unnecessary type breakage:
29-
export type { TlsHandshakeFailure as TlsRequest } from './types';
30-
export type {
31-
CertDataOptions as HttpsOptions,
32-
CertPathOptions as HttpsPathOptions
33-
} from './util/tls';
34-
3527
// Export rule data builders & type definitions:
3628
import * as matchers from './rules/matchers';
3729
import * as requestHandlers from './rules/requests/request-handlers';

0 commit comments

Comments
 (0)