@kesha-antonov/react-native-background-downloader 4.6.4 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -30
- package/android/src/main/java/com/eko/DownloadConstants.kt +18 -2
- package/android/src/main/java/com/eko/DownloadResult.kt +3 -1
- package/android/src/main/java/com/eko/Downloader.kt +37 -9
- package/android/src/main/java/com/eko/ProgressReporter.kt +14 -35
- package/android/src/main/java/com/eko/RNBGDUploadTaskConfig.kt +36 -1
- package/android/src/main/java/com/eko/RNBackgroundDownloaderModuleImpl.kt +243 -117
- package/android/src/main/java/com/eko/ResumableDownloadService.kt +2 -1
- package/android/src/main/java/com/eko/ResumableDownloader.kt +109 -16
- package/android/src/main/java/com/eko/UIDTDownloadJobService.kt +34 -8
- package/android/src/main/java/com/eko/UnmeteredNetworkGate.kt +134 -17
- package/android/src/main/java/com/eko/UploadEventEmitter.kt +26 -1
- package/android/src/main/java/com/eko/Uploader.kt +202 -28
- package/android/src/main/java/com/eko/handlers/OnBegin.kt +6 -1
- package/android/src/main/java/com/eko/handlers/OnProgress.kt +1 -1
- package/android/src/main/java/com/eko/uidt/CancelDownloadReceiver.kt +3 -1
- package/android/src/main/java/com/eko/uidt/UIDTJobManager.kt +18 -2
- package/android/src/main/java/com/eko/utils/HeaderUtils.kt +9 -0
- package/android/src/main/java/com/eko/utils/RedirectResolver.kt +32 -12
- package/android/src/main/java/com/eko/utils/StorageManager.kt +5 -0
- package/android/src/test/java/com/eko/DownloadCompleteReceiverTest.kt +57 -0
- package/android/src/test/java/com/eko/DownloaderRecoveryTest.kt +78 -0
- package/android/src/test/java/com/eko/DownloaderResumeTest.kt +97 -0
- package/android/src/test/java/com/eko/ResumableDownloaderRedirectTest.kt +77 -0
- package/android/src/test/java/com/eko/ResumableDownloaderStallTest.kt +145 -0
- package/android/src/test/java/com/eko/UnmeteredNetworkGateTest.kt +248 -0
- package/android/src/test/java/com/eko/UploadPartRangesTest.kt +33 -0
- package/android/src/test/java/com/eko/UploaderMultipartTest.kt +93 -0
- package/android/src/test/java/com/eko/UploaderPartsTest.kt +222 -0
- package/android/src/test/java/com/eko/UploaderResponseTest.kt +26 -0
- package/android/src/test/java/com/eko/handlers/OnBeginTest.kt +51 -0
- package/android/src/test/java/com/eko/uidt/CancelDownloadReceiverTest.kt +84 -0
- package/android/src/test/java/com/eko/utils/RedirectResolverTest.kt +133 -0
- package/ios/RNBGDTaskConfig.h +2 -0
- package/ios/RNBGDTaskConfig.mm +6 -0
- package/ios/RNBGDUploadTaskConfig.h +26 -0
- package/ios/RNBGDUploadTaskConfig.mm +69 -3
- package/ios/RNBackgroundDownloader.mm +1203 -133
- package/lib/DownloadTask.d.ts +2 -0
- package/lib/DownloadTask.js +17 -1
- package/lib/NativeRNBackgroundDownloader.d.ts +15 -0
- package/lib/UploadTask.d.ts +7 -1
- package/lib/UploadTask.js +38 -4
- package/lib/index.d.ts +1 -1
- package/lib/index.js +105 -61
- package/lib/types.d.ts +33 -2
- package/package.json +14 -11
- package/plugin/build/index.d.ts +1 -0
- package/plugin/build/index.js +18 -10
- package/react-native-background-downloader.podspec +8 -0
- package/src/DownloadTask.ts +18 -1
- package/src/NativeRNBackgroundDownloader.ts +23 -0
- package/src/UploadTask.ts +41 -2
- package/src/index.ts +131 -66
- package/src/types.ts +42 -2
package/lib/DownloadTask.d.ts
CHANGED
|
@@ -12,11 +12,13 @@ export declare class DownloadTask {
|
|
|
12
12
|
progressHandler?: ProgressHandler;
|
|
13
13
|
doneHandler?: DoneHandler;
|
|
14
14
|
errorHandler?: ErrorHandler;
|
|
15
|
+
private hasBegun;
|
|
15
16
|
constructor(taskParams: TaskInfo | TaskInfoNative, originalTask?: DownloadTaskType);
|
|
16
17
|
begin(handler: BeginHandler): this;
|
|
17
18
|
progress(handler: ProgressHandler): this;
|
|
18
19
|
done(handler: DoneHandler): this;
|
|
19
20
|
error(handler: ErrorHandler): this;
|
|
21
|
+
private isInactive;
|
|
20
22
|
onBegin(params: BeginHandlerParams): void;
|
|
21
23
|
onProgress(params: ProgressHandlerParams): void;
|
|
22
24
|
onDone(params: DoneHandlerParams): void;
|
package/lib/DownloadTask.js
CHANGED
|
@@ -22,6 +22,9 @@ class DownloadTask {
|
|
|
22
22
|
this.errorCode = 0;
|
|
23
23
|
this.bytesDownloaded = 0;
|
|
24
24
|
this.bytesTotal = 0;
|
|
25
|
+
// begin fires once per task. Android sends it again for each new transfer (a resume in
|
|
26
|
+
// a new job, a reschedule by the system), which isn't a new beginning for the app.
|
|
27
|
+
this.hasBegun = false;
|
|
25
28
|
this.id = taskParams.id;
|
|
26
29
|
if (taskParams.bytesDownloaded)
|
|
27
30
|
this.bytesDownloaded = taskParams.bytesDownloaded;
|
|
@@ -30,6 +33,8 @@ class DownloadTask {
|
|
|
30
33
|
if (taskParams.destination)
|
|
31
34
|
this.destination = (_a = taskParams.destination) !== null && _a !== void 0 ? _a : undefined;
|
|
32
35
|
this.metadata = (_b = this.tryParseJson(taskParams.metadata)) !== null && _b !== void 0 ? _b : {};
|
|
36
|
+
// A restored task that already has data has begun; so has one this replaces
|
|
37
|
+
this.hasBegun = (originalTask instanceof DownloadTask && originalTask.hasBegun) || this.bytesDownloaded > 0;
|
|
33
38
|
if (originalTask) {
|
|
34
39
|
this.beginHandler = originalTask.beginHandler;
|
|
35
40
|
this.progressHandler = originalTask.progressHandler;
|
|
@@ -63,14 +68,24 @@ class DownloadTask {
|
|
|
63
68
|
return this;
|
|
64
69
|
}
|
|
65
70
|
// event listeners
|
|
71
|
+
// Native can still deliver begin or progress that was in flight when the app paused
|
|
72
|
+
// or stopped the task, or when it ended. Those must not run handlers or change state.
|
|
73
|
+
isInactive() {
|
|
74
|
+
return this.state === 'PAUSED' || this.state === 'STOPPED' || this.state === 'DONE' || this.state === 'FAILED';
|
|
75
|
+
}
|
|
66
76
|
onBegin(params) {
|
|
67
77
|
var _a;
|
|
78
|
+
if (this.isInactive() || this.hasBegun)
|
|
79
|
+
return;
|
|
80
|
+
this.hasBegun = true;
|
|
68
81
|
this.state = 'DOWNLOADING';
|
|
69
82
|
this.bytesTotal = params.expectedBytes;
|
|
70
83
|
(_a = this.beginHandler) === null || _a === void 0 ? void 0 : _a.call(this, params);
|
|
71
84
|
}
|
|
72
85
|
onProgress(params) {
|
|
73
86
|
var _a;
|
|
87
|
+
if (this.isInactive())
|
|
88
|
+
return;
|
|
74
89
|
this.bytesDownloaded = params.bytesDownloaded;
|
|
75
90
|
this.bytesTotal = params.bytesTotal;
|
|
76
91
|
(_a = this.progressHandler) === null || _a === void 0 ? void 0 : _a.call(this, params);
|
|
@@ -85,6 +100,7 @@ class DownloadTask {
|
|
|
85
100
|
onError(params) {
|
|
86
101
|
var _a;
|
|
87
102
|
this.state = 'FAILED';
|
|
103
|
+
this.errorCode = params.errorCode;
|
|
88
104
|
(_a = this.errorHandler) === null || _a === void 0 ? void 0 : _a.call(this, params);
|
|
89
105
|
}
|
|
90
106
|
// methods
|
|
@@ -141,7 +157,7 @@ class DownloadTask {
|
|
|
141
157
|
...this.downloadParams,
|
|
142
158
|
headers: this.headersToUnsafeObject(this.downloadParams.headers),
|
|
143
159
|
isAllowedOverRoaming: (_c = this.downloadParams.isAllowedOverRoaming) !== null && _c !== void 0 ? _c : false,
|
|
144
|
-
isAllowedOverMetered: (_d = this.downloadParams.isAllowedOverMetered) !== null && _d !== void 0 ? _d :
|
|
160
|
+
isAllowedOverMetered: (_d = this.downloadParams.isAllowedOverMetered) !== null && _d !== void 0 ? _d : config_1.config.allowsCellularAccess,
|
|
145
161
|
// iOS-only; ignored on Android. Falls back to the global setConfig default.
|
|
146
162
|
iosDataProtection: (_e = this.downloadParams.iosDataProtection) !== null && _e !== void 0 ? _e : config_1.config.iosDataProtection,
|
|
147
163
|
});
|
|
@@ -31,10 +31,19 @@ export type UploadProgressEvent = {
|
|
|
31
31
|
bytesUploaded: number;
|
|
32
32
|
bytesTotal: number;
|
|
33
33
|
};
|
|
34
|
+
export type UploadPartResult = {
|
|
35
|
+
partNumber: number;
|
|
36
|
+
responseCode: number;
|
|
37
|
+
responseBody: string;
|
|
38
|
+
responseHeaders: UnsafeObject;
|
|
39
|
+
bytes: number;
|
|
40
|
+
};
|
|
34
41
|
export type UploadCompleteEvent = {
|
|
35
42
|
id: string;
|
|
36
43
|
responseCode: number;
|
|
37
44
|
responseBody: string;
|
|
45
|
+
responseHeaders?: UnsafeObject;
|
|
46
|
+
parts?: UploadPartResult[];
|
|
38
47
|
bytesUploaded: number;
|
|
39
48
|
bytesTotal: number;
|
|
40
49
|
};
|
|
@@ -102,6 +111,8 @@ export interface Spec extends TurboModule {
|
|
|
102
111
|
fieldName?: string;
|
|
103
112
|
mimeType?: string;
|
|
104
113
|
parameters?: UnsafeObject;
|
|
114
|
+
partUrls?: string[];
|
|
115
|
+
partSize?: number;
|
|
105
116
|
isAllowedOverRoaming: boolean;
|
|
106
117
|
isAllowedOverMetered: boolean;
|
|
107
118
|
}): void;
|
|
@@ -115,6 +126,10 @@ export interface Spec extends TurboModule {
|
|
|
115
126
|
bytesUploaded: number;
|
|
116
127
|
bytesTotal: number;
|
|
117
128
|
errorCode?: number | null;
|
|
129
|
+
responseCode?: number | null;
|
|
130
|
+
responseBody?: string | null;
|
|
131
|
+
responseHeaders?: UnsafeObject | null;
|
|
132
|
+
parts?: UploadPartResult[] | null;
|
|
118
133
|
}>>;
|
|
119
134
|
readonly onDownloadBegin: EventEmitter<DownloadBeginEvent>;
|
|
120
135
|
readonly onDownloadProgress: EventEmitter<DownloadProgressEvent[]>;
|
package/lib/UploadTask.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { UploadTaskInfo, UploadTask as UploadTaskType, UploadBeginHandler, UploadProgressHandler, UploadDoneHandler, UploadErrorHandler, UploadBeginHandlerParams, UploadProgressHandlerParams, UploadDoneHandlerParams, UploadErrorHandlerParams, UploadTaskInfoNative, UploadParams, UploadTaskState, Metadata } from './types';
|
|
1
|
+
import { UploadTaskInfo, UploadTask as UploadTaskType, UploadBeginHandler, UploadProgressHandler, UploadDoneHandler, UploadErrorHandler, UploadBeginHandlerParams, UploadProgressHandlerParams, UploadDoneHandlerParams, UploadErrorHandlerParams, UploadTaskInfoNative, UploadParams, UploadTaskState, Metadata, UploadPartResult } from './types';
|
|
2
2
|
export declare class UploadTask {
|
|
3
3
|
id: string;
|
|
4
4
|
metadata: Metadata;
|
|
@@ -7,15 +7,21 @@ export declare class UploadTask {
|
|
|
7
7
|
bytesUploaded: number;
|
|
8
8
|
bytesTotal: number;
|
|
9
9
|
uploadParams?: UploadParams;
|
|
10
|
+
responseCode?: number;
|
|
11
|
+
responseBody?: string;
|
|
12
|
+
responseHeaders?: Record<string, string>;
|
|
13
|
+
parts?: UploadPartResult[];
|
|
10
14
|
beginHandler?: UploadBeginHandler;
|
|
11
15
|
progressHandler?: UploadProgressHandler;
|
|
12
16
|
doneHandler?: UploadDoneHandler;
|
|
13
17
|
errorHandler?: UploadErrorHandler;
|
|
18
|
+
private hasBegun;
|
|
14
19
|
constructor(taskParams: UploadTaskInfo | UploadTaskInfoNative, originalTask?: UploadTaskType);
|
|
15
20
|
begin(handler: UploadBeginHandler): this;
|
|
16
21
|
progress(handler: UploadProgressHandler): this;
|
|
17
22
|
done(handler: UploadDoneHandler): this;
|
|
18
23
|
error(handler: UploadErrorHandler): this;
|
|
24
|
+
private isInactive;
|
|
19
25
|
onBegin(params: UploadBeginHandlerParams): void;
|
|
20
26
|
onProgress(params: UploadProgressHandlerParams): void;
|
|
21
27
|
onDone(params: UploadDoneHandlerParams): void;
|
package/lib/UploadTask.js
CHANGED
|
@@ -22,12 +22,27 @@ class UploadTask {
|
|
|
22
22
|
this.errorCode = 0;
|
|
23
23
|
this.bytesUploaded = 0;
|
|
24
24
|
this.bytesTotal = 0;
|
|
25
|
+
// begin fires once per task. Android sends it again for each new transfer (a resume in
|
|
26
|
+
// a new job, a reschedule by the system), which isn't a new beginning for the app.
|
|
27
|
+
this.hasBegun = false;
|
|
25
28
|
this.id = taskParams.id;
|
|
26
29
|
if (taskParams.bytesUploaded)
|
|
27
30
|
this.bytesUploaded = taskParams.bytesUploaded;
|
|
28
31
|
if (taskParams.bytesTotal)
|
|
29
32
|
this.bytesTotal = taskParams.bytesTotal;
|
|
30
33
|
this.metadata = (_a = this.tryParseJson(taskParams.metadata)) !== null && _a !== void 0 ? _a : {};
|
|
34
|
+
// A finished upload restored by getExistingUploadTasks carries its response
|
|
35
|
+
const nativeInfo = taskParams;
|
|
36
|
+
if (nativeInfo.responseCode != null)
|
|
37
|
+
this.responseCode = nativeInfo.responseCode;
|
|
38
|
+
if (nativeInfo.responseBody != null)
|
|
39
|
+
this.responseBody = nativeInfo.responseBody;
|
|
40
|
+
if (nativeInfo.responseHeaders != null)
|
|
41
|
+
this.responseHeaders = nativeInfo.responseHeaders;
|
|
42
|
+
if (nativeInfo.parts != null)
|
|
43
|
+
this.parts = nativeInfo.parts;
|
|
44
|
+
// A restored task that already has data has begun; so has one this replaces
|
|
45
|
+
this.hasBegun = (originalTask instanceof UploadTask && originalTask.hasBegun) || this.bytesUploaded > 0;
|
|
31
46
|
if (originalTask) {
|
|
32
47
|
this.beginHandler = originalTask.beginHandler;
|
|
33
48
|
this.progressHandler = originalTask.progressHandler;
|
|
@@ -61,14 +76,24 @@ class UploadTask {
|
|
|
61
76
|
return this;
|
|
62
77
|
}
|
|
63
78
|
// event listeners
|
|
79
|
+
// Native can still deliver begin or progress that was in flight when the app paused
|
|
80
|
+
// or stopped the upload, or when it ended. Those must not run handlers or change state.
|
|
81
|
+
isInactive() {
|
|
82
|
+
return this.state === 'PAUSED' || this.state === 'STOPPED' || this.state === 'DONE' || this.state === 'FAILED';
|
|
83
|
+
}
|
|
64
84
|
onBegin(params) {
|
|
65
85
|
var _a;
|
|
86
|
+
if (this.isInactive() || this.hasBegun)
|
|
87
|
+
return;
|
|
88
|
+
this.hasBegun = true;
|
|
66
89
|
this.state = 'UPLOADING';
|
|
67
90
|
this.bytesTotal = params.expectedBytes;
|
|
68
91
|
(_a = this.beginHandler) === null || _a === void 0 ? void 0 : _a.call(this, params);
|
|
69
92
|
}
|
|
70
93
|
onProgress(params) {
|
|
71
94
|
var _a;
|
|
95
|
+
if (this.isInactive())
|
|
96
|
+
return;
|
|
72
97
|
this.bytesUploaded = params.bytesUploaded;
|
|
73
98
|
this.bytesTotal = params.bytesTotal;
|
|
74
99
|
(_a = this.progressHandler) === null || _a === void 0 ? void 0 : _a.call(this, params);
|
|
@@ -78,11 +103,16 @@ class UploadTask {
|
|
|
78
103
|
this.state = 'DONE';
|
|
79
104
|
this.bytesUploaded = params.bytesUploaded;
|
|
80
105
|
this.bytesTotal = params.bytesTotal;
|
|
106
|
+
this.responseCode = params.responseCode;
|
|
107
|
+
this.responseBody = params.responseBody;
|
|
108
|
+
this.responseHeaders = params.responseHeaders;
|
|
109
|
+
this.parts = params.parts;
|
|
81
110
|
(_a = this.doneHandler) === null || _a === void 0 ? void 0 : _a.call(this, params);
|
|
82
111
|
}
|
|
83
112
|
onError(params) {
|
|
84
113
|
var _a;
|
|
85
114
|
this.state = 'FAILED';
|
|
115
|
+
this.errorCode = params.errorCode;
|
|
86
116
|
(_a = this.errorHandler) === null || _a === void 0 ? void 0 : _a.call(this, params);
|
|
87
117
|
}
|
|
88
118
|
// methods
|
|
@@ -109,7 +139,7 @@ class UploadTask {
|
|
|
109
139
|
(0, logger_1.log)('UploadTask: resume not supported - native implementation missing');
|
|
110
140
|
}
|
|
111
141
|
start() {
|
|
112
|
-
var _a, _b, _c, _d, _e, _f;
|
|
142
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
113
143
|
if (this.state !== 'PENDING') {
|
|
114
144
|
(0, logger_1.log)('UploadTask: start. Upload already started, can\' start again... ', this.id);
|
|
115
145
|
(_a = this.errorHandler) === null || _a === void 0 ? void 0 : _a.call(this, { error: 'Upload already started', errorCode: -1 });
|
|
@@ -128,16 +158,20 @@ class UploadTask {
|
|
|
128
158
|
}
|
|
129
159
|
this.state = 'UPLOADING';
|
|
130
160
|
// kick-off upload after returning the task
|
|
161
|
+
const { partUrls } = this.uploadParams;
|
|
131
162
|
nativeModule.upload({
|
|
132
163
|
id: this.id,
|
|
133
164
|
metadata: JSON.stringify(this.metadata),
|
|
134
165
|
progressInterval: config_1.config.progressInterval,
|
|
135
166
|
progressMinBytes: config_1.config.progressMinBytes,
|
|
136
167
|
...this.uploadParams,
|
|
137
|
-
|
|
168
|
+
// The native spec requires a url; with partUrls native uses those instead
|
|
169
|
+
url: (_e = (_d = this.uploadParams.url) !== null && _d !== void 0 ? _d : partUrls === null || partUrls === void 0 ? void 0 : partUrls[0]) !== null && _e !== void 0 ? _e : '',
|
|
170
|
+
// S3 UploadPart (and similar part APIs) take PUT
|
|
171
|
+
method: (_f = this.uploadParams.method) !== null && _f !== void 0 ? _f : (partUrls ? 'PUT' : 'POST'),
|
|
138
172
|
headers: this.headersToUnsafeObject(this.uploadParams.headers),
|
|
139
|
-
isAllowedOverRoaming: (
|
|
140
|
-
isAllowedOverMetered: (
|
|
173
|
+
isAllowedOverRoaming: (_g = this.uploadParams.isAllowedOverRoaming) !== null && _g !== void 0 ? _g : false,
|
|
174
|
+
isAllowedOverMetered: (_h = this.uploadParams.isAllowedOverMetered) !== null && _h !== void 0 ? _h : config_1.config.allowsCellularAccess,
|
|
141
175
|
});
|
|
142
176
|
}
|
|
143
177
|
async stop() {
|
package/lib/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare function cleanup(): void;
|
|
|
10
10
|
export declare function setConfig({ headers, progressInterval, progressMinBytes, isLogsEnabled, logCallback, maxParallelDownloads, allowsCellularAccess, showNotificationsEnabled, showCompletionNotification, showCancelAction, notificationsGrouping, iosDataProtection, }: Config): void;
|
|
11
11
|
export declare const getExistingDownloadTasks: () => Promise<DownloadTask[]>;
|
|
12
12
|
export declare const completeHandler: (jobId: string) => Promise<void> | undefined;
|
|
13
|
-
export declare function createDownloadTask({ isAllowedOverRoaming, isAllowedOverMetered, metadata, ...rest }: TaskInfo & DownloadParams): DownloadTask;
|
|
13
|
+
export declare function createDownloadTask({ isAllowedOverRoaming, isAllowedOverMetered, metadata, groupId, groupName, notificationTitle, ...rest }: TaskInfo & DownloadParams): DownloadTask;
|
|
14
14
|
export declare const getExistingUploadTasks: () => Promise<UploadTask[]>;
|
|
15
15
|
export declare function createUploadTask({ isAllowedOverRoaming, isAllowedOverMetered, metadata, ...rest }: UploadTaskInfo & UploadParams): UploadTask;
|
|
16
16
|
export declare const directories: {
|
package/lib/index.js
CHANGED
|
@@ -24,10 +24,11 @@ let isInitialized = false;
|
|
|
24
24
|
* This prevents issues with module loading before React Native's bridge is ready.
|
|
25
25
|
*/
|
|
26
26
|
function ensureNativeModuleInitialized() {
|
|
27
|
+
var _a;
|
|
27
28
|
if (isInitialized && RNBackgroundDownloader != null)
|
|
28
29
|
return RNBackgroundDownloader;
|
|
29
30
|
// Try TurboModules first
|
|
30
|
-
turboModule = react_native_1.TurboModuleRegistry.get('RNBackgroundDownloader');
|
|
31
|
+
turboModule = (_a = react_native_1.TurboModuleRegistry.get('RNBackgroundDownloader')) !== null && _a !== void 0 ? _a : null;
|
|
31
32
|
// Check if iOS new architecture event emitters are available
|
|
32
33
|
// On Android, we always use NativeEventEmitter because Android uses RCTDeviceEventEmitter
|
|
33
34
|
isIOSNewArchitecture = react_native_1.Platform.OS === 'ios' && turboModule != null && typeof turboModule.onDownloadBegin === 'function';
|
|
@@ -57,6 +58,40 @@ function ensureNativeModuleInitialized() {
|
|
|
57
58
|
return RNBackgroundDownloader;
|
|
58
59
|
}
|
|
59
60
|
const MIN_PROGRESS_INTERVAL = 250;
|
|
61
|
+
// Apps forward logs to remote services: log response header names and the body
|
|
62
|
+
// size, not values that can carry cookies or tokens
|
|
63
|
+
function loggableBegin({ headers, ...rest }) {
|
|
64
|
+
return { ...rest, headers: Object.keys(headers !== null && headers !== void 0 ? headers : {}) };
|
|
65
|
+
}
|
|
66
|
+
function loggableUploadComplete({ responseBody, responseHeaders, parts, ...rest }) {
|
|
67
|
+
var _a;
|
|
68
|
+
return {
|
|
69
|
+
...rest,
|
|
70
|
+
responseBodyLength: (_a = responseBody === null || responseBody === void 0 ? void 0 : responseBody.length) !== null && _a !== void 0 ? _a : 0,
|
|
71
|
+
responseHeaders: Object.keys(responseHeaders !== null && responseHeaders !== void 0 ? responseHeaders : {}),
|
|
72
|
+
parts: parts === null || parts === void 0 ? void 0 : parts.length,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function handleUploadComplete(data) {
|
|
76
|
+
var _a, _b, _c;
|
|
77
|
+
const { id, responseHeaders, ...rest } = data;
|
|
78
|
+
(0, logger_1.log)('uploadComplete', id, loggableUploadComplete(data));
|
|
79
|
+
const task = tasks_1.uploadTasksMap.get(id);
|
|
80
|
+
if (!task) {
|
|
81
|
+
// Nobody received it: after a relaunch, iOS can deliver a finished upload
|
|
82
|
+
// before the app has called getExistingUploadTasks. Native keeps the
|
|
83
|
+
// response, so that call still returns it as DONE.
|
|
84
|
+
(0, logger_1.log)('uploadComplete: task not found in uploadTasksMap', id);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// iOS keeps a finished upload's response until JS has it, so an upload that
|
|
88
|
+
// finishes while the app isn't listening still reaches getExistingUploadTasks.
|
|
89
|
+
// This one was delivered: let native drop it. Sent before the handlers run, so it
|
|
90
|
+
// can't race an upload the done handler starts with the same id.
|
|
91
|
+
(_c = (_b = (_a = ensureNativeModuleInitialized()).stopUploadTask) === null || _b === void 0 ? void 0 : _b.call(_a, id)) === null || _c === void 0 ? void 0 : _c.catch(() => { });
|
|
92
|
+
task.onDone({ ...rest, responseHeaders: responseHeaders !== null && responseHeaders !== void 0 ? responseHeaders : {} });
|
|
93
|
+
tasks_1.uploadTasksMap.delete(id);
|
|
94
|
+
}
|
|
60
95
|
// Set up event listeners based on architecture
|
|
61
96
|
// For old architecture, we need to defer NativeEventEmitter creation
|
|
62
97
|
// to avoid issues during module initialization
|
|
@@ -80,23 +115,22 @@ function cleanup() {
|
|
|
80
115
|
tasks_1.uploadTasksMap.clear();
|
|
81
116
|
}
|
|
82
117
|
function initializeEventListeners() {
|
|
83
|
-
var _a, _b, _c, _d;
|
|
84
118
|
if (eventListenersInitialized)
|
|
85
119
|
return;
|
|
86
120
|
eventListenersInitialized = true;
|
|
87
121
|
if (isIOSNewArchitecture && turboModule) {
|
|
88
122
|
// iOS new architecture: use EventEmitter from TurboModule spec
|
|
89
|
-
turboModule.onDownloadBegin((data) => {
|
|
123
|
+
eventSubscriptions.push(turboModule.onDownloadBegin((data) => {
|
|
90
124
|
const { id, ...rest } = data;
|
|
91
|
-
(0, logger_1.log)('downloadBegin', id, rest);
|
|
125
|
+
(0, logger_1.log)('downloadBegin', id, loggableBegin(rest));
|
|
92
126
|
const task = tasks_1.tasksMap.get(id);
|
|
93
127
|
if (!task) {
|
|
94
128
|
(0, logger_1.log)('downloadBegin: task not found in tasksMap', id);
|
|
95
129
|
return;
|
|
96
130
|
}
|
|
97
131
|
task.onBegin(rest);
|
|
98
|
-
});
|
|
99
|
-
turboModule.onDownloadProgress((events) => {
|
|
132
|
+
}));
|
|
133
|
+
eventSubscriptions.push(turboModule.onDownloadProgress((events) => {
|
|
100
134
|
(0, logger_1.log)('downloadProgress', events);
|
|
101
135
|
for (const event of events) {
|
|
102
136
|
const { id, ...rest } = event;
|
|
@@ -104,8 +138,8 @@ function initializeEventListeners() {
|
|
|
104
138
|
if (task)
|
|
105
139
|
task.onProgress(rest);
|
|
106
140
|
}
|
|
107
|
-
});
|
|
108
|
-
turboModule.onDownloadComplete((data) => {
|
|
141
|
+
}));
|
|
142
|
+
eventSubscriptions.push(turboModule.onDownloadComplete((data) => {
|
|
109
143
|
const { id, ...rest } = data;
|
|
110
144
|
(0, logger_1.log)('downloadComplete', id, rest);
|
|
111
145
|
const task = tasks_1.tasksMap.get(id);
|
|
@@ -114,8 +148,8 @@ function initializeEventListeners() {
|
|
|
114
148
|
else
|
|
115
149
|
task.onDone(rest);
|
|
116
150
|
tasks_1.tasksMap.delete(id);
|
|
117
|
-
});
|
|
118
|
-
turboModule.onDownloadFailed((data) => {
|
|
151
|
+
}));
|
|
152
|
+
eventSubscriptions.push(turboModule.onDownloadFailed((data) => {
|
|
119
153
|
const { id, ...rest } = data;
|
|
120
154
|
(0, logger_1.log)('downloadFailed', id, rest);
|
|
121
155
|
const task = tasks_1.tasksMap.get(id);
|
|
@@ -124,10 +158,10 @@ function initializeEventListeners() {
|
|
|
124
158
|
else
|
|
125
159
|
task.onError(rest);
|
|
126
160
|
tasks_1.tasksMap.delete(id);
|
|
127
|
-
});
|
|
161
|
+
}));
|
|
128
162
|
// Upload events for new architecture (optional - may not exist in all versions)
|
|
129
163
|
if (typeof turboModule.onUploadBegin === 'function') {
|
|
130
|
-
(
|
|
164
|
+
eventSubscriptions.push(turboModule.onUploadBegin((data) => {
|
|
131
165
|
const { id, ...rest } = data;
|
|
132
166
|
(0, logger_1.log)('uploadBegin', id, rest);
|
|
133
167
|
const task = tasks_1.uploadTasksMap.get(id);
|
|
@@ -136,8 +170,8 @@ function initializeEventListeners() {
|
|
|
136
170
|
return;
|
|
137
171
|
}
|
|
138
172
|
task.onBegin(rest);
|
|
139
|
-
});
|
|
140
|
-
(
|
|
173
|
+
}));
|
|
174
|
+
eventSubscriptions.push(turboModule.onUploadProgress((events) => {
|
|
141
175
|
(0, logger_1.log)('uploadProgress', events);
|
|
142
176
|
for (const event of events) {
|
|
143
177
|
const { id, ...rest } = event;
|
|
@@ -145,18 +179,9 @@ function initializeEventListeners() {
|
|
|
145
179
|
if (task)
|
|
146
180
|
task.onProgress(rest);
|
|
147
181
|
}
|
|
148
|
-
});
|
|
149
|
-
(
|
|
150
|
-
|
|
151
|
-
(0, logger_1.log)('uploadComplete', id, rest);
|
|
152
|
-
const task = tasks_1.uploadTasksMap.get(id);
|
|
153
|
-
if (!task)
|
|
154
|
-
(0, logger_1.log)('uploadComplete: task not found in uploadTasksMap', id);
|
|
155
|
-
else
|
|
156
|
-
task.onDone(rest);
|
|
157
|
-
tasks_1.uploadTasksMap.delete(id);
|
|
158
|
-
});
|
|
159
|
-
(_d = turboModule.onUploadFailed) === null || _d === void 0 ? void 0 : _d.call(turboModule, (data) => {
|
|
182
|
+
}));
|
|
183
|
+
eventSubscriptions.push(turboModule.onUploadComplete(handleUploadComplete));
|
|
184
|
+
eventSubscriptions.push(turboModule.onUploadFailed((data) => {
|
|
160
185
|
const { id, ...rest } = data;
|
|
161
186
|
(0, logger_1.log)('uploadFailed', id, rest);
|
|
162
187
|
const task = tasks_1.uploadTasksMap.get(id);
|
|
@@ -165,7 +190,7 @@ function initializeEventListeners() {
|
|
|
165
190
|
else
|
|
166
191
|
task.onError(rest);
|
|
167
192
|
tasks_1.uploadTasksMap.delete(id);
|
|
168
|
-
});
|
|
193
|
+
}));
|
|
169
194
|
}
|
|
170
195
|
}
|
|
171
196
|
else {
|
|
@@ -176,7 +201,7 @@ function initializeEventListeners() {
|
|
|
176
201
|
const eventEmitter = new react_native_1.NativeEventEmitter(RNBackgroundDownloader);
|
|
177
202
|
eventSubscriptions.push(eventEmitter.addListener('downloadBegin', (data) => {
|
|
178
203
|
const { id, ...rest } = data;
|
|
179
|
-
(0, logger_1.log)('downloadBegin', id, rest);
|
|
204
|
+
(0, logger_1.log)('downloadBegin', id, loggableBegin(rest));
|
|
180
205
|
const task = tasks_1.tasksMap.get(id);
|
|
181
206
|
if (!task) {
|
|
182
207
|
(0, logger_1.log)('downloadBegin: task not found in tasksMap', id);
|
|
@@ -233,16 +258,7 @@ function initializeEventListeners() {
|
|
|
233
258
|
task.onProgress(rest);
|
|
234
259
|
}
|
|
235
260
|
}));
|
|
236
|
-
eventSubscriptions.push(eventEmitter.addListener('uploadComplete',
|
|
237
|
-
const { id, ...rest } = data;
|
|
238
|
-
(0, logger_1.log)('uploadComplete', id, rest);
|
|
239
|
-
const task = tasks_1.uploadTasksMap.get(id);
|
|
240
|
-
if (!task)
|
|
241
|
-
(0, logger_1.log)('uploadComplete: task not found in uploadTasksMap', id);
|
|
242
|
-
else
|
|
243
|
-
task.onDone(rest);
|
|
244
|
-
tasks_1.uploadTasksMap.delete(id);
|
|
245
|
-
}));
|
|
261
|
+
eventSubscriptions.push(eventEmitter.addListener('uploadComplete', handleUploadComplete));
|
|
246
262
|
eventSubscriptions.push(eventEmitter.addListener('uploadFailed', (data) => {
|
|
247
263
|
const { id, ...rest } = data;
|
|
248
264
|
(0, logger_1.log)('uploadFailed', id, rest);
|
|
@@ -261,19 +277,23 @@ function initializeEventListeners() {
|
|
|
261
277
|
}
|
|
262
278
|
// Event listeners are now initialized lazily when ensureNativeModuleInitialized() is called
|
|
263
279
|
// This ensures the bridge is ready before any native module access
|
|
264
|
-
|
|
280
|
+
// Only the options passed are changed; the rest keep their current values
|
|
281
|
+
function setConfig({ headers, progressInterval, progressMinBytes, isLogsEnabled, logCallback, maxParallelDownloads, allowsCellularAccess, showNotificationsEnabled, showCompletionNotification, showCancelAction, notificationsGrouping, iosDataProtection, }) {
|
|
265
282
|
var _a, _b, _c, _d, _e;
|
|
266
|
-
|
|
283
|
+
if (headers !== undefined)
|
|
284
|
+
config_1.config.headers = headers;
|
|
267
285
|
if (iosDataProtection !== undefined)
|
|
268
286
|
config_1.config.iosDataProtection = iosDataProtection;
|
|
269
|
-
if (progressInterval
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
287
|
+
if (progressInterval !== undefined)
|
|
288
|
+
if (progressInterval >= MIN_PROGRESS_INTERVAL)
|
|
289
|
+
config_1.config.progressInterval = progressInterval;
|
|
290
|
+
else
|
|
291
|
+
console.warn(`[RNBackgroundDownloader] progressInterval must be a number >= ${MIN_PROGRESS_INTERVAL}. You passed ${progressInterval}`);
|
|
292
|
+
if (progressMinBytes !== undefined)
|
|
293
|
+
if (progressMinBytes >= 0)
|
|
294
|
+
config_1.config.progressMinBytes = progressMinBytes;
|
|
295
|
+
else
|
|
296
|
+
console.warn(`[RNBackgroundDownloader] progressMinBytes must be a number >= 0. You passed ${progressMinBytes}`);
|
|
277
297
|
if (maxParallelDownloads !== undefined)
|
|
278
298
|
if (maxParallelDownloads >= 1)
|
|
279
299
|
config_1.config.maxParallelDownloads = maxParallelDownloads;
|
|
@@ -299,12 +319,14 @@ function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_
|
|
|
299
319
|
...notificationsGrouping.texts,
|
|
300
320
|
},
|
|
301
321
|
};
|
|
302
|
-
|
|
303
|
-
|
|
322
|
+
if (isLogsEnabled !== undefined)
|
|
323
|
+
config_1.config.isLogsEnabled = isLogsEnabled;
|
|
324
|
+
if (logCallback !== undefined)
|
|
325
|
+
config_1.config.logCallback = logCallback;
|
|
304
326
|
// Notify native side about configuration changes
|
|
305
327
|
try {
|
|
306
328
|
const nativeModule = ensureNativeModuleInitialized();
|
|
307
|
-
if (nativeModule.setLogsEnabled)
|
|
329
|
+
if (nativeModule.setLogsEnabled && isLogsEnabled !== undefined)
|
|
308
330
|
nativeModule.setLogsEnabled(isLogsEnabled);
|
|
309
331
|
// Only call native methods if config was successfully updated
|
|
310
332
|
if (nativeModule.setMaxParallelDownloads && maxParallelDownloads !== undefined && maxParallelDownloads >= 1)
|
|
@@ -362,7 +384,8 @@ const getExistingDownloadTasks = async () => {
|
|
|
362
384
|
task.state = 'PAUSED';
|
|
363
385
|
}
|
|
364
386
|
else {
|
|
365
|
-
task
|
|
387
|
+
// Not awaited: the task is dropped either way, so a failed stop isn't actionable
|
|
388
|
+
task.stop().catch(() => { });
|
|
366
389
|
return undefined;
|
|
367
390
|
}
|
|
368
391
|
break;
|
|
@@ -391,16 +414,20 @@ const completeHandler = (jobId) => {
|
|
|
391
414
|
return nativeModule.completeHandler(jobId);
|
|
392
415
|
};
|
|
393
416
|
exports.completeHandler = completeHandler;
|
|
394
|
-
function createDownloadTask({ isAllowedOverRoaming = true,
|
|
417
|
+
function createDownloadTask({ isAllowedOverRoaming = true,
|
|
418
|
+
// Follows setConfig({ allowsCellularAccess }) unless the task sets its own
|
|
419
|
+
isAllowedOverMetered = config_1.config.allowsCellularAccess, metadata, groupId, groupName, notificationTitle, ...rest }) {
|
|
395
420
|
// Ensure native module and event listeners are initialized before creating tasks
|
|
396
421
|
ensureNativeModuleInitialized();
|
|
397
422
|
if (!rest.id || !rest.url || !rest.destination)
|
|
398
423
|
throw new Error('[RNBackgroundDownloader] id, url and destination are required');
|
|
399
424
|
rest.headers = { ...config_1.config.headers, ...rest.headers };
|
|
400
425
|
rest.destination = rest.destination.replace('file://', '');
|
|
426
|
+
// Native reads the notification fields from metadata only
|
|
427
|
+
const notificationFields = Object.fromEntries(Object.entries({ groupId, groupName, notificationTitle }).filter(([, value]) => value !== undefined));
|
|
401
428
|
const task = new DownloadTask_1.DownloadTask({
|
|
402
429
|
id: rest.id,
|
|
403
|
-
metadata,
|
|
430
|
+
metadata: Object.keys(notificationFields).length > 0 ? { ...metadata, ...notificationFields } : metadata,
|
|
404
431
|
});
|
|
405
432
|
task.setDownloadParams({
|
|
406
433
|
isAllowedOverRoaming,
|
|
@@ -418,7 +445,7 @@ const getExistingUploadTasks = async () => {
|
|
|
418
445
|
}
|
|
419
446
|
const uploads = await nativeModule.getExistingUploadTasks();
|
|
420
447
|
const uploadTasks = uploads.map(uploadInfo => {
|
|
421
|
-
var _a;
|
|
448
|
+
var _a, _b, _c;
|
|
422
449
|
// Parse metadata from JSON string to object
|
|
423
450
|
let metadata = {};
|
|
424
451
|
if (uploadInfo.metadata)
|
|
@@ -450,17 +477,23 @@ const getExistingUploadTasks = async () => {
|
|
|
450
477
|
task.state = 'PAUSED';
|
|
451
478
|
}
|
|
452
479
|
else {
|
|
453
|
-
task
|
|
480
|
+
// Not awaited: the task is dropped either way, so a failed stop isn't actionable
|
|
481
|
+
task.stop().catch(() => { });
|
|
454
482
|
return undefined;
|
|
455
483
|
}
|
|
456
484
|
break;
|
|
457
485
|
}
|
|
458
486
|
case nativeModule.TaskCompleted: {
|
|
459
|
-
if (taskInfo.bytesUploaded === taskInfo.bytesTotal)
|
|
487
|
+
if (taskInfo.bytesUploaded === taskInfo.bytesTotal) {
|
|
460
488
|
task.state = 'DONE';
|
|
461
|
-
|
|
489
|
+
// Finished while the app wasn't listening: the task carries the response
|
|
490
|
+
// now, so native can drop its copy
|
|
491
|
+
(_c = (_b = nativeModule.stopUploadTask) === null || _b === void 0 ? void 0 : _b.call(nativeModule, task.id)) === null || _c === void 0 ? void 0 : _c.catch(() => { });
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
462
494
|
// IOS completed the upload but it was not done.
|
|
463
495
|
return undefined;
|
|
496
|
+
}
|
|
464
497
|
}
|
|
465
498
|
}
|
|
466
499
|
return task;
|
|
@@ -470,11 +503,22 @@ const getExistingUploadTasks = async () => {
|
|
|
470
503
|
return uploadTasks;
|
|
471
504
|
};
|
|
472
505
|
exports.getExistingUploadTasks = getExistingUploadTasks;
|
|
473
|
-
function createUploadTask({ isAllowedOverRoaming = true,
|
|
506
|
+
function createUploadTask({ isAllowedOverRoaming = true,
|
|
507
|
+
// Follows setConfig({ allowsCellularAccess }) unless the task sets its own
|
|
508
|
+
isAllowedOverMetered = config_1.config.allowsCellularAccess, metadata, ...rest }) {
|
|
474
509
|
// Ensure native module and event listeners are initialized before creating tasks
|
|
475
510
|
ensureNativeModuleInitialized();
|
|
476
|
-
if (
|
|
477
|
-
|
|
511
|
+
if (rest.partUrls !== undefined) {
|
|
512
|
+
if (!Array.isArray(rest.partUrls) || rest.partUrls.length === 0 ||
|
|
513
|
+
rest.partUrls.some(url => typeof url !== 'string' || url.length === 0))
|
|
514
|
+
throw new Error('[RNBackgroundDownloader] partUrls must be a non-empty array of URLs');
|
|
515
|
+
if (rest.fieldName !== undefined || rest.parameters !== undefined)
|
|
516
|
+
throw new Error('[RNBackgroundDownloader] partUrls sends each part as the raw body; it can\'t be combined with fieldName or parameters');
|
|
517
|
+
if (rest.partSize !== undefined && !(Number.isInteger(rest.partSize) && rest.partSize > 0))
|
|
518
|
+
throw new Error('[RNBackgroundDownloader] partSize must be a positive whole number of bytes');
|
|
519
|
+
}
|
|
520
|
+
if (!rest.id || !(rest.url || rest.partUrls) || !rest.source)
|
|
521
|
+
throw new Error('[RNBackgroundDownloader] id, url (or partUrls) and source are required');
|
|
478
522
|
rest.headers = { ...config_1.config.headers, ...rest.headers };
|
|
479
523
|
rest.source = rest.source.replace('file://', '');
|
|
480
524
|
const task = new UploadTask_1.UploadTask({
|