spy-client 2.1.15 → 2.2.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.
Files changed (44) hide show
  1. package/.travis.yml +7 -4
  2. package/AGENTS.md +132 -0
  3. package/README.md +27 -19
  4. package/dist/head/base.d.ts +27 -26
  5. package/dist/head/error.d.ts +6 -6
  6. package/dist/head/observer.d.ts +5 -5
  7. package/dist/head/whitescreen.d.ts +6 -6
  8. package/dist/lib/data.d.ts +6 -6
  9. package/dist/lib/huffman.d.ts +9 -9
  10. package/dist/lib/interface.d.ts +209 -209
  11. package/dist/lib/spyHeadInterface.d.ts +36 -36
  12. package/dist/lib/util.d.ts +22 -22
  13. package/dist/module/fid.d.ts +15 -15
  14. package/dist/module/layoutShift.d.ts +18 -18
  15. package/dist/module/lcp.d.ts +17 -17
  16. package/dist/module/longtask.d.ts +46 -46
  17. package/dist/module/memory.d.ts +11 -11
  18. package/dist/module/navigatorInfo.d.ts +9 -9
  19. package/dist/module/resource.d.ts +39 -39
  20. package/dist/module/timing.d.ts +11 -11
  21. package/dist/module/tti.d.ts +19 -19
  22. package/dist/spy-client-basic.d.ts +118 -118
  23. package/dist/spy-client-basic.esm.js +298 -298
  24. package/dist/spy-client-basic.iife.js +298 -298
  25. package/dist/spy-client-basic.iife.min.js +1 -1
  26. package/dist/spy-client-basic.js +298 -298
  27. package/dist/spy-client-basic.min.js +1 -1
  28. package/dist/spy-client-basic.mjs +298 -298
  29. package/dist/spy-client.d.ts +32 -32
  30. package/dist/spy-client.esm.js +1430 -1423
  31. package/dist/spy-client.iife.js +1430 -1423
  32. package/dist/spy-client.iife.min.js +1 -1
  33. package/dist/spy-client.js +1430 -1423
  34. package/dist/spy-client.min.js +1 -1
  35. package/dist/spy-client.mjs +1430 -1423
  36. package/dist/spy-head.d.ts +6 -6
  37. package/dist/spy-head.js +359 -358
  38. package/dist/spy-head.min.js +1 -1
  39. package/dist/spy-local-cache.d.ts +18 -18
  40. package/dist/spy-local-cache.js +718 -711
  41. package/dist/spy-local-cache.min.js +1 -1
  42. package/package.json +26 -73
  43. package/test-results/.last-run.json +4 -0
  44. package/karma.conf.js +0 -111
package/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: node_js
2
2
 
3
3
  node_js:
4
- - '12'
4
+ - '24'
5
5
 
6
6
  env:
7
7
  - TRAVIS=true
@@ -9,9 +9,12 @@ env:
9
9
  addons:
10
10
  chrome: stable
11
11
 
12
+ before_install:
13
+ - "corepack enable"
14
+
12
15
  before_script:
13
- - "npm install"
14
- - "npm run build"
16
+ - "pnpm install"
17
+ - "pnpm build"
15
18
 
16
19
  script:
