Skip to content

Commit c0e76ca

Browse files
cyfung1031CodFrmCopilot
authored
修正雙重Promise (double-wrapped) (#482)
* 修正雙重Promise (double-wrapped) * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: wangyizhi <i@xloli.top> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7c6ba17 commit c0e76ca

35 files changed

Lines changed: 422 additions & 458 deletions

File tree

packages/chrome-extension-mock/cookies.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ export default class Cookies {
1313
callback: (cookies: chrome.cookies.Cookie[]) => void
1414
) => void | undefined;
1515

16-
getAll(
16+
async getAll(
1717
details: chrome.cookies.GetAllDetails,
1818
callback: (cookies: chrome.cookies.Cookie[]) => void
1919
): Promise<chrome.cookies.Cookie[]> {
2020
this.mockGetAll?.(details, callback);
21-
return Promise.resolve([]);
21+
return [];
2222
}
2323

2424
set(details: chrome.cookies.SetDetails, callback?: () => void): void {

packages/filesystem/auth.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,14 @@ export type Token = {
7878
};
7979

8080
export async function AuthVerify(netDiskType: NetDiskType, invalid?: boolean) {
81-
let token: Token | undefined;
81+
let token: Token | undefined = undefined;
8282
const localStorageDao = new LocalStorageDAO();
8383
const key = `netdisk:token:${netDiskType}`;
8484
try {
85-
token = await localStorageDao.get(key).then((resp) => {
86-
if (resp) {
87-
return resp.value;
88-
}
89-
return undefined;
90-
});
85+
const resp = await localStorageDao.get(key);
86+
if (resp) {
87+
token = resp.value;
88+
}
9189
} catch (e) {
9290
// ignore
9391
}
@@ -97,7 +95,7 @@ export async function AuthVerify(netDiskType: NetDiskType, invalid?: boolean) {
9795
await NetDisk(netDiskType);
9896
const resp = await GetNetDiskToken(netDiskType);
9997
if (resp.code !== 0) {
100-
return Promise.reject(new WarpTokenError(new Error(resp.msg)));
98+
throw new WarpTokenError(new Error(resp.msg));
10199
}
102100
token = {
103101
accessToken: resp.data.token.access_token,
@@ -119,9 +117,9 @@ export async function AuthVerify(netDiskType: NetDiskType, invalid?: boolean) {
119117
await localStorageDao.delete(key);
120118
// 刷新失败,并且标记失效,尝试重新获取token
121119
if (invalid) {
122-
return AuthVerify(netDiskType);
120+
return await AuthVerify(netDiskType);
123121
}
124-
return Promise.reject(new WarpTokenError(new Error(resp.msg)));
122+
throw new WarpTokenError(new Error(resp.msg));
125123
}
126124
token = {
127125
accessToken: resp.data.token.access_token,
@@ -135,10 +133,10 @@ export async function AuthVerify(netDiskType: NetDiskType, invalid?: boolean) {
135133
});
136134
} catch (e) {
137135
// 报错返回原token
138-
return Promise.resolve(token.accessToken);
136+
return token.accessToken;
139137
}
140138
} else {
141-
return Promise.resolve(token.accessToken);
139+
return token.accessToken;
142140
}
143-
return Promise.resolve(token.accessToken);
141+
return token.accessToken;
144142
}

packages/filesystem/baidu/baidu.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ export default class BaiduFileSystem implements FileSystem {
2020
return this.list().then();
2121
}
2222

23-
open(file: File): Promise<FileReader> {
23+
async open(file: File): Promise<FileReader> {
2424
// 获取fsid
25-
return Promise.resolve(new BaiduFileReader(this, file));
25+
return new BaiduFileReader(this, file);
2626
}
2727

28-
openDir(path: string): Promise<FileSystem> {
29-
return Promise.resolve(new BaiduFileSystem(joinPath(this.path, path), this.accessToken));
28+
async openDir(path: string): Promise<FileSystem> {
29+
return new BaiduFileSystem(joinPath(this.path, path), this.accessToken);
3030
}
3131

32-
create(path: string): Promise<FileWriter> {
33-
return Promise.resolve(new BaiduFileWriter(this, joinPath(this.path, path)));
32+
async create(path: string): Promise<FileWriter> {
33+
return new BaiduFileWriter(this, joinPath(this.path, path));
3434
}
3535

36-
createDir(dir: string): Promise<void> {
36+
async createDir(dir: string): Promise<void> {
3737
dir = joinPath(this.path, dir);
3838
const urlencoded = new URLSearchParams();
3939
urlencoded.append("path", dir);
@@ -42,17 +42,15 @@ export default class BaiduFileSystem implements FileSystem {
4242
urlencoded.append("rtype", "3");
4343
const myHeaders = new Headers();
4444
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
45-
return this.request(`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${this.accessToken}`, {
45+
const data = await this.request(`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${this.accessToken}`, {
4646
method: "POST",
4747
headers: myHeaders,
4848
body: urlencoded,
4949
redirect: "follow",
50-
}).then((data) => {
51-
if (data.errno) {
52-
throw new Error(JSON.stringify(data));
53-
}
54-
return Promise.resolve();
5550
});
51+
if (data.errno) {
52+
throw new Error(JSON.stringify(data));
53+
}
5654
}
5755

5856
async request(url: string, config?: RequestInit) {
@@ -148,7 +146,7 @@ export default class BaiduFileSystem implements FileSystem {
148146
});
149147
}
150148

151-
getDirUrl(): Promise<string> {
152-
return Promise.resolve(`https://pan.baidu.com/disk/main#/index?category=all&path=${encodeURIComponent(this.path)}`);
149+
async getDirUrl(): Promise<string> {
150+
return `https://pan.baidu.com/disk/main#/index?category=all&path=${encodeURIComponent(this.path)}`;
153151
}
154152
}

packages/filesystem/baidu/rw.ts

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ export class BaiduFileReader implements FileReader {
2121
}&fsids=[${this.file.fsid!}]&dlink=1`
2222
);
2323
if (!data.list.length) {
24-
return Promise.reject(new Error("file not found"));
24+
throw new Error("file not found");
2525
}
26+
const resp = await fetch(`${data.list[0].dlink}&access_token=${this.fs.accessToken}`);
2627
switch (type) {
2728
case "string":
28-
return fetch(
29-
`${data.list[0].dlink}&access_token=${this.fs.accessToken}`
30-
).then((resp) => resp.text());
29+
return await resp.text();
3130
default: {
32-
return fetch(
33-
`${data.list[0].dlink}&access_token=${this.fs.accessToken}`
34-
).then((resp) => resp.blob());
31+
return await resp.blob();
3532
}
3633
}
3734
}
@@ -75,21 +72,18 @@ export class BaiduFileWriter implements FileWriter {
7572
urlencoded.append("block_list", JSON.stringify(blockList));
7673
const myHeaders = new Headers();
7774
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
78-
const uploadid = await this.fs
79-
.request(
80-
`http://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=${this.fs.accessToken}`,
81-
{
82-
method: "POST",
83-
headers: myHeaders,
84-
body: urlencoded,
85-
}
86-
)
87-
.then((data) => {
88-
if (data.errno) {
89-
throw new Error(JSON.stringify(data));
90-
}
91-
return data.uploadid;
92-
});
75+
let data = await this.fs.request(
76+
`http://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=${this.fs.accessToken}`,
77+
{
78+
method: "POST",
79+
headers: myHeaders,
80+
body: urlencoded,
81+
}
82+
);
83+
if (data.errno) {
84+
throw new Error(JSON.stringify(data));
85+
}
86+
const uploadid = data.uploadid;
9387
const body = new FormData();
9488
if (content instanceof Blob) {
9589
// 分片上传
@@ -98,23 +92,19 @@ export class BaiduFileWriter implements FileWriter {
9892
body.append("file", new Blob([content]));
9993
}
10094

101-
await this.fs
102-
.request(
103-
`${
104-
`https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&access_token=${this.fs.accessToken}` +
105-
`&type=tmpfile&path=`
106-
}${encodeURIComponent(this.path)}&uploadid=${uploadid}&partseq=0`,
107-
{
108-
method: "POST",
109-
body,
110-
}
111-
)
112-
.then((data) => {
113-
if (data.errno) {
114-
throw new Error(JSON.stringify(data));
115-
}
116-
return data;
117-
});
95+
data = await this.fs.request(
96+
`${
97+
`https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&access_token=${this.fs.accessToken}` +
98+
`&type=tmpfile&path=`
99+
}${encodeURIComponent(this.path)}&uploadid=${uploadid}&partseq=0`,
100+
{
101+
method: "POST",
102+
body,
103+
}
104+
);
105+
if (data.errno) {
106+
throw new Error(JSON.stringify(data));
107+
}
118108
// 创建文件
119109
urlencoded = new URLSearchParams();
120110
urlencoded.append("path", this.path);
@@ -123,20 +113,16 @@ export class BaiduFileWriter implements FileWriter {
123113
urlencoded.append("block_list", JSON.stringify(blockList));
124114
urlencoded.append("uploadid", uploadid);
125115
urlencoded.append("rtype", "3");
126-
return this.fs
127-
.request(
128-
`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${this.fs.accessToken}`,
129-
{
130-
method: "POST",
131-
headers: myHeaders,
132-
body: urlencoded,
133-
}
134-
)
135-
.then((data) => {
136-
if (data.errno) {
137-
throw new Error(JSON.stringify(data));
138-
}
139-
return Promise.resolve();
140-
});
116+
data = await this.fs.request(
117+
`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${this.fs.accessToken}`,
118+
{
119+
method: "POST",
120+
headers: myHeaders,
121+
body: urlencoded,
122+
}
123+
);
124+
if(data.errno) {
125+
throw new Error(JSON.stringify(data));
126+
}
141127
}
142128
}

packages/filesystem/onedrive/onedrive.ts

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ export default class OneDriveFileSystem implements FileSystem {
1919
return this.list().then();
2020
}
2121

22-
open(file: File): Promise<FileReader> {
23-
return Promise.resolve(new OneDriveFileReader(this, file));
22+
async open(file: File): Promise<FileReader> {
23+
return new OneDriveFileReader(this, file);
2424
}
2525

26-
openDir(path: string): Promise<FileSystem> {
26+
async openDir(path: string): Promise<FileSystem> {
2727
if (path.startsWith("ScriptCat")) {
2828
path = path.substring(9);
2929
}
30-
return Promise.resolve(new OneDriveFileSystem(joinPath(this.path, path), this.accessToken));
30+
return new OneDriveFileSystem(joinPath(this.path, path), this.accessToken);
3131
}
3232

33-
create(path: string): Promise<FileWriter> {
34-
return Promise.resolve(new OneDriveFileWriter(this, joinPath(this.path, path)));
33+
async create(path: string): Promise<FileWriter> {
34+
return new OneDriveFileWriter(this, joinPath(this.path, path));
3535
}
3636

37-
createDir(dir: string): Promise<void> {
37+
async createDir(dir: string): Promise<void> {
3838
if (dir && dir.startsWith("ScriptCat")) {
3939
dir = dir.substring(9);
4040
if (dir.startsWith("/")) {
4141
dir = dir.substring(1);
4242
}
4343
}
4444
if (!dir) {
45-
return Promise.resolve();
45+
return;
4646
}
4747
dir = joinPath(this.path, dir);
4848
const dirs = dir.split("/");
@@ -55,23 +55,21 @@ export default class OneDriveFileSystem implements FileSystem {
5555
if (parent !== "") {
5656
parent = `:${parent}:`;
5757
}
58-
return this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${parent}/children`, {
58+
const data = await this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${parent}/children`, {
5959
method: "POST",
6060
headers: myHeaders,
6161
body: JSON.stringify({
6262
name: dirs[dirs.length - 1],
6363
folder: {},
6464
"@microsoft.graph.conflictBehavior": "replace",
6565
}),
66-
}).then((data: any) => {
67-
if (data.errno) {
68-
throw new Error(JSON.stringify(data));
69-
}
70-
return Promise.resolve();
7166
});
67+
if (data.errno) {
68+
throw new Error(JSON.stringify(data));
69+
}
7270
}
7371

74-
request(url: string, config?: RequestInit, nothen?: boolean) {
72+
request(url: string, config?: RequestInit, nothen?: boolean): Promise<Response | any> {
7573
config = config || {};
7674
const headers = <Headers>config.headers || new Headers();
7775
if (url.indexOf("uploadSession") === -1) {
@@ -105,42 +103,39 @@ export default class OneDriveFileSystem implements FileSystem {
105103
});
106104
}
107105

108-
delete(path: string): Promise<void> {
109-
return this.request(
106+
async delete(path: string): Promise<void> {
107+
const resp = await this.request(
110108
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${joinPath(this.path, path)}`,
111109
{
112110
method: "DELETE",
113111
},
114112
true
115-
).then(async (resp) => {
116-
if (resp.status !== 204) {
117-
throw new Error(await resp.text());
118-
}
119-
return resp;
120-
});
113+
);
114+
if (resp.status !== 204) {
115+
throw new Error(await resp.text());
116+
}
117+
return resp;
121118
}
122119

123-
list(): Promise<File[]> {
120+
async list(): Promise<File[]> {
124121
let { path } = this;
125122
if (path === "/") {
126123
path = "";
127124
} else {
128125
path = `:${path}:`;
129126
}
130-
return this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${path}/children`).then((data) => {
131-
const list: File[] = [];
132-
data.value.forEach((val: any) => {
133-
list.push({
134-
name: val.name,
135-
path: this.path,
136-
size: val.size,
137-
digest: val.eTag,
138-
createtime: new Date(val.createdDateTime).getTime(),
139-
updatetime: new Date(val.lastModifiedDateTime).getTime(),
140-
});
127+
const data = await this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${path}/children`);
128+
const list: File[] = data.value.map((val: any) => {
129+
return ({
130+
name: val.name,
131+
path: this.path,
132+
size: val.size,
133+
digest: val.eTag,
134+
createtime: new Date(val.createdDateTime).getTime(),
135+
updatetime: new Date(val.lastModifiedDateTime).getTime(),
141136
});
142-
return list;
143137
});
138+
return list;
144139
}
145140

146141
getDirUrl(): Promise<string> {

packages/filesystem/onedrive/rw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class OneDriveFileReader implements FileReader {
2424
true
2525
);
2626
if (data.status !== 200) {
27-
return Promise.reject(await data.text());
27+
throw new Error(await data.text());
2828
}
2929
switch (type) {
3030
case "string":

0 commit comments

Comments
 (0)