anote-server-libs 0.4.8 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/models/ApiCall.js +37 -37
- package/models/Migration.js +35 -35
- package/models/repository/BaseModelRepository.js +156 -156
- package/models/repository/MemoryCache.js +92 -92
- package/models/repository/ModelDao.js +239 -239
- package/package.json +9 -9
- package/services/WithBody.js +63 -63
- package/services/WithTransaction.js +136 -136
- package/services/utils.js +181 -181
- package/tsconfig.json +1 -0
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.withTransaction = void 0;
|
|
4
|
-
const utils_1 = require("./utils");
|
|
5
|
-
function withTransaction(repo, logger, previousMethod, lock) {
|
|
6
|
-
return function (req, res, next) {
|
|
7
|
-
const endTerminator = res.end.bind(res);
|
|
8
|
-
const jsonTerminator = (obj) => {
|
|
9
|
-
res.write((0, utils_1.jsonStringify)(obj) || '{}');
|
|
10
|
-
endTerminator();
|
|
11
|
-
};
|
|
12
|
-
const connectTimeoutHandler = setTimeout(() => {
|
|
13
|
-
logger.error('Error timed out getting a client, exiting...');
|
|
14
|
-
process.exit(22);
|
|
15
|
-
}, 15000);
|
|
16
|
-
Promise.all([
|
|
17
|
-
repo.db ? repo.db.connect() : Promise.resolve(undefined),
|
|
18
|
-
repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
|
|
19
|
-
]).then(([c1, c2]) => {
|
|
20
|
-
const dbClient = c1 || c2;
|
|
21
|
-
clearTimeout(connectTimeoutHandler);
|
|
22
|
-
utils_1.utils.logger = logger;
|
|
23
|
-
dbClient.removeListener('error', utils_1.utils.clientErrorHandler);
|
|
24
|
-
dbClient.on('error', utils_1.utils.clientErrorHandler);
|
|
25
|
-
res.locals.dbClient = dbClient;
|
|
26
|
-
res.locals.dbClientCommited = false;
|
|
27
|
-
res.locals.dbClientCommit = (cb) => {
|
|
28
|
-
if (!res.locals.dbClientCommited) {
|
|
29
|
-
res.locals.dbClientCommited = true;
|
|
30
|
-
(repo.db ? dbClient.query('COMMIT') : dbClient.commit()).catch((err) => err).then((err) => {
|
|
31
|
-
if (repo.db)
|
|
32
|
-
dbClient.release();
|
|
33
|
-
if (!(err instanceof Error)) {
|
|
34
|
-
for (let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
|
|
35
|
-
res.locals.dbClientOnCommit[i]();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
cb(err instanceof Error ? err : undefined);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
cb(undefined);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
res.locals.dbClientOnCommit = [];
|
|
46
|
-
return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
|
|
47
|
-
const finish = () => {
|
|
48
|
-
res.json = (obj) => {
|
|
49
|
-
if (res.statusCode > 303 && res.statusCode !== 412) {
|
|
50
|
-
if (logger) {
|
|
51
|
-
if (res.statusCode > 499) {
|
|
52
|
-
logger.error('Uncaught 500: %j', obj.error.additionalInfo);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
logger.warn('Client error 4XX: %j', obj.error);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj.error.additionalInfo2 = { message: err.message }).then(() => {
|
|
59
|
-
if (repo.db)
|
|
60
|
-
dbClient.release();
|
|
61
|
-
jsonTerminator(obj);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
res.locals.dbClientCommit((err) => {
|
|
66
|
-
if (err) {
|
|
67
|
-
res.status(500);
|
|
68
|
-
jsonTerminator({
|
|
69
|
-
error: {
|
|
70
|
-
errorKey: 'internal.db',
|
|
71
|
-
additionalInfo: { message: err.message }
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
else
|
|
76
|
-
jsonTerminator(obj);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
return res;
|
|
80
|
-
};
|
|
81
|
-
res.end = () => {
|
|
82
|
-
if (res.statusCode > 303 && res.statusCode !== 412) {
|
|
83
|
-
if (logger && res.statusCode > 499) {
|
|
84
|
-
logger.error('Uncaught 500 with no details...');
|
|
85
|
-
}
|
|
86
|
-
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
|
|
87
|
-
if (repo.db)
|
|
88
|
-
dbClient.release();
|
|
89
|
-
endTerminator();
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
res.locals.dbClientCommit((err) => {
|
|
94
|
-
if (err) {
|
|
95
|
-
res.status(500);
|
|
96
|
-
jsonTerminator({
|
|
97
|
-
error: {
|
|
98
|
-
errorKey: 'internal.db',
|
|
99
|
-
additionalInfo: { message: err.message }
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
else
|
|
104
|
-
endTerminator();
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
return res;
|
|
108
|
-
};
|
|
109
|
-
return previousMethod.call(this, req, res, next);
|
|
110
|
-
};
|
|
111
|
-
if (lock) {
|
|
112
|
-
dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
|
|
113
|
-
res.status(500).json({
|
|
114
|
-
error: {
|
|
115
|
-
errorKey: 'internal.db',
|
|
116
|
-
additionalInfo: { message: err.message }
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
dbClient.release();
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
finish();
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
}).catch(err => {
|
|
127
|
-
res.status(500).json({
|
|
128
|
-
error: {
|
|
129
|
-
errorKey: 'internal.db',
|
|
130
|
-
additionalInfo: { message: err.message }
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
exports.withTransaction = withTransaction;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withTransaction = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
function withTransaction(repo, logger, previousMethod, lock) {
|
|
6
|
+
return function (req, res, next) {
|
|
7
|
+
const endTerminator = res.end.bind(res);
|
|
8
|
+
const jsonTerminator = (obj) => {
|
|
9
|
+
res.write((0, utils_1.jsonStringify)(obj) || '{}');
|
|
10
|
+
endTerminator();
|
|
11
|
+
};
|
|
12
|
+
const connectTimeoutHandler = setTimeout(() => {
|
|
13
|
+
logger.error('Error timed out getting a client, exiting...');
|
|
14
|
+
process.exit(22);
|
|
15
|
+
}, 15000);
|
|
16
|
+
Promise.all([
|
|
17
|
+
repo.db ? repo.db.connect() : Promise.resolve(undefined),
|
|
18
|
+
repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
|
|
19
|
+
]).then(([c1, c2]) => {
|
|
20
|
+
const dbClient = c1 || c2;
|
|
21
|
+
clearTimeout(connectTimeoutHandler);
|
|
22
|
+
utils_1.utils.logger = logger;
|
|
23
|
+
dbClient.removeListener('error', utils_1.utils.clientErrorHandler);
|
|
24
|
+
dbClient.on('error', utils_1.utils.clientErrorHandler);
|
|
25
|
+
res.locals.dbClient = dbClient;
|
|
26
|
+
res.locals.dbClientCommited = false;
|
|
27
|
+
res.locals.dbClientCommit = (cb) => {
|
|
28
|
+
if (!res.locals.dbClientCommited) {
|
|
29
|
+
res.locals.dbClientCommited = true;
|
|
30
|
+
(repo.db ? dbClient.query('COMMIT') : dbClient.commit()).catch((err) => err).then((err) => {
|
|
31
|
+
if (repo.db)
|
|
32
|
+
dbClient.release();
|
|
33
|
+
if (!(err instanceof Error)) {
|
|
34
|
+
for (let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
|
|
35
|
+
res.locals.dbClientOnCommit[i]();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
cb(err instanceof Error ? err : undefined);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
cb(undefined);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
res.locals.dbClientOnCommit = [];
|
|
46
|
+
return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
|
|
47
|
+
const finish = () => {
|
|
48
|
+
res.json = (obj) => {
|
|
49
|
+
if (res.statusCode > 303 && res.statusCode !== 412) {
|
|
50
|
+
if (logger) {
|
|
51
|
+
if (res.statusCode > 499) {
|
|
52
|
+
logger.error('Uncaught 500: %j', obj.error.additionalInfo);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
logger.warn('Client error 4XX: %j', obj.error);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj.error.additionalInfo2 = { message: err.message }).then(() => {
|
|
59
|
+
if (repo.db)
|
|
60
|
+
dbClient.release();
|
|
61
|
+
jsonTerminator(obj);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
res.locals.dbClientCommit((err) => {
|
|
66
|
+
if (err) {
|
|
67
|
+
res.status(500);
|
|
68
|
+
jsonTerminator({
|
|
69
|
+
error: {
|
|
70
|
+
errorKey: 'internal.db',
|
|
71
|
+
additionalInfo: { message: err.message }
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else
|
|
76
|
+
jsonTerminator(obj);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return res;
|
|
80
|
+
};
|
|
81
|
+
res.end = () => {
|
|
82
|
+
if (res.statusCode > 303 && res.statusCode !== 412) {
|
|
83
|
+
if (logger && res.statusCode > 499) {
|
|
84
|
+
logger.error('Uncaught 500 with no details...');
|
|
85
|
+
}
|
|
86
|
+
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
|
|
87
|
+
if (repo.db)
|
|
88
|
+
dbClient.release();
|
|
89
|
+
endTerminator();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
res.locals.dbClientCommit((err) => {
|
|
94
|
+
if (err) {
|
|
95
|
+
res.status(500);
|
|
96
|
+
jsonTerminator({
|
|
97
|
+
error: {
|
|
98
|
+
errorKey: 'internal.db',
|
|
99
|
+
additionalInfo: { message: err.message }
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else
|
|
104
|
+
endTerminator();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return res;
|
|
108
|
+
};
|
|
109
|
+
return previousMethod.call(this, req, res, next);
|
|
110
|
+
};
|
|
111
|
+
if (lock) {
|
|
112
|
+
dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
|
|
113
|
+
res.status(500).json({
|
|
114
|
+
error: {
|
|
115
|
+
errorKey: 'internal.db',
|
|
116
|
+
additionalInfo: { message: err.message }
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
dbClient.release();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
finish();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}).catch(err => {
|
|
127
|
+
res.status(500).json({
|
|
128
|
+
error: {
|
|
129
|
+
errorKey: 'internal.db',
|
|
130
|
+
additionalInfo: { message: err.message }
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
exports.withTransaction = withTransaction;
|
package/services/utils.js
CHANGED
|
@@ -1,145 +1,145 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.digitize = exports.fpEuros = exports.sendSelfPostableMessage = exports.idempotent = exports.jsonStringify = exports.lcm = exports.gcd = exports.utils = exports.clientErrorHandle = exports.btoa = exports.atob = void 0;
|
|
4
|
-
function atob(str) {
|
|
5
|
-
return Buffer.from(str, 'base64').toString('binary');
|
|
6
|
-
}
|
|
7
|
-
exports.atob = atob;
|
|
8
|
-
function btoa(str) {
|
|
9
|
-
return Buffer.from(str).toString('base64');
|
|
10
|
-
}
|
|
11
|
-
exports.btoa = btoa;
|
|
12
|
-
function clientErrorHandle(err) {
|
|
13
|
-
this.error('Error on DB client: %j', err);
|
|
14
|
-
}
|
|
15
|
-
exports.clientErrorHandle = clientErrorHandle;
|
|
16
|
-
exports.utils = {
|
|
17
|
-
clientErrorHandler: undefined
|
|
18
|
-
};
|
|
19
|
-
function gcdTwo(a, b) {
|
|
20
|
-
if (a === 0)
|
|
21
|
-
return b;
|
|
22
|
-
return gcdTwo(b % a, a);
|
|
23
|
-
}
|
|
24
|
-
function gcd(values) {
|
|
25
|
-
let result = values[0];
|
|
26
|
-
for (let i = 1; i < values.length; i++)
|
|
27
|
-
result = gcdTwo(values[i], result);
|
|
28
|
-
return result;
|
|
29
|
-
}
|
|
30
|
-
exports.gcd = gcd;
|
|
31
|
-
function lcm(values) {
|
|
32
|
-
let l = 1, divisor = 2;
|
|
33
|
-
while (true) {
|
|
34
|
-
let counter = 0;
|
|
35
|
-
let divisible = false;
|
|
36
|
-
for (let i = 0; i < values.length; i++) {
|
|
37
|
-
if (values[i] === 0) {
|
|
38
|
-
return 0;
|
|
39
|
-
}
|
|
40
|
-
else if (values[i] < 0) {
|
|
41
|
-
values[i] = values[i] * (-1);
|
|
42
|
-
}
|
|
43
|
-
if (values[i] === 1) {
|
|
44
|
-
counter++;
|
|
45
|
-
}
|
|
46
|
-
if (values[i] % divisor === 0) {
|
|
47
|
-
divisible = true;
|
|
48
|
-
values[i] = values[i] / divisor;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (divisible) {
|
|
52
|
-
l = l * divisor;
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
divisor++;
|
|
56
|
-
}
|
|
57
|
-
if (counter === values.length) {
|
|
58
|
-
return l;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.lcm = lcm;
|
|
63
|
-
function jsonStringify(obj) {
|
|
64
|
-
const cache = {};
|
|
65
|
-
return JSON.stringify(obj, function (_, value) {
|
|
66
|
-
if (typeof value === 'object' && value !== null) {
|
|
67
|
-
if (cache[value] !== -1) {
|
|
68
|
-
try {
|
|
69
|
-
return JSON.parse(JSON.stringify(value));
|
|
70
|
-
}
|
|
71
|
-
catch (error) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
cache[value] = true;
|
|
76
|
-
}
|
|
77
|
-
return value;
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
exports.jsonStringify = jsonStringify;
|
|
81
|
-
function idempotent(repo, debug, logger) {
|
|
82
|
-
return function (req, res, next) {
|
|
83
|
-
let idempotenceKey = req.header('x-idempotent-key');
|
|
84
|
-
if (idempotenceKey) {
|
|
85
|
-
idempotenceKey = idempotenceKey.substring(0, 40);
|
|
86
|
-
repo.ApiCall.get(idempotenceKey).then(call => {
|
|
87
|
-
if (!call) {
|
|
88
|
-
const jsonTerminator = res.json;
|
|
89
|
-
const endTerminator = res.end;
|
|
90
|
-
const writeTerminator = res.write;
|
|
91
|
-
let response = '';
|
|
92
|
-
res.json = (function (obj) {
|
|
93
|
-
repo.ApiCall.create({
|
|
94
|
-
id: idempotenceKey,
|
|
95
|
-
responseCode: res.statusCode,
|
|
96
|
-
responseJson: jsonStringify(obj),
|
|
97
|
-
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
98
|
-
}).then(() => jsonTerminator.call(res, obj), err => {
|
|
99
|
-
if (err)
|
|
100
|
-
logger.warn('Cannot save idempotent key: %j', err);
|
|
101
|
-
jsonTerminator.call(res, obj);
|
|
102
|
-
});
|
|
103
|
-
}).bind(res);
|
|
104
|
-
res.end = (function (buf) {
|
|
105
|
-
if (buf)
|
|
106
|
-
response = response + buf.toString();
|
|
107
|
-
repo.ApiCall.create({
|
|
108
|
-
id: idempotenceKey,
|
|
109
|
-
responseCode: res.statusCode,
|
|
110
|
-
responseJson: response,
|
|
111
|
-
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
112
|
-
}).then(() => endTerminator.call(res, buf), err => {
|
|
113
|
-
if (err)
|
|
114
|
-
logger.warn('Cannot save idempotent key: %j', err);
|
|
115
|
-
endTerminator.call(res, buf);
|
|
116
|
-
});
|
|
117
|
-
}).bind(res);
|
|
118
|
-
res.write = (function (buf) {
|
|
119
|
-
if (buf)
|
|
120
|
-
response = response + buf.toString();
|
|
121
|
-
writeTerminator.call(res, buf);
|
|
122
|
-
}).bind(res);
|
|
123
|
-
next();
|
|
124
|
-
}
|
|
125
|
-
else
|
|
126
|
-
res.status(417).json({
|
|
127
|
-
responseCode: call.responseCode,
|
|
128
|
-
responseBody: call.responseJson
|
|
129
|
-
});
|
|
130
|
-
}, err => res.status(500).json({
|
|
131
|
-
error: {
|
|
132
|
-
errorKey: 'internal.db',
|
|
133
|
-
additionalInfo: { message: err.message, stack: debug && err.stack }
|
|
134
|
-
}
|
|
135
|
-
}));
|
|
136
|
-
}
|
|
137
|
-
else
|
|
138
|
-
next();
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
exports.idempotent = idempotent;
|
|
142
|
-
function sendSelfPostableMessage(res, code, messageType, err) {
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.digitize = exports.fpEuros = exports.sendSelfPostableMessage = exports.idempotent = exports.jsonStringify = exports.lcm = exports.gcd = exports.utils = exports.clientErrorHandle = exports.btoa = exports.atob = void 0;
|
|
4
|
+
function atob(str) {
|
|
5
|
+
return Buffer.from(str, 'base64').toString('binary');
|
|
6
|
+
}
|
|
7
|
+
exports.atob = atob;
|
|
8
|
+
function btoa(str) {
|
|
9
|
+
return Buffer.from(str).toString('base64');
|
|
10
|
+
}
|
|
11
|
+
exports.btoa = btoa;
|
|
12
|
+
function clientErrorHandle(err) {
|
|
13
|
+
this.error('Error on DB client: %j', err);
|
|
14
|
+
}
|
|
15
|
+
exports.clientErrorHandle = clientErrorHandle;
|
|
16
|
+
exports.utils = {
|
|
17
|
+
clientErrorHandler: undefined
|
|
18
|
+
};
|
|
19
|
+
function gcdTwo(a, b) {
|
|
20
|
+
if (a === 0)
|
|
21
|
+
return b;
|
|
22
|
+
return gcdTwo(b % a, a);
|
|
23
|
+
}
|
|
24
|
+
function gcd(values) {
|
|
25
|
+
let result = values[0];
|
|
26
|
+
for (let i = 1; i < values.length; i++)
|
|
27
|
+
result = gcdTwo(values[i], result);
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
exports.gcd = gcd;
|
|
31
|
+
function lcm(values) {
|
|
32
|
+
let l = 1, divisor = 2;
|
|
33
|
+
while (true) {
|
|
34
|
+
let counter = 0;
|
|
35
|
+
let divisible = false;
|
|
36
|
+
for (let i = 0; i < values.length; i++) {
|
|
37
|
+
if (values[i] === 0) {
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
else if (values[i] < 0) {
|
|
41
|
+
values[i] = values[i] * (-1);
|
|
42
|
+
}
|
|
43
|
+
if (values[i] === 1) {
|
|
44
|
+
counter++;
|
|
45
|
+
}
|
|
46
|
+
if (values[i] % divisor === 0) {
|
|
47
|
+
divisible = true;
|
|
48
|
+
values[i] = values[i] / divisor;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (divisible) {
|
|
52
|
+
l = l * divisor;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
divisor++;
|
|
56
|
+
}
|
|
57
|
+
if (counter === values.length) {
|
|
58
|
+
return l;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.lcm = lcm;
|
|
63
|
+
function jsonStringify(obj) {
|
|
64
|
+
const cache = {};
|
|
65
|
+
return JSON.stringify(obj, function (_, value) {
|
|
66
|
+
if (typeof value === 'object' && value !== null) {
|
|
67
|
+
if (cache[value] !== -1) {
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(JSON.stringify(value));
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
cache[value] = true;
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
exports.jsonStringify = jsonStringify;
|
|
81
|
+
function idempotent(repo, debug, logger) {
|
|
82
|
+
return function (req, res, next) {
|
|
83
|
+
let idempotenceKey = req.header('x-idempotent-key');
|
|
84
|
+
if (idempotenceKey) {
|
|
85
|
+
idempotenceKey = idempotenceKey.substring(0, 40);
|
|
86
|
+
repo.ApiCall.get(idempotenceKey).then(call => {
|
|
87
|
+
if (!call) {
|
|
88
|
+
const jsonTerminator = res.json;
|
|
89
|
+
const endTerminator = res.end;
|
|
90
|
+
const writeTerminator = res.write;
|
|
91
|
+
let response = '';
|
|
92
|
+
res.json = (function (obj) {
|
|
93
|
+
repo.ApiCall.create({
|
|
94
|
+
id: idempotenceKey,
|
|
95
|
+
responseCode: res.statusCode,
|
|
96
|
+
responseJson: jsonStringify(obj),
|
|
97
|
+
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
98
|
+
}).then(() => jsonTerminator.call(res, obj), err => {
|
|
99
|
+
if (err)
|
|
100
|
+
logger.warn('Cannot save idempotent key: %j', err);
|
|
101
|
+
jsonTerminator.call(res, obj);
|
|
102
|
+
});
|
|
103
|
+
}).bind(res);
|
|
104
|
+
res.end = (function (buf) {
|
|
105
|
+
if (buf)
|
|
106
|
+
response = response + buf.toString();
|
|
107
|
+
repo.ApiCall.create({
|
|
108
|
+
id: idempotenceKey,
|
|
109
|
+
responseCode: res.statusCode,
|
|
110
|
+
responseJson: response,
|
|
111
|
+
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
112
|
+
}).then(() => endTerminator.call(res, buf), err => {
|
|
113
|
+
if (err)
|
|
114
|
+
logger.warn('Cannot save idempotent key: %j', err);
|
|
115
|
+
endTerminator.call(res, buf);
|
|
116
|
+
});
|
|
117
|
+
}).bind(res);
|
|
118
|
+
res.write = (function (buf) {
|
|
119
|
+
if (buf)
|
|
120
|
+
response = response + buf.toString();
|
|
121
|
+
writeTerminator.call(res, buf);
|
|
122
|
+
}).bind(res);
|
|
123
|
+
next();
|
|
124
|
+
}
|
|
125
|
+
else
|
|
126
|
+
res.status(417).json({
|
|
127
|
+
responseCode: call.responseCode,
|
|
128
|
+
responseBody: call.responseJson
|
|
129
|
+
});
|
|
130
|
+
}, err => res.status(500).json({
|
|
131
|
+
error: {
|
|
132
|
+
errorKey: 'internal.db',
|
|
133
|
+
additionalInfo: { message: err.message, stack: debug && err.stack }
|
|
134
|
+
}
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
else
|
|
138
|
+
next();
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
exports.idempotent = idempotent;
|
|
142
|
+
function sendSelfPostableMessage(res, code, messageType, err) {
|
|
143
143
|
res.type('text/html').status(code).write(`
|
|
144
144
|
<!DOCTYPE HTML>
|
|
145
145
|
<html>
|
|
@@ -156,42 +156,42 @@ function sendSelfPostableMessage(res, code, messageType, err) {
|
|
|
156
156
|
</script>
|
|
157
157
|
</body>
|
|
158
158
|
</html>
|
|
159
|
-
`);
|
|
160
|
-
res.end();
|
|
161
|
-
}
|
|
162
|
-
exports.sendSelfPostableMessage = sendSelfPostableMessage;
|
|
163
|
-
function fpEuros(n) {
|
|
164
|
-
return Math.round(n * 100) / 100;
|
|
165
|
-
}
|
|
166
|
-
exports.fpEuros = fpEuros;
|
|
167
|
-
function digitize(value, opts) {
|
|
168
|
-
if (value === undefined || value === null)
|
|
169
|
-
return 'undefined';
|
|
170
|
-
if (typeof value === 'number') {
|
|
171
|
-
if (isNaN(value))
|
|
172
|
-
return '-';
|
|
173
|
-
if (!isFinite(value))
|
|
174
|
-
return 'Infinite';
|
|
175
|
-
value = String(value);
|
|
176
|
-
}
|
|
177
|
-
else if (typeof value === 'string')
|
|
178
|
-
return value;
|
|
179
|
-
const parts = value.split('.');
|
|
180
|
-
const initialLength = parts[0].length;
|
|
181
|
-
for (let i = initialLength - 3; i > 0; i -= 3) {
|
|
182
|
-
parts[0] = parts[0].slice(0, i) + ',' + parts[0].slice(i);
|
|
183
|
-
}
|
|
184
|
-
if (parts[0].startsWith('-,'))
|
|
185
|
-
parts[0] = '-' + parts[0].slice(2);
|
|
186
|
-
if (parts[1]) {
|
|
187
|
-
const expDecimals = parts[1].split(/[eE]-/);
|
|
188
|
-
if (expDecimals.length > 1 && parseInt(expDecimals[1], 10) > 2)
|
|
189
|
-
return '0.00';
|
|
190
|
-
let decimals = fpEuros(parseFloat('0.' + parts[1])).toString().substr(2, 2);
|
|
191
|
-
if (decimals.length === 1)
|
|
192
|
-
decimals += '0';
|
|
193
|
-
return parts[0] + '.' + decimals;
|
|
194
|
-
}
|
|
195
|
-
return (opts && opts.currency) ? (parts[0] + '.00') : parts[0];
|
|
196
|
-
}
|
|
197
|
-
exports.digitize = digitize;
|
|
159
|
+
`);
|
|
160
|
+
res.end();
|
|
161
|
+
}
|
|
162
|
+
exports.sendSelfPostableMessage = sendSelfPostableMessage;
|
|
163
|
+
function fpEuros(n) {
|
|
164
|
+
return Math.round(n * 100) / 100;
|
|
165
|
+
}
|
|
166
|
+
exports.fpEuros = fpEuros;
|
|
167
|
+
function digitize(value, opts) {
|
|
168
|
+
if (value === undefined || value === null)
|
|
169
|
+
return 'undefined';
|
|
170
|
+
if (typeof value === 'number') {
|
|
171
|
+
if (isNaN(value))
|
|
172
|
+
return '-';
|
|
173
|
+
if (!isFinite(value))
|
|
174
|
+
return 'Infinite';
|
|
175
|
+
value = String(value);
|
|
176
|
+
}
|
|
177
|
+
else if (typeof value === 'string')
|
|
178
|
+
return value;
|
|
179
|
+
const parts = value.split('.');
|
|
180
|
+
const initialLength = parts[0].length;
|
|
181
|
+
for (let i = initialLength - 3; i > 0; i -= 3) {
|
|
182
|
+
parts[0] = parts[0].slice(0, i) + ',' + parts[0].slice(i);
|
|
183
|
+
}
|
|
184
|
+
if (parts[0].startsWith('-,'))
|
|
185
|
+
parts[0] = '-' + parts[0].slice(2);
|
|
186
|
+
if (parts[1]) {
|
|
187
|
+
const expDecimals = parts[1].split(/[eE]-/);
|
|
188
|
+
if (expDecimals.length > 1 && parseInt(expDecimals[1], 10) > 2)
|
|
189
|
+
return '0.00';
|
|
190
|
+
let decimals = fpEuros(parseFloat('0.' + parts[1])).toString().substr(2, 2);
|
|
191
|
+
if (decimals.length === 1)
|
|
192
|
+
decimals += '0';
|
|
193
|
+
return parts[0] + '.' + decimals;
|
|
194
|
+
}
|
|
195
|
+
return (opts && opts.currency) ? (parts[0] + '.00') : parts[0];
|
|
196
|
+
}
|
|
197
|
+
exports.digitize = digitize;
|