topbit 1.0.0 → 3.0.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.
Files changed (89) hide show
  1. package/LICENSE +128 -0
  2. package/README.cn.md +1519 -0
  3. package/README.md +1483 -0
  4. package/bin/app.js +17 -0
  5. package/bin/loadinfo.sh +18 -0
  6. package/bin/new-ctl.js +234 -0
  7. package/bin/newapp.js +22 -0
  8. package/demo/allow.js +98 -0
  9. package/demo/cert/localhost-cert.pem +19 -0
  10. package/demo/cert/localhost-privkey.pem +28 -0
  11. package/demo/controller/api.js +15 -0
  12. package/demo/extends.js +5 -0
  13. package/demo/group-api.js +161 -0
  14. package/demo/group-api2.js +109 -0
  15. package/demo/http2.js +34 -0
  16. package/demo/http2_proxy_backend.js +45 -0
  17. package/demo/http2proxy.js +48 -0
  18. package/demo/http_proxy_backend.js +44 -0
  19. package/demo/httpproxy.js +47 -0
  20. package/demo/loader.js +27 -0
  21. package/demo/log.js +118 -0
  22. package/demo/memlimit.js +31 -0
  23. package/demo/min.js +7 -0
  24. package/demo/serv.js +15 -0
  25. package/images/middleware.jpg +0 -0
  26. package/images/topbit-middleware.png +0 -0
  27. package/images/topbit.png +0 -0
  28. package/package.json +42 -11
  29. package/src/_loadExtends.js +21 -0
  30. package/src/bodyparser.js +420 -0
  31. package/src/connfilter.js +125 -0
  32. package/src/context1.js +166 -0
  33. package/src/context2.js +182 -0
  34. package/src/ctxpool.js +39 -0
  35. package/src/ext.js +318 -0
  36. package/src/extends/Http2Pool.js +365 -0
  37. package/src/extends/__randstring.js +24 -0
  38. package/src/extends/cookie.js +44 -0
  39. package/src/extends/cors.js +334 -0
  40. package/src/extends/errorlog.js +252 -0
  41. package/src/extends/http2limit.js +126 -0
  42. package/src/extends/http2proxy.js +691 -0
  43. package/src/extends/jwt.js +217 -0
  44. package/src/extends/mixlogger.js +63 -0
  45. package/src/extends/paramcheck.js +266 -0
  46. package/src/extends/proxy.js +662 -0
  47. package/src/extends/realip.js +34 -0
  48. package/src/extends/referer.js +68 -0
  49. package/src/extends/resource.js +398 -0
  50. package/src/extends/session.js +174 -0
  51. package/src/extends/setfinal.js +50 -0
  52. package/src/extends/sni.js +48 -0
  53. package/src/extends/sse.js +293 -0
  54. package/src/extends/timing.js +111 -0
  55. package/src/extends/tofile.js +123 -0
  56. package/src/fastParseUrl.js +426 -0
  57. package/src/headerLimit.js +18 -0
  58. package/src/http1.js +336 -0
  59. package/src/http2.js +337 -0
  60. package/src/httpc.js +251 -0
  61. package/src/lib/npargv.js +354 -0
  62. package/src/lib/zipdata.js +45 -0
  63. package/src/loader/loader.js +999 -0
  64. package/src/logger.js +32 -0
  65. package/src/loggermsg.js +349 -0
  66. package/src/makeId.js +200 -0
  67. package/src/midcore.js +213 -0
  68. package/src/middleware1.js +103 -0
  69. package/src/middleware2.js +116 -0
  70. package/src/monitor.js +380 -0
  71. package/src/movefile.js +30 -0
  72. package/src/optionsCheck.js +54 -0
  73. package/src/randstring.js +23 -0
  74. package/src/router.js +682 -0
  75. package/src/sendmsg.js +27 -0
  76. package/src/strong.js +72 -0
  77. package/src/token/token.js +461 -0
  78. package/src/topbit.js +1293 -0
  79. package/src/versionCheck.js +31 -0
  80. package/test/test-bigctx.js +29 -0
  81. package/test/test-daemon-args.js +7 -0
  82. package/test/test-ext.js +81 -0
  83. package/test/test-find.js +69 -0
  84. package/test/test-route-sort.js +71 -0
  85. package/test/test-route.js +49 -0
  86. package/test/test-route2.js +51 -0
  87. package/test/test-run-args.js +7 -0
  88. package/test/test-url.js +52 -0
  89. package/main.js +0 -0
