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/interop-examples/index.html b/examples/access-go-ipfs-files/cat-a-file/public/index.html similarity index 86% rename from examples/interop-examples/index.html rename to examples/access-go-ipfs-files/cat-a-file/public/index.html index afb8dbe991..fa3b9d2bb1 100644 --- a/examples/interop-examples/index.html +++ b/examples/access-go-ipfs-files/cat-a-file/public/index.html @@ -3,7 +3,9 @@ - + + + @@ -56,34 +58,45 @@

Files

const multihashInput = document.getElementById("multihash") const catButton = document.getElementById("cat") - let ipfs, pollPeersTimer + let ipfs, peerInfo, pollPeersTimer + + const ipfsOptions = { + // Directory to which save IPFS data to + IpfsDataDir: dirInput.value, + // IPFS dev server: webrtc-star-signalling.cloud.ipfs.team + SignalServer: signalServerInput.value, + // Local webrtc-star server, you can get it from: + // https://github.com/libp2p/js-libp2p-webrtc-star + // SignalServer: '127.0.0.1:9090', + } // Start IPFS instance const start = () => { if (!ipfs) { - // Create an IPFS instance - ipfs = new IpfsDaemon({ - // Directory to which save IPFS data to - IpfsDataDir: dirInput.value, - // IPFS dev server: webrtc-star-signalling.cloud.ipfs.team - SignalServer: signalServerInput.value, - }) - // Update the UI with initial settings updateView('starting', ipfs) - // Wait for IPFS to start - ipfs.on('ready', () => { - // Update the UI - updateView('ready', ipfs) + // Create an IPFS instance + // window.startIpfs() is exposed in ./start-ipfs.js + window.startIpfs(ipfsOptions, (err, node) => { + if (err) { + onError(err) + return + } - // Poll for peers from IPFS and display them - pollPeersTimer = setInterval(updatePeers, 1000) - peers.innerHTML = '

Peers

Waiting for peers...' - }) + ipfs = node + + // Get our IPFS instance's info: ID and address + ipfs.id().then((id) => { + peerInfo = id + // Update the UI + updateView('ready', ipfs) - // Handle IPFS errors - ipfs.on('error', onError) + // Poll for peers from IPFS and display them + pollPeersTimer = setInterval(updatePeers, 1000) + peers.innerHTML = '

Peers

Waiting for peers...' + }) + }) } } @@ -93,7 +106,7 @@

Files

if (pollPeersTimer) clearInterval(pollPeersTimer) - ipfs.stop() + ipfs.goOffline() ipfs = null updateView('stopped', ipfs) } @@ -241,9 +254,9 @@

Files

details.innerHTML = '
' + '

IPFS Node

' + 'ID
' - + ipfs.PeerId + '

' + + peerInfo.id + '

' + 'Address
' - + ipfs.APIAddress[0] + '

' + + peerInfo.addresses[0] + '

' + 'IPFS Data Directory
' + dirInput.value // Set the file status 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/interop-examples/styles.css b/examples/access-go-ipfs-files/cat-a-file/public/styles.css similarity index 100% rename from examples/interop-examples/styles.css rename to examples/access-go-ipfs-files/cat-a-file/public/styles.css diff --git a/examples/interop-examples/README.md b/examples/interop-examples/README.md deleted file mode 100644 index fd9d9a8e63..0000000000 --- a/examples/interop-examples/README.md +++ /dev/null @@ -1,102 +0,0 @@ -# interop examples - -**WIP** - -## Run -``` -cd examples/interop-examples -npm i -npm start -``` - -Open http://127.0.0.1:8080 in your favorite browser. - -## Tutorial - -Steps -1. create IPFS instance - -First, we need an IPFS instance that we'll use throughout the tutorial. For the sake of convinience, we'll use `ipfs-daemon` module to create and IPFS instance. - -Add `ipfs-daemon` to your project: -``` -npm i ipfs-daemon --save -``` - -Add `ipfs-daemon` to your html: -```html - -``` - -Create the IPFS instance and wait for it to start: - -```javascript -const ipfs = new IpfsDaemon({ - // Directory to which save IPFS data to - IpfsDataDir: '/path/to/ipfs/data', - // IPFS dev server: webrtc-star-signalling.cloud.ipfs.team - SignalServer: '188.166.203.82:20000', -}) - -ipfs.on('error', (e) => console.error(e)) -ipfs.on('ready', () => { - // We're ready to use IPFS! -}) -``` - -2. listen for file drop - -```html -
-``` - -```javascript -const rootElement = document.getElementById("ipfs") -rootElement.addEventListener('drop', onDrop) -``` - -```javascript -const onDrop = (event) => { - event.preventDefault() - const files = event.dataTransfer.files - - // Go through each file (allows dropping multiple files) - for (let i = 0; i < files.length; i ++) { - const file = files[i] - // See details below for reading file contents - readFileContents(file) - .then((content) => { - // Add the file to IPFS - // See detailed documentation at: https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#javascript---ipfsfilesadddata-options-callback - return ipfs.files.add([{ - path: file.name, - content: new ipfs.types.Buffer(content) - }]) - }) - .then((files) => { - // Output the added files and their hashes to the HTML - rootElement.innerHTML = files - .map((e) => `Added ${e.path} as ${e.hash}`) - .join('
') - }) - .catch(onError) - } -} -``` - -The function to read file's contents: - -```javascript -const readFileContents = (file) => { - return new Promise((resolve) => { - const reader = new FileReader() - reader.onload = (event) => resolve(event.target.result) - reader.readAsArrayBuffer(file) - }) -} -``` - -3. add dropped file to IPFS -4. output file's hash -5. ipfs.files.cat -6. output to diff --git a/examples/interop-examples/package.json b/examples/interop-examples/package.json deleted file mode 100644 index 7edb7fbb1e..0000000000 --- a/examples/interop-examples/package.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name": "interop-examples", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "http-server -c-1" - }, - "author": "", - "license": "ISC", - "dependencies": { - "ipfs-daemon": "^0.3.0-beta.16" - }, - "devDependencies": { - "http-server": "^0.9.0" - } -}