-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathddoc.js
66 lines (53 loc) · 1.81 KB
/
ddoc.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
// Probe all details about a CouchDB design document
//
var lib = require('./lib')
, util = require('util')
, assert = require('assert')
, Emitter = require('./emitter').Emitter
;
function DesignDocument () {
var self = this;
Emitter.call(self);
self.db = null;
self.id = null;
self.url = null;
self.on('start', function probe_doc() {
self.log.debug("Fetching design document: " + self.url);
self.request({uri:self.url}, function(er, resp, body) {
if(er)
throw er;
else if(resp.statusCode !== 200 || typeof body !== 'object')
throw new Error("Bad ddoc response from " + self.url + ": " + JSON.stringify({code:resp.statusCode, body:body}));
self.x_emit('body', body);
})
})
self.on('start', function probe_info() {
var info_url = lib.join(self.url, '/_info');
self.log.debug("Fetching ddoc info: " + info_url);
self.request({uri:info_url}, function(er, resp, body) {
if(er)
throw er;
else if(resp.statusCode !== 200 || typeof body !== 'object')
throw new Error("Bad ddoc response from " + info_url + ": " + JSON.stringify({code:resp.statusCode, body:body}));
self.x_emit('info', body);
})
})
self.known('body', function(body) {
self.known('info', function(info) {
self.x_emit('end');
})
})
} // DesignDocument
util.inherits(DesignDocument, Emitter);
DesignDocument.prototype.start = function() {
var self = this;
if(!self.db)
throw new Error("Couch database URL required");
if(!self.id)
throw new Error("Document ID required");
// Since this is a design document, the slash must be kept.
self.url = lib.join(self.db, encodeURIComponent(self.id).replace(/^_design%2[fF]/, '_design/'));
self.x_emit('start');
}
module.exports = { "DesignDocument": DesignDocument
};