Skip to content

Commit 0dcea3f

Browse files
authored
Use stream#pipeline instead of stream.pipe (#8235)
1 parent b8b203d commit 0dcea3f

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

packages/core/cache/src/FSCache.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
// @flow strict-local
22

3-
import type {Readable} from 'stream';
3+
import type {Readable, Writable} from 'stream';
44
import type {FilePath} from '@parcel/types';
55
import type {FileSystem} from '@parcel/fs';
66
import type {Cache} from './types';
77

8+
import stream from 'stream';
89
import path from 'path';
10+
import {promisify} from 'util';
911
import logger from '@parcel/logger';
1012
import {serialize, deserialize, registerSerializableClass} from '@parcel/core';
1113
// flowlint-next-line untyped-import:off
1214
import packageJson from '../package.json';
1315

16+
const pipeline: (Readable, Writable) => Promise<void> = promisify(
17+
stream.pipeline,
18+
);
19+
1420
export class FSCache implements Cache {
1521
fs: FileSystem;
1622
dir: FilePath;
@@ -45,12 +51,10 @@ export class FSCache implements Cache {
4551
}
4652

4753
setStream(key: string, stream: Readable): Promise<void> {
48-
return new Promise((resolve, reject) => {
49-
stream
50-
.pipe(this.fs.createWriteStream(this._getCachePath(`${key}-large`)))
51-
.on('error', reject)
52-
.on('finish', resolve);
53-
});
54+
return pipeline(
55+
stream,
56+
this.fs.createWriteStream(this._getCachePath(`${key}-large`)),
57+
);
5458
}
5559

5660
has(key: string): Promise<boolean> {

packages/core/cache/src/LMDBCache.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
// @flow strict-local
22
import type {FilePath} from '@parcel/types';
33
import type {Cache} from './types';
4+
import type {Readable, Writable} from 'stream';
45

5-
import {Readable} from 'stream';
6+
import stream from 'stream';
67
import path from 'path';
8+
import {promisify} from 'util';
79
import {serialize, deserialize, registerSerializableClass} from '@parcel/core';
810
import {NodeFS} from '@parcel/fs';
911
// flowlint-next-line untyped-import:off
1012
import packageJson from '../package.json';
1113
// $FlowFixMe
1214
import lmdb from 'lmdb';
1315

16+
const pipeline: (Readable, Writable) => Promise<void> = promisify(
17+
stream.pipeline,
18+
);
19+
1420
export class LMDBCache implements Cache {
1521
fs: NodeFS;
1622
dir: FilePath;
@@ -64,12 +70,10 @@ export class LMDBCache implements Cache {
6470
}
6571

6672
setStream(key: string, stream: Readable): Promise<void> {
67-
return new Promise((resolve, reject) => {
68-
stream
69-
.pipe(this.fs.createWriteStream(path.join(this.dir, key)))
70-
.on('error', reject)
71-
.on('finish', resolve);
72-
});
73+
return pipeline(
74+
stream,
75+
this.fs.createWriteStream(path.join(this.dir, key)),
76+
);
7377
}
7478

7579
getBlob(key: string): Promise<Buffer> {

packages/core/fs/src/index.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// @flow strict-local
22
import type {FileSystem} from './types';
33
import type {FilePath} from '@parcel/types';
4+
import type {Readable, Writable} from 'stream';
5+
46
import path from 'path';
7+
import stream from 'stream';
8+
import {promisify} from 'util';
59

610
export type * from './types';
711
export * from './NodeFS';
812
export * from './MemoryFS';
913
export * from './OverlayFS';
1014

15+
const pipeline: (Readable, Writable) => Promise<void> = promisify(
16+
stream.pipeline,
17+
);
18+
1119
// Recursively copies a directory from the sourceFS to the destinationFS
1220
export async function ncp(
1321
sourceFS: FileSystem,
@@ -22,13 +30,10 @@ export async function ncp(
2230
let destPath = path.join(destination, file);
2331
let stats = await sourceFS.stat(sourcePath);
2432
if (stats.isFile()) {
25-
await new Promise((resolve, reject) => {
26-
sourceFS
27-
.createReadStream(sourcePath)
28-
.pipe(destinationFS.createWriteStream(destPath))
29-
.on('finish', () => resolve())
30-
.on('error', reject);
31-
});
33+
await pipeline(
34+
sourceFS.createReadStream(sourcePath),
35+
destinationFS.createWriteStream(destPath),
36+
);
3237
} else if (stats.isDirectory()) {
3338
await ncp(sourceFS, sourcePath, destinationFS, destPath);
3439
}

0 commit comments

Comments
 (0)