-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
738 lines (657 loc) · 25.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// 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 glossariesV2 = require('./glossariesV2');
const glossariesV3 = require('./glossariesV3');
const languages = require('./languages');
const { writingStyles, WritingStyle } = require('./writing_styles');
const { writingTones, WritingTone } = require('./writing_tones');
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) {
return 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 getParamWritingStyle(req, targetLang) {
return getParam(req, 'writing_style', {
allowedValues: writingStyles,
lower: true,
validator: (style) => {
if (!languages.supportsWritingStyle(targetLang, style)) {
const langName = languages.getLanguageName(targetLang);
throw new util.HttpError(`Language ${langName} does not support setting a writing style.`, 400);
}
},
});
}
function getParamTone(req, targetLang) {
return getParam(req, 'tone', {
allowedValues: writingTones,
lower: true,
validator: (tone) => {
if (!languages.supportsWritingTone(targetLang, tone)) {
const langName = languages.getLanguageName(targetLang);
throw new util.HttpError(`Language ${langName} does not support setting a tone.`, 400);
}
},
});
}
function getParamGlossary(req, sourceLang) {
const { authKey } = req.user_account;
const glossaryId = getParam(req, 'glossary_id',
{ validator: (id) => (id === undefined || glossariesV2.isValidGlossaryId(id)) });
if (glossaryId !== undefined && sourceLang === undefined) {
throw new util.HttpError('Use of a glossary requires the source_lang parameter to be specified', 400);
}
try {
return glossaryId === undefined ? undefined : glossariesV2.getGlossary(glossaryId, authKey);
} catch {
return glossaryId === undefined ? undefined : glossariesV3.getGlossary(glossaryId, authKey);
}
}
function getParamGlossaryId(req, required = true) {
return getParam(req, 'glossary_id',
{
params: true,
required,
validator: (id) => (id === undefined || glossariesV2.isValidGlossaryId(id)),
});
}
function getParamSourceLang(req, required = true) {
return getParam(req, 'source_lang',
{
required,
upper: true,
validator: languages.isGlossaryLanguage,
});
}
function getParamTargetLang(req, required = true) {
return getParam(req, 'target_lang',
{
required,
upper: true,
validator: languages.isGlossaryLanguage,
});
}
function getParamDictionaries(req, required = true) {
const glossaryDicts = getParam(req, 'dictionaries', {
required, multi: true,
});
return glossaryDicts.map((glossaryDict) => ({
sourceLang: glossaryDict.source_lang,
targetLang: glossaryDict.target_lang,
entries: glossaryDict.entries,
entriesFormat: glossaryDict.entries_format,
}));
}
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 handleRephrase(req, res) {
try {
let targetLang = getParam(req, 'target_lang', {
upper: true,
validator: (langCode) => languages.isTargetLanguage(langCode)
&& languages.supportsWrite(langCode),
});
if (targetLang === 'EN') {
targetLang = 'EN-US';
} else if (targetLang === 'PT') {
targetLang = 'PT-PT';
}
const textArray = getParam(req, 'text', { multi: true, required: true });
// The following parameters are validated but not used by the mock server
const writingStyle = getParamWritingStyle(req, targetLang);
const tone = getParamTone(req, targetLang);
if (writingStyle !== undefined && writingStyle !== null
&& writingStyle !== WritingStyle.DEFAULT && writingStyle !== WritingStyle.PREFER_DEFAULT
&& tone !== undefined && tone !== null
&& tone !== WritingTone.DEFAULT && tone !== WritingTone.PREFER_DEFAULT) {
throw new util.HttpError('Bad request. Reason: Both writing_style and tone defined. Currently only style OR tone can be chosen for a single request.', 400);
}
// Calculate the character count of the requested text improvement
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 = {
improvements: textArray.map((text) => {
const result = languages.rephrase(
text,
util.convertToBcp47(targetLang),
);
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 handleV2GlossaryList(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 = glossariesV2.getGlossaryInfo(glossaryId, authKey);
res.status(200).send(glossaryInfo);
} else {
const glossaryList = glossariesV2.getGlossaryInfoList(authKey);
res.status(200).send({ glossaries: glossaryList });
}
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
async function handleV3GlossaryList(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 = glossariesV3.getGlossaryInfo(glossaryId, authKey);
res.status(200).send(glossaryInfo);
} else {
const glossaryList = glossariesV3.getGlossaryInfoList(authKey);
res.status(200).send({ glossaries: glossaryList });
}
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
async function handleV2GlossaryEntries(req, res) {
try {
if (req.accepts('text/tab-separated-values')) {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
const entries = glossariesV2.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();
}
}
async function handleV3GlossaryEntries(req, res) {
try {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
const sourceLang = getParamSourceLang(req);
const targetLang = getParamTargetLang(req);
const entries = glossariesV3.getDictionaryEntries(
glossaryId,
sourceLang,
targetLang,
authKey,
);
res.status(200).send(entries);
} 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 handleV2GlossaryCreate(req, res) {
try {
const { authKey } = req.user_account;
const name = getParam(req, 'name', {
required: true,
newErrorMessage: true,
validator: (value) => value.length !== 0,
});
const targetLang = getParam(req, 'target_lang', {
upper: true, validator: languages.isGlossaryLanguage,
});
const sourceLang = getParam(req, 'source_lang', {
upper: true, validator: languages.isGlossaryLanguage,
});
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 glossariesV2.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 handleV3GlossaryCreate(req, res) {
try {
const { authKey } = req.user_account;
const name = getParam(req, 'name', {
required: true,
newErrorMessage: true,
validator: (value) => value.length !== 0,
});
const glossaryDicts = getParamDictionaries(req);
const glossaryInfo = await glossariesV3.createGlossary(name, authKey, glossaryDicts);
res.status(201).send(glossaryInfo);
} catch (err) {
console.log(err.message);
res.status(err.status()).send(err.body());
}
}
async function handleV2GlossaryDelete(req, res) {
try {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
glossariesV2.removeGlossary(glossaryId, authKey);
res.status(204).send();
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
async function handleV3GlossaryDelete(req, res) {
try {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
glossariesV3.removeGlossary(glossaryId, authKey);
res.status(204).send();
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
async function handleDictionaryDelete(req, res) {
try {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
const sourceLang = getParamSourceLang(req);
const targetLang = getParamTargetLang(req);
glossariesV3.removeDictionary(glossaryId, sourceLang, targetLang, authKey);
res.status(204).send();
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
async function handleGlossaryPatch(req, res) {
try {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
const name = getParam(req, 'name', {
required: false,
newErrorMessage: true,
validator: (value) => value.length !== 0,
});
const glossaryDicts = getParamDictionaries(req, false);
const glossary = await glossariesV3.patchGlossary(glossaryId, name, glossaryDicts, authKey);
res.status(200).send(glossary);
} catch (err) {
console.log(err.message);
res.status(err.status()).send();
}
}
async function handleDictionaryPut(req, res) {
try {
const glossaryId = getParamGlossaryId(req);
const { authKey } = req.user_account;
const sourceLang = getParamSourceLang(req);
const targetLang = getParamTargetLang(req);
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 glossary = await glossariesV3.putDictionary(
glossaryId,
sourceLang,
targetLang,
entries,
entriesFormat,
authKey,
);
res.status(200).send(glossary);
} 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);
// (internal only) Note that this is not a real endpoint on the DeepL API. It is only included
// to support testing path overrides in the client libraries.
app.post('/v2/translate_secondary', auth, requireUserAgent, handleTranslate);
app.use('/v2/write/rephrase', express.json());
app.get('/v2/write/rephrase', auth, requireUserAgent, handleRephrase);
app.post('/v2/write/rephrase', auth, requireUserAgent, handleRephrase);
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, handleV2GlossaryCreate.bind(null));
app.get('/v2/glossaries', auth, requireUserAgent, handleV2GlossaryList.bind(null));
app.get('/v2/glossaries/:glossary_id', auth, requireUserAgent, handleV2GlossaryList.bind(null));
app.get('/v2/glossaries/:glossary_id/entries', auth, requireUserAgent, handleV2GlossaryEntries.bind(null));
app.delete('/v2/glossaries/:glossary_id', auth, requireUserAgent, handleV2GlossaryDelete.bind(null));
// Maximum glossary size is 10MiB, but there is some extra request overhead
app.use('/v3/glossaries', express.json({ limit: '11mb' }));
app.post('/v3/glossaries', auth, requireUserAgent, handleV3GlossaryCreate.bind(null));
app.get('/v3/glossaries', auth, requireUserAgent, handleV3GlossaryList.bind(null));
app.get('/v3/glossaries/:glossary_id', auth, requireUserAgent, handleV3GlossaryList.bind(null));
app.get('/v3/glossaries/:glossary_id/entries', auth, requireUserAgent, handleV3GlossaryEntries.bind(null));
app.delete('/v3/glossaries/:glossary_id', auth, requireUserAgent, handleV3GlossaryDelete.bind(null));
app.delete('/v3/glossaries/:glossary_id/dictionaries', auth, requireUserAgent, handleDictionaryDelete);
app.patch('/v3/glossaries/:glossary_id', auth, requireUserAgent, handleGlossaryPatch);
app.put('/v3/glossaries/:glossary_id/dictionaries', auth, requireUserAgent, handleDictionaryPut);
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}`);
});
}