Преглед на файлове

tus,companion: terminate tus upload on-upload cancel

fixes #1544
ifedapoolarewaju преди 5 години
родител
ревизия
c7828139ed

+ 20 - 21
package-lock.json

@@ -6141,7 +6141,7 @@
         "request": "2.88.0",
         "semver": "6.1.1",
         "serialize-error": "^2.1.0",
-        "tus-js-client": "^1.8.0-0",
+        "tus-js-client": "1.8.0-2",
         "uuid": "2.0.2",
         "validator": "^9.0.0",
         "ws": "3.3.1"
@@ -6169,6 +6169,11 @@
             "type-is": "~1.6.15"
           }
         },
+        "buffer-from": {
+          "version": "0.1.2",
+          "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-0.1.2.tgz",
+          "integrity": "sha512-RiWIenusJsmI2KcvqQABB83tLxCByE3upSP8QU3rJDMVFGPWLvPQJt/O1Su9moRWeH7d+Q2HYb68f6+v+tw2vg=="
+        },
         "bytes": {
           "version": "3.0.0",
           "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
@@ -6337,6 +6342,20 @@
             "punycode": "^1.4.1"
           }
         },
+        "tus-js-client": {
+          "version": "1.8.0-2",
+          "resolved": "https://registry.npmjs.org/tus-js-client/-/tus-js-client-1.8.0-2.tgz",
+          "integrity": "sha512-8v/q4s9biAV1A1hZf2mtnvRlh7AXpXbozrX5bZgjRmhgY9TLlmfFVieamrOjN1DI9RPze9mSh3e6BQRRDIDsiA==",
+          "requires": {
+            "buffer-from": "^0.1.1",
+            "combine-errors": "^3.0.3",
+            "extend": "^3.0.2",
+            "js-base64": "^2.4.9",
+            "lodash.throttle": "^4.1.1",
+            "proper-lockfile": "^2.0.1",
+            "url-parse": "^1.4.3"
+          }
+        },
         "ultron": {
           "version": "1.1.1",
           "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz",
@@ -31493,26 +31512,6 @@
         "safe-buffer": "^5.0.1"
       }
     },
-    "tus-js-client": {
-      "version": "1.8.0-1",
-      "resolved": "https://registry.npmjs.org/tus-js-client/-/tus-js-client-1.8.0-1.tgz",
-      "integrity": "sha512-wYlIjdgxhomVuXFllAQa8kPsRsmArTz8YCvWSO84F+UGc1dkfMUsUNzyO+bznl1BV6sYJOx2Xq5XfDqzUS4xRA==",
-      "requires": {
-        "buffer-from": "^0.1.1",
-        "extend": "^3.0.0",
-        "js-base64": "^2.4.9",
-        "lodash.throttle": "^4.1.1",
-        "proper-lockfile": "^2.0.1",
-        "url-parse": "^1.4.3"
-      },
-      "dependencies": {
-        "buffer-from": {
-          "version": "0.1.2",
-          "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-0.1.2.tgz",
-          "integrity": "sha512-RiWIenusJsmI2KcvqQABB83tLxCByE3upSP8QU3rJDMVFGPWLvPQJt/O1Su9moRWeH7d+Q2HYb68f6+v+tw2vg=="
-        }
-      }
-    },
     "tus-node-server": {
       "version": "0.3.2",
       "resolved": "https://registry.npmjs.org/tus-node-server/-/tus-node-server-0.3.2.tgz",

+ 1 - 1
packages/@uppy/companion/package.json

@@ -59,7 +59,7 @@
     "request": "2.88.0",
     "semver": "6.1.1",
     "serialize-error": "^2.1.0",
-    "tus-js-client": "^1.8.0-0",
+    "tus-js-client": "1.8.0-2",
     "uuid": "2.0.2",
     "validator": "^9.0.0",
     "ws": "3.3.1"

+ 1 - 1
packages/@uppy/companion/src/companion.js

@@ -165,7 +165,7 @@ module.exports.socket = (server) => {
     ws.on('message', (jsonData) => {
       const data = JSON.parse(jsonData.toString())
       // whitelist triggered actions
-      if (data.action === 'pause' || data.action === 'resume') {
+      if (['pause', 'resume', 'cancel'].includes(data.action)) {
         emitter().emit(`${data.action}:${token}`)
       }
     })

+ 16 - 0
packages/@uppy/companion/src/server/Uploader.js

@@ -51,6 +51,7 @@ class Uploader {
     this.options.metadata = this.options.metadata || {}
     this.uploadFileName = this.options.metadata.name || path.basename(this.path)
     this.streamsEnded = false
+    this.uploadStopped = false
     this.duplexStream = null
     // @TODO disabling parallel uploads and downloads for now
     // if (this.options.protocol === PROTOCOLS.tus) {
@@ -78,6 +79,15 @@ class Uploader {
           this.tus.start()
         }
       })
+
+      emitter().on(`cancel:${this.token}`, () => {
+        this._paused = true
+        if (this.tus) {
+          const shouldTerminate = !!this.tus.url
+          this.tus.abort(shouldTerminate)
+        }
+        this.cleanUp()
+      })
     }
   }
 
@@ -184,6 +194,8 @@ class Uploader {
     })
     emitter().removeAllListeners(`pause:${this.token}`)
     emitter().removeAllListeners(`resume:${this.token}`)
+    emitter().removeAllListeners(`cancel:${this.token}`)
+    this.uploadStopped = true
   }
 
   /**
@@ -191,6 +203,10 @@ class Uploader {
    * @param {Buffer | Buffer[]} chunk
    */
   handleChunk (chunk) {
+    if (this.uploadStopped) {
+      return
+    }
+
     // @todo a default protocol should not be set. We should ensure that the user specifies her protocol.
     const protocol = this.options.protocol || PROTOCOLS.multipart
 

+ 10 - 4
packages/@uppy/tus/src/index.js

@@ -99,9 +99,9 @@ module.exports = class Tus extends Plugin {
    *
    * @param {string} fileID
    */
-  resetUploaderReferences (fileID) {
+  resetUploaderReferences (fileID, shouldTerminate) {
     if (this.uploaders[fileID]) {
-      this.uploaders[fileID].abort()
+      this.uploaders[fileID].abort(shouldTerminate)
       this.uploaders[fileID] = null
     }
     if (this.uploaderEvents[fileID]) {
@@ -244,7 +244,7 @@ module.exports = class Tus extends Plugin {
 
       this.onFileRemove(file.id, (targetFileID) => {
         queuedRequest.abort()
-        this.resetUploaderReferences(file.id)
+        this.resetUploaderReferences(file.id, true)
         resolve(`upload ${targetFileID} was removed`)
       })
 
@@ -270,7 +270,7 @@ module.exports = class Tus extends Plugin {
 
       this.onCancelAll(file.id, () => {
         queuedRequest.abort()
-        this.resetUploaderReferences(file.id)
+        this.resetUploaderReferences(file.id, true)
         resolve(`upload ${file.id} was canceled`)
       })
 
@@ -353,7 +353,10 @@ module.exports = class Tus extends Plugin {
 
       this.onFileRemove(file.id, () => {
         queuedRequest.abort()
+        // still send pause event in case we are dealing with older version of companion
+        // @todo don't send pause event in the next major release.
         socket.send('pause', {})
+        socket.send('cancel', {})
         this.resetUploaderReferences(file.id)
         resolve(`upload ${file.id} was removed`)
       })
@@ -380,7 +383,10 @@ module.exports = class Tus extends Plugin {
 
       this.onCancelAll(file.id, () => {
         queuedRequest.abort()
+        // still send pause event in case we are dealing with older version of companion
+        // @todo don't send pause event in the next major release.
         socket.send('pause', {})
+        socket.send('cancel', {})
         this.resetUploaderReferences(file.id)
         resolve(`upload ${file.id} was canceled`)
       })