Adapters

Screenshot storage

Upload screenshots to Cloudflare R2, AWS S3, Cloudflare Images, your database or your disk, and keep only their URL in the feedback.

Without a screenshotStorage, the Prisma and Drizzle stores keep each screenshot inline, as a base64 data: URL in the feedback row. That is fine to start with, but every list query then carries up to a megabyte per row. @siteping/screenshot-storage provides a screenshotStorage ready to use: the image goes to a backend of your choice, and the feedback stores only its URL.

npm i @siteping/screenshot-storage

It runs on Node 22+, Bun, Deno and edge runtimes: the S3 client signs requests with Web Crypto, with no AWS SDK and no runtime dependency. Only the filesystem backend needs Node. Its type declarations use Uint8Array<ArrayBuffer>, so type-checking them needs TypeScript 5.7 or later (or skipLibCheck).

Pick a backend

BackendImport fromScreenshots are served by
Cloudflare R2, AWS S3, Backblaze B2, MinIO and other S3-compatible buckets@siteping/screenshot-storage/s3The bucket's public domain or a CDN, or your app when the bucket is private
Cloudflare Images@siteping/screenshot-storage/cloudflare-imagesimagedelivery.net or your own domain
PostgreSQL through Drizzle@siteping/screenshot-storage/drizzle-pgYour app
Turso / libSQL through Drizzle@siteping/screenshot-storage/drizzle-libsqlYour app
Local disk (Node)@siteping/screenshot-storage/filesystemYour app
Memory (tests, demos)@siteping/screenshot-storage/memoryYour app

Each entry exports an object store, which only moves bytes. createScreenshotStorage wraps it into the ScreenshotStorage your store accepts:

// lib/siteping-screenshots.ts
import { createScreenshotStorage } from "@siteping/screenshot-storage";
import { createS3ObjectStore } from "@siteping/screenshot-storage/s3";

export const objectStore = createS3ObjectStore({ /* see below */ });
export const screenshotStorage = createScreenshotStorage(objectStore);

Then hand it to your store:

// Prisma: app/api/siteping/route.ts
export const { GET, POST, PATCH, DELETE, OPTIONS } = createSitepingHandler({ prisma, screenshotStorage });

// Drizzle
const store = createPgSitepingStore(db, { screenshotStorage, logger: console });

createScreenshotStorage does the work every backend shares:

  • It validates the data URL before any network or disk access: an allowed image type, then its size.
  • It stores each upload under a fresh random key (siteping-<32 hex characters>.<extension>), so no two feedbacks ever share a URL and the client's clientId never enters a key. That is the URL ownership rule the stores rely on when they delete screenshots.
  • It removes an upload whose outcome is unknown (see below).
  • delete only removes objects it created.

If an upload fails, the store still saves the feedback, without its screenshot, and logs the failure: a broken bucket never costs a client's comment.

Cloudflare R2 and other S3-compatible buckets

import { createS3ObjectStore } from "@siteping/screenshot-storage/s3";

