Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Blob error for use fetch if Network Inspect enabled #217

Merged
merged 5 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/middlewares/delta/DeltaPatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @format
*/

import { checkFetchExists, patchFetchPolyfill } from './patchFetchPolyfill';

/* eslint-disable no-underscore-dangle */

/**
Expand Down Expand Up @@ -35,6 +37,7 @@ export default class DeltaPatcher {
this._initialized = false;
this._lastNumModifiedFiles = 0;
this._lastModifiedDate = new Date();
this._fetchPolyfillPatched = false;
}

static get(id) {
Expand All @@ -58,6 +61,7 @@ export default class DeltaPatcher {
}

this._initialized = true;
this._fetchPolyfillPatched = false;

// Reset the current delta when we receive a fresh delta.
if (deltaBundle.reset) {
Expand All @@ -76,9 +80,9 @@ export default class DeltaPatcher {
this._lastModifiedDate = new Date();
}

this._patchMap(this._lastBundle.pre, deltaBundle.pre);
this._patchMap(this._lastBundle.post, deltaBundle.post);
this._patchMap(this._lastBundle.modules, deltaBundle.delta);
this._patchMap(this._lastBundle.pre, deltaBundle.pre, 'pre');
this._patchMap(this._lastBundle.post, deltaBundle.post, 'post');
this._patchMap(this._lastBundle.modules, deltaBundle.delta, 'delta');

this._lastBundle.id = deltaBundle.id;

Expand Down Expand Up @@ -111,10 +115,13 @@ export default class DeltaPatcher {
);
}

_patchMap(original, patch) {
_patchMap(original, patch, type) {
for (const [key, value] of patch.entries()) {
if (value == null) {
original.delete(key);
} else if (type === 'delta' && !this._fetchPolyfillPatched && checkFetchExists(value)) {
this._fetchPolyfillPatched = true;
original.set(key, patchFetchPolyfill(value));
} else {
original.set(key, value);
}
Expand Down
12 changes: 12 additions & 0 deletions app/middlewares/delta/patchFetchPolyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* See https://github.com/jhen0409/react-native-debugger/issues/209
*
* Patch whatwg-fetch code to get `support` var,
* so we could use it to fix the issue.
*/
const isFetch = '"node_modules/whatwg-fetch/fetch.js"';
const flag = /(if \(self.fetch\) {\n\s+return;\n\s+}\n\s+var support )(=)( {)/g;
const replaceStr = '$1= self._fetchSupport =$3';

export const checkFetchExists = code => code.indexOf(isFetch) > -1;
export const patchFetchPolyfill = code => code.replace(flag, replaceStr);
11 changes: 11 additions & 0 deletions app/worker/networkInspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ const isWorkerMethod = fn => String(fn).indexOf('[native code]') > -1;

let networkInspect;

/* eslint-disable no-underscore-dangle */
export const toggleNetworkInspect = enabled => {
if (!enabled && networkInspect) {
window.XMLHttpRequest = networkInspect.XMLHttpRequest;
window.FormData = networkInspect.FormData;
networkInspect = null;
if (window._fetchSupport) {
window._fetchSupport.blob = !!window._fetchSupport._blob;
}
return;
}
if (!enabled) return;
Expand All @@ -30,6 +34,13 @@ export const toggleNetworkInspect = enabled => {
: window.XMLHttpRequest;
window.FormData = window.originalFormData ? window.originalFormData : window.FormData;

// Disable `support.blob` in `whatwg-fetch` for use native XMLHttpRequest,
// See https://github.com/jhen0409/react-native-debugger/issues/56
if (window._fetchSupport) {
window._fetchSupport._blob = window._fetchSupport.blob;
window._fetchSupport.blob = false;
}

console.log(
'[RNDebugger]',
'Network Inspect is enabled,',
Expand Down
5 changes: 3 additions & 2 deletions app/worker/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
self.global = self;

/*
* Currently Blob is not supported for RN,
* we should remove it in WebWorker because it will used for `whatwg-fetch`
* Blob is not supported for RN < 0.54,
* we should remove it in WebWorker because
* it will used for `whatwg-fetch` on older RN versions
*/
if (self.Blob && self.Blob.toString() === 'function Blob() { [native code] }') {
delete self.Blob;
Expand Down