react-native-gleapsdk 15.2.3 → 15.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
1
  class GleapNetworkIntercepter {
3
2
  requestId = 0;
4
3
  requests: any = {};
5
- maxRequests = 25;
4
+ maxRequests = 30;
6
5
  stopped = false;
6
+ initialized = false;
7
7
  updatedCallback: any = null;
8
8
 
9
9
  setUpdatedCallback(updatedCallback: any) {
@@ -15,6 +15,9 @@ class GleapNetworkIntercepter {
15
15
  }
16
16
 
17
17
  setMaxRequests(maxRequests: number) {
18
+ if (maxRequests > 70) {
19
+ maxRequests = 70;
20
+ }
18
21
  this.maxRequests = maxRequests;
19
22
  }
20
23
 
@@ -41,31 +44,50 @@ class GleapNetworkIntercepter {
41
44
  return;
42
45
  }
43
46
 
44
- var startDate = this.requests[gleapRequestId].date;
45
- if (
46
- startDate &&
47
- typeof startDate.getTime === 'function' &&
48
- !Object.isFrozen(this.requests[gleapRequestId])
49
- ) {
50
- this.requests[gleapRequestId].duration =
51
- new Date().getTime() - startDate.getTime();
52
- this.requests[gleapRequestId].date =
53
- this.requests[gleapRequestId].date.toString();
47
+ var req = this.requests[gleapRequestId];
48
+ if (req.startTime) {
49
+ req.duration = Date.now() - req.startTime;
50
+ req.date = new Date(req.startTime).toString();
54
51
  }
55
52
  }
56
53
 
57
- getTextContentSizeOk(text: string) {
58
- if (text && text.length) {
59
- const size = text.length * 16;
60
- const kiloBytes = size / 1024;
61
- const megaBytes = kiloBytes / 1024;
62
- if (megaBytes < 0.2) {
54
+ isContentTypeSupported(contentType: string | null | undefined): boolean {
55
+ if (typeof contentType !== 'string') {
56
+ return false;
57
+ }
58
+
59
+ if (contentType === '') {
60
+ return true;
61
+ }
62
+
63
+ contentType = contentType.toLowerCase();
64
+ var supported = ['text/', 'xml', 'json'];
65
+ for (var i = 0; i < supported.length; i++) {
66
+ if (contentType.includes(supported[i])) {
63
67
  return true;
64
68
  }
65
69
  }
70
+
66
71
  return false;
67
72
  }
68
73
 
74
+ getTextContentSizeOk(text: string) {
75
+ if (!text || !text.length) {
76
+ return false;
77
+ }
78
+
79
+ try {
80
+ var size = new (global as any).TextEncoder().encode(text).length;
81
+ var megaBytes = size / 1024 / 1024;
82
+ return megaBytes < 0.15;
83
+ } catch (e) {}
84
+
85
+ // Fallback: assume ~2 bytes per char (UTF-16)
86
+ var estimatedBytes = text.length * 2;
87
+ var megaBytes = estimatedBytes / 1024 / 1024;
88
+ return megaBytes < 0.15;
89
+ }
90
+
69
91
  prepareContent(text: string) {
70
92
  if (!this.getTextContentSizeOk(text)) {
71
93
  return '<content_too_large>';
@@ -76,12 +98,15 @@ class GleapNetworkIntercepter {
76
98
 
77
99
  cleanupPayload(payload: any) {
78
100
  if (payload === undefined || payload === null) {
79
- return '{}';
101
+ return '';
80
102
  }
81
103
 
82
104
  try {
83
105
  if (ArrayBuffer.isView(payload)) {
84
- return `{ type: "binary", length: ${payload.byteLength} }`;
106
+ if (typeof (global as any).TextDecoder !== 'undefined') {
107
+ return new (global as any).TextDecoder().decode(payload);
108
+ }
109
+ return JSON.stringify({ type: 'binary', length: payload.byteLength });
85
110
  }
86
111
  } catch (exp) {}
87
112
 
@@ -93,15 +118,64 @@ class GleapNetworkIntercepter {
93
118
  return this.prepareContent(payloadText);
94
119
  }
95
120
 
121
+ extractFetchUrl(params: any[]): string {
122
+ if (params.length === 0) {
123
+ return '';
124
+ }
125
+
126
+ var first = params[0];
127
+
128
+ // Handle Request object
129
+ if (first && typeof first === 'object' && typeof first.url === 'string') {
130
+ return first.url;
131
+ }
132
+
133
+ // Handle string URL
134
+ if (typeof first === 'string') {
135
+ return first;
136
+ }
137
+
138
+ return String(first);
139
+ }
140
+
96
141
  start() {
142
+ if (this.initialized) {
143
+ return;
144
+ }
145
+
146
+ this.initialized = true;
97
147
  this.setStopped(false);
148
+
98
149
  this.interceptNetworkRequests({
99
150
  onFetch: (params: any[], gleapRequestId: any) => {
100
151
  if (this.stopped || params.length === 0) {
101
152
  return;
102
153
  }
103
154
 
104
- if (params.length >= 2 && params[1] !== undefined) {
155
+ var url = this.extractFetchUrl(params);
156
+ var first = params[0];
157
+
158
+ // Handle Request object: fetch(new Request(url, opts))
159
+ if (
160
+ first &&
161
+ typeof first === 'object' &&
162
+ typeof first.url === 'string'
163
+ ) {
164
+ this.requests[gleapRequestId] = {
165
+ url: first.url,
166
+ startTime: Date.now(),
167
+ date: new Date(),
168
+ request: {
169
+ payload: '',
170
+ headers:
171
+ first.headers && typeof first.headers.entries === 'function'
172
+ ? Object.fromEntries(first.headers.entries())
173
+ : {},
174
+ },
175
+ type: first.method || 'GET',
176
+ };
177
+ } else if (params.length >= 2 && params[1] !== undefined) {
178
+ // Handle fetch(url, options)
105
179
  var method = params[1].method ? params[1].method : 'GET';
106
180
  this.requests[gleapRequestId] = {
107
181
  request: {
@@ -109,14 +183,17 @@ class GleapNetworkIntercepter {
109
183
  headers: params[1].headers,
110
184
  },
111
185
  type: method,
112
- url: params[0],
186
+ url: url,
187
+ startTime: Date.now(),
113
188
  date: new Date(),
114
189
  };
115
190
  } else {
191
+ // Handle fetch(url)
116
192
  this.requests[gleapRequestId] = {
117
193
  request: {},
118
- url: params[0],
194
+ url: url,
119
195
  type: 'GET',
196
+ startTime: Date.now(),
120
197
  date: new Date(),
121
198
  };
122
199
  }
@@ -134,35 +211,42 @@ class GleapNetworkIntercepter {
134
211
  }
135
212
 
136
213
  try {
137
- this.requests[gleapRequestId].success = true;
138
- this.requests[gleapRequestId].response = {
139
- status: req.status,
140
- statusText: '',
141
- responseText: '<request_still_open>',
142
- };
143
- this.calcRequestTime(gleapRequestId);
144
- } catch (exp) {}
145
-
146
- req
147
- .text()
148
- .then((responseText: any) => {
149
- if (this.requests && this.requests[gleapRequestId]) {
150
- this.requests[gleapRequestId].success = true;
151
- this.requests[gleapRequestId].response = {
152
- status: req.status,
153
- statusText: req.statusText,
154
- responseText: this.prepareContent(responseText),
155
- };
156
-
157
- this.calcRequestTime(gleapRequestId);
158
- this.cleanRequests();
159
- }
160
- })
161
- .catch((_err: any) => {
162
- if (this) {
163
- this.cleanRequests();
164
- }
165
- });
214
+ var contentType = '';
215
+ if (req.headers && typeof req.headers.get === 'function') {
216
+ contentType = req.headers.get('content-type') || '';
217
+ }
218
+
219
+ if (this.isContentTypeSupported(contentType)) {
220
+ req
221
+ .text()
222
+ .then((responseText: any) => {
223
+ if (this.requests && this.requests[gleapRequestId]) {
224
+ this.requests[gleapRequestId].success = true;
225
+ this.requests[gleapRequestId].response = {
226
+ status: req.status,
227
+ statusText: req.statusText,
228
+ responseText: this.prepareContent(responseText),
229
+ };
230
+ this.calcRequestTime(gleapRequestId);
231
+ this.cleanRequests();
232
+ }
233
+ })
234
+ .catch((_err: any) => {
235
+ this.cleanRequests();
236
+ });
237
+ } else {
238
+ this.requests[gleapRequestId].success = true;
239
+ this.requests[gleapRequestId].response = {
240
+ status: req.status,
241
+ statusText: req.statusText,
242
+ responseText: '<content_type_not_supported>',
243
+ };
244
+ this.calcRequestTime(gleapRequestId);
245
+ this.cleanRequests();
246
+ }
247
+ } catch (exp) {
248
+ this.cleanRequests();
249
+ }
166
250
  },
167
251
  onFetchFailed: (_err: any, gleapRequestId: any) => {
168
252
  if (this.stopped || !gleapRequestId) {
@@ -189,6 +273,7 @@ class GleapNetworkIntercepter {
189
273
  this.requests[request.gleapRequestId] = {
190
274
  type: args[0],
191
275
  url: args[1],
276
+ startTime: Date.now(),
192
277
  date: new Date(),
193
278
  };
194
279
  }
@@ -239,22 +324,28 @@ class GleapNetworkIntercepter {
239
324
  this.requests &&
240
325
  this.requests[request.gleapRequestId]
241
326
  ) {
242
- const contentType = request.getResponseHeader('content-type');
243
- const isTextOrJSON =
244
- contentType &&
245
- (contentType.includes('json') || contentType.includes('text'));
327
+ var contentType = '';
328
+ try {
329
+ contentType = request.getResponseHeader('content-type') || '';
330
+ } catch (e) {}
246
331
 
247
332
  var responseText = '<' + contentType + '>';
248
- if (request.responseType === '' || request.responseType === 'text') {
249
- responseText = request.responseText;
250
- }
251
- if (request._response && isTextOrJSON) {
252
- responseText = request._response;
333
+ if (this.isContentTypeSupported(contentType)) {
334
+ if (
335
+ request.responseType === '' ||
336
+ request.responseType === 'text'
337
+ ) {
338
+ responseText = request.responseText;
339
+ }
340
+ if (request._response) {
341
+ responseText = request._response;
342
+ }
253
343
  }
254
344
 
255
345
  this.requests[request.gleapRequestId].success = true;
256
346
  this.requests[request.gleapRequestId].response = {
257
347
  status: request.status,
348
+ statusText: request.statusText,
258
349
  responseText: this.prepareContent(responseText),
259
350
  };
260
351
 
@@ -267,47 +358,48 @@ class GleapNetworkIntercepter {
267
358
  }
268
359
 
269
360
  interceptNetworkRequests(callback: any) {
270
- // eslint-disable-next-line consistent-this
271
361
  var self = this;
272
362
 
273
- // @ts-ignore
274
- if (XMLHttpRequest.prototype.gleapTouched) {
275
- return;
276
- }
277
-
278
- // @ts-ignore
279
- XMLHttpRequest.prototype.gleapTouched = true;
280
-
281
363
  // XMLHttpRequest
282
364
  const open = XMLHttpRequest.prototype.open;
283
365
  const send = XMLHttpRequest.prototype.send;
284
366
 
285
367
  // @ts-ignore
286
- XMLHttpRequest.prototype.wrappedSetRequestHeader =
287
- XMLHttpRequest.prototype.setRequestHeader;
288
- XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
368
+ if (XMLHttpRequest.prototype.gleapSetRequestHeader === undefined) {
289
369
  // @ts-ignore
290
- if (!this.requestHeaders) {
370
+ XMLHttpRequest.prototype.gleapSetRequestHeader =
371
+ XMLHttpRequest.prototype.setRequestHeader;
372
+ }
373
+
374
+ // @ts-ignore
375
+ if (XMLHttpRequest.prototype.gleapSetRequestHeader) {
376
+ XMLHttpRequest.prototype.setRequestHeader = function (
377
+ header: string,
378
+ value: string
379
+ ) {
291
380
  // @ts-ignore
292
- this.requestHeaders = {};
293
- }
381
+ if (!this.requestHeaders) {
382
+ // @ts-ignore
383
+ this.requestHeaders = {};
384
+ }
294
385
 
295
- // @ts-ignore
296
- if (this.requestHeaders && this.requestHeaders.hasOwnProperty(header)) {
297
- return;
298
- }
386
+ // @ts-ignore
387
+ if (this.requestHeaders && this.requestHeaders.hasOwnProperty(header)) {
388
+ return;
389
+ }
299
390
 
300
- // @ts-ignore
301
- if (!this.requestHeaders[header]) {
302
391
  // @ts-ignore
303
- this.requestHeaders[header] = [];
304
- }
392
+ if (!this.requestHeaders[header]) {
393
+ // @ts-ignore
394
+ this.requestHeaders[header] = [];
395
+ }
305
396
 
306
- // @ts-ignore
307
- this.requestHeaders[header].push(value);
308
- // @ts-ignore
309
- this.wrappedSetRequestHeader(header, value);
310
- };
397
+ // @ts-ignore
398
+ this.requestHeaders[header].push(value);
399
+ // @ts-ignore
400
+ this.gleapSetRequestHeader(header, value);
401
+ };
402
+ }
311
403
 
312
404
  XMLHttpRequest.prototype.open = function () {
313
405
  (this as any).gleapRequestId = ++self.requestId;
@@ -337,32 +429,30 @@ class GleapNetworkIntercepter {
337
429
  };
338
430
 
339
431
  // Fetch
340
- if (global) {
341
- (function () {
342
- var originalFetch = global.fetch;
343
- global.fetch = function () {
344
- var gleapRequestId = ++self.requestId;
345
- callback.onFetch(arguments, gleapRequestId);
346
-
347
- return (
348
- originalFetch
349
- // @ts-ignore
350
- .apply(this, arguments)
351
- .then(function (response) {
352
- if (response && typeof response.clone === 'function') {
353
- const data = response.clone();
354
- callback.onFetchLoad(data, gleapRequestId);
355
- }
356
-
357
- return response;
358
- })
359
- .catch((err) => {
360
- callback.onFetchFailed(err, gleapRequestId);
361
- throw err;
362
- })
363
- );
364
- };
365
- })();
432
+ if (global && global.fetch) {
433
+ var originalFetch = global.fetch;
434
+ global.fetch = function () {
435
+ var gleapRequestId = ++self.requestId;
436
+ callback.onFetch(arguments, gleapRequestId);
437
+
438
+ return (
439
+ originalFetch
440
+ // @ts-ignore
441
+ .apply(this, arguments)
442
+ .then(function (response: any) {
443
+ if (response && typeof response.clone === 'function') {
444
+ var data = response.clone();
445
+ callback.onFetchLoad(data, gleapRequestId);
446
+ }
447
+
448
+ return response;
449
+ })
450
+ .catch((err: any) => {
451
+ callback.onFetchFailed(err, gleapRequestId);
452
+ throw err;
453
+ })
454
+ );
455
+ };
366
456
  }
367
457
 
368
458
  return callback;