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