import { IncomingMessage, ServerResponse } from 'node:http'; import { Readable } from 'node:stream'; import { U as UploadFile, j as UploadResponse } from "../../../packem_shared/storage.d-C9EdfTf1.js"; import { H as Handlers, A as AsyncHandler, U as UploadOptions, R as ResponseFile, a as ResponseList, M as MethodHandler } from "../../../packem_shared/types.d-D6Jkz0Sp.js"; export type { B as BaseHandler, b as RequestEvent, c as UploadErrorEvent, d as UploadEvent } from "../../../packem_shared/types.d-D6Jkz0Sp.js"; import { B as BaseHandlerCore } from "../../../packem_shared/tus-base.d-Dgnbivbj.js"; export { T as TUS_RESUMABLE, a as TUS_VERSION } from "../../../packem_shared/tus-base.d-Dgnbivbj.js"; import 'node:timers'; import 'node:crypto'; import 'lru-cache'; import 'node:events'; import '@visulima/pagination'; import "../../../packem_shared/media-transformer.d-Co37isNg.js"; import "../../../packem_shared/types.d-D1TIRqKW.js"; /** * Base handler for Node.js platform (IncomingMessage/ServerResponse). * Extends BaseHandlerCore with Node.js-specific request/response handling. * @template TFile The file type used by this handler. * @template NodeRequest The Node.js request type. * @template NodeResponse The Node.js response type. */ declare abstract class BaseHandlerNode extends BaseHandlerCore implements MethodHandler { /** * Limiting enabled HTTP method handler. */ static readonly methods: Handlers[]; /** * Map of registered HTTP method handlers. */ protected registeredHandlers: Map>; constructor(options: UploadOptions); /** * Gets the registered handlers map. * @returns Map of registered handlers. */ get handlers(): Map>; /** * Handles HTTP request (alias for upload method). * @param request Node.js IncomingMessage. * @param response Node.js ServerResponse. */ handle: (request: NodeRequest, response: NodeResponse) => Promise; /** * Main upload handler that processes HTTP requests and routes them to appropriate method handlers. * @param request Node.js IncomingMessage. * @param response Node.js ServerResponse. * @param next Optional Express-style next function for middleware compatibility. * @throws {UploadError} When storage is not ready or method is not allowed. */ upload: (request: NodeRequest, response: NodeResponse, next?: () => void) => Promise; /** * Compose and register HTTP method handlers. * Subclasses should override this to register their specific handlers. */ protected abstract compose(): void; /** * Sends an HTTP response with the provided body, headers, and status code. * @param response Node.js ServerResponse to send data to. * @param body Response body, headers, and status code. */ send(response: NodeResponse, { body, headers, statusCode }: UploadResponse): void; /** * Sends an error response to the client with appropriate status code and message. * @param response Node.js ServerResponse to send error to. * @param error Error object to convert to HTTP error response. */ sendError(response: NodeResponse, error: Error): Promise; /** * Sends streaming response to client with proper backpressure handling and range request support. * @param response Node.js ServerResponse to stream data to. * @param stream Readable stream containing the file data. * @param options Streaming options including headers, range, size, and status code. */ sendStream(response: NodeResponse, stream: Readable, { headers, range, size, statusCode }: { headers?: Record; range?: { end: number; start: number; }; size?: number; statusCode?: number; }): void; /** * Finish upload by sending final response to client with completed file data. * @param _request HTTP request (unused parameter) * @param response HTTP response object to send final response to * @param uploadResponse Final upload response data containing body, headers, and status code */ protected finish(_request: NodeRequest, response: NodeResponse, uploadResponse: UploadResponse | ResponseFile): void; /** * Build file URL from request and file data. * @param request HTTP request with optional originalUrl property * @param file File object containing ID and content type * @returns Constructed file URL with extension based on content type */ protected buildFileUrl(request: NodeRequest & { originalUrl?: string; }, file: TFile): string; /** * Negotiates content type based on Accept header and supported formats. * @param request HTTP request object containing Accept header. * @param supportedTypes Array of supported MIME types to match against. * @returns Best matching content type or undefined if no match found. */ negotiateContentType(request: NodeRequest, supportedTypes: string[]): string | undefined; /** * Default OPTIONS handler. * @param _request HTTP request (unused) * @param _response HTTP response (unused) * @returns Promise resolving to ResponseFile with CORS headers */ options(_request: NodeRequest, _response: NodeResponse): Promise>; /** * Retrieves a file or list of files based on the request path. * @param request Node.js IncomingMessage with optional originalUrl. * @param response Node.js ServerResponse. * @returns Promise resolving to a single file, paginated list, or array of files. * @throws {UploadError} When file is not found or storage error occurs. */ get(request: NodeRequest & { originalUrl?: string; }, _response: NodeResponse): Promise | ResponseList>; /** * Returns a list of uploaded files with optional pagination support. * @param request Node.js IncomingMessage containing query parameters for pagination. * @param _response Node.js ServerResponse (unused). * @returns Promise resolving to a paginated or complete list of uploaded files. */ list(request: NodeRequest, _response: NodeResponse): Promise>; /** * Streams download of a file with resumable support using HTTP range requests. * @param request Node.js IncomingMessage with optional originalUrl and range header. * @param response Node.js ServerResponse to stream the file to. * @throws {HttpError} When file is not found or streaming is not supported. */ download(request: NodeRequest & { originalUrl?: string; }, response: NodeResponse): Promise; /** * Check if error is related to undefined ID or path and throw appropriate HTTP error. * @param error Error object to check */ protected override checkForUndefinedIdOrPath(error: unknown): void; } /** * Multipart/form-data upload handler (Node.js version). * @example * ```ts * const multipart = new Multipart({ * storage, * maxFileSize: 100 * 1024 * 1024, // 100MB * }); * * app.use('/files', multipart.handle); * ``` */ declare class Multipart extends BaseHandlerNode { /** * Limiting enabled http method handler */ static override readonly methods: Handlers[]; private readonly multipartBase; /** * Maximum file size allowed for multipart uploads */ private maxFileSize; /** * Maximum header size allowed for multipart parser */ private maxHeaderSize; constructor(options: UploadOptions); /** * Handles multipart/form-data POST requests for file uploads. * @param request Node.js IncomingMessage containing multipart data. * @returns Promise resolving to ResponseFile with upload result. */ post(request: NodeRequest): Promise>; /** * Delete an uploaded file. * @param request Node.js IncomingMessage with file ID * @returns Promise resolving to ResponseFile with deletion result */ delete(request: NodeRequest): Promise>; /** * Compose and register HTTP method handlers. */ protected compose(): void; } /** * REST API handler for direct binary file uploads (Node.js version). * * This handler provides a clean REST interface for file operations: * - POST: Create a new file with raw binary data or initialize chunked upload * - PUT: Create or update a file (requires ID in URL) * - PATCH: Upload chunks for chunked uploads (requires ID in URL) * - GET: Retrieve a file or list files * - DELETE: Delete a file (single) or multiple files (via ?ids=id1,id2 or JSON body) * - HEAD: Get file metadata and upload progress * - OPTIONS: CORS preflight * @example * ```ts * const rest = new Rest({ * storage, * }); * * app.use('/files', rest.handle); * ``` */ declare class Rest extends BaseHandlerNode { /** * Limiting enabled http method handler */ static override readonly methods: Handlers[]; private readonly restBase; constructor(options: UploadOptions); /** * Compose and register HTTP method handlers. */ protected override compose(): void; /** * Build file URL from request and file data. * @param requestUrl Request URL string * @param file File object containing ID and content type * @returns Constructed file URL with extension based on content type */ protected buildFileUrlForRest(requestUrl: string, file: TFile): string; /** * Creates a new file via POST request with raw binary data. * Supports both full file uploads and chunked upload initialization. * @param request Node.js IncomingMessage with file data. * @returns Promise resolving to ResponseFile with upload result. */ post(request: NodeRequest): Promise>; /** * Create or update a file via PUT request. * Requires file ID in the URL path. * @param request Node.js IncomingMessage with file ID and data * @returns Promise resolving to ResponseFile with upload result */ put(request: NodeRequest): Promise>; /** * Delete an uploaded file or multiple files. * Supports single file (ID in URL) or batch delete (via ?ids=id1,id2 or JSON body). * @param request Node.js IncomingMessage with file ID(s) * @returns Promise resolving to ResponseFile (single) or ResponseList (batch) with deletion result */ delete(request: NodeRequest): Promise | ResponseList>; /** * Uploads a chunk via PATCH request for chunked uploads. * Headers required: X-Chunk-Offset (byte offset), Content-Length (chunk size). * Optional: X-Chunk-Checksum (SHA256 checksum for validation). * @param request Node.js IncomingMessage with chunk data. * @returns Promise resolving to ResponseFile with upload progress. */ patch(request: NodeRequest): Promise>; /** * Get file metadata via HEAD request. * For chunked uploads, also returns upload progress information. * @param request Node.js IncomingMessage with file ID * @returns Promise resolving to ResponseFile with metadata headers */ head(request: NodeRequest): Promise>; /** * Handle OPTIONS requests with REST API capabilities. * @returns Promise resolving to ResponseFile with CORS headers */ override options(): Promise>; /** * Retrieves a file or list of files based on the request path. * Delegates to BaseHandlerNode.get() method. * @param request Node.js IncomingMessage with optional originalUrl. * @param response Node.js ServerResponse. * @returns Promise resolving to a single file, paginated list, or array of files. */ override get(request: NodeRequest, response: NodeResponse): Promise | ResponseList>; } /** * TUS resumable upload protocol handler (Node.js version). * * [tus resumable upload protocol](https://github.com/tus/tus-resumable-upload-protocol/blob/master/protocol.md) * @example * ```ts * const tus = new Tus({storage}); * * app.all('/files', tus.handle); * ``` */ declare class Tus extends BaseHandlerNode { /** * Limiting enabled http method handler */ static override readonly methods: Handlers[]; override disableTerminationForFinishedUploads: boolean; private readonly tusBase; constructor(options: UploadOptions); /** * Handle OPTIONS requests with TUS protocol capabilities. * @returns Promise resolving to ResponseFile with TUS headers */ override options(): Promise>; /** * Creates a new TUS upload and optionally starts uploading data. * @param request Node.js IncomingMessage with TUS headers. * @returns Promise resolving to ResponseFile with upload location and offset. */ post(request: NodeRequest): Promise>; /** * Write a chunk of data to an existing TUS upload. * @param request Node.js IncomingMessage with chunk data and TUS headers * @returns Promise resolving to ResponseFile with updated offset */ patch(request: NodeRequest): Promise>; /** * Get current upload offset and metadata for TUS resumable uploads. * @param request Node.js IncomingMessage with upload ID * @returns Promise resolving to ResponseFile with upload-offset and metadata headers */ head(request: NodeRequest): Promise>; /** * Get TUS upload metadata and current status. * @param request Node.js IncomingMessage with upload ID * @returns Promise resolving to ResponseFile with file metadata as JSON */ override get(request: NodeRequest): Promise>; /** * Delete a TUS upload and its associated data. * @param request Node.js IncomingMessage with upload ID * @returns Promise resolving to ResponseFile with deletion confirmation */ delete(request: NodeRequest): Promise>; /** * Send TUS protocol response with required headers. * @param response Node.js ServerResponse to send response to * @param uploadResponse Response data with body, headers, and status code */ override send(response: NodeResponse, { body, headers, statusCode }: UploadResponse): void; /** * Compose and register HTTP method handlers. */ protected compose(): void; /** * Build file URL for TUS uploads (without file extension). * * On the Node runtime the TUS protocol chain only carries the request *path* (`requestUrl` is * `request.originalUrl || request.url`), so an absolute `Location` can only be produced when that * path is itself an absolute URL (e.g. an upstream proxy rewrote it). When it is just a path the * `Location` is necessarily relative — the request headers are not threaded through the protocol * methods. Use the fetch/hono handlers (which receive `request.url` with the origin) or set * `useRelativeLocation: true` explicitly for relative URLs everywhere. * @param requestUrl Request URL string (path-only on Node unless rewritten to an absolute URL). * @param file File object containing ID * @returns Constructed file URL for TUS protocol */ protected buildFileUrlForTus(requestUrl: string, file: TFile): string; } export { type AsyncHandler, type Handlers, type MethodHandler, Multipart, Rest, Tus, type UploadOptions };