Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/xhr-upload: refactor to ESM #3695

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ module.exports = {
'packages/@uppy/svelte/rollup.config.js',
'packages/@uppy/vue/src/**/*.js',
'packages/@uppy/webcam/src/**/*.js',
'packages/@uppy/xhr-upload/src/**/*.js',
],
parser: 'espree',
parserOptions: {
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/xhr-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"main": "lib/index.js",
"types": "types/index.d.ts",
"type": "module",
"keywords": [
"file uploader",
"xhr",
Expand All @@ -29,6 +30,7 @@
"nanoid": "^3.1.25"
},
"devDependencies": {
"@jest/globals": "^27.4.2",
"nock": "^13.1.0"
},
"peerDependencies": {
Expand Down
60 changes: 28 additions & 32 deletions packages/@uppy/xhr-upload/src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const BasePlugin = require('@uppy/core/lib/BasePlugin')
const { nanoid } = require('nanoid/non-secure')
const { Provider, RequestClient, Socket } = require('@uppy/companion-client')
const emitSocketProgress = require('@uppy/utils/lib/emitSocketProgress')
const getSocketHost = require('@uppy/utils/lib/getSocketHost')
const settle = require('@uppy/utils/lib/settle')
const EventTracker = require('@uppy/utils/lib/EventTracker')
const ProgressTimeout = require('@uppy/utils/lib/ProgressTimeout')
const { RateLimitedQueue, internalRateLimitedQueue } = require('@uppy/utils/lib/RateLimitedQueue')
const NetworkError = require('@uppy/utils/lib/NetworkError')
const isNetworkError = require('@uppy/utils/lib/isNetworkError')

const locale = require('./locale')
import BasePlugin from '@uppy/core/lib/BasePlugin'
import { nanoid } from 'nanoid/non-secure'
import { Provider, RequestClient, Socket } from '@uppy/companion-client'
import emitSocketProgress from '@uppy/utils/lib/emitSocketProgress'
import getSocketHost from '@uppy/utils/lib/getSocketHost'
import settle from '@uppy/utils/lib/settle'
import EventTracker from '@uppy/utils/lib/EventTracker'
import ProgressTimeout from '@uppy/utils/lib/ProgressTimeout'
import { RateLimitedQueue, internalRateLimitedQueue } from '@uppy/utils/lib/RateLimitedQueue'
import NetworkError from '@uppy/utils/lib/NetworkError'
import isNetworkError from '@uppy/utils/lib/isNetworkError'

import packageJson from '../package.json'
import locale from './locale.js'

function buildResponseError (xhr, err) {
let error = err
Expand Down Expand Up @@ -45,9 +46,9 @@ function setTypeInBlob (file) {
return dataWithUpdatedType
}

module.exports = class XHRUpload extends BasePlugin {
export default class XHRUpload extends BasePlugin {
// eslint-disable-next-line global-require
static VERSION = require('../package.json').version
static VERSION = packageJson.version

constructor (uppy, opts) {
super(uppy, opts)
Expand All @@ -71,14 +72,7 @@ module.exports = class XHRUpload extends BasePlugin {
withCredentials: false,
responseType: '',
/**
* @typedef respObj
* @property {string} responseText
* @property {number} status
* @property {string} statusText
* @property {object.<string, string>} headers
*
* @param {string} responseText the response body string
* @param {XMLHttpRequest | respObj} response the response object (XHR or similar)
*/
getResponseData (responseText) {
let parsedResponse = {}
Expand All @@ -92,7 +86,7 @@ module.exports = class XHRUpload extends BasePlugin {
},
/**
*
* @param {string} responseText the response body string
* @param {string} _ the response body string
* @param {XMLHttpRequest | respObj} response the response object (XHR or similar)
*/
getResponseError (_, response) {
Expand Down Expand Up @@ -226,6 +220,7 @@ module.exports = class XHRUpload extends BasePlugin {

const xhr = new XMLHttpRequest()
this.uploaderEvents[file.id] = new EventTracker(this.uppy)
let queuedRequest

const timer = new ProgressTimeout(opts.timeout, () => {
xhr.abort()
Expand Down Expand Up @@ -317,7 +312,7 @@ module.exports = class XHRUpload extends BasePlugin {
xhr.responseType = opts.responseType
}

const queuedRequest = this.requests.run(() => {
queuedRequest = this.requests.run(() => {
this.uppy.emit('upload-started', file)

// When using an authentication system like JWT, the bearer token goes as a header. This
Expand Down Expand Up @@ -381,6 +376,7 @@ module.exports = class XHRUpload extends BasePlugin {
const host = getSocketHost(file.remote.companionUrl)
const socket = new Socket({ target: `${host}/api/${token}`, autoOpen: false })
this.uploaderEvents[file.id] = new EventTracker(this.uppy)
let queuedRequest

this.onFileRemove(file.id, () => {
socket.send('cancel', {})
Expand Down Expand Up @@ -439,7 +435,7 @@ module.exports = class XHRUpload extends BasePlugin {
reject(error)
})

const queuedRequest = this.requests.run(() => {
queuedRequest = this.requests.run(() => {
socket.open()
if (file.isPaused) {
socket.send('pause', {})
Expand Down Expand Up @@ -467,19 +463,19 @@ module.exports = class XHRUpload extends BasePlugin {

const xhr = new XMLHttpRequest()

const emitError = (error) => {
files.forEach((file) => {
this.uppy.emit('upload-error', file, error)
})
}

const timer = new ProgressTimeout(this.opts.timeout, () => {
xhr.abort()
const error = new Error(this.i18n('timedOut', { seconds: Math.ceil(this.opts.timeout / 1000) }))
emitError(error)
reject(error)
})

const emitError = (error) => {
files.forEach((file) => {
this.uppy.emit('upload-error', file, error)
})
}

xhr.upload.addEventListener('loadstart', () => {
this.uppy.log('[XHRUpload] started uploading bundle')
timer.progress()
Expand All @@ -493,7 +489,7 @@ module.exports = class XHRUpload extends BasePlugin {
files.forEach((file) => {
this.uppy.emit('upload-progress', file, {
uploader: this,
bytesUploaded: ev.loaded / ev.total * file.size,
bytesUploaded: (ev.loaded / ev.total) * file.size,
bytesTotal: file.size,
})
})
Expand Down
7 changes: 4 additions & 3 deletions packages/@uppy/xhr-upload/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const nock = require('nock')
const Core = require('@uppy/core')
const XHRUpload = require('./index')
import { jest, describe, it, expect } from '@jest/globals'
import nock from 'nock'
import Core from '@uppy/core'
import XHRUpload from './index.js'

describe('XHRUpload', () => {
describe('getResponseData', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/xhr-upload/src/locale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
strings: {
// Shown in the Informer if an upload is being canceled because it stalled for too long.
timedOut: 'Upload stalled for %{seconds} seconds, aborting.',
Expand Down
2 changes: 1 addition & 1 deletion website/src/docs/xhr-upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Indicates whether cross-site Access-Control requests should be made using creden
<!-- eslint-disable no-restricted-globals, no-multiple-empty-lines -->

```js
module.exports = {
export default {
strings: {
// Shown in the Informer if an upload is being canceled because it stalled for too long.
timedOut: 'Upload stalled for %{seconds} seconds, aborting.',
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10250,6 +10250,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@uppy/xhr-upload@workspace:packages/@uppy/xhr-upload"
dependencies:
"@jest/globals": ^27.4.2
"@uppy/companion-client": "workspace:^"
"@uppy/utils": "workspace:^"
nanoid: ^3.1.25
Expand Down