Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
fix(write): wrap stuff in promises to improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 18, 2017
1 parent a6b3448 commit 3624fc5
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions lib/content/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const contentPath = require('./path')
const fixOwner = require('../util/fix-owner')
const fs = require('graceful-fs')
const moveFile = require('../util/move-file')
const PassThrough = require('stream').PassThrough
const path = require('path')
const pipe = require('mississippi').pipe
const pipe = BB.promisify(require('mississippi').pipe)
const rimraf = BB.promisify(require('rimraf'))
const ssri = require('ssri')
const through = require('mississippi').through
const to = require('mississippi').to
const uniqueFilename = require('unique-filename')

Expand Down Expand Up @@ -43,7 +43,7 @@ function write (cache, data, opts) {
module.exports.stream = writeStream
function writeStream (cache, opts) {
opts = opts || {}
const inputStream = through()
const inputStream = new PassThrough()
let inputErr = false
function errCheck () {
if (inputErr) { throw inputErr }
Expand Down Expand Up @@ -90,34 +90,23 @@ function handleContent (inputStream, cache, opts, errCheck) {
}

function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
let sri
const hashStream = ssri.integrityStream({
integrity: opts.integrity,
algorithms: opts.algorithms,
size: opts.size
}).on('integrity', s => {
sri = s
})

let outStream = new BB((resolve, reject) => {
errCheck()
resolve(fs.createWriteStream(tmpTarget, {
return BB.resolve().then(() => {
let sri
const hashStream = ssri.integrityStream({
integrity: opts.integrity,
algorithms: opts.algorithms,
size: opts.size
}).on('integrity', s => {
sri = s
})
const outStream = fs.createWriteStream(tmpTarget, {
flags: 'wx'
}))
})
return BB.using(outStream, outStream => {
})
errCheck()
return new BB((resolve, reject) => {
errCheck()
inputStream.on('error', reject)
pipe(inputStream, hashStream, outStream, err => {
errCheck()
if (err) {
rimraf(tmpTarget).then(() => reject(err), reject)
} else {
resolve(sri)
}
})
return pipe(inputStream, hashStream, outStream).then(() => {
return sri
}, err => {
return rimraf(tmpTarget).then(() => { throw err })
})
})
}
Expand Down

0 comments on commit 3624fc5

Please sign in to comment.