forked from mr-er-vivek/node.couchapp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
324 lines (297 loc) · 10.4 KB
/
main.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
var path = require('path')
, fs = require('fs')
, watch = require('watch')
, request = require('request')
, crypto = require('crypto')
, mimetypes = require('./mimetypes')
, spawn = require('child_process').spawn
;
var h = {'content-type':'application/json', 'accept-type':'application/json'}
/**
* Recursively load directory contents into ddoc
*
* It's really convenient to see the main couchapp code in single file,
* rather than mapped into little files in lots of directories like
* the python couchapp. But there are definitely cases where we might want
* to use some module or another on the server side. This addition
* loads file contents from a given directory (recursively) into a js
* object that can be added to a design document and require()'d in
* lists, shows, etc.
*
* Use couchapp.loadFiles() in app.js like this:
*
* ddoc = {
* _id: '_design/app'
* , views: {}
* , ...
* , lib: couchapp.loadFiles('./lib')
* , vendor: couchapp.loadFiles('./vendor')
* }
*
* Optionally, pass in operators to process file contents. For example,
* generate mustache templates from jade templates.
*
* In yourapp/templates/index.jade
*
* !!!5
* html
* head
* //- jade locals.title
* title!= title
* body
* .item
* //- mustache variable for server-side rendering
* h1 {{ heading }}
*
* in yourapp/app.js
* var couchapp = require('couchapp')
* , jade = require('jade')
* , options = {
* , operators: [
* function renderJade (content, options) {
* var compiler = jade.compile(content);
* return compiler(options.locals || {});
* }
* ]
* , locals: { title: 'Now we\'re cookin with gas!' }
* };
*
* ddoc = { ... };
*
* ddoc.templates = loadFiles(dir, options);
*/
function loadFiles(dir, options) {
var listings = fs.readdirSync(dir)
, options = options || {}
, obj = {};
listings.forEach(function (listing) {
var file = path.join(dir, listing)
, prop = listing.split('.')[0] // probably want regexp or something more robust
, stat = fs.statSync(file);
if (stat.isFile()) {
var content = fs.readFileSync(file).toString();
if (options.operators) {
options.operators.forEach(function (op) {
content = op(content, options);
});
}
obj[prop] = content;
} else if (stat.isDirectory()) {
obj[listing] = loadFiles(file, options);
}
});
return obj;
}
/**
* End of patch (also see exports and end of file)
*/
function loadAttachments (doc, root, prefix) {
doc.__attachments = doc.__attachments || []
try {
fs.statSync(root)
} catch(e) {
throw e
throw new Error("Cannot stat file "+root)
}
doc.__attachments.push({root:root, prefix:prefix});
}
function copy (obj) {
var n = {}
for (i in obj) n[i] = obj[i];
return n
}
function createApp (doc, url, cb) {
var app = {doc:doc}
app.fds = {};
app.prepare = function () {
var p = function (x) {
for (i in x) {
if (i[0] != '_') {
if (typeof x[i] == 'function') {
x[i] = x[i].toString()
x[i] = 'function '+x[i].slice(x[i].indexOf('('))
}
if (typeof x[i] == 'object') {
p(x[i])
}
}
}
}
p(app.doc);
app.doc.__attachments = app.doc.__attachments || []
app.doc.attachments_md5 = app.doc.attachments_md5 || {}
app.doc._attachments = app.doc._attachments || {}
}
var push = function (callback) {
console.log('Serializing.')
var doc = copy(app.doc);
doc._attachments = copy(app.doc._attachments)
delete doc.__attachments;
var body = JSON.stringify(doc)
console.log('PUT '+url.replace(/^(https?:\/\/[^@:]+):[^@]+@/, '$1:******@'))
request({uri:url, method:'PUT', body:body, headers:h}, function (err, resp, body) {
if (err) throw err;
if (resp.statusCode !== 201) {
throw new Error("Could not push document\nCode: " + resp.statusCode + "\n"+body);
}
app.doc._rev = JSON.parse(body).rev
console.log('Finished push. '+app.doc._rev)
request({uri:url, headers:h}, function (err, resp, body) {
body = JSON.parse(body);
app.doc._attachments = body._attachments;
if (callback) callback()
})
})
}
app.push = function (callback) {
var revpos
, pending_dirs = 0
;
console.log('Preparing.')
var doc = app.current;
for (i in app.doc) {
if (i !== '_rev') doc[i] = app.doc[i]
}
app.doc = doc;
app.prepare();
revpos = app.doc._rev ? parseInt(app.doc._rev.slice(0,app.doc._rev.indexOf('-'))) : 0;
var coffeeCompile;
var coffeeExt;
try{
coffeeCompile = require('coffee-script');
coffeeExt = /\.(lit)?coffee$/;
} catch(e){}
app.doc.__attachments.forEach(function (att) {
watch.walk(att.root, {ignoreDotFiles:true}, function (err, files) {
pending_dirs += 1;
var pending_files = Object.keys(files).length;
for (i in files) { (function (f) {
fs.readFile(f, function (err, data) {
if(f.match(coffeeExt)){
data = new Buffer( coffeeCompile.compile(data.toString()) );
f = f.replace(coffeeExt,'.js');
}
f = f.replace(att.root, att.prefix || '').replace(/\\/g,"/");
if (f[0] == '/') f = f.slice(1)
if (!err) {
var d = data.toString('base64')
, md5 = crypto.createHash('md5')
, mime = mimetypes.lookup(path.extname(f).slice(1))
;
md5.update(d)
md5 = md5.digest('hex')
if (app.doc.attachments_md5[f] && app.doc._attachments[f]) {
if (app.doc._attachments[f].revpos === app.doc.attachments_md5[f].revpos &&
app.doc.attachments_md5[f].md5 === md5) {
pending_files -= 1;
if(pending_files === 0){
pending_dirs -= 1;
if(pending_dirs === 0){
push(callback);
}
}
return; // Does not need to be updated.
}
}
app.doc._attachments[f] = {data:d, content_type:mime};
app.doc.attachments_md5[f] = {revpos:revpos + 1, md5:md5};
}
pending_files -= 1
if(pending_files === 0){
pending_dirs -= 1;
if(pending_dirs === 0){
push(callback);
}
}
})
})(i)}
})
})
if (!app.doc.__attachments || app.doc.__attachments.length == 0) push(callback);
}
app.sync = function (callback) {
// A few notes.
// File change events are stored in an array and bundled up in to one write call.,
// this reduces the amount of unnecessary processing as we get a lof of change events.
// The file descriptors are stored and re-used because it cuts down on the number of bad change events.
// And finally, we check the md5 and only push when the document is actually been changed.
// A lot of crazy workarounds for the fact that we basically get an event every time someone
// looks funny at the underlying files and even reading and opening fds to check on the file trigger
// more events.
app.push(function () {
var changes = [];
console.log('Watching files for changes...')
app.doc.__attachments.forEach(function (att) {
var pre = att.root
var slash = (process.platform === 'win32') ? '\\' : '/';
if (pre[pre.length - 1] !== slash) pre += slash;
watch.createMonitor(att.root, {ignoreDotFiles:true}, function (monitor) {
monitor.on("removed", function (f, stat) {
f = f.replace(pre, '');
changes.push([null, f]);
})
monitor.on("created", function (f, stat) {
changes.push([f, f.replace(pre, ''), stat]);
})
monitor.on("changed", function (f, curr, prev) {
changes.push([f, f.replace(pre, ''), curr]);
})
})
})
var check = function () {
var pending = 0
, revpos = parseInt(app.doc._rev.slice(0,app.doc._rev.indexOf('-')))
, dirty = false
;
if (changes.length > 0) {
changes.forEach(function (change) {
if (!change[0]) {
delete app.doc._attachments[change[1]];
dirty = true;
console.log("Removed "+change[1]);
} else {
pending += 1
fs.readFile(change[0], function (err, data) {
var f = change[1]
, d = data.toString('base64')
, md5 = crypto.createHash('md5')
, mime = mimetypes.lookup(path.extname(f).slice(1))
;
md5.update(d)
md5 = md5.digest('hex')
pending -= 1
if (!app.doc.attachments_md5[f] || (md5 !== app.doc.attachments_md5[f].md5) ) {
app.doc._attachments[f] = {data:d, content_type:mime};
app.doc.attachments_md5[f] = {revpos:revpos + 1, md5:md5};
dirty = true;
console.log("Changed "+change[0]);
}
if (pending == 0 && dirty) push(function () {dirty = false; setTimeout(check, 50)})
else if (pending == 0 && !dirty) setTimeout(check, 50)
})
}
})
changes = []
if (pending == 0 && dirty) push(function () {dirty = false; setTimeout(check, 50)})
else if (pending == 0 && !dirty) setTimeout(check, 50)
} else {
setTimeout(check, 50);
}
}
setTimeout(check, 50)
})
}
var _id = doc.app ? doc.app._id : doc._id
if (url.slice(url.length - _id.length) !== _id) url += '/' + _id;
request({uri:url, headers:h}, function (err, resp, body) {
if (err) throw err;
if (resp.statusCode == 404) app.current = {};
else if (resp.statusCode !== 200) throw new Error("Failed to get doc\n"+body)
else app.current = JSON.parse(body)
cb(app)
})
}
exports.createApp = createApp
exports.loadAttachments = loadAttachments
exports.bin = require('./bin')
exports.loadFiles = loadFiles