uni-log-sdk 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,246 +1,16 @@
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
- }
1
+ import { P as PlatformAdapter, a as UniLogClient, U as UniLogOptions, L as LogLevel } from './client-Cup5Zw6E.js';
2
+ export { E as EventEntry, b as LogEntry, R as ReportPayload } from './client-Cup5Zw6E.js';
3
+
4
+ declare function createBrowserAdapter(): PlatformAdapter;
127
5
 
128
6
  /**
129
- * uni API 抽象层。
130
- * 运行时读全局 uni 对象(uni-app / uni-app x 都注入全局 uni);
131
- * 单测通过向 globalThis 注入 mock uni 来驱动真实 adapter,覆盖平台 API 差异。
7
+ * 运行环境探测:有 uni 全局就是 uniapp(uni-app / uni-app x),走 uni 适配器;
8
+ * 没有就是任意 TS Web 应用,走浏览器适配器。
9
+ * RN / Expo 不参与探测——经 `uni-log-sdk/react-native` 子路径的 initRN 显式注入
10
+ * (react-native 相关依赖由宿主 bundler 解析,核心包不引入)。
132
11
  */
133
12
 
134
- interface UniRequestOptions {
135
- url: string;
136
- data: unknown;
137
- header?: Record<string, string>;
138
- success?: (res: {
139
- statusCode: number;
140
- data?: unknown;
141
- }) => void;
142
- fail?: (err: {
143
- errMsg?: string;
144
- }) => void;
145
- }
146
- interface UniAdapter {
147
- request(opts: UniRequestOptions): void;
148
- getStorage(key: string): string | null;
149
- setStorage(key: string, value: string): void;
150
- /** 平台尽力采集:取不到返回 null,调用方自行兜底 */
151
- getDeviceInfo(): DeviceInfo | null;
152
- getCurrentPage(): string | null;
153
- /** 宿主平台版本号(微信/支付宝等 App 版本),取不到返回 null */
154
- getPlatformVersion(): string | null;
155
- onError(cb: (err: unknown) => void): void;
156
- onUnhandledRejection(cb: (err: unknown) => void): void;
157
- }
158
-
159
- interface UniLogOptions {
160
- /** 服务端地址,如 http://localhost:3100 */
161
- serverUrl: string;
162
- /** 看板「应用管理」分配的 appKey */
163
- appKey: string;
164
- /**
165
- * 应用签名密钥(与 appKey 一起下发)。
166
- * 配置后每个上报请求都会带 HMAC-SHA256 签名,服务端可拒绝伪造/重放请求。
167
- */
168
- appSecret?: string;
169
- appVer?: string;
170
- release?: string;
171
- /** 渠道/场景值 code,对应官方 channel(小程序可直接传场景值) */
172
- channel?: string;
173
- /** 设备名称(给人看的,如「Jane 的 iPhone」);不传则服务端按「品牌 + 型号」兜底 */
174
- deviceName?: string;
175
- /** 动态设备名称,优先级高于 deviceName(如登录后用昵称命名设备) */
176
- getDeviceName?: () => string | undefined;
177
- getUserId?: () => string | undefined;
178
- getNet?: () => string | undefined;
179
- /** 定时上报间隔毫秒,默认 10000(对标 uni-stat reportInterval) */
180
- flushInterval?: number;
181
- /** 自动捕获 uni.onError / onUnhandledRejection,默认 true */
182
- collectErrors?: boolean;
183
- /** 运行时劫持 console.* 转发到 ulogger,默认 true;HBuilderX 工程等无编译期插件场景用它 */
184
- hookConsole?: boolean;
185
- debug?: boolean;
186
- /** 测试注入;默认使用全局 uni 适配器 */
187
- adapter?: UniAdapter;
188
- }
189
- declare class UniLogClient {
190
- /** init 后才允许采集上报 */
191
- ready: boolean;
192
- readonly options: UniLogOptions;
193
- private readonly adapter;
194
- private readonly sessionId;
195
- private logs;
196
- private events;
197
- private timer;
198
- private flushingPromise;
199
- /** 会话启动时间(对标 uni-stat-session-logs.create_time) */
200
- private readonly launchTime;
201
- /** 本次会话浏览页面数(对标 page_count) */
202
- private pageCount;
203
- /** 上一次观察到的页面,用于把页面切换计为一次访问 */
204
- private lastPage;
205
- /** 设备信息只采一次,避免每批都调平台 API */
206
- private deviceInfo;
207
- /** 运行时覆盖的设备名称,优先于 options 与平台采集值 */
208
- private deviceNameOverride;
209
- constructor(options: UniLogOptions, adapter?: UniAdapter);
210
- /** 启动定时器、续传失败暂存、按需挂错误钩子 */
211
- start(): void;
212
- stop(): void;
213
- /** 手动日志入口;transform-console 会把 console.* 编译期替换为这里的调用 */
214
- log(lv: LogLevel, msg: string, opts?: {
215
- page?: string;
216
- stack?: string;
217
- extra?: Record<string, unknown>;
218
- tags?: string[];
219
- }): void;
220
- /** 自定义事件(对标 uni.report) */
221
- report(eventKey: string, val?: string): void;
222
- /**
223
- * 显式上报一次页面访问(对标 uni-stat page-log)。
224
- * 页面切换也能被自动采样到,此方法用于 onPageShow / onLoad 里精确埋点。
225
- */
226
- pageView(path?: string): void;
227
- /** 运行时设置设备名称(登录后用昵称命名设备等);传空串恢复自动兜底 */
228
- setDeviceName(name: string): void;
229
- /** 观察当前页面,页面变化即计一次访问 */
230
- private samplePage;
231
- /** 立即上报积压内容;失败的部分已暂存,等下次 flush */
232
- flush(): Promise<void>;
233
- private buildPayload;
234
- private fallbackDeviceId;
235
- /** 发送单个报文。只负责发送,失败时由调用方决定是否暂存。 */
236
- private send;
237
- /** 安全序列化:去掉循环引用、把 BigInt 转字符串,避免 `JSON.stringify` 抛错 */
238
- private safeStringify;
239
- private persistPending;
240
- /** 冷启动续传:读出暂存报文逐条重发,失败的那条起全部塞回暂存 */
241
- private retryPending;
242
- private installErrorHooks;
243
- }
13
+ declare function detectAdapter(): PlatformAdapter;
244
14
 