17
- - "npm run test"
20
+ - "pnpm test"
package/AGENTS.md ADDED
@@ -0,0 +1,132 @@
1
+ # spy-client 开发指南
2
+
3
+ ## 项目定位
4
+
5
+ 本仓库是 spy 日志采集 SDK,使用 TypeScript 开发,提供基础版 `spy-client-basic`、增强版 `spy-client` 和可尽早加载的 `spy-head`。SDK 面向浏览器,也允许基础版通过覆盖 `request` 适配 Node.js、跨端框架和小程序。
6
+
7
+ SDK 只负责采集、校验、采样和发送,不负责 Nginx 接入、统计聚合或平台查询。修改发送协议时必须联动 `rec-nginx`、`webb-analysis`、`webb-realtime` 和平台查询。
8
+
9
+ ## 技术栈和兼容性
10
+
11
+ - TypeScript 6、Rollup 4、TypeScript compiler。
12
+ - oxlint 静态检查。
13
+ - Playwright Test 端到端测试,使用本机 Chrome,不下载 Playwright 浏览器。
14
+ - `core-js` 运行时依赖。
15
+ - Performance、Long Task、LCP、FID、CLS、Resource Timing、Navigator 等浏览器 API。
16
+
17
+ 开发环境要求 Node.js `>=24`,包管理器使用 pnpm `10.33.0`。构建目标保持 ES5,发布包保留 UMD、IIFE、ESM 和 `.mjs` 多格式产物。
18
+
19
+ 基础版日志类型包括 `perf`、`except`、`dist`、`count`。增强版包含更多性能和异常采集模块并依赖浏览器能力;`spy-head` 负责全局 JS 错误、资源错误、白屏和部分早期性能观察。
20
+
21
+ ## 运行与发送链路
22
+
23
+ ```text
24
+ 业务页面/跨端应用
25
+ → SpyClient 参数、字段和采样处理
26
+ → Image / Beacon / fetch 或自定义 request
27
+ → rec-nginx 日志接入
28
+ → 批处理或实时分析
29
+ ```
30
+
31
+ 公共字段通常包括 `pid`、`lid`、时间戳、类型、分组、`info` 和 `dim`。修改字段名、类型、采样语义、默认服务地址或请求方法时,要检查 Nginx 路由、protobuf 以及下游解析。
32
+
33
+ ## 目录说明
34
+
35
+ ```text
36
+ spy-client/
37
+ ├── src/
38
+ │ ├── spy-client.ts # 增强版入口
39
+ │ ├── spy-client-basic.ts # 基础版入口和公共发送逻辑
40
+ │ ├── spy-head.ts # 早期注入入口
41
+ │ ├── module/ # timing、LCP、FID、TTI、资源、LongTask 等
42
+ │ ├── head/ # 错误、白屏、资源观察
43
+ │ ├── lib/ # 数据、压缩、工具和接口
44
+ │ └── types/ # 全局类型
45
+ ├── test/e2e/ # Playwright Test 测试
46
+ ├── example/ # 本地示例和缓存示例
47
+ ├── rollup.config.mjs
48
+ ├── playwright.config.ts
49
+ ├── .oxlintrc.json
50
+ ├── tsconfig.json
51
+ ├── pnpm-lock.yaml
52
+ └── package.json
53
+ ```
54
+
55
+ ## 配置和产物
56
+
57
+ 构建产物全部位于 `dist/`,包括基础版、增强版、`spy-head`、`spy-local-cache` 及其压缩文件和声明文件。`spy-client`、`spy-client-basic` 额外生成 IIFE、ESM 和 `.mjs` 格式。
58
+
59
+ ## 开发命令
60
+
61
+ ```bash
62
+ pnpm install
63
+ pnpm run lint
64
+ pnpm run test
65
+ pnpm run build
66
+ pnpm run dev
67
+ pnpm run watch
68
+ pnpm run example
69
+ ```
70
+
71
+ `build` 是生产 Rollup 构建;`test` 使用 Playwright Test,并通过 `playwright.config.ts` 启动静态服务器和系统 Chrome;`lint` 使用 oxlint 检查 `src`;`example` 会启动开发构建和本地示例服务。
72
+
73
+ 发布命令必须单独确认:
74
+
75
+ ```bash
76
+ pnpm run release_pre
77
+ pnpm run release
78
+ pnpm run release_post
79
+ ```
80
+
81
+ 其中 `release_pre` 会重新构建、lint、测试;`release` 会修改版本并发布 npm;`release_post` 会推送分支和 tag,不得在普通验证中执行。
82
+
83
+ ## 测试要求
84
+
85
+ 修改公共发送逻辑时至少覆盖:
86
+
87
+ - 必填 `pid` 和非法字段。
88
+ - 四种日志类型。
89
+ - 全局采样和单条采样覆盖关系。
90
+ - `info`/`dim` 字段编码和边界。
91
+ - 浏览器发送成功、发送失败和自定义 request。
92
+ - 基础版与增强版运行时差异。
93
+
94
+ 修改性能模块时检查对应浏览器 API 不可用的降级行为。增强版不得引入只适用于 Node.js 的全局假设。
95
+
96
+ ## 常见问题
97
+
98
+ ### 上报没有到达
99
+
100
+ 检查实例参数、采样值、浏览器 Network、请求 URL、CORS/凭据、Nginx 路由和服务端日志。不能只看 SDK 是否调用成功。
101
+
102
+ ### 指标为空或字段丢失
103
+
104
+ 检查指标是否在正确的 `info`/`dim` 中、字段是否通过 SDK 校验、Nginx/Lua 是否截断,以及批处理/实时消费者是否支持新字段。
105
+
106
+ ### Node.js/跨端加载异常
107
+
108
+ 基础版需要按源码约定覆盖请求实现;增强版依赖浏览器 API。不能把 README 中“支持跨端”理解为所有构建产物都能在裸 Node.js 直接加载。
109
+
110
+ ## 跨仓库约束
111
+
112
+ - SDK payload 变更必须检查 `rec-nginx/conf/proto/` 和 Lua。
113
+ - 日志类型变更必须检查批处理目录、实时同步副本和 Server 查询。
114
+ - 指标或维度变更要评估平台维度组合数和存储成本。
115
+ - README 中历史 CDN 版本、仓库地址和示例只作为线索,不能覆盖当前 package/source 事实。
116
+
117
+ ## 安全边界
118
+
119
+ - 不在示例、测试和日志中写入 token、Cookie 或真实业务数据。
120
+ - 不将外部输入直接拼接 URL、查询参数或 HTML。
121
+ - 不使用生产服务做自动化压力测试。
122
+ - 采集字段可能包含 URL、错误堆栈和请求相关信息,新增字段需评估隐私与数据量。
123
+
124
+ ## 验证清单
125
+
126
+ - [ ] `pnpm run lint`
127
+ - [ ] `pnpm run test`
128
+ - [ ] `pnpm run build`
129
+ - [ ] 检查 `dist/` 入口和声明文件
130
+ - [ ] 验证成功、采样为零、非法参数和 request 失败
131
+ - [ ] 检查下游日志协议兼容
132
+ - [ ] 不执行 npm/pnpm 发布和远端推送
package/README.md CHANGED
@@ -7,11 +7,19 @@
7
7
  2. 从2.1.0版本开始,不再兼容IE8及以下IE浏览器
