spy-client 2.1.4 → 2.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -124,40 +124,6 @@ export default class SpyClient {
124
124
  };
125
125
  }
126
126
 
127
- fetch(url: string, data: any) {
128
- if (!window.fetch) {
129
- return;
130
- }
131
- fetch(url, {
132
- method: 'POST',
133
- credentials: 'include',
134
- headers: {
135
- 'Content-Type': 'application/json',
136
- },
137
- body: JSON.stringify(data),
138
- });
139
- }
140
-
141
- sendPost(data: any) {
142
- let logItems: any[] = isArray(data) ? data : [data];
143
- const first = logItems[0];
144
- // 需要在页面暴漏出来pid等基本信息,方式调试查看基本信息与兼容目前的监控
145
- const query = {
146
- pid: first.pid,
147
- type: first.type,
148
- group: first.group,
149
- };
150
- const url = this.option.logServer + stringify(query);
151
-
152
- if (!(
153
- !isLtIos14
154
- && navigator.sendBeacon
155
- && navigator.sendBeacon(url, JSON.stringify(data))
156
- )) {
157
- this.fetch(url, data);
158
- }
159
- }
160
-
161
127
  handle(logItem: any) {
162
128
  if (!this.check(logItem)) {
163
129
  return;
@@ -209,19 +175,7 @@ export default class SpyClient {
209
175
  }
210
176
 
211
177
  const url = this.option.logServer + stringify(logItem);
212
-
213
- // 目前服务器端支持sendBeacon的post请求,可以优先采用,该api可以降低打点丢失率
214
- // 但是ios有些问题:在webkit浏览内核环境下,beacon requests的请求方式存在证书验证的bug,发送失败但返回正确.
215
- // 修复说明在如下链接中:https://bugs.webkit.org/show_bug.cgi?id=193508
216
- // 但是目前为止,在iOS12.2版本中该问题依旧存在
217
- // 2021-11-11: 发现ios 14可以送,故小于14的不发送
218
- if (!(
219
- !isLtIos14
220
- && navigator.sendBeacon
221
- && navigator.sendBeacon(url)
222
- )) {
223
- (new Image()).src = url;
224
- }
178
+ this.request(url);
225
179
  }
226
180
  if (post) {
227
181
  this.sendPost(postData);
@@ -384,4 +338,50 @@ export default class SpyClient {
384
338
  clearAllMark() {
385
339
  this.markCache = {};
386
340
  }
341
+
342
+ // send(data, true) 也能以post发送,但是会有严格校验
343
+ // sendPost能以post发送,但没有校验
344
+ sendPost(data: any) {
345
+ let logItems: any[] = isArray(data) ? data : [data];
346
+ const first = logItems[0];
347
+ // 需要在页面暴漏出来pid等基本信息,方式调试查看基本信息与兼容目前的监控
348
+ const query = {
349
+ pid: first.pid,
350
+ type: first.type,
351
+ group: first.group,
352
+ };
353
+ const url = this.option.logServer + stringify(query);
354
+
355
+ this.request(url, data);
356
+ }
357
+
358
+ // 有data时,意味着要用post发送请求
359
+ protected request(url: string, data?: any) {
360
+ if (!(
361
+ !isLtIos14
362
+ && navigator.sendBeacon
363
+ && navigator.sendBeacon(url, data ? JSON.stringify(data) : undefined)
364
+ )) {
365
+ if (data) {
366
+ this.fetch(url, data);
367
+ }
368
+ else {
369
+ (new Image()).src = url;
370
+ }
371
+ }
372
+ }
373
+
374
+ protected fetch(url: string, data: any) {
375
+ if (!window.fetch) {
376
+ return;
377
+ }
378
+ fetch(url, {
379
+ method: 'POST',
380
+ credentials: 'include',
381
+ headers: {
382
+ 'Content-Type': 'application/json',
383
+ },
384
+ body: JSON.stringify(data),
385
+ });
386
+ }
387
387
  }