fix webdav for new er

This commit is contained in:
fyears 2024-01-01 00:43:59 +08:00
parent ce8d8d1c62
commit 6a009b93cb

View File

@ -15,16 +15,16 @@ import type {
FileStat, FileStat,
WebDAVClient, WebDAVClient,
RequestOptionsWithState, RequestOptionsWithState,
Response, // Response,
ResponseDataDetailed, // ResponseDataDetailed,
} from "webdav"; } from "webdav";
import { getPatcher } from "webdav";
// @ts-ignore
import { getPatcher } from "webdav/dist/web/index.js";
if (VALID_REQURL) { if (VALID_REQURL) {
getPatcher().patch( getPatcher().patch(
"request", "request",
async ( async (options: RequestOptionsWithState): Promise<Response> => {
options: RequestOptionsWithState
): Promise<Response | ResponseDataDetailed<any>> => {
const transformedHeaders = { ...options.headers }; const transformedHeaders = { ...options.headers };
delete transformedHeaders["host"]; delete transformedHeaders["host"];
delete transformedHeaders["Host"]; delete transformedHeaders["Host"];
@ -37,80 +37,65 @@ if (VALID_REQURL) {
headers: transformedHeaders, headers: transformedHeaders,
}); });
let r2: Response | ResponseDataDetailed<any> = undefined; let contentType: string | undefined =
if ((options as any).responseType === undefined) { r.headers["Content-Type"] ||
r2 = { r.headers["content-type"] ||
data: undefined, options.headers["Content-Type"] ||
options.headers["content-type"] ||
options.headers["Accept"] ||
options.headers["accept"];
if (contentType !== undefined) {
contentType = contentType.toLowerCase();
}
// console.log(`contentType=${contentType}`)
let r2: Response = undefined;
if (contentType.includes("xml")) {
r2 = new Response(r.text, {
status: r.status, status: r.status,
statusText: getReasonPhrase(r.status), statusText: getReasonPhrase(r.status),
headers: r.headers, headers: r.headers,
}; });
} else if ((options as any).responseType === "json") { } else if (
r2 = { contentType.includes("json") ||
data: r.json, contentType.includes("javascript")
) {
r2 = new Response(r.json, {
status: r.status, status: r.status,
statusText: getReasonPhrase(r.status), statusText: getReasonPhrase(r.status),
headers: r.headers, headers: r.headers,
}; });
} else if ((options as any).responseType === "text") { } else if (contentType.includes("text")) {
r2 = { // avoid text/json,
data: r.text, // so we split this out from the above xml or json branch
r2 = new Response(r.text, {
status: r.status, status: r.status,
statusText: getReasonPhrase(r.status), statusText: getReasonPhrase(r.status),
headers: r.headers, headers: r.headers,
}; });
} else if ((options as any).responseType === "arraybuffer") { } else if (
r2 = { contentType.includes("octet-stream") ||
data: r.arrayBuffer, contentType.includes("binary") ||
contentType.includes("buffer")
) {
// application/octet-stream
r2 = new Response(r.arrayBuffer, {
status: r.status, status: r.status,
statusText: getReasonPhrase(r.status), statusText: getReasonPhrase(r.status),
headers: r.headers, headers: r.headers,
}; });
} else { } else {
throw Error( throw Error(
`do not know how to deal with responseType = ${ `do not know how to deal with requested content type = ${contentType}`
(options as any).responseType
}`
); );
} }
return r2; return r2;
} }
); );
} }
// getPatcher().patch("request", (options: any) => {
// // console.log("using fetch"); // @ts-ignore
// const r = fetch(options.url, { import { AuthType, BufferLike, createClient } from "webdav/dist/web/index.js";
// method: options.method,
// body: options.data as any,
// headers: options.headers,
// signal: options.signal,
// })
// .then((rsp) => {
// if (options.responseType === undefined) {
// return Promise.all([undefined, rsp]);
// }
// if (options.responseType === "json") {
// return Promise.all([rsp.json(), rsp]);
// }
// if (options.responseType === "text") {
// return Promise.all([rsp.text(), rsp]);
// }
// if (options.responseType === "arraybuffer") {
// return Promise.all([rsp.arrayBuffer(), rsp]);
// }
// })
// .then(([d, r]) => {
// return {
// data: d,
// status: r.status,
// statusText: r.statusText,
// headers: r.headers,
// };
// });
// // console.log("using fetch");
// return r;
// });
import { AuthType, BufferLike, createClient } from "webdav";
export type { WebDAVClient } from "webdav"; export type { WebDAVClient } from "webdav";
export const DEFAULT_WEBDAV_CONFIG = { export const DEFAULT_WEBDAV_CONFIG = {
@ -234,8 +219,9 @@ export class WrappedWebdavClient {
method: "PROPFIND", method: "PROPFIND",
headers: { headers: {
Depth: "infinity", Depth: "infinity",
Accept: "text/plain,application/xml",
}, },
responseType: "text", // responseType: "text",
} as any); } as any);
if (res.status === 403) { if (res.status === 403) {
throw Error("not support Infinity, get 403"); throw Error("not support Infinity, get 403");
@ -255,8 +241,9 @@ export class WrappedWebdavClient {
method: "PROPFIND", method: "PROPFIND",
headers: { headers: {
Depth: "1", Depth: "1",
Accept: "text/plain,application/xml",
}, },
responseType: "text", // responseType: "text",
} as any } as any
); );
testPassed = true; testPassed = true;