@solayao/koa-server-kit 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,180 @@
1
+ import { getPrototypeType } from './Common.js'
2
+
3
+ /**
4
+ * 数据库存储字符串转换
5
+ * @param {*} str
6
+ * @returns str[]
7
+ */
8
+ export const solGetStrArr = (str) => (str.replace(/ /g, '').replace(/\r/g, '\n').split('\n'))
9
+
10
+ /**
11
+ * 数据库存储字符串转换后结果转换成ts
12
+ * @param {*} strArr
13
+ * @returns object { [key]: [':'/'?:', tsTypeStr] }
14
+ */
15
+ export const solGetTsObj = (strArr) => {
16
+ for (let i = 0; i < strArr.length; i++) {
17
+ const temStr = strArr[i]
18
+
19
+ if (temStr.includes('{')) { // 截取对象字符串, 并修改原数组
20
+ let temIndex = i
21
+ let temAddNum = 1
22
+ do {
23
+ temIndex += 1
24
+ if (strArr[temIndex].includes('{')) {
25
+ temAddNum += 1
26
+ }
27
+ if (strArr[temIndex].includes('}')) {
28
+ temAddNum -= 1
29
+ }
30
+ } while (temAddNum > 0)
31
+
32
+ strArr.splice(i, temIndex - i + 1, strArr.slice(i, temIndex + 1).join('\n'))
33
+ }
34
+ }
35
+
36
+ return strArr.reduce((t, c) => {
37
+ const tempItemMatch = /^([\w_]+)(\??:)([\s\S]+)$/.exec(c)
38
+ if (tempItemMatch) {
39
+ t[tempItemMatch[1]] = [tempItemMatch[2], tempItemMatch[3]]
40
+ }
41
+ return t
42
+ }, {})
43
+ }
44
+
45
+ /**
46
+ * 验证数据是否符合基础Ts属性
47
+ * @param {*} data 数据
48
+ * @param {*} tsParam Ts定义
49
+ * @param {*} tsInclude : or ?:
50
+ * @returns {boolean}
51
+ */
52
+ export const solCheckDataFitOfTsParam = (data, tsParam, tsInclude) => {
53
+ if (tsInclude.includes('?') && ['', undefined, null].includes(data)) {
54
+ // tempIncludes = ?:, data的该key值为空
55
+ return true
56
+ }
57
+
58
+ if (/^(\s+)?(any)(\s+)?$/ig.test(tsParam)) {
59
+ // 验证any
60
+ return true
61
+ }
62
+
63
+ if (['', undefined, null].includes(data)) {
64
+ // data的该key值为空
65
+ return false
66
+ }
67
+
68
+ if (/^(\s+)?(string)(\s+)?$/ig.test(tsParam)) {
69
+ // 验证string
70
+ return getPrototypeType(data) == 'String'
71
+ }
72
+
73
+ if (/^(\s+)?(number)(\s+)?$/ig.test(tsParam)) {
74
+ // 验证number
75
+ return /^\d+(\.\d+)?$/.test(data)
76
+ }
77
+
78
+ if (/^(\s+)?(boolean)(\s+)?$/ig.test(tsParam)) {
79
+ // 验证boolean
80
+ return getPrototypeType(data) == 'Boolean'
81
+ }
82
+
83
+ return false
84
+ }
85
+
86
+ /**
87
+ * 验证数据是否符合基础Ts对象属性
88
+ * @param {*} data 数据
89
+ * @param {*} tsObjStr Ts对象定义
90
+ * @param {*} tsInclude : or ?:
91
+ * @returns {boolean}
92
+ */
93
+ export const solCheckDataFitOfTsObject = (data, tsObjStr, tsInclude) => {
94
+ if (tsInclude.includes('?') && ['', undefined, null].includes(data)) {
95
+ // tempIncludes = ?:, data的该key值为空
96
+ return true
97
+ }
98
+
99
+ if (getPrototypeType(data) != 'Object') {
100
+ return false
101
+ }
102
+
103
+ const tempCheckStr = tsObjStr.replace(/(\s+)?\}(\s+)?$/, '').replace(/^(\s+)?\{(\s+)?/, '') // 截取一头一尾的{}
104
+
105
+ const tempTsObj = solGetTsObj(solGetStrArr(tempCheckStr))
106
+
107
+ return Object.keys(tempTsObj).every(key => {
108
+ let [tempIncludes, tempCheckTs] = tempTsObj[key]
109
+
110
+ tempCheckTs = tempCheckTs.replace(/,$/, '')
111
+
112
+ if (/^(\s+)?\{[\s\S]+\}(\s+)?$/.test(tempCheckTs)) {
113
+ return solCheckDataFitOfTsObject(data[key], tempCheckTs, tempIncludes)
114
+ }
115
+
116
+ if (/\[\]$/.test(tempCheckTs)) {
117
+ return solCheckDataFitOfTsArray(data[key], tempCheckTs, tempIncludes)
118
+ }
119
+
120
+ return solCheckDataFitOfTsParam(data[key], tempCheckTs, tempIncludes)
121
+ })
122
+ }
123
+
124
+ /**
125
+ * 验证数据是否符合基础Ts数组属性
126
+ * @param {*} data 数据
127
+ * @param {*} tsArrStr Ts数组定义
128
+ * @param {*} tsInclude : or ?:
129
+ * @returns {boolean}
130
+ */
131
+ export const solCheckDataFitOfTsArray = (data, tsArrStr, tsInclude) => {
132
+ if (tsInclude.includes('?') && ['', undefined, null].includes(data)) {
133
+ // tempIncludes = ?:, data的该key值为空
134
+ return true
135
+ }
136
+
137
+ if (getPrototypeType(data) != 'Array') {
138
+ return false
139
+ }
140
+
141
+ const tempCheckStr = tsArrStr.replace(/\[\]$/, '') // 截取最后的[]
142
+
143
+ if (/^(\s+)?\{[\s\S]+\}(\s+)?$/.test(tempCheckStr)) {
144
+ return data.every(it => (
145
+ solCheckDataFitOfTsObject(it, tempCheckStr, ':')
146
+ ))
147
+ }
148
+
149
+ if (/\[\]$/.test(tempCheckStr)) {
150
+ return data.every(it => (
151
+ solCheckDataFitOfTsArray(it, tempCheckStr, ':')
152
+ ))
153
+ }
154
+
155
+ return data.every(it => (
156
+ solCheckDataFitOfTsParam(it, tempCheckStr, ':')
157
+ ))
158
+ }
159
+
160
+ /**
161
+ * 验证数据是否符合基础Ts
162
+ * @param {*} data 数据
163
+ * @param {*} tsObj ts对象
164
+ * @returns {boolean}
165
+ */
166
+ export const solCheckDataFitOfTsObj = (data, tsObj) => {
167
+ return Object.keys(tsObj).every(key => {
168
+ const [tempIncludes, tempCheckTs] = tsObj[key]
169
+
170
+ if (/^(\s+)?\{[\s\S]+\}(\s+)?$/.test(tempCheckTs)) {
171
+ return solCheckDataFitOfTsObject(data[key], tempCheckTs, tempIncludes)
172
+ }
173
+
174
+ if (/\[\]$/.test(tempCheckTs)) {
175
+ return solCheckDataFitOfTsArray(data[key], tempCheckTs, tempIncludes)
176
+ }
177
+
178
+ return solCheckDataFitOfTsParam(data[key], tempCheckTs, tempIncludes)
179
+ })
180
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * 中文解码通常涉及处理 URL 编码(也叫百分号编码)的字符串
3
+ * @param {string} str
4
+ * @returns {string} str
5
+ */
6
+ export function getUrlStringDecode(str, replaceEncode = false) {
7
+ if (typeof str !== 'string') {
8
+ return str
9
+ }
10
+
11
+ for (let i = 0; i < 5; i++) {
12
+ // 无编码序列时提前结束, 避免无谓解码
13
+ if (!str.includes('%')) {
14
+ break
15
+ }
16
+
17
+ try {
18
+ str = decodeURIComponent(str)
19
+ } catch (err) {
20
+ // 含非法编码序列(如裸"100%")时保留原样, 避免URIError打崩调用接口
21
+ break
22
+ }
23
+ }
24
+
25
+ if (replaceEncode && str.includes('#')) { // 仅处理 # 防止被截断,保留 / 等路径结构。
26
+ str = str.replace(/#/g, '%23')
27
+ }
28
+ if (replaceEncode && str.includes('\'')) { // 仅处理 ' 转码。
29
+ str = str.replace(/'/g, '%27')
30
+ }
31
+
32
+ return str
33
+ }
34
+
35
+ /** 路径段禁止的字符: 控制字符、路径分隔符 */
36
+ const PATH_SEGMENT_FORBIDDEN = /[\u0000-\u001f\u007f/\\]/
37
+
38
+ /**
39
+ * 校验并返回解码后的单个路径段(用于WebDAV等需要原始名称的场景)
40
+ * @throws {Error} 含分隔符、.. 或空段时抛出
41
+ */
42
+ export function getValidPathSegment(value = '') {
43
+ const tempDecoded = getUrlStringDecode(`${value}`)
44
+
45
+ if (!tempDecoded || tempDecoded === '.' || tempDecoded.includes('..') || PATH_SEGMENT_FORBIDDEN.test(tempDecoded)) {
46
+ throw new Error('非法路径参数')
47
+ }
48
+
49
+ return tempDecoded
50
+ }
51
+
52
+ /** 拼接校验后的解码路径段 */
53
+ export function getValidPath(...segments) {
54
+ return segments.map(it => getValidPathSegment(it)).join('/')
55
+ }
56
+
57
+ /** 拼接校验后的URL编码路径段(编码后可直接放进fetch URL) */
58
+ export function getSafePath(...segments) {
59
+ return segments.map(it => encodeURIComponent(getValidPathSegment(it)).replace(/'/g, '%27')).join('/')
60
+ }
@@ -0,0 +1,11 @@
1
+ export * from './Common.js'
2
+ export * from './FetchHandler.js'
3
+ export * from './IpHost.js'
4
+ export * from './TsStringHandler.js'
5
+ export { default as funcHandler } from './FuncHandler.js'
6
+ export { default as FetchHandler } from './FetchHandler.js'
7
+ export { default as LogColor } from './LogColor.js'
8
+ export * from './GetRandomValue.js'
9
+ export * from './GetWordFirstLetters.js'
10
+ export * from './StringHandler.js'
11
+ export * from './UrlStringDecode.js'