From 7ab9a3747995f940deac4283538ca4f6ff5b9663 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 4 May 2022 20:48:04 +0200 Subject: [PATCH] @uppy/xhr-upload: refactor to ESM --- .eslintrc.js | 1 + packages/@uppy/xhr-upload/package.json | 2 + packages/@uppy/xhr-upload/src/index.js | 60 ++++++++++----------- packages/@uppy/xhr-upload/src/index.test.js | 7 +-- packages/@uppy/xhr-upload/src/locale.js | 2 +- website/src/docs/xhr-upload.md | 2 +- yarn.lock | 1 + 7 files changed, 38 insertions(+), 37 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 457749e034..73b7900a58 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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: { diff --git a/packages/@uppy/xhr-upload/package.json b/packages/@uppy/xhr-upload/package.json index 61e39a1a73..858ee334be 100644 --- a/packages/@uppy/xhr-upload/package.json +++ b/packages/@uppy/xhr-upload/package.json @@ -5,6 +5,7 @@ "license": "MIT", "main": "lib/index.js", "types": "types/index.d.ts", + "type": "module", "keywords": [ "file uploader", "xhr", @@ -29,6 +30,7 @@ "nanoid": "^3.1.25" }, "devDependencies": { + "@jest/globals": "^27.4.2", "nock": "^13.1.0" }, "peerDependencies": { diff --git a/packages/@uppy/xhr-upload/src/index.js b/packages/@uppy/xhr-upload/src/index.js index 2590509e29..f2d245dbe8 100644 --- a/packages/@uppy/xhr-upload/src/index.js +++ b/packages/@uppy/xhr-upload/src/index.js @@ -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 @@ -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) @@ -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.} headers - * * @param {string} responseText the response body string - * @param {XMLHttpRequest | respObj} response the response object (XHR or similar) */ getResponseData (responseText) { let parsedResponse = {} @@ -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) { @@ -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() @@ -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 @@ -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', {}) @@ -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', {}) @@ -467,6 +463,12 @@ 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) })) @@ -474,12 +476,6 @@ module.exports = class XHRUpload extends BasePlugin { 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() @@ -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, }) }) diff --git a/packages/@uppy/xhr-upload/src/index.test.js b/packages/@uppy/xhr-upload/src/index.test.js index 6c64607928..2e43bd5532 100644 --- a/packages/@uppy/xhr-upload/src/index.test.js +++ b/packages/@uppy/xhr-upload/src/index.test.js @@ -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', () => { diff --git a/packages/@uppy/xhr-upload/src/locale.js b/packages/@uppy/xhr-upload/src/locale.js index 24f6aa38fc..ff9aa94070 100644 --- a/packages/@uppy/xhr-upload/src/locale.js +++ b/packages/@uppy/xhr-upload/src/locale.js @@ -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.', diff --git a/website/src/docs/xhr-upload.md b/website/src/docs/xhr-upload.md index 2b7fb6ab93..04ff16fb02 100644 --- a/website/src/docs/xhr-upload.md +++ b/website/src/docs/xhr-upload.md @@ -234,7 +234,7 @@ Indicates whether cross-site Access-Control requests should be made using creden ```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.', diff --git a/yarn.lock b/yarn.lock index 422aab9f78..61dfd7c532 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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