import { EventEmitter } from 'node:events'; import { IncomingHttpHeaders } from 'node:http'; import { Readable } from 'node:stream'; import { U as UploadFile, B as BaseStorage, E as ErrorResponses } from "./storage.d-Cnjmzau5.js"; import { M as MediaTransformer } from "./media-transformer.d-2MuwlsDB.js"; import { R as ResponseBodyType, U as UploadResponse } from "./types.d-DFh9698o.js"; import { U as UploadOptions, R as ResponseList, a as ResponseFile } from "./types.d-BeCD21R_.js"; /** * What a `Location` is built from: the request URL, plus on Node the headers and connection its * origin is recovered from (a Node request URL is only a path). */ interface LocationSource { headers?: IncomingHttpHeaders; socket?: { encrypted?: boolean; }; url: string; } /** * Core base class containing shared business logic for all handlers. * This class is platform-agnostic and contains no Node.js or Web API specific code. * @template TFile The file type used by this handler. */ declare abstract class BaseHandlerCore extends EventEmitter { /** * Response body type for the handler. */ responseType: ResponseBodyType; /** * Storage instance for file operations. */ storage: BaseStorage; /** * Optional media transformer for image/video processing. */ mediaTransformer?: MediaTransformer; /** * Whether `GET` on the collection path lists all stored files. See {@link UploadOptions.allowList}. */ allowList: boolean; /** * Whether to disable termination for finished uploads. */ disableTerminationForFinishedUploads?: boolean; /** * Logger instance for debugging and error reporting. */ protected logger?: Console; /** * Gets the logger instance. * @returns Logger instance or undefined. */ get loggerInstance(): Console | undefined; /** * Internal error responses configuration. */ protected internalErrorResponses: ErrorResponses; /** * Gets the error responses configuration. * @returns Error responses configuration. */ get errorResponses(): ErrorResponses; constructor({ allowList, disableTerminationForFinishedUploads, mediaTransformer, storage }: UploadOptions); /** * Sets custom error responses. * @param value Partial error responses to override defaults. */ set errorResponses(value: Partial); /** * Assemble error responses by merging defaults with custom overrides. * @param customErrors Custom error responses to override defaults */ assembleErrors: (customErrors?: {}) => void; /** * Parses HTTP Range header and returns start/end byte positions for partial content requests. * A malformed or multi-range header is ignored (the full file is sent); an end past the file is * read as the remainder of the file (RFC 9110 ยง14.1.2). * @param rangeHeader HTTP Range header value (e.g., "bytes=0-1023"). * @param fileSize Total size of the file in bytes. * @returns Object with start and end positions, or undefined if the range is to be ignored. * @throws {HttpError} 416 with `Content-Range: bytes *\/size` when no requested byte exists. */ parseRangeHeader(rangeHeader: string | undefined, fileSize: number): { end: number; start: number; } | undefined; /** * The byte range a GET of a streamed file asks for: its `Range` header, dropped when an * `If-Range` no longer matches the file. The file's stream is destroyed when the range can't be * satisfied, since no response will consume it. * @param file Streamed file response. * @param rangeHeader `Range` header value. * @param ifRange `If-Range` header value. * @param headers Headers of the file response, compared against `If-Range`. * @returns The range to send, or `undefined` for the whole file. * @throws {HttpError} 416 when no requested byte exists. */ resolveRange(file: { size?: number; stream: Readable; }, rangeHeader: string | undefined, ifRange: string | undefined, headers: Record): { end: number; start: number; } | undefined; /** * Build file URL from request and file data. * Platform-agnostic version that accepts URL string. * * When `useRelativeLocation` is `false` (the default) the absolute origin is resolved from, in * order: an absolute `requestUrl` (the fetch runtimes pass `request.url`, which carries the * origin), then the host/proto derived from the headers of `request` (the Node runtimes pass it, * since `request.url` there is only a path). When no proxy header names the protocol, a TLS * connection gives `https:` and any other stays protocol-relative (`//host`). If neither yields * a host the Location stays relative rather than emitting a bogus `http://localhost` origin. * @param requestUrl Request URL string (absolute on fetch runtimes, path-only on Node). * @param file File object containing ID and content type * @param request Headers and connection of a Node request, to recover its host/proto. * @returns Constructed file URL with extension based on content type */ protected buildFileUrlFromString(requestUrl: string, file: TFile, request?: Omit): string; /** * The origin an absolute `Location` starts with, or `""` for a relative one: `useRelativeLocation` * is set, or neither an absolute `requestUrl` nor the request headers name a host. * @param requestUrl Request URL string (absolute on fetch runtimes, path-only on Node). * @param request Headers and connection of a Node request. * @returns The origin, or `""`. */ protected locationOrigin(requestUrl: string, request?: Omit): string; /** * Negotiates content type based on Accept header and supported formats. * Platform-agnostic version that accepts header string. * @param acceptHeader Accept header value * @param supportedTypes Array of supported MIME types to match against * @returns Best matching content type or undefined if no match found */ negotiateContentTypeFromHeader(acceptHeader: string | undefined, supportedTypes: string[]): string | undefined; /** * Converts an error into the response sent to the client, after the storage's onError hook ran. * @param error Error to convert * @returns The error response */ protected buildErrorResponse(error: Error): Promise; /** * Resolves a GET request: the addressed file, or - when listing is enabled - the list of files. * @param path Request path (without query string). * @param searchParams Query parameters of the request. * @param hasRange Whether the request carries a `Range` header. * @param list Produces the file list for paths that do not address a file. * @returns The file or list response. * @throws {HttpError} 404 when the path does not address a stored file and listing is disabled. */ protected resolveGet(path: string, searchParams: URLSearchParams, hasRange: boolean, list: () => Promise>): Promise | ResponseList>; /** * Resolves a GET request for a single file (or its `/metadata`) from the request path. * Platform-agnostic: shared by the Node and Fetch handlers. * * UUID-like segments are always file ids. Any other segment may be a file id (e.g. a nanoid or a * PUT-chosen id) or a collection path such as `/api/attachments`, so it falls back to the list * when no such file exists. * @param path Request path (without query string). * @param searchParams Query parameters of the request (used for media transformations). * @param hasRange Whether the request carries a `Range` header (forces streaming). * @returns The file response, or `undefined` when the path does not address a file and a list should be returned instead. * @throws {HttpError} 400 for unsafe ids, 404 when the file or its metadata is not found. */ protected getFileResponse(path: string, searchParams: URLSearchParams, hasRange: boolean): Promise | undefined>; /** * Serves the stored metadata of a file as JSON. * @param id File id. * @returns The metadata response. * @throws {HttpError} 404 when the file does not exist. */ private getMetadataResponse; /** * Applies the media transformer when the request carries transformation query parameters. * @param id File id. * @param searchParams Query parameters of the request. * @returns The transformed response, or `undefined` when no transformation applies (or it failed and the original should be served). * @throws {HttpError} 400 when the transformation parameters are invalid. */ private getTransformedResponse; /** * Serves the original file, streamed for range requests and files over 1 MB when the storage supports it. * @param fileMeta Stored metadata of the file. * @param extension Extension from the URL. * @param hasRange Whether the request carries a `Range` header. * @returns The file response. */ private getStoredFileResponse; /** * Returns a list of uploaded files with optional pagination support. * * `limit` and `page` must be positive integers; malformed values are ignored. At most * {@link MAX_LIST_ITEMS} files are ever read from storage for one request, whatever the provider's own page size. * @param searchParams Query parameters (`limit`, `page`) of the request. * @returns Promise resolving to a paginated (when `page` is given) or plain list of uploaded files. */ protected listFiles(searchParams: URLSearchParams): Promise>; /** * Check for undefined ID or path errors and throw appropriate HTTP errors. * @param error The error to check */ protected checkForUndefinedIdOrPath(error: unknown): void; } declare const TUS_RESUMABLE: string; declare const TUS_VERSION: string; export { BaseHandlerCore as B, LocationSource as L, TUS_RESUMABLE as T, TUS_VERSION as a };