-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
303 lines (265 loc) · 8.49 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
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
/*
Copyright 2013-2014, Marten de Vries
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
"use strict";
var coucheval = require("couchdb-eval");
var couchdb_objects = require("couchdb-objects");
var nodify = require("promise-nodify");
var uuid = require("node-uuid");
var Promise = require("pouchdb-promise");
var PouchPluginError = require("pouchdb-plugin-error");
var dbs = [];
var methodNames = ["put", "post", "remove", "bulkDocs", "putAttachment", "removeAttachment"];
var methodsByDbsIdx = [];
function methods(db) {
var index = dbs.indexOf(db);
if (index === -1) {
return methodsFromDb(db);
}
return methodsByDbsIdx[index];
}
function methodsFromDb(db) {
var meths = {};
methodNames.forEach(function (name) {
meths[name] = db[name].bind(db);
});
return meths;
}
function oldDoc(db, id) {
return db.get(id, {revs: true}).catch(function () {
return null;
});
}
function validate(validationFuncs, newDoc, oldDoc, options) {
newDoc._revisions = (oldDoc || {})._revisions;
try {
validationFuncs.forEach(function (validationFuncInfo) {
var func = validationFuncInfo.func;
var designDoc = validationFuncInfo.designDoc;
func.call(designDoc, newDoc, oldDoc, options.userCtx, options.secObj);
});
} catch (e) {
if (typeof e.unauthorized !== "undefined") {
throw {
name: "unauthorized",
message: e.unauthorized,
status: 401
};
} else if (typeof e.forbidden !== "undefined") {
throw {
name: "forbidden",
message: e.forbidden,
status: 403
};
} else {
throw coucheval.wrapExecutionError(e);
}
}
//passed all validation functions (no errors thrown) -> success
}
function doValidation(db, newDoc, options, callback) {
var isHttp = ["http", "https"].indexOf(db.type()) !== -1;
if (isHttp && !(options || {}).checkHttp) {
//CouchDB does the checking for itself. Validate succesful.
return Promise.resolve();
}
if ((newDoc._id || "").indexOf("_design/") === 0) {
//a design document -> always validates succesful.
return Promise.resolve();
}
return getValidationFunctions(db).then(function (validationFuncs) {
if (!validationFuncs.length) {
//no validation functions, so valid!
return;
}
var completeOptionsPromise = completeValidationOptions(db, options);
var oldDocPromise = oldDoc(db, newDoc._id);
return Promise.all([completeOptionsPromise, oldDocPromise]).then(function (args) {
var completeOptions = args[0];
var oldDoc = args[1];
return validate(validationFuncs, newDoc, oldDoc, completeOptions);
});
});
}
function completeValidationOptions(db, options) {
if (!options) {
options = {};
}
if (!options.secObj) {
options.secObj = {};
}
var userCtxPromise;
if (options.userCtx) {
userCtxPromise = Promise.resolve(options.userCtx);
} else {
var buildUserContext = couchdb_objects.buildUserContextObject;
userCtxPromise = db.info().then(buildUserContext);
}
return userCtxPromise.then(function (userCtx) {
options.userCtx = userCtx;
return options;
});
}
function getValidationFunctions(db, callback) {
return db.allDocs({
startkey: "_design/",
endkey: "_design0",
include_docs: true
}).then(parseValidationFunctions);
}
function parseValidationFunctions(resp) {
var validationFuncs = resp.rows.map(function (row) {
return {
designDoc: row.doc,
code: row.doc.validate_doc_update
};
});
validationFuncs = validationFuncs.filter(function (info) {
return typeof info.code !== "undefined";
});
validationFuncs.forEach(function (info) {
//convert str -> function
info.func = coucheval.evaluate(info.designDoc, {}, info.code);
});
return validationFuncs;
}
function processArgs(db, callback, options) {
if (typeof options === "function") {
callback = options;
options = {};
}
return {
db: db,
callback: callback,
options: options
};
}
exports.validatingPut = function (doc, options, callback) {
var args = processArgs(this, callback, options);
var promise = doValidation(args.db, doc, args.options).then(function () {
return methods(args.db).put(doc, args.options);
});
nodify(promise, callback);
return promise;
};
exports.validatingPost = function (doc, options, callback) {
var args = processArgs(this, callback, options);
doc._id = doc._id || uuid.v4();
var promise = doValidation(args.db, doc, args.options).then(function () {
return methods(args.db).post(doc, args.options);
});
nodify(promise, callback);
return promise;
};
exports.validatingRemove = function (doc, options, callback) {
var args = processArgs(this, callback, options);
doc._deleted = true;
var promise = doValidation(args.db, doc, args.options).then(function () {
return methods(args.db).remove(doc, args.options);
});
nodify(promise, callback);
return promise;
};
exports.validatingBulkDocs = function (bulkDocs, options, callback) {
//the ``all_or_nothing`` attribute on ``bulkDocs`` is unsupported.
//Also, the result array might not be in the same order as
//``bulkDocs.docs``
var args = processArgs(this, callback, options);
var done = [];
var notYetDone = [];
var validations = bulkDocs.docs.map(function (doc) {
doc._id = doc._id || uuid.v4();
var validationPromise = doValidation(args.db, doc, args.options);
return validationPromise.then(function (resp) {
notYetDone.push(doc);
}).catch(function (err) {
err.id = doc._id;
done.push(err);
});
});
var allValidationsPromise = Promise.all(validations).then(function () {
return methods(args.db).bulkDocs({docs: notYetDone}, args.options);
}).then(function (insertedDocs) {
return done.concat(insertedDocs);
});
nodify(allValidationsPromise, callback);
return allValidationsPromise;
};
var vpa = function (docId, attachmentId, rev, attachment, type, options, callback) {
var args = processArgs(this, callback, options);
//get the doc
var promise = args.db.get(docId, {rev: rev, revs: true}).catch(function (err) {
return {_id: docId};
}).then(function (doc) {
//validate the doc + attachment
doc._attachments = doc._attachments || {};
doc._attachments[attachmentId] = {
content_type: type,
data: attachment,
};
return doValidation(args.db, doc, args.options);
}).then(function () {
//save the attachment
return methods(args.db).putAttachment(docId, attachmentId, rev, attachment, type);
});
nodify(promise, callback);
return promise;
};
exports.validatingPutAttachment = vpa;
var vra = function (docId, attachmentId, rev, options, callback) {
var args = processArgs(this, callback, options);
//get the doc
var promise = args.db.get(docId, {rev: rev, revs: true}).then(function (doc) {
//validate the doc without attachment
delete doc._attachments[attachmentId];
return doValidation(args.db, doc, args.options);
}).then(function () {
//remove the attachment
return methods(args.db).removeAttachment(docId, attachmentId, rev);
});
nodify(promise, callback);
return promise;
};
exports.validatingRemoveAttachment = vra;
exports.installValidationMethods = function () {
var db = this;
if (dbs.indexOf(db) !== -1) {
throw new PouchPluginError({
status: 500,
name: "already_installed",
message: "Validation methods are already installed on this database."
});
}
dbs.push(db);
methodsByDbsIdx.push(methodsFromDb(db));
methodNames.forEach(function (name) {
db[name] = exports["validating" + name[0].toUpperCase() + name.substr(1)].bind(db);
});
};
exports.uninstallValidationMethods = function () {
var db = this;
var index = dbs.indexOf(db);
if (index === -1) {
throw new PouchPluginError({
status: 500,
name: "already_not_installed",
message: "Validation methods are already not installed on this database."
});
}
var meths = methods(db);
methodNames.forEach(function (name) {
db[name] = meths[name];
});
//cleanup
dbs.splice(index, 1);
methodsByDbsIdx.splice(index, 1);
};