Skip to content

Commit

Permalink
update examples to be db2-first
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Dec 10, 2023
1 parent 927656e commit 2731cf1
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const caps = require('ssb-caps')


const stack = Stack({ caps })
.use(require('ssb-db')) // << required
.use(require('ssb-profile'))
.use(require('ssb-d2'))
.use(require('ssb-recps-guard')) // << must be last

const config = {
Expand All @@ -28,20 +27,24 @@ const sever = stack(config)

auto-blocked:
```js
const unallowedMsg = { type: 'profile' }
const unallowedMsg = {
content: { type: 'profile' }
}

server.publish(unallowedMsg, (err, msg) => {
server.db.create(unallowedMsg, (err, msg) => {
console.log(err)
// => Error: recps-guard - no accidental public messages allowed!
})
```

config-allowed:
```js
const allowedMsg = { type: 'contact' }
const allowedMsg = {
content: { type: 'contact' }
}
// this type was allowed in our config (see above)

server.publish(allowedType, (err, msg) => {
server.db.create(allowedType, (err, msg) => {
console.log(msg.value.content)
// => { type: 'contact' }
})
Expand All @@ -54,35 +57,23 @@ const explicitPublicMsg = {
allowPublic: true
}

server.publish(explicitPublicMsg, (err, msg) => {
console.log(msg.value.content)
// => { type: 'profile' }

// NOTE: only `content` is published
})

// with ssb-db2's ssb.db.create, note different option format!
const explicitPublicMsgDb2 = {
content: { type: 'profile' },
allowPublic: true
}

server.db.create(explicitPublicMsgDb2, (err, msg) => {
server.db.create(explicitPublicMsg, (err, msg) => {
console.log(msg.value.content)
// => { type: 'profile' }

// NOTE: only `content` is published (as usual)
})
```


private:
```js
const privateMsg = {
type: 'profile'
recps: ['@ye+QM09iPcDJD6YvQYjoQc7sLF/IFhmNbEqgdzQo3lQ=.ed25519']
content: {
type: 'profile'
recps: ['@ye+QM09iPcDJD6YvQYjoQc7sLF/IFhmNbEqgdzQo3lQ=.ed25519']
}
}

server.publish(privateMsg, (err, msg) => {
server.db.create(privateMsg, (err, msg) => {
console.log(msg.value.content)
// => VayTFa.....yZ3Wqsg==.box

Expand All @@ -91,6 +82,21 @@ server.publish(privateMsg, (err, msg) => {
})
```

NOTE that if you are using _classic_ `ssb-db`, the API behaves the same:

```js
const explicitPublicMsgDB1 = {
content: { type: 'profile' },
allowPublic: true
}

server.db.create(explicitPublicMsgDN!, (err, msg) => {
console.log(msg.value.content)
// => { type: 'profile' }
})
```


## Installation

Because `ssb-recps-guard` hooks the publish method you **must install it as the LAST plugin**
Expand Down Expand Up @@ -118,14 +124,15 @@ where `allowedTypes` is an Array of message types which are allowed to be publis
## Explicit bypass

Messages which would normally be blocked by the guard bypass the guard by changing what's passed to the
publish method to be of form `{ content, options: { allowPublic: true } }`
publish method to be of form `{ content, allowPublic: true }`

The `content` is what will be passed to the normal publish function.

Design: this is deliberately verbose to avoid accidental publishing.
It also has the benefit that if `ssb-guard-recps` isn't installed this publish will error because publish
will expect the `type` to be in a different place.


## API

You can check if `ssb-recps-guard` is installed in your server by looking to
Expand Down

0 comments on commit 2731cf1

Please sign in to comment.