Skip to content

Commit dbaeef1

Browse files
committed
Add IOS server push support wkh237#143 wkh237#144
1 parent ca038be commit dbaeef1

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function fetch(...args:any):Promise {
116116
// create task ID for receiving progress event
117117
let taskId = getUUID()
118118
let options = this || {}
119-
let subscription, subscriptionUpload, stateEvent
119+
let subscription, subscriptionUpload, stateEvent, partEvent
120120
let respInfo = {}
121121

122122
let promise = new Promise((resolve, reject) => {
@@ -143,6 +143,12 @@ function fetch(...args:any):Promise {
143143
}
144144
})
145145

146+
partEvent = emitter.addListener('RNFetchBlobServerPush', (e) => {
147+
if(e.taskId === taskId && promise.onPartData) {
148+
promise.onPartData(e.chunk)
149+
}
150+
})
151+
146152
// When the request body comes from Blob polyfill, we should use special its ref
147153
// as the request body
148154
if( body instanceof Blob && body.isRNFetchBlobPolyfill) {
@@ -168,9 +174,11 @@ function fetch(...args:any):Promise {
168174
subscription.remove()
169175
subscriptionUpload.remove()
170176
stateEvent.remove()
177+
partEvent.remove()
171178
delete promise['progress']
172179
delete promise['uploadProgress']
173180
delete promise['stateChange']
181+
delete promise['part']
174182
delete promise['cancel']
175183
promise.cancel = () => {}
176184

@@ -229,6 +237,10 @@ function fetch(...args:any):Promise {
229237
RNFetchBlob.enableUploadProgressReport(taskId, interval, count)
230238
return promise
231239
}
240+
promise.part = (fn) => {
241+
promise.onPartData = fn
242+
return promise
243+
}
232244
promise.stateChange = (fn) => {
233245
promise.onStateChange = fn
234246
return promise

src/ios/RNFetchBlobConst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern NSString *const MSG_EVENT_WARN;
1818
extern NSString *const MSG_EVENT_ERROR;
1919

2020
extern NSString *const EVENT_PROGRESS;
21+
extern NSString *const EVENT_SERVER_PUSH;
2122
extern NSString *const EVENT_PROGRESS_UPLOAD;
2223
extern NSString *const EVENT_STATE_CHANGE;
2324

src/ios/RNFetchBlobConst.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
extern NSString *const CONFIG_EXTRA_BLOB_CTYPE = @"binaryContentTypes";
2424

2525
extern NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
26+
extern NSString *const EVENT_SERVER_PUSH = @"RNFetchBlobServerPush";
2627
extern NSString *const EVENT_PROGRESS = @"RNFetchBlobProgress";
2728
extern NSString *const EVENT_PROGRESS_UPLOAD = @"RNFetchBlobProgress-upload";
2829

@@ -41,4 +42,4 @@
4142
// response type
4243
extern NSString *const RESP_TYPE_BASE64 = @"base64";
4344
extern NSString *const RESP_TYPE_UTF8 = @"utf8";
44-
extern NSString *const RESP_TYPE_PATH = @"path";
45+
extern NSString *const RESP_TYPE_PATH = @"path";

src/ios/RNFetchBlobNetwork.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
2222
@property (nullable, nonatomic) NSString * taskId;
2323
@property (nonatomic) int expectedBytes;
2424
@property (nonatomic) int receivedBytes;
25+
@property (nonatomic) BOOL isServerPush;
2526
@property (nullable, nonatomic) NSMutableData * respData;
2627
@property (strong, nonatomic) RCTResponseSenderBlock callback;
2728
@property (nullable, nonatomic) RCTBridge * bridge;

src/ios/RNFetchBlobNetwork.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
3838
@interface RNFetchBlobNetwork ()
3939
{
4040
BOOL * respFile;
41+
BOOL isNewPart;
42+
NSMutableData * partBuffer;
4143
NSString * destPath;
4244
NSOutputStream * writeStream;
4345
long bodyLength;
@@ -222,6 +224,26 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
222224
{
223225
NSDictionary *headers = [httpResponse allHeaderFields];
224226
NSString * respCType = [[RNFetchBlobReqBuilder getHeaderIgnoreCases:@"Content-Type" fromHeaders:headers] lowercaseString];
227+
if(self.isServerPush == NO)
228+
{
229+
self.isServerPush = [[respCType lowercaseString] RNFBContainsString:@"multipart/x-mixed-replace;"];
230+
}
231+
if(self.isServerPush)
232+
{
233+
if(partBuffer != nil)
234+
{
235+
[self.bridge.eventDispatcher
236+
sendDeviceEventWithName:EVENT_SERVER_PUSH
237+
body:@{
238+
@"taskId": taskId,
239+
@"chunk": [partBuffer base64EncodedStringWithOptions:0],
240+
}
241+
];
242+
}
243+
partBuffer = [[NSMutableData alloc] init];
244+
completionHandler(NSURLSessionResponseAllow);
245+
return;
246+
}
225247
if(respCType != nil)
226248
{
227249
NSArray * extraBlobCTypes = [options objectForKey:CONFIG_EXTRA_BLOB_CTYPE];
@@ -311,9 +333,17 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
311333
completionHandler(NSURLSessionResponseAllow);
312334
}
313335

336+
314337
// download progress handler
315338
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
316339
{
340+
// For #143 handling multipart/x-mixed-replace response
341+
if(self.isServerPush)
342+
{
343+
[partBuffer appendData:data];
344+
return ;
345+
}
346+
317347
NSNumber * received = [NSNumber numberWithLong:[data length]];
318348
receivedBytes += [received longValue];
319349
if(respFile == NO)
@@ -349,6 +379,7 @@ - (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable
349379
session = nil;
350380
}
351381

382+
352383
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
353384
{
354385

0 commit comments

Comments
 (0)