Skip to content

@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.

Terminal window
npm i @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/client-http zod
npm i @ts-pf/file

Put Blob in a schema like any other value:

contract.ts
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):

server.ts
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 }),
})
client.ts
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-data

MultipartCodec 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 against Content-Length and while reading. It also applies to JSON calls on the same handler, so set it to at least maxFiles × maxFileSize plus the envelope, or serve uploads from their own FetchHandler and prefix with a larger limit.
  • Check credentials from headers in a HandlerPlugin.onRequest, which runs before decode. Throw new PFError({ code: 'UNAUTHORIZED', status: 401 }) (a code that is not a protocol code needs its status, or it is a 400; a plain Error is a 500). It runs for every request under the prefix: list it after CORSPlugin so preflights are answered first, and limit it to multipart/form-data requests 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.

This package ships ts-pf-file. Sync with npx skills experimental_sync -y.