diesel-core 1.7.0 → 1.7.1
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/dist/request_pipeline.js +20 -42
- package/dist/src/main.js +38 -31
- package/dist/src/request_pipeline.js +61 -54
- package/package.json +1 -1
package/dist/request_pipeline.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Context } from "./ctx";
|
|
2
2
|
import { executeBunMiddlewares, generateErrorResponse, handleRouteNotFound, runFilter, runHooks } from "./utils/request.util";
|
|
3
|
+
import { isPromise } from "./utils/promise";
|
|
3
4
|
function extractBody(fn) {
|
|
4
5
|
const src = fn.toString().trim();
|
|
5
6
|
// Arrow function without braces: () => new Response("...")
|
|
@@ -95,55 +96,25 @@ const pushMiddlewares = (pipeline, globalMiddlewares) => {
|
|
|
95
96
|
};
|
|
96
97
|
export const buildRequestPipeline = (diesel) => {
|
|
97
98
|
const pipeline = [];
|
|
98
|
-
const globalMiddlewares = diesel.globalMiddlewares || [];
|
|
99
|
-
// const onRequestHooks = config?.hasOnReqHook ? diesel.hooks.onRequest : [] as any;
|
|
100
99
|
const PreHandlerHook = (diesel === null || diesel === void 0 ? void 0 : diesel.hasPreHandlerHook) ? diesel.hooks.preHandler : [];
|
|
101
100
|
const OnSendHook = (diesel === null || diesel === void 0 ? void 0 : diesel.hasOnSendHook) ? diesel.hooks.onSend : [];
|
|
102
101
|
// parse pathname
|
|
103
102
|
pushParsePathname(pipeline);
|
|
104
103
|
// finc routeHandler
|
|
105
104
|
pipeline.push(`
|
|
106
|
-
const
|
|
105
|
+
const matchedRouteHandler = diesel.router.find(req.method, pathname);
|
|
107
106
|
`);
|
|
108
|
-
// Hooks
|
|
109
|
-
// if (onRequestHooks && onRequestHooks.length > 0) {
|
|
110
|
-
// pushHooks(pipeline, onRequestHooks, 'onRequest', 'req', 'pathname', 'server')
|
|
111
|
-
// }
|
|
112
107
|
pipeline.push(`
|
|
113
108
|
const ctx = new Context(
|
|
114
109
|
req,
|
|
115
110
|
server,
|
|
116
111
|
pathname,
|
|
117
|
-
|
|
118
|
-
|
|
112
|
+
matchedRouteHandler?.path,
|
|
113
|
+
matchedRouteHandler?.params,
|
|
119
114
|
env,
|
|
120
115
|
executionContext
|
|
121
116
|
)
|
|
122
117
|
`);
|
|
123
|
-
// Middlewares
|
|
124
|
-
// if (config?.hasMiddleware) {
|
|
125
|
-
// if (globalMiddlewares.length > 0) {
|
|
126
|
-
// pushMiddlewares(pipeline, globalMiddlewares as any)
|
|
127
|
-
// }
|
|
128
|
-
// // Local/path-specific middlewares still run via function
|
|
129
|
-
// if (diesel.middlewares.size > 0) {
|
|
130
|
-
// pipeline.push(`
|
|
131
|
-
// const local = diesel.middlewares.get(pathname)
|
|
132
|
-
// if (local && local.length) {
|
|
133
|
-
// for (const middleware of local) {
|
|
134
|
-
// const result = await middleware(ctx);
|
|
135
|
-
// if (result) return result;
|
|
136
|
-
// }
|
|
137
|
-
// }
|
|
138
|
-
// `)
|
|
139
|
-
// }
|
|
140
|
-
// // if (diesel.middlewares.size > 0) {
|
|
141
|
-
// // pipeline.push(`
|
|
142
|
-
// // const mwResult = await runMiddlewares(diesel, pathname, ctx);
|
|
143
|
-
// // if (mwResult) return mwResult;
|
|
144
|
-
// // `);
|
|
145
|
-
// // }
|
|
146
|
-
// }
|
|
147
118
|
// Filters
|
|
148
119
|
if (diesel.hasFilterEnabled) {
|
|
149
120
|
pipeline.push(`
|
|
@@ -157,13 +128,20 @@ export const buildRequestPipeline = (diesel) => {
|
|
|
157
128
|
}
|
|
158
129
|
// Actual route handler
|
|
159
130
|
pipeline.push(`
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
131
|
+
let finalResult
|
|
132
|
+
const handlers = matchedRouteHandler?.handler;
|
|
133
|
+
|
|
134
|
+
if (handlers.length === 1) {
|
|
135
|
+
const result = handlers[0](ctx);
|
|
136
|
+
finalResult = isPromise(result) ? await result : result;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
for (let i = 0; i < handlers.length; i++) {
|
|
140
|
+
const result = handlers[i](ctx);
|
|
141
|
+
finalResult = isPromise(result) ? await result : result;
|
|
142
|
+
if (finalResult) break;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
167
145
|
`);
|
|
168
146
|
// onSend
|
|
169
147
|
if (diesel.hasOnSendHook) {
|
|
@@ -171,7 +149,7 @@ export const buildRequestPipeline = (diesel) => {
|
|
|
171
149
|
}
|
|
172
150
|
// Final response
|
|
173
151
|
pipeline.push(`
|
|
174
|
-
if (finalResult
|
|
152
|
+
if (finalResult) return finalResult;
|
|
175
153
|
`);
|
|
176
154
|
// Route not found check
|
|
177
155
|
pipeline.push(`
|
|
@@ -182,7 +160,7 @@ export const buildRequestPipeline = (diesel) => {
|
|
|
182
160
|
${pipeline.join("\n")}
|
|
183
161
|
}
|
|
184
162
|
`;
|
|
185
|
-
const fnc = new Function("runFilter", "handleRouteNotFound", "generateErrorResponse", "Context", fnBody)(runFilter, handleRouteNotFound, generateErrorResponse, Context);
|
|
163
|
+
const fnc = new Function("runFilter", "handleRouteNotFound", "generateErrorResponse", "Context", "isPromise", fnBody)(runFilter, handleRouteNotFound, generateErrorResponse, Context, isPromise);
|
|
186
164
|
// console.log(fnc.toString())
|
|
187
165
|
return fnc;
|
|
188
166
|
};
|