{"version":3,"file":"Upload.mjs","sources":["../../../lib/upload/uploader/Upload.ts"],"sourcesContent":["/*!\n * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type PQueue from 'p-queue'\n\nimport { TypedEventTarget } from 'typescript-event-target'\n\nexport const UploadStatus = Object.freeze({\n\t/** The upload was initialized */\n\tINITIALIZED: 0,\n\t/** The upload was scheduled but is not yet uploading */\n\tSCHEDULED: 1,\n\t/** The upload itself is running */\n\tUPLOADING: 2,\n\t/** Chunks are being assembled */\n\tASSEMBLING: 3,\n\t/** The upload finished successfully */\n\tFINISHED: 4,\n\t/** The upload was cancelled by the user */\n\tCANCELLED: 5,\n\t/** The upload failed */\n\tFAILED: 6,\n})\n\nexport type TUploadStatus = typeof UploadStatus[keyof typeof UploadStatus]\n\ninterface UploadEvents {\n\tfinished: CustomEvent<IUpload>\n\tprogress: CustomEvent<IUpload>\n}\n\nexport interface IUpload extends TypedEventTarget<UploadEvents> {\n\t/**\n\t * The source of the upload\n\t */\n\treadonly source: string\n\t/**\n\t * Whether the upload is chunked or not\n\t */\n\treadonly isChunked: boolean\n\t/**\n\t * The total size of the upload in bytes\n\t */\n\treadonly totalBytes: number\n\t/**\n\t * Timestamp of when the upload started.\n\t * Will return `undefined` if the upload has not started yet.\n\t */\n\treadonly startTime?: number\n\t/**\n\t * The number of bytes that have been uploaded so far\n\t */\n\treadonly uploadedBytes: number\n\t/**\n\t * The current status of the upload\n\t */\n\treadonly status: TUploadStatus\n\t/**\n\t * The internal abort signal\n\t */\n\treadonly signal: AbortSignal\n\n\t/**\n\t * The children of this upload.\n\t * - For a file upload, this will be an empty array.\n\t * - For a folder upload, this will be the uploads of the children files and folders.\n\t */\n\treadonly children: IUpload[]\n\n\t/**\n\t * Cancels the upload\n\t */\n\tcancel(): void\n}\n\nexport interface IUploadOptions {\n\theaders: Record<string, string>\n\tnoChunking: boolean\n\tretries: number\n}\n\nexport abstract class Upload<CustomOpts = object> extends TypedEventTarget<UploadEvents> implements Partial<IUpload> {\n\t#abortController = new AbortController()\n\tprotected readonly options: IUploadOptions & CustomOpts\n\n\tprotected constructor(options: Partial<IUploadOptions> & CustomOpts) {\n\t\tsuper()\n\t\tthis.options = {\n\t\t\theaders: {},\n\t\t\tnoChunking: false,\n\t\t\tretries: 5,\n\t\t\t...options,\n\t\t}\n\t}\n\n\t/**\n\t * The destination of this upload.\n\t * This is *not* URL encoded, it is encoded when the upload requests are made.\n\t */\n\tpublic abstract source: string\n\n\tget signal(): AbortSignal {\n\t\treturn this.#abortController.signal\n\t}\n\n\t/**\n\t * Move this upload to a new destination.\n\t *\n\t * This is needed when a parent folder is renamed while resolving conflicts,\n\t * as the child uploads are already initialized with the previous destination.\n\t *\n\t * @param source - The new destination of this upload\n\t */\n\tpublic rebase(source: string): void {\n\t\tthis.source = source\n\t}\n\n\t/**\n\t * Cancels the upload\n\t */\n\tpublic cancel(): void {\n\t\tthis.#abortController.abort()\n\t}\n\n\t/**\n\t * Get the children of this upload.\n\t * For a file upload, this will be an empty array, for a folder upload, this will be the uploads of the children files and folders.\n\t */\n\tpublic get children(): IUpload[] {\n\t\treturn []\n\t}\n\n\t/**\n\t * Start the upload\n\t *\n\t * @param queue - The job queue. It is used to limit the number of concurrent upload jobs.\n\t */\n\tpublic abstract start(queue: PQueue): Promise<void>\n}\n"],"names":[],"mappings":";AAAA;AAAA;AAAA;AAAA;AASO,MAAM,eAAe,OAAO,OAAO;AAAA;AAAA,EAEzC,aAAa;AAAA;AAAA,EAEb,WAAW;AAAA;AAAA,EAEX,WAAW;AAAA;AAAA,EAEX,YAAY;AAAA;AAAA,EAEZ,UAAU;AAAA;AAAA,EAEV,WAAW;AAAA;AAAA,EAEX,QAAQ;AACT,CAAC;AA2DM,MAAe,eAAoC,iBAA2D;AAAA,EACpH,mBAAmB,IAAI,gBAAA;AAAA,EACJ;AAAA,EAET,YAAY,SAA+C;AACpE,UAAA;AACA,SAAK,UAAU;AAAA,MACd,SAAS,CAAA;AAAA,MACT,YAAY;AAAA,MACZ,SAAS;AAAA,MACT,GAAG;AAAA,IAAA;AAAA,EAEL;AAAA,EAQA,IAAI,SAAsB;AACzB,WAAO,KAAK,iBAAiB;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUO,OAAO,QAAsB;AACnC,SAAK,SAAS;AAAA,EACf;AAAA;AAAA;AAAA;AAAA,EAKO,SAAe;AACrB,SAAK,iBAAiB,MAAA;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAW,WAAsB;AAChC,WAAO,CAAA;AAAA,EACR;AAQD;"}