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,65 +1,65 @@
1
- import {Request, Response} from 'express';
2
- import Ajv, {_} from 'ajv';
3
-
4
- export const ajv = new Ajv();
5
- ajv.addKeyword({
6
- keyword: 'maxDigits',
7
- type: 'number',
8
- schemaType: 'number',
9
- validate: (schema: number, data: any) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
10
- });
11
-
12
- export function WithBody(schema: any, collapseTopLevelNulls = true) {
13
- if(schema.type === 'object') {
14
- const required: string[] = [];
15
- const keys = Object.getOwnPropertyNames(schema.properties);
16
- keys.forEach(key => {
17
- if(schema.properties[key].required === true) {
18
- required.push(key);
19
- }
20
- if(schema.properties[key].required === true || schema.properties[key].required === false) {
21
- delete schema.properties[key].required;
22
- }
23
- });
24
- schema.required = required;
25
- }
26
- const validate = ajv.compile(schema);
27
- return function(_: any, __: string, descriptor: PropertyDescriptor) {
28
- if(typeof descriptor.value === 'function') {
29
- const previousMethod = descriptor.value;
30
- descriptor.value = function(this: any, req: Request, res: Response) {
31
- if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
32
- return previousMethod.call(this, req, res);
33
- }
34
- if(!req.body) {
35
- res.status(400).json({
36
- error: {
37
- errorKey: 'client.body.missing',
38
- additionalInfo: 'client.extended.badPayload'
39
- }
40
- });
41
- } else {
42
- if(collapseTopLevelNulls) {
43
- Object.keys(req.body).forEach(k => {
44
- if(req.body[k] === null) delete req.body[k];
45
- });
46
- }
47
- const valid = validate(req.body);
48
- if(valid) {
49
- return previousMethod.call(this, req, res);
50
- } else {
51
- res.status(400).json({
52
- error: {
53
- errorKey: 'client.body.missing',
54
- additionalInfo: 'client.extended.badPayload',
55
- detailedInfo: validate.errors
56
- }
57
- });
58
- }
59
- }
60
- };
61
- return descriptor;
62
- }
63
- return undefined;
64
- };
65
- }
1
+ import {Request, Response} from 'express';
2
+ import Ajv, {_} from 'ajv';
3
+
4
+ export const ajv = new Ajv();
5
+ ajv.addKeyword({
6
+ keyword: 'maxDigits',
7
+ type: 'number',
8
+ schemaType: 'number',
9
+ validate: (schema: number, data: any) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
10
+ });
11
+
12
+ export function WithBody(schema: any, collapseTopLevelNulls = true) {
13
+ if(schema.type === 'object') {
14
+ const required: string[] = [];
15
+ const keys = Object.getOwnPropertyNames(schema.properties);
16
+ keys.forEach(key => {
17
+ if(schema.properties[key].required === true) {
18
+ required.push(key);
19
+ }
20
+ if(schema.properties[key].required === true || schema.properties[key].required === false) {
21
+ delete schema.properties[key].required;
22
+ }
23
+ });
24
+ schema.required = required;
25
+ }
26
+ const validate = ajv.compile(schema);
27
+ return function(_: any, __: string, descriptor: PropertyDescriptor) {
28
+ if(typeof descriptor.value === 'function') {
29
+ const previousMethod = descriptor.value;
30
+ descriptor.value = function(this: any, req: Request, res: Response) {
31
+ if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
32
+ return previousMethod.call(this, req, res);
33
+ }
34
+ if(!req.body) {
35
+ res.status(400).json({
36
+ error: {
37
+ errorKey: 'client.body.missing',
38
+ additionalInfo: 'client.extended.badPayload'
39
+ }
40
+ });
41
+ } else {
42
+ if(collapseTopLevelNulls) {
43
+ Object.keys(req.body).forEach(k => {
44
+ if(req.body[k] === null) delete req.body[k];
45
+ });
46
+ }
47
+ const valid = validate(req.body);
48
+ if(valid) {
49
+ return previousMethod.call(this, req, res);
50
+ } else {
51
+ res.status(400).json({
52
+ error: {
53
+ errorKey: 'client.body.missing',
54
+ additionalInfo: 'client.extended.badPayload',
55
+ detailedInfo: validate.errors
56
+ }
57
+ });
58
+ }
59
+ }
60
+ };
61
+ return descriptor;
62
+ }
63
+ return undefined;
64
+ };
65
+ }
@@ -1,14 +1,63 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.withTransactionConfig = void 0;
4
+ exports.requestTransactionGate = requestTransactionGate;
5
+ exports.releaseTransactionGate = releaseTransactionGate;
4
6
  exports.withTransaction = withTransaction;
5
7
  const utils_1 = require("./utils");
6
8
  exports.withTransactionConfig = {
7
9
  logQueries: false,
8
- timeoutMillis: 15000
10
+ timeoutMillis: 15000,
11
+ activeCount: 0,
12
+ gateRequested: false,
13
+ gateActive: false,
14
+ gateQueue: [],
9
15
  };
