|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -var checksumStream = require('checksum-stream') |
4 |
| -var contentPath = require('./path') |
5 |
| -var dezalgo = require('dezalgo') |
6 |
| -var fs = require('graceful-fs') |
7 |
| -var pipe = require('mississippi').pipe |
| 3 | +const Promise = require('bluebird') |
| 4 | + |
| 5 | +const checksumStream = require('checksum-stream') |
| 6 | +const contentPath = require('./path') |
| 7 | +const fs = require('graceful-fs') |
| 8 | +const pipe = require('mississippi').pipe |
| 9 | + |
| 10 | +Promise.promisifyAll(fs) |
8 | 11 |
|
9 | 12 | module.exports.readStream = readStream
|
10 | 13 | function readStream (cache, address, opts) {
|
11 | 14 | opts = opts || {}
|
12 |
| - var stream = checksumStream({ |
| 15 | + const stream = checksumStream({ |
13 | 16 | digest: address,
|
14 | 17 | algorithm: opts.hashAlgorithm || 'sha1'
|
15 | 18 | })
|
16 |
| - var cpath = contentPath(cache, address) |
17 |
| - hasContent(cache, address, function (err, exists) { |
18 |
| - if (err) { return stream.emit('error', err) } |
| 19 | + const cpath = contentPath(cache, address) |
| 20 | + hasContent(cache, address).then(exists => { |
19 | 21 | if (!exists) {
|
20 |
| - err = new Error('content not found') |
| 22 | + const err = new Error('content not found') |
21 | 23 | err.code = 'ENOENT'
|
22 | 24 | err.cache = cache
|
23 | 25 | err.digest = address
|
24 | 26 | return stream.emit('error', err)
|
25 | 27 | } else {
|
26 | 28 | pipe(fs.createReadStream(cpath), stream)
|
27 | 29 | }
|
| 30 | + }).catch(err => { |
| 31 | + stream.emit('error', err) |
28 | 32 | })
|
29 | 33 | return stream
|
30 | 34 | }
|
31 | 35 |
|
32 | 36 | module.exports.hasContent = hasContent
|
33 | 37 | function hasContent (cache, address, cb) {
|
34 |
| - cb = dezalgo(cb) |
35 |
| - if (!address) { return cb(null, false) } |
36 |
| - fs.lstat(contentPath(cache, address), function (err) { |
| 38 | + if (!address) { return Promise.resolve(false) } |
| 39 | + return fs.lstatAsync( |
| 40 | + contentPath(cache, address) |
| 41 | + ).then(() => true).catch(err => { |
37 | 42 | if (err && err.code === 'ENOENT') {
|
38 |
| - return cb(null, false) |
| 43 | + return Promise.resolve(false) |
39 | 44 | } else if (err && process.platform === 'win32' && err.code === 'EPERM') {
|
40 |
| - return cb(null, false) |
41 |
| - } else if (err) { |
42 |
| - return cb(err) |
| 45 | + return Promise.resolve(false) |
43 | 46 | } else {
|
44 |
| - return cb(null, true) |
| 47 | + throw err |
45 | 48 | }
|
46 | 49 | })
|
47 | 50 | }
|
0 commit comments