// Cloudflare R2, served from the bucket's custom domain
export const objectStore = createS3ObjectStore({
  endpoint: `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
  bucket: "siteping-screenshots",
  accessKeyId: process.env.R2_ACCESS_KEY_ID!,
  secretAccessKey: process.env.R2_SECRET_ACCESS_KEY!,
  publicBaseUrl: "https://screenshots.example.com",
});
// AWS S3: the regional endpoint and the bucket's region
export const objectStore = createS3ObjectStore({
  endpoint: "https://s3.eu-west-3.amazonaws.com",
  region: "eu-west-3",
  bucket: "siteping-screenshots",
  accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
  sessionToken: process.env.AWS_SESSION_TOKEN, // temporary credentials only
  publicBaseUrl: "https://d1234abcd.cloudfront.net",
});
OptionDefaultWhat it does
endpointrequiredThe S3 API endpoint, without the bucket name (https://<account>.r2.cloudflarestorage.com, https://s3.<region>.amazonaws.com, https://s3.<region>.backblazeb2.com, your MinIO URL). An absolute http(s) URL. R2's dashboard shows its S3 API URL with the bucket at the end: remove it, or every object lands under <bucket>/<key> and every screenshot URL answers 404 (a warning flags it). A bucket under R2's EU jurisdiction takes https://<account>.eu.r2.cloudflarestorage.com
bucketrequiredThe bucket name
region"auto"The signing region: auto suits R2; on AWS, B2 and MinIO, use the region the bucket or server is configured with
accessKeyId, secretAccessKey, sessionTokenrequired, optionalThe credentials. sessionToken is for temporary (STS) credentials
publicBaseUrlrequiredWhere screenshots are read from: the bucket's public domain, a CDN in front of it, or your app when the bucket is private
cacheControl"public, max-age=31536000, immutable"The Cache-Control stored with each object, which a public bucket or CDN sends with the screenshot (see below)
treatAccessDeniedAsMissingfalseSee least-privilege credentials
timeoutMs5000Time budget of each call to the bucket, retries and response body included
nowthe host clockThe clock that signs requests. S3 refuses a signature more than a few minutes off, so pass a corrected clock if the host's drifts
fetchglobalThis.fetchThe fetch implementation, for tests or a proxy

Requests use path-style URLs (<endpoint>/<bucket>/<key>), which R2, AWS S3, B2 and MinIO all accept. Virtual-hosted-style addressing (<bucket>.<endpoint>) is not supported. By default, each object is stored with Cache-Control: public, max-age=31536000, immutable, since a key is never reused: a public bucket or CDN caches screenshots for good, even once deleted. cacheControl sets another value, such as public, max-age=86400, which keeps a deleted screenshot cached for a day at most. The bucket writes it into each object at upload, so a change only applies to screenshots uploaded after it: choose it before the first one. When your app serves a private bucket, the serve handler's own cacheControl applies instead.

Retries

A request that fails with a network error, a 5xx (S3's 503 SlowDown, an R2 500 InternalError) or a 429 is sent again, up to three attempts in all, after a short random wait or the Retry-After the backend asks for, as long as timeoutMs leaves time for it. A single transient failure would otherwise cost the feedback its screenshot. Sending a PUT again stores the same bytes under the same key, so a retry never leaves a second object behind.

Least-privilege credentials

Give the key access to your bucket only, and for AWS scope it to the key prefix:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:PutObject", "s3:DeleteObject", "s3:GetObject"],
      "Resource": "arn:aws:s3:::siteping-screenshots/siteping-*"
    }
  ]
}

s3:GetObject is only needed when your app serves a private bucket. Without s3:ListBucket, S3 answers 403 AccessDenied for a missing key instead of 404, so the serve handler would answer 500 for a deleted screenshot. Either grant s3:ListBucket on the bucket, or pass treatAccessDeniedAsMissing: true. That option maps only the AccessDenied code to "missing": signature and credential errors (SignatureDoesNotMatch, InvalidAccessKeyId, ExpiredToken…) still fail. It is off by default because it would also hide a policy that lacks s3:GetObject behind 404s. On R2, an API token with Object Read & Write on this one bucket is the equivalent.

Cloudflare Images

import { createCloudflareImagesObjectStore } from "@siteping/screenshot-storage/cloudflare-images";

export const objectStore = createCloudflareImagesObjectStore({
  accountId: process.env.CF_ACCOUNT_ID!,
  apiToken: process.env.CF_IMAGES_TOKEN!, // "Cloudflare Images: Edit" permission
  accountHash: process.env.CF_IMAGES_ACCOUNT_HASH!, // shown in the Images dashboard
});

Each screenshot is uploaded with its key as a custom image ID, so its delivery URL, https://imagedelivery.net/<accountHash>/<key>/public, is known before the upload finishes. variant picks another variant than public, and deliveryBaseUrl serves from your own domain (https://example.com/cdn-cgi/imagedelivery). timeoutMs, fetch and retries work as for S3, except for the upload itself: a POST is only sent again after a 429, since after a network error or a 5xx the image may already be stored under its ID.

Cloudflare does not allow signed URLs for images with a custom ID, so screenshots stay publicly readable by anyone who has the URL. Keys are 128-bit random values, which nobody can guess, but a URL shared in a ticket or a screenshot of the panel stays valid until the feedback is deleted. If screenshots must stay behind your login, use a private bucket served by your app.

Serve screenshots from your app

The filesystem, database and memory backends have no public URL of their own, and neither does a private bucket. createScreenshotServeHandler serves them from a route of your app, and publicBaseUrl points at that route:

// lib/siteping-screenshots.ts
import { createScreenshotStorage } from "@siteping/screenshot-storage";
import { createFilesystemObjectStore } from "@siteping/screenshot-storage/filesystem";

export const objectStore = createFilesystemObjectStore({
  directory: "/var/lib/siteping/screenshots", // created if missing
  publicBaseUrl: "https://app.example.com/api/siteping/screenshots",
});
export const screenshotStorage = createScreenshotStorage(objectStore);
// app/api/siteping/screenshots/[key]/route.ts
import { createScreenshotServeHandler } from "@siteping/screenshot-storage";
import { objectStore } from "@/lib/siteping-screenshots";

export const { GET } = createScreenshotServeHandler(objectStore);

The handler takes the key from the last path segment of the request. It is a Fetch API Request → Response function, so it mounts the same way in Hono, Express or a Worker.

  • Only its own keys. It serves keys of the siteping-<32 hex characters>.<extension> shape, under its keyPrefix (default siteping-: pass the one you gave createScreenshotStorage), and answers 404 to anything else before any read. No request can reach another file of the directory, another app's objects in a shared bucket, or a path outside the store.
  • Inert images only. Every response carries Content-Security-Policy: default-src 'none'; sandbox and X-Content-Type-Options: nosniff. JPEG, PNG, WebP and other raster types are served inline. Anything else a backend holds, like an SVG or text/html that reached it by another path, goes out as an application/octet-stream download, so it never runs as a page of your app's origin.
  • Cached for good. Responses are Cache-Control: public, max-age=31536000, immutable by default, with the key as a strong ETag. Pass cacheControl to send another value, such as public, max-age=86400, which keeps a deleted screenshot cached for a day at most: a revalidation reads the object, and a deleted one answers 404 (see what delete removes).
  • Behind your login. Pass authorize: (request, { key }) => boolean | Promise<boolean> to check the request (the same session cookie as your app, for instance): a refusal answers 403. Responses then become private, no-cache, whatever cacheControl says: no shared cache stores them, and the browser checks again on each reuse, which costs a 304 without reading the object. A logout or a revoked access takes effect at once. The browser sends your app's cookies with the <img> request only when the widget runs on the same site as the route.

The filesystem backend writes <key> and a <key>.content-type file beside it. It is not suited to serverless platforms, whose disk does not outlive the instance, nor to several instances without a shared volume.

Screenshots in your database

For a small or self-contained deployment, the Drizzle entries keep screenshots in a table of their own, next to the feedbacks: bytea rows on PostgreSQL, blob rows on SQLite. Every screenshot view then reads a row of up to about 1.1 MB from your database. In production, prefer object storage.

// db/schema.ts
import { createSitepingScreenshotsPgTable } from "@siteping/screenshot-storage/drizzle-pg";

export const sitepingScreenshots = createSitepingScreenshotsPgTable(); // "siteping_screenshots"
// lib/siteping-screenshots.ts
import { createScreenshotStorage } from "@siteping/screenshot-storage";
import { createPgScreenshotObjectStore } from "@siteping/screenshot-storage/drizzle-pg";
import { sitepingScreenshots } from "@/db/schema";
import { db } from "@/db";

export const objectStore = createPgScreenshotObjectStore(db, {
  publicBaseUrl: "https://app.example.com/api/siteping/screenshots",
  table: sitepingScreenshots,
});
export const screenshotStorage = createScreenshotStorage(objectStore);

Migrate the table with drizzle-kit like the rest of your schema, and serve it with the route above. On Turso / libSQL, use createSitepingScreenshotsSqliteTable and createLibSQLScreenshotObjectStore from @siteping/screenshot-storage/drizzle-libsql. Both need drizzle-orm 0.45 or later (below 1.0), an optional peer dependency. The table is independent from the store's tables: createSitepingScreenshotsPgTable("my_name") renames it, and you pass it as table.

A failed query (a table not migrated yet, a database out of reach) throws an ObjectStoreRequestError naming the statement and the key. Its cause is the code and message of the driver's error, never the query's parameters: those hold the whole image, which would otherwise land in your logs with every failed upload.

Options

createScreenshotStorage(objectStore, options):

OptionDefaultWhat it does
allowedContentTypes["image/jpeg", "image/png", "image/webp"]Image types accepted, case-insensitive. SVG and other XML-based types are refused when the storage is created, since they can run scripts once opened. The HTTP handler only accepts the three defaults anyway, so extra types matter only to code that calls the store directly
maxBytes1125000Largest decoded image, in bytes: what the server's 1.5-million-character data URL limit decodes to. The data URL's raw length and its base64 length are checked before anything is decoded
keyPrefix"siteping-"Prefix of generated keys (lowercase letters, digits, -, _). Keep it distinctive in a shared bucket, and pass the same value to createScreenshotServeHandler
loggerconsole.warnReceives the degraded-but-handled cases: a refused delete, a failed reclaim, a failing onUncertainUpload
onUncertainUpload—(key) => void | Promise<void>: see below

The factories throw on a configuration that could not work: a missing or blank required option (bucket, region, the credentials, accountId, apiToken, accountHash: an unset variable behind process.env.X! would otherwise build every screenshot URL on undefined), an accountHash or variant that is not a single URL path segment, an unsafe keyPrefix, a maxBytes that is not a positive integer, a timeoutMs that is not an integer from 1 to 2147483647 (Number() of a missing variable is NaN), a blank cacheControl or one with a character outside printable ASCII (a line break, a tab, an accented letter), or a publicBaseUrl, endpoint or deliveryBaseUrl that is not an absolute http(s) URL, or that carries credentials, a query or a fragment. The error names the option and the reason, never the value, which may hold a token. A plain-http publicBaseUrl or deliveryBaseUrl is accepted, with a warning unless it is on this machine: the widget's panel shows https screenshots, plus plain-http ones on localhost, *.localhost, 127.0.0.1 and [::1] for development. Any other http URL is missing from the panel, and a dashboard served over https blocks it as mixed content.

What delete removes

The stores call delete(url) when a feedback is deleted, and when a create that already uploaded its screenshot does not keep it — a duplicate clientId under Drizzle, a screenshot Prisma could not attach (the Prisma and Drizzle pages list the cases). It removes the object only when the URL is one this storage produced and its key is in its own namespace (keyPrefix plus the generated shape). An inline data: URL, another host or a malformed encoding is ignored. Another object behind the same bucket or CDN, such as a legacy screenshot or another app's file, is left alone with a warning to the logger. Deleting an object that is already gone succeeds.

delete removes the object from the backend, not the copies caches already hold. S3 objects, and screenshots your app serves without authorize, carry Cache-Control: public, max-age=31536000, immutable by default: a CDN in front of them (CloudFront, a custom domain on Cloudflare) and every browser that displayed one may keep serving it for up to a year to whoever holds its URL. When a screenshot must really disappear, such as a client asking for their data to be erased, also purge its URL from the CDN (a CloudFront invalidation, a Cloudflare cache purge). To bound that delay beforehand, shorten the cache lifetime with the cacheControl option of createS3ObjectStore and createScreenshotServeHandler (public, max-age=86400 lets caches keep a deleted screenshot for a day at most), or in the CDN's rules. An S3 object keeps the value it was uploaded with. Behind authorize, no shared cache keeps a screenshot, but a browser that displayed it keeps its own copy.

Uploads with an unknown outcome

When an upload still fails after its retries with a timeout, a network error or a 5xx, the backend may have stored the object anyway. The storage then removes the key right away, before it rethrows the error. A backend that commits the upload after that removal leaves an orphan, so onUncertainUpload(key) receives the key at the same time: enqueue it in a durable job that removes it again a few minutes later with objectStore.remove(key).

The storage waits for the removal and the hook 2 seconds at most, then rethrows and leaves them to finish in the background (a hook that fails later is still logged). A failed upload thus holds the feedback at most timeoutMs plus 2 seconds, within the widget's 10-second request timeout: keep the hook to the enqueue, and timeoutMs under 8 seconds.

A definitive refusal stored nothing and is not reclaimed: a 4xx, such as bad credentials, or a 3xx, such as the 301 PermanentRedirect AWS answers when endpoint is not the bucket's region. The exception is a refusal of a retry: when an earlier attempt of the same upload failed with a network error or a 5xx, that attempt may have stored the object, so the outcome is unknown and the key is reclaimed as above.

An expiration lifecycle rule on siteping-* does not replace this: it cannot tell orphans from screenshots a feedback still shows, so it only fits if you want screenshots to expire after a retention period anyway.

Your own backend

Vercel Blob, Supabase Storage, another ORM: implement ScreenshotObjectStore and wrap it with createScreenshotStorage, which keeps the validation, the random keys and the delete rules.

import { createPublicUrlMapping, type ScreenshotObjectStore } from "@siteping/screenshot-storage";

export const objectStore: ScreenshotObjectStore = {
  name: "my-backend", // used in error messages
  ...createPublicUrlMapping("https://files.example.com/screenshots"), // urlFor(key) and keyFromUrl(url)
  async put({ key, bytes, contentType }) { /* store the bytes; keys are unique and safe in paths */ },
  async remove(key) { /* delete it; succeed when it is already gone */ },
  async get(key) { /* optional: { bytes, contentType } or null, for createScreenshotServeHandler */ },
};

put should throw ScreenshotUploadRejectedError when the backend refused the upload for good, so it is not reclaimed. Match errors with isScreenshotUploadRejectedError(error), isObjectStoreRequestError(error) and isInvalidScreenshotError(error) rather than instanceof: in CommonJS, each entry point bundles its own copy of the error classes. InvalidScreenshotError (code INVALID_SCREENSHOT) reports a data URL refused by validation. isScreenshotUploadRejected, the guard's former name, still works until the next major.

Edit on GitHub

On this page