You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
150 lines
4.7 KiB
TypeScript
150 lines
4.7 KiB
TypeScript
/*
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import { parse as parseContentType, ParsedMediaType } from "content-type";
|
|
|
|
import { logger } from "../logger";
|
|
import { sleep } from "../utils";
|
|
import { ConnectionError, MatrixError } from "./errors";
|
|
|
|
// Ponyfill for https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout
|
|
export function timeoutSignal(ms: number): AbortSignal {
|
|
const controller = new AbortController();
|
|
setTimeout(() => {
|
|
controller.abort();
|
|
}, ms);
|
|
|
|
return controller.signal;
|
|
}
|
|
|
|
export function anySignal(signals: AbortSignal[]): {
|
|
signal: AbortSignal;
|
|
cleanup(): void;
|
|
} {
|
|
const controller = new AbortController();
|
|
|
|
function cleanup() {
|
|
for (const signal of signals) {
|
|
signal.removeEventListener("abort", onAbort);
|
|
}
|
|
}
|
|
|
|
function onAbort() {
|
|
controller.abort();
|
|
cleanup();
|
|
}
|
|
|
|
for (const signal of signals) {
|
|
if (signal.aborted) {
|
|
onAbort();
|
|
break;
|
|
}
|
|
signal.addEventListener("abort", onAbort);
|
|
}
|
|
|
|
return {
|
|
signal: controller.signal,
|
|
cleanup,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Attempt to turn an HTTP error response into a Javascript Error.
|
|
*
|
|
* If it is a JSON response, we will parse it into a MatrixError. Otherwise
|
|
* we return a generic Error.
|
|
*
|
|
* @param {XMLHttpRequest|Response} response response object
|
|
* @param {String} body raw body of the response
|
|
* @returns {Error}
|
|
*/
|
|
export function parseErrorResponse(response: XMLHttpRequest | Response, body?: string): Error {
|
|
let contentType: ParsedMediaType;
|
|
try {
|
|
contentType = getResponseContentType(response);
|
|
} catch (e) {
|
|
return e;
|
|
}
|
|
|
|
if (contentType?.type === "application/json" && body) {
|
|
return new MatrixError(JSON.parse(body), response.status);
|
|
}
|
|
if (contentType?.type === "text/plain") {
|
|
return new Error(`Server returned ${response.status} error: ${body}`);
|
|
}
|
|
return new Error(`Server returned ${response.status} error`);
|
|
}
|
|
|
|
function isXhr(response: XMLHttpRequest | Response): response is XMLHttpRequest {
|
|
return "getResponseHeader" in response;
|
|
}
|
|
|
|
/**
|
|
* extract the Content-Type header from the response object, and
|
|
* parse it to a `{type, parameters}` object.
|
|
*
|
|
* returns null if no content-type header could be found.
|
|
*
|
|
* @param {XMLHttpRequest|Response} response response object
|
|
* @returns {{type: String, parameters: Object}?} parsed content-type header, or null if not found
|
|
*/
|
|
function getResponseContentType(response: XMLHttpRequest | Response): ParsedMediaType | null {
|
|
let contentType: string | null;
|
|
if (isXhr(response)) {
|
|
contentType = response.getResponseHeader("Content-Type");
|
|
} else {
|
|
contentType = response.headers.get("Content-Type");
|
|
}
|
|
|
|
if (!contentType) return null;
|
|
|
|
try {
|
|
return parseContentType(contentType);
|
|
} catch (e) {
|
|
throw new Error(`Error parsing Content-Type '${contentType}': ${e}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retries a network operation run in a callback.
|
|
* @param {number} maxAttempts maximum attempts to try
|
|
* @param {Function} callback callback that returns a promise of the network operation. If rejected with ConnectionError, it will be retried by calling the callback again.
|
|
* @return {any} the result of the network operation
|
|
* @throws {ConnectionError} If after maxAttempts the callback still throws ConnectionError
|
|
*/
|
|
export async function retryNetworkOperation<T>(maxAttempts: number, callback: () => Promise<T>): Promise<T> {
|
|
let attempts = 0;
|
|
let lastConnectionError: ConnectionError | null = null;
|
|
while (attempts < maxAttempts) {
|
|
try {
|
|
if (attempts > 0) {
|
|
const timeout = 1000 * Math.pow(2, attempts);
|
|
logger.log(`network operation failed ${attempts} times, retrying in ${timeout}ms...`);
|
|
await sleep(timeout);
|
|
}
|
|
return await callback();
|
|
} catch (err) {
|
|
if (err instanceof ConnectionError) {
|
|
attempts += 1;
|
|
lastConnectionError = err;
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
throw lastConnectionError;
|
|
}
|