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

Commit 610fd7a

Browse files
committed
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.
1 parent 053716a commit 610fd7a

File tree

1 file changed

+110
-1
lines changed

1 file changed

+110
-1
lines changed

README.md

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,114 @@ The last published version of the package become [available for download](htt
110110

111111
### Examples
112112

113-
> **Will come soon**
113+
Below are spome examples of JavaScript IPFS in action.
114+
115+
#### Startup (in Node.js or browser)
116+
117+
There's still a bit of work required to start up a node in a robust way (i.e. without requiring the user to manually `js-ipfs init`). Here is one way to do it.
118+
119+
```javascript
120+
// Find the IPFS implementation
121+
var IPFS
122+
if (!(typeof require === 'undefined')) {
123+
// Can load through Node
124+
IPFS = require('ipfs')
125+
} else if (!(typeof Ipfs === 'undefined')) {
126+
// Can load through browser global
127+
IPFS = Ipfs
128+
}
129+
130+
// Make an IPFS node
131+
var ipfs = new IPFS()
132+
133+
// Init a repo in the given IPFS node it if hasn't got one already. Calsl the
134+
// setup callback, passing the normal callback, after first initialization.
135+
// Calls the normal callback directly after subsequent initializations. Calls
136+
// the normal callback with an error parameter if there is an error.
137+
function initIfNeeded (ipfs, setup, callback) {
138+
ipfs.init((err) => {
139+
if (!err) {
140+
// This is the first time we have started a node
141+
setup(callback)
142+
} else if (err.message == 'repo already exists') {
143+
// The node already exists
144+
callback()
145+
} else {
146+
callback(err)
147+
}
148+
})
149+
}
150+
151+
// Init the node
152+
initIfNeeded(ipfs, (callback) => {
153+
// On first initialization, do some setup
154+
// Get the node config we just init-ed
155+
ipfs.config.get((err, config) => {
156+
if (err) {
157+
throw err
158+
}
159+
   // Add at least one libp2p-webrtc-star address. Without an address like this
160+
// the libp2p-webrtc-star transport won't be installed, and the resulting
161+
// node won't be able to dial out to libp2p-webrtc-star addresses.
162+
var star_addr = ('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/' +
163+
config.Identity.PeerID)
164+
ipfs.config.set('Addresses.Swarm[1]', star_addr, (err) => {
165+
if (err) {
166+
throw err
167+
}
168+
// Continue down the already-initialized code path
169+
callback()
170+
})
171+
})
172+
}, (err) => {
173+
// If the repo was already initialized, or after the first-time initialization
174+
// code is run, we'll do this.
175+
if (err) {
176+
throw err
177+
}
178+
// Have the node set itself up
179+
ipfs.load(() => {
180+
// Go online and connect to things
181+
ipfs.goOnline(() => {
182+
console.log('Online status: ', ipfs.isOnline() ? 'online' : 'offline')
183+
// TODO: Write your code here!
184+
// Use methods like ipfs.files.add, ipfs.files.get, and so on in here
185+
// Methods requiring buffers can use ipfs.Buffer
186+
})
187+
})
188+
})
189+
```
190+
191+
#### Adding a file
192+
193+
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.
194+
195+
```javascript
196+
// Add a single file
197+
ipfs.files.add(ipfs.Buffer.from('Hello world'), (err, returned) => {
198+
if (err) {
199+
throw err
200+
}
201+
console.log('IPFS hash: ', returned[0].hash)
202+
})
203+
```
204+
205+
#### Retrieving a file
206+
207+
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.
208+
209+
```javascript
210+
ipfs.files.cat('QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve',
211+
(err, content_stream) => {
212+
213+
if (err) {
214+
throw err
215+
}
216+
content_stream.on('data', (buffer) => {
217+
console.log('File contents:', buffer.toString('ascii'))
218+
})
219+
})
220+
```
114221

115222
### API
116223

@@ -187,6 +294,8 @@ IPFS Core is divided into separate subsystems, each of them exist in their own r
187294

188295
IPFS Core is the entry point module for IPFS. It exposes an interface defined on [IPFS Specs.](https://github.com/ipfs/specs/blob/ipfs/api/api/core/README.md)
189296

297+
This particular implementation also provides some extensions to assist with usage in a browser, such as the `Buffer` type, required as input for some API methods, being available as `ipfs.Buffer` on every `IPFS` object.
298+
190299
#### Block Service
191300

192301
Block Service uses IPFS Repo (local storage) and Bitswap (network storage) to store and fetch blocks. A block is a serialized MerkleDAG node.

0 commit comments

Comments
 (0)