Skip to content

Commit

Permalink
Merge pull request #9455 from weseek/imprv/158095-158175-pipeline-fro…
Browse files Browse the repository at this point in the history
…m-stream-is-required

imprv: fix pipeline of callback version
  • Loading branch information
mergify[bot] authored Dec 5, 2024
2 parents 9eab0e9 + cfd4dbe commit 383b5fe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions apps/app/src/server/service/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const logger = loggerFactory('growi:services:ExportService'); // eslint-disable-
const fs = require('fs');
const path = require('path');
const { Transform } = require('stream');
const { pipeline } = require('stream/promises');
const { pipeline, finished } = require('stream/promises');

const archiver = require('archiver');
const mongoose = require('mongoose');
Expand Down Expand Up @@ -107,7 +107,7 @@ class ExportService {
writeStream.write(JSON.stringify(metaData));
writeStream.close();

await pipeline([writeStream]);
await finished(writeStream);

return metaJson;
}
Expand Down Expand Up @@ -355,7 +355,7 @@ class ExportService {
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
await stream;
await finished(stream);

logger.info(`zipped GROWI data into ${zipFile} (${archive.pointer()} bytes)`);

Expand Down
6 changes: 3 additions & 3 deletions apps/app/src/server/service/growi-bridge/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import { pipeline } from 'stream';
import { pipeline as pipelinePromise } from 'stream/promises';
import { finished } from 'stream/promises';

import unzipStream, { type Entry } from 'unzip-stream';

Expand Down Expand Up @@ -80,7 +80,7 @@ class GrowiBridgeService {

const readStream = fs.createReadStream(zipFile);
const parseStream = unzipStream.Parse();
const unzipEntryStream = pipeline(readStream, parseStream);
const unzipEntryStream = pipeline(readStream, parseStream, () => {});

let tapPromise;

Expand All @@ -103,7 +103,7 @@ class GrowiBridgeService {
});

try {
await pipelinePromise([unzipEntryStream]);
await finished(unzipEntryStream);
await tapPromise;
}
// if zip is broken
Expand Down
10 changes: 5 additions & 5 deletions apps/app/src/server/service/import/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';
import type { EventEmitter } from 'stream';
import { Writable, Transform, pipeline } from 'stream';
import { pipeline as pipelinePromise } from 'stream/promises';
import { finished, pipeline as pipelinePromise } from 'stream/promises';

import JSONStream from 'JSONStream';
import gc from 'expose-gc/function';
Expand Down Expand Up @@ -344,10 +344,10 @@ export class ImportService {
async unzip(zipFile) {
const readStream = fs.createReadStream(zipFile);
const parseStream = unzipStream.Parse();
const unzipStreamPipe = pipeline(readStream, parseStream);
const unzipEntryStream = pipeline(readStream, parseStream, () => {});
const files: string[] = [];

const unzipEntryStream = unzipStreamPipe.on('entry', (/** @type {Entry} */ entry) => {
unzipEntryStream.on('entry', (/** @type {Entry} */ entry) => {
const fileName = entry.path;
// https://regex101.com/r/mD4eZs/6
// prevent from unexpecting attack doing unzip file (path traversal attack)
Expand All @@ -365,12 +365,12 @@ export class ImportService {
else {
const jsonFile = path.join(this.baseDir, fileName);
const writeStream = fs.createWriteStream(jsonFile, { encoding: this.growiBridgeService.getEncoding() });
pipeline(entry, writeStream);
pipeline(entry, writeStream, () => {});
files.push(jsonFile);
}
});

await pipelinePromise([unzipEntryStream]);
await finished(unzipEntryStream);

return files;
}
Expand Down

0 comments on commit 383b5fe

Please sign in to comment.