topbit 3.1.8 → 3.1.9

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/README.cn.md CHANGED
@@ -62,7 +62,8 @@ const app = new Topbit({
62
62
  debug: true
63
63
  })
64
64
 
65
- app.run(1234)
65
+ //输出服务信息并运行服务
66
+ app.printServInfo().run(1234)
66
67
 
67
68
  ```
68
69
 
@@ -112,7 +113,7 @@ app.get('/', async ctx => {
112
113
  ctx.to('success')
113
114
  })
114
115
 
115
- app.run(1234)
116
+ app.printServInfo().run(1234)
116
117
 
117
118
  ```
118
119
 
@@ -159,7 +160,7 @@ app.put('/p', async c => {
159
160
  })
160
161
 
161
162
  //默认监听0.0.0.0,参数和原生接口listen一致。
162
- app.run(8080)
163
+ app.printServInfo().run(8080)
163
164
 
164
165
  ```
165
166
 
package/README.md CHANGED
@@ -46,7 +46,8 @@ const app = new Topbit({
46
46
  debug: true
47
47
  })
48
48
 
49
- app.run(1234)
49
+ //in debug mode, output service info
50
+ app.printServInfo().run(1234)
50
51
  ```
51
52
 
52
53
  When no routes are added, Topbit adds a default route:
@@ -72,7 +73,7 @@ app.get('/', async ctx => {
72
73
  })
73
74
 
74
75
  // Defaults to listening on 0.0.0.0, parameters are consistent with the native listen interface.
75
- app.run(1234)
76
+ app.printServInfo().run(1234)
76
77
 
77
78
  ```
78
79
 
@@ -94,7 +95,7 @@ app.get('/', async ctx => {
94
95
  ctx.to('success')
95
96
  })
96
97
 
97
- app.run(1234)
98
+ app.printServInfo().run(1234)
98
99
 
99
100
  ```
100
101
 
@@ -141,7 +142,7 @@ app.put('/p', async c => {
141
142
  })
142
143
 
143
144
  // Defaults to listening on 0.0.0.0
144
- app.run(8080)
145
+ app.printServInfo().run(8080)
145
146
 
146
147
  ```
147
148
 
package/demo/http2.js CHANGED
@@ -44,7 +44,6 @@ if (app.isWorker) {
44
44
 
45
45
 
46
46
  app.sched('none')
47
-
48
- app.autoWorker(3)
49
-
50
- app.daemon(1234, 1)
47
+ .autoWorker(3)
48
+ .printServInfo()
49
+ .daemon(1234, 2)
package/demo/loader.js CHANGED
@@ -21,10 +21,9 @@ if (app.isWorker) {
21
21
  }
22
22
 
23
23
  app.sched('none')
24
-
25
- app.autoWorker(12)
26
-
27
- app.daemon(1234, 5)
24
+ .autoWorker(12)
25
+ .printServInfo(100)
26
+ .daemon(1234, 5)
28
27
 
29
28
  //console.log(app.midware.midGroup)
30
29
  //console.log({...app.router})
@@ -61,8 +61,11 @@ if (app.isWorker) {
61
61
  new Loader().init(app)
62
62
  }
63
63
 
64
- app.autoWorker(16) // Max elastic workers
65
- app.daemon(443, 4) // 4 base workers
64
+ // Max elastic workers
65
+ app.autoWorker(16)
66
+ //delay 100ms, output service info
67
+ .printServInfo(100)
68
+ .daemon(443, 4) // 4 base workers
66
69
  ```
67
70
 
68
71
  Run `node app.js` → full-featured service is up!
@@ -59,8 +59,11 @@ if (app.isWorker) {
59
59
  new Loader().init(app)
60
60
  }
61
61
 
62
- app.autoWorker(16) // 最大弹性进程数
63
- app.daemon(443, 4) // 4 个基础进程
62
+ // 最大弹性进程数
63
+ app.autoWorker(16)
64
+ //延迟100ms后输出服务运行信息
65
+ .printServInfo(100)
66
+ .daemon(443, 4) // 4 个基础进程
64
67
  ```
65
68
 
66
69
  只需执行 `node app.js` 即可启动完整服务!
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topbit",
3
- "version": "3.1.8",
3
+ "version": "3.1.9",
4
4
  "description": "A Server-side web framework support http/1.1 and http/2",
5
5
  "main": "src/topbit.js",
6
6
  "directories": {
package/src/topbit.js CHANGED
@@ -39,7 +39,7 @@ const ErrorLog = require('./lib/errorlog.js')
39
39
  let __instance__ = 0;
40
40
 
41
41
  let _topbit_server_running = `
