index.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. import BasePlugin from '@uppy/core/lib/BasePlugin.js'
  2. import { Provider, RequestClient } from '@uppy/companion-client'
  3. import EventManager from '@uppy/utils/lib/EventManager'
  4. import { RateLimitedQueue } from '@uppy/utils/lib/RateLimitedQueue'
  5. import { filterNonFailedFiles, filterFilesToEmitUploadStarted } from '@uppy/utils/lib/fileFilters'
  6. import { createAbortError } from '@uppy/utils/lib/AbortController'
  7. import MultipartUploader, { pausingUploadReason } from './MultipartUploader.js'
  8. import createSignedURL from './createSignedURL.js'
  9. import packageJson from '../package.json'
  10. function assertServerError (res) {
  11. if (res && res.error) {
  12. const error = new Error(res.message)
  13. Object.assign(error, res.error)
  14. throw error
  15. }
  16. return res
  17. }
  18. /**
  19. * Computes the expiry time for a request signed with temporary credentials. If
  20. * no expiration was provided, or an invalid value (e.g. in the past) is
  21. * provided, undefined is returned. This function assumes the client clock is in
  22. * sync with the remote server, which is a requirement for the signature to be
  23. * validated for AWS anyway.
  24. *
  25. * @param {import('../types/index.js').AwsS3STSResponse['credentials']} credentials
  26. * @returns {number | undefined}
  27. */
  28. function getExpiry (credentials) {
  29. const expirationDate = credentials.Expiration
  30. if (expirationDate) {
  31. const timeUntilExpiry = Math.floor((new Date(expirationDate) - Date.now()) / 1000)
  32. if (timeUntilExpiry > 9) {
  33. return timeUntilExpiry
  34. }
  35. }
  36. return undefined
  37. }
  38. function getAllowedMetadata ({ meta, allowedMetaFields, querify = false }) {
  39. const metaFields = allowedMetaFields ?? Object.keys(meta)
  40. if (!meta) return {}
  41. return Object.fromEntries(
  42. metaFields
  43. .filter(key => meta[key] != null)
  44. .map((key) => {
  45. const realKey = querify ? `metadata[${key}]` : key
  46. const value = String(meta[key])
  47. return [realKey, value]
  48. }),
  49. )
  50. }
  51. function throwIfAborted (signal) {
  52. if (signal?.aborted) { throw createAbortError('The operation was aborted', { cause: signal.reason }) }
  53. }
  54. class HTTPCommunicationQueue {
  55. #abortMultipartUpload
  56. #cache = new WeakMap()
  57. #createMultipartUpload
  58. #fetchSignature
  59. #getUploadParameters
  60. #listParts
  61. #previousRetryDelay
  62. #requests
  63. #retryDelayIterator
  64. #sendCompletionRequest
  65. #setS3MultipartState
  66. #uploadPartBytes
  67. #getFile
  68. constructor (requests, options, setS3MultipartState, getFile) {
  69. this.#requests = requests
  70. this.#setS3MultipartState = setS3MultipartState
  71. this.#getFile = getFile
  72. this.setOptions(options)
  73. }
  74. setOptions (options) {
  75. const requests = this.#requests
  76. if ('abortMultipartUpload' in options) {
  77. this.#abortMultipartUpload = requests.wrapPromiseFunction(options.abortMultipartUpload, { priority:1 })
  78. }
  79. if ('createMultipartUpload' in options) {
  80. this.#createMultipartUpload = requests.wrapPromiseFunction(options.createMultipartUpload, { priority:-1 })
  81. }
  82. if ('signPart' in options) {
  83. this.#fetchSignature = requests.wrapPromiseFunction(options.signPart)
  84. }
  85. if ('listParts' in options) {
  86. this.#listParts = requests.wrapPromiseFunction(options.listParts)
  87. }
  88. if ('completeMultipartUpload' in options) {
  89. this.#sendCompletionRequest = requests.wrapPromiseFunction(options.completeMultipartUpload, { priority:1 })
  90. }
  91. if ('retryDelays' in options) {
  92. this.#retryDelayIterator = options.retryDelays?.values()
  93. }
  94. if ('uploadPartBytes' in options) {
  95. this.#uploadPartBytes = requests.wrapPromiseFunction(options.uploadPartBytes, { priority:Infinity })
  96. }
  97. if ('getUploadParameters' in options) {
  98. this.#getUploadParameters = requests.wrapPromiseFunction(options.getUploadParameters)
  99. }
  100. }
  101. async #shouldRetry (err) {
  102. const requests = this.#requests
  103. const status = err?.source?.status
  104. // TODO: this retry logic is taken out of Tus. We should have a centralized place for retrying,
  105. // perhaps the rate limited queue, and dedupe all plugins with that.
  106. if (status == null) {
  107. return false
  108. }
  109. if (status === 403 && err.message === 'Request has expired') {
  110. if (!requests.isPaused) {
  111. // We don't want to exhaust the retryDelayIterator as long as there are
  112. // more than one request in parallel, to give slower connection a chance
  113. // to catch up with the expiry set in Companion.
  114. if (requests.limit === 1 || this.#previousRetryDelay == null) {
  115. const next = this.#retryDelayIterator?.next()
  116. if (next == null || next.done) {
  117. return false
  118. }
  119. // If there are more than 1 request done in parallel, the RLQ limit is
  120. // decreased and the failed request is requeued after waiting for a bit.
  121. // If there is only one request in parallel, the limit can't be
  122. // decreased, so we iterate over `retryDelayIterator` as we do for
  123. // other failures.
  124. // `#previousRetryDelay` caches the value so we can re-use it next time.
  125. this.#previousRetryDelay = next.value
  126. }
  127. // No need to stop the other requests, we just want to lower the limit.
  128. requests.rateLimit(0)
  129. await new Promise(resolve => setTimeout(resolve, this.#previousRetryDelay))
  130. }
  131. } else if (status === 429) {
  132. // HTTP 429 Too Many Requests => to avoid the whole download to fail, pause all requests.
  133. if (!requests.isPaused) {
  134. const next = this.#retryDelayIterator?.next()
  135. if (next == null || next.done) {
  136. return false
  137. }
  138. requests.rateLimit(next.value)
  139. }
  140. } else if (status > 400 && status < 500 && status !== 409) {
  141. // HTTP 4xx, the server won't send anything, it's doesn't make sense to retry
  142. return false
  143. } else if (typeof navigator !== 'undefined' && navigator.onLine === false) {
  144. // The navigator is offline, let's wait for it to come back online.
  145. if (!requests.isPaused) {
  146. requests.pause()
  147. window.addEventListener('online', () => {
  148. requests.resume()
  149. }, { once: true })
  150. }
  151. } else {
  152. // Other error code means the request can be retried later.
  153. const next = this.#retryDelayIterator?.next()
  154. if (next == null || next.done) {
  155. return false
  156. }
  157. await new Promise(resolve => setTimeout(resolve, next.value))
  158. }
  159. return true
  160. }
  161. async getUploadId (file, signal) {
  162. let cachedResult
  163. // As the cache is updated asynchronously, there could be a race condition
  164. // where we just miss a new result so we loop here until we get nothing back,
  165. // at which point it's out turn to create a new cache entry.
  166. while ((cachedResult = this.#cache.get(file.data)) != null) {
  167. try {
  168. return await cachedResult
  169. } catch {
  170. // In case of failure, we want to ignore the cached error.
  171. // At this point, either there's a new cached value, or we'll exit the loop a create a new one.
  172. }
  173. }
  174. const promise = this.#createMultipartUpload(this.#getFile(file), signal)
  175. const abortPromise = () => {
  176. promise.abort(signal.reason)
  177. this.#cache.delete(file.data)
  178. }
  179. signal.addEventListener('abort', abortPromise, { once: true })
  180. this.#cache.set(file.data, promise)
  181. promise.then(async (result) => {
  182. signal.removeEventListener('abort', abortPromise)
  183. this.#setS3MultipartState(file, result)
  184. this.#cache.set(file.data, result)
  185. }, () => {
  186. signal.removeEventListener('abort', abortPromise)
  187. this.#cache.delete(file.data)
  188. })
  189. return promise
  190. }
  191. async abortFileUpload (file) {
  192. const result = this.#cache.get(file.data)
  193. if (result == null) {
  194. // If the createMultipartUpload request never was made, we don't
  195. // need to send the abortMultipartUpload request.
  196. return
  197. }
  198. // Remove the cache entry right away for follow-up requests do not try to
  199. // use the soon-to-be aborted chached values.
  200. this.#cache.delete(file.data)
  201. this.#setS3MultipartState(file, Object.create(null))
  202. let awaitedResult
  203. try {
  204. awaitedResult = await result
  205. } catch {
  206. // If the cached result rejects, there's nothing to abort.
  207. return
  208. }
  209. await this.#abortMultipartUpload(this.#getFile(file), awaitedResult)
  210. }
  211. async #nonMultipartUpload (file, chunk, signal) {
  212. const {
  213. method = 'POST',
  214. url,
  215. fields,
  216. headers,
  217. } = await this.#getUploadParameters(this.#getFile(file), { signal }).abortOn(signal)
  218. let body
  219. const data = chunk.getData()
  220. if (method.toUpperCase() === 'POST') {
  221. const formData = new FormData()
  222. Object.entries(fields).forEach(([key, value]) => formData.set(key, value))
  223. formData.set('file', data)
  224. body = formData
  225. } else {
  226. body = data
  227. }
  228. const { onProgress, onComplete } = chunk
  229. return this.#uploadPartBytes({
  230. signature: { url, headers, method },
  231. body,
  232. size: data.size,
  233. onProgress,
  234. onComplete,
  235. signal,
  236. }).abortOn(signal)
  237. }
  238. /**
  239. * @param {import("@uppy/core").UppyFile} file
  240. * @param {import("../types/chunk").Chunk[]} chunks
  241. * @param {AbortSignal} signal
  242. * @returns {Promise<void>}
  243. */
  244. async uploadFile (file, chunks, signal) {
  245. throwIfAborted(signal)
  246. if (chunks.length === 1 && !chunks[0].shouldUseMultipart) {
  247. return this.#nonMultipartUpload(file, chunks[0], signal)
  248. }
  249. const { uploadId, key } = await this.getUploadId(file, signal)
  250. throwIfAborted(signal)
  251. try {
  252. const parts = await Promise.all(chunks.map((chunk, i) => this.uploadChunk(file, i + 1, chunk, signal)))
  253. throwIfAborted(signal)
  254. return await this.#sendCompletionRequest(
  255. this.#getFile(file),
  256. { key, uploadId, parts, signal },
  257. ).abortOn(signal)
  258. } catch (err) {
  259. if (err?.cause !== pausingUploadReason && err?.name !== 'AbortError') {
  260. // We purposefully don't wait for the promise and ignore its status,
  261. // because we want the error `err` to bubble up ASAP to report it to the
  262. // user. A failure to abort is not that big of a deal anyway.
  263. this.abortFileUpload(file)
  264. }
  265. throw err
  266. }
  267. }
  268. restoreUploadFile (file, uploadIdAndKey) {
  269. this.#cache.set(file.data, uploadIdAndKey)
  270. }
  271. async resumeUploadFile (file, chunks, signal) {
  272. throwIfAborted(signal)
  273. if (chunks.length === 1 && !chunks[0].shouldUseMultipart) {
  274. return this.#nonMultipartUpload(file, chunks[0], signal)
  275. }
  276. const { uploadId, key } = await this.getUploadId(file, signal)
  277. throwIfAborted(signal)
  278. const alreadyUploadedParts = await this.#listParts(
  279. this.#getFile(file),
  280. { uploadId, key, signal },
  281. ).abortOn(signal)
  282. throwIfAborted(signal)
  283. const parts = await Promise.all(
  284. chunks
  285. .map((chunk, i) => {
  286. const partNumber = i + 1
  287. const alreadyUploadedInfo = alreadyUploadedParts.find(({ PartNumber }) => PartNumber === partNumber)
  288. if (alreadyUploadedInfo == null) {
  289. return this.uploadChunk(file, partNumber, chunk, signal)
  290. }
  291. // Already uploaded chunks are set to null. If we are restoring the upload, we need to mark it as already uploaded.
  292. chunk?.setAsUploaded?.()
  293. return { PartNumber: partNumber, ETag: alreadyUploadedInfo.ETag }
  294. }),
  295. )
  296. throwIfAborted(signal)
  297. return this.#sendCompletionRequest(
  298. this.#getFile(file),
  299. { key, uploadId, parts, signal },
  300. ).abortOn(signal)
  301. }
  302. /**
  303. *
  304. * @param {import("@uppy/core").UppyFile} file
  305. * @param {number} partNumber
  306. * @param {import("../types/chunk").Chunk} chunk
  307. * @param {AbortSignal} signal
  308. * @returns {Promise<object>}
  309. */
  310. async uploadChunk (file, partNumber, chunk, signal) {
  311. throwIfAborted(signal)
  312. const { uploadId, key } = await this.getUploadId(file, signal)
  313. throwIfAborted(signal)
  314. for (;;) {
  315. const chunkData = chunk.getData()
  316. const { onProgress, onComplete } = chunk
  317. const signature = await this.#fetchSignature(this.#getFile(file), {
  318. uploadId, key, partNumber, body: chunkData, signal,
  319. }).abortOn(signal)
  320. throwIfAborted(signal)
  321. try {
  322. return {
  323. PartNumber: partNumber,
  324. ...await this.#uploadPartBytes({
  325. signature, body: chunkData, size: chunkData.size, onProgress, onComplete, signal,
  326. }).abortOn(signal),
  327. }
  328. } catch (err) {
  329. if (!await this.#shouldRetry(err)) throw err
  330. }
  331. }
  332. }
  333. }
  334. export default class AwsS3Multipart extends BasePlugin {
  335. static VERSION = packageJson.version
  336. #companionCommunicationQueue
  337. #client
  338. constructor (uppy, opts) {
  339. super(uppy, opts)
  340. this.type = 'uploader'
  341. this.id = this.opts.id || 'AwsS3Multipart'
  342. this.title = 'AWS S3 Multipart'
  343. this.#client = new RequestClient(uppy, opts)
  344. const defaultOptions = {
  345. // TODO: null here means “include all”, [] means include none.
  346. // This is inconsistent with @uppy/aws-s3 and @uppy/transloadit
  347. allowedMetaFields: null,
  348. limit: 6,
  349. shouldUseMultipart: (file) => file.size !== 0, // TODO: Switch default to:
  350. // eslint-disable-next-line no-bitwise
  351. // shouldUseMultipart: (file) => file.size >> 10 >> 10 > 100,
  352. retryDelays: [0, 1000, 3000, 5000],
  353. createMultipartUpload: this.createMultipartUpload.bind(this),
  354. listParts: this.listParts.bind(this),
  355. abortMultipartUpload: this.abortMultipartUpload.bind(this),
  356. completeMultipartUpload: this.completeMultipartUpload.bind(this),
  357. getTemporarySecurityCredentials: false,
  358. signPart: opts?.getTemporarySecurityCredentials ? this.createSignedURL.bind(this) : this.signPart.bind(this),
  359. uploadPartBytes: AwsS3Multipart.uploadPartBytes,
  360. getUploadParameters: opts?.getTemporarySecurityCredentials
  361. ? this.createSignedURL.bind(this)
  362. : this.getUploadParameters.bind(this),
  363. companionHeaders: {},
  364. }
  365. this.opts = { ...defaultOptions, ...opts }
  366. if (opts?.prepareUploadParts != null && opts.signPart == null) {
  367. this.opts.signPart = async (file, { uploadId, key, partNumber, body, signal }) => {
  368. const { presignedUrls, headers } = await opts
  369. .prepareUploadParts(file, { uploadId, key, parts: [{ number: partNumber, chunk: body }], signal })
  370. return { url: presignedUrls?.[partNumber], headers: headers?.[partNumber] }
  371. }
  372. }
  373. /**
  374. * Simultaneous upload limiting is shared across all uploads with this plugin.
  375. *
  376. * @type {RateLimitedQueue}
  377. */
  378. this.requests = this.opts.rateLimitedQueue ?? new RateLimitedQueue(this.opts.limit)
  379. this.#companionCommunicationQueue = new HTTPCommunicationQueue(
  380. this.requests,
  381. this.opts,
  382. this.#setS3MultipartState,
  383. this.#getFile,
  384. )
  385. this.uploaders = Object.create(null)
  386. this.uploaderEvents = Object.create(null)
  387. this.uploaderSockets = Object.create(null)
  388. }
  389. [Symbol.for('uppy test: getClient')] () { return this.#client }
  390. setOptions (newOptions) {
  391. this.#companionCommunicationQueue.setOptions(newOptions)
  392. super.setOptions(newOptions)
  393. this.#setCompanionHeaders()
  394. }
  395. /**
  396. * Clean up all references for a file's upload: the MultipartUploader instance,
  397. * any events related to the file, and the Companion WebSocket connection.
  398. *
  399. * Set `opts.abort` to tell S3 that the multipart upload is cancelled and must be removed.
  400. * This should be done when the user cancels the upload, not when the upload is completed or errored.
  401. */
  402. resetUploaderReferences (fileID, opts = {}) {
  403. if (this.uploaders[fileID]) {
  404. this.uploaders[fileID].abort({ really: opts.abort || false })
  405. this.uploaders[fileID] = null
  406. }
  407. if (this.uploaderEvents[fileID]) {
  408. this.uploaderEvents[fileID].remove()
  409. this.uploaderEvents[fileID] = null
  410. }
  411. if (this.uploaderSockets[fileID]) {
  412. this.uploaderSockets[fileID].close()
  413. this.uploaderSockets[fileID] = null
  414. }
  415. }
  416. // TODO: make this a private method in the next major
  417. assertHost (method) {
  418. if (!this.opts.companionUrl) {
  419. throw new Error(`Expected a \`companionUrl\` option containing a Companion address, or if you are not using Companion, a custom \`${method}\` implementation.`)
  420. }
  421. }
  422. createMultipartUpload (file, signal) {
  423. this.assertHost('createMultipartUpload')
  424. throwIfAborted(signal)
  425. const metadata = getAllowedMetadata({ meta: file.meta, allowedMetaFields: this.opts.allowedMetaFields })
  426. return this.#client.post('s3/multipart', {
  427. filename: file.name,
  428. type: file.type,
  429. metadata,
  430. }, { signal }).then(assertServerError)
  431. }
  432. listParts (file, { key, uploadId }, signal) {
  433. this.assertHost('listParts')
  434. throwIfAborted(signal)
  435. const filename = encodeURIComponent(key)
  436. return this.#client.get(`s3/multipart/${uploadId}?key=${filename}`, { signal })
  437. .then(assertServerError)
  438. }
  439. completeMultipartUpload (file, { key, uploadId, parts }, signal) {
  440. this.assertHost('completeMultipartUpload')
  441. throwIfAborted(signal)
  442. const filename = encodeURIComponent(key)
  443. const uploadIdEnc = encodeURIComponent(uploadId)
  444. return this.#client.post(`s3/multipart/${uploadIdEnc}/complete?key=${filename}`, { parts }, { signal })
  445. .then(assertServerError)
  446. }
  447. /**
  448. * @type {import("../types").AwsS3STSResponse | Promise<import("../types").AwsS3STSResponse>}
  449. */
  450. #cachedTemporaryCredentials
  451. async #getTemporarySecurityCredentials (options) {
  452. throwIfAborted(options?.signal)
  453. if (this.#cachedTemporaryCredentials == null) {
  454. // We do not await it just yet, so concurrent calls do not try to override it:
  455. if (this.opts.getTemporarySecurityCredentials === true) {
  456. this.assertHost('getTemporarySecurityCredentials')
  457. this.#cachedTemporaryCredentials = this.#client.get('s3/sts', null, options).then(assertServerError)
  458. } else {
  459. this.#cachedTemporaryCredentials = this.opts.getTemporarySecurityCredentials(options)
  460. }
  461. this.#cachedTemporaryCredentials = await this.#cachedTemporaryCredentials
  462. setTimeout(() => {
  463. // At half the time left before expiration, we clear the cache. That's
  464. // an arbitrary tradeoff to limit the number of requests made to the
  465. // remote while limiting the risk of using an expired token in case the
  466. // clocks are not exactly synced.
  467. // The HTTP cache should be configured to ensure a client doesn't request
  468. // more tokens than it needs, but this timeout provides a second layer of
  469. // security in case the HTTP cache is disabled or misconfigured.
  470. this.#cachedTemporaryCredentials = null
  471. }, (getExpiry(this.#cachedTemporaryCredentials.credentials) || 0) * 500)
  472. }
  473. return this.#cachedTemporaryCredentials
  474. }
  475. async createSignedURL (file, options) {
  476. const data = await this.#getTemporarySecurityCredentials(options)
  477. const expires = getExpiry(data.credentials) || 604_800 // 604 800 is the max value accepted by AWS.
  478. const { uploadId, key, partNumber, signal } = options
  479. // Return an object in the correct shape.
  480. return {
  481. method: 'PUT',
  482. expires,
  483. fields: {},
  484. url: `${await createSignedURL({
  485. accountKey: data.credentials.AccessKeyId,
  486. accountSecret: data.credentials.SecretAccessKey,
  487. sessionToken: data.credentials.SessionToken,
  488. expires,
  489. bucketName: data.bucket,
  490. Region: data.region,
  491. Key: key ?? `${crypto.randomUUID()}-${file.name}`,
  492. uploadId,
  493. partNumber,
  494. signal,
  495. })}`,
  496. // Provide content type header required by S3
  497. headers: {
  498. 'Content-Type': file.type,
  499. },
  500. }
  501. }
  502. signPart (file, { uploadId, key, partNumber, signal }) {
  503. this.assertHost('signPart')
  504. throwIfAborted(signal)
  505. if (uploadId == null || key == null || partNumber == null) {
  506. throw new Error('Cannot sign without a key, an uploadId, and a partNumber')
  507. }
  508. const filename = encodeURIComponent(key)
  509. return this.#client.get(`s3/multipart/${uploadId}/${partNumber}?key=${filename}`, { signal })
  510. .then(assertServerError)
  511. }
  512. abortMultipartUpload (file, { key, uploadId }, signal) {
  513. this.assertHost('abortMultipartUpload')
  514. const filename = encodeURIComponent(key)
  515. const uploadIdEnc = encodeURIComponent(uploadId)
  516. return this.#client.delete(`s3/multipart/${uploadIdEnc}?key=${filename}`, undefined, { signal })
  517. .then(assertServerError)
  518. }
  519. getUploadParameters (file, options) {
  520. const { meta } = file
  521. const { type, name: filename } = meta
  522. const metadata = getAllowedMetadata({ meta, allowedMetaFields: this.opts.allowedMetaFields, querify: true })
  523. const query = new URLSearchParams({ filename, type, ...metadata })
  524. return this.#client.get(`s3/params?${query}`, options)
  525. }
  526. static async uploadPartBytes ({ signature: { url, expires, headers, method = 'PUT' }, body, size = body.size, onProgress, onComplete, signal }) {
  527. throwIfAborted(signal)
  528. if (url == null) {
  529. throw new Error('Cannot upload to an undefined URL')
  530. }
  531. return new Promise((resolve, reject) => {
  532. const xhr = new XMLHttpRequest()
  533. xhr.open(method, url, true)
  534. if (headers) {
  535. Object.keys(headers).forEach((key) => {
  536. xhr.setRequestHeader(key, headers[key])
  537. })
  538. }
  539. xhr.responseType = 'text'
  540. if (typeof expires === 'number') {
  541. xhr.timeout = expires * 1000
  542. }
  543. function onabort () {
  544. xhr.abort()
  545. }
  546. function cleanup () {
  547. signal.removeEventListener('abort', onabort)
  548. }
  549. signal.addEventListener('abort', onabort)
  550. xhr.upload.addEventListener('progress', (ev) => {
  551. onProgress(ev)
  552. })
  553. xhr.addEventListener('abort', () => {
  554. cleanup()
  555. reject(createAbortError())
  556. })
  557. xhr.addEventListener('timeout', () => {
  558. cleanup()
  559. const error = new Error('Request has expired')
  560. error.source = { status: 403 }
  561. reject(error)
  562. })
  563. xhr.addEventListener('load', (ev) => {
  564. cleanup()
  565. if (ev.target.status === 403 && ev.target.responseText.includes('<Message>Request has expired</Message>')) {
  566. const error = new Error('Request has expired')
  567. error.source = ev.target
  568. reject(error)
  569. return
  570. } if (ev.target.status < 200 || ev.target.status >= 300) {
  571. const error = new Error('Non 2xx')
  572. error.source = ev.target
  573. reject(error)
  574. return
  575. }
  576. // todo make a proper onProgress API (breaking change)
  577. onProgress?.({ loaded: size, lengthComputable: true })
  578. // NOTE This must be allowed by CORS.
  579. const etag = ev.target.getResponseHeader('ETag')
  580. const location = ev.target.getResponseHeader('Location')
  581. if (method.toUpperCase() === 'POST' && location === null) {
  582. // Not being able to read the Location header is not a fatal error.
  583. // eslint-disable-next-line no-console
  584. console.warn('AwsS3/Multipart: Could not read the Location header. This likely means CORS is not configured correctly on the S3 Bucket. See https://uppy.io/docs/aws-s3-multipart#S3-Bucket-Configuration for instructions.')
  585. }
  586. if (etag === null) {
  587. reject(new Error('AwsS3/Multipart: Could not read the ETag header. This likely means CORS is not configured correctly on the S3 Bucket. See https://uppy.io/docs/aws-s3-multipart#S3-Bucket-Configuration for instructions.'))
  588. return
  589. }
  590. onComplete?.(etag)
  591. resolve({
  592. ETag: etag,
  593. ...(location ? { location } : undefined),
  594. })
  595. })
  596. xhr.addEventListener('error', (ev) => {
  597. cleanup()
  598. const error = new Error('Unknown error')
  599. error.source = ev.target
  600. reject(error)
  601. })
  602. xhr.send(body)
  603. })
  604. }
  605. #setS3MultipartState = (file, { key, uploadId }) => {
  606. const cFile = this.uppy.getFile(file.id)
  607. if (cFile == null) {
  608. // file was removed from store
  609. return
  610. }
  611. this.uppy.setFileState(file.id, {
  612. s3Multipart: {
  613. ...cFile.s3Multipart,
  614. key,
  615. uploadId,
  616. },
  617. })
  618. }
  619. #getFile = (file) => {
  620. return this.uppy.getFile(file.id) || file
  621. }
  622. #uploadLocalFile (file) {
  623. return new Promise((resolve, reject) => {
  624. const onProgress = (bytesUploaded, bytesTotal) => {
  625. this.uppy.emit('upload-progress', file, {
  626. uploader: this,
  627. bytesUploaded,
  628. bytesTotal,
  629. })
  630. }
  631. const onError = (err) => {
  632. this.uppy.log(err)
  633. this.uppy.emit('upload-error', file, err)
  634. this.resetUploaderReferences(file.id)
  635. reject(err)
  636. }
  637. const onSuccess = (result) => {
  638. const uploadResp = {
  639. body: {
  640. ...result,
  641. },
  642. uploadURL: result.location,
  643. }
  644. this.resetUploaderReferences(file.id)
  645. this.uppy.emit('upload-success', this.#getFile(file), uploadResp)
  646. if (result.location) {
  647. this.uppy.log(`Download ${file.name} from ${result.location}`)
  648. }
  649. resolve()
  650. }
  651. const onPartComplete = (part) => {
  652. this.uppy.emit('s3-multipart:part-uploaded', this.#getFile(file), part)
  653. }
  654. const upload = new MultipartUploader(file.data, {
  655. // .bind to pass the file object to each handler.
  656. companionComm: this.#companionCommunicationQueue,
  657. log: (...args) => this.uppy.log(...args),
  658. getChunkSize: this.opts.getChunkSize ? this.opts.getChunkSize.bind(this) : null,
  659. onProgress,
  660. onError,
  661. onSuccess,
  662. onPartComplete,
  663. file,
  664. shouldUseMultipart: this.opts.shouldUseMultipart,
  665. ...file.s3Multipart,
  666. })
  667. this.uploaders[file.id] = upload
  668. const eventManager = new EventManager(this.uppy)
  669. this.uploaderEvents[file.id] = eventManager
  670. eventManager.onFileRemove(file.id, (removed) => {
  671. upload.abort()
  672. this.resetUploaderReferences(file.id, { abort: true })
  673. resolve(`upload ${removed.id} was removed`)
  674. })
  675. eventManager.onCancelAll(file.id, ({ reason } = {}) => {
  676. if (reason === 'user') {
  677. upload.abort()
  678. this.resetUploaderReferences(file.id, { abort: true })
  679. }
  680. resolve(`upload ${file.id} was canceled`)
  681. })
  682. eventManager.onFilePause(file.id, (isPaused) => {
  683. if (isPaused) {
  684. upload.pause()
  685. } else {
  686. upload.start()
  687. }
  688. })
  689. eventManager.onPauseAll(file.id, () => {
  690. upload.pause()
  691. })
  692. eventManager.onResumeAll(file.id, () => {
  693. upload.start()
  694. })
  695. upload.start()
  696. })
  697. }
  698. // eslint-disable-next-line class-methods-use-this
  699. #getCompanionClientArgs (file) {
  700. return {
  701. ...file.remote.body,
  702. protocol: 's3-multipart',
  703. size: file.data.size,
  704. metadata: file.meta,
  705. }
  706. }
  707. #upload = async (fileIDs) => {
  708. if (fileIDs.length === 0) return undefined
  709. const files = this.uppy.getFilesByIds(fileIDs)
  710. const filesFiltered = filterNonFailedFiles(files)
  711. const filesToEmit = filterFilesToEmitUploadStarted(filesFiltered)
  712. this.uppy.emit('upload-start', filesToEmit)
  713. const promises = filesFiltered.map((file) => {
  714. if (file.isRemote) {
  715. // INFO: the url plugin needs to use RequestClient,
  716. // while others use Provider
  717. const Client = file.remote.providerOptions.provider ? Provider : RequestClient
  718. const getQueue = () => this.requests
  719. const client = new Client(this.uppy, file.remote.providerOptions, getQueue)
  720. this.#setResumableUploadsCapability(false)
  721. const controller = new AbortController()
  722. const removedHandler = (removedFile) => {
  723. if (removedFile.id === file.id) controller.abort()
  724. }
  725. this.uppy.on('file-removed', removedHandler)
  726. const uploadPromise = client.uploadRemoteFile(
  727. file,
  728. this.#getCompanionClientArgs(file),
  729. { signal: controller.signal },
  730. )
  731. this.requests.wrapSyncFunction(() => {
  732. this.uppy.off('file-removed', removedHandler)
  733. }, { priority: -1 })()
  734. return uploadPromise
  735. }
  736. return this.#uploadLocalFile(file)
  737. })
  738. const upload = await Promise.all(promises)
  739. // After the upload is done, another upload may happen with only local files.
  740. // We reset the capability so that the next upload can use resumable uploads.
  741. this.#setResumableUploadsCapability(true)
  742. return upload
  743. }
  744. #setCompanionHeaders = () => {
  745. this.#client.setCompanionHeaders(this.opts.companionHeaders)
  746. }
  747. #setResumableUploadsCapability = (boolean) => {
  748. const { capabilities } = this.uppy.getState()
  749. this.uppy.setState({
  750. capabilities: {
  751. ...capabilities,
  752. resumableUploads: boolean,
  753. },
  754. })
  755. }
  756. #resetResumableCapability = () => {
  757. this.#setResumableUploadsCapability(true)
  758. }
  759. install () {
  760. this.#setResumableUploadsCapability(true)
  761. this.uppy.addPreProcessor(this.#setCompanionHeaders)
  762. this.uppy.addUploader(this.#upload)
  763. this.uppy.on('cancel-all', this.#resetResumableCapability)
  764. }
  765. uninstall () {
  766. this.uppy.removePreProcessor(this.#setCompanionHeaders)
  767. this.uppy.removeUploader(this.#upload)
  768. this.uppy.off('cancel-all', this.#resetResumableCapability)
  769. }
  770. }