Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Dec 6, 2018
1 parent 890a818 commit c634fab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/inject",
"version": "1.0.2",
"version": "1.0.3",
"description": "Fake HTTP injection library",
"author": "Ardalan Amini <ardalanamini22@gmail.com> [https://github.com/ardalanamini]",
"license": "MIT",
Expand Down
24 changes: 11 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const inject = (
const uri = Url.parse(opts.url);
req.url = uri.path;

req.headers = opts.headers;
req.headers = opts.headers || {};

req.headers["user-agent"] = req.headers["user-agent"] || `foxify-inject/${version}`;

Expand All @@ -100,14 +100,16 @@ const inject = (
};
req.headers.host = req.headers.host || hostHeaderFromUri() || "localhost:80";

req.connection = {
remoteAddress: opts.remoteAddress,
} as any;
(req.connection as any).remoteAddress = opts.remoteAddress;
// req.connection = {
// remoteAddress: opts.remoteAddress,
// } as any;

req._read = function _read() {
setImmediate(() => {
if ((this as any)._inject.isDone) {
this.push(null);

return;
}

Expand All @@ -117,7 +119,7 @@ const inject = (

(this as any)._inject.isDone = true;

this.emit("close");
// this.emit("close");

this.push(null);
});
Expand Down Expand Up @@ -163,7 +165,7 @@ const inject = (
res.once("error", callback);
res.connection.once("error", callback);

const cb = () => callback(null, {
res.once("finish", () => callback(null, {
body: Buffer.concat((res as any)._inject.bodyChinks).toString(),
headers: res.getHeaders(),
raw: {
Expand All @@ -172,9 +174,7 @@ const inject = (
},
statusCode: res.statusCode,
statusMessage: res.statusMessage,
});

res.once("finish", cb);
}));

return dispatch(req, res) as any;
}
Expand All @@ -183,7 +183,7 @@ const inject = (
res.once("error", reject);
res.connection.once("error", reject);

const cb = () => resolve({
res.once("finish", () => resolve({
body: Buffer.concat((res as any)._inject.bodyChinks).toString(),
headers: res.getHeaders(),
raw: {
Expand All @@ -192,9 +192,7 @@ const inject = (
},
statusCode: res.statusCode,
statusMessage: res.statusMessage,
});

res.once("finish", cb);
}));

dispatch(req, res);
});
Expand Down

0 comments on commit c634fab

Please sign in to comment.