@kesha-antonov/react-native-background-downloader 4.7.0 → 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 +75 -32
- package/android/src/main/java/com/eko/DownloadConstants.kt +5 -0
- package/android/src/main/java/com/eko/Downloader.kt +12 -5
- package/android/src/main/java/com/eko/RNBGDUploadTaskConfig.kt +36 -1
- package/android/src/main/java/com/eko/RNBackgroundDownloaderModuleImpl.kt +94 -54
- package/android/src/main/java/com/eko/ResumableDownloader.kt +34 -13
- 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 +187 -17
- package/android/src/main/java/com/eko/handlers/OnProgress.kt +1 -1
- package/android/src/main/java/com/eko/utils/HeaderUtils.kt +9 -0
- package/android/src/main/java/com/eko/utils/RedirectResolver.kt +29 -1
- package/android/src/test/java/com/eko/DownloadCompleteReceiverTest.kt +57 -0
- package/android/src/test/java/com/eko/ResumableDownloaderRedirectTest.kt +77 -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 +10 -1
- 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/utils/RedirectResolverTest.kt +66 -0
- package/ios/RNBGDUploadTaskConfig.h +26 -0
- package/ios/RNBGDUploadTaskConfig.mm +69 -3
- package/ios/RNBackgroundDownloader.mm +563 -43
- package/lib/NativeRNBackgroundDownloader.d.ts +15 -0
- package/lib/UploadTask.d.ts +5 -1
- package/lib/UploadTask.js +22 -4
- package/lib/index.js +57 -27
- package/lib/types.d.ts +33 -2
- package/package.json +1 -1
- package/src/NativeRNBackgroundDownloader.ts +23 -0
- package/src/UploadTask.ts +23 -1
- package/src/index.ts +60 -27
- package/src/types.ts +42 -2
|
@@ -5,12 +5,16 @@ import okhttp3.mockwebserver.MockResponse
|
|
|
5
5
|
import okhttp3.mockwebserver.MockWebServer
|
|
6
6
|
import okhttp3.mockwebserver.RecordedRequest
|
|
7
7
|
import org.junit.After
|
|
8
|
+
import com.facebook.react.bridge.JavaOnlyMap
|
|
8
9
|
import org.junit.Assert.assertEquals
|
|
10
|
+
import org.junit.Assert.assertFalse
|
|
11
|
+
import org.junit.Assert.assertTrue
|
|
9
12
|
import org.junit.Before
|
|
10
13
|
import org.junit.Test
|
|
11
14
|
import org.junit.runner.RunWith
|
|
12
15
|
import org.robolectric.RobolectricTestRunner
|
|
13
16
|
import org.robolectric.annotation.Config
|
|
17
|
+
import java.net.URL
|
|
14
18
|
|
|
15
19
|
/**
|
|
16
20
|
* A Location header may be relative. It has to be resolved against the URL that
|
|
@@ -64,4 +68,66 @@ class RedirectResolverTest {
|
|
|
64
68
|
|
|
65
69
|
assertEquals(server.url("/b/final.zip").toString(), resolve("/a/start"))
|
|
66
70
|
}
|
|
71
|
+
|
|
72
|
+
// A second server is a different origin (another port)
|
|
73
|
+
private fun otherServer(): MockWebServer = MockWebServer().also {
|
|
74
|
+
it.dispatcher = object : Dispatcher() {
|
|
75
|
+
override fun dispatch(request: RecordedRequest) = MockResponse().setResponseCode(200)
|
|
76
|
+
}
|
|
77
|
+
it.start()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Test
|
|
81
|
+
fun `stops before another origin while credentials are attached`() {
|
|
82
|
+
val other = otherServer()
|
|
83
|
+
try {
|
|
84
|
+
redirects["/start"] = other.url("/file.zip").toString()
|
|
85
|
+
val headers = JavaOnlyMap.of("Authorization", "Bearer secret")
|
|
86
|
+
|
|
87
|
+
val resolved = RedirectResolver.resolve(server.url("/start").toString(), 5, headers)
|
|
88
|
+
|
|
89
|
+
assertEquals(server.url("/start").toString(), resolved)
|
|
90
|
+
assertEquals(0, other.requestCount)
|
|
91
|
+
} finally {
|
|
92
|
+
other.shutdown()
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Test
|
|
97
|
+
fun `follows another origin when no credentials are attached`() {
|
|
98
|
+
val other = otherServer()
|
|
99
|
+
try {
|
|
100
|
+
redirects["/start"] = other.url("/file.zip").toString()
|
|
101
|
+
val headers = JavaOnlyMap.of("Accept", "*/*")
|
|
102
|
+
|
|
103
|
+
val resolved = RedirectResolver.resolve(server.url("/start").toString(), 5, headers)
|
|
104
|
+
|
|
105
|
+
assertEquals(other.url("/file.zip").toString(), resolved)
|
|
106
|
+
} finally {
|
|
107
|
+
other.shutdown()
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@Test
|
|
112
|
+
fun `doesn't resolve to a non-http location`() {
|
|
113
|
+
redirects["/start"] = "ftp://example.com/file.zip"
|
|
114
|
+
|
|
115
|
+
// DownloadManager.Request throws on an ftp URL, which crashed the app
|
|
116
|
+
assertEquals(server.url("/start").toString(), resolve("/start"))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Test
|
|
120
|
+
fun `refuses an https to http downgrade even on the same host`() {
|
|
121
|
+
assertFalse(RedirectResolver.isSafeHop(URL("https://a.example/x"), URL("http://a.example/y"), emptyMap()))
|
|
122
|
+
assertTrue(RedirectResolver.isSafeHop(URL("http://a.example/x"), URL("https://a.example/y"), emptyMap()))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
@Test
|
|
126
|
+
fun `credential headers are matched case-insensitively`() {
|
|
127
|
+
val from = URL("https://a.example/x")
|
|
128
|
+
val to = URL("https://b.example/y")
|
|
129
|
+
assertFalse(RedirectResolver.isSafeHop(from, to, mapOf("cookie" to "s=1")))
|
|
130
|
+
assertFalse(RedirectResolver.isSafeHop(from, to, mapOf("PROXY-AUTHORIZATION" to "x")))
|
|
131
|
+
assertTrue(RedirectResolver.isSafeHop(from, URL("https://a.example:443/y"), mapOf("Authorization" to "x")))
|
|
132
|
+
}
|
|
67
133
|
}
|
|
@@ -20,8 +20,34 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
20
20
|
// Stores the response body when upload completes
|
|
21
21
|
@property (nonatomic, strong, nullable) NSMutableData *responseData;
|
|
22
22
|
|
|
23
|
+
// Upload source in parts, one URL per part (S3 multipart upload and similar)
|
|
24
|
+
@property (nonatomic, copy, nullable) NSArray<NSString *> *partUrls;
|
|
25
|
+
// Bytes per part; 0 means ceil(file size / number of parts)
|
|
26
|
+
@property (nonatomic, assign) long long partSize;
|
|
27
|
+
// Session task identifier -> 0-based part index, for the part tasks still running
|
|
28
|
+
@property (nonatomic, copy, nullable) NSDictionary<NSNumber *, NSNumber *> *partIndexByTask;
|
|
29
|
+
// 0-based part index -> result (partNumber, responseCode, responseBody, responseHeaders, bytes)
|
|
30
|
+
@property (nonatomic, copy, nullable) NSDictionary<NSNumber *, NSDictionary *> *partResults;
|
|
31
|
+
|
|
32
|
+
// The response of a finished upload, kept until JS has it
|
|
33
|
+
@property (nonatomic, assign) NSInteger responseCode;
|
|
34
|
+
@property (nonatomic, copy, nullable) NSString *responseBody;
|
|
35
|
+
@property (nonatomic, copy, nullable) NSDictionary<NSString *, NSString *> *responseHeaders;
|
|
36
|
+
|
|
23
37
|
- (instancetype)initWithDictionary:(NSDictionary *)dict;
|
|
24
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Byte ranges of the parts as [offset, length] pairs: every part `partSize` bytes
|
|
41
|
+
* (default ceil(fileSize / partCount)) and the last one what's left. Nil when the parts
|
|
42
|
+
* can't cover the file exactly, which includes an empty last part.
|
|
43
|
+
*/
|
|
44
|
+
+ (nullable NSArray<NSArray<NSNumber *> *> *)partRangesForFileSize:(long long)fileSize
|
|
45
|
+
partCount:(NSUInteger)partCount
|
|
46
|
+
partSize:(long long)partSize;
|
|
47
|
+
|
|
48
|
+
// Results of the accepted parts in part order, as sent to JS
|
|
49
|
+
- (NSArray<NSDictionary *> *)orderedPartResults;
|
|
50
|
+
|
|
25
51
|
@end
|
|
26
52
|
|
|
27
53
|
NS_ASSUME_NONNULL_END
|
|
@@ -7,6 +7,24 @@
|
|
|
7
7
|
return YES;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
static NSString *_Nullable RNBGDStringOrNil(id value)
|
|
11
|
+
{
|
|
12
|
+
if ([value isKindOfClass:[NSString class]]) return value;
|
|
13
|
+
if (value == nil || value == [NSNull null]) return nil;
|
|
14
|
+
return [value description];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static NSDictionary<NSString *, NSString *> *_Nullable RNBGDStringParameters(id value)
|
|
18
|
+
{
|
|
19
|
+
if (![value isKindOfClass:[NSDictionary class]]) return nil;
|
|
20
|
+
NSMutableDictionary<NSString *, NSString *> *parameters = [NSMutableDictionary dictionary];
|
|
21
|
+
[(NSDictionary *)value enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
|
22
|
+
NSString *string = RNBGDStringOrNil(obj);
|
|
23
|
+
if ([key isKindOfClass:[NSString class]] && string != nil) parameters[key] = string;
|
|
24
|
+
}];
|
|
25
|
+
return parameters;
|
|
26
|
+
}
|
|
27
|
+
|
|
10
28
|
- (instancetype)initWithDictionary:(NSDictionary *)dict
|
|
11
29
|
{
|
|
12
30
|
self = [super init];
|
|
@@ -17,9 +35,14 @@
|
|
|
17
35
|
self.source = dict[@"source"];
|
|
18
36
|
self.method = dict[@"method"] ?: @"POST";
|
|
19
37
|
self.metadata = dict[@"metadata"] ?: @"{}";
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
self.
|
|
38
|
+
// NSNull (a missing option) or a number would be archived as is, and then the
|
|
39
|
+
// whole saved upload map fails to decode with the NSString/NSDictionary allowlist
|
|
40
|
+
self.fieldName = RNBGDStringOrNil(dict[@"fieldName"]);
|
|
41
|
+
self.mimeType = RNBGDStringOrNil(dict[@"mimeType"]);
|
|
42
|
+
self.parameters = RNBGDStringParameters(dict[@"parameters"]);
|
|
43
|
+
id partUrls = dict[@"partUrls"];
|
|
44
|
+
self.partUrls = [partUrls isKindOfClass:[NSArray class]] ? partUrls : nil;
|
|
45
|
+
self.partSize = [dict[@"partSize"] isKindOfClass:[NSNumber class]] ? [dict[@"partSize"] longLongValue] : 0;
|
|
23
46
|
self.reportedBegin = NO;
|
|
24
47
|
self.bytesUploaded = 0;
|
|
25
48
|
self.bytesTotal = 0;
|
|
@@ -46,6 +69,13 @@
|
|
|
46
69
|
[aCoder encodeInt64:self.bytesTotal forKey:@"bytesTotal"];
|
|
47
70
|
[aCoder encodeInteger:self.state forKey:@"state"];
|
|
48
71
|
[aCoder encodeInteger:self.errorCode forKey:@"errorCode"];
|
|
72
|
+
[aCoder encodeObject:self.partUrls forKey:@"partUrls"];
|
|
73
|
+
[aCoder encodeInt64:self.partSize forKey:@"partSize"];
|
|
74
|
+
[aCoder encodeObject:self.partIndexByTask forKey:@"partIndexByTask"];
|
|
75
|
+
[aCoder encodeObject:self.partResults forKey:@"partResults"];
|
|
76
|
+
[aCoder encodeInteger:self.responseCode forKey:@"responseCode"];
|
|
77
|
+
[aCoder encodeObject:self.responseBody forKey:@"responseBody"];
|
|
78
|
+
[aCoder encodeObject:self.responseHeaders forKey:@"responseHeaders"];
|
|
49
79
|
// Note: responseData is not persisted as it's only needed during active upload
|
|
50
80
|
}
|
|
51
81
|
|
|
@@ -73,9 +103,45 @@
|
|
|
73
103
|
self.state = [aDecoder decodeIntegerForKey:@"state"];
|
|
74
104
|
self.errorCode = [aDecoder decodeIntegerForKey:@"errorCode"];
|
|
75
105
|
self.responseData = nil;
|
|
106
|
+
self.partUrls = [aDecoder decodeObjectOfClasses:[NSSet setWithObjects:[NSArray class], [NSString class], nil] forKey:@"partUrls"];
|
|
107
|
+
self.partSize = [aDecoder decodeInt64ForKey:@"partSize"];
|
|
108
|
+
self.partIndexByTask = [aDecoder decodeObjectOfClasses:[NSSet setWithObjects:[NSDictionary class], [NSNumber class], nil] forKey:@"partIndexByTask"];
|
|
109
|
+
self.partResults = [aDecoder decodeObjectOfClasses:[NSSet setWithObjects:[NSDictionary class], [NSNumber class], [NSString class], nil] forKey:@"partResults"];
|
|
110
|
+
self.responseCode = [aDecoder decodeIntegerForKey:@"responseCode"];
|
|
111
|
+
self.responseBody = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"responseBody"];
|
|
112
|
+
self.responseHeaders = [aDecoder decodeObjectOfClasses:[NSSet setWithObjects:[NSDictionary class], [NSString class], nil] forKey:@"responseHeaders"];
|
|
76
113
|
}
|
|
77
114
|
|
|
78
115
|
return self;
|
|
79
116
|
}
|
|
80
117
|
|
|
118
|
+
+ (NSArray<NSArray<NSNumber *> *> *)partRangesForFileSize:(long long)fileSize
|
|
119
|
+
partCount:(NSUInteger)partCount
|
|
120
|
+
partSize:(long long)partSize
|
|
121
|
+
{
|
|
122
|
+
if (partCount == 0 || fileSize <= 0) return nil;
|
|
123
|
+
long long count = (long long)partCount;
|
|
124
|
+
long long size = partSize > 0 ? partSize : (fileSize + count - 1) / count;
|
|
125
|
+
long long beforeLast = size * (count - 1);
|
|
126
|
+
if (beforeLast >= fileSize || fileSize - beforeLast > size) return nil;
|
|
127
|
+
|
|
128
|
+
NSMutableArray<NSArray<NSNumber *> *> *ranges = [NSMutableArray arrayWithCapacity:partCount];
|
|
129
|
+
for (long long index = 0; index < count; index++) {
|
|
130
|
+
long long offset = size * index;
|
|
131
|
+
long long length = index == count - 1 ? fileSize - offset : size;
|
|
132
|
+
[ranges addObject:@[@(offset), @(length)]];
|
|
133
|
+
}
|
|
134
|
+
return ranges;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
- (NSArray<NSDictionary *> *)orderedPartResults
|
|
138
|
+
{
|
|
139
|
+
NSArray<NSNumber *> *indexes = [self.partResults.allKeys sortedArrayUsingSelector:@selector(compare:)];
|
|
140
|
+
NSMutableArray<NSDictionary *> *results = [NSMutableArray arrayWithCapacity:indexes.count];
|
|
141
|
+
for (NSNumber *index in indexes) {
|
|
142
|
+
[results addObject:self.partResults[index]];
|
|
143
|
+
}
|
|
144
|
+
return results;
|
|
145
|
+
}
|
|
146
|
+
|
|
81
147
|
@end
|