-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
177 lines (146 loc) · 4.72 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
// couchdb-push
// (c) 2014 Johannes J. Schmidt
const crypto = require('crypto')
const async = require('async')
const omit = require('lodash/omit')
const isEqual = require('lodash/isEqual')
const nanoOption = require('nano-option')
const compile = require('couchdb-compile')
const ensure = require('couchdb-ensure')
const chokidar = require('chokidar')
module.exports = function push (db, source, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
try {
db = nanoOption(db)
} catch (e) {
return callback(new Error('Not a valid database: ' + db))
}
if (!db.config.db) {
return callback(new Error('Not a database: ' + db))
}
function pushDoc (doc, attachments, done) {
if (options.multipart && attachments.length) {
db.multipart.insert(doc, attachments, doc._id, done)
} else {
db.insert(doc, doc._id, done)
}
}
function diffAttachment (attachment, existingAttachment) {
if (!existingAttachment) {
return false
}
const md5sum = crypto.createHash('md5')
const data = options.multipart ? attachment.data : Buffer.from(attachment.data, 'base64')
md5sum.update(data)
const digest = 'md5-' + md5sum.digest('base64')
return existingAttachment.digest === digest
}
function diffDoc (doc, existingDoc, attachments, done) {
doc._rev = existingDoc._rev
if (options.multipart) {
if (attachments.length) {
for (let i = 0; i < attachments.length; i++) {
const name = attachments[i].name
const identical = diffAttachment(attachments[i], existingDoc && existingDoc._attachments && existingDoc._attachments[name])
if (identical) {
doc._attachments = doc._attachments || {}
doc._attachments[name] = existingDoc._attachments[name]
attachments.splice(i--, 1)
}
}
}
} else {
if (doc._attachments) {
Object.keys(doc._attachments).forEach(function (name) {
const identical = diffAttachment(doc._attachments[name], existingDoc && existingDoc._attachments && existingDoc._attachments[name])
if (identical) {
doc._attachments[name] = existingDoc._attachments[name]
}
})
}
}
// cannot diff multipart attachments
if (options.multipart && attachments.length > 0) {
return pushDoc(doc, attachments, done)
}
hasChanged(doc, existingDoc, function (error, changed) {
if (error) return done(error)
if (changed) return pushDoc(doc, attachments, done)
done(null, {
ok: true,
id: doc._id,
rev: doc._rev,
unchanged: true
})
})
}
function hasChanged (doc, existingDoc, callback) {
if (isUserDoc(doc) && doc.name && doc.password) {
confirmSession(doc.name, doc.password, function (error, result) {
if (error) {
if (error.statusCode === 401) return callback(null, true)
return callback(error)
}
const userDocToCompare = omit(doc, 'password')
const existingDocToCompare = omit(existingDoc, 'derived_key', 'iterations', 'password_scheme', 'salt')
callback(null, !isEqual(userDocToCompare, existingDocToCompare))
})
} else {
callback(null, !isEqual(doc, existingDoc))
}
}
// TChecking against `_users` is not acurate, because the users db can be configured:
// [couch_httpd_auth]
// authentication_db = _users
function isUserDoc (doc) {
return db.config.db === '_users'
}
function confirmSession (name, password, done) {
db.auth(name, password, done)
}
function getDoc (doc, attachments, done) {
db.get(doc._id, function (err, response) {
if (err && err.statusCode === 404) {
return pushDoc(doc, attachments, done)
}
diffDoc(doc, response, attachments, done)
})
}
function compileDoc (done) {
compile(source, options, function (err, doc, attachments) {
if (err) {
return done(err)
}
if (!doc._id) {
return done(new Error('Missing _id property'))
}
attachments = attachments || []
getDoc(doc, attachments, done)
})
}
ensure(db, function (error) {
if (error) {
return callback(error)
}
if (options.watch) {
const queue = async.queue(function (task, done) {
compileDoc(function (error, response) {
error
? console.error(error)
: console.log(JSON.stringify(response, null, ' '))
done(error)
})
}, 1)
chokidar
.watch(source, { ignoreInitial: true, awaitWriteFinish: true })
.on('all', function () {
queue.push(true)
})
}
compileDoc(callback)
})
}