Binary Data
File, Blob, and ReadableStream<Uint8Array> are supported by the RPC Serializer and OpenAPI Serializer. Use them to handle binary data in your procedures.
File and Blob
Procedures can accept File and Blob as input and return them directly or inside nested structures.
const const example: DecoratedProcedure<DefaultInitialContext & object, object, z.ZodFile, z.ZodObject<{
anyFieldName: z.ZodCustom<File, File>;
}, z.core.$strip>, Record<never, never>, never>
example = const os: Builder<DefaultInitialContext & object, Record<never, never>>The oRPC procedure builder. Chain methods like `.input`, `.use`, and `.handler`
to define procedures, then compose them into routers.os
.Builder<DefaultInitialContext & object, Record<never, never>>.input<z.ZodFile>(schema: z.ZodFile): BuilderWithInput<DefaultInitialContext & object, object, z.ZodFile, Record<never, never>>input(import zz.function file(params?: string | z.core.$ZodFileParams): z.ZodFilefile())
.BuilderWithInput<DefaultInitialContext & object, object, ZodFile, Record<never, never>>['output']<z.ZodObject<{
anyFieldName: z.ZodCustom<File, File>;
}, z.core.$strip>>(schema: z.ZodObject<{
anyFieldName: z.ZodCustom<File, File>;
}, z.core.$strip>): BuilderWithInputOutput<DefaultInitialContext & object, object, z.ZodFile, z.ZodObject<{
anyFieldName: z.ZodCustom<File, File>;
}, z.core.$strip>, Record<never, never>>
output(import zz.function object<{
anyFieldName: z.ZodCustom<File, File>;
}>(shape?: {
anyFieldName: z.ZodCustom<File, File>;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
anyFieldName: z.ZodCustom<File, File>;
}, z.core.$strip>
object({ anyFieldName: z.ZodCustom<File, File>anyFieldName: import zz.instanceof<{
new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
prototype: File;
}>(cls: {
new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
prototype: File;
}, params?: {
when?: ((payload: z.core.ParsePayload) => boolean) | undefined | undefined;
error?: string | z.core.$ZodErrorMap<z.core.$ZodIssueCustom> | undefined;
message?: string | undefined | undefined;
}): z.ZodCustom<File, File>
export instanceof
instanceof(var File: {
new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
prototype: File;
}
The **`File`** interface provides information about files and allows JavaScript in a web page to access their content.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File)File) }))
.BuilderWithInputOutput<DefaultInitialContext & object, object, ZodFile, ZodObject<{ anyFieldName: ZodCustom<File, File>; }, $strip>, Record<...>>['handler']<{
anyFieldName: File;
}>(handler: ProcedureHandler<DefaultInitialContext & object, z.core.File, {
anyFieldName: File;
}, ORPCErrorConstructorMap<Record<never, never>>>): DecoratedProcedure<DefaultInitialContext & object, object, z.ZodFile, z.ZodObject<{
anyFieldName: z.ZodCustom<File, File>;
}, z.core.$strip>, Record<never, never>, never>
handler(async ({ input: z.core.Fileinput }) => {
const const file: z.core.Filefile = input: z.core.Fileinput
var console: Consoleconsole.Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(const file: z.core.Filefile.File.name: stringThe **`name`** read-only property of the File interface returns the name of the file represented by a File object. For security reasons, the path is excluded from this property.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name)name)
return {
anyFieldName: FileanyFieldName: new var File: new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag) => FileThe **`File`** interface provides information about files and allows JavaScript in a web page to access their content.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File)File(['Hello World'], 'hello.txt', { BlobPropertyBag.type?: string | undefinedtype: 'text/plain' }),
}
})
ReadableStream<Uint8Array>
Procedures can return ReadableStream<Uint8Array> to stream binary responses. The example below uses the Response Headers Plugin to set the appropriate Content-Type header.
const const example: DecoratedProcedure<ServerContext & object, object, InitialInputSchema, z.ZodCustom<ReadableStream<unknown>, ReadableStream<unknown>>, Record<never, never>, never>example = const base: Builder<ServerContext & object, Record<never, never>>base
.Builder<ServerContext & object, Record<never, never>>.output<z.ZodCustom<ReadableStream<unknown>, ReadableStream<unknown>>>(schema: z.ZodCustom<ReadableStream<unknown>, ReadableStream<unknown>>): BuilderWithOutput<ServerContext & object, object, z.ZodCustom<ReadableStream<unknown>, ReadableStream<unknown>>, Record<never, never>>output(import zz.instanceof<{
new (underlyingSource: UnderlyingByteSource, strategy?: {
highWaterMark?: number;
}): ReadableStream<Uint8Array<ArrayBuffer>>;
new <R = any>(underlyingSource: UnderlyingDefaultSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
prototype: ReadableStream;
}>(cls: {
new (underlyingSource: UnderlyingByteSource, strategy?: {
highWaterMark?: number;
}): ReadableStream<Uint8Array<ArrayBuffer>>;
new <R = any>(underlyingSource: UnderlyingDefaultSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
prototype: ReadableStream;
}, params?: {
...;
}): z.ZodCustom<...>
export instanceof
instanceof(var ReadableStream: {
new (underlyingSource: UnderlyingByteSource, strategy?: {
highWaterMark?: number;
}): ReadableStream<Uint8Array<ArrayBuffer>>;
new <R = any>(underlyingSource: UnderlyingDefaultSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
prototype: ReadableStream;
}
The **`ReadableStream`** interface of the Streams API represents a readable stream of byte data. The Fetch API offers a concrete instance of a ReadableStream through the body property of a Response object.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream)ReadableStream))
.BuilderWithOutput<ServerContext & object, object, ZodCustom<ReadableStream<unknown>, ReadableStream<unknown>>, Record<never, never>>['handler']<ReadableStream<Uint8Array<ArrayBufferLike>>>(handler: ProcedureHandler<ServerContext & object, unknown, ReadableStream<Uint8Array<ArrayBufferLike>>, ORPCErrorConstructorMap<Record<never, never>>>): DecoratedProcedure<ServerContext & object, object, InitialInputSchema, z.ZodCustom<ReadableStream<unknown>, ReadableStream<unknown>>, Record<...>, never>handler(async ({ context: ServerContext & objectcontext }) => {
context: ServerContext & objectcontext.ResponseHeadersHandlerPluginContext.resHeaders?: Headers | undefinedResponse headers as a Headers instance. This is injected by the Response Headers Plugin.
When set before the response is sent, these headers will be included in the response. If not set, no additional headers will be added.resHeaders?.Headers.set(name: string, value: string): voidThe **`set()`** method of the Headers interface sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/set)set('Content-Type', 'text/plain')
const const stream: ReadableStream<Uint8Array<ArrayBufferLike>>stream = new var ReadableStream: new <Uint8Array<ArrayBufferLike>>(underlyingSource: UnderlyingDefaultSource<Uint8Array<ArrayBufferLike>>, strategy?: QueuingStrategy<Uint8Array<ArrayBufferLike>> | undefined) => ReadableStream<Uint8Array<ArrayBufferLike>> (+2 overloads)ReadableStream<interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.Uint8Array>({
UnderlyingDefaultSource<Uint8Array<ArrayBufferLike>>.start?: ((controller: ReadableStreamDefaultController<Uint8Array<ArrayBufferLike>>) => any) | undefinedstart(controller: ReadableStreamDefaultController<Uint8Array<ArrayBufferLike>>controller) {
controller: ReadableStreamDefaultController<Uint8Array<ArrayBufferLike>>controller.ReadableStreamDefaultController<Uint8Array<ArrayBufferLike>>.enqueue(chunk: Uint8Array<ArrayBufferLike>): voidThe **`enqueue()`** method of the ReadableStreamDefaultController interface enqueues a given chunk in the associated stream.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/enqueue)enqueue(new var TextEncoder: new () => TextEncoderThe **`TextEncoder`** interface enables you to encode a JavaScript string using UTF-8.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder)TextEncoder().TextEncoder.encode(input?: string): Uint8Array<ArrayBuffer>The **`TextEncoder.encode()`** method takes a string as input, and returns a Uint8Array containing the string encoded using UTF-8.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encode)encode('Hello World'))
controller: ReadableStreamDefaultController<Uint8Array<ArrayBufferLike>>controller.ReadableStreamDefaultController<Uint8Array<ArrayBufferLike>>.close(): voidThe **`close()`** method of the ReadableStreamDefaultController interface closes the associated stream.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/close)close()
}
})
return const stream: ReadableStream<Uint8Array<ArrayBufferLike>>stream
})