@meng-xi/vite-plugin 0.1.2 → 0.1.4
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-en.md +86 -510
- package/README.md +83 -506
- package/dist/common/compress/index.cjs +1 -0
- package/dist/common/compress/index.d.cts +23 -0
- package/dist/common/compress/index.d.mts +23 -0
- package/dist/common/compress/index.d.ts +23 -0
- package/dist/common/compress/index.mjs +1 -0
- package/dist/common/format/index.cjs +1 -1
- package/dist/common/format/index.d.cts +33 -1
- package/dist/common/format/index.d.mts +33 -1
- package/dist/common/format/index.d.ts +33 -1
- package/dist/common/format/index.mjs +1 -1
- package/dist/common/fs/index.cjs +1 -1
- package/dist/common/fs/index.d.cts +70 -2
- package/dist/common/fs/index.d.mts +70 -2
- package/dist/common/fs/index.d.ts +70 -2
- package/dist/common/fs/index.mjs +1 -1
- package/dist/common/html/index.cjs +2 -2
- package/dist/common/html/index.d.cts +268 -19
- package/dist/common/html/index.d.mts +268 -19
- package/dist/common/html/index.d.ts +268 -19
- package/dist/common/html/index.mjs +2 -2
- package/dist/common/index.cjs +1 -1
- package/dist/common/index.d.cts +8 -3
- package/dist/common/index.d.mts +8 -3
- package/dist/common/index.d.ts +8 -3
- package/dist/common/index.mjs +1 -1
- package/dist/common/path/index.cjs +1 -0
- package/dist/common/path/index.d.cts +22 -0
- package/dist/common/path/index.d.mts +22 -0
- package/dist/common/path/index.d.ts +22 -0
- package/dist/common/path/index.mjs +1 -0
- package/dist/common/ui/index.cjs +1 -0
- package/dist/common/ui/index.d.cts +132 -0
- package/dist/common/ui/index.d.mts +132 -0
- package/dist/common/ui/index.d.ts +132 -0
- package/dist/common/ui/index.mjs +1 -0
- package/dist/common/validation/index.cjs +1 -1
- package/dist/common/validation/index.d.cts +1 -0
- package/dist/common/validation/index.d.mts +1 -0
- package/dist/common/validation/index.d.ts +1 -0
- package/dist/common/validation/index.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +11 -4
- package/dist/index.d.mts +11 -4
- package/dist/index.d.ts +11 -4
- package/dist/index.mjs +1 -1
- package/dist/plugins/buildProgress/index.cjs +2 -2
- package/dist/plugins/buildProgress/index.mjs +2 -2
- package/dist/plugins/bundleAnalyzer/index.cjs +235 -0
- package/dist/plugins/bundleAnalyzer/index.d.cts +215 -0
- package/dist/plugins/bundleAnalyzer/index.d.mts +215 -0
- package/dist/plugins/bundleAnalyzer/index.d.ts +215 -0
- package/dist/plugins/bundleAnalyzer/index.mjs +235 -0
- package/dist/plugins/compressAssets/index.cjs +1 -1
- package/dist/plugins/compressAssets/index.mjs +1 -1
- package/dist/plugins/copyFile/index.d.cts +20 -2
- package/dist/plugins/copyFile/index.d.mts +20 -2
- package/dist/plugins/copyFile/index.d.ts +20 -2
- package/dist/plugins/envGuard/index.cjs +67 -0
- package/dist/plugins/envGuard/index.d.cts +156 -0
- package/dist/plugins/envGuard/index.d.mts +156 -0
- package/dist/plugins/envGuard/index.d.ts +156 -0
- package/dist/plugins/envGuard/index.mjs +67 -0
- package/dist/plugins/faviconManager/index.cjs +1 -1
- package/dist/plugins/faviconManager/index.d.cts +43 -5
- package/dist/plugins/faviconManager/index.d.mts +43 -5
- package/dist/plugins/faviconManager/index.d.ts +43 -5
- package/dist/plugins/faviconManager/index.mjs +1 -1
- package/dist/plugins/generateRouter/index.cjs +4 -4
- package/dist/plugins/generateRouter/index.d.cts +61 -14
- package/dist/plugins/generateRouter/index.d.mts +61 -14
- package/dist/plugins/generateRouter/index.d.ts +61 -14
- package/dist/plugins/generateRouter/index.mjs +1 -1
- package/dist/plugins/generateVersion/index.cjs +1 -1
- package/dist/plugins/generateVersion/index.d.cts +12 -0
- package/dist/plugins/generateVersion/index.d.mts +12 -0
- package/dist/plugins/generateVersion/index.d.ts +12 -0
- package/dist/plugins/generateVersion/index.mjs +1 -1
- package/dist/plugins/htmlInject/index.cjs +1 -7
- package/dist/plugins/htmlInject/index.d.cts +49 -194
- package/dist/plugins/htmlInject/index.d.mts +49 -194
- package/dist/plugins/htmlInject/index.d.ts +49 -194
- package/dist/plugins/htmlInject/index.mjs +1 -7
- package/dist/plugins/index.cjs +1 -1
- package/dist/plugins/index.d.cts +5 -1
- package/dist/plugins/index.d.mts +5 -1
- package/dist/plugins/index.d.ts +5 -1
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/loadingManager/index.cjs +2 -2
- package/dist/plugins/loadingManager/index.mjs +1 -1
- package/dist/plugins/versionUpdateChecker/index.cjs +2 -2
- package/dist/plugins/versionUpdateChecker/index.mjs +3 -3
- package/dist/shared/vite-plugin.BCuhU1au.mjs +7 -0
- package/dist/shared/vite-plugin.BrI73DHA.cjs +7 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.cts +261 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.mts +261 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.ts +261 -0
- package/dist/shared/vite-plugin.DnFDPjNf.mjs +1 -0
- package/dist/shared/vite-plugin.Dumot0up.mjs +1 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.cts +143 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.mts +143 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.ts +143 -0
- package/dist/shared/vite-plugin.soT9a-KD.cjs +1 -0
- package/dist/shared/vite-plugin.vwox4bU0.cjs +1 -0
- package/package.json +26 -1
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 环境变量验证模块
|
|
3
|
+
*
|
|
4
|
+
* @module common/validation/env
|
|
5
|
+
* @description 提供环境变量的类型校验、范围验证、长度验证和批量验证等功能,
|
|
6
|
+
* 支持 string、number、url、boolean、enum、json、semver、path 等多种类型,
|
|
7
|
+
* 为 envGuard 等插件提供统一的环境变量校验能力。
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* 环境变量值类型
|
|
11
|
+
*
|
|
12
|
+
* @description 定义环境变量支持的所有值类型:
|
|
13
|
+
* - `string`:普通字符串(默认类型)
|
|
14
|
+
* - `number`:数字类型
|
|
15
|
+
* - `url`:URL 地址
|
|
16
|
+
* - `boolean`:布尔值
|
|
17
|
+
* - `enum`:枚举值
|
|
18
|
+
* - `json`:JSON 字符串
|
|
19
|
+
* - `semver`:语义化版本号
|
|
20
|
+
* - `path`:文件路径
|
|
21
|
+
*/
|
|
22
|
+
type EnvType = 'string' | 'number' | 'url' | 'boolean' | 'enum' | 'json' | 'semver' | 'path';
|
|
23
|
+
/**
|
|
24
|
+
* 环境变量字段校验规则
|
|
25
|
+
*
|
|
26
|
+
* @description 定义单个环境变量的校验规则,包括类型约束、范围限制、
|
|
27
|
+
* 自定义验证函数和元数据信息等。
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // 基本字符串字段
|
|
32
|
+
* const rule: EnvFieldRule = { type: 'string', required: true }
|
|
33
|
+
*
|
|
34
|
+
* // 带范围的数字字段
|
|
35
|
+
* const portRule: EnvFieldRule = { type: 'number', required: true, minValue: 1, maxValue: 65535 }
|
|
36
|
+
*
|
|
37
|
+
* // 枚举字段
|
|
38
|
+
* const envRule: EnvFieldRule = { type: 'enum', required: true, enumValues: ['development', 'staging', 'production'] }
|
|
39
|
+
*
|
|
40
|
+
* // 自定义验证
|
|
41
|
+
* const customRule: EnvFieldRule = {
|
|
42
|
+
* type: 'string',
|
|
43
|
+
* required: true,
|
|
44
|
+
* validator: (v) => v.startsWith('https://') || '必须使用 HTTPS'
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
interface EnvFieldRule {
|
|
49
|
+
/** 值类型,默认为 `'string'` */
|
|
50
|
+
type?: EnvType;
|
|
51
|
+
/** 是否为必需字段,默认为 `true` */
|
|
52
|
+
required?: boolean;
|
|
53
|
+
/** 正则表达式,值必须匹配此模式 */
|
|
54
|
+
pattern?: RegExp;
|
|
55
|
+
/**
|
|
56
|
+
* 自定义验证函数
|
|
57
|
+
*
|
|
58
|
+
* @description 返回 `true` 表示验证通过,返回字符串表示验证失败且字符串为错误消息
|
|
59
|
+
*/
|
|
60
|
+
validator?: (value: string) => boolean | string;
|
|
61
|
+
/** 自定义错误消息,覆盖默认的错误提示 */
|
|
62
|
+
message?: string;
|
|
63
|
+
/** 当值为空时使用的默认值 */
|
|
64
|
+
default?: string;
|
|
65
|
+
/** 枚举值列表(仅 `enum` 类型需要) */
|
|
66
|
+
enumValues?: string[];
|
|
67
|
+
/** 数值最小值(仅 `number` 类型) */
|
|
68
|
+
minValue?: number;
|
|
69
|
+
/** 数值最大值(仅 `number` 类型) */
|
|
70
|
+
maxValue?: number;
|
|
71
|
+
/** 字符串最小长度(仅字符串类类型) */
|
|
72
|
+
minLength?: number;
|
|
73
|
+
/** 字符串最大长度(仅字符串类类型) */
|
|
74
|
+
maxLength?: number;
|
|
75
|
+
/** 变量分组名称,用于模板生成时的分组显示 */
|
|
76
|
+
group?: string;
|
|
77
|
+
/** 变量描述信息,用于模板生成时的说明文本 */
|
|
78
|
+
description?: string;
|
|
79
|
+
/** 是否为敏感变量,模板中会隐藏实际值 */
|
|
80
|
+
sensitive?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 环境变量验证结果
|
|
84
|
+
*
|
|
85
|
+
* @description 表示单个环境变量的验证结果,包含验证状态、错误消息和有效值。
|
|
86
|
+
*/
|
|
87
|
+
interface EnvValidationResult {
|
|
88
|
+
/** 环境变量名 */
|
|
89
|
+
key: string;
|
|
90
|
+
/**
|
|
91
|
+
* 验证状态:
|
|
92
|
+
* - `pass`:验证通过
|
|
93
|
+
* - `missing`:必需变量缺失
|
|
94
|
+
* - `type_error`:类型不匹配
|
|
95
|
+
* - `custom_error`:自定义验证失败或正则不匹配
|
|
96
|
+
* - `enum_mismatch`:枚举值不匹配
|
|
97
|
+
* - `range_error`:数值超出范围
|
|
98
|
+
* - `length_error`:字符串长度不符合要求
|
|
99
|
+
*/
|
|
100
|
+
status: 'pass' | 'missing' | 'type_error' | 'custom_error' | 'enum_mismatch' | 'range_error' | 'length_error';
|
|
101
|
+
/** 验证消息(验证通过时为空字符串,失败时为错误描述) */
|
|
102
|
+
message: string;
|
|
103
|
+
/** 环境变量的有效值(缺失时可能为 `undefined` 或默认值) */
|
|
104
|
+
value: string | undefined;
|
|
105
|
+
/** 应用的校验规则(含默认值填充后的完整规则) */
|
|
106
|
+
rule: EnvFieldRule;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 字符串类类型集合
|
|
110
|
+
*
|
|
111
|
+
* @description 包含所有以字符串形式存储的值类型(以及未指定类型的默认情况),
|
|
112
|
+
* 用于在长度验证时判断是否需要检查 `minLength`/`maxLength`。
|
|
113
|
+
* 非字符串类类型(如 `number`、`boolean`、`json`)不进行长度验证。
|
|
114
|
+
*/
|
|
115
|
+
declare const STRING_LIKE_TYPES: ReadonlySet<EnvType | undefined>;
|
|
116
|
+
/**
|
|
117
|
+
* 验证环境变量值的类型
|
|
118
|
+
*
|
|
119
|
+
* @param value - 环境变量值的字符串表示
|
|
120
|
+
* @param rule - 校验规则(需包含 `type` 字段)
|
|
121
|
+
* @returns 验证结果对象,包含是否合法、状态码和错误消息
|
|
122
|
+
*
|
|
123
|
+
* @description 根据规则中指定的类型对值进行类型校验:
|
|
124
|
+
* - `string`:始终通过(默认类型)
|
|
125
|
+
* - `number`:必须为合法数字
|
|
126
|
+
* - `url`:必须能被 `URL` 构造函数解析
|
|
127
|
+
* - `boolean`:必须为 `true`/`false`/`1`/`0`/`yes`/`no`(不区分大小写)
|
|
128
|
+
* - `enum`:必须在 `enumValues` 列表中
|
|
129
|
+
* - `json`:必须能被 `JSON.parse` 解析
|
|
130
|
+
* - `semver`:必须匹配 `x.y.z` 格式(支持 prerelease 和 build metadata)
|
|
131
|
+
* - `path`:必须以 `./`、`/`、`\\` 或盘符开头
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* validateType('123', { type: 'number' }) // { valid: true, status: 'pass', message: '' }
|
|
136
|
+
* validateType('abc', { type: 'number' }) // { valid: false, status: 'type_error', message: '...' }
|
|
137
|
+
* validateType('d', { type: 'enum', enumValues: ['a', 'b', 'c'] })
|
|
138
|
+
* // { valid: false, status: 'enum_mismatch', message: '...' }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
declare function validateType(value: string, rule: EnvFieldRule): {
|
|
142
|
+
valid: boolean;
|
|
143
|
+
status: 'type_error' | 'enum_mismatch' | 'pass';
|
|
144
|
+
message: string;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* 验证数值范围
|
|
148
|
+
*
|
|
149
|
+
* @param value - 环境变量值的字符串表示
|
|
150
|
+
* @param rule - 校验规则(需包含 `minValue`/`maxValue` 字段)
|
|
151
|
+
* @returns 验证结果对象,包含是否合法、状态码和错误消息
|
|
152
|
+
*
|
|
153
|
+
* @description 仅对 `number` 类型进行范围验证,检查值是否在 `minValue` 和 `maxValue` 之间。
|
|
154
|
+
* 非 `number` 类型始终返回验证通过。
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* validateRange('5', { type: 'number', minValue: 1, maxValue: 10 })
|
|
159
|
+
* // { valid: true, status: 'pass', message: '' }
|
|
160
|
+
*
|
|
161
|
+
* validateRange('0', { type: 'number', minValue: 1 })
|
|
162
|
+
* // { valid: false, status: 'range_error', message: '数值 0 小于最小值 1' }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
declare function validateRange(value: string, rule: EnvFieldRule): {
|
|
166
|
+
valid: boolean;
|
|
167
|
+
status: 'range_error' | 'pass';
|
|
168
|
+
message: string;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* 验证字符串长度
|
|
172
|
+
*
|
|
173
|
+
* @param value - 环境变量值
|
|
174
|
+
* @param rule - 校验规则(需包含 `minLength`/`maxLength` 字段)
|
|
175
|
+
* @returns 验证结果对象,包含是否合法、状态码和错误消息
|
|
176
|
+
*
|
|
177
|
+
* @description 仅对字符串类类型(`string`、`url`、`path`、`enum`、`semver` 及未指定类型)
|
|
178
|
+
* 进行长度验证。非字符串类类型(如 `number`、`boolean`、`json`)始终返回验证通过。
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* validateLength('hello', { type: 'string', minLength: 1, maxLength: 10 })
|
|
183
|
+
* // { valid: true, status: 'pass', message: '' }
|
|
184
|
+
*
|
|
185
|
+
* validateLength('hi', { type: 'string', minLength: 5 })
|
|
186
|
+
* // { valid: false, status: 'length_error', message: '字符串长度 2 小于最小长度 5' }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
declare function validateLength(value: string, rule: EnvFieldRule): {
|
|
190
|
+
valid: boolean;
|
|
191
|
+
status: 'length_error' | 'pass';
|
|
192
|
+
message: string;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* 验证单个环境变量值
|
|
196
|
+
*
|
|
197
|
+
* @param key - 环境变量名
|
|
198
|
+
* @param value - 环境变量值(可能为 `undefined`)
|
|
199
|
+
* @param rule - 校验规则
|
|
200
|
+
* @returns 验证结果对象
|
|
201
|
+
*
|
|
202
|
+
* @description 按顺序执行以下验证步骤:
|
|
203
|
+
* 1. **缺失检查**:必需变量为空时返回 `missing` 状态,非必需变量为空时使用默认值
|
|
204
|
+
* 2. **类型验证**:调用 `validateType` 检查值的类型
|
|
205
|
+
* 3. **范围验证**:调用 `validateRange` 检查数值范围
|
|
206
|
+
* 4. **长度验证**:调用 `validateLength` 检查字符串长度
|
|
207
|
+
* 5. **正则验证**:检查值是否匹配 `pattern` 正则
|
|
208
|
+
* 6. **自定义验证**:执行 `validator` 函数
|
|
209
|
+
*
|
|
210
|
+
* 所有验证步骤中,自定义 `message` 会覆盖默认的错误消息。
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* // 必需变量缺失
|
|
215
|
+
* validateValue('API_KEY', undefined, { type: 'string', required: true })
|
|
216
|
+
* // { key: 'API_KEY', status: 'missing', message: '缺少必需的环境变量: API_KEY', ... }
|
|
217
|
+
*
|
|
218
|
+
* // 类型错误
|
|
219
|
+
* validateValue('PORT', 'abc', { type: 'number', required: true })
|
|
220
|
+
* // { key: 'PORT', status: 'type_error', message: '...', ... }
|
|
221
|
+
*
|
|
222
|
+
* // 使用默认值
|
|
223
|
+
* validateValue('DEBUG', undefined, { type: 'boolean', required: false, default: 'false' })
|
|
224
|
+
* // { key: 'DEBUG', status: 'pass', value: 'false', ... }
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
declare function validateValue(key: string, value: string | undefined, rule: EnvFieldRule): EnvValidationResult;
|
|
228
|
+
/**
|
|
229
|
+
* 批量验证环境变量
|
|
230
|
+
*
|
|
231
|
+
* @param env - 环境变量键值对对象
|
|
232
|
+
* @param rules - 环境变量校验规则映射
|
|
233
|
+
* @returns 所有变量的验证结果数组
|
|
234
|
+
*
|
|
235
|
+
* @description 遍历所有规则,对每个环境变量调用 `validateValue` 进行验证,
|
|
236
|
+
* 返回包含所有验证结果的数组。可用于汇总展示所有验证错误。
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* const results = validateEnvironment(
|
|
241
|
+
* { API_URL: 'https://api.example.com', PORT: 'abc', MISSING_VAR: undefined },
|
|
242
|
+
* {
|
|
243
|
+
* API_URL: { type: 'url', required: true },
|
|
244
|
+
* PORT: { type: 'number', required: true },
|
|
245
|
+
* MISSING_VAR: { type: 'string', required: true }
|
|
246
|
+
* }
|
|
247
|
+
* )
|
|
248
|
+
* // [
|
|
249
|
+
* // { key: 'API_URL', status: 'pass', ... },
|
|
250
|
+
* // { key: 'PORT', status: 'type_error', ... },
|
|
251
|
+
* // { key: 'MISSING_VAR', status: 'missing', ... }
|
|
252
|
+
* // ]
|
|
253
|
+
*
|
|
254
|
+
* // 检查是否有错误
|
|
255
|
+
* const hasErrors = results.some(r => r.status !== 'pass')
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
declare function validateEnvironment(env: Record<string, string | undefined>, rules: Record<string, EnvFieldRule>): EnvValidationResult[];
|
|
259
|
+
|
|
260
|
+
export { STRING_LIKE_TYPES as S, validateLength as c, validateRange as d, validateType as e, validateValue as f, validateEnvironment as v };
|
|
261
|
+
export type { EnvFieldRule as E, EnvType as a, EnvValidationResult as b };
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 环境变量验证模块
|
|
3
|
+
*
|
|
4
|
+
* @module common/validation/env
|
|
5
|
+
* @description 提供环境变量的类型校验、范围验证、长度验证和批量验证等功能,
|
|
6
|
+
* 支持 string、number、url、boolean、enum、json、semver、path 等多种类型,
|
|
7
|
+
* 为 envGuard 等插件提供统一的环境变量校验能力。
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* 环境变量值类型
|
|
11
|
+
*
|
|
12
|
+
* @description 定义环境变量支持的所有值类型:
|
|
13
|
+
* - `string`:普通字符串(默认类型)
|
|
14
|
+
* - `number`:数字类型
|
|
15
|
+
* - `url`:URL 地址
|
|
16
|
+
* - `boolean`:布尔值
|
|
17
|
+
* - `enum`:枚举值
|
|
18
|
+
* - `json`:JSON 字符串
|
|
19
|
+
* - `semver`:语义化版本号
|
|
20
|
+
* - `path`:文件路径
|
|
21
|
+
*/
|
|
22
|
+
type EnvType = 'string' | 'number' | 'url' | 'boolean' | 'enum' | 'json' | 'semver' | 'path';
|
|
23
|
+
/**
|
|
24
|
+
* 环境变量字段校验规则
|
|
25
|
+
*
|
|
26
|
+
* @description 定义单个环境变量的校验规则,包括类型约束、范围限制、
|
|
27
|
+
* 自定义验证函数和元数据信息等。
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // 基本字符串字段
|
|
32
|
+
* const rule: EnvFieldRule = { type: 'string', required: true }
|
|
33
|
+
*
|
|
34
|
+
* // 带范围的数字字段
|
|
35
|
+
* const portRule: EnvFieldRule = { type: 'number', required: true, minValue: 1, maxValue: 65535 }
|
|
36
|
+
*
|
|
37
|
+
* // 枚举字段
|
|
38
|
+
* const envRule: EnvFieldRule = { type: 'enum', required: true, enumValues: ['development', 'staging', 'production'] }
|
|
39
|
+
*
|
|
40
|
+
* // 自定义验证
|
|
41
|
+
* const customRule: EnvFieldRule = {
|
|
42
|
+
* type: 'string',
|
|
43
|
+
* required: true,
|
|
44
|
+
* validator: (v) => v.startsWith('https://') || '必须使用 HTTPS'
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
interface EnvFieldRule {
|
|
49
|
+
/** 值类型,默认为 `'string'` */
|
|
50
|
+
type?: EnvType;
|
|
51
|
+
/** 是否为必需字段,默认为 `true` */
|
|
52
|
+
required?: boolean;
|
|
53
|
+
/** 正则表达式,值必须匹配此模式 */
|
|
54
|
+
pattern?: RegExp;
|
|
55
|
+
/**
|
|
56
|
+
* 自定义验证函数
|
|
57
|
+
*
|
|
58
|
+
* @description 返回 `true` 表示验证通过,返回字符串表示验证失败且字符串为错误消息
|
|
59
|
+
*/
|
|
60
|
+
validator?: (value: string) => boolean | string;
|
|
61
|
+
/** 自定义错误消息,覆盖默认的错误提示 */
|
|
62
|
+
message?: string;
|
|
63
|
+
/** 当值为空时使用的默认值 */
|
|
64
|
+
default?: string;
|
|
65
|
+
/** 枚举值列表(仅 `enum` 类型需要) */
|
|
66
|
+
enumValues?: string[];
|
|
67
|
+
/** 数值最小值(仅 `number` 类型) */
|
|
68
|
+
minValue?: number;
|
|
69
|
+
/** 数值最大值(仅 `number` 类型) */
|
|
70
|
+
maxValue?: number;
|
|
71
|
+
/** 字符串最小长度(仅字符串类类型) */
|
|
72
|
+
minLength?: number;
|
|
73
|
+
/** 字符串最大长度(仅字符串类类型) */
|
|
74
|
+
maxLength?: number;
|
|
75
|
+
/** 变量分组名称,用于模板生成时的分组显示 */
|
|
76
|
+
group?: string;
|
|
77
|
+
/** 变量描述信息,用于模板生成时的说明文本 */
|
|
78
|
+
description?: string;
|
|
79
|
+
/** 是否为敏感变量,模板中会隐藏实际值 */
|
|
80
|
+
sensitive?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 环境变量验证结果
|
|
84
|
+
*
|
|
85
|
+
* @description 表示单个环境变量的验证结果,包含验证状态、错误消息和有效值。
|
|
86
|
+
*/
|
|
87
|
+
interface EnvValidationResult {
|
|
88
|
+
/** 环境变量名 */
|
|
89
|
+
key: string;
|
|
90
|
+
/**
|
|
91
|
+
* 验证状态:
|
|
92
|
+
* - `pass`:验证通过
|
|
93
|
+
* - `missing`:必需变量缺失
|
|
94
|
+
* - `type_error`:类型不匹配
|
|
95
|
+
* - `custom_error`:自定义验证失败或正则不匹配
|
|
96
|
+
* - `enum_mismatch`:枚举值不匹配
|
|
97
|
+
* - `range_error`:数值超出范围
|
|
98
|
+
* - `length_error`:字符串长度不符合要求
|
|
99
|
+
*/
|
|
100
|
+
status: 'pass' | 'missing' | 'type_error' | 'custom_error' | 'enum_mismatch' | 'range_error' | 'length_error';
|
|
101
|
+
/** 验证消息(验证通过时为空字符串,失败时为错误描述) */
|
|
102
|
+
message: string;
|
|
103
|
+
/** 环境变量的有效值(缺失时可能为 `undefined` 或默认值) */
|
|
104
|
+
value: string | undefined;
|
|
105
|
+
/** 应用的校验规则(含默认值填充后的完整规则) */
|
|
106
|
+
rule: EnvFieldRule;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 字符串类类型集合
|
|
110
|
+
*
|
|
111
|
+
* @description 包含所有以字符串形式存储的值类型(以及未指定类型的默认情况),
|
|
112
|
+
* 用于在长度验证时判断是否需要检查 `minLength`/`maxLength`。
|
|
113
|
+
* 非字符串类类型(如 `number`、`boolean`、`json`)不进行长度验证。
|
|
114
|
+
*/
|
|
115
|
+
declare const STRING_LIKE_TYPES: ReadonlySet<EnvType | undefined>;
|
|
116
|
+
/**
|
|
117
|
+
* 验证环境变量值的类型
|
|
118
|
+
*
|
|
119
|
+
* @param value - 环境变量值的字符串表示
|
|
120
|
+
* @param rule - 校验规则(需包含 `type` 字段)
|
|
121
|
+
* @returns 验证结果对象,包含是否合法、状态码和错误消息
|
|
122
|
+
*
|
|
123
|
+
* @description 根据规则中指定的类型对值进行类型校验:
|
|
124
|
+
* - `string`:始终通过(默认类型)
|
|
125
|
+
* - `number`:必须为合法数字
|
|
126
|
+
* - `url`:必须能被 `URL` 构造函数解析
|
|
127
|
+
* - `boolean`:必须为 `true`/`false`/`1`/`0`/`yes`/`no`(不区分大小写)
|
|
128
|
+
* - `enum`:必须在 `enumValues` 列表中
|
|
129
|
+
* - `json`:必须能被 `JSON.parse` 解析
|
|
130
|
+
* - `semver`:必须匹配 `x.y.z` 格式(支持 prerelease 和 build metadata)
|
|
131
|
+
* - `path`:必须以 `./`、`/`、`\\` 或盘符开头
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* validateType('123', { type: 'number' }) // { valid: true, status: 'pass', message: '' }
|
|
136
|
+
* validateType('abc', { type: 'number' }) // { valid: false, status: 'type_error', message: '...' }
|
|
137
|
+
* validateType('d', { type: 'enum', enumValues: ['a', 'b', 'c'] })
|
|
138
|
+
* // { valid: false, status: 'enum_mismatch', message: '...' }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
declare function validateType(value: string, rule: EnvFieldRule): {
|
|
142
|
+
valid: boolean;
|
|
143
|
+
status: 'type_error' | 'enum_mismatch' | 'pass';
|
|
144
|
+
message: string;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* 验证数值范围
|
|
148
|
+
*
|
|
149
|
+
* @param value - 环境变量值的字符串表示
|
|
150
|
+
* @param rule - 校验规则(需包含 `minValue`/`maxValue` 字段)
|
|
151
|
+
* @returns 验证结果对象,包含是否合法、状态码和错误消息
|
|
152
|
+
*
|
|
153
|
+
* @description 仅对 `number` 类型进行范围验证,检查值是否在 `minValue` 和 `maxValue` 之间。
|
|
154
|
+
* 非 `number` 类型始终返回验证通过。
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* validateRange('5', { type: 'number', minValue: 1, maxValue: 10 })
|
|
159
|
+
* // { valid: true, status: 'pass', message: '' }
|
|
160
|
+
*
|
|
161
|
+
* validateRange('0', { type: 'number', minValue: 1 })
|
|
162
|
+
* // { valid: false, status: 'range_error', message: '数值 0 小于最小值 1' }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
declare function validateRange(value: string, rule: EnvFieldRule): {
|
|
166
|
+
valid: boolean;
|
|
167
|
+
status: 'range_error' | 'pass';
|
|
168
|
+
message: string;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* 验证字符串长度
|
|
172
|
+
*
|
|
173
|
+
* @param value - 环境变量值
|
|
174
|
+
* @param rule - 校验规则(需包含 `minLength`/`maxLength` 字段)
|
|
175
|
+
* @returns 验证结果对象,包含是否合法、状态码和错误消息
|
|
176
|
+
*
|
|
177
|
+
* @description 仅对字符串类类型(`string`、`url`、`path`、`enum`、`semver` 及未指定类型)
|
|
178
|
+
* 进行长度验证。非字符串类类型(如 `number`、`boolean`、`json`)始终返回验证通过。
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* validateLength('hello', { type: 'string', minLength: 1, maxLength: 10 })
|
|
183
|
+
* // { valid: true, status: 'pass', message: '' }
|
|
184
|
+
*
|
|
185
|
+
* validateLength('hi', { type: 'string', minLength: 5 })
|
|
186
|
+
* // { valid: false, status: 'length_error', message: '字符串长度 2 小于最小长度 5' }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
declare function validateLength(value: string, rule: EnvFieldRule): {
|
|
190
|
+
valid: boolean;
|
|
191
|
+
status: 'length_error' | 'pass';
|
|
192
|
+
message: string;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* 验证单个环境变量值
|
|
196
|
+
*
|
|
197
|
+
* @param key - 环境变量名
|
|
198
|
+
* @param value - 环境变量值(可能为 `undefined`)
|
|
199
|
+
* @param rule - 校验规则
|
|
200
|
+
* @returns 验证结果对象
|
|
201
|
+
*
|
|
202
|
+
* @description 按顺序执行以下验证步骤:
|
|
203
|
+
* 1. **缺失检查**:必需变量为空时返回 `missing` 状态,非必需变量为空时使用默认值
|
|
204
|
+
* 2. **类型验证**:调用 `validateType` 检查值的类型
|
|
205
|
+
* 3. **范围验证**:调用 `validateRange` 检查数值范围
|
|
206
|
+
* 4. **长度验证**:调用 `validateLength` 检查字符串长度
|
|
207
|
+
* 5. **正则验证**:检查值是否匹配 `pattern` 正则
|
|
208
|
+
* 6. **自定义验证**:执行 `validator` 函数
|
|
209
|
+
*
|
|
210
|
+
* 所有验证步骤中,自定义 `message` 会覆盖默认的错误消息。
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* // 必需变量缺失
|
|
215
|
+
* validateValue('API_KEY', undefined, { type: 'string', required: true })
|
|
216
|
+
* // { key: 'API_KEY', status: 'missing', message: '缺少必需的环境变量: API_KEY', ... }
|
|
217
|
+
*
|
|
218
|
+
* // 类型错误
|
|
219
|
+
* validateValue('PORT', 'abc', { type: 'number', required: true })
|
|
220
|
+
* // { key: 'PORT', status: 'type_error', message: '...', ... }
|
|
221
|
+
*
|
|
222
|
+
* // 使用默认值
|
|
223
|
+
* validateValue('DEBUG', undefined, { type: 'boolean', required: false, default: 'false' })
|
|
224
|
+
* // { key: 'DEBUG', status: 'pass', value: 'false', ... }
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
declare function validateValue(key: string, value: string | undefined, rule: EnvFieldRule): EnvValidationResult;
|
|
228
|
+
/**
|
|
229
|
+
* 批量验证环境变量
|
|
230
|
+
*
|
|
231
|
+
* @param env - 环境变量键值对对象
|
|
232
|
+
* @param rules - 环境变量校验规则映射
|
|
233
|
+
* @returns 所有变量的验证结果数组
|
|
234
|
+
*
|
|
235
|
+
* @description 遍历所有规则,对每个环境变量调用 `validateValue` 进行验证,
|
|
236
|
+
* 返回包含所有验证结果的数组。可用于汇总展示所有验证错误。
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* const results = validateEnvironment(
|
|
241
|
+
* { API_URL: 'https://api.example.com', PORT: 'abc', MISSING_VAR: undefined },
|
|
242
|
+
* {
|
|
243
|
+
* API_URL: { type: 'url', required: true },
|
|
244
|
+
* PORT: { type: 'number', required: true },
|
|
245
|
+
* MISSING_VAR: { type: 'string', required: true }
|
|
246
|
+
* }
|
|
247
|
+
* )
|
|
248
|
+
* // [
|
|
249
|
+
* // { key: 'API_URL', status: 'pass', ... },
|
|
250
|
+
* // { key: 'PORT', status: 'type_error', ... },
|
|
251
|
+
* // { key: 'MISSING_VAR', status: 'missing', ... }
|
|
252
|
+
* // ]
|
|
253
|
+
*
|
|
254
|
+
* // 检查是否有错误
|
|
255
|
+
* const hasErrors = results.some(r => r.status !== 'pass')
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
declare function validateEnvironment(env: Record<string, string | undefined>, rules: Record<string, EnvFieldRule>): EnvValidationResult[];
|
|
259
|
+
|
|
260
|
+
export { STRING_LIKE_TYPES as S, validateLength as c, validateRange as d, validateType as e, validateValue as f, validateEnvironment as v };
|
|
261
|
+
export type { EnvFieldRule as E, EnvType as a, EnvValidationResult as b };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const i=new Set(["string","url","path","enum","semver",void 0]);function m(e,u){switch(u.type){case"number":{if(isNaN(Number(e))||e.trim()==="")return{valid:!1,status:"type_error",message:`\u73AF\u5883\u53D8\u91CF\u503C "${e}" \u4E0D\u662F\u5408\u6CD5\u6570\u5B57`};break}case"url":{try{new URL(e)}catch{return{valid:!1,status:"type_error",message:`\u73AF\u5883\u53D8\u91CF\u503C "${e}" \u4E0D\u662F\u5408\u6CD5 URL`}}break}case"boolean":{const s=e.toLowerCase();if(!["true","false","1","0","yes","no"].includes(s))return{valid:!1,status:"type_error",message:`\u73AF\u5883\u53D8\u91CF\u503C "${e}" \u4E0D\u662F\u5408\u6CD5\u5E03\u5C14\u503C (true/false/1/0/yes/no)`};break}case"enum":{if(!u.enumValues||u.enumValues.length===0)return{valid:!1,status:"enum_mismatch",message:"enum \u7C7B\u578B\u5FC5\u987B\u6307\u5B9A enumValues"};if(!u.enumValues.includes(e))return{valid:!1,status:"enum_mismatch",message:`\u73AF\u5883\u53D8\u91CF\u503C "${e}" \u4E0D\u5728\u5141\u8BB8\u7684\u679A\u4E3E\u503C\u4E2D: ${u.enumValues.join(", ")}`};break}case"json":{try{JSON.parse(e)}catch{return{valid:!1,status:"type_error",message:"\u73AF\u5883\u53D8\u91CF\u503C\u4E0D\u662F\u5408\u6CD5 JSON"}}break}case"semver":{if(!/^\d+\.\d+\.\d+(-[\w.]+)?(\+[\w.]+)?$/.test(e))return{valid:!1,status:"type_error",message:`\u73AF\u5883\u53D8\u91CF\u503C "${e}" \u4E0D\u662F\u5408\u6CD5\u8BED\u4E49\u5316\u7248\u672C\u53F7 (x.y.z)`};break}case"path":{if(!/^(?:[./\\]|(?:[a-zA-Z]:))/.test(e))return{valid:!1,status:"type_error",message:`\u73AF\u5883\u53D8\u91CF\u503C "${e}" \u4E0D\u662F\u5408\u6CD5\u6587\u4EF6\u8DEF\u5F84`};break}}return{valid:!0,status:"pass",message:""}}function g(e,u){if(u.type!=="number")return{valid:!0,status:"pass",message:""};const s=Number(e);return u.minValue!==void 0&&s<u.minValue?{valid:!1,status:"range_error",message:`\u6570\u503C ${s} \u5C0F\u4E8E\u6700\u5C0F\u503C ${u.minValue}`}:u.maxValue!==void 0&&s>u.maxValue?{valid:!1,status:"range_error",message:`\u6570\u503C ${s} \u5927\u4E8E\u6700\u5927\u503C ${u.maxValue}`}:{valid:!0,status:"pass",message:""}}function o(e,u){return i.has(u.type)?u.minLength!==void 0&&e.length<u.minLength?{valid:!1,status:"length_error",message:`\u5B57\u7B26\u4E32\u957F\u5EA6 ${e.length} \u5C0F\u4E8E\u6700\u5C0F\u957F\u5EA6 ${u.minLength}`}:u.maxLength!==void 0&&e.length>u.maxLength?{valid:!1,status:"length_error",message:`\u5B57\u7B26\u4E32\u957F\u5EA6 ${e.length} \u5927\u4E8E\u6700\u5927\u957F\u5EA6 ${u.maxLength}`}:{valid:!0,status:"pass",message:""}:{valid:!0,status:"pass",message:""}}function v(e,u,s){const a={type:"string",required:!0,...s};if(u===void 0||u==="")return a.required!==!1?{key:e,status:"missing",message:a.message||`\u7F3A\u5C11\u5FC5\u9700\u7684\u73AF\u5883\u53D8\u91CF: ${e}`,value:u,rule:a}:{key:e,status:"pass",message:"",value:a.default??u,rule:a};const t=m(u,a);if(!t.valid)return{key:e,status:t.status,message:a.message||t.message,value:u,rule:a};const r=g(u,a);if(!r.valid)return{key:e,status:r.status,message:a.message||r.message,value:u,rule:a};const n=o(u,a);if(!n.valid)return{key:e,status:n.status,message:a.message||n.message,value:u,rule:a};if(a.pattern&&!a.pattern.test(u))return{key:e,status:"custom_error",message:a.message||`\u73AF\u5883\u53D8\u91CF ${e} \u4E0D\u5339\u914D\u6B63\u5219: ${a.pattern.source}`,value:u,rule:a};if(a.validator){const l=a.validator(u);if(l!==!0){const d=typeof l=="string"?l:"";return{key:e,status:"custom_error",message:a.message||d||`\u73AF\u5883\u53D8\u91CF ${e} \u81EA\u5B9A\u4E49\u6821\u9A8C\u5931\u8D25`,value:u,rule:a}}}return{key:e,status:"pass",message:"",value:u,rule:a}}function C(e,u){const s=[];for(const[a,t]of Object.entries(u)){const r=e[a];s.push(v(a,r,t))}return s}export{i as S,o as a,g as b,m as c,v as d,C as v};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{containsScriptTag as e,validateIdentifierName as a}from"../common/script/index.mjs";function n(u,t){if(u)try{a(u)}catch(o){throw new Error(`${t} ${o.message}`)}}function f(u,t){if(u&&e(u))throw new Error(`${t} \u4E0D\u5141\u8BB8\u5305\u542B <script> \u6807\u7B7E`)}function c(u,t,o){for(const r of t){const i=u[r];if(i!==void 0&&typeof i!="string")throw new Error(`${o}.${r} \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`);if(i&&e(i))throw new Error(`${o}.${r} \u4E0D\u5141\u8BB8\u5305\u542B <script> \u6807\u7B7E`)}}function d(u,t){if(u!==void 0&&(typeof u!="number"||u<0))throw new Error(`${t} \u5FC5\u987B\u662F\u975E\u8D1F\u6570`)}function s(u,t){if(u?.duration!==void 0&&(typeof u.duration!="number"||u.duration<0))throw new Error(t)}function l(u,t,o){if(u&&!t.includes(u))throw new Error(`${o} \u5FC5\u987B\u662F ${t.join(", ")}`)}export{l as a,n as b,s as c,f as d,d as e,c as v};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML 注入与安全相关类型定义
|
|
3
|
+
*
|
|
4
|
+
* @module common/html/type
|
|
5
|
+
* @description 定义 HTML 注入位置、选择器匹配模式、条件判断、安全配置等核心类型,
|
|
6
|
+
* 为 HTML 注入和安全过滤功能提供统一的类型约束。
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* HTML 单点注入结果
|
|
10
|
+
*
|
|
11
|
+
* @description 表示在 HTML 中进行单点注入操作后的结果,
|
|
12
|
+
* 包含注入后的 HTML 内容和是否成功注入的标志。
|
|
13
|
+
*/
|
|
14
|
+
interface HtmlInjectResult {
|
|
15
|
+
/** 注入后的 HTML 内容 */
|
|
16
|
+
html: string;
|
|
17
|
+
/** 是否成功注入 */
|
|
18
|
+
injected: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* HTML 双区域注入结果
|
|
22
|
+
*
|
|
23
|
+
* @description 表示在 HTML 的 head 和 body 两个区域同时注入后的结果,
|
|
24
|
+
* 包含各区域的注入状态和是否使用了回退策略。
|
|
25
|
+
*/
|
|
26
|
+
interface DualInjectResult {
|
|
27
|
+
/** 注入后的 HTML 内容 */
|
|
28
|
+
html: string;
|
|
29
|
+
/** 是否成功注入到 head 区域 */
|
|
30
|
+
headInjected: boolean;
|
|
31
|
+
/** 是否成功注入到 body 区域 */
|
|
32
|
+
bodyInjected: boolean;
|
|
33
|
+
/** body 注入是否使用了回退策略(追加到末尾) */
|
|
34
|
+
usedFallback: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* HTML 注入位置类型
|
|
38
|
+
*
|
|
39
|
+
* @description 定义内容在 HTML 中可注入的所有位置:
|
|
40
|
+
* - `head-start`:`<head>` 标签开始后
|
|
41
|
+
* - `head-end`:`</head>` 标签前
|
|
42
|
+
* - `body-start`:`<body>` 标签开始后
|
|
43
|
+
* - `body-end`:`</body>` 标签前
|
|
44
|
+
* - `before-selector`:选择器匹配位置前
|
|
45
|
+
* - `after-selector`:选择器匹配位置后
|
|
46
|
+
* - `replace-selector`:替换选择器匹配的内容
|
|
47
|
+
*/
|
|
48
|
+
type InjectPosition = 'head-start' | 'head-end' | 'body-start' | 'body-end' | 'before-selector' | 'after-selector' | 'replace-selector';
|
|
49
|
+
/**
|
|
50
|
+
* 选择器匹配模式
|
|
51
|
+
*
|
|
52
|
+
* @description 定义 HTML 选择器的匹配方式:
|
|
53
|
+
* - `string`:使用字符串精确匹配(默认)
|
|
54
|
+
* - `regex`:使用正则表达式匹配
|
|
55
|
+
*/
|
|
56
|
+
type SelectorMatch = 'string' | 'regex';
|
|
57
|
+
/**
|
|
58
|
+
* 条件判断类型
|
|
59
|
+
*
|
|
60
|
+
* @description 定义注入条件的类型:
|
|
61
|
+
* - `env`:基于环境变量是否存在且为真值
|
|
62
|
+
* - `file-contains`:基于 HTML 内容是否包含指定字符串
|
|
63
|
+
* - `custom`:基于自定义函数的返回值
|
|
64
|
+
*/
|
|
65
|
+
type ConditionType = 'env' | 'file-contains' | 'custom';
|
|
66
|
+
/**
|
|
67
|
+
* 注入条件配置
|
|
68
|
+
*
|
|
69
|
+
* @description 定义 HTML 内容注入的条件判断规则,
|
|
70
|
+
* 支持环境变量检测、内容包含检测和自定义函数判断,
|
|
71
|
+
* 可通过 `negate` 字段取反结果。
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // 当环境变量 ENABLE_ANALYTICS 存在时注入
|
|
76
|
+
* { type: 'env', value: 'ENABLE_ANALYTICS' }
|
|
77
|
+
*
|
|
78
|
+
* // 当 HTML 不包含特定元素时注入(取反)
|
|
79
|
+
* { type: 'file-contains', value: '<div id="app">', negate: true }
|
|
80
|
+
*
|
|
81
|
+
* // 使用自定义函数判断
|
|
82
|
+
* { type: 'custom', value: () => process.env.NODE_ENV === 'production' }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
interface InjectCondition {
|
|
86
|
+
/** 条件类型 */
|
|
87
|
+
type: ConditionType;
|
|
88
|
+
/**
|
|
89
|
+
* 条件值:
|
|
90
|
+
* - `env` 类型:环境变量名
|
|
91
|
+
* - `file-contains` 类型:要搜索的字符串
|
|
92
|
+
* - `custom` 类型:返回布尔值的自定义函数
|
|
93
|
+
*/
|
|
94
|
+
value: string | ((...args: any[]) => boolean);
|
|
95
|
+
/** 是否取反条件结果,默认为 `false` */
|
|
96
|
+
negate?: boolean;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 按位置注入结果
|
|
100
|
+
*
|
|
101
|
+
* @description 表示按指定位置注入内容后的详细结果,
|
|
102
|
+
* 包含注入后的 HTML、是否成功标志和失败原因。
|
|
103
|
+
*/
|
|
104
|
+
interface PositionInjectResult {
|
|
105
|
+
/** 注入后的 HTML 内容 */
|
|
106
|
+
html: string;
|
|
107
|
+
/** 是否成功注入 */
|
|
108
|
+
injected: boolean;
|
|
109
|
+
/** 注入失败时的原因说明(仅在 `injected` 为 `false` 时存在) */
|
|
110
|
+
reason?: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* HTML 安全配置
|
|
114
|
+
*
|
|
115
|
+
* @description 控制 HTML 内容注入时的安全过滤策略,
|
|
116
|
+
* 可配置是否阻止危险标签和属性,以及自定义允许/阻止列表。
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* // 完全禁用安全过滤(不推荐)
|
|
121
|
+
* const config: SecurityConfig = { blockDangerousTags: false, blockDangerousAttributes: false }
|
|
122
|
+
*
|
|
123
|
+
* // 允许特定标签
|
|
124
|
+
* const config: SecurityConfig = { allowedTags: ['iframe'] }
|
|
125
|
+
*
|
|
126
|
+
* // 自定义阻止属性列表
|
|
127
|
+
* const config: SecurityConfig = { blockedAttributes: ['onclick', 'onerror'] }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
interface SecurityConfig {
|
|
131
|
+
/** 是否阻止危险 HTML 标签(如 script、iframe),默认为 `true` */
|
|
132
|
+
blockDangerousTags?: boolean;
|
|
133
|
+
/** 是否阻止危险 HTML 属性(如 onclick、onerror),默认为 `true` */
|
|
134
|
+
blockDangerousAttributes?: boolean;
|
|
135
|
+
/** 允许的标签列表,设置后将从默认阻止列表中排除这些标签 */
|
|
136
|
+
allowedTags?: string[];
|
|
137
|
+
/** 自定义阻止的标签列表,覆盖默认阻止列表 */
|
|
138
|
+
blockedTags?: string[];
|
|
139
|
+
/** 自定义阻止的属性列表,覆盖默认阻止属性列表 */
|
|
140
|
+
blockedAttributes?: string[];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type { ConditionType as C, DualInjectResult as D, HtmlInjectResult as H, InjectCondition as I, PositionInjectResult as P, SecurityConfig as S, InjectPosition as a, SelectorMatch as b };
|