Skip to content

Commit 7697c8d

Browse files
author
André Costa Lima
committed
use @react-native-community/fetch
1 parent 651d545 commit 7697c8d

24 files changed

+479
-4801
lines changed

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
module.exports = {
22
root: true,
33
extends: '@react-native-community',
4+
globals: {
5+
TextEncoder: true,
6+
TextDecoder: true,
7+
Blob: true,
8+
ReadableStream: true
9+
}
410
};

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ Another issue we uncovered is that `FormData` does not know how to handle `Blob`
8282

8383
#### FileReader
8484

85-
Judging by our experiments, response bodies in React Native appear to always be of blob type. As such, in order to `whatwg-fetch`'s `Response.arrayBuffer()` to work, we had to implement [`FileReader.readAsArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer) which React Native [does not](https://github.com/facebook/react-native/blob/0b9ea60b4fee8cacc36e7160e31b91fc114dbc0d/Libraries/Blob/FileReader.js#L84) at the moment. In order to get the raw binary data from the blob, we had to read it as a data URL and decode with `atob` which had to be polyfilled with the `base-64` package since it does not exist in the React Native environment.
86-
You can find the changes we have made in the [patch](patches/react-native+0.63.2.patch) provided.
85+
Judging by our experiments, response bodies in React Native appear to always be of blob type. As such, in order to `whatwg-fetch`'s `Response.arrayBuffer()` to work, we had to implement [`FileReader.readAsArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer) which React Native [does not](https://github.com/facebook/react-native/blob/0b9ea60b4fee8cacc36e7160e31b91fc114dbc0d/Libraries/Blob/FileReader.js#L84) at the moment. In order to get the raw binary data from the blob, we had to read it as a data URL and decode with `atob` which had to be polyfilled with the `base-64` package since it does not exist in the React Native environment.
86+
87+
~~You can find the changes we have made in the [patch](patches/react-native+0.63.2.patch) provided.~~ Now provided by `react-native-polyfill-globals`.
8788

8889
#### Node.js implementation (not being used)
8990

@@ -93,20 +94,27 @@ It's also worth noting that `nanoid` does not work out of the box in React Nativ
9394

9495
### [ipfs.get](https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#ipfsgetipfspath-options)
9596

96-
Works the the fixed made for `ipfs.add`.
97+
Works with the fixes made for `ipfs.add`.
9798

9899
### [ipfs.cat](https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#ipfscatipfspath-options)
99100

100-
Works the the fixed made for `ipfs.add`.
101+
Works with the fixes made for `ipfs.add`.
101102

102103
### [ipfs.pubsub](https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/PUBSUB.md) and [ipfs.swarm](https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/SWARM.md)
103104

104105
For pubsub, subscriptions operate on the basis of a long-running HTTP response, i.e., an endless stream. As React Native does not support returning a `ReadableStream` natively nor provide access to the underlying byte-stream (only base64 can be read through the bridge), so we have to fallback to `XMLHttpRequest`. React Native's XHR provides [progress events](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event) which buffers text allows us to concatenate a response by encoding it into its UTF-8 byte representation using the `TextEncoder` API. Although [very inefficient](https://github.com/jonnyreeves/fetch-readablestream/blob/cabccb98788a0141b001e6e775fc7fce87c62081/src/defaultTransportFactory.js#L33), it's some of sort of pseudo-streaming that works. The problem, however, is that we're reading text and not raw binary data so this may be a shortcoming for some use cases. Pubsub subcriptions are currently base64 encoded, so we should be fine for now in that regard.
105106

