-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
470 lines (412 loc) · 16.1 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
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
// Copyright 2022 DeepL SE (https://www.deepl.com)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
const express = require('express');
const httpProxy = require('http-proxy');
const app = express();
app.use(express.urlencoded({ limit: '50mb', extended: true }));
const nocache = require('nocache');
app.use(nocache());
const fileUpload = require('express-fileupload');
app.use(fileUpload({
createParentPath: true,
}));
const morgan = require('morgan');
// Logging utility
app.use(morgan('dev')); // Developer-style formatting
const sessions = require('./sessions');
app.use(sessions());
const auth = require('./auth');
const documents = require('./documents');
const glossaries = require('./glossaries');
const languages = require('./languages');
const util = require('./util');
const envVarPort = 'DEEPL_MOCK_SERVER_PORT';
const envVarProxyPort = 'DEEPL_MOCK_PROXY_SERVER_PORT';
const port = Number(process.env[envVarPort]);
const proxyPort = Number(process.env[envVarProxyPort]);
if (Number.isNaN(port)) {
console.error(`The ${envVarPort} environment variable must be defined as the port number.`);
process.exit(2);
}
if (Number.isNaN(proxyPort)) {
console.info(`The ${envVarProxyPort} environment variable is not defined, no proxy will be used.`);
}
function requireUserAgent(req, res, next) {
const userAgentHeader = req.headers['user-agent'];
if ((userAgentHeader === undefined || userAgentHeader === '') && !req.session?.allow_missing_user_agent) {
// Give no response and do not continue with next handler
res.status(400).send({ message: 'User-Agent header missing.' });
return undefined;
}
return next();
}
function getParam(req, name, options) {
let v = req.body[name] || req.query[name];
if (options?.params) {
v = req.params[name];
}
if (options?.multi) {
if (v === undefined) v = [];
v = Array.isArray(v) ? v : [v];
if (options?.required && v.length === 0) {
if (options?.newErrorMessage) throw new util.HttpError(`Missing or invalid argument: ${name}'`);
throw new util.HttpError(`Parameter '${name}' not specified`);
}
} else {
v = Array.isArray(v) ? v[0] : v;
if (options?.required && v === undefined) {
if (options?.newErrorMessage) throw new util.HttpError(`Missing or invalid argument: ${name}'`);
throw new util.HttpError(`Parameter '${name}' not specified`);
} else if (v === undefined) {
return options?.default;
}
if (options?.lower && v) v = v.toLowerCase();
else if (options?.upper && v) v = v.toUpperCase();
if (options?.validator && options?.validator(v) === false) {
if (options?.newErrorMessage) throw new util.HttpError(`Missing or invalid argument: ${name}'`);
throw new util.HttpError(`Value for '${name}' not supported.`);
}
if (options?.allowedValues && !options?.allowedValues.includes(v)) {
if (options?.newErrorMessage) throw new util.HttpError(`Missing or invalid argument: ${name}'`);
throw new util.HttpError(`Value for '${name}' not supported.`);
}
}
return v;
}
function getParamFormality(req, targetLang) {
getParam(req, 'formality', {
default: 'default',
allowedValues: ['less', 'more', 'default', 'prefer_less', 'prefer_more'],
validator: (formality) => {
if (!languages.supportsFormality(targetLang, formality)) {
throw new util.HttpError("'formality' is not supported for given 'target_lang'.", 400);
}
},
});
}
function getParamGlossary(req, sourceLang) {
const { authKey } = req.user_account;
const glossaryId = getParam(req, 'glossary_id',
{ validator: (id) => (id === undefined || glossaries.isValidGlossaryId(id)) });
if (glossaryId !== undefined && sourceLang === undefined) {
throw new util.HttpError('Use of a glossary requires the source_lang parameter to be specified', 400);
}
return glossaryId === undefined ? undefined : glossaries.getGlossary(glossaryId, authKey);
}
function getParamGlossaryId(req, required = true) {
return getParam(req, 'glossary_id',
{
params: true,
required,
validator: (id) => (id === undefined || glossaries.isValidGlossaryId(id)),
});
}
function checkLimit(usage, type, request) {
/* eslint-disable no-param-reassign */
// Note: this function modifies the usage argument, incrementing the count used
const count = `${type}_count`;
const limit = `${type}_limit`;
if (usage[limit] === undefined) return true;
if (usage[count] + request > usage[limit]) return false;
usage[count] += request;
return true;
/* eslint-enable no-param-reassign */
}
async function handleLanguages(req, res) {
try {
const paramType = getParam(req, 'type', {
default: 'source',
validator: (type) => {
if (!['source', 'target'].includes(type)) {
throw new Error("Parameter 'type' is invalid. 'source' and 'target' are valid values.");
}
},
});
if (paramType === 'target') {
res.send(languages.getTargetLanguages());
} else {
res.send(languages.getSourceLanguages());
}
} catch (err) {
res.status(400).send({ message: err.message });
}
}
async function handleUsage(req, res) {
res.send(req.user_account.usage);
}
async function handleTranslate(req, res) {
try {
const targetLang = getParam(req, 'target_lang', {
upper: true, validator: languages.isTargetLanguage,
});
const sourceLang = getParam(req, 'source_lang', {
upper: true, validator: languages.isSourceLanguage,
});
const textArray = getParam(req, 'text', { multi: true, required: true });
const glossary = getParamGlossary(req, sourceLang);
// The following parameters are validated but not used by the mock server
getParam(req, 'split_sentences', { default: '1', allowedValues: ['0', '1', 'nonewlines'] });
getParam(req, 'preserve_formatting', { default: '0', allowedValues: ['0', '1', true, false] });
getParamFormality(req, targetLang);
getParam(req, 'tag_handling', { default: 'xml', allowedValues: ['html', 'xml'] });
getParam(req, 'outline_detection', { default: '1', allowedValues: ['0', '1', true, false] });
const showBilledCharacters = getParam(req, 'show_billed_characters', { default: false, allowedValues: ['0', '1', true, false] });
const modelType = getParam(req, 'model_type', { allowedValues: ['quality_optimized', 'latency_optimized', 'prefer_quality_optimized'] });
// Calculate the character count of the requested translation
const totalCharacters = textArray.reduce((total, text) => (total + text.length), 0);
// Check if session is configured to respond with 429: too-many-requests
if (req.session.respond_429_count > 0) {
req.session.respond_429_count -= 1;
res.status(429).send();
} else if (!checkLimit(req.user_account.usage, 'character', totalCharacters)) {
res.status(456).send({ message: 'Quota for this billing period has been exceeded.' });
} else {
const body = {
translations: textArray.map((text) => {
const result = languages.translate(text, targetLang, sourceLang, glossary);
if (showBilledCharacters) {
result.billed_characters = text.length;
}
if (modelType) {
result.model_type_used = modelType.replace('prefer_', '');
}
return result;
}),
};
res.status(200).send(body);
}
} catch (err) {
res.status(400).send({ message: err.message });
}
}
async function handleDocument(req, res) {
try {
const targetLang = getParam(req, 'target_lang', {
upper: true, validator: languages.isTargetLanguage,
});
const sourceLang = getParam(req, 'source_lang', {
upper: true, validator: languages.isSourceLanguage,
});
getParamFormality(req, targetLang);
const glossary = getParamGlossary(req, sourceLang);
const outputFormat = getParam(req, 'output_format', { lower: true });
if (!req.files || req.files.file === undefined) {
res.status(400).send({ message: 'Invalid file data.' });
} else {
const { file } = req.files;
try {
// Mock server simplification: billed characters assumed to be file size
const totalCharacters = file.size;
if (!checkLimit(req.user_account.usage, 'character', totalCharacters)
|| !checkLimit(req.user_account.usage, 'document', 1)
|| !checkLimit(req.user_account.usage, 'team_document', 1)) {
res.status(456).send({ message: 'Quota for this billing period has been exceeded.' });
} else {
const { authKey } = req.user_account;
const document = await documents.createDocument(file, authKey, targetLang, sourceLang,
glossary, outputFormat);
res.status(200).send({
document_id: document.id,
document_key: document.key,
});
await documents.translateDocument(document, req.session);
}
} catch (err) {
res.status(err.status()).send(err.body());
}
}
} catch (err) {
res.status(400).send({ message: err.message });
}
}
async function handleDocumentStatus(req, res) {
try {
const { authKey } = req.user_account;
const documentKey = getParam(req, 'document_key', { single: true });
const document = documents.getDocument(req.params.document_id, documentKey, authKey,
req.session);
const body = {
document_id: document.id,
status: document.status,
};
if (document.status === 'translating') {
body.seconds_remaining = document.seconds_remaining;
} else if (document.status === 'done') {
body.seconds_remaining = document.seconds_remaining;
body.billed_characters = document.billed_characters;
} else if (document.status === 'error') {
body.error_message = document.error_message;
// Field 'message' is also set for backward compatibility
body.message = document.error_message;
}
res.status(200).send(body);
} catch (err) {
console.log(err.message);
res.status(404).send();
}
}
async function handleDocumentDownload(req, res) {
try {
const { authKey } = req.user_account;
const documentKey = getParam(req, 'document_key', { single: true });
const document = documents.getDocument(req.params.document_id, documentKey, authKey,
req.session);
if (document.status !== 'done') {
res.status(503).send({ message: 'Document translation is not done' });
} else {
res.status(200);
res.download(
document.path_out,
document.name_out,
{
headers: { 'Content-Type': document.contentType },
},
(err) => {
if (err) {
console.log(`Error occurred during file download: ${err}`);
} else {
documents.removeDocument(document);
}
},
);
}
} catch {
res.status(404).send();
}
}
async function handleGlossaryList(req, res) {
try {
// Access glossary_id param from path, note: glossary_id is optional, so may be undefined
const glossaryId = getParamGlossaryId(req, false);
const { authKey } = req.user_account;
if (glossaryId !== undefined) {
const glossaryInfo = glossaries.getGlossaryInfo(glossaryId, authKey);
res.status(200).send(glossaryInfo);
} else {
const glossaryList = glossaries.getGlossaryInfoList(authKey);
res.status(200).send({ glossaries: glossaryList });
}
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
async function handleGlossaryEntries(req, res) {
try {
if (req.accepts('text/tab-separated-values')) {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
const entries = glossaries.getGlossaryEntries(glossaryId, authKey);
res.contentType('text/tab-separated-values; charset=UTF-8');
res.status(200).send(entries);
} else {
res.status(415).send({ message: 'No supported media type specified in Accept header' });
}
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
function handleGlossaryLanguages(req, res) {
try {
res.status(200).send(
{
supported_languages: languages.getGlossaryLanguagePairs(),
},
);
} catch (err) {
console.log(err.message);
res.status(err.status()).send(err.body());
}
}
async function handleGlossaryCreate(req, res) {
try {
const { authKey } = req.user_account;
const targetLang = getParam(req, 'target_lang', {
upper: true, validator: languages.isGlossaryLanguage,
});
const sourceLang = getParam(req, 'source_lang', {
upper: true, validator: languages.isGlossaryLanguage,
});
const name = getParam(req, 'name', {
required: true,
newErrorMessage: true,
validator: (value) => value.length !== 0,
});
const entries = getParam(req, 'entries', { required: true });
const entriesFormat = getParam(req, 'entries_format', {
required: true,
validator: (value) => {
if (value !== 'tsv' && value !== 'csv') {
throw new util.HttpError('Unsupported entry format specified', 401);
}
},
});
const glossaryInfo = await glossaries.createGlossary(name, authKey, targetLang, sourceLang,
entriesFormat, entries);
res.status(201).send(glossaryInfo);
} catch (err) {
console.log(err.message);
res.status(err.status()).send(err.body());
}
}
async function handleGlossaryDelete(req, res) {
try {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
glossaries.removeGlossary(glossaryId, authKey);
res.status(204).send();
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
app.use('/v2/languages', express.json());
app.get('/v2/languages', auth, requireUserAgent, handleLanguages);
app.post('/v2/languages', auth, requireUserAgent, handleLanguages);
app.use('/v2/usage', express.json());
app.get('/v2/usage', auth, requireUserAgent, handleUsage);
app.post('/v2/usage', auth, requireUserAgent, handleUsage);
app.use('/v2/translate', express.json());
app.get('/v2/translate', auth, requireUserAgent, handleTranslate);
app.post('/v2/translate', auth, requireUserAgent, handleTranslate);
app.post('/v2/document', auth, requireUserAgent, handleDocument);
app.use('/v2/document/:document_id', express.json());
app.get('/v2/document/:document_id', auth, requireUserAgent, handleDocumentStatus);
app.post('/v2/document/:document_id', auth, requireUserAgent, handleDocumentStatus);
app.use('/v2/document/:document_id/result', express.json());
app.get('/v2/document/:document_id/result', auth, requireUserAgent, handleDocumentDownload);
app.post('/v2/document/:document_id/result', auth, requireUserAgent, handleDocumentDownload);
// Maximum glossary size is 10MiB, but there is some extra request overhead
app.use('/v2/glossaries', express.json({ limit: '11mb' }));
app.get('/v2/glossary-language-pairs', auth, requireUserAgent, handleGlossaryLanguages);
app.post('/v2/glossaries', auth, requireUserAgent, handleGlossaryCreate);
app.get('/v2/glossaries', auth, requireUserAgent, handleGlossaryList);
app.get('/v2/glossaries/:glossary_id', auth, requireUserAgent, handleGlossaryList);
app.get('/v2/glossaries/:glossary_id/entries', auth, requireUserAgent, handleGlossaryEntries);
app.delete('/v2/glossaries/:glossary_id', auth, requireUserAgent, handleGlossaryDelete);
app.all('/*', (req, res) => {
res.status(404).send();
});
const server = app.listen(port, () => {
console.log(`DeepL API mock-server listening on port ${port}`);
}).on('error', (error) => {
console.error(`Error occurred while starting the server: ${error}`);
process.exit(1);
});
server.keepAliveTimeout = 10 * 1000;
if (!Number.isNaN(proxyPort)) {
const proxyApp = express();
const proxy = httpProxy.createProxyServer({});
proxyApp.all('*', (req, res) => {
console.log('Proxying request:', req.method, req.url);
req.headers.forwarded = `for=${req.ip}`;
proxy.web(req, res, { target: `http://localhost:${port}` }, (err) => {
console.log('Error while proxying request:', err);
});
});
proxyApp.listen(proxyPort, () => {
console.log(`DeepL API mock-proxy-server listening on port ${proxyPort}`);
});
}