@@ -19,10 +19,15 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
19
19
public static OPERATION_END_NO_REFRESH_REPORT_CODE = 3 ;
20
20
public static DO_REFRESH = 1 ;
21
21
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." ;
22
27
public protocolVersion : string ;
23
28
private operationPromises : IDictionary < any > ;
24
29
private socketError : string | Error ;
25
- private socketConnection : INetSocket ;
30
+ private socketConnection : ILiveSyncSocket ;
26
31
private configuration : IAndroidLivesyncToolConfiguration ;
27
32
private pendingConnectionData : {
28
33
connectionTimer ?: NodeJS . Timer ,
@@ -46,15 +51,15 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
46
51
47
52
public async connect ( configuration : IAndroidLivesyncToolConfiguration ) : Promise < void > {
48
53
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 ) ;
50
55
}
51
56
52
57
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 ) ;
54
59
}
55
60
56
61
if ( this . socketConnection ) {
57
- this . $errors . fail ( "Socket connection already exists." ) ;
62
+ this . $errors . fail ( AndroidLivesyncTool . SOCKET_CONNECTION_ALREADY_EXISTS_ERROR ) ;
58
63
}
59
64
60
65
if ( ! configuration . localHostAddress ) {
@@ -98,9 +103,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
98
103
return this . sendFiles ( list ) ;
99
104
}
100
105
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 ( ) ;
104
108
const filePathData = this . getFilePathData ( filePath ) ;
105
109
const headerBuffer = Buffer . alloc ( PROTOCOL_OPERATION_LENGTH_SIZE +
106
110
SIZE_BYTE_LENGTH +
@@ -114,11 +118,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
114
118
headerBuffer . write ( filePathData . relativeFilePath , offset , filePathData . filePathLengthBytes ) ;
115
119
const hash = crypto . createHash ( "md5" ) . update ( headerBuffer ) . digest ( ) ;
116
120
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 ) ;
122
123
}
123
124
124
125
public removeFiles ( files : string [ ] ) {
@@ -133,10 +134,14 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
133
134
return ! ! this . operationPromises [ operationId ] ;
134
135
}
135
136
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 ;
137
140
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
+ }
140
145
const message = `${ AndroidLivesyncTool . DO_SYNC_OPERATION } ${ id } ` ;
141
146
const headerBuffer = Buffer . alloc ( Buffer . byteLength ( message ) + DO_REFRESH_LENGTH ) ;
142
147
const socketId = this . socketConnection . uid ;
@@ -145,11 +150,10 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
145
150
146
151
headerBuffer . writeUInt8 ( doRefreshCode , offset ) ;
147
152
const hash = crypto . createHash ( "md5" ) . update ( headerBuffer ) . digest ( ) ;
153
+ this . writeToSocket ( headerBuffer ) . then ( ( ) => {
154
+ this . writeToSocket ( hash ) . catch ( reject ) ;
155
+ } ) . catch ( reject ) ;
148
156
149
- this . socketConnection . write ( headerBuffer ) ;
150
- this . socketConnection . write ( hash ) ;
151
-
152
- timeout = timeout || SYNC_OPERATION_TIMEOUT ;
153
157
const timeoutId = setTimeout ( ( ) => {
154
158
if ( this . isOperationInProgress ( id ) ) {
155
159
this . handleSocketError ( socketId , "Sync operation is taking too long" ) ;
@@ -186,10 +190,8 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
186
190
return ! ! this . socketConnection ;
187
191
}
188
192
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 ( ) ;
193
195
const filePathData = this . getFilePathData ( filePath ) ;
194
196
const stats = this . $fs . getFsStats ( filePathData . filePath ) ;
195
197
const fileContentLengthBytes = stats . size ;
@@ -203,13 +205,9 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
203
205
fileContentLengthSize ) ;
204
206
205
207
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." ) ;
207
209
} 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." ) ;
213
211
}
214
212
215
213
let offset = 0 ;
@@ -221,56 +219,42 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
221
219
headerBuffer . write ( fileContentLengthString , offset , fileContentLengthSize ) ;
222
220
const hash = crypto . createHash ( "md5" ) . update ( headerBuffer ) . digest ( ) ;
223
221
224
- this . socketConnection . write ( headerBuffer ) ;
225
- this . socketConnection . write ( hash ) ;
226
- resolve ( ) ;
227
- } ) ;
222
+ await this . writeToSocket ( headerBuffer ) ;
223
+ await this . writeToSocket ( hash ) ;
228
224
}
229
225
230
226
private sendFileContent ( filePath : string ) : Promise < boolean > {
231
227
return new Promise ( ( resolve , reject ) => {
232
- this . verifyActiveConnection ( reject ) ;
228
+ if ( ! this . verifyActiveConnection ( reject ) ) {
229
+ return ;
230
+ }
231
+
233
232
const fileStream = this . $fs . createReadStream ( filePath ) ;
234
233
const fileHash = crypto . createHash ( "md5" ) ;
235
234
236
235
fileStream
237
- . on ( "data" , ( chunk : string | Buffer ) => {
236
+ . on ( "data" , ( chunk : Buffer ) => {
238
237
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 ) ;
248
239
} )
249
240
. 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 ) ;
258
242
} )
259
243
. on ( "error" , ( error : Error ) => {
260
244
reject ( error ) ;
261
245
} ) ;
262
246
} ) ;
263
247
}
264
248
265
- private createSocket ( port : number ) : INetSocket {
249
+ private createSocket ( port : number ) : ILiveSyncSocket {
266
250
const socket = this . $injector . resolve ( "LiveSyncSocket" ) ;
267
251
socket . connect ( port , this . configuration . localHostAddress ) ;
268
252
return socket ;
269
253
}
270
254
271
255
private checkConnectionStatus ( ) {
272
256
if ( this . socketConnection === null ) {
273
- const defaultError = this . getErrorWithMessage ( "No socket connection available." ) ;
257
+ const defaultError = this . getErrorWithMessage ( AndroidLivesyncTool . NO_SOCKET_CONNECTION_AVAILABLE_ERROR ) ;
274
258
const error = this . socketError || defaultError ;
275
259
276
260
return error ;
@@ -286,9 +270,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
286
270
if ( error && ! rejectHandler ) {
287
271
this . $errors . failWithoutHelp ( error . toString ( ) ) ;
288
272
}
273
+
274
+ return true ;
289
275
}
290
276
291
- private handleConnection ( { socket, data } : { socket : INetSocket , data : NodeBuffer | string } ) {
277
+ private handleConnection ( { socket, data } : { socket : ILiveSyncSocket , data : NodeBuffer | string } ) {
292
278
this . socketConnection = socket ;
293
279
this . socketConnection . uid = this . generateOperationIdentifier ( ) ;
294
280
@@ -313,7 +299,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
313
299
} ) ;
314
300
}
315
301
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 } > {
317
303
return new Promise ( ( resolve , reject ) => {
318
304
let lastKnownError : Error | string ,
319
305
isConnected = false ;
@@ -325,7 +311,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
325
311
clearTimeout ( this . pendingConnectionData . socketTimer ) ;
326
312
}
327
313
328
- reject ( lastKnownError || new Error ( "Socket connection timed out." ) ) ;
314
+ reject ( lastKnownError || new Error ( AndroidLivesyncTool . SOCKET_CONNECTION_TIMED_OUT_ERROR ) ) ;
329
315
this . pendingConnectionData = null ;
330
316
}
331
317
} , timeout ) ;
@@ -478,5 +464,11 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
478
464
clearTimeout ( operationPromise . timeoutId ) ;
479
465
} ) ;
480
466
}
467
+
468
+ private async writeToSocket ( data : Buffer ) : Promise < Boolean > {
469
+ this . verifyActiveConnection ( ) ;
470
+ const result = await this . socketConnection . writeAsync ( data ) ;
471
+ return result ;
472
+ }
481
473
}
482
474
$injector . register ( "androidLivesyncTool" , AndroidLivesyncTool ) ;
0 commit comments