Skip to content

Commit

Permalink
Moving next() into utility
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jul 3, 2018
1 parent 4399f51 commit f484303
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
27 changes: 27 additions & 0 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ function last (req, res, reject, err) {
}
}

function next (req, res, reject, middleware) {
const fn = err => process.nextTick(() => {
let obj = middleware.next();

if (obj.done === false) {
if (err !== void 0) {
while (obj.done === false && obj.value.length < 4) {
obj = middleware.next();
}

if (obj.done === false) {
obj.value(err, req, res, fn);
} else {
last(req, res, reject, err);
}
} else {
obj.value(req, res, fn);
}
} else {
last(req, res, reject, err);
}
});

return fn;
}

function params (req, pos = []) {
if (pos.length > 0) {
const uri = req.parsed.pathname.replace(regex.slashEnd, "").split("/");
Expand Down Expand Up @@ -193,6 +219,7 @@ module.exports = {
http2Send,
normalize,
last,
next,
params,
parse,
partial,
Expand Down
32 changes: 4 additions & 28 deletions lib/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require("path"),
methods = http.METHODS,
Base = require(path.join(__dirname, "base.js")),
regex = require(path.join(__dirname, "regex.js")),
{clone, each, http2Normalize, http2Send, normalize, params, parse, partial, pipeable, last, reduce, schemeless, wrapped, writeHead} = require(path.join(__dirname, "utility.js")),
{clone, each, http2Normalize, http2Send, normalize, next, params, parse, partial, pipeable, last, reduce, schemeless, wrapped, writeHead} = require(path.join(__dirname, "utility.js")),
all = "all",
delimiter = ":";

Expand Down Expand Up @@ -224,30 +224,7 @@ class Woodland extends Base {

return new Promise((resolve, reject) => {
let method = regex.isHead.test(req.method) ? "GET" : req.method,
mregex = new RegExp(method),
middleware, result;

const next = err => process.nextTick(() => {
let obj = middleware.next();

if (obj.done === false) {
if (err !== void 0) {
while (obj.done === false && obj.value.length < 4) {
obj = middleware.next();
}

if (obj.done === false) {
obj.value(err, req, res, next);
} else {
last(req, res, reject, err);
}
} else {
obj.value(req, res, next);
}
} else {
last(req, res, reject, err);
}
});
mregex = new RegExp(method);

res.on("finish", resolve);
res.on("close", () => {
Expand All @@ -263,14 +240,13 @@ class Woodland extends Base {
}

if (mregex.test(req.allow)) {
result = this.routes(req.parsed.pathname, method);
const result = this.routes(req.parsed.pathname, method);

if (result.params !== void 0) {
params(req, result.pos);
}

middleware = result.middleware[Symbol.iterator]();
next();
next(req, res, reject, result.middleware[Symbol.iterator]())();
} else {
last(req, res, reject);
}
Expand Down
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": "woodland",
"version": "7.1.3",
"version": "7.1.4",
"description": "Lightweight HTTP/HTTP2 router with automatic `Allow` & `CORS` headers",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f484303

Please sign in to comment.