diesel-core 0.0.3 → 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 +1 -1
- package/src/ctx.js +7 -8
- package/src/handleRequest.js +1 -2
- package/src/server.js +6 -2
- package/src/trie.js +7 -1
package/package.json
CHANGED
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
|
-
|
|
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
|
package/src/handleRequest.js
CHANGED
|
@@ -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
|
-
...
|
|
21
|
+
...diesel.middlewares.get(pathname) || []
|
|
23
22
|
];
|
|
24
23
|
|
|
25
24
|
const middlewareResult = await executeMiddleware(middlewares, ctx);
|
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
|
-
|
|
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...");
|
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
|
|
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
|
|