8
8
  3. 从2.1.8版本开始,兼容小程序环境(new Image类发送);通过继承类,覆盖request方法,可以支持Node.js/跨端框架/小程序环境
9
9
 
10
+ ## 开发环境
11
+
12
+ - Node.js `>=24`
13
+ - pnpm `10.33.0`(项目通过 `packageManager` 字段声明)
14
+ - 测试需要本机已安装 Google Chrome。Playwright 使用系统 Chrome,不会下载 Playwright 自带的浏览器。
15
+
16
+ 项目使用 Rollup 4 构建,并保留 ES5 兼容目标。`spy-client` 和 `spy-client-basic` 会生成 UMD、IIFE、ESM 和 `.mjs` 格式;`spy-head` 和 `spy-local-cache` 生成 UMD 格式。TypeScript 类型声明由构建脚本单独生成。
17
+
10
18
 
11
19
  ## 安装
12
20
 
13
21
  ```
14
- npm install spy-client --save
22
+ pnpm add spy-client
15
23
  ```
16
24
 
17
25
  CDN方式
@@ -20,13 +28,13 @@ CDN方式
20
28
 
21
29
  ```html
22
30
  <!--增强版SDK-->
23
- <script src="https://code.bdstatic.com/npm/spy-client@2.1.8/dist/spy-client.min.js" type="text/javascript"></script>
31
+ <script src="https://code.bdstatic.com/npm/spy-client@2.2.0/dist/spy-client.min.js" type="text/javascript"></script>
24
32
 
25
33
  <!--增强版SDK spy-head-->
26
- <script src="https://code.bdstatic.com/npm/spy-client@2.1.8/dist/spy-head.min.js" type="text/javascript"></script>
34
+ <script src="https://code.bdstatic.com/npm/spy-client@2.2.0/dist/spy-head.min.js" type="text/javascript"></script>
27
35
 
28
36
  <!--基础版SDK-->
29
- <script src="https://code.bdstatic.com/npm/spy-client@2.1.8/dist/spy-client-basic.min.js" type="text/javascript"></script>
37
+ <script src="https://code.bdstatic.com/npm/spy-client@2.2.0/dist/spy-client-basic.min.js" type="text/javascript"></script>
30
38
 
31
39
  ```
