Browse Source

storage-node: ensure write stream flushed correctly

Mokhtar Naamani 3 years ago
parent
commit
bbb39d99c9

+ 4 - 1
storage-node/packages/colossus/paths/asset/v0/{id}.js

@@ -206,7 +206,10 @@ module.exports = function (storage, runtime, ipfsHttpGatewayUrl, anonymous) {
           }
         })
 
-        stream.on('error', (err) => errorHandler(res, err))
+        stream.on('error', (err) => {
+          stream.cleanup()
+          errorHandler(res, err)
+        })
         req.pipe(stream)
       } catch (err) {
         errorHandler(res, err)

+ 17 - 17
storage-node/packages/storage/storage.js

@@ -99,29 +99,29 @@ class StorageWriteStream extends Transform {
       chunk = Buffer.from(chunk)
     }
 
-    this.temp.write(chunk)
-
-    // Try to detect file type during streaming.
-    if (!this.fileInfo && this.buf.byteLength <= fileType.minimumBytes) {
-      this.buf = Buffer.concat([this.buf, chunk])
-
-      if (this.buf >= fileType.minimumBytes) {
-        const info = fileType(this.buf)
-        // No info? We will try again at the end of the stream.
-        if (info) {
-          this.fileInfo = fixFileInfo(info)
-          this.emit('fileInfo', this.fileInfo)
+    this.temp.write(chunk, (err) => {
+      // Try to detect file type during streaming.
+      if (!this.fileInfo && this.buf.byteLength <= fileType.minimumBytes) {
+        this.buf = Buffer.concat([this.buf, chunk])
+
+        if (this.buf >= fileType.minimumBytes) {
+          const info = fileType(this.buf)
+          // No info? We will try again at the end of the stream.
+          if (info) {
+            this.fileInfo = fixFileInfo(info)
+            this.emit('fileInfo', this.fileInfo)
+          }
         }
       }
-    }
-
-    callback(null)
+      callback(err)
+    })
   }
 
   _flush(callback) {
     debug('Flushing temporary stream:', this.temp.path)
-    this.temp.end()
-    callback(null)
+    this.temp.end(() => {
+      callback(null)
+    })
   }
 
   /*