/
CozyPouchLink.js
582 lines (521 loc) · 16.5 KB
/
CozyPouchLink.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import {
MutationTypes,
CozyLink,
getDoctypeFromOperation,
BulkEditError
} from 'cozy-client'
import PouchDB from 'pouchdb-browser'
import PouchDBFind from 'pouchdb-find'
import omit from 'lodash/omit'
import defaults from 'lodash/defaults'
import mapValues from 'lodash/mapValues'
import zipWith from 'lodash/zipWith'
import get from 'lodash/get'
import { default as helpers } from './helpers'
import { getIndexNameFromFields, getIndexFields } from './mango'
import * as jsonapi from './jsonapi'
import PouchManager from './PouchManager'
import logger from './logger'
import { migratePouch } from './migrations/adapter'
import { getDatabaseName, getPrefix } from './utils'
import {
getPersistedSyncedDoctypes,
persistAdapterName,
getAdapterName,
destroyWarmedUpQueries
} from './localStorage'
PouchDB.plugin(PouchDBFind)
const { find, allDocs, withoutDesignDocuments } = helpers
const parseMutationResult = (original, res) => {
if (!res.ok) {
throw new Error('Pouch response is not OK')
}
return { ...original, ...omit(res, 'ok') }
}
const DEFAULT_OPTIONS = {
replicationInterval: 30 * 1000
}
const addBasicAuth = (url, basicAuth) => {
return url.replace('//', `//${basicAuth}`)
}
const sanitized = doc => omit(doc, '_type')
export const getReplicationURL = (uri, token, doctype) => {
const basicAuth = token.toBasicAuth()
const authenticatedURL = addBasicAuth(uri, basicAuth)
return `${authenticatedURL}/data/${doctype}`
}
const doNothing = () => {}
export const isExpiredTokenError = pouchError => {
return pouchError.error === 'code=400, message=Expired token'
}
const normalizeAll = (docs, doctype) => {
return docs.map(doc => jsonapi.normalizeDoc(doc, doctype))
}
/**
* @typedef {"idle"|"replicating"} SyncStatus
*/
/**
* Link to be passed to a `CozyClient` instance to support CouchDB. It instantiates
* PouchDB collections for each doctype that it supports and knows how
* to respond to queries and mutations.
*/
class PouchLink extends CozyLink {
/**
* constructor - Initializes a new PouchLink
*
* @param {object} [opts={}]
* @param {number} [opts.replicationInterval] Milliseconds between replications
* @param {string[]} opts.doctypes Doctypes to replicate
* @param {object[]} opts.doctypesReplicationOptions A mapping from doctypes to replication options. All pouch replication options can be used, as well as the "strategy" option that determines which way the replication is done (can be "sync", "fromRemote" or "toRemote")
* @returns {object} The PouchLink instance
*/
constructor(opts = {}) {
const options = defaults({}, opts, DEFAULT_OPTIONS)
super(options)
const { doctypes, doctypesReplicationOptions } = options
this.options = options
if (!doctypes) {
throw new Error(
"PouchLink must be instantiated with doctypes it manages. Ex: ['io.cozy.bills']"
)
}
this.doctypes = doctypes
this.doctypesReplicationOptions = doctypesReplicationOptions
this.indexes = {}
/** @type {Record<string, SyncStatus>} - Stores replication states per doctype */
this.replicationStatus = this.replicationStatus || {}
}
getReplicationURL(doctype) {
const url = this.client && this.client.stackClient.uri
const token = this.client && this.client.stackClient.token
if (!token) {
throw new Error(
"Can't get replication URL since the stack client doesn't have a token"
)
}
if (!url) {
throw new Error(
"Can't get replication URL since the stack client doesn't have a URI"
)
}
return getReplicationURL(url, token, doctype)
}
async registerClient(client) {
this.client = client
}
/**
* Migrate the current adapter
*
* @typedef {object} MigrationParams
* @property {string} [fromAdapter] - The current adapter type, e.g. 'idb'
* @property {string} [toAdapter] - The new adapter type, e.g. 'indexeddb'
* @property {string} [url] - The Cozy URL
* @property {Array<object>} [plugins] - The PouchDB plugins
*
* @param {MigrationParams} params - Migration params
*/
async migrateAdapter({ fromAdapter, toAdapter, url, plugins }) {
try {
for (const plugin of plugins) {
PouchDB.plugin(plugin)
}
const doctypes = getPersistedSyncedDoctypes()
for (const doctype of doctypes) {
const prefix = getPrefix(url)
const dbName = getDatabaseName(prefix, doctype)
await migratePouch({ dbName, fromAdapter, toAdapter })
destroyWarmedUpQueries() // force recomputing indexes
}
persistAdapterName('indexeddb')
} catch (err) {
console.error('PouchLink: PouchDB migration failed. ', err)
}
}
async onLogin() {
if (!this.client) {
logger.warn("PouchLink: no client registered, can't login")
return
}
const prefix = getPrefix(this.client.stackClient.uri)
const shouldDestroyDatabases =
this.pouches &&
this.pouches.options &&
this.pouches.options.prefix !== prefix
if (shouldDestroyDatabases) {
if (process.env.NODE_ENV !== 'production') {
logger.info('PouchLink: URI changed, destroy pouches')
}
try {
await this.pouches.destroy()
} catch (e) {
logger.warn('Error while destroying pouch DBs', e)
}
}
if (!prefix) {
throw new Error('PouchLink: Prefix is required')
}
if (process.env.NODE_ENV !== 'production') {
logger.log('Create pouches with ' + prefix + ' prefix')
}
if (!getAdapterName()) {
const adapter = get(this.options, 'pouch.options.adapter')
persistAdapterName(adapter)
}
this.pouches = new PouchManager(this.doctypes, {
pouch: this.options.pouch,
getReplicationURL: this.getReplicationURL.bind(this),
doctypesReplicationOptions: this.doctypesReplicationOptions,
onError: err => this.onSyncError(err),
onSync: this.handleOnSync.bind(this),
onDoctypeSyncStart: this.handleDoctypeSyncStart.bind(this),
onDoctypeSyncEnd: this.handleDoctypeSyncEnd.bind(this),
prefix,
executeQuery: this.executeQuery.bind(this)
})
if (this.client && this.options.initialSync) {
this.startReplication()
}
}
async reset() {
if (this.pouches) {
await this.pouches.destroy()
}
this.pouches = null
this.client = null
}
/**
* Receives PouchDB updates (documents grouped by doctype).
* Normalizes the data (.id -> ._id, .rev -> _rev).
* Passes the data to the client and to the onSync handler.
*
* Emits an event (pouchlink:sync:end) when the sync (all doctypes) is done
*/
handleOnSync(doctypeUpdates) {
const normalizedData = mapValues(doctypeUpdates, normalizeAll)
if (this.client) {
this.client.setData(normalizedData)
}
if (this.options.onSync) {
this.options.onSync.call(this, normalizedData)
}
if (process.env.NODE_ENV !== 'production') {
logger.info('Pouch synced')
}
this.client.emit('pouchlink:sync:end')
}
handleDoctypeSyncStart(doctype) {
this.replicationStatus[doctype] = 'replicating'
this.client.emit('pouchlink:doctypesync:start', doctype)
}
handleDoctypeSyncEnd(doctype) {
this.replicationStatus[doctype] = 'idle'
this.client.emit('pouchlink:doctypesync:end', doctype)
}
/**
* User of the link can call this to start ongoing replications.
* Typically, it can be used when the application regains focus.
*
* Emits pouchlink:sync:start event when the replication begins
*
* @public
* @returns {void}
*/
startReplication() {
this.client.emit('pouchlink:sync:start')
this.pouches.startReplicationLoop()
if (this.options.onStartReplication) {
this.options.onStartReplication.apply(this)
}
}
/**
* User of the link can call this to stop ongoing replications.
* Typically, it can be used when the applications loses focus.
*
* Emits pouchlink:sync:stop event
*
* @public
* @returns {void}
*/
stopReplication() {
this.pouches.stopReplicationLoop()
if (this.options.onStopReplication) {
this.options.onStopReplication.apply(this)
}
this.client.emit('pouchlink:sync:stop')
}
async onSyncError(error) {
if (isExpiredTokenError(error)) {
try {
await this.client.stackClient.refreshToken()
this.startReplication()
return
} catch (err) {
logger.warn('Could not refresh token, replication has stopped', err)
if (this.options.onSyncError) {
this.options.onSyncError.call(this, err)
}
}
} else {
logger.warn('CozyPouchLink: Synchronization error', error)
if (this.options.onSyncError) {
this.options.onSyncError.call(this, error)
}
}
}
getSyncInfo(doctype) {
return this.pouches.getSyncInfo(doctype)
}
getPouch(doctype) {
return this.pouches.getPouch(doctype)
}
supportsOperation(operation) {
const impactedDoctype = getDoctypeFromOperation(operation)
// If the Pouch is configured only to replicate from the remote,
// we don't want to apply the mutation on it, but to forward
// to the next link
if (
operation.mutationType &&
this.doctypesReplicationOptions &&
this.doctypesReplicationOptions[impactedDoctype] &&
this.doctypesReplicationOptions[impactedDoctype].strategy === 'fromRemote'
)
return false
return !!this.getPouch(impactedDoctype)
}
request(operation, result = null, forward = doNothing) {
const doctype = getDoctypeFromOperation(operation)
if (!this.pouches) {
if (process.env.NODE_ENV !== 'production') {
logger.info(
`Tried to access local ${doctype} but Cozy Pouch is not initialized yet. Forwarding the operation to next link`
)
}
return forward(operation)
}
if (!this.pouches.isSynced(doctype)) {
if (process.env.NODE_ENV !== 'production') {
logger.info(
`Tried to access local ${doctype} but Cozy Pouch is not synced yet. Forwarding the operation to next link`
)
}
return forward(operation)
}
if (this.needsToWaitWarmup(doctype)) {
if (process.env.NODE_ENV !== 'production') {
logger.info(
`Tried to access local ${doctype} but not warmuped yet. Forwarding the operation to next link`
)
}
return forward(operation)
}
// Forwards if doctype not supported
if (!this.supportsOperation(operation)) {
if (process.env.NODE_ENV !== 'production') {
logger.info(
`The doctype '${doctype}' is not supported. Forwarding the operation to next link`
)
}
return forward(operation)
}
if (operation.mutationType) {
return this.executeMutation(operation, result, forward)
} else {
return this.executeQuery(operation)
}
}
/**
*
* Check if there is warmup queries for this doctype
* and return if those queries are already warmedup or not
*
* @param {string} doctype
* @returns {boolean} the need to wait for the warmup
*/
needsToWaitWarmup(doctype) {
if (
this.doctypesReplicationOptions &&
this.doctypesReplicationOptions[doctype] &&
this.doctypesReplicationOptions[doctype].warmupQueries
) {
return !this.pouches.areQueriesWarmedUp(
doctype,
this.doctypesReplicationOptions[doctype].warmupQueries
)
}
return false
}
hasIndex(name) {
return Boolean(this.indexes[name])
}
// This merge is necessary because PouchDB does not support partial indexes
mergePartialIndexInSelector(selector, partialFilter) {
if (partialFilter) {
logger.info(
`PouchLink: The query contains a partial index but PouchDB does not support it. ` +
`Hence, the partial index definition is used in the selector for in-memory evaluation, ` +
`which might impact expected performances. If this support is important in your use-case, ` +
`please let us know or help us contribute to PouchDB!`
)
return { ...selector, ...partialFilter }
}
return selector
}
async ensureIndex(doctype, query) {
const fields = query.indexedFields || getIndexFields(query)
const name = getIndexNameFromFields(fields)
const absName = `${doctype}/${name}`
const db = this.pouches.getPouch(doctype)
if (this.indexes[absName]) {
return this.indexes[absName]
} else {
const index = await db.createIndex({
index: {
fields
}
})
this.indexes[absName] = index
return index
}
}
async executeQuery({
doctype,
selector,
sort,
fields,
limit,
id,
ids,
skip,
indexedFields,
partialFilter
}) {
const db = this.getPouch(doctype)
// The partial index is not supported by PouchDB, so we ensure the selector includes it
const mergedSelector = this.mergePartialIndexInSelector(
selector,
partialFilter
)
let res, withRows
if (id) {
res = await db.get(id)
withRows = false
} else if (ids) {
res = await allDocs(db, { include_docs: true, keys: ids })
res = withoutDesignDocuments(res)
res.total_rows = null // pouch indicates the total number of docs in res.total_rows, even though we use "keys". Setting it to null avoids cozy-client thinking there are more docs to fetch.
withRows = true
} else if (!mergedSelector && !fields && !sort) {
res = await allDocs(db, { include_docs: true, limit })
res = withoutDesignDocuments(res)
withRows = true
} else {
const findOpts = {
sort,
selector: mergedSelector,
fields,
limit,
skip
}
const index = await this.ensureIndex(doctype, {
...findOpts,
indexedFields
})
findOpts.use_index = index.id
res = await find(db, findOpts)
res.offset = skip
res.limit = limit
withRows = true
}
return jsonapi.fromPouchResult(res, withRows, doctype)
}
async executeMutation(mutation, result, forward) {
let pouchRes
switch (mutation.mutationType) {
case MutationTypes.CREATE_DOCUMENT:
pouchRes = await this.createDocument(mutation)
break
case MutationTypes.UPDATE_DOCUMENT:
pouchRes = await this.updateDocument(mutation)
break
case MutationTypes.UPDATE_DOCUMENTS:
pouchRes = await this.updateDocuments(mutation)
break
case MutationTypes.DELETE_DOCUMENT:
pouchRes = await this.deleteDocument(mutation)
break
case MutationTypes.ADD_REFERENCES_TO:
pouchRes = await this.addReferencesTo(mutation)
break
default:
return forward(mutation, result)
}
return jsonapi.fromPouchResult(
pouchRes,
false,
getDoctypeFromOperation(mutation)
)
}
async createDocument(mutation) {
const res = await this.dbMethod('post', mutation)
return parseMutationResult(mutation.document, res)
}
async updateDocument(mutation) {
const res = await this.dbMethod('put', mutation)
return parseMutationResult(mutation.document, res)
}
async updateDocuments(mutation) {
const docs = mutation.documents
const bulkResponse = await this.dbMethod('bulkDocs', mutation)
const updatedDocs = zipWith(
bulkResponse,
docs,
(bulkResult, originalDoc) => ({
...originalDoc,
_id: bulkResult.id,
_rev: bulkResult.rev
})
)
if (bulkResponse.find(x => !x.ok)) {
throw new BulkEditError(bulkResponse, updatedDocs)
}
return updatedDocs
}
async deleteDocument(mutation) {
const res = await this.dbMethod('remove', mutation)
const document = {
...mutation.document,
_id: res.id,
_rev: res.rev,
_deleted: true
}
return parseMutationResult(document, res)
}
async dbMethod(method, mutation) {
const doctype = getDoctypeFromOperation(mutation)
const { document: doc, documents: docs } = mutation
const db = this.getPouch(doctype)
let res
try {
if (docs) {
res = await db[method](docs.map(doc => sanitized(doc)))
} else if (doc) {
res = await db[method](sanitized(doc))
} else {
throw new Error(
'A mutation should either have document or documents member.'
)
}
return res
} catch (e) {
throw new Error(`Coud not apply mutation: ${e.message}`)
}
}
async syncImmediately() {
if (!this.pouches) {
logger.warn('Cannot sync immediately, no PouchManager')
return
}
this.pouches.syncImmediately()
}
}
export default PouchLink