uni-log-sdk 0.1.1 → 0.2.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,51 @@
1
+ # Changelog
2
+
3
+ `uni-log-sdk` 的版本变更记录,按新→旧排列,日期为 npm 实际发布日期。
4
+
5
+ ## [0.2.0] — 2026-11-13
6
+
7
+ ### 新增
8
+
9
+ - `init({ appSecret })`:新增应用签名密钥,与 `appKey` 成对由看板「应用管理」下发。
10
+ - 上报请求增加 HMAC-SHA256 签名(请求头 `x-uni-log-ts` / `x-uni-log-nonce` / `x-uni-log-sign`),服务端校验 5 分钟时间窗并拦截重放,防止伪造上报。
11
+ - 签名用纯 JS 实现,H5、小程序、App 端均无需额外依赖。
12
+
13
+ ### 变更
14
+
15
+ - 自定义 `adapter.request` 收到的 `data` 从对象变为已序列化的 JSON 字符串(`Content-Type` 仍为 `application/json`)。
16
+
17
+ ### 修复
18
+
19
+ - 修复 TypeScript 类型声明引用未发布依赖的问题:外部工程 `import 'uni-log-sdk'` 不再报 `TS2307`。
20
+
21
+ ### 破坏性变更 / 升级指南
22
+
23
+ - 服务端默认强制验签。**0.1.1 及更早版本不带签名,上报会被 401 拒绝**,这是跨版本的硬切换。
24
+ - 升级顺序:先部署支持 `appSecret` 的服务端 → 在「应用管理」复制与 `appKey` 成对的 `appSecret` → 各项目升级 SDK 到 0.2.0 并在 `init` 里传入。
25
+ - `appSecret` 请通过构建环境变量注入,不要提交进业务项目仓库;泄露后在看板「重置凭据」作废旧值。重置会同时更换 `appKey` 与 `appSecret` 且无宽限期,已发版客户端会立刻断报,需要重新发版。
26
+ - 灰度办法:临时把服务端 `INGEST_REQUIRE_SIGNATURE` 设为 `false` 放行旧客户端,全量升级后改回 `true`。
27
+ - 使用自定义 `adapter` 的工程需兼容 `data` 为字符串。
28
+ - `serverUrl` 仍不带尾斜杠;签名失败返回 401,SDK 会暂存并在下次启动续传。
29
+
30
+ ## [0.1.1] — 2026-11-12
31
+
32
+ ### 修复
33
+
34
+ - 修复所有日志的 `page` 字段恒为空:uni-app / uni-app x 的页面栈 API 是全局函数 `getCurrentPages()`,不是 `uni.getCurrentPages()`;现已兼容蒸汽模式、小程序模拟器等不同宿主。
35
+
36
+ ## [0.1.0] — 2026-11-12
37
+
38
+ 首个公开发布版本,包名 `uni-log-sdk`。
39
+
40
+ ### 新增
41
+
42
+ - 自动采集:设备信息、会话心跳、JS 错误(`uni.onError` / `onUnhandledRejection`);运行时劫持 `console.*` 上报,可用 `setConsoleReport(false)` 关闭。
43
+ - 批量上报:默认 10s 一次;失败暂存本地 storage(最多 20 批),冷启动自动续传;请求 8s 超时兜底。
44
+ - API:`init` / `ulog` / `ulogger` / `report` / `pageView` / `flush` / `setDeviceName` / `setConsoleReport` / `getClient`。
45
+ - 两种接入方式:直接 `import 'uni-log-sdk'`,或使用 `uni_modules/uni-log/`(`npx uni-log sync [--target <工程根>]` 可同步到宿主工程)。
46
+ - `postinstall` 自动探测宿主:仅当只找到一个 uni-app 工程时自动同步;可用 `UNI_LOG_AUTO_SYNC=0` 关闭。
47
+ - 上报约束:单批 ≤50 条、`msg` ≤4000 字符、请求体 ≤512KB,超长内容自动截断。
48
+
49
+ ### 说明
50
+
51
+ - npm 上的 `0.0.0-stage` 是无实际内容的占位版本,请勿安装。
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件、设备与会话心跳,批量上报到自建 uni-log 服务端。
4
4
 
5
- - 纯 JS、无外部运行时依赖(shared 协议已内联),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序中运行
5
+ - 纯 JS、无外部运行时依赖(协议代码已内置),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序中运行
6
6
  - 默认 10s 批量上报,满 50 条立即发送;发送失败写入本地暂存,冷启动自动续传
7
7
  - 提供 uni_modules 形态,HBuilderX 工程可直接引用
8
8
 
@@ -64,18 +64,23 @@ import { init } from '@/uni_modules/uni-log'
64
64
  init({
65
65
  serverUrl: 'https://log.example.com', // 不带尾斜杠;App 真机不要用 localhost
66
66
  appKey: '<看板「应用管理」分配>',
67
+ appSecret: '<看板「应用管理」分配,与 appKey 成对>',
67
68
  appVer: '1.0.0',
68
69
  release: 'release',
69
70
  channel: 'default',
70
71
  })
