@gaias/client_node 1.0.6 → 1.0.7

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 (2) hide show
  1. package/CONFIG.md +608 -0
  2. package/package.json +1 -1
package/CONFIG.md ADDED
@@ -0,0 +1,608 @@
1
+ # 项目配置文件详解
2
+
3
+ 本文档详细说明 `@gaias/client_node` 项目中所有配置文件的用途和配置项。
4
+
5
+ ## 📦 目录
6
+
7
+ - [package.json - 项目元数据与依赖管理](#packagejson)
8
+ - [tsconfig.json - TypeScript 编译配置](#tsconfigjson)
9
+ - [eslint.config.js - 代码质量检查配置](#eslintconfigjs)
10
+ - [jest.config.js - 测试框架配置](#jestconfigjs)
11
+ - [.prettierrc.json - 代码格式化配置](#prettierrrcjson)
12
+ - [.ctirc - 索引文件自动生成配置](#ctirc)
13
+
14
+ ---
15
+
16
+ ## package.json
17
+
18
+ ### 基本信息
19
+
20
+ ```json
21
+ {
22
+ "name": "@gaias/client_node",
23
+ "version": "1.0.6",
24
+ "description": "store data in cookie and localstorage",
25
+ "main": "dist/single/index.js",
26
+ "types": "dist/single/index.d.ts",
27
+ "license": "UNLICENSED"
28
+ }
29
+ ```
30
+
31
+ | 字段 | 说明 |
32
+ |------|------|
33
+ | `name` | NPM 包名,使用 `@gaias` 命名空间 |
34
+ | `version` | 当前版本号,遵循语义化版本 (Semver) |
35
+ | `description` | 包描述 |
36
+ | `main` | CommonJS 入口文件 |
37
+ | `types` | TypeScript 类型定义文件入口 |
38
+ | `license` | 许可证类型 |
39
+
40
+ ### 发布配置
41
+
42
+ ```json
43
+ {
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "registry": "https://registry.npmjs.org/"
47
+ }
48
+ }
49
+ ```
50
+
51
+ - `access`: `public` - 公开发布包
52
+ - `registry`: NPM 官方仓库地址
53
+
54
+ ### 关键词
55
+
56
+ ```json
57
+ {
58
+ "keywords": [
59
+ "restclient",
60
+ "globalstorage",
61
+ "messageclient",
62
+ "configclient"
63
+ ]
64
+ }
65
+ ```
66
+
67
+ 用于 NPM 搜索的关键词,提高包的可发现性。
68
+
69
+ ### 引擎要求
70
+
71
+ ```json
72
+ {
73
+ "engines": {
74
+ "node": ">=18.0.0",
75
+ "yarn": ">=1.22.x"
76
+ }
77
+ }
78
+ ```
79
+
80
+ 指定项目运行所需的最低 Node.js 和 Yarn 版本。
81
+
82
+ ### NPM Scripts
83
+
84
+ #### 构建相关
85
+
86
+ | Script | 命令 | 说明 |
87
+ |--------|------|------|
88
+ | `ncc:build` | `ncc build ./src/index.ts -m -o ./dist/single -e sqlite3 --no-source-map-register && ncc cache clean` | 使用 `@vercel/ncc` 将代码打包成单文件,排除 sqlite3 依赖 |
89
+ | `prepub` | `rimraf dist && mkdir dist && tsc && mv temp/* dist/ && rimraf temp` | 发布前准备:清理构建目录、运行 TypeScript 编译、移动产物 |
90
+
91
+ **prepub 命令详解**:
92
+ 1. `rimraf dist` - 删除现有的 dist 目录
93
+ 2. `mkdir dist` - 创建新的 dist 目录
94
+ 3. `tsc` - 使用 TypeScript 编译器编译(输出到 temp 目录)
95
+ 4. `mv temp/* dist/` - 将编译产物从 temp 移动到 dist
96
+ 5. `rimraf temp` - 删除临时目录
97
+
98
+ #### 开发相关
99
+
100
+ | Script | 命令 | 说明 |
101
+ |--------|------|------|
102
+ | `dev` | `http-server` | 启动本地开发服务器 |
103
+ | `upver` | `ncu -u && yarn install` | 更新所有依赖到最新版本 |
104
+ | `gen:idx` | `cti create ./src` | 自动生成 src 目录的 index.ts 文件 |
105
+
106
+ #### 测试相关
107
+
108
+ | Script | 命令 | 说明 |
109
+ |--------|------|------|
110
+ | `test` | `jest` | 运行 Jest 测试套件 |
111
+
112
+ #### 代码质量
113
+
114
+ | Script | 命令 | 说明 |
115
+ |--------|------|------|
116
+ | `lint` | `tsc --noEmit && eslint . --ext .ts` | 运行类型检查和 ESLint 检查 |
117
+ | `lint:fix` | `eslint . --ext .ts --fix` | 自动修复 ESLint 问题 |
118
+ | `precommit` | `yarn lint` | 提交前运行 lint 检查 |
119
+
120
+ #### 安全审计
121
+
122
+ | Script | 命令 | 说明 |
123
+ |--------|------|------|
124
+ | `security` | `yarn audit` | 运行完整的依赖安全审计 |
125
+ | `security:summary` | `yarn audit --summary` | 显示安全审计摘要 |
126
+ | `security:json` | `yarn audit --json` | 以 JSON 格式输出审计结果 |
127
+ | `security:check` | `node tools/security-check.js` | 自定义安全检查脚本 |
128
+
129
+ ### 依赖说明
130
+
131
+ #### 核心运行时依赖
132
+
133
+ | 依赖 | 版本 | 说明 |
134
+ |------|------|------|
135
+ | `axios` | ^1.12.2 | HTTP 客户端 |
136
+ | `cls-hooked` | ^4.2.2 | 异步上下文管理 |
137
+ | `dot-object` | ^2.1.5 | 对象路径操作工具 |
138
+ | `eventemitter3` | ^5.0.1 | 事件发射器 |
139
+ | `ioredis` | ^5.8.2 | Redis 客户端 |
140
+ | `js-cookie` | ^3.0.5 | Cookie 操作库 |
141
+ | `js-yaml` | ^4.1.0 | YAML 解析器 |
142
+ | `jsonata` | ^2.1.0 | JSON 查询和转换 |
143
+ | `lodash` | ^4.17.21 | 工具函数库 |
144
+ | `mustache` | ^4.2.0 | 模板引擎 |
145
+ | `storage-manager-js` | ^4.2.6 | 存储管理 |
146
+ | `store2` | ^2.14.4 | 本地存储封装 |
147
+ | `typedi` | ^0.10.0 | 依赖注入容器 |
148
+ | `whatwg-url` | ^15.1.0 | URL 解析 |
149
+
150
+ #### 开发依赖
151
+
152
+ 主要包括:
153
+ - TypeScript 相关工具链
154
+ - ESLint 及插件
155
+ - Jest 测试框架
156
+ - Prettier 代码格式化
157
+ - @vercel/ncc 打包工具
158
+
159
+ ---
160
+
161
+ ## tsconfig.json
162
+
163
+ TypeScript 编译器配置文件。
164
+
165
+ ### 编译选项
166
+
167
+ ```json
168
+ {
169
+ "compilerOptions": {
170
+ "outDir": "temp",
171
+ "target": "ES2021",
172
+ "module": "CommonJS"
173
+ }
174
+ }
175
+ ```
176
+
177
+ #### 基础配置
178
+
179
+ | 选项 | 值 | 说明 |
180
+ |------|-----|------|
181
+ | `outDir` | `"temp"` | 编译输出目录(由 prepub 脚本移动到 dist) |
182
+ | `target` | `"ES2021"` | 编译目标 JavaScript 版本 |
183
+ | `module` | `"CommonJS"` | 模块系统类型 |
184
+ | `lib` | `["ES2021"]` | 可用的库文件 |
185
+ | `moduleResolution` | `"node"` | Node.js 风格的模块解析 |
186
+
187
+ #### 严格模式选项
188
+
189
+ | 选项 | 值 | 说明 |
190
+ |------|-----|------|
191
+ | `strict` | `true` | 启用所有严格类型检查选项 |
192
+ | `noImplicitAny` | `true` | 禁止隐式 any 类型 |
193
+ | `strictPropertyInitialization` | `false` | 关闭类属性必须初始化的检查 |
194
+ | `forceConsistentCasingInFileNames` | `true` | 强制文件名大小写一致 |
195
+
196
+ #### 互操作性选项
197
+
198
+ | 选项 | 值 | 说明 |
199
+ |------|-----|------|
200
+ | `esModuleInterop` | `true` | 允许 CommonJS 和 ES 模块互操作 |
201
+ | `allowSyntheticDefaultImports` | `true` | 允许从没有默认导出的模块导入默认值 |
202
+ | `resolveJsonModule` | `true` | 允许导入 JSON 文件 |
203
+
204
+ #### 装饰器支持
205
+
206
+ | 选项 | 值 | 说明 |
207
+ |------|-----|------|
208
+ | `experimentalDecorators` | `true` | 启用实验性装饰器语法 |
209
+ | `emitDecoratorMetadata` | `true` | 为装饰器生成元数据(TypeDI 需要) |
210
+
211
+ #### 类型声明
212
+
213
+ | 选项 | 值 | 说明 |
214
+ |------|-----|------|
215
+ | `declaration` | `true` | 生成 .d.ts 类型声明文件 |
216
+ | `declarationMap` | `true` | 生成声明文件的 source map |
217
+
218
+ #### 调试与优化
219
+
220
+ | 选项 | 值 | 说明 |
221
+ |------|-----|------|
222
+ | `sourceMap` | `true` | 生成 source map 文件 |
223
+ | `removeComments` | `true` | 移除编译后的注释 |
224
+ | `skipLibCheck` | `true` | 跳过声明文件的类型检查(加快编译) |
225
+
226
+ #### 路径映射
227
+
228
+ ```json
229
+ {
230
+ "paths": {
231
+ "@/*": ["src/*"]
232
+ },
233
+ "baseUrl": "."
234
+ }
235
+ ```
236
+
237
+ 允许使用 `@/` 作为 `src/` 的别名。
238
+
239
+ #### 类型声明
240
+
241
+ ```json
242
+ {
243
+ "types": ["node", "jest"]
244
+ }
245
+ ```
246
+
247
+ 包含 Node.js 和 Jest 的类型定义。
248
+
249
+ ### 包含与排除
250
+
251
+ ```json
252
+ {
253
+ "include": [
254
+ "src/**/*",
255
+ "example/**/*",
256
+ "tools/**/*"
257
+ ],
258
+ "exclude": [
259
+ "node_modules",
260
+ "temp",
261
+ "dist",
262
+ "**/*.spec.ts",
263
+ "**/*.test.ts"
264
+ ]
265
+ }
266
+ ```
267
+
268
+ - **include**: 编译 src、example 和 tools 目录
269
+ - **exclude**: 排除 node_modules、构建产物和测试文件
270
+
271
+ ---
272
+
273
+ ## eslint.config.js
274
+
275
+ ESLint 9.x 新版扁平化配置格式。
276
+
277
+ ### 配置结构
278
+
279
+ 项目使用多配置策略,针对不同目录应用不同的规则:
280
+
281
+ #### 1. 忽略文件
282
+
283
+ ```javascript
284
+ {
285
+ ignores: [
286
+ '**/node_modules/**',
287
+ '**/temp/**',
288
+ '**/dist/**',
289
+ '**/*.js',
290
+ '**/*.d.ts',
291
+ ]
292
+ }
293
+ ```
294
+
295
+ 排除不需要检查的文件和目录。
296
+
297
+ #### 2. src/ 和 example/ 目录配置
298
+
299
+ **特点**: 启用类型感知的 lint 规则
300
+
301
+ ```javascript
302
+ {
303
+ files: ['src/**/*.ts', 'example/**/*.ts'],
304
+ languageOptions: {
305
+ parser: tsParser,
306
+ parserOptions: {
307
+ project: './tsconfig.json' // 使用项目配置进行类型检查
308
+ }
309
+ }
310
+ }
311
+ ```
312
+
313
+ **启用的类型感知规则**:
314
+ - `@typescript-eslint/no-floating-promises`: 'warn' - 警告未处理的 Promise
315
+ - `@typescript-eslint/await-thenable`: 'warn' - 避免 await 非 thenable 对象
316
+ - `@typescript-eslint/no-misused-promises`: 'warn' - Promise 误用检查
317
+
318
+ #### 3. tools/ 目录配置
319
+
320
+ **特点**: 不启用类型检查(提高性能)
321
+
322
+ ```javascript
323
+ {
324
+ files: ['tools/**/*.ts'],
325
+ languageOptions: {
326
+ parserOptions: {
327
+ // ✅ 不使用 project,避免解析错误
328
+ }
329
+ }
330
+ }
331
+ ```
332
+
333
+ #### 4. __tests__ 目录配置
334
+
335
+ **特点**: 测试文件专用配置
336
+
337
+ ### 通用规则设置
338
+
339
+ | 规则 | 设置 | 说明 |
340
+ |------|------|------|
341
+ | `@typescript-eslint/no-require-imports` | off | 允许使用 require() |
342
+ | `@typescript-eslint/no-var-requires` | off | 允许 require 赋值给变量 |
343
+ | `@typescript-eslint/explicit-function-return-type` | off | 不强制函数返回类型声明 |
344
+ | `@typescript-eslint/no-explicit-any` | off | 允许使用 any 类型 |
345
+ | `@typescript-eslint/no-unused-vars` | off | 不检查未使用变量 |
346
+ | `no-console` | off | 允许使用 console |
347
+ | `lines-between-class-members` | warn | 类成员之间需要空行 |
348
+
349
+ ---
350
+
351
+ ## jest.config.js
352
+
353
+ Jest 测试框架配置。
354
+
355
+ ```javascript
356
+ module.exports = {
357
+ preset: 'ts-jest',
358
+ testEnvironment: 'node',
359
+ setupFiles: ['reflect-metadata'],
360
+ };
361
+ ```
362
+
363
+ ### 配置说明
364
+
365
+ | 选项 | 值 | 说明 |
366
+ |------|-----|------|
367
+ | `preset` | `'ts-jest'` | 使用 ts-jest 预设,支持 TypeScript |
368
+ | `testEnvironment` | `'node'` | 在 Node.js 环境中运行测试 |
369
+ | `setupFiles` | `['reflect-metadata']` | 测试前加载 reflect-metadata(装饰器元数据支持) |
370
+
371
+ ### 使用方式
372
+
373
+ ```bash
374
+ # 运行所有测试
375
+ yarn test
376
+
377
+ # 运行特定测试文件
378
+ yarn test path/to/test.spec.ts
379
+ ```
380
+
381
+ ---
382
+
383
+ ## .prettierrc.json
384
+
385
+ Prettier 代码格式化配置。
386
+
387
+ ```json
388
+ {
389
+ "semi": true,
390
+ "trailingComma": "all",
391
+ "singleQuote": true,
392
+ "printWidth": 220,
393
+ "tabWidth": 4,
394
+ "arrowParens": "always"
395
+ }
396
+ ```
397
+
398
+ ### 配置说明
399
+
400
+ | 选项 | 值 | 说明 |
401
+ |------|-----|------|
402
+ | `semi` | `true` | 语句末尾添加分号 |
403
+ | `trailingComma` | `"all"` | 在所有可能的地方添加尾随逗号 |
404
+ | `singleQuote` | `true` | 使用单引号而非双引号 |
405
+ | `printWidth` | `220` | 单行最大字符数(较宽) |
406
+ | `tabWidth` | `4` | 缩进宽度为 4 个空格 |
407
+ | `arrowParens` | `"always"` | 箭头函数参数始终使用括号 |
408
+
409
+ ### 格式化示例
410
+
411
+ **Before:**
412
+ ```typescript
413
+ const foo = (x) => {return x + 1}
414
+ const obj = {a: 1, b: 2}
415
+ ```
416
+
417
+ **After:**
418
+ ```typescript
419
+ const foo = (x) => {
420
+ return x + 1;
421
+ };
422
+ const obj = { a: 1, b: 2 };
423
+ ```
424
+
425
+ ---
426
+
427
+ ## .ctirc
428
+
429
+ `create-ts-index` (cti) 工具配置,用于自动生成 TypeScript 索引文件。
430
+
431
+ ### 配置说明
432
+
433
+ ```json
434
+ {
435
+ "fileFirst": false,
436
+ "addNewline": true,
437
+ "useSemicolon": true,
438
+ "useTimestamp": false,
439
+ "includeCWD": true,
440
+ "excludes": ["__test__", "__tests__", "node_modules", "tools"],
441
+ "fileExcludePatterns": ["*excludeme.ts"],
442
+ "targetExts": ["ts", "tsx"],
443
+ "withoutBackupFile": true,
444
+ "quote": "'",
445
+ "verbose": false
446
+ }
447
+ ```
448
+
449
+ | 选项 | 值 | 说明 |
450
+ |------|-----|------|
451
+ | `fileFirst` | `false` | 不优先导出文件 |
452
+ | `addNewline` | `true` | 文件末尾添加换行符 |
453
+ | `useSemicolon` | `true` | 导出语句添加分号 |
454
+ | `useTimestamp` | `false` | 不在注释中添加时间戳 |
455
+ | `includeCWD` | `true` | 包含当前工作目录 |
456
+ | `excludes` | `[...]` | 排除的目录 |
457
+ | `targetExts` | `["ts", "tsx"]` | 目标文件扩展名 |
458
+ | `withoutBackupFile` | `true` | 不创建备份文件 |
459
+ | `quote` | `"'"` | 使用单引号 |
460
+ | `verbose` | `false` | 不显示详细日志 |
461
+
462
+ ### 使用方式
463
+
464
+ ```bash
465
+ # 生成 src 目录的 index.ts
466
+ yarn gen:idx
467
+ ```
468
+
469
+ ### 生成示例
470
+
471
+ 对于目录结构:
472
+ ```
473
+ src/
474
+ ├── foo.ts
475
+ ├── bar.ts
476
+ └── utils/
477
+ └── helper.ts
478
+ ```
479
+
480
+ 生成的 `src/index.ts`:
481
+ ```typescript
482
+ export * from './foo';
483
+ export * from './bar';
484
+ export * from './utils/helper';
485
+ ```
486
+
487
+ ---
488
+
489
+ ## 构建流程
490
+
491
+ ### 完整发布流程
492
+
493
+ 1. **清理旧构建**: `rimraf dist`
494
+ 2. **创建目录**: `mkdir dist`
495
+ 3. **TypeScript 编译**: `tsc` (输出到 temp)
496
+ 4. **移动产物**: `mv temp/* dist/`
497
+ 5. **清理临时文件**: `rimraf temp`
498
+ 6. **单文件打包**: `yarn ncc:build` (可选)
499
+
500
+ ### 输出结构
501
+
502
+ ```
503
+ dist/
504
+ ├── index.js # CommonJS 主入口
505
+ ├── index.d.ts # 类型定义
506
+ ├── index.d.ts.map # 类型定义 source map
507
+ └── ... # 其他编译产物
508
+
509
+ dist/single/
510
+ └── index.js # NCC 打包的单文件 (包含所有依赖)
511
+ ```
512
+
513
+ ---
514
+
515
+ ## 最佳实践
516
+
517
+ ### 开发工作流
518
+
519
+ 1. **启动开发**: `yarn dev`
520
+ 2. **自动生成索引**: `yarn gen:idx`
521
+ 3. **代码检查**: `yarn lint`
522
+ 4. **运行测试**: `yarn test`
523
+ 5. **发布前构建**: `yarn prepub`
524
+
525
+ ### 版本发布
526
+
527
+ ```bash
528
+ # 1. 更新版本号
529
+ npm version patch # 或 minor/major
530
+
531
+ # 2. 构建
532
+ yarn prepub
533
+
534
+ # 3. 发布到 NPM
535
+ npm publish --access public
536
+
537
+ # 4. 推送标签
538
+ git push origin main --follow-tags
539
+ ```
540
+
541
+ 详细发布流程请参考 [PUBLISHING.md](./PUBLISHING.md)。
542
+
543
+ ### 依赖更新
544
+
545
+ ```bash
546
+ # 检查可更新的依赖
547
+ ncu
548
+
549
+ # 更新所有依赖
550
+ yarn upver
551
+
552
+ # 安全审计
553
+ yarn security
554
+ ```
555
+
556
+ ---
557
+
558
+ ## 故障排除
559
+
560
+ ### TypeScript 编译错误
561
+
562
+ ```bash
563
+ # 只进行类型检查,不生成文件
564
+ yarn lint
565
+
566
+ # 查看详细错误信息
567
+ tsc --noEmit
568
+ ```
569
+
570
+ ### ESLint 配置问题
571
+
572
+ - **错误**: "Parsing error: Cannot read file 'tsconfig.json'"
573
+ - **解决**: 确保 `tsconfig.json` 在项目根目录
574
+ - **说明**: tools 和 __tests__ 目录不需要 project 配置
575
+
576
+ ### 构建产物问题
577
+
578
+ ```bash
579
+ # 清理所有构建产物
580
+ rimraf dist temp
581
+
582
+ # 重新构建
583
+ yarn prepub
584
+ ```
585
+
586
+ ### Jest 测试失败
587
+
588
+ - **错误**: "Cannot find module 'reflect-metadata'"
589
+ - **解决**: 确保 `setupFiles` 包含 'reflect-metadata'
590
+ - **说明**: TypeDI 装饰器需要元数据反射支持
591
+
592
+ ---
593
+
594
+ ## 相关文档
595
+
596
+ - [发布指南](./PUBLISHING.md)
597
+ - [TypeScript 官方文档](https://www.typescriptlang.org/tsconfig)
598
+ - [ESLint 配置文档](https://eslint.org/docs/latest/use/configure/)
599
+ - [Jest 配置文档](https://jestjs.io/docs/configuration)
600
+ - [Prettier 配置文档](https://prettier.io/docs/en/options.html)
601
+
602
+ ---
603
+
604
+ ## 更新日志
605
+
606
+ | 日期 | 版本 | 说明 |
607
+ |------|------|------|
608
+ | 2025-10-23 | 1.0.0 | 初始版本 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gaias/client_node",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "store data in cookie and localstorage",
5
5
  "main": "dist/single/index.js",
6
6
  "types": "dist/single/index.d.ts",