-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffable.js
More file actions
101 lines (89 loc) · 3.33 KB
/
diffable.js
File metadata and controls
101 lines (89 loc) · 3.33 KB
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
/*global module, require */
var fs = require('fs'),
crypto = require('crypto'),
requestHandler = require('./requestHandler'),
FileResourceManager = require('./fileResourceManager');
module.exports = (function () {
var that = null,
frm = new FileResourceManager(),
/**
* Instantiates diffable object
* @class
* @constructs
* @param {Object} config configuration object
* <code>
* <pre>
* {
* "diffableDir" : '/path/to/directory'
* "resourceDir" : '/path/to/directory'
* }
* </pre>
* </code>
* <code>diffableDir</code> - directory where delta and version files will
* be stored<br />
* <code>resourceDir</code> - directory where diffable can find static files.
*/
diffable = function (config) {
this.dir = fs.realpathSync(config.diffableDir);
this.resourceDir = config.resourceDir;
this.provider = requestHandler({
'resourceDir': config.resourceDir,
'frm': frm,
'diffableRoot': this.dir,
'logger' : config.logger
});
that = this;
};
/**
* Method adds resources that should be served with diffable.
* @private
* @param {String} path Path to resource that should be served by diffable.
* Path is relative to <code>resourceDir</code>.
*/
diffable.prototype.watch = function (path) {
//resolving absolute path to resource being tracked
fs.realpath(this.resourceDir + path, function (err, resolvedPath) {
if (err) {
throw err;
}
var hash = crypto.createHash('md5').update(resolvedPath),
resourceDir = that.dir + '/' + hash.digest('hex');
//Create resource directory (if doesn't exist) name of directory is
// md5 hash of resource's absolute path
fs.mkdir(resourceDir, 0755, function (err) {
//create first version of resource
frm.putResource(resolvedPath, resourceDir);
//add callback to track file changes
fs.watchFile(resolvedPath, function (curr, prev) {
//if changes were made add new version of resource
frm.putResource(resolvedPath, resourceDir);
});
});
});
};
/**
* Method adds files to diffable control, and returns Connect middleware.
* Returns connect stack interface, if request contains data that is
* relevant to diffable this middleware will serve appropriate version
* and/or delta files.
* @public
* @param {String} filename... varargs names of the files to look for. Paths
* are relative to <code>resourceDir</code> in configuration object
* @returns {Function}
*/
diffable.prototype.serve = function () {
var i = 0, len = arguments.length
if (len >= 1) {
for (; i < len; i += 1) {
that.watch(arguments[i]);
}
return that.provider;
} else {
console.log('Diffable: There are no files under control')
return function (req, res, next) {
next();
}
}
}
return diffable;
}());