@boxyhq/saml-jackson 0.2.4-beta.198 → 0.3.0-beta.248

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. package/Dockerfile +9 -7
  2. package/README.md +28 -29
  3. package/dist/controller/api.d.ts +32 -0
  4. package/dist/controller/api.js +193 -0
  5. package/dist/controller/error.d.ts +5 -0
  6. package/dist/controller/error.js +12 -0
  7. package/dist/controller/oauth/allowed.d.ts +1 -0
  8. package/dist/controller/oauth/allowed.js +17 -0
  9. package/dist/controller/oauth/code-verifier.d.ts +2 -0
  10. package/dist/controller/oauth/code-verifier.js +15 -0
  11. package/dist/controller/oauth/redirect.d.ts +1 -0
  12. package/dist/controller/oauth/redirect.js +11 -0
  13. package/dist/controller/oauth.d.ts +23 -0
  14. package/dist/controller/oauth.js +263 -0
  15. package/dist/controller/utils.d.ts +6 -0
  16. package/dist/controller/utils.js +17 -0
  17. package/dist/db/db.d.ts +15 -0
  18. package/dist/db/db.js +107 -0
  19. package/dist/db/encrypter.d.ts +3 -0
  20. package/dist/db/encrypter.js +29 -0
  21. package/dist/db/mem.d.ts +20 -0
  22. package/dist/db/mem.js +128 -0
  23. package/dist/db/mongo.d.ts +17 -0
  24. package/dist/db/mongo.js +106 -0
  25. package/dist/db/redis.d.ts +15 -0
  26. package/dist/db/redis.js +107 -0
  27. package/dist/db/sql/entity/JacksonIndex.d.ts +7 -0
  28. package/dist/db/sql/entity/JacksonIndex.js +41 -0
  29. package/dist/db/sql/entity/JacksonStore.d.ts +6 -0
  30. package/dist/db/sql/entity/JacksonStore.js +42 -0
  31. package/dist/db/sql/entity/JacksonTTL.d.ts +4 -0
  32. package/dist/db/sql/entity/JacksonTTL.js +29 -0
  33. package/dist/db/sql/sql.d.ts +20 -0
  34. package/dist/db/sql/sql.js +174 -0
  35. package/dist/db/store.d.ts +5 -0
  36. package/dist/db/store.js +68 -0
  37. package/dist/db/utils.d.ts +7 -0
  38. package/dist/db/utils.js +29 -0
  39. package/dist/env.d.ts +22 -0
  40. package/dist/env.js +35 -0
  41. package/dist/index.d.ts +9 -0
  42. package/dist/index.js +82 -0
  43. package/dist/jackson.d.ts +1 -0
  44. package/dist/jackson.js +153 -0
  45. package/dist/read-config.d.ts +3 -0
  46. package/dist/read-config.js +50 -0
  47. package/dist/saml/claims.d.ts +6 -0
  48. package/dist/saml/claims.js +35 -0
  49. package/dist/saml/saml.d.ts +11 -0
  50. package/dist/saml/saml.js +200 -0
  51. package/dist/saml/x509.d.ts +7 -0
  52. package/dist/saml/x509.js +69 -0
  53. package/dist/typings.d.ts +137 -0
  54. package/dist/typings.js +2 -0
  55. package/package.json +41 -21
  56. package/.dockerignore +0 -2
  57. package/.eslintrc.js +0 -13
  58. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -27
  59. package/.github/ISSUE_TEMPLATE/config.yml +0 -5
  60. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -43
  61. package/.github/pull_request_template.md +0 -31
  62. package/.github/workflows/codesee-arch-diagram.yml +0 -81
  63. package/.github/workflows/main.yml +0 -123
  64. package/_dev/docker-compose.yml +0 -37
  65. package/map.js +0 -1
  66. package/prettier.config.js +0 -4
  67. package/src/controller/api.js +0 -167
  68. package/src/controller/error.js +0 -12
  69. package/src/controller/oauth/allowed.js +0 -19
  70. package/src/controller/oauth/code-verifier.js +0 -16
  71. package/src/controller/oauth/redirect.js +0 -18
  72. package/src/controller/oauth.js +0 -321
  73. package/src/controller/utils.js +0 -19
  74. package/src/db/db.js +0 -81
  75. package/src/db/db.test.js +0 -302
  76. package/src/db/encrypter.js +0 -36
  77. package/src/db/mem.js +0 -111
  78. package/src/db/mongo.js +0 -89
  79. package/src/db/redis.js +0 -88
  80. package/src/db/sql/entity/JacksonIndex.js +0 -42
  81. package/src/db/sql/entity/JacksonStore.js +0 -42
  82. package/src/db/sql/entity/JacksonTTL.js +0 -23
  83. package/src/db/sql/model/JacksonIndex.js +0 -9
  84. package/src/db/sql/model/JacksonStore.js +0 -10
  85. package/src/db/sql/model/JacksonTTL.js +0 -8
  86. package/src/db/sql/sql.js +0 -153
  87. package/src/db/store.js +0 -42
  88. package/src/db/utils.js +0 -30
  89. package/src/env.js +0 -39
  90. package/src/index.js +0 -67
  91. package/src/jackson.js +0 -161
  92. package/src/read-config.js +0 -24
  93. package/src/saml/claims.js +0 -40
  94. package/src/saml/saml.js +0 -223
  95. package/src/saml/x509.js +0 -48
  96. package/src/test/api.test.js +0 -186
  97. package/src/test/data/metadata/boxyhq.js +0 -6
  98. package/src/test/data/metadata/boxyhq.xml +0 -30
  99. package/src/test/data/saml_response +0 -1
  100. package/src/test/oauth.test.js +0 -342
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ const mongodb_1 = require("mongodb");
32
+ const dbutils = __importStar(require("./utils"));
33
+ class Mongo {
34
+ constructor(options) {
35
+ this.options = options;
36
+ }
37
+ init() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ this.client = new mongodb_1.MongoClient(this.options.url);
40
+ yield this.client.connect();
41
+ this.db = this.client.db();
42
+ this.collection = this.db.collection('jacksonStore');
43
+ yield this.collection.createIndex({ indexes: 1 });
44
+ yield this.collection.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 1 });
45
+ return this;
46
+ });
47
+ }
48
+ get(namespace, key) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const res = yield this.collection.findOne({
51
+ _id: dbutils.key(namespace, key),
52
+ });
53
+ if (res && res.value) {
54
+ return res.value;
55
+ }
56
+ return null;
57
+ });
58
+ }
59
+ getByIndex(namespace, idx) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const docs = yield this.collection
62
+ .find({
63
+ indexes: dbutils.keyForIndex(namespace, idx),
64
+ })
65
+ .toArray();
66
+ const ret = [];
67
+ for (const doc of docs || []) {
68
+ ret.push(doc.value);
69
+ }
70
+ return ret;
71
+ });
72
+ }
73
+ put(namespace, key, val, ttl = 0, ...indexes) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ const doc = {
76
+ value: val,
77
+ };
78
+ if (ttl) {
79
+ doc.expiresAt = new Date(Date.now() + ttl * 1000);
80
+ }
81
+ // no ttl support for secondary indexes
82
+ for (const idx of indexes || []) {
83
+ const idxKey = dbutils.keyForIndex(namespace, idx);
84
+ if (!doc.indexes) {
85
+ doc.indexes = [];
86
+ }
87
+ doc.indexes.push(idxKey);
88
+ }
89
+ yield this.collection.updateOne({ _id: dbutils.key(namespace, key) }, {
90
+ $set: doc,
91
+ }, { upsert: true });
92
+ });
93
+ }
94
+ delete(namespace, key) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ return yield this.collection.deleteOne({
97
+ _id: dbutils.key(namespace, key),
98
+ });
99
+ });
100
+ }
101
+ }
102
+ exports.default = {
103
+ new: (options) => __awaiter(void 0, void 0, void 0, function* () {
104
+ return yield new Mongo(options).init();
105
+ }),
106
+ };
@@ -0,0 +1,15 @@
1
+ import { DatabaseDriver, DatabaseOption, Encrypted, Index } from '../typings';
2
+ declare class Redis implements DatabaseDriver {
3
+ private options;
4
+ private client;
5
+ constructor(options: DatabaseOption);
6
+ init(): Promise<Redis>;
7
+ get(namespace: string, key: string): Promise<any>;
8
+ getByIndex(namespace: string, idx: Index): Promise<any>;
9
+ put(namespace: string, key: string, val: Encrypted, ttl?: number, ...indexes: any[]): Promise<void>;
10
+ delete(namespace: string, key: string): Promise<any>;
11
+ }
12
+ declare const _default: {
13
+ new: (options: DatabaseOption) => Promise<Redis>;
14
+ };
15
+ export default _default;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ const redis = __importStar(require("redis"));
32
+ const dbutils = __importStar(require("./utils"));
33
+ class Redis {
34
+ constructor(options) {
35
+ this.options = options;
36
+ }
37
+ init() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const opts = {};
40
+ if (this.options && this.options.url) {
41
+ opts['socket'] = {
42
+ url: this.options.url,
43
+ };
44
+ }
45
+ this.client = redis.createClient(opts);
46
+ this.client.on('error', (err) => console.log('Redis Client Error', err));
47
+ yield this.client.connect();
48
+ return this;
49
+ });
50
+ }
51
+ get(namespace, key) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const res = yield this.client.get(dbutils.key(namespace, key));
54
+ if (res) {
55
+ return JSON.parse(res);
56
+ }
57
+ return null;
58
+ });
59
+ }
60
+ getByIndex(namespace, idx) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const dbKeys = yield this.client.sMembers(dbutils.keyForIndex(namespace, idx));
63
+ const ret = [];
64
+ for (const dbKey of dbKeys || []) {
65
+ ret.push(yield this.get(namespace, dbKey));
66
+ }
67
+ return ret;
68
+ });
69
+ }
70
+ put(namespace, key, val, ttl = 0, ...indexes) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ let tx = this.client.multi();
73
+ const k = dbutils.key(namespace, key);
74
+ tx = tx.set(k, JSON.stringify(val));
75
+ if (ttl) {
76
+ tx = tx.expire(k, ttl);
77
+ }
78
+ // no ttl support for secondary indexes
79
+ for (const idx of indexes || []) {
80
+ const idxKey = dbutils.keyForIndex(namespace, idx);
81
+ tx = tx.sAdd(idxKey, key);
82
+ tx = tx.sAdd(dbutils.keyFromParts(dbutils.indexPrefix, k), idxKey);
83
+ }
84
+ yield tx.exec();
85
+ });
86
+ }
87
+ delete(namespace, key) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ let tx = this.client.multi();
90
+ const k = dbutils.key(namespace, key);
91
+ tx = tx.del(k);
92
+ const idxKey = dbutils.keyFromParts(dbutils.indexPrefix, k);
93
+ // delete secondary indexes and then the mapping of the seconary indexes
94
+ const dbKeys = yield this.client.sMembers(idxKey);
95
+ for (const dbKey of dbKeys || []) {
96
+ tx.sRem(dbKey, key);
97
+ }
98
+ tx.del(idxKey);
99
+ return yield tx.exec();
100
+ });
101
+ }
102
+ }
103
+ exports.default = {
104
+ new: (options) => __awaiter(void 0, void 0, void 0, function* () {
105
+ return yield new Redis(options).init();
106
+ }),
107
+ };
@@ -0,0 +1,7 @@
1
+ import { JacksonStore } from './JacksonStore';
2
+ export declare class JacksonIndex {
3
+ id: number;
4
+ key: string;
5
+ storeKey: string;
6
+ store?: JacksonStore;
7
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.JacksonIndex = void 0;
10
+ const JacksonStore_1 = require("./JacksonStore");
11
+ const typeorm_1 = require("typeorm");
12
+ let JacksonIndex = class JacksonIndex {
13
+ };
14
+ __decorate([
15
+ (0, typeorm_1.PrimaryGeneratedColumn)()
16
+ ], JacksonIndex.prototype, "id", void 0);
17
+ __decorate([
18
+ (0, typeorm_1.Index)('_jackson_index_key'),
19
+ (0, typeorm_1.Column)({
20
+ type: 'varchar',
21
+ length: 1500,
22
+ })
23
+ ], JacksonIndex.prototype, "key", void 0);
24
+ __decorate([
25
+ (0, typeorm_1.Column)({
26
+ type: 'varchar',
27
+ length: 1500,
28
+ })
29
+ ], JacksonIndex.prototype, "storeKey", void 0);
30
+ __decorate([
31
+ (0, typeorm_1.ManyToOne)(() => JacksonStore_1.JacksonStore, undefined, {
32
+ //inverseSide: 'in',
33
+ eager: true,
34
+ onDelete: 'CASCADE',
35
+ })
36
+ ], JacksonIndex.prototype, "store", void 0);
37
+ JacksonIndex = __decorate([
38
+ (0, typeorm_1.Index)('_jackson_index_key_store', ['key', 'storeKey']),
39
+ (0, typeorm_1.Entity)()
40
+ ], JacksonIndex);
41
+ exports.JacksonIndex = JacksonIndex;
@@ -0,0 +1,6 @@
1
+ export declare class JacksonStore {
2
+ key: string;
3
+ value: string;
4
+ iv?: string;
5
+ tag?: string;
6
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.JacksonStore = void 0;
10
+ const typeorm_1 = require("typeorm");
11
+ let JacksonStore = class JacksonStore {
12
+ };
13
+ __decorate([
14
+ (0, typeorm_1.Column)({
15
+ primary: true,
16
+ type: 'varchar',
17
+ length: 1500,
18
+ })
19
+ ], JacksonStore.prototype, "key", void 0);
20
+ __decorate([
21
+ (0, typeorm_1.Column)({
22
+ type: 'text',
23
+ })
24
+ ], JacksonStore.prototype, "value", void 0);
25
+ __decorate([
26
+ (0, typeorm_1.Column)({
27
+ type: 'varchar',
28
+ length: 64,
29
+ nullable: true,
30
+ })
31
+ ], JacksonStore.prototype, "iv", void 0);
32
+ __decorate([
33
+ (0, typeorm_1.Column)({
34
+ type: 'varchar',
35
+ length: 64,
36
+ nullable: true,
37
+ })
38
+ ], JacksonStore.prototype, "tag", void 0);
39
+ JacksonStore = __decorate([
40
+ (0, typeorm_1.Entity)()
41
+ ], JacksonStore);
42
+ exports.JacksonStore = JacksonStore;
@@ -0,0 +1,4 @@
1
+ export declare class JacksonTTL {
2
+ key: string;
3
+ expiresAt: number;
4
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.JacksonTTL = void 0;
10
+ const typeorm_1 = require("typeorm");
11
+ let JacksonTTL = class JacksonTTL {
12
+ };
13
+ __decorate([
14
+ (0, typeorm_1.Column)({
15
+ primary: true,
16
+ type: 'varchar',
17
+ length: 1500,
18
+ })
19
+ ], JacksonTTL.prototype, "key", void 0);
20
+ __decorate([
21
+ (0, typeorm_1.Index)('_jackson_ttl_expires_at'),
22
+ (0, typeorm_1.Column)({
23
+ type: 'bigint',
24
+ })
25
+ ], JacksonTTL.prototype, "expiresAt", void 0);
26
+ JacksonTTL = __decorate([
27
+ (0, typeorm_1.Entity)()
28
+ ], JacksonTTL);
29
+ exports.JacksonTTL = JacksonTTL;
@@ -0,0 +1,20 @@
1
+ import { DatabaseDriver, DatabaseOption, Index, Encrypted } from '../../typings';
2
+ declare class Sql implements DatabaseDriver {
3
+ private options;
4
+ private connection;
5
+ private storeRepository;
6
+ private indexRepository;
7
+ private ttlRepository;
8
+ private ttlCleanup;
9
+ private timerId;
10
+ constructor(options: DatabaseOption);
11
+ init(): Promise<Sql>;
12
+ get(namespace: string, key: string): Promise<any>;
13
+ getByIndex(namespace: string, idx: Index): Promise<any>;
14
+ put(namespace: string, key: string, val: Encrypted, ttl?: number, ...indexes: any[]): Promise<void>;
15
+ delete(namespace: string, key: string): Promise<any>;
16
+ }
17
+ declare const _default: {
18
+ new: (options: DatabaseOption) => Promise<Sql>;
19
+ };
20
+ export default _default;
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
6
+ }) : (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ o[k2] = m[k];
9
+ }));
10
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
11
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
12
+ }) : function(o, v) {
13
+ o["default"] = v;
14
+ });
15
+ var __importStar = (this && this.__importStar) || function (mod) {
16
+ if (mod && mod.__esModule) return mod;
17
+ var result = {};
18
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
19
+ __setModuleDefault(result, mod);
20
+ return result;
21
+ };
22
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
23
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
+ return new (P || (P = Promise))(function (resolve, reject) {
25
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
29
+ });
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ require('reflect-metadata');
33
+ const typeorm_1 = require("typeorm");
34
+ const dbutils = __importStar(require("../utils"));
35
+ const JacksonStore_1 = require("./entity/JacksonStore");
36
+ const JacksonIndex_1 = require("./entity/JacksonIndex");
37
+ const JacksonTTL_1 = require("./entity/JacksonTTL");
38
+ class Sql {
39
+ constructor(options) {
40
+ this.options = options;
41
+ }
42
+ init() {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ while (true) {
45
+ try {
46
+ this.connection = yield (0, typeorm_1.createConnection)({
47
+ name: this.options.type + Math.floor(Math.random() * 100000),
48
+ type: this.options.type,
49
+ url: this.options.url,
50
+ synchronize: true,
51
+ migrationsTableName: '_jackson_migrations',
52
+ logging: ['error'],
53
+ entities: [JacksonStore_1.JacksonStore, JacksonIndex_1.JacksonIndex, JacksonTTL_1.JacksonTTL],
54
+ });
55
+ break;
56
+ }
57
+ catch (err) {
58
+ console.error(`error connecting to ${this.options.type} db: ${err}`);
59
+ yield dbutils.sleep(1000);
60
+ continue;
61
+ }
62
+ }
63
+ this.storeRepository = this.connection.getRepository(JacksonStore_1.JacksonStore);
64
+ this.indexRepository = this.connection.getRepository(JacksonIndex_1.JacksonIndex);
65
+ this.ttlRepository = this.connection.getRepository(JacksonTTL_1.JacksonTTL);
66
+ if (this.options.ttl && this.options.cleanupLimit) {
67
+ this.ttlCleanup = () => __awaiter(this, void 0, void 0, function* () {
68
+ const now = Date.now();
69
+ while (true) {
70
+ const ids = yield this.ttlRepository
71
+ .createQueryBuilder('jackson_ttl')
72
+ .limit(this.options.cleanupLimit)
73
+ .where('jackson_ttl.expiresAt <= :expiresAt', {
74
+ expiresAt: now,
75
+ })
76
+ .getMany();
77
+ if (ids.length <= 0) {
78
+ break;
79
+ }
80
+ const delIds = ids.map((id) => {
81
+ return id.key;
82
+ });
83
+ yield this.storeRepository.remove(ids);
84
+ yield this.ttlRepository.delete(delIds);
85
+ }
86
+ this.timerId = setTimeout(this.ttlCleanup, this.options.ttl * 1000);
87
+ });
88
+ this.timerId = setTimeout(this.ttlCleanup, this.options.ttl * 1000);
89
+ }
90
+ else {
91
+ console.log('Warning: ttl cleanup not enabled, set both "ttl" and "cleanupLimit" options to enable it!');
92
+ }
93
+ return this;
94
+ });
95
+ }
96
+ get(namespace, key) {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ let res = yield this.storeRepository.findOne({
99
+ key: dbutils.key(namespace, key),
100
+ });
101
+ if (res && res.value) {
102
+ return {
103
+ value: res.value,
104
+ iv: res.iv,
105
+ tag: res.tag,
106
+ };
107
+ }
108
+ return null;
109
+ });
110
+ }
111
+ getByIndex(namespace, idx) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ const res = yield this.indexRepository.find({
114
+ key: dbutils.keyForIndex(namespace, idx),
115
+ });
116
+ const ret = [];
117
+ if (res) {
118
+ res.forEach((r) => {
119
+ ret.push({
120
+ value: r.store.value,
121
+ iv: r.store.iv,
122
+ tag: r.store.tag,
123
+ });
124
+ });
125
+ }
126
+ return ret;
127
+ });
128
+ }
129
+ put(namespace, key, val, ttl = 0, ...indexes) {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ yield this.connection.transaction((transactionalEntityManager) => __awaiter(this, void 0, void 0, function* () {
132
+ const dbKey = dbutils.key(namespace, key);
133
+ const store = new JacksonStore_1.JacksonStore();
134
+ store.key = dbKey;
135
+ store.value = val.value;
136
+ store.iv = val.iv;
137
+ store.tag = val.tag;
138
+ yield transactionalEntityManager.save(store);
139
+ if (ttl) {
140
+ const ttlRec = new JacksonTTL_1.JacksonTTL();
141
+ ttlRec.key = dbKey;
142
+ ttlRec.expiresAt = Date.now() + ttl * 1000;
143
+ yield transactionalEntityManager.save(ttlRec);
144
+ }
145
+ // no ttl support for secondary indexes
146
+ for (const idx of indexes || []) {
147
+ const key = dbutils.keyForIndex(namespace, idx);
148
+ const rec = yield this.indexRepository.findOne({
149
+ key,
150
+ storeKey: store.key,
151
+ });
152
+ if (!rec) {
153
+ const ji = new JacksonIndex_1.JacksonIndex();
154
+ ji.key = key;
155
+ ji.store = store;
156
+ yield transactionalEntityManager.save(ji);
157
+ }
158
+ }
159
+ }));
160
+ });
161
+ }
162
+ delete(namespace, key) {
163
+ return __awaiter(this, void 0, void 0, function* () {
164
+ return yield this.storeRepository.remove({
165
+ key: dbutils.key(namespace, key),
166
+ });
167
+ });
168
+ }
169
+ }
170
+ exports.default = {
171
+ new: (options) => __awaiter(void 0, void 0, void 0, function* () {
172
+ return yield new Sql(options).init();
173
+ }),
174
+ };
@@ -0,0 +1,5 @@
1
+ import { Storable } from '../typings';
2
+ declare const _default: {
3
+ new: (namespace: string, db: any, ttl?: number) => Storable;
4
+ };
5
+ export default _default;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ const dbutils = __importStar(require("./utils"));
32
+ class Store {
33
+ constructor(namespace, db, ttl = 0) {
34
+ this.namespace = namespace;
35
+ this.db = db;
36
+ this.ttl = ttl;
37
+ }
38
+ get(key) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ return yield this.db.get(this.namespace, dbutils.keyDigest(key));
41
+ });
42
+ }
43
+ getByIndex(idx) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ idx.value = dbutils.keyDigest(idx.value);
46
+ return yield this.db.getByIndex(this.namespace, idx);
47
+ });
48
+ }
49
+ put(key, val, ...indexes) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ indexes = (indexes || []).map((idx) => {
52
+ idx.value = dbutils.keyDigest(idx.value);
53
+ return idx;
54
+ });
55
+ return yield this.db.put(this.namespace, dbutils.keyDigest(key), val, this.ttl, ...indexes);
56
+ });
57
+ }
58
+ delete(key) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ return yield this.db.delete(this.namespace, dbutils.keyDigest(key));
61
+ });
62
+ }
63
+ }
64
+ exports.default = {
65
+ new: (namespace, db, ttl = 0) => {
66
+ return new Store(namespace, db, ttl);
67
+ },
68
+ };
@@ -0,0 +1,7 @@
1
+ import { Index } from '../typings';
2
+ export declare const key: (namespace: string, k: string) => string;
3
+ export declare const keyForIndex: (namespace: string, idx: Index) => string;
4
+ export declare const keyDigest: (k: string) => string;
5
+ export declare const keyFromParts: (...parts: string[]) => string;
6
+ export declare const sleep: (ms: number) => Promise<void>;
7
+ export declare const indexPrefix = "_index";
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.indexPrefix = exports.sleep = exports.keyFromParts = exports.keyDigest = exports.keyForIndex = exports.key = void 0;
7
+ const ripemd160_1 = __importDefault(require("ripemd160"));
8
+ const key = (namespace, k) => {
9
+ return namespace + ':' + k;
10
+ };
11
+ exports.key = key;
12
+ const keyForIndex = (namespace, idx) => {
13
+ return (0, exports.key)((0, exports.key)(namespace, idx.name), idx.value);
14
+ };
15
+ exports.keyForIndex = keyForIndex;
16
+ const keyDigest = (k) => {
17
+ return new ripemd160_1.default().update(k).digest('hex');
18
+ };
19
+ exports.keyDigest = keyDigest;
20
+ const keyFromParts = (...parts) => {
21
+ // TODO: pick a better strategy, keys can collide now
22
+ return parts.join(':');
23
+ };
24
+ exports.keyFromParts = keyFromParts;
25
+ const sleep = (ms) => {
26
+ return new Promise((resolve) => setTimeout(resolve, ms));
27
+ };
28
+ exports.sleep = sleep;
29
+ exports.indexPrefix = '_index';