@@ -26,14 +26,24 @@ class WebReactNativeTransport {
2626 /**
2727 * Create and configure transport provider for Web and Rect environments.
2828 *
29+ * @param originalFetch - Pointer to the original (not monkey patched) `fetch` implementation.
2930 * @param [keepAlive] - Whether client should try to keep connections open for reuse or not.
3031 * @param logVerbosity - Whether verbose logs should be printed or not.
3132 *
3233 * @internal
3334 */
34- constructor ( keepAlive = false , logVerbosity ) {
35+ constructor ( originalFetch , keepAlive = false , logVerbosity = false ) {
3536 this . keepAlive = keepAlive ;
3637 this . logVerbosity = logVerbosity ;
38+ WebReactNativeTransport . originalFetch = originalFetch ;
39+ // Check whether `fetch` has been monkey patched or not.
40+ if ( logVerbosity && this . isFetchMonkeyPatched ( ) ) {
41+ console . warn ( "[PubNub] Native Web Fetch API 'fetch' function monkey patched." ) ;
42+ if ( ! this . isFetchMonkeyPatched ( WebReactNativeTransport . originalFetch ) )
43+ console . info ( "[PubNub] Use native Web Fetch API 'fetch' implementation from iframe as APM workaround." ) ;
44+ else
45+ console . warn ( '[PubNub] Unable receive native Web Fetch API. There can be issues with subscribe long-poll cancellation' ) ;
46+ }
3747 }
3848 makeSendable ( req ) {
3949 let controller ;
@@ -63,7 +73,11 @@ class WebReactNativeTransport {
6373 } , req . timeout * 1000 ) ;
6474 } ) ;
6575 return Promise . race ( [
66- fetch ( request , { signal : abortController === null || abortController === void 0 ? void 0 : abortController . signal , credentials : 'omit' , cache : 'no-cache' } ) ,
76+ WebReactNativeTransport . originalFetch ( request , {
77+ signal : abortController === null || abortController === void 0 ? void 0 : abortController . signal ,
78+ credentials : 'omit' ,
79+ cache : 'no-cache' ,
80+ } ) ,
6781 requestTimeout ,
6882 ] )
6983 . then ( ( response ) => response . arrayBuffer ( ) . then ( ( arrayBuffer ) => [ response , arrayBuffer ] ) )
@@ -178,6 +192,17 @@ class WebReactNativeTransport {
178192 console . log ( '-----' ) ;
179193 }
180194 }
195+ /**
196+ * Check whether original `fetch` has been monkey patched or not.
197+ *
198+ * @returns `true` if original `fetch` has been patched.
199+ *
200+ * @internal
201+ */
202+ isFetchMonkeyPatched ( oFetch ) {
203+ const fetchString = ( oFetch !== null && oFetch !== void 0 ? oFetch : fetch ) . toString ( ) ;
204+ return ! fetchString . includes ( '[native code]' ) && fetch . name !== 'fetch' ;
205+ }
181206}
182207exports . WebReactNativeTransport = WebReactNativeTransport ;
183208/**
0 commit comments