import * as i0 from '@angular/core'; import { InjectionToken, OnInit, EventEmitter, ModuleWithProviders, Provider, OnDestroy } from '@angular/core'; import { Observable } from 'rxjs'; /** * Allows canceling some operation by calling cancel(). * onCancel callback can be used to execute cleanup logic when cancel is called. */ declare class Canceler { /** * Callback function to execute cleanup logic when cancel() is called */ onCancel: () => void; /** * Cancels the operation. */ cancel(): void; } declare enum ErrorType { NotFound = 0, Auth = 1, Retryable = 2, Fatal = 3 } type ShouldRetryFunction = (code: number, attempts: number) => boolean; type KeepPartialFunction = (code: number) => boolean; interface RetryConfig { /** Maximum number of retry attempts */ maxAttempts?: number; /** Upload not exist status codes */ shouldRestartCodes?: number[]; /** Bad token? status codes */ authErrorCodes?: number[]; /** Retryable 4xx status codes */ shouldRetryCodes?: number[]; /** Overrides the built-in function that determines whether the operation should be repeated */ shouldRetry?: ShouldRetryFunction; /** The minimum retry delay */ minDelay?: number; /** The maximum retry delay */ maxDelay?: number; /** Delay used between retries for non-error responses with missing range/offset */ onBusyDelay?: number; /** Time interval after which hanged requests must be retried */ timeout?: number; /** Determines whether partial chunks should be kept */ keepPartial?: boolean | KeepPartialFunction; } /** * Retryable ErrorHandler */ declare class RetryHandler { attempts: number; config: Required; private observedValue?; cancel: () => void; constructor(configOptions?: RetryConfig); /** * Determine error type based on response code * @param code - HTTP response status code */ kind(code: number): ErrorType; /** * Wait before next retry attempt * @param time - Delay in ms */ wait(time?: number): Promise; /** * Observes value to reset retry attempts counter * @param value - Value to observe */ observe(value?: string | number): void; } type ResponseBody = any; type RequestHeaders = Record; type Metadata = Record; interface RequestConfig { body?: BodyInit | null; canceler: Canceler; signal?: AbortSignal; headers: RequestHeaders; method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'; onUploadProgress?: (evt: ProgressEvent) => void; responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text'; url: string; validateStatus?: (status: number) => boolean; withCredentials?: boolean; } type RequestOptions = Partial & { skipAuthorization?: boolean; }; type AuthorizeRequest = (req: RequestConfig, token?: string) => RequestConfig | Promise; type PreRequest = (req: RequestConfig) => Promise | RequestOptions | void; type UploadStatus = 'added' | 'queue' | 'uploading' | 'complete' | 'error' | 'cancelled' | 'paused' | 'retry' | 'updated'; type UploadAction = 'upload' | 'cancel' | 'pause' | 'update'; interface UploadState { /** Uploaded file */ readonly file: File; /** Original file name */ readonly name: string; /** Progress percentage */ readonly progress: number; /** Estimated remaining time */ readonly remaining: number; /** HTTP response body */ readonly response: ResponseBody; /** HTTP response status code */ readonly responseStatus: number; /** HTTP response headers */ readonly responseHeaders: Record; /** File size in bytes */ readonly size: number; /** Upload speed bytes/sec */ readonly speed: number; /** Upload status */ readonly status: UploadStatus; /** Unique upload id */ readonly uploadId: string; /** File url */ readonly url: string; } interface UploadItem { /** * URL to create new uploads. * @defaultValue '/upload' */ endpoint?: string; /** * Headers to be appended to each HTTP request */ headers?: RequestHeaders | ((file: File) => RequestHeaders); /** * Custom uploads metadata */ metadata?: Metadata | ((file: File) => Metadata); /** * Authorization token as a `string` or function returning a `string` or `Promise` */ token?: string | ((httpStatus: number) => string | Promise); } interface UploadxControlEvent extends UploadItem { readonly uploadId?: string; action?: UploadAction; } interface UploaderOptions extends UploadItem { retryConfig?: RetryConfig; /** * Set a fixed chunk size. * If not specified, the optimal size will be automatically adjusted based on the network speed. */ chunkSize?: number; /** Adaptive chunk size limit */ maxChunkSize?: number; withCredentials?: boolean; /** * Set the expected server response type */ responseType?: 'json' | 'text' | 'document'; /** * Function called before every request */ prerequest?: PreRequest; /** * Function used to apply authorization token */ authorize?: AuthorizeRequest; } interface AjaxRequestConfig extends RequestOptions { [x: string]: any; data?: BodyInit | null; url: string; } interface AjaxResponse { data: T; status: number; headers: Record; } interface Ajax { request: (config: AjaxRequestConfig) => Promise>; } declare class UploadxAjax { private buildXhr; constructor(buildXhr: () => XMLHttpRequest); request: ({ method, data, headers, url, responseType, signal, onUploadProgress, timeout, withCredentials, validateStatus }: AjaxRequestConfig) => Promise>; getResponseHeaders(xhr: XMLHttpRequest): Record; getResponseBody(xhr: XMLHttpRequest, responseType?: string): T; } declare const UPLOADX_AJAX: InjectionToken; /** * Adaptive chunk size */ declare class DynamicChunk { /** Maximum chunk size in bytes */ static maxSize: number; /** Minimum chunk size in bytes */ static minSize: number; /** Initial chunk size in bytes */ static size: number; static minChunkTime: number; static maxChunkTime: number; /** * Scales the chunk size based on the throughput. * If the elapsed time to upload a chunk is less than the min time, increase the chunk size. * If the elapsed time is more than the max time, decrease the chunk size. * Keeps the chunk size within the min and max limits. * @param throughput - represents the upload rate in bytes/sec. */ static scale(throughput: number): number; } /** * Uploader Base Class */ declare abstract class Uploader implements UploadState { readonly file: File; readonly options: Readonly; readonly stateChange: (uploader: Uploader) => void; readonly ajax: Ajax; name: string; readonly size: number; readonly uploadId: string; response: ResponseBody; responseStatus: number; responseHeaders: Record; progress: number; remaining: number; speed: number; /** Custom headers */ headers: RequestHeaders; /** Metadata Object */ metadata: Metadata; /** Upload endpoint */ endpoint: string; /** Chunk size in bytes */ chunkSize: number; /** Auth token/tokenGetter */ token: UploadxControlEvent['token']; /** Byte offset within the whole file */ offset?: number; /** Retries handler */ retry: RetryHandler; canceler: Canceler; abortController: AbortController; /** Set HttpRequest responseType */ responseType?: 'json' | 'text' | 'document'; protected _authorize: AuthorizeRequest; protected _prerequest: PreRequest; protected _token: string; private _eventsCount; constructor(file: File, options: Readonly, stateChange: (uploader: Uploader) => void, ajax: Ajax); private _url; get url(): string; set url(value: string); private _status; get status(): UploadStatus; set status(s: UploadStatus); /** * Configure uploader */ configure({ metadata, headers, token, endpoint, action }: UploadxControlEvent): void; /** * Starts uploading */ upload(): Promise; /** * Performs http requests */ request(requestOptions: RequestOptions): Promise; /** * Set auth token string */ updateToken: () => string | Promise; /** * Get file URI */ protected abstract getFileUrl(): Promise; /** * Send file content and return an offset for the next request */ protected abstract sendFileContent(): Promise; /** * Get an offset for the next request */ protected abstract getOffset(): Promise; /** * Updating the metadata of the upload */ protected update(_data: T): Promise; protected abort(): void; protected cancel(): Promise; /** * Gets the value from the response */ protected getValueFromResponse(key: string): string | null; /** * Get file chunk * @param offset - number of bytes of the file to skip * @param size - chunk size */ getChunk(offset?: number, size?: number): { start: number; end: number; body: Blob; }; private getRetryAfterFromBackend; private cancelAndSendState; private updateAndSendState; private cleanup; private onProgress; } interface UidService { generateId(uploader: Uploader): Promise | string; } declare class IdService implements UidService { generateId(uploader: Uploader): Promise | string; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } /** * Global Module Options */ interface UploadxOptions extends UploaderOptions { /** * Provide a user-defined class to support another upload protocol or to extend an existing one. * @defaultValue UploadX */ uploaderClass?: UploaderClass; /** * Set the maximum parallel uploads * @defaultValue 2 */ concurrency?: number; /** * Automatically start upload when files added * @defaultValue true */ autoUpload?: boolean; /** * File types the user can pick from the file input */ allowedTypes?: string; /** * Add 'multiple' attribute * @defaultValue true */ multiple?: boolean; /** * Retention time for incomplete uploads * @defaultValue 24 */ storeIncompleteHours?: number; } interface UploadxFactoryOptions extends UploadxOptions { endpoint: string; autoUpload: boolean; concurrency: number; uploaderClass: UploaderClass; authorize: AuthorizeRequest; storeIncompleteHours: number; } type UploaderClass = new (file: File, options: UploaderOptions, stateChange: (uploader: Uploader) => void, ajax: Ajax) => Uploader; declare const UPLOADX_FACTORY_OPTIONS: InjectionToken; declare const UPLOADX_OPTIONS: InjectionToken; declare class Store { readonly prefix: string; private ttl; constructor(prefix?: string); set(key: string, value: T): void; get(key: string): T | null; delete(key: string): void; clear(maxAgeHours?: number): void; private keys; } declare const store: Store | Map; declare function isLocalStorageAvailable(): boolean; /** * Implements tus resumable upload protocol * {@link https://github.com/tus/tus-resumable-upload-protocol/blob/master/protocol.md Github} */ declare class Tus extends Uploader { headers: { 'Tus-Resumable': string; }; getFileUrl(): Promise; sendFileContent(): Promise; getOffset(): Promise; protected getOffsetFromResponse(): number | undefined; } /** * Implements XHR/CORS Resumable Upload * {@link https://github.com/kukhariev/node-uploadx/blob/master/proto.md Github} * @see {@link https://developers.google.com/drive/api/v3/manage-uploads#resumable Google Drive API documentation} */ declare class UploaderX extends Uploader { responseType: "json"; getFileUrl(): Promise; sendFileContent(): Promise; getOffset(): Promise; update(data: T): Promise; protected getOffsetFromResponse(): number | undefined; } declare function getRangeEnd(range?: string): number; declare class UploadxDirective implements OnInit { set uploadx(value: UploadxOptions | ''); options: UploadxOptions; set control(value: UploadxControlEvent | ''); state: EventEmitter; private readonly elementRef; private readonly renderer; private readonly uploadService; ngOnInit(): void; fileChange(event: Event): void; fileListener(files?: FileList | File[]): void; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵdir: i0.ɵɵDirectiveDeclaration; } declare class UploadxDropDirective { active: boolean; fileInput?: UploadxDirective; private readonly uploadService; dropHandler(event: DragEvent): void; onDragOver(event: DragEvent): void; onDragLeave(event: DragEvent): void; /** * Extracts the files from a `DragEvent` object */ getFiles(event: DragEvent): FileList | File[]; protected _stopEvents(event: DragEvent): void; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵdir: i0.ɵɵDirectiveDeclaration; } declare class UploadxModule { static withConfig(options: UploadxOptions): ModuleWithProviders; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵmod: i0.ɵɵNgModuleDeclaration; static ɵinj: i0.ɵɵInjectorDeclaration; } /** * Provides configuration options for standalone app. * * @example * ```ts * bootstrapApplication(AppComponent, { * providers: [ * provideUploadx({ * endpoint: uploadUrl, * allowedTypes: 'video/*,audio/*', * maxChunkSize: 96 * 1024 * 1024 * }) * ] * }); * ``` */ declare function provideUploadx(options?: UploadxOptions): Provider[]; declare const UPLOAD_STATE_KEYS: (keyof UploadState)[]; declare class UploadxService implements OnDestroy { /** Upload Queue */ queue: Uploader[]; readonly options: UploadxFactoryOptions; private readonly eventsStream; private subs; private ngZone; readonly ajax: Ajax; private idService; constructor(); /** Upload status events */ get events(): Observable; /** * Initializes service * @param options global module options * @returns Observable that emits a new value on progress or status changes */ init(options?: UploadxOptions): Observable; /** * Initializes service * @param options global module options * @returns Observable that emits the current array of uploaders */ connect(options?: UploadxOptions): Observable; /** * Terminates all uploads and clears the queue */ disconnect(): void; /** * Returns current uploads state * @example * // restore background uploads * this.uploads = this.uploadService.state(); */ state(): UploadState[]; ngOnDestroy(): void; /** * Creates uploaders for files and adds them to the upload queue */ handleFiles(files: FileList | File | File[], options?: UploadxOptions): void; /** * Upload control * @example * // pause all * this.uploadService.control({ action: 'pause' }); * // pause upload with uploadId * this.uploadService.control({ action: 'pause', uploadId}); * // set token * this.uploadService.control({ token: `TOKEN` }); */ control(evt: UploadxControlEvent): void; /** * Number of active uploads */ get activeUploadsCount(): number; /** * Performs http requests */ request(config: AjaxRequestConfig): Promise>; private stateChange; private addUploaderInstance; private processQueue; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } declare function resolveUrl(url: string, base: string): string; /** * Unwraps a value if it is a function, otherwise returns the value directly. * Useful for allowing values to optionally be specified as functions. */ declare function unfunc(value: T | ((ref: V) => T), ref: V): T; declare const pick: (obj: T, props: K[]) => Pick; declare function isNumber(x?: unknown): x is number; /** * 32-bit FNV-1a hash function */ declare function createHash(str: string): number; /** * Utility functions for base64 encoding and decoding strings and objects. */ declare const b64: { encode: (str: string) => string; decode: (str: string) => string; serialize: (obj: Record) => string; parse: (encoded: string) => Record; }; declare function isBrowser(): boolean; declare function onLine(): boolean; export { Canceler, DynamicChunk, ErrorType, IdService, RetryHandler, Store, Tus, UPLOADX_AJAX, UPLOADX_FACTORY_OPTIONS, UPLOADX_OPTIONS, UPLOAD_STATE_KEYS, Uploader, UploaderX, UploadxAjax, UploadxDirective, UploadxDropDirective, UploadxModule, UploadxService, b64, createHash, getRangeEnd, isBrowser, isLocalStorageAvailable, isNumber, onLine, pick, provideUploadx, resolveUrl, store, unfunc }; export type { Ajax, AjaxRequestConfig, AjaxResponse, AuthorizeRequest, KeepPartialFunction, Metadata, PreRequest, RequestConfig, RequestHeaders, RequestOptions, ResponseBody, RetryConfig, ShouldRetryFunction, UidService, UploadAction, UploadState, UploadStatus, UploaderClass, UploaderOptions, UploadxControlEvent, UploadxFactoryOptions, UploadxOptions };