react-native-blob-util 0.19.0 → 0.19.2
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/.eslintrc.js +56 -56
- package/LICENSE +21 -21
- package/Migration.md +41 -41
- package/README.md +1071 -1071
- package/android/gradle.properties +1 -1
- package/android/proguard-rules.pro +17 -17
- package/android/src/main/AndroidManifest.xml +39 -40
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java +57 -57
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConst.java +20 -20
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java +2 -2
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java +9 -9
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilImpl.java +431 -431
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilProgressConfig.java +39 -39
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +972 -968
- package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilDefaultResp.java +98 -98
- package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java +157 -157
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileDescription.java +19 -19
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileProvider.java +4 -4
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/MimeType.java +70 -70
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/PathResolver.java +223 -223
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/Tls12SocketFactory.java +68 -68
- package/android/src/main/res/values/strings.xml +3 -3
- package/android/src/main/res/xml/provider_paths.xml +12 -12
- package/android/src/newarch/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java +1 -1
- package/android.js +58 -58
- package/class/ReactNativeBlobUtilCanceledFetchError.js +9 -9
- package/class/ReactNativeBlobUtilFile.js +5 -5
- package/class/ReactNativeBlobUtilReadStream.js +84 -84
- package/class/ReactNativeBlobUtilSession.js +68 -68
- package/class/ReactNativeBlobUtilWriteStream.js +50 -50
- package/class/StatefulPromise.js +7 -7
- package/codegenSpecs/NativeBlobUtils.js +86 -82
- package/components/Fetch.onPress.js +10 -10
- package/components/Fetch.when.js +15 -15
- package/fetch.js +344 -344
- package/fs.js +480 -480
- package/index.d.ts +871 -871
- package/index.js +64 -64
- package/index.js.flow +191 -191
- package/index.web.js +1 -1
- package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.h +55 -55
- package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.mm +993 -993
- package/ios/ReactNativeBlobUtil.xcodeproj/project.pbxproj +403 -403
- package/ios/ReactNativeBlobUtilConst.h +58 -58
- package/ios/ReactNativeBlobUtilConst.mm +47 -47
- package/ios/ReactNativeBlobUtilFS.h +108 -108
- package/ios/ReactNativeBlobUtilFS.mm +977 -977
- package/ios/ReactNativeBlobUtilFileTransformer.h +23 -23
- package/ios/ReactNativeBlobUtilFileTransformer.mm +20 -20
- package/ios/ReactNativeBlobUtilNetwork.h +49 -49
- package/ios/ReactNativeBlobUtilNetwork.mm +159 -159
- package/ios/ReactNativeBlobUtilProgress.h +32 -32
- package/ios/ReactNativeBlobUtilProgress.mm +57 -57
- package/ios/ReactNativeBlobUtilReqBuilder.h +37 -37
- package/ios/ReactNativeBlobUtilReqBuilder.mm +292 -292
- package/ios/ReactNativeBlobUtilRequest.h +48 -48
- package/ios/ReactNativeBlobUtilRequest.mm +535 -535
- package/ios.js +61 -61
- package/json-stream.js +42 -42
- package/lib/oboe-browser.js +2703 -2703
- package/mediacollection.js +36 -36
- package/package.json +55 -55
- package/polyfill/Blob.js +362 -362
- package/polyfill/Event.js +11 -11
- package/polyfill/EventTarget.js +78 -78
- package/polyfill/Fetch.js +219 -219
- package/polyfill/File.js +27 -27
- package/polyfill/FileReader.js +91 -91
- package/polyfill/ProgressEvent.js +32 -32
- package/polyfill/XMLHttpRequest.js +466 -466
- package/polyfill/XMLHttpRequestEventTarget.js +126 -126
- package/polyfill/index.js +11 -11
- package/react-native-blob-util.podspec +45 -45
- package/types.js +69 -69
- package/utils/log.js +40 -40
- package/utils/unicode.js +7 -7
- package/utils/uri.js +33 -33
- package/utils/uuid.js +4 -4
- package/windows/README.md +2 -2
- package/windows/ReactNativeBlobUtil/PropertySheet.props +15 -15
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.cpp +1701 -1693
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.def +3 -3
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.h +379 -366
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj +181 -181
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj.filters +31 -31
- package/windows/ReactNativeBlobUtil/ReactPackageProvider.cpp +15 -15
- package/windows/ReactNativeBlobUtil/ReactPackageProvider.h +20 -20
- package/windows/ReactNativeBlobUtil/ReactPackageProvider.idl +9 -9
- package/windows/ReactNativeBlobUtil/packages.config +3 -3
- package/windows/ReactNativeBlobUtil/pch.cpp +1 -1
- package/windows/ReactNativeBlobUtil/pch.h +3 -3
package/polyfill/Event.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// Copyright 2016 wkh237@github. All rights reserved.
|
|
2
|
-
// Use of this source code is governed by a MIT-style license that can be
|
|
3
|
-
// found in the LICENSE file.
|
|
4
|
-
|
|
5
|
-
export default class Event {
|
|
6
|
-
|
|
7
|
-
constructor() {
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
}
|
|
1
|
+
// Copyright 2016 wkh237@github. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a MIT-style license that can be
|
|
3
|
+
// found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
export default class Event {
|
|
6
|
+
|
|
7
|
+
constructor() {
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
}
|
package/polyfill/EventTarget.js
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
// Copyright 2016 wkh237@github. All rights reserved.
|
|
2
|
-
// Use of this source code is governed by a MIT-style license that can be
|
|
3
|
-
// found in the LICENSE file.
|
|
4
|
-
|
|
5
|
-
import Log from '../utils/log.js';
|
|
6
|
-
|
|
7
|
-
const log = new Log('EventTarget');
|
|
8
|
-
|
|
9
|
-
log.disable();
|
|
10
|
-
|
|
11
|
-
export default class EventTarget {
|
|
12
|
-
|
|
13
|
-
listeners: any;
|
|
14
|
-
|
|
15
|
-
constructor() {
|
|
16
|
-
log.info('constructor called');
|
|
17
|
-
this.listeners = {};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Add an event listener to given event type
|
|
22
|
-
* @param {string} type Event type string
|
|
23
|
-
* @param {(Event) => void} cb Event handler function
|
|
24
|
-
*/
|
|
25
|
-
addEventListener(type: string, cb: () => void) {
|
|
26
|
-
log.info('add event listener', type, cb);
|
|
27
|
-
if (!(type in this.listeners)) {
|
|
28
|
-
this.listeners[type] = [];
|
|
29
|
-
}
|
|
30
|
-
this.listeners[type].push(cb);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Remove an event listener
|
|
35
|
-
* @param {string} type Type of the event listener
|
|
36
|
-
* @param {()=>void} cb Event listener function.
|
|
37
|
-
* @return {[type]} [description]
|
|
38
|
-
*/
|
|
39
|
-
removeEventListener(type: string, cb: () => void) {
|
|
40
|
-
log.info('remove event listener', type, cb);
|
|
41
|
-
if (!(type in this.listeners))
|
|
42
|
-
return;
|
|
43
|
-
let handlers = this.listeners[type];
|
|
44
|
-
for (let i in handlers) {
|
|
45
|
-
if (cb === handlers[i]) {
|
|
46
|
-
handlers.splice(i, 1);
|
|
47
|
-
return this.removeEventListener(type, cb);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Dispatch an event
|
|
54
|
-
* @param {string} type Event type.
|
|
55
|
-
* @param {Event} event Event data payload.
|
|
56
|
-
*/
|
|
57
|
-
dispatchEvent(type: string, event: Event) {
|
|
58
|
-
log.info('dispatch event', event);
|
|
59
|
-
if (!(type in this.listeners))
|
|
60
|
-
return;
|
|
61
|
-
let handlers = this.listeners[type];
|
|
62
|
-
for (let i in handlers) {
|
|
63
|
-
handlers[i].call(this, event);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Remove all registered listeners from this object.
|
|
70
|
-
* @nonstandard
|
|
71
|
-
* @return {[type]} [description]
|
|
72
|
-
*/
|
|
73
|
-
clearEventListeners() {
|
|
74
|
-
for (let i in this.listeners) {
|
|
75
|
-
delete this.listeners[i];
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
1
|
+
// Copyright 2016 wkh237@github. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a MIT-style license that can be
|
|
3
|
+
// found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
import Log from '../utils/log.js';
|
|
6
|
+
|
|
7
|
+
const log = new Log('EventTarget');
|
|
8
|
+
|
|
9
|
+
log.disable();
|
|
10
|
+
|
|
11
|
+
export default class EventTarget {
|
|
12
|
+
|
|
13
|
+
listeners: any;
|
|
14
|
+
|
|
15
|
+
constructor() {
|
|
16
|
+
log.info('constructor called');
|
|
17
|
+
this.listeners = {};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Add an event listener to given event type
|
|
22
|
+
* @param {string} type Event type string
|
|
23
|
+
* @param {(Event) => void} cb Event handler function
|
|
24
|
+
*/
|
|
25
|
+
addEventListener(type: string, cb: () => void) {
|
|
26
|
+
log.info('add event listener', type, cb);
|
|
27
|
+
if (!(type in this.listeners)) {
|
|
28
|
+
this.listeners[type] = [];
|
|
29
|
+
}
|
|
30
|
+
this.listeners[type].push(cb);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Remove an event listener
|
|
35
|
+
* @param {string} type Type of the event listener
|
|
36
|
+
* @param {()=>void} cb Event listener function.
|
|
37
|
+
* @return {[type]} [description]
|
|
38
|
+
*/
|
|
39
|
+
removeEventListener(type: string, cb: () => void) {
|
|
40
|
+
log.info('remove event listener', type, cb);
|
|
41
|
+
if (!(type in this.listeners))
|
|
42
|
+
return;
|
|
43
|
+
let handlers = this.listeners[type];
|
|
44
|
+
for (let i in handlers) {
|
|
45
|
+
if (cb === handlers[i]) {
|
|
46
|
+
handlers.splice(i, 1);
|
|
47
|
+
return this.removeEventListener(type, cb);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Dispatch an event
|
|
54
|
+
* @param {string} type Event type.
|
|
55
|
+
* @param {Event} event Event data payload.
|
|
56
|
+
*/
|
|
57
|
+
dispatchEvent(type: string, event: Event) {
|
|
58
|
+
log.info('dispatch event', event);
|
|
59
|
+
if (!(type in this.listeners))
|
|
60
|
+
return;
|
|
61
|
+
let handlers = this.listeners[type];
|
|
62
|
+
for (let i in handlers) {
|
|
63
|
+
handlers[i].call(this, event);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Remove all registered listeners from this object.
|
|
70
|
+
* @nonstandard
|
|
71
|
+
* @return {[type]} [description]
|
|
72
|
+
*/
|
|
73
|
+
clearEventListeners() {
|
|
74
|
+
for (let i in this.listeners) {
|
|
75
|
+
delete this.listeners[i];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
79
|
}
|
package/polyfill/Fetch.js
CHANGED
|
@@ -1,219 +1,219 @@
|
|
|
1
|
-
import Log from '../utils/log.js';
|
|
2
|
-
import Blob from './Blob';
|
|
3
|
-
import {config as RNconfig} from "../fetch";
|
|
4
|
-
import type {ReactNativeBlobUtilConfig} from "../types";
|
|
5
|
-
import URIUtil from "../utils/uri";
|
|
6
|
-
import {FetchBlobResponse} from "../class/ReactNativeBlobUtilBlobResponse";
|
|
7
|
-
|
|
8
|
-
const log = new Log('FetchPolyfill');
|
|
9
|
-
|
|
10
|
-
log.disable();
|
|
11
|
-
// log.level(3)
|
|
12
|
-
|
|
13
|
-
export default class Fetch {
|
|
14
|
-
|
|
15
|
-
constructor(config: ReactNativeBlobUtilConfig) {
|
|
16
|
-
Object.assign(this, new ReactNativeBlobUtilFetchPolyfill(config));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
class ReactNativeBlobUtilFetchPolyfill {
|
|
22
|
-
|
|
23
|
-
constructor(config: ReactNativeBlobUtilConfig) {
|
|
24
|
-
this.build = () => (url, options = {}) => {
|
|
25
|
-
|
|
26
|
-
let body = options.body;
|
|
27
|
-
let promise = Promise.resolve();
|
|
28
|
-
let blobCache = null;
|
|
29
|
-
|
|
30
|
-
options.headers = options.headers || {};
|
|
31
|
-
let ctype = options['Content-Type'] || options['content-type'];
|
|
32
|
-
let ctypeH = options.headers['Content-Type'] || options.headers['content-type'];
|
|
33
|
-
options.headers['Content-Type'] = ctype || ctypeH;
|
|
34
|
-
options.headers['content-type'] = ctype || ctypeH;
|
|
35
|
-
options.method = options.method || 'GET';
|
|
36
|
-
if (body) {
|
|
37
|
-
// When the request body is an instance of FormData, create a Blob cache
|
|
38
|
-
// to upload the body.
|
|
39
|
-
if (body instanceof FormData) {
|
|
40
|
-
log.verbose('convert FormData to blob body');
|
|
41
|
-
promise = Blob.build(body).then((b) => {
|
|
42
|
-
blobCache = b;
|
|
43
|
-
|
|
44
|
-
const contentType = 'multipart/form-data;boundary=' + b.multipartBoundary
|
|
45
|
-
options.headers['Content-Type'] = contentType;
|
|
46
|
-
options.headers['content-type'] = contentType;
|
|
47
|
-
return Promise.resolve(URIUtil.wrap(b._ref));
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
// When request body is a Blob, use file URI of the Blob as request body.
|
|
51
|
-
else if (body.isReactNativeBlobUtilPolyfill)
|
|
52
|
-
promise = Promise.resolve(URIUtil.wrap(body.blobPath));
|
|
53
|
-
else if (typeof body !== 'object' && options.headers['Content-Type'] !== 'application/json')
|
|
54
|
-
promise = Promise.resolve(JSON.stringify(body));
|
|
55
|
-
else if (typeof body !== 'string')
|
|
56
|
-
promise = Promise.resolve(body.toString());
|
|
57
|
-
// send it as-is, leave the native module decide how to send the body.
|
|
58
|
-
else
|
|
59
|
-
promise = Promise.resolve(body);
|
|
60
|
-
}
|
|
61
|
-
// task is a progress reportable and cancellable Promise, however,
|
|
62
|
-
// task.then is not, so we have to extend task.then with progress and
|
|
63
|
-
// cancel function
|
|
64
|
-
let progressHandler, uploadHandler, cancelHandler;
|
|
65
|
-
let scopedTask = null;
|
|
66
|
-
let statefulPromise = promise
|
|
67
|
-
.then((body) => {
|
|
68
|
-
let task = RNconfig(config)
|
|
69
|
-
.fetch(options.method, url, options.headers, body);
|
|
70
|
-
scopedTask = task;
|
|
71
|
-
if (progressHandler)
|
|
72
|
-
task.progress(progressHandler);
|
|
73
|
-
if (uploadHandler)
|
|
74
|
-
task.uploadProgress(uploadHandler);
|
|
75
|
-
if (cancelHandler)
|
|
76
|
-
task.cancel();
|
|
77
|
-
return task.then((resp) => {
|
|
78
|
-
log.verbose('response', resp);
|
|
79
|
-
// release blob cache created when sending request
|
|
80
|
-
if (blobCache !== null && blobCache instanceof Blob)
|
|
81
|
-
blobCache.close();
|
|
82
|
-
return Promise.resolve(new ReactNativeBlobUtilFetchResponse(resp));
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
// extend task.then progress with report and cancelling functions
|
|
87
|
-
statefulPromise.progress = (fn) => {
|
|
88
|
-
progressHandler = fn;
|
|
89
|
-
};
|
|
90
|
-
statefulPromise.uploadProgress = (fn) => {
|
|
91
|
-
uploadHandler = fn;
|
|
92
|
-
};
|
|
93
|
-
statefulPromise.cancel = () => {
|
|
94
|
-
cancelHandler = true;
|
|
95
|
-
if (scopedTask && scopedTask.cancel)
|
|
96
|
-
scopedTask.cancel();
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
return statefulPromise;
|
|
100
|
-
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
class ReactNativeBlobUtilFetchResponse {
|
|
107
|
-
|
|
108
|
-
constructor(resp: FetchBlobResponse) {
|
|
109
|
-
let info = resp.info();
|
|
110
|
-
this.headers = info.headers;
|
|
111
|
-
this.ok = info.status >= 200 && info.status <= 299,
|
|
112
|
-
this.status = info.status;
|
|
113
|
-
this.type = 'basic';
|
|
114
|
-
this.bodyUsed = false;
|
|
115
|
-
this.resp = resp;
|
|
116
|
-
this.rnfbRespInfo = info;
|
|
117
|
-
this.rnfbResp = resp;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
rawResp() {
|
|
121
|
-
return Promise.resolve(this.rnfbResp);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
arrayBuffer() {
|
|
125
|
-
log.verbose('to arrayBuffer', this.rnfbRespInfo);
|
|
126
|
-
this.bodyUsed = true;
|
|
127
|
-
return readArrayBuffer(this.rnfbResp, this.rnfbRespInfo);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
text() {
|
|
131
|
-
log.verbose('to text', this.rnfbResp, this.rnfbRespInfo);
|
|
132
|
-
this.bodyUsed = true;
|
|
133
|
-
return readText(this.rnfbResp, this.rnfbRespInfo);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
json() {
|
|
137
|
-
log.verbose('to json', this.rnfbResp, this.rnfbRespInfo);
|
|
138
|
-
this.bodyUsed = true;
|
|
139
|
-
return readJSON(this.rnfbResp, this.rnfbRespInfo);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
blob() {
|
|
143
|
-
log.verbose('to blob', this.rnfbResp, this.rnfbRespInfo);
|
|
144
|
-
this.bodyUsed = true;
|
|
145
|
-
return readBlob(this.rnfbResp, this.rnfbRespInfo);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Get response data as array.
|
|
151
|
-
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
152
|
-
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
153
|
-
* @return {Promise<Array>}
|
|
154
|
-
*/
|
|
155
|
-
function readArrayBuffer(resp, info): Promise<Array> {
|
|
156
|
-
switch (info.rnfbEncode) {
|
|
157
|
-
case 'path':
|
|
158
|
-
return resp.readFile('ascii');
|
|
159
|
-
break;
|
|
160
|
-
default:
|
|
161
|
-
let buffer = [];
|
|
162
|
-
let str = resp.text();
|
|
163
|
-
for (let i in str) {
|
|
164
|
-
buffer[i] = str.charCodeAt(i);
|
|
165
|
-
}
|
|
166
|
-
return Promise.resolve(buffer);
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Get response data as string.
|
|
173
|
-
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
174
|
-
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
175
|
-
* @return {Promise<string>}
|
|
176
|
-
*/
|
|
177
|
-
function readText(resp, info): Promise<string> {
|
|
178
|
-
switch (info.rnfbEncode) {
|
|
179
|
-
case 'base64':
|
|
180
|
-
return Promise.resolve(resp.text());
|
|
181
|
-
break;
|
|
182
|
-
case 'path':
|
|
183
|
-
return resp.text();
|
|
184
|
-
break;
|
|
185
|
-
default:
|
|
186
|
-
return Promise.resolve(resp.text());
|
|
187
|
-
break;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Get response data as ReactNativeBlobUtil Blob polyfill object.
|
|
194
|
-
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
195
|
-
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
196
|
-
* @return {Promise<Blob>}
|
|
197
|
-
*/
|
|
198
|
-
function readBlob(resp, info): Promise<Blob> {
|
|
199
|
-
log.verbose('readBlob', resp, info);
|
|
200
|
-
return resp.blob();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Get response data as JSON object.
|
|
205
|
-
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
206
|
-
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
207
|
-
* @return {Promise<object>}
|
|
208
|
-
*/
|
|
209
|
-
function readJSON(resp, info): Promise<object> {
|
|
210
|
-
log.verbose('readJSON', resp, info);
|
|
211
|
-
switch (info.rnfbEncode) {
|
|
212
|
-
case 'base64':
|
|
213
|
-
return Promise.resolve(resp.json());
|
|
214
|
-
case 'path':
|
|
215
|
-
return resp.json();
|
|
216
|
-
default:
|
|
217
|
-
return Promise.resolve(resp.json());
|
|
218
|
-
}
|
|
219
|
-
}
|
|
1
|
+
import Log from '../utils/log.js';
|
|
2
|
+
import Blob from './Blob';
|
|
3
|
+
import {config as RNconfig} from "../fetch";
|
|
4
|
+
import type {ReactNativeBlobUtilConfig} from "../types";
|
|
5
|
+
import URIUtil from "../utils/uri";
|
|
6
|
+
import {FetchBlobResponse} from "../class/ReactNativeBlobUtilBlobResponse";
|
|
7
|
+
|
|
8
|
+
const log = new Log('FetchPolyfill');
|
|
9
|
+
|
|
10
|
+
log.disable();
|
|
11
|
+
// log.level(3)
|
|
12
|
+
|
|
13
|
+
export default class Fetch {
|
|
14
|
+
|
|
15
|
+
constructor(config: ReactNativeBlobUtilConfig) {
|
|
16
|
+
Object.assign(this, new ReactNativeBlobUtilFetchPolyfill(config));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class ReactNativeBlobUtilFetchPolyfill {
|
|
22
|
+
|
|
23
|
+
constructor(config: ReactNativeBlobUtilConfig) {
|
|
24
|
+
this.build = () => (url, options = {}) => {
|
|
25
|
+
|
|
26
|
+
let body = options.body;
|
|
27
|
+
let promise = Promise.resolve();
|
|
28
|
+
let blobCache = null;
|
|
29
|
+
|
|
30
|
+
options.headers = options.headers || {};
|
|
31
|
+
let ctype = options['Content-Type'] || options['content-type'];
|
|
32
|
+
let ctypeH = options.headers['Content-Type'] || options.headers['content-type'];
|
|
33
|
+
options.headers['Content-Type'] = ctype || ctypeH;
|
|
34
|
+
options.headers['content-type'] = ctype || ctypeH;
|
|
35
|
+
options.method = options.method || 'GET';
|
|
36
|
+
if (body) {
|
|
37
|
+
// When the request body is an instance of FormData, create a Blob cache
|
|
38
|
+
// to upload the body.
|
|
39
|
+
if (body instanceof FormData) {
|
|
40
|
+
log.verbose('convert FormData to blob body');
|
|
41
|
+
promise = Blob.build(body).then((b) => {
|
|
42
|
+
blobCache = b;
|
|
43
|
+
|
|
44
|
+
const contentType = 'multipart/form-data;boundary=' + b.multipartBoundary
|
|
45
|
+
options.headers['Content-Type'] = contentType;
|
|
46
|
+
options.headers['content-type'] = contentType;
|
|
47
|
+
return Promise.resolve(URIUtil.wrap(b._ref));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// When request body is a Blob, use file URI of the Blob as request body.
|
|
51
|
+
else if (body.isReactNativeBlobUtilPolyfill)
|
|
52
|
+
promise = Promise.resolve(URIUtil.wrap(body.blobPath));
|
|
53
|
+
else if (typeof body !== 'object' && options.headers['Content-Type'] !== 'application/json')
|
|
54
|
+
promise = Promise.resolve(JSON.stringify(body));
|
|
55
|
+
else if (typeof body !== 'string')
|
|
56
|
+
promise = Promise.resolve(body.toString());
|
|
57
|
+
// send it as-is, leave the native module decide how to send the body.
|
|
58
|
+
else
|
|
59
|
+
promise = Promise.resolve(body);
|
|
60
|
+
}
|
|
61
|
+
// task is a progress reportable and cancellable Promise, however,
|
|
62
|
+
// task.then is not, so we have to extend task.then with progress and
|
|
63
|
+
// cancel function
|
|
64
|
+
let progressHandler, uploadHandler, cancelHandler;
|
|
65
|
+
let scopedTask = null;
|
|
66
|
+
let statefulPromise = promise
|
|
67
|
+
.then((body) => {
|
|
68
|
+
let task = RNconfig(config)
|
|
69
|
+
.fetch(options.method, url, options.headers, body);
|
|
70
|
+
scopedTask = task;
|
|
71
|
+
if (progressHandler)
|
|
72
|
+
task.progress(progressHandler);
|
|
73
|
+
if (uploadHandler)
|
|
74
|
+
task.uploadProgress(uploadHandler);
|
|
75
|
+
if (cancelHandler)
|
|
76
|
+
task.cancel();
|
|
77
|
+
return task.then((resp) => {
|
|
78
|
+
log.verbose('response', resp);
|
|
79
|
+
// release blob cache created when sending request
|
|
80
|
+
if (blobCache !== null && blobCache instanceof Blob)
|
|
81
|
+
blobCache.close();
|
|
82
|
+
return Promise.resolve(new ReactNativeBlobUtilFetchResponse(resp));
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// extend task.then progress with report and cancelling functions
|
|
87
|
+
statefulPromise.progress = (fn) => {
|
|
88
|
+
progressHandler = fn;
|
|
89
|
+
};
|
|
90
|
+
statefulPromise.uploadProgress = (fn) => {
|
|
91
|
+
uploadHandler = fn;
|
|
92
|
+
};
|
|
93
|
+
statefulPromise.cancel = () => {
|
|
94
|
+
cancelHandler = true;
|
|
95
|
+
if (scopedTask && scopedTask.cancel)
|
|
96
|
+
scopedTask.cancel();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return statefulPromise;
|
|
100
|
+
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
class ReactNativeBlobUtilFetchResponse {
|
|
107
|
+
|
|
108
|
+
constructor(resp: FetchBlobResponse) {
|
|
109
|
+
let info = resp.info();
|
|
110
|
+
this.headers = info.headers;
|
|
111
|
+
this.ok = info.status >= 200 && info.status <= 299,
|
|
112
|
+
this.status = info.status;
|
|
113
|
+
this.type = 'basic';
|
|
114
|
+
this.bodyUsed = false;
|
|
115
|
+
this.resp = resp;
|
|
116
|
+
this.rnfbRespInfo = info;
|
|
117
|
+
this.rnfbResp = resp;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
rawResp() {
|
|
121
|
+
return Promise.resolve(this.rnfbResp);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
arrayBuffer() {
|
|
125
|
+
log.verbose('to arrayBuffer', this.rnfbRespInfo);
|
|
126
|
+
this.bodyUsed = true;
|
|
127
|
+
return readArrayBuffer(this.rnfbResp, this.rnfbRespInfo);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
text() {
|
|
131
|
+
log.verbose('to text', this.rnfbResp, this.rnfbRespInfo);
|
|
132
|
+
this.bodyUsed = true;
|
|
133
|
+
return readText(this.rnfbResp, this.rnfbRespInfo);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
json() {
|
|
137
|
+
log.verbose('to json', this.rnfbResp, this.rnfbRespInfo);
|
|
138
|
+
this.bodyUsed = true;
|
|
139
|
+
return readJSON(this.rnfbResp, this.rnfbRespInfo);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
blob() {
|
|
143
|
+
log.verbose('to blob', this.rnfbResp, this.rnfbRespInfo);
|
|
144
|
+
this.bodyUsed = true;
|
|
145
|
+
return readBlob(this.rnfbResp, this.rnfbRespInfo);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Get response data as array.
|
|
151
|
+
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
152
|
+
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
153
|
+
* @return {Promise<Array>}
|
|
154
|
+
*/
|
|
155
|
+
function readArrayBuffer(resp, info): Promise<Array> {
|
|
156
|
+
switch (info.rnfbEncode) {
|
|
157
|
+
case 'path':
|
|
158
|
+
return resp.readFile('ascii');
|
|
159
|
+
break;
|
|
160
|
+
default:
|
|
161
|
+
let buffer = [];
|
|
162
|
+
let str = resp.text();
|
|
163
|
+
for (let i in str) {
|
|
164
|
+
buffer[i] = str.charCodeAt(i);
|
|
165
|
+
}
|
|
166
|
+
return Promise.resolve(buffer);
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Get response data as string.
|
|
173
|
+
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
174
|
+
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
175
|
+
* @return {Promise<string>}
|
|
176
|
+
*/
|
|
177
|
+
function readText(resp, info): Promise<string> {
|
|
178
|
+
switch (info.rnfbEncode) {
|
|
179
|
+
case 'base64':
|
|
180
|
+
return Promise.resolve(resp.text());
|
|
181
|
+
break;
|
|
182
|
+
case 'path':
|
|
183
|
+
return resp.text();
|
|
184
|
+
break;
|
|
185
|
+
default:
|
|
186
|
+
return Promise.resolve(resp.text());
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Get response data as ReactNativeBlobUtil Blob polyfill object.
|
|
194
|
+
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
195
|
+
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
196
|
+
* @return {Promise<Blob>}
|
|
197
|
+
*/
|
|
198
|
+
function readBlob(resp, info): Promise<Blob> {
|
|
199
|
+
log.verbose('readBlob', resp, info);
|
|
200
|
+
return resp.blob();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Get response data as JSON object.
|
|
205
|
+
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
|
|
206
|
+
* @param {ReactNativeBlobUtilResponseInfo} info Response informations.
|
|
207
|
+
* @return {Promise<object>}
|
|
208
|
+
*/
|
|
209
|
+
function readJSON(resp, info): Promise<object> {
|
|
210
|
+
log.verbose('readJSON', resp, info);
|
|
211
|
+
switch (info.rnfbEncode) {
|
|
212
|
+
case 'base64':
|
|
213
|
+
return Promise.resolve(resp.json());
|
|
214
|
+
case 'path':
|
|
215
|
+
return resp.json();
|
|
216
|
+
default:
|
|
217
|
+
return Promise.resolve(resp.json());
|
|
218
|
+
}
|
|
219
|
+
}
|
package/polyfill/File.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
// Copyright 2016 wkh237@github. All rights reserved.
|
|
2
|
-
// Use of this source code is governed by a MIT-style license that can be
|
|
3
|
-
// found in the LICENSE file.
|
|
4
|
-
|
|
5
|
-
import Blob from './Blob.js';
|
|
6
|
-
|
|
7
|
-
export default class File extends Blob {
|
|
8
|
-
|
|
9
|
-
name: string = '';
|
|
10
|
-
|
|
11
|
-
static build(name: string, data: any, cType: string): Promise<File> {
|
|
12
|
-
return new Promise((resolve, reject) => {
|
|
13
|
-
if (data === undefined) {
|
|
14
|
-
reject(new TypeError('data is undefined'));
|
|
15
|
-
}
|
|
16
|
-
new File(data, cType).onCreated((f) => {
|
|
17
|
-
f.name = name;
|
|
18
|
-
resolve(f);
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
constructor(data: any, cType: string) {
|
|
24
|
-
super(data, cType);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
}
|
|
1
|
+
// Copyright 2016 wkh237@github. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a MIT-style license that can be
|
|
3
|
+
// found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
import Blob from './Blob.js';
|
|
6
|
+
|
|
7
|
+
export default class File extends Blob {
|
|
8
|
+
|
|
9
|
+
name: string = '';
|
|
10
|
+
|
|
11
|
+
static build(name: string, data: any, cType: string): Promise<File> {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
if (data === undefined) {
|
|
14
|
+
reject(new TypeError('data is undefined'));
|
|
15
|
+
}
|
|
16
|
+
new File(data, cType).onCreated((f) => {
|
|
17
|
+
f.name = name;
|
|
18
|
+
resolve(f);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
constructor(data: any, cType: string) {
|
|
24
|
+
super(data, cType);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
}
|