@@ -0,0 +1,109 @@
1
+ 'use strict'
2
+
3
+ const Titbit = require('../lib/titbit.js')
4
+
5
+ let app = new Titbit({
6
+ debug: true,
7
+ globalLog: true
8
+ })
9
+
10
+ let sub = app.group('/api')
11
+
12
+ sub.pre(async (ctx, next) => {
13
+ console.log('sub start')
14
+ await next()
15
+ console.log('sub end')
16
+ })
17
+
18
+ sub.get('/t', async ctx => {
19
+ ctx.send({
20
+ group: ctx.group,
21
+ path: ctx.path
22
+ })
23
+ })
24
+
25
+ let subsub = sub.group('/sub')
26
+
27
+ subsub.pre(async (ctx, next) => {
28
+ console.log('sub 2 start')
29
+ await next()
30
+ console.log('sub 2 end')
31
+ })
32
+ .get('/.ok', async ctx => {
33
+ ctx.send('ok')
34
+ })
35
+
36
+ subsub.get('/subt', async ctx => {
37
+ ctx.send({
38
+ group: ctx.group,
39
+ path: ctx.path
40
+ })
41
+ })
42
+
43
+ let ar = app.middleware([
44
+ async (ctx, next) => {
45
+ console.log('request timing start')
46
+ console.time('request')
47
+ await next()
48
+ console.timeEnd('request')
49
+ }
50
+ ], {pre: true}).group('/ar')
51
+
52
+ ar.get('/test', async ctx => {
53
+ ctx.send('test ar')
54
+ })
55
+
56
+ ar.post('/test', async ctx => {
57
+ ctx.send(ctx.body)
58
+ })
59
+
60
+ let arsub = ar.group('/s')
61
+
62
+ arsub.use(async (ctx, next) => {
63
+ console.log('ar sub start')
64
+ await next()
65
+ console.log('ar sub end')
66
+ })
67
+
68
+ arsub.get('/rich', async ctx => {
69
+ ctx.send('success')
70
+ })
71
+
72
+ app.post('/d', async ctx => {
73
+ ctx.send(ctx.body)
74
+ }, {group: 'data', name: 'data'})
75
+
76
+ app.post('/x', async ctx => {
77
+ ctx.send(ctx.body)
78
+ }, {group: 'data', name: 'x'})
79
+
80
+ app.put('/y/:id', async ctx => {
81
+ ctx.send({
82
+ param: ctx.param,
83
+ body: ctx.body
84
+ })
85
+ }, {group: 'data', name: 'y'})
86
+
87
+ app.pre(async (ctx, next) => {
88
+ console.log(ctx.group, ctx.path, 'start')
89
+ await next()
90
+ console.log(ctx.group, ctx.path, 'end')
91
+ }, '@data')
92
+
93
+ app.pre(async (ctx, next) => {
94
+ console.log(ctx.group, ctx.path, 'start', Math.random())
95
+ await next()
96
+ console.log(ctx.group, ctx.path, 'end', Math.random())
97
+ }, {group: 'data', name: 'x'})
98
+
99
+ app.pre(async (ctx, next) => {
100
+ console.log(ctx.group, ctx.path, ctx.routepath, 'start')
101
+ await next()
102
+ console.log(ctx.group, ctx.path, ctx.routepath, 'end')
103
+ }, {
104
+ group: 'data',
105
+ method: 'PUT',
106
+ name: 'y'
107
+ })
108
+
109
+ app.run(1235)
package/demo/http2.js ADDED
@@ -0,0 +1,34 @@
1
+ 'use strict'
2
+
3
+ 'use strict'
4
+
5
+ process.chdir(__dirname)
6
+
7
+ let Topbit = require('../src/topbit.js')
8
+ let {Loader} = Topbit
9
+
10
+ let app = new Topbit({
11
+ debug: true,
12
+ globalLog: true,
13
+ logType: 'stdio',
14
+ loadInfoFile: '--mem',
15
+ cert: './cert/localhost-cert.pem',
16
+ key: './cert/localhost-privkey.pem',
17
+ http2: true
18
+ })
19
+
20
+ if (app.isWorker) {
21
+ app.get('/', async ctx => {
22
+ ctx.ok('ok')
23
+ })
24
+
25
+ let ld = new Loader()
26
+
27
+ ld.init(app)
28
+ }
29
+
30
+ app.sched('none')
31
+
32
+ app.autoWorker(3)
33
+
34
+ app.daemon(1234, 1)
@@ -0,0 +1,45 @@
1
+ 'use strict'
2
+
3
+ const Topbit = require('../src/topbit.js')
4
+
5
+ const app = new Topbit({
6
+ debug: true,
7
+ http2: true,
8
+ loadInfoFile: '/tmp/loadinfo.log',
9
+ globalLog: true,
10
+ monitorTimeSlice: 512,
11
+ timeout: 0
12
+ })
13
+
14
+ app.use(async (c, next) => {
15
+ c.setHeader('x-set-key', `${parseInt(Math.random() * 10000) + Date.now()}`)
16
+ await next(c)
17
+ })
18
+
19
+ app.get('/header', async c => {
20
+ c.to(c.headers)
21
+ })
22
+
23
+ app.get('/', async c => {
24
+ c.to(Math.random())
25
+ })
26
+
27
+ app.get('/:name/:age/:mobile/:info', async c => {
28
+ c.to(c.param)
29
+ })
30
+
31
+ app.post('/p', async c => {
32
+ c.to(c.body)
33
+ })
34
+
35
+ let port = 2022
36
+ let port_ind = process.argv.indexOf('--port')
37
+
38
+ if (port_ind > 0 && port_ind < process.argv.length - 1) {
39
+ port = parseInt(process.argv[port_ind + 1])
40
+
41
+ if (typeof port !== 'number')
42
+ port = 2022
43
+ }
44
+
45
+ app.run(port)
@@ -0,0 +1,48 @@
1
+ 'use strict'
2
+
3
+ let Topbit = require('../src/topbit.js')
4
+
5
+ let {Http2Proxy} = Topbit.extensions
6
+
7
+ let app = new Topbit({
8
+ debug: true,
9
+ globalLog: true,
10
+ loadInfoFile: '--mem',
11
+ http2: true
12
+ })
13
+
14
+ if (app.isWorker) {
15
+ let h2proxy = new Http2Proxy({
16
+ config: {
17
+ 'x.com': [
18
+ {
19
+ url: 'http://localhost:3001',
20
+ weight: 10,
21
+ path : '/',
22
+ reconnDelay: 200,
23
+ max: 2,
24
+ headers: {
25
+ 'x-test-key': `${Date.now()}-${Math.random()}`
26
+ },
27
+ connectTimeout: 2000
28
+ },
29
+
30
+ {
31
+ url: 'http://localhost:3002',
32
+ weight: 4,
33
+ path : '/',
34
+ max: 2,
35
+ reconnDelay: 100,
36
+ headers: {
37
+ 'x-test-key2': `${Date.now()}-${Math.random()}`
38
+ }
39
+ }
40
+ ]
41
+ },
42
+ debug: true
43
+ })
44
+
45
+ h2proxy.init(app)
46
+ }
47
+
48
+ app.daemon(1234, 2)
@@ -0,0 +1,44 @@
1
+ 'use strict'
2
+
3
+ const Topbit = require('../src/topbit.js')
4
+
5
+ const app = new Topbit({
6
+ debug: true,
7
+ loadInfoFile: '/tmp/loadinfo.log',
8
+ globalLog: true,
9
+ monitorTimeSlice: 512,
10
+ timeout: 100000
11
+ })
12
+
13
+ app.use(async (c, next) => {
14
+ c.setHeader('x-set-key', `${parseInt(Math.random() * 10000) + Date.now()}`)
15
+ await next(c)
16
+ })
17
+
18
+ app.get('/header', async c => {
19
+ c.to(c.headers)
20
+ })
21
+
22
+ app.get('/', async c => {
23
+ c.to(Math.random())
24
+ })
25
+
26
+ app.get('/:name/:age/:mobile/:info', async c => {
27
+ c.to(c.param)
28
+ })
29
+
30
+ app.post('/p', async c => {
31
+ c.to(c.body)
32
+ })
33
+
34
+ let port = 2022
35
+ let port_ind = process.argv.indexOf('--port')
36
+
37
+ if (port_ind > 0 && port_ind < process.argv.length - 1) {
38
+ port = parseInt(process.argv[port_ind + 1])
39
+
40
+ if (typeof port !== 'number')
41
+ port = 2022
42
+ }
43
+
44
+ app.run(port)
@@ -0,0 +1,47 @@
1
+ 'use strict'
2
+
3
+ let Topbit = require('../src/topbit.js')
4
+
5
+ let {Proxy} = Topbit.extensions
6
+
7
+ let app = new Topbit({
8
+ debug: true,
9
+ globalLog: true,
10
+ loadInfoFile: '--mem',
11
+ })
12
+
13
+ if (app.isWorker) {
14
+ let pxy = new Proxy({
15
+ config: {
16
+ 'x.com': [
17
+ {
18
+ url: 'http://localhost:3001',
19
+ weight: 10,
20
+ path : '/',
21
+ reconnDelay: 200,
22
+ max: 2,
23
+ headers: {
24
+ 'x-test-key': `${Date.now()}-${Math.random()}`
25
+ },
26
+ connectTimeout: 2000
27
+ },
28
+
29
+ {
30
+ url: 'http://localhost:3002',
31
+ weight: 4,
32
+ path : '/',
33
+ max: 2,
34
+ reconnDelay: 100,
35
+ headers: {
36
+ 'x-test-key2': `${Date.now()}-${Math.random()}`
37
+ }
38
+ }
39
+ ]
40
+ },
41
+ debug: true
42
+ })
43
+
44
+ pxy.init(app)
45
+ }
46
+
47
+ app.daemon(1234, 2)
package/demo/loader.js ADDED
@@ -0,0 +1,27 @@
1
+ 'use strict'
2
+
3
+ process.chdir(__dirname)
4
+
5
+ let Topbit = require('../src/topbit.js')
6
+ let {Loader} = Topbit
7
+
8
+ let app = new Topbit({
9
+ debug: true,
10
+ //loadInfoFile: '--mem',
11
+ })
12
+
13
+ if (app.isWorker) {
14
+ app.get('/', async ctx => {
15
+ ctx.ok('ok')
16
+ })
17
+
18
+ let ld = new Loader()
19
+
20
+ ld.init(app)
21
+ }
22
+
23
+ app.sched('none')
24
+
25
+ app.autoWorker(3)
26
+
27
+ app.daemon(1234, 1)
package/demo/log.js ADDED
@@ -0,0 +1,118 @@
1
+ const titbit = require('../src/topbit.js');
2
+ const v8 = require('v8');
3
+ const cluster = require('cluster');
4
+
5
+ process.on('exit', (code) => {
6
+ console.log('EXIT CODE:', code);
7
+ });
8
+
9
+ /*
10
+ if (cluster.isWorker) {
11
+ setInterval(() => {
12
+ console.log(v8.getHeapStatistics());
13
+ }, 15000);
14
+ }
15
+ */
16
+
17
+ async function delay(t) {
18
+ return await new Promise((rv, rj) => {
19
+ setTimeout(() => {
20
+ rv();
21
+ }, t);
22
+ });
23
+ }
24
+
25
+ let app = new titbit({
26
+ debug: true,
27
+ globalLog : true,
28
+ //loadInfoType : 'text',
29
+ loadInfoFile : '/tmp/loadinfo.log',
30
+ timeout : 15000,
31
+ //socktimeout: 1000,
32
+ useLimit: true,
33
+ maxConn: 6000,
34
+ logType : 'file',
35
+ logFile: '/tmp/access.log',
36
+ errorLogFile : '/tmp/error.log',
37
+ logMaxLines: 10,
38
+ logHistory: 10
39
+ });
40
+
41
+ app.addService('name', 'brave');
42
+
43
+ var _key = 'abcdefghijklmnopqrstuvwxyz123456';
44
+
45
+ app.get('/', async c => {
46
+ c.data = 'success';
47
+ },{name:'home', group:'/'});
48
+
49
+ app.get('/uuid', async c => {
50
+ c.data = c.ext.uuid()
51
+ });
52
+
53
+ app.get('/timeout/:tm', async ctx => {
54
+ await new Promise((rv, rj) => {
55
+ setTimeout(() => {
56
+ rv()
57
+ }, parseInt(ctx.param.tm) || 10)
58
+ })
59
+
60
+ ctx.send(`timeout ok ${ctx.param.tm}`)
61
+ });
62
+
63
+ app.post('/p', async c => {
64
+ c.data = c.body;
65
+ });
66
+
67
+ app.get('/name', async c => {
68
+ c.data = c.service.name;
69
+ });
70
+
71
+ app.get('/tout', async c => {
72
+
73
+ await delay(1800);
74
+
75
+ c.response.write('handling...');
76
+
77
+ await delay(1000);
78
+
79
+ c.data = 'timeout test';
80
+ });
81
+
82
+ app.post('/tout', async c => {
83
+ await delay (119);
84
+
85
+ console.log('start');
86
+ c.response.write('start');
87
+
88
+ await delay (119);
89
+
90
+ console.log('not end');
91
+ c.response.write('start 2');
92
+
93
+ await delay(18000);
94
+
95
+ c.response.write('handling...');
96
+
97
+ await delay(10000);
98
+
99
+ c.data = 'timeout test' + JSON.stringify(c.body);
100
+ });
101
+
102
+ app.get('/encrypt', async c => {
103
+ c.data = c.helper.aesEncrypt(JSON.stringify(c.query), _key);
104
+ });
105
+
106
+ app.get('/decrypt', async c => {
107
+ c.data = c.helper.aesDecrypt(c.query.data, _key);
108
+ });
109
+
110
+ app.get('/sha256', async c => {
111
+ c.data = c.helper.sha256(`${Math.random()}${Date.now()}`);
112
+ });
113
+
114
+ //app.logger.watch();
115
+
116
+ app.sched('none')
117
+
118
+ app.daemon(2025, 2)
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ const titbit = require('../src/topbit.js');
4
+
5
+ const cluster = require('cluster');
6
+
7
+ const app = new titbit({
8
+ maxBody : 100000000,
9
+ debug: true,
10
+ //showLoadInfo: false,
11
+ memFactor: -0.43,
12
+ loadInfoFile: '/tmp/loadinfo.log'
13
+ })
14
+
15
+ if (app.isWorker) {
16
+ app.addService('data', {})
17
+
18
+ setInterval(() => {
19
+ for (let i = 0; i < 100; i++)
20
+ app.service.data[ `${Math.random()}` ] = Date.now()
21
+ }, 5)
22
+ }
23
+
24
+
25
+ if (cluster.isMaster) {
26
+ setTimeout(() => {
27
+ console.log(app.secure);
28
+ }, 10);
29
+ }
30
+
31
+ app.daemon(1234, 9)
package/demo/min.js ADDED
@@ -0,0 +1,7 @@
1
+ const titbit = require('../src/topbit.js');
2
+
3
+ const app = new titbit({
4
+ debug: true
5
+ })
6
+
7
+ app.run(1234)
package/demo/serv.js ADDED
@@ -0,0 +1,15 @@
1
+ const titbit = require('../src/topbit.js')
2
+
3
+ const app = new titbit({
4
+ debug: true,
5
+ loadInfoFile: '--mem',
6
+ //http2: true,
7
+ key: __dirname + '/../cache/rsa/localhost-privkey.pem',
8
+ cert: __dirname + '/../cache/rsa/localhost-cert.pem'
9
+ })
10
+
11
+ app.get('/js', async ctx => {
12
+ await ctx.pipeText(__filename)
13
+ })
14
+
15
+ app.run(1230)
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,27 +1,58 @@
1
1
  {
2
2
  "name": "topbit",
3
- "version": "1.0.0",
4
- "description": "webserver",
3
+ "version": "3.0.0",
4
+ "description": "A Server-side web framework support http/1.1 and http/2",
5
+ "main": "src/topbit.js",
6
+ "directories": {
7
+ "src": "src"
8
+ },
9
+ "bin": {
10
+ "tbload": "bin/loadinfo.sh",
11
+ "topbit-new-app": "bin/newapp.js",
12
+ "new-route": "bin/new-ctl.js"
13
+ },
14
+ "scripts": {
15
+ "test": "node test/test-ext.js",
16
+ "test-route": "node test/test-route.js"
17
+ },
5
18
  "keywords": [
6
- "web"
19
+ "top",
20
+ "topbit",
21
+ "tit",
22
+ "tibit",
23
+ "titbit",
24
+ "tidbit",
25
+ "bit",
26
+ "http",
27
+ "server",
28
+ "http/1.1",
29
+ "http/2",
30
+ "http2",
31
+ "web",
32
+ "middleware",
33
+ "framework",
34
+ "tidbit",
35
+ "proxy",
36
+ "http2proxy",
37
+ "mvc",
38
+ "api",
39
+ "webserver",
40
+ "backend",
41
+ "serverside"
7
42
  ],
8
- "homepage": "https://github.com/master-genius/nodebit#readme",
43
+ "homepage": "https://github.com/master-genius/topbit#readme",
9
44
  "bugs": {
10
- "url": "https://github.com/master-genius/nodebit/issues"
45
+ "url": "https://github.com/master-genius/topbit/issues"
11
46
  },
12
47
  "repository": {
13
48
  "type": "git",
14
- "url": "git+https://github.com/master-genius/nodebit.git"
49
+ "url": "git+https://github.com/master-genius/topbit.git"
15
50
  },
16
51
  "license": "ISC",
17
52
  "author": "BraveWang",
18
53
  "type": "commonjs",
19
- "main": "main.js",
20
- "scripts": {
21
- "test": "echo ok"
22
- },
23
54
  "dependencies": {
24
- "pg": "^8.11.3"
55
+
25
56
  },
26
57
  "devDependencies": {}
27
58
  }
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs')
4
+
5
+ let files = fs.readdirSync(`${__dirname}/extends/`, {withFileTypes: true})
6
+
7
+ for (let i = 0; i < files.length; i++) {
8
+
9
+ if (!files[i].isFile()) continue
10
+
11
+ if (files[i].name.indexOf('.js') < 0) continue
12
+
13
+ if (files[i].name.substring(files[i].name.length - 3) !== '.js') continue
14
+
15
+ if (files[i].name[0] === '_' || files[i].name[0] === '!') continue
16
+
17
+ let modname = files[i].name.substring(0, files[i].name.length-3)
18
+
19
+ let em = require('./extends/'+files[i].name)
20
+ exports[em.name] = em
21
+ }