@tinyhttp/app 2.3.1 → 2.4.0
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/app.d.ts +9 -9
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +377 -0
- package/dist/app.js.map +1 -0
- package/dist/extend.d.ts +4 -7
- package/dist/extend.d.ts.map +1 -1
- package/dist/extend.js +63 -0
- package/dist/extend.js.map +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +7 -464
- package/dist/index.js.map +1 -1
- package/dist/onError.d.ts +4 -5
- package/dist/onError.js +15 -0
- package/dist/onError.js.map +1 -0
- package/dist/request.d.ts +17 -12
- package/dist/request.d.ts.map +1 -1
- package/dist/request.js +85 -0
- package/dist/request.js.map +1 -0
- package/dist/response.d.ts +7 -8
- package/dist/response.d.ts.map +1 -1
- package/dist/response.js +9 -0
- package/dist/response.js.map +1 -0
- package/dist/types.d.ts +8 -8
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/view.d.ts +11 -4
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +94 -0
- package/dist/view.js.map +1 -0
- package/package.json +5 -6
package/dist/app.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Server } from 'node:http';
|
|
2
|
-
import { Handler, Middleware, NextFunction, UseMethodParams
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
1
|
+
import { type Server } from 'node:http';
|
|
2
|
+
import type { Handler, Middleware, NextFunction, UseMethodParams } from '@tinyhttp/router';
|
|
3
|
+
import { Router } from '@tinyhttp/router';
|
|
4
|
+
import type { TemplateEngineOptions } from './index.js';
|
|
5
|
+
import type { ErrorHandler } from './onError.js';
|
|
6
|
+
import type { Request } from './request.js';
|
|
7
|
+
import type { Response } from './response.js';
|
|
8
|
+
import type { AppConstructor, AppRenderOptions, AppSettings, TemplateEngine } from './types.js';
|
|
9
9
|
/**
|
|
10
10
|
* `App` class - the starting point of tinyhttp app.
|
|
11
11
|
*
|
|
@@ -36,7 +36,7 @@ export declare class App<Req extends Request = Request, Res extends Response = R
|
|
|
36
36
|
onError: ErrorHandler;
|
|
37
37
|
settings: AppSettings;
|
|
38
38
|
engines: Record<string, TemplateEngine>;
|
|
39
|
-
applyExtensions
|
|
39
|
+
applyExtensions?: Handler;
|
|
40
40
|
attach: (req: Req, res: Res, next?: NextFunction) => void;
|
|
41
41
|
cache: Record<string, unknown>;
|
|
42
42
|
constructor(options?: AppConstructor<Req, Res>);
|
package/dist/app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,WAAW,CAAA;AAErD,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC1F,OAAO,EAAE,MAAM,EAAkB,MAAM,kBAAkB,CAAA;AAGzD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAGhD,OAAO,KAAK,EAAE,OAAO,EAAa,MAAM,cAAc,CAAA;AACtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,WAAW,CAAA;AAErD,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,YAAY,EAA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MAAM,GAAG,MAAM;CAG9D"}
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _App_instances, _App_find;
|
|
7
|
+
import { createServer } from 'node:http';
|
|
8
|
+
import { getPathname } from '@tinyhttp/req';
|
|
9
|
+
import { Router, pushMiddleware } from '@tinyhttp/router';
|
|
10
|
+
import { parse as rg } from 'regexparam';
|
|
11
|
+
import { extendMiddleware } from './extend.js';
|
|
12
|
+
import { onErrorHandler } from './onError.js';
|
|
13
|
+
import { getURLParams } from './request.js';
|
|
14
|
+
import { View } from './view.js';
|
|
15
|
+
/**
|
|
16
|
+
* Add leading slash if not present (e.g. path -> /path, /path -> /path)
|
|
17
|
+
* @param x
|
|
18
|
+
*/
|
|
19
|
+
const lead = (x) => (x.charCodeAt(0) === 47 ? x : `/${x}`);
|
|
20
|
+
const trail = (x) => (x.charCodeAt(x.length - 1) === 47 ? x.substring(0, x.length - 1) : x);
|
|
21
|
+
const mount = (fn) => (fn instanceof App ? fn.attach : fn);
|
|
22
|
+
const applyHandler = (h) => async (req, res, next) => {
|
|
23
|
+
try {
|
|
24
|
+
if (h[Symbol.toStringTag] === 'AsyncFunction') {
|
|
25
|
+
await h(req, res, next);
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
h(req, res, next);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
next(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* `App` class - the starting point of tinyhttp app.
|
|
36
|
+
*
|
|
37
|
+
* With the `App` you can:
|
|
38
|
+
* * use routing methods and `.use(...)`
|
|
39
|
+
* * set no match (404) and error (500) handlers
|
|
40
|
+
* * configure template engines
|
|
41
|
+
* * store data in locals
|
|
42
|
+
* * listen the http server on a specified port
|
|
43
|
+
*
|
|
44
|
+
* In case you use TypeScript, you can pass custom types to this class because it is also a generic class.
|
|
45
|
+
*
|
|
46
|
+
* Example:
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* interface CoolReq extends Request {
|
|
50
|
+
* genericsAreDope: boolean
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* const app = App<any, CoolReq, Response>()
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export class App extends Router {
|
|
57
|
+
constructor(options = {}) {
|
|
58
|
+
super();
|
|
59
|
+
_App_instances.add(this);
|
|
60
|
+
this.middleware = [];
|
|
61
|
+
this.locals = {};
|
|
62
|
+
this.engines = {};
|
|
63
|
+
this.onError = (options === null || options === void 0 ? void 0 : options.onError) || onErrorHandler;
|
|
64
|
+
// @ts-expect-error typescript is not smart enough to understand "this" ts(2345)
|
|
65
|
+
this.noMatchHandler = (options === null || options === void 0 ? void 0 : options.noMatchHandler) || this.onError.bind(this, { code: 404 });
|
|
66
|
+
this.settings = {
|
|
67
|
+
view: View,
|
|
68
|
+
xPoweredBy: true,
|
|
69
|
+
views: `${process.cwd()}/views`,
|
|
70
|
+
'view cache': process.env.NODE_ENV === 'production',
|
|
71
|
+
'trust proxy': 0,
|
|
72
|
+
...options.settings
|
|
73
|
+
};
|
|
74
|
+
if (options.applyExtensions)
|
|
75
|
+
this.applyExtensions = options === null || options === void 0 ? void 0 : options.applyExtensions;
|
|
76
|
+
const boundHandler = this.handler.bind(this);
|
|
77
|
+
this.attach = (req, res, next) => setImmediate(boundHandler, req, res, next);
|
|
78
|
+
this.cache = {};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Set app setting
|
|
82
|
+
* @param setting setting name
|
|
83
|
+
* @param value setting value
|
|
84
|
+
*/
|
|
85
|
+
set(setting, value) {
|
|
86
|
+
this.settings[setting] = value;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Enable app setting
|
|
91
|
+
* @param setting Setting name
|
|
92
|
+
*/
|
|
93
|
+
enable(setting) {
|
|
94
|
+
this.settings[setting] = true;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check if setting is enabled
|
|
99
|
+
* @param setting Setting name
|
|
100
|
+
* @returns
|
|
101
|
+
*/
|
|
102
|
+
enabled(setting) {
|
|
103
|
+
return Boolean(this.settings[setting]);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Disable app setting
|
|
107
|
+
* @param setting Setting name
|
|
108
|
+
*/
|
|
109
|
+
disable(setting) {
|
|
110
|
+
this.settings[setting] = false;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Return the app's absolute pathname
|
|
115
|
+
* based on the parent(s) that have
|
|
116
|
+
* mounted it.
|
|
117
|
+
*
|
|
118
|
+
* For example if the application was
|
|
119
|
+
* mounted as `"/admin"`, which itself
|
|
120
|
+
* was mounted as `"/blog"` then the
|
|
121
|
+
* return value would be `"/blog/admin"`.
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
124
|
+
path() {
|
|
125
|
+
return this.parent ? this.parent.path() + this.mountpath : '';
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Register a template engine with extension
|
|
129
|
+
*/
|
|
130
|
+
engine(ext, fn) {
|
|
131
|
+
this.engines[ext[0] === '.' ? ext : `.${ext}`] = fn;
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Render a template
|
|
136
|
+
* @param name What to render
|
|
137
|
+
* @param data data that is passed to a template
|
|
138
|
+
* @param options Template engine options
|
|
139
|
+
* @param cb Callback that consumes error and html
|
|
140
|
+
*/
|
|
141
|
+
render(name, data = {}, options = {}, cb = () => { }) {
|
|
142
|
+
let view;
|
|
143
|
+
const { _locals, ...opts } = options;
|
|
144
|
+
let locals = this.locals;
|
|
145
|
+
if (_locals)
|
|
146
|
+
locals = { ...locals, ..._locals };
|
|
147
|
+
locals = { ...locals, ...data };
|
|
148
|
+
if (opts.cache == null)
|
|
149
|
+
opts.cache = this.enabled('view cache');
|
|
150
|
+
if (opts.cache) {
|
|
151
|
+
view = this.cache[name];
|
|
152
|
+
}
|
|
153
|
+
if (!view) {
|
|
154
|
+
const View = this.settings.view;
|
|
155
|
+
view = new View(name, {
|
|
156
|
+
defaultEngine: this.settings['view engine'],
|
|
157
|
+
root: this.settings.views,
|
|
158
|
+
engines: this.engines
|
|
159
|
+
});
|
|
160
|
+
if (!view.path) {
|
|
161
|
+
const dirs = Array.isArray(view.root) && view.root.length > 1
|
|
162
|
+
? `directories "${view.root.slice(0, -1).join('", "')}" or "${view.root[view.root.length - 1]}"`
|
|
163
|
+
: `directory "${view.root}"`;
|
|
164
|
+
const err = new Error(`Failed to lookup view "${name}" in views ${dirs}`);
|
|
165
|
+
return cb(err);
|
|
166
|
+
}
|
|
167
|
+
if (opts.cache) {
|
|
168
|
+
this.cache[name] = view;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
view.render(opts, locals, cb);
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
cb(err);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
use(...args) {
|
|
179
|
+
var _a;
|
|
180
|
+
const base = args[0];
|
|
181
|
+
const fns = args.slice(1).flat();
|
|
182
|
+
let pathArray = [];
|
|
183
|
+
if (typeof base === 'function' || base instanceof App) {
|
|
184
|
+
fns.unshift(base);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
// if base is not an array of paths, then convert it to an array.
|
|
188
|
+
let basePaths = [];
|
|
189
|
+
if (Array.isArray(base))
|
|
190
|
+
basePaths = base;
|
|
191
|
+
else if (typeof base === 'string')
|
|
192
|
+
basePaths = [base];
|
|
193
|
+
basePaths = basePaths.filter((element) => {
|
|
194
|
+
if (typeof element === 'string') {
|
|
195
|
+
pathArray.push(element);
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
return true;
|
|
199
|
+
});
|
|
200
|
+
fns.unshift(...basePaths);
|
|
201
|
+
}
|
|
202
|
+
pathArray = pathArray.length ? pathArray.map((path) => lead(path)) : ['/'];
|
|
203
|
+
const mountpath = pathArray.join(', ');
|
|
204
|
+
let regex;
|
|
205
|
+
for (const fn of fns) {
|
|
206
|
+
if (fn instanceof App) {
|
|
207
|
+
for (const path of pathArray) {
|
|
208
|
+
regex = rg(path, true);
|
|
209
|
+
fn.mountpath = mountpath;
|
|
210
|
+
this.apps[path] = fn;
|
|
211
|
+
// @ts-expect-error typescript is not smart enough to understand "this" ts(2345)
|
|
212
|
+
fn.parent = this;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
for (const path of pathArray) {
|
|
217
|
+
const handlerPaths = [];
|
|
218
|
+
const handlerFunctions = [];
|
|
219
|
+
const handlerPathBase = path === '/' ? '' : lead(path);
|
|
220
|
+
for (const fn of fns) {
|
|
221
|
+
if (fn instanceof App && ((_a = fn.middleware) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
222
|
+
for (const mw of fn.middleware) {
|
|
223
|
+
handlerPaths.push(handlerPathBase + lead(mw.path));
|
|
224
|
+
handlerFunctions.push(fn);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
handlerPaths.push('');
|
|
229
|
+
handlerFunctions.push(fn);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
pushMiddleware(this.middleware)({
|
|
233
|
+
path,
|
|
234
|
+
regex,
|
|
235
|
+
type: 'mw',
|
|
236
|
+
handler: mount(handlerFunctions[0]),
|
|
237
|
+
handlers: handlerFunctions.slice(1).map(mount),
|
|
238
|
+
fullPaths: handlerPaths
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
route(path) {
|
|
244
|
+
const app = new App({ settings: this.settings });
|
|
245
|
+
this.use(path, app);
|
|
246
|
+
return app;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Extends Req / Res objects, pushes 404 and 500 handlers, dispatches middleware
|
|
250
|
+
* @param req Req object
|
|
251
|
+
* @param res Res object
|
|
252
|
+
* @param next 'Next' function
|
|
253
|
+
*/
|
|
254
|
+
handler(req, res, next) {
|
|
255
|
+
/* Set X-Powered-By header */
|
|
256
|
+
const { xPoweredBy } = this.settings;
|
|
257
|
+
if (xPoweredBy)
|
|
258
|
+
res.setHeader('X-Powered-By', typeof xPoweredBy === 'string' ? xPoweredBy : 'tinyhttp');
|
|
259
|
+
// @ts-expect-error typescript is not smart enough to understand "this" ts(2345)
|
|
260
|
+
const exts = this.applyExtensions || extendMiddleware(this);
|
|
261
|
+
let mw = [
|
|
262
|
+
{
|
|
263
|
+
handler: exts,
|
|
264
|
+
type: 'mw',
|
|
265
|
+
path: '/'
|
|
266
|
+
}
|
|
267
|
+
];
|
|
268
|
+
req.baseUrl = '';
|
|
269
|
+
const handle = (mw, pathname) => async (req, res, next) => {
|
|
270
|
+
var _a;
|
|
271
|
+
const { path, handler, regex } = mw;
|
|
272
|
+
let params;
|
|
273
|
+
try {
|
|
274
|
+
params = regex ? getURLParams(regex, pathname) : {};
|
|
275
|
+
}
|
|
276
|
+
catch (e) {
|
|
277
|
+
console.error(e);
|
|
278
|
+
if (e instanceof URIError)
|
|
279
|
+
return res.sendStatus(400);
|
|
280
|
+
throw e;
|
|
281
|
+
}
|
|
282
|
+
// Warning: users should not use :wild as a pattern
|
|
283
|
+
let prefix = path;
|
|
284
|
+
if (regex) {
|
|
285
|
+
for (const key of regex.keys) {
|
|
286
|
+
if (key === 'wild') {
|
|
287
|
+
prefix = prefix.replace('*', params.wild);
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
prefix = prefix.replace(`:${key}`, params[key]);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
req.params = { ...req.params, ...params };
|
|
295
|
+
if (mw.type === 'mw') {
|
|
296
|
+
req.url = lead(req.originalUrl.substring(prefix.length));
|
|
297
|
+
req.baseUrl = trail(req.originalUrl.substring(0, prefix.length));
|
|
298
|
+
}
|
|
299
|
+
if (!req.path)
|
|
300
|
+
req.path = pathname;
|
|
301
|
+
if ((_a = this.settings) === null || _a === void 0 ? void 0 : _a.enableReqRoute)
|
|
302
|
+
req.route = mw;
|
|
303
|
+
await applyHandler(handler)(req, res, next);
|
|
304
|
+
};
|
|
305
|
+
let idx = 0;
|
|
306
|
+
const loop = () => {
|
|
307
|
+
req.originalUrl = req.baseUrl + req.url;
|
|
308
|
+
const pathname = getPathname(req.url);
|
|
309
|
+
const matched = __classPrivateFieldGet(this, _App_instances, "m", _App_find).call(this, pathname).filter((x) => (req.method === 'HEAD' || (x.method ? x.method === req.method : true)) && !mw.includes(x));
|
|
310
|
+
if (matched.length && matched[0] !== null) {
|
|
311
|
+
if (idx !== 0) {
|
|
312
|
+
idx = mw.length;
|
|
313
|
+
req.params = {};
|
|
314
|
+
}
|
|
315
|
+
mw = [
|
|
316
|
+
...mw,
|
|
317
|
+
...matched,
|
|
318
|
+
{
|
|
319
|
+
type: 'mw',
|
|
320
|
+
handler: (req, res, next) => {
|
|
321
|
+
if (req.method === 'HEAD') {
|
|
322
|
+
res.statusCode = 204;
|
|
323
|
+
return res.end('');
|
|
324
|
+
}
|
|
325
|
+
next();
|
|
326
|
+
},
|
|
327
|
+
path: '/'
|
|
328
|
+
}
|
|
329
|
+
];
|
|
330
|
+
}
|
|
331
|
+
else if (this.parent == null) {
|
|
332
|
+
mw.push({
|
|
333
|
+
handler: this.noMatchHandler,
|
|
334
|
+
type: 'route',
|
|
335
|
+
path: '/'
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
void handle(mw[idx++], pathname)(req, res, next);
|
|
339
|
+
};
|
|
340
|
+
const parentNext = next;
|
|
341
|
+
next = (err) => {
|
|
342
|
+
if (err != null) {
|
|
343
|
+
// @ts-expect-error The 'this' context of type 'this' is not assignable to method's 'this' of type 'App<Request, Response<unknown>>' ts(2345)
|
|
344
|
+
return this.onError(err, req, res);
|
|
345
|
+
}
|
|
346
|
+
if (res.writableEnded)
|
|
347
|
+
return;
|
|
348
|
+
if (idx >= mw.length) {
|
|
349
|
+
if (parentNext != null)
|
|
350
|
+
parentNext();
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
loop();
|
|
354
|
+
};
|
|
355
|
+
loop();
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Creates HTTP server and dispatches middleware
|
|
359
|
+
* @param port server listening port
|
|
360
|
+
* @param cb callback to be invoked after server starts listening
|
|
361
|
+
* @param host server listening host
|
|
362
|
+
*/
|
|
363
|
+
listen(port, cb, host) {
|
|
364
|
+
return createServer().on('request', this.attach).listen(port, host, cb);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
_App_instances = new WeakSet(), _App_find = function _App_find(url) {
|
|
368
|
+
return this.middleware.filter((m) => {
|
|
369
|
+
m.regex = m.regex || rg(m.path, m.type === 'mw');
|
|
370
|
+
let fullPathRegex;
|
|
371
|
+
m.fullPath && typeof m.fullPath === 'string'
|
|
372
|
+
? (fullPathRegex = rg(m.fullPath, m.type === 'mw'))
|
|
373
|
+
: (fullPathRegex = null);
|
|
374
|
+
return m.regex.pattern.test(url) && (m.type === 'mw' && fullPathRegex ? fullPathRegex.pattern.test(url) : true);
|
|
375
|
+
});
|
|
376
|
+
};
|
|
377
|
+
//# sourceMappingURL=app.js.map
|
package/dist/app.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names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dist/extend.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { Response } from './response.js';
|
|
5
|
-
import { TemplateEngineOptions } from './types.js';
|
|
6
|
-
|
|
1
|
+
import type { App } from './app.js';
|
|
2
|
+
import type { Handler } from './index.js';
|
|
3
|
+
import type { TemplateEngineOptions } from './types.js';
|
|
7
4
|
/**
|
|
8
5
|
* Extends Request and Response objects with custom properties and methods
|
|
9
6
|
*/
|
|
10
|
-
export declare const extendMiddleware: <EngineOptions extends TemplateEngineOptions = TemplateEngineOptions>(app: App) =>
|
|
7
|
+
export declare const extendMiddleware: <EngineOptions extends TemplateEngineOptions = TemplateEngineOptions>(app: App) => Handler;
|
|
11
8
|
//# sourceMappingURL=extend.d.ts.map
|
package/dist/extend.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extend.d.ts","sourceRoot":"","sources":["../src/extend.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"extend.d.ts","sourceRoot":"","sources":["../src/extend.ts"],"names":[],"mappings":"AAiCA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AACnC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAKzC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAEvD;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,aAAa,SAAS,qBAAqB,+BAA+B,GAAG,KA8DtG,OAAO,CAAA"}
|
package/dist/extend.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { compile } from '@tinyhttp/proxy-addr';
|
|
2
|
+
import { checkIfXMLHttpRequest, getAccepts, getAcceptsCharsets, getAcceptsEncodings, getAcceptsLanguages, getFreshOrStale, getQueryParams, getRangeFromHeader, getRequestHeader } from '@tinyhttp/req';
|
|
3
|
+
import { append, attachment, clearCookie, download, formatResponse, getResponseHeader, json, redirect, send, sendFile, sendStatus, setContentType, setCookie, setHeader, setLinksHeader, setLocationHeader, setVaryHeader, status } from '@tinyhttp/res';
|
|
4
|
+
import { getSubdomains } from './request.js';
|
|
5
|
+
import { getHost, getIP, getIPs, getProtocol } from './request.js';
|
|
6
|
+
import { renderTemplate } from './response.js';
|
|
7
|
+
/**
|
|
8
|
+
* Extends Request and Response objects with custom properties and methods
|
|
9
|
+
*/
|
|
10
|
+
export const extendMiddleware = (app) => ((req, res, next) => {
|
|
11
|
+
const { settings } = app;
|
|
12
|
+
res.get = getResponseHeader(res);
|
|
13
|
+
req.get = getRequestHeader(req);
|
|
14
|
+
if (settings === null || settings === void 0 ? void 0 : settings.bindAppToReqRes) {
|
|
15
|
+
req.app = app;
|
|
16
|
+
res.app = app;
|
|
17
|
+
}
|
|
18
|
+
if (settings === null || settings === void 0 ? void 0 : settings.networkExtensions) {
|
|
19
|
+
let trust = settings === null || settings === void 0 ? void 0 : settings['trust proxy'];
|
|
20
|
+
if (typeof trust !== 'function') {
|
|
21
|
+
trust = compile(trust);
|
|
22
|
+
settings['trust proxy'] = trust;
|
|
23
|
+
}
|
|
24
|
+
req.protocol = getProtocol(req, trust);
|
|
25
|
+
req.secure = req.protocol === 'https';
|
|
26
|
+
const host = getHost(req, trust);
|
|
27
|
+
req.hostname = host === null || host === void 0 ? void 0 : host.hostname;
|
|
28
|
+
req.port = host === null || host === void 0 ? void 0 : host.port;
|
|
29
|
+
req.subdomains = getSubdomains(req, trust, settings.subdomainOffset);
|
|
30
|
+
req.ip = getIP(req, trust);
|
|
31
|
+
req.ips = getIPs(req, trust);
|
|
32
|
+
}
|
|
33
|
+
req.query = getQueryParams(req.url);
|
|
34
|
+
req.range = getRangeFromHeader(req);
|
|
35
|
+
req.accepts = getAccepts(req);
|
|
36
|
+
req.acceptsCharsets = getAcceptsCharsets(req);
|
|
37
|
+
req.acceptsEncodings = getAcceptsEncodings(req);
|
|
38
|
+
req.acceptsLanguages = getAcceptsLanguages(req);
|
|
39
|
+
req.xhr = checkIfXMLHttpRequest(req);
|
|
40
|
+
res.header = res.set = setHeader(res);
|
|
41
|
+
res.send = send(req, res);
|
|
42
|
+
res.json = json(res);
|
|
43
|
+
res.status = status(res);
|
|
44
|
+
res.sendStatus = sendStatus(req, res);
|
|
45
|
+
res.sendFile = sendFile(req, res);
|
|
46
|
+
res.type = setContentType(res);
|
|
47
|
+
res.location = setLocationHeader(req, res);
|
|
48
|
+
res.links = setLinksHeader(res);
|
|
49
|
+
res.vary = setVaryHeader(res);
|
|
50
|
+
res.cookie = setCookie(req, res);
|
|
51
|
+
res.clearCookie = clearCookie(req, res);
|
|
52
|
+
res.render = renderTemplate(req, res, app);
|
|
53
|
+
res.format = formatResponse(req, res, next);
|
|
54
|
+
res.redirect = redirect(req, res, next);
|
|
55
|
+
res.attachment = attachment(res);
|
|
56
|
+
res.download = download(req, res);
|
|
57
|
+
res.append = append(res);
|
|
58
|
+
res.locals = res.locals || Object.create(null);
|
|
59
|
+
Object.defineProperty(req, 'fresh', { get: getFreshOrStale.bind(null, req, res), configurable: true });
|
|
60
|
+
req.stale = !req.fresh;
|
|
61
|
+
next();
|
|
62
|
+
});
|
|
63
|
+
//# sourceMappingURL=extend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extend.js","sourceRoot":"","sources":["../src/extend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAC9C,OAAO,EACL,qBAAqB,EACrB,UAAU,EACV,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,eAAe,EACf,cAAc,EACd,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,eAAe,CAAA;AACtB,OAAO,EACL,MAAM,EACN,UAAU,EACV,WAAW,EACX,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,IAAI,EACJ,QAAQ,EACR,IAAI,EACJ,QAAQ,EACR,UAAU,EACV,cAAc,E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AI,EAAE,CAAC,CAAA;IACtG,GAAG,CAAC,KAAK,GAAG,CAAC,GAAG,CAAC,KAAK,CAAA;IAEtB,IAAI,EAAE,CAAA;AACR,CAAC,CAAY,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { Middleware, NextFunction, AsyncHandler as RAsyncHandler, Handler as RHandler, SyncHandler as RSyncHandler } from '@tinyhttp/router';
|
|
2
|
-
import { Request } from './request.js';
|
|
3
|
-
import { Response } from './response.js';
|
|
4
1
|
export { App } from './app.js';
|
|
5
2
|
export * from './request.js';
|
|
6
3
|
export * from './response.js';
|
|
@@ -8,6 +5,9 @@ export { extendMiddleware } from './extend.js';
|
|
|
8
5
|
export { onErrorHandler, type ErrorHandler } from './onError.js';
|
|
9
6
|
export { View } from './view.js';
|
|
10
7
|
export type { AppSettings, TemplateEngineOptions, TemplateEngine, AppConstructor } from './types.js';
|
|
8
|
+
import type { Middleware, NextFunction, AsyncHandler as RAsyncHandler, Handler as RHandler, SyncHandler as RSyncHandler } from '@tinyhttp/router';
|
|
9
|
+
import type { Request } from './request.js';
|
|
10
|
+
import type { Response } from './response.js';
|
|
11
11
|
export type Handler = RHandler<Request, Response>;
|
|
12
12
|
export type AsyncHandler = RAsyncHandler<Request, Response>;
|
|
13
13
|
export type SyncHandler = RSyncHandler<Request, Response>;
|