twoot
Version:
toot and/or skeet. a lowest-common-denominator api for simplifying crossposting to mastodon and bsky
35 lines • 1.42 kB
JavaScript
import assert from "node:assert/strict";
import { setTimeout } from "node:timers/promises";
import retry from "async-retry";
/**
* Kludge (or superstition) to try to avoid hitting API rate limits. Should be
* made configurable -- or rate limiting should just be handled better.
*/
export const WAIT_TIME_BETWEEN_REPLIES = 50;
export function formatRejection(rej) {
assert(Array.isArray(rej.reason));
const [error, config] = rej.reason;
const message = error instanceof Error
? (error.stack ?? error.toString())
: JSON.stringify(error);
return `${config.type}${config.type === "mastodon" ? ` (${config.server})` : ""} error:\n\n${message}`;
}
export const formatMastoStatus = (s) => `${s.content || "<no text>"}\n${s.createdAt} => ${s.uri}`;
export const formatBskyStatus = (s) => `${s.status || "<no text>"}\n => ${s.uri}`;
export async function doWithRetryAndTimeout(fn, context) {
return retry(() => Promise.race([
fn(),
// unsure if maxRetryTime option below times out the first call
setTimeout(30_000).then(() => {
throw new Error("Timeout exceeded!");
}),
]), {
retries: 5,
maxTimeout: 10_000,
maxRetryTime: 20_000,
onRetry(e, attempt) {
console.warn(`Error in retry block! (Attempt ${attempt})\n`, `Context: ${context}\n`, e);
},
});
}
//# sourceMappingURL=util.js.map