@spacetimedb/files
v0.1.0
Published
Transactional file storage, visibility, hashing, and serving primitives for SpacetimeDB TypeScript modules.
Readme
@spacetimedb/files
Store and share files in your SpacetimeDB application, such as avatars and message attachments. Users can upload, list, rename, and delete their files. Keep files private or make them available through an HTTP download route.
File contents are stored in your database. Each file can be up to 4 MB, with a default total limit of 100 MB per owner.
Install
Run this in your SpacetimeDB TypeScript module directory:
npm install @spacetimedb/filesRequires SpacetimeDB SDK, CLI, and server 2.12 or later.
Integrate into an application
Add Files to your module, then expose operations for uploading and reading a file. This example uses the caller's SpacetimeDB identity as the owner. If your application has user accounts, use the authenticated user ID instead.
The file helpers run in your transaction: pass ctx.as.files from a reducer,
or tx.as.files inside a procedure's withTx.
import { schema } from 'spacetimedb/server';
import * as files from '@spacetimedb/files/submodule';
const spacetimedb = schema({ files });
export default spacetimedb;
export const uploadFile = spacetimedb.reducer(
files.uploadFileParams,
(ctx, args) => {
files.uploadFile(ctx.as.files, args, ctx.sender.toHexString());
}
);
export const readFileBytes = spacetimedb.procedure(
files.readFileBytesParams,
files.readFileBytesReturn,
(ctx, args) =>
ctx.withTx(tx =>
files.readFileBytes(tx.as.files, args, ctx.sender.toHexString())
)
);See the Vault host module for folder handling, scoped metadata views, and an HTTP download route.
Host views should return fileSummary rows, which omit ownerUserId,
ownerPathKey, and blob bytes, so subscriptions carry safe metadata only:
import { tables } from './module_bindings';
await conn.reducers.uploadFile({
path: '/avatars/me.png',
mimeType: 'image/png',
bytes: pngBytes,
visibility: 'owner',
});
conn.subscriptionBuilder().subscribe([tables.myFileSummaries]);Standalone table builders
@spacetimedb/files exports fileRow and fileBlobRow for hosts that own a
file-like table. The helpers below operate on the submodule tables only.
| Field | Type | Notes |
| ------------------------- | ----------------- | -------------------------------------------------------------- |
| id | u64 PK auto-inc | |
| ownerPathKey | string unique | Collision-safe internal owner/path key |
| path | string indexed | Canonical caller-supplied path, up to 1024 chars |
| ownerUserId | string indexed | Opaque identity, application user ID, or host-defined actor ID |
| mimeType | string | |
| size | u64 | |
| sha256Hex | string | Lowercase hex of SHA-256(bytes); used as strong ETag |
| visibility | string indexed | FILE_VISIBILITY_OWNER or FILE_VISIBILITY_PUBLIC |
| createdAt / updatedAt | timestamp | |
file_blob stores { fileId, bytes } separately, so metadata lookups, HEAD,
and conditional 304 responses avoid reading the blob.
API
Package entrypoints:
@spacetimedb/files/submodule: the submodule namespace, helpers, andserveFile.@spacetimedb/files: row builders,fileSummary, constants, anderrors.@spacetimedb/files/constants: constants anderrors, safe to import in browser code.
Each helper takes (ctx, args, owner). Derive owner from the authenticated
caller.
uploadFile(ctx, args, owner, opts?)
- Args:
path,mimeType,bytes(u8[]),visibility. - Returns:
bigint(the fileid). - Upserts by the owner/path pair. Different owners may use the same path.
- Requires an absolute canonical path such as
/images/avatar.png. - Enforces
bytes.length <= FILE_BYTES_MAX(4 MB) andpath.length <= 1024. - Enforces a per-owner total of
opts.maxOwnerBytes, defaultFILE_OWNER_BYTES_MAX(100 MB). - Accepts a media type such as
image/pngorimage/svg+xml. Parameters and control characters are rejected. - Computes the authoritative
sha256HexETag server-side.
renameFile(ctx, args, owner)
- Args:
oldPath,newPath. - Throws
files.not_foundfor a missing file andfiles.path_takenwhen the owner already has a file atnewPath.
deleteFile(ctx, args, owner)
- Args:
path. - Does nothing when the owner has no file at that path.
listFiles(ctx, args, owner)
- Args:
prefix, optionalcursor, and optionallimitfrom 1 to 200. - Returns:
{ files, nextCursor }, ordered bypath. PassnextCursorinto the next call until it is absent.bytesis omitted.
setFileVisibility(ctx, args, owner)
- Args:
path,visibility.
readFileBytes(ctx, args, owner)
- Args:
path. Returns:{ bytes: u8[], mimeType: string }. - HTTP handlers have no caller identity, so private files are read through a procedure that knows the sender. Use HTTP for cacheable public files.
HTTP serve handler
export const fileServe = spacetimedb.httpHandler((ctx, req) =>
files.serveFile(ctx.as.files, req)
);
export const router = spacetimedb.httpRouter(
new Router().get('/files', fileServe).head('/files', fileServe)
);serveFile(ctx, req, canAccess?):
- Accepts
GETandHEADonly; everything else 405s. - Reads the stable file ID from
?id=<fileId>. - Serves public files. Other files are served only when
canAccess(file)returns true; without it they return 403. A host that authenticates HTTP requests (for example with@spacetimedb/auth'srequestUserId) decides access there. - Returns 404 for an unknown file.
- Sends
etag: "<sha256Hex>"and honorsIf-None-Matchwith 304. - Sets
cache-control: public, max-age=300, must-revalidatefor public files andprivate, max-age=60, must-revalidatefor others. - Sends
x-content-type-options: nosniffandcontent-security-policy: sandbox. PNG, JPEG, GIF, WebP, and AVIF images render inline; every other type is sent withcontent-disposition: attachment, so uploaded HTML or SVG never runs on the serving origin.
Constants
| Constant | Value |
| ------------------------ | ------------- |
| FILE_BYTES_MAX | 4_000_000 |
| FILE_OWNER_BYTES_MAX | 100_000_000 |
| FILE_PATH_MAX | 1024 |
| FILE_MIME_TYPE_MAX | 127 |
| FILE_LIST_PAGE_MAX | 200 |
| FILE_VISIBILITY_OWNER | 'owner' |
| FILE_VISIBILITY_PUBLIC | 'public' |
Errors
Thrown as SenderError with the codes in errors:
files.invalid_owner- empty, longer than 512, or containing control charactersfiles.invalid_path- non-canonical, unsafe, or longer than 1024files.invalid_prefix/files.invalid_cursor- invalid listing positionfiles.invalid_page_size- listing limit outside 1 to 200files.invalid_mime_type- invalid or unsafe HTTP media typefiles.invalid_visibility- notownerorpublicfiles.too_large:<actual>/<max>- body exceedsFILE_BYTES_MAXfiles.quota_exceeded- the owner's total would exceed the quotafiles.not_found- no file at that path for this ownerfiles.path_taken-renameFiletarget already exists
Testing
pnpm test
pnpm run typecheckLicense
Apache-2.0. See the license.
