hoa 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/dist/cjs/context.cjs +152 -0
- package/dist/cjs/hoa.cjs +158 -0
- package/dist/cjs/lib/compose.cjs +35 -0
- package/dist/cjs/lib/http-error.cjs +42 -0
- package/dist/cjs/lib/utils.cjs +214 -0
- package/dist/cjs/request.cjs +516 -0
- package/dist/cjs/response.cjs +335 -0
- package/dist/esm/context.mjs +152 -0
- package/dist/esm/hoa.mjs +148 -0
- package/dist/esm/lib/compose.mjs +34 -0
- package/dist/esm/lib/http-error.mjs +42 -0
- package/dist/esm/lib/utils.mjs +207 -0
- package/dist/esm/request.mjs +516 -0
- package/dist/esm/response.mjs +335 -0
- package/package.json +7 -7
- package/dist/cjs/context.js +0 -177
- package/dist/cjs/hoa.js +0 -192
- package/dist/cjs/lib/compose.js +0 -40
- package/dist/cjs/lib/http-error.js +0 -61
- package/dist/cjs/lib/utils.js +0 -190
- package/dist/cjs/request.js +0 -546
- package/dist/cjs/response.js +0 -379
- package/dist/esm/context.js +0 -148
- package/dist/esm/hoa.js +0 -151
- package/dist/esm/lib/compose.js +0 -21
- package/dist/esm/lib/http-error.js +0 -42
- package/dist/esm/lib/utils.js +0 -161
- package/dist/esm/request.js +0 -527
- package/dist/esm/response.js +0 -360
package/dist/esm/response.js
DELETED
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
import { statusTextMapping, statusEmptyMapping, statusRedirectMapping, commonTypeMapping, encodeUrl } from "./lib/utils.js";
|
|
2
|
-
class HoaResponse {
|
|
3
|
-
/**
|
|
4
|
-
* Expose a plain object snapshot of response headers.
|
|
5
|
-
* Uses the Web Standard Headers API internally for proper header handling.
|
|
6
|
-
* Returns a plain object representation where header names are normalized.
|
|
7
|
-
*
|
|
8
|
-
* @returns {Record<string, string>} Object with all response headers
|
|
9
|
-
* @public
|
|
10
|
-
*/
|
|
11
|
-
get headers() {
|
|
12
|
-
this._headers = this._headers || new Headers();
|
|
13
|
-
return Object.fromEntries(this._headers.entries());
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Set the response headers.
|
|
17
|
-
* Accepts either a Headers object or a plain object/array of header entries.
|
|
18
|
-
* This replaces all existing headers with the new ones.
|
|
19
|
-
*
|
|
20
|
-
* @param {Headers|Record<string, string>|Array<[string, string]>} val - Headers to set
|
|
21
|
-
* @public
|
|
22
|
-
*/
|
|
23
|
-
set headers(val) {
|
|
24
|
-
if (val instanceof Headers) {
|
|
25
|
-
this._headers = val;
|
|
26
|
-
} else {
|
|
27
|
-
this._headers = new Headers(val);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Get a response header value by name (case-insensitive).
|
|
32
|
-
* Uses the Web Standard Headers API for proper header retrieval.
|
|
33
|
-
* Special-cases "referer/referrer" for compatibility.
|
|
34
|
-
* Returns null for empty or whitespace-only field names.
|
|
35
|
-
*
|
|
36
|
-
* @param {string} field - The header name
|
|
37
|
-
* @returns {string|null} The header value or null if not found
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
get(field) {
|
|
41
|
-
if (!field) return null;
|
|
42
|
-
this._headers = this._headers || new Headers();
|
|
43
|
-
switch (field = field.toLowerCase()) {
|
|
44
|
-
case "referer":
|
|
45
|
-
case "referrer": {
|
|
46
|
-
return this._headers.get("referrer") ?? this._headers.get("referer");
|
|
47
|
-
}
|
|
48
|
-
default:
|
|
49
|
-
return this._headers.get(field);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Get all Set-Cookie header values as an array.
|
|
54
|
-
* Returns all Set-Cookie headers that will be sent with the response.
|
|
55
|
-
*
|
|
56
|
-
* @returns {string[]} Array of Set-Cookie header values
|
|
57
|
-
* @public
|
|
58
|
-
*/
|
|
59
|
-
getSetCookie() {
|
|
60
|
-
this._headers = this._headers || new Headers();
|
|
61
|
-
return this._headers.getSetCookie();
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Check if a response header is present.
|
|
65
|
-
* Uses the Web Standard Headers API for case-insensitive header checking.
|
|
66
|
-
*
|
|
67
|
-
* @param {string} field - The header name to check
|
|
68
|
-
* @returns {boolean} True if the header exists
|
|
69
|
-
* @public
|
|
70
|
-
*/
|
|
71
|
-
has(field) {
|
|
72
|
-
if (!field) return false;
|
|
73
|
-
this._headers = this._headers || new Headers();
|
|
74
|
-
return this._headers.has(field);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Set response header(s) using the Web Standard Headers API.
|
|
78
|
-
* Accepts various input formats:
|
|
79
|
-
* - Single key/value pair
|
|
80
|
-
* - Plain object with multiple headers
|
|
81
|
-
* Replaces existing header values.
|
|
82
|
-
*
|
|
83
|
-
* @param {string|Record<string,string>} field - Header name or headers object
|
|
84
|
-
* @param {string} [val] - Header value when field is a string
|
|
85
|
-
* @public
|
|
86
|
-
*/
|
|
87
|
-
set(field, val) {
|
|
88
|
-
if (!field) return;
|
|
89
|
-
this._headers = this._headers || new Headers();
|
|
90
|
-
if (typeof field === "string") {
|
|
91
|
-
this._headers.set(field, val);
|
|
92
|
-
} else {
|
|
93
|
-
Object.keys(field).forEach((header) => this._headers.set(header, field[header]));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Append a response header value using the Web Standard Headers API.
|
|
98
|
-
* Does not replace existing values, but appends to them.
|
|
99
|
-
* This is useful for headers that can have multiple values like Set-Cookie.
|
|
100
|
-
*
|
|
101
|
-
* @param {string|Record<string,string>} field - The header name or headers object
|
|
102
|
-
* @param {string} [val] - The value to append when field is a string
|
|
103
|
-
* @public
|
|
104
|
-
*/
|
|
105
|
-
append(field, val) {
|
|
106
|
-
if (!field) return;
|
|
107
|
-
this._headers = this._headers || new Headers();
|
|
108
|
-
if (typeof field === "string") {
|
|
109
|
-
this._headers.append(field, val);
|
|
110
|
-
} else {
|
|
111
|
-
Object.keys(field).forEach((header) => this._headers.append(header, field[header]));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Delete a response header by name using the Web Standard Headers API.
|
|
116
|
-
* Header deletion is case-insensitive.
|
|
117
|
-
*
|
|
118
|
-
* @param {string} field - The header name to delete
|
|
119
|
-
* @public
|
|
120
|
-
*/
|
|
121
|
-
delete(field) {
|
|
122
|
-
if (!field) return;
|
|
123
|
-
this._headers = this._headers || new Headers();
|
|
124
|
-
this._headers.delete(field);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Get the response status code.
|
|
128
|
-
* Defaults to 404 if not explicitly set.
|
|
129
|
-
*
|
|
130
|
-
* @returns {number} The HTTP status code
|
|
131
|
-
* @public
|
|
132
|
-
*/
|
|
133
|
-
get status() {
|
|
134
|
-
return this._status || 404;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Set the response status code.
|
|
138
|
-
* Automatically clears body for status codes that should not have content.
|
|
139
|
-
*
|
|
140
|
-
* @param {number} val - The HTTP status code (100-599)
|
|
141
|
-
* @throws {TypeError}
|
|
142
|
-
* @public
|
|
143
|
-
*/
|
|
144
|
-
set status(val) {
|
|
145
|
-
if (!Number.isInteger(val)) {
|
|
146
|
-
throw new TypeError("status code must be an integer");
|
|
147
|
-
}
|
|
148
|
-
if (val < 100 || val > 1e3) {
|
|
149
|
-
throw new TypeError(`invalid status code: ${val}`);
|
|
150
|
-
}
|
|
151
|
-
this._status = val;
|
|
152
|
-
this._explicitStatus = true;
|
|
153
|
-
if (!this._explicitStatusText) {
|
|
154
|
-
this._statusText = statusTextMapping[val];
|
|
155
|
-
}
|
|
156
|
-
if (this.body && statusEmptyMapping[val]) this.body = null;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Get the response status text.
|
|
160
|
-
*
|
|
161
|
-
* @returns {string} The statusText (e.g., 'OK', 'Not Found')
|
|
162
|
-
* @public
|
|
163
|
-
*/
|
|
164
|
-
get statusText() {
|
|
165
|
-
if (this._explicitStatusText) {
|
|
166
|
-
return this._statusText;
|
|
167
|
-
}
|
|
168
|
-
return this._statusText || (this._statusText = statusTextMapping[this.status]);
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Set a custom response status text.
|
|
172
|
-
*
|
|
173
|
-
* @param {string} val - The custom status text
|
|
174
|
-
* @public
|
|
175
|
-
*/
|
|
176
|
-
set statusText(val) {
|
|
177
|
-
this._statusText = val;
|
|
178
|
-
this._explicitStatusText = true;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Get the response body.
|
|
182
|
-
*
|
|
183
|
-
* @returns {any} The response body content
|
|
184
|
-
* @public
|
|
185
|
-
*/
|
|
186
|
-
get body() {
|
|
187
|
-
return this._body;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Set response body with automatic content-type detection.
|
|
191
|
-
* Supports various body types and automatically sets appropriate headers.
|
|
192
|
-
* When set to null/undefined, if current Content-Type is application/json,
|
|
193
|
-
* body becomes the literal string 'null'; otherwise status is set to 204 and
|
|
194
|
-
* Content-Type/Transfer-Encoding are removed.
|
|
195
|
-
*
|
|
196
|
-
* @param {string|Object|ReadableStream|Blob|Response|ArrayBuffer|TypedArray|FormData|URLSearchParams|null} val - The response body
|
|
197
|
-
* @public
|
|
198
|
-
*/
|
|
199
|
-
set body(val) {
|
|
200
|
-
this._body = val;
|
|
201
|
-
if (val == null) {
|
|
202
|
-
if (!statusEmptyMapping[this.status]) {
|
|
203
|
-
if (this.type === "application/json") {
|
|
204
|
-
this._body = "null";
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
this.status = 204;
|
|
208
|
-
}
|
|
209
|
-
if (val === null) this._explicitNullBody = true;
|
|
210
|
-
this.delete("Content-Type");
|
|
211
|
-
this.delete("Content-Length");
|
|
212
|
-
this.delete("Transfer-Encoding");
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
if (!this._explicitStatus) this.status = 200;
|
|
216
|
-
const noType = !this.has("Content-Type");
|
|
217
|
-
if (typeof val === "string") {
|
|
218
|
-
if (noType) this.type = /^\s*</.test(val) ? "html" : "text";
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
if (val instanceof Blob || val instanceof ArrayBuffer || ArrayBuffer.isView(val) || val instanceof ReadableStream) {
|
|
222
|
-
if (noType) {
|
|
223
|
-
if (val instanceof Blob && val.type) {
|
|
224
|
-
this.set("Content-Type", val.type);
|
|
225
|
-
} else {
|
|
226
|
-
this.type = "bin";
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (val instanceof FormData) {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
if (val instanceof URLSearchParams) {
|
|
235
|
-
if (noType) this.type = "form";
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
if (val instanceof Response) {
|
|
239
|
-
if (noType) this.type = "bin";
|
|
240
|
-
this.status = val.status;
|
|
241
|
-
for (const [k, v] of val.headers) this.set(k, v);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
if (!this.type || !/\bjson\b/i.test(this.type)) this.type = "json";
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Perform an HTTP redirect to the specified URL.
|
|
248
|
-
* Automatically sets the Location header and appropriate status code.
|
|
249
|
-
* Absolute URLs are normalized and Location value is URL-encoded.
|
|
250
|
-
*
|
|
251
|
-
* @param {string} url - The URL to redirect to (absolute or relative)
|
|
252
|
-
* @public
|
|
253
|
-
*/
|
|
254
|
-
redirect(url) {
|
|
255
|
-
if (/^https?:\/\//i.test(url)) {
|
|
256
|
-
url = new URL(url).toString();
|
|
257
|
-
}
|
|
258
|
-
this.set("Location", encodeUrl(url));
|
|
259
|
-
if (!statusRedirectMapping[this.status]) this.status = 302;
|
|
260
|
-
this.type = "text";
|
|
261
|
-
this.body = `Redirecting to ${url}.`;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Perform a special-cased "back" redirect using the Referrer header.
|
|
265
|
-
* When Referrer is not present or unsafe, falls back to the provided alternative or "/".
|
|
266
|
-
* Only redirects to same-origin referrers for security.
|
|
267
|
-
* If referrer is an absolute URL with different origin or an invalid URL, falls back.
|
|
268
|
-
*
|
|
269
|
-
* @param {string} [alt='/'] - Alternative URL when referrer is unavailable or unsafe
|
|
270
|
-
* @public
|
|
271
|
-
*/
|
|
272
|
-
back(alt) {
|
|
273
|
-
const referrer = this.req.get("Referrer");
|
|
274
|
-
if (referrer) {
|
|
275
|
-
if (referrer.startsWith("/")) {
|
|
276
|
-
this.redirect(referrer);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
const url = new URL(referrer, this.req.href);
|
|
280
|
-
if (url.origin === this.req.origin) {
|
|
281
|
-
this.redirect(referrer);
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
this.redirect(alt || "/");
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Get the response Content-Type without parameters.
|
|
289
|
-
* Returns only the media type without parameters (e.g., 'application/json').
|
|
290
|
-
*
|
|
291
|
-
* @returns {string|null} The content type, or null if not set
|
|
292
|
-
* @public
|
|
293
|
-
*/
|
|
294
|
-
get type() {
|
|
295
|
-
const type = this.get("Content-Type");
|
|
296
|
-
if (!type) return null;
|
|
297
|
-
return type.split(";", 1)[0];
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Set the response Content-Type.
|
|
301
|
-
* Supports both full MIME types and shorthand aliases.
|
|
302
|
-
*
|
|
303
|
-
* @param {string} type - The content type or alias (e.g., 'json', 'html', 'application/json')
|
|
304
|
-
* @public
|
|
305
|
-
*/
|
|
306
|
-
set type(val) {
|
|
307
|
-
if (!val) return;
|
|
308
|
-
val = commonTypeMapping[val] || val;
|
|
309
|
-
this.set("Content-Type", val);
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Get the response Content-Length as a number.
|
|
313
|
-
* Returns the parsed Content-Length header value, or calculates it from the body if available.
|
|
314
|
-
*
|
|
315
|
-
* @returns {number|null} The content length in bytes, or null if not determinable
|
|
316
|
-
* @public
|
|
317
|
-
*/
|
|
318
|
-
get length() {
|
|
319
|
-
if (this.has("Content-Length")) {
|
|
320
|
-
return Number.parseInt(this.get("Content-Length"), 10) || 0;
|
|
321
|
-
}
|
|
322
|
-
if (this.body == null || this.body instanceof ReadableStream || this.body instanceof FormData || this.body instanceof Response) {
|
|
323
|
-
return null;
|
|
324
|
-
}
|
|
325
|
-
if (typeof this.body === "string") return new TextEncoder().encode(this.body).length;
|
|
326
|
-
if (this.body instanceof Blob) return this.body.size;
|
|
327
|
-
if (this.body instanceof ArrayBuffer) return this.body.byteLength;
|
|
328
|
-
if (ArrayBuffer.isView(this.body)) return this.body.byteLength;
|
|
329
|
-
if (this.body instanceof URLSearchParams) return new TextEncoder().encode(this.body.toString()).length;
|
|
330
|
-
return new TextEncoder().encode(JSON.stringify(this.body)).length;
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Set the response Content-Length header.
|
|
334
|
-
* Only sets the header if Transfer-Encoding is not present.
|
|
335
|
-
*
|
|
336
|
-
* @param {number|string} val - The content length in bytes
|
|
337
|
-
* @public
|
|
338
|
-
*/
|
|
339
|
-
set length(val) {
|
|
340
|
-
if (!this.has("Transfer-Encoding")) {
|
|
341
|
-
this.set("Content-Length", val);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* Return JSON representation of the response.
|
|
346
|
-
*
|
|
347
|
-
* @returns {ResJSON} JSON representation of response
|
|
348
|
-
* @public
|
|
349
|
-
*/
|
|
350
|
-
toJSON() {
|
|
351
|
-
return {
|
|
352
|
-
status: this.status,
|
|
353
|
-
statusText: this.statusText,
|
|
354
|
-
headers: this.headers
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
export {
|
|
359
|
-
HoaResponse as default
|
|
360
|
-
};
|