Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Adapt HTTP client tests for React Native (iOS and Android) #3517

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions packages/ipfs-http-client/.aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ const server = createServer({
ipfsBin: require('go-ipfs').path()
})

module.exports = {
bundlesize: { maxSize: '83kB' },
hooks: {
browser: {
pre: async () => {
await server.start()
},
post: async () => {
await server.stop()
}
}
}
}
server.start()

// module.exports = {
// bundlesize: { maxSize: '83kB' },
// hooks: {
// browser: {
// pre: async () => {
// await server.start()
// },
// post: async () => {
// await server.stop()
// }
// }
// }
// }
9 changes: 8 additions & 1 deletion packages/ipfs-http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"test:electron-renderer": "aegir test -t electron-renderer",
"test:chrome": "aegir test -t browser -t webworker -- --browsers ChromeHeadless",
"test:firefox": "aegir test -t browser -t webworker -- --browsers FirefoxHeadless",
"test:react-native:android": "aegir test -t react-native-android",
"test:react-native:ios": "aegir test -t react-native-ios",
"lint": "aegir lint",
"prepare": "aegir build --no-bundle",
"coverage": "npx nyc -r html npm run test:node -- --bail",
Expand Down Expand Up @@ -76,6 +78,7 @@
"nanoid": "^3.1.12",
"native-abort-controller": "^1.0.3",
"parse-duration": "^0.4.4",
"react-native-fetch-api": "^1.0.2",
"stream-to-it": "^0.2.2",
"uint8arrays": "^2.0.5"
},
Expand All @@ -89,7 +92,11 @@
"it-concat": "^1.0.1",
"it-first": "^1.0.4",
"nock": "^13.0.2",
"rimraf": "^3.0.2"
"react-native-polyfill-globals": "^3.0.0",
"react-native-test-runner": "^3.0.2",
"rimraf": "^3.0.2",
"text-encoding": "^0.7.0",
"typescript": "4.0.x"
},
"engines": {
"node": ">=10.3.0",
Expand Down
16 changes: 16 additions & 0 deletions packages/ipfs-http-client/rn-test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const path = require('path')

module.exports = {
require: path.join(__dirname, 'rn-test.require.js'),
runner: 'mocha',
modules: [
'react-native-get-random-values',
'react-native-url-polyfill',
'web-streams-polyfill'
],
patches: [{
path: require.resolve('react-native-polyfill-globals/patches/react-native+0.63.3.patch')
}]
}
10 changes: 10 additions & 0 deletions packages/ipfs-http-client/rn-test.require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

require('react-native-get-random-values')
const { polyfill: polyfillReadableStream } = require('react-native-polyfill-globals/src/readable-stream')
const { polyfill: polyfillEncoding } = require('react-native-polyfill-globals/src/encoding')
const { polyfill: polyfillURL } = require('react-native-polyfill-globals/src/url')

polyfillReadableStream()
polyfillEncoding()
polyfillURL()
6 changes: 5 additions & 1 deletion packages/ipfs-http-client/src/log/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ module.exports = configure(api => {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams(options),
headers: options.headers
headers: options.headers,
// Enables text streaming support for React Native when using https://github.com/react-native-community/fetch
reactNative: {
textStreaming: true
}
})

yield * res.ndjson()
Expand Down
6 changes: 5 additions & 1 deletion packages/ipfs-http-client/src/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ module.exports = configure((api, options) => {
arg: topic,
...options
}),
headers: options.headers
headers: options.headers,
// Enables text streaming support for React Native when using https://github.com/react-native-community/fetch
reactNative: {
textStreaming: true
}
})
.catch((err) => {
// Initial subscribe fail, ensure we clean up
Expand Down
4 changes: 3 additions & 1 deletion packages/ipfs-http-client/test/constructor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const f = require('./utils/factory')()
const ipfsClient = require('../src/index.js')
const { isBrowser } = require('ipfs-utils/src/env')

const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'

describe('ipfs-http-client constructor tests', () => {
describe('parameter permuations', () => {
it('none', () => {
const ipfs = ipfsClient()
if (typeof self !== 'undefined') {
if (typeof self !== 'undefined' && !isReactNative) {
const { hostname, port } = self.location
expectConfig(ipfs, { host: hostname, port })
} else {
Expand Down
29 changes: 24 additions & 5 deletions packages/ipfs-http-client/test/dag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const CID = require('cids')
const f = require('./utils/factory')()
const ipfsHttpClient = require('../src')

const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'

let ipfs

describe('.dag', function () {
Expand All @@ -21,7 +23,12 @@ describe('.dag', function () {

after(() => f.clean())

it('should be able to put and get a DAG node with format dag-pb', async () => {
it('should be able to put and get a DAG node with format dag-pb', async function () {
if (isReactNative) {
// React Native does not support constructing Blobs out of arrays
return this.skip()
}

const data = uint8ArrayFromString('some data')
const node = new DAGNode(data)

Expand All @@ -36,7 +43,12 @@ describe('.dag', function () {
expect(result.value.Data).to.deep.equal(data)
})

it('should be able to put and get a DAG node with format dag-cbor', async () => {
it('should be able to put and get a DAG node with format dag-cbor', async function () {
if (isReactNative) {
// React Native does not support constructing Blobs out of arrays
return this.skip()
}

const cbor = { foo: 'dag-cbor-bar' }
let cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })

Expand All @@ -50,7 +62,9 @@ describe('.dag', function () {
})

it('should be able to put and get a DAG node with format raw', async () => {
const node = uint8ArrayFromString('some data')
const textData = 'some data'
const rawData = uint8ArrayFromString(textData)
const node = isReactNative ? textData : rawData
let cid = await ipfs.dag.put(node, { format: 'raw', hashAlg: 'sha2-256' })

expect(cid.codec).to.equal('raw')
Expand All @@ -59,7 +73,7 @@ describe('.dag', function () {

const result = await ipfs.dag.get(cid)

expect(result.value).to.deep.equal(node)
expect(result.value).to.deep.equal(rawData)
})

it('should error when missing DAG resolver for multicodec from requested CID', async () => {
Expand Down Expand Up @@ -100,7 +114,12 @@ describe('.dag', function () {
expect(askedToLoadFormat).to.be.true()
})

it('should allow formats to be specified without overwriting others', async () => {
it('should allow formats to be specified without overwriting others', async function () {
if (isReactNative) {
// React Native does not support constructing Blobs out of arrays
return this.skip()
}

const ipfs2 = ipfsHttpClient({
url: `http://${ipfs.apiHost}:${ipfs.apiPort}`,
ipld: {
Expand Down
5 changes: 4 additions & 1 deletion packages/ipfs-http-client/test/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
const { expect } = require('aegir/utils/chai')
const f = require('./utils/factory')()

const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'

describe('.add', function () {
this.timeout(20 * 1000)

Expand All @@ -18,7 +20,8 @@ describe('.add', function () {
after(() => f.clean())

it('should ignore metadata until https://github.com/ipfs/go-ipfs/issues/6920 is implemented', async () => {
const data = uint8ArrayFromString('some data')
const textData = 'some data'
const data = isReactNative ? textData : uint8ArrayFromString(textData)
const result = await ipfs.add(data, {
mode: 0o600,
mtime: {
Expand Down
5 changes: 4 additions & 1 deletion packages/ipfs-http-client/test/log.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
const f = require('./utils/factory')()
const first = require('it-first')

const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'

describe('.log', function () {
this.timeout(100 * 1000)

Expand All @@ -21,7 +23,8 @@ describe('.log', function () {
it('.log.tail', async () => {
const i = setInterval(async () => {
try {
await ipfs.add(uint8ArrayFromString('just adding some data to generate logs'))
const textData = 'just adding some data to generate logs'
await ipfs.add(isReactNative ? textData : uint8ArrayFromString(textData))
} catch (_) {
// this can error if the test has finished and we're shutting down the node
}
Expand Down