Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 4c95bf1

Browse files
authored
Merge branch 'master' into feat/more-dag-api
2 parents 2cec2a4 + e769719 commit 4c95bf1

File tree

7 files changed

+157
-3
lines changed

7 files changed

+157
-3
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ You can check the development status at:
9494
- [Files API](#files-api)
9595
- [Swarm API](#swarm-api)
9696
- [libp2p API](#libp2p-api)
97+
- [Domain data types](#domain-data-types)
9798
- [Packages](#packages)
9899
- [Development](#development)
99100
- [Clone](#clone)
@@ -193,6 +194,8 @@ The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https:/
193194

194195
#### Create a IPFS node instance
195196

197+
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:
198+
196199
```JavaScript
197200
// IPFS will need a repo, it can create one for you or you can pass
198201
// it a repo instance of the type IPFS Repo
@@ -229,6 +232,8 @@ node.init({ emptyRepo: true, bits: 2048 }, (err) => {
229232
230233
> We are working on making this init process better, see https://github.com/ipfs/js-ipfs/issues/556 for the discussion.
231234
235+
More examples can be found in the [examples folder](./examples)
236+
232237
### [Tutorials and Examples](/examples)
233238
234239
You can find some examples and tutorials in the [examples](/examples) folder, these exist to help you get started using `js-ipfs`.
@@ -252,6 +257,17 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int
252257
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-libp2p-ipfs-nodejs)
253258
- [libp2p-ipfs-browser](https://github.com/ipfs/js-libp2p-ipfs-browser)
254259
260+
#### Domain data types
261+
262+
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.
263+
264+
* [`ipfs.types.Buffer`](https://www.npmjs.com/package/buffer)
265+
* [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id)
266+
* [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info)
267+
* [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr)
268+
* [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash)
269+
* [`ipfs.types.CID`](https://github.com/ipld/js-cid)
270+
255271
## Packages
256272
257273
| Package | Version | Deps | DevDeps |
@@ -287,8 +303,6 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int
287303
| [`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) |
288304
| [`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) |
289305
290-
291-
292306
## Development
293307
294308
### Clone and install dependencies

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Let us know if you find any issue or if you want to contribute and add a new tut
99
- [js-ipfs basic, how to spawn a node and add a file to IPFS](./basics)
1010
- [How to bundle js-ipfs with Browserify](./bundle-browserify)
1111
- [How to bundle js-ipfs with WebPack](./bundle-webpack)
12+
- [How to use js-ipfs with a script tag](./browser-script-tag)
1213
- [Use IPFS to explore the Ethereum BlockChain](./explore-ethereum)
1314
- [Create and resolve through graphs with the dag API](./dag)
1415

examples/browser-script-tag/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Use IPFS in the browser with `<script>` tags
2+
3+
You can use IPFS in your in-browser JavaScript code with just a `<script>` tag.
4+
5+
```
6+
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
7+
```
8+
9+
This exposes a global `Ipfs`; you can get a node by making a `new Ipfs()`.
10+
11+
See `index.html` for a working example.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>IPFS in the Browser</title>
5+
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
6+
<script type="text/javascript">
7+
// We provide a hosted signalling endpoint that you can use to discover
8+
// and dial to other nodes. It is hosted at `star-signal.cloud.ipfs.team`
9+
// If you run your own signalling, you can change this multiaddr.
10+
const SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/'
11+
12+
const repoPath = 'ipfs-' + Math.random()
13+
14+
// Create an IPFS node
15+
const node = new Ipfs({
16+
repo: repoPath
17+
})
18+
19+
// Init the node
20+
node.init(handleInit)
21+
22+
function handleInit (err) {
23+
if (!err) { // The repo was initialized for the first time, we need to configure it
24+
addWebRTCMultiaddr()
25+
} else if (err && err.message !== 'repo already exists') { // The repo already existed, let's just load it
26+
loadRepo()
27+
} else {
28+
throw err
29+
}
30+
}
31+
32+
function addWebRTCMultiaddr() {
33+
// Addj the WebrTCStar Multiaddr to your node
34+
node.config.get(function (err, config) {
35+
if (err) {
36+
throw err
37+
}
38+
39+
const starAddr = (SIGNALING_SERVER + config.Identity.PeerID)
40+
41+
node.config.set('Addresses.Swarm[1]', starAddr, loadRepo)
42+
})
43+
}
44+
45+
function loadRepo() {
46+
node.load(() => node.goOnline(() => {
47+
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')
48+
49+
document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')
50+
51+
// \o/ Now you have an IPFS node using WebRTC to find other nodes!
52+
// You can write more code here to use it. Use methods like
53+
// node.files.add, node.files.get. See the API docs here:
54+
// https://github.com/ipfs/interface-ipfs-core/tree/master/API
55+
}))
56+
}
57+
</script>
58+
</head>
59+
<body>
60+
<h1>IPFS in the Browser</h1>
61+
<p>This page creates an IPFS node in your browser and drops it into the global Javascript namespace as <b><em style="background-color:#d7d6d6">node</em></b>. Open the console to play around with it.</p>
62+
<p>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.</p>
63+
<div id="status">Node status: offline</div>
64+
65+
<h2>Some suggestions</h2>
66+
67+
<p>Try adding a new file:</p>
68+
69+
<code style="display:block; white-space:pre-wrap; background-color:#d7d6d6">
70+
node.files.add(new node.types.Buffer('Hello world!'), function (err, res) {
71+
if (err || !res) {
72+
return console.error('Error - ipfs files add', err, res)
73+
}
74+
75+
res.forEach(function (file) {
76+
console.log('successfully stored', file)
77+
})
78+
})
79+
</code>
80+
81+
<p>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'</p>
82+
83+
<code style="display:block; white-space:pre-wrap; background-color:#d7d6d6">
84+
node.files.cat('QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY', function (err, stream) {
85+
var res = ''
86+
87+
stream.on('data', function (chunk) {
88+
res += chunk.toString()
89+
})
90+
91+
stream.on('error', function (err) {
92+
console.error('Error - ipfs files cat ', err)
93+
})
94+
95+
stream.on('end', function () {
96+
console.log('Got:', res)
97+
})
98+
})
99+
</code>
100+
</body>
101+
</html>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"bl": "^1.2.0",
9595
"boom": "^4.2.0",
9696
"debug": "^2.6.2",
97+
"cids": "^0.4.1",
9798
"fs-pull-blob-store": "~0.4.1",
9899
"glob": "^7.1.1",
99100
"hapi": "^16.1.0",

src/core/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
const BlockService = require('ipfs-block-service')
44
const IPLDResolver = require('ipld-resolver')
5+
const PeerId = require('peer-id')
6+
const PeerInfo = require('peer-info')
7+
const multiaddr = require('multiaddr')
8+
const multihash = require('multihashes')
59
const PeerBook = require('peer-book')
10+
const CID = require('cids')
611
const debug = require('debug')
712

813
const defaultRepo = require('./default-repo')
@@ -24,7 +29,12 @@ class IPFS {
2429

2530
// IPFS utils
2631
this.types = {
27-
Buffer: Buffer
32+
Buffer,
33+
PeerId,
34+
PeerInfo,
35+
multiaddr,
36+
multihash,
37+
CID
2838
}
2939
this.log = debug('jsipfs')
3040
this.log.err = debug('jsipfs:err')

test/core/init.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
const expect = require('chai').expect
66
const isNode = require('detect-node')
7+
const PeerId = require('peer-id')
8+
const PeerInfo = require('peer-info')
9+
const multiaddr = require('multiaddr')
10+
const multihash = require('multihashes')
11+
const CID = require('cids')
712
const IPFS = require('../../src/core')
813

914
// This gets replaced by require('../utils/create-repo-browser.js')
@@ -83,4 +88,15 @@ describe('init', () => {
8388
})
8489
})
8590
})
91+
92+
it('data types', () => {
93+
expect(ipfs.types).to.be.deep.equal({
94+
Buffer,
95+
PeerId,
96+
PeerInfo,
97+
multiaddr,
98+
multihash,
99+
CID
100+
})
101+
})
86102
})

0 commit comments

Comments
 (0)