@bepalo/spine 1.0.3
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/LICENSE +21 -0
- package/README.md +830 -0
- package/dist/auth-middlewares.d.ts +119 -0
- package/dist/auth-middlewares.d.ts.map +1 -0
- package/dist/auth-middlewares.js +168 -0
- package/dist/auth-middlewares.js.map +1 -0
- package/dist/cjs/auth-middlewares.d.ts +119 -0
- package/dist/cjs/auth-middlewares.d.ts.map +1 -0
- package/dist/cjs/auth-middlewares.js +168 -0
- package/dist/cjs/auth-middlewares.js.map +1 -0
- package/dist/cjs/helpers.d.ts +180 -0
- package/dist/cjs/helpers.d.ts.map +1 -0
- package/dist/cjs/helpers.js +366 -0
- package/dist/cjs/helpers.js.map +1 -0
- package/dist/cjs/index.d.ts +11 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +30 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/middlewares.d.ts +88 -0
- package/dist/cjs/middlewares.d.ts.map +1 -0
- package/dist/cjs/middlewares.js +262 -0
- package/dist/cjs/middlewares.js.map +1 -0
- package/dist/cjs/parsers.d.ts +177 -0
- package/dist/cjs/parsers.d.ts.map +1 -0
- package/dist/cjs/parsers.js +1021 -0
- package/dist/cjs/parsers.js.map +1 -0
- package/dist/cjs/router.d.ts +124 -0
- package/dist/cjs/router.d.ts.map +1 -0
- package/dist/cjs/router.js +1378 -0
- package/dist/cjs/router.js.map +1 -0
- package/dist/cjs/status.d.ts +77 -0
- package/dist/cjs/status.d.ts.map +1 -0
- package/dist/cjs/status.js +252 -0
- package/dist/cjs/status.js.map +1 -0
- package/dist/cjs/types.d.ts +135 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +109 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/utils.d.ts +11 -0
- package/dist/cjs/utils.d.ts.map +1 -0
- package/dist/cjs/utils.js +76 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/cjs/utils.node.d.ts +4 -0
- package/dist/cjs/utils.node.d.ts.map +1 -0
- package/dist/cjs/utils.node.js +99 -0
- package/dist/cjs/utils.node.js.map +1 -0
- package/dist/helpers.d.ts +180 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +366 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/middlewares.d.ts +88 -0
- package/dist/middlewares.d.ts.map +1 -0
- package/dist/middlewares.js +262 -0
- package/dist/middlewares.js.map +1 -0
- package/dist/parsers.d.ts +177 -0
- package/dist/parsers.d.ts.map +1 -0
- package/dist/parsers.js +1021 -0
- package/dist/parsers.js.map +1 -0
- package/dist/router.d.ts +124 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +1378 -0
- package/dist/router.js.map +1 -0
- package/dist/status.d.ts +77 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +252 -0
- package/dist/status.js.map +1 -0
- package/dist/types.d.ts +135 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +109 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +76 -0
- package/dist/utils.js.map +1 -0
- package/dist/utils.node.d.ts +4 -0
- package/dist/utils.node.d.ts.map +1 -0
- package/dist/utils.node.js +66 -0
- package/dist/utils.node.js.map +1 -0
- package/package.json +57 -0
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.clearCookie = exports.setCookie = exports.send = exports.usp = exports.formData = exports.octetStream = exports.blob = exports.json = exports.html = exports.text = exports.forward = exports.redirect = exports.status = void 0;
|
|
13
|
+
const status_ts_1 = require("./status.js");
|
|
14
|
+
/**
|
|
15
|
+
* Creates a Response with the specified status code.
|
|
16
|
+
* Defaults to 'text/plain; charset=utf-8' content-type if not provided in init.headers.
|
|
17
|
+
* @param {number} status - The HTTP status code
|
|
18
|
+
* @param {string|null} [content] - The response body content
|
|
19
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
20
|
+
* @returns {Response} A Response object
|
|
21
|
+
* @example
|
|
22
|
+
* status(200, "Success");
|
|
23
|
+
* status(404, "Not Found");
|
|
24
|
+
* status(204, null); // No content response
|
|
25
|
+
*/
|
|
26
|
+
const status = (status, content, init) => {
|
|
27
|
+
var _a;
|
|
28
|
+
const statusText = (_a = init === null || init === void 0 ? void 0 : init.statusText) !== null && _a !== void 0 ? _a : (0, status_ts_1.getHttpStatusText)(status);
|
|
29
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
30
|
+
if (content !== null && !headers.has("content-type")) {
|
|
31
|
+
headers.set("content-type", "text/plain; charset=utf-8");
|
|
32
|
+
}
|
|
33
|
+
return new Response(content !== undefined ? content : statusText, Object.assign(Object.assign({ statusText }, init), { status,
|
|
34
|
+
headers }));
|
|
35
|
+
};
|
|
36
|
+
exports.status = status;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a redirect Response.
|
|
39
|
+
* Defaults to 302 Found unless another status is provided.
|
|
40
|
+
* @param {string} location - The URL to redirect to
|
|
41
|
+
* @param {number} [code=302] - The HTTP status code (301, 302, 303, 307, 308)
|
|
42
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
43
|
+
* @returns {Response} A Response object with Location header
|
|
44
|
+
*/
|
|
45
|
+
const redirect = (location, init) => {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 302;
|
|
48
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
49
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
50
|
+
headers.set("Location", location);
|
|
51
|
+
return new Response(null, Object.assign(Object.assign({ statusText }, init), { status,
|
|
52
|
+
headers }));
|
|
53
|
+
};
|
|
54
|
+
exports.redirect = redirect;
|
|
55
|
+
/**
|
|
56
|
+
* Forwards the request to another route internally.
|
|
57
|
+
* Does not send a redirect to the client but changes the path and method,
|
|
58
|
+
* adds X-Forwarded-[Method|Path] and X-Original-Path headers and calls
|
|
59
|
+
* `(this as Router).respond(newReq, ctx)`.
|
|
60
|
+
* NOTE: parse body only once at the first handler using `parseBody({once: true})`
|
|
61
|
+
* as the body will be consumed at the first parseBody call.
|
|
62
|
+
* @param {Router<ExtendContext>} router - The router instance to forward to
|
|
63
|
+
* @param {string} path - The new path to forward to
|
|
64
|
+
* @returns {Response} A Response object with the forwarded request's response
|
|
65
|
+
*/
|
|
66
|
+
const forward = (router, path, options) => {
|
|
67
|
+
return function (ctx) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
var _a;
|
|
70
|
+
const { request } = ctx;
|
|
71
|
+
const method = (_a = options === null || options === void 0 ? void 0 : options.method) !== null && _a !== void 0 ? _a : request.method;
|
|
72
|
+
const headers = new Headers(request.headers);
|
|
73
|
+
const body = request.body ? request.clone().body : undefined;
|
|
74
|
+
const url = new URL(request.url);
|
|
75
|
+
const originalPathname = url.pathname;
|
|
76
|
+
url.pathname = path;
|
|
77
|
+
if (method != request.method)
|
|
78
|
+
headers.set("X-Forwarded-Method", request.method);
|
|
79
|
+
headers.set("X-Forwarded-Path", originalPathname);
|
|
80
|
+
if (!request.headers.has("X-Original-Path")) {
|
|
81
|
+
headers.set("X-Original-Path", originalPathname);
|
|
82
|
+
}
|
|
83
|
+
const newReq = new Request(url.toString(), { method, headers, body });
|
|
84
|
+
return router.respond(newReq, ctx);
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
exports.forward = forward;
|
|
89
|
+
/**
|
|
90
|
+
* Creates a text/plain Response.
|
|
91
|
+
* Defaults to status 200 and 'text/plain; charset=utf-8' content-type if not specified.
|
|
92
|
+
* @param {string} content - The text content to return
|
|
93
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
94
|
+
* @returns {Response} A Response object with text/plain content-type
|
|
95
|
+
* @example
|
|
96
|
+
* text("Hello, world!");
|
|
97
|
+
* text("Error occurred", { status: 500 });
|
|
98
|
+
*/
|
|
99
|
+
const text = (content, init) => {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
102
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
103
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
104
|
+
if (!headers.has("content-type")) {
|
|
105
|
+
headers.set("content-type", "text/plain; charset=utf-8");
|
|
106
|
+
}
|
|
107
|
+
return new Response(content, Object.assign(Object.assign({ statusText }, init), { status,
|
|
108
|
+
headers }));
|
|
109
|
+
};
|
|
110
|
+
exports.text = text;
|
|
111
|
+
/**
|
|
112
|
+
* Creates an HTML Response.
|
|
113
|
+
* Defaults to status 200 and text/html content-type if not specified.
|
|
114
|
+
* @param {string} content - The HTML content to return
|
|
115
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
116
|
+
* @returns {Response} A Response object with text/html content-type
|
|
117
|
+
* @example
|
|
118
|
+
* html("<h1>Hello</h1>");
|
|
119
|
+
* html("<p>Not Found</p>", { status: 404 });
|
|
120
|
+
*/
|
|
121
|
+
const html = (content, init) => {
|
|
122
|
+
var _a, _b;
|
|
123
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
124
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
125
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
126
|
+
if (!headers.has("content-type")) {
|
|
127
|
+
headers.set("content-type", "text/html; charset=utf-8");
|
|
128
|
+
}
|
|
129
|
+
return new Response(content, Object.assign(Object.assign({ statusText }, init), { status,
|
|
130
|
+
headers }));
|
|
131
|
+
};
|
|
132
|
+
exports.html = html;
|
|
133
|
+
/**
|
|
134
|
+
* Creates a JSON Response.
|
|
135
|
+
* Defaults to status 200 and 'application/json; charset=utf-8' content-type if not specified.
|
|
136
|
+
* Uses Response.json() internally which automatically serializes the body.
|
|
137
|
+
* @param {any} body - The data to serialize as JSON
|
|
138
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
139
|
+
* @returns {Response} A Response object with application/json content-type
|
|
140
|
+
* @example
|
|
141
|
+
* json({ message: "Success" });
|
|
142
|
+
* json({ error: "Not found" }, { status: 404 });
|
|
143
|
+
*/
|
|
144
|
+
const json = (body, init) => {
|
|
145
|
+
var _a, _b;
|
|
146
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
147
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
148
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
149
|
+
if (!headers.has("content-type")) {
|
|
150
|
+
headers.set("content-type", "application/json; charset=utf-8");
|
|
151
|
+
}
|
|
152
|
+
return Response.json(body, Object.assign(Object.assign({ statusText }, init), { status,
|
|
153
|
+
headers }));
|
|
154
|
+
};
|
|
155
|
+
exports.json = json;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a Response from a Blob.
|
|
158
|
+
* Automatically sets content-type from blob.type or defaults to application/octet-stream.
|
|
159
|
+
* Also sets content-length header.
|
|
160
|
+
* @param {Blob} blob - The blob data to return
|
|
161
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
162
|
+
* @returns {Response} A Response object with appropriate content-type
|
|
163
|
+
* @example
|
|
164
|
+
* const blob = new Blob(["file content"], { type: "text/plain" });
|
|
165
|
+
* blob(blob);
|
|
166
|
+
*/
|
|
167
|
+
const blob = (blob, init) => {
|
|
168
|
+
var _a, _b;
|
|
169
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
170
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
171
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
172
|
+
if (!headers.has("content-type")) {
|
|
173
|
+
headers.set("content-type", blob.type || "application/octet-stream");
|
|
174
|
+
}
|
|
175
|
+
headers.set("content-length", blob.size.toFixed());
|
|
176
|
+
return new Response(blob, Object.assign(Object.assign({ statusText }, init), { status,
|
|
177
|
+
headers }));
|
|
178
|
+
};
|
|
179
|
+
exports.blob = blob;
|
|
180
|
+
/**
|
|
181
|
+
* Creates a Response from a Blob or ArrayBuffer with application/octet-stream content-type.
|
|
182
|
+
* Forces octet-stream content-type.
|
|
183
|
+
* Also sets content-length header.
|
|
184
|
+
* @param {Blob|ArrayBuffer} octetStream - The blob data to return
|
|
185
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
186
|
+
* @returns {Response} A Response object with application/octet-stream content-type
|
|
187
|
+
* @example
|
|
188
|
+
* const blob = new Blob([binaryData]);
|
|
189
|
+
* octetStream(blob);
|
|
190
|
+
*/
|
|
191
|
+
const octetStream = (octet, init) => {
|
|
192
|
+
var _a, _b;
|
|
193
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
194
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
195
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
196
|
+
if (!headers.has("content-type")) {
|
|
197
|
+
headers.set("content-type", "application/octet-stream");
|
|
198
|
+
}
|
|
199
|
+
if (!(octet instanceof ReadableStream)) {
|
|
200
|
+
headers.set("content-length", (octet instanceof Blob ? octet.size : octet.byteLength).toFixed());
|
|
201
|
+
}
|
|
202
|
+
return new Response(octet, Object.assign(Object.assign({ statusText }, init), { status,
|
|
203
|
+
headers }));
|
|
204
|
+
};
|
|
205
|
+
exports.octetStream = octetStream;
|
|
206
|
+
/**
|
|
207
|
+
* Creates a Response from FormData.
|
|
208
|
+
* @param {FormData} [formData] - The form data to return
|
|
209
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
210
|
+
* @returns {Response} A Response object
|
|
211
|
+
* @example
|
|
212
|
+
* const form = new FormData();
|
|
213
|
+
* form.append("key", "value");
|
|
214
|
+
* formData(form);
|
|
215
|
+
*/
|
|
216
|
+
const formData = (formData, init) => {
|
|
217
|
+
var _a, _b;
|
|
218
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
219
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
220
|
+
return new Response(formData, Object.assign(Object.assign({ statusText }, init), { status }));
|
|
221
|
+
};
|
|
222
|
+
exports.formData = formData;
|
|
223
|
+
/**
|
|
224
|
+
* Creates a Response from URLSearchParams with application/x-www-form-urlencoded content-type.
|
|
225
|
+
* @param {URLSearchParams} [usp] - The URL search parameters to return
|
|
226
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
227
|
+
* @returns {Response} A Response object with application/x-www-form-urlencoded content-type
|
|
228
|
+
* @example
|
|
229
|
+
* const params = new URLSearchParams({ q: "search term" });
|
|
230
|
+
* usp(params);
|
|
231
|
+
*/
|
|
232
|
+
const usp = (usp, init) => {
|
|
233
|
+
var _a, _b;
|
|
234
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
235
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
236
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
237
|
+
if (!headers.has("content-type")) {
|
|
238
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
239
|
+
}
|
|
240
|
+
return new Response(usp, Object.assign(Object.assign({ statusText }, init), { status,
|
|
241
|
+
headers }));
|
|
242
|
+
};
|
|
243
|
+
exports.usp = usp;
|
|
244
|
+
/**
|
|
245
|
+
* Creates a Response from various body types with automatic content-type detection.
|
|
246
|
+
* Supports strings, objects (JSON), Blobs, ArrayBuffers, FormData, URLSearchParams, and ReadableStreams.
|
|
247
|
+
* @param {BodyInit|Record<string, unknown>} [body] - The body content to return
|
|
248
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
249
|
+
* @returns {Response} A Response object with appropriate content-type
|
|
250
|
+
* @example
|
|
251
|
+
* send("text"); // text/plain
|
|
252
|
+
* send({ message: "success" }); // application/json; charset=utf-8
|
|
253
|
+
* send(new Blob([])); // blob.type || application/octet-stream
|
|
254
|
+
* send(new FormData()); // multipart/form-data
|
|
255
|
+
* send(new URLSearchParams()); // application/x-www-form-urlencoded
|
|
256
|
+
*/
|
|
257
|
+
const send = (body, init) => {
|
|
258
|
+
var _a, _b;
|
|
259
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
260
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
261
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
262
|
+
const isContentTypeNotSet = !headers.has("content-type");
|
|
263
|
+
if (body instanceof URLSearchParams) {
|
|
264
|
+
if (isContentTypeNotSet) {
|
|
265
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
else if (body instanceof FormData) {
|
|
269
|
+
// content type will be generated
|
|
270
|
+
}
|
|
271
|
+
else if (typeof body === "string") {
|
|
272
|
+
if (isContentTypeNotSet) {
|
|
273
|
+
headers.set("content-type", "text/plain; charset=utf-8");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
else if (body instanceof Blob) {
|
|
277
|
+
if (isContentTypeNotSet) {
|
|
278
|
+
headers.set("content-type", body.type || "application/octet-stream");
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
else if (body instanceof ArrayBuffer ||
|
|
282
|
+
ArrayBuffer.isView(body) ||
|
|
283
|
+
body instanceof ReadableStream) {
|
|
284
|
+
if (isContentTypeNotSet) {
|
|
285
|
+
headers.set("content-type", "application/octet-stream");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
else if (body != null) {
|
|
289
|
+
if (isContentTypeNotSet) {
|
|
290
|
+
headers.set("content-type", "application/json; charset=utf-8");
|
|
291
|
+
}
|
|
292
|
+
return Response.json(body, Object.assign(Object.assign({ status,
|
|
293
|
+
statusText }, init), { headers }));
|
|
294
|
+
}
|
|
295
|
+
return new Response(body, Object.assign(Object.assign({ statusText }, init), { status,
|
|
296
|
+
headers }));
|
|
297
|
+
};
|
|
298
|
+
exports.send = send;
|
|
299
|
+
/**
|
|
300
|
+
* Creates a Set-Cookie header tuple with the given name, value, and options.
|
|
301
|
+
* @param {string} name - The name of the cookie
|
|
302
|
+
* @param {string} value - The value of the cookie
|
|
303
|
+
* @param {CookieOptions} [options] - Cookie configuration options
|
|
304
|
+
* @returns {CookieTuple} A tuple containing the header name "Set-Cookie" and the cookie string
|
|
305
|
+
* @example
|
|
306
|
+
* const cookie = setCookie("session", "abc123", { httpOnly: true, secure: true });
|
|
307
|
+
* // Returns: ["Set-Cookie", "session=abc123; HttpOnly; Secure"]
|
|
308
|
+
*/
|
|
309
|
+
const setCookie = (name, value, options) => {
|
|
310
|
+
const parts = [`${name}=${value}`];
|
|
311
|
+
if (options) {
|
|
312
|
+
if (options.path)
|
|
313
|
+
parts.push(`Path=${options.path}`);
|
|
314
|
+
if (options.domain)
|
|
315
|
+
parts.push(`Domain=${options.domain}`);
|
|
316
|
+
if (options.expires)
|
|
317
|
+
parts.push(`Expires=${new Date(options.expires).toUTCString()}`);
|
|
318
|
+
if (options.maxAge !== undefined)
|
|
319
|
+
parts.push(`Max-Age=${options.maxAge}`);
|
|
320
|
+
if (options.httpOnly)
|
|
321
|
+
parts.push(`HttpOnly`);
|
|
322
|
+
if (options.secure)
|
|
323
|
+
parts.push(`Secure`);
|
|
324
|
+
if (options.sameSite)
|
|
325
|
+
parts.push(`SameSite=${options.sameSite}`);
|
|
326
|
+
}
|
|
327
|
+
const cookie = parts.join("; ");
|
|
328
|
+
return ["Set-Cookie", cookie];
|
|
329
|
+
};
|
|
330
|
+
exports.setCookie = setCookie;
|
|
331
|
+
/**
|
|
332
|
+
* Creates a Set-Cookie header tuple to clear/remove a cookie.
|
|
333
|
+
* Sets the cookie with an empty value and an expired date.
|
|
334
|
+
* @param {string} name - The name of the cookie to clear
|
|
335
|
+
* @param {CookieOptions} [options] - Cookie configuration options (path/domain must match original cookie)
|
|
336
|
+
* @returns {CookieTuple} A tuple containing the header name "Set-Cookie" and the cookie clearing string
|
|
337
|
+
* @example
|
|
338
|
+
* const cookie = clearCookie("session", { path: "/" });
|
|
339
|
+
* // Returns: ["Set-Cookie", "session=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/"]
|
|
340
|
+
*/
|
|
341
|
+
const clearCookie = (name, options) => {
|
|
342
|
+
const parts = [`${name}=`];
|
|
343
|
+
const expires = (options === null || options === void 0 ? void 0 : options.expires)
|
|
344
|
+
? new Date(options.expires).toUTCString()
|
|
345
|
+
: "Thu, 01 Jan 1970 00:00:00 GMT";
|
|
346
|
+
if (options) {
|
|
347
|
+
if (options.path)
|
|
348
|
+
parts.push(`Path=${options.path}`);
|
|
349
|
+
if (options.domain)
|
|
350
|
+
parts.push(`Domain=${options.domain}`);
|
|
351
|
+
if (options.maxAge !== undefined)
|
|
352
|
+
parts.push(`Max-Age=${options.maxAge}`);
|
|
353
|
+
if (options.httpOnly)
|
|
354
|
+
parts.push(`HttpOnly`);
|
|
355
|
+
if (options.secure)
|
|
356
|
+
parts.push(`Secure`);
|
|
357
|
+
if (options.sameSite)
|
|
358
|
+
parts.push(`SameSite=${options.sameSite}`);
|
|
359
|
+
}
|
|
360
|
+
if (expires)
|
|
361
|
+
parts.push(`Expires=${expires}`);
|
|
362
|
+
const cookie = parts.join("; ");
|
|
363
|
+
return ["Set-Cookie", cookie];
|
|
364
|
+
};
|
|
365
|
+
exports.clearCookie = clearCookie;
|
|
366
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,2CAAgD;AAGhD;;;;;;;;;;;GAWG;AACI,MAAM,MAAM,GAAG,CACpB,MAAc,EACd,OAAuB,EACvB,IAAmB,EACT,EAAE;;IACZ,MAAM,UAAU,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,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AI,CAAC,IAAI,CAAC,CAAC;IAChC,OAAO,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;AAChC,CAAC,CAAC;AAnBW,QAAA,WAAW,eAmBtB"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./types.ts";
|
|
2
|
+
export * from "./utils.node.ts";
|
|
3
|
+
export * from "./status.ts";
|
|
4
|
+
export * from "./helpers.ts";
|
|
5
|
+
export * from "./parsers.ts";
|
|
6
|
+
export * from "./auth-middlewares.ts";
|
|
7
|
+
export * from "./middlewares.ts";
|
|
8
|
+
export * from "./router.ts";
|
|
9
|
+
import Router from "./router.ts";
|
|
10
|
+
export default Router;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAE5B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,eAAe,MAAM,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./types.js"), exports);
|
|
21
|
+
__exportStar(require("./utils.node.js"), exports);
|
|
22
|
+
__exportStar(require("./status.js"), exports);
|
|
23
|
+
__exportStar(require("./helpers.js"), exports);
|
|
24
|
+
__exportStar(require("./parsers.js"), exports);
|
|
25
|
+
__exportStar(require("./auth-middlewares.js"), exports);
|
|
26
|
+
__exportStar(require("./middlewares.js"), exports);
|
|
27
|
+
__exportStar(require("./router.js"), exports);
|
|
28
|
+
const router_ts_1 = __importDefault(require("./router.js"));
|
|
29
|
+
exports.default = router_ts_1.default;
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,kDAAgC;AAChC,8CAA4B;AAC5B,+CAA6B;AAC7B,+CAA6B;AAC7B,wDAAsC;AACtC,mDAAiC;AACjC,8CAA4B;AAE5B,4DAAiC;AACjC,kBAAe,mBAAM,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type Context, type Handler, type HttpMethod } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a rate limiting middleware using token bucket algorithm.
|
|
4
|
+
* Supports both fixed interval refill and continuous rate-based refill.
|
|
5
|
+
*
|
|
6
|
+
* @template {Record<string, unknown>} ExtendContext - Extend Router Context
|
|
7
|
+
* @param {Object} config - Rate limiting configuration
|
|
8
|
+
* @param {Function} [config.key] - Function to generate cache key from request and context
|
|
9
|
+
* @param {number} [config.maxTokens] - Maximum number of tokens in the bucket
|
|
10
|
+
* @param {number} [config.refillInterval] - Fixed interval in seconds for token refill
|
|
11
|
+
* @param {number} [config.refillRate] - Continuous refill rate in tokens per second
|
|
12
|
+
* @param {number} [config.cleanUpInterval] - Interval in seconds for cleanup timer
|
|
13
|
+
* @param {number} [config.cleanUpIdleDelay] - Time in seconds to delay cleanup of filled token buckets
|
|
14
|
+
* @param {boolean} [config.setXRateLimitHeaders=false] - Whether to set X-RateLimit headers in response
|
|
15
|
+
* @returns {Function} Middleware function that enforces rate limits
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Fixed interval rate limiting (10 requests per minute)
|
|
19
|
+
* const rateLimiter = limitRate({
|
|
20
|
+
* key: (ctx) => ctx.clientId, // IP-based limiting
|
|
21
|
+
* refillInterval: 60, // 1 minute
|
|
22
|
+
* refillRate: 10, // 10 tokens per interval
|
|
23
|
+
* maxTokens: 10,
|
|
24
|
+
* setXRateLimitHeaders: true
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Continuous rate limiting (100 requests per hour)
|
|
29
|
+
* const rateLimiter = limitRate({
|
|
30
|
+
* key: (ctx) => ctx.clientId || 'anonymous',
|
|
31
|
+
* refillRate: 60, // 100 tokens per 60 seconds
|
|
32
|
+
* maxTokens: 100,
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* @throws {RouterError} If neither refillInterval nor refillRate is provided
|
|
36
|
+
*/
|
|
37
|
+
export declare const limitRate: <ExtendContext extends Record<string, unknown> = Record<string, never>>(config: {
|
|
38
|
+
key: (ctx: Context<ExtendContext>) => string | Promise<string>;
|
|
39
|
+
maxTokens: number;
|
|
40
|
+
refillInterval?: number;
|
|
41
|
+
refillRate?: number;
|
|
42
|
+
cleanUpInterval?: number;
|
|
43
|
+
cleanUpIdleDelay?: number;
|
|
44
|
+
setXRateLimitHeaders?: boolean;
|
|
45
|
+
breakPipeline?: boolean;
|
|
46
|
+
}) => Handler<ExtendContext>;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a CORS (Cross-Origin Resource Sharing) middleware.
|
|
49
|
+
* Supports preflight requests and configurable CORS headers.
|
|
50
|
+
*
|
|
51
|
+
* @template {Record<string, unknown>} ExtendContext - Extend Router Context
|
|
52
|
+
* @param {Object} [config] - CORS configuration
|
|
53
|
+
* @param {string|string[]|"*"} [config.origins="*"] - Allowed origins (wildcard "*", single origin, or array)
|
|
54
|
+
* @param {HttpMethod[]} [config.methods=["GET","HEAD","PUT","PATCH","POST","DELETE"]] - Allowed HTTP methods
|
|
55
|
+
* @param {string[]} [config.allowedHeaders=["Content-Type","Authorization"]] - Allowed request headers
|
|
56
|
+
* @param {string[]} [config.exposedHeaders] - Headers exposed to the browser
|
|
57
|
+
* @param {boolean} [config.credentials=false] - Allow credentials (cookies, authorization headers)
|
|
58
|
+
* @param {number} [config.maxAge=86400] - Maximum age for preflight cache in seconds
|
|
59
|
+
* @param {boolean} [config.varyOrigin=false] - Add Vary: Origin header for caching
|
|
60
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipeline flow per handler type after success.
|
|
61
|
+
* @returns {Function} Middleware function that handles CORS headers
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // Basic CORS with all defaults
|
|
65
|
+
* const corsMiddleware = cors();
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // Specific origins with credentials
|
|
69
|
+
* const corsMiddleware = cors({
|
|
70
|
+
* origins: ["https://example.com", "https://api.example.com"],
|
|
71
|
+
* credentials: true,
|
|
72
|
+
* methods: ["GET", "POST", "PUT", "DELETE"],
|
|
73
|
+
* allowedHeaders: ["Content-Type", "Authorization", "X-Custom-Header"]
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* @throws {HttpError} If credentials is true with wildcard origin ("*")
|
|
77
|
+
*/
|
|
78
|
+
export declare const cors: <ExtendContext extends Record<string, unknown> = Record<string, never>>(config?: {
|
|
79
|
+
origins: "*" | string | string[];
|
|
80
|
+
methods?: HttpMethod[] | null;
|
|
81
|
+
allowedHeaders?: string[] | null;
|
|
82
|
+
exposedHeaders?: string[] | null;
|
|
83
|
+
credentials?: boolean | null;
|
|
84
|
+
maxAge?: number | null;
|
|
85
|
+
varyOrigin?: boolean;
|
|
86
|
+
breakPipeline?: boolean;
|
|
87
|
+
}) => Handler<ExtendContext>;
|
|
88
|
+
//# sourceMappingURL=middlewares.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middlewares.d.ts","sourceRoot":"","sources":["../src/middlewares.ts"],"names":[],"mappings":"AACA,OAAO,EAKL,KAAK,OAAO,EACZ,KAAK,OAAO,EACZ,KAAK,UAAU,EAChB,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,SAAS,GACpB,aAAa,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EACrE,QAAQ;IACR,GAAG,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,aAAa,CAAC,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/D,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,KAAG,OAAO,CAAC,aAAa,CA4HxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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