Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

modularize js-ipfs-api #561

Merged
merged 7 commits into from
Jun 27, 2017
Merged

Conversation

nunofmn
Copy link
Contributor

@nunofmn nunofmn commented Jun 5, 2017

Related to #544.

The structure will be similar to the async module structure, but to do this with minimal changes to the current structure, is necessary a "module init" step where the config is passed to the request-api inner module.

The usage will be similar to the following:

const bitswap = require('ipfs-api/src/bitswap')('/ip4/127.0.0.1/tcp/5001')

bitswap.unwant(key, (err) => {
  // ...
}
  • bitswap
  • block
  • bootstrap
  • config
  • dag
  • dht
  • files
  • id
  • version
  • ping
  • log
  • key
  • name
  • object
  • pin
  • pubsub
  • refs
  • repo
  • swarm
  • diag
  • commands
  • ls
  • mount
  • update
  • util
  • add
  • get
  • cat

const promisify = require('promisify-es6')

module.exports = (send) => {
module.exports = (arg) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nicer to rename to bitswap/index.js

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I had some problems when I structured it like you are saying. But I don´t remember what was the problem. I will try again 😃

send = requestAPI(arg)
} else {
throw new Error('Argument must be a send function or a config object.')
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like the above boilerplate should be extracted into its own file and be reused, rather than copied into each file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was thinking in extracting the config processing part in the IpfsAPI function, and use it in all modules.

@@ -50,13 +68,39 @@ describe('.bitswap', () => {
})
})

it('.stat (modular)', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to find a way to not duplicate the tests, but rather reuse them and just pass in the two different versions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any idea of how this could be done?

@daviddias
Copy link
Contributor

@nunofmn I like this approach, it builds nicely. Follow @dignifiedquire suggesting, making it modular should be about importing only what you need but also avoiding code duplication.

Thanks! 🌟

@daviddias
Copy link
Contributor

Hi @nunofmn, any hopes of getting this PR finished? This will make the life so much easier for everyone and I would love to get this merged before other PR's are made :)

Copy link
Contributor

@daviddias daviddias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lot's of good progress here @nunofmn !! :D I'm super excited about this PR (as you know :D)

README.md Outdated
@@ -224,7 +224,12 @@ This means:
- [`ipfs.id([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#id)
- [`ipfs.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#version)
- [`ipfs.ping()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#ping)
- [`ipfs.log()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#log)

#### [log](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove these headings.

See how it is done in line 212

README.md Outdated

- [`ipfs.log.ls([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
- [`ipfs.log.tail([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
- [`ipfs.log.level(subsystem, level, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)

#### [key](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove these headings.

See how it is done in line 212

stat: require('./stat')(arg),
unwant: require('./unwant')(arg)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be bubbled up.

src/bitswap/index.js and not src/api/bitswap/index.js

const moduleConfig = require('../../module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this step at the index.js level. Otherwise, you are creating a 'send' instance for every API call.

And also, at the index.js, you should check if the arg is already a send instance, for when the user uses ipfs-api directly and not a subapi.

Copy link
Contributor Author

@nunofmn nunofmn Jun 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main problem of doing this step at index.js level is that will lose the capability of require individual sub-modules (for example, require('ipfs-api/bitswap/unwant')(config) )

The moduleConfig already checks if the arg is a send instance, or if is necessary to init one.

}
} catch (err) {
return callback(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you refactor this into a function?

@daviddias
Copy link
Contributor

daviddias commented Jun 21, 2017

} else {
throw new Error('Argument must be a send function or a config object.')
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be correct. What if a user does const node = ipfsApi('/ip4/127.0.0.1/tcp/5001') ?

@daviddias
Copy link
Contributor

daviddias commented Jun 26, 2017

@nunofmn almost there :)

  • resolve merge conflict
  • git ignore the generated files in the example image
  • update the readme

"babel-preset-babili": "^0.1.4",
"babel-preset-env": "^1.5.2",
"babili": "^0.1.4",
"babili-webpack-plugin": "^0.1.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so much babel? Shouldn't this module require any babel at all?

@@ -5,7 +5,7 @@ const promisify = require('promisify-es6')
module.exports = (send) => {
return promisify((callback) => {
send({
path: 'commands'
path: 'bitswap/stat'
}, callback)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that git thinks this was a rename from commands.js

@daviddias
Copy link
Contributor

Values in KBs
image

Good results! Once we get ipfs/kubo#4008 it will even be better! :)

repo - 684.56 - 232.02
swarm - 1324.18 - 527.03
update - 684.45 - 231.96
version - 684.21 - 231.88
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use commas in the comma separated values files :)

Also, add the units to the top.

@@ -0,0 +1,25 @@
#!/bin/sh

Copy link
Contributor

@victorb victorb Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have at least set -e so it exits the entire script if there is a error

@daviddias daviddias changed the title [WIP] Modularize js-ipfs-api modularize js-ipfs-api Jun 27, 2017
@daviddias
Copy link
Contributor

This has been an impressive refactor! Thank you @nunofmn for pulling this off.

There is one failing test, not related with the changes itself. Needs to be investigated separately. Merging!

@daviddias daviddias merged commit 2970c9f into ipfs-inactive:master Jun 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants