fix(zone.js): should not try to patch fetch if it is not writable (#36311)

Close #36142

In Firefox extensions, the `window.fetch` is not configurable, that means

```
const desc = Object.getOwnPropertyDescriptor(window, 'fetch');
desc.writable === false;
```

So in this case, we should not try to patch `fetch`, otherwise, it will
throw error ('fetch is ReadOnly`)

PR Close #36311
This commit is contained in:
JiaLiPassion 2020-03-30 09:14:02 +09:00 committed by Kara Erickson
parent 93302b7fb8
commit 416c786774
1 changed files with 5 additions and 7 deletions

View File

@ -1,3 +1,5 @@
import {patchMethod} from './utils';
/** /**
* @license * @license
* Copyright Google Inc. All Rights Reserved. * Copyright Google Inc. All Rights Reserved.
@ -500,8 +502,8 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
api.patchThen = patchThen; api.patchThen = patchThen;
function zoneify(fn: Function) { function zoneify(fn: Function) {
return function(this: unknown) { return function(self: any, args: any[]) {
let resultPromise = fn.apply(this, arguments); let resultPromise = fn.apply(self, args);
if (resultPromise instanceof ZoneAwarePromise) { if (resultPromise instanceof ZoneAwarePromise) {
return resultPromise; return resultPromise;
} }
@ -515,11 +517,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
if (NativePromise) { if (NativePromise) {
patchThen(NativePromise); patchThen(NativePromise);
const fetch = global['fetch']; patchMethod(global, 'fetch', delegate => zoneify(delegate));
if (typeof fetch == 'function') {
global[api.symbol('fetch')] = fetch;
global['fetch'] = zoneify(fetch);
}
} }
// This is not part of public API, but it is useful for tests, so we expose it. // This is not part of public API, but it is useful for tests, so we expose it.