@@ -4,7 +4,7 @@ import path from 'path';
4
4
5
5
import busboy from 'busboy' ;
6
6
import { NextFunction , Request , Response } from 'express' ;
7
- import { Extract , ParseOptions } from 'unzipper' ;
7
+ import { Extract } from 'unzipper' ;
8
8
9
9
import { MetaCallJSON } from '@metacall/protocol/deployment' ;
10
10
import { Application , Applications , Resource } from '../app' ;
@@ -56,10 +56,10 @@ export const uploadPackage = (
56
56
jsons : [ ]
57
57
} ;
58
58
59
- let handled = false ; // TODO: Promisify the whole controller
59
+ let handled = false ;
60
60
61
61
const errorHandler = ( error : AppError ) => {
62
- if ( handled == false ) {
62
+ if ( handled === false ) {
63
63
req . unpipe ( bb ) ;
64
64
next ( error ) ;
65
65
}
@@ -88,8 +88,8 @@ export const uploadPackage = (
88
88
const { mimeType, filename } = info ;
89
89
90
90
if (
91
- mimeType != 'application/x-zip-compressed' &&
92
- mimeType != 'application/zip'
91
+ mimeType !== 'application/x-zip-compressed' &&
92
+ mimeType !== 'application/zip'
93
93
) {
94
94
return errorHandler (
95
95
new AppError ( 'Please upload a zip file' , 404 )
@@ -147,7 +147,7 @@ export const uploadPackage = (
147
147
148
148
eventHandler ( 'finish' , ( ) => {
149
149
if ( resource . blob === undefined ) {
150
- throw Error ( 'Invalid file upload, blob path is not defined' ) ;
150
+ throw new Error ( 'Invalid file upload, blob path is not defined' ) ;
151
151
}
152
152
153
153
const deleteBlob = ( ) => {
@@ -201,33 +201,43 @@ export const uploadPackage = (
201
201
}
202
202
) ;
203
203
204
- const options : ParseOptions = { path : resource . path } ;
204
+ const unzipAndResolve = ( ) => {
205
+ return new Promise < void > ( ( resolve , reject ) => {
206
+ fs . createReadStream ( resource . blob as string )
207
+ . pipe ( Extract ( { path : resource . path } ) )
208
+ . on ( 'close' , ( ) => {
209
+ deleteBlob ( ) ;
210
+ resolve ( ) ;
211
+ } )
212
+ . on ( 'error' , error => {
213
+ deleteBlob ( ) ;
214
+ deleteFolder ( ) ;
215
+ reject (
216
+ new AppError (
217
+ `Failed to unzip the resource at: ${ error . toString ( ) } ` ,
218
+ 500
219
+ )
220
+ ) ;
221
+ } ) ;
222
+ } ) ;
223
+ } ;
205
224
206
- fs . createReadStream ( resource . blob )
207
- . pipe ( Extract ( options ) )
208
- . on ( 'close' , ( ) => {
209
- deleteBlob ( ) ;
225
+ unzipAndResolve ( )
226
+ . then ( ( ) => {
210
227
resourceResolve ( resource ) ;
211
228
} )
212
- . on ( 'error' , error => {
213
- deleteBlob ( ) ;
214
- deleteFolder ( ) ;
215
- const appError = new AppError (
216
- `Failed to unzip the resource at: ${ error . toString ( ) } ` ,
217
- 500
218
- ) ;
219
- errorHandler ( appError ) ;
220
- resourceReject ( appError ) ;
229
+ . catch ( error => {
230
+ resourceReject ( error ) ;
231
+ errorHandler ( error ) ;
221
232
} ) ;
222
233
} ) ;
223
234
224
235
eventHandler ( 'close' , ( ) => {
225
- if ( handled == false ) {
236
+ if ( handled === false ) {
226
237
res . status ( 201 ) . json ( {
227
238
id : resource . id
228
239
} ) ;
229
240
}
230
-
231
241
handled = true ;
232
242
} ) ;
233
243
0 commit comments