Browse Source

fix(server): Fix error when populating assets

When running the populate script, I was getting an ERR_STREAM_WRITE_AFTER_END error each time. It seems related to https://github.com/jaydenseric/graphql-upload/issues/131 but unfortunately this is a transitive dependency of apollo-server-express and I can't directly upgrade to the latest version of graphql-upload. This work-around seems to fix things though.
Michael Bromley 7 years ago
parent
commit
80534db242

+ 4 - 4
server/src/plugin/default-asset-server-plugin/default-asset-storage-strategy.ts

@@ -1,5 +1,5 @@
-import { INestApplication, INestExpressApplication } from '@nestjs/common';
 import { Request } from 'express';
+import { ReadStream } from 'fs';
 import fs from 'fs-extra';
 import path from 'path';
 import { Stream } from 'stream';
@@ -14,12 +14,12 @@ export class DefaultAssetStorageStrategy implements AssetStorageStrategy {
         this.ensureUploadPathExists(this.uploadPath);
     }
 
-    writeFileFromStream(fileName: string, data: Stream): Promise<string> {
+    writeFileFromStream(fileName: string, data: ReadStream): Promise<string> {
         const filePath = path.join(this.uploadPath, fileName);
         const writeStream = fs.createWriteStream(filePath, 'binary');
         return new Promise<string>((resolve, reject) => {
-            data.pipe(writeStream);
-            writeStream.on('close', () => resolve(this.filePathToIdentifier(filePath)));
+            data.on('data', chunk => writeStream.write(chunk));
+            data.on('close', () => resolve(this.filePathToIdentifier(filePath)));
             writeStream.on('error', reject);
         });
     }