Skip to content

Commit

Permalink
Merge pull request #301 from ssbc/boxer-init
Browse files Browse the repository at this point in the history
Add boxer init method
  • Loading branch information
mixmix authored Jun 22, 2020
2 parents 43b25c7 + eafa5e7 commit c37ea16
Show file tree
Hide file tree
Showing 23 changed files with 551 additions and 227 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ node_js:
- node # latest

os:
- linux
- osx
- windows
- osx
- linux

31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,34 +593,43 @@ db.since(fn(seq)) => Obv
An [observable](https://github.com/dominictarr/obv) of the current log sequence. This is always a positive
integer that usually increases, except in the exceptional circumstance that the log is deleted or corrupted.

### db.addBoxer(boxer)
### db.addBoxer: sync

```js
db.addUnboxer({ value: boxer, init: initUnboxer })
```

Add a `boxer`, which will be added to the list of boxers which will try to
automatically box (encrypt) the message `content` if the appropriate
`content.recps` is provided.

`boxer` is a function of signature `boxer(msg.value.content) => ciphertext` which is expected to:
- successfully box the content (based on `content.recps`), returning a `ciphertext` String
- not know how to box this content (because recps are outside it's understanding), and `undefined` (or `null`)
- break (because it should know how to handle `recps`, but can't), and so throw an `Error`
Where:
- `boxer (msg.value.content) => ciphertext` which is expected to either:
- successfully box the content (based on `content.recps`), returning a `ciphertext` String
- not know how to box this content (because recps are outside it's understanding), and `undefined` (or `null`)
- break (because it should know how to handle `recps`, but can't), and so throw an `Error`
- `initUnboxer (done) => null` (optional)
- is a functional which allows you set up your unboxer
- you're expected to call `done()` once all your initialisation is complete

## db.addUnboxer: sync

```js
db.addUnboxer({ key: unboxKey, value: unboxValue, init: initBoxer })
```

Add an unboxer object, any encrypted message is passed to the unboxer object to
test if it can be unboxed (decrypted)

Where:
- `unboxKey(msg.value.content, msg.value) => readKey`
- Is a function which tries to extract the message key from the encrypted content (`ciphertext`).
- Is expected to return `readKey` which is the read capability for the message
- `unboxValue(msg.value.content, msg.value, readKey) => plainContent`
- Is a function which takes a `readKey` and uses it to try to extract the `plainContent` from the `ciphertext- `initBoxer(done)`
- Is an optional initialisation function (useful for asynchronously setting up state for unboxer)
- It's pased a `done` callback which you need to call once everything is ready to go

NOTE: There's an alternative way to use `addUnboxer` but read the source to understand that.
- `unboxValue(msg.value.content, msg.value, readKey) => plainContent`
- Is a function which takes a `readKey` and uses it to try to extract the `plainContent` from the `ciphertext
- `initBoxer (done) => null` (optional)
- is a functional which allows you set up your boxer
- you're expected to call `done()` once all your initialisation is complete

## db.box(content, recps, cb)

Expand Down
20 changes: 17 additions & 3 deletions minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = function (dirname, keys, opts) {

var setup = {
validators: new u.AsyncJobQueue(),
unboxers: new u.AsyncJobQueue()
unboxers: new u.AsyncJobQueue(),
boxers: new u.AsyncJobQueue()
}
function waitForValidators (fn) {
return function (...args) {
Expand All @@ -41,6 +42,11 @@ module.exports = function (dirname, keys, opts) {
setup.unboxers.runAll(() => fn(...args))
}
}
function waitForBoxers (fn) {
return function (...args) {
setup.boxers.runAll(() => fn(...args))
}
}

const unboxerMap = waitForUnboxers((msg, cb) => {
try {
Expand Down Expand Up @@ -108,7 +114,7 @@ module.exports = function (dirname, keys, opts) {
})
})

db.append = waitForUnboxers(waitForValidators(function dbAppend (opts, cb) {
db.append = waitForBoxers(waitForValidators(function dbAppend (opts, cb) {
try {
const content = box(opts.content, boxers)
var msg = V.create(
Expand Down Expand Up @@ -144,7 +150,15 @@ module.exports = function (dirname, keys, opts) {
}

db.addBoxer = function addBoxer (boxer) {
boxers.push(boxer)
if (typeof boxer === 'function') return db.addBoxer({ value: boxer })
if (typeof boxer.value !== 'function') throw new Error('invalid boxer')

if (boxer.init) {
setup.boxers.add(boxer.init)
setup.boxers.runAll()
}

boxers.push(boxer.value)
}

db.addUnboxer = function addUnboxer (unboxer) {
Expand Down
Loading

0 comments on commit c37ea16

Please sign in to comment.