Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 801685d

Browse files
committed
Add IOS increment HTTP request support and JSON stream lib
1 parent b1fc691 commit 801685d

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import getUUID from './utils/uuid'
2222
import base64 from 'base-64'
2323
import polyfill from './polyfill'
2424
import android from './android'
25+
import JSONStream from './json-stream'
2526
const {
2627
RNFetchBlobSession,
2728
readStream,
@@ -126,7 +127,7 @@ function fetch(...args:any):Promise {
126127
// on progress event listener
127128
subscription = emitter.addListener('RNFetchBlobProgress', (e) => {
128129
if(e.taskId === taskId && promise.onProgress) {
129-
promise.onProgress(e.written, e.total)
130+
promise.onProgress(e.written, e.total, e.chunk)
130131
}
131132
})
132133

@@ -398,5 +399,6 @@ export default {
398399
session,
399400
fs,
400401
wrap,
401-
polyfill
402+
polyfill,
403+
JSONStream
402404
}

src/ios/RNFetchBlobNetwork.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ @interface RNFetchBlobNetwork ()
3636
long bodyLength;
3737
NSMutableDictionary * respInfo;
3838
NSInteger respStatus;
39+
BOOL isInrement;
3940
}
4041

4142
@end
@@ -125,6 +126,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
125126
self.expectedBytes = 0;
126127
self.receivedBytes = 0;
127128
self.options = options;
129+
isInrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue];
128130

129131
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
130132
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
@@ -284,6 +286,13 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
284286
{
285287
NSNumber * received = [NSNumber numberWithLong:[data length]];
286288
receivedBytes += [received longValue];
289+
NSString * chunkString = @"";
290+
291+
if(isInrement == YES)
292+
{
293+
chunkString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
294+
}
295+
287296
if(respFile == NO)
288297
{
289298
[respData appendData:data];
@@ -300,7 +309,8 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
300309
body:@{
301310
@"taskId": taskId,
302311
@"written": [NSString stringWithFormat:@"%d", receivedBytes],
303-
@"total": [NSString stringWithFormat:@"%d", expectedBytes]
312+
@"total": [NSString stringWithFormat:@"%d", expectedBytes],
313+
@"chunk": chunkString
304314
}
305315
];
306316
}

src/json-stream.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Oboe from './lib/oboe-browser.min.js'
2+
import XMLHttpRequest from './polyfill/XMLHttpRequest'
3+
4+
const OboeExtended = (arg: string | object) => {
5+
6+
window.XMLHttpRequest = XMLHttpRequest
7+
window.location = ''
8+
9+
if(typeof arg === 'string')
10+
arg = 'JSONStream://' + arg
11+
else if(typeof arg === 'object')
12+
arg = Object.assign(arg, { url : 'JSONStream://' + arg.url })
13+
return Oboe(arg)
14+
}
15+
16+
export default OboeExtended

src/polyfill/XMLHttpRequest.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ProgressEvent from './ProgressEvent.js'
1111
const log = new Log('XMLHttpRequest')
1212

1313
log.disable()
14-
// log.level(2)
14+
// log.level(3)
1515

1616
const UNSENT = 0
1717
const OPENED = 1
@@ -30,8 +30,9 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
3030

3131
// readonly
3232
_readyState : number = UNSENT;
33+
_uriType : 'net' | 'file' = 'net';
3334
_response : any = '';
34-
_responseText : any = null;
35+
_responseText : any = '';
3536
_responseHeaders : any = {};
3637
_responseType : '' | 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' = '';
3738
// TODO : not suppoted ATM
@@ -42,6 +43,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
4243
_timeout : number = 60000;
4344
_sendFlag : boolean = false;
4445
_uploadStarted : boolean = false;
46+
_increment : boolean = false;
4547

4648
// RNFetchBlob compatible data structure
4749
_config : RNFetchBlobConfig = {};
@@ -122,15 +124,20 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
122124
this._method = method
123125
this._url = url
124126
this._headers = {}
127+
this._increment = /^JSONStream\:\/\//.test(this._url)
128+
this._url = this._url.replace(/^JSONStream\:\/\//, '')
125129
this._dispatchReadStateChange(XMLHttpRequest.OPENED)
130+
126131
}
127132

128133
/**
129134
* Invoke this function to send HTTP request, and set body.
130135
* @param {any} body Body in RNfetchblob flavor
131136
*/
132137
send(body) {
138+
133139
this._body = body
140+
134141
if(this._readyState !== XMLHttpRequest.OPENED)
135142
throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
136143
let promise = Promise.resolve()
@@ -164,10 +171,12 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
164171
for(let h in _headers) {
165172
_headers[h] = _headers[h].toString()
166173
}
174+
167175
this._task = RNFetchBlob
168176
.config({
169177
auto: true,
170178
timeout : this._timeout,
179+
increment : this._increment,
171180
binaryContentTypes : XMLHttpRequest.binaryContentTypes
172181
})
173182
.fetch(_method, _url, _headers, body)
@@ -271,14 +280,18 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
271280
this.upload.dispatchEvent('progress', new ProgressEvent(true, send, total))
272281
}
273282

274-
_progressEvent(send:number, total:number) {
283+
_progressEvent(send:number, total:number, chunk:string) {
275284
log.verbose(this.readyState)
276285
if(this._readyState === XMLHttpRequest.HEADERS_RECEIVED)
277286
this._dispatchReadStateChange(XMLHttpRequest.LOADING)
278287
let lengthComputable = false
279288
if(total && total >= 0)
280289
lengthComputable = true
281290
let e = new ProgressEvent(lengthComputable, send, total)
291+
292+
if(this._increment) {
293+
this._responseText += chunk
294+
}
282295
this.dispatchEvent('progress', e)
283296
}
284297

src/polyfill/XMLHttpRequestEventTarget.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Log from '../utils/log.js'
88
const log = new Log('XMLHttpRequestEventTarget')
99

1010
log.disable()
11+
// log.level(3)
1112

1213
export default class XMLHttpRequestEventTarget extends EventTarget {
1314

0 commit comments

Comments
 (0)