71
72
  ```
72
73
 
74
+ > 服务端默认强制 HMAC-SHA256 签名。`appSecret` 只应通过构建环境变量/CI 注入,
75
+ > 不要提交进各项目仓库;泄露后在看板「重置凭据」即可作废旧值。
76
+
73
77
  ### 选项
74
78
 
75
79
  | 选项 | 类型 | 默认 | 说明 |
76
80
  | --- | --- | --- | --- |
77
81
  | `serverUrl` | string | 必填 | 服务端地址,上报路径为 `/ingest/v1/logs` |
78
82
  | `appKey` | string | 必填 | 应用 Key,看板「应用管理」获取 |
83
+ | `appSecret` | string | - | 签名密钥;服务端开启强制签名时必填 |
79
84
  | `appVer` | string | - | 业务应用版本号 |
80
85
  | `release` | string | - | 发布渠道/环境标识 |
81
86
  | `channel` | string | - | 渠道 code / 小程序场景值 |
@@ -115,8 +120,9 @@ await flush() // 应用退后台/页面隐藏时可主动上报
115
120
  - `serverUrl` 不要带尾斜杠;App 真机需用局域网 IP 或 HTTPS 域名,Android 9+ 默认禁止明文 HTTP。
116
121
  - 小程序需要将上报域名加入 request 合法域名(开发期可在开发者工具勾选「不校验合法域名」)。
117
122
  - 自动可用的采集:设备信息、会话心跳、JS 错误;页面 PV 与自定义事件需手动调用。
118
- - `console` 全量上报目前依赖运行时劫持;编译期 `transform-console` 插件尚未发布。
123
+ - `console` 全量上报通过运行时劫持实现,初始化时自动开启,可用 `hookConsole: false` 关闭。
119
124
  - 上报内容会经过服务端协议校验:单批 ≤50 条、`msg` ≤4000 字符、请求体 ≤512KB,超长由 SDK 自动截断。
125
+ - 各版本的新增、修复与**升级指南(含破坏性变更)**见 [CHANGELOG.md](./CHANGELOG.md)。
120
126
 
121
127
  ## License
122
128
 
package/bin/uni-log.mjs CHANGED
@@ -48,7 +48,7 @@ if (targetIndex >= 0 && !targetArg) {
48
48
  const targetRoot = resolve(targetArg ?? process.cwd())
49
49
  const src = join(pkgRoot, 'uni_modules', 'uni-log')
50
50
  if (!existsSync(join(src, 'index.js'))) {
51
- console.error('[uni-log] 包内缺少 uni_modules/uni-log,请先执行 npm run build')
51
+ console.error('[uni-log] 安装包不完整,缺少 uni_modules/uni-log,请重新安装 uni-log-sdk')
52
52
  process.exit(1)
53
53
  }
54
54
 
package/dist/index.d.mts CHANGED
@@ -1,5 +1,129 @@
1
- import { DeviceInfo, LogLevel } from '@uni-log/shared';
2
- export { EventEntry, LogEntry, LogLevel, ReportPayload } from '@uni-log/shared';
1
+ /**
2
+ * uni-log 上报协议
3
+ * 客户端 SDK 与服务端共同遵守的数据契约。
4
+ *
5
+ * 字段命名对齐官方 uni统计 2.0 的日志表 / 设备表:
6
+ * - 日志条目仍用短名(t/lv/msg/stack/page/extra/tags/fp)以减小上报体积
7
+ * - 设备、会话、维度的字段名与官方 `uni-stat-*-logs` 表一致,便于看板出参对齐
8
+ */
9
+ /** 日志级别,crash 为原生崩溃专用 */
10
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'crash';
11
+ /** 单条日志条目 */
12
+ interface LogEntry {
13
+ /** 时间戳(毫秒),以客户端为准,服务端同时记录接收时间 */
14
+ t: number;
15
+ /** 级别 */
16
+ lv: LogLevel;
17
+ /** 消息主体(console 原文 / 错误 message) */
18
+ msg: string;
19
+ /** 错误堆栈(原始,未经 sourcemap 还原) */
20
+ stack?: string;
21
+ /** 发生时的页面路径(uni-app route) */
22
+ page?: string;
23
+ /** 附加结构化数据 */
24
+ extra?: Record<string, unknown>;
25
+ /** 过滤标签,类似微信实时日志 setFilterMsg */
26
+ tags?: string[];
27
+ /** 错误指纹(SDK 可选预计算,服务端兜底) */
28
+ fp?: string;
29
+ /** 原生崩溃落盘文件内容(仅 crash 级别) */
30
+ crashFile?: string;
31
+ }
32
+ /** 自定义事件条目(对标 uni.report(eventKey, param)) */
33
+ interface EventEntry {
34
+ /** 时间戳(毫秒) */
35
+ t: number;
36
+ /** 事件 ID(eventKey),字母数字下划线,≤64 */
37
+ ev: string;
38
+ /** 事件参数(uni.report 的 param),≤255 字符 */
39
+ val?: string;
40
+ /** 发生时的页面路径 */
41
+ page?: string;
42
+ }
43
+ /**
44
+ * 设备信息:字段对齐官方 `uni-stat-*-logs` 的 device_* 列。
45
+ * 平台取不到的字段一律留空,服务端按 null 落库(不伪造)。
46
+ */
47
+ interface DeviceInfo {
48
+ /** uni.getDeviceInfo / plus.device.uuid 得到的设备标识 */
49
+ deviceId: string;
50
+ /** 平台 code:android / ios / harmony / web / mp-weixin ...(服务端归一化,h5→web、wx→mp-weixin) */
51
+ platform: string;
52
+ /**
53
+ * 设备名称(给人看的,如「Jane 的 iPhone」)。
54
+ * 业务显式指定或平台提供;缺省时服务端用「品牌 + 型号」兜底,再缺省用型号/系统/标识。
55
+ */
56
+ deviceName?: string;
57
+ /** 设备型号 device_model */
58
+ model?: string;
59
+ /** 设备品牌/供应商 device_vendor */
60
+ vendor?: string;
61
+ /** 系统版本 device_os(iOS 为 15.1,Android 为 API 等级 30) */
62
+ os?: string;
63
+ /** 系统版本名称 device_os_version(Android 为 5.1.1) */
64
+ osVersion?: string;
65
+ /** 系统名称 device_os_name,用于区分 Android 与鸿蒙 */
66
+ osName?: string;
67
+ /** 网络型号 device_net */
68
+ net?: string;
69
+ /** 语言包 device_language */
70
+ language?: string;
71
+ /** 像素比 device_pixel_ratio */
72
+ pixelRatio?: number;
73
+ /** 窗口宽高 device_window_width / height */
74
+ windowWidth?: number;
75
+ windowHeight?: number;
76
+ /** 屏幕宽高 device_screen_width / height */
77
+ screenWidth?: number;
78
+ screenHeight?: number;
79
+ }
80
+ /** 上报公共头 */
81
+ interface ReportHeader {
82
+ /** 服务端注册应用时分配的 key */
83
+ appKey: string;
84
+ /** 版本号(业务应用自身的 versionName/versionCode 组合) */
85
+ appVer?: string;
86
+ /** 发布渠道/环境标识,如 release / trial */
87
+ release?: string;
88
+ /** 客户端上报的渠道 code / 场景值,对应官方 channel */
89
+ channel?: string;
90
+ /** 本次启动生成的会话 id */
91
+ sessionId: string;
92
+ /** 业务侧用户标识(可选) */
93
+ userId?: string;
94
+ /** 网络类型 wifi/4g/... */
95
+ net?: string;
96
+ /** SDK 版本,对应官方 sdk_version / 基础库版本号 */
97
+ sdkVer: string;
98
+ /** 宿主平台版本,如微信、支付宝 App 版本号,对应官方 platform_version */
99
+ platformVersion?: string;
100
+ /** 原生应用包名 package_name(App 端可采集) */
101
+ packageName?: string;
102
+ /** 原生应用包版本 package_version */
103
+ packageVersion?: string;
104
+ }
105
+ /**
106
+ * 会话心跳:对应官方 `uni-stat-session-logs`。
107
+ * 每次上报携带一次快照,服务端按 (appId, sessionId) 取最大值合并。
108
+ */
109
+ interface SessionReport {
110
+ /** 启动时间(毫秒) */
111
+ launchTime: number;
112
+ /** 已持续时长(秒),对应官方 duration */
113
+ duration: number;
114
+ /** 本次会话浏览页面数,对应官方 page_count */
115
+ pageCount: number;
116
+ }
117
+ /** 批量上报请求体 */
118
+ interface ReportPayload {
119
+ header: ReportHeader;
120
+ device: DeviceInfo;
121
+ /** 会话心跳,缺省则不更新会话 */
122
+ session?: SessionReport;
123
+ logs: LogEntry[];
124
+ /** 自定义事件(与 logs 同批上报,共用限流与鉴权) */
125
+ events?: EventEntry[];
126
+ }
3
127
 
4
128
  /**
5
129
  * uni API 抽象层。
@@ -37,6 +161,11 @@ interface UniLogOptions {
37
161
  serverUrl: string;
38
162
  /** 看板「应用管理」分配的 appKey */
39
163
  appKey: string;
164
+ /**
165
+ * 应用签名密钥(与 appKey 一起下发)。
166
+ * 配置后每个上报请求都会带 HMAC-SHA256 签名,服务端可拒绝伪造/重放请求。
167
+ */
168
+ appSecret?: string;
40
169
  appVer?: string;
41
170
  release?: string;
42
171
  /** 渠道/场景值 code,对应官方 channel(小程序可直接传场景值) */
@@ -159,4 +288,4 @@ declare function setDeviceName(name: string): void;
159
288
  /** 立即上报积压内容(页面隐藏/应用退后台时可调用) */
160
289
  declare function flush(): Promise<void>;
161
290
 
162
- export { type UniAdapter, UniLogClient, type UniLogOptions, flush, getClient, hookConsole, init, report, setConsoleReport, setDeviceName, ulog, ulogger };
291
+ export { type EventEntry, type LogEntry, type LogLevel, type ReportPayload, type UniAdapter, UniLogClient, type UniLogOptions, flush, getClient, hookConsole, init, report, setConsoleReport, setDeviceName, ulog, ulogger };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,129 @@
1
- import { DeviceInfo, LogLevel } from '@uni-log/shared';
2
- export { EventEntry, LogEntry, LogLevel, ReportPayload } from '@uni-log/shared';
1
+ /**
2
+ * uni-log 上报协议
3
+ * 客户端 SDK 与服务端共同遵守的数据契约。
4
+ *
5
+ * 字段命名对齐官方 uni统计 2.0 的日志表 / 设备表:
6
+ * - 日志条目仍用短名(t/lv/msg/stack/page/extra/tags/fp)以减小上报体积
7
+ * - 设备、会话、维度的字段名与官方 `uni-stat-*-logs` 表一致,便于看板出参对齐
8
+ */
9
+ /** 日志级别,crash 为原生崩溃专用 */
10
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'crash';
11
+ /** 单条日志条目 */
12
+ interface LogEntry {
13
+ /** 时间戳(毫秒),以客户端为准,服务端同时记录接收时间 */
14
+ t: number;
15
+ /** 级别 */
16
+ lv: LogLevel;
17
+ /** 消息主体(console 原文 / 错误 message) */
18
+ msg: string;
19
+ /** 错误堆栈(原始,未经 sourcemap 还原) */
20
+ stack?: string;
21
+ /** 发生时的页面路径(uni-app route) */
22
+ page?: string;
23
+ /** 附加结构化数据 */
24
+ extra?: Record<string, unknown>;
25
+ /** 过滤标签,类似微信实时日志 setFilterMsg */
26
+ tags?: string[];
27
+ /** 错误指纹(SDK 可选预计算,服务端兜底) */
28
+ fp?: string;
29
+ /** 原生崩溃落盘文件内容(仅 crash 级别) */
30
+ crashFile?: string;
31
+ }
32
+ /** 自定义事件条目(对标 uni.report(eventKey, param)) */
33
+ interface EventEntry {
34
+ /** 时间戳(毫秒) */
35
+ t: number;
36
+ /** 事件 ID(eventKey),字母数字下划线,≤64 */
37
+ ev: string;
38
+ /** 事件参数(uni.report 的 param),≤255 字符 */
39
+ val?: string;
40
+ /** 发生时的页面路径 */
41
+ page?: string;
42
+ }
43
+ /**
44
+ * 设备信息:字段对齐官方 `uni-stat-*-logs` 的 device_* 列。
45
+ * 平台取不到的字段一律留空,服务端按 null 落库(不伪造)。
46
+ */
47
+ interface DeviceInfo {
48
+ /** uni.getDeviceInfo / plus.device.uuid 得到的设备标识 */
49
+ deviceId: string;
50
+ /** 平台 code:android / ios / harmony / web / mp-weixin ...(服务端归一化,h5→web、wx→mp-weixin) */
51
+ platform: string;
52
+ /**
53
+ * 设备名称(给人看的,如「Jane 的 iPhone」)。
54
+ * 业务显式指定或平台提供;缺省时服务端用「品牌 + 型号」兜底,再缺省用型号/系统/标识。
55
+ */
56
+ deviceName?: string;
57
+ /** 设备型号 device_model */
58
+ model?: string;
59
+ /** 设备品牌/供应商 device_vendor */
60
+ vendor?: string;
61
+ /** 系统版本 device_os(iOS 为 15.1,Android 为 API 等级 30) */
62
+ os?: string;
63
+ /** 系统版本名称 device_os_version(Android 为 5.1.1) */
64
+ osVersion?: string;
65
+ /** 系统名称 device_os_name,用于区分 Android 与鸿蒙 */
66
+ osName?: string;
67
+ /** 网络型号 device_net */
68
+ net?: string;
69
+ /** 语言包 device_language */
70
+ language?: string;
71
+ /** 像素比 device_pixel_ratio */
72
+ pixelRatio?: number;
73
+ /** 窗口宽高 device_window_width / height */
74
+ windowWidth?: number;
75
+ windowHeight?: number;
76
+ /** 屏幕宽高 device_screen_width / height */
77
+ screenWidth?: number;
78
+ screenHeight?: number;
79
+ }
80
+ /** 上报公共头 */
81
+ interface ReportHeader {
82
+ /** 服务端注册应用时分配的 key */
83
+ appKey: string;
84
+ /** 版本号(业务应用自身的 versionName/versionCode 组合) */
85
+ appVer?: string;
86
+ /** 发布渠道/环境标识,如 release / trial */
87
+ release?: string;
88
+ /** 客户端上报的渠道 code / 场景值,对应官方 channel */
89
+ channel?: string;
90
+ /** 本次启动生成的会话 id */
91
+ sessionId: string;
92
+ /** 业务侧用户标识(可选) */
93
+ userId?: string;
94
+ /** 网络类型 wifi/4g/... */
95
+ net?: string;
96
+ /** SDK 版本,对应官方 sdk_version / 基础库版本号 */
97
+ sdkVer: string;
98
+ /** 宿主平台版本,如微信、支付宝 App 版本号,对应官方 platform_version */
99
+ platformVersion?: string;
100
+ /** 原生应用包名 package_name(App 端可采集) */
101
+ packageName?: string;
102
+ /** 原生应用包版本 package_version */
103
+ packageVersion?: string;
104
+ }
105
+ /**
106
+ * 会话心跳:对应官方 `uni-stat-session-logs`。
107
+ * 每次上报携带一次快照,服务端按 (appId, sessionId) 取最大值合并。
108
+ */
109
+ interface SessionReport {
110
+ /** 启动时间(毫秒) */
111
+ launchTime: number;
112
+ /** 已持续时长(秒),对应官方 duration */
113
+ duration: number;
114
+ /** 本次会话浏览页面数,对应官方 page_count */
115
+ pageCount: number;
116
+ }
117
+ /** 批量上报请求体 */
118
+ interface ReportPayload {
119
+ header: ReportHeader;
120
+ device: DeviceInfo;
121
+ /** 会话心跳,缺省则不更新会话 */
122
+ session?: SessionReport;
123
+ logs: LogEntry[];
124
+ /** 自定义事件(与 logs 同批上报,共用限流与鉴权) */
125
+ events?: EventEntry[];
126
+ }
3
127
 
4
128
  /**
5
129
  * uni API 抽象层。
@@ -37,6 +161,11 @@ interface UniLogOptions {
37
161
  serverUrl: string;
38
162
  /** 看板「应用管理」分配的 appKey */
39
163
  appKey: string;
164
+ /**
165
+ * 应用签名密钥(与 appKey 一起下发)。
166
+ * 配置后每个上报请求都会带 HMAC-SHA256 签名,服务端可拒绝伪造/重放请求。
167
+ */
168
+ appSecret?: string;
40
169
  appVer?: string;
41
170
  release?: string;
42
171
  /** 渠道/场景值 code,对应官方 channel(小程序可直接传场景值) */
@@ -159,4 +288,4 @@ declare function setDeviceName(name: string): void;
159
288
  /** 立即上报积压内容(页面隐藏/应用退后台时可调用) */
160
289
  declare function flush(): Promise<void>;
161
290
 
162
- export { type UniAdapter, UniLogClient, type UniLogOptions, flush, getClient, hookConsole, init, report, setConsoleReport, setDeviceName, ulog, ulogger };
291
+ export { type EventEntry, type LogEntry, type LogLevel, type ReportPayload, type UniAdapter, UniLogClient, type UniLogOptions, flush, getClient, hookConsole, init, report, setConsoleReport, setDeviceName, ulog, ulogger };
package/dist/index.js CHANGED
@@ -1 +1,3 @@
1
- "use strict";var K=Object.create;var m=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Y=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports),X=(n,t)=>{for(var e in t)m(n,e,{get:t[e],enumerable:!0})},A=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of J(t))!F.call(n,i)&&i!==e&&m(n,i,{get:()=>t[i],enumerable:!(r=$(t,i))||r.enumerable});return n};var z=(n,t,e)=>(e=n!=null?K(Y(n)):{},A(t||!n||!n.__esModule?m(e,"default",{value:n,enumerable:!0}):e,n)),Q=n=>A(m({},"__esModule",{value:!0}),n);var T=V(u=>{"use strict";Object.defineProperty(u,"__esModule",{value:!0});u.PROTOCOL_LIMITS=u.INGEST_PATH=u.LOG_LEVELS=void 0;u.LOG_LEVELS=["debug","info","warn","error","crash"];u.INGEST_PATH="/ingest/v1/logs";u.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var j=V(h=>{"use strict";var Z=h&&h.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),ee=h&&h.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&Z(t,n,e)};Object.defineProperty(h,"__esModule",{value:!0});ee(T(),h)});var pe={};X(pe,{flush:()=>he,getClient:()=>ae,hookConsole:()=>q,init:()=>se,report:()=>ue,setConsoleReport:()=>le,setDeviceName:()=>ce,ulog:()=>ge,ulogger:()=>W});module.exports=Q(pe);var g=z(j());function p(){let n=globalThis.uni;return n!=null?n:null}function te(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function ne(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function S(n){return typeof n=="string"&&n.length>0?n:void 0}function re(n){let t=l=>{var c;try{return(c=l==null?void 0:l())!=null?c:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(S).find(Boolean),d=(...l)=>l.map(ne).find(c=>c!==void 0),y=s(r.deviceId,e.deviceId,e.deviceUUID),v=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!y||!v?null:{deviceId:y,platform:v,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function B(){return{request(n){var e;let t=p();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=p())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=p())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return re(p())}catch{return null}},getCurrentPage(){var n;try{let t=te(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=p(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=S(s.hostVersion))!=null?i:S(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=p())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=p())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var w="uni-log:pending",M=20,ie="0.1.1";function f(n,t){return n.length>t?n.slice(0,t):n}function b(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function oe(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var P=class{constructor(t,e=B()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(oe(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,y,v,l,c,k,I,L,E,R,_,N,O,U,x,C,D;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(c=(l=(v=this.deviceNameOverride)!=null?v:b((y=(d=this.options).getDeviceName)==null?void 0:y.call(d)))!=null?l:b(this.options.deviceName))!=null?c:b(e==null?void 0:e.deviceName),i={deviceId:(k=e==null?void 0:e.deviceId)!=null?k:this.fallbackDeviceId(),platform:(I=e==null?void 0:e.platform)!=null?I:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((R=e==null?void 0:e.net)!=null?R:(E=(L=this.options).getNet)!=null&&E.call(L))?{net:(O=e==null?void 0:e.net)!=null?O:(N=(_=this.options).getNet)==null?void 0:N.call(_)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(x=(U=this.options).getUserId)!=null&&x.call(U)?{userId:this.options.getUserId()}:{},...(D=(C=this.options).getNet)!=null&&D.call(C)?{net:this.options.getNet()}:{},sdkVer:ie,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(w),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>M&&(r=r.slice(-M));try{this.adapter.setStorage(w,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(w);if(!t)return;this.adapter.setStorage(w,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function se(n){return o==null||o.stop(),o=new P(n),o.start(),n.hookConsole!==!1&&q(),o}function ae(){return o}function ge(n,t,e){o==null||o.log(n,t,e)}var W={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},H=new Set,G=!0;function le(n){G=n}function q(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(H.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{G&&W[r](s.map(de).join(" ")),a(...s)},H.add(e)}}function de(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function ue(n,t){o==null||o.report(n,t)}function ce(n){o==null||o.setDeviceName(n)}async function he(){await(o==null?void 0:o.flush())}0&&(module.exports={flush,getClient,hookConsole,init,report,setConsoleReport,setDeviceName,ulog,ulogger});
1
+ "use strict";var z=Object.create;var U=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var ee=Object.getPrototypeOf,te=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports),ne=(n,t)=>{for(var e in t)U(n,e,{get:t[e],enumerable:!0})},M=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Z(t))!te.call(n,i)&&i!==e&&U(n,i,{get:()=>t[i],enumerable:!(r=Q(t,i))||r.enumerable});return n};var re=(n,t,e)=>(e=n!=null?z(ee(n)):{},M(t||!n||!n.__esModule?U(e,"default",{value:n,enumerable:!0}):e,n)),ie=n=>M(U({},"__esModule",{value:!0}),n);var H=V(p=>{"use strict";Object.defineProperty(p,"__esModule",{value:!0});p.PROTOCOL_LIMITS=p.SIGNATURE_MAX_SKEW_MS=p.SIGNATURE_HEADERS=p.INGEST_PATH=p.LOG_LEVELS=void 0;p.LOG_LEVELS=["debug","info","warn","error","crash"];p.INGEST_PATH="/ingest/v1/logs";p.SIGNATURE_HEADERS={ts:"x-uni-log-ts",nonce:"x-uni-log-nonce",sign:"x-uni-log-sign"};p.SIGNATURE_MAX_SKEW_MS=300*1e3;p.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var j=V(S=>{"use strict";var oe=S&&S.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),se=S&&S.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&oe(t,n,e)};Object.defineProperty(S,"__esModule",{value:!0});se(H(),S)});var Se={};ne(Se,{flush:()=>we,getClient:()=>pe,hookConsole:()=>Y,init:()=>he,report:()=>ve,setConsoleReport:()=>me,setDeviceName:()=>be,ulog:()=>xe,ulogger:()=>J});module.exports=ie(Se);var u=re(j());function P(){let n=globalThis.uni;return n!=null?n:null}function ae(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function ce(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function A(n){return typeof n=="string"&&n.length>0?n:void 0}function ge(n){let t=d=>{var h;try{return(h=d==null?void 0:d())!=null?h:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),s=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let a=(...d)=>d.map(A).find(Boolean),c=(...d)=>d.map(ce).find(h=>h!==void 0),f=a(r.deviceId,e.deviceId,e.deviceUUID),l=a(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!f||!l?null:{deviceId:f,platform:l,deviceName:a(r.deviceName,e.deviceName),model:a(r.deviceModel,e.deviceModel,e.model),vendor:a(r.deviceBrand,e.deviceBrand,e.brand),os:a(e.osVersion,e.system,r.osVersion,r.system),osVersion:a(e.osVersion,r.osVersion),osName:a(e.osName,r.osName,i.osName),net:a(e.networkType,e.netType),language:a(i.language,e.language),pixelRatio:c(s.pixelRatio,e.pixelRatio),windowWidth:c(s.windowWidth,e.windowWidth),windowHeight:c(s.windowHeight,e.windowHeight),screenWidth:c(s.screenWidth,e.screenWidth),screenHeight:c(s.screenHeight,e.screenHeight)}}function B(){return{request(n){var e;let t=P();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=P())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=P())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return ge(P())}catch{return null}},getCurrentPage(){var n;try{let t=ae(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=P(),t=r=>{var i,s;try{let a=r();return a&&(s=(i=A(a.hostVersion))!=null?i:A(a.version))!=null?s:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=P())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=P())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var de=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];function y(n,t){return n>>>t|n<<32-t}function G(n){let t=[];for(let e=0;e<n.length;e++){let r=n.charCodeAt(e);if(r<128)t.push(r);else if(r<2048)t.push(192|r>>6,128|r&63);else if(r>=55296&&r<=56319&&e+1<n.length){let i=n.charCodeAt(e+1);if(i>=56320&&i<=57343){e+=1;let s=65536+(r-55296<<10)+(i-56320);t.push(240|s>>18,128|s>>12&63,128|s>>6&63,128|s&63)}else t.push(239,191,189)}else r>=56320&&r<=57343?t.push(239,191,189):t.push(224|r>>12,128|r>>6&63,128|r&63)}return t}function D(n){let t=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],e=n.length,r=n.slice();for(r.push(128);r.length%64!==56;)r.push(0);let i=e*8,s=Math.floor(i/4294967296),a=i>>>0;r.push(s>>>24&255,s>>>16&255,s>>>8&255,s&255),r.push(a>>>24&255,a>>>16&255,a>>>8&255,a&255);let c=new Array(64);for(let l=0;l<r.length;l+=64){for(let g=0;g<16;g++){let m=l+g*4;c[g]=(r[m]<<24|r[m+1]<<16|r[m+2]<<8|r[m+3])>>>0}for(let g=16;g<64;g++){let m=y(c[g-15],7)^y(c[g-15],18)^c[g-15]>>>3,E=y(c[g-2],17)^y(c[g-2],19)^c[g-2]>>>10;c[g]=c[g-16]+m+c[g-7]+E>>>0}let[d,h,v,I,x,b,w,k]=t;for(let g=0;g<64;g++){let m=y(x,6)^y(x,11)^y(x,25),E=x&b^~x&w,R=k+m+E+de[g]+c[g]>>>0,_=y(d,2)^y(d,13)^y(d,22),N=d&h^d&v^h&v,F=_+N>>>0;k=w,w=b,b=x,x=I+R>>>0,I=v,v=h,h=d,d=R+F>>>0}t[0]=t[0]+d>>>0,t[1]=t[1]+h>>>0,t[2]=t[2]+v>>>0,t[3]=t[3]+I>>>0,t[4]=t[4]+x>>>0,t[5]=t[5]+b>>>0,t[6]=t[6]+w>>>0,t[7]=t[7]+k>>>0}let f=[];for(let l of t)f.push(l>>>24&255,l>>>16&255,l>>>8&255,l&255);return f}function le(n){let t="";for(let e of n)t+=e.toString(16).padStart(2,"0");return t}function W(n,t){let e=G(n);for(e.length>64&&(e=D(e));e.length<64;)e.push(0);let r=e.map(s=>s^54),i=e.map(s=>s^92);return le(D(i.concat(D(r.concat(G(t))))))}function q(n=16){let t="";for(let e=0;e<n;e++)t+=Math.floor(Math.random()*256).toString(16).padStart(2,"0");return t}var O="uni-log:pending",K=20,ue="0.2.0";function L(n,t){return n.length>t?n.slice(0,t):n}function T(n){let t=n==null?void 0:n.trim();return t?L(t,128):void 0}function fe(n,t,e){var i;let r={t:Date.now(),lv:n,msg:L(t||"(empty)",u.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=L(e.stack,u.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=L(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var C=class{constructor(t,e=B()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=u.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let s=this.adapter.getCurrentPage();this.logs.push(fe(i,e,{...r,...s?{page:s}:{}})),this.logs.length>=u.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>u.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=L(e,u.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=u.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?L(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var a,c,f,l,d,h,v,I,x,b,w,k,g,m,E,R,_,N;let e=(a=this.deviceInfo)!=null?a:this.deviceInfo=this.adapter.getDeviceInfo(),r=(h=(d=(l=this.deviceNameOverride)!=null?l:T((f=(c=this.options).getDeviceName)==null?void 0:f.call(c)))!=null?d:T(this.options.deviceName))!=null?h:T(e==null?void 0:e.deviceName),i={deviceId:(v=e==null?void 0:e.deviceId)!=null?v:this.fallbackDeviceId(),platform:(I=e==null?void 0:e.platform)!=null?I:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((w=e==null?void 0:e.net)!=null?w:(b=(x=this.options).getNet)!=null&&b.call(x))?{net:(m=e==null?void 0:e.net)!=null?m:(g=(k=this.options).getNet)==null?void 0:g.call(k)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(R=(E=this.options).getUserId)!=null&&R.call(E)?{userId:this.options.getUserId()}:{},...(N=(_=this.options).getNet)!=null&&N.call(_)?{net:this.options.getNet()}:{},sdkVer:ue,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,u.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,u.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=a=>{r||(r=!0,clearTimeout(s),e(a))},s=setTimeout(()=>i(!1),8e3);try{let a=JSON.stringify(t),c={"Content-Type":"application/json"};if(this.options.appSecret){let f=String(Date.now()),l=q(16);c[u.SIGNATURE_HEADERS.ts]=f,c[u.SIGNATURE_HEADERS.nonce]=l,c[u.SIGNATURE_HEADERS.sign]=W(this.options.appSecret,`${f}
2
+ ${l}
3
+ ${a}`)}this.adapter.request({url:`${this.options.serverUrl}${u.INGEST_PATH}`,data:a,header:c,success:f=>i(!!f&&f.statusCode>=200&&f.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(O),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>K&&(r=r.slice(-K));try{this.adapter.setStorage(O,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(O);if(!t)return;this.adapter.setStorage(O,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function he(n){return o==null||o.stop(),o=new C(n),o.start(),n.hookConsole!==!1&&Y(),o}function pe(){return o}function xe(n,t,e){o==null||o.log(n,t,e)}var J={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},$=new Set,X=!0;function me(n){X=n}function Y(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if($.has(e))continue;let i=t[e];if(typeof i!="function")continue;let s=i.bind(console);t[e]=(...a)=>{X&&J[r](a.map(ye).join(" ")),s(...a)},$.add(e)}}function ye(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function ve(n,t){o==null||o.report(n,t)}function be(n){o==null||o.setDeviceName(n)}async function we(){await(o==null?void 0:o.flush())}0&&(module.exports={flush,getClient,hookConsole,init,report,setConsoleReport,setDeviceName,ulog,ulogger});
package/dist/index.mjs CHANGED
@@ -1 +1,3 @@
1
- var W=Object.create;var D=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var J=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of q(t))!$.call(n,i)&&i!==e&&D(n,i,{get:()=>t[i],enumerable:!(r=G(t,i))||r.enumerable});return n};var Y=(n,t,e)=>(e=n!=null?W(K(n)):{},J(t||!n||!n.__esModule?D(e,"default",{value:n,enumerable:!0}):e,n));var A=V(u=>{"use strict";Object.defineProperty(u,"__esModule",{value:!0});u.PROTOCOL_LIMITS=u.INGEST_PATH=u.LOG_LEVELS=void 0;u.LOG_LEVELS=["debug","info","warn","error","crash"];u.INGEST_PATH="/ingest/v1/logs";u.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var T=V(h=>{"use strict";var F=h&&h.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),X=h&&h.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&F(t,n,e)};Object.defineProperty(h,"__esModule",{value:!0});X(A(),h)});var g=Y(T());function p(){let n=globalThis.uni;return n!=null?n:null}function z(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function Q(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function P(n){return typeof n=="string"&&n.length>0?n:void 0}function Z(n){let t=l=>{var c;try{return(c=l==null?void 0:l())!=null?c:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(P).find(Boolean),d=(...l)=>l.map(Q).find(c=>c!==void 0),y=s(r.deviceId,e.deviceId,e.deviceUUID),v=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!y||!v?null:{deviceId:y,platform:v,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=p();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=p())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=p())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return Z(p())}catch{return null}},getCurrentPage(){var n;try{let t=z(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=p(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=P(s.hostVersion))!=null?i:P(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=p())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=p())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var m="uni-log:pending",B=20,ee="0.1.1";function f(n,t){return n.length>t?n.slice(0,t):n}function S(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function te(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var w=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(te(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,y,v,l,c,b,k,I,L,E,R,_,N,O,U,x,C;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(c=(l=(v=this.deviceNameOverride)!=null?v:S((y=(d=this.options).getDeviceName)==null?void 0:y.call(d)))!=null?l:S(this.options.deviceName))!=null?c:S(e==null?void 0:e.deviceName),i={deviceId:(b=e==null?void 0:e.deviceId)!=null?b:this.fallbackDeviceId(),platform:(k=e==null?void 0:e.platform)!=null?k:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((E=e==null?void 0:e.net)!=null?E:(L=(I=this.options).getNet)!=null&&L.call(I))?{net:(N=e==null?void 0:e.net)!=null?N:(_=(R=this.options).getNet)==null?void 0:_.call(R)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(U=(O=this.options).getUserId)!=null&&U.call(O)?{userId:this.options.getUserId()}:{},...(C=(x=this.options).getNet)!=null&&C.call(x)?{net:this.options.getNet()}:{},sdkVer:ee,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(m),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>B&&(r=r.slice(-B));try{this.adapter.setStorage(m,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(m);if(!t)return;this.adapter.setStorage(m,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function ce(n){return o==null||o.stop(),o=new w(n),o.start(),n.hookConsole!==!1&&re(),o}function he(){return o}function pe(n,t,e){o==null||o.log(n,t,e)}var ne={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},M=new Set,H=!0;function fe(n){H=n}function re(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(M.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{H&&ne[r](s.map(ie).join(" ")),a(...s)},M.add(e)}}function ie(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function ye(n,t){o==null||o.report(n,t)}function ve(n){o==null||o.setDeviceName(n)}async function me(){await(o==null?void 0:o.flush())}export{me as flush,he as getClient,re as hookConsole,ce as init,ye as report,fe as setConsoleReport,ve as setDeviceName,pe as ulog,ne as ulogger};
1
+ var X=Object.create;var T=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var z=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var Z=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of F(t))!Q.call(n,i)&&i!==e&&T(n,i,{get:()=>t[i],enumerable:!(r=Y(t,i))||r.enumerable});return n};var ee=(n,t,e)=>(e=n!=null?X(z(n)):{},Z(t||!n||!n.__esModule?T(e,"default",{value:n,enumerable:!0}):e,n));var M=V(p=>{"use strict";Object.defineProperty(p,"__esModule",{value:!0});p.PROTOCOL_LIMITS=p.SIGNATURE_MAX_SKEW_MS=p.SIGNATURE_HEADERS=p.INGEST_PATH=p.LOG_LEVELS=void 0;p.LOG_LEVELS=["debug","info","warn","error","crash"];p.INGEST_PATH="/ingest/v1/logs";p.SIGNATURE_HEADERS={ts:"x-uni-log-ts",nonce:"x-uni-log-nonce",sign:"x-uni-log-sign"};p.SIGNATURE_MAX_SKEW_MS=300*1e3;p.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var H=V(S=>{"use strict";var te=S&&S.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),ne=S&&S.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&te(t,n,e)};Object.defineProperty(S,"__esModule",{value:!0});ne(M(),S)});var u=ee(H());function P(){let n=globalThis.uni;return n!=null?n:null}function re(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function ie(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function C(n){return typeof n=="string"&&n.length>0?n:void 0}function oe(n){let t=d=>{var h;try{return(h=d==null?void 0:d())!=null?h:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),s=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let a=(...d)=>d.map(C).find(Boolean),c=(...d)=>d.map(ie).find(h=>h!==void 0),f=a(r.deviceId,e.deviceId,e.deviceUUID),l=a(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!f||!l?null:{deviceId:f,platform:l,deviceName:a(r.deviceName,e.deviceName),model:a(r.deviceModel,e.deviceModel,e.model),vendor:a(r.deviceBrand,e.deviceBrand,e.brand),os:a(e.osVersion,e.system,r.osVersion,r.system),osVersion:a(e.osVersion,r.osVersion),osName:a(e.osName,r.osName,i.osName),net:a(e.networkType,e.netType),language:a(i.language,e.language),pixelRatio:c(s.pixelRatio,e.pixelRatio),windowWidth:c(s.windowWidth,e.windowWidth),windowHeight:c(s.windowHeight,e.windowHeight),screenWidth:c(s.screenWidth,e.screenWidth),screenHeight:c(s.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=P();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=P())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=P())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return oe(P())}catch{return null}},getCurrentPage(){var n;try{let t=re(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=P(),t=r=>{var i,s;try{let a=r();return a&&(s=(i=C(a.hostVersion))!=null?i:C(a.version))!=null?s:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=P())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=P())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var se=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];function y(n,t){return n>>>t|n<<32-t}function B(n){let t=[];for(let e=0;e<n.length;e++){let r=n.charCodeAt(e);if(r<128)t.push(r);else if(r<2048)t.push(192|r>>6,128|r&63);else if(r>=55296&&r<=56319&&e+1<n.length){let i=n.charCodeAt(e+1);if(i>=56320&&i<=57343){e+=1;let s=65536+(r-55296<<10)+(i-56320);t.push(240|s>>18,128|s>>12&63,128|s>>6&63,128|s&63)}else t.push(239,191,189)}else r>=56320&&r<=57343?t.push(239,191,189):t.push(224|r>>12,128|r>>6&63,128|r&63)}return t}function A(n){let t=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],e=n.length,r=n.slice();for(r.push(128);r.length%64!==56;)r.push(0);let i=e*8,s=Math.floor(i/4294967296),a=i>>>0;r.push(s>>>24&255,s>>>16&255,s>>>8&255,s&255),r.push(a>>>24&255,a>>>16&255,a>>>8&255,a&255);let c=new Array(64);for(let l=0;l<r.length;l+=64){for(let g=0;g<16;g++){let m=l+g*4;c[g]=(r[m]<<24|r[m+1]<<16|r[m+2]<<8|r[m+3])>>>0}for(let g=16;g<64;g++){let m=y(c[g-15],7)^y(c[g-15],18)^c[g-15]>>>3,E=y(c[g-2],17)^y(c[g-2],19)^c[g-2]>>>10;c[g]=c[g-16]+m+c[g-7]+E>>>0}let[d,h,v,I,x,b,w,k]=t;for(let g=0;g<64;g++){let m=y(x,6)^y(x,11)^y(x,25),E=x&b^~x&w,R=k+m+E+se[g]+c[g]>>>0,_=y(d,2)^y(d,13)^y(d,22),N=d&h^d&v^h&v,J=_+N>>>0;k=w,w=b,b=x,x=I+R>>>0,I=v,v=h,h=d,d=R+J>>>0}t[0]=t[0]+d>>>0,t[1]=t[1]+h>>>0,t[2]=t[2]+v>>>0,t[3]=t[3]+I>>>0,t[4]=t[4]+x>>>0,t[5]=t[5]+b>>>0,t[6]=t[6]+w>>>0,t[7]=t[7]+k>>>0}let f=[];for(let l of t)f.push(l>>>24&255,l>>>16&255,l>>>8&255,l&255);return f}function ae(n){let t="";for(let e of n)t+=e.toString(16).padStart(2,"0");return t}function G(n,t){let e=B(n);for(e.length>64&&(e=A(e));e.length<64;)e.push(0);let r=e.map(s=>s^54),i=e.map(s=>s^92);return ae(A(i.concat(A(r.concat(B(t))))))}function W(n=16){let t="";for(let e=0;e<n;e++)t+=Math.floor(Math.random()*256).toString(16).padStart(2,"0");return t}var U="uni-log:pending",q=20,ce="0.2.0";function L(n,t){return n.length>t?n.slice(0,t):n}function D(n){let t=n==null?void 0:n.trim();return t?L(t,128):void 0}function ge(n,t,e){var i;let r={t:Date.now(),lv:n,msg:L(t||"(empty)",u.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=L(e.stack,u.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=L(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var O=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=u.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let s=this.adapter.getCurrentPage();this.logs.push(ge(i,e,{...r,...s?{page:s}:{}})),this.logs.length>=u.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>u.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=L(e,u.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=u.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?L(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var a,c,f,l,d,h,v,I,x,b,w,k,g,m,E,R,_,N;let e=(a=this.deviceInfo)!=null?a:this.deviceInfo=this.adapter.getDeviceInfo(),r=(h=(d=(l=this.deviceNameOverride)!=null?l:D((f=(c=this.options).getDeviceName)==null?void 0:f.call(c)))!=null?d:D(this.options.deviceName))!=null?h:D(e==null?void 0:e.deviceName),i={deviceId:(v=e==null?void 0:e.deviceId)!=null?v:this.fallbackDeviceId(),platform:(I=e==null?void 0:e.platform)!=null?I:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((w=e==null?void 0:e.net)!=null?w:(b=(x=this.options).getNet)!=null&&b.call(x))?{net:(m=e==null?void 0:e.net)!=null?m:(g=(k=this.options).getNet)==null?void 0:g.call(k)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(R=(E=this.options).getUserId)!=null&&R.call(E)?{userId:this.options.getUserId()}:{},...(N=(_=this.options).getNet)!=null&&N.call(_)?{net:this.options.getNet()}:{},sdkVer:ce,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,u.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,u.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=a=>{r||(r=!0,clearTimeout(s),e(a))},s=setTimeout(()=>i(!1),8e3);try{let a=JSON.stringify(t),c={"Content-Type":"application/json"};if(this.options.appSecret){let f=String(Date.now()),l=W(16);c[u.SIGNATURE_HEADERS.ts]=f,c[u.SIGNATURE_HEADERS.nonce]=l,c[u.SIGNATURE_HEADERS.sign]=G(this.options.appSecret,`${f}
2
+ ${l}
3
+ ${a}`)}this.adapter.request({url:`${this.options.serverUrl}${u.INGEST_PATH}`,data:a,header:c,success:f=>i(!!f&&f.statusCode>=200&&f.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(U),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>q&&(r=r.slice(-q));try{this.adapter.setStorage(U,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(U);if(!t)return;this.adapter.setStorage(U,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function Se(n){return o==null||o.stop(),o=new O(n),o.start(),n.hookConsole!==!1&&le(),o}function Pe(){return o}function Ie(n,t,e){o==null||o.log(n,t,e)}var de={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},K=new Set,$=!0;function ke(n){$=n}function le(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(K.has(e))continue;let i=t[e];if(typeof i!="function")continue;let s=i.bind(console);t[e]=(...a)=>{$&&de[r](a.map(ue).join(" ")),s(...a)},K.add(e)}}function ue(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function Ee(n,t){o==null||o.report(n,t)}function Le(n){o==null||o.setDeviceName(n)}async function Re(){await(o==null?void 0:o.flush())}export{Re as flush,Pe as getClient,le as hookConsole,Se as init,Ee as report,ke as setConsoleReport,Le as setDeviceName,Ie as ulog,de as ulogger};
package/package.json CHANGED
@@ -1,54 +1,54 @@
1
1
  {
2
- "name": "uni-log-sdk",
3
- "version": "0.1.1",
4
- "private": false,
5
- "description": "uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件与设备信息批量上报",
6
- "keywords": [
7
- "uni-app",
8
- "uni-app x",
9
- "uni_modules",
10
- "日志",
11
- "埋点",
12
- "sdk"
13
- ],
14
- "license": "UNLICENSED",
15
- "main": "dist/index.js",
16
- "module": "dist/index.mjs",
17
- "types": "dist/index.d.ts",
18
- "exports": {
19
- ".": {
20
- "types": "./dist/index.d.ts",
21
- "import": "./dist/index.mjs",
22
- "require": "./dist/index.js"
23
- }
24
- },
25
- "bin": {
26
- "uni-log": "bin/uni-log.mjs"
27
- },
28
- "files": [
29
- "dist",
30
- "uni_modules",
31
- "bin",
32
- "scripts",
33
- "README.md"
34
- ],
35
- "engines": {
36
- "node": ">=18"
37
- },
38
- "scripts": {
39
- "build": "tsup && node scripts/build-uni-module.mjs",
40
- "test": "vitest run",
41
- "test:watch": "vitest",
42
- "postinstall": "node scripts/postinstall.mjs",
43
- "prepublishOnly": "npm run build && npm test"
44
- },
45
- "publishConfig": {
46
- "registry": "https://registry.npmjs.org/"
47
- },
48
- "devDependencies": {
49
- "@uni-log/shared": "workspace:*",
50
- "tsup": "^8.0.0",
51
- "typescript": "^5.6.0",
52
- "vitest": "^3.0.0"
2
+ "name": "uni-log-sdk",
3
+ "version": "0.2.0",
4
+ "private": false,
5
+ "description": "uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件与设备信息批量上报",
6
+ "keywords": [
7
+ "uni-app",
8
+ "uni-app x",
9
+ "uni_modules",
10
+ "日志",
11
+ "埋点",
12
+ "sdk"
13
+ ],
14
+ "license": "UNLICENSED",
15
+ "main": "dist/index.js",
16
+ "module": "dist/index.mjs",
17
+ "types": "dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js"
53
23
  }
54
- }
24
+ },
25
+ "bin": {
26
+ "uni-log": "bin/uni-log.mjs"
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "uni_modules",
31
+ "bin",
32
+ "scripts/postinstall.mjs",
33
+ "README.md",
34
+ "CHANGELOG.md"
35
+ ],
36
+ "engines": {
37
+ "node": ">=18"
38
+ },
39
+ "publishConfig": {
40
+ "registry": "https://registry.npmjs.org/"
41
+ },
42
+ "devDependencies": {
43
+ "tsup": "^8.0.0",
44
+ "typescript": "^5.6.0",
45
+ "vitest": "^3.0.0",
46
+ "@uni-log/shared": "0.1.0"
47
+ },
48
+ "scripts": {
49
+ "build": "tsup && node scripts/build-uni-module.mjs",
50
+ "test": "vitest run",
51
+ "test:watch": "vitest",
52
+ "postinstall": "node scripts/postinstall.mjs"
53
+ }
54
+ }
@@ -3,7 +3,7 @@
3
3
  * 探测宿主工程里是否只有一个 uni-app 工程,是则把 uni_modules/uni-log 物化过去。
4
4
  *
5
5
  * 设计约束:
6
- * - 只在本包作为依赖安装时执行(包位于 node_modules 下);本仓库源码开发不触发
6
+ * - 只在本包作为依赖安装时执行(包位于 node_modules 下);直接引用源码时不触发
7
7
  * - 找不到工程 / 找到多个工程时不写文件,只提示,避免写错目录
8
8
  * - 目标版本不低于当前包版本时跳过,避免降级覆盖
9
9
  * - UNI_LOG_AUTO_SYNC=0 可关闭
@@ -19,7 +19,7 @@ function info(msg) {
19
19
  console.log(`[uni-log] ${msg}`)
20
20
  }
21
21
 
22
- /** 本仓库源码目录(packages/sdk)不自动同步,避免 pnpm install 时改动 demo 工程 */
22
+ /** 直接以源码方式引用时不自动同步,避免安装依赖时改动业务工程 */
23
23
  if (!pkgRoot.split(sep).includes('node_modules')) process.exit(0)
24
24
 
25
25
  if (process.env.UNI_LOG_AUTO_SYNC === '0') {
@@ -1 +1,3 @@
1
- var W=Object.create;var D=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var J=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of q(t))!$.call(n,i)&&i!==e&&D(n,i,{get:()=>t[i],enumerable:!(r=G(t,i))||r.enumerable});return n};var Y=(n,t,e)=>(e=n!=null?W(K(n)):{},J(t||!n||!n.__esModule?D(e,"default",{value:n,enumerable:!0}):e,n));var A=V(u=>{"use strict";Object.defineProperty(u,"__esModule",{value:!0});u.PROTOCOL_LIMITS=u.INGEST_PATH=u.LOG_LEVELS=void 0;u.LOG_LEVELS=["debug","info","warn","error","crash"];u.INGEST_PATH="/ingest/v1/logs";u.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var T=V(h=>{"use strict";var F=h&&h.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),X=h&&h.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&F(t,n,e)};Object.defineProperty(h,"__esModule",{value:!0});X(A(),h)});var g=Y(T());function p(){let n=globalThis.uni;return n!=null?n:null}function z(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function Q(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function P(n){return typeof n=="string"&&n.length>0?n:void 0}function Z(n){let t=l=>{var c;try{return(c=l==null?void 0:l())!=null?c:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(P).find(Boolean),d=(...l)=>l.map(Q).find(c=>c!==void 0),y=s(r.deviceId,e.deviceId,e.deviceUUID),v=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!y||!v?null:{deviceId:y,platform:v,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=p();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=p())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=p())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return Z(p())}catch{return null}},getCurrentPage(){var n;try{let t=z(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=p(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=P(s.hostVersion))!=null?i:P(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=p())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=p())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var m="uni-log:pending",B=20,ee="0.1.1";function f(n,t){return n.length>t?n.slice(0,t):n}function S(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function te(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var w=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(te(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,y,v,l,c,b,k,I,L,E,R,_,N,O,U,x,C;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(c=(l=(v=this.deviceNameOverride)!=null?v:S((y=(d=this.options).getDeviceName)==null?void 0:y.call(d)))!=null?l:S(this.options.deviceName))!=null?c:S(e==null?void 0:e.deviceName),i={deviceId:(b=e==null?void 0:e.deviceId)!=null?b:this.fallbackDeviceId(),platform:(k=e==null?void 0:e.platform)!=null?k:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((E=e==null?void 0:e.net)!=null?E:(L=(I=this.options).getNet)!=null&&L.call(I))?{net:(N=e==null?void 0:e.net)!=null?N:(_=(R=this.options).getNet)==null?void 0:_.call(R)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(U=(O=this.options).getUserId)!=null&&U.call(O)?{userId:this.options.getUserId()}:{},...(C=(x=this.options).getNet)!=null&&C.call(x)?{net:this.options.getNet()}:{},sdkVer:ee,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(m),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>B&&(r=r.slice(-B));try{this.adapter.setStorage(m,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(m);if(!t)return;this.adapter.setStorage(m,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function ce(n){return o==null||o.stop(),o=new w(n),o.start(),n.hookConsole!==!1&&re(),o}function he(){return o}function pe(n,t,e){o==null||o.log(n,t,e)}var ne={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},M=new Set,H=!0;function fe(n){H=n}function re(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(M.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{H&&ne[r](s.map(ie).join(" ")),a(...s)},M.add(e)}}function ie(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function ye(n,t){o==null||o.report(n,t)}function ve(n){o==null||o.setDeviceName(n)}async function me(){await(o==null?void 0:o.flush())}export{me as flush,he as getClient,re as hookConsole,ce as init,ye as report,fe as setConsoleReport,ve as setDeviceName,pe as ulog,ne as ulogger};
1
+ var X=Object.create;var T=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var z=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var Z=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of F(t))!Q.call(n,i)&&i!==e&&T(n,i,{get:()=>t[i],enumerable:!(r=Y(t,i))||r.enumerable});return n};var ee=(n,t,e)=>(e=n!=null?X(z(n)):{},Z(t||!n||!n.__esModule?T(e,"default",{value:n,enumerable:!0}):e,n));var M=V(p=>{"use strict";Object.defineProperty(p,"__esModule",{value:!0});p.PROTOCOL_LIMITS=p.SIGNATURE_MAX_SKEW_MS=p.SIGNATURE_HEADERS=p.INGEST_PATH=p.LOG_LEVELS=void 0;p.LOG_LEVELS=["debug","info","warn","error","crash"];p.INGEST_PATH="/ingest/v1/logs";p.SIGNATURE_HEADERS={ts:"x-uni-log-ts",nonce:"x-uni-log-nonce",sign:"x-uni-log-sign"};p.SIGNATURE_MAX_SKEW_MS=300*1e3;p.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var H=V(S=>{"use strict";var te=S&&S.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),ne=S&&S.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&te(t,n,e)};Object.defineProperty(S,"__esModule",{value:!0});ne(M(),S)});var u=ee(H());function P(){let n=globalThis.uni;return n!=null?n:null}function re(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function ie(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function C(n){return typeof n=="string"&&n.length>0?n:void 0}function oe(n){let t=d=>{var h;try{return(h=d==null?void 0:d())!=null?h:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),s=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let a=(...d)=>d.map(C).find(Boolean),c=(...d)=>d.map(ie).find(h=>h!==void 0),f=a(r.deviceId,e.deviceId,e.deviceUUID),l=a(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!f||!l?null:{deviceId:f,platform:l,deviceName:a(r.deviceName,e.deviceName),model:a(r.deviceModel,e.deviceModel,e.model),vendor:a(r.deviceBrand,e.deviceBrand,e.brand),os:a(e.osVersion,e.system,r.osVersion,r.system),osVersion:a(e.osVersion,r.osVersion),osName:a(e.osName,r.osName,i.osName),net:a(e.networkType,e.netType),language:a(i.language,e.language),pixelRatio:c(s.pixelRatio,e.pixelRatio),windowWidth:c(s.windowWidth,e.windowWidth),windowHeight:c(s.windowHeight,e.windowHeight),screenWidth:c(s.screenWidth,e.screenWidth),screenHeight:c(s.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=P();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=P())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=P())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return oe(P())}catch{return null}},getCurrentPage(){var n;try{let t=re(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=P(),t=r=>{var i,s;try{let a=r();return a&&(s=(i=C(a.hostVersion))!=null?i:C(a.version))!=null?s:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=P())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=P())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var se=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];function y(n,t){return n>>>t|n<<32-t}function B(n){let t=[];for(let e=0;e<n.length;e++){let r=n.charCodeAt(e);if(r<128)t.push(r);else if(r<2048)t.push(192|r>>6,128|r&63);else if(r>=55296&&r<=56319&&e+1<n.length){let i=n.charCodeAt(e+1);if(i>=56320&&i<=57343){e+=1;let s=65536+(r-55296<<10)+(i-56320);t.push(240|s>>18,128|s>>12&63,128|s>>6&63,128|s&63)}else t.push(239,191,189)}else r>=56320&&r<=57343?t.push(239,191,189):t.push(224|r>>12,128|r>>6&63,128|r&63)}return t}function A(n){let t=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],e=n.length,r=n.slice();for(r.push(128);r.length%64!==56;)r.push(0);let i=e*8,s=Math.floor(i/4294967296),a=i>>>0;r.push(s>>>24&255,s>>>16&255,s>>>8&255,s&255),r.push(a>>>24&255,a>>>16&255,a>>>8&255,a&255);let c=new Array(64);for(let l=0;l<r.length;l+=64){for(let g=0;g<16;g++){let m=l+g*4;c[g]=(r[m]<<24|r[m+1]<<16|r[m+2]<<8|r[m+3])>>>0}for(let g=16;g<64;g++){let m=y(c[g-15],7)^y(c[g-15],18)^c[g-15]>>>3,E=y(c[g-2],17)^y(c[g-2],19)^c[g-2]>>>10;c[g]=c[g-16]+m+c[g-7]+E>>>0}let[d,h,v,I,x,b,w,k]=t;for(let g=0;g<64;g++){let m=y(x,6)^y(x,11)^y(x,25),E=x&b^~x&w,R=k+m+E+se[g]+c[g]>>>0,_=y(d,2)^y(d,13)^y(d,22),N=d&h^d&v^h&v,J=_+N>>>0;k=w,w=b,b=x,x=I+R>>>0,I=v,v=h,h=d,d=R+J>>>0}t[0]=t[0]+d>>>0,t[1]=t[1]+h>>>0,t[2]=t[2]+v>>>0,t[3]=t[3]+I>>>0,t[4]=t[4]+x>>>0,t[5]=t[5]+b>>>0,t[6]=t[6]+w>>>0,t[7]=t[7]+k>>>0}let f=[];for(let l of t)f.push(l>>>24&255,l>>>16&255,l>>>8&255,l&255);return f}function ae(n){let t="";for(let e of n)t+=e.toString(16).padStart(2,"0");return t}function G(n,t){let e=B(n);for(e.length>64&&(e=A(e));e.length<64;)e.push(0);let r=e.map(s=>s^54),i=e.map(s=>s^92);return ae(A(i.concat(A(r.concat(B(t))))))}function W(n=16){let t="";for(let e=0;e<n;e++)t+=Math.floor(Math.random()*256).toString(16).padStart(2,"0");return t}var U="uni-log:pending",q=20,ce="0.2.0";function L(n,t){return n.length>t?n.slice(0,t):n}function D(n){let t=n==null?void 0:n.trim();return t?L(t,128):void 0}function ge(n,t,e){var i;let r={t:Date.now(),lv:n,msg:L(t||"(empty)",u.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=L(e.stack,u.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=L(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var O=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=u.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let s=this.adapter.getCurrentPage();this.logs.push(ge(i,e,{...r,...s?{page:s}:{}})),this.logs.length>=u.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>u.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=L(e,u.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=u.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?L(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var a,c,f,l,d,h,v,I,x,b,w,k,g,m,E,R,_,N;let e=(a=this.deviceInfo)!=null?a:this.deviceInfo=this.adapter.getDeviceInfo(),r=(h=(d=(l=this.deviceNameOverride)!=null?l:D((f=(c=this.options).getDeviceName)==null?void 0:f.call(c)))!=null?d:D(this.options.deviceName))!=null?h:D(e==null?void 0:e.deviceName),i={deviceId:(v=e==null?void 0:e.deviceId)!=null?v:this.fallbackDeviceId(),platform:(I=e==null?void 0:e.platform)!=null?I:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((w=e==null?void 0:e.net)!=null?w:(b=(x=this.options).getNet)!=null&&b.call(x))?{net:(m=e==null?void 0:e.net)!=null?m:(g=(k=this.options).getNet)==null?void 0:g.call(k)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(R=(E=this.options).getUserId)!=null&&R.call(E)?{userId:this.options.getUserId()}:{},...(N=(_=this.options).getNet)!=null&&N.call(_)?{net:this.options.getNet()}:{},sdkVer:ce,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,u.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,u.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=a=>{r||(r=!0,clearTimeout(s),e(a))},s=setTimeout(()=>i(!1),8e3);try{let a=JSON.stringify(t),c={"Content-Type":"application/json"};if(this.options.appSecret){let f=String(Date.now()),l=W(16);c[u.SIGNATURE_HEADERS.ts]=f,c[u.SIGNATURE_HEADERS.nonce]=l,c[u.SIGNATURE_HEADERS.sign]=G(this.options.appSecret,`${f}
2
+ ${l}
3
+ ${a}`)}this.adapter.request({url:`${this.options.serverUrl}${u.INGEST_PATH}`,data:a,header:c,success:f=>i(!!f&&f.statusCode>=200&&f.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(U),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>q&&(r=r.slice(-q));try{this.adapter.setStorage(U,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(U);if(!t)return;this.adapter.setStorage(U,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function Se(n){return o==null||o.stop(),o=new O(n),o.start(),n.hookConsole!==!1&&le(),o}function Pe(){return o}function Ie(n,t,e){o==null||o.log(n,t,e)}var de={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},K=new Set,$=!0;function ke(n){$=n}function le(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(K.has(e))continue;let i=t[e];if(typeof i!="function")continue;let s=i.bind(console);t[e]=(...a)=>{$&&de[r](a.map(ue).join(" ")),s(...a)},K.add(e)}}function ue(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function Ee(n,t){o==null||o.report(n,t)}function Le(n){o==null||o.setDeviceName(n)}async function Re(){await(o==null?void 0:o.flush())}export{Re as flush,Pe as getClient,le as hookConsole,Se as init,Ee as report,ke as setConsoleReport,Le as setDeviceName,Ie as ulog,de as ulogger};
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "id": "uni-log",
3
3
  "displayName": "uni-log 日志上报",
4
- "version": "0.1.1",
4
+ "version": "0.2.0",
5
5
  "description": "uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件与设备信息批量上报",
6
6
  "keywords": [
7
7
  "uni-app",
@@ -2,7 +2,7 @@
2
2
 
3
3
  uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件、设备与会话心跳,批量上报到自建 uni-log 服务端。
4
4
 
5
- - 纯 JS、无外部运行时依赖(shared 协议已内联),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序中运行
5
+ - 纯 JS、无外部运行时依赖(协议代码已内置),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序中运行
6
6
  - 默认 10s 批量上报,满 50 条立即发送;发送失败写入本地暂存,冷启动自动续传
7
7
  - 提供 uni_modules 形态,HBuilderX 工程可直接引用
8
8
 
@@ -64,18 +64,23 @@ import { init } from '@/uni_modules/uni-log'
64
64
  init({
65
65
  serverUrl: 'https://log.example.com', // 不带尾斜杠;App 真机不要用 localhost
66
66
  appKey: '<看板「应用管理」分配>',
67
+ appSecret: '<看板「应用管理」分配,与 appKey 成对>',
67
68
  appVer: '1.0.0',
68
69
  release: 'release',
69
70
  channel: 'default',
70
71
  })
71
72
  ```
