Skip to content

Commit

Permalink
Removing send event & replacing with `onsend(req, res, body, status…
Browse files Browse the repository at this point in the history
…, headers)` to customize the response by returning `[body, status, headers]` - event cannot mutate `body` if the variable type changes
  • Loading branch information
avoidwork committed Jan 3, 2021
1 parent 91c101e commit e086815
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Returns an `Array` or `Object` of routes for the specified method.
##### log (msg = "", level = "debug")
Logs to `stdout` or `stderr` depending on the `level`, & what the minimum log level is set to.

##### onsend (req, res, body, status, headers)
**Override** to customize response `body`, `status`, or `headers`. Must return `[body, status, headers]`!

##### route (req, res)
Function for `http.createServer()` or `https.createServer()`.

Expand Down Expand Up @@ -131,9 +134,6 @@ Executes after the response has been sent.
##### finish (req, res)
Executes after the response has been sent.

##### send (req, res, body, status, headers)
Executes before the response has been sent; arguments are by reference such that they can be mutated.

## Helpers
`req` & `res` are decorated with helper functions to simplify responding.

Expand Down
27 changes: 13 additions & 14 deletions lib/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,24 @@ class Woodland extends EventEmitter {
res.header = res.setHeader;
res.json = (arg, status = 200, headers = {"content-type": "application/json; charset=utf-8"}) => res.send(JSON.stringify(arg), status, headers);
res.redirect = (uri, perm = true) => res.send("", perm ? 301 : 302, {"location": uri});
res.send = (body, status = 200, headers = {}) => {
res.send = (body = "", status = 200, headers = {}) => {
if (res.headersSent === false) {
const ev = "send";
let output = body;

if (this.listenerCount(ev) > 0) {
this.emit(ev, req, res, output, status, headers);
}
[body, status, headers] = this.onsend(req, res, body, status, headers);

if (this.time) {
headers["x-response-time"] = `${ms(req.precise.stop().diff(), this.digit)}`;
}

if (pipeable(req.method, output)) {
if (pipeable(req.method, body)) {
writeHead(res, status, headers);
output.on("error", () => void 0).pipe(res);
body.on("error", () => void 0).pipe(res);
} else {
if (typeof output !== "string" && "toString" in output) {
output = output.toString();
if (typeof body !== "string" && "toString" in body) {
body = body.toString();
}

if (req.headers.range !== void 0) {
const buffered = Buffer.from(output);
const buffered = Buffer.from(body);

partial(req, res, buffered, status, headers);

Expand All @@ -161,11 +156,11 @@ class Woodland extends EventEmitter {
const cl = "content-length";

if (res.getHeader(cl) === void 0) {
res.header(cl, Buffer.byteLength(output));
res.header(cl, Buffer.byteLength(body));
}

writeHead(res, status, headers);
res.end(output, this.charset);
res.end(body, this.charset);
}
}

Expand Down Expand Up @@ -296,6 +291,10 @@ class Woodland extends EventEmitter {
return this;
}

onsend (req, res, body, status, headers) {
return [body, status, headers];
}

options (...args) {
return this.use(...args, "OPTIONS");
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "woodland",
"version": "15.1.12",
"version": "16.0.0",
"description": "Lightweight HTTP router with automatic headers",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ function always (req, res, next) {
}

router.on("connect", (req, res) => res.header("x-onconnect", "true"));
router.on("send", (req, res, body, status, headers) => {
router.onsend = (req, res, body, status, headers) => {
headers["x-by-reference"] = "true";
});

return [body, status, headers];
};
router.on("finish", () => void 0);
router.always("/.*", always).ignore(always);
router.use("/", (req, res) => res.send(req.method !== "OPTIONS" ? "Hello World!" : ""));
Expand Down

0 comments on commit e086815

Please sign in to comment.