Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
fix(fetch): AbortController not support issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Dec 19, 2023
1 parent 4e0c272 commit 10f6dbc
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions packages/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export async function serviceRequest<T extends AlwatrServiceResponse = AlwatrSer
let response: Response;
try {
response = await fetch(options);
}
catch (err) {
} catch (err) {

Check failure on line 36 in packages/fetch/src/fetch.ts

View workflow job for this annotation

GitHub Actions / Build & Lint Typescript

Closing curly brace appears on the same line as the subsequent block
const errMessage = (err as Error).message;
if (errMessage !== 'fetch_cache_not_found') {
logger.error('serviceRequest', (err as Error).message || 'fetch_failed', err, options);
Expand All @@ -45,8 +44,7 @@ export async function serviceRequest<T extends AlwatrServiceResponse = AlwatrSer
let responseText: string;
try {
responseText = await response.text();
}
catch (err) {
} catch (err) {

Check failure on line 47 in packages/fetch/src/fetch.ts

View workflow job for this annotation

GitHub Actions / Build & Lint Typescript

Closing curly brace appears on the same line as the subsequent block
logger.error('serviceRequest', 'invalid_response', err, {
response,
});
Expand All @@ -56,8 +54,7 @@ export async function serviceRequest<T extends AlwatrServiceResponse = AlwatrSer
let responseJson: T;
try {
responseJson = JSON.parse(responseText);
}
catch (err) {
} catch (err) {

Check failure on line 57 in packages/fetch/src/fetch.ts

View workflow job for this annotation

GitHub Actions / Build & Lint Typescript

Closing curly brace appears on the same line as the subsequent block
logger.error('serviceRequest', 'invalid_json', err, {responseText});
throw err;
}
Expand All @@ -66,8 +63,7 @@ export async function serviceRequest<T extends AlwatrServiceResponse = AlwatrSer
if (typeof responseJson.errorCode === 'string') {
logger.accident('serviceRequest', responseJson.errorCode, {responseJson});
throw new Error(responseJson.errorCode);
}
else {
} else {

Check failure on line 66 in packages/fetch/src/fetch.ts

View workflow job for this annotation

GitHub Actions / Build & Lint Typescript

Closing curly brace appears on the same line as the subsequent block
logger.error('serviceRequest', 'fetch_nok', {responseJson});
throw new Error('fetch_nok');
}
Expand Down Expand Up @@ -204,8 +200,7 @@ async function _handleCacheStrategy(options: Required<FetchOptions>): Promise<Re
cacheStorage.put(request, networkResponse.clone());
}
return networkResponse;
}
catch (err) {
} catch (err) {

Check failure on line 203 in packages/fetch/src/fetch.ts

View workflow job for this annotation

GitHub Actions / Build & Lint Typescript

Closing curly brace appears on the same line as the subsequent block
const cachedResponse = await cacheStorage.match(request);
if (cachedResponse != null) {
return cachedResponse;
Expand Down Expand Up @@ -268,8 +263,7 @@ async function _handleRemoveDuplicate(options: Required<FetchOptions>): Promise<
}

return response.clone();
}
catch (err) {
} catch (err) {

Check failure on line 266 in packages/fetch/src/fetch.ts

View workflow job for this annotation

GitHub Actions / Build & Lint Typescript

Closing curly brace appears on the same line as the subsequent block
// clean cache on any error.
delete duplicateRequestStorage[cacheKey];
throw err;
Expand All @@ -295,8 +289,7 @@ async function _handleRetryPattern(options: Required<FetchOptions>): Promise<Res
}
// else
throw new Error('fetch_server_error');
}
catch (err) {
} catch (err) {

Check failure on line 292 in packages/fetch/src/fetch.ts

View workflow job for this annotation

GitHub Actions / Build & Lint Typescript

Closing curly brace appears on the same line as the subsequent block
logger.accident('fetch', 'fetch_failed_retry', err);

if (globalScope.navigator?.onLine === false) {
Expand All @@ -320,21 +313,20 @@ function _handleTimeout(options: FetchOptions): Promise<Response> {
// else
logger.logMethod?.('_handleTimeout');
return new Promise((resolved, reject) => {
// TODO: AbortController polyfill
const abortController = new AbortController();
const abortController = typeof globalScope.AbortController === 'function' ? new AbortController() : null;
const externalAbortSignal = options.signal;
options.signal = abortController.signal;
options.signal = abortController?.signal;

const timeoutId = setTimeout(() => {
reject(new Error('fetch_timeout'));
abortController.abort('fetch_timeout');
}, options.timeout);

if (externalAbortSignal != null) {
if (abortController !== null && externalAbortSignal != null) {
// Respect external abort signal
externalAbortSignal.addEventListener('abort', () => abortController.abort(), {once: true});
}

const timeoutId = setTimeout(() => {
reject(new Error('fetch_timeout'));
abortController?.abort('fetch_timeout');
}, options.timeout);

// abortController.signal.addEventListener('abort', () => {
// logger.incident('fetch', 'fetch_abort_signal', {
// reason: abortController.signal.reason,
Expand Down

0 comments on commit 10f6dbc

Please sign in to comment.