@zero-server/auth 0.9.1 → 0.9.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 -21
- package/index.d.ts +1 -1
- package/index.js +26 -26
- package/lib/auth/authorize.js +399 -0
- package/lib/auth/enrollment.js +367 -0
- package/lib/auth/index.js +57 -0
- package/lib/auth/jwt.js +731 -0
- package/lib/auth/oauth.js +362 -0
- package/lib/auth/session.js +588 -0
- package/lib/auth/trustedDevice.js +409 -0
- package/lib/auth/twoFactor.js +1150 -0
- package/lib/auth/webauthn.js +946 -0
- package/lib/debug.js +372 -0
- package/package.json +12 -2
- package/types/app.d.ts +223 -0
- package/types/auth.d.ts +520 -0
- package/types/body.d.ts +14 -0
- package/types/cli.d.ts +2 -0
- package/types/cluster.d.ts +75 -0
- package/types/env.d.ts +80 -0
- package/types/errors.d.ts +316 -0
- package/types/fetch.d.ts +43 -0
- package/types/grpc.d.ts +432 -0
- package/types/index.d.ts +384 -0
- package/types/lifecycle.d.ts +60 -0
- package/types/middleware.d.ts +320 -0
- package/types/observe.d.ts +304 -0
- package/types/orm.d.ts +1887 -0
- package/types/request.d.ts +109 -0
- package/types/response.d.ts +157 -0
- package/types/router.d.ts +78 -0
- package/types/sse.d.ts +78 -0
- package/types/websocket.d.ts +126 -0
package/lib/debug.js
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module debug
|
|
3
|
+
* @description Lightweight namespaced debug logger with levels, colors, and timestamps.
|
|
4
|
+
* Enable via DEBUG env variable: DEBUG=app:*,router (supports glob patterns).
|
|
5
|
+
* Each namespace gets a unique color for easy visual scanning.
|
|
6
|
+
*
|
|
7
|
+
* Levels: trace (0), debug (1), info (2), warn (3), error (4), fatal (5), silent (6).
|
|
8
|
+
* Set level via DEBUG_LEVEL env var or programmatically.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const debug = require('@zero-server/sdk').debug;
|
|
12
|
+
* const log = debug('app:routes');
|
|
13
|
+
*
|
|
14
|
+
* log.info('server started on port %d', 3000);
|
|
15
|
+
* log.warn('deprecation notice');
|
|
16
|
+
* log.error('failed to connect', err);
|
|
17
|
+
* log('shorthand for debug level');
|
|
18
|
+
*
|
|
19
|
+
* // Set minimum level — anything below is silenced
|
|
20
|
+
* debug.level('warn'); // only warn, error, fatal
|
|
21
|
+
* debug.level('silent'); // suppress all output
|
|
22
|
+
* debug.level('trace'); // show everything
|
|
23
|
+
* debug.level(0); // same as 'trace'
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const LEVELS = { trace: 0, debug: 1, info: 2, warn: 3, error: 4, fatal: 5, silent: 6 };
|
|
27
|
+
const LEVEL_NAMES = ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'];
|
|
28
|
+
const LEVEL_COLORS = ['\x1b[2m', '\x1b[36m', '\x1b[32m', '\x1b[33m', '\x1b[31m', '\x1b[35;1m'];
|
|
29
|
+
|
|
30
|
+
// Namespace colors (rotate through these)
|
|
31
|
+
const NS_COLORS = [
|
|
32
|
+
'\x1b[36m', '\x1b[33m', '\x1b[32m', '\x1b[35m', '\x1b[34m',
|
|
33
|
+
'\x1b[36;1m', '\x1b[33;1m', '\x1b[32;1m', '\x1b[35;1m', '\x1b[34;1m',
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const RESET = '\x1b[0m';
|
|
37
|
+
const DIM = '\x1b[2m';
|
|
38
|
+
|
|
39
|
+
let _colorIdx = 0;
|
|
40
|
+
const _nsColorMap = new Map();
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Global state
|
|
44
|
+
*/
|
|
45
|
+
let _globalLevel = LEVELS[process.env.DEBUG_LEVEL] !== undefined
|
|
46
|
+
? LEVELS[process.env.DEBUG_LEVEL]
|
|
47
|
+
: LEVELS.debug;
|
|
48
|
+
|
|
49
|
+
let _enabledPatterns = null;
|
|
50
|
+
let _output = process.stdout;
|
|
51
|
+
let _outputCustom = false;
|
|
52
|
+
let _useColors = process.stdout.isTTY || false;
|
|
53
|
+
let _timestamps = true;
|
|
54
|
+
let _jsonMode = false;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Parse DEBUG env var into patterns.
|
|
58
|
+
* @private
|
|
59
|
+
*/
|
|
60
|
+
function _parsePatterns()
|
|
61
|
+
{
|
|
62
|
+
const raw = process.env.DEBUG;
|
|
63
|
+
if (!raw) return null;
|
|
64
|
+
return raw.split(/[\s,]+/).filter(Boolean).map(p =>
|
|
65
|
+
{
|
|
66
|
+
const neg = p.startsWith('-');
|
|
67
|
+
const pat = neg ? p.slice(1) : p;
|
|
68
|
+
// Convert glob to regex: * => .*, ? => .
|
|
69
|
+
const re = new RegExp('^' + pat.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.') + '$');
|
|
70
|
+
return { neg, re };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_enabledPatterns = _parsePatterns();
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check if a namespace is enabled.
|
|
78
|
+
* @private
|
|
79
|
+
* @param {string} ns - Debug namespace.
|
|
80
|
+
* @returns {boolean} True if the namespace matches the DEBUG patterns.
|
|
81
|
+
*/
|
|
82
|
+
function _isEnabled(ns)
|
|
83
|
+
{
|
|
84
|
+
if (!_enabledPatterns) return true; // No DEBUG set — enable all
|
|
85
|
+
let enabled = false;
|
|
86
|
+
for (const { neg, re } of _enabledPatterns)
|
|
87
|
+
{
|
|
88
|
+
if (re.test(ns)) enabled = !neg;
|
|
89
|
+
}
|
|
90
|
+
return enabled;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Get a color for a namespace.
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
function _nsColor(ns)
|
|
98
|
+
{
|
|
99
|
+
if (!_nsColorMap.has(ns))
|
|
100
|
+
{
|
|
101
|
+
_nsColorMap.set(ns, NS_COLORS[_colorIdx % NS_COLORS.length]);
|
|
102
|
+
_colorIdx++;
|
|
103
|
+
}
|
|
104
|
+
return _nsColorMap.get(ns);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Format a timestamp.
|
|
109
|
+
* @private
|
|
110
|
+
*/
|
|
111
|
+
function _ts()
|
|
112
|
+
{
|
|
113
|
+
const d = new Date();
|
|
114
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
115
|
+
return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}.${String(d.getMilliseconds()).padStart(3, '0')}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Format arguments (like console.log — supports %s, %d, %j, %o).
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
function _format(args)
|
|
123
|
+
{
|
|
124
|
+
if (args.length === 0) return '';
|
|
125
|
+
if (typeof args[0] === 'string' && args.length > 1)
|
|
126
|
+
{
|
|
127
|
+
let i = 1;
|
|
128
|
+
const str = args[0].replace(/%([sdjo%])/g, (_, f) =>
|
|
129
|
+
{
|
|
130
|
+
if (f === '%') return '%';
|
|
131
|
+
if (i >= args.length) return `%${f}`;
|
|
132
|
+
const v = args[i++];
|
|
133
|
+
if (f === 's') return String(v);
|
|
134
|
+
if (f === 'd') return Number(v);
|
|
135
|
+
if (f === 'j' || f === 'o')
|
|
136
|
+
{
|
|
137
|
+
try { return JSON.stringify(v); }
|
|
138
|
+
catch { return String(v); }
|
|
139
|
+
}
|
|
140
|
+
return String(v);
|
|
141
|
+
});
|
|
142
|
+
const rest = args.slice(i).map(a =>
|
|
143
|
+
typeof a === 'object' ? JSON.stringify(a) : String(a)
|
|
144
|
+
);
|
|
145
|
+
return rest.length > 0 ? str + ' ' + rest.join(' ') : str;
|
|
146
|
+
}
|
|
147
|
+
return args.map(a =>
|
|
148
|
+
{
|
|
149
|
+
if (a instanceof Error) return a.stack || a.message;
|
|
150
|
+
if (typeof a === 'object')
|
|
151
|
+
{
|
|
152
|
+
try { return JSON.stringify(a); }
|
|
153
|
+
catch { return String(a); }
|
|
154
|
+
}
|
|
155
|
+
return String(a);
|
|
156
|
+
}).join(' ');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Write a log entry.
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
function _write(ns, level, args)
|
|
164
|
+
{
|
|
165
|
+
const msg = _format(args);
|
|
166
|
+
const out = (_outputCustom || level < LEVELS.warn) ? _output : process.stderr;
|
|
167
|
+
|
|
168
|
+
if (_jsonMode)
|
|
169
|
+
{
|
|
170
|
+
const entry = {
|
|
171
|
+
timestamp: new Date().toISOString(),
|
|
172
|
+
level: LEVEL_NAMES[level],
|
|
173
|
+
namespace: ns,
|
|
174
|
+
message: msg,
|
|
175
|
+
};
|
|
176
|
+
// If last arg is an Error, attach stack
|
|
177
|
+
const last = args[args.length - 1];
|
|
178
|
+
if (last instanceof Error)
|
|
179
|
+
{
|
|
180
|
+
entry.error = { message: last.message, stack: last.stack, code: last.code };
|
|
181
|
+
}
|
|
182
|
+
out.write(JSON.stringify(entry) + '\n');
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Pretty text output
|
|
187
|
+
const parts = [];
|
|
188
|
+
if (_timestamps)
|
|
189
|
+
{
|
|
190
|
+
parts.push(_useColors ? `${DIM}${_ts()}${RESET}` : _ts());
|
|
191
|
+
}
|
|
192
|
+
// Level
|
|
193
|
+
const lvlName = LEVEL_NAMES[level];
|
|
194
|
+
if (_useColors)
|
|
195
|
+
{
|
|
196
|
+
parts.push(`${LEVEL_COLORS[level]}${lvlName.padEnd(5)}${RESET}`);
|
|
197
|
+
}
|
|
198
|
+
else
|
|
199
|
+
{
|
|
200
|
+
parts.push(lvlName.padEnd(5));
|
|
201
|
+
}
|
|
202
|
+
// Namespace
|
|
203
|
+
if (_useColors)
|
|
204
|
+
{
|
|
205
|
+
parts.push(`${_nsColor(ns)}${ns}${RESET}`);
|
|
206
|
+
}
|
|
207
|
+
else
|
|
208
|
+
{
|
|
209
|
+
parts.push(ns);
|
|
210
|
+
}
|
|
211
|
+
parts.push(msg);
|
|
212
|
+
|
|
213
|
+
out.write(parts.join(' ') + '\n');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Create a namespaced debug logger.
|
|
218
|
+
*
|
|
219
|
+
* @param {string} namespace - Logger namespace (e.g. 'app:routes', 'db:queries').
|
|
220
|
+
* @returns {Function & { trace, debug, info, warn, error, fatal, enabled }} Logger function.
|
|
221
|
+
*/
|
|
222
|
+
function debug(namespace)
|
|
223
|
+
{
|
|
224
|
+
const enabled = _isEnabled(namespace);
|
|
225
|
+
|
|
226
|
+
// Default call = debug level
|
|
227
|
+
const logger = (...args) =>
|
|
228
|
+
{
|
|
229
|
+
if (!enabled || _globalLevel > LEVELS.debug) return;
|
|
230
|
+
_write(namespace, LEVELS.debug, args);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
logger.trace = (...args) =>
|
|
234
|
+
{
|
|
235
|
+
if (!enabled || _globalLevel > LEVELS.trace) return;
|
|
236
|
+
_write(namespace, LEVELS.trace, args);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
logger.debug = logger;
|
|
240
|
+
|
|
241
|
+
logger.info = (...args) =>
|
|
242
|
+
{
|
|
243
|
+
if (!enabled || _globalLevel > LEVELS.info) return;
|
|
244
|
+
_write(namespace, LEVELS.info, args);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
logger.warn = (...args) =>
|
|
248
|
+
{
|
|
249
|
+
if (!enabled || _globalLevel > LEVELS.warn) return;
|
|
250
|
+
_write(namespace, LEVELS.warn, args);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
logger.error = (...args) =>
|
|
254
|
+
{
|
|
255
|
+
if (!enabled || _globalLevel > LEVELS.error) return;
|
|
256
|
+
_write(namespace, LEVELS.error, args);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
logger.fatal = (...args) =>
|
|
260
|
+
{
|
|
261
|
+
if (!enabled || _globalLevel > LEVELS.fatal) return;
|
|
262
|
+
_write(namespace, LEVELS.fatal, args);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/** Whether this namespace is active. */
|
|
266
|
+
logger.enabled = enabled;
|
|
267
|
+
|
|
268
|
+
/** The namespace string */
|
|
269
|
+
logger.namespace = namespace;
|
|
270
|
+
|
|
271
|
+
return logger;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// --- Configuration API -------------------------------------------
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Set the minimum log level globally.
|
|
278
|
+
* Messages below this level are silenced.
|
|
279
|
+
*
|
|
280
|
+
* @param {string|number} level - Level name or number.
|
|
281
|
+
* `'trace'` (0) — all output
|
|
282
|
+
* `'debug'` (1) — debug and above
|
|
283
|
+
* `'info'` (2) — info and above
|
|
284
|
+
* `'warn'` (3) — warn and above
|
|
285
|
+
* `'error'` (4) — error and fatal only
|
|
286
|
+
* `'fatal'` (5) — fatal only
|
|
287
|
+
* `'silent'` (6) — nothing
|
|
288
|
+
*/
|
|
289
|
+
debug.level = function(level)
|
|
290
|
+
{
|
|
291
|
+
if (typeof level === 'string') _globalLevel = LEVELS[level.toLowerCase()] ?? LEVELS.debug;
|
|
292
|
+
else _globalLevel = level;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Enable/disable namespaces programmatically (same syntax as DEBUG env var).
|
|
297
|
+
* @param {string} patterns - Comma-separated patterns. Use '-ns' to exclude.
|
|
298
|
+
*/
|
|
299
|
+
debug.enable = function(patterns)
|
|
300
|
+
{
|
|
301
|
+
process.env.DEBUG = patterns;
|
|
302
|
+
_enabledPatterns = _parsePatterns();
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Disable all debug output.
|
|
307
|
+
*/
|
|
308
|
+
debug.disable = function()
|
|
309
|
+
{
|
|
310
|
+
process.env.DEBUG = '';
|
|
311
|
+
_enabledPatterns = [{ neg: false, re: /^$/ }]; // match nothing
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Enable structured JSON output.
|
|
316
|
+
* @param {boolean} [on=true] - Enable flag.
|
|
317
|
+
*/
|
|
318
|
+
debug.json = function(on = true)
|
|
319
|
+
{
|
|
320
|
+
_jsonMode = on;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Enable/disable timestamps.
|
|
325
|
+
* @param {boolean} [on=true] - Enable flag.
|
|
326
|
+
*/
|
|
327
|
+
debug.timestamps = function(on = true)
|
|
328
|
+
{
|
|
329
|
+
_timestamps = on;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Enable/disable colors.
|
|
334
|
+
* @param {boolean} [on=true] - Enable flag.
|
|
335
|
+
*/
|
|
336
|
+
debug.colors = function(on = true)
|
|
337
|
+
{
|
|
338
|
+
_useColors = on;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Set custom output stream.
|
|
343
|
+
* @param {object} stream - Writable stream with write() method.
|
|
344
|
+
*/
|
|
345
|
+
debug.output = function(stream)
|
|
346
|
+
{
|
|
347
|
+
_output = stream;
|
|
348
|
+
_outputCustom = true;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Reset all settings to defaults.
|
|
353
|
+
*/
|
|
354
|
+
debug.reset = function()
|
|
355
|
+
{
|
|
356
|
+
_globalLevel = LEVELS[process.env.DEBUG_LEVEL] !== undefined
|
|
357
|
+
? LEVELS[process.env.DEBUG_LEVEL]
|
|
358
|
+
: LEVELS.debug;
|
|
359
|
+
_enabledPatterns = _parsePatterns();
|
|
360
|
+
_output = process.stdout;
|
|
361
|
+
_outputCustom = false;
|
|
362
|
+
_useColors = process.stdout.isTTY || false;
|
|
363
|
+
_timestamps = true;
|
|
364
|
+
_jsonMode = false;
|
|
365
|
+
_colorIdx = 0;
|
|
366
|
+
_nsColorMap.clear();
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/** Expose level constants. */
|
|
370
|
+
debug.LEVELS = LEVELS;
|
|
371
|
+
|
|
372
|
+
module.exports = debug;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zero-server/auth",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"description": "JWT, sessions, OAuth, authorize, MFA stack.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"zero-server",
|
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
"./package.json": "./package.json"
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
|
+
"lib",
|
|
24
|
+
"types",
|
|
23
25
|
"index.js",
|
|
24
26
|
"index.d.ts",
|
|
25
27
|
"README.md",
|
|
@@ -43,6 +45,14 @@
|
|
|
43
45
|
},
|
|
44
46
|
"sideEffects": false,
|
|
45
47
|
"dependencies": {
|
|
46
|
-
"@zero-server/
|
|
48
|
+
"@zero-server/fetch": "0.9.3"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@zero-server/sdk": ">=0.9.3"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@zero-server/sdk": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
47
57
|
}
|
|
48
58
|
}
|
package/types/app.d.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
import { Server as HttpServer } from 'http';
|
|
4
|
+
import { Server as HttpsServer, ServerOptions as TlsOptions } from 'https';
|
|
5
|
+
import { Http2Server, Http2SecureServer } from 'http2';
|
|
6
|
+
import { Request } from './request';
|
|
7
|
+
import { Response } from './response';
|
|
8
|
+
import { RouterInstance, RouteChain, RouteInfo, RouteOptions, RouteHandler } from './router';
|
|
9
|
+
import { MiddlewareFunction, ErrorHandlerFunction, NextFunction } from './middleware';
|
|
10
|
+
import { WebSocketHandler, WebSocketOptions, WebSocketPool } from './websocket';
|
|
11
|
+
import { SSEStream } from './sse';
|
|
12
|
+
import { LifecycleState } from './lifecycle';
|
|
13
|
+
import { MetricsRegistry, HealthCheckResult } from './observe';
|
|
14
|
+
import { ProtoSchema, GrpcServiceOptions, GrpcInterceptor, GrpcHandler } from './grpc';
|
|
15
|
+
|
|
16
|
+
export interface ListenOptions {
|
|
17
|
+
/** Create an HTTP/2 server. Combined with TLS options for h2 over TLS, or h2c (cleartext) otherwise. */
|
|
18
|
+
http2?: boolean;
|
|
19
|
+
/** Allow HTTP/1.1 fallback on HTTP/2 TLS servers (ALPN negotiation). Default: true. */
|
|
20
|
+
allowHTTP1?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface App {
|
|
24
|
+
/** Internal router instance. */
|
|
25
|
+
router: RouterInstance;
|
|
26
|
+
/** Middleware stack. */
|
|
27
|
+
middlewares: MiddlewareFunction[];
|
|
28
|
+
/** Application-level locals, merged into every request/response locals. */
|
|
29
|
+
locals: Record<string, any>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Register middleware or mount a sub-router.
|
|
33
|
+
*/
|
|
34
|
+
use(fn: MiddlewareFunction): App;
|
|
35
|
+
use(path: string, fn: MiddlewareFunction): App;
|
|
36
|
+
use(path: string, router: RouterInstance): App;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Register a global error handler.
|
|
40
|
+
*/
|
|
41
|
+
onError(fn: ErrorHandlerFunction): void;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Core request handler for use with `http.createServer()`.
|
|
45
|
+
*/
|
|
46
|
+
handler(req: import('http').IncomingMessage, res: import('http').ServerResponse): void;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Start listening for connections.
|
|
50
|
+
* Pass `{ http2: true }` to create an HTTP/2 server (h2c cleartext or TLS with ALPN).
|
|
51
|
+
*/
|
|
52
|
+
listen(port?: number, cb?: () => void): HttpServer;
|
|
53
|
+
listen(port: number, opts: TlsOptions, cb?: () => void): HttpsServer;
|
|
54
|
+
listen(port: number, opts: ListenOptions & { http2: true } & TlsOptions, cb?: () => void): Http2SecureServer;
|
|
55
|
+
listen(port: number, opts: ListenOptions & { http2: true }, cb?: () => void): Http2Server;
|
|
56
|
+
listen(port: number, opts: ListenOptions, cb?: () => void): HttpServer | HttpsServer | Http2Server | Http2SecureServer;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Gracefully close the server.
|
|
60
|
+
*/
|
|
61
|
+
close(cb?: (err?: Error) => void): void;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Perform a full graceful shutdown.
|
|
65
|
+
*/
|
|
66
|
+
shutdown(opts?: { timeout?: number }): Promise<void>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Register a lifecycle event listener.
|
|
70
|
+
*/
|
|
71
|
+
on(event: 'beforeShutdown' | 'shutdown', fn: () => void | Promise<void>): App;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Remove a lifecycle event listener.
|
|
75
|
+
*/
|
|
76
|
+
off(event: 'beforeShutdown' | 'shutdown', fn: () => void | Promise<void>): App;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Register a WebSocket pool for graceful shutdown.
|
|
80
|
+
*/
|
|
81
|
+
registerPool(pool: WebSocketPool): App;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Unregister a WebSocket pool from lifecycle management.
|
|
85
|
+
*/
|
|
86
|
+
unregisterPool(pool: WebSocketPool): App;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Track an SSE stream for graceful shutdown.
|
|
90
|
+
*/
|
|
91
|
+
trackSSE(stream: SSEStream): App;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Register an ORM Database for graceful shutdown.
|
|
95
|
+
*/
|
|
96
|
+
registerDatabase(db: { close(): Promise<void> }): App;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Unregister an ORM Database from lifecycle management.
|
|
100
|
+
*/
|
|
101
|
+
unregisterDatabase(db: { close(): Promise<void> }): App;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Configure the shutdown timeout in milliseconds.
|
|
105
|
+
*/
|
|
106
|
+
shutdownTimeout(ms: number): App;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Current lifecycle state.
|
|
110
|
+
*/
|
|
111
|
+
readonly lifecycleState: LifecycleState;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Register a liveness health check endpoint.
|
|
115
|
+
*/
|
|
116
|
+
health(path?: string, checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
117
|
+
health(checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Register a readiness health check endpoint.
|
|
121
|
+
*/
|
|
122
|
+
ready(path?: string, checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
123
|
+
ready(checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Register a custom health check.
|
|
127
|
+
*/
|
|
128
|
+
addHealthCheck(name: string, fn: () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>): App;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Get the application metrics registry.
|
|
132
|
+
*/
|
|
133
|
+
metrics(): MetricsRegistry;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Mount a Prometheus metrics endpoint.
|
|
137
|
+
*/
|
|
138
|
+
metricsEndpoint(path?: string, opts?: { registry?: MetricsRegistry }): App;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Register a WebSocket upgrade handler.
|
|
142
|
+
*/
|
|
143
|
+
ws(path: string, handler: WebSocketHandler): void;
|
|
144
|
+
ws(path: string, opts: WebSocketOptions, handler: WebSocketHandler): void;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Register a gRPC service with handlers.
|
|
148
|
+
*/
|
|
149
|
+
grpc(schema: ProtoSchema, serviceName: string, handlers: Record<string, GrpcHandler>, opts?: GrpcServiceOptions): App;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Add a global gRPC interceptor.
|
|
153
|
+
*/
|
|
154
|
+
grpcInterceptor(fn: GrpcInterceptor): App;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Return a flat list of all registered routes.
|
|
158
|
+
*/
|
|
159
|
+
routes(): RouteInfo[];
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Register a route with a specific HTTP method.
|
|
163
|
+
*/
|
|
164
|
+
route(method: string, path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get a setting value (1 arg) or set a setting value (2 args).
|
|
168
|
+
*/
|
|
169
|
+
set(key: string): any;
|
|
170
|
+
set(key: string, val: any): App;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Get a setting value, or register a GET route.
|
|
174
|
+
* With 1 string arg: returns the setting value.
|
|
175
|
+
* With path + handlers: registers a GET route.
|
|
176
|
+
*/
|
|
177
|
+
get(key: string): any;
|
|
178
|
+
get(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Enable a boolean setting (set to `true`).
|
|
182
|
+
*/
|
|
183
|
+
enable(key: string): App;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Disable a boolean setting (set to `false`).
|
|
187
|
+
*/
|
|
188
|
+
disable(key: string): App;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Check if a setting is truthy.
|
|
192
|
+
*/
|
|
193
|
+
enabled(key: string): boolean;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Check if a setting is falsy.
|
|
197
|
+
*/
|
|
198
|
+
disabled(key: string): boolean;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Register a parameter pre-processing handler.
|
|
202
|
+
*/
|
|
203
|
+
param(name: string, fn: (req: Request, res: Response, next: NextFunction, value: string) => void): App;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Create a route group under a prefix with shared middleware.
|
|
207
|
+
*/
|
|
208
|
+
group(prefix: string, ...args: [...MiddlewareFunction[], (router: RouterInstance) => void]): App;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Create a chainable route builder for the given path.
|
|
212
|
+
*/
|
|
213
|
+
chain(path: string): RouteChain;
|
|
214
|
+
|
|
215
|
+
// HTTP method shortcuts
|
|
216
|
+
post(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
217
|
+
put(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
218
|
+
delete(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
219
|
+
patch(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
220
|
+
options(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
221
|
+
head(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
222
|
+
all(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
223
|
+
}
|