Skip to content

Open source alternative to Semrush and Ahrefs

3.9 KB
Raw Blame
import { isAuditRenderingAllowed } from "@/server/lib/audit/rendering-policy";
import { CrawlerCredentialService } from "@/server/features/audit/services/CrawlerCredentialService";
import { createServerFn } from "@tanstack/react-start";
import { waitUntil } from "cloudflare:workers";
import { requireOrgPermission } from "@/server/auth/org-gate";
import { AuditService } from "@/server/features/audit/services/AuditService";
import { captureServerEvent } from "@/server/lib/posthog";
import { requireProjectContext } from "@/serverFunctions/middleware";
import {
  deleteAuditSchema,
  getAuditHistorySchema,
  getAuditResultsSchema,
  getAuditStatusSchema,
  getCrawlProgressSchema,
  startAuditSchema,
} from "@/types/schemas/audit";

export const startAudit = createServerFn({ method: "POST" })
  .middleware(requireProjectContext)
  .validator(startAuditSchema)
  .handler(async ({ data, context }) => {
    const limitTier = await AuditService.resolveAuditLimitTier(context);
    // The dashboard's scan button always asks for rendering; installs with no
    // renderer (Docker without a Context key) crawl without it instead of
    // failing. The launch form only offers rendering where it's available.
    const renderJavaScript =
      data.renderJavaScript && (await isAuditRenderingAllowed());

    const result = await AuditService.startAudit({
      actorUserId: context.userId,
      billingCustomer: context,
      projectId: context.projectId,
      startUrl: data.startUrl,
      maxPages: data.maxPages,
      lighthouseStrategy: data.lighthouseStrategy,
      limitTier,
      renderJavaScript,
    });

    waitUntil(
      captureServerEvent({
        distinctId: context.userId,
        event: "site_audit:start",
        organizationId: context.organizationId,
        properties: {
          project_id: context.projectId,
          max_pages: data.maxPages ?? 50,
          run_lighthouse: data.lighthouseStrategy !== "none",
          plan_tier: limitTier,
        },
      }),
    );

    return result;
  });

export const getAuditStatus = createServerFn({ method: "POST" })
  .middleware(requireProjectContext)
  .validator(getAuditStatusSchema)
  .handler(async ({ data, context }) => {
    return AuditService.getStatus(data.auditId, context.projectId);
  });

export const getAuditResults = createServerFn({ method: "POST" })
  .middleware(requireProjectContext)
  .validator(getAuditResultsSchema)
  .handler(async ({ data, context }) => {
    return AuditService.getResults(data.auditId, context.projectId);
  });

export const getAuditHistory = createServerFn({ method: "POST" })
  .middleware(requireProjectContext)
  .validator(getAuditHistorySchema)
  .handler(async ({ context }) => {
    return AuditService.getHistory(context.projectId);
  });

export const getCrawlProgress = createServerFn({ method: "POST" })
  .middleware(requireProjectContext)
  .validator(getCrawlProgressSchema)
  .handler(async ({ data, context }) => {
    return AuditService.getCrawlProgress(data.auditId, context.projectId);
  });

export const deleteAudit = createServerFn({ method: "POST" })
  .middleware(requireProjectContext)
  .validator(deleteAuditSchema)
  .handler(async ({ data, context }) => {
    // Deleting audits frees the org's free-plan capacity ceiling (a SUM over
    // audit rows), so it gets the same destructive-action gate as archiving.
    requireOrgPermission(context, { project: ["delete"] });
    await AuditService.remove(data.auditId, context.projectId);
    return { success: true };
  });

export const getAuditCapabilities = createServerFn({ method: "POST" })
  .middleware(requireProjectContext)
  .validator(getAuditHistorySchema)
  .handler(async () => ({
    canRenderJavaScript: await isAuditRenderingAllowed(),
    // Saving and replaying Shopify crawler access needs the optional
    // CRAWLER_ACCESS_SECRET.
    canStoreCrawlerAccess: await CrawlerCredentialService.hasEncryptionKey(),
  }));