forked from mdn/kumascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
389 lines (336 loc) · 12.4 KB
/
api.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// ## KumaScript template API
//
// This module provides the API exposed to templates for utilities and wiki
// query functionality.
//
// A lot of this code started from implementing APIs that are vaguely
// compatible with [things provided by MindTouch in DekiScript][dekiref].
//
// This shouldn't end up being a full reimplementation of the DekiScript API,
// though. We just need a subset of the API actually used by legacy MDN
// templates, and we can diverge from there.
//
// [dekiref]: http://developer.mindtouch.com/en/docs/DekiScript/Reference
//
// TODO: Maybe split this module up into namespace-specific modules for easier
// editing?
/*jshint node: true, expr: false, boss: true */
// ### Prerequisites
var util = require('util'),
vm = require("vm"),
_ = require('underscore'),
async = require('async'),
// This also injects `Fiber` and `yield`
fibers = require('fibers'),
Future = require('fibers/future'),
wait = Future.wait,
request = require('request'),
Memcached = require('memcached'),
ks_macros = require(__dirname + '/macros'),
ks_utils = require(__dirname + '/utils');
// ### BaseAPI
//
// Base container for a namespaced sub-API
var BaseAPI = ks_utils.Class({
initialize: function (options) {
this.parent = this.options.parent;
// HACK: Create mixed case name aliases for all functions.
var fn_names = _.functions(this);
for (var i=0, name; name=fn_names[i]; i++) {
setCaseVariantAliases(this, name, this[name]);
}
},
// #### setVars(object)
// Copy the properties from the given object onto this API.
setVars: function (vars) {
var $this = this;
_.each(vars, function (v,n) {
setCaseVariantAliases($this, n, v);
});
}
});
// ### KumaAPI
//
// Grab bag of Kuma-specific API methods and utilities.
//
// This has to live here, rather than in an auto-required template. Because,
// it grants access to some node.js modules that are otherwise inaccessible to
// templates which are not allowed to use require() from node.js
var KumaAPI = ks_utils.Class(BaseAPI, {
// #### debug
// Expose util.debug from node.js
debug: util.debug,
// #### url
// Expose url from node.js to templates
url: require('url'),
// #### htmlEscape(string)
// Escape the given string for HTML inclusion.
htmlEscape: function (s) {
return (''+s).replace(/&/g,'&').
replace(/>/g,'>').
replace(/</g,'<').
replace(/"/g,'"');
}
});
// ### PageAPI
// <http://developer.mindtouch.com/en/docs/DekiScript/Reference/Wiki_Functions_and_Variables/Page>
var PageAPI = ks_utils.Class(BaseAPI, {
initialize: function (options) {
BaseAPI.prototype.initialize.call(this, options);
var $this = this;
// TODO: Need to thread through page details from Server to APIContext to here.
this.setVars({
uri: 'http://example.com/en/HTML/FakePage',
language: 'en'
});
}
});
// ### FakeMemcached
//
// A minimal stub replacement for Memcached, in case it's missing from the
// config. That way, kumascript can be used without memcache, even if that's
// not recommended.
var FakeMemcached = ks_utils.Class({
initialize: function (options) {
this._cache = {};
},
set: function (key, value, tm_out, next) {
this._cache[key] = value;
next(null, true);
},
get: function (key, next) {
next(null, this._cache[key]);
}
});
// ### APIContext
//
// Instances of this class manage instances of sub-APIs, supplying them with
// contextual info about the page in which macros are evaluated. Template
// scripts, in turn, use instances of this class to access sub-APIs.
var APIContext = ks_utils.Class({
default_options: {
server_options: {},
env: {},
source: '',
apis: {
kuma: KumaAPI
}
},
// #### initialize
//
// Initialize the API context.
initialize: function (options) {
_.each(this.options.apis, _.bind(this.installAPI, this));
// Make the env vars more easily used, if given
if (this.options && this.options.env) {
this.env = this.options.env;
}
// Create a memcache instance, if necessary
if (this.options.server_options.memcache) {
var mo = this.options.server_options.memcache;
this.memcached = new Memcached(mo.server, mo.options || {});
} else {
// If the configuration is missing, use the fake stub cache
this.memcached = new FakeMemcached();
}
// Create a new cache for required templates.
this._require_cache = {};
},
// #### BaseAPI
// Grant access to the BaseAPI class
BaseAPI: BaseAPI,
// #### Future
// Allow access to node-fiber Future from templates.
Future: Future,
// #### request
//
// Allow access to mikeal/request in templates and libraries, so they can
// easily make HTTP requests.
//
// TODO: Very permissive. Should there be more restrictions on net access?
request: request,
// Install a new instance of the given API class, with the given name.
installAPI: function (cls, name) {
setCaseVariantAliases(this, name, new cls({parent: this}));
},
// #### buildAPI(prototype)
//
// Utility method for building a new BaseAPI-based API, useful for building
// APIs in templates. Handy in conjuction with autorequire. See tests for
// details.
buildAPI: function (proto) {
var cls = ks_utils.Class(BaseAPI, proto);
return new cls({ parent: this });
},
// #### performAutoRequire
//
// Auto-require some templates and install the exports as APIs. This is
// kind of a hack, but I wanted to use the require method.
performAutoRequire: function (next) {
var $this = this,
server_options = $this.options.server_options;
// Skip this whole thing, if there are no autorequires
if (!server_options.autorequire) { return next(null); }
// Run this in a parallel forEach, to block less on network.
async.forEach(
_.keys(server_options.autorequire),
function (install_name, fe_next) {
// require() expects to run inside a Fiber
Fiber(function () {
var tmpl_name = server_options.autorequire[install_name],
exports = $this.require(tmpl_name);
setCaseVariantAliases($this, install_name, exports);
fe_next();
}).run();
},
next
);
},
// #### setArguments
// Given a list of arguments, make them available to a template as $0..$n
// variables.
setArguments: function (args) {
var $this = this;
// Both arguments and $$ are aliases for the list of macro args.
$this['arguments'] = $this.$$ = args || [];
// HACK: Clear out, yet ensure $0..$99 exist
for (var i=0; i<99; i++) {
$this['$'+i] = '';
}
// Assign each element of args to $0..$n
_.each(args, function (v, i) {
$this['$'+i] = v;
});
return this;
},
// #### cacheFn
// Cache the results of a function
cacheFn: function (key, tm_out, to_cache) {
var result = null,
f = new Future(),
mc = this.memcached;
mc.get(key, function (err, c_result) {
if (c_result) {
result = c_result; f['return']();
} else {
to_cache(function (val) {
mc.set(key, val, tm_out, function (err, c_result) {
result = val; f['return']();
})
})
}
});
f.wait();
return result;
},
// #### template(name, arguments)
//
// Attempt to load and execute a template with the given name and
// arguments. The output, if any, is returned. Errors, if any, are pushed
// up to the macro processor
template: function (name, args) {
// TODO: Implement caching here by building a key out of hashed args?
// Probably not a big win, since the result of the template calling
// this template will itself be cached during macro evaluation.
// Try loading the template, using a node-fibers Future to avoid
// imposing async on templates.
var $this = this,
future = new Future(),
loader = $this.loader,
errors = $this.errors,
output = '';
try {
// Try loading the template...
loader.get(name, function (err, tmpl) {
if (!err) {
// Try executing the template...
clone_ctx = _.clone($this).setArguments(args);
tmpl.execute(args, clone_ctx, function (err, result) {
if (err) {
// There was an error executing the template. :(
var tok = {type: 'none', name: name};
errors.push(new ks_macros.TemplateExecutionError(
{token: tok, error: err}));
}
output = result;
future['return']();
});
} else {
// There was an error loading the template. :(
errors.push(new ks_macros.TemplateLoadingError(
{name: name, error: err}));
future['return']();
}
});
} catch (e) {
// There was an error executing the template. :(
errors.push(new ks_macros.TemplateLoadingError(
{name: name, error: e}));
future['return']();
}
// Wait here for the async magic to complete.
future.wait();
return output;
},
// #### require(path)
//
// Attempts to load and execute a template which, as a side effect, can
// populate an exports object in quasi-CommonJS style. The template output
// is ignored.
require: function (name) {
// Use an internal cache, so that repeated require() calls reuse the
// previously loaded results.
if (!(name in this._require_cache)) {
clone_ctx = _.clone(this);
// Let's pretend we're following CommonJS module conventions
clone_ctx.module = { exports: {} };
clone_ctx.exports = clone_ctx.module.exports;
// clone_ctx is just like calling a template, only we ignore the output
// and return the side effect of populating exports.
var output = clone_ctx.template(name, []);
this._require_cache[name] = clone_ctx.module.exports;
}
return this._require_cache[name];
}
});
// ### setCaseVariantAliases
//
// THIS IS A BIG FAT HAIRY HACK. And, it has a long name, so no one forgets it.
//
// Set a property on an object with aliases of various mixed cases. For example:
//
// page.location, Page.location, Page.Location, page.Location
//
// There's no such thing as case-insensitive object keys in JS, but
// apparently there are in DekiScript. This hack just covers the most
// common slack in known MDN templates.
//
// Harmony proxies might be a solution, in that an attribute access proxy could
// intercept and squash case. But, it requires a compiled C++ component and
// seems like overkill.
//
// <https://github.com/samshull/node-proxy>
//
// Running with the V8 option --harmony_proxies seems like a lead, especially
// when combined with this (thanks, David Bruant):
//
// <https://github.com/Benvie/Direct-Proxies-Shim-Shim-for-Node-and-Chrome>
//
// But, `--harmony_proxies` is off by default, which makes me hesitant to rely
// on it for a production service.
//
function setCaseVariantAliases($this, name, val) {
// As-is from the source.
$this[name] = val;
// lowercase
$this[name.toLowerCase()] = val;
// Capitalized
var uc_name = name.charAt(0).toUpperCase() + name.slice(1);
$this[uc_name] = val;
}
// ### Exported public API
module.exports = {
APIContext: APIContext,
BaseAPI: BaseAPI,
KumaAPI: KumaAPI
};