topbit 1.0.0 → 2.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 (71) hide show
  1. package/LICENSE +128 -0
  2. package/README.cn.md +1562 -0
  3. package/README.md +1272 -0
  4. package/bin/app.js +17 -0
  5. package/bin/loadinfo.sh +18 -0
  6. package/bin/new-ctl.js +230 -0
  7. package/bin/newapp.js +22 -0
  8. package/cache/allow.js +130 -0
  9. package/cache/errserv.js +45 -0
  10. package/cache/minserv.js +167 -0
  11. package/cache/router.js +84 -0
  12. package/cache/rsa/localhost-cert.pem +19 -0
  13. package/cache/rsa/localhost-privkey.pem +28 -0
  14. package/cache/servsock.js +286 -0
  15. package/cache/sni.js +66 -0
  16. package/demo/allow.js +98 -0
  17. package/demo/group-api.js +161 -0
  18. package/demo/group-api2.js +109 -0
  19. package/demo/log.js +118 -0
  20. package/demo/memlimit.js +31 -0
  21. package/demo/min.js +7 -0
  22. package/demo/serv.js +15 -0
  23. package/images/middleware.jpg +0 -0
  24. package/images/titbit-middleware.png +0 -0
  25. package/images/titbit.png +0 -0
  26. package/package.json +38 -8
  27. package/src/bodyparser.js +420 -0
  28. package/src/connfilter.js +125 -0
  29. package/src/context1.js +166 -0
  30. package/src/context2.js +179 -0
  31. package/src/ctxpool.js +38 -0
  32. package/src/ext.js +318 -0
  33. package/src/fastParseUrl.js +426 -0
  34. package/src/headerLimit.js +18 -0
  35. package/src/http1.js +337 -0
  36. package/src/http2.js +337 -0
  37. package/src/httpc.js +251 -0
  38. package/src/loader/loader.js +999 -0
  39. package/src/logger.js +32 -0
  40. package/src/loggermsg.js +349 -0
  41. package/src/makeId.js +200 -0
  42. package/src/midcore.js +213 -0
  43. package/src/middleware1.js +104 -0
  44. package/src/middleware2.js +121 -0
  45. package/src/monitor.js +380 -0
  46. package/src/movefile.js +30 -0
  47. package/src/optionsCheck.js +54 -0
  48. package/src/randstring.js +23 -0
  49. package/src/router.js +682 -0
  50. package/src/sendmsg.js +27 -0
  51. package/src/strong.js +72 -0
  52. package/src/token/token.js +462 -0
  53. package/src/topbit.js +1291 -0
  54. package/src/versionCheck.js +31 -0
  55. package/test/test-bigctx.js +29 -0
  56. package/test/test-daemon-args.js +7 -0
  57. package/test/test-find.js +69 -0
  58. package/test/test-helper.js +81 -0
  59. package/test/test-route-sort.js +71 -0
  60. package/test/test-route.js +49 -0
  61. package/test/test-route2.js +51 -0
  62. package/test/test-run-args.js +7 -0
  63. package/test/test-url.js +52 -0
  64. package/tmp/buff-code +134 -0
  65. package/tmp/devplan +9 -0
  66. package/tmp/evt-test.js +34 -0
  67. package/tmp/fastParseUrl.js +302 -0
  68. package/tmp/router-rule.js +559 -0
  69. package/tmp/test-cdps.js +122 -0
  70. package/tmp/titbit.js +1286 -0
  71. package/main.js +0 -0
