@boxyhq/saml-jackson 0.1.5-beta.139 → 0.1.5-beta.140

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boxyhq/saml-jackson",
3
- "version": "0.1.5-beta.139",
3
+ "version": "0.1.5-beta.140",
4
4
  "license": "Apache 2.0",
5
5
  "description": "SAML 2.0 service",
6
6
  "main": "src/index.js",
package/src/db/db.test.js CHANGED
@@ -224,7 +224,7 @@ t.test('dbs', ({ end }) => {
224
224
  }
225
225
 
226
226
  await new Promise((resolve) =>
227
- setTimeout(resolve, ((dbEngine === 'mem' ? 5 : 0) + ttl + 0.5) * 1000)
227
+ setTimeout(resolve, (2*ttl + 0.5) * 1000)
228
228
  );
229
229
 
230
230
  const ret1 = await ttlStore.get(record1.id);
@@ -27,10 +27,6 @@ module.exports = (type) => {
27
27
  value: {
28
28
  type: valueType(type),
29
29
  },
30
- expiresAt: {
31
- type: 'bigint',
32
- nullable: true,
33
- },
34
30
  },
35
31
  });
36
32
  };
@@ -0,0 +1,23 @@
1
+ const EntitySchema = require('typeorm').EntitySchema;
2
+ const JacksonTTL = require('../model/JacksonTTL.js');
3
+
4
+ module.exports = new EntitySchema({
5
+ name: 'JacksonTTL',
6
+ target: JacksonTTL,
7
+ columns: {
8
+ key: {
9
+ primary: true,
10
+ type: 'varchar',
11
+ length: 1500,
12
+ },
13
+ expiresAt: {
14
+ type: 'bigint',
15
+ },
16
+ },
17
+ indices: [
18
+ {
19
+ name: '_jackson_ttl_expires_at',
20
+ columns: ['expiresAt'],
21
+ },
22
+ ],
23
+ });
@@ -1,8 +1,7 @@
1
1
  /*export */ class JacksonStore {
2
- constructor(key, value, expiresAt) {
2
+ constructor(key, value) {
3
3
  this.key = key;
4
4
  this.value = value;
5
- this.expiresAt = expiresAt;
6
5
  }
7
6
  }
8
7
 
@@ -0,0 +1,8 @@
1
+ /*export */ class JacksonTTL {
2
+ constructor(key, expiresAt) {
3
+ this.key = key;
4
+ this.expiresAt = expiresAt;
5
+ }
6
+ }
7
+
8
+ module.exports = JacksonTTL;
package/src/db/sql/sql.js CHANGED
@@ -2,6 +2,7 @@ require('reflect-metadata');
2
2
  const typeorm = require('typeorm');
3
3
  const JacksonStore = require('./model/JacksonStore.js');
4
4
  const JacksonIndex = require('./model/JacksonIndex.js');
5
+ const JacksonTTL = require('./model/JacksonTTL.js');
5
6
 
6
7
  const dbutils = require('../utils.js');
7
8
 
@@ -20,6 +21,7 @@ class Sql {
20
21
  entities: [
21
22
  require('./entity/JacksonStore.js')(options.type),
22
23
  require('./entity/JacksonIndex.js'),
24
+ require('./entity/JacksonTTL.js'),
23
25
  ],
24
26
  });
25
27
 
@@ -33,22 +35,29 @@ class Sql {
33
35
 
34
36
  this.storeRepository = this.connection.getRepository(JacksonStore);
35
37
  this.indexRepository = this.connection.getRepository(JacksonIndex);
38
+ this.ttlRepository = this.connection.getRepository(JacksonTTL);
36
39
 
37
40
  if (options.ttl && options.limit) {
38
41
  this.ttlCleanup = async () => {
39
42
  const now = Date.now();
40
43
 
41
44
  while (true) {
42
- const ids = await this.storeRepository.find({
43
- expiresAt: typeorm.MoreThan(now),
44
- take: options.limit,
45
- });
45
+ const ids = await this.ttlRepository
46
+ .createQueryBuilder('jackson_ttl')
47
+ .limit(options.limit)
48
+ .where('jackson_ttl.expiresAt <= :expiresAt', { expiresAt: now })
49
+ .getMany();
46
50
 
47
51
  if (ids.length <= 0) {
48
52
  break;
49
53
  }
50
54
 
55
+ const delIds = ids.map((id) => {
56
+ return id.key;
57
+ });
58
+
51
59
  await this.storeRepository.remove(ids);
60
+ await this.ttlRepository.delete(delIds);
52
61
  }
53
62
 
54
63
  this.timerId = setTimeout(this.ttlCleanup, options.ttl * 1000);
@@ -99,13 +108,15 @@ class Sql {
99
108
 
100
109
  async put(namespace, key, val, ttl = 0, ...indexes) {
101
110
  await this.connection.transaction(async (transactionalEntityManager) => {
102
- const store = new JacksonStore(
103
- dbutils.key(namespace, key),
104
- JSON.stringify(val),
105
- ttl > 0 ? Date.now() + ttl * 1000 : null
106
- );
111
+ const dbKey = dbutils.key(namespace, key);
112
+ const store = new JacksonStore(dbKey, JSON.stringify(val));
107
113
  await transactionalEntityManager.save(store);
108
114
 
115
+ if (ttl) {
116
+ const ttlRec = new JacksonTTL(dbKey, Date.now() + ttl * 1000);
117
+ await transactionalEntityManager.save(ttlRec);
118
+ }
119
+
109
120
  // no ttl support for secondary indexes
110
121
  for (const idx of indexes || []) {
111
122
  const key = dbutils.keyForIndex(namespace, idx);