From 75f140aa9bb053fb9a51479b970de7b11c75b176 Mon Sep 17 00:00:00 2001 From: interfect Date: Mon, 17 Oct 2016 19:14:11 -0700 Subject: [PATCH 01/10] Expose Buffer on ipfs objects, add examples This lets you construct a Buffer even in browser code that doesn't have access to the Node Buffer global, so that you can pass it back to IPFS API calls that want a Buffer. Add some usage documentation Mentions the `ipfs.Buffer` field you can use to stamp out `Buffer`s so you can actually insert stuff given just an `IPFS` instance and no Node.js APIs in scope. Also add examples for using IPFS in browser with a script tag, and for using libp2p-webrtc-star. --- README.md | 52 +++++++++++++- examples/browser-script/README.md | 13 ++++ examples/browser-script/index.html | 85 ++++++++++++++++++++++ examples/libp2p-webrtc-star/README.md | 14 ++++ examples/libp2p-webrtc-star/index.html | 99 ++++++++++++++++++++++++++ src/core/index.js | 20 ++++-- 6 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 examples/browser-script/README.md create mode 100644 examples/browser-script/index.html create mode 100644 examples/libp2p-webrtc-star/README.md create mode 100644 examples/libp2p-webrtc-star/index.html diff --git a/README.md b/README.md index fa3e2a2760..d8f0dd955e 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,9 @@ You can check the development status at: - [HTTP-API](#http-api) - [IPFS Core (use IPFS as a module in Node.js or in the Browser)](#ipfs-core-examples-use-ipfs-as-a-module) - [Create a IPFS node instance](#create-a-ipfs-node-instance) + - [Add a file](#add-a-file) + - [Retrieve a file](#retrieve-a-file) + - [More to come](#more-to-come) - [Tutorials and Examples](#tutorials-and-examples) - [API](#api) - [Generic API](#generic-api) @@ -94,6 +97,7 @@ You can check the development status at: - [Files API](#files-api) - [Swarm API](#swarm-api) - [libp2p API](#libp2p-api) + - [Domain data types](#domain-data-types) - [Packages](#packages) - [Development](#development) - [Clone](#clone) @@ -193,6 +197,8 @@ The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https:/ #### Create a IPFS node instance +The basic startup flow involves (optionally) creating a Repo, creating an IPFS node, `init`-ing it so it can generate its keys, `load`-ing its configuration, and putting it online with `goOnline`. Here is a structural example: + ```JavaScript // IPFS will need a repo, it can create one for you or you can pass // it a repo instance of the type IPFS Repo @@ -229,6 +235,42 @@ node.init({ emptyRepo: true, bits: 2048 }, (err) => { > We are working on making this init process better, see https://github.com/ipfs/js-ipfs/issues/556 for the discussion. +Below are some more examples of JavaScript IPFS in action. + +#### Add a file + +Once you have an IPFS node up and running, you can add files to it from `Buffer`s, `Readable` streams, or [arrays of objects of a certain form](https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#add). If you don't have `Buffer` conveniently available (say, because you're in a browser without the Node API handy), it's available as a property of the IPFS node. + +```javascript +// Add a single file +node.files.add(node.Buffer.from('Hello world'), (err, returned) => { + if (err) { + throw err + } + console.log('IPFS hash: ', returned[0].hash) +}) +``` + +#### Retrieve a file + +To retrieve the contents of a file, you can use the [cat method](https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#cat), which will call your callback with a Node.js-style `Readable` stream. + +```javascript +node.files.cat('QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve', + (err, content_stream) => { + if (err) { + throw err + } + content_stream.on('data', (buffer) => { + console.log('File contents:', buffer.toString('ascii')) + }) +}) +``` + +#### More to come + +> If you have built an example, please share it with the community by submitting a Pull Request to this repo!. + ### [Tutorials and Examples](/examples) You can find some examples and tutorials in the [examples](/examples) folder, these exist to help you get started using `js-ipfs`. @@ -252,6 +294,12 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int - [libp2p-ipfs-nodejs](https://github.com/ipfs/js-libp2p-ipfs-nodejs) - [libp2p-ipfs-browser](https://github.com/ipfs/js-libp2p-ipfs-browser) +#### Domain data types + +IPFS exposes the Buffer class in every ipfs instance, so that you can create buffers and add them to IPFS just like if you were using it in Node.js. + +You can get it at `ipfs.Buffer` + ## Packages | Package | Version | Deps | DevDeps | @@ -287,8 +335,6 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int | [`multihashing`](//github.com/multiformats/js-multihashing) | [![npm](https://img.shields.io/npm/v/multihashing.svg?maxAge=86400&style=flat-square)](//github.com/multiformats/js-multihashing/releases) | [![Dep Status](https://david-dm.org/multiformats/js-multihashing.svg?style=flat-square)](https://david-dm.org/multiformats/js-multihashing) | [![devDep Status](https://david-dm.org/multiformats/js-multihashing/dev-status.svg?style=flat-square)](https://david-dm.org/multiformats/js-multihashing?type=dev) | | [`mafmt`](//github.com/whyrusleeping/js-mafmt) | [![npm](https://img.shields.io/npm/v/mafmt.svg?maxAge=86400&style=flat-square)](//github.com/whyrusleeping/js-mafmt/releases) | [![Dep Status](https://david-dm.org/whyrusleeping/js-mafmt.svg?style=flat-square)](https://david-dm.org/whyrusleeping/js-mafmt) | [![devDep Status](https://david-dm.org/whyrusleeping/js-mafmt/dev-status.svg?style=flat-square)](https://david-dm.org/whyrusleeping/js-mafmt?type=dev) | - - ## Development ### Clone and install dependencies @@ -301,6 +347,8 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int ### Run unit tests +#### Block Service + ```sh # run all the unit tsts > npm test diff --git a/examples/browser-script/README.md b/examples/browser-script/README.md new file mode 100644 index 0000000000..41f006600d --- /dev/null +++ b/examples/browser-script/README.md @@ -0,0 +1,13 @@ +# Use IPFS in the browser with ` +``` + +This exposes a global `Ipfs`; you can get a node by making a `new Ipfs()`. + +See `index.html` for a working example. + + diff --git a/examples/browser-script/index.html b/examples/browser-script/index.html new file mode 100644 index 0000000000..f8ef09adb1 --- /dev/null +++ b/examples/browser-script/index.html @@ -0,0 +1,85 @@ + + + + IPFS in the Browser + + + + +

IPFS in the Browser

+

This page creates an IPFS node in your browser and drops it into the global Javascript namespace as ipfs. Open the console to play around with it.

+

Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.

+
Node status: offline
+ + diff --git a/examples/libp2p-webrtc-star/README.md b/examples/libp2p-webrtc-star/README.md new file mode 100644 index 0000000000..5debdc60fc --- /dev/null +++ b/examples/libp2p-webrtc-star/README.md @@ -0,0 +1,14 @@ +# Robust Initialization and libp2p-webrtc-star Signaling + +There's still a bit of work required to start up an in-browser node in a robust way, so that it will work whether or not there is an existing initialized IPFS repo in the user's browser. If there isn't one, you need to call `init` as above, but if there is one, calling `init` will fail. Moreover, there's currently no good way to check if you need to call `init` or not. + +Also, an in-browser node isn't able to call up normal IPFS nodes over raw TCP; it can only communicate over Websockets and WebRTC. Currently, there are no Websockets or WebRTC bootstrap nodes run by the IPFS maintainers. You will probably want to set up a [libp2p-webrtc-star signaling server](https://github.com/libp2p/js-libp2p-webrtc-star) so nodes used in your application can find each other: + +```bash +npm i libp2p-webrtc-star -g +star-sig +``` + +You will then want to point IPFS nodes used in your application at your signaling server, so they can connect to each other. This is accomplished by adding an address to the node's configuration referencing the signaling server, of the form `/libp2p-webrtc-star/ip4//tcp//ws/ipfs/`, where `` is the peer ID of the node that the address is being added to. This causes the node to think of itself as being contactable through the signaling server. It will then initializes its libp2p-webrtc-star implementation and automatically peer with other nodes using the same server. + +The `index.html` page in this directory is an example which initializes an IPFS node in a browser safely, whether a node has already been initialized by the current domain or not. It also configures `libp2p-webrtc-star` communication, using a signaling server running on the local host. (Note that since IPFS node configuration information is stored in IndexedDB in browsers, opening two tabs of this code from a local file in the same browser won't work, because they'll share the same node keys and identity. Either run the code from multiple domains, or run it in two different browsers, like Chrome and Firefox.) diff --git a/examples/libp2p-webrtc-star/index.html b/examples/libp2p-webrtc-star/index.html new file mode 100644 index 0000000000..f614a806aa --- /dev/null +++ b/examples/libp2p-webrtc-star/index.html @@ -0,0 +1,99 @@ + + + + IPFS in the Browser with WebRTC + + + + +

IPFS in the Browser with WebRTC

+

This page creates an IPFS node in your browser and drops it into the global Javascript namespace as ipfs. Open the console to play around with it.

+

Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.

+
Node status: offline
+

Run a libp2p-webrtc-star signaling server! Peers detected through the server will be displayed below:

+

Peers:

+
+ + diff --git a/src/core/index.js b/src/core/index.js index fc4c54d6e5..e27dcd4589 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -63,10 +63,22 @@ class IPFS { this.ping = components.ping(this) this.pubsub = components.pubsub(this) - if (configOpts.EXPERIMENTAL.pubsub) { - this.log('EXPERIMENTAL pubsub is enabled') - } - } + // interface-ipfs-core defined API + this.version = version(this) + this.id = id(this) + this.repo = repo(this) + this.bootstrap = bootstrap(this) + this.config = config(this) + this.block = block(this) + this.object = object(this) + this.libp2p = libp2p(this) + this.swarm = swarm(this) + this.files = files(this) + this.bitswap = bitswap(this) + this.ping = ping(this) + + // expose Buffer for browser applications + this.Buffer = Buffer } module.exports = IPFS From 144f59bc49f91f4a37c462210c8a5943a74a75c9 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 11 Mar 2017 17:16:58 +0000 Subject: [PATCH 02/10] docs: Update README with info from domain data types --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d8f0dd955e..aced7d67b5 100644 --- a/README.md +++ b/README.md @@ -296,9 +296,9 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int #### Domain data types -IPFS exposes the Buffer class in every ipfs instance, so that you can create buffers and add them to IPFS just like if you were using it in Node.js. +A set of data types are exposed directly from the IPFS instance under `ipfs.types`. That way you're not required to import/require the following. -You can get it at `ipfs.Buffer` +* `ipfs.types.Buffer` ## Packages @@ -347,8 +347,6 @@ You can get it at `ipfs.Buffer` ### Run unit tests -#### Block Service - ```sh # run all the unit tsts > npm test From 489db45d86d89576488e44687d7885e2c1cd316a Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 11 Mar 2017 20:37:08 +0000 Subject: [PATCH 03/10] feat: expose peer-id, peer-info, multiaddr and multihash types --- README.md | 4 ++++ src/core/index.js | 31 +++++++++++++++---------------- test/core/init.spec.js | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index aced7d67b5..fc01dade6f 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,10 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int A set of data types are exposed directly from the IPFS instance under `ipfs.types`. That way you're not required to import/require the following. * `ipfs.types.Buffer` +* [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id) +* [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info) +* [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr) +* [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash) ## Packages diff --git a/src/core/index.js b/src/core/index.js index e27dcd4589..642b7e9ff0 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -2,6 +2,10 @@ const BlockService = require('ipfs-block-service') const IPLDResolver = require('ipld-resolver') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') +const multiaddr = require('multiaddr') +const multihash = require('multihashes') const PeerBook = require('peer-book') const debug = require('debug') @@ -24,7 +28,11 @@ class IPFS { // IPFS utils this.types = { - Buffer: Buffer + Buffer, + PeerId, + PeerInfo, + multiaddr, + multihash } this.log = debug('jsipfs') this.log.err = debug('jsipfs:err') @@ -63,22 +71,13 @@ class IPFS { this.ping = components.ping(this) this.pubsub = components.pubsub(this) - // interface-ipfs-core defined API - this.version = version(this) - this.id = id(this) - this.repo = repo(this) - this.bootstrap = bootstrap(this) - this.config = config(this) - this.block = block(this) - this.object = object(this) - this.libp2p = libp2p(this) - this.swarm = swarm(this) - this.files = files(this) - this.bitswap = bitswap(this) - this.ping = ping(this) + // expose Buffer for browser applications + this.Buffer = Buffer - // expose Buffer for browser applications - this.Buffer = Buffer + if (configOpts.EXPERIMENTAL.pubsub) { + this.log('EXPERIMENTAL pubsub is enabled') + } + } } module.exports = IPFS diff --git a/test/core/init.spec.js b/test/core/init.spec.js index 16b382eba9..afab808618 100644 --- a/test/core/init.spec.js +++ b/test/core/init.spec.js @@ -4,6 +4,10 @@ const expect = require('chai').expect const isNode = require('detect-node') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') +const multiaddr = require('multiaddr') +const multihash = require('multihashes') const IPFS = require('../../src/core') // This gets replaced by require('../utils/create-repo-browser.js') @@ -83,4 +87,14 @@ describe('init', () => { }) }) }) + + it('data types', () => { + expect(ipfs.types).to.be.deep.equal({ + Buffer, + PeerId, + PeerInfo, + multiaddr, + multihash + }) + }) }) From 788ed8116ec5e4375f3b01db9858bc35c30300a5 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 11 Mar 2017 21:10:58 +0000 Subject: [PATCH 04/10] docs(examples): rename browser script tag and webrtc star examples --- examples/README.md | 2 ++ examples/{browser-script => browser-script-tag}/README.md | 0 examples/{browser-script => browser-script-tag}/index.html | 0 .../{libp2p-webrtc-star => how-to-use-webrtc-star}/README.md | 2 +- .../{libp2p-webrtc-star => how-to-use-webrtc-star}/index.html | 0 5 files changed, 3 insertions(+), 1 deletion(-) rename examples/{browser-script => browser-script-tag}/README.md (100%) rename examples/{browser-script => browser-script-tag}/index.html (100%) rename examples/{libp2p-webrtc-star => how-to-use-webrtc-star}/README.md (97%) rename examples/{libp2p-webrtc-star => how-to-use-webrtc-star}/index.html (100%) diff --git a/examples/README.md b/examples/README.md index 98fda715cc..bb08209f62 100644 --- a/examples/README.md +++ b/examples/README.md @@ -9,6 +9,8 @@ Let us know if you find any issue or if you want to contribute and add a new tut - [js-ipfs basic, how to spawn a node and add a file to IPFS](./basics) - [How to bundle js-ipfs with Browserify](./bundle-browserify) - [How to bundle js-ipfs with WebPack](./bundle-webpack) +- [How to use js-ipfs with a script tag](./browser-script-tag) - [Use IPFS to explore the Ethereum BlockChain](./explore-ethereum) +- [How to use WebRTC star](./how-to-use-webrtc-star) ## Tutorials diff --git a/examples/browser-script/README.md b/examples/browser-script-tag/README.md similarity index 100% rename from examples/browser-script/README.md rename to examples/browser-script-tag/README.md diff --git a/examples/browser-script/index.html b/examples/browser-script-tag/index.html similarity index 100% rename from examples/browser-script/index.html rename to examples/browser-script-tag/index.html diff --git a/examples/libp2p-webrtc-star/README.md b/examples/how-to-use-webrtc-star/README.md similarity index 97% rename from examples/libp2p-webrtc-star/README.md rename to examples/how-to-use-webrtc-star/README.md index 5debdc60fc..49bdc88d0f 100644 --- a/examples/libp2p-webrtc-star/README.md +++ b/examples/how-to-use-webrtc-star/README.md @@ -1,4 +1,4 @@ -# Robust Initialization and libp2p-webrtc-star Signaling +# How to use WebRTC star There's still a bit of work required to start up an in-browser node in a robust way, so that it will work whether or not there is an existing initialized IPFS repo in the user's browser. If there isn't one, you need to call `init` as above, but if there is one, calling `init` will fail. Moreover, there's currently no good way to check if you need to call `init` or not. diff --git a/examples/libp2p-webrtc-star/index.html b/examples/how-to-use-webrtc-star/index.html similarity index 100% rename from examples/libp2p-webrtc-star/index.html rename to examples/how-to-use-webrtc-star/index.html From 02dce62ed6431d1de422bc75416da41e542c0891 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 11 Mar 2017 21:25:35 +0000 Subject: [PATCH 05/10] feat: add CID to the exported data types and remove unneeded examples --- README.md | 38 ++------------------------------------ package.json | 3 ++- src/core/index.js | 4 +++- test/core/init.spec.js | 4 +++- 4 files changed, 10 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index fc01dade6f..5a2e688234 100644 --- a/README.md +++ b/README.md @@ -85,9 +85,6 @@ You can check the development status at: - [HTTP-API](#http-api) - [IPFS Core (use IPFS as a module in Node.js or in the Browser)](#ipfs-core-examples-use-ipfs-as-a-module) - [Create a IPFS node instance](#create-a-ipfs-node-instance) - - [Add a file](#add-a-file) - - [Retrieve a file](#retrieve-a-file) - - [More to come](#more-to-come) - [Tutorials and Examples](#tutorials-and-examples) - [API](#api) - [Generic API](#generic-api) @@ -235,39 +232,7 @@ node.init({ emptyRepo: true, bits: 2048 }, (err) => { > We are working on making this init process better, see https://github.com/ipfs/js-ipfs/issues/556 for the discussion. -Below are some more examples of JavaScript IPFS in action. - -#### Add a file - -Once you have an IPFS node up and running, you can add files to it from `Buffer`s, `Readable` streams, or [arrays of objects of a certain form](https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#add). If you don't have `Buffer` conveniently available (say, because you're in a browser without the Node API handy), it's available as a property of the IPFS node. - -```javascript -// Add a single file -node.files.add(node.Buffer.from('Hello world'), (err, returned) => { - if (err) { - throw err - } - console.log('IPFS hash: ', returned[0].hash) -}) -``` - -#### Retrieve a file - -To retrieve the contents of a file, you can use the [cat method](https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#cat), which will call your callback with a Node.js-style `Readable` stream. - -```javascript -node.files.cat('QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve', - (err, content_stream) => { - if (err) { - throw err - } - content_stream.on('data', (buffer) => { - console.log('File contents:', buffer.toString('ascii')) - }) -}) -``` - -#### More to come +More examples can be found in the [examples folder](./examples) > If you have built an example, please share it with the community by submitting a Pull Request to this repo!. @@ -303,6 +268,7 @@ A set of data types are exposed directly from the IPFS instance under `ipfs.type * [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info) * [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr) * [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash) +* [`ipfs.types.CID`](https://github.com/ipld/js-cid) ## Packages diff --git a/package.json b/package.json index 1a36705dbb..fedf3b2c08 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,7 @@ "async": "^2.1.4", "bl": "^1.2.0", "boom": "^4.2.0", + "cids": "^0.4.1", "debug": "^2.6.1", "fs-pull-blob-store": "~0.4.1", "glob": "^7.1.1", @@ -178,4 +179,4 @@ "npmcdn-to-unpkg-bot ", "ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ " ] -} \ No newline at end of file +} diff --git a/src/core/index.js b/src/core/index.js index 642b7e9ff0..1bffd394fa 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -7,6 +7,7 @@ const PeerInfo = require('peer-info') const multiaddr = require('multiaddr') const multihash = require('multihashes') const PeerBook = require('peer-book') +const CID = require('cids') const debug = require('debug') const defaultRepo = require('./default-repo') @@ -32,7 +33,8 @@ class IPFS { PeerId, PeerInfo, multiaddr, - multihash + multihash, + CID } this.log = debug('jsipfs') this.log.err = debug('jsipfs:err') diff --git a/test/core/init.spec.js b/test/core/init.spec.js index afab808618..f2f1cec902 100644 --- a/test/core/init.spec.js +++ b/test/core/init.spec.js @@ -8,6 +8,7 @@ const PeerId = require('peer-id') const PeerInfo = require('peer-info') const multiaddr = require('multiaddr') const multihash = require('multihashes') +const CID = require('cids') const IPFS = require('../../src/core') // This gets replaced by require('../utils/create-repo-browser.js') @@ -94,7 +95,8 @@ describe('init', () => { PeerId, PeerInfo, multiaddr, - multihash + multihash, + CID }) }) }) From 6af5061596f7de4df308d9ef1c9d64d96f51c549 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sat, 11 Mar 2017 22:35:50 +0000 Subject: [PATCH 06/10] docs: remove unneeded line about examples --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 5a2e688234..21e38e6d8b 100644 --- a/README.md +++ b/README.md @@ -234,8 +234,6 @@ node.init({ emptyRepo: true, bits: 2048 }, (err) => { More examples can be found in the [examples folder](./examples) -> If you have built an example, please share it with the community by submitting a Pull Request to this repo!. - ### [Tutorials and Examples](/examples) You can find some examples and tutorials in the [examples](/examples) folder, these exist to help you get started using `js-ipfs`. From bb5fae18df98e9ffedf607d47286294fe6f7931d Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sun, 12 Mar 2017 16:23:34 +0000 Subject: [PATCH 07/10] chore: remove unneeded Buffer --- src/core/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/core/index.js b/src/core/index.js index 1bffd394fa..f3207a383f 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -73,9 +73,6 @@ class IPFS { this.ping = components.ping(this) this.pubsub = components.pubsub(this) - // expose Buffer for browser applications - this.Buffer = Buffer - if (configOpts.EXPERIMENTAL.pubsub) { this.log('EXPERIMENTAL pubsub is enabled') } From aa21ed1c50f58e30a0af39d74737928d2bdd8bb1 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sun, 12 Mar 2017 21:53:58 +0000 Subject: [PATCH 08/10] docs(examples): refactor browser tag script example and add possible commands --- examples/browser-script-tag/index.html | 139 ++++++++++++++----------- 1 file changed, 80 insertions(+), 59 deletions(-) diff --git a/examples/browser-script-tag/index.html b/examples/browser-script-tag/index.html index f8ef09adb1..072cd5f588 100644 --- a/examples/browser-script-tag/index.html +++ b/examples/browser-script-tag/index.html @@ -6,80 +6,101 @@

IPFS in the Browser

-

This page creates an IPFS node in your browser and drops it into the global Javascript namespace as ipfs. Open the console to play around with it.

+

This page creates an IPFS node in your browser and drops it into the global Javascript namespace as node. Open the console to play around with it.

Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.

Node status: offline
+ +

Some suggestions

+ +

Try adding a new file:

+ + + node.files.add(new node.types.Buffer('Hello world!'), function (err, res) { + if (err || !res) { + return console.error('Error - ipfs files add', err, res) + } + + res.forEach(function (file) { + console.log('successfully stored', file) + }) + }) + + +

You can cat that same file. If you used the exact same string as above ('Hello world!') you should have an hash like this: 'QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY'

+ + + node.files.cat('QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY', function (err, stream) { + var res = '' + + stream.on('data', function (chunk) { + res += chunk.toString() + }) + + stream.on('error', function (err) { + console.error('Error - ipfs files cat ', err) + }) + + stream.on('end', function () { + console.log('Got:', res) + }) + }) + From fea0d521d54eb6b645eb518726fece2cd8290b10 Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Sun, 12 Mar 2017 22:19:04 +0000 Subject: [PATCH 09/10] docs(examples): remove web rtc star examples from this specific branch --- examples/how-to-use-webrtc-star/README.md | 14 --- examples/how-to-use-webrtc-star/index.html | 99 ---------------------- 2 files changed, 113 deletions(-) delete mode 100644 examples/how-to-use-webrtc-star/README.md delete mode 100644 examples/how-to-use-webrtc-star/index.html diff --git a/examples/how-to-use-webrtc-star/README.md b/examples/how-to-use-webrtc-star/README.md deleted file mode 100644 index 49bdc88d0f..0000000000 --- a/examples/how-to-use-webrtc-star/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# How to use WebRTC star - -There's still a bit of work required to start up an in-browser node in a robust way, so that it will work whether or not there is an existing initialized IPFS repo in the user's browser. If there isn't one, you need to call `init` as above, but if there is one, calling `init` will fail. Moreover, there's currently no good way to check if you need to call `init` or not. - -Also, an in-browser node isn't able to call up normal IPFS nodes over raw TCP; it can only communicate over Websockets and WebRTC. Currently, there are no Websockets or WebRTC bootstrap nodes run by the IPFS maintainers. You will probably want to set up a [libp2p-webrtc-star signaling server](https://github.com/libp2p/js-libp2p-webrtc-star) so nodes used in your application can find each other: - -```bash -npm i libp2p-webrtc-star -g -star-sig -``` - -You will then want to point IPFS nodes used in your application at your signaling server, so they can connect to each other. This is accomplished by adding an address to the node's configuration referencing the signaling server, of the form `/libp2p-webrtc-star/ip4//tcp//ws/ipfs/`, where `` is the peer ID of the node that the address is being added to. This causes the node to think of itself as being contactable through the signaling server. It will then initializes its libp2p-webrtc-star implementation and automatically peer with other nodes using the same server. - -The `index.html` page in this directory is an example which initializes an IPFS node in a browser safely, whether a node has already been initialized by the current domain or not. It also configures `libp2p-webrtc-star` communication, using a signaling server running on the local host. (Note that since IPFS node configuration information is stored in IndexedDB in browsers, opening two tabs of this code from a local file in the same browser won't work, because they'll share the same node keys and identity. Either run the code from multiple domains, or run it in two different browsers, like Chrome and Firefox.) diff --git a/examples/how-to-use-webrtc-star/index.html b/examples/how-to-use-webrtc-star/index.html deleted file mode 100644 index f614a806aa..0000000000 --- a/examples/how-to-use-webrtc-star/index.html +++ /dev/null @@ -1,99 +0,0 @@ - - - - IPFS in the Browser with WebRTC - - - - -

IPFS in the Browser with WebRTC

-

This page creates an IPFS node in your browser and drops it into the global Javascript namespace as ipfs. Open the console to play around with it.

-

Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.

-
Node status: offline
-

Run a libp2p-webrtc-star signaling server! Peers detected through the server will be displayed below:

-

Peers:

-
- - From ebb0717233f49c64f3a021108d258391615a7229 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 13 Mar 2017 10:26:54 +0000 Subject: [PATCH 10/10] docs: last touches to the README and browser-script-tag example --- README.md | 2 +- examples/browser-script-tag/README.md | 2 - examples/browser-script-tag/index.html | 63 ++++++++++++-------------- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 21e38e6d8b..1c34e57ec8 100644 --- a/README.md +++ b/README.md @@ -261,7 +261,7 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int A set of data types are exposed directly from the IPFS instance under `ipfs.types`. That way you're not required to import/require the following. -* `ipfs.types.Buffer` +* [`ipfs.types.Buffer`](https://www.npmjs.com/package/buffer) * [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id) * [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info) * [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr) diff --git a/examples/browser-script-tag/README.md b/examples/browser-script-tag/README.md index 41f006600d..08c42b0228 100644 --- a/examples/browser-script-tag/README.md +++ b/examples/browser-script-tag/README.md @@ -9,5 +9,3 @@ You can use IPFS in your in-browser JavaScript code with just a `