@zero-server/cli 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 +3 -3
- package/lib/cli.js +845 -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/types/auth.d.ts
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { Request } from './request';
|
|
2
|
+
import { Response } from './response';
|
|
3
|
+
import { MiddlewareFunction } from './middleware';
|
|
4
|
+
|
|
5
|
+
// --- JWT ---------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
export interface JwtHeader {
|
|
8
|
+
alg: string;
|
|
9
|
+
typ: string;
|
|
10
|
+
kid?: string;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface JwtPayload {
|
|
15
|
+
iss?: string;
|
|
16
|
+
sub?: string;
|
|
17
|
+
aud?: string | string[];
|
|
18
|
+
exp?: number;
|
|
19
|
+
nbf?: number;
|
|
20
|
+
iat?: number;
|
|
21
|
+
jti?: string;
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface JwtDecoded {
|
|
26
|
+
header: JwtHeader;
|
|
27
|
+
payload: JwtPayload;
|
|
28
|
+
signature: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface JwtSignOptions {
|
|
32
|
+
algorithm?: string;
|
|
33
|
+
expiresIn?: number;
|
|
34
|
+
issuer?: string;
|
|
35
|
+
audience?: string;
|
|
36
|
+
subject?: string;
|
|
37
|
+
jwtId?: string;
|
|
38
|
+
notBefore?: number;
|
|
39
|
+
header?: Record<string, any>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface JwtVerifyOptions {
|
|
43
|
+
algorithms?: string | string[];
|
|
44
|
+
audience?: string | string[];
|
|
45
|
+
issuer?: string | string[];
|
|
46
|
+
subject?: string;
|
|
47
|
+
clockTolerance?: number;
|
|
48
|
+
maxAge?: number;
|
|
49
|
+
ignoreExpiration?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface JwtVerifyResult {
|
|
53
|
+
header: JwtHeader;
|
|
54
|
+
payload: JwtPayload;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface JwtMiddlewareOptions {
|
|
58
|
+
secret?: string | Buffer;
|
|
59
|
+
publicKey?: string | Buffer;
|
|
60
|
+
getKey?: (header: JwtHeader, payload: JwtPayload) => string | Buffer | Promise<string | Buffer>;
|
|
61
|
+
jwksUri?: string;
|
|
62
|
+
algorithms?: string | string[];
|
|
63
|
+
getToken?: (req: Request) => string | null;
|
|
64
|
+
tokenLocation?: 'header' | 'cookie' | 'query';
|
|
65
|
+
cookieName?: string;
|
|
66
|
+
queryParam?: string;
|
|
67
|
+
audience?: string | string[];
|
|
68
|
+
issuer?: string | string[];
|
|
69
|
+
subject?: string;
|
|
70
|
+
clockTolerance?: number;
|
|
71
|
+
maxAge?: number;
|
|
72
|
+
credentialsRequired?: boolean;
|
|
73
|
+
isRevoked?: (payload: JwtPayload) => boolean | Promise<boolean>;
|
|
74
|
+
onError?: (err: { message: string; code: string; statusCode: number }, req: Request, res: Response) => void;
|
|
75
|
+
fetcher?: Function;
|
|
76
|
+
cacheTtl?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function jwt(opts: JwtMiddlewareOptions): MiddlewareFunction;
|
|
80
|
+
export function jwtSign(payload: Record<string, any>, secret: string | Buffer, opts?: JwtSignOptions): string;
|
|
81
|
+
export function jwtVerify(token: string, secretOrKey: string | Buffer, opts?: JwtVerifyOptions): JwtVerifyResult;
|
|
82
|
+
export function jwtDecode(token: string): JwtDecoded | null;
|
|
83
|
+
|
|
84
|
+
export interface JwksGetKey {
|
|
85
|
+
(header: JwtHeader, payload?: JwtPayload): Promise<string>;
|
|
86
|
+
_clearCache(): void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface JwksOptions {
|
|
90
|
+
fetcher?: Function;
|
|
91
|
+
cacheTtl?: number;
|
|
92
|
+
requestTimeout?: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function jwks(jwksUri: string, opts?: JwksOptions): JwksGetKey;
|
|
96
|
+
|
|
97
|
+
export interface TokenPairConfig {
|
|
98
|
+
accessSecret: string | Buffer;
|
|
99
|
+
refreshSecret?: string | Buffer;
|
|
100
|
+
accessExpiresIn?: number;
|
|
101
|
+
refreshExpiresIn?: number;
|
|
102
|
+
algorithm?: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface TokenPairInstance {
|
|
106
|
+
generateTokens(payload: Record<string, any>): { accessToken: string; refreshToken: string };
|
|
107
|
+
verifyRefreshToken(token: string): JwtVerifyResult;
|
|
108
|
+
verifyAccessToken(token: string): JwtVerifyResult;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function tokenPair(config: TokenPairConfig): TokenPairInstance;
|
|
112
|
+
export function createRefreshToken(payload: Record<string, any>, secret: string | Buffer, opts?: { expiresIn?: number; algorithm?: string }): string;
|
|
113
|
+
export const SUPPORTED_ALGORITHMS: string[];
|
|
114
|
+
|
|
115
|
+
// --- Session -----------------------------------------------------
|
|
116
|
+
|
|
117
|
+
export interface SessionData {
|
|
118
|
+
[key: string]: any;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export declare class Session {
|
|
122
|
+
id: string;
|
|
123
|
+
constructor(id: string, data?: SessionData);
|
|
124
|
+
get(key: string): any;
|
|
125
|
+
set(key: string, value: any): Session;
|
|
126
|
+
has(key: string): boolean;
|
|
127
|
+
delete(key: string): boolean;
|
|
128
|
+
all(): SessionData;
|
|
129
|
+
readonly size: number;
|
|
130
|
+
clear(): Session;
|
|
131
|
+
destroy(): void;
|
|
132
|
+
regenerate(): void;
|
|
133
|
+
flash(key: string, value: any): Session;
|
|
134
|
+
flashes(key?: string): any[] | Record<string, any[]>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface SessionCookieOptions {
|
|
138
|
+
maxAge?: number;
|
|
139
|
+
path?: string;
|
|
140
|
+
domain?: string;
|
|
141
|
+
secure?: boolean;
|
|
142
|
+
httpOnly?: boolean;
|
|
143
|
+
sameSite?: 'Strict' | 'Lax' | 'None' | string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface SessionStore {
|
|
147
|
+
get(sid: string): Promise<string | null>;
|
|
148
|
+
set(sid: string, data: string, maxAge?: number): Promise<void>;
|
|
149
|
+
destroy(sid: string): Promise<void>;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface MemoryStoreOptions {
|
|
153
|
+
ttl?: number;
|
|
154
|
+
pruneInterval?: number;
|
|
155
|
+
maxSessions?: number;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export declare class MemoryStore implements SessionStore {
|
|
159
|
+
constructor(opts?: MemoryStoreOptions);
|
|
160
|
+
get(sid: string): Promise<string | null>;
|
|
161
|
+
set(sid: string, data: string, maxAge?: number): Promise<void>;
|
|
162
|
+
destroy(sid: string): Promise<void>;
|
|
163
|
+
readonly length: number;
|
|
164
|
+
clear(): void;
|
|
165
|
+
close(): void;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface SessionOptions {
|
|
169
|
+
secret: string | string[];
|
|
170
|
+
store?: SessionStore;
|
|
171
|
+
name?: string;
|
|
172
|
+
cookie?: SessionCookieOptions;
|
|
173
|
+
rolling?: boolean;
|
|
174
|
+
genid?: () => string;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function session(opts: SessionOptions): MiddlewareFunction;
|
|
178
|
+
|
|
179
|
+
// --- OAuth2 ------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
export interface OAuthProviderPreset {
|
|
182
|
+
authorizeUrl: string;
|
|
183
|
+
tokenUrl: string;
|
|
184
|
+
userInfoUrl: string | null;
|
|
185
|
+
scope: string;
|
|
186
|
+
pkce: boolean;
|
|
187
|
+
responseMode?: string;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface OAuthOptions {
|
|
191
|
+
provider?: 'google' | 'github' | 'microsoft' | 'apple';
|
|
192
|
+
clientId: string;
|
|
193
|
+
clientSecret: string;
|
|
194
|
+
callbackUrl: string;
|
|
195
|
+
authorizeUrl?: string;
|
|
196
|
+
tokenUrl?: string;
|
|
197
|
+
userInfoUrl?: string;
|
|
198
|
+
scope?: string;
|
|
199
|
+
pkce?: boolean;
|
|
200
|
+
responseMode?: string;
|
|
201
|
+
fetcher?: Function;
|
|
202
|
+
timeout?: number;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface OAuthAuthorizeResult {
|
|
206
|
+
url: string;
|
|
207
|
+
state: string;
|
|
208
|
+
codeVerifier?: string;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface OAuthTokens {
|
|
212
|
+
access_token: string;
|
|
213
|
+
token_type: string;
|
|
214
|
+
expires_in?: number;
|
|
215
|
+
refresh_token?: string;
|
|
216
|
+
id_token?: string;
|
|
217
|
+
scope?: string;
|
|
218
|
+
[key: string]: any;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface OAuthClient {
|
|
222
|
+
authorize(params?: { scope?: string; state?: string; extra?: Record<string, string> }): OAuthAuthorizeResult;
|
|
223
|
+
callback(query: { code?: string; state?: string; error?: string; error_description?: string }, verify?: { state?: string; codeVerifier?: string }): Promise<OAuthTokens>;
|
|
224
|
+
refresh(refreshToken: string): Promise<OAuthTokens>;
|
|
225
|
+
userInfo(accessToken: string): Promise<Record<string, any>>;
|
|
226
|
+
readonly config: Readonly<OAuthOptions>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function oauth(opts: OAuthOptions): OAuthClient;
|
|
230
|
+
export function generatePKCE(length?: number): { codeVerifier: string; codeChallenge: string };
|
|
231
|
+
export function generateState(bytes?: number): string;
|
|
232
|
+
export const OAUTH_PROVIDERS: Record<string, OAuthProviderPreset>;
|
|
233
|
+
|
|
234
|
+
// --- Authorization -----------------------------------------------
|
|
235
|
+
|
|
236
|
+
export function authorize(...roles: (string | string[])[]): MiddlewareFunction;
|
|
237
|
+
export function can(...permissions: (string | string[])[]): MiddlewareFunction;
|
|
238
|
+
export function canAny(...permissions: (string | string[])[]): MiddlewareFunction;
|
|
239
|
+
|
|
240
|
+
export declare class Policy {
|
|
241
|
+
before?(user: any, action: string, resource?: any): boolean | undefined;
|
|
242
|
+
check(action: string, user: any, resource?: any): boolean | Promise<boolean>;
|
|
243
|
+
[action: string]: any;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function gate(
|
|
247
|
+
policy: Policy,
|
|
248
|
+
action: string,
|
|
249
|
+
getResource?: (req: Request) => any | Promise<any>,
|
|
250
|
+
): MiddlewareFunction;
|
|
251
|
+
|
|
252
|
+
export function attachUserHelpers(): MiddlewareFunction;
|
|
253
|
+
|
|
254
|
+
// --- Two-Factor Authentication -----------------------------------
|
|
255
|
+
|
|
256
|
+
export interface TwoFactorSecret {
|
|
257
|
+
raw: Buffer;
|
|
258
|
+
base32: string;
|
|
259
|
+
hex: string;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export interface TOTPOptions {
|
|
263
|
+
period?: number;
|
|
264
|
+
digits?: number;
|
|
265
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
266
|
+
time?: number;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface TOTPVerifyOptions extends TOTPOptions {
|
|
270
|
+
window?: number;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export interface TOTPVerifyResult {
|
|
274
|
+
valid: boolean;
|
|
275
|
+
delta: number | null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export interface OtpauthURIOptions {
|
|
279
|
+
secret: string | Buffer;
|
|
280
|
+
issuer: string;
|
|
281
|
+
account: string;
|
|
282
|
+
period?: number;
|
|
283
|
+
digits?: number;
|
|
284
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export interface BackupCodesResult {
|
|
288
|
+
codes: string[];
|
|
289
|
+
hashes: string[];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export interface BackupCodeVerifyResult {
|
|
293
|
+
valid: boolean;
|
|
294
|
+
index: number | null;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export interface Require2FAOptions {
|
|
298
|
+
sessionKey?: string;
|
|
299
|
+
errorMessage?: string;
|
|
300
|
+
statusCode?: number;
|
|
301
|
+
isEnabled?: (req: Request) => boolean | Promise<boolean>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export interface VerifyTOTPMiddlewareOptions {
|
|
305
|
+
getSecret: (req: Request) => string | Buffer | Promise<string | Buffer>;
|
|
306
|
+
codeField?: string;
|
|
307
|
+
sessionKey?: string;
|
|
308
|
+
maxAttempts?: number;
|
|
309
|
+
lockoutMs?: number;
|
|
310
|
+
window?: number;
|
|
311
|
+
period?: number;
|
|
312
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
313
|
+
digits?: number;
|
|
314
|
+
replayStore?: ReplayStore;
|
|
315
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
316
|
+
onSuccess?: (req: Request, res: Response) => void;
|
|
317
|
+
onFailure?: (req: Request, res: Response, attemptsLeft: number) => void;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export interface ReplayStore {
|
|
321
|
+
get(userId: string): Promise<number | null>;
|
|
322
|
+
set(userId: string, counter: number, ttlMs: number): Promise<void>;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export declare class InMemoryReplayStore implements ReplayStore {
|
|
326
|
+
constructor();
|
|
327
|
+
get(userId: string): Promise<number | null>;
|
|
328
|
+
set(userId: string, counter: number, ttlMs: number): Promise<void>;
|
|
329
|
+
clear(): void;
|
|
330
|
+
destroy(): void;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export interface Verify2FAOptions {
|
|
334
|
+
getSecret: (req: Request) => string | Buffer | Promise<string | Buffer>;
|
|
335
|
+
getBackupHashes?: (req: Request) => string[] | Promise<string[]>;
|
|
336
|
+
onBackupUsed?: (req: Request, index: number) => void | Promise<void>;
|
|
337
|
+
getCredential?: (req: Request, credId: string) => any | Promise<any>;
|
|
338
|
+
updateCredentialCounter?: (req: Request, credId: string, newCounter: number) => void | Promise<void>;
|
|
339
|
+
expectedOrigin?: string;
|
|
340
|
+
expectedRPID?: string;
|
|
341
|
+
codeField?: string;
|
|
342
|
+
backupField?: string;
|
|
343
|
+
sessionKey?: string;
|
|
344
|
+
maxAttempts?: number;
|
|
345
|
+
lockoutMs?: number;
|
|
346
|
+
window?: number;
|
|
347
|
+
period?: number;
|
|
348
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
349
|
+
digits?: number;
|
|
350
|
+
replayStore?: ReplayStore;
|
|
351
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
352
|
+
onSuccess?: (req: Request, res: Response, method: 'totp' | 'backup' | 'webauthn') => void;
|
|
353
|
+
onFailure?: (req: Request, res: Response, attemptsLeft: number) => void;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export interface TwoFactor {
|
|
357
|
+
generateSecret(bytes?: number): TwoFactorSecret;
|
|
358
|
+
generateTOTP(secret: string | Buffer, opts?: TOTPOptions): string;
|
|
359
|
+
verifyTOTP(token: string, secret: string | Buffer, opts?: TOTPVerifyOptions): TOTPVerifyResult;
|
|
360
|
+
otpauthURI(opts: OtpauthURIOptions): string;
|
|
361
|
+
generateBackupCodes(count?: number, bytes?: number): BackupCodesResult;
|
|
362
|
+
verifyBackupCode(code: string, hashes: string[]): BackupCodeVerifyResult;
|
|
363
|
+
require2FA(opts?: Require2FAOptions): MiddlewareFunction;
|
|
364
|
+
verifyTOTPMiddleware(opts: VerifyTOTPMiddlewareOptions): MiddlewareFunction;
|
|
365
|
+
verify2FA(opts: Verify2FAOptions): MiddlewareFunction;
|
|
366
|
+
InMemoryReplayStore: typeof InMemoryReplayStore;
|
|
367
|
+
DEFAULT_PERIOD: number;
|
|
368
|
+
DEFAULT_DIGITS: number;
|
|
369
|
+
DEFAULT_ALGORITHM: string;
|
|
370
|
+
DEFAULT_WINDOW: number;
|
|
371
|
+
SUPPORTED_TOTP_ALGORITHMS: string[];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export const twoFactor: TwoFactor;
|
|
375
|
+
|
|
376
|
+
// --- WebAuthn / Passkeys -----------------------------------------
|
|
377
|
+
|
|
378
|
+
export interface WebAuthnRegistrationOptions {
|
|
379
|
+
rpName: string;
|
|
380
|
+
rpId: string;
|
|
381
|
+
userId: string;
|
|
382
|
+
userName: string;
|
|
383
|
+
userDisplayName?: string;
|
|
384
|
+
challenge?: Buffer;
|
|
385
|
+
challengeSize?: number;
|
|
386
|
+
attestation?: 'none' | 'direct' | 'indirect';
|
|
387
|
+
authenticatorSelection?: {
|
|
388
|
+
authenticatorAttachment?: 'platform' | 'cross-platform';
|
|
389
|
+
residentKey?: 'discouraged' | 'preferred' | 'required';
|
|
390
|
+
requireResidentKey?: boolean;
|
|
391
|
+
userVerification?: 'discouraged' | 'preferred' | 'required';
|
|
392
|
+
};
|
|
393
|
+
excludeCredentials?: Array<{ id: string | Buffer; type?: string; transports?: string[] }>;
|
|
394
|
+
timeout?: number;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export interface WebAuthnRegistrationResult {
|
|
398
|
+
verified: boolean;
|
|
399
|
+
credential: {
|
|
400
|
+
id: string;
|
|
401
|
+
publicKey: Buffer;
|
|
402
|
+
counter: number;
|
|
403
|
+
type: string;
|
|
404
|
+
transports?: string[];
|
|
405
|
+
};
|
|
406
|
+
authData: {
|
|
407
|
+
rpIdHash: Buffer;
|
|
408
|
+
flags: number;
|
|
409
|
+
signCount: number;
|
|
410
|
+
aaguid: Buffer;
|
|
411
|
+
credentialId: Buffer;
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export interface WebAuthnVerifyRegistrationOptions {
|
|
416
|
+
response: any;
|
|
417
|
+
expectedChallenge: string;
|
|
418
|
+
expectedOrigin: string;
|
|
419
|
+
expectedRPID: string;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export interface WebAuthnAuthenticationOptions {
|
|
423
|
+
rpId: string;
|
|
424
|
+
challenge?: Buffer;
|
|
425
|
+
challengeSize?: number;
|
|
426
|
+
allowCredentials?: Array<{ id: string | Buffer; type?: string; transports?: string[] }>;
|
|
427
|
+
userVerification?: 'discouraged' | 'preferred' | 'required';
|
|
428
|
+
timeout?: number;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export interface WebAuthnAuthenticationResult {
|
|
432
|
+
verified: boolean;
|
|
433
|
+
authData: {
|
|
434
|
+
rpIdHash: Buffer;
|
|
435
|
+
flags: number;
|
|
436
|
+
signCount: number;
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export interface WebAuthnVerifyAuthenticationOptions {
|
|
441
|
+
response: any;
|
|
442
|
+
expectedChallenge: string;
|
|
443
|
+
expectedOrigin: string;
|
|
444
|
+
expectedRPID: string;
|
|
445
|
+
credential: { publicKey: Buffer; counter: number; id?: string };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export interface WebAuthn {
|
|
449
|
+
generateRegistrationOptions(opts: WebAuthnRegistrationOptions): any;
|
|
450
|
+
verifyRegistration(opts: WebAuthnVerifyRegistrationOptions): Promise<WebAuthnRegistrationResult>;
|
|
451
|
+
generateAuthenticationOptions(opts: WebAuthnAuthenticationOptions): any;
|
|
452
|
+
verifyAuthentication(opts: WebAuthnVerifyAuthenticationOptions): Promise<WebAuthnAuthenticationResult>;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export const webauthn: WebAuthn;
|
|
456
|
+
|
|
457
|
+
// --- Trusted Device / Remember Me --------------------------------
|
|
458
|
+
|
|
459
|
+
export interface TrustedDeviceIssueOptions {
|
|
460
|
+
secret: string;
|
|
461
|
+
maxAge?: number;
|
|
462
|
+
cookieName?: string;
|
|
463
|
+
fingerprint?: (req: Request) => string | Promise<string>;
|
|
464
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export interface TrustedDeviceVerifyOptions {
|
|
468
|
+
secret: string;
|
|
469
|
+
previousSecrets?: string | string[];
|
|
470
|
+
cookieName?: string;
|
|
471
|
+
fingerprint?: (req: Request) => string | Promise<string>;
|
|
472
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
473
|
+
checkIP?: boolean;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export interface TrustedDeviceRevokeOptions {
|
|
477
|
+
cookieName?: string;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export interface TrustedDevice {
|
|
481
|
+
issue(opts: TrustedDeviceIssueOptions): MiddlewareFunction;
|
|
482
|
+
verify(opts: TrustedDeviceVerifyOptions): (req: Request) => Promise<boolean>;
|
|
483
|
+
revoke(opts?: TrustedDeviceRevokeOptions): MiddlewareFunction;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
export const trustedDevice: TrustedDevice;
|
|
487
|
+
|
|
488
|
+
// --- 2FA Enrollment Flow -----------------------------------------
|
|
489
|
+
|
|
490
|
+
export interface EnrollmentOptions {
|
|
491
|
+
saveSecret: (req: Request, secret: string, backupHashes: string[]) => Promise<void>;
|
|
492
|
+
removeSecret: (req: Request) => Promise<void>;
|
|
493
|
+
issuer?: string;
|
|
494
|
+
getAccount?: (req: Request) => string | Promise<string>;
|
|
495
|
+
sessionKey?: string;
|
|
496
|
+
ttl?: number;
|
|
497
|
+
backupCount?: number;
|
|
498
|
+
window?: number;
|
|
499
|
+
period?: number;
|
|
500
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
501
|
+
digits?: number;
|
|
502
|
+
isEnabled?: (req: Request) => boolean | Promise<boolean>;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export interface EnrollmentDisableOptions {
|
|
506
|
+
confirm?: (req: Request) => boolean | Promise<boolean>;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export interface EnrollmentVerifyOptions {
|
|
510
|
+
codeField?: string;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export interface EnrollmentFlow {
|
|
514
|
+
start(): MiddlewareFunction;
|
|
515
|
+
verify(opts?: EnrollmentVerifyOptions): MiddlewareFunction;
|
|
516
|
+
complete(): MiddlewareFunction;
|
|
517
|
+
disable(opts?: EnrollmentDisableOptions): MiddlewareFunction;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
export function enrollment(opts: EnrollmentOptions): EnrollmentFlow;
|
package/types/body.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Re-exports for @zero-server/body — body parser types
|
|
2
|
+
export {
|
|
3
|
+
BodyParserOptions,
|
|
4
|
+
JsonParserOptions,
|
|
5
|
+
UrlencodedParserOptions,
|
|
6
|
+
TextParserOptions,
|
|
7
|
+
MultipartOptions,
|
|
8
|
+
MultipartFile,
|
|
9
|
+
json,
|
|
10
|
+
urlencoded,
|
|
11
|
+
text,
|
|
12
|
+
raw,
|
|
13
|
+
multipart,
|
|
14
|
+
} from './middleware';
|
package/types/cli.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
import { Worker } from 'cluster';
|
|
4
|
+
|
|
5
|
+
export interface ClusterOptions {
|
|
6
|
+
/** Number of worker processes (default: CPU count). */
|
|
7
|
+
workers?: number;
|
|
8
|
+
/** Automatically respawn crashed workers (default: true). */
|
|
9
|
+
respawn?: boolean;
|
|
10
|
+
/** Initial delay (ms) before respawning (default: 1000). */
|
|
11
|
+
respawnDelay?: number;
|
|
12
|
+
/** Maximum respawn delay after backoff (default: 30000). */
|
|
13
|
+
maxRespawnDelay?: number;
|
|
14
|
+
/** Multiplier for exponential backoff (default: 2). */
|
|
15
|
+
backoffFactor?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export declare class ClusterManager {
|
|
19
|
+
constructor(opts?: ClusterOptions);
|
|
20
|
+
|
|
21
|
+
/** Whether this is the primary (master) process. */
|
|
22
|
+
readonly isPrimary: boolean;
|
|
23
|
+
|
|
24
|
+
/** Whether this is a worker process. */
|
|
25
|
+
readonly isWorker: boolean;
|
|
26
|
+
|
|
27
|
+
/** Configured number of workers. */
|
|
28
|
+
readonly workerCount: number;
|
|
29
|
+
|
|
30
|
+
/** IDs of currently active workers. */
|
|
31
|
+
readonly workerIds: number[];
|
|
32
|
+
|
|
33
|
+
/** Number of currently alive workers. */
|
|
34
|
+
readonly activeWorkers: number;
|
|
35
|
+
|
|
36
|
+
/** Fork all worker processes. */
|
|
37
|
+
fork(): ClusterManager;
|
|
38
|
+
|
|
39
|
+
/** Broadcast a typed message to all workers. */
|
|
40
|
+
broadcast(type: string, data: any): void;
|
|
41
|
+
|
|
42
|
+
/** Send a typed message to a specific worker. */
|
|
43
|
+
sendTo(workerId: number, type: string, data: any): void;
|
|
44
|
+
|
|
45
|
+
/** Send a typed message from a worker to the primary. */
|
|
46
|
+
sendToPrimary(type: string, data: any): void;
|
|
47
|
+
|
|
48
|
+
/** Register a handler for a typed IPC message. */
|
|
49
|
+
onMessage(type: string, fn: (data: any, worker?: Worker) => void): ClusterManager;
|
|
50
|
+
|
|
51
|
+
/** Perform a rolling restart of all workers. */
|
|
52
|
+
reload(): Promise<void>;
|
|
53
|
+
|
|
54
|
+
/** Shut down the entire cluster gracefully. */
|
|
55
|
+
shutdown(opts?: { timeout?: number }): Promise<void>;
|
|
56
|
+
|
|
57
|
+
/** Enable automatic per-worker metrics aggregation. */
|
|
58
|
+
enableMetrics(registry: import('./observe').MetricsRegistry, opts?: { interval?: number }): ClusterManager;
|
|
59
|
+
|
|
60
|
+
/** Stop the per-worker metrics reporting timer. */
|
|
61
|
+
disableMetrics(): void;
|
|
62
|
+
|
|
63
|
+
/** Enable sticky sessions by hashing client IPs to workers. */
|
|
64
|
+
enableSticky(server: import('http').Server | import('https').Server, opts?: {
|
|
65
|
+
hash?: (ip: string, workerCount: number) => number;
|
|
66
|
+
}): ClusterManager;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* High-level clustering helper.
|
|
71
|
+
*/
|
|
72
|
+
export declare function cluster(
|
|
73
|
+
workerFn: (mgr: ClusterManager) => void,
|
|
74
|
+
opts?: ClusterOptions
|
|
75
|
+
): ClusterManager;
|
package/types/env.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// --- Env Field Definition ----------------------------------------
|
|
2
|
+
|
|
3
|
+
export interface EnvFieldDef {
|
|
4
|
+
/** Value type for coercion. */
|
|
5
|
+
type?: 'string' | 'number' | 'integer' | 'boolean' | 'port' | 'array' | 'json' | 'url' | 'enum';
|
|
6
|
+
/** Field is required. */
|
|
7
|
+
required?: boolean;
|
|
8
|
+
/** Default value (or function returning a value). */
|
|
9
|
+
default?: any | (() => any);
|
|
10
|
+
/** Min value (number/integer) or min length (string). */
|
|
11
|
+
min?: number;
|
|
12
|
+
/** Max value (number/integer) or max length (string). */
|
|
13
|
+
max?: number;
|
|
14
|
+
/** Pattern match (string type). */
|
|
15
|
+
match?: RegExp;
|
|
16
|
+
/** Delimiter for array type. Default: ','. */
|
|
17
|
+
separator?: string;
|
|
18
|
+
/** Allowed values for enum type. */
|
|
19
|
+
values?: string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type EnvSchema = Record<string, EnvFieldDef>;
|
|
23
|
+
|
|
24
|
+
export interface EnvLoadOptions {
|
|
25
|
+
/** Directory to load .env files from. Default: `process.cwd()`. */
|
|
26
|
+
path?: string;
|
|
27
|
+
/** Write file values into `process.env`. Default: false. */
|
|
28
|
+
override?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// --- Env Proxy ---------------------------------------------------
|
|
32
|
+
|
|
33
|
+
export interface Env {
|
|
34
|
+
/**
|
|
35
|
+
* Get a variable by key.
|
|
36
|
+
*/
|
|
37
|
+
(key: string): any;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Load environment variables, optionally with a typed schema.
|
|
41
|
+
*/
|
|
42
|
+
load(schema?: EnvSchema, options?: EnvLoadOptions): Record<string, any>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get a variable by key.
|
|
46
|
+
*/
|
|
47
|
+
get(key: string): any;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get a variable by key, throwing if missing.
|
|
51
|
+
*/
|
|
52
|
+
require(key: string): any;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Check if a variable exists (in store or `process.env`).
|
|
56
|
+
*/
|
|
57
|
+
has(key: string): boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Return all loaded variables.
|
|
61
|
+
*/
|
|
62
|
+
all(): Record<string, any>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Clear all loaded variables from the internal store.
|
|
66
|
+
*/
|
|
67
|
+
reset(): void;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Parse a raw `.env` string into key-value pairs.
|
|
71
|
+
*/
|
|
72
|
+
parse(src: string): Record<string, string>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Property access for variables (e.g., `env.PORT`).
|
|
76
|
+
*/
|
|
77
|
+
[key: string]: any;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const env: Env;
|