@heojeongbo/wirestage 0.2.0 → 0.3.0
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.
- package/README.md +1 -1
- package/dist/features/rest/index.d.ts +1 -1
- package/dist/features/rest/index.d.ts.map +1 -1
- package/dist/features/rest/lib/calls.d.ts +7 -1
- package/dist/features/rest/lib/calls.d.ts.map +1 -1
- package/dist/features/rest/lib/calls.js +11 -0
- package/dist/features/rest/lib/calls.js.map +1 -1
- package/dist/features/rest/lib/create-rest.d.ts.map +1 -1
- package/dist/features/rest/lib/create-rest.js +22 -8
- package/dist/features/rest/lib/create-rest.js.map +1 -1
- package/dist/features/rest/lib/serve.d.ts +21 -0
- package/dist/features/rest/lib/serve.d.ts.map +1 -0
- package/dist/features/rest/lib/serve.js +223 -0
- package/dist/features/rest/lib/serve.js.map +1 -0
- package/dist/features/rest/model/types.d.ts +37 -1
- package/dist/features/rest/model/types.d.ts.map +1 -1
- package/dist/shared/api/http-server/index.d.ts +2 -0
- package/dist/shared/api/http-server/index.d.ts.map +1 -1
- package/dist/shared/api/http-server/index.js +1 -0
- package/dist/shared/api/http-server/index.js.map +1 -1
- package/dist/shared/api/http-server/serve.d.ts +29 -0
- package/dist/shared/api/http-server/serve.d.ts.map +1 -0
- package/dist/shared/api/http-server/serve.js +54 -0
- package/dist/shared/api/http-server/serve.js.map +1 -0
- package/package.json +1 -1
- package/src/features/rest/index.ts +2 -0
- package/src/features/rest/lib/calls.ts +27 -1
- package/src/features/rest/lib/create-rest.ts +28 -12
- package/src/features/rest/lib/serve.ts +338 -0
- package/src/features/rest/model/types.ts +40 -1
- package/src/shared/api/http-server/index.ts +6 -0
- package/src/shared/api/http-server/serve.ts +112 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heojeongbo/wirestage",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Record, fake and fault-inject browser wire traffic in Playwright: REST, Connect, gRPC-web, gRPC, SSE, WebSocket and WebRTC data channels on one timeline.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import type { Frame } from "../../../entities/frame/index.js";
|
|
2
|
-
import type { RestCall } from "../model/types.js";
|
|
2
|
+
import type { RestCall, RestPath } from "../model/types.js";
|
|
3
|
+
import { compilePath } from "./path.js";
|
|
4
|
+
|
|
5
|
+
export interface CallFilter {
|
|
6
|
+
readonly method?: string;
|
|
7
|
+
readonly path?: RestPath;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The calls of `frames` a `calls()` filter asks for. */
|
|
11
|
+
export function filterCalls(
|
|
12
|
+
frames: readonly Frame[],
|
|
13
|
+
filter: CallFilter,
|
|
14
|
+
baseURL: string | undefined,
|
|
15
|
+
): RestCall[] {
|
|
16
|
+
const matcher =
|
|
17
|
+
filter.path === undefined ? undefined : compilePath(filter.path, baseURL);
|
|
18
|
+
const wanted = filter.method?.toUpperCase();
|
|
19
|
+
return assembleCalls(frames).filter(
|
|
20
|
+
(call) =>
|
|
21
|
+
(wanted === undefined || wanted === call.method) &&
|
|
22
|
+
(matcher === undefined || matcher.match(new URL(call.url)) !== undefined),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
3
25
|
|
|
4
26
|
/**
|
|
5
27
|
* Pair each recorded request with how it ended. One call per exchange,
|
|
@@ -25,6 +47,10 @@ export function assembleCalls(frames: readonly Frame[]): RestCall[] {
|
|
|
25
47
|
request,
|
|
26
48
|
response: end?.kind === "response" ? end : undefined,
|
|
27
49
|
status: typeof status === "number" ? status : undefined,
|
|
50
|
+
server:
|
|
51
|
+
typeof request.meta["server"] === "string"
|
|
52
|
+
? request.meta["server"]
|
|
53
|
+
: undefined,
|
|
28
54
|
});
|
|
29
55
|
}
|
|
30
56
|
return calls;
|
|
@@ -5,15 +5,18 @@ import {
|
|
|
5
5
|
type WirePlugin,
|
|
6
6
|
} from "../../../entities/plugin/index.js";
|
|
7
7
|
import { createArrivals } from "../../../shared/lib/async/index.js";
|
|
8
|
+
import { createSequence } from "../../../shared/lib/id/index.js";
|
|
8
9
|
import type {
|
|
9
10
|
RestApi,
|
|
10
11
|
RestMethod,
|
|
11
12
|
RestOptions,
|
|
12
13
|
RestReply,
|
|
13
14
|
RestRequest,
|
|
15
|
+
RestServer,
|
|
14
16
|
} from "../model/types.js";
|
|
15
|
-
import {
|
|
17
|
+
import { filterCalls } from "./calls.js";
|
|
16
18
|
import { compilePath } from "./path.js";
|
|
19
|
+
import { serveRest } from "./serve.js";
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
22
|
* REST and other plain HTTP: routes by method and path with `:params`, replies
|
|
@@ -27,6 +30,9 @@ export function rest(options: RestOptions = {}): WirePlugin<"rest", RestApi> {
|
|
|
27
30
|
const { name } = context;
|
|
28
31
|
/** The record rule being added, once: every context gets it. */
|
|
29
32
|
let recording: Promise<unknown> | undefined;
|
|
33
|
+
const servers = new Set<RestServer>();
|
|
34
|
+
// Under the plugin's own name: two instances never share an id.
|
|
35
|
+
const servedCorrelation = createSequence(`${name}-`);
|
|
30
36
|
|
|
31
37
|
const toOutcome = async (reply: RestReply): Promise<HttpOutcome> => {
|
|
32
38
|
switch (reply.kind) {
|
|
@@ -125,24 +131,34 @@ export function rest(options: RestOptions = {}): WirePlugin<"rest", RestApi> {
|
|
|
125
131
|
delete: verb("DELETE"),
|
|
126
132
|
async calls(filter = {}) {
|
|
127
133
|
await context.sync();
|
|
128
|
-
|
|
129
|
-
filter.path === undefined
|
|
130
|
-
? undefined
|
|
131
|
-
: compilePath(filter.path, options.baseURL);
|
|
132
|
-
const wanted = filter.method?.toUpperCase();
|
|
133
|
-
return assembleCalls(
|
|
134
|
+
return filterCalls(
|
|
134
135
|
context.timeline.frames({ protocol: name }),
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
(wanted === undefined || wanted === call.method) &&
|
|
138
|
-
(matcher === undefined ||
|
|
139
|
-
matcher.match(new URL(call.url)) !== undefined),
|
|
136
|
+
filter,
|
|
137
|
+
options.baseURL,
|
|
140
138
|
);
|
|
141
139
|
},
|
|
140
|
+
async serve(serveOptions = {}) {
|
|
141
|
+
const server = await serveRest(
|
|
142
|
+
{
|
|
143
|
+
protocol: name,
|
|
144
|
+
timeline: context.timeline,
|
|
145
|
+
clock: context.clock,
|
|
146
|
+
log: context.log,
|
|
147
|
+
correlation: servedCorrelation,
|
|
148
|
+
onClose: (closed) => servers.delete(closed),
|
|
149
|
+
},
|
|
150
|
+
serveOptions,
|
|
151
|
+
);
|
|
152
|
+
servers.add(server);
|
|
153
|
+
return server;
|
|
154
|
+
},
|
|
142
155
|
};
|
|
143
156
|
|
|
144
157
|
return {
|
|
145
158
|
api,
|
|
159
|
+
async dispose() {
|
|
160
|
+
await Promise.all([...servers].map((server) => server.close()));
|
|
161
|
+
},
|
|
146
162
|
async attach() {
|
|
147
163
|
if (options.record === undefined || options.record === false) return;
|
|
148
164
|
const base = compilePath(`${options.baseURL ?? ""}/**`, undefined);
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import type { FrameInit, Timeline } from "../../../entities/frame/index.js";
|
|
2
|
+
import {
|
|
3
|
+
type ServedRequest,
|
|
4
|
+
type ServedResponse,
|
|
5
|
+
serveHttp,
|
|
6
|
+
} from "../../../shared/api/http-server/index.js";
|
|
7
|
+
import {
|
|
8
|
+
type Arrivals,
|
|
9
|
+
createArrivals,
|
|
10
|
+
} from "../../../shared/lib/async/index.js";
|
|
11
|
+
import type { Clock } from "../../../shared/lib/clock/index.js";
|
|
12
|
+
import { WirestageError } from "../../../shared/lib/errors/index.js";
|
|
13
|
+
import type { ScopedLogger } from "../../../shared/lib/logger/index.js";
|
|
14
|
+
import type {
|
|
15
|
+
RestApi,
|
|
16
|
+
RestMethod,
|
|
17
|
+
RestReply,
|
|
18
|
+
RestRequest,
|
|
19
|
+
RestResolver,
|
|
20
|
+
RestServeOptions,
|
|
21
|
+
RestServer,
|
|
22
|
+
} from "../model/types.js";
|
|
23
|
+
import { filterCalls } from "./calls.js";
|
|
24
|
+
import { compilePath, type PathMatcher } from "./path.js";
|
|
25
|
+
|
|
26
|
+
export interface ServeDeps {
|
|
27
|
+
/** The protocol the server's frames are recorded under. */
|
|
28
|
+
readonly protocol: string;
|
|
29
|
+
readonly timeline: Timeline;
|
|
30
|
+
readonly clock: Clock;
|
|
31
|
+
readonly log: ScopedLogger;
|
|
32
|
+
readonly correlation: () => string;
|
|
33
|
+
/** Told when the server closes, so the plugin can forget it. */
|
|
34
|
+
readonly onClose: (server: RestServer) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Route {
|
|
38
|
+
readonly label: string;
|
|
39
|
+
readonly method: string | undefined;
|
|
40
|
+
readonly matcher: PathMatcher;
|
|
41
|
+
readonly resolver: RestResolver;
|
|
42
|
+
readonly match: ((request: RestRequest) => boolean) | undefined;
|
|
43
|
+
readonly requests: Arrivals<RestRequest>;
|
|
44
|
+
remaining: number | undefined;
|
|
45
|
+
hits: number;
|
|
46
|
+
active: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const TEXT = { "content-type": "text/plain; charset=utf-8" };
|
|
50
|
+
|
|
51
|
+
const failureOf = (error: unknown): string =>
|
|
52
|
+
error instanceof Error ? error.message : String(error);
|
|
53
|
+
|
|
54
|
+
/** A served request as a route sees it, with that route's params. */
|
|
55
|
+
function restRequest(
|
|
56
|
+
request: ServedRequest,
|
|
57
|
+
correlation: string,
|
|
58
|
+
params: Record<string, string>,
|
|
59
|
+
): RestRequest {
|
|
60
|
+
const text = () => new TextDecoder().decode(request.body);
|
|
61
|
+
return {
|
|
62
|
+
correlation,
|
|
63
|
+
method: request.method,
|
|
64
|
+
url: request.url,
|
|
65
|
+
headers: request.headers,
|
|
66
|
+
page: undefined,
|
|
67
|
+
context: "",
|
|
68
|
+
resourceType: "other",
|
|
69
|
+
body: () => request.body,
|
|
70
|
+
text,
|
|
71
|
+
json: <T>() => JSON.parse(text()) as T,
|
|
72
|
+
params,
|
|
73
|
+
query: request.url.searchParams,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A served fake: routes, newest first, as the browser's are; every exchange
|
|
79
|
+
* on the timeline, the client's request as `network` and the answer as
|
|
80
|
+
* `server`.
|
|
81
|
+
*/
|
|
82
|
+
export async function serveRest(
|
|
83
|
+
deps: ServeDeps,
|
|
84
|
+
options: RestServeOptions,
|
|
85
|
+
): Promise<RestServer> {
|
|
86
|
+
const name = options.name ?? `${deps.protocol} server`;
|
|
87
|
+
/** Routes, oldest first. */
|
|
88
|
+
const routes: Route[] = [];
|
|
89
|
+
let url = "";
|
|
90
|
+
|
|
91
|
+
const record = (
|
|
92
|
+
init: Omit<FrameInit, "protocol" | "endpoint">,
|
|
93
|
+
path: string,
|
|
94
|
+
) =>
|
|
95
|
+
deps.timeline.append({
|
|
96
|
+
protocol: deps.protocol,
|
|
97
|
+
endpoint: path,
|
|
98
|
+
...init,
|
|
99
|
+
meta: { ...init.meta, server: url },
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const retire = (route: Route) => {
|
|
103
|
+
if (!route.active) return;
|
|
104
|
+
route.active = false;
|
|
105
|
+
routes.splice(routes.indexOf(route), 1);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** Carry a reply out; `undefined` when it passes the request on. */
|
|
109
|
+
const answer = async (
|
|
110
|
+
reply: RestReply,
|
|
111
|
+
response: ServedResponse,
|
|
112
|
+
path: string,
|
|
113
|
+
correlation: string,
|
|
114
|
+
): Promise<"answered" | undefined> => {
|
|
115
|
+
switch (reply.kind) {
|
|
116
|
+
case "response": {
|
|
117
|
+
const body = reply.body ?? "";
|
|
118
|
+
// Recorded before it is sent: a client that reads its answer and
|
|
119
|
+
// asks for `calls()` at once must find it there.
|
|
120
|
+
record(
|
|
121
|
+
{
|
|
122
|
+
kind: "response",
|
|
123
|
+
direction: "in",
|
|
124
|
+
source: "server",
|
|
125
|
+
data: body,
|
|
126
|
+
meta: { status: reply.status, headers: reply.headers },
|
|
127
|
+
correlation,
|
|
128
|
+
},
|
|
129
|
+
path,
|
|
130
|
+
);
|
|
131
|
+
await response.send(reply.status, reply.headers, body);
|
|
132
|
+
return "answered";
|
|
133
|
+
}
|
|
134
|
+
case "passthrough":
|
|
135
|
+
return undefined;
|
|
136
|
+
case "abort":
|
|
137
|
+
record(
|
|
138
|
+
{
|
|
139
|
+
kind: "fault",
|
|
140
|
+
direction: "none",
|
|
141
|
+
source: "server",
|
|
142
|
+
meta: { reason: "abort", error: reply.error },
|
|
143
|
+
correlation,
|
|
144
|
+
},
|
|
145
|
+
path,
|
|
146
|
+
);
|
|
147
|
+
response.reset();
|
|
148
|
+
return "answered";
|
|
149
|
+
case "delay":
|
|
150
|
+
await deps.clock.sleep(reply.ms);
|
|
151
|
+
return answer(reply.reply, response, path, correlation);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/** A route's bug: logged, on the timeline, and the client gets a 500 saying so. */
|
|
156
|
+
const fail = async (
|
|
157
|
+
route: Route,
|
|
158
|
+
error: unknown,
|
|
159
|
+
response: ServedResponse,
|
|
160
|
+
path: string,
|
|
161
|
+
correlation: string,
|
|
162
|
+
) => {
|
|
163
|
+
deps.log.error("rule failed", { rule: route.label, server: name }, error);
|
|
164
|
+
record(
|
|
165
|
+
{
|
|
166
|
+
kind: "fault",
|
|
167
|
+
direction: "none",
|
|
168
|
+
source: "server",
|
|
169
|
+
meta: {
|
|
170
|
+
reason: "rule-error",
|
|
171
|
+
rule: route.label,
|
|
172
|
+
error: failureOf(error),
|
|
173
|
+
},
|
|
174
|
+
correlation,
|
|
175
|
+
},
|
|
176
|
+
path,
|
|
177
|
+
);
|
|
178
|
+
await response.send(
|
|
179
|
+
500,
|
|
180
|
+
TEXT,
|
|
181
|
+
`wirestage: rule "${route.label}" failed: ${failureOf(error)}`,
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const handle = async (request: ServedRequest, response: ServedResponse) => {
|
|
186
|
+
const correlation = deps.correlation();
|
|
187
|
+
const path = request.url.pathname;
|
|
188
|
+
record(
|
|
189
|
+
{
|
|
190
|
+
kind: "request",
|
|
191
|
+
direction: "out",
|
|
192
|
+
source: "network",
|
|
193
|
+
data: request.body,
|
|
194
|
+
meta: {
|
|
195
|
+
method: request.method,
|
|
196
|
+
url: request.url.href,
|
|
197
|
+
headers: request.headers,
|
|
198
|
+
},
|
|
199
|
+
correlation,
|
|
200
|
+
},
|
|
201
|
+
path,
|
|
202
|
+
);
|
|
203
|
+
// The chain as it stands now: a route added while this one waits on a
|
|
204
|
+
// resolver takes the next request, not this one.
|
|
205
|
+
for (const route of [...routes].reverse()) {
|
|
206
|
+
if (!route.active) continue;
|
|
207
|
+
if (route.method !== undefined && route.method !== request.method)
|
|
208
|
+
continue;
|
|
209
|
+
const params = route.matcher.match(request.url);
|
|
210
|
+
if (params === undefined) continue;
|
|
211
|
+
const view = restRequest(request, correlation, params);
|
|
212
|
+
try {
|
|
213
|
+
if (route.match !== undefined && !route.match(view)) continue;
|
|
214
|
+
// Claimed before the resolver runs, which may wait: two requests
|
|
215
|
+
// at once cannot both take a route's last turn.
|
|
216
|
+
route.hits += 1;
|
|
217
|
+
if (route.remaining !== undefined && --route.remaining === 0)
|
|
218
|
+
retire(route);
|
|
219
|
+
route.requests.push(view);
|
|
220
|
+
const reply =
|
|
221
|
+
typeof route.resolver === "function"
|
|
222
|
+
? await route.resolver(view)
|
|
223
|
+
: route.resolver;
|
|
224
|
+
if ((await answer(reply, response, path, correlation)) !== undefined)
|
|
225
|
+
return;
|
|
226
|
+
} catch (error) {
|
|
227
|
+
await fail(route, error, response, path, correlation);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
record(
|
|
232
|
+
{
|
|
233
|
+
kind: "response",
|
|
234
|
+
direction: "in",
|
|
235
|
+
source: "server",
|
|
236
|
+
data: "",
|
|
237
|
+
meta: { status: 404, headers: TEXT, unmatched: true },
|
|
238
|
+
correlation,
|
|
239
|
+
},
|
|
240
|
+
path,
|
|
241
|
+
);
|
|
242
|
+
await response.send(
|
|
243
|
+
404,
|
|
244
|
+
TEXT,
|
|
245
|
+
`wirestage: ${name} has no route for ${request.method} ${path}`,
|
|
246
|
+
);
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const listening = await serveHttp(
|
|
250
|
+
{
|
|
251
|
+
name,
|
|
252
|
+
...(options.host === undefined ? {} : { host: options.host }),
|
|
253
|
+
...(options.port === undefined ? {} : { port: options.port }),
|
|
254
|
+
},
|
|
255
|
+
handle,
|
|
256
|
+
);
|
|
257
|
+
url = listening.url;
|
|
258
|
+
|
|
259
|
+
const on: RestApi["on"] = async (
|
|
260
|
+
method,
|
|
261
|
+
path,
|
|
262
|
+
resolver,
|
|
263
|
+
routeOptions = {},
|
|
264
|
+
) => {
|
|
265
|
+
if (
|
|
266
|
+
routeOptions.times !== undefined &&
|
|
267
|
+
(!Number.isInteger(routeOptions.times) || routeOptions.times < 1)
|
|
268
|
+
)
|
|
269
|
+
throw new WirestageError(
|
|
270
|
+
"INVALID_ARGUMENT",
|
|
271
|
+
`route "${method} ${String(path)}": times must be a positive integer, got ${routeOptions.times}`,
|
|
272
|
+
);
|
|
273
|
+
const label = `${method} ${String(path)}`;
|
|
274
|
+
const route: Route = {
|
|
275
|
+
label,
|
|
276
|
+
method: method === "*" ? undefined : method,
|
|
277
|
+
matcher: compilePath(path, options.baseURL),
|
|
278
|
+
resolver,
|
|
279
|
+
match: routeOptions.match,
|
|
280
|
+
requests: createArrivals<RestRequest>({
|
|
281
|
+
clock: deps.clock,
|
|
282
|
+
label: `${label} request to ${name}`,
|
|
283
|
+
}),
|
|
284
|
+
remaining: routeOptions.times,
|
|
285
|
+
hits: 0,
|
|
286
|
+
active: true,
|
|
287
|
+
};
|
|
288
|
+
routes.push(route);
|
|
289
|
+
return {
|
|
290
|
+
get hits() {
|
|
291
|
+
return route.hits;
|
|
292
|
+
},
|
|
293
|
+
get requests() {
|
|
294
|
+
return route.requests.items;
|
|
295
|
+
},
|
|
296
|
+
next: (waitOptions) => route.requests.next(waitOptions),
|
|
297
|
+
async remove() {
|
|
298
|
+
retire(route);
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const verb =
|
|
304
|
+
(method: RestMethod): RestApi["get"] =>
|
|
305
|
+
(path, resolver, routeOptions) =>
|
|
306
|
+
on(method, path, resolver, routeOptions);
|
|
307
|
+
|
|
308
|
+
let closing: Promise<void> | undefined;
|
|
309
|
+
const server: RestServer = {
|
|
310
|
+
url,
|
|
311
|
+
port: listening.port,
|
|
312
|
+
on,
|
|
313
|
+
get: verb("GET"),
|
|
314
|
+
post: verb("POST"),
|
|
315
|
+
put: verb("PUT"),
|
|
316
|
+
patch: verb("PATCH"),
|
|
317
|
+
delete: verb("DELETE"),
|
|
318
|
+
async calls(filter = {}) {
|
|
319
|
+
return filterCalls(
|
|
320
|
+
deps.timeline.frames({
|
|
321
|
+
protocol: deps.protocol,
|
|
322
|
+
where: (frame) => frame.meta["server"] === url,
|
|
323
|
+
}),
|
|
324
|
+
filter,
|
|
325
|
+
options.baseURL,
|
|
326
|
+
);
|
|
327
|
+
},
|
|
328
|
+
close() {
|
|
329
|
+
closing ??= (async () => {
|
|
330
|
+
for (const route of [...routes]) retire(route);
|
|
331
|
+
deps.onClose(server);
|
|
332
|
+
await listening.close();
|
|
333
|
+
})();
|
|
334
|
+
return closing;
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
return server;
|
|
338
|
+
}
|
|
@@ -25,6 +25,11 @@ export interface RestOptions {
|
|
|
25
25
|
readonly record?: boolean | UrlMatcher;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* A request a route is deciding about. For a request a served fake received
|
|
30
|
+
* (`serve()`), `page` is undefined, `context` is empty and `resourceType` is
|
|
31
|
+
* `other`.
|
|
32
|
+
*/
|
|
28
33
|
export interface RestRequest extends HttpRequestView {
|
|
29
34
|
/** `:name` segments of the matched path. */
|
|
30
35
|
readonly params: Readonly<Record<string, string>>;
|
|
@@ -69,6 +74,38 @@ export interface RestCall {
|
|
|
69
74
|
readonly response: Frame | undefined;
|
|
70
75
|
/** The HTTP status; undefined for an aborted or unfinished request. */
|
|
71
76
|
readonly status: number | undefined;
|
|
77
|
+
/** The served fake that received it (its URL); undefined for a page's. */
|
|
78
|
+
readonly server: string | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface RestServeOptions {
|
|
82
|
+
/** 0, the default, picks a free port. A fixed port that is taken fails, naming the server. */
|
|
83
|
+
readonly port?: number;
|
|
84
|
+
/** Default 127.0.0.1. */
|
|
85
|
+
readonly host?: string;
|
|
86
|
+
/** Names the server in errors, 404s and logs. Default `<plugin> server`. */
|
|
87
|
+
readonly name?: string;
|
|
88
|
+
/** A path prefix the server's routes are relative to. */
|
|
89
|
+
readonly baseURL?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A fake HTTP backend for clients outside the browser — a service under
|
|
94
|
+
* test, an agent — routed and recorded like the page's own requests. A route
|
|
95
|
+
* that passes a request through hands it to the next older route; with none
|
|
96
|
+
* left, the server answers 404.
|
|
97
|
+
*/
|
|
98
|
+
export interface RestServer
|
|
99
|
+
extends Pick<RestApi, "on" | "get" | "post" | "put" | "patch" | "delete"> {
|
|
100
|
+
readonly url: string;
|
|
101
|
+
readonly port: number;
|
|
102
|
+
/** This server's calls, oldest first. */
|
|
103
|
+
calls(filter?: {
|
|
104
|
+
readonly method?: string;
|
|
105
|
+
readonly path?: RestPath;
|
|
106
|
+
}): Promise<RestCall[]>;
|
|
107
|
+
/** Stop accepting and drop open connections. The stage closes it too. */
|
|
108
|
+
close(): Promise<void>;
|
|
72
109
|
}
|
|
73
110
|
|
|
74
111
|
export type RestPath = string | RegExp;
|
|
@@ -105,9 +142,11 @@ export interface RestApi {
|
|
|
105
142
|
resolver: RestResolver,
|
|
106
143
|
options?: RestRouteOptions,
|
|
107
144
|
): Promise<RestRoute>;
|
|
108
|
-
/** Recorded REST exchanges, oldest first. */
|
|
145
|
+
/** Recorded REST exchanges, oldest first: the pages' and served fakes'. */
|
|
109
146
|
calls(filter?: {
|
|
110
147
|
readonly method?: string;
|
|
111
148
|
readonly path?: RestPath;
|
|
112
149
|
}): Promise<RestCall[]>;
|
|
150
|
+
/** Serve routes on a port of their own, for clients outside the browser. */
|
|
151
|
+
serve(options?: RestServeOptions): Promise<RestServer>;
|
|
113
152
|
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createServer,
|
|
3
|
+
type IncomingMessage,
|
|
4
|
+
type ServerResponse,
|
|
5
|
+
} from "node:http";
|
|
6
|
+
|
|
7
|
+
import { type Listening, type ListenOptions, listen } from "./listen.js";
|
|
8
|
+
|
|
9
|
+
/** A request a served fake received, read in full before it is handed on. */
|
|
10
|
+
export interface ServedRequest {
|
|
11
|
+
readonly method: string;
|
|
12
|
+
/** Absolute: the server's origin, then the path and query the client sent. */
|
|
13
|
+
readonly url: URL;
|
|
14
|
+
/** Lower-cased names; a repeated header's values joined with `, `. */
|
|
15
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
16
|
+
readonly body: Uint8Array;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The answer to a {@link ServedRequest}: one of the two, once. */
|
|
20
|
+
export interface ServedResponse {
|
|
21
|
+
/**
|
|
22
|
+
* Send a complete response. A header may carry several values separated
|
|
23
|
+
* by a newline (`set-cookie`). Resolves once it is written.
|
|
24
|
+
*/
|
|
25
|
+
send(
|
|
26
|
+
status: number,
|
|
27
|
+
headers: Readonly<Record<string, string>>,
|
|
28
|
+
body: Uint8Array | string,
|
|
29
|
+
): Promise<void>;
|
|
30
|
+
/** Drop the connection: no status, no body — a network failure. */
|
|
31
|
+
reset(): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type RequestHandler = (
|
|
35
|
+
request: ServedRequest,
|
|
36
|
+
response: ServedResponse,
|
|
37
|
+
) => void | Promise<void>;
|
|
38
|
+
|
|
39
|
+
const headersOf = (message: IncomingMessage): Record<string, string> =>
|
|
40
|
+
Object.fromEntries(
|
|
41
|
+
Object.entries(message.headersDistinct as Record<string, string[]>).map(
|
|
42
|
+
([name, values]) => [name, values.join(", ")],
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const readBody = (message: IncomingMessage): Promise<Uint8Array> =>
|
|
47
|
+
new Promise((resolve, reject) => {
|
|
48
|
+
const chunks: Buffer[] = [];
|
|
49
|
+
message.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
50
|
+
message.once("end", () => resolve(new Uint8Array(Buffer.concat(chunks))));
|
|
51
|
+
message.once("error", reject);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
function responderOf(response: ServerResponse): ServedResponse {
|
|
55
|
+
return {
|
|
56
|
+
send(status, headers, body) {
|
|
57
|
+
for (const [name, value] of Object.entries(headers))
|
|
58
|
+
response.setHeader(
|
|
59
|
+
name,
|
|
60
|
+
value.includes("\n") ? value.split("\n") : value,
|
|
61
|
+
);
|
|
62
|
+
response.statusCode = status;
|
|
63
|
+
return new Promise<void>((resolve) => {
|
|
64
|
+
// `close` too: a client that went away never sees `finish`.
|
|
65
|
+
response.once("close", () => resolve());
|
|
66
|
+
response.end(body);
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
reset() {
|
|
70
|
+
response.destroy();
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* An HTTP/1.1 server for clients outside the browser, handing each request,
|
|
77
|
+
* body read, to `handle`. The handler is the caller's to make safe; one that
|
|
78
|
+
* throws anyway gets a 500 rather than a client left waiting, and closing the
|
|
79
|
+
* server drops open connections at once (`listen`).
|
|
80
|
+
*/
|
|
81
|
+
export async function serveHttp(
|
|
82
|
+
options: Omit<ListenOptions, "protocol">,
|
|
83
|
+
handle: RequestHandler,
|
|
84
|
+
): Promise<Listening> {
|
|
85
|
+
const server = createServer((message, response) => {
|
|
86
|
+
const responder = responderOf(response);
|
|
87
|
+
readBody(message)
|
|
88
|
+
.then((body) =>
|
|
89
|
+
handle(
|
|
90
|
+
{
|
|
91
|
+
// Always set on a request an http.Server received.
|
|
92
|
+
method: message.method as string,
|
|
93
|
+
url: new URL(message.url as string, listening.url),
|
|
94
|
+
headers: headersOf(message),
|
|
95
|
+
body,
|
|
96
|
+
},
|
|
97
|
+
responder,
|
|
98
|
+
),
|
|
99
|
+
)
|
|
100
|
+
.catch((error: unknown) => {
|
|
101
|
+
if (response.headersSent) response.destroy();
|
|
102
|
+
else
|
|
103
|
+
void responder.send(
|
|
104
|
+
500,
|
|
105
|
+
{ "content-type": "text/plain; charset=utf-8" },
|
|
106
|
+
`wirestage: ${options.name} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
const listening = await listen(server, { ...options, protocol: "http" });
|
|
111
|
+
return listening;
|
|
112
|
+
}
|