In the last few decades, great strides have been made in the area of distributed computing. An interesting outcome is a prospect that modules can be shared within the community without a central registry.
Today, it's possible to publish a module to the InterPlanetary FileSystem (IPFS). IPFS is an immutable peer-to-peer filesystem composed of various innovations in cryptography and decentralized networks since the last 30 years, including technologies found in Git, BitTorrent, the Tor network, and BitCoins blockchain.
Let's publish our module to IPFS!
First, we'll need to install IPFS. Let's head to https://ipfs.io/docs/install and follow the instructions to install IPFS on our system.
Once installed, we need to initialize:
sh ipfs init
Then, start the IPFS service:
ipfs daemon
There's a handy npm module called stay-cli that makes it trivial to publish to and install modules from IPFS.
Let's open another Terminal and install stay-cli:
npm install -g stay-cli
The stay-cli will prepare our module for decentralized publishing by injecting a prepublish script into our package.json. If we followed the Prepublish section, we'll already have a prepublish script.
That being the case, we'll need to alter the scripts section in our package.json file to the following:
"scripts": {
"check": "npm ls && npm test" ,
"test": "npm run lint && tap --cov test" ,
"lint": "standard"
}
Note that we've renamed the prepublish field to check.
Next, we can run the following in our module folder:
stay init
This will place a publish-dep.sh in our module directory and alter our package.json file to look thus:
"scripts": {
"check": "npm ls && npm test",
"test": "npm run lint && tap --cov test",
"lint": "standard",
"prepublish": ". /publish-dep. sh"
},
Now, let's ensure that our prepublish checks happen by editing the prepublish field as follows:
"prepublish": "npm run check && ./publish-dep.sh"
Let's bump our module's patch version since we've made edits:
npm version patch
Now, we're ready to publish our module to the IPFS peer-to-peer network:
npm publish
This will publish it to both npmjs.org and IPFS.
In the publish output, we should see something like this:
## Publishing dependency
Published as QmPqxGscbc6Qv9zdN3meifT7TRfBJKXT4VVRrQZ4skbFZ5
We can use the supplied hash to install our hsl-to-hex module from IPFS.
Let's try that out. First, let's create a new folder called stay-play:
mkdir stay-play
Now, we'll initialize a new module:
npm init
Next, we need to add the decentralized dependency:
stay add hsl-to-hex@QmPqxGscbc6Qv9zdN3meifT7TRfBJKXT4VVRrQZ4skbFZ5
We can replace the hash (beginning Qmpqx) with the hash of our own published module when we run npm publish.
This will add an esDependencies field to our package.json file, which looks like this:
"esDependencies": {
"hsl-to-hex" : "QmPqxGscbc6Qv9zdN3meifT7TRfBJKXT4VVRrQZ4skbFZ5"
}
Finally, we can grab install distributed dependencies listed in the esDepdendencies field with this:
stay install
In all likelihood, this will install hsl-to-hex from our own system since we're the closest network node to ourselves. As an experiment, we can always try passing on the hash to a friend and see whether they can install it.