MCPcopy
hub / github.com/inkeep/open-knowledge / rawRequest

Function rawRequest

packages/cli/src/commands/ui-proxy.test.ts:12–45  ·  view source on GitHub ↗

Send a request with arbitrary Host / Origin headers. We can't use `fetch` * because some runtimes silently rewrite Host.

(opts: {
  port: number;
  path: string;
  method?: string;
  host?: string;
  origin?: string;
})

Source from the content-addressed store, hash-verified

10/** Send a request with arbitrary Host / Origin headers. We can't use `fetch`
11 * because some runtimes silently rewrite Host. */
12async function rawRequest(opts: {
13 port: number;
14 path: string;
15 method?: string;
16 host?: string;
17 origin?: string;
18}): Promise<{ status: number; body: string }> {
19 return new Promise((done, fail) => {
20 const req = httpRequest(
21 {
22 host: '127.0.0.1',
23 port: opts.port,
24 path: opts.path,
25 method: opts.method ?? 'GET',
26 headers: {
27 host: opts.host ?? `127.0.0.1:${opts.port}`,
28 ...(opts.origin !== undefined ? { origin: opts.origin } : {}),
29 },
30 },
31 (res: IncomingMessage) => {
32 const chunks: Buffer[] = [];
33 res.on('data', (c) => chunks.push(c as Buffer));
34 res.on('end', () => {
35 done({
36 status: res.statusCode ?? 0,
37 body: Buffer.concat(chunks).toString('utf-8'),
38 });
39 });
40 },
41 );
42 req.on('error', fail);
43 req.end();
44 });
45}
46
47type UpstreamHandle = { httpServer: HttpServer; port: number; close: () => Promise<void> };
48

Callers 1

ui-proxy.test.tsFile · 0.70

Calls 4

onMethod · 0.65
endMethod · 0.65
doneFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected