Browse Source

storage-node: handle errors in temp write stream

Mokhtar Naamani 3 years ago
parent
commit
225af696f3

+ 2 - 0
storage-node/packages/colossus/paths/asset/v0/{id}.js

@@ -26,6 +26,7 @@ const assert = require('assert')
 function errorHandler(response, err, code) {
   debug(err)
   response.status(err.code || code || 500).send({ message: err.toString() })
+  response.end()
 }
 
 // The maximum total estimated balance that will be spent submitting transactions
@@ -207,6 +208,7 @@ module.exports = function (storage, runtime, ipfsHttpGatewayUrl, anonymous) {
         })
 
         stream.on('error', (err) => {
+          stream.end()
           stream.cleanup()
           errorHandler(res, err)
         })

+ 7 - 3
storage-node/packages/storage/storage.js

@@ -90,6 +90,10 @@ class StorageWriteStream extends Transform {
 
     // Create temp target.
     this.temp = temp.createWriteStream()
+    this.temp.on('error', (err) => this.emit('error', err))
+
+    // Small temporary buffer storing first fileType.minimumBytes of stream
+    // used for early file type detection
     this.buf = Buffer.alloc(0)
   }
 
@@ -118,11 +122,11 @@ class StorageWriteStream extends Transform {
     //   callback(err)
     // })
 
-    // Respect backpressure
+    // Respect backpressure and handle write error
     if (!this.temp.write(chunk)) {
-      this.temp.once('drain', callback)
+      this.temp.once('drain', () => callback(null))
     } else {
-      process.nextTick(callback)
+      process.nextTick(() => callback(null))
     }
   }