react-native-gleapsdk 15.2.2 → 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.
- package/android/build.gradle +1 -1
- package/lib/commonjs/networklogger.js +166 -87
- package/lib/commonjs/networklogger.js.map +1 -1
- package/lib/module/networklogger.js +166 -87
- package/lib/module/networklogger.js.map +1 -1
- package/lib/typescript/networklogger.d.ts +3 -0
- package/lib/typescript/networklogger.d.ts.map +1 -1
- package/package.json +1 -1
- package/react-native-gleapsdk.podspec +1 -1
- package/src/networklogger.ts +205 -115
package/src/networklogger.ts
CHANGED
|
@@ -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 =
|
|
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
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
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:
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
contentType
|
|
245
|
-
|
|
327
|
+
var contentType = '';
|
|
328
|
+
try {
|
|
329
|
+
contentType = request.getResponseHeader('content-type') || '';
|
|
330
|
+
} catch (e) {}
|
|
246
331
|
|
|
247
332
|
var responseText = '<' + contentType + '>';
|
|
248
|
-
if (
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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.
|
|
287
|
-
XMLHttpRequest.prototype.setRequestHeader;
|
|
288
|
-
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
|
|
368
|
+
if (XMLHttpRequest.prototype.gleapSetRequestHeader === undefined) {
|
|
289
369
|
// @ts-ignore
|
|
290
|
-
|
|
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
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
.
|
|
360
|
-
|
|
361
|
-
|
|
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;
|