-
Notifications
You must be signed in to change notification settings - Fork 220
/
event.ts
141 lines (115 loc) · 3.2 KB
/
event.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { IncomingHttpHeaders } from "node:http";
import type { H3EventContext, HTTPMethod, EventHandlerRequest } from "../types";
import type { NodeIncomingMessage, NodeServerResponse } from "../adapters/node";
import { sendWebResponse } from "../utils";
import { hasProp } from "../utils/internal/object";
export interface NodeEventContext {
req: NodeIncomingMessage & { originalUrl?: string };
res: NodeServerResponse;
}
export interface WebEventContext {
request?: Request;
url?: URL;
}
export class H3Event<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_RequestT extends EventHandlerRequest = EventHandlerRequest,
> implements Pick<FetchEvent, "respondWith">
{
"__is_event__" = true;
// Context
node: NodeEventContext; // Node
web?: WebEventContext; // Web
context: H3EventContext = {}; // Shared
// Request
_method?: HTTPMethod;
_path?: string;
_headers?: Headers;
_requestBody?: BodyInit;
// Response
_handled = false;
constructor(req: NodeIncomingMessage, res: NodeServerResponse) {
this.node = { req, res };
}
// --- Request ---
get method(): HTTPMethod {
if (!this._method) {
this._method = (
this.node.req.method || "GET"
).toUpperCase() as HTTPMethod;
}
return this._method;
}
get path() {
return this._path || this.node.req.url || "/";
}
get headers(): Headers {
if (!this._headers) {
this._headers = _normalizeNodeHeaders(this.node.req.headers);
}
return this._headers;
}
// --- Respoonse ---
get handled(): boolean {
return (
this._handled || this.node.res.writableEnded || this.node.res.headersSent
);
}
respondWith(response: Response | PromiseLike<Response>): Promise<void> {
return Promise.resolve(response).then((_response) =>
sendWebResponse(this, _response),
);
}
// --- Utils ---
toString() {
return `[${this.method}] ${this.path}`;
}
toJSON() {
return this.toString();
}
// --- Deprecated ---
/** @deprecated Please use `event.node.req` instead. **/
get req() {
return this.node.req;
}
/** @deprecated Please use `event.node.res` instead. **/
get res() {
return this.node.res;
}
}
/**
* Checks if the input is an H3Event object.
* @param input - The input to check.
* @returns True if the input is an H3Event object, false otherwise.
* @see H3Event
*/
export function isEvent(input: any): input is H3Event {
return hasProp(input, "__is_event__");
}
/**
* Creates a new H3Event instance from the given Node.js request and response objects.
* @param req - The NodeIncomingMessage object.
* @param res - The NodeServerResponse object.
* @returns A new H3Event instance.
* @see H3Event
*/
export function createEvent(
req: NodeIncomingMessage,
res: NodeServerResponse,
): H3Event {
return new H3Event(req, res);
}
// --- Internal ---
function _normalizeNodeHeaders(nodeHeaders: IncomingHttpHeaders): Headers {
const headers = new Headers();
for (const [name, value] of Object.entries(nodeHeaders)) {
if (Array.isArray(value)) {
for (const item of value) {
headers.append(name, item);
}
} else if (value) {
headers.set(name, value);
}
}
return headers;
}