@@ -13,34 +13,37 @@ const path = require('path')
13
13
const rimraf = util . promisify ( require ( 'rimraf' ) )
14
14
const ssri = require ( 'ssri' )
15
15
const uniqueFilename = require ( 'unique-filename' )
16
- const { disposer } = require ( './../util/disposer' )
17
16
const fsm = require ( 'fs-minipass' )
18
17
19
18
const writeFile = util . promisify ( fs . writeFile )
20
19
21
20
module . exports = write
22
21
23
- function write ( cache , data , opts = { } ) {
22
+ async function write ( cache , data , opts = { } ) {
24
23
const { algorithms, size, integrity } = opts
25
24
if ( algorithms && algorithms . length > 1 ) {
26
25
throw new Error ( 'opts.algorithms only supports a single algorithm for now' )
27
26
}
28
27
29
28
if ( typeof size === 'number' && data . length !== size ) {
30
- return Promise . reject ( sizeError ( size , data . length ) )
29
+ throw sizeError ( size , data . length )
31
30
}
32
31
33
32
const sri = ssri . fromData ( data , algorithms ? { algorithms } : { } )
34
33
if ( integrity && ! ssri . checkData ( data , integrity , opts ) ) {
35
- return Promise . reject ( checksumError ( integrity , sri ) )
34
+ throw checksumError ( integrity , sri )
36
35
}
37
36
38
- return disposer ( makeTmp ( cache , opts ) , makeTmpDisposer ,
39
- ( tmp ) => {
40
- return writeFile ( tmp . target , data , { flag : 'wx' } )
41
- . then ( ( ) => moveToDestination ( tmp , cache , sri , opts ) )
42
- } )
43
- . then ( ( ) => ( { integrity : sri , size : data . length } ) )
37
+ const tmp = await makeTmp ( cache , opts )
38
+ try {
39
+ await writeFile ( tmp . target , data , { flag : 'wx' } )
40
+ await moveToDestination ( tmp , cache , sri , opts )
41
+ return { integrity : sri , size : data . length }
42
+ } finally {
43
+ if ( ! tmp . moved ) {
44
+ await rimraf ( tmp . target )
45
+ }
46
+ }
44
47
}
45
48
46
49
module . exports . stream = writeStream
@@ -94,18 +97,22 @@ function writeStream (cache, opts = {}) {
94
97
return new CacacheWriteStream ( cache , opts )
95
98
}
96
99
97
- function handleContent ( inputStream , cache , opts ) {
98
- return disposer ( makeTmp ( cache , opts ) , makeTmpDisposer , ( tmp ) => {
99
- return pipeToTmp ( inputStream , cache , tmp . target , opts )
100
- . then ( ( res ) => {
101
- return moveToDestination (
102
- tmp ,
103
- cache ,
104
- res . integrity ,
105
- opts
106
- ) . then ( ( ) => res )
107
- } )
108
- } )
100
+ async function handleContent ( inputStream , cache , opts ) {
101
+ const tmp = await makeTmp ( cache , opts )
102
+ try {
103
+ const res = await pipeToTmp ( inputStream , cache , tmp . target , opts )
104
+ await moveToDestination (
105
+ tmp ,
106
+ cache ,
107
+ res . integrity ,
108
+ opts
109
+ )
110
+ return res
111
+ } finally {
112
+ if ( ! tmp . moved ) {
113
+ await rimraf ( tmp . target )
114
+ }
115
+ }
109
116
}
110
117
111
118
function pipeToTmp ( inputStream , cache , tmpTarget , opts ) {
@@ -136,11 +143,7 @@ function pipeToTmp (inputStream, cache, tmpTarget, opts) {
136
143
outStream
137
144
)
138
145
139
- return pipeline . promise ( )
140
- . then ( ( ) => ( { integrity, size } ) )
141
- . catch ( er => rimraf ( tmpTarget ) . then ( ( ) => {
142
- throw er
143
- } ) )
146
+ return pipeline . promise ( ) . then ( ( ) => ( { integrity, size } ) )
144
147
}
145
148
146
149
function makeTmp ( cache , opts ) {
@@ -151,14 +154,6 @@ function makeTmp (cache, opts) {
151
154
} ) )
152
155
}
153
156
154
- function makeTmpDisposer ( tmp ) {
155
- if ( tmp . moved ) {
156
- return Promise . resolve ( )
157
- }
158
-
159
- return rimraf ( tmp . target )
160
- }
161
-
162
157
function moveToDestination ( tmp , cache , sri , opts ) {
163
158
const destination = contentPath ( cache , sri )
164
159
const destDir = path . dirname ( destination )
0 commit comments