@ladjs/web 12.0.2 → 13.0.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.
Files changed (2) hide show
  1. package/index.js +45 -32
  2. package/package.json +8 -7
package/index.js CHANGED
@@ -12,6 +12,7 @@ const CacheResponses = require('@ladjs/koa-cache-responses');
12
12
  const I18N = require('@ladjs/i18n');
13
13
  const Koa = require('koa');
14
14
  const Meta = require('koa-meta');
15
+ const Passport = require('@ladjs/passport');
15
16
  const RedirectLoop = require('koa-redirect-loop');
16
17
  const Redis = require('@ladjs/redis');
17
18
  const StateHelper = require('@ladjs/state-helper');
@@ -70,7 +71,7 @@ const RATE_LIMIT_EXCEEDED = `Rate limit exceeded, retry in %s.`;
70
71
 
71
72
  class Web {
72
73
  // eslint-disable-next-line complexity
73
- constructor(config, client) {
74
+ constructor(config, Users) {
74
75
  this.config = {
75
76
  ...sharedConfig('WEB'),
76
77
  meta: {},
@@ -169,38 +170,50 @@ class Web {
169
170
  ...config
170
171
  };
171
172
 
172
- const cabin = new Cabin({
173
- logger: this.config.logger,
174
- ...this.config.cabin
175
- });
176
-
177
- // initialize redis
178
- this.client = client
179
- ? client
180
- : new Redis(this.config.redis, cabin, this.config.redisMonitor);
181
-
182
173
  // initialize the app
183
174
  const app = new Koa();
184
175
 
185
- // allow middleware to access redis client
176
+ // only trust proxy if enabled
177
+ app.proxy = boolean(process.env.TRUST_PROXY);
178
+
179
+ // inherit cache variable for cache-pug-templates
180
+ app.cache = boolean(this.config.views.locals.cache);
181
+
182
+ // initialize cabin
183
+ this.logger = _.isPlainObject(this.config.logger)
184
+ ? new Cabin(this.config.logger)
185
+ : this.config.logger instanceof Cabin
186
+ ? this.config.logger
187
+ : new Cabin({
188
+ logger: this.config.logger ? this.config.logger : console
189
+ });
190
+ app.context.logger = this.logger;
191
+
192
+ // initialize redis
193
+ this.client =
194
+ this.config.redis === false
195
+ ? false
196
+ : _.isPlainObject(this.config.redis)
197
+ ? new Redis(this.config.redis, this.logger, this.config.redisMonitor)
198
+ : this.config.redis;
186
199
  app.context.client = this.client;
187
200
 
188
- // listen for error and log events emitted by app
201
+ // expose passport
202
+ this.passport =
203
+ this.config.passport === false
204
+ ? false
205
+ : _.isPlainObject(this.config.passport)
206
+ ? new Passport(this.config.passport, Users)
207
+ : this.config.passport;
208
+ app.context.passport = this.passport;
209
+
210
+ // listen for errors emitted by app
189
211
  app.on('error', (err, ctx) => {
190
- const level = err.status && err.status < 500 ? 'warn' : 'error';
191
- if (ctx.logger) ctx.logger[level](err);
192
- else cabin[level](err);
212
+ ctx.logger[err.status && err.status < 500 ? 'warn' : 'error'](err);
193
213
  });
194
- app.on('log', cabin.log);
195
214
 
196
215
  // override koa's undocumented error handler
197
- app.context.onerror = errorHandler(this.config.cookiesKey, cabin);
198
-
199
- // only trust proxy if enabled
200
- app.proxy = boolean(process.env.TRUST_PROXY);
201
-
202
- // inherit cache variable for cache-pug-templates
203
- app.cache = boolean(this.config.views.locals.cache);
216
+ app.context.onerror = errorHandler(this.config.cookiesKey);
204
217
 
205
218
  // adds request received hrtime and date symbols to request object
206
219
  // (which is used by Cabin internally to add `request.timestamp` to logs
@@ -219,7 +232,7 @@ class Web {
219
232
  app.use(koaConnect(requestId()));
220
233
 
221
234
  // add cabin middleware
222
- app.use(cabin.middleware);
235
+ app.use(this.logger.middleware);
223
236
 
224
237
  // allow before hooks to get setup
225
238
  if (_.isFunction(this.config.hookBeforeSetup))
@@ -243,7 +256,7 @@ class Web {
243
256
  return ratelimit({
244
257
  ...this.config.rateLimit,
245
258
  db: this.client,
246
- logger: cabin,
259
+ logger: this.logger,
247
260
  errorMessage(exp) {
248
261
  const fn =
249
262
  typeof ctx.request.t === 'function' ? ctx.request.t : util.format;
@@ -266,7 +279,7 @@ class Web {
266
279
  // create new @ladjs/i18n instance
267
280
  const i18n = this.config.i18n.config
268
281
  ? this.config.i18n
269
- : new I18N({ ...this.config.i18n, logger: cabin });
282
+ : new I18N({ ...this.config.i18n, logger: this.logger });
270
283
 
271
284
  // setup localization (must come before `i18n.redirect`)
272
285
  app.use(i18n.middleware);
@@ -321,7 +334,7 @@ class Web {
321
334
  // NOTE: this must come after ctx.render is added (via koa-views)
322
335
  //
323
336
  if (this.config.meta) {
324
- const meta = new Meta(this.config.meta, cabin);
337
+ const meta = new Meta(this.config.meta, this.logger);
325
338
  app.use(meta.middleware);
326
339
  }
327
340
 
@@ -396,15 +409,15 @@ class Web {
396
409
  }
397
410
 
398
411
  // passport
399
- if (this.config.passport) {
400
- app.use(this.config.passport.initialize());
401
- app.use(this.config.passport.session());
412
+ if (this.passport) {
413
+ app.use(this.passport.initialize());
414
+ app.use(this.passport.session());
402
415
  }
403
416
 
404
417
  // store the user's last ip address in the background
405
418
  if (this.config.storeIPAddress) {
406
419
  const storeIPAddress = new StoreIPAddress({
407
- logger: cabin,
420
+ logger: this.logger,
408
421
  ...this.config.storeIPAddress
409
422
  });
410
423
  app.use(storeIPAddress.middleware);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ladjs/web",
3
3
  "description": "Web server for Lad",
4
- "version": "12.0.2",
4
+ "version": "13.0.0",
5
5
  "author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
6
6
  "ava": {
7
7
  "failFast": true,
@@ -28,7 +28,7 @@
28
28
  "@ladjs/koa-isajax": "^2.0.0",
29
29
  "@ladjs/koa-simple-ratelimit": "^3.0.0",
30
30
  "@ladjs/redis": "^1.0.7",
31
- "@ladjs/shared-config": "^7.0.0",
31
+ "@ladjs/shared-config": "^7.0.3",
32
32
  "@ladjs/state-helper": "^1.0.0",
33
33
  "@ladjs/store-ip-address": "^0.0.7",
34
34
  "boolean": "^3.2.0",
@@ -67,20 +67,21 @@
67
67
  "response-time": "^2.3.2"
68
68
  },
69
69
  "devDependencies": {
70
- "@commitlint/cli": "^17.0.1",
71
- "@commitlint/config-conventional": "^17.0.0",
72
- "ava": "^4.2.0",
70
+ "@commitlint/cli": "^17.0.2",
71
+ "@commitlint/config-conventional": "^17.0.2",
72
+ "@ladjs/passport": "^5.0.0",
73
+ "ava": "^4.3.0",
73
74
  "codecov": "^3.8.3",
74
75
  "cross-env": "^7.0.3",
75
76
  "eslint": "^8.16.0",
76
77
  "eslint-config-xo-lass": "^1.0.6",
77
78
  "fixpack": "^4.0.0",
78
79
  "husky": "^8.0.1",
79
- "lint-staged": "^12.4.3",
80
+ "lint-staged": "^13.0.0",
80
81
  "nyc": "^15.1.0",
81
82
  "pug": "^3.0.2",
82
83
  "remark-cli": "^10.0.1",
83
- "remark-preset-github": "^4.0.1",
84
+ "remark-preset-github": "^4.0.2",
84
85
  "supertest": "^6.2.3",
85
86
  "xo": "^0.49.0"
86
87
  },