Просмотр исходного кода

module the logic to read the meta in zip

Ryu Sato 2 лет назад
Родитель
Сommit
2c8d45f67c

+ 8 - 19
apps/app/src/server/service/growi-bridge/index.ts

@@ -1,10 +1,10 @@
-import { Transform } from 'stream';
-
 import { Model } from 'mongoose';
 import unzipStream, { type Entry } from 'unzip-stream';
 
 import loggerFactory from '~/utils/logger';
 
+import { tapStreamDataByPromise } from './unzip-stream-entry-wrapper';
+
 const fs = require('fs');
 const path = require('path');
 
@@ -102,25 +102,15 @@ class GrowiBridgeService {
 
     const readStream = fs.createReadStream(zipFile);
     const unzipStreamPipe = readStream.pipe(unzipStream.Parse());
-    const entryStreams: Array<Promise<void>> = [];
+    let tapPromise;
 
-    unzipStreamPipe.on('entry', (entry: Entry) => {
+    const unzipEntryStream = unzipStreamPipe.on('entry', (entry: Entry) => {
       const fileName = entry.path;
       const size = entry.size; // might be undefined in some archives
       if (fileName === this.getMetaFileName()) {
-        const metaBuffers: Array<Buffer> = [];
-        const metaStream = new Transform({
-          transform: (chunk, _encoding, callback) => {
-            metaBuffers.push(Buffer.from(chunk));
-            callback();
-          },
-          final: (callback) => {
-            meta = JSON.parse(Buffer.concat(metaBuffers).toString());
-            callback();
-          },
+        tapPromise = tapStreamDataByPromise(entry).then((metaBuffer) => {
+          meta = JSON.parse(metaBuffer.toString());
         });
-
-        entryStreams.push(streamToPromise(entry.pipe(metaStream)));
       }
       else {
         innerFileStats.push({
@@ -129,13 +119,12 @@ class GrowiBridgeService {
           size,
         });
       }
-
       entry.autodrain();
     });
 
     try {
-      await streamToPromise(unzipStreamPipe);
-      await Promise.all(entryStreams);
+      await streamToPromise(unzipEntryStream);
+      await tapPromise;
     }
     // if zip is broken
     catch (err) {

+ 22 - 0
apps/app/src/server/service/growi-bridge/unzip-stream-entry-wrapper.ts

@@ -0,0 +1,22 @@
+import { PassThrough } from 'stream';
+
+import type { Entry } from 'unzip-stream';
+
+export const tapStreamDataByPromise = (entry: Entry): Promise<Buffer> => {
+  return new Promise((resolve, reject) => {
+    const buffers: Array<Buffer> = [];
+
+    const entryContentGetterStream = new PassThrough()
+      .on('data', (chunk) => {
+        buffers.push(Buffer.from(chunk));
+      })
+      .on('end', () => {
+        resolve(Buffer.concat(buffers));
+      })
+      .on('error', reject);
+
+    entry
+      .pipe(entryContentGetterStream)
+      .on('error', reject);
+  });
+};