topbit 3.0.7 → 3.0.8

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/bin/new-ctl.js CHANGED
@@ -4,66 +4,33 @@
4
4
 
5
5
  let simple_mode = false
6
6
 
7
- let type_context = `\n//!!注意:以下注释是为了能够在支持jsdoc的代码编辑器中提示以及方便查阅提供,不需要可以去掉。
7
+ let type_context = `
8
8
  /**
9
- * ---------------------- ctx.ext模块具备的方法 -----------------------------
10
- *
11
- * @typedef {object} ext
12
- * @property {function} extName(filename: string) - 解析文件扩展名。
13
- * @property {function} makeName(filename: string) - 生成唯一文件名,默认为时间字符串+随机数:2021-01-01_12-12-23_27789.jpg,filename主要用于获取扩展名。
14
- * @property {function} nrand(n, m) - 返回两个数之间的随机数。
15
- * @property {function} uuid() - 返回唯一字符串用于唯一ID,支持标准的8-4-4-4-12和短字符串8-2-2-4两种模式(传递参数为true)。
16
- * @property {function} timestr() - 默认返回年-月-日_时-分-秒,没有空格,方便保存和解析。
17
- * @property {function} sha1(text, encoding = 'hex')
18
- * @property {function} sha256(text, encoding = 'hex')
19
- * @property {function} sha512(text, encoding = 'hex')
20
- * @property {function} sm3(text, encoding = 'hex')
21
- * @property {function} hmacsha1(text, key, encoding = 'hex')
22
- * @property {function} pipe(filename: string, reply: object)
23
- *
24
- * 更多参考:{@link https://gitee.com/daoio/titbit/wikis/helper-function}
25
- */
26
- /**
27
- * --------------------------------- 请求上下文 -----------------------------------------
9
+ * ----------------------------- 请求上下文 -----------------------------------
28
10
  *
29
11
  * @typedef {object} context
30
12
  * @property {string} version - 协议版本,字符串类型,为'1.1' 或 '2' 或 '3'。
31
13
  * @property {number} major - 协议主要版本号,1、2、3分别表示HTTP/1.1 HTTP/2 HTTP/3。
32
14
  * @property {string} method - 请求类型,GET POST等HTTP请求类型,大写字母的字符串。
33
15
  * @property {string} path - 具体请求的路径,比如 /x/y。
34
- * @property {string} routepath - 实际执行请求的路由字符串,比如 /x/:id。
35
- * @property {boolean} isUpload - 是否为上传文件请求,此时就是检测消息头content-type是否为multipart/form-data格式。
16
+ * @property {string} routepath - 实际执行请求的路由字符串。
36
17
  * @property {object} box - 默认为空对象,可以添加任何属性值,用来动态传递给下一层组件需要使用的信息。
37
18
  * @property {object} query - url传递的参数。
38
19
  * @property {object} param - 路由参数。
39
- * @property {object} headers - 指向request.headers。
40
- * @property {object} files - 上传文件保存的信息。
41
- * @property {(object|string|buffer|null)} body - body请求体的数据,具体格式需要看content-type,一般为字符串或者对象,也可能是buffer。
42
- * @property {object} request HTTP/1.1 - 就是http模块request事件的参数IncomingMessage对象,HTTP/2 指向stream对象。
43
- * @property {object} reply - HTTP/1.1协议,指向response,HTTP/2 指向stream。
20
+ * @property {object} headers - 指向req.headers。
21
+ * @property {(object|string|buffer)} body - body请求体的数据,具体格式需要看content-type,一般为字符串或者对象,也可能是buffer。
22
+ * @property {object} req HTTP/1.1 - 就是http模块request事件的参数IncomingMessage对象,HTTP/2 指向stream对象。
23
+ * @property {object} res - HTTP/1.1协议,指向response,HTTP/2 指向stream
44
24
  * @property {object} service - 用于依赖注入的对象,指向app.service。
45
- * @property {function} moveFile(file: object) - 用来移动上传的文件到指定路径,file是通过getFile获取的文件对象。
46
- * @property {function} getFile(name: string, index = 0) - 根据上传名获取上传的文件,如果index是-1表示获取整个数组。
47
- * @property {function} sendHeader() - 发送消息头,针对http/2设计,http/1.1只是一个空函数,为了代码保持一致。
25
+ * @property {function} moveFile(file: object) - 用来移动上传的文件到指定路径。
26
+ * @property {function} getFile() - 根据上传名获取上传的文件。
48
27
  * @property {function} setHeader(key: string, value: string|array)
49
- * @property {function} send(body: string|object|buffer) - 设置要返回的body数据。
28
+ * @property {function} to(body: string|object|buffer) - 设置要返回的body数据。
50
29
  * @property {function} status(code: null | number) - 设置状态码,默认为null表示返回状态码。
51
- * @property {ext} ext - 指向ext模块,提供了一些助手函数,具体参考wiki。
52
- *
53
- * 更多参考:{@link https://gitee.com/daoio/titbit/blob/master/README.md#%E8%AF%B7%E6%B1%82%E4%B8%8A%E4%B8%8B%E6%96%87}
54
30
  */
55
31
  `
56
32
 
57
- let head_hint = `/**********************************************************************
58
- 提示:
59
- 表单提交或异步请求,对应于POST或PUT请求,对应函数post和put,提交的请求体数据通过 ctx.body 获取。
60
-
61
- 路由参数通过 ctx.param 获取,示例:let id = ctx.param.id
62
-
63
- url参数(?a=1&b=2)通过 ctx.query 获取,示例:let name = ctx.query.name
64
-
65
- 使用ctx.getFile(name)获取上传的文件,示例:let f = ctx.getFile('image')
66
- **********************************************************************/\n\n`
33
+ let head_hint = ''
67
34
 
68
35
  function fmt_ctx_param(text) {
69
36
  let ctx_param = `
