Skip to content

Commit

Permalink
Added route cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkumardusad committed Mar 22, 2023
1 parent 358b919 commit 79a5f09
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 120 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@routejs/router",
"version": "2.1.5",
"version": "2.1.6",
"description": "Fast and lightweight http routing engine for nodejs",
"main": "index.mjs",
"type": "module",
Expand Down Expand Up @@ -50,5 +50,8 @@
"index.cjs",
"index.mjs",
"index.d.ts"
]
],
"dependencies": {
"@opensnip/lrujs": "^1.0.0"
}
}
6 changes: 2 additions & 4 deletions src/methods.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports.use = function use(...callbacks) {
if (typeof callbacks[0] === "string" || callbacks[0] instanceof String) {
if (callbacks.length < 2) {
throw new TypeError(
"Error: use function callback accepts function or router as an argument"
"use function callback accepts function or router as an argument"
);
}
return mergeRoute({
Expand All @@ -28,9 +28,7 @@ module.exports.all = function all(path, ...callbacks) {

module.exports.domain = function domain(host, routes) {
if (!(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: domain host accepts only string as an argument"
);
throw new TypeError("domain host accepts only string as an argument");
}

if (typeof routes === "function") {
Expand Down
6 changes: 2 additions & 4 deletions src/methods.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function use(...callbacks) {
if (typeof callbacks[0] === "string" || callbacks[0] instanceof String) {
if (callbacks.length < 2) {
throw new TypeError(
"Error: use function callback accepts function or router as an argument"
"use function callback accepts function or router as an argument"
);
}
return mergeRoute({
Expand All @@ -28,9 +28,7 @@ export function all(path, ...callbacks) {

export function domain(host, routes) {
if (!(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: domain host accepts only string as an argument"
);
throw new TypeError("domain host accepts only string as an argument");
}

if (typeof routes === "function") {
Expand Down
40 changes: 15 additions & 25 deletions src/route.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ module.exports = class Route {

constructor({ host, method, path, name, group, callbacks, caseSensitive }) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: route host accepts only string as an argument"
);
throw new TypeError("route host accepts only string as an argument");
}

if (method) {
if (Array.isArray(method)) {
method = method.map((e) => {
if (!(typeof e === "string" || e instanceof String)) {
throw new TypeError(
"Error: route method accepts only string or array of string as an argument"
"route method accepts only string or array of string as an argument"
);
}
return e.toUpperCase();
Expand All @@ -32,20 +30,18 @@ module.exports = class Route {
method = method.toUpperCase();
} else {
throw new TypeError(
"Error: route method accepts only string or array of string as an argument"
"route method accepts only string or array of string as an argument"
);
}
}

if (path && !(typeof path === "string" || path instanceof String)) {
throw new TypeError(
"Error: route path accepts only string as an argument"
);
throw new TypeError("route path accepts only string as an argument");
}

if (Array.isArray(callbacks) === false && typeof callbacks !== "function") {
throw new TypeError(
"Error: route callback accepts only function as an argument"
"route callback accepts only function as an argument"
);
}

Expand All @@ -70,7 +66,7 @@ module.exports = class Route {
? callbacks.map((callback) => {
if (typeof callback !== "function") {
throw new TypeError(
"Error: " +
"" +
(path ? "route" : "middleware") +
" callback accepts only function as an argument"
);
Expand All @@ -87,29 +83,23 @@ module.exports = class Route {

match({ host, method, path }) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: request host accepts only string as an argument"
);
throw new TypeError("request host accepts only string as an argument");
}

if (!method) {
throw new TypeError("Error: request method is required");
throw new TypeError("request method is required");
}

if (!(typeof method === "string" || method instanceof String)) {
throw new TypeError(
"Error: request method accepts only string as an argument"
);
throw new TypeError("request method accepts only string as an argument");
}

if (!path) {
throw new TypeError("Error: request path is required");
throw new TypeError("request path is required");
}

if (!(typeof path === "string" || path instanceof String)) {
throw new TypeError(
"Error: request path accepts only string as an argument"
);
throw new TypeError("request path accepts only string as an argument");
}

if (this.pathRegexp === null) {
Expand Down Expand Up @@ -174,7 +164,7 @@ module.exports = class Route {
}
return { regexp, params };
} catch (err) {
throw new TypeError("Error: " + host + " invalid regular expression");
throw new TypeError("" + host + " invalid regular expression");
}
}

Expand All @@ -196,7 +186,7 @@ module.exports = class Route {
}
return { regexp, params };
} catch (err) {
throw new TypeError("Error: " + path + " invalid regular expression");
throw new TypeError("" + path + " invalid regular expression");
}
}

Expand All @@ -218,7 +208,7 @@ module.exports = class Route {
}
return { regexp, params };
} catch (err) {
throw new TypeError("Error: " + path + " invalid regular expression");
throw new TypeError("" + path + " invalid regular expression");
}
}

Expand Down Expand Up @@ -256,7 +246,7 @@ module.exports = class Route {
return { regexp: regexp.join("\\" + delimiter), params: params };
} catch (err) {
console.log(err);
throw new TypeError("Error: " + path + " invalid regular expression");
throw new TypeError("" + path + " invalid regular expression");
}
}
};
40 changes: 15 additions & 25 deletions src/route.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ export default class Route {

constructor({ host, method, path, name, group, callbacks, caseSensitive }) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: route host accepts only string as an argument"
);
throw new TypeError("route host accepts only string as an argument");
}

if (method) {
if (Array.isArray(method)) {
method = method.map((e) => {
if (!(typeof e === "string" || e instanceof String)) {
throw new TypeError(
"Error: route method accepts only string or array of string as an argument"
"route method accepts only string or array of string as an argument"
);
}
return e.toUpperCase();
Expand All @@ -32,20 +30,18 @@ export default class Route {
method = method.toUpperCase();
} else {
throw new TypeError(
"Error: route method accepts only string or array of string as an argument"
"route method accepts only string or array of string as an argument"
);
}
}

if (path && !(typeof path === "string" || path instanceof String)) {
throw new TypeError(
"Error: route path accepts only string as an argument"
);
throw new TypeError("route path accepts only string as an argument");
}

if (Array.isArray(callbacks) === false && typeof callbacks !== "function") {
throw new TypeError(
"Error: route callback accepts only function as an argument"
"route callback accepts only function as an argument"
);
}

Expand All @@ -70,7 +66,7 @@ export default class Route {
? callbacks.map((callback) => {
if (typeof callback !== "function") {
throw new TypeError(
"Error: " +
"" +
(path ? "route" : "middleware") +
" callback accepts only function as an argument"
);
Expand All @@ -87,29 +83,23 @@ export default class Route {

match({ host, method, path }) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: request host accepts only string as an argument"
);
throw new TypeError("request host accepts only string as an argument");
}

if (!method) {
throw new TypeError("Error: request method is required");
throw new TypeError("request method is required");
}

if (!(typeof method === "string" || method instanceof String)) {
throw new TypeError(
"Error: request method accepts only string as an argument"
);
throw new TypeError("request method accepts only string as an argument");
}

if (!path) {
throw new TypeError("Error: request path is required");
throw new TypeError("request path is required");
}

if (!(typeof path === "string" || path instanceof String)) {
throw new TypeError(
"Error: request path accepts only string as an argument"
);
throw new TypeError("request path accepts only string as an argument");
}

if (this.pathRegexp === null) {
Expand Down Expand Up @@ -174,7 +164,7 @@ export default class Route {
}
return { regexp, params };
} catch (err) {
throw new TypeError("Error: " + host + " invalid regular expression");
throw new TypeError("" + host + " invalid regular expression");
}
}

Expand All @@ -196,7 +186,7 @@ export default class Route {
}
return { regexp, params };
} catch (err) {
throw new TypeError("Error: " + path + " invalid regular expression");
throw new TypeError("" + path + " invalid regular expression");
}
}

Expand All @@ -218,7 +208,7 @@ export default class Route {
}
return { regexp, params };
} catch (err) {
throw new TypeError("Error: " + path + " invalid regular expression");
throw new TypeError("" + path + " invalid regular expression");
}
}

Expand Down Expand Up @@ -256,7 +246,7 @@ export default class Route {
return { regexp: regexp.join("\\" + delimiter), params: params };
} catch (err) {
console.log(err);
throw new TypeError("Error: " + path + " invalid regular expression");
throw new TypeError("" + path + " invalid regular expression");
}
}
}
Loading

0 comments on commit 79a5f09

Please sign in to comment.