Skip to content

Commit

Permalink
Merge branch 'main' into cypress
Browse files Browse the repository at this point in the history
* main:
  @uppy/transloadit: fix handling of Tus errors and rate limiting (transloadit#3429)
  Add Unsplash to website dashboard example (transloadit#3431)
  • Loading branch information
Murderlon committed Jan 13, 2022
2 parents d969725 + 38a5d3b commit 2bed82b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
38 changes: 26 additions & 12 deletions packages/@uppy/transloadit/src/Client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
const fetchWithNetworkError = require('@uppy/utils/lib/fetchWithNetworkError')
const NetworkError = require('@uppy/utils/lib/NetworkError')

function fetchJSON (...args) {
return fetchWithNetworkError(...args).then(response => {
if (response.status === 429) {
// If the server asks the client to rate limit, reschedule the request 2s later.
// TODO: there are several instances of rate limiting accross the code base, having one global one could be useful.
return new Promise((resolve, reject) => {
setTimeout(() => fetchJSON(...args).then(resolve, reject), 2_000)
})
}

if (!response.ok) {
return Promise.reject(new NetworkError(response.statusText))
}

return response.json()
})
}

/**
* A Barebones HTTP API client for Transloadit.
Expand Down Expand Up @@ -43,12 +62,12 @@ module.exports = class Client {
data.append('num_expected_upload_files', expectedFiles)

const url = new URL('/assemblies', `${this.opts.service}`).href
return fetchWithNetworkError(url, {
return fetchJSON(url, {
method: 'post',
headers: this.#headers,
body: data,
})
.then((response) => response.json()).then((assembly) => {
.then((assembly) => {
if (assembly.error) {
const error = new Error(assembly.error)
error.details = assembly.message
Expand All @@ -73,8 +92,7 @@ module.exports = class Client {
reserveFile (assembly, file) {
const size = encodeURIComponent(file.size)
const url = `${assembly.assembly_ssl_url}/reserve_file?size=${size}`
return fetchWithNetworkError(url, { method: 'post', headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { method: 'post', headers: this.#headers })
.catch((err) => this.#reportError(err, { assembly, file, url, type: 'API_ERROR' }))
}

Expand All @@ -95,8 +113,7 @@ module.exports = class Client {

const qs = `size=${size}&filename=${filename}&fieldname=${fieldname}&s3Url=${uploadUrl}`
const url = `${assembly.assembly_ssl_url}/add_file?${qs}`
return fetchWithNetworkError(url, { method: 'post', headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { method: 'post', headers: this.#headers })
.catch((err) => this.#reportError(err, { assembly, file, url, type: 'API_ERROR' }))
}

Expand All @@ -107,8 +124,7 @@ module.exports = class Client {
*/
cancelAssembly (assembly) {
const url = assembly.assembly_ssl_url
return fetchWithNetworkError(url, { method: 'delete', headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { method: 'delete', headers: this.#headers })
.catch((err) => this.#reportError(err, { url, type: 'API_ERROR' }))
}

Expand All @@ -118,8 +134,7 @@ module.exports = class Client {
* @param {string} url The status endpoint of the assembly.
*/
getAssemblyStatus (url) {
return fetchWithNetworkError(url, { headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { headers: this.#headers })
.catch((err) => this.#reportError(err, { url, type: 'STATUS_ERROR' }))
}

Expand All @@ -128,7 +143,7 @@ module.exports = class Client {
? `${err.message} (${err.details})`
: err.message

return fetchWithNetworkError('https://transloaditstatus.com/client_error', {
return fetchJSON('https://transloaditstatus.com/client_error', {
method: 'post',
body: JSON.stringify({
endpoint,
Expand All @@ -139,7 +154,6 @@ module.exports = class Client {
error: message,
}),
})
.then((response) => response.json())
}

#reportError = (err, params) => {
Expand Down
13 changes: 7 additions & 6 deletions packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,21 +694,22 @@ module.exports = class Transloadit extends BasePlugin {
})
}

#closeAssemblyIfExists = (assemblyID) => {
this.activeAssemblies[assemblyID]?.close()
}

#onError = (err = null, uploadID) => {
const state = this.getPluginState()
const assemblyIDs = state.uploadsAssemblies[uploadID]
assemblyIDs?.forEach(this.#closeAssemblyIfExists)

assemblyIDs?.forEach((assemblyID) => {
if (this.activeAssemblies[assemblyID]) {
this.activeAssemblies[assemblyID].close()
}
})
this.client.submitError(err)
// if we can't report the error that sucks
.catch(sendErrorToConsole(err))
}

#onTusError = (err) => {
#onTusError = (file, err) => {
this.#closeAssemblyIfExists(file.transloadit?.assembly)
if (err?.message?.startsWith('tus: ')) {
const endpoint = err.originalRequest?.getUnderlyingObject()?.responseURL
this.client.submitError(err, { endpoint, type: 'TUS_ERROR' })
Expand Down
8 changes: 8 additions & 0 deletions website/src/examples/dashboard/app.es6
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Instagram = require('@uppy/instagram')
const Facebook = require('@uppy/facebook')
const OneDrive = require('@uppy/onedrive')
const Zoom = require('@uppy/zoom')
const Unsplash = require('@uppy/unsplash')
// const Box = require('@uppy/box')
const ImageEditor = require('@uppy/image-editor')
const Url = require('@uppy/url')
Expand Down Expand Up @@ -139,6 +140,13 @@ function uppySetOptions () {
if (!opts.OneDrive && oneDriveInstance) {
window.uppy.removePlugin(oneDriveInstance)
}
const unsplashInstance = window.uppy.getPlugin('Unsplash')
if (opts.Unsplash && !unsplashInstance) {
window.uppy.use(Unsplash, { target: Dashboard, companionUrl: COMPANION })
}
if (!opts.Unsplash && unsplashInstance) {
window.uppy.removePlugin(unsplashInstance)
}

const zoomInstance = window.uppy.getPlugin('Zoom')
if (opts.Zoom && !zoomInstance) {
Expand Down
3 changes: 3 additions & 0 deletions website/src/examples/dashboard/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<li><label for="opts-ScreenCapture"><input type="checkbox" id="opts-ScreenCapture" checked/> Screen Capture</label></li>
<li><label for="opts-GoogleDrive"><input type="checkbox" id="opts-GoogleDrive" checked/> Google Drive</label></li>
<li><label for="opts-Dropbox"><input type="checkbox" id="opts-Dropbox" checked/> Dropbox</label></li>
<li><label for="opts-Unsplash"><input type="checkbox" id="opts-Unsplash" checked/> Unsplash</label></li>
<!-- <li><label for="opts-Box"><input type="checkbox" id="opts-Box" checked/> Box</label></li> -->
<li><label for="opts-Instagram"><input type="checkbox" id="opts-Instagram" checked/> Instagram</label></li>
<li><label for="opts-Facebook"><input type="checkbox" id="opts-Facebook" checked/> Facebook</label></li>
Expand Down Expand Up @@ -49,6 +50,7 @@
ScreenCapture: document.querySelector('#opts-ScreenCapture'),
GoogleDrive: document.querySelector('#opts-GoogleDrive'),
Dropbox: document.querySelector('#opts-Dropbox'),
Unsplash: document.querySelector('#opts-Unsplash'),
// Box: document.querySelector('#opts-Box'),
Instagram: document.querySelector('#opts-Instagram'),
Facebook: document.querySelector('#opts-Facebook'),
Expand All @@ -71,6 +73,7 @@
GoogleDrive: true,
Instagram: true,
Dropbox: true,
Unsplash: true,
// Box: true,
OneDrive: true,
Facebook: false,
Expand Down
2 changes: 2 additions & 0 deletions website/src/examples/dashboard/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Uppy = require('@uppy/core')
const Dashboard = require('@uppy/dashboard')
const GoogleDrive = require('@uppy/google-drive')
const Dropbox = require('@uppy/dropbox')
const Unsplash = require('@uppy/unsplash')
const Box = require('@uppy/box')
const Instagram = require('@uppy/instagram')
const Facebook = require('@uppy/facebook')
Expand Down Expand Up @@ -68,6 +69,7 @@ const uppy = new Uppy({
.use(Instagram, { target: Dashboard, companionUrl: 'https://companion.uppy.io' })
.use(Facebook, { target: Dashboard, companionUrl: 'https://companion.uppy.io' })
.use(OneDrive, { target: Dashboard, companionUrl: 'https://companion.uppy.io' })
.use(Unsplash, { target: Dashboard, companionUrl: 'https://companion.uppy.io' })
.use(Webcam, { target: Dashboard })
.use(Audio, { target: Dashboard })
.use(ScreenCapture, { target: Dashboard })
Expand Down

0 comments on commit 2bed82b

Please sign in to comment.