-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_server_request.js
More file actions
156 lines (132 loc) · 4.16 KB
/
http_server_request.js
File metadata and controls
156 lines (132 loc) · 4.16 KB
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
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
import EventEmitter from "node:events";
/** A request from a client to the HTTP server. */
export class HttpServerRequest {
/**
* Factory method. Wraps a Node.js HTTP request.
* @param nodeRequest the Node.js HTTP request
* @returns {HttpServerRequest} the wrapped request
*/
static create(nodeRequest) {
ensure.signature(arguments, [ Object ]);
return new HttpServerRequest(nodeRequest);
}
/**
* Factory method. Simulates an HTTP request.
* @param [options] details of the simulated HTTP request
* @param [options.url] path and optional querystring
* @param [options.method] method (GET, POST, etc.)
* @param [options.headers] headers
* @param [options.body] body
* @returns {HttpServerRequest} the simulated request
*/
static createNull(options) {
// signature checking in StubbedNodeRequest
return new HttpServerRequest(new StubbedNodeRequest(options));
}
/** Only for use by tests. (Use a factory method instead.) */
constructor(nodeRequest) {
ensure.signature(arguments, [ Object ]);
this._request = nodeRequest;
}
/**
* @returns {string} URL path (excluding query)
*/
get urlPathname() {
const url = new URL(this._request.url, "http://unused.placeholder.host");
return decodeURIComponent(url.pathname);
}
/**
* @returns {string} method (GET, POST, etc.)
*/
get method() {
return this._request.method;
}
/**
* @returns {object} headers
*/
get headers() {
return { ...this._request.headers };
}
/**
* Determine whether the request matches a particular content-type header.
* @param expectedMediaType the media type to match (e.g., "application/json")
* @returns {boolean} true if the content-type header matches the media type
*/
hasContentType(expectedMediaType) {
ensure.signature(arguments, [ String ]);
const contentType = this.headers["content-type"];
if (contentType === undefined) return false;
const [ mediaType, ignoredParameters ] = contentType.split(";");
return mediaType.trim().toLowerCase() === expectedMediaType.trim().toLowerCase();
}
/**
* Read the request body. The body may only be done once.
* @returns {Promise<string>} the body
*/
async readBodyAsync() {
return await new Promise((resolve, reject) => {
ensure.signature(arguments, []);
if (this._request.readableEnded) return reject(new Error("Can't read request body because it's already been read"));
let body = "";
this._request.on("error", reject); // this event is not tested
this._request.on("data", (chunk) => {
body += chunk;
});
this._request.on("end", () => {
resolve(body);
});
});
}
/**
* Read the request body and parse it as if it were a URL-encoded form. The body may only be read once.
* @returns {Promise<{}>} an object containing the form data, where the object keys are the form names
* and the object values are arrays containing the form values. For example, "a=1&b=2&b=3&c" will return
* this object: { a: [ "1" ], b: [ "2", "3" ], c: [ "" ] }
*/
async readBodyAsUrlEncodedFormAsync() {
const body = await this.readBodyAsync();
const params = new URLSearchParams(body);
const result = {};
for (const key of params.keys()) {
if (result[key] === undefined) result[key] = params.getAll(key);
}
return result;
}
}
class StubbedNodeRequest extends EventEmitter {
constructor({
url = "/null-request-url",
method = "GET",
headers = {},
body = "",
} = {}) {
ensure.signature(arguments, [[ undefined, {
url: [ undefined, String ],
method: [ undefined, String ],
headers: [ undefined, Object ],
body: [ undefined, String ],
}]]);
super();
this.url = url;
this.method = method.toUpperCase();
this.headers = normalizeHeaders(headers);
this._body = body;
this.readableEnded = false;
}
on(event, fn) {
super.on(event, fn);
if (event === "end") {
setImmediate(() => {
this.emit("data", this._body);
this.emit("end");
this.readableEnded = true;
});
}
}
}
function normalizeHeaders(headers) {
const normalizedEntries = Object.entries(headers).map(([ name, value ]) => [ name.toLowerCase(), value ]);
return Object.fromEntries(normalizedEntries);
}