72
73
 
74
+ > 服务端默认强制 HMAC-SHA256 签名。`appSecret` 只应通过构建环境变量/CI 注入,
75
+ > 不要提交进各项目仓库;泄露后在看板「重置凭据」即可作废旧值。
76
+
73
77
  ### 选项
74
78
 
75
79
  | 选项 | 类型 | 默认 | 说明 |
76
80
  | --- | --- | --- | --- |
77
81
  | `serverUrl` | string | 必填 | 服务端地址,上报路径为 `/ingest/v1/logs` |
78
82
  | `appKey` | string | 必填 | 应用 Key,看板「应用管理」获取 |
83
+ | `appSecret` | string | - | 签名密钥;服务端开启强制签名时必填 |
79
84
  | `appVer` | string | - | 业务应用版本号 |
80
85
  | `release` | string | - | 发布渠道/环境标识 |
81
86
  | `channel` | string | - | 渠道 code / 小程序场景值 |
@@ -115,8 +120,9 @@ await flush() // 应用退后台/页面隐藏时可主动上报
115
120
  - `serverUrl` 不要带尾斜杠;App 真机需用局域网 IP 或 HTTPS 域名,Android 9+ 默认禁止明文 HTTP。
116
121
  - 小程序需要将上报域名加入 request 合法域名(开发期可在开发者工具勾选「不校验合法域名」)。
