Debian Codegraph @

pretender/src/create-passthrough.ts

raw
1 export function createPassthrough(fakeXHR, nativeXMLHttpRequest) {
2 // event types to handle on the xhr
3 var evts = ['error', 'timeout', 'abort', 'readystatechange'];
4
5 // event types to handle on the xhr.upload
6 var uploadEvents = [];
7
8 // properties to copy from the native xhr to fake xhr
9 var lifecycleProps = [
10 'readyState',
11 'responseText',
12 'response',
13 'responseXML',
14 'responseURL',
15 'status',
16 'statusText',
17 ];
18
19 var xhr = (fakeXHR._passthroughRequest = new nativeXMLHttpRequest());
20 xhr.open(
21 fakeXHR.method,
22 fakeXHR.url,
23 fakeXHR.async,
24 fakeXHR.username,
25 fakeXHR.password
26 );
27
28 if (fakeXHR.responseType === 'arraybuffer') {
29 lifecycleProps = ['readyState', 'response', 'status', 'statusText'];
30 xhr.responseType = fakeXHR.responseType;
31 }
32
33 // use onload if the browser supports it
34 if ('onload' in xhr) {
35 evts.push('load');
36 }
37
38 // add progress event for async calls
39 // avoid using progress events for sync calls, they will hang https://bugs.webkit.org/show_bug.cgi?id=40996.
40 if (fakeXHR.async && fakeXHR.responseType !== 'arraybuffer') {
41 evts.push('progress');
42 uploadEvents.push('progress');
43 }
44
45 // update `propertyNames` properties from `fromXHR` to `toXHR`
46 function copyLifecycleProperties(propertyNames, fromXHR, toXHR) {
47 for (var i = 0; i < propertyNames.length; i++) {
48 var prop = propertyNames[i];
49 if (prop in fromXHR) {
50 toXHR[prop] = fromXHR[prop];
51 }
52 }
53 }
54
55 // fire fake event on `eventable`
56 function dispatchEvent(eventable, eventType, event) {
57 eventable.dispatchEvent(event);
58 if (eventable['on' + eventType]) {
59 eventable['on' + eventType](event);
60 }
61 }
62
63 // set the on- handler on the native xhr for the given eventType
64 function createHandler(eventType) {
65 xhr['on' + eventType] = function (event) {
66 copyLifecycleProperties(lifecycleProps, xhr, fakeXHR);
67 dispatchEvent(fakeXHR, eventType, event);
68 };
69 }
70
71 // set the on- handler on the native xhr's `upload` property for
72 // the given eventType
73 function createUploadHandler(eventType) {
74 if (xhr.upload && fakeXHR.upload && fakeXHR.upload['on' + eventType]) {
75 xhr.upload['on' + eventType] = function (event) {
76 dispatchEvent(fakeXHR.upload, eventType, event);
77 };
78 }
79 }
80
81 var i;
82 for (i = 0; i < evts.length; i++) {
83 createHandler(evts[i]);
84 }
85 for (i = 0; i < uploadEvents.length; i++) {
86 createUploadHandler(uploadEvents[i]);
87 }
88
89 if (fakeXHR.async) {
90 xhr.timeout = fakeXHR.timeout;
91 xhr.withCredentials = fakeXHR.withCredentials;
92 }
93 // XMLHttpRequest.timeout default initializes to 0, and is not allowed to be used for
94 // synchronous XMLHttpRequests requests in a document environment. However, when a XHR
95 // polyfill does not sets the timeout value, it will throw in React Native environment.
96 // TODO:
97 // synchronous XHR is deprecated, make async the default as XMLHttpRequest.open(),
98 // and throw error if sync XHR has timeout not 0
99 if (!xhr.timeout && xhr.timeout !== 0) {
100 xhr.timeout = 0; // default XMLHttpRequest timeout
101 }
102 for (var h in fakeXHR.requestHeaders) {
103 xhr.setRequestHeader(h, fakeXHR.requestHeaders[h]);
104 }
105 return xhr;
106 }