Compare commits

...
5 Commits
Author SHA1 Message Date
fyears d3eb2166aa bump to 0.4.8
Release A New Version / build (16.x) (push) Failing after 36s
2024-03-21 00:08:07 +08:00
fyears 7ecce29940 fix webdav encodeURI 2024-03-21 00:07:32 +08:00
fyears 581c6237cc bump to 0.4.7
Release A New Version / build (16.x) (push) Failing after 32s
2024-03-20 03:44:34 +08:00
fyears 257c995090 fix a mysterious bug for webdav 2024-03-20 03:10:57 +08:00
fyears fd095a9093 more debug for webdav and bump to 0.4.6
Release A New Version / build (16.x) (push) Failing after 39s
2024-03-20 02:01:51 +08:00
3 changed files with 44 additions and 68 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"id": "remotely-save", "id": "remotely-save",
"name": "Remotely Save", "name": "Remotely Save",
"version": "0.4.5", "version": "0.4.8",
"minAppVersion": "0.13.21", "minAppVersion": "0.13.21",
"description": "Yet another unofficial plugin allowing users to synchronize notes between local device and the cloud service.", "description": "Yet another unofficial plugin allowing users to synchronize notes between local device and the cloud service.",
"author": "fyears", "author": "fyears",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "remotely-save", "name": "remotely-save",
"version": "0.4.5", "version": "0.4.8",
"description": "This is yet another sync plugin for Obsidian app.", "description": "This is yet another sync plugin for Obsidian app.",
"scripts": { "scripts": {
"dev2": "node esbuild.config.mjs --watch", "dev2": "node esbuild.config.mjs --watch",
+42 -66
View File
@@ -1,5 +1,5 @@
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import { Vault, requestUrl } from "obsidian"; import { Platform, Vault, requestUrl } from "obsidian";
import { Queue } from "@fyears/tsqueue"; import { Queue } from "@fyears/tsqueue";
import chunk from "lodash/chunk"; import chunk from "lodash/chunk";
@@ -47,29 +47,49 @@ if (VALID_REQURL) {
delete transformedHeaders["host"]; delete transformedHeaders["host"];
delete transformedHeaders["content-length"]; delete transformedHeaders["content-length"];
const r = await requestUrl({ const reqContentType =
transformedHeaders["accept"] ?? transformedHeaders["content-type"];
console.debug(`before request:`);
console.debug(`url: ${options.url}`);
console.debug(`method: ${options.method}`);
console.debug(`headers: ${JSON.stringify(transformedHeaders, null, 2)}`);
console.debug(`reqContentType: ${reqContentType}`);
let r = await requestUrl({
url: options.url, url: options.url,
method: options.method, method: options.method,
body: options.data as string | ArrayBuffer, body: options.data as string | ArrayBuffer,
headers: transformedHeaders, headers: transformedHeaders,
contentType: reqContentType,
throw: false, throw: false,
}); });
let contentType: string | undefined = if (
r.headers["Content-Type"] || r.headers["content-type"]; r.status === 401 &&
if (options.headers !== undefined) { Platform.isIosApp &&
contentType = !options.url.endsWith("/") &&
contentType || !options.url.endsWith(".md") &&
transformedHeaders["content-type"] || options.method.toUpperCase() === "PROPFIND"
transformedHeaders["accept"]; ) {
} // don't ask me why,
if (contentType !== undefined) { // some webdav servers have some mysterious behaviours,
contentType = contentType.toLowerCase(); // if a folder doesn't exist without slash, the servers return 401 instead of 404
// here is a dirty hack that works
console.debug(`so we have 401, try appending request url with slash`);
r = await requestUrl({
url: `${options.url}/`,
method: options.method,
body: options.data as string | ArrayBuffer,
headers: transformedHeaders,
contentType: reqContentType,
throw: false,
});
} }
console.debug(`after request:`);
const rspHeaders = objKeyToLower({ ...r.headers }); const rspHeaders = objKeyToLower({ ...r.headers });
console.log("rspHeaders"); console.debug(`rspHeaders: ${JSON.stringify(rspHeaders, null, 2)}`);
console.log(rspHeaders);
for (let key in rspHeaders) { for (let key in rspHeaders) {
if (rspHeaders.hasOwnProperty(key)) { if (rspHeaders.hasOwnProperty(key)) {
// avoid the error: // avoid the error:
@@ -86,74 +106,28 @@ if (VALID_REQURL) {
// } // }
// } // }
if (!onlyAscii(rspHeaders[key])) { if (!onlyAscii(rspHeaders[key])) {
console.debug(`rspHeaders[key] needs encode: ${key}`);
rspHeaders[key] = encodeURIComponent(rspHeaders[key]); rspHeaders[key] = encodeURIComponent(rspHeaders[key]);
} }
} }
} }
// console.info(`requesting url=${options.url}`);
// console.info(`contentType=${contentType}`);
// console.info(`rspHeaders=${JSON.stringify(rspHeaders)}`)
// let r2: Response = undefined;
// if (contentType.includes("xml")) {
// r2 = new Response(r.text, {
// status: r.status,
// statusText: getReasonPhrase(r.status),
// headers: rspHeaders,
// });
// } else if (
// contentType.includes("json") ||
// contentType.includes("javascript")
// ) {
// console.info('inside json branch');
// // const j = r.json;
// // console.info(j);
// r2 = new Response(
// r.text, // yea, here is the text because Response constructor expects a text
// {
// status: r.status,
// statusText: getReasonPhrase(r.status),
// headers: rspHeaders,
// });
// } else if (contentType.includes("text")) {
// // avoid text/json,
// // so we split this out from the above xml or json branch
// r2 = new Response(r.text, {
// status: r.status,
// statusText: getReasonPhrase(r.status),
// headers: rspHeaders,
// });
// } else if (
// contentType.includes("octet-stream") ||
// contentType.includes("binary") ||
// contentType.includes("buffer")
// ) {
// // application/octet-stream
// r2 = new Response(r.arrayBuffer, {
// status: r.status,
// statusText: getReasonPhrase(r.status),
// headers: rspHeaders,
// });
// } else {
// throw Error(
// `do not know how to deal with requested content type = ${contentType}`
// );
// }
let r2: Response | undefined = undefined; let r2: Response | undefined = undefined;
const statusText = getReasonPhrase(r.status);
console.debug(`statusText: ${statusText}`);
if ([101, 103, 204, 205, 304].includes(r.status)) { if ([101, 103, 204, 205, 304].includes(r.status)) {
// A null body status is a status that is 101, 103, 204, 205, or 304. // A null body status is a status that is 101, 103, 204, 205, or 304.
// https://fetch.spec.whatwg.org/#statuses // https://fetch.spec.whatwg.org/#statuses
// fix this: Failed to construct 'Response': Response with null body status cannot have body // fix this: Failed to construct 'Response': Response with null body status cannot have body
r2 = new Response(null, { r2 = new Response(null, {
status: r.status, status: r.status,
statusText: getReasonPhrase(r.status), statusText: statusText,
headers: rspHeaders, headers: rspHeaders,
}); });
} else { } else {
r2 = new Response(r.arrayBuffer, { r2 = new Response(r.arrayBuffer, {
status: r.status, status: r.status,
statusText: getReasonPhrase(r.status), statusText: statusText,
headers: rspHeaders, headers: rspHeaders,
}); });
} }
@@ -165,6 +139,7 @@ if (VALID_REQURL) {
// @ts-ignore // @ts-ignore
import { AuthType, BufferLike, createClient } from "webdav/dist/web/index.js"; import { AuthType, BufferLike, createClient } from "webdav/dist/web/index.js";
import cloneDeep from "lodash/cloneDeep";
export type { WebDAVClient } from "webdav"; export type { WebDAVClient } from "webdav";
export const DEFAULT_WEBDAV_CONFIG = { export const DEFAULT_WEBDAV_CONFIG = {
@@ -236,7 +211,8 @@ export class WrappedWebdavClient {
remoteBaseDir: string, remoteBaseDir: string,
saveUpdatedConfigFunc: () => Promise<any> saveUpdatedConfigFunc: () => Promise<any>
) { ) {
this.webdavConfig = webdavConfig; this.webdavConfig = cloneDeep(webdavConfig);
this.webdavConfig.address = encodeURI(this.webdavConfig.address);
this.remoteBaseDir = remoteBaseDir; this.remoteBaseDir = remoteBaseDir;
this.vaultFolderExists = false; this.vaultFolderExists = false;
this.saveUpdatedConfigFunc = saveUpdatedConfigFunc; this.saveUpdatedConfigFunc = saveUpdatedConfigFunc;