Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class Client {

delete data.query;

const body = JSON.stringify(data.body);
const body = JSON.stringify({ body: data.body, rawdata: data.rawdata });
delete data.body;
delete data.rawdata;

const headers: { [key: string]: any } = {};

Expand All @@ -85,6 +86,7 @@ class Client {
const response = await this.#fetch(url.format(target), {
method: "POST",
mode: data["sec-fetch-mode"],
// Take advantage of the `rawData` field to send the original request body in order to be able to verify the signature
body,
headers,
dispatcher: proxyAgent,
Expand Down
14 changes: 8 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("client", () => {
});
});

function getPayload(request: IncomingMessage) {
function getPayload(request: IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let body = "";
request.on("error", reject);
Expand Down Expand Up @@ -82,12 +82,12 @@ describe("client", () => {
expect(req.method).toBe("POST");
expect(req.url).toBe("/");

const body = await getPayload(req);
const reqBody = JSON.parse(await getPayload(req));

expect(body).toBe(JSON.stringify({ hello: "world" }));
expect(reqBody.body).toEqual({ hello: "world" });

res.writeHead(200, { "content-type": "application/json" });
res.end(body);
res.end(JSON.stringify(reqBody));

++callCount;

Expand Down Expand Up @@ -132,18 +132,20 @@ describe("client", () => {
client.start();

await readyPromise.promise;
const payload = { hello: "world" };
const rawData = JSON.stringify(payload);

await fetch(target + "/", {
method: "POST",
body: JSON.stringify({ hello: "world" }),
body: JSON.stringify({ body: payload, rawdata: rawData }),
headers: {
"content-type": "application/json",
},
});

await fetch(source, {
method: "POST",
body: JSON.stringify({ hello: "world" }),
body: JSON.stringify(payload),
headers: {
"content-type": "application/json",
},
Expand Down