Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3624fc5

Browse files
committedApr 18, 2017
fix(write): wrap stuff in promises to improve errors
1 parent a6b3448 commit 3624fc5

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed
 

Diff for: ‎lib/content/write.js

+18-29
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const contentPath = require('./path')
66
const fixOwner = require('../util/fix-owner')
77
const fs = require('graceful-fs')
88
const moveFile = require('../util/move-file')
9+
const PassThrough = require('stream').PassThrough
910
const path = require('path')
10-
const pipe = require('mississippi').pipe
11+
const pipe = BB.promisify(require('mississippi').pipe)
1112
const rimraf = BB.promisify(require('rimraf'))
1213
const ssri = require('ssri')
13-
const through = require('mississippi').through
1414
const to = require('mississippi').to
1515
const uniqueFilename = require('unique-filename')
1616

@@ -43,7 +43,7 @@ function write (cache, data, opts) {
4343
module.exports.stream = writeStream
4444
function writeStream (cache, opts) {
4545
opts = opts || {}
46-
const inputStream = through()
46+
const inputStream = new PassThrough()
4747
let inputErr = false
4848
function errCheck () {
4949
if (inputErr) { throw inputErr }
@@ -90,34 +90,23 @@ function handleContent (inputStream, cache, opts, errCheck) {
9090
}
9191

9292
function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
93-
let sri
94-
const hashStream = ssri.integrityStream({
95-
integrity: opts.integrity,
96-
algorithms: opts.algorithms,
97-
size: opts.size
98-
}).on('integrity', s => {
99-
sri = s
100-
})
101-
102-
let outStream = new BB((resolve, reject) => {
103-
errCheck()
104-
resolve(fs.createWriteStream(tmpTarget, {
93+
return BB.resolve().then(() => {
94+
let sri
95+
const hashStream = ssri.integrityStream({
96+
integrity: opts.integrity,
97+
algorithms: opts.algorithms,
98+
size: opts.size
99+
}).on('integrity', s => {
100+
sri = s
101+
})
102+
const outStream = fs.createWriteStream(tmpTarget, {
105103
flags: 'wx'
106-
}))
107-
})
108-
return BB.using(outStream, outStream => {
104+
})
109105
errCheck()
110-
return new BB((resolve, reject) => {
111-
errCheck()
112-
inputStream.on('error', reject)
113-
pipe(inputStream, hashStream, outStream, err => {
114-
errCheck()
115-
if (err) {
116-
rimraf(tmpTarget).then(() => reject(err), reject)
117-
} else {
118-
resolve(sri)
119-
}
120-
})
106+
return pipe(inputStream, hashStream, outStream).then(() => {
107+
return sri
108+
}, err => {
109+
return rimraf(tmpTarget).then(() => { throw err })
121110
})
122111
})
123112
}

0 commit comments

Comments
 (0)
Failed to load comments.