-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.min.js
More file actions
166 lines (161 loc) · 4.73 KB
/
index.min.js
File metadata and controls
166 lines (161 loc) · 4.73 KB
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
"use strict";
var headerRegex = /([a-z]{2})/gi, all = {
ab: "Abkhazian",
af: "Afrikaans",
an: "Aragonese",
ar: "Arabic",
as: "Assamese",
az: "Azerbaijani",
be: "Belarusian",
bg: "Bulgarian",
bn: "Bengali",
bo: "Tibetan",
br: "Breton",
bs: "Bosnian",
ca: "Catalan",
ce: "Chechen",
co: "Corsican",
cs: "Czech",
cu: "Church",
cy: "Welsh",
da: "Danish",
de: "German",
el: "Greek",
en: "English",
eo: "Esperanto",
es: "Spanish",
et: "Estonian",
eu: "Basque",
fa: "Persian",
fi: "Finnish",
fj: "Fijian",
fo: "Faroese",
fr: "French",
fy: "Frisian",
ga: "Irish",
gd: "Gaelic",
gl: "Galician",
gv: "Manx",
he: "Hebrew",
hi: "Hindi",
hr: "Croatian",
ht: "Haitian",
hu: "Hungarian",
hy: "Armenian",
id: "Indonesian",
is: "Icelandic",
it: "Italian",
ja: "Japanese",
jv: "Javanese",
ka: "Georgian",
kg: "Kongo",
ko: "Korean",
ku: "Kurdish",
kw: "Cornish",
ky: "Kirghiz",
la: "Latin",
lb: "Luxembourgish",
li: "Limburgan",
ln: "Lingala",
lt: "Lithuanian",
lv: "Latvian",
mg: "Malagasy",
mk: "Macedonian",
mn: "Mongolian",
mo: "Moldavian",
ms: "Malay",
mt: "Maltese",
my: "Burmese",
nb: "Norwegian",
ne: "Nepali",
nl: "Dutch",
nn: "Norwegian",
no: "Norwegian",
oc: "Occitan",
pl: "Polish",
pt: "Portuguese",
rm: "Raeto-Romance",
ro: "Romanian",
ru: "Russian",
sc: "Sardinian",
se: "Sami",
sk: "Slovak",
sl: "Slovenian",
so: "Somali",
sq: "Albanian",
sr: "Serbian",
sv: "Swedish",
sw: "Swahili",
tk: "Turkmen",
tr: "Turkish",
ty: "Tahitian",
uk: "Ukrainian",
ur: "Urdu",
uz: "Uzbek",
vi: "Vietnamese",
vo: "Volapuk",
yi: "Yiddish",
zh: "Chinese"
};
function set(my, res, lang) {
return res.cookie(my.cookie, lang, {
domain: my.domain,
path: my.path,
maxAge: my.maxAge,
httpOnly: my.httpOnly,
secure: my.secure,
signed: my.signed
}), lang;
}
function detect(ll, header, lang) {
if (header) for (var tmp, language = header.match(headerRegex), i = 0, ii = (language = language.filter(function(elem, pos, self) {
return self.indexOf(elem.toLowerCase()) === pos;
})).length; i < ii; ++i) if (tmp = lang[language[i]]) return tmp;
return ll;
}
function language(opt) {
var options = opt || Object.create(null), include = __dirname + "/min/lib/dictionary.js", lang = options.dictionary || require(include).LANG, my = {
cookie: String(options.cookie || "lang"),
domain: String(options.domain || ""),
path: String(options.path || "/"),
maxAge: Number(options.maxAge) || 31536e6,
httpOnly: Boolean(options.httpOnly),
secure: Boolean(options.secure),
signed: Boolean(options.signed)
};
void 0 === lang._default ? lang = require(include).LANG : all[lang._default] || (console.error("language misconfigured"),
lang = require(include).LANG), process.env.lang = lang._default;
var cookie = "cookies";
if (my.signed && (cookie = "signedCookies"), "string" == typeof options.encryptionSecret) {
var vault = require("cookie-encryption"), encryptionOptions = opt.encryptionOptions || Object.create(null);
return vault = vault(options.encryptionSecret, {
cookie: my.cookie,
cipher: encryptionOptions.cipher,
encoding: encryptionOptions.encoding,
extra: encryptionOptions.extra,
domain: encryptionOptions.domain || my.domain,
path: encryptionOptions.path || my.path,
maxAge: encryptionOptions.maxAge || my.maxAge,
httpOnly: encryptionOptions.httpOnly || my.httpOnly,
secure: encryptionOptions.secure || my.secure,
signed: encryptionOptions.signed || my.signed
}), function(req, res, next) {
var biscotto;
try {
biscotto = vault.read(req);
} catch (e) {
biscotto = void 0;
}
if (biscotto && lang[biscotto]) return req[cookie][my.cookie] = biscotto, next();
var ll = detect(lang._default, req.headers["accept-language"], lang);
return req[cookie][my.cookie] = vault.write(req, ll), next();
};
}
return function(req, res, next) {
var biscotto = req[cookie];
if (void 0 === biscotto) biscotto = Object.create(null); else if (lang[biscotto[my.cookie]]) return next();
var ll = detect(lang._default, req.headers["accept-language"], lang);
return biscotto[my.cookie] = set(my, res, ll), next();
};
}
module.exports = language;