Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import RNFetchBlob from '../index.js'
  2. import Log from '../utils/log.js'
  3. import fs from '../fs'
  4. import unicode from '../utils/unicode'
  5. import Blob from './Blob'
  6. const log = new Log('FetchPolyfill')
  7. log.disable()
  8. // log.level(3)
  9. export default class Fetch {
  10. constructor(config:RNFetchBlobConfig) {
  11. Object.assign(this, new RNFetchBlobFetchPolyfill(config))
  12. }
  13. }
  14. class RNFetchBlobFetchPolyfill {
  15. constructor(config:RNFetchBlobConfig) {
  16. this.build = () => (url, options = {}) => {
  17. let body = options.body
  18. let promise = Promise.resolve()
  19. let blobCache = null
  20. options.headers = options.headers || {}
  21. let ctype = options['Content-Type'] || options['content-type']
  22. let ctypeH = options.headers['Content-Type'] || options.headers['content-type']
  23. options.headers['Content-Type'] = ctype || ctypeH
  24. options.headers['content-type'] = ctype || ctypeH
  25. options.method = options.method || 'GET'
  26. if(body) {
  27. // When the request body is an instance of FormData, create a Blob cache
  28. // to upload the body.
  29. if(body instanceof FormData) {
  30. log.verbose('convert FormData to blob body')
  31. promise = Blob.build(body).then((b) => {
  32. blobCache = b
  33. options.headers['Content-Type'] = 'multipart/form-data;boundary=' + b.multipartBoundary
  34. return Promise.resolve(RNFetchBlob.wrap(b._ref))
  35. })
  36. }
  37. // When request body is a Blob, use file URI of the Blob as request body.
  38. else if (body.isRNFetchBlobPolyfill)
  39. promise = Promise.resolve(RNFetchBlob.wrap(body.blobPath))
  40. else if (typeof body !== 'object' && options.headers['Content-Type'] !== 'application/json')
  41. promise = Promise.resolve(JSON.stringify(body))
  42. else if (typeof body !== 'string')
  43. promise = Promise.resolve(body.toString())
  44. // send it as-is, leave the native module decide how to send the body.
  45. else
  46. promise = Promise.resolve(body)
  47. }
  48. // task is a progress reportable and cancellable Promise, however,
  49. // task.then is not, so we have to extend task.then with progress and
  50. // cancel function
  51. let task = promise
  52. .then((body) => {
  53. return RNFetchBlob.config(config)
  54. .fetch(options.method, url, options.headers, body)
  55. })
  56. let statefulPromise = task.then((resp) => {
  57. log.verbose('response', resp)
  58. // release blob cache created when sending request
  59. if(blobCache !== null && blobCache instanceof Blob)
  60. blobCache.close()
  61. return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
  62. })
  63. // extend task.then progress with report and cancelling functions
  64. statefulPromise.cancel = task.cancel
  65. statefulPromise.progress = task.progress
  66. statefulPromise.uploadProgress = task.uploadProgress
  67. return statefulPromise
  68. }
  69. }
  70. }
  71. class RNFetchBlobFetchRepsonse {
  72. constructor(resp:FetchBlobResponse) {
  73. let info = resp.info()
  74. this.headers = info.headers
  75. this.ok = info.status >= 200 && info.status <= 299,
  76. this.status = info.status
  77. this.type = 'basic'
  78. this.bodyUsed = false
  79. this.resp = resp
  80. this.rnfbRespInfo = info
  81. this.rnfbResp = resp
  82. }
  83. rawResp() {
  84. return Promise.resolve(this.rnfbResp)
  85. }
  86. arrayBuffer(){
  87. log.verbose('to arrayBuffer', this.rnfbRespInfo)
  88. this.bodyUsed = true
  89. return readArrayBuffer(this.rnfbResp, this.rnfbRespInfo)
  90. }
  91. text() {
  92. log.verbose('to text', this.rnfbResp, this.rnfbRespInfo)
  93. this.bodyUsed = true
  94. return readText(this.rnfbResp, this.rnfbRespInfo)
  95. }
  96. json() {
  97. log.verbose('to json', this.rnfbResp, this.rnfbRespInfo)
  98. this.bodyUsed = true
  99. return readJSON(this.rnfbResp, this.rnfbRespInfo)
  100. }
  101. blob() {
  102. log.verbose('to blob', this.rnfbResp, this.rnfbRespInfo)
  103. this.bodyUsed = true
  104. return readBlob(this.rnfbResp, this.rnfbRespInfo)
  105. }
  106. }
  107. /**
  108. * Get response data as array.
  109. * @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
  110. * @param {RNFetchBlobResponseInfo} info Response informations.
  111. * @return {Promise<Array>}
  112. */
  113. function readArrayBuffer(resp, info):Promise<Array> {
  114. switch (info.rnfbEncode) {
  115. case 'path':
  116. return resp.readFile('ascii')
  117. break
  118. default:
  119. let buffer = []
  120. let str = resp.text()
  121. for (let i in str) {
  122. buffer[i] = str.charCodeAt(i);
  123. }
  124. return Promise.resolve(buffer)
  125. break
  126. }
  127. }
  128. /**
  129. * Get response data as string.
  130. * @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
  131. * @param {RNFetchBlobResponseInfo} info Response informations.
  132. * @return {Promise<string>}
  133. */
  134. function readText(resp, info):Promise<string> {
  135. switch (info.rnfbEncode) {
  136. case 'base64':
  137. return Promise.resolve(resp.text())
  138. break
  139. case 'path':
  140. return resp.text()
  141. break
  142. default:
  143. return Promise.resolve(resp.text())
  144. break
  145. }
  146. }
  147. /**
  148. * Get response data as RNFetchBlob Blob polyfill object.
  149. * @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
  150. * @param {RNFetchBlobResponseInfo} info Response informations.
  151. * @return {Promise<Blob>}
  152. */
  153. function readBlob(resp, info):Promise<Blob> {
  154. log.verbose('readBlob', resp, info)
  155. return resp.blob()
  156. }
  157. /**
  158. * Get response data as JSON object.
  159. * @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
  160. * @param {RNFetchBlobResponseInfo} info Response informations.
  161. * @return {Promise<object>}
  162. */
  163. function readJSON(resp, info):Promise<object> {
  164. log.verbose('readJSON', resp, info)
  165. switch (info.rnfbEncode) {
  166. case 'base64':
  167. return Promise.resolve(resp.json())
  168. case 'path':
  169. return resp.json()
  170. default:
  171. return Promise.resolve(resp.json())
  172. }
  173. }