Skip to content

Commit 602d8eb

Browse files
author
Ron Radtke
committed
some more fixes for another import cycle
1 parent 9fa85bf commit 602d8eb

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

class/ReactnativeBlobUtilBlobResponse.js renamed to class/ReactNativeBlobUtilBlobResponse.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {ReactNativeBlobUtilResponseInfo, ReactNativeBlobUtilStream} from "../types";
22
import fs from "../fs";
3-
import polyfill from "../polyfill";
3+
import Blob from "../polyfill/Blob";
4+
import ReactNativeBlobUtilSession from "./ReactNativeBlobUtilSession";
5+
import URIUtil from "../utils/uri";
46

57
/**
68
* ReactNativeBlobUtil response object class.
@@ -55,18 +57,17 @@ export class FetchBlobResponse {
5557
* @return {Promise<Blob>} Return a promise resolves Blob object.
5658
*/
5759
this.blob = (): Promise<Blob> => {
58-
let Blob = polyfill.Blob;
5960
let cType = info.headers['Content-Type'] || info.headers['content-type'];
6061
return new Promise((resolve, reject) => {
6162
switch (this.type) {
6263
case 'base64':
6364
Blob.build(this.data, {type: cType + ';BASE64'}).then(resolve);
6465
break;
6566
case 'path':
66-
polyfill.Blob.build(wrap(this.data), {type: cType}).then(resolve);
67+
Blob.build(URIUtil.wrap(this.data), {type: cType}).then(resolve);
6768
break;
6869
default:
69-
polyfill.Blob.build(this.data, {type: 'text/plain'}).then(resolve);
70+
Blob.build(this.data, {type: 'text/plain'}).then(resolve);
7071
break;
7172
}
7273
});
@@ -76,7 +77,6 @@ export class FetchBlobResponse {
7677
* @return {string} Decoded base64 string.
7778
*/
7879
this.text = (): string | Promise<any> => {
79-
let res = this.data;
8080
switch (this.type) {
8181
case 'base64':
8282
return base64.decode(this.data);
@@ -123,7 +123,7 @@ export class FetchBlobResponse {
123123
let path = this.path();
124124
if (!path || this.type !== 'path')
125125
return;
126-
return unlink(path);
126+
return fs.unlink(path);
127127
};
128128
/**
129129
* get path of response temp file
@@ -137,7 +137,7 @@ export class FetchBlobResponse {
137137

138138
this.session = (name: string): ReactNativeBlobUtilSession | null => {
139139
if (this.type === 'path')
140-
return session(name).add(this.data);
140+
return fs.session(name).add(this.data);
141141
else {
142142
console.warn('only file paths can be add into session.');
143143
return null;
@@ -150,7 +150,7 @@ export class FetchBlobResponse {
150150
*/
151151
this.readStream = (encoding: 'base64' | 'utf8' | 'ascii'): ReactNativeBlobUtilStream | null => {
152152
if (this.type === 'path') {
153-
return readStream(this.data, encoding);
153+
return fs.readStream(this.data, encoding);
154154
}
155155
else {
156156
console.warn('ReactNativeBlobUtil', 'this response data does not contains any available stream');
@@ -165,7 +165,7 @@ export class FetchBlobResponse {
165165
*/
166166
this.readFile = (encoding: 'base64' | 'utf8' | 'ascii') => {
167167
if (this.type === 'path') {
168-
return readFile(this.data, encoding);
168+
return fs.readFile(this.data, encoding);
169169
}
170170
else {
171171
console.warn('ReactNativeBlobUtil', 'this response does not contains a readable file');

fetch.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import URIUtil from "./utils/uri";
33
import fs from "./fs";
44
import getUUID from "./utils/uuid";
55
import {DeviceEventEmitter, NativeModules} from "react-native";
6-
import {FetchBlobResponse} from "./class/ReactnativeBlobUtilBlobResponse";
6+
import {FetchBlobResponse} from "./class/ReactNativeBlobUtilBlobResponse";
77

88
const emitter = DeviceEventEmitter;
99
const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil;
@@ -22,11 +22,6 @@ emitter.addListener("ReactNativeBlobUtilMessage", (e) => {
2222
}
2323
});
2424

25-
export function wrap(path: string): string {
26-
const prefix = path.startsWith('content://') ? 'ReactNativeBlobUtil-content://' : 'ReactNativeBlobUtil-file://';
27-
return prefix + path;
28-
}
29-
3025
/**
3126
* Calling this method will inject configurations into followed `fetch` method.
3227
* @param {ReactNativeBlobUtilConfig} options

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import polyfill from './polyfill';
1111
import android from './android';
1212
import ios from './ios';
1313
import JSONStream from './json-stream';
14-
import {config, fetch, wrap} from './fetch';
14+
import {config, fetch} from './fetch';
15+
import URIUtil from "./utils/uri";
1516

1617
const {
1718
ReactNativeBlobUtilSession,
@@ -32,6 +33,7 @@ const {
3233
const Blob = polyfill.Blob;
3334
const emitter = DeviceEventEmitter;
3435
const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil;
36+
const wrap = URIUtil.wrap;
3537

3638
// when app resumes, check if there's any expired network task and trigger
3739
// their .expire event
@@ -51,9 +53,10 @@ if (!ReactNativeBlobUtil || !ReactNativeBlobUtil.fetchBlobForm || !ReactNativeBl
5153
'and restart RN packager or manually compile IOS/Android project.'
5254
);
5355
}
56+
5457
export {ReactNativeBlobUtilConfig, ReactNativeBlobUtilResponseInfo, ReactNativeBlobUtilStream} from './types';
5558
export URIUtil from './utils/uri';
56-
export {FetchBlobResponse} from './class/ReactnativeBlobUtilBlobResponse';
59+
export {FetchBlobResponse} from './class/ReactNativeBlobUtilBlobResponse';
5760
export getUUID from './utils/uuid';
5861
export default {
5962
fetch,
@@ -65,5 +68,5 @@ export default {
6568
fs,
6669
wrap,
6770
polyfill,
68-
JSONStream
71+
JSONStream,
6972
};

polyfill/Blob.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import fs from '../fs.js';
66
import getUUID from '../utils/uuid';
77
import Log from '../utils/log.js';
8+
import URIUtil from "../utils/uri";
89
import EventTarget from './EventTarget';
910

10-
import {wrap} from "../fetch";
11-
1211
const log = new Log('Blob');
1312
const blobCacheDir = fs.dirs.DocumentDir + '/ReactNativeBlobUtil-blobs/';
1413

@@ -237,7 +236,7 @@ export default class Blob extends EventTarget {
237236
let resPath = blobCacheDir + getBlobName();
238237
let pass = false;
239238
log.debug('fs.slice new blob will at', resPath);
240-
let result = new Blob(wrap(resPath), {type: contentType}, true);
239+
let result = new Blob(URIUtil.wrap(resPath), {type: contentType}, true);
241240
fs.exists(blobCacheDir)
242241
.then((exist) => {
243242
if (exist)

polyfill/Fetch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Log from '../utils/log.js';
22
import Blob from './Blob';
33
import {config as RNconfig, wrap} from "../fetch";
44
import type {ReactNativeBlobUtilConfig} from "../types";
5+
import {FetchBlobResponse} from "../class/ReactNativeBlobUtilBlobResponse";
56

67
const log = new Log('FetchPolyfill');
78

utils/uri.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
export default {
22

3-
isFileURI : (uri:string):boolean => {
4-
if(typeof uri !== 'string')
5-
return false;
6-
return /^ReactNativeBlobUtil-file\:\/\//.test(uri);
7-
},
3+
isFileURI: (uri: string): boolean => {
4+
if (typeof uri !== 'string')
5+
return false;
6+
return /^ReactNativeBlobUtil-file\:\/\//.test(uri);
7+
},
88

9-
isJSONStreamURI : (uri:string):boolean => {
10-
if(typeof uri !== 'string')
11-
return false;
12-
return /^JSONStream\:\/\//.test(uri);
13-
},
9+
isJSONStreamURI: (uri: string): boolean => {
10+
if (typeof uri !== 'string')
11+
return false;
12+
return /^JSONStream\:\/\//.test(uri);
13+
},
1414

15-
removeURIScheme : (uri:string, iterations:number):string => {
16-
iterations = iterations || 1;
17-
let result = uri;
18-
for(let i=0; i<iterations; i++) {
19-
result = String(result).replace(/^[^\:]+\:\/\//, '');
20-
}
21-
return String(result);
22-
},
15+
removeURIScheme: (uri: string, iterations: number): string => {
16+
iterations = iterations || 1;
17+
let result = uri;
18+
for (let i = 0; i < iterations; i++) {
19+
result = String(result).replace(/^[^\:]+\:\/\//, '');
20+
}
21+
return String(result);
22+
},
23+
24+
unwrapFileURI: (uri: string): string => {
25+
return String(uri).replace(/^ReactNativeBlobUtil-file\:\/\//, '');
26+
},
2327

24-
unwrapFileURI : (uri:string):string => {
25-
return String(uri).replace(/^ReactNativeBlobUtil-file\:\/\//, '');
26-
}
28+
wrap: (path: string): string => {
29+
const prefix = path.startsWith('content://') ? 'ReactNativeBlobUtil-content://' : 'ReactNativeBlobUtil-file://';
30+
return prefix + path;
31+
}
2732

2833
};

0 commit comments

Comments
 (0)