@radatek/microserver 2.0.2 → 2.2.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/microserver.d.ts +72 -35
- package/dist/microserver.js +349 -243
- package/microserver.ts +3803 -0
- package/package.json +9 -4
- package/readme.md +50 -47
package/dist/microserver.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.0
|
|
3
|
+
* @version 2.2.0
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import http from 'http';
|
|
9
9
|
import net from 'net';
|
|
10
10
|
import { Readable } from 'stream';
|
|
11
|
+
import fs from 'fs';
|
|
11
12
|
import { EventEmitter } from 'events';
|
|
12
13
|
export declare class Warning extends Error {
|
|
13
14
|
constructor(text: string);
|
|
@@ -39,9 +40,9 @@ export type Routes = () => {
|
|
|
39
40
|
export declare abstract class Plugin {
|
|
40
41
|
name?: string;
|
|
41
42
|
priority?: number;
|
|
42
|
-
handler?(req: ServerRequest, res: ServerResponse, next: Function): void;
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
handler?(req: ServerRequest, res: ServerResponse, next: Function): Promise<string | object | void> | string | object | void;
|
|
44
|
+
routes?(): Promise<Routes> | Routes;
|
|
45
|
+
initialise?(): Promise<void> | void;
|
|
45
46
|
constructor(router: Router, ...args: any);
|
|
46
47
|
}
|
|
47
48
|
interface PluginClass {
|
|
@@ -65,8 +66,8 @@ export declare class ServerRequest extends http.IncomingMessage {
|
|
|
65
66
|
baseUrl: string;
|
|
66
67
|
/** Original url */
|
|
67
68
|
originalUrl?: string;
|
|
68
|
-
/**
|
|
69
|
-
|
|
69
|
+
/** Query parameters */
|
|
70
|
+
query: {
|
|
70
71
|
[key: string]: string;
|
|
71
72
|
};
|
|
72
73
|
/** Router named parameters */
|
|
@@ -117,17 +118,20 @@ export declare class ServerResponse extends http.ServerResponse {
|
|
|
117
118
|
headersOnly: boolean;
|
|
118
119
|
private constructor();
|
|
119
120
|
/** Send error reponse */
|
|
120
|
-
error(error: string | number | Error
|
|
121
|
+
error(error: string | number | Error): void;
|
|
121
122
|
/** Sets Content-Type acording to data and sends response */
|
|
122
123
|
send(data?: string | Buffer | Error | Readable | object): void;
|
|
123
124
|
/** Send json response */
|
|
124
125
|
json(data: any): void;
|
|
125
126
|
/** Send json response in form { success: false, error: err } */
|
|
126
|
-
jsonError(error: string | number | object | Error
|
|
127
|
+
jsonError(error: string | number | object | Error): void;
|
|
127
128
|
/** Send json response in form { success: true, ... } */
|
|
128
|
-
jsonSuccess(data?: object | string
|
|
129
|
+
jsonSuccess(data?: object | string): void;
|
|
129
130
|
/** Send redirect response to specified URL with optional status code (default: 302) */
|
|
130
131
|
redirect(code: number | string, url?: string): void;
|
|
132
|
+
/** Set status code */
|
|
133
|
+
status(code: number): this;
|
|
134
|
+
download(path: string, filename?: string): void;
|
|
131
135
|
}
|
|
132
136
|
/** WebSocket options */
|
|
133
137
|
export interface WebSocketOptions {
|
|
@@ -219,6 +223,14 @@ export interface Middleware {
|
|
|
219
223
|
priority?: number;
|
|
220
224
|
plugin?: Plugin;
|
|
221
225
|
}
|
|
226
|
+
declare class Waiter {
|
|
227
|
+
get busy(): boolean;
|
|
228
|
+
startJob(): void;
|
|
229
|
+
endJob(id?: string): void;
|
|
230
|
+
get nextId(): string;
|
|
231
|
+
wait(id: string): Promise<void>;
|
|
232
|
+
resolve(id: string): void;
|
|
233
|
+
}
|
|
222
234
|
/** Router */
|
|
223
235
|
export declare class Router extends EventEmitter {
|
|
224
236
|
server: MicroServer;
|
|
@@ -226,8 +238,11 @@ export declare class Router extends EventEmitter {
|
|
|
226
238
|
plugins: {
|
|
227
239
|
[key: string]: Plugin;
|
|
228
240
|
};
|
|
241
|
+
_waiter: Waiter;
|
|
229
242
|
/** @param {MicroServer} server */
|
|
230
243
|
constructor(server: MicroServer);
|
|
244
|
+
/** bind middleware or create one from string like: 'redirect:302,https://redirect.to', 'error:422', 'param:name=value', 'acl:users/get', 'model:User', 'group:Users', 'user:admin' */
|
|
245
|
+
bind(fn: string | Function | object): Function;
|
|
231
246
|
/** Handler */
|
|
232
247
|
handler(req: ServerRequest, res: ServerResponse, next: Function, method?: string): any;
|
|
233
248
|
/** Clear routes and middlewares */
|
|
@@ -238,54 +253,55 @@ export declare class Router extends EventEmitter {
|
|
|
238
253
|
*
|
|
239
254
|
* @signature add(plugin: Plugin)
|
|
240
255
|
* @param {Plugin} plugin plugin module instance
|
|
241
|
-
* @return {
|
|
256
|
+
* @return {Promise<>}
|
|
242
257
|
*
|
|
243
258
|
* @signature add(pluginid: string, ...args: any)
|
|
244
259
|
* @param {string} pluginid pluginid module
|
|
245
260
|
* @param {...any} args arguments passed to constructor
|
|
246
|
-
* @return {
|
|
261
|
+
* @return {Promise<>}
|
|
247
262
|
*
|
|
248
263
|
* @signature add(pluginClass: typeof Plugin, ...args: any)
|
|
249
264
|
* @param {typeof Plugin} pluginClass plugin class
|
|
250
265
|
* @param {...any} args arguments passed to constructor
|
|
251
|
-
* @return {
|
|
266
|
+
* @return {Promise<>}
|
|
252
267
|
*
|
|
253
268
|
* @signature add(middleware: Middleware)
|
|
254
269
|
* @param {Middleware} middleware
|
|
255
|
-
* @return {
|
|
270
|
+
* @return {Promise<>}
|
|
256
271
|
*
|
|
257
272
|
* @signature add(methodUrl: string, ...middlewares: any)
|
|
258
273
|
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
259
274
|
* @param {...any} middlewares
|
|
260
|
-
* @return {
|
|
275
|
+
* @return {Promise<>}
|
|
261
276
|
*
|
|
262
277
|
* @signature add(methodUrl: string, controllerClass: typeof Controller)
|
|
263
278
|
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
264
279
|
* @param {typeof Controller} controllerClass
|
|
265
|
-
* @return {
|
|
280
|
+
* @return {Promise<>}
|
|
266
281
|
*
|
|
267
282
|
* @signature add(methodUrl: string, routes: Array<Array<any>>)
|
|
268
283
|
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
269
284
|
* @param {Array<Array<any>>} routes list with subroutes: ['METHOD /suburl', ...middlewares]
|
|
270
|
-
* @return {
|
|
285
|
+
* @return {Promise<>}
|
|
271
286
|
*
|
|
272
287
|
* @signature add(methodUrl: string, routes: Array<Array<any>>)
|
|
273
288
|
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
274
289
|
* @param {Array<Array<any>>} routes list with subroutes: ['METHOD /suburl', ...middlewares]
|
|
275
|
-
* @return {
|
|
290
|
+
* @return {Promise<>}
|
|
276
291
|
*
|
|
277
292
|
* @signature add(routes: { [key: string]: Array<any> })
|
|
278
293
|
* @param { {[key: string]: Array<any>} } routes list with subroutes: 'METHOD /suburl': [...middlewares]
|
|
279
|
-
* @return {
|
|
294
|
+
* @return {Promise<>}
|
|
280
295
|
*
|
|
281
296
|
* @signature add(methodUrl: string, routes: { [key: string]: Array<any> })
|
|
282
297
|
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
283
298
|
* @param { {[key: string]: Array<any>} } routes list with subroutes: 'METHOD /suburl': [...middlewares]
|
|
284
|
-
* @return {
|
|
299
|
+
* @return {Promise<>}
|
|
285
300
|
*/
|
|
286
|
-
use(...args: any):
|
|
301
|
+
use(...args: any): Promise<void>;
|
|
302
|
+
waitPlugin(id: string): Promise<Plugin>;
|
|
287
303
|
/** Add hook */
|
|
288
|
-
hook(url: string, ...mid: Middleware[]):
|
|
304
|
+
hook(url: string, ...mid: Middleware[]): void;
|
|
289
305
|
/** Check if middleware allready added */
|
|
290
306
|
has(mid: Middleware): boolean;
|
|
291
307
|
}
|
|
@@ -361,23 +377,22 @@ export declare class MicroServer extends EventEmitter {
|
|
|
361
377
|
sockets: Set<net.Socket>;
|
|
362
378
|
/** server instances */
|
|
363
379
|
servers: Set<net.Server>;
|
|
380
|
+
_waiter: Waiter;
|
|
364
381
|
static plugins: {
|
|
365
382
|
[key: string]: PluginClass;
|
|
366
383
|
};
|
|
367
|
-
get plugins(): {
|
|
368
|
-
[key: string]: Plugin;
|
|
369
|
-
};
|
|
370
384
|
constructor(config: MicroServerConfig);
|
|
371
385
|
/** Add one time listener or call immediatelly for 'ready' */
|
|
372
386
|
once(name: string, cb: Function): this;
|
|
373
387
|
/** Add listener and call immediatelly for 'ready' */
|
|
374
388
|
on(name: string, cb: Function): this;
|
|
389
|
+
isReady(): boolean;
|
|
390
|
+
waitReady(): Promise<void>;
|
|
391
|
+
waitPlugin(id: string): Promise<void>;
|
|
375
392
|
/** Listen server, should be used only if config.listen is not set */
|
|
376
|
-
listen(config?: ListenConfig): Promise<
|
|
377
|
-
/**
|
|
378
|
-
|
|
379
|
-
/** Add middleware, routes, etc.. see {Router.add} */
|
|
380
|
-
use(...args: any): MicroServer;
|
|
393
|
+
listen(config?: ListenConfig): Promise<void>;
|
|
394
|
+
/** Add middleware, routes, etc.. see {router.use} */
|
|
395
|
+
use(...args: any): Promise<void>;
|
|
381
396
|
/** Default server handler */
|
|
382
397
|
handler(req: ServerRequest, res: ServerResponse): void;
|
|
383
398
|
protected requestInit(req: ServerRequest, res?: ServerResponse): void;
|
|
@@ -389,17 +404,19 @@ export declare class MicroServer extends EventEmitter {
|
|
|
389
404
|
handlerUpgrade(req: ServerRequest, socket: net.Socket, head: any): void;
|
|
390
405
|
/** Close server instance */
|
|
391
406
|
close(): Promise<void>;
|
|
392
|
-
/** Add route, alias to `server.router.
|
|
407
|
+
/** Add route, alias to `server.router.use(url, ...args)` */
|
|
408
|
+
all(url: string, ...args: any): MicroServer;
|
|
409
|
+
/** Add route, alias to `server.router.use('GET ' + url, ...args)` */
|
|
393
410
|
get(url: string, ...args: any): MicroServer;
|
|
394
|
-
/** Add route, alias to `server.router.
|
|
411
|
+
/** Add route, alias to `server.router.use('POST ' + url, ...args)` */
|
|
395
412
|
post(url: string, ...args: any): MicroServer;
|
|
396
|
-
/** Add route, alias to `server.router.
|
|
413
|
+
/** Add route, alias to `server.router.use('PUT ' + url, ...args)` */
|
|
397
414
|
put(url: string, ...args: any): MicroServer;
|
|
398
|
-
/** Add route, alias to `server.router.
|
|
415
|
+
/** Add route, alias to `server.router.use('PATCH ' + url, ...args)` */
|
|
399
416
|
patch(url: string, ...args: any): MicroServer;
|
|
400
|
-
/** Add route, alias to `server.router.
|
|
417
|
+
/** Add route, alias to `server.router.use('DELETE ' + url, ...args)` */
|
|
401
418
|
delete(url: string, ...args: any): MicroServer;
|
|
402
|
-
/** Add websocket handler, alias to `server.router.
|
|
419
|
+
/** Add websocket handler, alias to `server.router.use('WEBSOCKET ' + url, ...args)` */
|
|
403
420
|
websocket(url: string, ...args: any): MicroServer;
|
|
404
421
|
/** Add router hook, alias to `server.router.hook(url, ...args)` */
|
|
405
422
|
hook(url: string, ...args: any): MicroServer;
|
|
@@ -429,6 +446,26 @@ export interface StaticOptions {
|
|
|
429
446
|
/** Max file age in seconds */
|
|
430
447
|
maxAge?: number;
|
|
431
448
|
}
|
|
449
|
+
export interface ServeFileOptions {
|
|
450
|
+
/** path */
|
|
451
|
+
path: string;
|
|
452
|
+
/** root */
|
|
453
|
+
root?: string;
|
|
454
|
+
/** file name */
|
|
455
|
+
filename?: string;
|
|
456
|
+
/** file mime type */
|
|
457
|
+
mimeType?: string;
|
|
458
|
+
/** last modified date */
|
|
459
|
+
lastModified?: boolean;
|
|
460
|
+
/** etag */
|
|
461
|
+
etag?: boolean;
|
|
462
|
+
/** max age */
|
|
463
|
+
maxAge?: number;
|
|
464
|
+
/** range */
|
|
465
|
+
range?: boolean;
|
|
466
|
+
/** stat */
|
|
467
|
+
stats?: fs.Stats;
|
|
468
|
+
}
|
|
432
469
|
/** Proxy plugin options */
|
|
433
470
|
export interface ProxyPluginOptions {
|
|
434
471
|
/** Base path */
|