diesel-core 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diesel-core",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "main": "src/server.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/src/ctx.js CHANGED
@@ -11,11 +11,10 @@ export default function createCtx(req, url) {
11
11
  req,
12
12
  url,
13
13
  next: () => {},
14
-
14
+ ///////
15
15
  async body() {
16
16
  if (!parsedBody) {
17
17
  parsedBody = await parseBody(req)
18
- return parsedBody;
19
18
  }
20
19
  return parsedBody;
21
20
  },
@@ -83,9 +82,9 @@ export default function createCtx(req, url) {
83
82
  });
84
83
  },
85
84
 
86
- getParams(props) {
85
+ async getParams(props) {
87
86
  if (!parsedParams) {
88
- parsedParams = extractDynamicParams(req.routePattern, url.pathname);
87
+ parsedParams = await extractDynamicParams(req.routePattern, url.pathname);
89
88
  }
90
89
  return props ? parsedParams[props] : parsedParams;
91
90
  },
@@ -123,16 +122,16 @@ export default function createCtx(req, url) {
123
122
  headers["Set-Cookie"] = cookieString;
124
123
  }
125
124
  },
126
- getCookie(cookieName) {
125
+ async getCookie(cookieName) {
127
126
  if (!parsedCookie) {
128
- parsedCookie = parseCookie(req.headers.get("cookie"));
127
+ parsedCookie = await parseCookie(req.headers.get("cookie"));
129
128
  }
130
129
  return cookieName ? parsedCookie[cookieName] : parsedCookie;
131
130
  },
132
131
  };
133
132
  }
134
133
 
135
- function parseCookie(header) {
134
+ async function parseCookie(header) {
136
135
  const cookies = {};
137
136
  if (!header) return cookies;
138
137
 
@@ -144,7 +143,7 @@ function parseCookie(header) {
144
143
  return cookies;
145
144
  }
146
145
 
147
- const extractDynamicParams = (routePattern, path) => {
146
+ async function extractDynamicParams (routePattern, path) {
148
147
  const object = {};
149
148
  const routeSegments = routePattern.split("/");
150
149
  const [pathWithoutQuery] = path.split("?"); // Ignore the query string in the path
@@ -10,7 +10,6 @@ export default async function handleRequest(req, url, diesel) {
10
10
  // Early return if route or method is not found
11
11
  if (!routeHandler || !routeHandler.handler) return responseNotFound(pathname);
12
12
  if (routeHandler.method !== method) return responseMethodNotAllowed();
13
-
14
13
  // If the route is dynamic, we only set routePattern if necessary
15
14
  if (routeHandler.isDynamic) req.routePattern = routeHandler.path;
16
15
 
@@ -19,7 +18,7 @@ export default async function handleRequest(req, url, diesel) {
19
18
  if (diesel.hasMiddleware) {
20
19
  const middlewares = [
21
20
  ...diesel.globalMiddlewares,
22
- ...(diesel.middlewares.get(pathname) || [])
21
+ ...diesel.middlewares.get(pathname) || []
23
22
  ];
24
23
 
25
24
  const middlewareResult = await executeMiddleware(middlewares, ctx);
package/src/router.js CHANGED
@@ -1,53 +1,43 @@
1
1
  import diesel from "./server";
2
2
 
3
- class Router extends diesel{
4
- constructor(){
5
- super()
6
- }
7
- #addRoute(method,path,handlers){
8
-
3
+ class Router extends diesel {
4
+ constructor() {
5
+ super();
6
+ }
7
+ #addRoute(method, path, handlers) {
8
+ if (!this.trie.root.subMiddlewares.has(path)) {
9
+ this.trie.root.subMiddlewares.set(path,[])
10
+ }
9
11
  const middlewareHandlers = handlers.slice(0, -1);
10
12
 
11
- if (!this.middlewares.has(path)) {
12
- this.middlewares.set(path,[])
13
- }
14
- if (path === '/') {
15
- middlewareHandlers.forEach(midlleware => {
16
- if(!this.globalMiddlewares.includes(midlleware)){
17
- this.globalMiddlewares.push(midlleware)
18
- }
19
- })
20
- } else {
21
- if (!this.middlewares.get(path).includes(...middlewareHandlers)) {
22
- this.middlewares.get(path).push(...middlewareHandlers);
23
- }
24
- }
25
-
26
- const handler = handlers[handlers.length-1]
27
- this.trie.insert(path,{handler,method})
28
-
13
+ if (!this.trie.root.subMiddlewares.get(path).includes(...middlewareHandlers)) {
14
+ this.trie.root.subMiddlewares.get(path).push(...middlewareHandlers)
29
15
  }
30
- get(path,...handlers){
31
- return this.#addRoute("GET",path,handlers)
32
- }
33
16
 
34
- post(path,...handlers){
35
- return this.#addRoute("POST",path,handlers)
36
- }
17
+ const handler = handlers[handlers.length - 1];
18
+ this.trie.insert(path, { handler, method });
19
+ }
20
+ get(path, ...handlers) {
21
+ return this.#addRoute("GET", path, handlers);
22
+ }
37
23
 
38
- put(path,...handlers){
39
- return this.#addRoute("PUT",path,handlers)
40
- }
24
+ post(path, ...handlers) {
25
+ return this.#addRoute("POST", path, handlers);
26
+ }
41
27
 
42
- patch(path,...handlers){
43
- if (handlers.length>0) {
44
- return this.#addRoute("PATCH",path,handlers)
28
+ put(path, ...handlers) {
29
+ return this.#addRoute("PUT", path, handlers);
30
+ }
31
+
32
+ patch(path, ...handlers) {
33
+ if (handlers.length > 0) {
34
+ return this.#addRoute("PATCH", path, handlers);
45
35
  }
46
- }
36
+ }
47
37
 
48
- delete(path,...handlers){
49
- return this.#addRoute("DELETE",path,handlers)
50
- }
38
+ delete(path, ...handlers) {
39
+ return this.#addRoute("DELETE", path, handlers);
40
+ }
51
41
  }
52
42
 
53
- export default Router
43
+ export default Router;
package/src/server.js CHANGED
@@ -27,9 +27,13 @@ class diesel {
27
27
  this.compile()
28
28
  const server = serve({
29
29
  port,
30
- fetch: (req) => {
30
+ fetch: async (req) => {
31
31
  const url = new URL(req.url)
32
- return handleRequest(req,url,this)
32
+ try {
33
+ return await handleRequest(req,url,this)
34
+ } catch (error) {
35
+ return new Response('Internal Server Error', { status: 500 });
36
+ }
33
37
  },
34
38
  onClose() {
35
39
  console.log("Server is shutting down...");
@@ -44,6 +48,7 @@ class diesel {
44
48
 
45
49
  register(pathPrefix,handlerInstance){
46
50
  const routeEntries = Object.entries(handlerInstance.trie.root.children);
51
+ // console.log(handlerInstance.trie.root);
47
52
  handlerInstance.trie.root.subMiddlewares.forEach((middleware,path)=>{
48
53
  if (!this.middlewares.has(pathPrefix+path)) {
49
54
  this.middlewares.set(pathPrefix+path, []);
package/src/trie.js CHANGED
@@ -98,7 +98,13 @@ class TrieNode {
98
98
  }
99
99
 
100
100
  // Fallback if method is not found
101
- return null;
101
+ return {
102
+ path: node.path,
103
+ handler: node.handler,
104
+ isDynamic: node.isDynamic,
105
+ pattern: node.pattern,
106
+ method: node.method[routeMethodIndex]
107
+ };
102
108
  }
103
109
 
104
110