uni-log-sdk 0.2.0 → 0.2.2

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.
@@ -1,11 +1,37 @@
1
1
  # uni-log-sdk
2
2
 
3
- uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件、设备与会话心跳,批量上报到自建 uni-log 服务端。
3
+ 多端日志上报 SDK(uni-app / uni-app x / React Native / 任意 TS Web):console、JS 错误、自定义事件、设备与会话心跳,批量上报到自建 uni-log 服务端。
4
4
 
5
- - 纯 JS、无外部运行时依赖(协议代码已内置),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序中运行
5
+ - 纯 JS、无外部运行时依赖(协议代码已内置),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序、React Native(CLI / Expo)、任意 TS Web 应用中运行
6
6
  - 默认 10s 批量上报,满 50 条立即发送;发送失败写入本地暂存,冷启动自动续传
7
7
  - 提供 uni_modules 形态,HBuilderX 工程可直接引用
8
8
 
9
+ ## 支持平台
10
+
11
+ | 运行环境 | 接入方式 | 说明 |
12
+ | --- | --- | --- |
13
+ | uniapp / uniapp x(App·H5·小程序) | 主入口 `init()` 自动探测 uni 适配器 | console 劫持、页面栈、错误钩子全自动 |
14
+ | **React Native(CLI / Expo)** | 子路径 `uni-log-sdk/react-native` 的 `initRN()` | 存储经 AsyncStorage(Expo Go 内置);页面路由需一行接线 |
15
+ | **任意 TS Web 应用**(React / Vue / 纯浏览器) | 主入口 `init()` 自动探测浏览器适配器 | fetch + localStorage + window 错误事件,零配置 |
16
+ | 非 TS 技术栈(Flutter / 原生) | 按协议自行实现(HTTP + JSON + HMAC-SHA256) | 服务端零改动,详见协议定义 `@uni-log/shared` |
17
+
18
+ ### 适配器架构(贡献者向)
19
+
20
+ 平台差异全部收敛在 `src/adapters/`,`client.ts` 只面向 `types.ts` 里的 `PlatformAdapter` 接口:
21
+
22
+ ```
23
+ src/adapters/
24
+ ├── types.ts # PlatformAdapter / RequestOptions / RequestTask 纯接口
25
+ ├── uni.ts # uni 适配器(uni-app / uni-app x):uni.request / 同步 storage / 页面栈三级兜底
26
+ ├── web.ts # 浏览器适配器:localStorage / window 错误事件(SSR 防御)
27
+ ├── react-native.ts # RN 适配器:AsyncStorage 异步写穿+预读(ready 信号)/ ErrorUtils 链式
28
+ ├── fetch.ts # web / react-native 共用的 fetch 请求实现(POST + AbortController)
29
+ ├── detect.ts # detectAdapter:有 uni 全局走 uni,否则浏览器
30
+ └── index.ts # 桶出口(不得引出 RN 适配器——主入口依赖图禁止沾染 react-native)
31
+ ```
32
+
33
+ 新增平台支持 = 在 `adapters/` 加一个适配器文件 +(如需子路径)在 `tsup.config.ts` 加入口;队列、签名、重试、暂存续传等核心逻辑无需改动。
34
+
9
35
  ## 安装
10
36
 
11
37
  ### 方式一:uni_modules(推荐,HBuilderX / CLI 通用)
@@ -115,13 +141,35 @@ setConsoleReport(false) // 关闭 console.* 上报(保留原生打印),避
115
141
  await flush() // 应用退后台/页面隐藏时可主动上报
116
142
  ```
117
143
 
144
+ ## React Native / Expo 接入
145
+
146
+ ```bash
147
+ npm i uni-log-sdk @react-native-async-storage/async-storage
148
+ ```
149
+
150
+ ```ts
151
+ import { initRN } from 'uni-log-sdk/react-native'
152
+
153
+ initRN({
154
+ serverUrl: 'https://log.example.com',
155
+ appKey: '<看板分配>',
156
+ appSecret: '<与 appKey 成对>',
157
+ // 页面路由接线(react-navigation 为例):
158
+ getPage: () => navigationRef.getCurrentRoute()?.name ?? null,
159
+ })
160
+ ```
161
+
162
+ - JS 错误经 `ErrorUtils.setGlobalHandler` 自动捕获(链式保留原 handler)。
163
+ - 未处理 Promise rejection 需宿主接线:`require('promise/setimmediate/rejection-tracking').enable({ allRejections: true, onUnhandled: (id, e) => ulog('error', String(e)) })`。
164
+ - 存储经 AsyncStorage 异步写穿 + 冷启动预读;Expo Go 内置 async-storage,bare RN 需安装。
165
+
118
166
  ## 注意事项
119
167
 
120
168
  - `serverUrl` 不要带尾斜杠;App 真机需用局域网 IP 或 HTTPS 域名,Android 9+ 默认禁止明文 HTTP。
121
169
  - 小程序需要将上报域名加入 request 合法域名(开发期可在开发者工具勾选「不校验合法域名」)。
122
170
  - 自动可用的采集:设备信息、会话心跳、JS 错误;页面 PV 与自定义事件需手动调用。
123
171
  - `console` 全量上报通过运行时劫持实现,初始化时自动开启,可用 `hookConsole: false` 关闭。
124
- - 上报内容会经过服务端协议校验:单批 ≤50 条、`msg` ≤4000 字符、请求体 ≤512KB,超长由 SDK 自动截断。
172
+ - 单批最多 50 条日志 / 50 条事件;单条 `msg` 超过 4000 字符、`stack` 超过 8000 字符会自动截断。服务端请求体上限 1MB,超限批次会被拒绝,SDK 会暂存并在下次启动续传。
125
173
  - 各版本的新增、修复与**升级指南(含破坏性变更)**见 [CHANGELOG.md](./CHANGELOG.md)。
126
174
 
127
175
  ## License