@ladjs/web 14.0.8 → 16.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.
- package/index.js +50 -12
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -342,17 +342,54 @@ class Web {
|
|
|
342
342
|
const stateHelper = new StateHelper(this.config.views.locals);
|
|
343
343
|
app.use(stateHelper.middleware);
|
|
344
344
|
|
|
345
|
+
// add specific locals
|
|
346
|
+
app.use((ctx, next) => {
|
|
347
|
+
// passport-related helpers (e.g. for rendering log in with X buttons)
|
|
348
|
+
ctx.state.passport = ctx.passport ? {} : false;
|
|
349
|
+
if (
|
|
350
|
+
ctx.passport &&
|
|
351
|
+
ctx.passport.config &&
|
|
352
|
+
ctx.passport.config.providers
|
|
353
|
+
) {
|
|
354
|
+
for (const key of Object.keys(ctx.passport.config.providers)) {
|
|
355
|
+
ctx.state.passport[key] = boolean(ctx.passport.config.providers[key]);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// add limited `ctx` object to the state for views
|
|
360
|
+
ctx.state.ctx = {};
|
|
361
|
+
ctx.state.ctx.get = ctx.get;
|
|
362
|
+
ctx.state.ctx.locale = ctx.locale;
|
|
363
|
+
ctx.state.ctx.params = ctx.params;
|
|
364
|
+
ctx.state.ctx.path = ctx.path;
|
|
365
|
+
ctx.state.ctx.pathWithoutLocale = ctx.pathWithoutLocale;
|
|
366
|
+
ctx.state.ctx.query = ctx.query;
|
|
367
|
+
ctx.state.ctx.session = ctx.session;
|
|
368
|
+
ctx.state.ctx.sessionId = ctx.sessionId;
|
|
369
|
+
ctx.state.ctx.translate = ctx.translate;
|
|
370
|
+
ctx.state.ctx.url = ctx.url;
|
|
371
|
+
|
|
372
|
+
return next();
|
|
373
|
+
});
|
|
374
|
+
|
|
345
375
|
// session store
|
|
346
376
|
app.keys = this.config.sessionKeys;
|
|
347
|
-
app.use(
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
377
|
+
app.use(async (ctx, next) => {
|
|
378
|
+
try {
|
|
379
|
+
await session({
|
|
380
|
+
store: redisStore({ client: this.client }),
|
|
381
|
+
key: this.config.cookiesKey,
|
|
382
|
+
cookie: this.config.cookies,
|
|
383
|
+
genSid: this.config.genSid,
|
|
384
|
+
...this.config.session
|
|
385
|
+
})(ctx, () => Promise.resolve());
|
|
386
|
+
} catch (err) {
|
|
387
|
+
// this would indicate that redis is down
|
|
388
|
+
ctx.logger.error(err);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return next();
|
|
392
|
+
});
|
|
356
393
|
|
|
357
394
|
// redirect loop (must come after sessions added)
|
|
358
395
|
if (this.config.redirectLoop) {
|
|
@@ -400,11 +437,12 @@ class Web {
|
|
|
400
437
|
try {
|
|
401
438
|
await csrf(ctx, next);
|
|
402
439
|
} catch (err) {
|
|
440
|
+
ctx.logger.error(err);
|
|
403
441
|
let error = err;
|
|
404
|
-
|
|
442
|
+
// this would indicate that redis is down
|
|
443
|
+
if (!ctx.session) error = Boom.clientTimeout();
|
|
444
|
+
else if (err.name && err.name === 'ForbiddenError')
|
|
405
445
|
error = Boom.forbidden(err.message);
|
|
406
|
-
if (err.stack) error.stack = err.stack;
|
|
407
|
-
}
|
|
408
446
|
|
|
409
447
|
ctx.throw(error);
|
|
410
448
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ladjs/web",
|
|
3
3
|
"description": "Web server for Lad",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "16.0.0",
|
|
5
5
|
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/ladjs/web/issues",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"@ladjs/koa-simple-ratelimit": "^3.0.0",
|
|
21
21
|
"@ladjs/redis": "^1.0.7",
|
|
22
22
|
"@ladjs/shared-config": "^7.0.3",
|
|
23
|
-
"@ladjs/state-helper": "^
|
|
23
|
+
"@ladjs/state-helper": "^2.0.0",
|
|
24
24
|
"@ladjs/store-ip-address": "^0.0.7",
|
|
25
25
|
"boolean": "^3.2.0",
|
|
26
26
|
"cabin": "^9.1.2",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"koa-compress": "^5.1.0",
|
|
40
40
|
"koa-conditional-get": "^3.0.0",
|
|
41
41
|
"koa-connect": "^2.1.0",
|
|
42
|
-
"koa-csrf": "^
|
|
42
|
+
"koa-csrf": "^4.0.0",
|
|
43
43
|
"koa-etag": "^4.0.0",
|
|
44
44
|
"koa-favicon": "^2.1.0",
|
|
45
45
|
"koa-generic-session": "^2.3.0",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"pug": "^3.0.2",
|
|
75
75
|
"remark-cli": "^11.0.0",
|
|
76
76
|
"remark-preset-github": "^4.0.4",
|
|
77
|
-
"supertest": "^6.2.
|
|
77
|
+
"supertest": "^6.2.4",
|
|
78
78
|
"xo": "^0.50.0"
|
|
79
79
|
},
|
|
80
80
|
"engines": {
|