245
15
  /**
246
16
  * uni-log SDK 公共入口。
@@ -288,4 +58,4 @@ declare function setDeviceName(name: string): void;
288
58
  /** 立即上报积压内容(页面隐藏/应用退后台时可调用) */
289
59
  declare function flush(): Promise<void>;
290
60
 
291
- export { type EventEntry, type LogEntry, type LogLevel, type ReportPayload, type UniAdapter, UniLogClient, type UniLogOptions, flush, getClient, hookConsole, init, report, setConsoleReport, setDeviceName, ulog, ulogger };
61
+ export { LogLevel, PlatformAdapter, UniLogClient, UniLogOptions, createBrowserAdapter, detectAdapter, flush, getClient, hookConsole, init, report, setConsoleReport, setDeviceName, ulog, ulogger };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
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});
1
+ "use strict";var oe=Object.create;var U=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var ae=Object.getPrototypeOf,de=Object.prototype.hasOwnProperty;var q=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports),ce=(n,t)=>{for(var e in t)U(n,e,{get:t[e],enumerable:!0})},G=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of se(t))!de.call(n,o)&&o!==e&&U(n,o,{get:()=>t[o],enumerable:!(r=ie(t,o))||r.enumerable});return n};var le=(n,t,e)=>(e=n!=null?oe(ae(n)):{},G(t||!n||!n.__esModule?U(e,"default",{value:n,enumerable:!0}):e,n)),ge=n=>G(U({},"__esModule",{value:!0}),n);var X=q(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 Y=q(P=>{"use strict";var me=P&&P.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var o=Object.getOwnPropertyDescriptor(t,e);(!o||("get"in o?!t.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,o)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),xe=P&&P.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&me(t,n,e)};Object.defineProperty(P,"__esModule",{value:!0});xe(X(),P)});var Ne={};ce(Ne,{createBrowserAdapter:()=>_,detectAdapter:()=>O,flush:()=>Le,getClient:()=>Pe,hookConsole:()=>ne,init:()=>Se,report:()=>Ee,setConsoleReport:()=>Re,setDeviceName:()=>Ae,ulog:()=>Ie,ulogger:()=>ee});module.exports=ge(Ne);function S(){let n=globalThis.uni;return n!=null?n:null}function ue(){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 fe(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function V(n){return typeof n=="string"&&n.length>0?n:void 0}function he(n){let t=c=>{var h;try{return(h=c==null?void 0:c())!=null?h:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),o=t(n==null?void 0:n.getAppBaseInfo),i=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let a=(...c)=>c.map(V).find(Boolean),d=(...c)=>c.map(fe).find(h=>h!==void 0),f=a(r.deviceId,e.deviceId,e.deviceUUID),g=a(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!f||!g?null:{deviceId:f,platform:g,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,o.osName),net:a(e.networkType,e.netType),language:a(o.language,e.language),pixelRatio:d(i.pixelRatio,e.pixelRatio),windowWidth:d(i.windowWidth,e.windowWidth),windowHeight:d(i.windowHeight,e.windowHeight),screenWidth:d(i.screenWidth,e.screenWidth),screenHeight:d(i.screenHeight,e.screenHeight)}}function N(){return{request(n){var e;let t=S();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}return t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=S())==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=S())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return he(S())}catch{return null}},getCurrentPage(){var n;try{let t=ue(),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=S(),t=r=>{var o,i;try{let a=r();return a&&(i=(o=V(a.hostVersion))!=null?o:V(a.version))!=null?i: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=S())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=S())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}function W(n){let t=new AbortController;return fetch(n.url,{method:"POST",headers:n.header,body:typeof n.data=="string"?n.data:JSON.stringify(n.data),signal:t.signal}).then(async e=>{var i;let r=await e.text(),o;try{o=JSON.parse(r)}catch{o=r}(i=n.success)==null||i.call(n,{statusCode:e.status,data:o})}).catch(()=>{var e;return(e=n.fail)==null?void 0:e.call(n,{errMsg:"fetch failed"})}),{abort:()=>t.abort()}}var K="uni-log:web-did",M;function $(n){try{return window.localStorage.getItem(n)}catch{return null}}function J(n,t){try{window.localStorage.setItem(n,t)}catch{}}function pe(){var e;let n=(e=$(K))!=null?e:M;if(n)return M=n,n;let t=`web-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`;return M=t,J(K,t),t}function _(){return{request:W,getStorage:n=>$(n),setStorage:(n,t)=>J(n,t),getDeviceInfo(){return{deviceId:pe(),platform:"web",osName:"web",language:typeof navigator!="undefined"?navigator.language:void 0,screenWidth:typeof screen!="undefined"&&Number(screen.width)||void 0,screenHeight:typeof screen!="undefined"&&Number(screen.height)||void 0}},getCurrentPage:()=>typeof location!="undefined"?location.pathname:null,getPlatformVersion:()=>null,onError(n){typeof window!="undefined"&&window.addEventListener("error",t=>{var e,r;return n((r=(e=t.error)!=null?e:t.message)!=null?r:"window error")})},onUnhandledRejection(n){typeof window!="undefined"&&window.addEventListener("unhandledrejection",t=>{var e;return n((e=t.reason)!=null?e:t)})}}}function O(){let n=globalThis;return typeof n!="undefined"&&n.uni?N():_()}var u=le(Y());var ye=[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 F(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 o=n.charCodeAt(e+1);if(o>=56320&&o<=57343){e+=1;let i=65536+(r-55296<<10)+(o-56320);t.push(240|i>>18,128|i>>12&63,128|i>>6&63,128|i&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 B(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 o=e*8,i=Math.floor(o/4294967296),a=o>>>0;r.push(i>>>24&255,i>>>16&255,i>>>8&255,i&255),r.push(a>>>24&255,a>>>16&255,a>>>8&255,a&255);let d=new Array(64);for(let g=0;g<r.length;g+=64){for(let l=0;l<16;l++){let x=g+l*4;d[l]=(r[x]<<24|r[x+1]<<16|r[x+2]<<8|r[x+3])>>>0}for(let l=16;l<64;l++){let x=y(d[l-15],7)^y(d[l-15],18)^d[l-15]>>>3,k=y(d[l-2],17)^y(d[l-2],19)^d[l-2]>>>10;d[l]=d[l-16]+x+d[l-7]+k>>>0}let[c,h,b,I,m,v,w,R]=t;for(let l=0;l<64;l++){let x=y(m,6)^y(m,11)^y(m,25),k=m&v^~m&w,L=R+x+k+ye[l]+d[l]>>>0,C=y(c,2)^y(c,13)^y(c,22),D=c&h^c&b^h&b,re=C+D>>>0;R=w,w=v,v=m,m=I+L>>>0,I=b,b=h,h=c,c=L+re>>>0}t[0]=t[0]+c>>>0,t[1]=t[1]+h>>>0,t[2]=t[2]+b>>>0,t[3]=t[3]+I>>>0,t[4]=t[4]+m>>>0,t[5]=t[5]+v>>>0,t[6]=t[6]+w>>>0,t[7]=t[7]+R>>>0}let f=[];for(let g of t)f.push(g>>>24&255,g>>>16&255,g>>>8&255,g&255);return f}function be(n){let t="";for(let e of n)t+=e.toString(16).padStart(2,"0");return t}function z(n,t){let e=F(n);for(e.length>64&&(e=B(e));e.length<64;)e.push(0);let r=e.map(i=>i^54),o=e.map(i=>i^92);return be(B(o.concat(B(r.concat(F(t))))))}function H(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 E="uni-log:pending",Q=20,ve="0.2.1";function A(n,t){return n.length>t?n.slice(0,t):n}function j(n){let t=n==null?void 0:n.trim();return t?A(t,128):void 0}function we(n,t,e){var o;let r={t:Date.now(),lv:n,msg:A(t||"(empty)",u.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=A(e.stack,u.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=A(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(o=e==null?void 0:e.tags)!=null&&o.length&&(r.tags=e.tags),r}var T=class{constructor(t,e=N()){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,r;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),((r=this.adapter.ready)!=null?r:Promise.resolve()).then(()=>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 o=u.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let i=this.adapter.getCurrentPage();this.logs.push(we(o,e,{...r,...i?{page:i}:{}})),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=A(e,u.PROTOCOL_LIMITS.maxEventValLength));let o=this.adapter.getCurrentPage();o&&(r.page=o),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?A(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(o){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",o)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var a,d,f,g,c,h,b,I,m,v,w,R,l,x,k,L,C,D;let e=(a=this.deviceInfo)!=null?a:this.deviceInfo=this.adapter.getDeviceInfo(),r=(h=(c=(g=this.deviceNameOverride)!=null?g:j((f=(d=this.options).getDeviceName)==null?void 0:f.call(d)))!=null?c:j(this.options.deviceName))!=null?h:j(e==null?void 0:e.deviceName),o={deviceId:(b=e==null?void 0:e.deviceId)!=null?b: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:(v=(m=this.options).getNet)!=null&&v.call(m))?{net:(x=e==null?void 0:e.net)!=null?x:(l=(R=this.options).getNet)==null?void 0:l.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,batchId:H(12),...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(L=(k=this.options).getUserId)!=null&&L.call(k)?{userId:this.options.getUserId()}:{},...(D=(C=this.options).getNet)!=null&&D.call(C)?{net:this.options.getNet()}:{},sdkVer:ve,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:o,...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=>{var d;let r=!1,o,i=f=>{r||(r=!0,clearTimeout(a),e(f))},a=setTimeout(()=>{var f;try{(f=o==null?void 0:o.abort)==null||f.call(o)}catch{}i(!1)},8e3);try{let f=JSON.stringify(t),g={"Content-Type":"application/json"};if(this.options.appSecret){let c=String(Date.now()),h=H(16);g[u.SIGNATURE_HEADERS.ts]=c,g[u.SIGNATURE_HEADERS.nonce]=h,g[u.SIGNATURE_HEADERS.sign]=z(this.options.appSecret,`${c}
2
+ ${h}
3
+ ${f}`)}o=(d=this.adapter.request({url:`${this.options.serverUrl}${u.INGEST_PATH}`,data:f,header:g,success:c=>i(!!c&&c.statusCode>=200&&c.statusCode<300),fail:()=>i(!1)}))!=null?d:void 0}catch{i(!1)}})}safeStringify(t){var o;let e=new Set,r=i=>{if(typeof i=="bigint")return i.toString();if(i!==null&&typeof i=="object"){if(e.has(i))return"[Circular]";e.add(i);try{if(Array.isArray(i))return i.map(r);let a={};for(let d of Object.keys(i))a[d]=r(i[d]);return a}finally{e.delete(i)}}return i};return(o=JSON.stringify(r(t)))!=null?o:""}persistPending(t){let e=this.adapter.getStorage(E),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(E,this.safeStringify(r))}catch(o){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",o)}}async retryPending(){let t=this.adapter.getStorage(E);if(!t)return;let e;try{e=JSON.parse(t)}catch{this.adapter.setStorage(E,""),this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u6570\u636E\u635F\u574F\uFF0C\u5DF2\u4E22\u5F03");return}if(!Array.isArray(e)||e.length===0){this.adapter.setStorage(E,"");return}this.adapter.setStorage(E,"");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 o;let e=t,r=(o=e==null?void 0:e.reason)!=null?o:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var s=null;function Se(n){var t;return s==null||s.stop(),s=new T(n,(t=n.adapter)!=null?t:O()),s.start(),n.hookConsole!==!1&&ne(),s}function Pe(){return s}function Ie(n,t,e){s==null||s.log(n,t,e)}var ee={debug:(n,t)=>s==null?void 0:s.log("debug",n,t),info:(n,t)=>s==null?void 0:s.log("info",n,t),warn:(n,t)=>s==null?void 0:s.log("warn",n,t),error:(n,t)=>s==null?void 0:s.log("error",n,t)},Z=new Set,te=!0;function Re(n){te=n}function ne(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(Z.has(e))continue;let o=t[e];if(typeof o!="function")continue;let i=o.bind(console);t[e]=(...a)=>{te&&ee[r](a.map(ke).join(" ")),i(...a)},Z.add(e)}}function ke(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){s==null||s.report(n,t)}function Ae(n){s==null||s.setDeviceName(n)}async function Le(){await(s==null?void 0:s.flush())}0&&(module.exports={createBrowserAdapter,detectAdapter,flush,getClient,hookConsole,init,report,setConsoleReport,setDeviceName,ulog,ulogger});
package/dist/index.mjs CHANGED
@@ -1,3 +1 @@
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
+ import{b as a,c as b,d as c,e as d,f as e,g as f,h as g,i as h,j as i,k as j,l as k}from"./chunk-SOQF2GQO.mjs";export{a as createBrowserAdapter,b as detectAdapter,k as flush,d as getClient,h as hookConsole,c as init,i as report,g as setConsoleReport,j as setDeviceName,e as ulog,f as ulogger};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uni-log-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "private": false,
5
5
  "description": "uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件与设备信息批量上报",
6
6
  "keywords": [
@@ -20,6 +20,11 @@
20
20
  "types": "./dist/index.d.ts",
21
21
  "import": "./dist/index.mjs",
22
22
  "require": "./dist/index.js"
23
+ },
24
+ "./react-native": {
25
+ "types": "./dist/adapters/react-native.d.ts",
26
+ "import": "./dist/adapters/react-native.mjs",
27
+ "require": "./dist/adapters/react-native.js"
23
28
  }
24
29
  },
25
30
  "bin": {
@@ -45,6 +50,18 @@
45
50
  "vitest": "^3.0.0",
46
51
  "@uni-log/shared": "0.1.0"
47
52
  },
53
+ "peerDependencies": {
54
+ "@react-native-async-storage/async-storage": ">=1.20.0",
55
+ "react-native": ">=0.60.0"
56
+ },
57
+ "peerDependenciesMeta": {
58
+ "@react-native-async-storage/async-storage": {
59
+ "optional": true
60
+ },
61
+ "react-native": {
62
+ "optional": true
63
+ }
64
+ },
48
65
  "scripts": {
49
66
  "build": "tsup && node scripts/build-uni-module.mjs",
50
67
  "test": "vitest run",
@@ -1,3 +1 @@
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
+ import{b as a,c as b,d as c,e as d,f as e,g as f,h as g,i as h,j as i,k as j,l as k}from"./chunk-SOQF2GQO.mjs";export{a as createBrowserAdapter,b as detectAdapter,k as flush,d as getClient,h as hookConsole,c as init,i as report,g as setConsoleReport,j as setDeviceName,e as ulog,f as ulogger};
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "id": "uni-log",
3
3
  "displayName": "uni-log 日志上报",
4
- "version": "0.2.0",
4
+ "version": "0.2.1",
5
5
  "description": "uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件与设备信息批量上报",
6
6
  "keywords": [
7
7
  "uni-app",
@@ -1,11 +1,37 @@
1
1
  # uni-log-sdk
2
2
 
3
- uni-app / uni-app x 日志上报 SDK:console、JS 错误、自定义事件、设备与会话心跳,批量上报到自建 uni-log 服务端。
3
+ 多端日志上报 SDK(uni-app / uni-app x / React Native / 任意 TS Web):console、JS 错误、自定义事件、设备与会话心跳,批量上报到自建 uni-log 服务端。
4
4
 
5
- - 纯 JS、无外部运行时依赖(协议代码已内置),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序中运行
5
+ - 纯 JS、无外部运行时依赖(协议代码已内置),可在 uni-app x 蒸汽模式、普通 uni-app、H5、小程序、React Native(CLI / Expo)、任意 TS Web 应用中运行
6
6
  - 默认 10s 批量上报,满 50 条立即发送;发送失败写入本地暂存,冷启动自动续传
7
7
  - 提供 uni_modules 形态,HBuilderX 工程可直接引用
8
8
 
9
+ ## 支持平台
10
+
11
+ | 运行环境 | 接入方式 | 说明 |
12
+ | --- | --- | --- |
13
+ | uniapp / uniapp x(App·H5·小程序) | 主入口 `init()` 自动探测 uni 适配器 | console 劫持、页面栈、错误钩子全自动 |
14
+ | **React Native(CLI / Expo)** | 子路径 `uni-log-sdk/react-native` 的 `initRN()` | 存储经 AsyncStorage(Expo Go 内置);页面路由需一行接线 |
15
+ | **任意 TS Web 应用**(React / Vue / 纯浏览器) | 主入口 `init()` 自动探测浏览器适配器 | fetch + localStorage + window 错误事件,零配置 |
16
+ | 非 TS 技术栈(Flutter / 原生) | 按协议自行实现(HTTP + JSON + HMAC-SHA256) | 服务端零改动,详见协议定义 `@uni-log/shared` |
17
+
18
+ ### 适配器架构(贡献者向)
19
+
20
+ 平台差异全部收敛在 `src/adapters/`,`client.ts` 只面向 `types.ts` 里的 `PlatformAdapter` 接口:
21
+
22
+ ```
23
+ src/adapters/
24
+ ├── types.ts # PlatformAdapter / RequestOptions / RequestTask 纯接口
25
+ ├── uni.ts # uni 适配器(uni-app / uni-app x):uni.request / 同步 storage / 页面栈三级兜底
26
+ ├── web.ts # 浏览器适配器:localStorage / window 错误事件(SSR 防御)
27
+ ├── react-native.ts # RN 适配器:AsyncStorage 异步写穿+预读(ready 信号)/ ErrorUtils 链式
28
+ ├── fetch.ts # web / react-native 共用的 fetch 请求实现(POST + AbortController)
29
+ ├── detect.ts # detectAdapter:有 uni 全局走 uni,否则浏览器
30
+ └── index.ts # 桶出口(不得引出 RN 适配器——主入口依赖图禁止沾染 react-native)
31
+ ```
32
+
33
+ 新增平台支持 = 在 `adapters/` 加一个适配器文件 +(如需子路径)在 `tsup.config.ts` 加入口;队列、签名、重试、暂存续传等核心逻辑无需改动。
34
+
9
35
  ## 安装
10
36
 
11
37
  ### 方式一:uni_modules(推荐,HBuilderX / CLI 通用)
@@ -115,13 +141,35 @@ setConsoleReport(false) // 关闭 console.* 上报(保留原生打印),避
115
141
  await flush() // 应用退后台/页面隐藏时可主动上报
116
142
  ```
117
143
 
144
+ ## React Native / Expo 接入
145
+
146
+ ```bash
147
+ npm i uni-log-sdk @react-native-async-storage/async-storage
148
+ ```
149
+
150
+ ```ts
151
+ import { initRN } from 'uni-log-sdk/react-native'
152
+
153
+ initRN({
154
+ serverUrl: 'https://log.example.com',
155
+ appKey: '<看板分配>',
156
+ appSecret: '<与 appKey 成对>',
157
+ // 页面路由接线(react-navigation 为例):
158
+ getPage: () => navigationRef.getCurrentRoute()?.name ?? null,
159
+ })
160
+ ```
161
+
162
+ - JS 错误经 `ErrorUtils.setGlobalHandler` 自动捕获(链式保留原 handler)。
163
+ - 未处理 Promise rejection 需宿主接线:`require('promise/setimmediate/rejection-tracking').enable({ allRejections: true, onUnhandled: (id, e) => ulog('error', String(e)) })`。
164
+ - 存储经 AsyncStorage 异步写穿 + 冷启动预读;Expo Go 内置 async-storage,bare RN 需安装。
165
+
118
166
  ## 注意事项
119
167
 
120
168
  - `serverUrl` 不要带尾斜杠;App 真机需用局域网 IP 或 HTTPS 域名,Android 9+ 默认禁止明文 HTTP。
121
169
  - 小程序需要将上报域名加入 request 合法域名(开发期可在开发者工具勾选「不校验合法域名」)。
122
170
  - 自动可用的采集:设备信息、会话心跳、JS 错误;页面 PV 与自定义事件需手动调用。
123
171
  - `console` 全量上报通过运行时劫持实现,初始化时自动开启,可用 `hookConsole: false` 关闭。
124
- - 上报内容会经过服务端协议校验:单批 ≤50 条、`msg` ≤4000 字符、请求体 ≤512KB,超长由 SDK 自动截断。
172
+ - 单批最多 50 条日志 / 50 条事件;单条 `msg` 超过 4000 字符、`stack` 超过 8000 字符会自动截断。服务端请求体上限 1MB,超限批次会被拒绝,SDK 会暂存并在下次启动续传。
125
173
  - 各版本的新增、修复与**升级指南(含破坏性变更)**见 [CHANGELOG.md](./CHANGELOG.md)。
126
174
 
127
175
  ## License