UNPKG

@mojojs/core

Version:

Real-time web framework

103 lines (102 loc) 2.9 kB
import type { JSONValue, UploadOptions } from './types.js'; import type { Readable, Writable } from 'node:stream'; import { Params } from './body/params.js'; import { Headers } from './headers.js'; import DOM from '@mojojs/dom'; interface FileUpload { fieldname: string; file: Readable; filename: string; encoding: string; mimetype: string; } type FormIterator = AsyncIterableIterator<[string, Readable, string, string, string]>; /** * HTTP message body base class. */ export declare class Body { /** * Automatically decompress message body if necessary. */ autoDecompress: boolean; /** * HTTP headers. */ headers: Headers; _form: Params | undefined; _stream: Readable; constructor(headers: string[], stream: Readable); [Symbol.asyncIterator](): AsyncIterable<Buffer>; /** * Get message body as `Buffer` object. */ buffer(): Promise<Buffer>; /** * Get message body as a readable stream. */ createReadStream(): Readable; /** * Get async iterator for uploaded files from message body. * @example * // Iterate over uploaded files * for await (const {fieldname, file, filename} of body.files()) { * const parts = []; * for await (const chunk of file) { * parts.push(chunk); * } * const content = Buffer.concat(parts).toString(); * console.write(`${fieldname}: ${content}`); * } */ files(options?: UploadOptions): AsyncIterableIterator<FileUpload>; /** * Get form parameters from message body. * @example * // Get a specific parameter * const params = await body.form(); * const foo = params.get('foo'); */ form(options?: UploadOptions): Promise<Params>; /** * Get HTTP header from message. * @example * // Get User-Agent header * const agent = body.get('User-Agent'); */ get(name: string): string | null; /** * Get HTML message body as `@mojojs/dom` object. */ html(): Promise<DOM>; /** * Get JSON message body as parsed data structure. */ json<T = JSONValue>(): Promise<T>; /** * Pipe message body to writable stream. */ pipe(writer: Writable): Promise<void>; /** * Set HTTP header for message. * // Set Server header * body.set('Server', 'mojo.js'); */ set(name: string, value: string): this; /** * Get message body as string. */ text(charset?: BufferEncoding): Promise<string>; /** * Get XML message body as `@mojojs/dom` object. */ xml(): Promise<DOM>; /** * Get YAML message body as parsed data structure. */ yaml(): Promise<unknown>; _consumeBody(): Promise<Uint8Array[]>; _formIterator(options?: UploadOptions): FormIterator; _isForm(): boolean; get _params(): Params; } export {};