@radatek/microserver 2.3.11 → 3.0.1
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/microserver.d.ts +332 -261
- package/microserver.js +1673 -1494
- package/package.json +2 -2
package/microserver.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version
|
|
3
|
+
* @version 3.0.0
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -32,25 +32,31 @@ export declare class WebSocketError extends Error {
|
|
|
32
32
|
statusCode: number;
|
|
33
33
|
constructor(text?: string, code?: number);
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
type DeferPromise<T = void> = Promise<T> & {
|
|
36
|
+
resolve: (res?: T | Error) => void;
|
|
37
|
+
};
|
|
38
|
+
/** Middleware */
|
|
39
|
+
export interface Middleware {
|
|
40
|
+
(req: ServerRequest, res: ServerResponse, next: Function): any;
|
|
41
|
+
/** @default 0 */
|
|
42
|
+
priority?: number;
|
|
43
|
+
plugin?: Plugin;
|
|
44
|
+
}
|
|
36
45
|
export declare abstract class Plugin {
|
|
37
46
|
name?: string;
|
|
38
47
|
priority?: number;
|
|
39
48
|
handler?(req: ServerRequest, res: ServerResponse, next: Function): Promise<string | object | void> | string | object | void;
|
|
40
|
-
routes?(): Promise<
|
|
41
|
-
|
|
42
|
-
constructor(router: Router, ...args: any);
|
|
49
|
+
routes?(): Promise<RoutesSet | void> | RoutesSet | void;
|
|
50
|
+
constructor();
|
|
43
51
|
}
|
|
44
52
|
interface PluginClass {
|
|
45
|
-
new (
|
|
53
|
+
new (options?: any, server?: MicroServer): Plugin;
|
|
46
54
|
}
|
|
47
55
|
export type ServerRequestBody<T = any> = T extends Model<infer U extends ModelSchema> ? ModelDocument<U> : Record<string, any>;
|
|
48
56
|
/** Extended http.IncomingMessage */
|
|
49
57
|
export declare class ServerRequest<T = any> extends http.IncomingMessage {
|
|
50
|
-
/** Request protocol: http or https */
|
|
51
|
-
protocol: string;
|
|
52
58
|
/** Request client IP */
|
|
53
|
-
ip
|
|
59
|
+
ip: string;
|
|
54
60
|
/** Request from local network */
|
|
55
61
|
localip?: boolean;
|
|
56
62
|
/** Request is secure (https) */
|
|
@@ -70,7 +76,7 @@ export declare class ServerRequest<T = any> extends http.IncomingMessage {
|
|
|
70
76
|
/** Router named parameters list */
|
|
71
77
|
paramsList: string[];
|
|
72
78
|
/** Router */
|
|
73
|
-
|
|
79
|
+
server: MicroServer;
|
|
74
80
|
/** Authentication object */
|
|
75
81
|
auth?: Auth;
|
|
76
82
|
/** Authenticated user info */
|
|
@@ -83,9 +89,14 @@ export declare class ServerRequest<T = any> extends http.IncomingMessage {
|
|
|
83
89
|
rawBody: Buffer[];
|
|
84
90
|
/** Request raw body size */
|
|
85
91
|
rawBodySize: number;
|
|
92
|
+
_body?: ServerRequestBody<T>;
|
|
93
|
+
_isReady: DeferPromise | undefined;
|
|
86
94
|
private constructor();
|
|
95
|
+
static extend(req: http.IncomingMessage, server: MicroServer): ServerRequest;
|
|
96
|
+
get isReady(): boolean;
|
|
97
|
+
waitReady(): Promise<void>;
|
|
87
98
|
/** Update request url */
|
|
88
|
-
updateUrl(url: string):
|
|
99
|
+
updateUrl(url: string): this;
|
|
89
100
|
/** Rewrite request url */
|
|
90
101
|
rewrite(url: string): void;
|
|
91
102
|
/** Request body: JSON or POST parameters */
|
|
@@ -95,17 +106,15 @@ export declare class ServerRequest<T = any> extends http.IncomingMessage {
|
|
|
95
106
|
/** Get websocket */
|
|
96
107
|
get websocket(): WebSocket;
|
|
97
108
|
/** get files list in request */
|
|
98
|
-
files(): Promise<
|
|
99
|
-
/** Decode request body */
|
|
100
|
-
bodyDecode(res: ServerResponse, options: any, next: () => void): void;
|
|
109
|
+
files(): Promise<UploadFile[] | undefined>;
|
|
101
110
|
}
|
|
102
111
|
/** Extends http.ServerResponse */
|
|
103
112
|
export declare class ServerResponse<T = any> extends http.ServerResponse {
|
|
104
|
-
req: ServerRequest<T>;
|
|
105
|
-
router: Router;
|
|
113
|
+
readonly req: ServerRequest<T>;
|
|
106
114
|
isJson: boolean;
|
|
107
115
|
headersOnly: boolean;
|
|
108
116
|
private constructor();
|
|
117
|
+
static extend(res: http.ServerResponse): void;
|
|
109
118
|
/** Send error reponse */
|
|
110
119
|
error(error: string | number | Error): void;
|
|
111
120
|
/** Sets Content-Type acording to data and sends response */
|
|
@@ -118,179 +127,104 @@ export declare class ServerResponse<T = any> extends http.ServerResponse {
|
|
|
118
127
|
jsonSuccess(data?: object | string): void;
|
|
119
128
|
/** Send redirect response to specified URL with optional status code (default: 302) */
|
|
120
129
|
redirect(code: number | string, url?: string): void;
|
|
130
|
+
/** Rewrite URL */
|
|
131
|
+
rewrite(url: string): void;
|
|
121
132
|
/** Set status code */
|
|
122
133
|
status(code: number): this;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* ```js
|
|
155
|
-
* class MyController extends Controller {
|
|
156
|
-
* static model = MyModel;
|
|
157
|
-
* static acl = 'auth';
|
|
158
|
-
*
|
|
159
|
-
* static 'acl:index' = '';
|
|
160
|
-
* static 'url:index' = 'GET /index';
|
|
161
|
-
* async index (req, res) {
|
|
162
|
-
* res.send('Hello World')
|
|
163
|
-
* }
|
|
164
|
-
*
|
|
165
|
-
* //function name prefixes translated to HTTP methods:
|
|
166
|
-
* // all => GET, get => GET, insert => POST, post => POST,
|
|
167
|
-
* // update => PUT, put => PUT, delete => DELETE,
|
|
168
|
-
* // modify => PATCH, patch => PATCH,
|
|
169
|
-
* // websocket => internal WebSocket
|
|
170
|
-
* // automatic acl will be: class_name + '/' + function_name_prefix
|
|
171
|
-
* // automatic url will be: method + ' /' + class_name + '/' + function_name_without_prefix
|
|
172
|
-
*
|
|
173
|
-
* //static 'acl:allUsers' = 'MyController/all';
|
|
174
|
-
* //static 'url:allUsers' = 'GET /MyController/Users';
|
|
175
|
-
* async allUsers () {
|
|
176
|
-
* return ['usr1', 'usr2', 'usr3']
|
|
177
|
-
* }
|
|
178
|
-
*
|
|
179
|
-
* //static 'acl:getOrder' = 'MyController/get';
|
|
180
|
-
* //static 'url:getOrder' = 'GET /Users/:id/:id1';
|
|
181
|
-
* static 'group:getOrder' = 'orders';
|
|
182
|
-
* static 'model:getOrder' = OrderModel;
|
|
183
|
-
* async getOrder (id: string, id1: string) {
|
|
184
|
-
* return {id, extras: id1, type: 'order'}
|
|
185
|
-
* }
|
|
186
|
-
*
|
|
187
|
-
* //static 'acl:insertOrder' = 'MyController/insert';
|
|
188
|
-
* //static 'url:insertOrder' = 'POST /Users/:id';
|
|
189
|
-
* static 'model:insertOrder' = OrderModel;
|
|
190
|
-
* async insertOrder (id: string, id1: string) {
|
|
191
|
-
* return {id, extras: id1, type: 'order'}
|
|
192
|
-
* }
|
|
193
|
-
*
|
|
194
|
-
* static 'acl:POST /login' = '';
|
|
195
|
-
* async 'POST /login' () {
|
|
196
|
-
* return {id, extras: id1, type: 'order'}
|
|
197
|
-
* }
|
|
198
|
-
* }
|
|
199
|
-
* ```
|
|
200
|
-
*/
|
|
201
|
-
export declare class Controller<T extends Model<any> = any> {
|
|
202
|
-
req: ServerRequest<T>;
|
|
203
|
-
res: ServerResponse<T>;
|
|
204
|
-
get model(): T | undefined;
|
|
205
|
-
constructor(req: ServerRequest<T>, res: ServerResponse<T>);
|
|
206
|
-
/** Generate routes for this controller */
|
|
207
|
-
static routes(): any[];
|
|
208
|
-
}
|
|
209
|
-
/** Middleware */
|
|
210
|
-
export interface Middleware {
|
|
211
|
-
(req: ServerRequest, res: ServerResponse, next: Function): any;
|
|
212
|
-
/** @default 0 */
|
|
213
|
-
priority?: number;
|
|
214
|
-
plugin?: Plugin;
|
|
134
|
+
/** Send file */
|
|
135
|
+
file(path: string | ServeFileOptions): void;
|
|
136
|
+
}
|
|
137
|
+
type RouteURL = `${Uppercase<string>} /${string}` | `/${string}`;
|
|
138
|
+
type StringMiddleware = `response:json` | `response:html` | `response:end` | `redirect:${string}` | `error:${number}` | `param:${string}=${string}` | `acl:${string}` | `user:${string}` | `group:${string}` | `model:${string}` | `${string}=${string}`;
|
|
139
|
+
type RoutesMiddleware = Middleware | Plugin | StringMiddleware | Promise<Middleware | Plugin>;
|
|
140
|
+
type RoutesList = [RouteURL, ...Array<RoutesMiddleware>] | [RouteURL, ControllerClass | Promise<ControllerClass>];
|
|
141
|
+
type RoutesSet = Array<RoutesList | ControllerClass | Promise<ControllerClass>> | Record<RouteURL, RoutesList | ControllerClass | Promise<ControllerClass> | Middleware>;
|
|
142
|
+
export interface MicroServerConfig extends ListenConfig {
|
|
143
|
+
/** Auth options */
|
|
144
|
+
auth?: AuthOptions;
|
|
145
|
+
/** routes to add */
|
|
146
|
+
routes?: RoutesSet;
|
|
147
|
+
/** Static file options */
|
|
148
|
+
static?: StaticFilesOptions;
|
|
149
|
+
/** max body size (default: 5MB) */
|
|
150
|
+
body?: BodyOptions;
|
|
151
|
+
/** allowed HTTP methods */
|
|
152
|
+
methods?: string;
|
|
153
|
+
/** trust proxy */
|
|
154
|
+
trustproxy?: string[];
|
|
155
|
+
/** cors options */
|
|
156
|
+
cors?: string | CorsOptions | boolean;
|
|
157
|
+
/** upload dir */
|
|
158
|
+
upload?: UploadOptions;
|
|
159
|
+
/** websocket options */
|
|
160
|
+
websocket?: WebSocketOptions;
|
|
161
|
+
/** extra options for plugins */
|
|
162
|
+
[key: string]: any;
|
|
215
163
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
164
|
+
interface MicroServerEvents {
|
|
165
|
+
ready: () => void;
|
|
166
|
+
close: () => void;
|
|
167
|
+
listen: (port: number, address: string, server: http.Server) => void;
|
|
168
|
+
error: (err: Error) => void;
|
|
169
|
+
[key: `plugin:${string}`]: (plugin: Plugin) => void;
|
|
222
170
|
}
|
|
223
|
-
/**
|
|
224
|
-
export declare class
|
|
225
|
-
|
|
171
|
+
/** Lighweight HTTP server */
|
|
172
|
+
export declare class MicroServer extends EventEmitter {
|
|
173
|
+
config: MicroServerConfig;
|
|
226
174
|
auth?: Auth;
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
|
|
175
|
+
/** all sockets */
|
|
176
|
+
sockets: Set<net.Socket> | undefined;
|
|
177
|
+
/** server instances */
|
|
178
|
+
servers: Set<net.Server> | undefined;
|
|
179
|
+
/** @param {MicroServerConfig} [config] MicroServer configuration */
|
|
180
|
+
constructor(config?: MicroServerConfig);
|
|
181
|
+
on<K extends keyof MicroServerEvents>(event: K, listener: MicroServerEvents[K]): this;
|
|
182
|
+
addListener<K extends keyof MicroServerEvents>(event: K, listener: MicroServerEvents[K]): this;
|
|
183
|
+
once<K extends keyof MicroServerEvents>(event: K, listener: MicroServerEvents[K]): this;
|
|
184
|
+
off<K extends keyof MicroServerEvents>(event: K, listener: MicroServerEvents[K]): this;
|
|
185
|
+
removeListener<K extends keyof MicroServerEvents>(event: K, listener: MicroServerEvents[K]): this;
|
|
186
|
+
get isReady(): boolean;
|
|
187
|
+
waitReady(): Promise<void>;
|
|
188
|
+
/** Listen server, should be used only if config.listen is not set */
|
|
189
|
+
listen(config: ListenConfig): Promise<void>;
|
|
231
190
|
/** 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' */
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
handler(req: ServerRequest, res: ServerResponse
|
|
191
|
+
_bind(fn: string | Function | object): Function;
|
|
192
|
+
/** Default server handler */
|
|
193
|
+
handler(req: ServerRequest, res: ServerResponse): void;
|
|
194
|
+
/** Last request handler */
|
|
195
|
+
handlerLast(req: ServerRequest, res: ServerResponse, next?: Function): any;
|
|
235
196
|
/** Clear routes and middlewares */
|
|
236
197
|
clear(): this;
|
|
237
198
|
/**
|
|
238
199
|
* Add middleware route.
|
|
239
200
|
* Middlewares may return promises for res.jsonSuccess(...), throw errors for res.error(...), return string or {} for res.send(...)
|
|
240
|
-
*
|
|
241
|
-
* @signature add(plugin: Plugin)
|
|
242
|
-
* @param {Plugin} plugin plugin module instance
|
|
243
|
-
* @return {Promise<>}
|
|
244
|
-
*
|
|
245
|
-
* @signature add(pluginid: string, ...args: any)
|
|
246
|
-
* @param {string} pluginid pluginid module
|
|
247
|
-
* @param {...any} args arguments passed to constructor
|
|
248
|
-
* @return {Promise<>}
|
|
249
|
-
*
|
|
250
|
-
* @signature add(pluginClass: typeof Plugin, ...args: any)
|
|
251
|
-
* @param {typeof Plugin} pluginClass plugin class
|
|
252
|
-
* @param {...any} args arguments passed to constructor
|
|
253
|
-
* @return {Promise<>}
|
|
254
|
-
*
|
|
255
|
-
* @signature add(middleware: Middleware)
|
|
256
|
-
* @param {Middleware} middleware
|
|
257
|
-
* @return {Promise<>}
|
|
258
|
-
*
|
|
259
|
-
* @signature add(methodUrl: string, ...middlewares: any)
|
|
260
|
-
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
261
|
-
* @param {...any} middlewares
|
|
262
|
-
* @return {Promise<>}
|
|
263
|
-
*
|
|
264
|
-
* @signature add(methodUrl: string, controllerClass: typeof Controller)
|
|
265
|
-
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
266
|
-
* @param {typeof Controller} controllerClass
|
|
267
|
-
* @return {Promise<>}
|
|
268
|
-
*
|
|
269
|
-
* @signature add(methodUrl: string, routes: Array<Array<any>>)
|
|
270
|
-
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
271
|
-
* @param {Array<Array<any>>} routes list with subroutes: ['METHOD /suburl', ...middlewares]
|
|
272
|
-
* @return {Promise<>}
|
|
273
|
-
*
|
|
274
|
-
* @signature add(methodUrl: string, routes: Array<Array<any>>)
|
|
275
|
-
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
276
|
-
* @param {Array<Array<any>>} routes list with subroutes: ['METHOD /suburl', ...middlewares]
|
|
277
|
-
* @return {Promise<>}
|
|
278
|
-
*
|
|
279
|
-
* @signature add(routes: { [key: string]: Array<any> })
|
|
280
|
-
* @param { {[key: string]: Array<any>} } routes list with subroutes: 'METHOD /suburl': [...middlewares]
|
|
281
|
-
* @return {Promise<>}
|
|
282
|
-
*
|
|
283
|
-
* @signature add(methodUrl: string, routes: { [key: string]: Array<any> })
|
|
284
|
-
* @param {string} methodUrl 'METHOD /url' or '/url'
|
|
285
|
-
* @param { {[key: string]: Array<any>} } routes list with subroutes: 'METHOD /suburl': [...middlewares]
|
|
286
|
-
* @return {Promise<>}
|
|
201
|
+
* RouteURL: 'METHOD /suburl', 'METHOD', '* /suburl'
|
|
287
202
|
*/
|
|
288
|
-
use(...args:
|
|
203
|
+
use(...args: [
|
|
204
|
+
Middleware | Plugin | ControllerClass | RoutesSet
|
|
205
|
+
] | [Promise<Middleware | Plugin | ControllerClass | RoutesSet>] | RoutesList | [RouteURL, RoutesSet] | [PluginClass | Promise<PluginClass>, options?: any]): Promise<void>;
|
|
206
|
+
addStack(middleware: Middleware): void;
|
|
207
|
+
getPlugin(id: string): Plugin | undefined;
|
|
289
208
|
waitPlugin(id: string): Promise<Plugin>;
|
|
290
|
-
/** Add
|
|
291
|
-
|
|
209
|
+
/** Add route, alias to `server.router.use(url, ...args)` */
|
|
210
|
+
all(url: `/${string}`, ...args: RoutesMiddleware[]): MicroServer;
|
|
211
|
+
/** Add route, alias to `server.router.use('GET ' + url, ...args)` */
|
|
212
|
+
get(url: `/${string}`, ...args: RoutesMiddleware[]): MicroServer;
|
|
213
|
+
/** Add route, alias to `server.router.use('POST ' + url, ...args)` */
|
|
214
|
+
post(url: `/${string}`, ...args: RoutesMiddleware[]): MicroServer;
|
|
215
|
+
/** Add route, alias to `server.router.use('PUT ' + url, ...args)` */
|
|
216
|
+
put(url: `/${string}`, ...args: RoutesMiddleware[]): MicroServer;
|
|
217
|
+
/** Add route, alias to `server.router.use('PATCH ' + url, ...args)` */
|
|
218
|
+
patch(url: `/${string}`, ...args: RoutesMiddleware[]): MicroServer;
|
|
219
|
+
/** Add route, alias to `server.router.use('DELETE ' + url, ...args)` */
|
|
220
|
+
delete(url: `/${string}`, ...args: RoutesMiddleware[]): MicroServer;
|
|
221
|
+
/** Add websocket handler, alias to `server.router.use('WEBSOCKET ' + url, ...args)` */
|
|
222
|
+
websocket(url: `/${string}`, ...args: RoutesMiddleware[]): MicroServer;
|
|
223
|
+
/** Add router hook, alias to `server.router.hook(url, ...args)` */
|
|
224
|
+
hook(url: RouteURL, ...args: RoutesMiddleware[]): MicroServer;
|
|
292
225
|
/** Check if middleware allready added */
|
|
293
226
|
has(mid: Middleware): boolean;
|
|
227
|
+
close(): Promise<void>;
|
|
294
228
|
}
|
|
295
229
|
export interface HttpHandler {
|
|
296
230
|
(req: ServerRequest, res: ServerResponse): void;
|
|
@@ -322,94 +256,120 @@ export interface CorsOptions {
|
|
|
322
256
|
/** Max age */
|
|
323
257
|
maxAge?: number;
|
|
324
258
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
259
|
+
export declare class CorsPlugin extends Plugin {
|
|
260
|
+
priority: number;
|
|
261
|
+
name: string;
|
|
262
|
+
options?: CorsOptions;
|
|
263
|
+
constructor(options?: CorsOptions | string | true);
|
|
264
|
+
handler?(req: ServerRequest, res: ServerResponse, next: Function): Promise<string | object | void> | string | object | void;
|
|
265
|
+
}
|
|
266
|
+
export declare class MethodsPlugin extends Plugin {
|
|
267
|
+
priority: number;
|
|
268
|
+
name: string;
|
|
269
|
+
_methods: string;
|
|
270
|
+
_methodsIdx: Record<string, boolean>;
|
|
271
|
+
constructor(methods?: string);
|
|
272
|
+
handler(req: ServerRequest, res: ServerResponse, next: Function): any;
|
|
273
|
+
}
|
|
274
|
+
export interface BodyOptions {
|
|
336
275
|
maxBodySize?: number;
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
276
|
+
}
|
|
277
|
+
export declare class BodyPlugin extends Plugin {
|
|
278
|
+
priority: number;
|
|
279
|
+
name: string;
|
|
280
|
+
_maxBodySize: number;
|
|
281
|
+
constructor(options?: BodyOptions);
|
|
282
|
+
handler(req: ServerRequest, res: ServerResponse, next: () => void): void;
|
|
283
|
+
}
|
|
284
|
+
export interface UploadOptions {
|
|
344
285
|
uploadDir?: string;
|
|
345
|
-
|
|
346
|
-
|
|
286
|
+
maxFileSize?: number;
|
|
287
|
+
}
|
|
288
|
+
export interface UploadFile {
|
|
289
|
+
name: string;
|
|
290
|
+
fileName?: string;
|
|
291
|
+
contentType?: string;
|
|
292
|
+
size: number;
|
|
293
|
+
filePath?: string;
|
|
294
|
+
}
|
|
295
|
+
export declare class UploadPlugin extends Plugin {
|
|
296
|
+
priority: number;
|
|
297
|
+
name: string;
|
|
298
|
+
_maxFileSize: number;
|
|
299
|
+
_uploadDir?: string;
|
|
300
|
+
constructor(options?: UploadOptions);
|
|
301
|
+
handler(req: ServerRequest, res: ServerResponse, next: () => void): void;
|
|
302
|
+
}
|
|
303
|
+
/** WebSocket options */
|
|
304
|
+
export interface WebSocketOptions {
|
|
347
305
|
/** max websocket payload (default: 1MB) */
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
|
|
306
|
+
maxPayload?: number;
|
|
307
|
+
/** automatically send pong frame (default: true) */
|
|
308
|
+
autoPong?: boolean;
|
|
309
|
+
/** allow websocket per message deflate compression (default: false) */
|
|
310
|
+
permessageDeflate?: boolean;
|
|
311
|
+
/** websocket deflate compression max window bits 8-15 for deflate (default: 10) */
|
|
312
|
+
maxWindowBits?: number;
|
|
313
|
+
/** timeout for idle websocket connection (default: 120s) */
|
|
314
|
+
timeout?: number;
|
|
315
|
+
/** allow websocket deflate compression (default: false) */
|
|
316
|
+
deflate?: boolean;
|
|
353
317
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
/** Add websocket handler, alias to `server.router.use('WEBSOCKET ' + url, ...args)` */
|
|
407
|
-
websocket(url: string, ...args: any): MicroServer;
|
|
408
|
-
/** Add router hook, alias to `server.router.hook(url, ...args)` */
|
|
409
|
-
hook(url: string, ...args: any): MicroServer;
|
|
318
|
+
interface WebSocketEvents {
|
|
319
|
+
close: (code: number, reason: string) => void;
|
|
320
|
+
end: () => void;
|
|
321
|
+
error: (error: Error) => void;
|
|
322
|
+
ping: (data: Buffer) => void;
|
|
323
|
+
pong: (data: Buffer) => void;
|
|
324
|
+
message: (data: string | Buffer | ArrayBuffer | Blob) => void;
|
|
325
|
+
open: () => void;
|
|
326
|
+
}
|
|
327
|
+
/** WebSocket class */
|
|
328
|
+
export declare class WebSocket extends EventEmitter {
|
|
329
|
+
ready: boolean;
|
|
330
|
+
constructor(req: ServerRequest, options?: WebSocketOptions);
|
|
331
|
+
/** Close connection */
|
|
332
|
+
close(reason?: number, data?: Buffer): void;
|
|
333
|
+
/** Generate WebSocket frame from data */
|
|
334
|
+
static getFrame(data: number | string | Buffer | undefined, options?: any): Buffer;
|
|
335
|
+
/** Send data */
|
|
336
|
+
send(data: string | Buffer): void;
|
|
337
|
+
/** Send ping frame */
|
|
338
|
+
ping(buffer?: Buffer): void;
|
|
339
|
+
/** Send pong frame */
|
|
340
|
+
pong(buffer?: Buffer): void;
|
|
341
|
+
protected _sendFrame(opcode: number, data: Buffer, cb?: () => void): void;
|
|
342
|
+
on<K extends keyof WebSocketEvents>(event: K, listener: WebSocketEvents[K]): this;
|
|
343
|
+
addListener<K extends keyof WebSocketEvents>(event: K, listener: WebSocketEvents[K]): this;
|
|
344
|
+
once<K extends keyof WebSocketEvents>(event: K, listener: WebSocketEvents[K]): this;
|
|
345
|
+
off<K extends keyof WebSocketEvents>(event: K, listener: WebSocketEvents[K]): this;
|
|
346
|
+
removeListener<K extends keyof WebSocketEvents>(event: K, listener: WebSocketEvents[K]): this;
|
|
347
|
+
}
|
|
348
|
+
export declare class WebSocketPlugin extends Plugin {
|
|
349
|
+
name: string;
|
|
350
|
+
_handler: (req: ServerRequest, socket: net.Socket, head: any) => void;
|
|
351
|
+
constructor(options?: any, server?: MicroServer);
|
|
352
|
+
addUpgradeHandler(srv: http.Server): void;
|
|
353
|
+
upgradeHandler(server: MicroServer, req: ServerRequest, socket: net.Socket, head: any): void;
|
|
354
|
+
static create(req: ServerRequest, options?: WebSocketOptions): WebSocket;
|
|
355
|
+
}
|
|
356
|
+
/** Trust proxy plugin, adds `req.ip` and `req.localip` */
|
|
357
|
+
export declare class TrustProxyPlugin extends Plugin {
|
|
358
|
+
priority: number;
|
|
359
|
+
name: string;
|
|
360
|
+
constructor(options?: string[]);
|
|
361
|
+
isLocal(ip: string): boolean;
|
|
362
|
+
handler(req: ServerRequest, res: ServerResponse, next: Function): void;
|
|
363
|
+
}
|
|
364
|
+
/** Virtual host plugin */
|
|
365
|
+
export declare class VHostPlugin extends Plugin {
|
|
366
|
+
priority: number;
|
|
367
|
+
vhosts?: Record<string, MicroServer>;
|
|
368
|
+
constructor(options: Record<string, RoutesSet | MicroServer>, server?: MicroServer);
|
|
369
|
+
handler?(req: ServerRequest, res: ServerResponse, next: Function): void;
|
|
410
370
|
}
|
|
411
371
|
/** Static files options */
|
|
412
|
-
export interface
|
|
372
|
+
export interface StaticFilesOptions {
|
|
413
373
|
/** files root directory */
|
|
414
374
|
root?: string;
|
|
415
375
|
/** url path */
|
|
@@ -439,7 +399,7 @@ export interface ServeFileOptions {
|
|
|
439
399
|
/** root */
|
|
440
400
|
root?: string;
|
|
441
401
|
/** file name */
|
|
442
|
-
filename?: string;
|
|
402
|
+
filename?: string | true;
|
|
443
403
|
/** file mime type */
|
|
444
404
|
mimeType?: string;
|
|
445
405
|
/** last modified date */
|
|
@@ -453,8 +413,45 @@ export interface ServeFileOptions {
|
|
|
453
413
|
/** stat */
|
|
454
414
|
stats?: fs.Stats;
|
|
455
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* Static files middleware plugin
|
|
418
|
+
* Usage: server.use('static', '/public')
|
|
419
|
+
* Usage: server.use('static', { root: 'public', path: '/static' })
|
|
420
|
+
*/
|
|
421
|
+
export declare class StaticFilesPlugin extends Plugin {
|
|
422
|
+
priority: number;
|
|
423
|
+
/** Default mime types */
|
|
424
|
+
static mimeTypes: {
|
|
425
|
+
[key: string]: string;
|
|
426
|
+
};
|
|
427
|
+
/** Custom mime types */
|
|
428
|
+
mimeTypes: {
|
|
429
|
+
[key: string]: string;
|
|
430
|
+
};
|
|
431
|
+
/** File extension handlers */
|
|
432
|
+
handlers?: {
|
|
433
|
+
[key: string]: Middleware;
|
|
434
|
+
};
|
|
435
|
+
/** Files root directory */
|
|
436
|
+
root: string;
|
|
437
|
+
/** Ignore prefixes */
|
|
438
|
+
ignore: string[];
|
|
439
|
+
/** Index file. default: 'index.html' */
|
|
440
|
+
index: string;
|
|
441
|
+
/** Update Last-Modified header. default: true */
|
|
442
|
+
lastModified: boolean;
|
|
443
|
+
/** Update ETag header. default: true */
|
|
444
|
+
etag: boolean;
|
|
445
|
+
/** Max file age in seconds (default: 31536000) */
|
|
446
|
+
maxAge?: number;
|
|
447
|
+
prefix: string;
|
|
448
|
+
constructor(options?: StaticFilesOptions | string, server?: MicroServer);
|
|
449
|
+
/** Default static files handler */
|
|
450
|
+
handler(req: ServerRequest, res: ServerResponse, next: Function): any;
|
|
451
|
+
serveFile(req: ServerRequest, res: ServerResponse, options: ServeFileOptions): void;
|
|
452
|
+
}
|
|
456
453
|
/** Proxy plugin options */
|
|
457
|
-
export interface
|
|
454
|
+
export interface ProxyOptions {
|
|
458
455
|
/** Base path */
|
|
459
456
|
path?: string;
|
|
460
457
|
/** Remote url */
|
|
@@ -471,6 +468,8 @@ export interface ProxyPluginOptions {
|
|
|
471
468
|
};
|
|
472
469
|
}
|
|
473
470
|
export declare class ProxyPlugin extends Plugin {
|
|
471
|
+
priority: number;
|
|
472
|
+
name: string;
|
|
474
473
|
/** Default valid headers */
|
|
475
474
|
static validHeaders: {
|
|
476
475
|
[key: string]: boolean;
|
|
@@ -487,7 +486,7 @@ export declare class ProxyPlugin extends Plugin {
|
|
|
487
486
|
remoteUrl: URL;
|
|
488
487
|
/** Match regex filter */
|
|
489
488
|
regex?: RegExp;
|
|
490
|
-
constructor(
|
|
489
|
+
constructor(options?: ProxyOptions | string, server?: MicroServer);
|
|
491
490
|
/** Default proxy handler */
|
|
492
491
|
proxyHandler(req: ServerRequest, res: ServerResponse, next: Function): any;
|
|
493
492
|
/** Proxy plugin handler as middleware */
|
|
@@ -618,8 +617,80 @@ export declare class Auth {
|
|
|
618
617
|
/** Clear user cache if users setting where changed */
|
|
619
618
|
clearCache(): void;
|
|
620
619
|
}
|
|
621
|
-
/**
|
|
622
|
-
export declare
|
|
620
|
+
/** Authentication plugin */
|
|
621
|
+
export declare class AuthPlugin extends Plugin {
|
|
622
|
+
priority: number;
|
|
623
|
+
name: string;
|
|
624
|
+
options: AuthOptions;
|
|
625
|
+
constructor(options?: AuthOptions, server?: MicroServer);
|
|
626
|
+
/** Authentication middleware */
|
|
627
|
+
handler(req: ServerRequest, res: ServerResponse, next: Function): Promise<any>;
|
|
628
|
+
}
|
|
629
|
+
export declare class StandardPlugins extends Plugin {
|
|
630
|
+
constructor(options?: any, server?: MicroServer);
|
|
631
|
+
}
|
|
632
|
+
interface ControllerClass {
|
|
633
|
+
new (req: ServerRequest<any>, res: ServerResponse<any>): Controller<any>;
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Controller for dynamic routes
|
|
637
|
+
*
|
|
638
|
+
* @example
|
|
639
|
+
* ```js
|
|
640
|
+
* class MyController extends Controller {
|
|
641
|
+
* static model = MyModel;
|
|
642
|
+
* static acl = 'auth';
|
|
643
|
+
*
|
|
644
|
+
* static 'acl:index' = '';
|
|
645
|
+
* static 'url:index' = 'GET /index';
|
|
646
|
+
* async index (req, res) {
|
|
647
|
+
* res.send('Hello World')
|
|
648
|
+
* }
|
|
649
|
+
*
|
|
650
|
+
* //function name prefixes translated to HTTP methods:
|
|
651
|
+
* // all => GET, get => GET, insert => POST, post => POST,
|
|
652
|
+
* // update => PUT, put => PUT, delete => DELETE,
|
|
653
|
+
* // modify => PATCH, patch => PATCH,
|
|
654
|
+
* // websocket => internal WebSocket
|
|
655
|
+
* // automatic acl will be: class_name + '/' + function_name_prefix
|
|
656
|
+
* // automatic url will be: method + ' /' + class_name + '/' + function_name_without_prefix
|
|
657
|
+
*
|
|
658
|
+
* //static 'acl:allUsers' = 'MyController/all';
|
|
659
|
+
* //static 'url:allUsers' = 'GET /MyController/Users';
|
|
660
|
+
* async allUsers () {
|
|
661
|
+
* return ['usr1', 'usr2', 'usr3']
|
|
662
|
+
* }
|
|
663
|
+
*
|
|
664
|
+
* //static 'acl:getOrder' = 'MyController/get';
|
|
665
|
+
* //static 'url:getOrder' = 'GET /Users/:id/:id1';
|
|
666
|
+
* static 'group:getOrder' = 'orders';
|
|
667
|
+
* static 'model:getOrder' = OrderModel;
|
|
668
|
+
* async getOrder (id: string, id1: string) {
|
|
669
|
+
* return {id, extras: id1, type: 'order'}
|
|
670
|
+
* }
|
|
671
|
+
*
|
|
672
|
+
* //static 'acl:insertOrder' = 'MyController/insert';
|
|
673
|
+
* //static 'url:insertOrder' = 'POST /Users/:id';
|
|
674
|
+
* static 'model:insertOrder' = OrderModel;
|
|
675
|
+
* async insertOrder (id: string, id1: string) {
|
|
676
|
+
* return {id, extras: id1, type: 'order'}
|
|
677
|
+
* }
|
|
678
|
+
*
|
|
679
|
+
* static 'acl:POST /login' = '';
|
|
680
|
+
* async 'POST /login' () {
|
|
681
|
+
* return {id, extras: id1, type: 'order'}
|
|
682
|
+
* }
|
|
683
|
+
* }
|
|
684
|
+
* ```
|
|
685
|
+
*/
|
|
686
|
+
export declare class Controller<T extends Model<any> = any> {
|
|
687
|
+
req: ServerRequest<T>;
|
|
688
|
+
res: ServerResponse<T>;
|
|
689
|
+
get model(): T | undefined;
|
|
690
|
+
constructor(req: ServerRequest<T>, res: ServerResponse<T>);
|
|
691
|
+
/** Generate routes for this controller */
|
|
692
|
+
static routes(): any[];
|
|
693
|
+
}
|
|
623
694
|
export interface FileStoreOptions {
|
|
624
695
|
/** Base directory */
|
|
625
696
|
dir?: string;
|