32
40
 
@@ -872,7 +880,7 @@ export interface NavigatorInfoMetric {
872
880
  ## 自定义构建
873
881
 
874
882
  #### node版本
875
- node版本 = 16
883
+ Node.js版本 >= 24,包管理器使用 pnpm 10.33.0
876
884
 
877
885
  #### clone准备
878
886
  如果觉得spy-client太大,只想要部分模块,比如禁用 longtask,可以拉取源码,自行编译
@@ -881,7 +889,7 @@ node版本 = 16
881
889
  git clone https://github.com/kaivean/spy-client.git
882
890
 
883
891
  cd spy-client
884
- npm install
892
+ pnpm install
885
893
  ```
886
894
 
887
895
  #### 禁用模块
@@ -901,7 +909,7 @@ npm install
901
909
  #### 构建
902
910
 
903
911
  ```bash
904
- npm run build
912
+ pnpm run build
905
913
  ```
906
914
 
907
915
  然后找到dist/spy-client.min.js 就是构建压缩版代码
@@ -911,33 +919,33 @@ npm run build
911
919
 
912
920
  ```bash
913
921
  # 启动本地调试页面,进行调试
914
- npm run example
922
+ pnpm run example
915
923
 
916
924
  # 进行watch 编译, 一般和上个命令配合使用
917
- npm run watch
925
+ pnpm run watch
918
926
 
919
- # lint
920
- npm run lint
927
+ # 使用 oxlint 检查 src
928
+ pnpm run lint
921
929
 
922
- # 测试
923
- npm run test
930
+ # 使用系统 Chrome 运行 Playwright 测试
931
+ pnpm run test
924
932
 
925
933
  # production编译,产出到dist
926
- npm run build
934
+ pnpm run build
927
935
 
928
936
  # development编译,产出到dist
929
- npm run dev
937
+ pnpm run dev
930
938
 
931
939
  # 发布
932
940
  # 1. 构建测试
933
- npm run release_pre
941
+ pnpm run release_pre
934
942
  # 2. 提交代码
935
943
  git add . && git commit -m "升级/Fix"
936
944
  # 3. 发布npm包,增加tag
937
- npm run release
945
+ pnpm run release
938
946
 
939
947
  # 4. 修改Readme文档里版本号
940
948
  git add . && git commit -m "修改文档版本"
941
949
  # 5. 把代码push到远程
942
- npm run release_post
943
- ```
950
+ pnpm run release_post
951
+ ```
@@ -1,26 +1,27 @@
1
- /**
2
- * @file 资源、JS、白屏监控的基础
3
- * @author kaivean
4
- */
5
- import { SpyHeadConf, SpyHeadUpdateConf } from '../lib/spyHeadInterface';
6
- interface SendObj {
7
- type?: 'perf' | 'except' | 'dist' | 'count';
8
- group: string;
9
- info: any;
10
- dim?: any;
11
- lid?: string;
12
- pid?: string;
13
- ts?: number;
14
- }
15
- declare const _default: {
16
- conf: SpyHeadConf;
17
- winerrors: SendObj[];
18
- errorDestroy(): void;
19
- observerDestroy(): void;
20
- entryMap: any;
21
- init(conf: SpyHeadConf): void;
22
- update(conf: SpyHeadUpdateConf): void;
23
- addError(obj: SendObj): void;
24
- send(obj: SendObj, isSend?: boolean, logServer?: string): void;
25
- };
26
- export default _default;
1
+ /**
2
+ * @file 资源、JS、白屏监控的基础
3
+ * @author kaivean
4
+ */
5
+ import { SpyHeadConf, SpyHeadUpdateConf } from '../lib/spyHeadInterface';
6
+ interface SendObj {
7
+ type?: 'perf' | 'except' | 'dist' | 'count';
8
+ group: string;
9
+ info: any;
10
+ dim?: any;
11
+ lid?: string;
12
+ pid?: string;
13
+ ts?: number;
14
+ }
15
+ declare const _default: {
16
+ conf: SpyHeadConf;
17
+ winerrors: SendObj[];
18
+ errorDestroy(): void;
19
+ observerDestroy(): void;
20
+ entryMap: any;
21
+ interceptor: ((obj: SendObj) => void) | undefined;
22
+ init(conf: SpyHeadConf): void;
23
+ update(conf: SpyHeadUpdateConf): void;
24
+ addError(obj: SendObj): void;
25
+ send(obj: SendObj, isSend?: boolean, logServer?: string): void;
26
+ };
27
+ export default _default;
@@ -1,6 +1,6 @@
1
- /**
2
- * @file 资源和JS错误监控
3
- * @author kaivean
4
- */
5
- import { SpyHeadConf } from '../lib/spyHeadInterface';
6
- export declare function init(conf: SpyHeadConf): void;
1
+ /**
2
+ * @file 资源和JS错误监控
3
+ * @author kaivean
4
+ */
5
+ import { SpyHeadConf } from '../lib/spyHeadInterface';
6
+ export declare function init(conf: SpyHeadConf): void;
@@ -1,5 +1,5 @@
1
- /**
2
- * @file PerformanceObserver指标采集
3
- * @author kaivean
4
- */
5
- export declare function init(): void;
1
+ /**
2
+ * @file PerformanceObserver指标采集
3
+ * @author kaivean
4
+ */
5
+ export declare function init(): void;
@@ -1,6 +1,6 @@
1
- /**
2
- * @file 白屏监控
3
- * @author kaivean
4
- */
5
- import { SpyHeadConf } from '../lib/spyHeadInterface';
6
- export declare function init(conf: SpyHeadConf): void;
1
+ /**
2
+ * @file 白屏监控
3
+ * @author kaivean
4
+ */
5
+ import { SpyHeadConf } from '../lib/spyHeadInterface';
6
+ export declare function init(conf: SpyHeadConf): void;
@@ -1,6 +1,6 @@
1
- /**
2
- * @file 简单的公共数据模块,内部模块之间数据交换
3
- * @author kaivean
4
- */
5
- export declare function setData(key: string, value: any): void;
6
- export declare function getData(key: string): any;
1
+ /**
2
+ * @file 简单的公共数据模块,内部模块之间数据交换
3
+ * @author kaivean
4
+ */
5
+ export declare function setData(key: string, value: any): void;
6
+ export declare function getData(key: string): any;
@@ -1,9 +1,9 @@
1
- /**
2
- * @file Huffman
3
- * @author kaivean
4
- */
5
- export declare function huffmanEncode(str: string): {
6
- codes: any;
7
- result: string;
8
- };
9
- export declare function huffmanDecode(codes: any, str: string): string;
1
+ /**
2
+ * @file Huffman
3
+ * @author kaivean
4
+ */
5
+ export declare function huffmanEncode(str: string): {
6
+ codes: any;
7
+ result: string;
8
+ };
9
+ export declare function huffmanDecode(codes: any, str: string): string;