From 4c5443a7a4353070cfc1cdc6dcb222d9e7cc4adb Mon Sep 17 00:00:00 2001 From: haad Date: Mon, 16 Jan 2017 17:28:08 +0100 Subject: [PATCH] docs(example): create an example of cat'ing files added from ipfs CLI using only webrtc-star as a transport --- .../cat-a-file/.gitignore | 2 + .../access-go-ipfs-files/cat-a-file/README.md | 90 ++++++ .../cat-a-file/package.json | 14 + .../cat-a-file/public/index.html | 281 ++++++++++++++++++ .../cat-a-file/public/start-ipfs.js | 47 +++ .../cat-a-file/public/styles.css | 72 +++++ package.json | 1 + src/core/index.js | 7 + 8 files changed, 514 insertions(+) create mode 100644 examples/access-go-ipfs-files/cat-a-file/.gitignore create mode 100644 examples/access-go-ipfs-files/cat-a-file/README.md create mode 100644 examples/access-go-ipfs-files/cat-a-file/package.json create mode 100644 examples/access-go-ipfs-files/cat-a-file/public/index.html create mode 100644 examples/access-go-ipfs-files/cat-a-file/public/start-ipfs.js create mode 100644 examples/access-go-ipfs-files/cat-a-file/public/styles.css diff --git a/examples/access-go-ipfs-files/cat-a-file/.gitignore b/examples/access-go-ipfs-files/cat-a-file/.gitignore new file mode 100644 index 0000000000..f96c726227 --- /dev/null +++ b/examples/access-go-ipfs-files/cat-a-file/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +public/ipfs.js diff --git a/examples/access-go-ipfs-files/cat-a-file/README.md b/examples/access-go-ipfs-files/cat-a-file/README.md new file mode 100644 index 0000000000..3d6b61134d --- /dev/null +++ b/examples/access-go-ipfs-files/cat-a-file/README.md @@ -0,0 +1,90 @@ +# access-go-ipfs-files - cat-a-file + +**WIP** + +## TODO + +- Add "connect to peer" input field and "connect" button under the "Peers" section in index.html +- Add `connectToPeer` function which calls `ipfs.swarm.connect(multiaddr)`. See https://github.com/ipfs/js-ipfs/blob/b0a7cd83cbf146b0f147467dedc686f94a5f751f/examples/ipfm/src/DataStore.js#L82 and https://github.com/ipfs/js-ipfs/blob/b0a7cd83cbf146b0f147467dedc686f94a5f751f/examples/ipfm/README.md#start-an-interplanetary-file-exchange-daemon. The multiaddr to connect to looks like this: `/ip4/127.0.0.1/tcp/9999/ws/ipfs/QmZGH8GeASSkSZoNLPEBu1MqtzLTERNUEwh9yTHLEF5kcW` +- Hook up "connect" button's click event to `connectToPeer` function +- Add instructions to this README on how to add a file in go-ipfs +- Add instructions to this README on how to cat it in the UI +- Make sure the "Start a go-ipfs daemon" instructions are correct +- Make sure go-ipfs daemon and the example connect to each other + +## Step-by-step Instructions + +### Start a go-ipfs daemon + +1. Install go-ipfs from master (TODO: link). + +2. Run `ipfs init` + +3. Edit your IPFS config file, located at `~/.ipfs/config` + +4. Add a Websocket listener address to `Addresses.Swarm`. It should look like this after editing: +``` +"Addresses": { + "API": "/ip4/127.0.0.1/tcp/5001", + "Gateway": "/ip4/0.0.0.0/tcp/8080", + "Swarm": [ + "/ip4/0.0.0.0/tcp/4001", + "/ip4/0.0.0.0/tcp/9999/ws" + ] +}, +``` + +5. Start the go-ipfs daemon with: +``` +ipfs daemon +``` + +6. You should see the Websocket address in the output: +``` +Initializing daemon... +Swarm listening on /ip4/127.0.0.1/tcp/4001 +Swarm listening on /ip4/127.0.0.1/tcp/9999/ws +Swarm listening on /ip4/192.168.10.38/tcp/4001 +Swarm listening on /ip4/192.168.10.38/tcp/9999/ws +API server listening on /ip4/127.0.0.1/tcp/5001 +Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8080 +Daemon is ready +``` + +If you see address like `Swarm listening on /ip4/127.0.0.1/tcp/9999/ws`, it means all good! + +## Start the example + +**NOTE!** Before running the examples, you need to build `js-ipfs`. You can do this by following the instructions in https://github.com/ipfs/js-ipfs#clone-and-install-dependnecies and then building it as per https://github.com/ipfs/js-ipfs#build-a-dist-version. + +``` +npm install +npm start +``` + +Open http://127.0.0.1:8080 in a browser. + +**TODO: add instructions how to cat a hash in the UI.** + +## Tutorial + +Steps +1. create IPFS instance + +TODO. See `./start-ipfs.js` + +3. add a file in go-ipfs +4. copy file's hash +5. ipfs.files.cat + +TODO. add ipfs.files.cat code examples from index.html + +6. output the buffer to + +``` +... +stream.on('end', () => { + const blob = new Blob(buf) + picture.src = URL.createObjectURL(blob) +}) +``` diff --git a/examples/access-go-ipfs-files/cat-a-file/package.json b/examples/access-go-ipfs-files/cat-a-file/package.json new file mode 100644 index 0000000000..8b03ca9591 --- /dev/null +++ b/examples/access-go-ipfs-files/cat-a-file/package.json @@ -0,0 +1,14 @@ +{ + "name": "cat-a-file", + "version": "1.0.0", + "description": "", + "scripts": { + "postinstall": "cp ../../../dist/index.js ./public/ipfs.js", + "start": "http-server -c-1" + }, + "author": "Haad", + "license": "MIT", + "devDependencies": { + "http-server": "^0.9.0" + } +} diff --git a/examples/access-go-ipfs-files/cat-a-file/public/index.html b/examples/access-go-ipfs-files/cat-a-file/public/index.html new file mode 100644 index 0000000000..fa3b9d2bb1 --- /dev/null +++ b/examples/access-go-ipfs-files/cat-a-file/public/index.html @@ -0,0 +1,281 @@ + + + + + + + + + + + +
+
+