@@ -0,0 +1,302 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ *
5
+ * 此函数是专门为了解析请求的路径和查询字符串部分而设计,因为url.parse在后续版本要抛弃,而URL解析后的searchParams无法和之前的程序兼容。
6
+ *
7
+ *
8
+ * 通过maxArgs控制最大解析的参数个数。
9
+ *
10
+ * 为了更快的处理,fpqs和fpurl以两个独立函数的方式编写,虽然有很多代码的逻辑重复。
11
+ *
12
+ * fpqs主要是针对content-type为application/x-www-form-urlencoded这种格式提供的。
13
+ *
14
+ */
15
+
16
+ let http_url_preg = /^https?:\/\//
17
+
18
+ let chararr = [
19
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
20
+ 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd',
21
+ 'e', 'f'
22
+ ]
23
+
24
+ let charMap = {}
25
+ chararr.forEach(x => {
26
+ charMap[x] = true
27
+ })
28
+
29
+ //这个函数没有对状态使用单独的变量标记
30
+ let is_encoded = (str) => {
31
+ let i = 0
32
+ let state = 0
33
+
34
+ for (i=0; i < str.length; ++i) {
35
+ //如果计数超过2说明是16进制的数字
36
+ if (state > 2) return true
37
+
38
+ if (state > 0) {
39
+ if (charMap[str[i]]) {
40
+ state++
41
+ continue
42
+ }
43
+ else state = 0
44
+ }
45
+
46
+ if (str[i] === '%') {
47
+ state = 1
48
+ } else {
49
+ state = 0
50
+ }
51
+ }
52
+
53
+ return false
54
+ }
55
+
56
+ function fpqs (search, obj, autoDecode=true, maxArgs=0) {
57
+ let ind = 0
58
+ let and_ind = 0
59
+ let last_ind = 0
60
+ let val
61
+ let org_val
62
+ let t
63
+ let count = 0
64
+ let send = search.length
65
+
66
+ while (and_ind < send) {
67
+ and_ind = search.indexOf('&', last_ind)
68
+
69
+ if (and_ind < 0) and_ind = send
70
+
71
+ if (maxArgs > 0 && count >= maxArgs) {
72
+ return
73
+ }
74
+
75
+ if (and_ind === last_ind) {
76
+ last_ind++
77
+ continue
78
+ }
79
+
80
+ ind = last_ind
81
+
82
+ while (ind < and_ind && search[ind] !== '=') ind++
83
+
84
+ if (last_ind >= ind) {
85
+ last_ind = and_ind + 1
86
+ continue
87
+ }
88
+
89
+ t = search.substring(last_ind, ind)
90
+
91
+ org_val = ind < and_ind ? search.substring(ind+1, and_ind) : ''
92
+
93
+ if (autoDecode) {
94
+ if (org_val.length > 2 && is_encoded(org_val)) {
95
+ try {
96
+ val = decodeURIComponent(org_val)
97
+ } catch (err) {
98
+ val = org_val
99
+ }
100
+ } else {
101
+ val = org_val
102
+ }
103
+ } else {
104
+ val = org_val
105
+ }
106
+
107
+ if (Array.isArray(obj[t])) {
108
+ obj[ t ].push(val)
109
+ } else {
110
+ if (obj[ t ] !== undefined) {
111
+ obj[ t ] = [ obj[ t ], val ]
112
+ } else {
113
+ count++
114
+ obj[ t ] = val
115
+ }
116
+ }
117
+
118
+ last_ind = and_ind + 1
119
+ }
120
+
121
+ }
122
+
123
+ /**
124
+ *
125
+ * @param {string} org_url
126
+ * url可能是完整的格式,一般来说是/开始的路径,这两种都是协议允许的格式。
127
+ *
128
+ * */
129
+
130
+ let httpchar = ['', '', '', '', '', '']
131
+
132
+ function fpurl (org_url, autoDecode=false, fastMode=true, maxArgs=0) {
133
+ let urlobj = {
134
+ path : '/',
135
+ query : {},
136
+ hash : ''
137
+ }
138
+
139
+ let oend = org_url.length
140
+ let start_ind = 0
141
+ let path_start = -1
142
+ let path_end = -1
143
+ let search_start = -1
144
+ let hash_start = -1
145
+ let search_end = -1
146
+
147
+ let http_mode = 0
148
+ let protocol = ''
149
+ let protocol_slash = false
150
+
151
+ let ch
152
+ for (let i=0; i < oend; i++) {
153
+ ch = org_url[i]
154
+ switch (ch) {
155
+ case '/':
156
+ ;(path_start < 0) && (path_start = i);
157
+ break
158
+
159
+ case '?':
160
+ ;(path_end < 0) && (path_end = i);
161
+ ;(search_start < 0) && (search_start = i + 1);
162
+ let sind = i + 1
163
+ while (sind < oend && org_url[sind] !== '#') sind++
164
+ ;(search_end < 0) && (search_end = sind);
165
+ i = sind-1
166
+
167
+ /*
168
+ let sind = i+1
169
+ let name_start = 0
170
+ let name_end = 0
171
+ let value_start = 0
172
+ let value_end = 0
173
+ let cur_name = ''
174
+ let org_val = ''
175
+ let val = ''
176
+ let query = urlobj.query
177
+
178
+ for (; sind < oend; sind++) {
179
+ if (org_url[sind] === '#') break
180
+
181
+ switch (org_url[sind]) {
182
+ case '=':
183
+ value_start = sind + 1
184
+ name_end = sind
185
+ cur_name = org_url.slice(name_start, name_end)
186
+ break
187
+
188
+ case '&':
189
+ value_end = sind
190
+ name_start = sind + 1
191
+ org_val = org_url.slice(value_start, value_end)
192
+
193
+ if (autoDecode && is_encoded(org_val)) {
194
+ try {
195
+ val = decodeURIComponent(org_val)
196
+ } catch (err) {
197
+ val = org_val
198
+ }
199
+ } else {
200
+ val = org_val
201
+ }
202
+
203
+ if (query[cur_name] === undefined || fastMode) {
204
+ query[cur_name] = val
205
+ } else {
206
+ if (!Array.isArray(query[cur_name])) {
207
+ query[cur_name] = [query[cur_name]]
208
+ }
209
+ query[cur_name].push(val)
210
+ }
211
+ break
212
+
213
+ }
214
+ }
215
+ i = sind-1
216
+ */
217
+ break
218
+
219
+ case '#':
220
+ ;(path_end < 0) && (path_end = i);
221
+ urlobj.hash = org_url.slice(i+1, oend)
222
+ i = oend
223
+ break
224
+ }
225
+ }
226
+
227
+ if (path_start >= 0) {
228
+ urlobj.path = org_url.slice(path_start, path_end < 0 ? oend : path_end)
229
+ }
230
+
231
+ if (search_start < 0) return urlobj
232
+
233
+ let search = org_url.slice(search_start, search_end < 0 ? oend : search_end)
234
+
235
+ let query = urlobj.query
236
+
237
+ let and_ind = 0
238
+ let last_ind = 0
239
+ let val
240
+ let org_val
241
+ let t
242
+ let ind = 0
243
+ let send = search.length
244
+ let count = 0
245
+
246
+ while (and_ind < send) {
247
+ and_ind = search.indexOf('&', last_ind)
248
+ if (and_ind < 0) and_ind = send
249
+
250
+ if (maxArgs > 0 && count >= maxArgs) {
251
+ break
252
+ }
253
+
254
+ if (and_ind === last_ind) {
255
+ last_ind++
256
+ continue
257
+ }
258
+
259
+ ind = last_ind
260
+
261
+ while (ind < and_ind && search[ind] !== '=') ind++
262
+
263
+ if (last_ind >= ind) {
264
+ last_ind = and_ind + 1
265
+ continue
266
+ }
267
+
268
+ t = search.substring(last_ind, ind)
269
+
270
+ org_val = search.slice(ind+1, and_ind)
271
+
272
+ if (autoDecode && is_encoded(org_val)) {
273
+ try {
274
+ val = decodeURIComponent(org_val)
275
+ } catch (err) {
276
+ val = org_val
277
+ }
278
+ } else {
279
+ val = org_val
280
+ }
281
+
282
+ if (query[t] === undefined || fastMode) {
283
+ query[t] = val
284
+ count++
285
+ } else {
286
+ if (!Array.isArray(query[t])) {
287
+ query[t] = [ query[t] ]
288
+ }
289
+ query[t].push(val)
290
+ }
291
+
292
+ last_ind = and_ind + 1
293
+ }
294
+
295
+ return urlobj
296
+ }
297
+
298
+ module.exports = {
299
+ fpurl,
300
+ fpqs
301
+ }
302
+