anote-server-libs 0.11.8 → 0.12.0

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.
@@ -1,161 +1,260 @@
1
- import {NextFunction, Request, Response} from 'express';
2
- import {Logger} from 'winston';
3
- import {BaseModelRepository} from '../models/repository/BaseModelRepository';
4
- import {jsonStringify, utils} from './utils';
5
-
6
- export const enum SystemLock {
7
- CHECK_CROSSING = 1,
8
- FLUSH_CALLS = 2,
9
- TX_CHECK = 3
10
- }
11
-
12
- export const withTransactionConfig = {
13
- logQueries: false,
14
- timeoutMillis: 15000
15
- };
16
-
17
- export function withTransaction(repo: BaseModelRepository, logger: Logger, previousMethod: (req: Request, res: Response, next: NextFunction) => void, lock?: SystemLock, commitIfLost = true) {
18
- return function(req: Request, res: Response, next: NextFunction) {
19
- let commit = true;
20
- if(!commitIfLost) {
21
- res.once('close', () => commit = false);
22
- }
23
- const endTerminator = res.end.bind(res);
24
- const jsonTerminator = (obj: any) => {
25
- try { res.set('Content-Type', 'application/json'); } catch(_) {}
26
- res.write(jsonStringify(obj) || '{}');
27
- endTerminator();
28
- };
29
- const connectTimeoutHandler = setTimeout(() => {
30
- // Timed out getting a client, restart worker or process...
31
- logger.error('Error timed out getting a client, exiting...');
32
- process.exit(22);
33
- }, withTransactionConfig.timeoutMillis);
34
- Promise.all([
35
- repo.db ? repo.db.connect() : Promise.resolve(undefined),
36
- repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
37
- ]).then(([c1, c2]) => {
38
- const dbClient = c1 || c2;
39
- clearTimeout(connectTimeoutHandler);
40
- // On error, will rollback...
41
- utils.logger = logger;
42
- if(withTransactionConfig.logQueries && !dbClient.queryPatched) {
43
- const originalQuery = dbClient.query.bind(dbClient);
44
- dbClient.query = (...args: any[]) => {
45
- logger.debug('SQL [Client %d] QUERY: %s', dbClient.processID, args[0]);
46
- return originalQuery(...args);
47
- };
48
- dbClient.queryPatched = true;
49
- }
50
- dbClient.removeAllListeners('error');
51
- dbClient.on('error', (err: any) => utils.clientErrorHandler(err, dbClient));
52
-
53
- res.locals.dbClient = dbClient;
54
- res.locals.dbClientCommited = false;
55
- res.locals.dbClientCommit = (cb: (err: any) => any) => {
56
- if(!res.locals.dbClientCommited) {
57
- res.locals.dbClientCommited = true;
58
- (commit ? (repo.db ? dbClient.query('COMMIT') : dbClient.commit()) : Promise.reject(new Error('Client lost'))).catch((err: any) => err).then((err: any) => {
59
- if(repo.db) dbClient.release();
60
- if(!(err instanceof Error)) {
61
- for(let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
62
- res.locals.dbClientOnCommit[i]();
63
- }
64
- }
65
- cb(err instanceof Error ? err : undefined);
66
- });
67
- } else {
68
- cb(undefined);
69
- }
70
- };
71
- res.locals.dbClientOnCommit = [];
72
- return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
73
- const finish = () => {
74
- res.send = () => {
75
- throw new Error('res.send() should not be used within transactions. Use res.json() instead.');
76
- };
77
- res.json = (obj: any) => {
78
- if(res.statusCode > 303 && res.statusCode !== 412) {
79
- if(logger) {
80
- if(res.statusCode > 499) {
81
- logger.error('Uncaught 500: %j', obj?.error?.additionalInfo);
82
- } else {
83
- logger.warn('Client error 4XX: %j', obj?.error);
84
- }
85
- }
86
- (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err: any) => obj && obj.error && (obj.error.additionalInfo2 = {message: err.message})).then(() => {
87
- if(repo.db) dbClient.release();
88
- jsonTerminator(obj);
89
- });
90
- } else {
91
- res.locals.dbClientCommit((err: any) => {
92
- if(err && commitIfLost) {
93
- res.status(500);
94
- jsonTerminator({
95
- error: {
96
- errorKey: 'internal.db',
97
- additionalInfo: {message: err.message}
98
- }
99
- });
100
- } else jsonTerminator(obj);
101
- });
102
- }
103
- return res;
104
- };
105
- res.end = () => {
106
- if(res.statusCode > 303 && res.statusCode !== 412) {
107
- if(logger && res.statusCode > 499) {
108
- logger.error('Uncaught 500 with no details...');
109
- }
110
- (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((): any => undefined).then(() => {
111
- if(repo.db) dbClient.release();
112
- endTerminator();
113
- });
114
- } else {
115
- res.locals.dbClientCommit((err: any) => {
116
- if(err && commitIfLost) {
117
- res.status(500);
118
- jsonTerminator({
119
- error: {
120
- errorKey: 'internal.db',
121
- additionalInfo: {message: err.message}
122
- }
123
- });
124
- } else endTerminator();
125
- });
126
- }
127
- return res;
128
- };
129
- return previousMethod.call(this, req, res, next);
130
- };
131
-
132
- if(lock) {
133
- dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err: any) => {
134
- res.status(500).json({
135
- error: {
136
- errorKey: 'internal.db',
137
- additionalInfo: {message: err.message}
138
- }
139
- });
140
- dbClient.release(); // Lock acquisition failed, release will also rollback
141
- });
142
- } else {
143
- finish();
144
- }
145
- }).catch((err: any) => {
146
- // Error beginning transaction
147
- dbClient.release();
148
- throw err;
149
- });
150
- }).catch(err => {
151
- // Error connecting to database for other reason than timeout or beginning transaction
152
- clearTimeout(connectTimeoutHandler);
153
- res.status(500).json({
154
- error: {
155
- errorKey: 'internal.db',
156
- additionalInfo: {message: err.message}
157
- }
158
- });
159
- });
160
- };
161
- }
1
+ import {NextFunction, Request, Response} from 'express';
2
+ import {Logger} from 'winston';
3
+ import {BaseModelRepository} from '../models/repository/BaseModelRepository';
4
+ import {jsonStringify, utils} from './utils';
5
+
6
+ export const enum SystemLock {
7
+ CHECK_CROSSING = 1,
8
+ FLUSH_CALLS = 2,
9
+ TX_CHECK = 3
10
+ }
11
+
12
+ export const withTransactionConfig = {
13
+ logQueries: false,
14
+ timeoutMillis: 15000,
15
+ // Number of currently active transactions tracked for gating.
16
+ activeCount: 0,
17
+ // Whether a gate has been requested (waiting for activeCount to reach 0).
18
+ gateRequested: false,
19
+ // Whether the gate is active (new transactions are queued).
20
+ gateActive: false,
21
+ // Queue of pending transactions waiting for the gate to release.
22
+ gateQueue: [] as Array<{resolve: () => void; reject: (err: Error) => void}>,
23
+ };
24
+
25
+ /**
26
+ * Request a transaction gate. Returns a Promise that resolves when all currently
27
+ * active transactions have completed (drained). While waiting and after resolution,
28
+ * new withTransaction calls will be queued until releaseTransactionGate() is called.
29
+ *
30
+ * Use this before starting a long-running operation to ensure no other transactions
31
+ * are killed by PostgreSQL's idle_in_transaction_session_timeout while the long
32
+ * operation holds locks or occupies the connection pool.
33
+ *
34
+ * @returns A Promise that resolves when the gate is acquired.
35
+ */
36
+ export function requestTransactionGate(): Promise<void> {
37
+ if(withTransactionConfig.gateActive || withTransactionConfig.gateRequested) {
38
+ return Promise.reject(new Error('Transaction gate already active or requested'));
39
+ }
40
+ withTransactionConfig.gateRequested = true;
41
+ return new Promise<void>((resolve, _reject) => {
42
+ const check = () => {
43
+ // Allow the calling transaction to remain active (activeCount <= 1).
44
+ // When called from inside a withTransaction handler, the current
45
+ // transaction counts as 1. When called from outside (e.g. cron),
46
+ // activeCount may be 0.
47
+ if(withTransactionConfig.activeCount <= 1) {
48
+ withTransactionConfig.gateActive = true;
49
+ withTransactionConfig.gateRequested = false;
50
+ resolve();
51
+ } else {
52
+ // Poll every 100ms until other transactions drain
53
+ setTimeout(check, 100);
54
+ }
55
+ };
56
+ // Small initial delay to let in-flight transactions settle
57
+ setTimeout(check, 50);
58
+ });
59
+ }
60
+
61
+ /**
62
+ * Release the transaction gate, allowing all queued transactions to proceed.
63
+ * Must be called after the long-running operation completes, in both success
64
+ * and error paths.
65
+ */
66
+ export function releaseTransactionGate(): void {
67
+ withTransactionConfig.gateActive = false;
68
+ withTransactionConfig.gateRequested = false;
69
+ // Drain the queue — resolve all pending waiters
70
+ const queue = withTransactionConfig.gateQueue.splice(0);
71
+ for(const entry of queue) {
72
+ entry.resolve();
73
+ }
74
+ }
75
+
76
+ function releaseAndDecrement(repo: BaseModelRepository, dbClient: any) {
77
+ if(repo.db && dbClient) {
78
+ try { dbClient.release(); } catch (_) { /* already released */ }
79
+ }
80
+ withTransactionConfig.activeCount = Math.max(0, withTransactionConfig.activeCount - 1);
81
+ }
82
+
83
+ export function withTransaction(repo: BaseModelRepository, logger: Logger, previousMethod: (req: Request, res: Response, next: NextFunction) => void,
84
+ lock?: SystemLock, commitIfLost = true, idleInTransactionTimeout?: number): (req: Request, res: Response, next: NextFunction) => any {
85
+ return function(this: any, req: Request, res: Response, next: NextFunction): any {
86
+ // If gate is active, queue this request and retry when gate is released
87
+ if(withTransactionConfig.gateActive) {
88
+ return new Promise<void>((resolve, reject) => {
89
+ withTransactionConfig.gateQueue.push({ resolve, reject });
90
+ }).then(() => {
91
+ // Re-enter with gate now released
92
+ return withTransaction(repo, logger, previousMethod, lock, commitIfLost)(req, res, next);
93
+ });
94
+ }
95
+
96
+ let commit = true;
97
+ if(!commitIfLost) {
98
+ res.once('close', () => commit = false);
99
+ }
100
+ const endTerminator = res.end.bind(res);
101
+ const jsonTerminator = (obj: any) => {
102
+ try { res.set('Content-Type', 'application/json'); } catch(_) {}
103
+ res.write(jsonStringify(obj) || '{}');
104
+ endTerminator();
105
+ };
106
+
107
+ // Increment activeCount before attempting to connect to the database
108
+ withTransactionConfig.activeCount++;
109
+
110
+ const connectTimeoutHandler = setTimeout(() => {
111
+ // Timed out getting a client, restart worker or process...
112
+ logger.error('Error timed out getting a client, exiting...');
113
+ process.exit(22);
114
+ }, withTransactionConfig.timeoutMillis);
115
+ Promise.all([
116
+ repo.db ? repo.db.connect() : Promise.resolve(undefined),
117
+ repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
118
+ ]).then(([c1, c2]) => {
119
+ const dbClient = c1 || c2;
120
+ clearTimeout(connectTimeoutHandler);
121
+ // On error, will rollback...
122
+ utils.logger = logger;
123
+ if(withTransactionConfig.logQueries && !dbClient.queryPatched) {
124
+ const originalQuery = dbClient.query.bind(dbClient);
125
+ dbClient.query = (...args: any[]) => {
126
+ logger.debug('SQL [Client %d] QUERY: %s', dbClient.processID, args[0]);
127
+ return originalQuery(...args);
128
+ };
129
+ dbClient.queryPatched = true;
130
+ }
131
+ dbClient.removeAllListeners('error');
132
+ dbClient.on('error', (err: any) => utils.clientErrorHandler(err, dbClient));
133
+
134
+ res.locals.dbClient = dbClient;
135
+ res.locals.dbClientCommited = false;
136
+ res.locals.dbClientCommit = (cb: (err: any) => any) => {
137
+ if(!res.locals.dbClientCommited) {
138
+ res.locals.dbClientCommited = true;
139
+ (commit ? (repo.db ? dbClient.query('COMMIT') : dbClient.commit()) : Promise.reject(new Error('Client lost'))).catch((err: any) => err).then((err: any) => {
140
+ releaseAndDecrement(repo, dbClient);
141
+ if(!(err instanceof Error)) {
142
+ for(let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
143
+ res.locals.dbClientOnCommit[i]();
144
+ }
145
+ }
146
+ cb(err instanceof Error ? err : undefined);
147
+ });
148
+ } else {
149
+ cb(undefined);
150
+ }
151
+ };
152
+ res.locals.dbClientOnCommit = [];
153
+
154
+ // Build the BEGIN + idle_in_transaction_session_timeout setup
155
+ const beginPromise = repo.db ? dbClient.query('BEGIN') : dbClient.begin();
156
+ const idleTimeoutMs = Math.max(idleInTransactionTimeout, withTransactionConfig.timeoutMillis);
157
+ let setupPromise: Promise<any> = beginPromise;
158
+ if(idleInTransactionTimeout && idleTimeoutMs > 0 && repo.db) {
159
+ setupPromise = setupPromise.then(() => {
160
+ return dbClient.query(`SET LOCAL idle_in_transaction_session_timeout TO ${idleTimeoutMs}`);
161
+ });
162
+ }
163
+
164
+ return setupPromise.then(() => {
165
+ const finish = () => {
166
+ res.send = () => {
167
+ throw new Error('res.send() should not be used within transactions. Use res.json() instead.');
168
+ };
169
+ res.json = (obj: any) => {
170
+ if(res.statusCode > 303 && res.statusCode !== 412) {
171
+ if(logger) {
172
+ if(res.statusCode > 499) {
173
+ logger.error('Uncaught 500: %j', obj?.error?.additionalInfo);
174
+ } else {
175
+ logger.warn('Client error 4XX: %j', obj?.error);
176
+ }
177
+ }
178
+ (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err: any) => obj && obj.error && (obj.error.additionalInfo2 = {message: err.message})).then(() => {
179
+ releaseAndDecrement(repo, dbClient);
180
+ jsonTerminator(obj);
181
+ });
182
+ } else {
183
+ res.locals.dbClientCommit((err: any) => {
184
+ if(err && commitIfLost) {
185
+ res.status(500);
186
+ jsonTerminator({
187
+ error: {
188
+ errorKey: 'internal.db',
189
+ additionalInfo: {message: err.message}
190
+ }
191
+ });
192
+ } else jsonTerminator(obj);
193
+ });
194
+ }
195
+ return res;
196
+ };
197
+ res.end = () => {
198
+ if(res.statusCode > 303 && res.statusCode !== 412) {
199
+ if(logger && res.statusCode > 499) {
200
+ logger.error('Uncaught 500 with no details...');
201
+ }
202
+ (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((): any => undefined).then(() => {
203
+ releaseAndDecrement(repo, dbClient);
204
+ endTerminator();
205
+ });
206
+ } else {
207
+ res.locals.dbClientCommit((err: any) => {
208
+ if(err && commitIfLost) {
209
+ res.status(500);
210
+ jsonTerminator({
211
+ error: {
212
+ errorKey: 'internal.db',
213
+ additionalInfo: {message: err.message}
214
+ }
215
+ });
216
+ } else endTerminator();
217
+ });
218
+ }
219
+ return res;
220
+ };
221
+ return previousMethod.call(this, req, res, next);
222
+ };
223
+
224
+ if(lock) {
225
+ dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err: any) => {
226
+ res.status(500).json({
227
+ error: {
228
+ errorKey: 'internal.db',
229
+ additionalInfo: {message: err.message}
230
+ }
231
+ });
232
+ releaseAndDecrement(repo, dbClient); // Lock acquisition failed, release will also rollback
233
+ });
234
+ } else {
235
+ finish();
236
+ }
237
+ }).catch((err: any) => {
238
+ // Error beginning transaction
239
+ releaseAndDecrement(repo, dbClient);
240
+ logger.error('Error beginning transaction: %j', err);
241
+ res.status(500).json({
242
+ error: {
243
+ errorKey: 'internal.db',
244
+ additionalInfo: {message: err.message}
245
+ }
246
+ });
247
+ });
248
+ }).catch(err => {
249
+ // Error connecting to database for other reason than timeout or beginning transaction
250
+ clearTimeout(connectTimeoutHandler);
251
+ withTransactionConfig.activeCount = Math.max(0, withTransactionConfig.activeCount - 1);
252
+ res.status(500).json({
253
+ error: {
254
+ errorKey: 'internal.db',
255
+ additionalInfo: {message: err.message}
256
+ }
257
+ });
258
+ });
259
+ };
260
+ }
package/services/utils.js CHANGED
@@ -147,22 +147,22 @@ function idempotent(repo, debug, logger) {
147
147
  };
