Skip to content

Commit d5898aa

Browse files
Improve stream handling (#13)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 5471e67 commit d5898aa

File tree

3 files changed

+4
-19
lines changed

3 files changed

+4
-19
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ jobs:
1212
node-version:
1313
- 14
1414
- 12
15-
- 10
16-
- 8
1715
steps:
1816
- uses: actions/checkout@v2
1917
- uses: actions/setup-node@v1

index.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
'use strict';
22
const {promisify} = require('util');
33
const path = require('path');
4+
const stream = require('stream');
45
const fs = require('graceful-fs');
56
const isStream = require('is-stream');
67
const makeDir = require('make-dir');
78
const uuid = require('uuid');
89
const tempDir = require('temp-dir');
910

1011
const writeFileP = promisify(fs.writeFile);
12+
const pipelineP = promisify(stream.pipeline);
1113

1214
const tempfile = filePath => path.join(tempDir, uuid.v4(), (filePath || ''));
1315

14-
const writeStream = async (filePath, fileContent) => new Promise((resolve, reject) => {
15-
const writable = fs.createWriteStream(filePath);
16-
17-
fileContent
18-
.on('error', error => {
19-
// Be careful to reject before writable.end(), otherwise the writable's
20-
// 'finish' event will fire first and we will resolve the promise
21-
// before we reject it.
22-
reject(error);
23-
fileContent.unpipe(writable);
24-
writable.end();
25-
})
26-
.pipe(writable)
27-
.on('error', reject)
28-
.on('finish', resolve);
29-
});
16+
const writeStream = async (filePath, data) => pipelineP(data, fs.createWriteStream(filePath));
3017

3118
module.exports = async (fileContent, filePath) => {
3219
const tempPath = tempfile(filePath);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"engines": {
14-
"node": ">=8"
14+
"node": ">=12"
1515
},
1616
"scripts": {
1717
"test": "xo && ava && tsd"

0 commit comments

Comments
 (0)