Skip to content

Commit 0a91d82

Browse files
committed
移除 Baidu filesystem 对全局 DNR 规则的依赖,改为请求级禁用 cookie
1 parent 144dc25 commit 0a91d82

2 files changed

Lines changed: 30 additions & 27 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it, vi, afterEach } from "vitest";
2+
import BaiduFileSystem from "./baidu";
3+
4+
describe("BaiduFileSystem", () => {
5+
afterEach(() => {
6+
vi.restoreAllMocks();
7+
});
8+
9+
it("request should omit credentials without using global DNR rules", async () => {
10+
const fetchMock = vi.fn().mockResolvedValue({
11+
json: async () => ({ errno: 0 }),
12+
});
13+
vi.stubGlobal("fetch", fetchMock);
14+
const fs = new BaiduFileSystem("/apps", "token");
15+
16+
await expect(fs.request("https://pan.baidu.com/rest/2.0/xpan/file?method=list")).resolves.toEqual({
17+
errno: 0,
18+
});
19+
20+
expect(fetchMock).toHaveBeenCalledTimes(1);
21+
expect(fetchMock).toHaveBeenCalledWith(
22+
"https://pan.baidu.com/rest/2.0/xpan/file?method=list",
23+
expect.objectContaining({
24+
credentials: "omit",
25+
})
26+
);
27+
});
28+
});

packages/filesystem/baidu/baidu.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,9 @@ export default class BaiduFileSystem implements FileSystem {
5959
async request(url: string, config?: RequestInit) {
6060
config = config || {};
6161
const headers = <Headers>config.headers || new Headers();
62-
// 处理请求匿名不发送cookie
63-
await chrome.declarativeNetRequest.updateDynamicRules({
64-
removeRuleIds: [100],
65-
addRules: [
66-
{
67-
id: 100,
68-
action: {
69-
type: "modifyHeaders",
70-
responseHeaders: [
71-
{
72-
operation: "remove",
73-
header: "cookie",
74-
},
75-
],
76-
},
77-
condition: {
78-
urlFilter: url,
79-
resourceTypes: ["xmlhttprequest"],
80-
},
81-
},
82-
],
83-
});
8462
config.headers = headers;
63+
// 对百度网盘请求显式禁用 cookie,避免依赖全局 DNR 规则造成并发竞态
64+
config.credentials = "omit";
8565
return fetch(url, config)
8666
.then((data) => data.json())
8767
.then(async (data) => {
@@ -99,11 +79,6 @@ export default class BaiduFileSystem implements FileSystem {
9979
});
10080
}
10181
return data;
102-
})
103-
.finally(() => {
104-
chrome.declarativeNetRequest.updateDynamicRules({
105-
removeRuleIds: [100],
106-
});
10782
});
10883
}
10984

0 commit comments

Comments
 (0)