-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
106 lines (89 loc) · 3.25 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
module.exports = StoreAPIFactory
var EventEmitter = require('events').EventEmitter
var hoodieApi = require('pouchdb-hoodie-api')
var request = require('request').defaults({json: true})
var create = require('./store/create')
var destroy = require('./store/destroy')
var exists = require('./store/exists')
var open = require('./store/open')
var on = require('./store/on')
var one = require('./store/one')
var off = require('./store/off')
var grantAccess = require('./store/grant')
var hasAccess = require('./store/has-access')
var revokeAccess = require('./store/revoke')
var replicate = require('./store/replicate')
var cancelReplication = require('./store/cancel-replication')
var cache = require('./utils/cache')
var replicationIdPrefix = require('./utils/to-replication-id').prefix
var toCouchDbUrl = require('./utils/pouchdb-options-to-couchdb-url')
function StoreAPIFactory (PouchDB) {
if (typeof PouchDB !== 'function') {
throw new Error('Store API Factory requires PouchDB as first argument')
}
// https://github.com/hoodiehq/pouchdb-hoodie-api
PouchDB.plugin(hoodieApi)
var stateStore = new PouchDB('hoodie-store').hoodieApi()
var replicationsMap = {}
var adapter = new PouchDB('hack', {skip_setup: true}).adapter
var usesHttpAdapter = adapter === 'http' || adapter === 'https'
var couchDbUrl = toCouchDbUrl(stateStore.db.__opts)
var replicationsReady = stateStore.withIdPrefix(replicationIdPrefix).findAll()
.then(function (replications) {
return replications.forEach(function (replication) {
if (replicationsMap[replication._id]) {
return
}
replicationsMap[replication._id] = PouchDB.replicate(replication.source, replication.target, replication.options)
})
})
if (usesHttpAdapter) {
replicationsReady = replicationsReady.then(function () {
return new Promise(function (resolve, reject) {
request({
method: 'put',
url: couchDbUrl + 'hoodie-store/_security',
body: {
members: {
roles: ['_hoodie_admin_only']
}
}
}, function (error, response, data) {
if (error) {
return reject(error)
}
if (response.statusCode >= 400) {
return reject(new Error(data.reason))
}
resolve()
})
})
})
}
var state = {
PouchDB: PouchDB,
stateStore: stateStore,
couchDbUrl: couchDbUrl,
usesHttpAdapter: usesHttpAdapter,
replicationsReady: replicationsReady,
replicationsMap: replicationsMap,
emitter: new EventEmitter(),
cache: cache(stateStore)
}
var Store = {}
// set adapter to http for both http & https
Store.adapter = usesHttpAdapter ? 'http' : adapter
Store.create = create.bind(null, state)
Store.destroy = destroy.bind(null, state)
Store.exists = exists.bind(null, state)
Store.open = open.bind(null, state)
Store.grant = grantAccess.bind(null, state)
Store.hasAccess = hasAccess.bind(null, state)
Store.revoke = revokeAccess.bind(null, state)
Store.replicate = replicate.bind(null, state)
Store.cancelReplication = cancelReplication.bind(null, state)
Store.on = on.bind(Store, state)
Store.one = one.bind(Store, state)
Store.off = off.bind(Store, state)
return Store
}