From 9d3aa35b0b8c20caa36d82b4edeb992aa16bfc49 Mon Sep 17 00:00:00 2001 From: vorotamoroz Date: Wed, 20 Apr 2022 15:02:06 +0900 Subject: [PATCH] Fixed: - Problems around new request API's --- manifest.json | 2 +- package-lock.json | 34 +++++++++++++++++----------------- package.json | 18 +++++++++--------- src/utils.ts | 2 +- src/utils_couchdb.ts | 22 ++++++++++++++++------ 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/manifest.json b/manifest.json index 46bdba3..e33f0df 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-livesync", "name": "Self-hosted LiveSync", - "version": "0.8.2", + "version": "0.8.3", "minAppVersion": "0.9.12", "description": "Community implementation of self-hosted livesync. Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.", "author": "vorotamoroz", diff --git a/package-lock.json b/package-lock.json index 3ae58cd..a02abd5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-livesync", - "version": "0.8.2", + "version": "0.8.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-livesync", - "version": "0.8.2", + "version": "0.8.3", "license": "MIT", "dependencies": { "diff-match-patch": "^1.0.5", @@ -27,7 +27,7 @@ "eslint": "^7.32.0", "eslint-config-airbnb-base": "^14.2.1", "eslint-plugin-import": "^2.25.2", - "obsidian": "^0.13.30", + "obsidian": "^0.14.6", "rollup": "^2.32.1", "svelte-preprocess": "^4.10.2", "tslib": "^2.2.0", @@ -2571,9 +2571,9 @@ } }, "node_modules/moment": { - "version": "2.29.1", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", - "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==", + "version": "2.29.2", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.2.tgz", + "integrity": "sha512-UgzG4rvxYpN15jgCmVJwac49h9ly9NurikMWGPdVxm8GZD6XjkKPxDTjQQ43gtGgnV3X0cAyWDdP2Wexoquifg==", "dev": true, "engines": { "node": "*" @@ -2659,15 +2659,15 @@ } }, "node_modules/obsidian": { - "version": "0.13.30", - "resolved": "https://registry.npmjs.org/obsidian/-/obsidian-0.13.30.tgz", - "integrity": "sha512-uAOrIyeHE9qYzg1Qjfpy/qlyLUFX9oyKWeHYO8NVDoI+pm5VUTMe7XWcsXPwb9iVsVmggVJcdV15Vqm9bljhxQ==", + "version": "0.14.6", + "resolved": "https://registry.npmjs.org/obsidian/-/obsidian-0.14.6.tgz", + "integrity": "sha512-oXPJ8Zt10WhN19bk5l4mZuXRZbbdT1QoMgxGGJ0bB7UcJa0bozDzugS5L/QiV9gDoujpUPxDWNVahEel6r0Fpw==", "dev": true, "dependencies": { "@codemirror/state": "^0.19.6", "@codemirror/view": "^0.19.31", "@types/codemirror": "0.0.108", - "moment": "2.29.1" + "moment": "2.29.2" } }, "node_modules/once": { @@ -5406,9 +5406,9 @@ } }, "moment": { - "version": "2.29.1", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", - "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==", + "version": "2.29.2", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.2.tgz", + "integrity": "sha512-UgzG4rvxYpN15jgCmVJwac49h9ly9NurikMWGPdVxm8GZD6XjkKPxDTjQQ43gtGgnV3X0cAyWDdP2Wexoquifg==", "dev": true }, "ms": { @@ -5470,15 +5470,15 @@ } }, "obsidian": { - "version": "0.13.30", - "resolved": "https://registry.npmjs.org/obsidian/-/obsidian-0.13.30.tgz", - "integrity": "sha512-uAOrIyeHE9qYzg1Qjfpy/qlyLUFX9oyKWeHYO8NVDoI+pm5VUTMe7XWcsXPwb9iVsVmggVJcdV15Vqm9bljhxQ==", + "version": "0.14.6", + "resolved": "https://registry.npmjs.org/obsidian/-/obsidian-0.14.6.tgz", + "integrity": "sha512-oXPJ8Zt10WhN19bk5l4mZuXRZbbdT1QoMgxGGJ0bB7UcJa0bozDzugS5L/QiV9gDoujpUPxDWNVahEel6r0Fpw==", "dev": true, "requires": { "@codemirror/state": "^0.19.6", "@codemirror/view": "^0.19.31", "@types/codemirror": "0.0.108", - "moment": "2.29.1" + "moment": "2.29.2" } }, "once": { diff --git a/package.json b/package.json index f85aad3..f5bd96f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-livesync", - "version": "0.8.2", + "version": "0.8.3", "description": "Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.", "main": "main.js", "type": "module", @@ -21,17 +21,17 @@ "@types/pouchdb-browser": "^6.1.3", "@typescript-eslint/eslint-plugin": "^5.7.0", "@typescript-eslint/parser": "^5.0.0", - "eslint": "^7.32.0", - "eslint-config-airbnb-base": "^14.2.1", - "eslint-plugin-import": "^2.25.2", - "obsidian": "^0.13.30", - "rollup": "^2.32.1", - "tslib": "^2.2.0", - "typescript": "^4.2.4", "builtin-modules": "^3.2.0", "esbuild": "0.13.12", "esbuild-svelte": "^0.6.0", - "svelte-preprocess": "^4.10.2" + "eslint": "^7.32.0", + "eslint-config-airbnb-base": "^14.2.1", + "eslint-plugin-import": "^2.25.2", + "obsidian": "^0.14.6", + "rollup": "^2.32.1", + "svelte-preprocess": "^4.10.2", + "tslib": "^2.2.0", + "typescript": "^4.2.4" }, "dependencies": { "diff-match-patch": "^1.0.5", diff --git a/src/utils.ts b/src/utils.ts index 29de4d0..1c5b9a3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -66,7 +66,7 @@ export const escapeStringToHTML = (str: string) => { export function resolveWithIgnoreKnownError(p: Promise, def: T): Promise { return new Promise((res, rej) => { - p.then(res).catch((ex) => ((ex.status && ex.status == 404) || (ex.message && ex.message == "Request Error:404") ? res(def) : rej(ex))); + p.then(res).catch((ex) => (ex.status && ex.status == 404 ? res(def) : rej(ex))); }); } diff --git a/src/utils_couchdb.ts b/src/utils_couchdb.ts index 8c6e48b..9bb77f6 100644 --- a/src/utils_couchdb.ts +++ b/src/utils_couchdb.ts @@ -2,7 +2,7 @@ import { Logger } from "./logger"; import { LOG_LEVEL, VER, VERSIONINFO_DOCID, EntryVersionInfo, EntryDoc } from "./types"; import { resolveWithIgnoreKnownError } from "./utils"; import { PouchDB } from "../pouchdb-browser-webpack/dist/pouchdb-browser.js"; -import { requestUrl, RequestUrlParam } from "obsidian"; +import { requestUrl, RequestUrlParam, RequestUrlResponse } from "obsidian"; export const isValidRemoteCouchDBURI = (uri: string): boolean => { if (uri.startsWith("https://")) return true; @@ -13,6 +13,19 @@ let last_post_successed = false; export const getLastPostFailedBySize = () => { return !last_post_successed; }; +const fetchByAPI = async (request: RequestUrlParam): Promise => { + const ret = await requestUrl(request); + if (ret.status - (ret.status % 100) !== 200) { + const er: Error & { status?: number } = new Error(`Request Error:${ret.status}`); + if (ret.json) { + er.message = ret.json.reason; + er.name = `${ret.json.error ?? ""}:${ret.json.message ?? ""}`; + } + er.status = ret.status; + throw er; + } + return ret; +}; export const connectRemoteCouchDB = async (uri: string, auth: { username: string; password: string }, disableRequestURI: boolean): Promise; info: PouchDB.Core.DatabaseInfo }> => { if (!isValidRemoteCouchDBURI(uri)) return "Remote URI is not valid"; @@ -65,15 +78,12 @@ export const connectRemoteCouchDB = async (uri: string, auth: { username: string }; try { - const r = await requestUrl(requestParam); + const r = await fetchByAPI(requestParam); if (method == "POST" || method == "PUT") { last_post_successed = r.status - (r.status % 100) == 200; } else { last_post_successed = true; } - if (r.status - (r.status % 100) !== 200) { - throw new Error(`Request Error:${r.status}`); - } Logger(`HTTP:${method}${size} to:${localURL} -> ${r.status}`, LOG_LEVEL.VERBOSE); return new Response(r.arrayBuffer, { @@ -146,7 +156,7 @@ export const checkRemoteVersion = async (db: PouchDB.Database, migrate: (from: n if (version == barrier) return true; return false; } catch (ex) { - if ((ex.status && ex.status == 404) || (ex.message && ex.message == "Request Error:404")) { + if (ex.status && ex.status == 404) { if (await bumpRemoteVersion(db)) { return true; }