@@ -18,6 +18,7 @@ const Y = require('../util/y.js')
18
18
const writeFileAsync = BB . promisify ( fs . writeFile )
19
19
20
20
module . exports = write
21
+
21
22
function write ( cache , data , opts ) {
22
23
opts = opts || { }
23
24
if ( opts . algorithms && opts . algorithms . length > 1 ) {
@@ -34,20 +35,23 @@ function write (cache, data, opts) {
34
35
if ( opts . integrity && ! ssri . checkData ( data , opts . integrity , opts ) ) {
35
36
return BB . reject ( checksumError ( opts . integrity , sri ) )
36
37
}
37
- return BB . using ( makeTmp ( cache , opts ) , tmp => (
38
- writeFileAsync (
39
- tmp . target , data , { flag : 'wx' }
40
- ) . then ( ( ) => (
41
- moveToDestination ( tmp , cache , sri , opts )
42
- ) )
43
- ) ) . then ( ( ) => ( { integrity : sri , size : data . length } ) )
38
+ return makeTmp ( cache , opts )
39
+ . then ( ( tmp ) => {
40
+ return writeFileAsync (
41
+ tmp . target , data , { flag : 'wx' }
42
+ ) . then ( ( ) => moveToDestination ( tmp , cache , sri , opts ) )
43
+ . then ( ( result ) => makeTmpDisposer ( tmp , result ) )
44
+ . catch ( ( err ) => makeTmpDisposer ( tmp , err , true ) )
45
+ } ) . then ( ( ) => ( { integrity : sri , size : data . length } ) )
44
46
}
45
47
46
48
module . exports . stream = writeStream
49
+
47
50
function writeStream ( cache , opts ) {
48
51
opts = opts || { }
49
52
const inputStream = new PassThrough ( )
50
53
let inputErr = false
54
+
51
55
function errCheck ( ) {
52
56
if ( inputErr ) { throw inputErr }
53
57
}
@@ -81,16 +85,19 @@ function writeStream (cache, opts) {
81
85
}
82
86
83
87
function handleContent ( inputStream , cache , opts , errCheck ) {
84
- return BB . using ( makeTmp ( cache , opts ) , tmp => {
85
- errCheck ( )
86
- return pipeToTmp (
87
- inputStream , cache , tmp . target , opts , errCheck
88
- ) . then ( res => {
89
- return moveToDestination (
90
- tmp , cache , res . integrity , opts , errCheck
91
- ) . then ( ( ) => res )
88
+ return makeTmp ( cache , opts )
89
+ . then ( ( tmp ) => {
90
+ errCheck ( )
91
+ return pipeToTmp (
92
+ inputStream , cache , tmp . target , opts , errCheck
93
+ ) . then ( res => {
94
+ return moveToDestination (
95
+ tmp , cache , res . integrity , opts , errCheck
96
+ ) . then ( ( ) => res )
97
+ } )
98
+ . then ( ( result ) => makeTmpDisposer ( tmp , result ) )
99
+ . catch ( ( err ) => makeTmpDisposer ( tmp , err , true ) )
92
100
} )
93
- } )
94
101
}
95
102
96
103
function pipeToTmp ( inputStream , cache , tmpTarget , opts , errCheck ) {
@@ -125,7 +132,23 @@ function makeTmp (cache, opts) {
125
132
) . then ( ( ) => ( {
126
133
target : tmpTarget ,
127
134
moved : false
128
- } ) ) . disposer ( tmp => ( ! tmp . moved && rimraf ( tmp . target ) ) )
135
+ } ) )
136
+ }
137
+
138
+ function makeTmpDisposer ( tmp , result , shouldThrow = false ) {
139
+ const returnResult = ( ) => {
140
+ if ( shouldThrow ) {
141
+ throw result
142
+ }
143
+ return result
144
+ }
145
+
146
+ if ( tmp . moved ) {
147
+ return returnResult ( )
148
+ }
149
+ return rimraf ( tmp . target )
150
+ . then ( ( ) => returnResult ( ) )
151
+ . catch ( ( ) => returnResult ( ) )
129
152
}
130
153
131
154
function moveToDestination ( tmp , cache , sri , opts , errCheck ) {
0 commit comments