/
server.ts
241 lines (209 loc) · 5.96 KB
/
server.ts
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
import type { NextHandleFunction } from 'connect';
import { Match, check } from 'meteor/check';
import { DDP } from 'meteor/ddp';
import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import stripJsonComments from 'strip-json-comments';
import { parse, resolve } from 'url';
import { getJS, getJSON, getYML } from './code-generators';
import { GetCacheEntry, GetCacheFunction, i18n } from './common';
import './global';
i18n.setOptions({ hostUrl: Meteor.absoluteUrl() });
const _get = i18n._contextualLocale.get.bind(i18n._contextualLocale);
i18n._contextualLocale.get = () => _get() ?? i18n._getConnectionLocale();
i18n._formatgetters = { getJS, getJSON, getYML };
const _publishConnectionId = new Meteor.EnvironmentVariable<
string | undefined
>();
i18n._getConnectionId = connection => {
let connectionId = connection?.id;
try {
connectionId =
(DDP as any)._CurrentInvocation.get()?.connection?.id ??
_publishConnectionId.get();
} catch (error) {
// Outside of fibers we cannot detect the connection id.
}
return connectionId;
};
const _localesPerConnections: Record<string, string> = {};
i18n._getConnectionLocale = connection =>
_localesPerConnections[i18n._getConnectionId(connection)!];
const cache: Record<string, GetCacheEntry> = {};
i18n.getCache = (locale => {
if (!locale) {
return cache;
}
if (!cache[locale]) {
cache[locale] = {
updatedAt: new Date().toUTCString(),
getYML,
getJSON,
getJS,
};
}
return cache[locale];
}) as GetCacheFunction;
i18n.loadLocale = async (
locale,
{
fresh = false,
host = i18n.options.hostUrl,
pathOnHost = i18n.options.pathOnHost,
queryParams = {},
silent = false,
} = {},
) => {
const normalizedLocale = i18n.normalize(locale);
if (!normalizedLocale) {
i18n._logger(new Error(`Unrecognized locale "${locale}"`));
return Promise.resolve(undefined);
}
queryParams.type = 'json';
if (fresh) {
queryParams.ts = new Date().getTime();
}
const url = resolve(
host,
pathOnHost + normalizedLocale + '?type=' + queryParams.type,
);
try {
const data = await fetch(url, { method: 'GET' });
const json = await data.json();
const { content } = json || {};
if (content) {
i18n.addTranslations(
normalizedLocale,
JSON.parse(stripJsonComments(content as string)),
);
delete cache[normalizedLocale];
if (!silent) {
const locale = i18n.getLocale();
// If current locale is changed we must notify about that.
if (
locale.indexOf(normalizedLocale) === 0 ||
i18n.options.defaultLocale.indexOf(normalizedLocale) === 0
) {
i18n._emitChange();
}
}
} else {
console.error('missing content');
}
} catch (error) {
console.error(error);
}
return undefined;
};
i18n.setLocaleOnConnection = (
locale: string,
connectionId = i18n._getConnectionId(),
) => {
if (typeof _localesPerConnections[connectionId!] === 'string') {
_localesPerConnections[connectionId!] = i18n.normalize(locale)!;
return;
}
throw new Error(`There is no connection under id: ${connectionId}`);
};
WebApp.connectHandlers.use('/universe/locale/', ((request, response, next) => {
const {
pathname,
query: {
attachment = false,
diff = false,
namespace,
preload = false,
type,
},
} = parse(request.url || '', true);
if (type && !['js', 'json', 'yml'].includes(type as string)) {
response.writeHead(415);
response.end();
return;
}
const locale = pathname?.match(/^\/?([a-z]{2}[a-z0-9\-_]*)/i)?.[1];
if (!locale) {
next();
return;
}
const cache = i18n.getCache(locale);
if (!cache?.updatedAt) {
response.writeHead(501);
response.end();
return;
}
const headers: Record<string, string> = {
...i18n.options.translationsHeaders,
'Last-Modified': cache.updatedAt,
};
if (attachment) {
const filename = `${locale}.i18n.${type || 'js'}`;
headers['Content-Disposition'] = `attachment; filename="${filename}"`;
}
switch (type) {
case 'json':
response.writeHead(200, {
'Content-Type': 'application/json; charset=utf-8',
...headers,
});
response.end(cache.getJSON(locale, namespace as string, diff as string));
break;
case 'yml':
response.writeHead(200, {
'Content-Type': 'text/yaml; charset=utf-8',
...headers,
});
response.end(cache.getYML(locale, namespace as string, diff as string));
break;
default:
response.writeHead(200, {
'Content-Type': 'application/javascript; charset=utf-8',
...headers,
});
response.end(
cache.getJS(locale, namespace as string, preload as boolean),
);
break;
}
}) as NextHandleFunction);
Meteor.methods({
'universe.i18n.setServerLocaleForConnection'(locale) {
check(locale, Match.Any);
if (
typeof locale !== 'string' ||
!i18n.options.sameLocaleOnServerConnection
) {
return;
}
const connectionId = i18n._getConnectionId(this.connection);
if (!connectionId) {
return;
}
i18n.setLocaleOnConnection(locale, connectionId);
},
});
Meteor.onConnection(connection => {
_localesPerConnections[connection.id] = '';
connection.onClose(() => {
delete _localesPerConnections[connection.id];
});
});
function patchPublish(publish: typeof Meteor.publish) {
return function (this: typeof Meteor, name, func, ...args) {
return publish.call(
this,
name,
function (...args) {
return _publishConnectionId.withValue(this?.connection?.id, () =>
func.apply(this, args),
);
},
...args,
);
} as typeof Meteor.publish;
}
Meteor.publish = patchPublish(Meteor.publish);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(Meteor as any).server.publish = patchPublish((Meteor as any).server.publish);
export { i18n };
export default i18n;