mm_os 5.0.1 → 5.0.2
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/lib/os.js +571 -535
- package/package.json +62 -58
package/lib/os.js
CHANGED
|
@@ -1,536 +1,572 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const { Mod } = require('mm_mod');
|
|
3
|
-
const { containerAdmin } = require('mm_container');
|
|
4
|
-
const { getLogger } = require('mm_logger');
|
|
5
|
-
const { Loader } = require('mm_loader');
|
|
6
|
-
const { appAdmin } = require('mm_apps');
|
|
7
|
-
const { eventAdmin } = require('mm_event');
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
*
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
this.container
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
this.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
admin
|
|
196
|
-
admin.
|
|
197
|
-
admin.get('
|
|
198
|
-
admin.get('
|
|
199
|
-
admin.get('
|
|
200
|
-
admin.get('
|
|
201
|
-
admin.get('
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
*
|
|
225
|
-
* @param {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
*
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
353
|
-
|
|
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
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
*
|
|
463
|
-
* @
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const { Mod } = require('mm_mod');
|
|
3
|
+
const { containerAdmin } = require('mm_container');
|
|
4
|
+
const { getLogger } = require('mm_logger');
|
|
5
|
+
const { Loader } = require('mm_loader');
|
|
6
|
+
const { appAdmin } = require('mm_apps');
|
|
7
|
+
const { eventAdmin } = require('mm_event');
|
|
8
|
+
const { Response } = require('mm_response');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* OS类
|
|
12
|
+
*/
|
|
13
|
+
class OS extends Mod {
|
|
14
|
+
static config = {
|
|
15
|
+
name: 'os',
|
|
16
|
+
in_adapters: [{
|
|
17
|
+
name: 'web',
|
|
18
|
+
module: 'mm_web_server'
|
|
19
|
+
}, {
|
|
20
|
+
name: 'mqtt',
|
|
21
|
+
module: 'mm_mqtt_server'
|
|
22
|
+
}, {
|
|
23
|
+
name: 'socket',
|
|
24
|
+
module: 'mm_socket_server'
|
|
25
|
+
}, {
|
|
26
|
+
name: 'websocket',
|
|
27
|
+
module: 'mm_websocket_server'
|
|
28
|
+
}],
|
|
29
|
+
// 输出适配器
|
|
30
|
+
out_adapters: [{
|
|
31
|
+
name: 'redis',
|
|
32
|
+
module: 'mm_redis'
|
|
33
|
+
}, {
|
|
34
|
+
name: 'mongodb',
|
|
35
|
+
module: 'mm_mongodb'
|
|
36
|
+
}, {
|
|
37
|
+
name: 'mysql',
|
|
38
|
+
module: 'mm_mysql'
|
|
39
|
+
}, {
|
|
40
|
+
name: 'sqlite',
|
|
41
|
+
module: 'mm_sqlite'
|
|
42
|
+
}, {
|
|
43
|
+
name: 'jsondb',
|
|
44
|
+
module: 'mm_jsondb'
|
|
45
|
+
}, {
|
|
46
|
+
name: 'file',
|
|
47
|
+
module: 'mm_files'
|
|
48
|
+
}],
|
|
49
|
+
tools: [{
|
|
50
|
+
name: 'lang',
|
|
51
|
+
module: 'mm_lang'
|
|
52
|
+
}, {
|
|
53
|
+
name: 'tpl',
|
|
54
|
+
module: 'mm_tpl'
|
|
55
|
+
}, {
|
|
56
|
+
name: 'ret',
|
|
57
|
+
module: 'mm_ret'
|
|
58
|
+
}],
|
|
59
|
+
admins: [{
|
|
60
|
+
name: 'event',
|
|
61
|
+
module: 'mm_event'
|
|
62
|
+
}, {
|
|
63
|
+
name: 'api',
|
|
64
|
+
module: 'mm_api'
|
|
65
|
+
}, {
|
|
66
|
+
name: 'param',
|
|
67
|
+
module: 'mm_param'
|
|
68
|
+
}, {
|
|
69
|
+
name: 'dao',
|
|
70
|
+
module: 'mm_dao'
|
|
71
|
+
}, {
|
|
72
|
+
name: 'service',
|
|
73
|
+
module: 'mm_service'
|
|
74
|
+
}, {
|
|
75
|
+
name: 'template',
|
|
76
|
+
module: 'mm_template'
|
|
77
|
+
}, {
|
|
78
|
+
name: 'pendant',
|
|
79
|
+
module: 'mm_pendant'
|
|
80
|
+
}, {
|
|
81
|
+
name: 'timer',
|
|
82
|
+
module: 'mm_timer'
|
|
83
|
+
}, {
|
|
84
|
+
name: 'static',
|
|
85
|
+
module: 'mm_statics'
|
|
86
|
+
}],
|
|
87
|
+
cache: {
|
|
88
|
+
type: 'redis'
|
|
89
|
+
},
|
|
90
|
+
sql: {
|
|
91
|
+
type: 'mysql'
|
|
92
|
+
},
|
|
93
|
+
web: {
|
|
94
|
+
port: 8000
|
|
95
|
+
},
|
|
96
|
+
mqtt: {
|
|
97
|
+
port: 1883
|
|
98
|
+
},
|
|
99
|
+
socket: {
|
|
100
|
+
port: 9000
|
|
101
|
+
},
|
|
102
|
+
websocket: {
|
|
103
|
+
port: 9001
|
|
104
|
+
},
|
|
105
|
+
jsondb: {
|
|
106
|
+
dir: './data'
|
|
107
|
+
},
|
|
108
|
+
mysql: {
|
|
109
|
+
host: '127.0.0.1',
|
|
110
|
+
port: 3306,
|
|
111
|
+
user: 'root',
|
|
112
|
+
password: 'Asd159357',
|
|
113
|
+
database: 'mm'
|
|
114
|
+
},
|
|
115
|
+
redis: {
|
|
116
|
+
host: '127.0.0.1',
|
|
117
|
+
password: 'asd159357',
|
|
118
|
+
port: 6379,
|
|
119
|
+
db: 0
|
|
120
|
+
},
|
|
121
|
+
mongodb: {
|
|
122
|
+
host: '127.0.0.1',
|
|
123
|
+
port: 27017,
|
|
124
|
+
user: 'root',
|
|
125
|
+
password: 'asd123',
|
|
126
|
+
database: 'mm'
|
|
127
|
+
},
|
|
128
|
+
sqlite: {
|
|
129
|
+
dir: './db',
|
|
130
|
+
database: 'mm',
|
|
131
|
+
user: 'root',
|
|
132
|
+
password: 'asd123'
|
|
133
|
+
},
|
|
134
|
+
ret: {
|
|
135
|
+
type: 'json-rpc'
|
|
136
|
+
},
|
|
137
|
+
lang: {
|
|
138
|
+
type: 'zh_cn'
|
|
139
|
+
},
|
|
140
|
+
tpl: {
|
|
141
|
+
dir: './static/template',
|
|
142
|
+
ext: '.html'
|
|
143
|
+
},
|
|
144
|
+
loader: {
|
|
145
|
+
dir: ''
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* OS类构造函数
|
|
151
|
+
* @param {object} config 配置对象
|
|
152
|
+
*/
|
|
153
|
+
constructor(config) {
|
|
154
|
+
super(config);
|
|
155
|
+
/**
|
|
156
|
+
* 实例容器(按作用域共享)
|
|
157
|
+
* @type {object}
|
|
158
|
+
*/
|
|
159
|
+
this.container = containerAdmin('os', { name: 'os' });
|
|
160
|
+
this.container.server = this;
|
|
161
|
+
/**
|
|
162
|
+
* 初始化阶段创建的全局管理器列表
|
|
163
|
+
* @type {Array<object>}
|
|
164
|
+
*/
|
|
165
|
+
this._admin_managers = [];
|
|
166
|
+
/**
|
|
167
|
+
* 已启动的全局管理器集合,避免重复启动
|
|
168
|
+
* @type {Set<object>}
|
|
169
|
+
*/
|
|
170
|
+
this._started_admins = new Set();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 生命周期初始化钩子
|
|
175
|
+
* @returns {void} 无返回值
|
|
176
|
+
*/
|
|
177
|
+
async onInit() {
|
|
178
|
+
this.initConfig();
|
|
179
|
+
this.initLogger();
|
|
180
|
+
// 初始化事件总线
|
|
181
|
+
this.initEventBus();
|
|
182
|
+
// 存储需先于管理器初始化:mm_dao 等管理器会通过 mm_sql 的 getSql() 复用同名 SQL 实例,
|
|
183
|
+
// 若管理器先创建,则会以默认(空库名)配置占用实例,导致后续 SQL 报 No database selected
|
|
184
|
+
await this.initpStorage();
|
|
185
|
+
await this.initAdmins();
|
|
186
|
+
await this.initAdapter();
|
|
187
|
+
await this.initLoader();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 初始化配置管理器
|
|
193
|
+
*/
|
|
194
|
+
OS.prototype.initConfig = function () {
|
|
195
|
+
const { admin } = require('mm_config');
|
|
196
|
+
admin.setServer(this);
|
|
197
|
+
admin.get('os', this.config);
|
|
198
|
+
admin.get('cache', { ...this.config.cache, redis: this.config.redis, mongodb: this.config.mongodb });
|
|
199
|
+
admin.get('sql', { ...this.config.sql, mysql: this.config.mysql, sqlite: this.config.sqlite, jsondb: this.config.jsondb });
|
|
200
|
+
admin.get('web', this.config.web);
|
|
201
|
+
admin.get('mqtt', this.config.mqtt);
|
|
202
|
+
admin.get('socket', this.config.socket);
|
|
203
|
+
admin.get('websocket', this.config.websocket);
|
|
204
|
+
admin.get('jsondb', this.config.jsondb);
|
|
205
|
+
admin.get('redis', this.config.redis);
|
|
206
|
+
admin.get('mongodb', this.config.mongodb);
|
|
207
|
+
admin.get('sqlite', this.config.sqlite);
|
|
208
|
+
admin.get('mysql', this.config.mysql);
|
|
209
|
+
admin.get('ret', this.config.ret);
|
|
210
|
+
admin.get('lang', this.config.lang);
|
|
211
|
+
admin.get('tpl', this.config.tpl);
|
|
212
|
+
admin.get('loader', this.config.loader);
|
|
213
|
+
this.reg('config', admin);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 初始化日志记录器
|
|
218
|
+
*/
|
|
219
|
+
OS.prototype.initLogger = function () {
|
|
220
|
+
this.reg('logger', getLogger(this.config.log || { level: 'info' }));
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 记录日志
|
|
225
|
+
* @param {string} type 日志类型
|
|
226
|
+
* @param {string} msg 日志消息
|
|
227
|
+
* @param {...*} args 日志参数
|
|
228
|
+
*/
|
|
229
|
+
OS.prototype.log = function (type, msg, ...args) {
|
|
230
|
+
this.container.get('logger')[type](msg, ...args);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 注册管理
|
|
235
|
+
* @param {string} name 管理名称
|
|
236
|
+
* @param {object} admin 管理对象
|
|
237
|
+
*/
|
|
238
|
+
OS.prototype.reg = function (name, admin) {
|
|
239
|
+
this.container.set(name, admin);
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 加载模块
|
|
244
|
+
* @param {string} mod_name 模块名称
|
|
245
|
+
* @returns {object} 模块对象
|
|
246
|
+
*/
|
|
247
|
+
OS.prototype.require = function (mod_name) {
|
|
248
|
+
return require(mod_name);
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* 初始化适配器
|
|
253
|
+
* @returns {void} 无返回值
|
|
254
|
+
*/
|
|
255
|
+
OS.prototype.initAdapter = async function () {
|
|
256
|
+
// 入站适配器
|
|
257
|
+
let adapters = this.config.in_adapters || [];
|
|
258
|
+
for (let mod of adapters) {
|
|
259
|
+
try {
|
|
260
|
+
let admin = this.require(mod.module).admin;
|
|
261
|
+
admin.setServer(this);
|
|
262
|
+
let manager = admin.get('os', this.config[mod.name]);
|
|
263
|
+
if (typeof manager.init === 'function') {
|
|
264
|
+
await manager.init();
|
|
265
|
+
}
|
|
266
|
+
this.reg(mod.name, manager);
|
|
267
|
+
} catch (error) {
|
|
268
|
+
this.log('error', `初始化适配器 ${mod.name} 失败: ${error}`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// 出站适配器
|
|
273
|
+
let out_adapters = this.config.out_adapters || [];
|
|
274
|
+
for (let mod of out_adapters) {
|
|
275
|
+
try {
|
|
276
|
+
let admin = this.require(mod.module).admin;
|
|
277
|
+
admin.setServer(this);
|
|
278
|
+
let manager = admin.get('os', this.config[mod.name]);
|
|
279
|
+
if (typeof manager.init === 'function') {
|
|
280
|
+
await manager.init();
|
|
281
|
+
}
|
|
282
|
+
this.reg(mod.name, manager);
|
|
283
|
+
} catch (error) {
|
|
284
|
+
this.log('error', `初始化适配器 ${mod.name} 失败: ${error}`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 初始化工具
|
|
291
|
+
* @returns {void} 无返回值
|
|
292
|
+
*/
|
|
293
|
+
OS.prototype.initTools = async function () {
|
|
294
|
+
let tools = this.config.tools || [];
|
|
295
|
+
for (let mod of tools) {
|
|
296
|
+
try {
|
|
297
|
+
let admin = this.require(mod.module).admin;
|
|
298
|
+
admin.setServer(this);
|
|
299
|
+
let manager = admin.get('os', this.config[mod.name]);
|
|
300
|
+
if (typeof manager.init === 'function') {
|
|
301
|
+
await manager.init();
|
|
302
|
+
}
|
|
303
|
+
this.reg(mod.name, manager);
|
|
304
|
+
} catch (error) {
|
|
305
|
+
this.log('error', `初始化工具 ${mod.name} 失败: ${error}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 初始化管理
|
|
312
|
+
* @returns {void} 无返回值
|
|
313
|
+
*/
|
|
314
|
+
OS.prototype.initAdmins = async function () {
|
|
315
|
+
let admins = this.config.admins || [];
|
|
316
|
+
for (let mod of admins) {
|
|
317
|
+
try {
|
|
318
|
+
let admin = this.require(mod.module).admin;
|
|
319
|
+
admin.setServer(this);
|
|
320
|
+
let manager = admin.get('os', this.config[mod.name]);
|
|
321
|
+
if (typeof manager.init === 'function') {
|
|
322
|
+
await manager.init();
|
|
323
|
+
}
|
|
324
|
+
this.reg(mod.name, manager);
|
|
325
|
+
this._admin_managers.push(manager);
|
|
326
|
+
} catch (error) {
|
|
327
|
+
this.log('error', `初始化管理 ${mod.name} 失败: ${error}`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* 初始化存储
|
|
334
|
+
* @returns {void} 无返回值
|
|
335
|
+
*/
|
|
336
|
+
OS.prototype.initpStorage = async function () {
|
|
337
|
+
// 初始化缓存存储
|
|
338
|
+
let cacheAdmin = this.require('mm_cache').admin;
|
|
339
|
+
this._setAdminServer(cacheAdmin);
|
|
340
|
+
let cache = cacheAdmin.get('os', Object.assign({}, this.config.cache, { redis: this.config.redis, mongodb: this.config.mongodb, cachebase: this.config.cachebase }));
|
|
341
|
+
if (typeof cache.init === 'function') {
|
|
342
|
+
await cache.init();
|
|
343
|
+
}
|
|
344
|
+
this.reg('cache', cache);
|
|
345
|
+
|
|
346
|
+
// 初始化数据库
|
|
347
|
+
let sqlAdmin = this.require('mm_sql').admin;
|
|
348
|
+
this._setAdminServer(sqlAdmin);
|
|
349
|
+
let sql = sqlAdmin.get('os', Object.assign({}, this.config.sql, { mysql: this.config.mysql, sqlite: this.config.sqlite, jsondb: this.config.jsondb }));
|
|
350
|
+
if (typeof sql.init === 'function') {
|
|
351
|
+
await sql.init();
|
|
352
|
+
}
|
|
353
|
+
this.reg('sql', sql);
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 设置管理器服务器:已设置时跳过,避免同一 Admin 实例重复绑定
|
|
358
|
+
* @param {object} admin 管理器工厂实例
|
|
359
|
+
*/
|
|
360
|
+
OS.prototype._setAdminServer = function (admin) {
|
|
361
|
+
if (!admin.getServer()) {
|
|
362
|
+
admin.setServer(this);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 初始化应用加载器(协调层:只创建加载器与应用管理器,装配逻辑在 mm_apps/mm_plugins)
|
|
368
|
+
* @returns {void} 无返回值
|
|
369
|
+
*/
|
|
370
|
+
OS.prototype.initLoader = async function () {
|
|
371
|
+
// 配置加载器:注入 socket/mqtt 类型,其余类型由 mm_loader 默认提供
|
|
372
|
+
const loader = new Loader(this.config.loader || {});
|
|
373
|
+
loader.addType('socket', 'socket.json');
|
|
374
|
+
loader.addType('mqtt', 'mqtt.json');
|
|
375
|
+
this.reg('loader', loader);
|
|
376
|
+
|
|
377
|
+
// 应用管理器:装配应用由 AppManager.load 负责,插件随 App.onInit 引入
|
|
378
|
+
const app = appAdmin('os');
|
|
379
|
+
app.setServer(this);
|
|
380
|
+
app.setLogger(this.container.get('logger'));
|
|
381
|
+
this.reg('app', app);
|
|
382
|
+
|
|
383
|
+
const dir = this.config.loader.dir || path.join(__dirname, '..', 'example', 'app');
|
|
384
|
+
await app.load(dir, loader);
|
|
385
|
+
await app.init();
|
|
386
|
+
|
|
387
|
+
// 注入 Web 请求路由
|
|
388
|
+
this._initWeb();
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* 初始化 Web 适配器:注入跨作用域事件路由与响应写出器
|
|
393
|
+
* @returns {void} 无返回值
|
|
394
|
+
*/
|
|
395
|
+
OS.prototype._initWeb = function () {
|
|
396
|
+
const web = this.container.get('web');
|
|
397
|
+
if (!web) return;
|
|
398
|
+
web.setEventManager(this._createEventRouter());
|
|
399
|
+
new Response().bind(web);
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* 创建跨作用域事件路由:Web 适配器只持有 web 作用域,需转发到各应用作用域
|
|
404
|
+
* @returns {object} 含 run 方法的事件管理器
|
|
405
|
+
*/
|
|
406
|
+
OS.prototype._createEventRouter = function () {
|
|
407
|
+
return {
|
|
408
|
+
run: (target, params, ctx, options) => this._routeRequest(target, params, ctx, options)
|
|
409
|
+
};
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* 路由请求:先尝试静态资源,未命中再按应用顺序执行事件
|
|
414
|
+
* @param {string} target 请求路径
|
|
415
|
+
* @param {object} params 请求参数
|
|
416
|
+
* @param {object} ctx 请求上下文
|
|
417
|
+
* @param {object} options 事件运行选项
|
|
418
|
+
* @returns {Promise<*>} 事件执行结果
|
|
419
|
+
*/
|
|
420
|
+
OS.prototype._routeRequest = async function (target, params, ctx, options = {}) {
|
|
421
|
+
const statics = this.container.get('static');
|
|
422
|
+
if (statics && typeof statics.run === 'function') {
|
|
423
|
+
// 静态模块为服务式调用:命中即返回 true 并已写出响应,未命中返回 false 继续后续事件
|
|
424
|
+
const served = await statics.run(params, ctx, options);
|
|
425
|
+
if (served) {
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return await this._runAppEvents(target, params, ctx, options);
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* 按应用排序依次执行事件:命中结果、响应已写出或发生错误时中止
|
|
434
|
+
* @param {string} target 请求路径
|
|
435
|
+
* @param {object} params 请求参数
|
|
436
|
+
* @param {object} ctx 请求上下文
|
|
437
|
+
* @param {object} options 事件运行选项
|
|
438
|
+
* @returns {Promise<*>} 事件执行结果
|
|
439
|
+
*/
|
|
440
|
+
OS.prototype._runAppEvents = async function (target, params, ctx, options = {}) {
|
|
441
|
+
const app = this.container.get('app');
|
|
442
|
+
if (!app || typeof app.list !== 'function') return undefined;
|
|
443
|
+
|
|
444
|
+
const apps = app.list().slice().sort((a, b) => this._getSort(a) - this._getSort(b));
|
|
445
|
+
for (const item of apps) {
|
|
446
|
+
if (this._isRequestHandled(ctx) || ctx.error) return undefined;
|
|
447
|
+
const name = item.config && item.config.name;
|
|
448
|
+
if (!name) continue;
|
|
449
|
+
const app_options = {};
|
|
450
|
+
const result = await eventAdmin(name).run(target, params, ctx, app_options);
|
|
451
|
+
if (app_options.error) {
|
|
452
|
+
ctx.error = ctx.error || app_options.error;
|
|
453
|
+
options.error = options.error || app_options.error;
|
|
454
|
+
return undefined;
|
|
455
|
+
}
|
|
456
|
+
if (result !== undefined && result !== null) return result;
|
|
457
|
+
}
|
|
458
|
+
return undefined;
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* 判断请求是否已处理完毕:事件或中间件已写出响应
|
|
463
|
+
* @param {object} ctx 请求上下文
|
|
464
|
+
* @returns {boolean} 响应是否已写出
|
|
465
|
+
*/
|
|
466
|
+
OS.prototype._isRequestHandled = function (ctx) {
|
|
467
|
+
if (!ctx) return false;
|
|
468
|
+
if (ctx.handled === true) return true;
|
|
469
|
+
const res = ctx.res;
|
|
470
|
+
return !!(res && (res.writableEnded || res.headersSent));
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* 获取应用执行顺序
|
|
475
|
+
* @param {object} app 应用驱动实例
|
|
476
|
+
* @returns {number} 排序值,缺省 100
|
|
477
|
+
*/
|
|
478
|
+
OS.prototype._getSort = function (app) {
|
|
479
|
+
const sort = app && app.config ? app.config.sort : undefined;
|
|
480
|
+
return typeof sort === 'number' ? sort : 100;
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* 初始化事件总线
|
|
485
|
+
* @returns {void} 无返回值
|
|
486
|
+
*/
|
|
487
|
+
OS.prototype.initEventBus = function () {
|
|
488
|
+
let admin = this.require('mm_eventer').admin;
|
|
489
|
+
admin.setServer(this);
|
|
490
|
+
this.reg('eventer', admin.get('os'));
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* 启动适配器:按配置中的入站、出站适配器名称从容器取出管理器并启动
|
|
495
|
+
* @returns {void} 无返回值
|
|
496
|
+
*/
|
|
497
|
+
OS.prototype._startAdapters = async function () {
|
|
498
|
+
let mods = this.config.in_adapters;
|
|
499
|
+
for (let mod of mods) {
|
|
500
|
+
let manager = this.container.get(mod.name);
|
|
501
|
+
if (!manager || typeof manager.start !== 'function') continue;
|
|
502
|
+
try {
|
|
503
|
+
await manager.start();
|
|
504
|
+
this._printAddress(mod.name, manager);
|
|
505
|
+
} catch (error) {
|
|
506
|
+
this.log('error', `启动适配器 ${mod.name} 失败: ${error}`);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* 启动初始化阶段创建的全局管理器
|
|
513
|
+
* 仅启动一次,避免重复执行管理器生命周期
|
|
514
|
+
* @returns {void} 无返回值
|
|
515
|
+
*/
|
|
516
|
+
OS.prototype._startAdmins = async function () {
|
|
517
|
+
let managers = this._admin_managers || [];
|
|
518
|
+
for (let manager of managers) {
|
|
519
|
+
if (!manager || typeof manager.start !== 'function') continue;
|
|
520
|
+
if (this._started_admins.has(manager)) continue;
|
|
521
|
+
try {
|
|
522
|
+
await manager.start();
|
|
523
|
+
this._started_admins.add(manager);
|
|
524
|
+
} catch (error) {
|
|
525
|
+
let name = manager.config && manager.config.name;
|
|
526
|
+
this.log('error', `启动管理 ${name} 失败: ${error}`);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* 启动
|
|
533
|
+
* @returns {void} 无返回值
|
|
534
|
+
*/
|
|
535
|
+
OS.prototype.start = async function () {
|
|
536
|
+
// 启动适配器
|
|
537
|
+
await this._startAdapters();
|
|
538
|
+
|
|
539
|
+
// 启动缓存存储
|
|
540
|
+
await this.container.get('cache').start();
|
|
541
|
+
// 启动数据库
|
|
542
|
+
await this.container.get('sql').start();
|
|
543
|
+
|
|
544
|
+
// 启动应用
|
|
545
|
+
const app = this.container.get('app');
|
|
546
|
+
if (app) {
|
|
547
|
+
await app.start();
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// 启动初始化阶段创建的全局管理器(含 os 作用域静态资源)
|
|
551
|
+
await this._startAdmins();
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* 打印适配器监听地址
|
|
556
|
+
* @param {string} name 适配器名称
|
|
557
|
+
* @param {object} manager 适配器管理器实例
|
|
558
|
+
*/
|
|
559
|
+
OS.prototype._printAddress = function (name, manager) {
|
|
560
|
+
let { host, port } = manager.config || {};
|
|
561
|
+
if (!host || host === '0.0.0.0' || host === '::') {
|
|
562
|
+
host = 'localhost';
|
|
563
|
+
}
|
|
564
|
+
let protocol = { web: 'http', websocket: 'ws', socket: 'tcp', mqtt: 'mqtt' }[name] || name;
|
|
565
|
+
let address = `${protocol}://${host}`;
|
|
566
|
+
if (port) {
|
|
567
|
+
address += `:${port}`;
|
|
568
|
+
}
|
|
569
|
+
this.log('info', `适配器 ${name} 已启动: ${address}`);
|
|
570
|
+
};
|
|
571
|
+
|
|
536
572
|
module.exports = OS;
|
package/package.json
CHANGED
|
@@ -1,60 +1,64 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
2
|
+
"name": "mm_os",
|
|
3
|
+
"version": "5.0.2",
|
|
4
|
+
"description": "这是OS系统",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://gitee.com/qiuwenwu91/mm_os.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"author": "qww",
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"main": "index.js",
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"lib",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"directories": {
|
|
19
|
+
"example": "example",
|
|
20
|
+
"lib": "lib"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "node test.js && node --test tests/os.test.js",
|
|
24
|
+
"demo": "node --preserve-symlinks example/demo.js",
|
|
25
|
+
"api:test": "node api_test.js"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"markdown": "^0.5.0",
|
|
29
|
+
"mm_apps": "^1.0.3",
|
|
30
|
+
"mm_cache": "^1.5.5",
|
|
31
|
+
"mm_config": "^2.5.3",
|
|
32
|
+
"mm_container": "^1.0.2",
|
|
33
|
+
"mm_corejs": "^1.1.6",
|
|
34
|
+
"mm_dao": "^1.8.1",
|
|
35
|
+
"mm_es6_to_amd": "^1.5.9",
|
|
36
|
+
"mm_event": "^2.2.5",
|
|
37
|
+
"mm_eventer": "^1.0.0",
|
|
38
|
+
"mm_files": "^1.0.1",
|
|
39
|
+
"mm_handle": "^1.0.0",
|
|
40
|
+
"mm_jsondb": "^1.0.0",
|
|
41
|
+
"mm_loader": "^1.1.2",
|
|
42
|
+
"mm_logger": "^1.0.0",
|
|
43
|
+
"mm_mod": "^1.0.7",
|
|
44
|
+
"mm_mongodb": "^2.0.3",
|
|
45
|
+
"mm_mqtt_server": "^2.0.1",
|
|
46
|
+
"mm_mysql": "^2.5.6",
|
|
47
|
+
"mm_param": "^1.1.4",
|
|
48
|
+
"mm_pendant": "^1.1.2",
|
|
49
|
+
"mm_plugins": "^1.0.3",
|
|
50
|
+
"mm_redis": "^2.1.7",
|
|
51
|
+
"mm_render": "^1.0.1",
|
|
52
|
+
"mm_response": "^1.0.0",
|
|
53
|
+
"mm_socket_server": "^3.0.0",
|
|
54
|
+
"mm_sql": "^1.8.2",
|
|
55
|
+
"mm_sqlite": "^1.5.5",
|
|
56
|
+
"mm_statics": "^1.9.2",
|
|
57
|
+
"mm_template": "^1.2.2",
|
|
58
|
+
"mm_timer": "^1.4.5",
|
|
59
|
+
"mm_tpl": "^2.7.3",
|
|
60
|
+
"mm_web_server": "^2.0.2",
|
|
61
|
+
"mm_websocket_server": "^2.0.1"
|
|
62
|
+
},
|
|
63
|
+
"layer": "terminal"
|
|
60
64
|
}
|