-
Notifications
You must be signed in to change notification settings - Fork 4
/
http-incoming.js
281 lines (241 loc) · 6.63 KB
/
http-incoming.js
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
import { URL } from 'node:url';
import { Assets } from './assets.js';
const inspect = Symbol.for('nodejs.util.inspect.custom');
const urlFromRequest = (request) => {
const protocol = request?.protocol || 'http';
const host = request?.headers?.host || 'localhost';
const url = request?.url || '';
return new URL(url, `${protocol.replace(/:/, '')}://${host}`);
};
/**
* @template {Record<string, unknown>} [T=Record<string, unknown>]
* @template {Record<string, unknown>} [U=Record<string, unknown>]
* @template {Record<string, unknown>} [V=Record<string, unknown>]
* @typedef {object} PodiumHttpIncoming
* @property {string} name
* @property {U} context
* @property {V} view
* @property {URL} url
* @property {Array<import('./asset-css.js').CssAsset | string>} css
* @property {Array<import('./asset-js.js').JavaScriptAsset | string>} js
* @property {T} [params]
* @property {boolean} [development]
* @property {boolean} [proxy]
*/
/**
* @template {Record<string, unknown>} [T=Record<string, unknown>]
* @template {Record<string, unknown>} [U=Record<string, unknown>]
* @template {Record<string, unknown>} [V=Record<string, unknown>]
*/
export default class HttpIncoming {
#development;
#response;
#request;
/** @type {U} */
#context;
/** @type {T} */
#params;
#proxy;
#name;
/** @type {V} */
#view;
/**
* @type {URL | undefined}
*/
#url;
/**
* @type {Array<import('./asset-css.js').CssAsset | string>}
*/
#css;
/**
* @type {Array<import('./asset-js.js').JavaScriptAsset | string>}
*/
#js;
/** @type {Assets} */
assets = new Assets();
/**
* @constructor
* @param {object} [request={}] The incoming HTTP request
* @param {object} [response={}] The HTTP response
* @param {T} [params={}] Parameters such as locale. Typically res.locals.
*
* @example
* ```js
* const incoming = new HttpIncoming(req, res, res.locals);
* ```
*/
// @ts-expect-error Not happy about the generics, but this is safe
constructor(request = {}, response = {}, params = {}) {
this.#development = false;
this.#response = response;
this.#request = request;
// @ts-expect-error Not happy about the generics, but this is safe
this.#context = {};
this.#params = params;
this.#proxy = false;
this.#name = '';
// @ts-expect-error Not happy about the generics, but this is safe
this.#view = {};
this.#url = undefined;
this.#css = [];
this.#js = [];
}
set development(value) {
this.#development = value;
}
get development() {
return this.#development;
}
/**
* @throws {Error} Read only-property
*/
set response(value) {
throw new Error('Cannot set read-only property.');
}
get response() {
return this.#response;
}
set request(value) {
throw new Error('Cannot set read-only property.');
}
get request() {
return this.#request;
}
set context(value) {
this.#context = value;
}
get context() {
return this.#context;
}
// No way to type writeonly properly at time of writing: https://github.com/microsoft/TypeScript/issues/21759
// This is a workaround for a type error where TS complains we're trying to set a value to something that is type void.
/**
* @type {unknown}
*/
set podlets(value) {
const podlets = Array.isArray(value) ? value : [value];
podlets.forEach((podlet) => {
if (podlet.css) {
podlet.css.forEach((item) => {
this.#css.push(item);
});
}
if (podlet.js) {
podlet.js.forEach((item) => {
this.#js.push(item);
});
}
});
}
/**
* @type {unknown}
* @throws This prop is write only. Read from {@link css} and {@link js}.
*/
get podlets() {
throw new Error(
`The getter for .podlets is reserved for later implementation`,
);
}
set params(value) {
throw new Error('Cannot set read-only property.');
}
/**
* @returns {T}
*/
get params() {
return this.#params;
}
set proxy(value) {
this.#proxy = value;
}
get proxy() {
return this.#proxy;
}
set name(value) {
this.#name = value;
}
get name() {
return this.#name;
}
set view(value) {
this.#view = value;
}
get view() {
return this.#view;
}
set url(value) {
this.#url = new URL(value);
}
get url() {
if (this.#url) return this.#url;
return urlFromRequest(this.#request);
}
set css(value) {
if (!Array.isArray(value))
throw new Error(`Value for property ".css" must be an Array`);
this.#css = value;
}
get css() {
return this.#css;
}
set js(value) {
if (!Array.isArray(value))
throw new Error(`Value for property ".js" must be an Array`);
this.#js = value;
}
get js() {
return this.#js;
}
/**
* Wait for assets to be received from podlets
*
* @returns {Promise<{js: Array<import('./asset-js.js').JavaScriptAsset | string>, css: Array<import('./asset-css.js').CssAsset | string>}>}
*/
waitForAssets() {
if (this.assets.allAssetsReceived) {
return Promise.resolve({
js: this.assets.js,
css: this.assets.css,
});
}
return new Promise((resolve) => {
this.assets.on('received', (assets) => {
resolve(assets);
});
});
}
/**
* @returns {PodiumHttpIncoming<T>}
*/
toJSON() {
return {
development: this.development,
context: this.context,
params: this.params,
proxy: this.proxy,
name: this.name,
view: this.view,
url: this.url,
css: this.css,
js: this.js,
};
}
[inspect]() {
return {
development: this.development,
response: this.response,
request: this.request,
context: this.context,
params: this.params,
proxy: this.proxy,
name: this.name,
view: this.view,
url: this.url,
css: this.css,
js: this.js,
};
}
get [Symbol.toStringTag]() {
return 'PodiumHttpIncoming';
}
}