@@ -88,16 +55,14 @@ function makeController(name) {
88
55
  + (simple_mode ? '' : head_hint)
89
56
  + `class ${modname} {\n\n`
90
57
  + ` constructor() {\n`
91
- + ` //param用于指定最后的路由参数,默认就是/:id\n`
92
- + ` //若要改变路由,则可以设置此属性,比如设置为/:name\n`
58
+ + ` //若要改变路由,则设置此属性,比如设置为/:name\n`
93
59
  + ` //this.param = '/:id'\n`
94
60
  + ` }\n\n`
95
- + ` //控制器类初始化后会执行此函数。\n`
96
- + ` //service默认是app.service,此参数通过titbit-loader初始化的initArgs选项进行控制。\n`
61
+ + ` //加载器初始化后会执行此函数。service默认是app.service。\n`
97
62
  + ` async init(service) {\n`
98
63
  + ` \n`
99
64
  + ` }\n\n`
100
- + (simple_mode ? '' : ` //以下方法,若不需要,要去掉,避免无意义的路由。\n`)
65
+ + (simple_mode ? '' : ` // 根据实际需要增加或删除请求方法。\n`)
101
66
  + `${fmt_ctx_param('获取资源具体内容')}`
102
67
  + ` async get(ctx) {\n\n`
103
68
  + ` }\n`
@@ -112,7 +77,7 @@ function makeController(name) {
112
77
  + ` }\n`
113
78
  + `${fmt_ctx_param('删除资源')}`
114
79
  + ` async _delete(ctx) {\n\n`
115
- + ` }\n`
80
+ + ` }\n\n`
116
81
  + `}\n\nmodule.exports = ${modname}\n`
117
82
  + (simple_mode ? '' : `${type_context}`)
118
83
 
@@ -35,8 +35,8 @@ Built entirely on Node.js native `crypto`, supports:
35
35
  ### 3. 30-Second Quick Start
36
36
 
37
37
  ```js
38
- // middleware/@token.js
39
- const TopbitToken = require('topbit-token')
38
+ const Topbit = require('topbit')
39
+ const TopbitToken = Topbit.Token
40
40
 
41
41
  const token = new TopbitToken({
42
42
  key : 'your-very-strong-32-byte-secret!!',
@@ -79,6 +79,9 @@ async post(c) {
79
79
  ### 5. Advanced: Multi-Key + Instant Revocation
80
80
 
81
81
  ```js
82
+ const Topbit = require('topbit')
83
+ const TopbitToken = Topbit.Token
84
+
82
85
  const token = new TopbitToken({ key: 'current-master-key' })
83
86
 
84
87
  // Add multiple key versions
@@ -112,8 +115,8 @@ Frontend just replaces the old one → seamless “never expire” experience.
112
115
  ### 7. Production Recommended Setup
113
116
 
114
117
  ```js
115
- // middleware/@auth.js
116
- const TopbitToken = require('topbit-token')
118
+ const Topbit = require('topbit')
119
+ const TopbitToken = Topbit.Token
117
120
 
118
121
  const token = new TopbitToken({
119
122
  algorithm : 'aes-256-gcm',
@@ -36,8 +36,8 @@ TopbitToken 是专为 Topbit 框架打造的零依赖、极简、高安全的加
36
36
  ### 三、快速上手(30 秒搞定登录认证)
37
37
 
38
38
  ```js
39
- // middleware/@token.js
40
- const TopbitToken = require('topbit-token')
39
+ const Topbit = require('topbit')
40
+ const TopbitToken = Topbit.Token
41
41
 
42
42
  const token = new TopbitToken({
43
43
  key : 'your-32-byte-secret-key-here!!', // 必须 32 字节(AES-256)
@@ -89,6 +89,9 @@ token.mid() 会自动把验证后的用户信息挂到 c.user
89
89
  ### 五、高级功能:多密钥 + 即时失效(防泄漏神器)
90
90
 
91
91
  ```js
92
+ const Topbit = require('topbit')
93
+ const TopbitToken = Topbit.Token
94
+
92
95
  const token = new TopbitToken({
93
96
  key: 'master-key-2025-01-01',
94
97
  expires: 3600 * 24 * 30
@@ -113,6 +116,9 @@ token.tokenIds = []
113
116
  ### 六、自动刷新 Token 机制
114
117
 
115
118
  ```js
119
+ const Topbit = require('topbit')
120
+ const TopbitToken = Topbit.Token
121
+
116
122
  const token = new TopbitToken({
117
123
  expires: 3600 * 24, // 24小时有效
118
124
  refresh: true // 开启自动刷新
@@ -128,8 +134,8 @@ const token = new TopbitToken({
128
134
  ### 七、最佳实践(生产级推荐配置)
129
135
 
130
136
  ```js
131
- // middleware/@auth.js
132
- const TopbitToken = require('topbit-token')
137
+ const Topbit = require('topbit')
138
+ const TopbitToken = Topbit.Token
133
139
 
134
140
  const token = new TopbitToken({
135
141
  algorithm : 'aes-256-gcm',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topbit",
3
- "version": "3.0.7",
3
+ "version": "3.0.8",
4
4
  "description": "A Server-side web framework support http/1.1 and http/2",
5
5
  "main": "src/topbit.js",
6
6
  "directories": {