-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
177 lines (154 loc) · 4.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var permit = require('couch-nacl-permit')
var box = require('couch-box')
var Promise = require('pouchdb-promise')
var ddoc = {
_id: '_design/box',
views: {
receivers: {
map: function(doc) {
if (typeof doc.box !== 'object') return
if (typeof doc.box.receivers !== 'object') return
for (var receiver in doc.box.receivers) {
emit(receiver, null)
}
}.toString(),
reduce: '_count'
}
}
}
exports.transform = require('transform-pouch').transform
exports.box = function(sessionKeyPair, receivers) {
var db = this
var mypermit = permit(sessionKeyPair)
return db
// setup design document
.get(ddoc._id)
.catch(function(err) {
if (err.status === 404) {
return db.put(ddoc)
}
return err
})
// setup permit
.then(function() {
return db.get(mypermit._id, { conflicts: true })
})
.then(function(doc) {
mypermit.parse(doc)
})
.catch(function(err) {
if (err.status === 404) {
mypermit.build()
var doc = mypermit.toJSON()
return db.put(doc)
.then(function(resp) {
mypermit._rev = resp.rev
})
}
return err
})
// handle permit conflicts
.then(function() {
// if conflicts, resolve and use the new permit
if (mypermit._conflicts) {
return db
// fetch other conflicting revisions
.get(mypermit._id, {
open_revs: mypermit._conflicts
})
// parse other conflicting revisions
.then(function(conflicts) {
return conflicts
.map(function(conflict) {
return permit(sessionKeyPair)
.parse(conflict.ok)
})
.concat(mypermit)
})
// query receiver counts
.then(function(permits) {
return db
.query('box/receivers', {
keys: permits.map(function(p) {
return p.receiver()
}),
group: true
})
// remember receiver counts
.then(function(counts) {
counts.rows.forEach(function(row, i) {
permits[i].count = row.value
})
// sort by receiver counts
return permits.sort(function(a, b) {
if (a.count === b.count) return 0
return a.count > b.count ? 1 : -1
})
})
})
// convert permits
.then(function(permits) {
var choosenPermit = permits.pop()
var choosenBox = box(choosenPermit.databaseKey)
return Promise
.all(permits.map(function(p) {
if (p.count === 0) return db.remove(p._id, p._rev)
return db
// get docs for receiver
.query('box/receivers', {
reduce: false,
key: p.receiver(),
include_docs: true
})
// decrypt docs
.then(function(view) {
var pbox = box(p.databaseKey)
return view.rows.map(function(row) {
return pbox.open(row.doc)
})
})
// encrypt docs with chosen permit
.then(function(docs) {
return docs.map(function(doc) {
return choosenBox(doc)
})
})
// save docs
.then(function(docs) {
return db.bulkDocs(docs)
})
// delete permit
.then(function(resps) {
var failures = resps.filter(function(resp) {
return !resp.ok
})
if (failures.length === 0) return db.remove(p._id, p._rev)
})
}))
.then(function() {
delete choosenPermit._conflicts
})
// update current permit with choosen permit
.then(function() {
if (mypermit._rev !== choosenPermit._rev) {
mypermit.parse(choosenPermit.toJSON())
}
})
})
}
})
// setup transform pouch
.then(function() {
var mybox = box(mypermit.databaseKey, receivers)
db.transform({
incoming: mybox,
outgoing: mybox.open
})
db.closeBox = mybox.close
return mypermit
})
}
if (typeof window !== 'undefined' && window.PouchDB) {
window.nacl = require('tweetnacl')
window.PouchDB.plugin(module.exports)
}