Move from browser-request to fetch (#23427)

This commit is contained in:
Michael Telatynski 2022-10-12 18:59:10 +01:00 committed by GitHub
parent 326a1a9056
commit 2ef6abbfb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 169 additions and 148 deletions

View file

@ -18,8 +18,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// To ensure we load the browser-request version
import "matrix-js-sdk"; // eslint-disable-line no-restricted-imports
// To ensure we load the browser-matrix version first
import "matrix-js-sdk/src/browser-index";
import React from 'react';
import PlatformPeg from 'matrix-react-sdk/src/PlatformPeg';

View file

@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import request from 'browser-request';
import type { IConfigOptions } from "matrix-react-sdk/src/IConfigOptions";
// Load the config file. First try to load up a domain-specific config of the
@ -32,44 +30,28 @@ export async function getVectorConfig(relativeLocation=''): Promise<IConfigOptio
if (Object.keys(configJson).length === 0) {
throw new Error(); // throw to enter the catch
}
return configJson as IConfigOptions;
return configJson;
} catch (e) {
return await generalConfigPromise as IConfigOptions;
return generalConfigPromise;
}
}
function getConfig(configJsonFilename: string): Promise<{}> {
return new Promise(function(resolve, reject) {
request(
{ method: "GET", url: configJsonFilename, qs: { cachebuster: Date.now() } },
(err, response, body) => {
try {
if (err || response.status < 200 || response.status >= 300) {
// Lack of a config isn't an error, we should
// just use the defaults.
// Also treat a blank config as no config, assuming
// the status code is 0, because we don't get 404s
// from file: URIs so this is the only way we can
// not fail if the file doesn't exist when loading
// from a file:// URI.
if (response) {
if (response.status == 404 || (response.status == 0 && body == '')) {
resolve({});
}
}
reject({ err: err, response: response });
return;
}
// We parse the JSON ourselves rather than use the JSON
// parameter, since this throws a parse error on empty
// which breaks if there's no config.json and we're
// loading from the filesystem (see above).
resolve(JSON.parse(body));
} catch (e) {
reject({ err: e });
}
},
);
async function getConfig(configJsonFilename: string): Promise<IConfigOptions> {
const url = new URL(configJsonFilename, window.location.href);
url.searchParams.set("cachebuster", Date.now().toString());
const res = await fetch(url, {
cache: "no-cache",
method: "GET",
});
if (res.status === 404 || res.status === 0) {
// Lack of a config isn't an error, we should just use the defaults.
// Also treat a blank config as no config, assuming the status code is 0, because we don't get 404s from file:
// URIs so this is the only way we can not fail if the file doesn't exist when loading from a file:// URI.
return {} as IConfigOptions;
}
if (res.ok) {
return res.json();
}
}

View file

@ -17,7 +17,6 @@ limitations under the License.
*/
import { UpdateCheckStatus, UpdateStatus } from "matrix-react-sdk/src/BasePlatform";
import request from 'browser-request';
import dis from 'matrix-react-sdk/src/dispatcher/dispatcher';
import { _t } from 'matrix-react-sdk/src/languageHandler';
import { hideToast as hideUpdateToast, showToast as showUpdateToast } from "matrix-react-sdk/src/toasts/UpdateToast";
@ -87,31 +86,17 @@ export default class WebPlatform extends VectorBasePlatform {
});
}
private getMostRecentVersion(): Promise<string> {
// We add a cachebuster to the request to make sure that we know about
// the most recent version on the origin server. That might not
// actually be the version we'd get on a reload (particularly in the
// presence of intermediate caching proxies), but still: we're trying
// to tell the user that there is a new version.
return new Promise((resolve, reject) => {
request(
{
method: "GET",
url: "version",
qs: { cachebuster: Date.now() },
},
(err, response, body) => {
if (err || response.status < 200 || response.status >= 300) {
if (err === null) err = { status: response.status };
reject(err);
return;
}
resolve(getNormalizedAppVersion(body.trim()));
},
);
private async getMostRecentVersion(): Promise<string> {
const res = await fetch("version", {
method: "GET",
cache: "no-cache",
});
if (res.ok) {
return getNormalizedAppVersion(await res.text());
}
return Promise.reject({ status: res.status });
}
public getAppVersion(): Promise<string> {