Skip to content

Commit d686316

Browse files
cyfung1031CodFrm
andauthored
🔥 FirefoxMV2: 跟随MV3 移除 axios (#1339)
* FirefoxMV2: 跟随MV3 移除 axios * 🔧 修复资源管理器测试:将 axios mock 替换为 fetch mock --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent 179b711 commit d686316

9 files changed

Lines changed: 205 additions & 84 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tailwind.config.js
3232

3333
superpowers
3434
.claude
35+
.omc
3536
CLAUDE.md
3637

3738
test-results

package-lock.json

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"@dnd-kit/utilities": "^3.2.2",
3030
"@emotion/react": "^11.10.4",
3131
"@emotion/styled": "^11.10.4",
32-
"axios": "^1.13.2",
3332
"core-js": "^3.47.0",
3433
"cron": "^2.4.4",
3534
"crx": "^5.0.1",

pkg/filesystem/auth.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable camelcase */
22
/* eslint-disable import/prefer-default-export */
3-
import { ExtServer } from "@App/app/const";
4-
import { api } from "@App/pkg/axios";
3+
import { ExtServer, ExtServerApi } from "@App/app/const";
54
import { WarpTokenError } from "./error";
65

76
type NetDiskType = "baidu" | "onedrive";
@@ -11,11 +10,7 @@ export function GetNetDiskToken(netDiskType: NetDiskType): Promise<{
1110
msg: string;
1211
data: { token: { access_token: string; refresh_token: string } };
1312
}> {
14-
return api
15-
.get(`/auth/net-disk/token?netDiskType=${netDiskType}`)
16-
.then((resp) => {
17-
return resp.data;
18-
});
13+
return fetch(ExtServerApi + `auth/net-disk/token?netDiskType=${netDiskType}`).then((resp) => resp.json());
1914
}
2015

2116
export function RefreshToken(
@@ -26,14 +21,16 @@ export function RefreshToken(
2621
msg: string;
2722
data: { token: { access_token: string; refresh_token: string } };
2823
}> {
29-
return api
30-
.post(`/auth/net-disk/token/refresh?netDiskType=${netDiskType}`, {
24+
return fetch(ExtServerApi + `auth/net-disk/token/refresh?netDiskType=${netDiskType}`, {
25+
method: "POST",
26+
headers: {
27+
"Content-Type": "application/json",
28+
},
29+
body: JSON.stringify({
3130
netDiskType,
3231
refreshToken,
33-
})
34-
.then((resp) => {
35-
return resp.data;
36-
});
32+
}),
33+
}).then((resp) => resp.json());
3734
}
3835

3936
export function NetDisk(netDiskType: NetDiskType) {

src/app/const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { version } from "../../package.json";
33
export const ExtVersion = version;
44

55
export const ExtServer = "https://ext.scriptcat.org/";
6+
export const ExtServerApi = ExtServer + "api/v1/";
67

78
export const ExternalWhitelist = [
89
"greasyfork.org",

src/app/service/resource/manager.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ import {
1010
} from "@App/app/repo/resource";
1111
import { ResourceLinkDAO } from "@App/app/repo/resource_link";
1212
import { Script } from "@App/app/repo/scripts";
13-
import axios from "axios";
1413
import Cache from "@App/app/cache";
1514
import { blobToBase64 } from "@App/pkg/utils/utils";
1615
import CacheKey from "@App/pkg/utils/cache_key";
1716
import { isText } from "@App/pkg/utils/istextorbinary";
1817
import Manager from "../manager";
1918
import { calculateHashFromArrayBuffer } from "@App/pkg/utils/crypto";
2019
import { base64ToHex, isBase64 } from "./utils";
20+
import { blobToUint8Array } from "@App/pkg/utils/datatype";
2121

2222
// 资源管理器,负责资源的更新获取等操作
2323

2424
function calculateHash(blob: Blob): Promise<ResourceHash> {
2525
return new Promise((resolve) => {
2626
const reader = new FileReader();
2727
reader.readAsArrayBuffer(blob);
28-
reader.onloadend = () => {
29-
if (!reader.result) {
28+
reader.onloadend = function () {
29+
if (!this.result) {
3030
resolve({
3131
md5: "",
3232
sha1: "",
@@ -35,7 +35,7 @@ function calculateHash(blob: Blob): Promise<ResourceHash> {
3535
sha512: "",
3636
});
3737
} else {
38-
resolve(calculateHashFromArrayBuffer(<ArrayBuffer>reader.result));
38+
resolve(calculateHashFromArrayBuffer(<ArrayBuffer>this.result));
3939
}
4040
};
4141
});
@@ -366,41 +366,34 @@ export class ResourceManager extends Manager {
366366
return Promise.resolve(undefined);
367367
}
368368

369-
loadByUrl(url: string, type: ResourceType): Promise<Resource> {
370-
return new Promise((resolve, reject) => {
371-
const u = this.parseUrl(url);
372-
axios
373-
.get(u.url, {
374-
responseType: "blob",
375-
})
376-
.then(async (response) => {
377-
if (response.status !== 200) {
378-
return reject(
379-
new Error(`resource response status not 200:${response.status}`)
380-
);
381-
}
382-
const resource: Resource = {
383-
id: 0,
384-
url: u.url,
385-
content: "",
386-
contentType: (
387-
response.headers["content-type"] || "application/octet-stream"
388-
).split(";")[0],
389-
hash: await calculateHash(<Blob>response.data),
390-
base64: "",
391-
type,
392-
createtime: new Date().getTime(),
393-
};
394-
const arrayBuffer = await (<Blob>response.data).arrayBuffer();
395-
const uint8Array = new Uint8Array(arrayBuffer);
396-
if (isText(uint8Array)) {
397-
resource.content = await (<Blob>response.data).text();
398-
}
399-
resource.base64 = (await blobToBase64(<Blob>response.data)) || "";
400-
return resolve(resource);
401-
})
402-
.catch((e) => reject(e));
403-
});
369+
async loadByUrl(url: string, type: ResourceType): Promise<Resource> {
370+
const u = this.parseUrl(url);
371+
const resp = await fetch(u.url);
372+
if (resp.status !== 200) {
373+
throw new Error(`resource response status not 200: ${resp.status}`);
374+
}
375+
const data = await resp.blob();
376+
const [hash, uint8Array, base64] = await Promise.all([
377+
calculateHash(data),
378+
blobToUint8Array(data),
379+
blobToBase64(data),
380+
]);
381+
const contentType = resp.headers.get("content-type");
382+
const resource: Resource = {
383+
id: 0,
384+
url: u.url,
385+
content: "",
386+
contentType: (contentType || "application/octet-stream").split(";")[0],
387+
hash: hash,
388+
base64: "",
389+
type,
390+
createtime: Date.now(),
391+
};
392+
if (isText(uint8Array)) {
393+
resource.content = await data.text();
394+
}
395+
resource.base64 = base64 || "";
396+
return resource;
404397
}
405398

406399
parseUrl(url: string): {

0 commit comments

Comments
 (0)