You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
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.
Copy file name to clipboardExpand all lines: README.md
+110-1Lines changed: 110 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,7 +110,114 @@ The last published version of the package become [available for download](htt
110
110
111
111
### Examples
112
112
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
+
varIPFS
122
+
if (!(typeof require ==='undefined')) {
123
+
// Can load through Node
124
+
IPFS=require('ipfs')
125
+
} elseif (!(typeof Ipfs ==='undefined')) {
126
+
// Can load through browser global
127
+
IPFS= Ipfs
128
+
}
129
+
130
+
// Make an IPFS node
131
+
var ipfs =newIPFS()
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
+
functioninitIfNeeded (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
+
} elseif (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/'+
// 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.
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.
@@ -187,6 +294,8 @@ IPFS Core is divided into separate subsystems, each of them exist in their own r
187
294
188
295
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)
189
296
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
+
190
299
#### Block Service
191
300
192
301
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