-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
111 lines (94 loc) · 4.25 KB
/
index.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
/*───────────────────────────────────────────────────────────────────────────*\
│ Copyright (C) 2016 PayPal │
│ │
│hh ,'""`. │
│ / _ _ \ Licensed under the Apache License, Version 2.0 (the "License"); │
│ |(@)(@)| you may not use this file except in compliance with the License. │
│ ) __ ( You may obtain a copy of the License at │
│ /,'))((`.\ │
│(( (( )) )) http://www.apache.org/licenses/LICENSE-2.0 │
│ `\ `)(' /' │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
"use strict";
var makeViewClass = require('engine-munger');
var WeakMap = require('es6-weak-map');
var bundalo = require('bundalo');
var aproba = require('aproba');
var associated = new WeakMap();
module.exports = function setupViewClass(options) {
var opts = {};
opts['.properties'] = {};
opts['.js'] = {};
opts['.dust'] = {};
var bundler;
if (options.i18n) {
opts['.properties'].root = [].concat(options.i18n.contentPath);
opts['.properties'].i18n = {
formatPath: options.i18n.formatPath || formatPath,
fallback: options.i18n.fallback
};
bundler = bundalo({
contentPath: options.i18n.contentPath
});
}
if (options.specialization) {
opts['.properties'].specialization = options.specialization;
opts['.js'].specialization = options.specialization;
opts['.dust'].specialization = options.specialization;
}
var hasConfiguredApp = false;
var localeContext = options.localeContext || 'locale';
return function (req, res, next) {
if (!hasConfiguredApp) {
req.app.set('view', makeViewClass(opts));
hasConfiguredApp = true;
}
associated.set(req, {
get: function (bundle, model, cb) {
aproba('*OF', arguments);
if (!bundler) {
return cb(new Error('i18n is not configured'));
} else {
return bundler.get({bundle: bundle, locality: req[localeContext] || options.i18n.fallback, model: model}, cb);
}
}
});
res.on('finish', function () {
associated.delete(req);
});
res.once('error', function (err) {
associated.delete(req);
if (res.listeners('error').length === 0) {
res.emit('error', err);
}
});
next();
};
};
function getBundler(req) {
var bundler = associated.get(req);
if (!bundler) {
throw new Error("No bundle reader available");
} else {
return bundler;
}
}
function formatPath(locale) {
if (!locale || !locale.langtag || !locale.langtag.language) {
var e = new Error("locale must be a bcp47-style object");
e.code = 'EINVALIDTYPE'
throw e;
} else {
return locale.langtag.region + '/' + locale.langtag.language.language;
}
}
module.exports.js = require('adaro').js;
module.exports.dust = require('adaro').dust;
module.exports.getBundler = getBundler;
module.exports.formatPath = formatPath;