Skip to content

Commit 6320179

Browse files
authored
Merge pull request mediaurl#30 from watchedcom/fetch-agent
Fetch agent
2 parents 2dd21d0 + 03ef391 commit 6320179

File tree

11 files changed

+512
-4873
lines changed

11 files changed

+512
-4873
lines changed

packages/example/package-lock.json

Lines changed: 176 additions & 1301 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/i18n/package-lock.json

Lines changed: 32 additions & 1751 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/puppeteer/package-lock.json

Lines changed: 24 additions & 1803 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sdk/package-lock.json

Lines changed: 18 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@types/express": "^4.17.3",
1313
"@watchedcom/schema": "^0.34.1",
14+
"agent-base": "^6.0.1",
1415
"body-parser": "^1.19.0",
1516
"debug": "^4.1.1",
1617
"dotenv": "^8.2.0",

packages/sdk/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as program from "commander";
1+
import program from "commander";
22
import "dotenv/config";
33
import { BasicAddonClass } from "./addons";
44
import { createEngine } from "./engine";

packages/sdk/src/express-server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { TranslatedText } from "@watchedcom/schema";
2-
import * as bodyParser from "body-parser";
3-
import * as express from "express";
2+
import bodyParser from "body-parser";
3+
import express from "express";
44
import "express-async-errors";
5-
import * as morgan from "morgan";
6-
import * as path from "path";
5+
import morgan from "morgan";
6+
import path from "path";
77
import "pug";
88
import { BasicAddonClass } from "./addons";
99
import { errorHandler } from "./error-handler";

packages/sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export {
1818
replayRecordFile,
1919
} from "./utils/request-recorder";
2020
export { translateDeep } from "./utils/translate-deep";
21+
export { FetchAgent } from "./utils/fetch-agent";

packages/sdk/src/utils/fetch-agent.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Agent, ClientRequest, RequestOptions } from "agent-base";
2+
import * as net from "net";
3+
import { ActionHandlerContext } from "../";
4+
5+
/**
6+
* Experimental: can be used basically for GET or POST (with JSON payload)
7+
* More complex requests can cause issues
8+
*/
9+
export class FetchAgent extends Agent {
10+
constructor(private fetchFn: ActionHandlerContext["fetch"]) {
11+
super();
12+
}
13+
14+
async callback(
15+
req: ClientRequest,
16+
opts: RequestOptions
17+
): Promise<net.Socket> {
18+
const fakeSocket = new net.Socket({ writable: false });
19+
fakeSocket.readable = true;
20+
21+
const resp = await this.fetchFn((<any>opts).href, {
22+
method: opts.method || "GET",
23+
body: (<any>opts).body,
24+
headers: (opts.headers || {}) as { [key: string]: string },
25+
});
26+
27+
const headers = {
28+
...resp.headers.raw(),
29+
"transfer-encoding": ["identity"],
30+
"content-length": [undefined],
31+
};
32+
33+
const responseHeadersLines = Object.keys(headers)
34+
.filter((key) => headers[key][0])
35+
.map((key) => {
36+
return `${key}: ${headers[key][0]}`;
37+
});
38+
39+
req.once("socket", async (s) => {
40+
s.resume();
41+
42+
const headersPayload =
43+
[`HTTP/1.1 ${resp.status}`, ...responseHeadersLines].join("\n") +
44+
"\n\n";
45+
46+
const text = await resp.text();
47+
48+
fakeSocket.push(headersPayload);
49+
fakeSocket.push(Buffer.from(text));
50+
fakeSocket.push(null);
51+
});
52+
53+
return fakeSocket;
54+
}
55+
}

packages/sdk/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"esModuleInterop": true
56
},
67
"include": ["src/index.ts", "src/scripts/**/*.ts"]
78
}

0 commit comments

Comments
 (0)