-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.js
41 lines (35 loc) · 1.1 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
// 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;
}
}
module.exports = { scheduleCleanup, HttpError };