117
122
  - 自动可用的采集:设备信息、会话心跳、JS 错误;页面 PV 与自定义事件需手动调用。
118
- - `console` 全量上报目前依赖运行时劫持;编译期 `transform-console` 插件尚未发布。
123
+ - `console` 全量上报通过运行时劫持实现,初始化时自动开启,可用 `hookConsole: false` 关闭。
119
124
  - 上报内容会经过服务端协议校验:单批 ≤50 条、`msg` ≤4000 字符、请求体 ≤512KB,超长由 SDK 自动截断。
125
+ - 各版本的新增、修复与**升级指南(含破坏性变更)**见 [CHANGELOG.md](./CHANGELOG.md)。
120
126
 
121
127
  ## License
122
128
 
@@ -1,32 +0,0 @@
1
- /**
2
- * 由 tsup 产物生成 uni_modules 形态:
3
- * dist/index.mjs -> uni_modules/uni-log/index.js
4
- * 同时写入 uni_modules 插件清单(HBuilderX 需要 id/displayName/version)。
5
- * 在 packages/sdk 下执行 `npm run build` 时自动运行。
6
- */
7
- import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
8
- import { dirname, join } from 'node:path'
9
- import { fileURLToPath } from 'node:url'
10
-
11
- const pkgRoot = join(dirname(fileURLToPath(import.meta.url)), '..')
12
- const pkg = JSON.parse(readFileSync(join(pkgRoot, 'package.json'), 'utf8'))
13
- const pluginDir = join(pkgRoot, 'uni_modules', 'uni-log')
14
-
15
- rmSync(pluginDir, { recursive: true, force: true })
16
- mkdirSync(pluginDir, { recursive: true })
17
- copyFileSync(join(pkgRoot, 'dist', 'index.mjs'), join(pluginDir, 'index.js'))
18
-
19
- const pluginPkg = {
20
- id: 'uni-log',
21
- displayName: 'uni-log 日志上报',
22
- version: pkg.version,
23
- description: pkg.description,
24
- keywords: pkg.keywords ?? [],
25
- }
26
- writeFileSync(join(pluginDir, 'package.json'), `${JSON.stringify(pluginPkg, null, 4)}\n`)
27
-
28
- // HBuilderX 插件详情页展示 readme.md(小写文件名是 uni_modules 约定)
29
- const readme = join(pkgRoot, 'README.md')
30
- if (existsSync(readme)) copyFileSync(readme, join(pluginDir, 'readme.md'))
31
-
32
- console.log(`[uni-log] uni_modules 已生成:${pluginDir}(v${pkg.version})`)