-
Notifications
You must be signed in to change notification settings - Fork 1
/
dustjs-linkedin.js
151 lines (136 loc) · 4.1 KB
/
dustjs-linkedin.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
function DustHandler(options) {
var logger = options.logger, eb = options.eb, version = options.version, dustPath = options.dustPath, helperVersion = options.helperVersion, dustHelpersPath = options.dustHelpersPath, debug = options.debug;
if (!logger || !eb)
throw Error("You must pass a logger and eb (eventBus)");
if (!dustPath)
dustPath = "node_modules/dustjs-linkedin/";
if (!dustHelpersPath)
dustHelpersPath = "node_modules/dustjs-helpers/";
if (!helperVersion)
helperVersion = "1.1.1";
this.getVersion = function() {
return version ? version : "using dust lib files";
};
if (version)
dust = loadFromClientDistribution(version);
else
dust = loadNodeFiles();
function logError(method, msg, e) {
logger.error("error: " + method + (msg?" failed processing " + JSON.stringify(msg):""));
if (e && e.stack)
logger.error("exception and stack: " + e + "\n" + e.stack);
}
function logDebug(msg) {
if (debug)
logger.debug(msg);
}
function makeContext(context) {
var contextType = Object.prototype.toString.call(context);
if (contextType === "[object Array]") {
var len = context.length, array = context;
logDebug("context is array " + len + ". making context ");
if (len == 0)
context = {};
else
context = dust.makeBase(array[0]);
for ( var i = 0; i < len; i++) {
context = context.push(array[i]);
logDebug("context push " + JSON.stringify(array[i]));
}
} else {
logDebug("context was " + contextType);
}
return context;
}
function loadNodeFiles() {
logDebug("loading from node source files in " + dustPath + "lib");
var dust = require(dustPath + "lib/dust");
var compiler = require(dustPath + "lib/compiler");
compiler = compiler(dust);
dust.compile = compiler.compile;
var parser = require(dustPath + "lib/parser");
compiler.parse = parser.parse;
require(dustHelpersPath + "lib/dust-helpers");
return dust;
}
function loadFromClientDistribution(version) {
var file = require("vertx/file_system");
var Buffer = require("vertx/buffer");
logDebug("loading client distribution file in " + dustPath + "lib"
+ version);
var patchFile = dustPath + "dist/dust-full-" + version + ".patched.js";
var patch = file.readFileSync("dustjs-linkedin/patch.js");
var src = file.readFileSync(dustPath + "dist/dust-full-" + version
+ ".js");
var srcNew = new Buffer(src.toString().replace("function getGlobal",
patch + "function getGlobalOld"));
if (file.existsSync(patchFile)
&& srcNew.equals(file.readFileSync(patchFile))) {
logDebug("it appears this version already has patched file.");
} else {
file.writeFileSync(patchFile, srcNew);
}
load(patchFile);
load(dustHelpersPath + "dist/dust-helpers-" + helperVersion + ".js");
return dust;
}
function compileHandler(m, reply) {
try {
logDebug("compileHandler: " + JSON.stringify(m));
var compiled = dust.compile(m.source, m.name);
dust.loadSource(compiled);
logDebug("compileHandler: finished compiling and loading " + m.name
+ " => " + compiled);
reply({
success : true,
name : m.name
});
} catch (e) {
logError("compileHandler", m, e);
reply({
error : "failed to compile " + m.name + ". " + e
});
}
}
function loadHandler(m, reply) {
try {
dust.loadSource(m.compiled, m.name);
reply(m);
} catch (e) {
logError("loadHandler", m, e);
reply({
error : e.stack
});
}
}
;
function renderHandler(m, reply) {
try {
var context = makeContext(m.context);
logDebug("rendering " + m.name);
dust.render(m.name, context, function(err, out) {
if (err) {
logError("render error " + err);
logError("caused by : " + JSON.stringify(m));
reply({
error : "" + err
});
} else {
reply({
output : out
});
}
});
} catch (e) {
logDebug("error rendering " + m.name);
logError("render", m, e);
reply({
error : "exception while rendering " + m.name + ". " + e
});
}
}
eb.registerHandler("dust.compile", compileHandler);
eb.registerHandler("dust.load", loadHandler);
eb.registerHandler("dust.render", renderHandler);
};
module.exports = DustHandler;