forked from Sefaria/Sefaria-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadControl.js
1193 lines (1088 loc) · 41 KB
/
DownloadControl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
import {FileSystem} from 'react-native-unimodules'
import {unzip} from 'react-native-zip-archive'; //for unzipping -- (https://github.com/plrthink/react-native-zip-archive)
import strings from './LocalizedStrings'
import {Alert, Platform} from 'react-native';
import AsyncStorage from "@react-native-async-storage/async-storage";
import NetInfo from "@react-native-community/netinfo";
import crashlytics from '@react-native-firebase/crashlytics';
const SCHEMA_VERSION = "6";
// const DOWNLOAD_SERVER = "http://10.0.2.2:5000" // this ip will allow the android emulator to access a localhost server
const DOWNLOAD_SERVER = "https://readonly.sefaria.org";
const HOST_PATH = `${DOWNLOAD_SERVER}/static/ios-export/${SCHEMA_VERSION}`;
const HOST_BUNDLE_URL = `${HOST_PATH}/bundles`;
const [FILE_DIRECTORY, TMP_DIRECTORY] = [`${FileSystem.documentDirectory}library`, `${FileSystem.cacheDirectory}tmp`]; //todo: make sure these are used
let BooksState = {}; // maps titles to Book objects
let PackagesState = {}; // maps package titles to Package objects
async function simpleDelete(filePath) {
try{
await FileSystem.deleteAsync(encodeURI(filePath));
} catch (e) { console.warn(e) }
}
/*
* A note on package tracking:
* The PackagesState object contains detailed data on packages. This object is a combination of the file packages.json
* and the data stored in AsyncStorage under the key packagesSelected.
* packagesSelected is a simple mapping of packageName -> bool
*/
function selectedPackages() {
// Test with Jest
let selections = {};
for (let [packageTitle, packObj] of Object.entries(PackagesState)) {
if (packObj.wasSelectedByUser())
selections[packageTitle] = true;
}
return selections
}
function createCancelMethods() {
/*
* These methods are here so that async methods and promises can set up their own cancel method.
*/
let cancelMethods = [];
const addCancelMethod = value => cancelMethods.push(value);
const resetCancelMethods = () => cancelMethods = [];
const runCancelMethods = () => cancelMethods.map(x => x());
return {addCancelMethod, resetCancelMethods, runCancelMethods}
}
const {addCancelMethod, resetCancelMethods, runCancelMethods} = createCancelMethods();
/*
* A word on design - DownloadTracker was originally supposed to implement the Observer pattern (Pub - Sub). As the
* complexity of the project grew, DownloadTracker got a little over-sized. It is now behaving as a semaphore, keeping
* track of who can start a download as well as what downloads have happened in the past. A future project might examine
* breaking out the responsibilities of this class.
*/
class DownloadTracker {
// Test with Jest
constructor() {
this.currentDownload = null;
this.subscriptions = {};
this.removeProgressTracker(); // sets default values for the progress event listener
this.networkEventListener = null;
this._alreadyDownloaded = 0;
this._downloadSession = null;
this.recoverLock = false;
this.arrayDownloadState = {
currentDownload: 0,
totalDownloads: 0,
downloadAllowed: false
};
this.progressListener = null;
this.downloadSize = 0;
this.downloadSavable = null;
}
addDownload(downloadState) {
if (this.downloadInProgress()) {
throw "Another download is in Progress!"
}
this.currentDownload = downloadState;
// if (!!this.progressTracker) {
// const [tracker, config] = this.progressTracker;
// downloadState.progress(config, tracker);
// }
}
removeDownload(endSession=false) {
this.currentDownload = null;
if (endSession) {
this.removeDownloadSession().then(() => {})
} else {
this.updateSession({downloadActive: false})
}
}
downloadInProgress() {
return (!!this.currentDownload);
}
cancelDownload(cleanup=true) {
// kill any async methods or promises that need to be stopped due to to downloads being canceled.
runCancelMethods();
resetCancelMethods();
// if (this.downloadInProgress()) {
// this.currentDownload.catch(e => {});
// }
if (!this.downloadInProgress()) {
this.removeDownload(cleanup);
if (cleanup) { cleanTmpDirectory().then( () => {} ) }
return
}
if (cleanup) {
this.currentDownload.pauseAsync().then(() => {
this.downloadSavable = null;
this.removeDownload(true);
cleanTmpDirectory().then( () => {} )
});
} else {
this.currentDownload.pauseAsync().then(x => {
this.downloadSavable = x;
this.removeDownload(false);
});
}
}
attachProgressTracker(progressTracker, identity) {
// todo rnfb refactor -> config is a thing from rnfb, we can get rid of it. identity is for react components to subscribe / unsubscribe
const enhancedProgressTracker = (received, total) => {
let [trueReceived, trueTotal] = [parseInt(received) + parseInt(this._alreadyDownloaded),
parseInt(total) + parseInt(this._alreadyDownloaded)];
let [numDownloads, totalDownloads] =
this.arrayDownloadState ? [this.arrayDownloadState.currentDownload, this.arrayDownloadState.totalDownloads]
: [0, 1];
totalDownloads = totalDownloads >= 1 ? parseInt(totalDownloads) : 1;
// the following is just algebraic expansion of an expression which adjust the progress to account for an array of downloads
return progressTracker((trueReceived + numDownloads * trueTotal) / totalDownloads , trueTotal)
};
this.progressListener = identity;
this.progressTracker = enhancedProgressTracker;
}
removeProgressTracker(identity) {
if (identity !== this.progressListener) {
return
}
this.progressListener = null;
const dummyLogger = (received, total) => { console.log(`downloaded: ${received}/${total}`) };
this.attachProgressTracker(dummyLogger, 'dummy');
}
subscribe(listenerName, listenerFunc) {
/*
* add a function that will be updated when downloads begin and finish. Function should accept a boolean. A
* name must be supplied as well. This will avoid a single method subscribing multiple times, as well as allowing
* for unsubscribing.
*/
this.subscriptions[listenerName] = listenerFunc;
}
unsubscribe(listenerName) {
delete this.subscriptions[listenerName];
}
notify() {
/*
* At the start of a download we should notify via the method that initiated the download. This should happen from a
* UI component. This allows the UI component to send whatever messages it needs to down to all the components
* listening.
*
* At the end of a download we'll call notify(false) from the DownloadTracker. This is because only the DownloadTracker
* can know when a download has completed
*/
Object.values(this.subscriptions).map(x => {
try{
x(this._downloadSession);
} catch (e) {
crashlytics().log(`notification error: ${e}`);
}
})
}
getDownloadStatus() {
return this._downloadSession;
}
addEventListener(networkSetting, downloadBuffer, runEvent=false) {
/* the netinfo package triggers an event as soon as we attach an event listener
* but that's not necessary - we only want to trigger the change method if the network status actually changed
* we set up a variable so that nothing happens an that initial event
*/
let firstRun = true;
this.removeEventListener(); // keep things clean
const allowedToDownload = state => isDownloadAllowed(state, networkSetting);
const networkChangeMethod = state => {
if (firstRun) {
firstRun = false;
return
}
if (allowedToDownload(state)) {
downloadRecover(networkSetting, downloadBuffer).then(() => {});
} else if (Tracker.downloadInProgress()){
Tracker.cancelDownload(false);
}
};
this.networkEventListener = NetInfo.addEventListener(networkChangeMethod);
if (runEvent) { NetInfo.fetch().then(networkChangeMethod) } // simulates an event so we can trigger the worker manually
}
removeEventListener() {
if (!!this.networkEventListener) {
this.networkEventListener(); // calling the event listener unsubscribes
this.networkEventListener = null;
}
}
hasEventListener() {
return !!this.networkEventListener
}
setAlreadyDownloaded(value) {
this._alreadyDownloaded = value;
}
async startDownloadSession(downloadContent) {
this._downloadSession = {
downloadNotification: downloadContent,
downloadActive: false
};
this.notify();
}
async removeDownloadSession() {
this.arrayDownloadState.downloadAllowed = false;
this._downloadSession = null;
await AsyncStorage.removeItem('lastDownloadLocation');
this.notify();
}
updateSession(updates) {
Object.assign(this._downloadSession, updates);
this.notify();
}
}
const Tracker = new DownloadTracker();
class Package {
// Test with Jest
constructor(jsonData, order=0) {
this.name = jsonData['en'];
this.jsonData = jsonData;
this.children = [];
this.supersededByParent = false;
this.clicked = false;
this.parent = this.getParent();
this.order = order;
}
addChild = function (child) {
this.children.push(child);
};
getParent = function () {
if (this.name === 'COMPLETE LIBRARY') {
return null;
}
else if (!this.jsonData['parent']) {
return 'COMPLETE LIBRARY'
}
else return this.jsonData['parent']
};
_propagateClick(clicked) {
this.children.forEach(child => {
child = PackagesState[child];
[child.clicked, child.supersededByParent] = [clicked, clicked];
child._propagateClick(clicked);
})
}
markAsClicked = async function (writeToDisk=true) {
/*
* we want to correct for bad data. If both a parent and a child are marked as clicked, we need to make sure the
* child is disabled. In the event that a child was marked before it's parent, the `disabled` parameter will trickle
* down when the parent is marked. In case the parent was already marked, we'll look up the parent in
* PackagesState.
*
* We've separated the concerns of marking which packages were clicked and actually downloading packages. Keeping
* this distinction is important both for usability (we can run UI logic between the click and when the download
* starts) and for maintainability.
*/
this.clicked = true;
const parent = PackagesState[this.parent];
const supersededByParent = parent && parent.clicked;
this.supersededByParent = Boolean(supersededByParent);
this._propagateClick(true);
if (writeToDisk)
await AsyncStorage.setItem('packagesSelected', JSON.stringify(selectedPackages()));
await repopulateBooksState()
};
unclick = async function () {
/*
* Set this package as desired = false.
*
* This method does not actually delete the books. Deleting takes some time. For better responsiveness it is
* useful to be call the deleteBooks method explicitly.
*/
if (this.supersededByParent) {
throw "A disabled package cannot be unclicked"
}
this.clicked = false;
this._propagateClick(false);
await AsyncStorage.setItem('packagesSelected', JSON.stringify(selectedPackages()));
// do we want to separate the concerns here? Less of an issue as there is not a network dependency
setDesiredBooks();
return calculateBooksToDelete(BooksState);
};
wasSelectedByUser = function () {
return this.clicked && !this.supersededByParent
}
}
class Book {
constructor(title, desired, localLastUpdated=null) {
this.title = title;
this.desired = desired;
if (!!localLastUpdated)
this.localLastUpdated = new Date(localLastUpdated);
else
this.localLastUpdated = null;
}
}
async function fileExists(filePath) {
// RNFB had an explicit exists method, while Filesystem does not. This is useful for making the refactor simpler
filePath = Platform.OS === "ios" ? encodeURI(filePath) : filePath;
const fileInfo = await FileSystem.getInfoAsync(filePath);
return fileInfo.exists
}
function deriveDownloadState(pkgStateData) {
// Test with Jest
// pkgStateData is the contents of packages.json
const packageResult = {};
pkgStateData.forEach((pkgData, index) => {
packageResult[pkgData['en']] = new Package(pkgData, index);
});
// children are not defined in package.json. Each package points to a parent and we can now use that to set children
let parentPackage;
for (let [packageTitle, packObj] of Object.entries(packageResult)) {
if (packageTitle === 'COMPLETE LIBRARY') {}
else {
parentPackage = packObj.parent;
packageResult[parentPackage].addChild(packageTitle);
}
}
return packageResult
}
async function loadJSONFile(JSONSourcePath) {
if (Platform.OS === "ios") {JSONSourcePath = encodeURI(JSONSourcePath)}
const readResult = await FileSystem.readAsStringAsync(JSONSourcePath);
let parsedResult;
try {
parsedResult = JSON.parse(readResult);
} catch (e) {
parsedResult = {}; // if file can't be parsed, fall back to empty object
}
return parsedResult
}
async function loadCoreFile(filename) {
// Test explicitly with Appium. Platform dependencies are not modeled well with Jest
const exists = await fileExists(`${FILE_DIRECTORY}/${filename}`);
const pkgPath = exists ? `${FILE_DIRECTORY}/${filename}`
: Platform.OS === "ios" ? `${FileSystem.bundleDirectory}/sources/${filename}`
: `${FileSystem.bundleDirectory}sources/${filename}`;
try {
return await loadJSONFile(pkgPath);
} catch (e) {
crashlytics().recordError(e);
}
}
async function lastUpdated() {
const lastUpdatedSource = `${FILE_DIRECTORY}/last_updated.json`;
let exists = await fileExists(lastUpdatedSource);
if (!exists)
{
return null;
}
return await loadJSONFile(lastUpdatedSource);
/*
* In the event the user never downloaded last_updated.json and the download failed we're going to throw out some
* placeholder values. There might be a better way of dealing with this issue.
*/
// return {
// schema_version: SCHEMA_VERSION,
// titles: []
// };
}
function getFullBookList() { // todo: Use Sefaria.cacheIndexFromToc to initialize?
// Test with Jest
return Object.keys(Sefaria.booksDict);
}
async function packageSetupProtocol() {
// addDir adds a directory if it does not exist. We'll set up the app directory here
await addDir(FILE_DIRECTORY);
const [packageData, packagesSelected] = await Promise.all([
loadCoreFile('packages.json').then(pkgStateData => deriveDownloadState(pkgStateData)),
AsyncStorage.getItem('packagesSelected').then(x => !!x ? JSON.parse(x) : {})
]).catch(e => crashlytics().log(`failed in packageSetupProtocol: ${e}`));
PackagesState = packageData;
let falseSelections = [];
// for (let packName of Object.keys(packagesSelected)) {
// packageData[packName].markAsClicked(false)
// } // rewrote these lines as a Promise.all as markAsClicked is async. Untested, so keeping the old code around
await Promise.all(Object.keys(packagesSelected).map(
packName => packageData[packName].markAsClicked(false)
));
for (let packName of Object.keys(packagesSelected)) {
if (!!packageData[packName].supersededByParent) {
falseSelections.push(packName)
}
}
if (falseSelections.length) {
falseSelections.map(x => delete packagesSelected[x]);
try {
// this is here for cleaning up falseSelections on disk
await AsyncStorage.setItem('packagesSelected', JSON.stringify(packagesSelected))
} catch (e) {
throw new Error(`AsyncStorage failed to save: ${error}`);
}
}
await repopulateBooksState();
}
function setDesiredBooks() {
// Test with Jest
if (PackagesState['COMPLETE LIBRARY'].clicked) {
Object.keys(BooksState).forEach(b => BooksState[b].desired = true)
}
else {
// mark all books as not desired as a starting point
Object.values(BooksState).map(x => x.desired=false);
const selectedPackages = Object.values(PackagesState).filter(x => {
/*
* All books in packages marked desired should be flagged as desired. COMPLETE LIBRARY is a special case and
* was dealt with above. Disabled packages are redundant (these are packages that are wholly contained within
* a larger package that was already selected).
*/
return x.name !== 'COMPLETE LIBRARY' && x.wasSelectedByUser()
});
selectedPackages.forEach(packageObj => {
packageObj.jsonData['indexes'].forEach(book => {
if (book in BooksState) {
BooksState[book].desired = true;
}
else { // edge case, new books should not be added here
BooksState[book] = new Book(book, packageObj.clicked, null);
}
})
})
}
}
async function setLocalBookTimestamps(bookTitleList) {
let fileList = await FileSystem.readDirectoryAsync(FILE_DIRECTORY);
fileList = fileList.filter(x => x.endsWith('.zip'));
// todo: we desperately need an lstat method here. Not currently known to be supported by FileSystem
let fileData = await throttlePromiseAll(fileList, x => {
if (Platform.OS === "ios") { x = encodeURI(x) }
return FileSystem.getInfoAsync(`${FILE_DIRECTORY}/${x}`)
}, 10);
const stamps = {};
fileData.forEach((f, i) => {
const bookName = fileList[i].slice(0, -4);
stamps[bookName] = new Date(parseInt(f['modificationTime'] * 1000)); // FileSystem records seconds since the epoch
});
bookTitleList.map(title => {
const timestamp = (title in stamps) ? stamps[title] : null;
if (title in BooksState)
BooksState[title].localLastUpdated = timestamp;
else
BooksState[title] = new Book(title, false, timestamp);
});
}
async function getLocalBookList() {
// This method is for getting the books that are stored on disk
let books;
try {
books = await FileSystem.readDirectoryAsync(FILE_DIRECTORY);
} catch (e) {
crashlytics().error(e);
books = [];
}
const reg = /([^/]+).zip$/;
return books.filter(filename => filename.endsWith(".zip")).map(fileName => reg.exec(fileName)[1]);
}
async function repopulateBooksState() {
BooksState = {};
const allBooks = getFullBookList();
await setLocalBookTimestamps(allBooks);
setDesiredBooks();
return BooksState
}
async function deleteBooks(bookList, shouldCleanTmpDirectory=true) {
// Test with Appium
const deleteBook = async (bookTitle) => {
const filepath = `${FILE_DIRECTORY}/${bookTitle}.zip`;
const exists = true;
if (exists) {
try {
await simpleDelete(filepath);
} catch (e) {
console.log(`Error deleting file: ${e}`)
crashlytics().log(`Error deleting file: ${e}`);
}
}
return bookTitle
};
await throttlePromiseAll(bookList, deleteBook, 50);
bookList.forEach(bookTitle => {
if (bookTitle in BooksState) {
BooksState[bookTitle].localLastUpdated = null;
BooksState[bookTitle].desired = false;
}
});
// deleting should clean out the tmp directory. This will clear out partially completed downloads, so we want to be able to opt out
if (shouldCleanTmpDirectory) { await cleanTmpDirectory(); }
}
async function unzipBundle(bundleSessionLocation) {
// Test with Appium
await addDir(FILE_DIRECTORY);
await unzip(bundleSessionLocation, FILE_DIRECTORY);
}
function timeoutPromise(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
async function requestNewBundle(bookList, badResponseWaitTime=3000) {
/*
* send a post request to the server. This will cause the server to create a new set of zip files specially
* tailored for the specific device.
* This method will continually ping the server until the download is ready (and a 200 is received).
*/
while (true) {
let response = await fetch(`${DOWNLOAD_SERVER}/makeBundle`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({books: bookList})
});
if (response.ok && response.status !== 202) { // 202 is returned when the download file is still under construction
return await response.json();
} else {
if (!response.ok) { console.log('bad status from downloadServer - trying bundle request again'); }
await timeoutPromise(badResponseWaitTime);
}
}
}
function getPackageUrls(packageName) {
const aborter = {abort: false, complete: false}
addCancelMethod(() => aborter.abort = true)
const mainPromise = async pn => {
const response = await fetch(`${DOWNLOAD_SERVER}/packageData?package=${pn}&schema_version=${SCHEMA_VERSION}`);
const json = await response.json();
aborter.complete = true;
return json
}
const rejector = async () => {
do {
await timeoutPromise(100);
if (aborter.abort) { throw 'aborted'}
} while (!aborter.complete)
}
return Promise.race([mainPromise(packageName), rejector()])
}
function isDownloadAllowed(networkState, downloadNetworkSetting) {
if (!networkState.isInternetReachable) { return false }
switch (networkState.type) {
case 'none':
return false;
case 'wifi':
return true;
default:
return downloadNetworkSetting !== 'wifiOnly';
}
}
async function _downloadRecover(networkMode='wifiOnly', downloadBuffer) {
if (Tracker.downloadInProgress()) {
return
}
/*
* Tricky puzzle here. downloadRecover is scheduled by asynchronous events. Therefore, it is possible for more than
* one event to schedule. We check Tracker.downloadInProgress early to avoid running unecessary code. But the actual
* download is only scheduled much further down the line, after MANY asynchronous lines of code. A second
* downloadRecover can get scheduled while this recovery is awaiting a piece of asynchronous code.
*
* My solution is to set a second flag on Tracker. This flag MUST be set before downloadRecover gives up control to
* the event loop (before an `await` or Promise.then is called). This should be used sparingly, as mistakes
* here can create "deadlocks" where the Tracker gets locked but no recover that can release the lock can be scheduled.
*/
const networkState = await NetInfo.fetch();
if (!isDownloadAllowed(networkState, networkMode)) {
return
}
const downloadSavable = Tracker.downloadSavable;
if (!downloadSavable) {
console.log('no savable on Tracker');
return
}
await downloadBundle(downloadSavable.url, networkMode, downloadBuffer, true, downloadSavable);
}
async function downloadBundle(bundleName, networkSetting, downloadBuffer, recoveryMode=false, resumeData='') {
// Test with Appium
// console.log(`expect to see ${
// Object.values(BooksState).filter(b => b.desired).length
// } books after download`);
const getUniqueFilename = () => { // It's not critical to have a truly unique filename
return `${FileSystem.cacheDirectory}/${String(Math.floor(Math.random()*10000000))}.zip`
};
const getDownloadUrl = (bundle) => {
// if (recoveryMode) { return bundleName }
// return `${HOST_BUNDLE_URL}/${encodeURIComponent(bundle)}`
return bundle
};
const [filename, url] = [getUniqueFilename(), getDownloadUrl(bundleName)];
console.log(`downloading ${url}`);
// const downloadState = downloadFilePromise(url, filename, downloadFrom);
const callback = progress => Tracker.progressTracker(progress.totalBytesWritten, progress.totalBytesExpectedToWrite);
const downloadState = recoveryMode
? new FileSystem.createDownloadResumable(
resumeData.url, resumeData.fileUri, resumeData.options, callback, resumeData.resumeData
)
: new FileSystem.createDownloadResumable(
url, filename, {}, callback
);
try {
Tracker.addDownload(downloadState); // todo rnfb refactor -> check all the places the downloadState is being used, make sure api is compatible with new library
Tracker.updateSession({downloadActive: true});
} catch (e) {
console.warn(e);
crashlytics().log(e);
}
let downloadResult;
Tracker.addEventListener(networkSetting, downloadBuffer);
try {
downloadResult = await downloadState.downloadAsync();
} catch (e) {
// don't start again if download "failed" due to user request. Download Removal is handled by the cancel method
if (e.message === 'stream was reset: CANCEL') { return }
else {
/* Try again if download failed; recover will abort if the failure is due to network
*
* The NetInfo package is updated asynchronously. If we run immediately into a recovery download as soon as a
* call to `fetch` fails, the Promise updating NetInfo won't get a chance to resolve. Because of this, it is
* important to schedule the download recovery, rather than running into it directly. Hence the call to setTimeout.
* Further investigation recommended.
*/
console.log('Error when awaiting download Promise:');
console.log(e);
Tracker.removeDownload();
Tracker.downloadSavable = await downloadState.pauseAsync();
setTimeout(async () => {
console.log('scheduled download recovery from downloadBundle');
await downloadRecover(networkSetting, downloadBuffer)
}, 250);
}
return
}
Tracker.removeEventListener();
const status = downloadResult.status;
if (status >= 300 || status < 200) {
crashlytics().log(`Got status ${status} from download server. Full info below`);
// crashlytics().log(downloadResult.info());
// we're going to schedule a recover in the hope that the server will come back up at a later point
Tracker.removeDownload();
setTimeout(async () => {
await downloadRecover(networkSetting, downloadBuffer);
}, 60*1000);
return
}
try {
await postDownload(downloadResult.uri, !recoveryMode);
} catch (e) {
crashlytics().log(e);
Alert.alert(
strings.downloadError,
strings.downloadErrorMessage,
[{text: strings.ok}]
)
}
const nextDownload = downloadBuffer.next();
if (nextDownload.done) {
Tracker.removeDownload(true);
}
else {
Tracker.removeDownload();
nextDownload.value();
}
}
const downloadRecover = async (networkMode, downloadBuffer) => {
if (Tracker.recoverLock) { return }
Tracker.recoverLock = true;
try {
await _downloadRecover(networkMode, downloadBuffer);
} finally {
Tracker.recoverLock = false;
}
};
async function calculateBooksToDownload(booksState) {
// Test with Jest
let timeFudge = null;
const getTimeFudge = () => {
if (!!timeFudge) { return timeFudge }
const [now, later] = [new Date(0), new Date(0)];
later.setHours(later.getHours() + 6);
timeFudge = later - now;
return timeFudge
}
const remoteBookUpdates = await lastUpdated();
if (remoteBookUpdates === null)
{
crashlytics().log('no last_updated.json');
return [];
}
const booksToDownload = [];
Object.keys(booksState).forEach(bookTitle => {
const bookObj = booksState[bookTitle];
if (bookObj.desired) {
if (!bookObj.localLastUpdated) {
booksToDownload.push(bookTitle);
} else {
const [localUpdate, remoteUpdate] = [booksState[bookTitle].localLastUpdated, new Date(remoteBookUpdates.titles[bookTitle])];
// file timestamp will come from the server on certain platforms - we pad a bit as these values can be very similar to those on last_updated.json
if ((remoteUpdate - localUpdate) > getTimeFudge()) {
if (bookTitle === 'Genesis') {
console.log(`Genesis localUpdate: ${localUpdate}; remoteUpdate: ${remoteUpdate}`);
}
booksToDownload.push(bookTitle)
}
}
}
});
return booksToDownload
}
function calculateBooksToDelete(booksState) {
return Object.entries(booksState).filter(
([bookTitle, bookObj]) => !bookObj.desired && !!(bookObj.localLastUpdated)
).map(([bookTitle, bookObj]) => bookTitle);
}
async function cleanTmpDirectory() {
let files = [];
try {
files = await FileSystem.readDirectoryAsync(TMP_DIRECTORY);
} catch (e) { // if for whatever reason TMP_DIRECTORY doesn't exist, we can just exit now
return
}
await Promise.all(files.map(f => simpleDelete(`${TMP_DIRECTORY}/${f}`)));
}
async function postDownload(downloadPath, newDownload=true) {
const exists = await fileExists(downloadPath);
if (!exists) {
console.log(`file at ${downloadPath} missing`); // todo: we may want to flag a failed download here
return
}
try {
await unzipBundle(downloadPath);
} catch (e) {
// Take to the settings page and check for updates?
crashlytics().log(`Error when unzipping bundle: ${e}`)
}
try {
await simpleDelete(downloadPath)
} catch (e) { console.warn(e) }
await cleanTmpDirectory();
await repopulateBooksState();
Tracker.downloadSavable = null;
console.log(`we have ${
Object.values(BooksState).filter(b => !!b.localLastUpdated).length
} books on disk`)
// -- DEBUG CODE --
// const genesisExists = await fileExists(`${FILE_DIRECTORY}/Genesis.zip`);
// if (genesisExists) {
// const s = await FileSystem.getInfoAsync(`${FILE_DIRECTORY}/Genesis.zip`);
// const genesisAdded = new Date(parseInt(s.lastModified));
// console.log(`Genesis added at ${genesisAdded}. Raw value: ${s.lastModified}`);
// } else { console.log('Genesis file does not exist'); }
// -- END DEBUG --
}
const downloadBlockedNotification = () => {
Alert.alert(
"Download Blocked by Network",
`Current network setting forbids download`,
[{text: strings.ok}]
)
};
async function downloadBundleArray(bundleArray, downloadData, networkSetting) {
/*
* downloadData is an object with the properties:
* {
* currentDownload
* totalDownloads
* downloadAllowed
* }
* All fields will be set here, but as this Object will be created and used elsewhere, proper initialization is
* recommended.
*
* We want to make sure the disk is properly cleaned up before starting a download.
*/
const abortDownload = {abort: false};
addCancelMethod(() => abortDownload.abort = true); // allow this method to be cancelled when downloads are cancelled
const booksToDelete = calculateBooksToDelete(BooksState);
await deleteBooks(booksToDelete);
const buffer = [];
const bufferGen = buffer[Symbol.iterator]();
Object.assign(downloadData, {
currentDownload: -1,
totalDownloads: bundleArray.length,
downloadAllowed: true,
});
/*
* We can't naively iterate over the urls and download each one. We need to take into account that any one download
* process can get interrupted and then restart. Therefore, we want to convert each download into a callback. We'll
* then pass along the list of callbacks so they can be scheduled only after the proceeding download terminated
* successfully.
*/
for (const b of bundleArray) {
buffer.push(async () => {
downloadData.currentDownload += 1;
if (abortDownload.abort) { return }
await downloadBundle(`${DOWNLOAD_SERVER}/${b}`, networkSetting, bufferGen);
});
}
// last method will be resetting the cancel methods and turning off the progress bar
buffer.push(() => {
resetCancelMethods();
Tracker.removeDownload(true);
});
if (abortDownload.abort) { return }
bufferGen.next().value()
// Tracker.removeDownload(true);
// console.log('ending download')
}
async function downloadPackage(packageName, networkSetting) {
const aborter = {abort: false}
addCancelMethod(() => aborter.abort=true)
let packageSize = 0;
try {
packageSize = PackagesState[packageName].jsonData.size;
} catch (e) {
console.log(e);
}
Tracker.downloadSize = packageSize;
await schemaCheckAndPurge(); // necessary for maintaining consistency
const netState = await NetInfo.fetch();
if (!isDownloadAllowed(netState, networkSetting)) {
downloadBlockedNotification();
return
} // todo: review: should notification to the user be sent for a forbidden download?
let bundles;
try {
bundles = await getPackageUrls(packageName);
} catch (e) { return }
bundles = bundles.map(u => encodeURIComponent(u));
if (aborter.abort) { return }
await downloadBundleArray(bundles, Tracker.arrayDownloadState, networkSetting);
// bundles.map(async b => {await downloadBundle(`${DOWNLOAD_SERVER}/${b}`, networkSetting)});
// await downloadBundle(`${packageName}.zip`, networkSetting)
}
async function downloadUpdate(networkSetting, triggeredByUser=true, booksToDownload=null) {
// Test with Appium
const aborter = {abort: false};
addCancelMethod(() => aborter.abort = true);
const netState = await NetInfo.fetch();
if (!isDownloadAllowed(netState, networkSetting)) {
if (triggeredByUser) { downloadBlockedNotification(); }
return
}
if (!booksToDownload) {
booksToDownload = await calculateBooksToDownload(BooksState);
}
if (!booksToDownload.length) { return }
console.log('requesting new bundle');
await Tracker.startDownloadSession('Update');
const bundles = await requestNewBundle(booksToDownload);
if (Tracker.downloadInProgress()) { // before starting the process, double check that another one wasn't triggered
Tracker.cancelDownload(true); // we'll want to abort everything, this state should be illegal
return
}
Tracker.downloadSize = bundles.downloadSize;
if (aborter.abort) {
Tracker.cancelDownload(true);
return
}
await downloadBundleArray(bundles['bundleArray'], Tracker.arrayDownloadState, networkSetting);
}
function wereBooksDownloaded() {
// Test with Jest
return Object.values(PackagesState).some(x => !!x.clicked)
}
function booksNotDownloaded(bookList) {
// Test with Jest
/*
* check a list of book titles against the BooksState to see which books in the list were not downloaded.
* !IMPORTANT! Book data should only be derived from the files saved on disk. This method should be used only for
* telegraphing information to the user, state should not be changed based on the results of this method.
*/
return bookList.filter(x => !(x in BooksState) || !(BooksState[x].localLastUpdated))
}
async function markLibraryForDeletion() {
// Test with Appium
return await PackagesState['COMPLETE LIBRARY'].unclick()
}
async function addDir(path) {
const exists = await fileExists(path);
if (!exists) {
try {
await FileSystem.makeDirectoryAsync(path);
} catch(e) {
crashlytics().log(`Could not create directory at ${path}; ${e}`);
}
}
}
async function downloadCoreFile(filename) {
// Test with Appium
await Promise.all([
addDir(TMP_DIRECTORY),
addDir(FILE_DIRECTORY),
]);
const [fileUrl, tempPath] = [`${HOST_PATH}/${encodeURIComponent(filename)}`, `${TMP_DIRECTORY}/${filename}`];
let downloadResp = null;
try{
downloadResp = await FileSystem.downloadAsync(fileUrl, tempPath);
} catch (e) {
crashlytics().log(e);
return
}
const status = !!downloadResp ? downloadResp.status : 'total failure';
if ((status >= 300 || status < 200) || (status === 'total failure')) {
await simpleDelete(tempPath);
const e = new Error(`bad download status; got : ${status} from ${fileUrl}`);
crashlytics().recordError(e);
throw e // todo: review. Should we alert the user here?
}
else {
}
try {