@@ -29,7 +29,7 @@ const FLAG_DIRECTORY = 0x01;
29
29
const BYTES_PER_WRITE = 20 ;
30
30
31
31
class FileTransferClient {
32
- constructor ( bleDevice , bufferSize = 4096 ) {
32
+ constructor ( bleDevice , bufferSize = 4096 ) {
33
33
this . _resolve = null ;
34
34
this . _reject = null ;
35
35
this . _command = ANY_COMMAND ;
@@ -38,6 +38,7 @@ class FileTransferClient {
38
38
this . _buffer = new Uint8Array ( bufferSize ) ;
39
39
this . _transfer = null ;
40
40
this . _device = bleDevice ;
41
+ this . _raw = false ;
41
42
bleDevice . addEventListener ( "gattserverdisconnected" , this . onDisconnected . bind ( this ) ) ;
42
43
this . _onTransferNotifty = this . onTransferNotifty . bind ( this ) ;
43
44
}
@@ -75,7 +76,7 @@ class FileTransferClient {
75
76
this . _transfer . removeEventListener ( 'characteristicvaluechanged' , this . _onTransferNotifty ) ;
76
77
this . _transfer . addEventListener ( 'characteristicvaluechanged' , this . _onTransferNotifty ) ;
77
78
await this . _transfer . startNotifications ( ) ;
78
- } catch ( e ) {
79
+ } catch ( e ) {
79
80
console . log ( "caught connection error" , e , e . stack ) ;
80
81
this . onDisconnected ( ) ;
81
82
}
@@ -108,34 +109,35 @@ class FileTransferClient {
108
109
}
109
110
110
111
async onTransferNotifty ( event ) {
111
- this . _buffer . set ( new Uint8Array ( event . target . value . buffer ) , this . _offset ) ;
112
- this . _command = this . _buffer [ 0 ] ;
113
- this . _offset += event . target . value . byteLength ;
114
- if ( this . _command == READ_DATA ) {
115
- this . _command = await this . processReadData ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
116
- } else if ( this . _command == WRITE_PACING ) {
117
- this . _command = await this . processWritePacing ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
118
- } else if ( this . _command == LISTDIR_ENTRY ) {
119
- this . _command = await this . processListDirEntry ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
120
- } else if ( this . _command == MKDIR_STATUS ) {
121
- this . _command = await this . processMkDirStatus ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
122
- } else if ( this . _command == DELETE_STATUS ) {
123
- this . _command = await this . processDeleteStatus ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
124
- } else if ( this . _command == MOVE_STATUS ) {
125
- this . _command = await this . processMoveStatus ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
126
- } else {
127
- console . log ( "Unknown Command: " + this . _command ) ;
128
- }
129
- if ( this . _command != THIS_COMMAND ) {
130
- //reset buffer
131
- this . _offset = 0 ;
132
- }
112
+ this . _buffer . set ( new Uint8Array ( event . target . value . buffer ) , this . _offset ) ;
113
+ this . _command = this . _buffer [ 0 ] ;
114
+ this . _offset += event . target . value . byteLength ;
115
+ if ( this . _command == READ_DATA ) {
116
+ this . _command = await this . processReadData ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
117
+ } else if ( this . _command == WRITE_PACING ) {
118
+ this . _command = await this . processWritePacing ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
119
+ } else if ( this . _command == LISTDIR_ENTRY ) {
120
+ this . _command = await this . processListDirEntry ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
121
+ } else if ( this . _command == MKDIR_STATUS ) {
122
+ this . _command = await this . processMkDirStatus ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
123
+ } else if ( this . _command == DELETE_STATUS ) {
124
+ this . _command = await this . processDeleteStatus ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
125
+ } else if ( this . _command == MOVE_STATUS ) {
126
+ this . _command = await this . processMoveStatus ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
127
+ } else {
128
+ console . log ( "Unknown Command: " + this . _command ) ;
129
+ }
130
+ if ( this . _command != THIS_COMMAND ) {
131
+ //reset buffer
132
+ this . _offset = 0 ;
133
+ }
133
134
}
134
135
135
- async readFile ( filename ) {
136
+ async readFile ( filename , raw = false ) {
136
137
await this . checkConnection ( ) ;
137
138
this . _incomingFile = null ;
138
139
this . _incomingOffset = 0 ;
140
+ this . _raw = raw ;
139
141
140
142
var header = new ArrayBuffer ( 12 ) ;
141
143
var view = new DataView ( header ) ;
@@ -157,7 +159,18 @@ class FileTransferClient {
157
159
return p ;
158
160
}
159
161
160
- async writeFile ( path , offset , contents , modificationTime ) {
162
+ async writeFile ( path , offset , contents , modificationTime , raw = false ) {
163
+ let encoder = new TextEncoder ( ) ;
164
+
165
+ if ( ! raw ) {
166
+ let same = contents . slice ( 0 , offset ) ;
167
+ let different = contents . slice ( offset ) ;
168
+ offset = encoder . encode ( same ) . byteLength ;
169
+ contents = encoder . encode ( different ) ;
170
+ } else if ( ! ( contents instanceof Uint8Array ) ) {
171
+ contents = new Uint8Array ( contents ) ;
172
+ }
173
+
161
174
await this . checkConnection ( ) ;
162
175
if ( modificationTime === undefined ) {
163
176
modificationTime = Date . now ( ) ;
@@ -186,7 +199,7 @@ class FileTransferClient {
186
199
}
187
200
188
201
sleep ( ms ) {
189
- return new Promise ( resolve => setTimeout ( resolve , ms ) )
202
+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
190
203
}
191
204
192
205
async processWritePacing ( payload ) {
@@ -207,10 +220,10 @@ class FileTransferClient {
207
220
return ANY_COMMAND ;
208
221
}
209
222
if ( freeSpace == 0 ) {
210
- this . _resolve ( ) ;
211
- this . _reject = null ;
212
- this . _resolve = null ;
213
- return ANY_COMMAND ;
223
+ this . _resolve ( ) ;
224
+ this . _reject = null ;
225
+ this . _resolve = null ;
226
+ return ANY_COMMAND ;
214
227
}
215
228
var header = new ArrayBuffer ( 12 ) ;
216
229
var view = new DataView ( header ) ;
@@ -247,19 +260,23 @@ class FileTransferClient {
247
260
return ANY_COMMAND ;
248
261
}
249
262
if ( payload . byteLength < headerSize + chunkLength ) {
250
- // need more
251
- return THIS_COMMAND ;
263
+ // need more
264
+ return THIS_COMMAND ;
252
265
}
253
266
// full payload
254
267
if ( this . _incomingFile == null ) {
255
- this . _incomingFile = new Uint8Array ( totalLength ) ;
268
+ this . _incomingFile = new Uint8Array ( totalLength ) ;
256
269
}
257
270
this . _incomingFile . set ( new Uint8Array ( payload . buffer . slice ( headerSize , payload . byteLength ) ) , chunkOffset ) ;
258
271
this . _incomingOffset += chunkLength ;
259
272
260
273
let remaining = this . _incomingFile . byteLength - this . _incomingOffset ;
261
274
if ( remaining == 0 ) {
262
- this . _resolve ( new TextDecoder ( ) . decode ( this . _incomingFile ) ) ;
275
+ if ( this . _raw ) {
276
+ this . _resolve ( new Blob ( [ this . _incomingFile ] ) ) ;
277
+ } else {
278
+ this . _resolve ( new TextDecoder ( ) . decode ( this . _incomingFile ) ) ;
279
+ }
263
280
this . _resolve = null ;
264
281
this . _reject = null ;
265
282
this . _incomingFile = null ;
@@ -277,7 +294,7 @@ class FileTransferClient {
277
294
return READ_DATA ;
278
295
}
279
296
280
- async processListDirEntry ( payload , offset = 0 ) {
297
+ async processListDirEntry ( payload , offset = 0 ) {
281
298
let paths = [ ] ;
282
299
let b = this . _buffer . buffer ;
283
300
const headerSize = 28 ;
@@ -406,14 +423,14 @@ class FileTransferClient {
406
423
this . _reject = null ;
407
424
return ANY_COMMAND ;
408
425
}
409
-
426
+
410
427
async processMoveStatus ( payload ) {
411
428
const headerSize = 2 ;
412
429
413
430
if ( payload . byteLength < headerSize ) {
414
431
return THIS_COMMAND ;
415
432
}
416
-
433
+
417
434
let status = payload . getUint8 ( 1 ) ;
418
435
if ( status != STATUS_OK ) {
419
436
if ( status == STATUS_ERROR_USB_MOUNTED ) {
@@ -499,7 +516,7 @@ class FileTransferClient {
499
516
500
517
let encodedOldPath = new TextEncoder ( ) . encode ( oldPath ) ;
501
518
let encodedNewPath = new TextEncoder ( ) . encode ( newPath ) ;
502
-
519
+
503
520
var header = new ArrayBuffer ( 6 ) ;
504
521
var view = new DataView ( header ) ;
505
522
view . setUint8 ( 0 , MOVE_COMMAND ) ;
@@ -532,4 +549,4 @@ class ValueError extends Error {
532
549
}
533
550
}
534
551
535
- export { FileTransferClient , ProtocolError , ValueError } ;
552
+ export { FileTransferClient , ProtocolError , ValueError } ;
0 commit comments