-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.js
57 lines (50 loc) · 1.64 KB
/
util.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
// 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.
function cleanup(dictionary, lifetimeMs, callback) {
const now = new Date();
// Remove all objects with a used timestamp older than specified lifetime
dictionary.forEach((object, id) => {
if (now - object.used > lifetimeMs) {
callback(object, id);
dictionary.delete(id);
}
});
}
function scheduleCleanup(map, callback) {
// Schedules a repeated check for objects older than 10min in given map, callback is called
// for each expired object
setInterval(() => cleanup(map, 600000, callback), 1000);
}
class HttpError extends Error {
constructor(message, status, detail) {
super(message);
this.status_internal = status;
this.detail = detail;
}
status() {
return this.status_internal || 400;
}
body() {
const result = {};
if (this.message) { result.message = this.message; }
if (this.detail) { result.detail = this.detail; }
return result;
}
}
// This is a simplified implementation of this conversion that won't work for all
// BCP-47 codes, but all we currently use at DeepL.
// Before further complicating this implementation, it might make sense to switch
// to a library.
function convertToBcp47(langCode) {
let tokens = langCode.split('-');
const numTokens = tokens.length;
tokens = tokens.map((token, index) => {
if (index !== 0 && index === numTokens - 1) {
return token.toUpperCase();
}
return token.toLowerCase();
});
return tokens.join('-');
}
module.exports = { convertToBcp47, scheduleCleanup, HttpError };