ovm
Version:
OVM is a CLI application for managing Obsidian vaults.
112 lines • 4.68 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findPluginInRegistry = exports.fetchPlugins = exports.handleExceedRateLimitError = void 0;
const node_fetch_cache_1 = __importStar(require("node-fetch-cache"));
const os_1 = require("os");
const env_1 = require("../utils/env");
const isProduction = !(0, env_1.isTestEnv)();
const cacheSubdir = isProduction ? 'obsidian-utils' : 'obsidian-utils-test';
const cacheTTL = isProduction ? 3600 : 86400;
const cacheDirectory = (0, os_1.platform)() === 'win32'
? `${(0, os_1.tmpdir)()}\\.cache\\${cacheSubdir}`
: `${(0, os_1.tmpdir)()}/.cache/${cacheSubdir}`;
const fetch = node_fetch_cache_1.default.create({
cache: new node_fetch_cache_1.FileSystemCache({
cacheDirectory,
ttl: cacheTTL,
}),
calculateCacheKey: async (url, options) => {
return JSON.stringify([options?.method, url]);
},
shouldCacheResponse: (response) => response.ok,
});
const handleExceedRateLimitError = (error) => {
if (error instanceof Error) {
const errorMessage = error.message.toLowerCase();
const isRateLimitError = errorMessage.includes('rate limit') ||
errorMessage.includes('api rate limit exceeded') ||
errorMessage.includes('403') ||
(errorMessage.includes('find') && errorMessage.includes('github'));
if (isRateLimitError) {
const apiRateLimitMessage = 'API rate limit exceeded, Try again later. Check out Github documentation for rate limit.';
throw new Error(apiRateLimitMessage);
}
}
};
exports.handleExceedRateLimitError = handleExceedRateLimitError;
const retryFetch = async (url, retries = 3, delay = 1000) => {
for (let i = 0; i < retries; i++) {
try {
const response = await fetch(url);
if (response.ok) {
return response;
}
if (response.status === 429 || response.status === 403) {
if (i < retries - 1) {
await new Promise((resolve) => global.setTimeout(resolve, delay * Math.pow(2, i)));
continue;
}
}
return response;
}
catch (error) {
if (i === retries - 1) {
throw error;
}
await new Promise((resolve) => global.setTimeout(resolve, delay * Math.pow(2, i)));
}
}
throw new Error('Max retries reached');
};
const fetchPlugins = async () => {
if (!isProduction) {
const { fetchPluginsMock } = await Promise.resolve().then(() => __importStar(require('./registry-mock')));
return fetchPluginsMock();
}
const url = 'https://raw.githubusercontent.com/obsidianmd/obsidian-releases/master/community-plugins.json';
try {
const response = await retryFetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch plugins: ${response.status} ${response.statusText}`);
}
return response.json();
}
catch (error) {
(0, exports.handleExceedRateLimitError)(error);
throw error;
}
};
exports.fetchPlugins = fetchPlugins;
const findPluginInRegistry = async (name) => {
if (!isProduction) {
const { findPluginInRegistryMock } = await Promise.resolve().then(() => __importStar(require('./registry-mock')));
return findPluginInRegistryMock(name);
}
const pluginsRegistry = await (0, exports.fetchPlugins)();
return pluginsRegistry.find(({ id }) => id === name);
};
exports.findPluginInRegistry = findPluginInRegistry;
//# sourceMappingURL=registry.js.map