|
3370 | 3370 | /** |
3371 | 3371 | * Create and configure transport provider for Web and Rect environments. |
3372 | 3372 | * |
| 3373 | + * @param originalFetch - Pointer to the original (not monkey patched) `fetch` implementation. |
3373 | 3374 | * @param [keepAlive] - Whether client should try to keep connections open for reuse or not. |
3374 | 3375 | * @param logVerbosity - Whether verbose logs should be printed or not. |
3375 | 3376 | * |
3376 | 3377 | * @internal |
3377 | 3378 | */ |
3378 | | - constructor(keepAlive = false, logVerbosity) { |
| 3379 | + constructor(originalFetch, keepAlive = false, logVerbosity = false) { |
3379 | 3380 | this.keepAlive = keepAlive; |
3380 | 3381 | this.logVerbosity = logVerbosity; |
| 3382 | + WebReactNativeTransport.originalFetch = originalFetch; |
| 3383 | + // Check whether `fetch` has been monkey patched or not. |
| 3384 | + if (logVerbosity && this.isFetchMonkeyPatched()) { |
| 3385 | + console.warn("[PubNub] Native Web Fetch API 'fetch' function monkey patched."); |
| 3386 | + if (!this.isFetchMonkeyPatched(WebReactNativeTransport.originalFetch)) |
| 3387 | + console.info("[PubNub] Use native Web Fetch API 'fetch' implementation from iframe as APM workaround."); |
| 3388 | + else |
| 3389 | + console.warn('[PubNub] Unable receive native Web Fetch API. There can be issues with subscribe long-poll cancellation'); |
| 3390 | + } |
3381 | 3391 | } |
3382 | 3392 | makeSendable(req) { |
3383 | 3393 | let controller; |
|
3407 | 3417 | }, req.timeout * 1000); |
3408 | 3418 | }); |
3409 | 3419 | return Promise.race([ |
3410 | | - fetch(request, { signal: abortController === null || abortController === void 0 ? void 0 : abortController.signal, credentials: 'omit', cache: 'no-cache' }), |
| 3420 | + WebReactNativeTransport.originalFetch(request, { |
| 3421 | + signal: abortController === null || abortController === void 0 ? void 0 : abortController.signal, |
| 3422 | + credentials: 'omit', |
| 3423 | + cache: 'no-cache', |
| 3424 | + }), |
3411 | 3425 | requestTimeout, |
3412 | 3426 | ]) |
3413 | 3427 | .then((response) => response.arrayBuffer().then((arrayBuffer) => [response, arrayBuffer])) |
|
3522 | 3536 | console.log('-----'); |
3523 | 3537 | } |
3524 | 3538 | } |
| 3539 | + /** |
| 3540 | + * Check whether original `fetch` has been monkey patched or not. |
| 3541 | + * |
| 3542 | + * @returns `true` if original `fetch` has been patched. |
| 3543 | + * |
| 3544 | + * @internal |
| 3545 | + */ |
| 3546 | + isFetchMonkeyPatched(oFetch) { |
| 3547 | + const fetchString = (oFetch !== null && oFetch !== void 0 ? oFetch : fetch).toString(); |
| 3548 | + return !fetchString.includes('[native code]') && fetch.name !== 'fetch'; |
| 3549 | + } |
3525 | 3550 | } |
3526 | 3551 | /** |
3527 | 3552 | * Service {@link ArrayBuffer} response decoder. |
|
3975 | 4000 | return base.PubNubFile; |
3976 | 4001 | }, |
3977 | 4002 | get version() { |
3978 | | - return '8.7.0'; |
| 4003 | + return '8.7.1'; |
3979 | 4004 | }, |
3980 | 4005 | getVersion() { |
3981 | 4006 | return this.version; |
|
14544 | 14569 | let cryptography; |
14545 | 14570 | cryptography = new WebCryptography(); |
14546 | 14571 | // Setup transport provider. |
14547 | | - let transport = new WebReactNativeTransport(clientConfiguration.keepAlive, clientConfiguration.logVerbosity); |
| 14572 | + let transport = new WebReactNativeTransport(PubNub.originalFetch(), clientConfiguration.keepAlive, clientConfiguration.logVerbosity); |
14548 | 14573 | { |
14549 | 14574 | if (configurationCopy.subscriptionWorkerUrl) { |
14550 | 14575 | // Inject subscription worker into transport provider stack. |
|
14593 | 14618 | this.listenerManager.announceNetworkUp(); |
14594 | 14619 | this.reconnect(); |
14595 | 14620 | } |
| 14621 | + static originalFetch() { |
| 14622 | + let iframe = document.querySelector('iframe[name="pubnub-context-unpatched-fetch"]'); |
| 14623 | + if (!iframe) { |
| 14624 | + iframe = document.createElement('iframe'); |
| 14625 | + iframe.style.display = 'none'; |
| 14626 | + iframe.name = 'pubnub-context-unpatched-fetch'; |
| 14627 | + iframe.src = 'about:blank'; |
| 14628 | + document.body.appendChild(iframe); |
| 14629 | + } |
| 14630 | + if (iframe.contentWindow) |
| 14631 | + return iframe.contentWindow.fetch.bind(iframe.contentWindow); |
| 14632 | + return fetch; |
| 14633 | + } |
14596 | 14634 | } |
14597 | 14635 | /** |
14598 | 14636 | * Data encryption / decryption module constructor. |
|
0 commit comments