-
Notifications
You must be signed in to change notification settings - Fork 226
/
index.ts
95 lines (87 loc) · 3.06 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import type { H3, EventHandler, EventHandlerResponse } from "../../types";
import type { NodeHandler, NodeMiddleware } from "../../types/node";
import { NodeEvent } from "./event";
import { sendNodeResponse, callNodeHandler } from "./internal/utils";
import { prepareResponse, prepareErrorResponseBody } from "../../response";
import { createError } from "../../error";
export { kNodeReq, kNodeRes } from "./internal/utils";
/**
* Convert H3 app instance to a NodeHandler with (IncomingMessage, ServerResponse) => void signature.
*/
export function toNodeHandler(app: H3): NodeHandler {
const nodeHandler: NodeHandler = async function (nodeReq, nodeRes) {
// Create a new event instance
const event = new NodeEvent(nodeReq, nodeRes);
// Execute the handler
let handlerRes: unknown | Promise<unknown>;
try {
handlerRes = app._handler(event);
} catch (error: any) {
handlerRes = Promise.reject(error);
}
// Handle the response
return Promise.resolve(handlerRes)
.catch((error) => {
const h3Error = createError(error);
return app.config.onError
? Promise.resolve(app.config.onError(h3Error, event)).then(
(_res) => _res ?? h3Error,
)
: h3Error;
})
.then((resolvedRes) => {
const preparedRes = prepareResponse(resolvedRes, event, app.config);
let promise = app.config.onBeforeResponse
? Promise.resolve(
app.config.onBeforeResponse(event, preparedRes),
).then(() => sendNodeResponse(nodeRes, preparedRes.body))
: sendNodeResponse(nodeRes, preparedRes.body);
if (app.config.onAfterResponse) {
promise = promise.then(() =>
app.config.onAfterResponse!(event, preparedRes),
);
}
return promise;
})
.catch((error) => {
// Possible cases: Stream canceled, headers already sent, etc.
if (nodeRes.headersSent || nodeRes.writableEnded) {
return;
}
nodeRes.end(prepareErrorResponseBody(error, event, app.config));
});
};
return nodeHandler;
}
/**
* Convert a Node.js handler function (req, res, next?) to an EventHandler.
*
* **Note:** The returned event handler requires to be executed with h3 Node.js handler.
*/
export function fromNodeHandler(handler: NodeMiddleware): EventHandler;
export function fromNodeHandler(handler: NodeHandler): EventHandler;
export function fromNodeHandler(
handler: NodeHandler | NodeMiddleware,
): EventHandler {
if (typeof handler !== "function") {
throw new TypeError(`Invalid handler. It should be a function: ${handler}`);
}
return (event) => {
if (!event.node) {
throw new Error(
"[h3] Executing Node.js middleware is not supported in this server!",
);
}
return callNodeHandler(
handler,
event.node.req,
event.node.res,
) as EventHandlerResponse;
};
}
export function defineNodeHandler(handler: NodeHandler) {
return handler;
}
export function defineNodeMiddleware(handler: NodeMiddleware) {
return handler;
}