-
Notifications
You must be signed in to change notification settings - Fork 0
/
rssi.js
484 lines (387 loc) · 13.1 KB
/
rssi.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*jslint indent: 2, maxlen: 80, continue: false, unparam: false, node: true */
/* -*- tab-width: 2 -*- */
'use strict';
var CF, PT, noOp = Boolean.bind(null, false),
nodeFs = require('fs'), resolveRelativePath = require('path').resolve,
flexiTimeout = require('callback-timeout-flexible'),
readFileCached = require('readfile-cache-pmb'),
makeUniqueIdCounter = require('maxuniqid'),
stringPeeks = require('string-peeks'),
XmlTag = require('xmlattrdict/xmltag');
function isStr(x, no) { return (((typeof x) === 'string') || no); }
function ifFun(x, d) { return ((typeof x) === 'function' ? x : d); }
function typeofIf(x) { return (x && typeof x); }
CF = function RenderSsiFile(opts) {
if (!(this instanceof CF)) { return new CF(opts); }
this.commands = Object.assign({}, PT.commands);
this.nextUniqId = makeUniqueIdCounter();
this.segments = null;
this.pending = {
inserts: {},
hooks: {},
};
this.phase = 'init';
this.postFx = [];
this.lateFx = [];
this.text = '';
Object.assign(this, opts);
if (!this.readFile) { this.readFile = readFileCached.rf(); }
};
PT = CF.prototype;
PT.toString = function () {
return '['.concat(this.constructor.name, ' ',
(this.filename || '<no file name>'), ']');
};
PT.readFile = null; // will be set by constructor
PT.encoding = 'utf-8';
PT.preserveByteOrderMark = true;
PT.defaultFetchTimeoutSec = 0.5;
PT.commands = { // template for the constructor's independent copy
'>prefix': '!--#',
'>suffix': ' --',
};
PT.log = function (level, event, detail) { return [level, event, detail]; };
CF.checkMissingCallback = function (f) {
if (ifFun(f)) { return; }
throw new Error('Callback function required');
};
CF.oneLineJSONify = function (x) {
return JSON.stringify(x, null, 2).replace(/\s*\n\s*/g, ' ');
};
CF.identity = function (x) { return x; };
CF.throwIfTruthy = function (err) { if (err) { throw err; } };
CF.getAnyObjValue = function (obj) { return obj[Object.keys(obj)[0]]; };
CF.normalizeWhitespace = function (text) {
return String(text).replace(/^[ \t\r]+(\n)/g, '$1'
).replace(/\s*$/, '\n');
};
CF.tagToString = function (tag) {
if (!tag) { tag = this; }
return ((tag.cmdName ? 'cmd "' + tag.cmdName + '"'
: 'tag <' + tag.tagName + '>') + ' @ ' + tag.srcPos.fmt());
};
CF.describe = function (x) {
var d = typeofIf(x);
switch (d) {
case '':
case 'string':
return JSON.stringify(x);
case 'object':
d = String(x);
try {
d += ' ' + JSON.stringify(x);
} catch (jsonifyErr) {
d += ' ?? ' + String(jsonifyErr);
}
return d;
}
return String(x);
};
PT.expectPhase = function (want) {
var phase = this.phase;
if (phase === want) { return; }
if (Array.isArray(want) && (want.indexOf(phase) >= 0)) { return; }
throw new Error('Wrong render phase ' + phase + ', expected ' + want);
};
PT.setSourceText = function (text) {
this.expectPhase(['init', 'readSourceFile']);
var normWsp = this.normalizeWhitespace;
switch (typeofIf(normWsp)) {
case 'function':
text = normWsp(text);
break;
case 'boolean': // => true
text = CF.normalizeWhitespace(text);
break;
}
this.text = text;
this.log('D', 'setSourceTextOk', [text.slice(0, 128), text.length]);
this.phase = 'hasSourceText';
return this;
};
PT.recvSourceText = function (next, fetchErr, text) {
this.log('D', 'recvSourceText', [fetchErr, text && text.length]);
this.expectPhase('readSourceFile');
CF.checkMissingCallback(next);
if (fetchErr) { return next(fetchErr); }
try {
this.setSourceText(text);
} catch (setTextErr) {
return next(setTextErr);
}
return next(null);
};
PT.readFileRel = function (relFn, encoding, deliver) {
if (!this.filename) {
relFn = 'Cannot resolve relative path without source filename: ' + relFn;
return deliver(new Error(relFn), null);
}
var absFn = resolveRelativePath(this.filename, '..', relFn);
this.log('D', 'readFileRel:resolve', [relFn, absFn]);
return this.readFile(absFn, encoding, deliver);
};
PT.verifyNonePending = function (what) {
if (Object.keys(this.pending[what]).length === 0) { return; }
throw new Error('Callback flow error! We still have pending ' + what + '.');
};
PT.runNextHook = function (qName, whenHookDone) {
this.verifyNonePending('hooks');
var self = this, q = this[qName], n = q.length, f = (n ? q.shift() : null);
this.log('D', 'runNextHook:checkQ', { qName: qName, qLength: n,
nextHookType: typeofIf(f) });
if (!n) { return false; }
if (!ifFun(f)) {
throw new TypeError('Found non-function hook in Q ' + qName);
}
function g(err) {
delete self.pending.hooks[g.id];
return whenHookDone(err);
}
g.id = qName + '$' + this.nextUniqId();
self.pending.hooks[g.id] = g;
return f(this, g);
};
PT.tryAsync = function (mtdName, args, cb) {
var err = null, val;
try {
val = this[mtdName].apply(this, args);
} catch (caught) {
err = (caught || ('False-y error: ' + caught));
}
return cb(err, val);
};
PT.render = function (whenRendered, err) {
var phase = this.phase,
retryRender = this.render.bind(this, whenRendered);
CF.checkMissingCallback(whenRendered);
if (err) { return whenRendered(err); }
if (phase === 'init') {
if (this.filename) {
this.log('D', 'render:segments:readFile');
this.phase = 'readSourceFile';
this.readFile(this.filename, this.encoding,
this.recvSourceText.bind(this, retryRender));
return;
}
this.log('D', 'render:segments:init');
}
if (!this.segments) {
this.verifyNonePending('inserts');
return this.tryAsync('tokenize', [], retryRender);
}
if (phase === 'tokenized') {
return this.fetchPendingInserts(retryRender);
}
if (phase === 'fetchedAllInserts') {
this.verifyNonePending('inserts');
return this.tryAsync('mergeSegments', [], retryRender);
}
this.expectPhase('segmentsMerged');
this.verifyNonePending('inserts');
if (this.runNextHook('postFx', retryRender)) { return; }
if (this.runNextHook('lateFx', retryRender)) { return; }
return whenRendered(null, this);
};
PT.tokenize = function () {
var self = this, buf, seg = [], tag,
tagStart = '<' + (this.commands['>prefix'] || '');
this.expectPhase(['init', 'hasSourceText']);
this.phase = 'tokenizing';
buf = stringPeeks.fromText(this.text);
this.byteOrderMark = buf.byteOrderMark;
buf.willDrain(function () {
while (buf.eatUntilMarkOrEnd(tagStart, { collect: seg, eatMark: false })) {
tag = self.tokenizeMaybeTag(buf, seg);
if (tag) {
tag = self.foundTag(tag, buf);
switch (tag && (typeof tag) && tag.insertType) {
case 'fetcher':
self.pending.inserts[seg.length] = tag;
break;
}
seg[seg.length] = tag;
}
}
});
this.segments = seg;
this.phase = 'tokenized';
return this;
};
PT.tokenizeMaybeTag = function (buf, seg) {
var tag = buf.peekTag(), tagPrefix = this.commands['>prefix'],
tagSuffix = this.commands['>suffix'];
if (!tag) {
if (seg) { buf.eatUntilMarkOrEnd(1, { collect: seg }); }
// ^--- the invocation without `seg` is a feature for command handlers
// to help them invoke other command handlers without modifying
// `buf` if their suspect code wasn't a tag.
return false;
}
if (tagSuffix) {
if (!tag.endsWith(tagSuffix)) {
if (seg) { seg.push(buf.eat()); }
return false;
}
tag = tag.slice(0, -tagSuffix.length);
}
if (isStr(tag)) { tag = new XmlTag(tag); }
if (tagPrefix) {
if (!tag.tagName.startsWith(tagPrefix)) {
if (seg) { seg.push(buf.eat()); }
return false;
}
tag.cmdName = tag.tagName.slice(tagPrefix.length);
}
tag.srcPos = buf.calcPosLnChar();
tag.toString = CF.tagToString;
tag.origText = buf.eat();
return tag;
};
PT.foundTag = function (tag, buf) {
var val, meta = {};
if (!tag) { throw new Error('missing tag'); }
if (!isStr(tag.origText)) {
throw new Error('missing tag.origText on tag (' +
(typeof tag) + ') "' + tag + '"');
}
val = this.applyCmdFunc('>before', val, tag, buf);
val = this.applyCmdFunc((tag.cmdName || ('<' + tag.tagName)),
val, tag, buf, meta);
if (!meta.func) {
val = this.applyCmdFunc('>other', val, tag, buf, meta);
}
val = this.applyCmdFunc('>after', val, tag, buf);
switch (typeofIf(val)) {
case undefined:
case null:
case false:
return tag.origText;
// We can skip the entire match without risk of missing anything,
// since there can't be a "<" inside a tag.
case '':
case 'string':
return val;
case 'function':
return { insertType: 'fetcher', fetcher: val, tag: tag };
}
throw tag.err('Unsupported return value from command handler: ' + val);
};
PT.applyCmdFunc = function (func, val, tag, buf, meta) {
var hnd = this.commands[func];
this.log('D', 'applyCmdFunc:resolve',
{ func: func, hndType: typeofIf(hnd) });
if (isStr(hnd)) { hnd = (this.commands[hnd] || CF[hnd]); }
if (!ifFun(hnd)) { return val; }
if (meta) { meta.func = hnd; }
this.log('D', 'applyCmdFunc:call',
{ func: func, hndType: typeofIf(hnd) });
return hnd.call(this, val, tag, buf);
};
PT.fetchPendingInserts = function (whenAllFetched) {
var self = this, todo = Object.keys(self.pending.inserts), recvOneSeg;
this.expectPhase('tokenized');
this.phase = 'fetchingInserts';
if (!self.failedInserts) { self.failedInserts = {}; }
if (todo.length < 1) {
this.phase = 'fetchedAllInserts';
return whenAllFetched(null, self);
}
recvOneSeg = this.receiveFetchedSegment.bind(this, whenAllFetched);
try {
todo = todo.map(this.prepareFetchOneInsert.bind(this, recvOneSeg));
} catch (prepErr) {
prepErr.message = 'Failed to start deferred rendering: ' + prepErr.message;
return whenAllFetched(prepErr);
}
todo.forEach(function (how) { how(); });
};
PT.prepareFetchOneInsert = function (rcv, idx) {
var pend = this.pending.inserts, ins = pend[idx],
how = 'fetchOneInsert_' + ins.insertType, proxy;
how = (this[how] || how);
this.log('D', 'fetchPendingInserts:prepareTodo', [idx, ins, how]);
if (ifFun(how)) {
proxy = function RenderSsiFile_hasOneInsert(er, tx) { rcv(idx, er, tx); };
return how.bind(this, ins, proxy, idx);
}
throw new Error('unsupported insert type: ' + CF.describe(ins));
};
function wrapError(origErr, intro, extras) {
if (!origErr) { return null; }
intro = (intro || '');
var bettErr = new Error(intro + String(origErr.message || origErr));
if (origErr.stack) { bettErr.stack = intro + origErr.stack; }
if (extras) { Object.assign(bettErr, extras); }
return bettErr;
}
PT.receiveFetchedSegment = function (whenAllFetched, idx, err, text) {
var fetcher = this.pending.inserts[idx], tag;
if (!fetcher) {
this.log('W', 'inserts:received_nonpending', [idx, err, text]);
return;
}
if (err) {
this.segments[idx] = err;
this.failedInserts[idx] = { fetcher: fetcher, err: err };
} else {
tag = (fetcher.tag || false);
if (tag.filterFetchedText) { text = tag.filterFetchedText(text); }
this.segments[idx] = text;
}
delete this.pending.inserts[idx];
this.expectPhase('fetchingInserts');
if (Object.keys(this.pending.inserts).length > 0) { return; }
this.phase = 'fetchedAllInserts';
err = wrapError((CF.getAnyObjValue(this.failedInserts) || false).err,
'Errors in deferred rendering, see .failedInserts. One of them: ',
{ failedInserts: this.failedInserts });
return whenAllFetched(err, this);
};
PT.fetchOneInsert_fetcher = function (ins, whenReceived) {
var tmo = flexiTimeout(whenReceived, {
limitSec: (ins.fetcher.fetchTimeoutSec || this.defaultFetchTimeoutSec),
name: 'content fetcher for ' + String(ins.tag),
errMsg: 'No feedback from \v{name}',
});
this.log('D', 'fetchOneInsert_fetcher', String(tmo));
setImmediate(function fetchOneInsert_proxy() { return ins.fetcher(tmo); });
};
function stringifySegment(seg, idx) {
if (isStr(seg)) { return seg; }
throw new Error('Segment #' + idx + ': unsupported segment type: '
+ CF.describe(seg));
}
PT.mergeSegments = function () {
this.text = this.segments.map(stringifySegment).join('');
this.phase = 'segmentsMerged';
};
PT.getText = function () {
var text = this.text;
if (text === null) { throw new Error('not .render()ed'); }
return text;
};
PT.getOutputBOM = function () {
return ((this.preserveByteOrderMark && this.byteOrderMark) || '');
};
PT.saveToFile = function (destFn, whenSaved) {
var text = this.getOutputBOM() + this.getText();
if (destFn === '-') {
process.stdout.write(text);
return (whenSaved && whenSaved(null));
}
nodeFs.writeFile(destFn, text, { encoding: this.encoding }, whenSaved);
};
CF.rejectLeftoverAttrs = function (text, tag) {
if (arguments.length === 1) { tag = text; }
var leftover = Object.keys(tag.attrs);
if (leftover.length === 0) { return text; }
leftover = CF.oneLineJSONify(tag.attrs);
throw tag.err('leftover attributes: ' + leftover);
};
CF.fromFile = function (srcFn, deliver) {
var ssiFile = new CF();
ssiFile.filename = srcFn;
return ssiFile.render(deliver);
};
module.exports = CF;
if (require.main === module) { CF.fromFile('index.shtml', process); }