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/dist/cjs/hoa.js DELETED
@@ -1,192 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
- var hoa_exports = {};
29
- __export(hoa_exports, {
30
- Hoa: () => Hoa,
31
- HoaContext: () => import_context.default,
32
- HoaRequest: () => import_request.default,
33
- HoaResponse: () => import_response.default,
34
- HttpError: () => import_http_error.default,
35
- compose: () => import_compose.default,
36
- default: () => Hoa,
37
- statusEmptyMapping: () => import_utils.statusEmptyMapping,
38
- statusRedirectMapping: () => import_utils.statusRedirectMapping,
39
- statusTextMapping: () => import_utils.statusTextMapping
40
- });
41
- module.exports = __toCommonJS(hoa_exports);
42
- var import_compose = __toESM(require("./lib/compose.js"), 1);
43
- var import_http_error = __toESM(require("./lib/http-error.js"), 1);
44
- var import_utils = require("./lib/utils.js");
45
- var import_context = __toESM(require("./context.js"), 1);
46
- var import_request = __toESM(require("./request.js"), 1);
47
- var import_response = __toESM(require("./response.js"), 1);
48
- class Hoa {
49
- /**
50
- * Create an Hoa instance.
51
- *
52
- * @param {Object} [options={}] - Application options
53
- * @param {string} [options.name='Hoa'] - Application name for identification
54
- */
55
- constructor(options = {}) {
56
- this.name = options.name || "Hoa";
57
- this.HoaContext = import_context.default;
58
- this.HoaRequest = import_request.default;
59
- this.HoaResponse = import_response.default;
60
- this.middlewares = [];
61
- this.fetch = this.fetch.bind(this);
62
- }
63
- /**
64
- * Extend the application with a plugin initializer.
65
- *
66
- * @param {HoaExtension} fn - Plugin function that receives the app instance
67
- * @returns {Hoa} The Hoa instance for method chaining
68
- * @throws {TypeError}
69
- * @public
70
- */
71
- extend(fn) {
72
- if (typeof fn !== "function") {
73
- throw new TypeError("extend() must receive a function!");
74
- }
75
- fn(this);
76
- return this;
77
- }
78
- /**
79
- * Register a middleware. Executed in registration order.
80
- *
81
- * @param {HoaMiddleware} fn - Middleware function
82
- * @returns {Hoa} The Hoa instance for method chaining
83
- * @throws {TypeError}
84
- * @public
85
- */
86
- use(fn) {
87
- if (typeof fn !== "function") {
88
- throw new TypeError("use() must receive a function!");
89
- }
90
- this.middlewares.push(fn);
91
- this._composedMiddleware = null;
92
- return this;
93
- }
94
- /**
95
- * Web Standards fetch handler - main entry point for HTTP requests.
96
- * Compatible with Cloudflare Workers, Deno, and other Web Standards environments.
97
- *
98
- * @param {Request} request - Web Standard Request object
99
- * @param {any} [env] - Environment variables (platform-specific)
100
- * @param {any} [executionCtx] - Execution context (platform-specific)
101
- * @returns {Promise<Response>} Web Standard Response object
102
- * @public
103
- */
104
- fetch(request, env, executionCtx) {
105
- const ctx = this.createContext(request, env, executionCtx);
106
- if (!this._composedMiddleware) this._composedMiddleware = (0, import_compose.default)(this.middlewares);
107
- return this.handleRequest(ctx, this._composedMiddleware);
108
- }
109
- /**
110
- * Handle incoming request through the middleware stack.
111
- * Manages error handling and response building.
112
- *
113
- * @param {HoaContext} ctx - Request context
114
- * @param {HoaMiddleware} middlewareFn - Composed middleware function
115
- * @returns {Promise<Response>} Web Standard Response object
116
- * @private
117
- */
118
- handleRequest(ctx, middlewareFn) {
119
- return middlewareFn(ctx).then(() => ctx.response).catch((err) => ctx.onerror(err));
120
- }
121
- /**
122
- * Create context for incoming request with linked request/response objects.
123
- * Establishes the context chain: ctx ↔ req ↔ res ↔ app
124
- *
125
- * @param {Request} request - Web Standard Request object
126
- * @param {any} [env] - Environment variables
127
- * @param {any} [executionCtx] - Execution context
128
- * @returns {HoaContext} Created context instance
129
- * @private
130
- */
131
- createContext(request, env, executionCtx) {
132
- const ctx = new this.HoaContext({ request, env, executionCtx });
133
- const req = ctx.req = new this.HoaRequest();
134
- const res = ctx.res = new this.HoaResponse();
135
- ctx.app = req.app = res.app = this;
136
- req.ctx = res.ctx = ctx;
137
- req.res = res;
138
- res.req = req;
139
- return ctx;
140
- }
141
- /**
142
- * Default error handler for unhandled application errors.
143
- * Logs errors to console unless they're client errors (4xx) or explicitly exposed.
144
- *
145
- * @param {Error} err - Error to handle
146
- * @param {HoaContext} [ctx] - Request context (optional)
147
- * @returns {void}
148
- * @throws {TypeError}
149
- * @private
150
- */
151
- onerror(err, ctx) {
152
- const isNativeError = Object.prototype.toString.call(err) === "[object Error]" || err instanceof Error;
153
- if (!isNativeError) {
154
- throw new TypeError(`non-error thrown: ${JSON.stringify(err)}`);
155
- }
156
- if (err.status === 404 || err.expose) return;
157
- if (this.silent) return;
158
- console.error(err);
159
- }
160
- /**
161
- * ESM/CJS interop helper for default exports.
162
- *
163
- * @returns {typeof Hoa} The Hoa class
164
- * @static
165
- */
166
- static get default() {
167
- return Hoa;
168
- }
169
- /**
170
- * Return JSON representation of the app.
171
- *
172
- * @returns {AppJSON} JSON representation of application
173
- * @public
174
- */
175
- toJSON() {
176
- return {
177
- name: this.name
178
- };
179
- }
180
- }
181
- // Annotate the CommonJS export names for ESM import in node:
182
- 0 && (module.exports = {
183
- Hoa,
184
- HoaContext,
185
- HoaRequest,
186
- HoaResponse,
187
- HttpError,
188
- compose,
189
- statusEmptyMapping,
190
- statusRedirectMapping,
191
- statusTextMapping
192
- });
@@ -1,40 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var compose_exports = {};
19
- __export(compose_exports, {
20
- default: () => compose
21
- });
22
- module.exports = __toCommonJS(compose_exports);
23
- const composeSlim = (middlewares) => async (ctx, next) => {
24
- const dispatch = (i) => async () => {
25
- const fn = i === middlewares.length ? next : middlewares[i];
26
- if (!fn) return;
27
- return await fn(ctx, dispatch(i + 1));
28
- };
29
- return dispatch(0)();
30
- };
31
- function compose(middlewares) {
32
- if (!Array.isArray(middlewares)) {
33
- throw new TypeError("compose() must receive an array of middleware functions!");
34
- }
35
- middlewares = middlewares.flat();
36
- for (const middleware of middlewares) {
37
- if (typeof middleware !== "function") throw new TypeError("Middleware must be composed of functions!");
38
- }
39
- return composeSlim(middlewares);
40
- }
@@ -1,61 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var http_error_exports = {};
19
- __export(http_error_exports, {
20
- default: () => HttpError
21
- });
22
- module.exports = __toCommonJS(http_error_exports);
23
- var import_utils = require("./utils.js");
24
- class HttpError extends Error {
25
- /**
26
- * Create a new HttpError instance.
27
- *
28
- * @param {number} status - HTTP status code (400-599, invalid codes become 500)
29
- * @param {string|HttpErrorOptions} [message] - Error message or options object
30
- * @param {HttpErrorOptions} [options] - Additional options when second param is string
31
- * @throws {TypeError}
32
- */
33
- constructor(status, message, options) {
34
- if (!Number.isInteger(status)) {
35
- throw new TypeError("status code must be an integer");
36
- }
37
- if (status < 400 || status >= 600) {
38
- status = 500;
39
- }
40
- let finalOptions = {};
41
- if (typeof message === "string") {
42
- finalOptions.message = message;
43
- if (options && typeof options === "object") {
44
- finalOptions = { ...finalOptions, ...options };
45
- }
46
- } else if (message && typeof message === "object") {
47
- finalOptions = message;
48
- }
49
- message = finalOptions.message ?? import_utils.statusTextMapping[status] ?? "Unknown error";
50
- super(message, { cause: finalOptions.cause });
51
- this.name = "HttpError";
52
- this.status = this.statusCode = status;
53
- this.expose = finalOptions.expose ?? status < 500;
54
- if (finalOptions.headers) {
55
- this.headers = Object.fromEntries(new Headers(finalOptions.headers).entries());
56
- }
57
- if (Error.captureStackTrace) {
58
- Error.captureStackTrace(this, HttpError);
59
- }
60
- }
61
- }
@@ -1,190 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var utils_exports = {};
19
- __export(utils_exports, {
20
- commonTypeMapping: () => commonTypeMapping,
21
- encodeUrl: () => encodeUrl,
22
- parseSearchParamsToQuery: () => parseSearchParamsToQuery,
23
- statusEmptyMapping: () => statusEmptyMapping,
24
- statusRedirectMapping: () => statusRedirectMapping,
25
- statusTextMapping: () => statusTextMapping,
26
- stringifyQueryToString: () => stringifyQueryToString
27
- });
28
- module.exports = __toCommonJS(utils_exports);
29
- function parseSearchParamsToQuery(searchParams) {
30
- const query = {};
31
- for (const [key, value] of searchParams) {
32
- if (query[key] !== void 0) {
33
- query[key] = [].concat(query[key], value);
34
- } else {
35
- query[key] = value;
36
- }
37
- }
38
- return query;
39
- }
40
- function stringifyQueryToString(query) {
41
- if (!query) {
42
- return "";
43
- }
44
- const params = new URLSearchParams();
45
- for (const [key, value] of Object.entries(query)) {
46
- if (Array.isArray(value)) {
47
- value.forEach((v) => params.append(key, v ?? ""));
48
- } else {
49
- params.append(key, value ?? "");
50
- }
51
- }
52
- return params.toString();
53
- }
54
- const statusTextMapping = {
55
- 100: "Continue",
56
- 101: "Switching Protocols",
57
- 102: "Processing",
58
- 103: "Early Hints",
59
- 200: "OK",
60
- 201: "Created",
61
- 202: "Accepted",
62
- 203: "Non-Authoritative Information",
63
- 204: "No Content",
64
- 205: "Reset Content",
65
- 206: "Partial Content",
66
- 207: "Multi-Status",
67
- 208: "Already Reported",
68
- 226: "IM Used",
69
- 300: "Multiple Choices",
70
- 301: "Moved Permanently",
71
- 302: "Found",
72
- 303: "See Other",
73
- 304: "Not Modified",
74
- 305: "Use Proxy",
75
- 307: "Temporary Redirect",
76
- 308: "Permanent Redirect",
77
- 400: "Bad Request",
78
- 401: "Unauthorized",
79
- 402: "Payment Required",
80
- 403: "Forbidden",
81
- 404: "Not Found",
82
- 405: "Method Not Allowed",
83
- 406: "Not Acceptable",
84
- 407: "Proxy Authentication Required",
85
- 408: "Request Timeout",
86
- 409: "Conflict",
87
- 410: "Gone",
88
- 411: "Length Required",
89
- 412: "Precondition Failed",
90
- 413: "Payload Too Large",
91
- 414: "URI Too Long",
92
- 415: "Unsupported Media Type",
93
- 416: "Range Not Satisfiable",
94
- 417: "Expectation Failed",
95
- 418: "I'm a Teapot",
96
- 421: "Misdirected Request",
97
- 422: "Unprocessable Entity",
98
- 423: "Locked",
99
- 424: "Failed Dependency",
100
- 425: "Too Early",
101
- 426: "Upgrade Required",
102
- 428: "Precondition Required",
103
- 429: "Too Many Requests",
104
- 431: "Request Header Fields Too Large",
105
- 451: "Unavailable For Legal Reasons",
106
- 500: "Internal Server Error",
107
- 501: "Not Implemented",
108
- 502: "Bad Gateway",
109
- 503: "Service Unavailable",
110
- 504: "Gateway Timeout",
111
- 505: "HTTP Version Not Supported",
112
- 506: "Variant Also Negotiates",
113
- 507: "Insufficient Storage",
114
- 508: "Loop Detected",
115
- 509: "Bandwidth Limit Exceeded",
116
- 510: "Not Extended",
117
- 511: "Network Authentication Required"
118
- };
119
- const statusRedirectMapping = {
120
- 300: true,
121
- 301: true,
122
- 302: true,
123
- 303: true,
124
- 305: true,
125
- 307: true,
126
- 308: true
127
- };
128
- const statusEmptyMapping = {
129
- 204: true,
130
- 205: true,
131
- 304: true
132
- };
133
- const commonTypeMapping = {
134
- // Text types
135
- html: "text/html;charset=UTF-8",
136
- text: "text/plain;charset=UTF-8",
137
- xml: "text/xml;charset=UTF-8",
138
- md: "text/markdown;charset=UTF-8",
139
- // Application types
140
- json: "application/json",
141
- form: "application/x-www-form-urlencoded;charset=UTF-8",
142
- pdf: "application/pdf",
143
- zip: "application/zip",
144
- wasm: "application/wasm",
145
- webmanifest: "application/manifest+json",
146
- // JavaScript/TypeScript
147
- js: "application/javascript;charset=UTF-8",
148
- ts: "application/typescript;charset=UTF-8",
149
- // Image types
150
- png: "image/png",
151
- jpg: "image/jpeg",
152
- jpeg: "image/jpeg",
153
- gif: "image/gif",
154
- svg: "image/svg+xml",
155
- webp: "image/webp",
156
- avif: "image/avif",
157
- ico: "image/x-icon",
158
- // Audio types
159
- mp3: "audio/mpeg",
160
- wav: "audio/wav",
161
- ogg: "audio/ogg",
162
- // Video types
163
- mp4: "video/mp4",
164
- webm: "video/webm",
165
- avi: "video/x-msvideo",
166
- mov: "video/quicktime",
167
- // Font types
168
- woff: "font/woff",
169
- woff2: "font/woff2",
170
- ttf: "font/ttf",
171
- otf: "font/otf",
172
- // Binary
173
- bin: "application/octet-stream"
174
- };
175
- const ENCODE_CHARS_REGEXP = /(?:[^\x21\x23-\x3B\x3D\x3F-\x5F\x61-\x7A\x7C\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g;
176
- const UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g;
177
- const UNMATCHED_SURROGATE_PAIR_REPLACE = "$1\uFFFD$2";
178
- function encodeUrl(url) {
179
- return String(url).replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE).replace(ENCODE_CHARS_REGEXP, encodeURI);
180
- }
181
- // Annotate the CommonJS export names for ESM import in node:
182
- 0 && (module.exports = {
183
- commonTypeMapping,
184
- encodeUrl,
185
- parseSearchParamsToQuery,
186
- statusEmptyMapping,
187
- statusRedirectMapping,
188
- statusTextMapping,
189
- stringifyQueryToString
190
- });