Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit 85e6848

Browse files
authored
Pre release (#85)
* docs: Update README to match new version. * docs: Update events example to use new API. * docs: Correctly print out db query results. * test: Remove concurrent. * test: Remove unimplemented and 3rd party AC tests. * test: Remove unimplemented and 3rd party identity tests. * docs: Move jsdoc config to conf directory. * Point package.json main at index.js to access all exported functions. * docs: Vetted AC docs; these examples should work if implemented in code. Explicitly show orbit-db function imports. * docs: Fix incorrectly declared write objects. * docs: Improved canAppend documentation. Better JS syntax highlighting. * docs: wss and define filters for localhost separately. * docs: Simplified webSockets implementation with filters. * docs: Return manifest json only (no hash). JS highlighting. * docs: Remove operations documentation. * docs: Update heading levels. * docs: Differentiate between db types which expose put/add function. * docs: Correctly import IPFS and pass config. * docs: A simple method for full db replication. * docs: Link to existing examples of db implementation. * docs: Update heading. * docs: JS code formatting. import statements. * docs: Expand on the concepts of identities and identity management. * docs: Describe head sync-ing and full replication. * docs: Comprehensive explanation of setting up a db and sync-ing/replicating data across peers. Examples can be run in node.js. * docs: Syntax highlighting. Correct code implementation for custom/3rd party storage implementations. * docs: Getting started cleanup. * docs: Manifest as an IPLD data strcture.
1 parent 0c01bd2 commit 85e6848

7 files changed

+501
-171
lines changed

docs/ACCESS_CONTROLLERS.md

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ An access controller is passed when a database is opened for the first time. Onc
66

77
Different access controllers can be assigned to the database using the `AccessController` param and passing it to OrbitDB's `open` function.
88

9-
```
10-
const orbitdb = await OrbitDB()
9+
```js
10+
import { create } from 'ipfs-core'
11+
import { OrbitDB, getAccessController } from 'orbit-db'
12+
13+
const ipfs = create({ options })
14+
15+
const orbitdb = await OrbitDB({ ipfs })
16+
17+
// SomeAccessController must already be available in the AC list.
18+
const SomeAccessController = getAccessController('some-access-controller')
19+
1120
const db = orbitdb.open('my-db', { AccessController: SomeAccessController() })
1221
```
1322

@@ -17,65 +26,99 @@ OrbitDB is bundled with two AccessControllers; IPFSAccessController, an immutabl
1726

1827
By default, the database `db` will use the IPFSAccessController and allow only the creator to write to the database.
1928

20-
```
21-
const orbitdb = await OrbitDB()
29+
```js
30+
const orbitdb = await OrbitDB({ ipfs })
2231
const db = orbitdb.open('my-db')
32+
33+
await db.add('hello world') // only orbitdb.identity.id can write to the db.
2334
```
2435

25-
To change write access, pass the IPFSAccessController with the `write ` parameter and an array of one or more Identity ids:
36+
To change write access, pass the IPFSAccessController with the `write` parameter and an array of one or more Identity ids:
37+
38+
```js
39+
import { create } from 'ipfs-core'
40+
import { OrbitDB, Identities, getAccessController } from 'orbit-db'
41+
42+
const ipfs = create({ options })
2643

27-
```
2844
const identities = await Identities()
29-
const identity1 = identities.createIdentity('userA')
30-
const identity2 = identities.createIdentity('userB')
45+
const anotherIdentity = identities.createIdentity('userB')
46+
47+
// OrbitDB will create an identity using the id 'UserA'.
48+
const orbitdb = await OrbitDB({ ipfs, id: 'userA' })
49+
50+
// Retrieve the access controller from the list of preloaded ACs.
51+
const IPFSAccessController = getAccessController('ipfs')
3152

32-
const orbitdb = await OrbitDB()
33-
const db = orbitdb.open('my-db', { AccessController: IPFSAccessController(write: [identity1.id, identity2.id]) })
53+
// Open a db with write access for userA and userB.
54+
const db = orbitdb.open('my-db', { AccessController: IPFSAccessController({ write: [orbitdb.identity.id, anotherIdentity.id]) })
3455
```
3556
3657
To allow anyone to write to the database, specify the wildcard '*':
3758
59+
```js
60+
import { create } from 'ipfs-core'
61+
import { OrbitDB, Identities, getAccessController } from 'orbit-db'
62+
63+
const ipfs = create({ options })
64+
65+
const orbitdb = await OrbitDB({ ipfs })
66+
67+
const IPFSAccessController = getAccessController('ipfs')
68+
69+
const db = orbitdb.open('my-db', { AccessController: IPFSAccessController({ write: ['*'] }) })
3870
```
39-
const orbitdb = await OrbitDB()
40-
const db = orbitdb.open('my-db', { AccessController: IPFSAccessController(write: ['*']) })
41-
```
71+
72+
**The access information cannot be changed after the initial setup (as it is immutable).** If different write access is needed, you will need to set up a new database and associated IPFSAccessController. It is important to note that this will change the address of the database.
4273
4374
## OrbitDB Access Controller
4475
4576
The OrbitDB access controller provides configurable write access using grant and revoke.
4677
47-
```
78+
```js
79+
import { create } from 'ipfs-core'
80+
import { OrbitDB, Identities, getAccessController } from 'orbit-db'
81+
82+
const ipfs = create({ options })
83+
84+
const orbitdb = await OrbitDB({ ipfs })
85+
4886
const identities = await Identities()
49-
const identity1 = identities.createIdentity('userA')
50-
const identity2 = identities.createIdentity('userB')
87+
const anotherIdentity = identities.createIdentity('userB')
5188

52-
const orbitdb = await OrbitDB()
53-
const db = orbitdb.open('my-db', { AccessController: OrbitDBAccessController(write: [identity1.id]) })
89+
// Retrieve the access controller from the list of preloaded ACs.
90+
const OrbitDBAccessController = getAccessController('orbitdb')
5491

55-
db.access.grant('write', identity2.id)
56-
db.access.revoke('write', identity2.id)
92+
const db = orbitdb.open('my-db', { AccessController: OrbitDBAccessController({ write: [orbitdb.identity.id, anotherIdentity.id]) })
93+
94+
db.access.grant('write', anotherIdentity.id)
95+
db.access.revoke('write', anotherIdentity.id)
5796
```
5897
5998
When granting or revoking access, a capability and the identity's id must be defined.
6099
61100
Grant and revoke are not limited to 'write' access only. A custom access capability can be specified, for example, `db.access.grant('custom-access', identity1.id)`.
62101
102+
The OrbitDBAccessController is a mutable access controller. Granting and revoking access does not change the address of the database.
103+
63104
## Custom Access Controller
64105
65106
Access can be customized by implementing a custom access controller. To implement a custom access controller, specify:
66107
67-
- A curried function with the function signature `async ({ orbitdb, identities, address })`,
108+
- A partial function with the function signature `async ({ orbitdb, identities, address })`,
68109
- A `type` constant,
69-
- A canAppend function with the param `entry`.
110+
- A canAppend function with the param `entry` and a boolean return type.
70111
71-
```
112+
The canAppend function must return true if the entry can be appended to the log or false otherwise.
113+
114+
```js
72115
const type = 'custom'
73116

74117
const CustomAccessController = () => async ({ orbitdb, identities, address }) => {
75118
address = '/custom/access-controller'
76119

77120
const canAppend = (entry) => {
78-
121+
// return true if the entry can be appended to the log, false otherwise.
79122
}
80123
}
81124

@@ -84,7 +127,7 @@ CustomAccessController.type = type
84127
85128
Additional configuration can be passed to the access controller by adding one or more parameters to the `CustomAccessController` function. For example, passing a configurable object parameter with the variable `write`:
86129
87-
```
130+
```js
88131
const CustomAccessController = ({ write }) => async ({ orbitdb, identities, address }) => {
89132
}
90133
```
@@ -95,7 +138,7 @@ The main driver of the access controller is the canAppend function. This specifi
95138
96139
How the custom access controller evaluates access will be determined by its use case, but in most instances, the canAppend function will want to check whether the entry being created can be written to the database (and underlying operations log). Therefore, the entry's identity will need to be used to retrieve the identity's id:
97140
98-
```
141+
```js
99142
write = [identity.id]
100143

101144
const canAppend = async (entry) => {
@@ -119,8 +162,11 @@ In the above example, the `entry.identity` will be the hash of the identity. Usi
119162
120163
Before passing the custom access controller to the `open` function, it must be added to OrbitDB's AccessControllers:
121164
122-
```
123-
AccessControllers.add(CustomAccessController)
124-
const orbitdb = await OrbitDB()
165+
```js
166+
import { create } from 'ipfs-core'
167+
import { OrbitDB, addAccessController } from 'orbit-db'
168+
169+
addAccessController(CustomAccessController)
170+
const orbitdb = await OrbitDB({ ipfs })
125171
const db = await orbitdb.open('my-db', { AccessController: CustomAccessController(params) })
126172
```

docs/CONNECTING_PEERS.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@ OrbitDB peers connect to one another using js-libp2p. Connection settings will v
77
Node.js allows libp2p to open connections with other Node.js daemons.
88

99
```javascript
10-
ipfs1 = await IPFS.create({ repo: './ipfs1' })
11-
ipfs2 = await IPFS.create({ repo: './ipfs2' })
10+
import { create } from 'ipfs-core'
11+
12+
const ipfs1 = await create({ repo: './ipfs1' })
13+
const ipfs2 = await create({ repo: './ipfs2' })
1214

1315
const cid = await ipfs1.block.put('here is some data')
1416
const block = await ipfs2.block.get(cid)
1517
```
1618

17-
On localhost or a local network, both ipfs nodes should discover each other quickly enough that ipfs2 will retrieve the block added to ipfs1.
19+
On localhost or a local network, both ipfs nodes should discover each other quickly enough so that ipfs2 will retrieve the block added to ipfs1.
1820

19-
In remote networks, retrieval of content across peers may take significantly longer. To speed up communication between the two peers, connect one peer to another directly using the swarm API and a peer's publicly accessible address. For example, assuming ipfs1 is listening on the address /ip4/1.2.3.4/tcp/12345/p2p/ipfs1-peer-hash:
21+
In remote networks, retrieval of content across peers may take significantly longer. To speed up communication between the two peers, one peer can be directly connected to another using the swarm API and a peer's publicly accessible address. For example, assuming ipfs1 is listening on the address /ip4/1.2.3.4/tcp/12345/p2p/ipfs1-peer-hash:
2022

2123
```javascript
22-
ipfs1 = await IPFS.create({ repo: './ipfs1' })
23-
ipfs2 = await IPFS.create({ repo: './ipfs2' })
24+
import { create } from 'ipfs-core'
25+
26+
const ipfs1 = await create({ repo: './ipfs1' })
27+
const ipfs2 = await create({ repo: './ipfs2' })
2428

2529
await ipfs2.swarm.connect('/ip4/1.2.3.4/tcp/12345/p2p/ipfs1-peer-hash')
2630

@@ -35,17 +39,17 @@ For various security reasons, a browser cannot dial another peer over a raw TCP
3539
On the server, listen for incoming websocket connections:
3640

3741
```javascript
38-
import { WebSockets } from '@libp2p/websockets'
42+
import { webSockets } from '@libp2p/websockets'
3943
import { create } from 'ipfs-core'
4044

41-
ipfs1 = await IPFS.create({
45+
ipfs1 = await create({
4246
libp2p: {
4347
addresses: {
4448
listen: [
45-
'/ip4/0.0.0.0/tcp/0/ws'
49+
'/ip4/0.0.0.0/tcp/0/wss'
4650
]
4751
},
48-
transports: [new WebSockets()]
52+
transports: [webSockets()]
4953
},
5054
repo: './ipfs1'
5155
})
@@ -55,21 +59,27 @@ Within the browser, dial into the server using the server's exposed web socket:
5559

5660
```javascript
5761
// import the following libraries if using a build environment such as vite.
58-
import { WebSockets } from '@libp2p/websockets'
62+
import { webSockets } from '@libp2p/websockets'
5963
import { create } from 'ipfs-core'
60-
import { all } from '@libp2p/websockets/filters'
6164

62-
// uncomment { filter: all } if no tls certificate is deployed. Only do this in development environments.
63-
const ws = new webSockets(/* { filter: all } */)
65+
const ws = new webSockets()
6466

65-
ipfs1 = await IPFS.create({
67+
ipfs1 = await create({
6668
libp2p: {
67-
transports: [new webSockets()]
69+
transports: [ws]
6870
}},
6971
repo: './ipfs1'
7072
})
7173
```
7274

75+
You may find IPFS is unable to connect to a local WebRTC Star server. This will likely to due to the local WebSockets transport being insecure (ws instead of wss). To solve this issue, pass the `all` filter to the `webSockets` function:
76+
77+
```
78+
import { all } from '@libp2p/websockets/filters'
79+
80+
const ws = webSockets({ filter: all })
81+
```
82+
7383
## Browser to Browser and Node Daemon to Browser
7484

7585
A connection cannot be made directly to a browser node. Browsers do not listen for incoming connections, they operate in a server/client environment where the server address is known and the browser connects to the server using the known address. Therefore, for a browser to respond to an incoming connection a relay is required to "listen" on the browser's behalf. The relay assigns and advertises multi addresses on behalf of the browser nodes, allowing the nodes to create a direct connection between each other.
@@ -86,7 +96,7 @@ In the first browser peer, configure
8696
import { create } from 'ipfs-core'
8797
import { multiaddr } from 'multiaddr'
8898

89-
ipfs = await IPFS.create({
99+
ipfs = await create({
90100
config: {
91101
Addresses: {
92102
Swarm: ['/ip4/0.0.0.0/tcp/12345/ws/p2p-webrtc-star']
@@ -101,7 +111,7 @@ Configure the second browser node in the same way as the first, then dial in to
101111
import { create } from 'ipfs-core'
102112
import { multiaddr } from 'multiaddr'
103113

104-
ipfs = await IPFS.create({
114+
ipfs = await create({
105115
config: {
106116
Addresses: {
107117
Swarm: ['/ip4/0.0.0.0/tcp/12345/ws/p2p-webrtc-star']

0 commit comments

Comments
 (0)