Something went wrong. Try again.
Schedule posts and reposts to Bluesky (and other PDSes) with Cloudflare workers. skyscheduler.work
scheduling social-media cloudflare bsky service cloudflare-workers bsky-tool bluesky
Something went wrong. Try again.
2.6 kB · 87 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import { validate as isValid } from "uuid";import { USE_CAPTCHA } from "../config";import { EmbedDataType } from "../enums";import type { AllContext, BaseContext } from "../types";import { invalidUUID } from "../validation/regexCases";
export function isInDev(env?: Env) { if (env === undefined) return false;
// @ts-expect-error: overlap bs because of wrangler typegen // eslint-disable-next-line @typescript-eslint/dot-notation, @typescript-eslint/no-unnecessary-condition return env["IN_DEV"] === "true";}
export function isInMaintenance(env?: Env) { if (env === undefined) return false;
// @ts-expect-error: overlap bs because of wrangler typegen // eslint-disable-next-line @typescript-eslint/dot-notation, @typescript-eslint/no-unnecessary-condition return env["MAINTAIN_LOCKDOWN"] === "true";}
export function useCFTurnstile(ctx: AllContext|undefined): boolean { if (isInDev(ctx?.env)) return false;
return USE_CAPTCHA;}
export function isAltEditableType(type: EmbedDataType) { return type === EmbedDataType.Image || type === EmbedDataType.Video;}
export function has(obj: unknown, property: string): boolean { if (!obj) return false;
// eslint-disable-next-line n/no-unsupported-features/es-builtins if (Object.hasOwn(obj, property)) { // @ts-ignore return obj[property] !== undefined && obj[property] !== null; } return false;}
export const logoutAccount = async (c: BaseContext): Promise<boolean> => { try { const auth = c.get("auth"); await auth.api.signOut({ headers: c.req.raw.headers }); return true; } catch (err: unknown) { console.error("Unable to handle logout properly, redirecting anyways. " + String(err)); } return false;}
export const clearWorkersCache = async (ctx: ExecutionContext, options: CachePurgeOptions): Promise<boolean> => { if (!ctx.cache) return false;
const clearRequest = await ctx.cache.purge(options); if (!clearRequest.success) { if (clearRequest.errors.length > 0) { const clearErrors: string[] = clearRequest.errors.map((err) => err.message); console.error(`Unable to clear workers cache, got ${clearErrors.join(",")}`); } return false; } return true;}
export function getEnumKeyByValue<T extends Record<string, unknown>>(inEnum: T, value: unknown): keyof T|null { return Object.keys(inEnum).find((x) => inEnum[x] == value) ?? null;}
export function isUUIDValid(input: unknown): boolean { if (typeof input !== 'string' ) return false;
// drop invalid uuids if (invalidUUID.test(input)) return false;
return isValid(input);}