@midwayjs/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 +11 -0
- package/LICENSE +21 -0
- package/README.md +112 -0
- package/dist/config/config.default.d.ts +8 -0
- package/dist/config/config.default.js +12 -0
- package/dist/configuration.d.ts +8 -0
- package/dist/configuration.js +61 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +18 -0
- package/dist/middleware/session.d.ts +9 -0
- package/dist/middleware/session.js +44 -0
- package/dist/store.d.ts +11 -0
- package/dist/store.js +27 -0
- package/index.d.ts +12 -0
- package/package.json +45 -0
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,112 @@
|
|
|
1
|
+
# @midwayjs/session
|
|
2
|
+
|
|
3
|
+
Session component for @midwayjs/koa and @midwayjs/faas
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
$ npm i @midwayjs/session --save
|
|
10
|
+
$ npm i @types/koa-session --save-dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
@midwayjs/koa has enabled this component by default, @midwayjs/faas needs to be manually enabled.
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
// src/configuration.ts
|
|
20
|
+
|
|
21
|
+
import { join } from 'path';
|
|
22
|
+
import * as faas from '@midwayjs/faas';
|
|
23
|
+
import * as session from '@midwayjs/session';
|
|
24
|
+
|
|
25
|
+
@Configuration({
|
|
26
|
+
imports: [
|
|
27
|
+
faas,
|
|
28
|
+
session,
|
|
29
|
+
],
|
|
30
|
+
// ...
|
|
31
|
+
})
|
|
32
|
+
export class ContainerLifeCycle implements ILifeCycle {}
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Config
|
|
37
|
+
|
|
38
|
+
You can configure session in your `config.*.ts`.
|
|
39
|
+
|
|
40
|
+
default value.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
export const session = {
|
|
44
|
+
maxAge: 24 * 3600 * 1000, // ms
|
|
45
|
+
key: 'mw.sess',
|
|
46
|
+
httpOnly: true,
|
|
47
|
+
encrypt: true,
|
|
48
|
+
// sameSite: null,
|
|
49
|
+
logValue: true,
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
you can use all config from koa-session.
|
|
54
|
+
|
|
55
|
+
## Custom Session Store
|
|
56
|
+
|
|
57
|
+
Write a session store class first, extends abstract `SessionStore` class.
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { SessionStore } from '@midwayjs/session';
|
|
61
|
+
|
|
62
|
+
@Provide()
|
|
63
|
+
@Scope(ScopeEnum.Singleton)
|
|
64
|
+
export class MemorySessionStore extends SessionStore {
|
|
65
|
+
sessions = {};
|
|
66
|
+
async get(key) {
|
|
67
|
+
return this.sessions[key];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async set(key, value) {
|
|
71
|
+
this.sessions[key] = value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async destroy(key) {
|
|
75
|
+
this.sessions[key] = undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
add custom sessionStore to session component.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { MemorySessionStore } from './store';
|
|
84
|
+
import * as session from '@midwayjs/session';
|
|
85
|
+
|
|
86
|
+
@Configuration({
|
|
87
|
+
imports: [
|
|
88
|
+
koa,
|
|
89
|
+
session,
|
|
90
|
+
],
|
|
91
|
+
//...
|
|
92
|
+
})
|
|
93
|
+
export class AutoConfiguration {
|
|
94
|
+
@Inject()
|
|
95
|
+
memoryStore: MemorySessionStore;
|
|
96
|
+
|
|
97
|
+
@Inject()
|
|
98
|
+
sessionStoreManager: session.SessionStoreManager;
|
|
99
|
+
|
|
100
|
+
async onReady() {
|
|
101
|
+
this.sessionStoreManager.setSessionStore(this.memoryStore);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Questions & Suggestions
|
|
107
|
+
|
|
108
|
+
Please open an issue [here](https://github.com/midwayjs/midway/issues/).
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.session = void 0;
|
|
4
|
+
exports.session = {
|
|
5
|
+
enable: true,
|
|
6
|
+
maxAge: 24 * 3600 * 1000,
|
|
7
|
+
key: 'MW_SESS',
|
|
8
|
+
httpOnly: true,
|
|
9
|
+
// sameSite: null,
|
|
10
|
+
logValue: true,
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=config.default.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MidwayApplicationManager } from '@midwayjs/core';
|
|
2
|
+
export declare class SessionConfiguration {
|
|
3
|
+
applicationManager: MidwayApplicationManager;
|
|
4
|
+
logger: any;
|
|
5
|
+
sessionConfig: any;
|
|
6
|
+
onReady(): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
if (this.sessionConfig.enable) {
|
|
20
|
+
this.applicationManager.getApplications(['koa', 'faas']).forEach(app => {
|
|
21
|
+
if (app.on) {
|
|
22
|
+
// listen on session's events
|
|
23
|
+
app.on('session:missed', ({ ctx, key }) => {
|
|
24
|
+
this.logger.warn('[session][missed] key(%s)', key);
|
|
25
|
+
});
|
|
26
|
+
app.on('session:expired', ({ ctx, key, value }) => {
|
|
27
|
+
this.logger.warn('[session][expired] key(%s) value(%j)', key, this.sessionConfig.logValue ? value : '');
|
|
28
|
+
});
|
|
29
|
+
app.on('session:invalid', ({ ctx, key, value }) => {
|
|
30
|
+
this.logger.warn('[session][invalid] key(%s) value(%j)', key, this.sessionConfig.logValue ? value : '');
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
app.useMiddleware(session_1.SessionMiddleware);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
__decorate([
|
|
39
|
+
(0, decorator_1.Inject)(),
|
|
40
|
+
__metadata("design:type", core_1.MidwayApplicationManager)
|
|
41
|
+
], SessionConfiguration.prototype, "applicationManager", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, decorator_1.Logger)('coreLogger'),
|
|
44
|
+
__metadata("design:type", Object)
|
|
45
|
+
], SessionConfiguration.prototype, "logger", void 0);
|
|
46
|
+
__decorate([
|
|
47
|
+
(0, decorator_1.Config)('session'),
|
|
48
|
+
__metadata("design:type", Object)
|
|
49
|
+
], SessionConfiguration.prototype, "sessionConfig", void 0);
|
|
50
|
+
SessionConfiguration = __decorate([
|
|
51
|
+
(0, decorator_1.Configuration)({
|
|
52
|
+
namespace: 'session',
|
|
53
|
+
importConfigs: [
|
|
54
|
+
{
|
|
55
|
+
default: DefaultConfig,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
})
|
|
59
|
+
], SessionConfiguration);
|
|
60
|
+
exports.SessionConfiguration = SessionConfiguration;
|
|
61
|
+
//# 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,9 @@
|
|
|
1
|
+
import { IMiddleware } 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
|
+
resolve(app: any): any;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1,44 @@
|
|
|
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 koaSession = require("koa-session");
|
|
15
|
+
const store_1 = require("../store");
|
|
16
|
+
let SessionMiddleware = class SessionMiddleware {
|
|
17
|
+
resolve(app) {
|
|
18
|
+
if (!this.sessionConfig.httpOnly) {
|
|
19
|
+
this.logger.warn('[midway-session]: please set `config.session.httpOnly` to true. It is very dangerous if session can read by client JavaScript.');
|
|
20
|
+
}
|
|
21
|
+
const store = this.sessionStoreManager.getSessionStore();
|
|
22
|
+
if (store) {
|
|
23
|
+
this.sessionConfig.store = store;
|
|
24
|
+
}
|
|
25
|
+
return koaSession(this.sessionConfig, app);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
__decorate([
|
|
29
|
+
(0, decorator_1.Config)('session'),
|
|
30
|
+
__metadata("design:type", Object)
|
|
31
|
+
], SessionMiddleware.prototype, "sessionConfig", void 0);
|
|
32
|
+
__decorate([
|
|
33
|
+
(0, decorator_1.Logger)(),
|
|
34
|
+
__metadata("design:type", Object)
|
|
35
|
+
], SessionMiddleware.prototype, "logger", void 0);
|
|
36
|
+
__decorate([
|
|
37
|
+
(0, decorator_1.Inject)(),
|
|
38
|
+
__metadata("design:type", store_1.SessionStoreManager)
|
|
39
|
+
], SessionMiddleware.prototype, "sessionStoreManager", void 0);
|
|
40
|
+
SessionMiddleware = __decorate([
|
|
41
|
+
(0, decorator_1.Middleware)()
|
|
42
|
+
], SessionMiddleware);
|
|
43
|
+
exports.SessionMiddleware = SessionMiddleware;
|
|
44
|
+
//# sourceMappingURL=session.js.map
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare abstract class SessionStore {
|
|
2
|
+
abstract get(key: string): any;
|
|
3
|
+
abstract set(key: string, value: string, maxAge: number): any;
|
|
4
|
+
abstract destroy(key: any): any;
|
|
5
|
+
}
|
|
6
|
+
export declare class SessionStoreManager {
|
|
7
|
+
private sessionStore;
|
|
8
|
+
setSessionStore(sessionStore: any): void;
|
|
9
|
+
getSessionStore(): SessionStore;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=store.d.ts.map
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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 = exports.SessionStore = void 0;
|
|
10
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
11
|
+
class SessionStore {
|
|
12
|
+
}
|
|
13
|
+
exports.SessionStore = SessionStore;
|
|
14
|
+
let SessionStoreManager = class SessionStoreManager {
|
|
15
|
+
setSessionStore(sessionStore) {
|
|
16
|
+
this.sessionStore = sessionStore;
|
|
17
|
+
}
|
|
18
|
+
getSessionStore() {
|
|
19
|
+
return this.sessionStore;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
SessionStoreManager = __decorate([
|
|
23
|
+
(0, decorator_1.Provide)(),
|
|
24
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
25
|
+
], SessionStoreManager);
|
|
26
|
+
exports.SessionStoreManager = SessionStoreManager;
|
|
27
|
+
//# sourceMappingURL=store.js.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/session",
|
|
3
|
+
"description": "midway session component for koa and faas",
|
|
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/koa-session": "^5.10.4"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"koa-session": "^6.2.0"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"midway",
|
|
23
|
+
"web",
|
|
24
|
+
"koa",
|
|
25
|
+
"faas",
|
|
26
|
+
"session"
|
|
27
|
+
],
|
|
28
|
+
"author": "czy88840616 <czy88840616@gmail.com>",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc",
|
|
32
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest",
|
|
33
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit",
|
|
34
|
+
"ci": "npm run test",
|
|
35
|
+
"lint": "mwts check"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=12"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/midwayjs/midway.git"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "153870f2e2dd6b17673ec7591e49224a6bd51b36"
|
|
45
|
+
}
|