Ingen beskrivning

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. // @flow
  5. import {
  6. NativeModules,
  7. DeviceEventEmitter,
  8. NativeAppEventEmitter,
  9. Platform,
  10. AsyncStorage,
  11. } from 'react-native'
  12. import type {
  13. RNFetchBlobNative,
  14. RNFetchBlobConfig,
  15. RNFetchBlobStream,
  16. RNFetchBlobResponseInfo
  17. } from './types'
  18. import StatefulPromise from './class/StatefulPromise.js'
  19. import fs from './fs'
  20. import getUUID from './utils/uuid'
  21. import base64 from 'base-64'
  22. import polyfill from './polyfill'
  23. import android from './android'
  24. import JSONStream from './json-stream'
  25. const {
  26. RNFetchBlobSession,
  27. readStream,
  28. createFile,
  29. unlink,
  30. exists,
  31. mkdir,
  32. session,
  33. writeStream,
  34. readFile,
  35. ls,
  36. isDir,
  37. mv,
  38. cp
  39. } = fs
  40. const Blob = polyfill.Blob
  41. const emitter = DeviceEventEmitter
  42. const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
  43. // register message channel event handler.
  44. emitter.addListener("RNFetchBlobMessage", (e) => {
  45. if(e.event === 'warn') {
  46. console.warn(e.detail)
  47. }
  48. else if (e.event === 'error') {
  49. throw e.detail
  50. }
  51. else {
  52. console.log("RNFetchBlob native message", e.detail)
  53. }
  54. })
  55. // Show warning if native module not detected
  56. if(!RNFetchBlob || !RNFetchBlob.fetchBlobForm || !RNFetchBlob.fetchBlob) {
  57. console.warn(
  58. 'react-native-fetch-blob could not find valid native module.',
  59. 'please make sure you have linked native modules using `rnpm link`,',
  60. 'and restart RN packager or manually compile IOS/Android project.'
  61. )
  62. }
  63. function wrap(path:string):string {
  64. return 'RNFetchBlob-file://' + path
  65. }
  66. /**
  67. * Calling this method will inject configurations into followed `fetch` method.
  68. * @param {RNFetchBlobConfig} options
  69. * Fetch API configurations, contains the following options :
  70. * @property {boolean} fileCache
  71. * When fileCache is `true`, response data will be saved in
  72. * storage with a random generated file name, rather than
  73. * a BASE64 encoded string.
  74. * @property {string} appendExt
  75. * Set this property to change file extension of random-
  76. * generated file name.
  77. * @property {string} path
  78. * If this property has a valid string format, resonse data
  79. * will be saved to specific file path. Default string format
  80. * is : `RNFetchBlob-file://path-to-file`
  81. * @property {string} key
  82. * If this property is set, it will be converted to md5, to
  83. * check if a file with this name exists.
  84. * If it exists, the absolute path is returned (no network
  85. * activity takes place )
  86. * If it doesn't exist, the file is downloaded as usual
  87. * @property {number} timeout
  88. * Request timeout in millionseconds, by default it's 30000ms.
  89. *
  90. * @return {function} This method returns a `fetch` method instance.
  91. */
  92. function config (options:RNFetchBlobConfig) {
  93. return { fetch : fetch.bind(options) }
  94. }
  95. /**
  96. * Create a HTTP request by settings, the `this` context is a `RNFetchBlobConfig` object.
  97. * @param {string} method HTTP method, should be `GET`, `POST`, `PUT`, `DELETE`
  98. * @param {string} url Request target url string.
  99. * @param {object} headers HTTP request headers.
  100. * @param {string} body
  101. * Request body, can be either a BASE64 encoded data string,
  102. * or a file path with prefix `RNFetchBlob-file://` (can be changed)
  103. * @return {Promise}
  104. * This promise instance also contains a Customized method `progress`for
  105. * register progress event handler.
  106. */
  107. function fetch(...args:any):Promise {
  108. // create task ID for receiving progress event
  109. let taskId = getUUID()
  110. let options = this || {}
  111. let subscription, subscriptionUpload, stateEvent
  112. let respInfo = {}
  113. let promise = new Promise((resolve, reject) => {
  114. let [method, url, headers, body] = [...args]
  115. let nativeMethodName = Array.isArray(body) ? 'fetchBlobForm' : 'fetchBlob'
  116. // on progress event listener
  117. subscription = emitter.addListener('RNFetchBlobProgress', (e) => {
  118. if(e.taskId === taskId && promise.onProgress) {
  119. promise.onProgress(e.written, e.total, e.chunk)
  120. }
  121. })
  122. subscriptionUpload = emitter.addListener('RNFetchBlobProgress-upload', (e) => {
  123. if(e.taskId === taskId && promise.onUploadProgress) {
  124. promise.onUploadProgress(e.written, e.total)
  125. }
  126. })
  127. stateEvent = emitter.addListener('RNFetchBlobState', (e) => {
  128. respInfo = e
  129. if(e.taskId === taskId && promise.onStateChange) {
  130. promise.onStateChange(e)
  131. }
  132. })
  133. // When the request body comes from Blob polyfill, we should use special its ref
  134. // as the request body
  135. if( body instanceof Blob && body.isRNFetchBlobPolyfill) {
  136. body = body.getRNFetchBlobRef()
  137. }
  138. let req = RNFetchBlob[nativeMethodName]
  139. /**
  140. * Send request via native module, the response callback accepts three arguments
  141. * @callback
  142. * @param err {any} Error message or object, when the request success, this
  143. * parameter should be `null`.
  144. * @param rawType { 'utf8' | 'base64' | 'path'} RNFB request will be stored
  145. * as UTF8 string, BASE64 string, or a file path reference
  146. * in JS context, and this parameter indicates which one
  147. * dose the response data presents.
  148. * @param data {string} Response data or its reference.
  149. */
  150. req(options, taskId, method, url, headers || {}, body, (err, rawType, data) => {
  151. // task done, remove event listeners
  152. subscription.remove()
  153. subscriptionUpload.remove()
  154. stateEvent.remove()
  155. delete promise['progress']
  156. delete promise['uploadProgress']
  157. delete promise['stateChange']
  158. delete promise['cancel']
  159. promise.cancel = () => {
  160. console.warn('finished request could not be canceled')
  161. }
  162. if(err)
  163. reject(new Error(err, respInfo))
  164. else {
  165. // response data is saved to storage, create a session for it
  166. if(options.path || options.fileCache || options.addAndroidDownloads
  167. || options.key || options.auto && respInfo.respType === 'blob') {
  168. if(options.session)
  169. session(options.session).add(data)
  170. }
  171. respInfo.rnfbEncode = rawType
  172. resolve(new FetchBlobResponse(taskId, respInfo, data))
  173. }
  174. })
  175. })
  176. // extend Promise object, add `progress`, `uploadProgress`, and `cancel`
  177. // method for register progress event handler and cancel request.
  178. promise.progress = (fn) => {
  179. promise.onProgress = fn
  180. RNFetchBlob.enableProgressReport(taskId)
  181. return promise
  182. }
  183. promise.uploadProgress = (fn) => {
  184. promise.onUploadProgress = fn
  185. RNFetchBlob.enableUploadProgressReport(taskId)
  186. return promise
  187. }
  188. promise.stateChange = (fn) => {
  189. promise.onStateChange = fn
  190. return promise
  191. }
  192. promise.cancel = (fn) => {
  193. fn = fn || function(){}
  194. subscription.remove()
  195. subscriptionUpload.remove()
  196. stateEvent.remove()
  197. RNFetchBlob.cancelRequest(taskId, fn)
  198. }
  199. promise.taskId = taskId
  200. return promise
  201. }
  202. /**
  203. * RNFetchBlob response object class.
  204. */
  205. class FetchBlobResponse {
  206. taskId : string;
  207. path : () => string | null;
  208. type : 'base64' | 'path' | 'utf8';
  209. data : any;
  210. blob : (contentType:string, sliceSize:number) => null;
  211. text : () => string;
  212. json : () => any;
  213. base64 : () => any;
  214. flush : () => void;
  215. respInfo : RNFetchBlobResponseInfo;
  216. session : (name:string) => RNFetchBlobSession | null;
  217. readFile : (encode: 'base64' | 'utf8' | 'ascii') => ?Promise;
  218. readStream : (
  219. encode: 'utf8' | 'ascii' | 'base64',
  220. ) => RNFetchBlobStream | null;
  221. constructor(taskId:string, info:RNFetchBlobResponseInfo, data:any) {
  222. this.data = data
  223. this.taskId = taskId
  224. this.type = info.rnfbEncode
  225. this.respInfo = info
  226. this.info = ():RNFetchBlobResponseInfo => {
  227. return this.respInfo
  228. }
  229. /**
  230. * Convert result to javascript RNFetchBlob object.
  231. * @return {Promise<Blob>} Return a promise resolves Blob object.
  232. */
  233. this.blob = ():Promise<Blob> => {
  234. let Blob = polyfill.Blob
  235. let cType = info.headers['Content-Type'] || info.headers['content-type']
  236. return new Promise((resolve, reject) => {
  237. switch(this.type) {
  238. case 'base64':
  239. Blob.build(this.data, { type : cType + ';BASE64' }).then(resolve)
  240. break
  241. case 'path':
  242. polyfill.Blob.build(wrap(this.data), { type : cType }).then(resolve)
  243. break
  244. default:
  245. polyfill.Blob.build(this.data, { type : 'text/plain' }).then(resolve)
  246. break
  247. }
  248. })
  249. }
  250. /**
  251. * Convert result to text.
  252. * @return {string} Decoded base64 string.
  253. */
  254. this.text = ():string => {
  255. let res = this.data
  256. switch(this.type) {
  257. case 'base64':
  258. return base64.decode(this.data)
  259. break
  260. case 'path':
  261. return fs.readFile(this.data, 'base64').then((b64) => Promise.resolve(base64.decode(b64)))
  262. break
  263. default:
  264. return this.data
  265. break
  266. }
  267. }
  268. /**
  269. * Convert result to JSON object.
  270. * @return {object} Parsed javascript object.
  271. */
  272. this.json = ():any => {
  273. switch(this.type) {
  274. case 'base64':
  275. return JSON.parse(base64.decode(this.data))
  276. break
  277. case 'path':
  278. return fs.readFile(this.data, 'utf8')
  279. .then((text) => Promise.resolve(JSON.parse(text)))
  280. break
  281. default:
  282. return JSON.parse(this.data)
  283. break
  284. }
  285. }
  286. /**
  287. * Return BASE64 string directly.
  288. * @return {string} BASE64 string of response body.
  289. */
  290. this.base64 = ():string => {
  291. switch(this.type) {
  292. case 'base64':
  293. return this.data
  294. break
  295. case 'path':
  296. return fs.readFile(this.data, 'base64')
  297. break
  298. default:
  299. return base64.encode(this.data)
  300. break
  301. }
  302. }
  303. /**
  304. * Remove cahced file
  305. * @return {Promise}
  306. */
  307. this.flush = () => {
  308. let path = this.path()
  309. if(!path || this.type !== 'path')
  310. return
  311. return unlink(path)
  312. }
  313. /**
  314. * get path of response temp file
  315. * @return {string} File path of temp file.
  316. */
  317. this.path = () => {
  318. if(this.type === 'path')
  319. return this.data
  320. return null
  321. }
  322. this.session = (name:string):RNFetchBlobSession | null => {
  323. if(this.type === 'path')
  324. return session(name).add(this.data)
  325. else {
  326. console.warn('only file paths can be add into session.')
  327. return null
  328. }
  329. }
  330. /**
  331. * Start read stream from cached file
  332. * @param {String} encoding Encode type, should be one of `base64`, `ascrii`, `utf8`.
  333. * @param {Function} fn On data event handler
  334. * @return {void}
  335. */
  336. this.readStream = (encode: 'base64' | 'utf8' | 'ascii'):RNFetchBlobStream | null => {
  337. if(this.type === 'path') {
  338. return readStream(this.data, encode)
  339. }
  340. else {
  341. console.warn('RNFetchblob', 'this response data does not contains any available stream')
  342. return null
  343. }
  344. }
  345. /**
  346. * Read file content with given encoding, if the response does not contains
  347. * a file path, show warning message
  348. * @param {String} encoding Encode type, should be one of `base64`, `ascrii`, `utf8`.
  349. * @return {String}
  350. */
  351. this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
  352. if(this.type === 'path') {
  353. encode = encode || 'utf8'
  354. return readFile(this.data, encode)
  355. }
  356. else {
  357. console.warn('RNFetchblob', 'this response does not contains a readable file')
  358. return null
  359. }
  360. }
  361. }
  362. }
  363. export default {
  364. fetch,
  365. base64,
  366. android,
  367. config,
  368. session,
  369. fs,
  370. wrap,
  371. polyfill,
  372. JSONStream
  373. }