@midwayjs/express-session 3.0.0-beta.9
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 +76 -0
- package/dist/config/config.default.d.ts +3 -0
- package/dist/config/config.default.js +15 -0
- package/dist/configuration.d.ts +6 -0
- package/dist/configuration.js +39 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +18 -0
- package/dist/middleware/session.d.ts +10 -0
- package/dist/middleware/session.js +64 -0
- package/dist/store.d.ts +8 -0
- package/dist/store.js +28 -0
- package/index.d.ts +8 -0
- package/package.json +45 -0
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,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.session = void 0;
|
|
4
|
+
exports.session = {
|
|
5
|
+
secret: undefined,
|
|
6
|
+
name: 'MW_SESS',
|
|
7
|
+
resave: true,
|
|
8
|
+
saveUninitialized: true,
|
|
9
|
+
cookie: {
|
|
10
|
+
maxAge: 24 * 3600 * 1000,
|
|
11
|
+
httpOnly: true,
|
|
12
|
+
// sameSite: null,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=config.default.js.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
|
package/dist/index.d.ts
ADDED
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,64 @@
|
|
|
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
|
+
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');
|
|
22
|
+
if (!secret) {
|
|
23
|
+
throw new core_1.MidwayConfigMissingError('config.session.secret');
|
|
24
|
+
}
|
|
25
|
+
this.sessionConfig.secret = [].concat(secret);
|
|
26
|
+
if (!this.sessionConfig.cookie.httpOnly) {
|
|
27
|
+
this.logger.warn('[midway-session]: please set `config.session.cookie.httpOnly` to true. It is very dangerous if session can read by client JavaScript.');
|
|
28
|
+
}
|
|
29
|
+
const store = this.sessionStoreManager.getSessionStore(session);
|
|
30
|
+
if (store) {
|
|
31
|
+
this.sessionConfig.store = store;
|
|
32
|
+
}
|
|
33
|
+
if (!this.sessionConfig.store) {
|
|
34
|
+
return cookieSession(Object.assign(this.sessionConfig.cookie, {
|
|
35
|
+
keys: this.sessionConfig.secret,
|
|
36
|
+
name: this.sessionConfig.name,
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return session(this.sessionConfig);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
__decorate([
|
|
45
|
+
(0, decorator_1.Config)('session'),
|
|
46
|
+
__metadata("design:type", Object)
|
|
47
|
+
], SessionMiddleware.prototype, "sessionConfig", void 0);
|
|
48
|
+
__decorate([
|
|
49
|
+
(0, decorator_1.Logger)(),
|
|
50
|
+
__metadata("design:type", Object)
|
|
51
|
+
], SessionMiddleware.prototype, "logger", void 0);
|
|
52
|
+
__decorate([
|
|
53
|
+
(0, decorator_1.Inject)(),
|
|
54
|
+
__metadata("design:type", store_1.SessionStoreManager)
|
|
55
|
+
], SessionMiddleware.prototype, "sessionStoreManager", void 0);
|
|
56
|
+
__decorate([
|
|
57
|
+
(0, decorator_1.Inject)(),
|
|
58
|
+
__metadata("design:type", core_1.MidwayConfigService)
|
|
59
|
+
], SessionMiddleware.prototype, "configService", void 0);
|
|
60
|
+
SessionMiddleware = __decorate([
|
|
61
|
+
(0, decorator_1.Middleware)()
|
|
62
|
+
], SessionMiddleware);
|
|
63
|
+
exports.SessionMiddleware = SessionMiddleware;
|
|
64
|
+
//# sourceMappingURL=session.js.map
|
package/dist/store.d.ts
ADDED
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
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/express-session",
|
|
3
|
+
"description": "midway session component for express",
|
|
4
|
+
"version": "3.0.0-beta.9",
|
|
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.9",
|
|
14
|
+
"@midwayjs/decorator": "^3.0.0-beta.9",
|
|
15
|
+
"@midwayjs/mock": "^3.0.0-beta.9",
|
|
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
|
+
}
|