-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathrequest-handler-definitions.ts
More file actions
1110 lines (979 loc) · 41.3 KB
/
request-handler-definitions.ts
File metadata and controls
1110 lines (979 loc) · 41.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import _ = require('lodash');
import url = require('url');
import type * as net from 'net';
import { encode as encodeBase64 } from 'base64-arraybuffer';
import { Readable, Transform } from 'stream';
import { stripIndent } from 'common-tags';
import {
Headers,
CompletedRequest,
CompletedBody,
Explainable,
RawHeaders
} from "../../types";
import { MaybePromise, Replace } from '../../util/type-utils';
import { buildBodyReader } from '../../util/request-utils';
import { asBuffer } from '../../util/buffer-utils';
import {
Serializable,
ClientServerChannel,
serializeBuffer,
SerializedProxyConfig,
serializeProxyConfig
} from "../../serialization/serialization";
import {
withDeserializedBodyReader,
withSerializedCallbackBuffers
} from '../../serialization/body-serialization';
import { ProxyConfig } from '../proxy-config';
/*
This file defines request handler *definitions*, which includes everything necessary to define
and serialize a request handler's behaviour, but doesn't include the actual handling logic (which
lives in ./request-handlers instead). This is intended to allow tree-shaking in browser usage
or remote clients to import only the necessary code, with no need to include all the real
request-processing and handling code that is only used at HTTP-runtime, so isn't relevant when
defining rules.
Every RequestHandler extends its definition, simply adding a handle() method, which handles
requests according to the configuration, and adding a deserialize static method that takes
the serialized output from the serialize() methods defined here and creates a working handler.
*/
export interface RequestHandlerDefinition extends Explainable, Serializable {
type: keyof typeof HandlerDefinitionLookup;
}
export type SerializedBuffer = { type: 'Buffer', data: number[] };
/**
* Can be returned from callbacks to override parts of a request.
*
* All fields are optional, and omitted values will default to the original
* request value.
*/
export interface CallbackRequestResult {
/**
* A replacement HTTP method, capitalized.
*/
method?: string;
/**
* The full URL to send the request to. If set, this will redirect
* the request and automatically update the Host header accordingly,
* unless you also provide a `headers` value that includes a Host
* header, in which case that will take used as-is.
*/
url?: string;
/**
* The replacement HTTP headers, as an object of string keys and either
* single string or array of string values.
*/
headers?: Headers;
/**
* A string or buffer, which replaces the request body if set. This will
* be automatically content-encoded to match the Content-Encoding defined
* in your request headers.
*
* If this is set, the Content-Length header will be automatically updated
* accordingly to match, unless you also provide a `headers` value that
* includes a Content-Length header, in which case that will take used
* as-is.
*
* You should only return one body field: either `body`, `rawBody` or
* `json`.
*/
body?: string | Buffer | Uint8Array;
/**
* A buffer, which replaces the request body if set, which is sent exactly
* as is, and is not automatically encoded.
*
* If this is set, the Content-Length header will be automatically updated
* accordingly to match, unless you also provide a `headers` value that
* includes a Content-Length header, in which case that will take used
* as-is.
*
* You should only return one body field: either `body`, `rawBody` or
* `json`.
*/
rawBody?: Buffer | Uint8Array;
/**
* A JSON value, which will be stringified and send as a JSON-encoded
* request body. This will be automatically content-encoded to match
* the Content-Encoding defined in your request headers.
*
* If this is set, the Content-Length header will be automatically updated
* accordingly to match, unless you also provide a `headers` value that
* includes a Content-Length header, in which case that will take used
* as-is.
*
* You should only return one body field: either `body`, `rawBody` or
* `json`.
*/
json?: any;
/**
* A response: either a response object defining the fields of a response
* or the string 'close' to immediately close the connection.
*
* See {@link CallbackResponseMessageResult} for the possible fields that can
* be set to define the response.
*
* If set, the request will not be forwarded at all, and this will be used
* as the response to immediately return to the client (or for 'close', this
* will immediately close the connection to the client).
*/
response?: CallbackResponseResult;
}
export type CallbackResponseResult =
| CallbackResponseMessageResult
| 'close'
| 'reset';
/**
* Can be returned from callbacks to define parts of a response, or
* override parts when given an existing repsonse.
*
* All fields are optional, and omitted values will default to the original
* response value or a default value.
*/
export interface CallbackResponseMessageResult {
/**
* The response status code as a number.
*
* Defaults to 200 if not set.
*/
statusCode?: number;
/**
* Supported only for backward compatibility.
*
* @deprecated Use statusCode instead.
*/
status?: number;
/**
* The response status message, as a string. This is ignored for
* HTTP/2 responses.
*
* Defaults to the default status message for the status code if not set.
*/
statusMessage?: string;
/**
* The replacement HTTP headers, as an object of string keys and either
* single string or array of string values.
*
* Defaults to a minimum set of standard required headers if not set.
*/
headers?: Headers;
/**
* A string or buffer, which replaces the response body if set. This will
* be automatically encoded to match the Content-Encoding defined in your
* response headers.
*
* If this is set, the Content-Length header will be automatically updated
* accordingly to match, unless you also provide a `headers` value that
* includes a Content-Length header, in which case that will take used
* as-is.
*
* Defaults to empty.
*
* You should only return one body field: either `body`, `rawBody` or
* `json`.
*/
body?: string | Buffer | Uint8Array;
/**
* A buffer, which replaces the response body if set, which is sent exactly
* as is, and is not automatically encoded.
*
* If this is set, the Content-Length header will be automatically updated
* accordingly to match, unless you also provide a `headers` value that
* includes a Content-Length header, in which case that will take used
* as-is.
*
* You should only return one body field: either `body`, `rawBody` or
* `json`.
*/
rawBody?: Buffer | Uint8Array | Readable;
/**
* A JSON value, which will be stringified and send as a JSON-encoded
* request body. This will be automatically content-encoded to match the
* Content-Encoding defined in your response headers.
*
* If this is set, the Content-Length header will be automatically updated
* accordingly to match, unless you also provide a `headers` value that
* includes a Content-Length header, in which case that will take used
* as-is.
*
* You should only return one body field: either `body`, `rawBody` or
* `json`.
*/
json?: any;
}
function validateCustomHeaders(
originalHeaders: Headers,
modifiedHeaders: Headers | undefined,
headerWhitelist: readonly string[] = []
) {
if (!modifiedHeaders) return;
// We ignore most returned pseudo headers, so we error if you try to manually set them
const invalidHeaders = _(modifiedHeaders)
.pickBy((value, name) =>
name.toString().startsWith(':') &&
// We allow returning a preexisting header value - that's ignored
// silently, so that mutating & returning the provided headers is always safe.
value !== originalHeaders[name] &&
// In some cases, specific custom pseudoheaders may be allowed, e.g. requests
// can have custom :scheme and :authority headers set.
!headerWhitelist.includes(name)
)
.keys();
if (invalidHeaders.size() > 0) {
throw new Error(
`Cannot set custom ${invalidHeaders.join(', ')} pseudoheader values`
);
}
}
export class SimpleHandlerDefinition extends Serializable implements RequestHandlerDefinition {
readonly type = 'simple';
constructor(
public status: number,
public statusMessage?: string,
public data?: string | Uint8Array | Buffer | SerializedBuffer,
public headers?: Headers
) {
super();
validateCustomHeaders({}, headers);
}
explain() {
return `respond with status ${this.status}` +
(this.statusMessage ? ` (${this.statusMessage})`: "") +
(this.headers ? `, headers ${JSON.stringify(this.headers)}` : "") +
(this.data ? ` and body "${this.data}"` : "");
}
}
/**
* @internal
*/
export interface SerializedCallbackHandlerData {
type: string;
name?: string;
version?: number;
}
/**
* @internal
*/
export interface CallbackRequestMessage {
args: [
| Replace<CompletedRequest, { body: string }> // New format
| CompletedRequest // Old format with directly serialized body
];
}
export class CallbackHandlerDefinition extends Serializable implements RequestHandlerDefinition {
readonly type = 'callback';
constructor(
public callback: (request: CompletedRequest) => MaybePromise<CallbackResponseResult>
) {
super();
}
explain() {
return 'respond using provided callback' + (this.callback.name ? ` (${this.callback.name})` : '');
}
/**
* @internal
*/
serialize(channel: ClientServerChannel): SerializedCallbackHandlerData {
channel.onRequest<
CallbackRequestMessage,
CallbackResponseResult
>(async (streamMsg) => {
const request = _.isString(streamMsg.args[0].body)
? withDeserializedBodyReader( // New format: body serialized as base64
streamMsg.args[0] as Replace<CompletedRequest, { body: string }>
)
: { // Backward compat: old fully-serialized format
...streamMsg.args[0],
body: buildBodyReader(streamMsg.args[0].body.buffer, streamMsg.args[0].headers)
};
const callbackResult = await this.callback.call(null, request);
if (typeof callbackResult === 'string') {
return callbackResult;
} else {
return withSerializedCallbackBuffers(callbackResult);
}
});
return { type: this.type, name: this.callback.name, version: 2 };
}
}
/**
* @internal
*/
export interface SerializedStreamHandlerData {
type: string;
status: number;
headers?: Headers;
};
interface StreamHandlerMessage {
event: 'data' | 'end' | 'close' | 'error';
content: StreamHandlerEventMessage;
}
type StreamHandlerEventMessage =
{ type: 'string', value: string } |
{ type: 'buffer', value: string } |
{ type: 'arraybuffer', value: string } |
{ type: 'nil' };
export class StreamHandlerDefinition extends Serializable implements RequestHandlerDefinition {
readonly type = 'stream';
constructor(
public status: number,
public stream: Readable & { done?: true },
public headers?: Headers
) {
super();
validateCustomHeaders({}, headers);
}
explain() {
return `respond with status ${this.status}` +
(this.headers ? `, headers ${JSON.stringify(this.headers)},` : "") +
' and a stream of response data';
}
/**
* @internal
*/
serialize(channel: ClientServerChannel): SerializedStreamHandlerData {
const serializationStream = new Transform({
objectMode: true,
transform: function (this: Transform, chunk, _encoding, callback) {
let serializedEventData: StreamHandlerEventMessage | false =
_.isString(chunk) ? { type: 'string', value: chunk } :
_.isBuffer(chunk) ? { type: 'buffer', value: chunk.toString('base64') } :
(_.isArrayBuffer(chunk) || _.isTypedArray(chunk)) ? { type: 'arraybuffer', value: encodeBase64(<any> chunk) } :
_.isNil(chunk) && { type: 'nil' };
if (!serializedEventData) {
callback(new Error(`Can't serialize streamed value: ${chunk.toString()}. Streaming must output strings, buffers or array buffers`));
}
callback(undefined, <StreamHandlerMessage> {
event: 'data',
content: serializedEventData
});
},
flush: function(this: Transform, callback) {
this.push(<StreamHandlerMessage> {
event: 'end'
});
callback();
}
});
// When we get a ping from the server-side, pipe the real stream to serialize it and send the data across
channel.once('data', () => {
this.stream.pipe(serializationStream).pipe(channel, { end: false });
});
return { type: this.type, status: this.status, headers: this.headers };
}
}
export class FileHandlerDefinition extends Serializable implements RequestHandlerDefinition {
readonly type = 'file';
constructor(
public status: number,
public statusMessage: string | undefined,
public filePath: string,
public headers?: Headers
) {
super();
validateCustomHeaders({}, headers);
}
explain() {
return `respond with status ${this.status}` +
(this.statusMessage ? ` (${this.statusMessage})`: "") +
(this.headers ? `, headers ${JSON.stringify(this.headers)}` : "") +
(this.filePath ? ` and body from file ${this.filePath}` : "");
}
}
// This is different from CompletedResponse because CompletedResponse is a client request to Mockttp
// whereas this is a real response from an upstream server that we modify before forwarding.
// We aim for a similar shape, but they're not exactly the same.
export interface PassThroughResponse {
id: string;
statusCode: number;
statusMessage?: string;
headers: Headers;
rawHeaders: RawHeaders;
body: CompletedBody;
}
export interface ForwardingOptions {
targetHost: string,
// Should the host (H1) or :authority (H2) header be updated to match?
updateHostHeader?: true | false | string // Change automatically/ignore/change to custom value
}
export interface PassThroughLookupOptions {
/**
* The maximum time to cache a DNS response. Up to this limit,
* responses will be cached according to their own TTL. Defaults
* to Infinity.
*/
maxTtl?: number;
/**
* How long to cache a DNS ENODATA or ENOTFOUND response. Defaults
* to 0.15.
*/
errorTtl?: number;
/**
* The primary servers to use. DNS queries will be resolved against
* these servers first. If no data is available, queries will fall
* back to dns.lookup, and use the OS's default DNS servers.
*
* This defaults to dns.getServers().
*/
servers?: string[];
}
export interface PassThroughHandlerOptions {
/**
* The forwarding configuration for the passthrough rule.
* This generally shouldn't be used explicitly unless you're
* building rule data by hand. Instead, call `thenPassThrough`
* to send data directly or `thenForwardTo` with options to
* configure traffic forwarding.
*/
forwarding?: ForwardingOptions,
/**
* A list of hostnames for which server certificate and TLS version errors
* should be ignored (none, by default).
*
* If set to 'true', HTTPS errors will be ignored for all hosts. WARNING:
* Use this at your own risk. Setting this to `true` can open your
* application to MITM attacks and should never be used over any network
* that is not completed trusted end-to-end.
*/
ignoreHostHttpsErrors?: string[] | boolean;
/**
* An array of additional certificates, which should be trusted as certificate
* authorities for upstream hosts, in addition to Node.js's built-in certificate
* authorities.
*
* Each certificate should be an object with either a `cert` key and a string
* or buffer value containing the PEM certificate, or a `certPath` key and a
* string value containing the local path to the PEM certificate.
*/
trustAdditionalCAs?: Array<{ cert: string | Buffer } | { certPath: string }>;
/**
* A mapping of hosts to client certificates to use, in the form of
* `{ key, cert }` objects (none, by default)
*/
clientCertificateHostMap?: {
[host: string]: { pfx: Buffer, passphrase?: string }
};
/**
* Upstream proxy configuration: pass through requests via this proxy.
*
* If this is undefined, no proxy will be used. To configure a proxy
* provide either:
* - a ProxySettings object
* - a callback which will be called with an object containing the
* hostname, and must return a ProxySettings object or undefined.
* - an array of ProxySettings or callbacks. The array will be
* processed in order, and the first not-undefined ProxySettings
* found will be used.
*
* When using a remote client, this parameter or individual array
* values may be passed by reference, using the name of a rule
* parameter configured in the admin server.
*/
proxyConfig?: ProxyConfig;
/**
* Custom DNS options, to allow configuration of the resolver used
* when forwarding requests upstream. Passing any option switches
* from using node's default dns.lookup function to using the
* cacheable-lookup module, which will cache responses.
*/
lookupOptions?: PassThroughLookupOptions;
/**
* Whether to simulate connection errors back to the client.
*
* By default (in most cases - see below) when an upstream request fails
* outright a 502 "Bad Gateway" response is sent to the downstream client,
* explicitly indicating the failure and containing the error that caused
* the issue in the response body.
*
* Only in the case of upstream connection reset errors is a connection reset
* normally sent back downstream to existing clients (this behaviour exists
* for backward compatibility, and will change to match other error behaviour
* in a future version).
*
* When this option is set to `true`, low-level connection failures will
* always trigger a downstream connection close/reset, rather than a 502
* response.
*
* This includes DNS failures, TLS connection errors, TCP connection resets,
* etc (but not HTTP non-200 responses, which are still proxied as normal).
* This is less convenient for debugging in a testing environment or when
* using a proxy intentionally, but can be more accurate when trying to
* transparently proxy network traffic, errors and all.
*/
simulateConnectionErrors?: boolean;
/**
* A set of data to automatically transform a request. This includes properties
* to support many transformation common use cases.
*
* For advanced cases, a custom callback using beforeRequest can be used instead.
* Using this field however where possible is typically simpler, more declarative,
* and can be more performant. The two options are mutually exclusive: you cannot
* use both transformRequest and a beforeRequest callback.
*
* Only one transformation for each target (method, headers & body) can be
* specified. If more than one is specified then an error will be thrown when the
* rule is registered.
*/
transformRequest?: RequestTransform;
/**
* A set of data to automatically transform a response. This includes properties
* to support many transformation common use cases.
*
* For advanced cases, a custom callback using beforeResponse can be used instead.
* Using this field however where possible is typically simpler, more declarative,
* and can be more performant. The two options are mutually exclusive: you cannot
* use both transformResponse and a beforeResponse callback.
*
* Only one transformation for each target (status, headers & body) can be
* specified. If more than one is specified then an error will be thrown when the
* rule is registered.
*/
transformResponse?: ResponseTransform;
/**
* A callback that will be passed the full request before it is passed through,
* and which returns an object that defines how the the request content should
* be transformed before it's passed to the upstream server.
*
* The callback can return an object to define how the request should be changed.
* All fields on the object are optional, and returning undefined is equivalent
* to returning an empty object (transforming nothing).
*
* See {@link CallbackRequestResult} for the possible fields that can be set.
*/
beforeRequest?: (req: CompletedRequest) => MaybePromise<CallbackRequestResult | void> | void;
/**
* A callback that will be passed the full response before it is passed through,
* and which returns a value that defines how the the response content should
* be transformed before it's returned to the client.
*
* The callback can either return an object to define how the response should be
* changed, or the string 'close' to immediately close the underlying connection.
*
* All fields on the object are optional, and returning undefined is equivalent
* to returning an empty object (transforming nothing).
*
* See {@link CallbackResponseMessageResult} for the possible fields that can be set.
*/
beforeResponse?: (res: PassThroughResponse) => MaybePromise<CallbackResponseResult | void> | void;
}
export interface RequestTransform {
/**
* A replacement HTTP method. Case insensitive.
*/
replaceMethod?: string;
/**
* A headers object which will be merged with the real request headers to add or
* replace values. Headers with undefined values will be removed.
*/
updateHeaders?: Headers;
/**
* A headers object which will completely replace the real request headers.
*/
replaceHeaders?: Headers;
/**
* A string or buffer that replaces the request body entirely.
*
* If this is specified, the upstream request will not wait for the original request
* body, so this may make responses faster than they would be otherwise given large
* request bodies or slow/streaming clients.
*/
replaceBody?: string | Uint8Array | Buffer;
/**
* The path to a file, which will be used to replace the request body entirely. The
* file will be re-read for each request, so the body will always reflect the latest
* file contents.
*
* If this is specified, the upstream request will not wait for the original request
* body, so this may make responses faster than they would be otherwise given large
* request bodies or slow/streaming clients.
*/
replaceBodyFromFile?: string;
/**
* A JSON object which will be merged with the real request body. Undefined values
* will be removed. Any requests which are received with an invalid JSON body that
* match this rule will fail.
*/
updateJsonBody?: {
[key: string]: any;
};
/**
* Perform a series of string match & replace operations on the request body.
*
* This parameter should be an array of pairs, which will be applied to the body
* decoded as a string like `body.replace(p1, p2)`, applied in the order provided.
* The first parameter can be either a string or RegExp to match, and the second
* must be a string to insert. The normal `str.replace` $ placeholders can be
* used in the second argument, so that e.g. $1 will insert the 1st matched group.
*/
matchReplaceBody?: Array<[string | RegExp, string]>;
}
export interface ResponseTransform {
/**
* A replacement response status code.
*/
replaceStatus?: number;
/**
* A headers object which will be merged with the real response headers to add or
* replace values. Headers with undefined values will be removed.
*/
updateHeaders?: Headers;
/**
* A headers object which will completely replace the real response headers.
*/
replaceHeaders?: Headers;
/**
* A string or buffer that replaces the response body entirely.
*
* If this is specified, the downstream response will not wait for the original response
* body, so this may make responses arrive faster than they would be otherwise given large
* response bodies or slow/streaming servers.
*/
replaceBody?: string | Uint8Array | Buffer;
/**
* The path to a file, which will be used to replace the response body entirely. The
* file will be re-read for each response, so the body will always reflect the latest
* file contents.
*
* If this is specified, the downstream response will not wait for the original response
* body, so this may make responses arrive faster than they would be otherwise given large
* response bodies or slow/streaming servers.
*/
replaceBodyFromFile?: string;
/**
* A JSON object which will be merged with the real response body. Undefined values
* will be removed. Any responses which are received with an invalid JSON body that
* match this rule will fail.
*/
updateJsonBody?: {
[key: string]: any;
};
/**
* Perform a series of string match & replace operations on the response body.
*
* This parameter should be an array of pairs, which will be applied to the body
* decoded as a string like `body.replace(p1, p2)`, applied in the order provided.
* The first parameter can be either a string or RegExp to match, and the second
* must be a string to insert. The normal `str.replace` $ placeholders can be
* used in the second argument, so that e.g. $1 will insert the 1st matched group.
*/
matchReplaceBody?: Array<[string | RegExp, string]>;
}
/**
* @internal
*/
export interface SerializedPassThroughData {
type: 'passthrough';
forwardToLocation?: string;
forwarding?: ForwardingOptions;
proxyConfig?: SerializedProxyConfig;
ignoreHostCertificateErrors?: string[] | boolean; // Doesn't match option name, backward compat
extraCACertificates?: Array<{ cert: string } | { certPath: string }>;
clientCertificateHostMap?: { [host: string]: { pfx: string, passphrase?: string } };
lookupOptions?: PassThroughLookupOptions;
simulateConnectionErrors?: boolean;
transformRequest?: Replace<RequestTransform, {
'replaceBody'?: string, // Serialized as base64 buffer
'updateHeaders'?: string, // // Serialized as a string to preserve undefined values
'updateJsonBody'?: string, // Serialized as a string to preserve undefined values
'matchReplaceBody'?: Array<[
string | { regexSource: string, flags: string }, // Regexes serialized
string
]>
}>,
transformResponse?: Replace<ResponseTransform, {
'replaceBody'?: string, // Serialized as base64 buffer
'updateHeaders'?: string, // // Serialized as a string to preserve undefined values
'updateJsonBody'?: string, // Serialized as a string to preserve undefined values
'matchReplaceBody'?: Array<[
string | { regexSource: string, flags: string }, // Regexes serialized
string
]>
}>,
hasBeforeRequestCallback?: boolean;
hasBeforeResponseCallback?: boolean;
}
/**
* @internal
*/
export interface BeforePassthroughRequestRequest {
args: [Replace<CompletedRequest, { body: string }>];
}
/**
* @internal
*/
export interface BeforePassthroughResponseRequest {
args: [Replace<PassThroughResponse, { body: string }>];
}
/**
* Used in merging as a marker for values to omit, because lodash ignores undefineds.
* @internal
*/
export const SERIALIZED_OMIT = "__mockttp__transform__omit__";
export class PassThroughHandlerDefinition extends Serializable implements RequestHandlerDefinition {
readonly type = 'passthrough';
public readonly forwarding?: ForwardingOptions;
public readonly ignoreHostHttpsErrors: string[] | boolean = [];
public readonly clientCertificateHostMap: {
[host: string]: { pfx: Buffer, passphrase?: string }
};
public readonly extraCACertificates: Array<{ cert: string | Buffer } | { certPath: string }> = [];
public readonly transformRequest?: RequestTransform;
public readonly transformResponse?: ResponseTransform;
public readonly beforeRequest?: (req: CompletedRequest) =>
MaybePromise<CallbackRequestResult | void> | void;
public readonly beforeResponse?: (res: PassThroughResponse) =>
MaybePromise<CallbackResponseResult | void> | void;
public readonly proxyConfig?: ProxyConfig;
public readonly lookupOptions?: PassThroughLookupOptions;
public readonly simulateConnectionErrors: boolean;
// Used in subclass - awkwardly needs to be initialized here to ensure that its set when using a
// handler built from a definition. In future, we could improve this (compose instead of inheritance
// to better control handler construction?) but this will do for now.
protected outgoingSockets = new Set<net.Socket>();
constructor(options: PassThroughHandlerOptions = {}) {
super();
// If a location is provided, and it's not a bare hostname, it must be parseable
const { forwarding } = options;
if (forwarding && forwarding.targetHost.includes('/')) {
const { protocol, hostname, port, path } = url.parse(forwarding.targetHost);
if (path && path.trim() !== "/") {
const suggestion = url.format({ protocol, hostname, port }) ||
forwarding.targetHost.slice(0, forwarding.targetHost.indexOf('/'));
throw new Error(stripIndent`
URLs for forwarding cannot include a path, but "${forwarding.targetHost}" does. ${''
}Did you mean ${suggestion}?
`);
}
}
this.forwarding = forwarding;
this.ignoreHostHttpsErrors = options.ignoreHostHttpsErrors || [];
if (!Array.isArray(this.ignoreHostHttpsErrors) && typeof this.ignoreHostHttpsErrors !== 'boolean') {
throw new Error("ignoreHostHttpsErrors must be an array or a boolean");
}
this.lookupOptions = options.lookupOptions;
this.proxyConfig = options.proxyConfig;
this.simulateConnectionErrors = !!options.simulateConnectionErrors;
this.clientCertificateHostMap = options.clientCertificateHostMap || {};
this.extraCACertificates = options.trustAdditionalCAs || [];
if (options.beforeRequest && options.transformRequest && !_.isEmpty(options.transformRequest)) {
throw new Error("BeforeRequest and transformRequest options are mutually exclusive");
} else if (options.beforeRequest) {
this.beforeRequest = options.beforeRequest;
} else if (options.transformRequest) {
if ([
options.transformRequest.updateHeaders,
options.transformRequest.replaceHeaders
].filter(o => !!o).length > 1) {
throw new Error("Only one request header transform can be specified at a time");
}
if ([
options.transformRequest.replaceBody,
options.transformRequest.replaceBodyFromFile,
options.transformRequest.updateJsonBody,
options.transformRequest.matchReplaceBody
].filter(o => !!o).length > 1) {
throw new Error("Only one request body transform can be specified at a time");
}
this.transformRequest = options.transformRequest;
}
if (options.beforeResponse && options.transformResponse && !_.isEmpty(options.transformResponse)) {
throw new Error("BeforeResponse and transformResponse options are mutually exclusive");
} else if (options.beforeResponse) {
this.beforeResponse = options.beforeResponse;
} else if (options.transformResponse) {
if ([
options.transformResponse.updateHeaders,
options.transformResponse.replaceHeaders
].filter(o => !!o).length > 1) {
throw new Error("Only one response header transform can be specified at a time");
}
if ([
options.transformResponse.replaceBody,
options.transformResponse.replaceBodyFromFile,
options.transformResponse.updateJsonBody,
options.transformResponse.matchReplaceBody
].filter(o => !!o).length > 1) {
throw new Error("Only one response body transform can be specified at a time");
}
this.transformResponse = options.transformResponse;
}
}
explain() {
return this.forwarding
? `forward the request to ${this.forwarding.targetHost}`
: 'pass the request through to the target host';
}
/**
* @internal
*/
serialize(channel: ClientServerChannel): SerializedPassThroughData {
if (this.beforeRequest) {
channel.onRequest<
BeforePassthroughRequestRequest,
CallbackRequestResult | undefined
>('beforeRequest', async (req) => {
const callbackResult = await this.beforeRequest!(
withDeserializedBodyReader(req.args[0])
);
const serializedResult = callbackResult
? withSerializedCallbackBuffers(callbackResult)
: undefined;
if (serializedResult?.response && typeof serializedResult?.response !== 'string') {
serializedResult.response = withSerializedCallbackBuffers(serializedResult.response);
}
return serializedResult;
});
}
if (this.beforeResponse) {
channel.onRequest<
BeforePassthroughResponseRequest,
CallbackResponseResult | undefined
>('beforeResponse', async (req) => {
const callbackResult = await this.beforeResponse!(
withDeserializedBodyReader(req.args[0])
);
if (typeof callbackResult === 'string') {
return callbackResult;
} else if (callbackResult) {
return withSerializedCallbackBuffers(callbackResult);
} else {
return undefined;
}
});
}
return {
type: this.type,
...this.forwarding ? {
forwardToLocation: this.forwarding.targetHost,
forwarding: this.forwarding
} : {},
proxyConfig: serializeProxyConfig(this.proxyConfig, channel),
lookupOptions: this.lookupOptions,
simulateConnectionErrors: this.simulateConnectionErrors,
ignoreHostCertificateErrors: this.ignoreHostHttpsErrors,
extraCACertificates: this.extraCACertificates.map((certObject) => {
// We use toString to make sure that buffers always end up as
// as UTF-8 string, to avoid serialization issues. Strings are an
// easy safe format here, since it's really all just plain-text PEM
// under the hood.
if ('cert' in certObject) {
return { cert: certObject.cert.toString('utf8') }
} else {
return certObject;
}
}),
clientCertificateHostMap: _.mapValues(this.clientCertificateHostMap,
({ pfx, passphrase }) => ({ pfx: serializeBuffer(pfx), passphrase })
),
transformRequest: this.transformRequest ? {
...this.transformRequest,
// Body is always serialized as a base64 buffer:
replaceBody: !!this.transformRequest?.replaceBody
? serializeBuffer(asBuffer(this.transformRequest.replaceBody))
: undefined,
// Update objects need to capture undefined & null as distict values:
updateHeaders: !!this.transformRequest?.updateHeaders
? JSON.stringify(
this.transformRequest.updateHeaders,
(k, v) => v === undefined ? SERIALIZED_OMIT : v
)
: undefined,
updateJsonBody: !!this.transformRequest?.updateJsonBody
? JSON.stringify(