Shared runtime utilities
The shared runtime utility layer gives every UploadThing SDK the same rules for errors, file policy, requests, security checks, and common data shapes. The public surface starts at packages/…/index.ts and supports both the server upload flow and framework-specific client packages. It keeps these cross-SDK rules aligned without making each SDK define its own version.
This page is a child of Shared foundations. File classification uses the MIME type catalog when a browser does not provide a file type.
Error contracts
The error contracts turn failures from different runtimes into values that callers can inspect, log, serialize, or show to users. They separate errors that cross an SDK boundary from errors that help internal Effect flows choose a recovery path.
Serialized error model
UploadThingError is the common public error model for UploadThing failures. It carries a stable error code, a message, optional structured data, and an optional cause. Its serialized form keeps only the code, message, and JSON-safe data so server responses and client callbacks can share one shape. The code also maps to an HTTP status when a failure must cross an HTTP boundary.
References: packages/…/error.ts• L6-29 packages/…/error.ts• L54-123
Tagged failure model
Tagged failures are typed internal errors with a stable tag. They let an Effect flow handle a bad route, unknown file type, invalid size, failed fetch, bad JSON response, paused upload, or aborted upload without matching message text. The upload clients and server reporter can then convert these failures into the public UploadThingError model at the boundary that needs it.
References: packages/…/tagged-errors.ts• L4-26 packages/…/tagged-errors.ts• L28-72 packages/…/tagged-errors.ts• L74-99 packages/…/ut-reporter.ts• L74-112
Effect request flow
Effect is the shared way to describe work that can fail and can need a runtime service. The browser request helpers use a supplied fetch service, keep the request URL with the response, parse JSON only after a successful response, and expose fetch, JSON, and HTTP failures as typed values.
The request path moves from an SDK operation to a fetch service, then to JSON parsing, and finally to a public UploadThing error when the reporter sends the failure to an application endpoint.
flowchart LR
A[SDK operation] --> B[Fetch service]
B --> C{HTTP response}
C -->|success| D[Parse JSON]
C -->|failure| E[Bad request failure]
B -->|network failure| F[Fetch failure]
D --> G[SDK result]
D -->|invalid JSON| H[Client error]
E --> H
F --> H
Browser request helpers
fetchEff and parseResponseJson provide the small request layer used by browser-side upload and event flows. A caller supplies the fetch implementation through FetchContext, while the helpers add request details to failures and preserve the request URL for later error handling. The reporter maps those typed failures to stable UploadThing error codes before they reach SDK consumers.
References: packages/…/effect.ts• L7-24 packages/…/effect.ts• L25-55 packages/…/effect.ts• L57-78 packages/…/ut-reporter.ts• L37-40 packages/…/ut-reporter.ts• L74-115
File policy contracts
The shared file policy layer turns a short route declaration into a complete upload policy and applies that policy to real files. This gives client-side checks and server-side checks the same view of allowed types, file counts, sizes, content disposition, access control, and route-specific data.
Route policy normalization
Route configuration accepts either a list of allowed file keys or a partial configuration for each key. The shared layer fills missing policy values with type-based defaults and returns a complete route configuration. The normalized policy becomes the contract used to create endpoint metadata and to check uploads before transfer.
The supported high-level file keys are image, video, audio, pdf, text, and blob. A blob policy acts as the catch-all option for a file that does not match a more specific type.
References: packages/…/file-types.ts• L1-10 packages/…/types.ts• L139-177 packages/…/types.ts• L223-238 packages/…/utils.ts• L26-52 packages/…/utils.ts• L64-92
File type and size validation
File validation first prefers the file's MIME type and then uses the file name as a fallback lookup. It maps specific MIME values into the route's high-level categories, treats PDF as its own category, and reports unknown or disallowed types as typed failures. Size values use human-readable units and convert to bytes before comparison.
The same policy is checked in more than one place by design. Client checks can reject a file before network work, while server checks protect the upload boundary when client input cannot be trusted.
References: packages/…/utils.ts• L94-136 packages/…/utils.ts• L138-167 packages/…/client.ts• L54-83 packages/…/route-config.ts• L54-115
Dropzone behavior
The dropzone utilities define the shared behavior behind file picker, drag-and-drop, and keyboard upload controls. Framework packages use these rules to keep file acceptance and visible interaction state consistent across React, Solid, Svelte, and Vue.
File acceptance rules
Dropzone acceptance combines three checks: the file matches an extension or MIME allow list, its size is within the configured range, and the selection obeys the single-file or maximum-file rule. The utilities also detect file-bearing events and convert a framework-friendly accept object into the browser's file-picker attribute.
The dropzone state tracks focus, drag status, dialog status, and accepted files. Reducer actions move this state through focus, drag, dialog, file-selection, and reset events without changing the shared acceptance rules.
References: packages/…/dropzone-utils.ts• L1-20 packages/…/dropzone-utils.ts• L26-47 packages/…/dropzone-utils.ts• L63-116 packages/…/dropzone-utils.ts• L118-165 packages/…/dropzone-utils.ts• L173-243
Cryptographic operations
The cryptographic helpers protect requests and create stable file identities for UploadThing URLs. They use a secret for HMAC signing and keep secret values wrapped as redacted data while the Effect result records success or failure.
Signed requests and URLs
Payload signing produces an HMAC-SHA-256 signature with the hmac-sha256= prefix. Verification returns false for a missing, malformed, or non-matching signature instead of exposing a cryptographic failure to the caller. Signed URLs include an expiration time and optional metadata before the final URL is signed.
This protects both direct URL use and server-to-service event checks. The SDK uses the same helper when it creates presigned upload URLs, so URL creation and verification follow one contract.
References: packages/…/crypto.ts• L11-13 packages/…/crypto.ts• L31-87 packages/…/crypto.ts• L139-169 packages/…/utils.ts• L81-105 packages/…/handler.ts• L240-252
File key identity
File keys combine an application identity with a hash-derived file seed. The default seed uses file metadata and the current time, while route options can choose the metadata used for hashing or provide a wider hash. Verification checks that the key carries the expected application prefix before the key is trusted for that application.
References: packages/…/crypto.ts• L89-137 packages/…/types.ts• L27-46 packages/…/types.ts• L199-220
Cross-SDK type contracts
Common types give the server SDK, browser client, native integrations, and UI packages the same vocabulary. They describe JSON-safe values, file metadata, fetch-like requests and responses, route options, endpoint metadata, file size and time values, access rules, and result shapes.
File and route types
FileProperties is the small file record used by validation and key generation. RouteConfig describes limits and delivery policy for one file type, while ExpandedRouteConfig and EndpointMetadata describe the complete policy that SDKs pass between route discovery, validation, and upload work.
References: packages/…/types.ts• L27-32 packages/…/types.ts• L139-177 packages/…/types.ts• L223-238
Transport and option types
The fetch-like types define only the request and response parts that shared code needs. This keeps the core flow usable with browser fetch, framework adapters, and other compatible runtimes. Time, FileSize, ACL, and ContentDisposition constrain values that affect expiration, limits, and delivery behavior before those values reach runtime code.
References: packages/…/types.ts• L5-24 packages/…/types.ts• L27-32 packages/…/types.ts• L49-123 packages/…/types.ts• L139-177 packages/…/types.ts• L190-238
Uploader presentation helpers
Shared presentation helpers convert route policy into values that UI packages can use without knowing the server configuration format. They generate accepted MIME values and extension filters, determine whether multiple files are allowed, round progress for display, extract clipboard files, and resolve style or content fields for different UI runtimes.
Route-to-interface mapping
The helpers map route file keys to browser MIME filters, file-picker extension filters, and human-readable content labels. Generic types include the wildcard MIME value and known MIME values so browsers with limited wildcard support still offer the right files. Specific MIME types include their known extensions, while blob leaves the picker unrestricted. The helpers also define runtime-neutral style and content fields so each framework can keep its own rendering model while sharing the same uploader meaning.
References: packages/…/component-utils.ts• L20-28 packages/…/component-utils.ts• L30-81 packages/…/component-utils.ts• L83-133 packages/…/component-utils.ts• L136-160 packages/…/component-utils.ts• L162-201 packages/…/component-utils.ts• L203-293 packages/…/component-utils.test.ts• L11-73
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
UploadThingErrorcodes aligned with HTTP status mapping and with the reporter's conversion of tagged request failures. - Keep route normalization, client validation, server validation, and endpoint metadata aligned when a route option or file key changes.
- Keep file-type matching aligned with the MIME type catalog when adding or changing a MIME group or extension fallback.
- Keep signed URL creation and signature verification compatible when changing expiration fields, query data, or signature encoding.
- Keep
FetchEsque,ResponseEsque, andFetchContextcompatible with every browser and framework fetch adapter. - Keep dropzone acceptance rules, generated picker filters, and specific MIME extension mappings aligned so the picker does not offer files that the drop handler rejects.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A file has no browser MIME type and no known extension | Type matching fails, unless the route allows blob | Use the MIME catalog fallback and treat blob as an explicit catch-all policy |
| A route omits a required value or has a minimum count above its maximum | Route expansion or upload validation returns a typed configuration failure | Normalize route policy before use and validate count bounds at the upload boundary |
| A file exceeds its route size or count limit | The upload is rejected before or during the upload flow | Apply the shared client check for early feedback and the server check for trust-boundary protection |
| A request cannot be fetched, returns bad JSON, or returns a non-success status | The SDK reports a stable client or HTTP error instead of a normal result | Run requests through the typed fetch and response parsing flow |
| A signing secret is invalid or a signature does not match | URL or event verification fails | Keep signing and verification on the same HMAC-SHA-256 contract and fail closed |
| A generated key has a different application prefix | The key is not accepted as belonging to the current application | Verify the key prefix against the application identity |
| A MIME filter omits the extension for a specific file type | Some browser file pickers do not show valid files even when the MIME type is allowed | Build picker filters from the shared MIME catalog and include known extensions |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.ts | Shows the public shared utility surface exported to the SDKs. |
| packages/…/error.ts | Defines the public error codes, serialized error shape, and status mapping. |
| packages/…/tagged-errors.ts | Defines typed failures used inside request, validation, and upload flows. |
| packages/…/effect.ts | Defines the browser request and JSON parsing boundary. |
| packages/…/utils.ts | Connects route defaults, file matching, size conversion, URL resolution, and time values. |
| packages/…/dropzone-utils.ts | Defines shared file acceptance and dropzone state behavior. |
| packages/…/crypto.ts | Defines signatures, signed URLs, and application-scoped file keys. |
| packages/…/types.ts | Defines the common file, route, transport, and option contracts. |
| packages/…/component-utils.ts | Shows how route policy becomes MIME filters, picker filters, and framework-neutral uploader UI values. |
| packages/…/component-utils.test.ts | Shows the contract for generic, specific, PDF, and unrestricted file-picker filters. |
| packages/…/route-config.ts | Shows how shared policy is enforced at the upload boundary. |
| packages/…/ut-reporter.ts | Shows how shared request failures become public SDK errors. |
| packages/…/utils.ts | Shows how shared key and URL helpers support server-side uploads. |