1 | import Constants from 'expo-constants';
|
2 | import { EventEmitter } from 'fbemitter';
|
3 | import invariant from 'invariant';
|
4 | import uuidv4 from 'uuid/v4';
|
5 | import LogSerialization from './LogSerialization';
|
6 | const _sessionId = uuidv4();
|
7 | const _logQueue = [];
|
8 | const _transportEventEmitter = new EventEmitter();
|
9 | let _logCounter = 0;
|
10 | let _isSendingLogs = false;
|
11 | let _completionPromise = null;
|
12 | let _resolveCompletion = null;
|
13 | async function enqueueRemoteLogAsync(level, additionalFields, data) {
|
14 | if (_isReactNativeWarning(data)) {
|
15 |
|
16 | if (data.length === 0) {
|
17 | throw new Error(`Warnings must include log arguments`);
|
18 | }
|
19 | const warning = data[0];
|
20 | if (typeof warning !== 'string') {
|
21 | throw new TypeError(`The log argument for a warning must be a string`);
|
22 | }
|
23 | const lines = warning.split('\n');
|
24 | if (lines.length > 1 && /^\s+in /.test(lines[1])) {
|
25 | data[0] = lines[0];
|
26 | }
|
27 | }
|
28 | const { body, includesStack } = await LogSerialization.serializeLogDataAsync(data, level);
|
29 | _logQueue.push({
|
30 | count: _logCounter++,
|
31 | level,
|
32 | body,
|
33 | includesStack,
|
34 | ...additionalFields,
|
35 | });
|
36 |
|
37 | _sendRemoteLogsAsync().catch(error => {
|
38 | setImmediate(() => {
|
39 | throw error;
|
40 | });
|
41 | });
|
42 | }
|
43 | async function _sendRemoteLogsAsync() {
|
44 | if (_isSendingLogs || !_logQueue.length) {
|
45 | return;
|
46 | }
|
47 |
|
48 |
|
49 | const batch = _logQueue.splice(0);
|
50 | const { logUrl } = Constants.manifest;
|
51 | if (typeof logUrl !== 'string') {
|
52 | throw new Error('The Expo project manifest must specify `logUrl`');
|
53 | }
|
54 | _isSendingLogs = true;
|
55 | try {
|
56 | await _sendNextLogBatchAsync(batch, logUrl);
|
57 | }
|
58 | finally {
|
59 | _isSendingLogs = false;
|
60 | }
|
61 | if (_logQueue.length) {
|
62 | return _sendRemoteLogsAsync();
|
63 | }
|
64 | else if (_resolveCompletion) {
|
65 | _resolveCompletion();
|
66 | }
|
67 | }
|
68 | async function _sendNextLogBatchAsync(batch, logUrl) {
|
69 | let response;
|
70 | const headers = {
|
71 | 'Content-Type': 'application/json',
|
72 | Connection: 'keep-alive',
|
73 | 'Proxy-Connection': 'keep-alive',
|
74 | Accept: 'application/json',
|
75 | 'Device-Id': Constants.installationId,
|
76 | 'Session-Id': _sessionId,
|
77 | };
|
78 | if (Constants.deviceName) {
|
79 | headers['Device-Name'] = Constants.deviceName;
|
80 | }
|
81 | try {
|
82 | response = await fetch(logUrl, {
|
83 | method: 'POST',
|
84 | headers,
|
85 | body: JSON.stringify(batch),
|
86 | });
|
87 | }
|
88 | catch (error) {
|
89 | _transportEventEmitter.emit('error', { error });
|
90 | return;
|
91 | }
|
92 | const success = response.status >= 200 && response.status < 300;
|
93 | if (!success) {
|
94 | _transportEventEmitter.emit('error', {
|
95 | error: new Error(`An HTTP error occurred when sending remote logs`),
|
96 | response,
|
97 | });
|
98 | }
|
99 | }
|
100 | function addTransportErrorListener(listener) {
|
101 | return _transportEventEmitter.addListener('error', listener);
|
102 | }
|
103 | function _isReactNativeWarning(data) {
|
104 |
|
105 | const message = data[0];
|
106 | return data.length === 1 && typeof message === 'string' && message.startsWith('Warning: ');
|
107 | }
|
108 | export default {
|
109 | enqueueRemoteLogAsync,
|
110 | addTransportErrorListener,
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | export function __waitForEmptyLogQueueAsync() {
|
117 | if (_completionPromise) {
|
118 | return _completionPromise;
|
119 | }
|
120 | if (!_isSendingLogs && !_logQueue.length) {
|
121 | return Promise.resolve();
|
122 | }
|
123 | _completionPromise = new Promise(resolve => {
|
124 | _resolveCompletion = () => {
|
125 | invariant(!_isSendingLogs, `Must not be sending logs at completion`);
|
126 | invariant(!_logQueue.length, `Log queue must be empty at completion`);
|
127 | _completionPromise = null;
|
128 | _resolveCompletion = null;
|
129 | resolve();
|
130 | };
|
131 | });
|
132 | return _completionPromise;
|
133 | }
|
134 |
|
\ | No newline at end of file |