+
+
+ + +
+
+

IPFS Settings

+ + +

+ + +
+
+
+
+
+

Files

+
+
+ + + + +
+

+      
+
+ + + + diff --git a/examples/access-go-ipfs-files/cat-a-file/public/start-ipfs.js b/examples/access-go-ipfs-files/cat-a-file/public/start-ipfs.js new file mode 100644 index 0000000000..284c53a208 --- /dev/null +++ b/examples/access-go-ipfs-files/cat-a-file/public/start-ipfs.js @@ -0,0 +1,47 @@ +'use strict' + +// Start an IPFS instance +window.startIpfs = (options, callback) => { + const repoPath = options.IpfsDataDir || '/tmp/ipfs' + + const node = new window.Ipfs(repoPath) + + node.init({ emptyRepo: true, bits: 2048 }, (err) => { + if (err && err.message !== 'repo already exists') { + return callback(err) + } + + node.config.get((err, config) => { + if (err) { + return callback(err) + } + + const host = options.SignalServer.split(':')[0] || '127.0.0.1' + const port = options.SignalServer.split(':')[1] || 9090 + const signalServer = `/libp2p-webrtc-star/ip4/${host}/tcp/${port}/ws/ipfs/${config.Identity.PeerID}` + + config.Addresses = { + Swarm: [ + signalServer + ], + API: '', + Gateway: '' + } + + config.Discovery.MDNS.Enabled = false + + node.config.replace(config, (err) => { + if (err) { return callback(err) } + + node.load((err) => { + if (err) { return callback(err) } + node.goOnline((err) => { + if (err) { return callback(err) } + console.log('IPFS node is ready') + callback(null, node) + }) + }) + }) + }) + }) +} diff --git a/examples/access-go-ipfs-files/cat-a-file/public/styles.css b/examples/access-go-ipfs-files/cat-a-file/public/styles.css new file mode 100644 index 0000000000..6dd63add24 --- /dev/null +++ b/examples/access-go-ipfs-files/cat-a-file/public/styles.css @@ -0,0 +1,72 @@ +@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,700'); + +body { + font-family: 'Roboto', sans-serif; + font-size: 1.0em; + font-weight: 400; +} + +h1 { + font-size: 2em; + font-weight: 300; + margin: 0em; +} + +h2 { + font-size: 1.25em; + font-weight: 700; +} + +h3 { + font-size: 1.0em; + font-weight: 700; +} + +label { + margin-right: 1em; +} + +input { + margin-right: 1em; +} + +.hidden { + display: none; +} + +.visible { + display: inherit; + font-size: 0.8em; +} + +.error { + font-style: italic; + color: red; +} + +.center { + display: flex; + justify-content: center; +} + +.ipfs { + min-width: 22em; + max-width: 44em; + flex-grow: 1; + flex-basis: 22em; + border: 1px dashed grey; + padding: 1em; + overflow: auto; +} + +.buttons { + margin-top: 1em; +} + +.picture { + margin-top: 1em; + width: 100%; + background-color: rgba(196, 196, 196, 0.1); + /*padding: 0.25em;*/ + /*font-size: 1.2em;*/ +} \ No newline at end of file diff --git a/package.json b/package.json index f64d6b18a8..31fb72cf91 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "async": "^2.1.4", "bl": "^1.2.0", "boom": "^4.2.0", + "buffer": "^5.0.2", "debug": "^2.6.0", "fs-pull-blob-store": "^0.3.0", "glob": "^7.1.1", diff --git a/src/core/index.js b/src/core/index.js index fa4c2e8ae2..6d0df427fd 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -25,6 +25,8 @@ const files = require('./components/files') const bitswap = require('./components/bitswap') const pubsub = require('./components/pubsub') +const Buffer = require('buffer/').Buffer + exports = module.exports = IPFS function IPFS (repoInstance) { @@ -70,4 +72,9 @@ function IPFS (repoInstance) { this.bitswap = bitswap(this) this.ping = ping(this) this.pubsub = pubsub(this) + + // Exposed data types + this.types = { + Buffer: Buffer + } }