106-
To make pubsub subscriptions work, we have polyfilled [`ReadableStream`](https://github.com/MattiasBuelens/web-streams-polyfill) and integrated the stream's controller with XHR's progress events in React Native's [`fetch` implementation](patches/react-native+0.63.2.patch). It's important to note that progress events only work when [`XMLHttpRequest.responseType`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType) is set to `text`. If you wish to process raw binary data, either `blob` or `arraybuffer` has to be used. In this case, the response is read as a whole, when the load event is fired, and enqueued to the stream's controller as single chunk. We're currently using `Content-Type` header to selectively determine whether `text` or other response type should be used, but we probably should find another way to do this.
107+
To make pubsub subscriptions work, we have polyfilled [`ReadableStream`](https://github.com/MattiasBuelens/web-streams-polyfill) and integrated the stream's controller with XHR's progress events in React Native's [`fetch` implementation](patches/react-native+0.63.2.patch). It's important to note that progress events only work when [`XMLHttpRequest.responseType`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType) is set to `text`. If you wish to process raw binary data, either `blob` or `arraybuffer` has to be used. In this case, the response is read as a whole, when the load event is fired, and enqueued to the stream's controller as single chunk.
107108

108109
Other HTTP client methods continue to work as expected after these changes, on both iOS and Android.
109110

111+
## Fetch API for React Native (13 November 2020)
112+
113+
Instead of using `whatwg-fetch`, this repository is now using `@react-native-community/fetch` which is JavaScript implementation of the Fetch API built on top of React Native's low-level networking primitives. It does not rely on `XMLHttpRequest` but instead on `RCTNetworking`. It also implements `Response.body` and adds support for text streaming via native incremental data events.
114+
115+
**NOTE**: `@react-native-community/fetch` is not yet published and being worked on.
116+
110117
## Related
111118

112-
- [react-native-polyfill-globals](https://github.com/acostalima/react-native-polyfill-globals) - Polyfills and patches missing or partially supported web and core APIs.
119+
- [react-native-polyfill-globals](https://github.com/acostalima/react-native-polyfill-globals) - Polyfills and patches missing or partially supported web and core APIs.
120+
- [@react-native-community/fetch](https://github.com/react-native-community/fetch) - A Fetch API polyfill for React Native with text streaming support built on top of React Native's low-level Networking API.

ios/Podfile.lock

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ PODS:
236236
- React-cxxreact (= 0.63.3)
237237
- React-jsi (= 0.63.3)
238238
- React-jsinspector (0.63.3)
239-
- react-native-get-random-values (1.5.0):
240-
- React
241239
- react-native-safe-area-context (3.1.7):
242240
- React
243241
- React-RCTActionSheet (0.63.3):
@@ -353,7 +351,6 @@ DEPENDENCIES:
353351
- React-jsi (from `../node_modules/react-native/ReactCommon/jsi`)
354352
- React-jsiexecutor (from `../node_modules/react-native/ReactCommon/jsiexecutor`)
355353
- React-jsinspector (from `../node_modules/react-native/ReactCommon/jsinspector`)
356-
- react-native-get-random-values (from `../node_modules/react-native-get-random-values`)
357354
- react-native-safe-area-context (from `../node_modules/react-native-safe-area-context`)
358355
- React-RCTActionSheet (from `../node_modules/react-native/Libraries/ActionSheetIOS`)
359356
- React-RCTAnimation (from `../node_modules/react-native/Libraries/NativeAnimation`)
@@ -419,8 +416,6 @@ EXTERNAL SOURCES:
419416
:path: "../node_modules/react-native/ReactCommon/jsiexecutor"
420417
React-jsinspector:
421418
:path: "../node_modules/react-native/ReactCommon/jsinspector"
422-
react-native-get-random-values:
423-
:path: "../node_modules/react-native-get-random-values"
424419
react-native-safe-area-context:
425420
:path: "../node_modules/react-native-safe-area-context"
426421
React-RCTActionSheet:
@@ -485,7 +480,6 @@ SPEC CHECKSUMS:
485480
React-jsi: df07aa95b39c5be3e41199921509bfa929ed2b9d
486481
React-jsiexecutor: b56c03e61c0dd5f5801255f2160a815f4a53d451
487482
React-jsinspector: 8e68ffbfe23880d3ee9bafa8be2777f60b25cbe2
488-
react-native-get-random-values: 1404bd5cc0ab0e287f75ee1c489555688fc65f89
489483
react-native-safe-area-context: fcece23844742b6f7f0627c3715b806a7deff946
490484
React-RCTActionSheet: 53ea72699698b0b47a6421cb1c8b4ab215a774aa
491485
React-RCTAnimation: 1befece0b5183c22ae01b966f5583f42e69a83c2
@@ -508,4 +502,4 @@ SPEC CHECKSUMS:
508502

509503
PODFILE CHECKSUM: 6c2c943da66cfa1155477f07b74a390e65e705c2
510504

511-
COCOAPODS: 1.9.1
505+
COCOAPODS: 1.10.0

ios/ipfsDemo.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@
903903
COPY_PHASE_STRIP = NO;
904904
ENABLE_STRICT_OBJC_MSGSEND = YES;
905905
ENABLE_TESTABILITY = YES;
906+
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = arm64;
906907
GCC_C_LANGUAGE_STANDARD = gnu99;
907908
GCC_DYNAMIC_NO_PIC = NO;
908909
GCC_NO_COMMON_BLOCKS = YES;
@@ -963,6 +964,7 @@
963964
COPY_PHASE_STRIP = YES;
964965
ENABLE_NS_ASSERTIONS = NO;
965966
ENABLE_STRICT_OBJC_MSGSEND = YES;
967+
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = arm64;
966968
GCC_C_LANGUAGE_STANDARD = gnu99;
967969
GCC_NO_COMMON_BLOCKS = YES;
968970
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;

0 commit comments

Comments
 (0)