@midwayjs/express-session 3.0.0-beta.10

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/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [3.0.0-beta.10](https://github.com/midwayjs/midway/compare/v3.0.0-beta.9...v3.0.0-beta.10) (2021-12-20)
7
+
8
+
9
+ ### Features
10
+
11
+ * default add session & bodyparser support for koa/express/faas ([#1420](https://github.com/midwayjs/midway/issues/1420)) ([cdaff31](https://github.com/midwayjs/midway/commit/cdaff317c3e862a95494a167995a28280af639bf))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013 - Now midwayjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # @midwayjs/express-session
2
+
3
+ Session component for @midwayjs/express
4
+
5
+
6
+ ## Install
7
+
8
+ ```bash
9
+ $ npm i @midwayjs/express-session --save
10
+ $ npm i @types/express-session --save-dev
11
+ ```
12
+
13
+
14
+ ## Usage
15
+
16
+ @midwayjs/express has enabled this component by default.
17
+
18
+ We use [cookie-session](https://github.com/expressjs/cookie-session) to keep session by default and use [express-session](https://github.com/expressjs/session) when set custom session store.
19
+
20
+ ## Config
21
+
22
+ You can configure session in your `config.*.ts`.
23
+
24
+ default value.
25
+
26
+ ```ts
27
+ export const session = {
28
+ secret: undefined, // must be set in application
29
+ name: 'MW_SESS',
30
+ cookie: {
31
+ maxAge: 24 * 3600 * 1000, // ms
32
+ httpOnly: true,
33
+ // sameSite: null,
34
+ },
35
+ };
36
+ ```
37
+
38
+ You can set 'session.cookie' for cookie-session config.
39
+
40
+ ## Custom Session Store
41
+
42
+ You can use compatible session store [here](https://github.com/expressjs/session#compatible-session-stores).
43
+
44
+ Let's give an example for [memorystore](https://github.com/roccomuso/memorystore).
45
+
46
+ ```ts
47
+ import { Configuration, Inject } from '@midwayjs/decorator';
48
+ import * as session from '@midwayjs/express-session';
49
+ import MemoryStore = require('memorystore');
50
+
51
+ @Configuration({
52
+ imports: [
53
+ express,
54
+ session,
55
+ ],
56
+ //...
57
+ })
58
+ export class AutoConfiguration {
59
+ @Inject()
60
+ sessionStoreManager: session.SessionStoreManager;
61
+
62
+ async onReady() {
63
+ this.sessionStoreManager.setSessionStore(MemoryStore, {
64
+ checkPeriod: 86400000 // prune expired entries every 24h
65
+ });
66
+ }
67
+ }
68
+ ```
69
+
70
+ ## Questions & Suggestions
71
+
72
+ Please open an issue [here](https://github.com/midwayjs/midway/issues/).
73
+
74
+ ## License
75
+
76
+ MIT
@@ -0,0 +1,5 @@
1
+ import { SessionOptions } from 'express-session';
2
+ export declare const session: SessionOptions & {
3
+ enable: boolean;
4
+ };
5
+ //# sourceMappingURL=config.default.d.ts.map
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.session = void 0;
4
+ exports.session = {
5
+ enable: true,
6
+ secret: undefined,
7
+ name: 'MW_SESS',
8
+ resave: true,
9
+ saveUninitialized: true,
10
+ cookie: {
11
+ maxAge: 24 * 3600 * 1000,
12
+ httpOnly: true,
13
+ // sameSite: null,
14
+ },
15
+ };
16
+ //# sourceMappingURL=config.default.js.map
@@ -0,0 +1,6 @@
1
+ import { MidwayApplicationManager } from '@midwayjs/core';
2
+ export declare class SessionConfiguration {
3
+ applicationManager: MidwayApplicationManager;
4
+ onReady(): Promise<void>;
5
+ }
6
+ //# sourceMappingURL=configuration.d.ts.map
@@ -0,0 +1,39 @@
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
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SessionConfiguration = void 0;
13
+ const decorator_1 = require("@midwayjs/decorator");
14
+ const DefaultConfig = require("./config/config.default");
15
+ const session_1 = require("./middleware/session");
16
+ const core_1 = require("@midwayjs/core");
17
+ let SessionConfiguration = class SessionConfiguration {
18
+ async onReady() {
19
+ this.applicationManager.getApplications(['express']).forEach(app => {
20
+ app.useMiddleware(session_1.SessionMiddleware);
21
+ });
22
+ }
23
+ };
24
+ __decorate([
25
+ (0, decorator_1.Inject)(),
26
+ __metadata("design:type", core_1.MidwayApplicationManager)
27
+ ], SessionConfiguration.prototype, "applicationManager", void 0);
28
+ SessionConfiguration = __decorate([
29
+ (0, decorator_1.Configuration)({
30
+ namespace: 'session',
31
+ importConfigs: [
32
+ {
33
+ default: DefaultConfig,
34
+ },
35
+ ],
36
+ })
37
+ ], SessionConfiguration);
38
+ exports.SessionConfiguration = SessionConfiguration;
39
+ //# sourceMappingURL=configuration.js.map
@@ -0,0 +1,4 @@
1
+ export { SessionConfiguration as Configuration } from './configuration';
2
+ export * from './middleware/session';
3
+ export * from './store';
4
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.Configuration = void 0;
14
+ var configuration_1 = require("./configuration");
15
+ Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.SessionConfiguration; } });
16
+ __exportStar(require("./middleware/session"), exports);
17
+ __exportStar(require("./store"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ import { IMiddleware, MidwayConfigService } from '@midwayjs/core';
2
+ import { SessionStoreManager } from '../store';
3
+ export declare class SessionMiddleware implements IMiddleware<any, any> {
4
+ sessionConfig: any;
5
+ logger: any;
6
+ sessionStoreManager: SessionStoreManager;
7
+ configService: MidwayConfigService;
8
+ resolve(): any;
9
+ }
10
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1,66 @@
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
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SessionMiddleware = void 0;
13
+ const decorator_1 = require("@midwayjs/decorator");
14
+ const core_1 = require("@midwayjs/core");
15
+ const session = require("express-session");
16
+ const cookieSession = require("cookie-session");
17
+ const store_1 = require("../store");
18
+ let SessionMiddleware = class SessionMiddleware {
19
+ resolve() {
20
+ var _a, _b;
21
+ if (this.sessionConfig.enable) {
22
+ const secret = (_b = (_a = this.sessionConfig.secret) !== null && _a !== void 0 ? _a : this.configService.getConfiguration('express.keys')) !== null && _b !== void 0 ? _b : this.configService.getConfiguration('keys');
23
+ if (!secret) {
24
+ throw new core_1.MidwayConfigMissingError('config.session.secret');
25
+ }
26
+ this.sessionConfig.secret = [].concat(secret);
27
+ if (!this.sessionConfig.cookie.httpOnly) {
28
+ this.logger.warn('[midway-session]: please set `config.session.cookie.httpOnly` to true. It is very dangerous if session can read by client JavaScript.');
29
+ }
30
+ const store = this.sessionStoreManager.getSessionStore(session);
31
+ if (store) {
32
+ this.sessionConfig.store = store;
33
+ }
34
+ if (!this.sessionConfig.store) {
35
+ return cookieSession(Object.assign(this.sessionConfig.cookie, {
36
+ keys: this.sessionConfig.secret,
37
+ name: this.sessionConfig.name,
38
+ }));
39
+ }
40
+ else {
41
+ return session(this.sessionConfig);
42
+ }
43
+ }
44
+ }
45
+ };
46
+ __decorate([
47
+ (0, decorator_1.Config)('session'),
48
+ __metadata("design:type", Object)
49
+ ], SessionMiddleware.prototype, "sessionConfig", void 0);
50
+ __decorate([
51
+ (0, decorator_1.Logger)(),
52
+ __metadata("design:type", Object)
53
+ ], SessionMiddleware.prototype, "logger", void 0);
54
+ __decorate([
55
+ (0, decorator_1.Inject)(),
56
+ __metadata("design:type", store_1.SessionStoreManager)
57
+ ], SessionMiddleware.prototype, "sessionStoreManager", void 0);
58
+ __decorate([
59
+ (0, decorator_1.Inject)(),
60
+ __metadata("design:type", core_1.MidwayConfigService)
61
+ ], SessionMiddleware.prototype, "configService", void 0);
62
+ SessionMiddleware = __decorate([
63
+ (0, decorator_1.Middleware)()
64
+ ], SessionMiddleware);
65
+ exports.SessionMiddleware = SessionMiddleware;
66
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1,8 @@
1
+ export declare class SessionStoreManager {
2
+ private sessionStoreClz;
3
+ private sessionStore;
4
+ private sessionStoreOptions;
5
+ setSessionStore(sessionStore: any, options: any): void;
6
+ getSessionStore(session?: any): any;
7
+ }
8
+ //# sourceMappingURL=store.d.ts.map
package/dist/store.js ADDED
@@ -0,0 +1,28 @@
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.SessionStoreManager = void 0;
10
+ const decorator_1 = require("@midwayjs/decorator");
11
+ let SessionStoreManager = class SessionStoreManager {
12
+ setSessionStore(sessionStore, options) {
13
+ this.sessionStoreClz = sessionStore;
14
+ this.sessionStoreOptions = options;
15
+ }
16
+ getSessionStore(session) {
17
+ if (!this.sessionStore && this.sessionStoreClz) {
18
+ this.sessionStore = new (this.sessionStoreClz(session))(this.sessionStoreOptions);
19
+ }
20
+ return this.sessionStore;
21
+ }
22
+ };
23
+ SessionStoreManager = __decorate([
24
+ (0, decorator_1.Provide)(),
25
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
26
+ ], SessionStoreManager);
27
+ exports.SessionStoreManager = SessionStoreManager;
28
+ //# sourceMappingURL=store.js.map
package/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { SessionOptions } from 'express-session';
2
+ export * from './dist/index';
3
+
4
+ declare module '@midwayjs/core/dist/interface' {
5
+ interface MidwayConfig {
6
+ session: Partial<
7
+ SessionOptions & {
8
+ enable: boolean;
9
+ }
10
+ >;
11
+ }
12
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@midwayjs/express-session",
3
+ "description": "midway session component for express",
4
+ "version": "3.0.0-beta.10",
5
+ "main": "dist/index",
6
+ "typings": "index.d.ts",
7
+ "files": [
8
+ "dist/**/*.js",
9
+ "dist/**/*.d.ts",
10
+ "index.d.ts"
11
+ ],
12
+ "devDependencies": {
13
+ "@midwayjs/core": "^3.0.0-beta.10",
14
+ "@midwayjs/decorator": "^3.0.0-beta.10",
15
+ "@midwayjs/mock": "^3.0.0-beta.10",
16
+ "@types/express-session": "^1.17.4",
17
+ "memorystore": "^1.6.6"
18
+ },
19
+ "dependencies": {
20
+ "cookie-session": "^1.4.0",
21
+ "express-session": "^1.17.2"
22
+ },
23
+ "keywords": [
24
+ "midway",
25
+ "web",
26
+ "express",
27
+ "session"
28
+ ],
29
+ "author": "czy88840616 <czy88840616@gmail.com>",
30
+ "license": "MIT",
31
+ "scripts": {
32
+ "build": "tsc",
33
+ "test": "node --require=ts-node/register ../../node_modules/.bin/jest",
34
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit",
35
+ "ci": "npm run test",
36
+ "lint": "mwts check"
37
+ },
38
+ "engines": {
39
+ "node": ">=12"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/midwayjs/midway.git"
44
+ },
45
+ "gitHead": "153870f2e2dd6b17673ec7591e49224a6bd51b36"
46
+ }