This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtemplar.js
200 lines (166 loc) · 5.47 KB
/
templar.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
module.exports = Templar
var path = require('path')
, fs = require('fs')
, LRU = require('lru-cache')
, compileCache = new LRU({max:50})
, outputCache = new LRU({max:500})
, crypto = require('crypto')
, templateCache = {}
, loaded = {}
, sigmund = require('sigmund')
// should really load the template folder before using it.
Templar.loadFolder = loadFolder
function Templar (req, res, opts) {
if (!opts) throw new Error('Please provide options to Templar')
var folder = opts.folder
, engine = opts.engine
, nocache = opts.cache === false
, stamp = opts.stamp
if (!engine || !folder) {
throw new Error('Templar needs engine and folder options')
}
folder = path.resolve(folder)
// In order to support include() methods, we need to load up
// all of the templates in the folder. This has the other
// somewhat nice effect of meaning that we don't have to do
// any fs.readFile calls later on.
//
// Of course, this sucks a lot if you have a lot of different
// folders where templates go, but that's pretty rare.
if (!loaded[folder]) loadFolder(folder)
template.available = available
template.has = has
template.locals = {}
template.locals.locals = template.locals
return template
function has (f) {
f = path.resolve(folder, f)
return !!templateCache[f]
}
function available () {
return Object.keys(templateCache).filter(function (f) {
return f.indexOf(folder) === 0
}).map(function (f) {
return path.relative(folder, f)
})
}
function template () {
var f, data, code, endres
endres = true;
for (var i = 0; i < arguments.length; i ++) {
switch (typeof arguments[i]) {
case 'number': code = arguments[i]; break
case 'string': f = arguments[i]; break
case 'boolean' : endres = arguments[i]; break
case 'object': data = arguments[i]; break
default: throw new Error('bad argument to template')
}
}
if (!f) throw new Error('no template provided')
f = path.resolve(folder, f)
if (!code) code = 200
if (data && (typeof data === 'object')) {
Object.keys(data).forEach(function (k) {
template.locals[k] = data[k]
})
template.locals.locals = template.locals
}
var tpl = templateCache[f]
if (!tpl) throw new Error('invalid template: '+f)
// the data is part of the ETag
// serving the same template with the same data = same result
var ins = sigmund(template.locals)
, tag = getETag(tpl.key + ":" + ins)
if (!nocache && req.headers['if-none-match'] === tag) {
res.statusCode = 304
return res.end()
}
res.setHeader('etag', tag)
if (stamp) res.setHeader('x-templar-stamp', stamp)
// stamp is applied *after* getting the etag, otherwise
// etags will be worker-specific which makes then much less
// likely to hit the browser cache.
template.locals.stamp = stamp
var out = output(f, template.locals, tag)
// ok, actually send a result.
res.statusCode = code || 200
var curCT = res.getHeader('content-type')
if (!curCT) res.setHeader('content-type', 'text/html')
if(endres)
res.end(out)
else
res.write(out);
}
function output (f, data, tag) {
// only generate if we have to
var out = outputCache.get(tag)
if (!nocache && out) return out
// we're not actually going to provide THAT data object
// to the template, however. Instead, we're going to make a copy,
// so that we can provide an 'include' function, which works just
// like require(), in that each template includes relative to
// itself
var tplData = {}
Object.keys(data).forEach(function (k) {
tplData[k] = data[k]
})
tplData.include = include(f, tag)
// include a link to 'locals' to see what we've been provided.
tplData.locals = tplData
out = compile(f)(tplData)
outputCache.set(tag, out)
return out
}
// a partial's effective tag is the parent tag + f + data
function include (from, tag) { return function (f, data) {
var ins = sigmund(data)
, t = tag + f + ins
return output(path.resolve(path.dirname(from), f), data || {}, t)
}}
function compile (f) {
var tpl = templateCache[f]
// only compile if we have to.
var compiled = compileCache.get(f)
if (!compiled || nocache) {
if (nocache) {
tpl = loadFile_(f, fs.statSync(f))
}
compiled = engine.compile(tpl.contents, { filename: f })
compileCache.set(f, compiled)
}
if (!compiled) throw new Error('failed to compile template: '+f)
return compiled
}
}
function getETag (str) {
var h = crypto.createHash("sha1")
h.update(str)
return '"' + h.digest('base64') + '"'
}
function loadFolder (folder) {
loadFolder_(folder, templateCache, 0, 10)
loaded[folder] = true
}
function loadFolder_ (folder, c, depth, maxDepth) {
if (depth > maxDepth) return
// synchronously read all the files in the folder, and save
// their data and signatures for later.
var queue = []
fs.readdirSync(folder).forEach(function (file) {
file = path.resolve(folder, file)
var st = fs.statSync(file)
if (!st) return
if (st.isDirectory()) return queue.push(file)
st = loadFile_(file, st)
c[file] = st
})
queue.forEach(function (folder) {
loadFolder_(folder, c, depth + 1, maxDepth)
})
}
function loadFile_ (file, st) {
st.contents = fs.readFileSync(file, 'utf8')
st.key = process.platform === 'win32' ? path.resolve(file)
: (st.dev + ':' + st.ino)
return st
}