10
- function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true) {
16
+ function requestTransactionGate() {
17
+ if (exports.withTransactionConfig.gateActive || exports.withTransactionConfig.gateRequested) {
18
+ return Promise.reject(new Error('Transaction gate already active or requested'));
19
+ }
20
+ exports.withTransactionConfig.gateRequested = true;
21
+ return new Promise((resolve, _reject) => {
22
+ const check = () => {
23
+ if (exports.withTransactionConfig.activeCount <= 1) {
24
+ exports.withTransactionConfig.gateActive = true;
25
+ exports.withTransactionConfig.gateRequested = false;
26
+ resolve();
27
+ }
28
+ else {
29
+ setTimeout(check, 100);
30
+ }
31
+ };
32
+ setTimeout(check, 50);
33
+ });
34
+ }
35
+ function releaseTransactionGate() {
36
+ exports.withTransactionConfig.gateActive = false;
37
+ exports.withTransactionConfig.gateRequested = false;
38
+ const queue = exports.withTransactionConfig.gateQueue.splice(0);
39
+ for (const entry of queue) {
40
+ entry.resolve();
41
+ }
42
+ }
43
+ function releaseAndDecrement(repo, dbClient) {
44
+ if (repo.db && dbClient) {
45
+ try {
46
+ dbClient.release();
47
+ }
48
+ catch (_) { }
49
+ }
50
+ exports.withTransactionConfig.activeCount = Math.max(0, exports.withTransactionConfig.activeCount - 1);
51
+ }
52
+ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true, idleInTransactionTimeout) {
11
53
  return function (req, res, next) {
54
+ if (exports.withTransactionConfig.gateActive) {
55
+ return new Promise((resolve, reject) => {
56
+ exports.withTransactionConfig.gateQueue.push({ resolve, reject });
57
+ }).then(() => {
58
+ return withTransaction(repo, logger, previousMethod, lock, commitIfLost)(req, res, next);
59
+ });
60
+ }
12
61
  let commit = true;
13
62
  if (!commitIfLost) {
14
63
  res.once('close', () => commit = false);
@@ -22,6 +71,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
22
71
  res.write((0, utils_1.jsonStringify)(obj) || '{}');
23
72
  endTerminator();
24
73
  };
74
+ exports.withTransactionConfig.activeCount++;
25
75
  const connectTimeoutHandler = setTimeout(() => {
26
76
  logger.error('Error timed out getting a client, exiting...');
27
77
  process.exit(22);
@@ -49,8 +99,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
49
99
  if (!res.locals.dbClientCommited) {
50
100
  res.locals.dbClientCommited = true;
51
101
  (commit ? (repo.db ? dbClient.query('COMMIT') : dbClient.commit()) : Promise.reject(new Error('Client lost'))).catch((err) => err).then((err) => {
52
- if (repo.db)
53
- dbClient.release();
102
+ releaseAndDecrement(repo, dbClient);
54
103
  if (!(err instanceof Error)) {
55
104
  for (let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
56
105
  res.locals.dbClientOnCommit[i]();
@@ -64,7 +113,15 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
64
113
  }
65
114
  };
66
115
  res.locals.dbClientOnCommit = [];
67
- return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
116
+ const beginPromise = repo.db ? dbClient.query('BEGIN') : dbClient.begin();
117
+ const idleTimeoutMs = Math.max(idleInTransactionTimeout, exports.withTransactionConfig.timeoutMillis);
118
+ let setupPromise = beginPromise;
119
+ if (idleInTransactionTimeout && idleTimeoutMs > 0 && repo.db) {
120
+ setupPromise = setupPromise.then(() => {
121
+ return dbClient.query(`SET LOCAL idle_in_transaction_session_timeout TO ${idleTimeoutMs}`);
122
+ });
123
+ }
124
+ return setupPromise.then(() => {
68
125
  const finish = () => {
69
126
  res.send = () => {
70
127
  throw new Error('res.send() should not be used within transactions. Use res.json() instead.');
@@ -80,8 +137,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
80
137
  }
81
138
  }
82
139
  (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj && obj.error && (obj.error.additionalInfo2 = { message: err.message })).then(() => {
83
- if (repo.db)
84
- dbClient.release();
140
+ releaseAndDecrement(repo, dbClient);
85
141
  jsonTerminator(obj);
86
142
  });
87
143
  }
@@ -108,8 +164,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
108
164
  logger.error('Uncaught 500 with no details...');
109
165
  }
110
166
  (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
111
- if (repo.db)
112
- dbClient.release();
167
+ releaseAndDecrement(repo, dbClient);
113
168
  endTerminator();
114
169
  });
115
170
  }
@@ -140,18 +195,25 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
140
195
  additionalInfo: { message: err.message }
141
196
  }
142
197
  });
143
- dbClient.release();
198
+ releaseAndDecrement(repo, dbClient);
144
199
  });
145
200
  }
146
201
  else {
147
202
  finish();
148
203
  }
149
204
  }).catch((err) => {
150
- dbClient.release();
151
- throw err;
205
+ releaseAndDecrement(repo, dbClient);
206
+ logger.error('Error beginning transaction: %j', err);
207
+ res.status(500).json({
208
+ error: {
209
+ errorKey: 'internal.db',
210
+ additionalInfo: { message: err.message }
211
+ }
212
+ });
152
213
  });
153
214
  }).catch(err => {
154
215
  clearTimeout(connectTimeoutHandler);
216
+ exports.withTransactionConfig.activeCount = Math.max(0, exports.withTransactionConfig.activeCount - 1);
155
217
  res.status(500).json({
156
218
  error: {
157
219
  errorKey: 'internal.db',