@boxyhq/saml-jackson 0.1.4-beta.88 → 0.1.5-beta.102

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/README.md CHANGED
@@ -1,25 +1,34 @@
1
1
  # SAML Jackson (not fiction anymore)
2
-
2
+
3
3
  SAML service [SAML in a box from BoxyHQ]
4
-
4
+
5
5
  You need someone like Jules Winnfield to save you from the vagaries of SAML login.
6
-
7
- ## Source code visualizer
6
+
7
+ # Source code visualizer
8
8
  [CodeSee codebase visualizer](https://app.codesee.io/maps/public/53e91640-23b5-11ec-a724-79d7dd589517)
9
-
10
- # npm library
11
- Jackson is available as an npm library (https://www.npmjs.com/package/@boxyhq/saml-jackson) that can be integrated into express.js routes. In theory, the library should be usable with other node.js web application frameworks but is currently untested. Please file an issue or submit a PR if you encounter any issues.
12
-
13
- Refer to https://github.com/boxyhq/jackson#configuration for the configuration options to pass to the library.
14
-
15
- Here's how to use the npm library:
9
+
10
+ # Getting Started
11
+
12
+ There are two ways to use this repo.
13
+ - As an npm library (for Express compatible frameworks)
14
+ - As a separate service
15
+
16
+ ## Install as an npm library
17
+ Jackson is available as an [npm package](https://www.npmjs.com/package/@boxyhq/saml-jackson) that can be integrated into Express.js routes. The library should be usable with other node.js web application frameworks but is currently untested. Please file an issue or submit a PR if you encounter any issues.
18
+
19
+ ```
20
+ npm i @boxyhq/saml-jackson
21
+ ```
22
+
23
+ ### Add Express Routes
24
+
16
25
  ```
17
26
  // express
18
27
  const express = require('express');
19
28
  const router = express.Router();
20
29
  const cors = require('cors'); // needed if you are calling the token userinfo endpoints from the frontend
21
-
22
- // Set the required options
30
+
31
+ // Set the required options. Refer to https://github.com/boxyhq/jackson#configuration for the full list
23
32
  const opts = {
24
33
  externalUrl: 'https://my-cool-app.com',
25
34
  samlAudience: 'https://my-cool-app.com',
@@ -29,7 +38,7 @@ const opts = {
29
38
  url: 'mongodb://localhost:27017/my-cool-app',
30
39
  }
31
40
  };
32
-
41
+
33
42
  // Please note that the initialization of @boxyhq/saml-jackson is async, you cannot run it at the top level
34
43
  // Run this in a function where you initialise the express server.
35
44
  async function init() {
@@ -37,17 +46,17 @@ async function init() {
37
46
  const apiController = ret.apiController;
38
47
  const oauthController = ret.oauthController;
39
48
  }
40
-
49
+
41
50
  // express.js middlewares needed to parse json and x-www-form-urlencoded
42
51
  router.use(express.json());
43
52
  router.use(express.urlencoded({ extended: true }));
44
-
53
+
45
54
  // SAML config API. You should pass this route through your authentication checks, do not expose this on the public interface without proper authentication in place.
46
55
  router.post('/api/v1/saml/config', async (req, res) => {
47
56
  try {
48
57
  // apply your authentication flow (or ensure this route has passed through your auth middleware)
49
58
  ...
50
-
59
+
51
60
  // only when properly authenticated, call the config function
52
61
  res.json(await apiController.config(req.body));
53
62
  } catch (err) {
@@ -56,7 +65,7 @@ router.post('/api/v1/saml/config', async (req, res) => {
56
65
  });
57
66
  }
58
67
  });
59
-
68
+
60
69
  // OAuth 2.0 flow
61
70
  router.get('/oauth/authorize', async (req, res) => {
62
71
  try {
@@ -65,7 +74,7 @@ router.get('/oauth/authorize', async (req, res) => {
65
74
  res.status(500).send(err.message);
66
75
  }
67
76
  });
68
-
77
+
69
78
  router.post('/oauth/saml', async (req, res) => {
70
79
  try {
71
80
  await oauthController.samlResponse(req, res);
@@ -73,7 +82,7 @@ router.post('/oauth/saml', async (req, res) => {
73
82
  res.status(500).send(err.message);
74
83
  }
75
84
  });
76
-
85
+
77
86
  router.post('/oauth/token', cors(), async (req, res) => {
78
87
  try {
79
88
  await oauthController.token(req, res);
@@ -81,7 +90,7 @@ router.post('/oauth/token', cors(), async (req, res) => {
81
90
  res.status(500).send(err.message);
82
91
  }
83
92
  });
84
-
93
+
85
94
  router.get('/oauth/userinfo', cors(), async (req, res) => {
86
95
  try {
87
96
  await oauthController.userInfo(req, res);
@@ -89,71 +98,35 @@ router.get('/oauth/userinfo', cors(), async (req, res) => {
89
98
  res.status(500).send(err.message);
90
99
  }
91
100
  });
92
-
101
+
93
102
  // set the router
94
103
  app.user('/sso', router);
95
-
104
+
96
105
  ```
97
- You can also refer to our usage of the library internally in the Jackson service here - https://github.com/boxyhq/jackson/blob/main/src/jackson.js
98
-
99
- # Deployment
100
- The docker container can be found at https://hub.docker.com/r/boxyhq/jackson/tags. It is preferable to use a specific version instead of the `latest` tag. Jackson uses two ports (configurable if needed, see below) 5000 and 6000. 6000 is the internal port and ideally should not be exposed to a public network.
101
-
102
- Example of a docker run:
106
+
107
+ ## Deployment as a service: Docker
108
+ The docker container can be found at [boxyhq/jackson](https://hub.docker.com/r/boxyhq/jackson/tags). It is preferable to use a specific version instead of the `latest` tag. Jackson uses two ports (configurable if needed, see below) 5000 and 6000. 6000 is the internal port and ideally should not be exposed to a public network.
109
+
103
110
  ```
104
111
  docker run -p 5000:5000 -p 6000:6000 boxyhq/jackson:78e9099d
105
112
  ```
106
113
 
107
- # Database Support
108
- Jackson currently supports SQL databases (Postgres, CockroachDB, MySQL and MariaDB), MongoDB and Redis.
109
-
110
- # Configuration
111
- Configuration is done via env vars (and in the case of the npm library via an options object). The following options are supported and will have to be configured during deployment:
112
- - HOST_URL: The URL to bind to, defaults to `localhost`
113
- - HOST_PORT: The port to bind to, defaults to `5000`
114
- - EXTERNAL_URL (npm: externalUrl): The public URL to reach this service, used internally for documenting the SAML configuration instructions. Defaults to `http://{HOST_URL}:{HOST_PORT}` for Jackson service, required for npm library
115
- - INTERNAL_HOST_URL: The URL to bind to expose the internal APIs, defaults to `localhost`. Do not configure this to a public network
116
- - INTERNAL_HOST_PORT: The port to bind to for the internal APIs, defaults to `6000`
117
- - SAML_AUDIENCE (npm: samlAudience): This is just an identitifer to validate the SAML audience, this value will also get configured in the SAML apps created by your customers. Once set do not change this value unless you get your customers to reconfigure their SAML again. Defaults to `https://saml.boxyhq.com` and is case sensitive. This does not have be a real URL
118
- - IDP_ENABLED (npm: idpEnabled): Set to `true` to enable IdP initiated login for SAML. SP initiated login is the only recommended flow but you might have to support IdP login at times. Defaults to `false`
119
- - DB_ENGINE (npm: db.engine): Supported values are `redis`, `sql`, `mongo`, `mem`. Defaults to `sql`
120
- - DB_URL (npm: db.url): The database URL to connect to, for example `postgres://postgres:postgres@localhost:5450/jackson`
121
- - DB_TYPE (npm: db.type): Only needed when DB_ENGINE is `sql`. Supported values are `postgres`, `cockroachdb`, `mysql`, `mariadb`. Defaults to `postgres`
122
- - PRE_LOADED_CONFIG: If you only need a single tenant or a handful of pre-configured tenants then this config will help you read and load SAML configs. It works well with the mem DB engine so you don't have to configure any external databases for this to work (though it works with those as well). This is a path (absolute or relative) to a directory that contains files organized in the format described in the next section.
114
+ Refer to https://github.com/boxyhq/jackson#configuration for the full configuration.
123
115
 
124
- # Pre-loaded SAML Configuration
125
- If PRE_LOADED_CONFIG is set then it should point to a directory with the following structure (example below):-
126
- ```
127
- boxyhq.js
128
- boxyhq.xml
129
- anothertenant.js
130
- anothertenant.xml
131
- ```
132
- The JS file has the following structure:-
133
- ```
134
- module.exports = {
135
- defaultRedirectUrl: 'http://localhost:3000/login/saml',
136
- redirectUrl: '["http://localhost:3000/*", "http://localhost:5000/*"]',
137
- tenant: 'boxyhq.com',
138
- product: 'demo',
139
- };
140
- ```
141
- The XML file (should share the name with the .js file) is the raw XML metadata file you receive from your Identity Provider. Please ensure it is saved in the `utf-8` encoding.
142
-
143
- The config and XML above correspond to the `SAML API config` (see below).
144
-
145
- # SAML Login flows
146
- There are two kinds of SAML login flows - SP-initiated and IdP-initiated. We highly recommend sticking to the SP-initiated flow since it is more secure but Jackson also supports the IdP-initiated flow if you enable it. For an in-depth understanding of SAML and the two flows please refer to Okta's comprehensive guide - https://developer.okta.com/docs/concepts/saml/.
147
-
148
- # Setting up SAML with your customer's Identity Provider
149
- Please follow the instructions here to guide your customer's in setting up SAML correctly for your product(s). You should create a copy of the doc and modify it with your custom settings, we have used the values that work for our demo apps - https://docs.google.com/document/d/1fk---Z9Ln59u-2toGKUkyO3BF6Dh3dscT2u4J2xHANE.
150
-
151
- # SAML config API
152
- Once your customer has set up the SAML app on their Identity Provider, the Identity Provider will generate an IdP or SP metadata file. Some Identity Providers only generate an IdP metadata file but it usually works for the SP login flow as well. It is an XML file that contains various attributes Jackson needs in order to validate incoming SAML login requests. This step is the equivalent of setting an OAuth 2.0 app and generating a client ID and client secret that will be used in the login flow.
116
+ Kubernetes and docker-compose deployment files will be coming soon.
153
117
 
118
+ ## Usage
119
+
120
+ ### 1. Setting up SAML with your customer's Identity Provider
121
+ Please follow the instructions [here](https://docs.google.com/document/d/1fk---Z9Ln59u-2toGKUkyO3BF6Dh3dscT2u4J2xHANE) to guide your customers in setting up SAML correctly for your product(s). You should create a copy of the doc and modify it with your custom settings, we have used the values that work for our demo apps.
122
+
123
+ ### 2. SAML config API
124
+ Once your customer has set up the SAML app on their Identity Provider, the Identity Provider will generate an IdP or SP metadata file. Some Identity Providers only generate an IdP metadata file but it usually works for the SP login flow as well. It is an XML file that contains various attributes Jackson needs to validate incoming SAML login requests. This step is the equivalent of setting an OAuth 2.0 app and generating a client ID and client secret that will be used in the login flow.
125
+
154
126
  You will need to provide a place in the UI for your customers (The account settings page is usually a good place for this) to configure this and then call the API below.
155
-
127
+
156
128
  The following API call sets up the configuration in Jackson:
129
+
157
130
  ```
158
131
  curl --location --request POST 'http://localhost:6000/api/v1/saml/config' \
159
132
  --header 'Content-Type: application/x-www-form-urlencoded' \
@@ -163,22 +136,23 @@ curl --location --request POST 'http://localhost:6000/api/v1/saml/config' \
163
136
  --data-urlencode 'tenant=boxyhq.com' \
164
137
  --data-urlencode 'product=demo'
165
138
  ```
166
-
139
+
167
140
  - rawMetadata: The XML metadata file your customer gets from their Identity Provider
168
141
  - defaultRedirectUrl: The redirect URL to use in the IdP login flow. Jackson will call this URL after completing an IdP login flow
169
142
  - redirectUrl: JSON encoded array containing a list of allowed redirect URLs. Jackson will disallow any redirects not on this list (or not the default URL above)
170
- - tenant: Jackson supports a multi-tenant architecture, this is a unique identifier you set from your side that relates back to your customer's tenant. This is normally an email, domain, an account id, or user id
143
+ - tenant: Jackson supports a multi-tenant architecture, this is a unique identifier you set from your side that relates back to your customer's tenant. This is normally an email, domain, an account id, or user-id
171
144
  - product: Jackson support multiple products, this is a unique identifier you set from your side that relates back to the product your customer is using
172
-
145
+
173
146
  The response returns a JSON with `client_id` and `client_secret` that can be stored against your tenant and product for a more secure OAuth 2.0 flow. If you do not want to store the `client_id` and `client_secret` you can alternatively use `client_id=tentant=<tenantID>&product=<productID>` and any arbitrary value for `client_secret` when setting up the OAuth 2.0 flow.
174
-
175
- # OAuth 2.0 Flow
147
+
148
+ ### 3. OAuth 2.0 Flow
176
149
  Jackson has been designed to abstract the SAML login flow as a pure OAuth 2.0 flow. This means it's compatible with any standard OAuth 2.0 library out there, both client-side and server-side. It is important to remember that SAML is configured per customer unlike OAuth 2.0 where you can have a single OAuth app supporting logins for all customers.
177
-
150
+
178
151
  Jackson also supports the PKCE authorization flow (https://oauth.net/2/pkce/), so you can protect your SPAs.
179
-
152
+
180
153
  If for any reason you need to implement the flow on your own, the steps are outlined below:
181
- ## Authorize
154
+
155
+ ### 4. Authorize
182
156
  The OAuth flow begins with redirecting your user to the `authorize` URL:
183
157
  ```
184
158
  https://localhost:5000/oauth/authorize
@@ -187,15 +161,15 @@ https://localhost:5000/oauth/authorize
187
161
  &redirect_uri=<redirect URL>
188
162
  &state=<randomly generated state id>
189
163
  ```
190
-
164
+
191
165
  - response_type=code: This is the only supported type for now but maybe extended in the future
192
166
  - client_id: Use the client_id returned by the SAML config API or use `tentant=<tenantID>&product=<productID>` to use the tenant and product IDs instead
193
167
  - redirect_uri: This is where the user will be taken back once the authorization flow is complete
194
168
  - state: Use a randomly generated string as the state, this will be echoed back as a query parameter when taking the user back to the `redirect_uri` above. You should validate the state to prevent XSRF attacks
195
-
196
- ## Code exchange
197
- After successful authorization, the user is redirected back to the `redirect_uri`. The query parameters will include the `code` and `state` parameters. You should validate that the state matches the one you sent in the authorize request.
198
-
169
+
170
+ ### 5. Code Exchange
171
+ After successful authorization, the user is redirected back to the `redirect_uri`. The query parameters will include the `code` and `state` parameters. You should validate that the state matches the one you sent in the `authorize` request.
172
+
199
173
  The code can then be exchanged for a token by making the following request:
200
174
  ```
201
175
  curl --request POST \
@@ -211,7 +185,7 @@ curl --request POST \
211
185
  - client_id: Use the client_id returned by the SAML config API or use `tentant=<tenantID>&product=<productID>` to use the tenant and product IDs instead
212
186
  - client_secret: Use the client_secret returned by the SAML config API or any arbitrary value if using the tenant and product in the clientID
213
187
  - redirect_uri: This is where the user will be taken back once the authorization flow is complete. Use the same redirect_uri as the previous request
214
-
188
+
215
189
  If everything goes well you should receive a JSON response that includes the access token. This token is needed for the next step where we fetch the user profile.
216
190
  ```
217
191
  {
@@ -220,8 +194,8 @@ If everything goes well you should receive a JSON response that includes the acc
220
194
  "expires_in": 300
221
195
  }
222
196
  ```
223
-
224
- ## Profile request
197
+
198
+ ### 6. Profile Request
225
199
  The short-lived access token can now be used to request the user's profile. You'll need to make the following request:
226
200
  ```
227
201
  curl --request GET \
@@ -229,7 +203,7 @@ curl --request GET \
229
203
  --header 'authorization: Bearer <access token>' \
230
204
  --header 'content-type: application/json'
231
205
  ```
232
-
206
+
233
207
  If everything goes well you should receive a JSON response with the user's profile:
234
208
  ```
235
209
  {
@@ -239,8 +213,83 @@ If everything goes well you should receive a JSON response with the user's profi
239
213
  "lastName": "Jackson",
240
214
  }
241
215
  ```
242
-
216
+
243
217
  - email: The email address of the user as provided by the Identity Provider
244
218
  - id: The id of the user as provided by the Identity Provider
245
219
  - firstName: The first name of the user as provided by the Identity Provider
246
220
  - lastName: The last name of the user as provided by the Identity Provider
221
+
222
+ ## Examples
223
+ To Do
224
+
225
+ ## Database Support
226
+ Jackson currently supports the following databases.
227
+
228
+ - Postgres
229
+ - CockroachDB
230
+ - MySQL
231
+ - MariaDB
232
+ - MongoDB
233
+ - Redis
234
+
235
+ ## Configuration
236
+ Configuration is done via env vars (and in the case of the npm library via an options object).
237
+
238
+ The following options are supported and will have to be configured during deployment.
239
+
240
+ | Key | Description | Default |
241
+ |-----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------|
242
+ | HOST_URL | The URL to bind to | `localhost` |
243
+ | HOST_PORT | The port to bind to | `5000` |
244
+ | EXTERNAL_URL (npm: externalUrl) | The public URL to reach this service, used internally for documenting the SAML configuration instructions. | `http://{HOST_URL}:{HOST_PORT}` |
245
+ | INTERNAL_HOST_URL | The URL to bind to expose the internal APIs. Do not configure this to a public network. | `localhost` |
246
+ | INTERNAL_HOST_PORT | The port to bind to for the internal APIs. | `6000` |
247
+ | SAML_AUDIENCE (npm: samlAudience) | This is just an identifier to validate the SAML audience, this value will also get configured in the SAML apps created by your customers. Once set do not change this value unless you get your customers to reconfigure their SAML again. It is case-sensitive. This does not have to be a real URL. | `https://saml.boxyhq.com` |
248
+ | IDP_ENABLED (npm: idpEnabled) | Set to `true` to enable IdP initiated login for SAML. SP initiated login is the only recommended flow but you might have to support IdP login at times. | `false` |
249
+ | DB_ENGINE (npm: db.engine) | Supported values are `redis`, `sql`, `mongo`, `mem`. | `sql` |
250
+ | DB_URL (npm: db.url) | The database URL to connect to. For example `postgres://postgres:postgres@localhost:5450/jackson` | |
251
+ | DB_TYPE (npm: db.type) | Only needed when DB_ENGINE is `sql`. Supported values are `postgres`, `cockroachdb`, `mysql`, `mariadb`. | `postgres` |
252
+ | PRE_LOADED_CONFIG | If you only need a single tenant or a handful of pre-configured tenants then this config will help you read and load SAML configs. It works well with the mem DB engine so you don't have to configure any external databases for this to work (though it works with those as well). This is a path (absolute or relative) to a directory that contains files organized in the format described in the next section. | |
253
+
254
+ ## Pre-loaded SAML Configuration
255
+ If PRE_LOADED_CONFIG is set then it should point to a directory with the following structure (example below):-
256
+ ```
257
+ boxyhq.js
258
+ boxyhq.xml
259
+ anothertenant.js
260
+ anothertenant.xml
261
+ ```
262
+ The JS file has the following structure:-
263
+ ```
264
+ module.exports = {
265
+ defaultRedirectUrl: 'http://localhost:3000/login/saml',
266
+ redirectUrl: '["http://localhost:3000/*", "http://localhost:5000/*"]',
267
+ tenant: 'boxyhq.com',
268
+ product: 'demo',
269
+ };
270
+ ```
271
+ The XML file (should share the name with the .js file) is the raw XML metadata file you receive from your Identity Provider. Please ensure it is saved in the `utf-8` encoding.
272
+
273
+ The config and XML above correspond to the `SAML API config` (see below).
274
+
275
+ ## SAML Login flows
276
+ There are two kinds of SAML login flows - SP-initiated and IdP-initiated. We highly recommend sticking to the SP-initiated flow since it is more secure but Jackson also supports the IdP-initiated flow if you enable it. For an in-depth understanding of SAML and the two flows please refer to Okta's comprehensive guide - https://developer.okta.com/docs/concepts/saml/.
277
+
278
+ ## Contributing
279
+ Thanks for taking the time to contribute! Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are appreciated.
280
+
281
+ Please try to create bug reports that are:
282
+
283
+ - _Reproducible._ Include steps to reproduce the problem.
284
+ - _Specific._ Include as much detail as possible: which version, what environment, etc.
285
+ - _Unique._ Do not duplicate existing opened issues.
286
+ - _Scoped to a Single Bug._ One bug per report.
287
+
288
+ ## Support
289
+ Reach out to the maintainer at one of the following places:
290
+
291
+ - [GitHub Issues](https://github.com/boxyhq/jackson/issues)
292
+ - The email which is located [in GitHub profile](https://github.com/deepakprabhakara)
293
+
294
+ ## License
295
+ [Apache 2.0 License](https://github.com/boxyhq/jackson/blob/main/LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boxyhq/saml-jackson",
3
- "version": "0.1.4-beta.88",
3
+ "version": "0.1.5-beta.102",
4
4
  "license": "Apache 2.0",
5
5
  "description": "SAML 2.0 service",
6
6
  "main": "src/index.js",
@@ -32,27 +32,27 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@boxyhq/saml20": "0.2.0",
35
- "@peculiar/webcrypto": "1.1.7",
36
- "@peculiar/x509": "1.4.1",
35
+ "@peculiar/webcrypto": "1.2.2",
36
+ "@peculiar/x509": "1.6.0",
37
37
  "cors": "2.8.5",
38
38
  "express": "4.17.1",
39
- "mongodb": "4.1.3",
40
- "mysql2": "^2.3.3-rc.0",
39
+ "mongodb": "4.2.0",
40
+ "mysql2": "2.3.3",
41
41
  "pg": "8.7.1",
42
42
  "rambda": "6.9.0",
43
- "redis": "4.0.0-rc.3",
43
+ "redis": "4.0.0-rc.4",
44
44
  "reflect-metadata": "0.1.13",
45
45
  "ripemd160": "2.0.2",
46
46
  "thumbprint": "0.0.1",
47
- "typeorm": "0.2.38",
47
+ "typeorm": "0.2.41",
48
48
  "xml-crypto": "2.1.3",
49
49
  "xml2js": "0.4.23",
50
50
  "xmlbuilder": "15.1.1"
51
51
  },
52
52
  "devDependencies": {
53
53
  "cross-env": "7.0.3",
54
- "eslint": "^8.2.0",
55
- "nodemon": "2.0.13",
56
- "tap": "15.0.10"
54
+ "eslint": "8.3.0",
55
+ "nodemon": "2.0.15",
56
+ "tap": "15.1.2"
57
57
  }
58
58
  }
@@ -13,9 +13,11 @@ module.exports = new EntitySchema({
13
13
  },
14
14
  key: {
15
15
  type: 'varchar',
16
+ length: 1500,
16
17
  },
17
18
  storeKey: {
18
19
  type: 'varchar',
20
+ length: 1500,
19
21
  }
20
22
  },
21
23
  relations: {
@@ -1,20 +1,36 @@
1
1
  const EntitySchema = require('typeorm').EntitySchema;
2
2
  const JacksonStore = require('../model/JacksonStore.js');
3
3
 
4
- module.exports = new EntitySchema({
5
- name: 'JacksonStore',
6
- target: JacksonStore,
7
- columns: {
8
- key: {
9
- primary: true,
10
- type: 'varchar',
11
- },
12
- value: {
13
- type: 'varchar',
4
+ const valueType = (type) => {
5
+ switch (type) {
6
+ case 'postgres':
7
+ case 'cockroachdb':
8
+ return 'text';
9
+ case 'mysql':
10
+ case 'mariadb':
11
+ return 'mediumtext';
12
+ default:
13
+ return 'varchar';
14
+ }
15
+ };
16
+
17
+ module.exports = (type) => {
18
+ return new EntitySchema({
19
+ name: 'JacksonStore',
20
+ target: JacksonStore,
21
+ columns: {
22
+ key: {
23
+ primary: true,
24
+ type: 'varchar',
25
+ length: 1500,
26
+ },
27
+ value: {
28
+ type: valueType(type),
29
+ },
30
+ expiresAt: {
31
+ type: 'bigint',
32
+ nullable: true,
33
+ },
14
34
  },
15
- expiresAt: {
16
- type: 'bigint',
17
- nullable: true,
18
- }
19
- },
20
- });
35
+ });
36
+ };
package/src/db/sql/sql.js CHANGED
@@ -8,17 +8,28 @@ const dbutils = require('../utils.js');
8
8
  class Sql {
9
9
  constructor(options) {
10
10
  return (async () => {
11
- this.connection = await typeorm.createConnection({
12
- name: options.type,
13
- type: options.type,
14
- url: options.url,
15
- synchronize: true,
16
- logging: false,
17
- entities: [
18
- require('./entity/JacksonStore.js'),
19
- require('./entity/JacksonIndex.js'),
20
- ],
21
- });
11
+ while (true) {
12
+ try {
13
+ this.connection = await typeorm.createConnection({
14
+ name: options.type,
15
+ type: options.type,
16
+ url: options.url,
17
+ synchronize: true,
18
+ migrationsTableName: '_jackson_migrations',
19
+ logging: false,
20
+ entities: [
21
+ require('./entity/JacksonStore.js')(options.type),
22
+ require('./entity/JacksonIndex.js'),
23
+ ],
24
+ });
25
+
26
+ break;
27
+ } catch (err) {
28
+ console.error(`error connecting to ${options.type} db: ${err}`);
29
+ await dbutils.sleep(1000);
30
+ continue;
31
+ }
32
+ }
22
33
 
23
34
  this.storeRepository = this.connection.getRepository(JacksonStore);
24
35
  this.indexRepository = this.connection.getRepository(JacksonIndex);
@@ -45,7 +56,9 @@ class Sql {
45
56
 
46
57
  this.timerId = setTimeout(this.ttlCleanup, options.ttl * 1000);
47
58
  } else {
48
- console.log('Warning: ttl cleanup not enabled, set both "ttl" and "limit" options to enable it!')
59
+ console.log(
60
+ 'Warning: ttl cleanup not enabled, set both "ttl" and "limit" options to enable it!'
61
+ );
49
62
  }
50
63
 
51
64
  return this;
package/src/db/utils.js CHANGED
@@ -16,14 +16,15 @@ const keyFromParts = (...parts) => {
16
16
  return parts.join(':'); // TODO: pick a better strategy, keys can collide now
17
17
  };
18
18
 
19
+ const sleep = (ms) => {
20
+ return new Promise((resolve) => setTimeout(resolve, ms));
21
+ };
22
+
19
23
  module.exports = {
20
24
  key,
21
-
22
25
  keyForIndex,
23
-
24
26
  keyDigest,
25
-
26
27
  keyFromParts,
27
-
28
+ sleep,
28
29
  indexPrefix: '_index',
29
30
  };
package/src/index.js CHANGED
@@ -56,7 +56,8 @@ module.exports = async function (opts) {
56
56
  }
57
57
  }
58
58
 
59
- console.log(`Using engine: ${opts.db.engine}`);
59
+ const type = opts.db.type ? ' Type: ' + opts.db.type : '';
60
+ console.log(`Using engine: ${opts.db.engine}.${type}`);
60
61
 
61
62
  return {
62
63
  apiController,
package/src/jackson.js CHANGED
@@ -78,7 +78,7 @@ internalApp.post(apiPath + '/config', async (req, res) => {
78
78
 
79
79
  let internalServer = server;
80
80
  if (env.useInternalServer) {
81
- internalServer = internalApp.listen(env.internalPort, async () => {
81
+ internalServer = internalApp.listen(env.internalHostPort, async () => {
82
82
  console.log(
83
83
  `🚀 The path of the righteous internal server: http://${env.internalHostUrl}:${env.internalHostPort}`
84
84
  );