dreaction-react 1.2.0 → 1.3.0

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,4 +1,4 @@
1
- import type { DReactionCore } from 'dreaction-client-core';
1
+ import { type DReactionCore } from 'dreaction-client-core';
2
2
  export interface NetworkingOptions {
3
3
  ignoreContentTypes?: RegExp;
4
4
  ignoreUrls?: RegExp;
@@ -1 +1 @@
1
- {"version":3,"file":"networking.d.ts","sourceRoot":"","sources":["../../src/plugins/networking.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,uBAAuB,CAAC;AAOnE,MAAM,WAAW,iBAAiB;IAChC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAMD,QAAA,MAAM,UAAU,kBACC,iBAAiB,iBACpB,aAAa;;;CAwTxB,CAAC;AAEJ,eAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"networking.d.ts","sourceRoot":"","sources":["../../src/plugins/networking.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAGnB,MAAM,uBAAuB,CAAC;AAO/B,MAAM,WAAW,iBAAiB;IAChC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAMD,QAAA,MAAM,UAAU,kBACC,iBAAiB,iBACpB,aAAa;;;CA+UxB,CAAC;AAEJ,eAAe,UAAU,CAAC"}
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const dreaction_client_core_1 = require("dreaction-client-core");
3
4
  /**
4
5
  * Don't include the response bodies for images by default.
5
6
  */
@@ -11,8 +12,6 @@ const networking = (pluginConfig = {}) => (dreaction) => {
11
12
  const options = Object.assign({}, DEFAULTS, pluginConfig);
12
13
  // a RegExp to suppress adding the body cuz it costs a lot to serialize
13
14
  const ignoreContentTypes = options.ignoreContentTypes || DEFAULT_CONTENT_TYPES_RX;
14
- // a XHR call tracker
15
- let dreactionCounter = 1000;
16
15
  // a temporary cache to hold requests so we can match up the data
17
16
  const requestCache = {};
18
17
  // Store original functions
@@ -36,16 +35,52 @@ const networking = (pluginConfig = {}) => (dreaction) => {
36
35
  xhr._skipDReaction = true;
37
36
  return originalXHRSend.apply(this, [data]);
38
37
  }
39
- // bump the counter
40
- dreactionCounter++;
38
+ // Generate unique request ID
39
+ const requestId = (0, dreaction_client_core_1.generateRequestId)();
41
40
  // tag
42
- xhr._trackingName = dreactionCounter;
41
+ xhr._trackingName = requestId;
42
+ // Parse request headers
43
+ let requestHeaders = {};
44
+ try {
45
+ if (xhr._requestHeaders) {
46
+ requestHeaders = xhr._requestHeaders;
47
+ }
48
+ }
49
+ catch (e) {
50
+ // ignore
51
+ }
52
+ // Parse URL and params
53
+ const url = xhr._url;
54
+ let params = null;
55
+ const queryParamIdx = url ? url.indexOf('?') : -1;
56
+ if (queryParamIdx > -1) {
57
+ params = {};
58
+ url
59
+ .substr(queryParamIdx + 1)
60
+ .split('&')
61
+ .forEach((pair) => {
62
+ const [key, value] = pair.split('=');
63
+ if (key && value !== undefined) {
64
+ params[key] = decodeURIComponent(value.replace(/\+/g, ' '));
65
+ }
66
+ });
67
+ }
68
+ const tronRequest = {
69
+ url: url || '',
70
+ method: xhr._method || 'GET',
71
+ data,
72
+ headers: requestHeaders,
73
+ params,
74
+ };
43
75
  // cache
44
- requestCache[dreactionCounter] = {
76
+ requestCache[requestId] = {
45
77
  data,
46
78
  xhr,
79
+ request: tronRequest,
47
80
  stopTimer: dreaction.startTimer(),
48
81
  };
82
+ // Send api.request event
83
+ dreaction.apiRequest(requestId, tronRequest);
49
84
  // Setup listener for response
50
85
  const originalOnReadyStateChange = xhr.onreadystatechange;
51
86
  xhr.onreadystatechange = function () {
@@ -66,45 +101,14 @@ const networking = (pluginConfig = {}) => (dreaction) => {
66
101
  if (xhr._skipDReaction) {
67
102
  return;
68
103
  }
69
- const url = xhr._url;
70
- let params = null;
71
- const queryParamIdx = url ? url.indexOf('?') : -1;
72
- if (queryParamIdx > -1) {
73
- params = {};
74
- url
75
- .substr(queryParamIdx + 1)
76
- .split('&')
77
- .forEach((pair) => {
78
- const [key, value] = pair.split('=');
79
- if (key && value !== undefined) {
80
- params[key] = decodeURIComponent(value.replace(/\+/g, ' '));
81
- }
82
- });
83
- }
84
104
  // fetch and clear the request data from the cache
85
- const rid = xhr._trackingName;
86
- const cachedRequest = requestCache[rid] || { xhr };
87
- requestCache[rid] = null;
88
- // assemble the request object
89
- const { data, stopTimer } = cachedRequest;
90
- // Parse request headers
91
- let requestHeaders = {};
92
- try {
93
- const requestHeaderString = xhr._requestHeaders;
94
- if (requestHeaderString) {
95
- requestHeaders = requestHeaderString;
96
- }
97
- }
98
- catch (e) {
99
- // ignore
105
+ const requestId = xhr._trackingName;
106
+ const cachedRequest = requestCache[requestId];
107
+ if (!cachedRequest) {
108
+ return;
100
109
  }
101
- const tronRequest = {
102
- url: url || cachedRequest.xhr._url,
103
- method: xhr._method || null,
104
- data,
105
- headers: requestHeaders || null,
106
- params,
107
- };
110
+ delete requestCache[requestId];
111
+ const { request: tronRequest, stopTimer } = cachedRequest;
108
112
  // Parse response headers
109
113
  let responseHeaders = {};
110
114
  try {
@@ -147,9 +151,9 @@ const networking = (pluginConfig = {}) => (dreaction) => {
147
151
  const tronResponse = {
148
152
  body,
149
153
  status: xhr.status,
150
- headers: responseHeaders || null,
154
+ headers: responseHeaders || {},
151
155
  };
152
- dreaction.apiResponse(tronRequest, tronResponse, stopTimer ? stopTimer() : null);
156
+ dreaction.apiResponse(requestId, tronRequest, tronResponse, stopTimer ? stopTimer() : 0);
153
157
  };
154
158
  /**
155
159
  * Intercept fetch API
@@ -165,9 +169,8 @@ const networking = (pluginConfig = {}) => (dreaction) => {
165
169
  if (options.ignoreUrls && options.ignoreUrls.test(url)) {
166
170
  return originalFetch.call(this, input, init);
167
171
  }
168
- // bump the counter
169
- dreactionCounter++;
170
- const requestId = dreactionCounter;
172
+ // Generate unique request ID
173
+ const requestId = (0, dreaction_client_core_1.generateRequestId)();
171
174
  const stopTimer = dreaction.startTimer();
172
175
  // Parse URL and params
173
176
  let params = null;
@@ -184,13 +187,32 @@ const networking = (pluginConfig = {}) => (dreaction) => {
184
187
  }
185
188
  });
186
189
  }
190
+ // Parse request headers
191
+ let requestHeaders = {};
192
+ if (init?.headers) {
193
+ if (init.headers instanceof Headers) {
194
+ init.headers.forEach((value, key) => {
195
+ requestHeaders[key] = value;
196
+ });
197
+ }
198
+ else if (Array.isArray(init.headers)) {
199
+ init.headers.forEach(([key, value]) => {
200
+ requestHeaders[key] = value;
201
+ });
202
+ }
203
+ else {
204
+ requestHeaders = init.headers;
205
+ }
206
+ }
187
207
  const tronRequest = {
188
208
  url,
189
- method: init?.method || 'GET',
209
+ method: (init?.method || 'GET'),
190
210
  data: init?.body || null,
191
- headers: init?.headers || {},
211
+ headers: requestHeaders,
192
212
  params,
193
213
  };
214
+ // Send api.request event
215
+ dreaction.apiRequest(requestId, tronRequest);
194
216
  return originalFetch
195
217
  .call(this, input, init)
196
218
  .then(async (response) => {
@@ -222,7 +244,7 @@ const networking = (pluginConfig = {}) => (dreaction) => {
222
244
  status: response.status,
223
245
  headers: responseHeaders,
224
246
  };
225
- dreaction.apiResponse(tronRequest, tronResponse, stopTimer());
247
+ dreaction.apiResponse(requestId, tronRequest, tronResponse, stopTimer());
226
248
  return response;
227
249
  });
228
250
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dreaction-react",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "private": false,
5
5
  "description": "DReaction client for React web applications",
6
6
  "main": "lib/index.js",
@@ -25,8 +25,8 @@
25
25
  },
26
26
  "homepage": "https://github.com/moonrailgun/dreaction#readme",
27
27
  "dependencies": {
28
- "dreaction-client-core": "1.3.0",
29
- "dreaction-protocol": "1.0.10"
28
+ "dreaction-client-core": "1.4.0",
29
+ "dreaction-protocol": "1.0.11"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/react": "^18.0.0",
@@ -1,4 +1,8 @@
1
- import type { DReactionCore, Plugin } from 'dreaction-client-core';
1
+ import {
2
+ type DReactionCore,
3
+ type Plugin,
4
+ generateRequestId,
5
+ } from 'dreaction-client-core';
2
6
 
3
7
  /**
4
8
  * Don't include the response bodies for images by default.
@@ -23,11 +27,8 @@ const networking =
23
27
  const ignoreContentTypes =
24
28
  options.ignoreContentTypes || DEFAULT_CONTENT_TYPES_RX;
25
29
 
26
- // a XHR call tracker
27
- let dreactionCounter = 1000;
28
-
29
30
  // a temporary cache to hold requests so we can match up the data
30
- const requestCache: Record<number, any> = {};
31
+ const requestCache: Record<string, any> = {};
31
32
 
32
33
  // Store original functions
33
34
  let originalXHROpen: typeof XMLHttpRequest.prototype.open;
@@ -59,19 +60,58 @@ const networking =
59
60
  return originalXHRSend.apply(this, [data]);
60
61
  }
61
62
 
62
- // bump the counter
63
- dreactionCounter++;
63
+ // Generate unique request ID
64
+ const requestId = generateRequestId();
64
65
 
65
66
  // tag
66
- xhr._trackingName = dreactionCounter;
67
+ xhr._trackingName = requestId;
68
+
69
+ // Parse request headers
70
+ let requestHeaders: Record<string, string> = {};
71
+ try {
72
+ if (xhr._requestHeaders) {
73
+ requestHeaders = xhr._requestHeaders;
74
+ }
75
+ } catch (e) {
76
+ // ignore
77
+ }
78
+
79
+ // Parse URL and params
80
+ const url = xhr._url;
81
+ let params = null;
82
+ const queryParamIdx = url ? url.indexOf('?') : -1;
83
+ if (queryParamIdx > -1) {
84
+ params = {} as Record<string, string>;
85
+ url
86
+ .substr(queryParamIdx + 1)
87
+ .split('&')
88
+ .forEach((pair: string) => {
89
+ const [key, value] = pair.split('=');
90
+ if (key && value !== undefined) {
91
+ params![key] = decodeURIComponent(value.replace(/\+/g, ' '));
92
+ }
93
+ });
94
+ }
95
+
96
+ const tronRequest = {
97
+ url: url || '',
98
+ method: xhr._method || 'GET',
99
+ data,
100
+ headers: requestHeaders,
101
+ params,
102
+ };
67
103
 
68
104
  // cache
69
- requestCache[dreactionCounter] = {
105
+ requestCache[requestId] = {
70
106
  data,
71
107
  xhr,
108
+ request: tronRequest,
72
109
  stopTimer: dreaction.startTimer(),
73
110
  };
74
111
 
112
+ // Send api.request event
113
+ (dreaction as any).apiRequest(requestId, tronRequest);
114
+
75
115
  // Setup listener for response
76
116
  const originalOnReadyStateChange = xhr.onreadystatechange;
77
117
  xhr.onreadystatechange = function () {
@@ -96,48 +136,15 @@ const networking =
96
136
  return;
97
137
  }
98
138
 
99
- const url = xhr._url;
100
- let params = null;
101
- const queryParamIdx = url ? url.indexOf('?') : -1;
102
- if (queryParamIdx > -1) {
103
- params = {} as Record<string, string>;
104
- url
105
- .substr(queryParamIdx + 1)
106
- .split('&')
107
- .forEach((pair: string) => {
108
- const [key, value] = pair.split('=');
109
- if (key && value !== undefined) {
110
- params![key] = decodeURIComponent(value.replace(/\+/g, ' '));
111
- }
112
- });
113
- }
114
-
115
139
  // fetch and clear the request data from the cache
116
- const rid = xhr._trackingName;
117
- const cachedRequest = requestCache[rid] || { xhr };
118
- requestCache[rid] = null;
119
-
120
- // assemble the request object
121
- const { data, stopTimer } = cachedRequest;
122
-
123
- // Parse request headers
124
- let requestHeaders: Record<string, string> = {};
125
- try {
126
- const requestHeaderString = xhr._requestHeaders;
127
- if (requestHeaderString) {
128
- requestHeaders = requestHeaderString;
129
- }
130
- } catch (e) {
131
- // ignore
140
+ const requestId = xhr._trackingName;
141
+ const cachedRequest = requestCache[requestId];
142
+ if (!cachedRequest) {
143
+ return;
132
144
  }
145
+ delete requestCache[requestId];
133
146
 
134
- const tronRequest = {
135
- url: url || cachedRequest.xhr._url,
136
- method: xhr._method || null,
137
- data,
138
- headers: requestHeaders || null,
139
- params,
140
- };
147
+ const { request: tronRequest, stopTimer } = cachedRequest;
141
148
 
142
149
  // Parse response headers
143
150
  let responseHeaders: Record<string, string> = {};
@@ -185,13 +192,14 @@ const networking =
185
192
  const tronResponse = {
186
193
  body,
187
194
  status: xhr.status,
188
- headers: responseHeaders || null,
195
+ headers: responseHeaders || {},
189
196
  };
190
197
 
191
198
  (dreaction as any).apiResponse(
199
+ requestId,
192
200
  tronRequest,
193
201
  tronResponse,
194
- stopTimer ? stopTimer() : null
202
+ stopTimer ? stopTimer() : 0
195
203
  );
196
204
  };
197
205
 
@@ -216,9 +224,8 @@ const networking =
216
224
  return originalFetch.call(this, input as RequestInfo, init);
217
225
  }
218
226
 
219
- // bump the counter
220
- dreactionCounter++;
221
- const requestId = dreactionCounter;
227
+ // Generate unique request ID
228
+ const requestId = generateRequestId();
222
229
 
223
230
  const stopTimer = dreaction.startTimer();
224
231
 
@@ -238,14 +245,33 @@ const networking =
238
245
  });
239
246
  }
240
247
 
248
+ // Parse request headers
249
+ let requestHeaders: Record<string, string> = {};
250
+ if (init?.headers) {
251
+ if (init.headers instanceof Headers) {
252
+ init.headers.forEach((value: string, key: string) => {
253
+ requestHeaders[key] = value;
254
+ });
255
+ } else if (Array.isArray(init.headers)) {
256
+ init.headers.forEach(([key, value]: [string, string]) => {
257
+ requestHeaders[key] = value;
258
+ });
259
+ } else {
260
+ requestHeaders = init.headers as Record<string, string>;
261
+ }
262
+ }
263
+
241
264
  const tronRequest = {
242
265
  url,
243
- method: init?.method || 'GET',
266
+ method: (init?.method || 'GET') as any,
244
267
  data: init?.body || null,
245
- headers: init?.headers || {},
268
+ headers: requestHeaders,
246
269
  params,
247
270
  };
248
271
 
272
+ // Send api.request event
273
+ (dreaction as any).apiRequest(requestId, tronRequest);
274
+
249
275
  return originalFetch
250
276
  .call(this, input as RequestInfo, init)
251
277
  .then(async (response) => {
@@ -282,6 +308,7 @@ const networking =
282
308
  };
283
309
 
284
310
  (dreaction as any).apiResponse(
311
+ requestId,
285
312
  tronRequest,
286
313
  tronResponse,
287
314
  stopTimer()