@boxyhq/saml-jackson 1.0.6 → 1.0.7

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.
@@ -47,8 +47,9 @@ const OAuthErrorResponse = ({ error, error_description, redirect_uri, state, })
47
47
  exports.OAuthErrorResponse = OAuthErrorResponse;
48
48
  // https://kentcdodds.com/blog/get-a-catch-block-error-message-with-typescript
49
49
  function getErrorMessage(error) {
50
- if (error instanceof Error)
50
+ if (error instanceof Error) {
51
51
  return error.message;
52
+ }
52
53
  return String(error);
53
54
  }
54
55
  exports.getErrorMessage = getErrorMessage;
package/dist/db/db.js CHANGED
@@ -41,6 +41,12 @@ const mongo_1 = __importDefault(require("./mongo"));
41
41
  const redis_1 = __importDefault(require("./redis"));
42
42
  const sql_1 = __importDefault(require("./sql/sql"));
43
43
  const store_1 = __importDefault(require("./store"));
44
+ const JacksonStore_1 = require("./sql/entity/JacksonStore");
45
+ const JacksonIndex_1 = require("./sql/entity/JacksonIndex");
46
+ const JacksonTTL_1 = require("./sql/entity/JacksonTTL");
47
+ const JacksonStore_2 = require("./planetscale/entity/JacksonStore");
48
+ const JacksonIndex_2 = require("./planetscale/entity/JacksonIndex");
49
+ const JacksonTTL_2 = require("./planetscale/entity/JacksonTTL");
44
50
  const decrypt = (res, encryptionKey) => {
45
51
  if (res.iv && res.tag) {
46
52
  return JSON.parse(encrypter.decrypt(res.value, res.iv, res.tag, encryptionKey));
@@ -107,7 +113,17 @@ exports.default = {
107
113
  case 'redis':
108
114
  return new DB(yield redis_1.default.new(options), encryptionKey);
109
115
  case 'sql':
110
- return new DB(yield sql_1.default.new(options), encryptionKey);
116
+ return new DB(yield sql_1.default.new(options, {
117
+ JacksonStore: JacksonStore_1.JacksonStore,
118
+ JacksonIndex: JacksonIndex_1.JacksonIndex,
119
+ JacksonTTL: JacksonTTL_1.JacksonTTL,
120
+ }), encryptionKey);
121
+ case 'planetscale':
122
+ return new DB(yield sql_1.default.new(options, {
123
+ JacksonStore: JacksonStore_2.JacksonStore,
124
+ JacksonIndex: JacksonIndex_2.JacksonIndex,
125
+ JacksonTTL: JacksonTTL_2.JacksonTTL,
126
+ }), encryptionKey);
111
127
  case 'mongo':
112
128
  return new DB(yield mongo_1.default.new(options), encryptionKey);
113
129
  case 'mem':
package/dist/db/mongo.js CHANGED
@@ -67,8 +67,9 @@ class Mongo {
67
67
  const docs = yield this.collection
68
68
  .find({ _id: _namespaceMatch }, { sort: { createdAt: -1 }, skip: offset, limit: limit })
69
69
  .toArray();
70
- if (docs)
70
+ if (docs) {
71
71
  return docs.map(({ value }) => value);
72
+ }
72
73
  return [];
73
74
  });
74
75
  }
@@ -0,0 +1,5 @@
1
+ export declare class JacksonIndex {
2
+ id: number;
3
+ key: string;
4
+ storeKey: string;
5
+ }
@@ -0,0 +1,34 @@
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 typeorm_1 = require("typeorm");
11
+ let JacksonIndex = class JacksonIndex {
12
+ };
13
+ __decorate([
14
+ (0, typeorm_1.PrimaryGeneratedColumn)()
15
+ ], JacksonIndex.prototype, "id", void 0);
16
+ __decorate([
17
+ (0, typeorm_1.Index)('_jackson_index_key'),
18
+ (0, typeorm_1.Column)({
19
+ type: 'varchar',
20
+ length: 250,
21
+ })
22
+ ], JacksonIndex.prototype, "key", void 0);
23
+ __decorate([
24
+ (0, typeorm_1.Index)('_jackson_index_store'),
25
+ (0, typeorm_1.Column)({
26
+ type: 'varchar',
27
+ length: 250,
28
+ })
29
+ ], JacksonIndex.prototype, "storeKey", void 0);
30
+ JacksonIndex = __decorate([
31
+ (0, typeorm_1.Index)('_jackson_index_key_store', ['key', 'storeKey']),
32
+ (0, typeorm_1.Entity)()
33
+ ], JacksonIndex);
34
+ exports.JacksonIndex = JacksonIndex;
@@ -0,0 +1,8 @@
1
+ export declare class JacksonStore {
2
+ key: string;
3
+ value: string;
4
+ iv?: string;
5
+ tag?: string;
6
+ createdAt?: Date;
7
+ modifiedAt?: string;
8
+ }
@@ -0,0 +1,55 @@
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: 250,
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
+ __decorate([
40
+ (0, typeorm_1.Column)({
41
+ type: 'timestamp',
42
+ default: () => 'CURRENT_TIMESTAMP',
43
+ nullable: false,
44
+ })
45
+ ], JacksonStore.prototype, "createdAt", void 0);
46
+ __decorate([
47
+ (0, typeorm_1.Column)({
48
+ type: 'timestamp',
49
+ nullable: true,
50
+ })
51
+ ], JacksonStore.prototype, "modifiedAt", void 0);
52
+ JacksonStore = __decorate([
53
+ (0, typeorm_1.Entity)()
54
+ ], JacksonStore);
55
+ 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: 250,
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;
@@ -7,8 +7,15 @@ declare class Sql implements DatabaseDriver {
7
7
  private ttlRepository;
8
8
  private ttlCleanup;
9
9
  private timerId;
10
+ private JacksonStore;
11
+ private JacksonIndex;
12
+ private JacksonTTL;
10
13
  constructor(options: DatabaseOption);
11
- init(): Promise<Sql>;
14
+ init({ JacksonStore, JacksonIndex, JacksonTTL }: {
15
+ JacksonStore: any;
16
+ JacksonIndex: any;
17
+ JacksonTTL: any;
18
+ }): Promise<Sql>;
12
19
  get(namespace: string, key: string): Promise<any>;
13
20
  getAll(namespace: string, pageOffset: number, pageLimit: number): Promise<unknown[]>;
14
21
  getByIndex(namespace: string, idx: Index): Promise<any>;
@@ -16,6 +23,6 @@ declare class Sql implements DatabaseDriver {
16
23
  delete(namespace: string, key: string): Promise<any>;
17
24
  }
18
25
  declare const _default: {
19
- new: (options: DatabaseOption) => Promise<Sql>;
26
+ new: (options: DatabaseOption, entities: any) => Promise<Sql>;
20
27
  };
21
28
  export default _default;
@@ -36,25 +36,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  require('reflect-metadata');
37
37
  const typeorm_1 = require("typeorm");
38
38
  const dbutils = __importStar(require("../utils"));
39
- const JacksonStore_1 = require("./entity/JacksonStore");
40
- const JacksonIndex_1 = require("./entity/JacksonIndex");
41
- const JacksonTTL_1 = require("./entity/JacksonTTL");
42
39
  class Sql {
43
40
  constructor(options) {
44
41
  this.options = options;
45
42
  }
46
- init() {
43
+ init({ JacksonStore, JacksonIndex, JacksonTTL }) {
47
44
  return __awaiter(this, void 0, void 0, function* () {
48
45
  while (true) {
49
46
  try {
50
47
  this.dataSource = new typeorm_1.DataSource({
51
48
  // name: this.options.type! + Math.floor(Math.random() * 100000),
52
- type: this.options.type,
49
+ type: this.options.engine === 'planetscale' ? 'mysql' : this.options.type,
53
50
  url: this.options.url,
54
- synchronize: true,
51
+ synchronize: this.options.engine !== 'planetscale',
55
52
  migrationsTableName: '_jackson_migrations',
56
53
  logging: ['error'],
57
- entities: [JacksonStore_1.JacksonStore, JacksonIndex_1.JacksonIndex, JacksonTTL_1.JacksonTTL],
54
+ entities: [JacksonStore, JacksonIndex, JacksonTTL],
55
+ ssl: this.options.ssl,
58
56
  });
59
57
  yield this.dataSource.initialize();
60
58
  break;
@@ -65,9 +63,12 @@ class Sql {
65
63
  continue;
66
64
  }
67
65
  }
68
- this.storeRepository = this.dataSource.getRepository(JacksonStore_1.JacksonStore);
69
- this.indexRepository = this.dataSource.getRepository(JacksonIndex_1.JacksonIndex);
70
- this.ttlRepository = this.dataSource.getRepository(JacksonTTL_1.JacksonTTL);
66
+ this.JacksonStore = JacksonStore;
67
+ this.JacksonIndex = JacksonIndex;
68
+ this.JacksonTTL = JacksonTTL;
69
+ this.storeRepository = this.dataSource.getRepository(JacksonStore);
70
+ this.indexRepository = this.dataSource.getRepository(JacksonIndex);
71
+ this.ttlRepository = this.dataSource.getRepository(JacksonTTL);
71
72
  if (this.options.ttl && this.options.cleanupLimit) {
72
73
  this.ttlCleanup = () => __awaiter(this, void 0, void 0, function* () {
73
74
  const now = Date.now();
@@ -121,15 +122,11 @@ class Sql {
121
122
  select: ['value', 'iv', 'tag'],
122
123
  order: {
123
124
  ['createdAt']: 'DESC',
124
- // ['createdAt']: 'ASC',
125
125
  },
126
126
  take: offsetAndLimitValueCheck ? this.options.pageLimit : pageLimit,
127
127
  skip: offsetAndLimitValueCheck ? 0 : pageOffset,
128
128
  });
129
- const returnValue = JSON.parse(JSON.stringify(response));
130
- if (returnValue)
131
- return returnValue;
132
- return [];
129
+ return JSON.parse(JSON.stringify(response)) || [];
133
130
  });
134
131
  }
135
132
  getByIndex(namespace, idx) {
@@ -139,13 +136,19 @@ class Sql {
139
136
  });
140
137
  const ret = [];
141
138
  if (res) {
142
- res.forEach((r) => {
139
+ for (const r of res) {
140
+ let value = r.store;
141
+ if (this.options.engine === 'planetscale') {
142
+ value = yield this.storeRepository.findOneBy({
143
+ key: r.storeKey,
144
+ });
145
+ }
143
146
  ret.push({
144
- value: r.store.value,
145
- iv: r.store.iv,
146
- tag: r.store.tag,
147
+ value: value.value,
148
+ iv: value.iv,
149
+ tag: value.tag,
147
150
  });
148
- });
151
+ }
149
152
  }
150
153
  return ret;
151
154
  });
@@ -154,7 +157,7 @@ class Sql {
154
157
  return __awaiter(this, void 0, void 0, function* () {
155
158
  yield this.dataSource.transaction((transactionalEntityManager) => __awaiter(this, void 0, void 0, function* () {
156
159
  const dbKey = dbutils.key(namespace, key);
157
- const store = new JacksonStore_1.JacksonStore();
160
+ const store = new this.JacksonStore();
158
161
  store.key = dbKey;
159
162
  store.value = val.value;
160
163
  store.iv = val.iv;
@@ -162,7 +165,7 @@ class Sql {
162
165
  store.modifiedAt = new Date().toISOString();
163
166
  yield transactionalEntityManager.save(store);
164
167
  if (ttl) {
165
- const ttlRec = new JacksonTTL_1.JacksonTTL();
168
+ const ttlRec = new this.JacksonTTL();
166
169
  ttlRec.key = dbKey;
167
170
  ttlRec.expiresAt = Date.now() + ttl * 1000;
168
171
  yield transactionalEntityManager.save(ttlRec);
@@ -175,9 +178,14 @@ class Sql {
175
178
  storeKey: store.key,
176
179
  });
177
180
  if (!rec) {
178
- const ji = new JacksonIndex_1.JacksonIndex();
181
+ const ji = new this.JacksonIndex();
179
182
  ji.key = key;
180
- ji.store = store;
183
+ if (this.options.engine === 'planetscale') {
184
+ ji.storeKey = store.key;
185
+ }
186
+ else {
187
+ ji.store = store;
188
+ }
181
189
  yield transactionalEntityManager.save(ji);
182
190
  }
183
191
  }
@@ -188,6 +196,18 @@ class Sql {
188
196
  return __awaiter(this, void 0, void 0, function* () {
189
197
  const dbKey = dbutils.key(namespace, key);
190
198
  yield this.ttlRepository.remove({ key: dbKey });
199
+ if (this.options.engine === 'planetscale') {
200
+ const response = yield this.indexRepository.find({
201
+ where: { storeKey: dbKey },
202
+ select: ['id'],
203
+ });
204
+ const returnValue = response || [];
205
+ for (const r of returnValue) {
206
+ yield this.indexRepository.remove({
207
+ id: r.id,
208
+ });
209
+ }
210
+ }
191
211
  return yield this.storeRepository.remove({
192
212
  key: dbKey,
193
213
  });
@@ -195,7 +215,7 @@ class Sql {
195
215
  }
196
216
  }
197
217
  exports.default = {
198
- new: (options) => __awaiter(void 0, void 0, void 0, function* () {
199
- return yield new Sql(options).init();
218
+ new: (options, entities) => __awaiter(void 0, void 0, void 0, function* () {
219
+ return yield new Sql(options).init(entities);
200
220
  }),
201
221
  };
package/dist/typings.d.ts CHANGED
@@ -106,7 +106,7 @@ export interface Encrypted {
106
106
  value: string;
107
107
  }
108
108
  export declare type EncryptionKey = any;
109
- export declare type DatabaseEngine = 'redis' | 'sql' | 'mongo' | 'mem';
109
+ export declare type DatabaseEngine = 'redis' | 'sql' | 'mongo' | 'mem' | 'planetscale';
110
110
  export declare type DatabaseType = 'postgres' | 'mysql' | 'mariadb';
111
111
  export interface DatabaseOption {
112
112
  engine?: DatabaseEngine;
@@ -116,6 +116,7 @@ export interface DatabaseOption {
116
116
  cleanupLimit?: number;
117
117
  encryptionKey?: string;
118
118
  pageLimit?: number;
119
+ ssl?: any;
119
120
  }
120
121
  export interface JacksonOption {
121
122
  externalUrl: string;
@@ -0,0 +1,22 @@
1
+ import { MigrationInterface, QueryRunner } from "typeorm";
2
+
3
+ export class msNocascade1653746497237 implements MigrationInterface {
4
+ name = 'msNocascade1653746497237'
5
+
6
+ public async up(queryRunner: QueryRunner): Promise<void> {
7
+ await queryRunner.query(`CREATE TABLE \`jackson_store\` (\`key\` varchar(250) NOT NULL, \`value\` text NOT NULL, \`iv\` varchar(64) NULL, \`tag\` varchar(64) NULL, \`createdAt\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, \`modifiedAt\` timestamp NULL, PRIMARY KEY (\`key\`)) ENGINE=InnoDB`);
8
+ await queryRunner.query(`CREATE TABLE \`jackson_ttl\` (\`key\` varchar(250) NOT NULL, \`expiresAt\` bigint NOT NULL, INDEX \`_jackson_ttl_expires_at\` (\`expiresAt\`), PRIMARY KEY (\`key\`)) ENGINE=InnoDB`);
9
+ await queryRunner.query(`CREATE TABLE \`jackson_index\` (\`id\` int NOT NULL AUTO_INCREMENT, \`key\` varchar(250) NOT NULL, \`storeKey\` varchar(250) NOT NULL, INDEX \`_jackson_index_key\` (\`key\`), INDEX \`_jackson_index_store\` (\`storeKey\`), INDEX \`_jackson_index_key_store\` (\`key\`, \`storeKey\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
10
+ }
11
+
12
+ public async down(queryRunner: QueryRunner): Promise<void> {
13
+ await queryRunner.query(`DROP INDEX \`_jackson_index_key_store\` ON \`jackson_index\``);
14
+ await queryRunner.query(`DROP INDEX \`_jackson_index_store\` ON \`jackson_index\``);
15
+ await queryRunner.query(`DROP INDEX \`_jackson_index_key\` ON \`jackson_index\``);
16
+ await queryRunner.query(`DROP TABLE \`jackson_index\``);
17
+ await queryRunner.query(`DROP INDEX \`_jackson_ttl_expires_at\` ON \`jackson_ttl\``);
18
+ await queryRunner.query(`DROP TABLE \`jackson_ttl\``);
19
+ await queryRunner.query(`DROP TABLE \`jackson_store\``);
20
+ }
21
+
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boxyhq/saml-jackson",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "SAML Jackson library",
5
5
  "keywords": [
6
6
  "SAML 2.0"
@@ -20,9 +20,11 @@
20
20
  "build": "tsc -p tsconfig.build.json",
21
21
  "db:migration:generate:postgres": "ts-node --transpile-only ./node_modules/typeorm/cli.js migration:generate -d typeorm.ts migration/postgres/pg_${MIGRATION_NAME}",
22
22
  "db:migration:generate:mysql": "cross-env DB_TYPE=mysql DB_URL=mysql://root:mysql@localhost:3307/mysql ts-node --transpile-only ./node_modules/typeorm/cli.js migration:generate -d typeorm.ts migration/mysql/ms_${MIGRATION_NAME}",
23
+ "db:migration:generate:planetscale": "cross-env DB_ENGINE=planetscale DB_URL=mysql://root:mysql@localhost:3307/mysql ts-node --transpile-only ./node_modules/typeorm/cli.js migration:generate -d typeorm.ts migration/mysql/ms_${MIGRATION_NAME}",
23
24
  "db:migration:generate:mariadb": "cross-env DB_TYPE=mariadb DB_URL=mariadb://root@localhost:3306/mysql ts-node --transpile-only ./node_modules/typeorm/cli.js migration:generate -d typeorm.ts migration/mariadb/md_${MIGRATION_NAME}",
24
25
  "db:migration:run:postgres": "ts-node --transpile-only ./node_modules/typeorm/cli.js migration:run -d typeorm.ts",
25
26
  "db:migration:run:mysql": "cross-env DB_TYPE=mysql DB_URL=mysql://root:mysql@localhost:3307/mysql ts-node --transpile-only ./node_modules/typeorm/cli.js migration:run -d typeorm.ts",
27
+ "db:migration:run:planetscale": "cross-env DB_SSL=true DB_ENGINE=planetscale DB_URL=${PLANETSCALE_URL} ts-node --transpile-only ./node_modules/typeorm/cli.js migration:run -d typeorm.ts",
26
28
  "db:migration:run:mariadb": "cross-env DB_TYPE=mariadb DB_URL=mariadb://root@localhost:3306/mysql ts-node --transpile-only ./node_modules/typeorm/cli.js migration:run -d typeorm.ts",
27
29
  "prepublishOnly": "npm run build",
28
30
  "test": "tap --ts --timeout=100 --coverage test/**/*.test.ts",