148
148
  }
149
149
  function sendSelfPostableMessage(res, code, messageType, err) {
150
- res.type('text/html').status(code).write(`
151
- <!DOCTYPE HTML>
152
- <html>
153
- <head>
154
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
155
- </head>
156
- <body>
157
- <script type="text/javascript">
158
- window.parent.postMessage({
159
- type: '${messageType}',
160
- confirm: ${!err},
161
- error: JSON.parse('${JSON.stringify(err) || 'null'}')
162
- }, '*');
163
- </script>
164
- </body>
165
- </html>
150
+ res.type('text/html').status(code).write(`
151
+ <!DOCTYPE HTML>
152
+ <html>
153
+ <head>
154
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
155
+ </head>
156
+ <body>
157
+ <script type="text/javascript">
158
+ window.parent.postMessage({
159
+ type: '${messageType}',
160
+ confirm: ${!err},
161
+ error: JSON.parse('${JSON.stringify(err) || 'null'}')
162
+ }, '*');
163
+ </script>
164
+ </body>
165
+ </html>
166
166
  `);
167
167
  res.end();
168
168
  }
@@ -261,15 +261,15 @@ function getHtmlReplaced(config, templates, key, lang, translations, data = {},
261
261
  };
262
262
  let html = getHtml(templates[key], merged);
263
263
  if (newPage) {
264
- html = html.replace('</head>', `<style>
265
- @media print {
266
- .new-page {
267
- page-break-before: always;
268
- }
269
- }
270
- </style>
264
+ html = html.replace('</head>', `<style>
265
+ @media print {
266
+ .new-page {
267
+ page-break-before: always;
268
+ }
269
+ }
270
+ </style>
271
271
  </head>`);
272
- html = html.replace('</body>', `<p class="new-page">Provided by Satoris</p>
272
+ html = html.replace('</body>', `<p class="new-page">Provided by Satoris</p>
273
273
  </body>`);
274
274
  }
275
275
  return html;
@@ -284,5 +284,5 @@ function getEmailOneSimpleValue(config, key, lang, translations, data = {}, dyna
284
284
  ...dynamicData
285
285
  };
286
286
  const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
287
- return merged[key]?.replace(pattern, (_, placeholderKey) => String(dynamicData[placeholderKey] ?? placeholderKey));
287
+ return merged[key]?.replace(pattern, (_, placeholderKey) => String(merged[placeholderKey] ?? placeholderKey));
288
288
  }