-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.ts
300 lines (267 loc) · 6.31 KB
/
request.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import {
// HTTP Modules
ServerRequest,
HTTPOptions,
HTTPSOptions,
// HTTP Cookies
Cookies,
getCookies
} from "./deps.ts";
import { RouteData } from "./route_parser.ts";
import { RequestData, urlSearchQuery } from "./request_data.ts";
import Conn = Deno.Conn;
export interface HTTPMethods {
[key: string]: string;
}
export const HTTP: HTTPMethods = {
ALL: "",
GET: "GET",
HEAD: "HEAD",
POST: "POST",
PUT: "PUT",
DELETE: "DELETE",
CONNECT: "CONNECT",
OPTIONS: "OPTIONS",
TRACE: "TRACE",
PATCH: "PATCH",
};
export interface ServerInfo {
protocol: string;
hostname: string;
port: string | number;
certFile?: string;
keyFile?: string;
}
/**
* Parse Server Information ( string | HTTPOptions | HTTPSOptions -> ServerInfo )
*
* @param {string | HTTPOptions | HTTPSOptions} addr - HTTP/HTTPS Information
* @return {ServerInfo} Server Information
*
*/
export function parseServerInfo(
addr: string | HTTPOptions | HTTPSOptions,
): ServerInfo {
let [hostname, port] = (typeof addr === "string")
? addr.split(":")
: ["hostname" in addr ? addr["hostname"] : "localhost", addr.port];
hostname = hostname || "localhost";
const certFile = (typeof addr === "object" && "certFile" in addr)
? addr["certFile"]
: undefined;
const keyFile = (typeof addr === "object" && "keyFile" in addr)
? addr["keyFile"]
: undefined;
const protocol = certFile ? "https://" : "http://";
return {
protocol: protocol,
hostname: hostname,
port: port,
certFile: certFile,
keyFile: keyFile,
};
}
export interface RequestInfo {
conn: Conn;
url: URL;
method: string;
protocol: string;
headers: Headers;
cookies: Cookies;
params: RouteData;
query: RequestData;
body: ArrayBuffer | ArrayBufferView | undefined;
}
/**
* Create a new Request object based from Deno Request
*
* @param {string | HTTPOptions | HTTPSOptions} addr - HTTP/HTTPS Information
* @return {Promise<Request>} HTTP Request Object
*
*/
export async function createFromDenoRequest(
addr: string | HTTPOptions | HTTPSOptions,
serverRequest: ServerRequest,
params: RouteData = <RouteData> {},
): Promise<Request> {
const { protocol, hostname, port } = parseServerInfo(addr);
// TODO: Currently deno doesn't support auth with ServerRequest
const hostUrlString = `${protocol}${hostname}${
port == "443" || port == "80" ? "" : ":" + port
}${serverRequest.url}`;
const hostUrl = new URL(hostUrlString);
// TODO: Deno is checking for `cookie` instead of set-cookie
serverRequest.headers.set(
"cookie",
serverRequest.headers.get("set-cookie") || "",
);
const httpRequestContent: RequestInfo = {
conn: serverRequest.conn,
url: hostUrl,
method: serverRequest.method,
protocol: serverRequest.proto,
headers: serverRequest.headers,
cookies: getCookies(serverRequest),
body: await Deno.readAll(serverRequest.body),
params: params,
query: urlSearchQuery(hostUrl.search),
};
return new Promise((resolve) => {
resolve(new Request(httpRequestContent));
});
}
/**
* Rute HTTP Request Object
*
*/
export class Request {
private _requestData: RequestInfo;
/**
* New HTTP Request
*
* @param {RequestInfo} requestData - RequestInfo based from Deno HTTP Request
*
*/
constructor(requestData: RequestInfo) {
this._requestData = requestData;
}
/**
* URL Route Parameters
*
* @param {string} key - URL Pattern Key (default = "")
* @param {any} fallback - Fallback value
* @return {any} Param value
*
*/
param(key: string = "", fallback: any = null): any {
return this._requestData.params[key] || fallback;
}
/**
* Get params
*
* @return {RouteData} RouteData
*
*/
get params(): RouteData {
return this._requestData.params;
}
/**
* URL Search Query
*
* @param {string} key - URL Pattern Key (default = "")
* @param {any} fallback - Fallback value
* @return {any} Query search value
*
*/
query(key: string, fallback: any = null): any {
return this._requestData.query[key] || fallback;
}
/**
* URL Search Queries
*
* @return {RequestData} RequestData
*
*/
get queries(): RequestData {
return this._requestData.query;
}
/**
* Get request connection
*
* @return {Conn} Deno.Connection
*
*/
get connection() {
return this._requestData.conn;
}
/**
* Get request protocol
*
* @return {string} Request Protocol
*
*/
get protocol() {
return this._requestData.protocol;
}
/**
* Get request url
*
* @return {string} Request URL
*
*/
get url(): URL {
return this._requestData.url;
}
/**
* Get request method
*
* @return {string} Request method
*
*/
get method(): string {
return this._requestData.method;
}
/**
* Check if content-type is
*
* @param {string} contentType - MIME Type
* @return {boolean} boolean
*
*/
is(contentType: string): boolean {
let contentTypeTest: RegExp = new RegExp(
`^.*/${contentType.toLowerCase()}`,
);
let header: string = this.header("content-type", "");
return contentTypeTest.test(header);
}
/**
* Get request header
*
* @param {string} key - Header name
* @param {any} fallback - Fallback data (default: null)
* @return {string} Request header
*
*/
header(key: string, fallback: any = null): string {
return this._requestData.headers.get(key) || fallback;
}
/**
* Request Headers
*
* @return {Headers} Request Headers
*
*/
get headers(): Headers {
return this._requestData.headers;
}
/**
* Get request cookie
*
* @param {string} key - Cookie name
* @param {any} fallback - Fallback data (default: null)
* @return {string} Request cookie
*
*/
cookie(key: string, fallback: any = null): string {
return this._requestData.cookies[key] || fallback;
}
/**
* Get request cookies
*
* @return {Cookies} Request Cookies
*
*/
get cookies(): Cookies {
return this._requestData.cookies;
}
/**
* Get request body
*
* @return {ArrayBuffer | ArrayBufferView | undefined} Request body
*
*/
get body(): ArrayBuffer | ArrayBufferView | undefined {
return this._requestData.body;
}
}