@@ -14,48 +14,34 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
1414 } ) ;
1515} ;
1616Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
17- exports . WebReactNativeTransport = void 0 ;
17+ exports . ReactNativeTransport = void 0 ;
1818const pubnub_api_error_1 = require ( "../errors/pubnub-api-error" ) ;
1919const utils_1 = require ( "../core/utils" ) ;
2020/**
21- * Class representing a `fetch`-based browser and React Native transport provider.
21+ * Class representing a React Native transport provider.
2222 *
2323 * @internal
2424 */
25- class WebReactNativeTransport {
25+ class ReactNativeTransport {
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.
3029 * @param [keepAlive] - Whether client should try to keep connections open for reuse or not.
3130 * @param logVerbosity - Whether verbose logs should be printed or not.
3231 *
3332 * @internal
3433 */
35- constructor ( originalFetch , keepAlive = false , logVerbosity = false ) {
34+ constructor ( keepAlive = false , logVerbosity = false ) {
3635 this . keepAlive = keepAlive ;
3736 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- }
4737 }
4838 makeSendable ( req ) {
49- let controller ;
50- let abortController ;
51- if ( req . cancellable ) {
52- abortController = new AbortController ( ) ;
53- controller = {
54- // Storing controller inside to prolong object lifetime.
55- abortController,
56- abort : ( ) => abortController === null || abortController === void 0 ? void 0 : abortController . abort ( ) ,
57- } ;
58- }
39+ const abortController = new AbortController ( ) ;
40+ const controller = {
41+ // Storing controller inside to prolong object lifetime.
42+ abortController,
43+ abort : ( ) => ! abortController . signal . aborted && abortController . abort ( ) ,
44+ } ;
5945 return [
6046 this . requestFromTransportRequest ( req ) . then ( ( request ) => {
6147 const start = new Date ( ) . getTime ( ) ;
@@ -65,21 +51,27 @@ class WebReactNativeTransport {
6551 *
6652 * **Note:** Native Fetch API doesn't support `timeout` out-of-box.
6753 */
54+ let timeoutId ;
6855 const requestTimeout = new Promise ( ( _ , reject ) => {
69- const timeoutId = setTimeout ( ( ) => {
70- // Clean up.
56+ timeoutId = setTimeout ( ( ) => {
7157 clearTimeout ( timeoutId ) ;
7258 reject ( new Error ( 'Request timeout' ) ) ;
59+ controller . abort ( ) ;
7360 } , req . timeout * 1000 ) ;
7461 } ) ;
7562 return Promise . race ( [
76- WebReactNativeTransport . originalFetch ( request , {
77- signal : abortController === null || abortController === void 0 ? void 0 : abortController . signal ,
63+ fetch ( request , {
64+ signal : abortController . signal ,
7865 credentials : 'omit' ,
7966 cache : 'no-cache' ,
8067 } ) ,
8168 requestTimeout ,
8269 ] )
70+ . then ( ( response ) => {
71+ if ( timeoutId )
72+ clearTimeout ( timeoutId ) ;
73+ return response ;
74+ } )
8375 . then ( ( response ) => response . arrayBuffer ( ) . then ( ( arrayBuffer ) => [ response , arrayBuffer ] ) )
8476 . then ( ( response ) => {
8577 const responseBody = response [ 1 ] . byteLength > 0 ? response [ 1 ] : undefined ;
@@ -177,7 +169,7 @@ class WebReactNativeTransport {
177169 if ( typeof requestBody === 'string' )
178170 outgoing += `\n\n${ requestBody } ` ;
179171 else
180- outgoing += `\n\n${ WebReactNativeTransport . decoder . decode ( requestBody ) } ` ;
172+ outgoing += `\n\n${ ReactNativeTransport . decoder . decode ( requestBody ) } ` ;
181173 }
182174 console . log ( `<<<<<` ) ;
183175 console . log ( outgoing ) ;
@@ -186,28 +178,17 @@ class WebReactNativeTransport {
186178 else {
187179 let outgoing = `[${ timestamp } / ${ elapsed } ]\n${ protocol } //${ host } ${ pathname } \n${ search } ` ;
188180 if ( body )
189- outgoing += `\n\n${ WebReactNativeTransport . decoder . decode ( body ) } ` ;
181+ outgoing += `\n\n${ ReactNativeTransport . decoder . decode ( body ) } ` ;
190182 console . log ( '>>>>>>' ) ;
191183 console . log ( outgoing ) ;
192184 console . log ( '-----' ) ;
193185 }
194186 }
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- }
206187}
207- exports . WebReactNativeTransport = WebReactNativeTransport ;
188+ exports . ReactNativeTransport = ReactNativeTransport ;
208189/**
209190 * Service {@link ArrayBuffer} response decoder.
210191 *
211192 * @internal
212193 */
213- WebReactNativeTransport . decoder = new TextDecoder ( ) ;
194+ ReactNativeTransport . decoder = new TextDecoder ( ) ;
0 commit comments