42
- :.:.:.:.:.:.topbit in service.:.:.:.:.:.:
42
+ :.:.:.:.:\x1b[34;5m.\x1b[0m:\x1b[36;5m.\x1b[0m topbit in service \x1b[36;5m.\x1b[0m:\x1b[34;5m.\x1b[0m:.:.:.:.:
43
43
  `;
44
44
 
45
45
  let _topbit_home_page = `<!DOCTYPE html><html>
@@ -916,39 +916,70 @@ class Topbit {
916
916
 
917
917
  });
918
918
  }
919
-
920
- /**
921
- * 输出路由表,如果是启用了cluster,则通过发送消息的方式让master进程输出。
922
- * */
923
- if (this.config.debug) {
924
- if (typeof port === 'string' && port.indexOf('.sock') > 0) {
925
- host = '';
926
- }
927
-
928
- let is_https = this.config.https;
929
-
930
- let protocol = this.config.http2
931
- ? ('http2' + (is_https ? '[https]' : ''))
932
- : (is_https ? 'https' : 'http');
933
-
934
- if (cluster.isMaster) {
935
- this.router.printTable();
936
- console.log(`PID: ${process.pid}, Listen ${host}:${port}, Protocol: ${protocol}`);
937
- console.log(_topbit_server_running);
938
- } else if (process.send && typeof process.send === 'function') {
939
- process.send({type:'_route-table',
940
- route: this.router.getTable(),
941
- listen: `Listen: ${host}${host.length > 0 ? ':' : ''}${port}, `,
942
- protocol: `Protocol: ${protocol}`
943
- });
944
- }
945
- }
946
-
919
+
947
920
  this.server = this.httpServ.run(port, host);
948
921
  return this.server;
949
922
  }
950
923
  //run end
951
924
 
925
+ /**
926
+ *
927
+ * @param {number} timeout - default 0
928
+ * @param {boolean} debug - default true(true表示只有debug模式才会输出)
929
+ * @returns
930
+ */
931
+ printServInfo(timeout=0, debug=true) {
932
+ if (typeof timeout === 'boolean') {
933
+ debug = timeout
934
+ timeout = 0
935
+ }
936
+
937
+ if (!debug || (debug && this.config.debug)) {
938
+ if (timeout > 0) {
939
+ setTimeout(() => {
940
+ this._corePrintServ()
941
+ }, timeout)
942
+ } else {
943
+ queueMicrotask(() => {
944
+ this._corePrintServ()
945
+ })
946
+ }
947
+ }
948
+
949
+ return this
950
+ }
951
+
952
+ _corePrintServ() {
953
+ let {port, host} = this.rundata
954
+
955
+ /**
956
+ * 输出路由表,如果是启用了cluster,则通过发送消息的方式让master进程输出。
957
+ * */
958
+ if (typeof port === 'string' && port.indexOf('.sock') > 0) {
959
+ host = '';
960
+ }
961
+
962
+ let is_https = this.config.https;
963
+
964
+ let protocol = this.config.http2
965
+ ? ('http2' + (is_https ? '[https]' : ''))
966
+ : (is_https ? 'https' : 'http');
967
+
968
+ if (cluster.isMaster) {
969
+ this.router.printTable();
970
+ console.log(`PID: ${process.pid}, Listen ${host}:${port}, Protocol: ${protocol}`);
971
+ console.log(_topbit_server_running);
972
+ } else if (process.send && typeof process.send === 'function') {
973
+ process.send({type:'_route-table',
974
+ route: this.router.getTable(),
975
+ listen: `Listen: ${host}${host.length > 0 ? ':' : ''}${port}, `,
976
+ protocol: `Protocol: ${protocol}`
977
+ });
978
+ }
979
+
980
+ return this
981
+ }
982
+
952
983
  /**
953
984
  * @param {string} evt
954
985
  * @param {function} callback
@@ -968,8 +999,9 @@ class Topbit {
968
999
  count: 0,
969
1000
  mode: mode,
970
1001
  callback: callback
971
- };
972
- return true;
1002
+ }
1003
+
1004
+ return this;
973
1005
  }
974
1006
 
975
1007
  getMsgEvent(evt) {
@@ -1047,11 +1079,13 @@ class Topbit {
1047
1079
  }
1048
1080
 
1049
1081
  autoWorker(max) {
1050
- if (typeof max === 'number' && max > 0) {
1051
- this.workerCount.max = max;
1082
+ if (!isNaN(max) && typeof max === 'number' && max >= 0) {
1083
+ this.workerCount.max = max > 0 ? max : os.cpus().length;
1052
1084
  } else {
1053
- throw new Error('autoWorker参数必须是一个大于0的数字,表示最大允许创建多少个子进程处理请求。');
1085
+ throw new Error('autoWorker参数必须是一个>=0的数字,表示最大允许创建多少个子进程处理请求。');
1054
1086
  }
1087
+
1088
+ return this;
1055
1089
  }
1056
1090
 
1057
1091
  /**
@@ -1066,6 +1100,8 @@ class Topbit {
1066
1100
  } else {
1067
1101
  return cluster.schedulingPolicy;
1068
1102
  }
1103
+
1104
+ return this;
1069
1105
  }
1070
1106
 
1071
1107
  _checkDaemonArgs() {