@ts-pf/file
Opt-in MultipartCodec for File / Blob attachments in ts-pf. JSON calls stay JSON; a call whose input or output contains a Blob goes as multipart/form-data with the JSON envelope in part rpc. Same contracts, same typed errors.
Install
Section titled “Install”npm i @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/client-http zodnpm i @ts-pf/filePut Blob in a schema like any other value:
import { procedure, router } from '@ts-pf/contract'import { z } from 'zod'
export const contract = router({ avatar: { upload: procedure .input(z.object({ userId: z.string(), image: z.instanceof(Blob) })) .output(z.object({ bytes: z.number() })), },})Put the same codec on both ends. Per-file caps (maxFiles, maxFileSize) are codec options; cap the total body with RequestLimitPlugin (below):
import { createImplementer } from '@ts-pf/server'import { FetchHandler } from '@ts-pf/server-http'import { MultipartCodec } from '@ts-pf/file'import { contract } from './contract.js'
const impl = createImplementer(contract)export const app = impl.router({ avatar: { upload: impl.avatar.upload.handler(async ({ input }) => ({ bytes: input.image.size })), },})
export const handler = new FetchHandler(app, { codec: new MultipartCodec({ maxFiles: 1, maxFileSize: 5 * 1024 * 1024 }),})import { createClient } from '@ts-pf/client'import { FetchLink } from '@ts-pf/client-http'import { MultipartCodec } from '@ts-pf/file'import type { contract } from './contract.js'
const client = createClient<typeof contract>( new FetchLink({ url: '/rpc', codec: new MultipartCodec() }),)
const image = new File(['…'], 'me.png', { type: 'image/png' })await client.avatar.upload({ userId: 'u1', image }) // sent as multipart/form-dataMultipartCodec reads the whole body (request.formData()) while decoding, and checks maxFiles / maxFileSize on the parsed parts. The defaults (32 files of 10 MiB) allow about 320 MiB per request. FetchHandler decodes before it runs your context function, so a caller with no credentials can still make the server read a body. To limit that:
- Cap the total with
RequestLimitPlugin({ maxBodySize }), checked againstContent-Lengthand while reading. It also applies to JSON calls on the same handler, so set it to at leastmaxFiles × maxFileSizeplus the envelope, or serve uploads from their ownFetchHandlerand prefix with a larger limit. - Check credentials from headers in a
HandlerPlugin.onRequest, which runs before decode. Thrownew PFError({ code: 'UNAUTHORIZED', status: 401 })(a code that is not a protocol code needs itsstatus, or it is a 400; a plainErroris a 500). It runs for every request under the prefix: list it afterCORSPluginso preflights are answered first, and limit it tomultipart/form-datarequests or the upload paths so public procedures still work. - For large files, upload to storage directly (a presigned URL from a procedure) or serve a plain route that streams the body.
MultipartCodec wraps JsonCodec (override with { inner }). Wire details: PROTOCOL.md (Binary attachments). Serving handler: root README.
Agent skills
Section titled “Agent skills”This package ships ts-pf-file. Sync with npx skills experimental_sync -y.