@@ -19,10 +19,15 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
1919 public static OPERATION_END_NO_REFRESH_REPORT_CODE = 3 ;
2020 public static DO_REFRESH = 1 ;
2121 public static SKIP_REFRESH = 0 ;
22+ public static APP_IDENTIFIER_MISSING_ERROR = 'You need to provide "appIdentifier" as a configuration property!' ;
23+ public static APP_PLATFORMS_PATH_MISSING_ERROR = 'You need to provide "appPlatformsPath" as a configuration property!' ;
24+ public static SOCKET_CONNECTION_ALREADY_EXISTS_ERROR = "Socket connection already exists." ;
25+ public static SOCKET_CONNECTION_TIMED_OUT_ERROR = "Socket connection timed out." ;
26+ public static NO_SOCKET_CONNECTION_AVAILABLE_ERROR = "No socket connection available." ;
2227 public protocolVersion : string ;
2328 private operationPromises : IDictionary < any > ;
2429 private socketError : string | Error ;
25- private socketConnection : INetSocket ;
30+ private socketConnection : ILiveSyncSocket ;
2631 private configuration : IAndroidLivesyncToolConfiguration ;
2732 private pendingConnectionData : {
2833 connectionTimer ?: NodeJS . Timer ,
@@ -46,15 +51,15 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
4651
4752 public async connect ( configuration : IAndroidLivesyncToolConfiguration ) : Promise < void > {
4853 if ( ! configuration . appIdentifier ) {
49- this . $errors . fail ( `You need to provide "appIdentifier" as a configuration property!` ) ;
54+ this . $errors . fail ( AndroidLivesyncTool . APP_IDENTIFIER_MISSING_ERROR ) ;
5055 }
5156
5257 if ( ! configuration . appPlatformsPath ) {
53- this . $errors . fail ( `You need to provide "appPlatformsPath" as a configuration property!` ) ;
58+ this . $errors . fail ( AndroidLivesyncTool . APP_PLATFORMS_PATH_MISSING_ERROR ) ;
5459 }
5560
5661 if ( this . socketConnection ) {
57- this . $errors . fail ( "Socket connection already exists." ) ;
62+ this . $errors . fail ( AndroidLivesyncTool . SOCKET_CONNECTION_ALREADY_EXISTS_ERROR ) ;
5863 }
5964
6065 if ( ! configuration . localHostAddress ) {
@@ -98,9 +103,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
98103 return this . sendFiles ( list ) ;
99104 }
100105
101- public removeFile ( filePath : string ) : Promise < boolean > {
102- return new Promise ( ( resolve : Function , reject : Function ) => {
103- this . verifyActiveConnection ( reject ) ;
106+ public async removeFile ( filePath : string ) : Promise < void > {
107+ this . verifyActiveConnection ( ) ;
104108 const filePathData = this . getFilePathData ( filePath ) ;
105109 const headerBuffer = Buffer . alloc ( PROTOCOL_OPERATION_LENGTH_SIZE +
106110 SIZE_BYTE_LENGTH +
@@ -114,11 +118,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
114118 headerBuffer . write ( filePathData . relativeFilePath , offset , filePathData . filePathLengthBytes ) ;
115119 const hash = crypto . createHash ( "md5" ) . update ( headerBuffer ) . digest ( ) ;
116120
117- this . socketConnection . write ( headerBuffer ) ;
118- this . socketConnection . write ( hash , ( ) => {
119- resolve ( true ) ;
120- } ) ;
121- } ) ;
121+ await this . writeToSocket ( headerBuffer ) ;
122+ await this . writeToSocket ( hash ) ;
122123 }
123124
124125 public removeFiles ( files : string [ ] ) {
@@ -133,10 +134,14 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
133134 return ! ! this . operationPromises [ operationId ] ;
134135 }
135136
136- public sendDoSyncOperation ( doRefresh = true , timeout ?: number , operationId ?: string ) : Promise < IAndroidLivesyncSyncOperationResult > {
137+ public sendDoSyncOperation ( options ?: IDoSyncOperationOptions ) : Promise < IAndroidLivesyncSyncOperationResult > {
138+ options = _ . assign ( { doRefresh : true , timeout : SYNC_OPERATION_TIMEOUT } , options ) ;
139+ const { doRefresh , timeout, operationId } = options ;
137140 const id = operationId || this . generateOperationIdentifier ( ) ;
138- const operationPromise : Promise < IAndroidLivesyncSyncOperationResult > = new Promise ( ( resolve : Function , reject : Function ) => {
139- this . verifyActiveConnection ( reject ) ;
141+ const operationPromise : Promise < IAndroidLivesyncSyncOperationResult > = new Promise ( ( resolve , reject ) => {
142+ if ( ! this . verifyActiveConnection ( reject ) ) {
143+ return ;
144+ }
140145 const message = `${ AndroidLivesyncTool . DO_SYNC_OPERATION } ${ id } ` ;
141146 const headerBuffer = Buffer . alloc ( Buffer . byteLength ( message ) + DO_REFRESH_LENGTH ) ;
142147 const socketId = this . socketConnection . uid ;
@@ -145,11 +150,10 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
145150
146151 headerBuffer . writeUInt8 ( doRefreshCode , offset ) ;
147152 const hash = crypto . createHash ( "md5" ) . update ( headerBuffer ) . digest ( ) ;
153+ this . writeToSocket ( headerBuffer ) . then ( ( ) => {
154+ this . writeToSocket ( hash ) . catch ( reject ) ;
155+ } ) . catch ( reject ) ;
148156
149- this . socketConnection . write ( headerBuffer ) ;
150- this . socketConnection . write ( hash ) ;
151-
152- timeout = timeout || SYNC_OPERATION_TIMEOUT ;
153157 const timeoutId = setTimeout ( ( ) => {
154158 if ( this . isOperationInProgress ( id ) ) {
155159 this . handleSocketError ( socketId , "Sync operation is taking too long" ) ;
@@ -186,10 +190,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
186190 return ! ! this . socketConnection ;
187191 }
188192
189- private sendFileHeader ( filePath : string ) : Promise < void > {
190- return new Promise ( ( resolve , reject ) => {
191- let error ;
192- this . verifyActiveConnection ( reject ) ;
193+ private async sendFileHeader ( filePath : string ) : Promise < void > {
194+ this . verifyActiveConnection ( ) ;
193195 const filePathData = this . getFilePathData ( filePath ) ;
194196 const stats = this . $fs . getFsStats ( filePathData . filePath ) ;
195197 const fileContentLengthBytes = stats . size ;
@@ -203,13 +205,9 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
203205 fileContentLengthSize ) ;
204206
205207 if ( filePathData . filePathLengthSize > 255 ) {
206- error = this . getErrorWithMessage ( "File name size is longer that 255 digits." ) ;
208+ this . $errors . failWithoutHelp ( "File name size is longer that 255 digits." ) ;
207209 } else if ( fileContentLengthSize > 255 ) {
208- error = this . getErrorWithMessage ( "File name size is longer that 255 digits." ) ;
209- }
210-
211- if ( error ) {
212- reject ( error ) ;
210+ this . $errors . failWithoutHelp ( "File name size is longer that 255 digits." ) ;
213211 }
214212
215213 let offset = 0 ;
@@ -221,56 +219,42 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
221219 headerBuffer . write ( fileContentLengthString , offset , fileContentLengthSize ) ;
222220 const hash = crypto . createHash ( "md5" ) . update ( headerBuffer ) . digest ( ) ;
223221
224- this . socketConnection . write ( headerBuffer ) ;
225- this . socketConnection . write ( hash ) ;
226- resolve ( ) ;
227- } ) ;
222+ await this . writeToSocket ( headerBuffer ) ;
223+ await this . writeToSocket ( hash ) ;
228224 }
229225
230226 private sendFileContent ( filePath : string ) : Promise < boolean > {
231227 return new Promise ( ( resolve , reject ) => {
232- this . verifyActiveConnection ( reject ) ;
228+ if ( ! this . verifyActiveConnection ( reject ) ) {
229+ return ;
230+ }
231+
233232 const fileStream = this . $fs . createReadStream ( filePath ) ;
234233 const fileHash = crypto . createHash ( "md5" ) ;
235234
236235 fileStream
237- . on ( "data" , ( chunk : string | Buffer ) => {
236+ . on ( "data" , ( chunk : Buffer ) => {
238237 fileHash . update ( chunk ) ;
239- if ( this . socketConnection ) {
240- this . socketConnection . write ( chunk ) ;
241- } else {
242- const error = this . checkConnectionStatus ( ) ;
243- //TODO Destroy method added in node 8.0.0.
244- //when we deprecate node 6.x uncomment the line below
245- //fileStream.destroy(error);
246- reject ( error ) ;
247- }
238+ this . writeToSocket ( chunk ) . catch ( reject ) ;
248239 } )
249240 . on ( "end" , ( ) => {
250- if ( this . socketConnection ) {
251- this . socketConnection . write ( fileHash . digest ( ) , ( ) => {
252- resolve ( true ) ;
253- } ) ;
254- } else {
255- const error = this . checkConnectionStatus ( ) ;
256- reject ( error ) ;
257- }
241+ this . writeToSocket ( fileHash . digest ( ) ) . then ( ( ) => resolve ( ) ) . catch ( reject ) ;
258242 } )
259243 . on ( "error" , ( error : Error ) => {
260244 reject ( error ) ;
261245 } ) ;
262246 } ) ;
263247 }
264248
265- private createSocket ( port : number ) : INetSocket {
249+ private createSocket ( port : number ) : ILiveSyncSocket {
266250 const socket = this . $injector . resolve ( "LiveSyncSocket" ) ;
267251 socket . connect ( port , this . configuration . localHostAddress ) ;
268252 return socket ;
269253 }
270254
271255 private checkConnectionStatus ( ) {
272256 if ( this . socketConnection === null ) {
273- const defaultError = this . getErrorWithMessage ( "No socket connection available." ) ;
257+ const defaultError = this . getErrorWithMessage ( AndroidLivesyncTool . NO_SOCKET_CONNECTION_AVAILABLE_ERROR ) ;
274258 const error = this . socketError || defaultError ;
275259
276260 return error ;
@@ -286,9 +270,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
286270 if ( error && ! rejectHandler ) {
287271 this . $errors . failWithoutHelp ( error . toString ( ) ) ;
288272 }
273+
274+ return true ;
289275 }
290276
291- private handleConnection ( { socket, data } : { socket : INetSocket , data : NodeBuffer | string } ) {
277+ private handleConnection ( { socket, data } : { socket : ILiveSyncSocket , data : NodeBuffer | string } ) {
292278 this . socketConnection = socket ;
293279 this . socketConnection . uid = this . generateOperationIdentifier ( ) ;
294280
@@ -313,7 +299,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
313299 } ) ;
314300 }
315301
316- private connectEventuallyUntilTimeout ( factory : ( ) => INetSocket , timeout : number ) : Promise < { socket : INetSocket , data : NodeBuffer | string } > {
302+ private connectEventuallyUntilTimeout ( factory : ( ) => ILiveSyncSocket , timeout : number ) : Promise < { socket : ILiveSyncSocket , data : NodeBuffer | string } > {
317303 return new Promise ( ( resolve , reject ) => {
318304 let lastKnownError : Error | string ,
319305 isConnected = false ;
@@ -325,7 +311,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
325311 clearTimeout ( this . pendingConnectionData . socketTimer ) ;
326312 }
327313
328- reject ( lastKnownError || new Error ( "Socket connection timed out." ) ) ;
314+ reject ( lastKnownError || new Error ( AndroidLivesyncTool . SOCKET_CONNECTION_TIMED_OUT_ERROR ) ) ;
329315 this . pendingConnectionData = null ;
330316 }
331317 } , timeout ) ;
@@ -478,5 +464,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
478464 clearTimeout ( operationPromise . timeoutId ) ;
479465 } ) ;
480466 }
467+
468+ private async writeToSocket ( data : Buffer ) : Promise < Boolean > {
469+ this . verifyActiveConnection ( ) ;
470+ const result = await this . socketConnection . writeAsync ( data ) ;
471+ return result ;
472+ }
481473}
482474$injector . register ( "androidLivesyncTool" , AndroidLivesyncTool ) ;
0 commit comments