Skip to content

Commit

Permalink
some more fixes for another import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Radtke committed Aug 29, 2021
1 parent 9fa85bf commit 602d8eb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {ReactNativeBlobUtilResponseInfo, ReactNativeBlobUtilStream} from "../types";
import fs from "../fs";
import polyfill from "../polyfill";
import Blob from "../polyfill/Blob";
import ReactNativeBlobUtilSession from "./ReactNativeBlobUtilSession";
import URIUtil from "../utils/uri";

/**
* ReactNativeBlobUtil response object class.
Expand Down Expand Up @@ -55,18 +57,17 @@ export class FetchBlobResponse {
* @return {Promise<Blob>} Return a promise resolves Blob object.
*/
this.blob = (): Promise<Blob> => {
let Blob = polyfill.Blob;
let cType = info.headers['Content-Type'] || info.headers['content-type'];
return new Promise((resolve, reject) => {
switch (this.type) {
case 'base64':
Blob.build(this.data, {type: cType + ';BASE64'}).then(resolve);
break;
case 'path':
polyfill.Blob.build(wrap(this.data), {type: cType}).then(resolve);
Blob.build(URIUtil.wrap(this.data), {type: cType}).then(resolve);
break;
default:
polyfill.Blob.build(this.data, {type: 'text/plain'}).then(resolve);
Blob.build(this.data, {type: 'text/plain'}).then(resolve);
break;
}
});
Expand All @@ -76,7 +77,6 @@ export class FetchBlobResponse {
* @return {string} Decoded base64 string.
*/
this.text = (): string | Promise<any> => {
let res = this.data;
switch (this.type) {
case 'base64':
return base64.decode(this.data);
Expand Down Expand Up @@ -123,7 +123,7 @@ export class FetchBlobResponse {
let path = this.path();
if (!path || this.type !== 'path')
return;
return unlink(path);
return fs.unlink(path);
};
/**
* get path of response temp file
Expand All @@ -137,7 +137,7 @@ export class FetchBlobResponse {

this.session = (name: string): ReactNativeBlobUtilSession | null => {
if (this.type === 'path')
return session(name).add(this.data);
return fs.session(name).add(this.data);
else {
console.warn('only file paths can be add into session.');
return null;
Expand All @@ -150,7 +150,7 @@ export class FetchBlobResponse {
*/
this.readStream = (encoding: 'base64' | 'utf8' | 'ascii'): ReactNativeBlobUtilStream | null => {
if (this.type === 'path') {
return readStream(this.data, encoding);
return fs.readStream(this.data, encoding);
}
else {
console.warn('ReactNativeBlobUtil', 'this response data does not contains any available stream');
Expand All @@ -165,7 +165,7 @@ export class FetchBlobResponse {
*/
this.readFile = (encoding: 'base64' | 'utf8' | 'ascii') => {
if (this.type === 'path') {
return readFile(this.data, encoding);
return fs.readFile(this.data, encoding);
}
else {
console.warn('ReactNativeBlobUtil', 'this response does not contains a readable file');
Expand Down
7 changes: 1 addition & 6 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import URIUtil from "./utils/uri";
import fs from "./fs";
import getUUID from "./utils/uuid";
import {DeviceEventEmitter, NativeModules} from "react-native";
import {FetchBlobResponse} from "./class/ReactnativeBlobUtilBlobResponse";
import {FetchBlobResponse} from "./class/ReactNativeBlobUtilBlobResponse";

const emitter = DeviceEventEmitter;
const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil;
Expand All @@ -22,11 +22,6 @@ emitter.addListener("ReactNativeBlobUtilMessage", (e) => {
}
});

export function wrap(path: string): string {
const prefix = path.startsWith('content://') ? 'ReactNativeBlobUtil-content://' : 'ReactNativeBlobUtil-file://';
return prefix + path;
}

/**
* Calling this method will inject configurations into followed `fetch` method.
* @param {ReactNativeBlobUtilConfig} options
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import polyfill from './polyfill';
import android from './android';
import ios from './ios';
import JSONStream from './json-stream';
import {config, fetch, wrap} from './fetch';
import {config, fetch} from './fetch';
import URIUtil from "./utils/uri";

const {
ReactNativeBlobUtilSession,
Expand All @@ -32,6 +33,7 @@ const {
const Blob = polyfill.Blob;
const emitter = DeviceEventEmitter;
const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil;
const wrap = URIUtil.wrap;

// when app resumes, check if there's any expired network task and trigger
// their .expire event
Expand All @@ -51,9 +53,10 @@ if (!ReactNativeBlobUtil || !ReactNativeBlobUtil.fetchBlobForm || !ReactNativeBl
'and restart RN packager or manually compile IOS/Android project.'
);
}

export {ReactNativeBlobUtilConfig, ReactNativeBlobUtilResponseInfo, ReactNativeBlobUtilStream} from './types';
export URIUtil from './utils/uri';
export {FetchBlobResponse} from './class/ReactnativeBlobUtilBlobResponse';
export {FetchBlobResponse} from './class/ReactNativeBlobUtilBlobResponse';
export getUUID from './utils/uuid';
export default {
fetch,
Expand All @@ -65,5 +68,5 @@ export default {
fs,
wrap,
polyfill,
JSONStream
JSONStream,
};
5 changes: 2 additions & 3 deletions polyfill/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import fs from '../fs.js';
import getUUID from '../utils/uuid';
import Log from '../utils/log.js';
import URIUtil from "../utils/uri";
import EventTarget from './EventTarget';

import {wrap} from "../fetch";

const log = new Log('Blob');
const blobCacheDir = fs.dirs.DocumentDir + '/ReactNativeBlobUtil-blobs/';

Expand Down Expand Up @@ -237,7 +236,7 @@ export default class Blob extends EventTarget {
let resPath = blobCacheDir + getBlobName();
let pass = false;
log.debug('fs.slice new blob will at', resPath);
let result = new Blob(wrap(resPath), {type: contentType}, true);
let result = new Blob(URIUtil.wrap(resPath), {type: contentType}, true);
fs.exists(blobCacheDir)
.then((exist) => {
if (exist)
Expand Down
1 change: 1 addition & 0 deletions polyfill/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Log from '../utils/log.js';
import Blob from './Blob';
import {config as RNconfig, wrap} from "../fetch";
import type {ReactNativeBlobUtilConfig} from "../types";
import {FetchBlobResponse} from "../class/ReactNativeBlobUtilBlobResponse";

const log = new Log('FetchPolyfill');

Expand Down
47 changes: 26 additions & 21 deletions utils/uri.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
export default {

isFileURI : (uri:string):boolean => {
if(typeof uri !== 'string')
return false;
return /^ReactNativeBlobUtil-file\:\/\//.test(uri);
},
isFileURI: (uri: string): boolean => {
if (typeof uri !== 'string')
return false;
return /^ReactNativeBlobUtil-file\:\/\//.test(uri);
},

isJSONStreamURI : (uri:string):boolean => {
if(typeof uri !== 'string')
return false;
return /^JSONStream\:\/\//.test(uri);
},
isJSONStreamURI: (uri: string): boolean => {
if (typeof uri !== 'string')
return false;
return /^JSONStream\:\/\//.test(uri);
},

removeURIScheme : (uri:string, iterations:number):string => {
iterations = iterations || 1;
let result = uri;
for(let i=0; i<iterations; i++) {
result = String(result).replace(/^[^\:]+\:\/\//, '');
}
return String(result);
},
removeURIScheme: (uri: string, iterations: number): string => {
iterations = iterations || 1;
let result = uri;
for (let i = 0; i < iterations; i++) {
result = String(result).replace(/^[^\:]+\:\/\//, '');
}
return String(result);
},

unwrapFileURI: (uri: string): string => {
return String(uri).replace(/^ReactNativeBlobUtil-file\:\/\//, '');
},

unwrapFileURI : (uri:string):string => {
return String(uri).replace(/^ReactNativeBlobUtil-file\:\/\//, '');
}
wrap: (path: string): string => {
const prefix = path.startsWith('content://') ? 'ReactNativeBlobUtil-content://' : 'ReactNativeBlobUtil-file://';
return prefix + path;
}

};

0 comments on commit 602d8eb

Please sign in to comment.