@zero-server/fetch 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 +2 -5
- package/lib/fetch/index.js +256 -0
- package/package.json +10 -3
- 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/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Tony Wiedman
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tony Wiedman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// AUTO-GENERATED by .tools/generate-package-stubs.js — edit .tools/scope-manifest.js and re-run `npm run packages:generate`.
|
|
2
|
-
export
|
|
2
|
+
export * from './types/fetch';
|
package/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
// AUTO-GENERATED by .tools/generate-package-stubs.js — edit .tools/scope-manifest.js and re-run `npm run packages:generate`.
|
|
2
2
|
'use strict';
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
fetch: sdk.fetch,
|
|
7
|
-
};
|
|
3
|
+
const fetch = require('./lib/fetch');
|
|
4
|
+
module.exports = { fetch };
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module fetch
|
|
3
|
+
* @description Minimal, zero-dependency server-side `fetch()` replacement.
|
|
4
|
+
* Supports HTTP/HTTPS, JSON/URLSearchParams/Buffer/stream bodies,
|
|
5
|
+
* download & upload progress callbacks, timeouts, and AbortSignal.
|
|
6
|
+
*/
|
|
7
|
+
const http = require('http');
|
|
8
|
+
const https = require('https');
|
|
9
|
+
const { URL } = require('url');
|
|
10
|
+
|
|
11
|
+
const STATUS_CODES = http.STATUS_CODES;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Perform an HTTP(S) request.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} url - Absolute URL to fetch.
|
|
17
|
+
* @param {object} [opts] - Configuration options.
|
|
18
|
+
* @param {string} [opts.method='GET'] - HTTP method.
|
|
19
|
+
* @param {object} [opts.headers] - Request headers.
|
|
20
|
+
* @param {string|Buffer|object|ReadableStream} [opts.body] - Request body.
|
|
21
|
+
* @param {number} [opts.timeout] - Request timeout in ms.
|
|
22
|
+
* @param {AbortSignal} [opts.signal] - Abort signal for cancellation.
|
|
23
|
+
* @param {import('http').Agent} [opts.agent] - Custom HTTP agent.
|
|
24
|
+
* @param {Function} [opts.onDownloadProgress] - `({ loaded, total }) => void` download progress callback.
|
|
25
|
+
* @param {Function} [opts.onUploadProgress] - `({ loaded, total }) => void` upload progress callback.
|
|
26
|
+
* @param {boolean} [opts.rejectUnauthorized] - Reject connections with unverified certs (default: Node default `true`). TLS option — passed to `https.request()`.
|
|
27
|
+
* @param {string|Buffer|Array} [opts.ca] - Override default CA certificates.
|
|
28
|
+
* @param {string|Buffer} [opts.cert] - Client certificate (PEM) for mutual TLS.
|
|
29
|
+
* @param {string|Buffer} [opts.key] - Private key (PEM) for mutual TLS.
|
|
30
|
+
* @param {string|Buffer} [opts.pfx] - PFX / PKCS12 bundle (alternative to cert+key).
|
|
31
|
+
* @param {string} [opts.passphrase] - Passphrase for the key or PFX.
|
|
32
|
+
* @param {string} [opts.servername] - SNI server name override.
|
|
33
|
+
* @param {string} [opts.ciphers] - Colon-separated cipher list.
|
|
34
|
+
* @param {string} [opts.secureProtocol] - SSL/TLS protocol method name.
|
|
35
|
+
* @param {string} [opts.minVersion] - Minimum TLS version (`'TLSv1.2'`, etc.).
|
|
36
|
+
* @param {string} [opts.maxVersion] - Maximum TLS version.
|
|
37
|
+
*
|
|
38
|
+
* @returns {Promise<{ status: number, statusText: string, ok: boolean, secure: boolean, url: string, headers: object, arrayBuffer: Function, text: Function, json: Function }>} Resolves with a response object containing status info, headers, and body-reading helpers (`text()`, `json()`, `arrayBuffer()`).
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const res = await fetch('https://api.example.com/data');
|
|
42
|
+
* const body = await res.json();
|
|
43
|
+
*
|
|
44
|
+
* // POST with JSON body & timeout
|
|
45
|
+
* const res2 = await fetch('https://api.example.com/items', {
|
|
46
|
+
* method: 'POST',
|
|
47
|
+
* body: { name: 'widget' },
|
|
48
|
+
* timeout: 5000,
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
function miniFetch(url, opts = {})
|
|
52
|
+
{
|
|
53
|
+
return new Promise((resolve, reject) =>
|
|
54
|
+
{
|
|
55
|
+
try
|
|
56
|
+
{
|
|
57
|
+
const u = new URL(url);
|
|
58
|
+
const lib = u.protocol === 'https:' ? https : http;
|
|
59
|
+
const method = (opts.method || 'GET').toUpperCase();
|
|
60
|
+
const headers = Object.assign({}, opts.headers || {});
|
|
61
|
+
|
|
62
|
+
// Normalize body
|
|
63
|
+
let body = opts.body;
|
|
64
|
+
if (body && typeof body === 'object' && typeof body.toString === 'function' && body.constructor && body.constructor.name === 'URLSearchParams')
|
|
65
|
+
{
|
|
66
|
+
if (!headers['Content-Type'] && !headers['content-type']) headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
67
|
+
body = body.toString();
|
|
68
|
+
}
|
|
69
|
+
else if (body && typeof body === 'object' && !Buffer.isBuffer(body) && !(body instanceof ArrayBuffer) && !(body instanceof Uint8Array) && !(body && typeof body.pipe === 'function'))
|
|
70
|
+
{
|
|
71
|
+
if (!headers['Content-Type'] && !headers['content-type']) headers['Content-Type'] = 'application/json';
|
|
72
|
+
body = Buffer.from(JSON.stringify(body), 'utf8');
|
|
73
|
+
}
|
|
74
|
+
else if (body instanceof ArrayBuffer)
|
|
75
|
+
{
|
|
76
|
+
body = Buffer.from(body);
|
|
77
|
+
}
|
|
78
|
+
else if (body instanceof Uint8Array && !Buffer.isBuffer(body))
|
|
79
|
+
{
|
|
80
|
+
body = Buffer.from(body);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Set Content-Length for known-size bodies
|
|
84
|
+
if ((Buffer.isBuffer(body) || typeof body === 'string') && !headers['Content-Length'] && !headers['content-length'])
|
|
85
|
+
{
|
|
86
|
+
headers['Content-Length'] = String(Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const options = { method, headers };
|
|
90
|
+
if (opts.agent) options.agent = opts.agent;
|
|
91
|
+
|
|
92
|
+
// Pass through TLS options for HTTPS requests
|
|
93
|
+
if (lib === https)
|
|
94
|
+
{
|
|
95
|
+
const tlsKeys = [
|
|
96
|
+
'rejectUnauthorized', 'ca', 'cert', 'key', 'pfx', 'passphrase',
|
|
97
|
+
'servername', 'ciphers', 'secureProtocol', 'minVersion', 'maxVersion'
|
|
98
|
+
];
|
|
99
|
+
for (const k of tlsKeys)
|
|
100
|
+
{
|
|
101
|
+
if (opts[k] !== undefined) options[k] = opts[k];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const req = lib.request(u, options, (res) =>
|
|
106
|
+
{
|
|
107
|
+
const chunks = [];
|
|
108
|
+
let downloaded = 0;
|
|
109
|
+
const total = parseInt(res.headers['content-length'] || '0', 10) || null;
|
|
110
|
+
|
|
111
|
+
res.on('data', (c) =>
|
|
112
|
+
{
|
|
113
|
+
chunks.push(c);
|
|
114
|
+
downloaded += c.length;
|
|
115
|
+
if (typeof opts.onDownloadProgress === 'function')
|
|
116
|
+
{
|
|
117
|
+
try { opts.onDownloadProgress({ loaded: downloaded, total }); } catch (e) { }
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
res.on('end', () =>
|
|
122
|
+
{
|
|
123
|
+
const buf = Buffer.concat(chunks);
|
|
124
|
+
const status = res.statusCode;
|
|
125
|
+
const rawHeaders = res.headers || {};
|
|
126
|
+
const responseHeaders = {
|
|
127
|
+
get(name)
|
|
128
|
+
{
|
|
129
|
+
if (!name) return undefined;
|
|
130
|
+
const v = rawHeaders[name.toLowerCase()];
|
|
131
|
+
return Array.isArray(v) ? v.join(', ') : v;
|
|
132
|
+
},
|
|
133
|
+
raw: rawHeaders,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
resolve({
|
|
137
|
+
status,
|
|
138
|
+
statusText: STATUS_CODES[status] || '',
|
|
139
|
+
ok: status >= 200 && status < 300,
|
|
140
|
+
secure: u.protocol === 'https:',
|
|
141
|
+
url: u.href,
|
|
142
|
+
headers: responseHeaders,
|
|
143
|
+
arrayBuffer: () => Promise.resolve(buf),
|
|
144
|
+
text: () => Promise.resolve(buf.toString('utf8')),
|
|
145
|
+
json: () =>
|
|
146
|
+
{
|
|
147
|
+
try { return Promise.resolve(JSON.parse(buf.toString('utf8'))); }
|
|
148
|
+
catch (e) { return Promise.reject(e); }
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
req.on('error', reject);
|
|
155
|
+
|
|
156
|
+
// Timeout
|
|
157
|
+
if (typeof opts.timeout === 'number' && opts.timeout > 0)
|
|
158
|
+
{
|
|
159
|
+
req.setTimeout(opts.timeout, () =>
|
|
160
|
+
{
|
|
161
|
+
const err = new Error('Request timed out');
|
|
162
|
+
err.code = 'ETIMEOUT';
|
|
163
|
+
req.destroy(err);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// AbortSignal support
|
|
168
|
+
let abortHandler;
|
|
169
|
+
if (opts.signal)
|
|
170
|
+
{
|
|
171
|
+
if (opts.signal.aborted)
|
|
172
|
+
{
|
|
173
|
+
const err = new Error('Request aborted');
|
|
174
|
+
err.name = 'AbortError';
|
|
175
|
+
req.destroy(err);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
abortHandler = () =>
|
|
179
|
+
{
|
|
180
|
+
const err = new Error('Request aborted');
|
|
181
|
+
err.name = 'AbortError';
|
|
182
|
+
req.destroy(err);
|
|
183
|
+
};
|
|
184
|
+
if (typeof opts.signal.addEventListener === 'function') opts.signal.addEventListener('abort', abortHandler);
|
|
185
|
+
else if (typeof opts.signal.on === 'function') opts.signal.on('abort', abortHandler);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Cleanup signal listener
|
|
189
|
+
const cleanup = () =>
|
|
190
|
+
{
|
|
191
|
+
if (opts.signal && abortHandler)
|
|
192
|
+
{
|
|
193
|
+
try
|
|
194
|
+
{
|
|
195
|
+
if (typeof opts.signal.removeEventListener === 'function') opts.signal.removeEventListener('abort', abortHandler);
|
|
196
|
+
else if (typeof opts.signal.off === 'function') opts.signal.off('abort', abortHandler);
|
|
197
|
+
}
|
|
198
|
+
catch (e) { }
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
req.on('close', cleanup);
|
|
202
|
+
|
|
203
|
+
// Write body
|
|
204
|
+
if (body && typeof body.pipe === 'function')
|
|
205
|
+
{
|
|
206
|
+
let uploaded = 0;
|
|
207
|
+
body.on('data', (chunk) =>
|
|
208
|
+
{
|
|
209
|
+
uploaded += chunk.length;
|
|
210
|
+
if (typeof opts.onUploadProgress === 'function')
|
|
211
|
+
{
|
|
212
|
+
try { opts.onUploadProgress({ loaded: uploaded, total: headers['Content-Length'] ? Number(headers['Content-Length']) : null }); } catch (e) { }
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
body.on('error', (err) => req.destroy(err));
|
|
216
|
+
body.pipe(req);
|
|
217
|
+
}
|
|
218
|
+
else if (Buffer.isBuffer(body) || typeof body === 'string')
|
|
219
|
+
{
|
|
220
|
+
const buf = Buffer.isBuffer(body) ? body : Buffer.from(body);
|
|
221
|
+
const total = buf.length;
|
|
222
|
+
const CHUNK = 64 * 1024;
|
|
223
|
+
let sent = 0;
|
|
224
|
+
|
|
225
|
+
function writeNext()
|
|
226
|
+
{
|
|
227
|
+
if (sent >= total) { req.end(); return; }
|
|
228
|
+
const slice = buf.slice(sent, Math.min(sent + CHUNK, total));
|
|
229
|
+
const ok = req.write(slice, () =>
|
|
230
|
+
{
|
|
231
|
+
sent += slice.length;
|
|
232
|
+
if (typeof opts.onUploadProgress === 'function')
|
|
233
|
+
{
|
|
234
|
+
try { opts.onUploadProgress({ loaded: sent, total }); } catch (e) { }
|
|
235
|
+
}
|
|
236
|
+
writeNext();
|
|
237
|
+
});
|
|
238
|
+
if (!ok) req.once('drain', writeNext);
|
|
239
|
+
}
|
|
240
|
+
writeNext();
|
|
241
|
+
}
|
|
242
|
+
else if (body == null)
|
|
243
|
+
{
|
|
244
|
+
req.end();
|
|
245
|
+
}
|
|
246
|
+
else
|
|
247
|
+
{
|
|
248
|
+
req.write(String(body));
|
|
249
|
+
req.end();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
catch (e) { reject(e); }
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
module.exports = miniFetch;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zero-server/fetch",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"description": "Server-side fetch with mTLS, timeouts, AbortSignal.",
|
|
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",
|
|
@@ -42,7 +44,12 @@
|
|
|
42
44
|
"access": "public"
|
|
43
45
|
},
|
|
44
46
|
"sideEffects": false,
|
|
45
|
-
"
|
|
46
|
-
"@zero-server/sdk": "0.9.
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@zero-server/sdk": ">=0.9.3"
|
|
49
|
+
},
|
|
50
|
+
"peerDependenciesMeta": {
|
|
51
|
+
"@zero-server/sdk": {
|
|
52
|
+
"optional": true
|
|
53
|
+
}
|
|
47
54
|
}
|
|
48
55
|
}
|
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
|
+
}
|