@midwayjs/session 3.0.0-beta.9 → 3.0.3

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/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 CHANGED
@@ -7,7 +7,6 @@ Session component for @midwayjs/koa and @midwayjs/faas
7
7
 
8
8
  ```bash
9
9
  $ npm i @midwayjs/session --save
10
- $ npm i @types/koa-session --save-dev
11
10
  ```
12
11
 
13
12
 
@@ -1,4 +1,5 @@
1
1
  export declare const session: {
2
+ enable: boolean;
2
3
  maxAge: number;
3
4
  key: string;
4
5
  httpOnly: boolean;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.session = void 0;
4
4
  exports.session = {
5
+ enable: true,
5
6
  maxAge: 24 * 3600 * 1000,
6
7
  key: 'MW_SESS',
7
8
  httpOnly: true,
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.default.js","sourceRoot":"","sources":["../../src/config/config.default.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG;IACrB,MAAM,EAAE,IAAI;IACZ,MAAM,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI;IACxB,GAAG,EAAE,SAAS;IACd,QAAQ,EAAE,IAAI;IACd,kBAAkB;IAClB,QAAQ,EAAE,IAAI;CACf,CAAC","sourcesContent":["export const session = {\n enable: true,\n maxAge: 24 * 3600 * 1000, // ms\n key: 'MW_SESS',\n httpOnly: true,\n // sameSite: null,\n logValue: true,\n};\n"]}
@@ -16,21 +16,23 @@ const session_1 = require("./middleware/session");
16
16
  const core_1 = require("@midwayjs/core");
17
17
  let SessionConfiguration = class SessionConfiguration {
18
18
  async onReady() {
19
- this.applicationManager.getApplications(['koa', 'faas']).forEach(app => {
20
- if (app.on) {
21
- // listen on session's events
22
- app.on('session:missed', ({ ctx, key }) => {
23
- this.logger.warn('[session][missed] key(%s)', key);
24
- });
25
- app.on('session:expired', ({ ctx, key, value }) => {
26
- this.logger.warn('[session][expired] key(%s) value(%j)', key, this.sessionConfig.logValue ? value : '');
27
- });
28
- app.on('session:invalid', ({ ctx, key, value }) => {
29
- this.logger.warn('[session][invalid] key(%s) value(%j)', key, this.sessionConfig.logValue ? value : '');
30
- });
31
- }
32
- app.useMiddleware(session_1.SessionMiddleware);
33
- });
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
+ }
34
36
  }
35
37
  };
36
38
  __decorate([
@@ -0,0 +1 @@
1
+ {"version":3,"file":"configuration.js","sourceRoot":"","sources":["../src/configuration.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAA4E;AAC5E,yDAAyD;AACzD,kDAAyD;AACzD,yCAA0D;AAU1D,IAAa,oBAAoB,GAAjC,MAAa,oBAAoB;IAU/B,KAAK,CAAC,OAAO;QACX,IAAI,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE;YAC7B,IAAI,CAAC,kBAAkB,CAAC,eAAe,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;gBACrE,IAAK,GAAW,CAAC,EAAE,EAAE;oBACnB,6BAA6B;oBAC5B,GAAW,CAAC,EAAE,CAAC,gBAAgB,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE;wBACjD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAC;oBACrD,CAAC,CAAC,CAAC;oBACF,GAAW,CAAC,EAAE,CAAC,iBAAiB,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE;wBACzD,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,sCAAsC,EACtC,GAAG,EACH,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CACzC,CAAC;oBACJ,CAAC,CAAC,CAAC;oBACF,GAAW,CAAC,EAAE,CAAC,iBAAiB,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE;wBACzD,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,sCAAsC,EACtC,GAAG,EACH,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CACzC,CAAC;oBACJ,CAAC,CAAC,CAAC;iBACJ;gBAED,GAAG,CAAC,aAAa,CAAC,2BAAiB,CAAC,CAAC;YACvC,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;CACF,CAAA;AApCC;IADC,IAAA,kBAAM,GAAE;8BACW,+BAAwB;gEAAC;AAG7C;IADC,IAAA,kBAAM,EAAC,YAAY,CAAC;;oDACd;AAGP;IADC,IAAA,kBAAM,EAAC,SAAS,CAAC;;2DACJ;AARH,oBAAoB;IARhC,IAAA,yBAAa,EAAC;QACb,SAAS,EAAE,SAAS;QACpB,aAAa,EAAE;YACb;gBACE,OAAO,EAAE,aAAa;aACvB;SACF;KACF,CAAC;GACW,oBAAoB,CAsChC;AAtCY,oDAAoB","sourcesContent":["import { Config, Configuration, Inject, Logger } from '@midwayjs/decorator';\nimport * as DefaultConfig from './config/config.default';\nimport { SessionMiddleware } from './middleware/session';\nimport { MidwayApplicationManager } from '@midwayjs/core';\n\n@Configuration({\n namespace: 'session',\n importConfigs: [\n {\n default: DefaultConfig,\n },\n ],\n})\nexport class SessionConfiguration {\n @Inject()\n applicationManager: MidwayApplicationManager;\n\n @Logger('coreLogger')\n logger;\n\n @Config('session')\n sessionConfig;\n\n async onReady() {\n if (this.sessionConfig.enable) {\n this.applicationManager.getApplications(['koa', 'faas']).forEach(app => {\n if ((app as any).on) {\n // listen on session's events\n (app as any).on('session:missed', ({ ctx, key }) => {\n this.logger.warn('[session][missed] key(%s)', key);\n });\n (app as any).on('session:expired', ({ ctx, key, value }) => {\n this.logger.warn(\n '[session][expired] key(%s) value(%j)',\n key,\n this.sessionConfig.logValue ? value : ''\n );\n });\n (app as any).on('session:invalid', ({ ctx, key, value }) => {\n this.logger.warn(\n '[session][invalid] key(%s) value(%j)',\n key,\n this.sessionConfig.logValue ? value : ''\n );\n });\n }\n\n app.useMiddleware(SessionMiddleware);\n });\n }\n }\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
+ export * from './interface';
1
2
  export { SessionConfiguration as Configuration } from './configuration';
2
3
  export * from './middleware/session';
3
- export * from './store';
4
+ export * from './lib/store';
4
5
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -11,8 +11,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.Configuration = void 0;
14
+ __exportStar(require("./interface"), exports);
14
15
  var configuration_1 = require("./configuration");
15
16
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.SessionConfiguration; } });
16
17
  __exportStar(require("./middleware/session"), exports);
17
- __exportStar(require("./store"), exports);
18
+ __exportStar(require("./lib/store"), exports);
18
19
  //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,8CAA4B;AAC5B,iDAAwE;AAA/D,8GAAA,oBAAoB,OAAiB;AAC9C,uDAAqC;AACrC,8CAA4B","sourcesContent":["export * from './interface';\nexport { SessionConfiguration as Configuration } from './configuration';\nexport * from './middleware/session';\nexport * from './lib/store';\n"]}
@@ -0,0 +1,105 @@
1
+ import { CookieSetOptions } from '@midwayjs/cookies';
2
+ import { opts } from 'koa-session';
3
+ export interface ISession {
4
+ /**
5
+ * JSON representation of the session.
6
+ */
7
+ toJSON(): object;
8
+ /**
9
+ * Return how many values there are in the session object.
10
+ * Used to see if it"s "populated".
11
+ */
12
+ readonly length: number;
13
+ /**
14
+ * populated flag, which is just a boolean alias of .length.
15
+ */
16
+ readonly populated: boolean;
17
+ /**
18
+ * get/set session maxAge
19
+ */
20
+ maxAge: opts["maxAge"];
21
+ /**
22
+ * save this session no matter whether it is populated
23
+ */
24
+ save(): void;
25
+ /**
26
+ * allow to put any value on session object
27
+ */
28
+ [_: string]: any;
29
+ }
30
+ interface ExternalKeys {
31
+ /**
32
+ * get session object by key
33
+ */
34
+ get(ctx: any): string;
35
+ /**
36
+ * set session object for key, with a maxAge (in ms)
37
+ */
38
+ set(ctx: any, value: any): void;
39
+ }
40
+ export interface SessionOptions extends Omit<CookieSetOptions, 'maxAge'> {
41
+ enable: boolean;
42
+ /**
43
+ * cookie key (default is koa:sess)
44
+ */
45
+ key: string;
46
+ /**
47
+ * maxAge in ms (default is 1 days)
48
+ * "session" will result in a cookie that expires when session/browser is closed
49
+ * Warning: If a session cookie is stolen, this cookie will never expire
50
+ */
51
+ maxAge?: number | "session" | undefined;
52
+ /**
53
+ * custom encode method
54
+ */
55
+ encode: (str: string) => Record<any, any>;
56
+ /**
57
+ * custom decode method
58
+ */
59
+ decode: (obj: Record<any, any>) => string;
60
+ /**
61
+ * The way of generating external session id is controlled by the options.genid, which defaults to Date.now() + "-" + uid.sync(24).
62
+ */
63
+ genid: () => string;
64
+ /**
65
+ * Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. default is false
66
+ */
67
+ rolling?: boolean | undefined;
68
+ /**
69
+ * Renew session when session is nearly expired, so we can always keep user logged in. (default is false)
70
+ */
71
+ renew?: boolean | undefined;
72
+ /**
73
+ * External key is used the cookie by default,
74
+ * but you can use options.externalKey to customize your own external key methods.
75
+ */
76
+ externalKey?: ExternalKeys | undefined;
77
+ /**
78
+ * If your session store requires data or utilities from context, opts.ContextStore is alse supported.
79
+ * ContextStore must be a class which claims three instance methods demonstrated above.
80
+ * new ContextStore(ctx) will be executed on every request.
81
+ */
82
+ ContextStore?: {
83
+ new (ctx: any): SessionStore;
84
+ } | undefined;
85
+ /**
86
+ * If you want to add prefix for all external session id, you can use options.prefix, it will not work if options.genid present.
87
+ */
88
+ prefix?: string | undefined;
89
+ /**
90
+ * Hook: valid session value before use it
91
+ */
92
+ valid?(ctx: any, session: Partial<ISession>): void;
93
+ /**
94
+ * Hook: before save session
95
+ */
96
+ beforeSave?(ctx: any, session: ISession): void;
97
+ autoCommit: boolean;
98
+ }
99
+ export declare abstract class SessionStore {
100
+ abstract get(key: string): any;
101
+ abstract set(key: string, value: string, maxAge: number): any;
102
+ abstract destroy(key: any): any;
103
+ }
104
+ export {};
105
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionStore = void 0;
4
+ class SessionStore {
5
+ }
6
+ exports.SessionStore = SessionStore;
7
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.js","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":";;;AAsHA,MAAsB,YAAY;CAIjC;AAJD,oCAIC","sourcesContent":["import { CookieSetOptions } from '@midwayjs/cookies';\nimport { opts } from 'koa-session';\n\nexport interface ISession {\n /**\n * JSON representation of the session.\n */\n toJSON(): object;\n\n /**\n * Return how many values there are in the session object.\n * Used to see if it\"s \"populated\".\n */\n readonly length: number;\n\n /**\n * populated flag, which is just a boolean alias of .length.\n */\n readonly populated: boolean;\n\n /**\n * get/set session maxAge\n */\n maxAge: opts[\"maxAge\"];\n\n /**\n * save this session no matter whether it is populated\n */\n save(): void;\n\n /**\n * allow to put any value on session object\n */\n [_: string]: any;\n}\n\ninterface ExternalKeys {\n /**\n * get session object by key\n */\n get(ctx: any): string;\n\n /**\n * set session object for key, with a maxAge (in ms)\n */\n set(ctx: any, value: any): void;\n}\n\nexport interface SessionOptions extends Omit<CookieSetOptions, 'maxAge'> {\n enable: boolean;\n /**\n * cookie key (default is koa:sess)\n */\n key: string;\n\n /**\n * maxAge in ms (default is 1 days)\n * \"session\" will result in a cookie that expires when session/browser is closed\n * Warning: If a session cookie is stolen, this cookie will never expire\n */\n maxAge?: number | \"session\" | undefined;\n\n /**\n * custom encode method\n */\n encode: (str: string) => Record<any, any>;\n\n /**\n * custom decode method\n */\n decode: (obj: Record<any, any>) => string;\n\n /**\n * The way of generating external session id is controlled by the options.genid, which defaults to Date.now() + \"-\" + uid.sync(24).\n */\n genid: () => string;\n\n /**\n * Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. default is false\n */\n rolling?: boolean | undefined;\n\n /**\n * Renew session when session is nearly expired, so we can always keep user logged in. (default is false)\n */\n renew?: boolean | undefined;\n\n /**\n * External key is used the cookie by default,\n * but you can use options.externalKey to customize your own external key methods.\n */\n externalKey?: ExternalKeys | undefined;\n\n /**\n * If your session store requires data or utilities from context, opts.ContextStore is alse supported.\n * ContextStore must be a class which claims three instance methods demonstrated above.\n * new ContextStore(ctx) will be executed on every request.\n */\n ContextStore?: { new(ctx: any): SessionStore } | undefined;\n\n /**\n * If you want to add prefix for all external session id, you can use options.prefix, it will not work if options.genid present.\n */\n prefix?: string | undefined;\n\n /**\n * Hook: valid session value before use it\n */\n valid?(ctx: any, session: Partial<ISession>): void;\n\n /**\n * Hook: before save session\n */\n beforeSave?(ctx: any, session: ISession): void;\n\n autoCommit: boolean;\n}\n\nexport abstract class SessionStore {\n abstract get(key: string);\n abstract set(key: string, value: string, maxAge: number);\n abstract destroy(key);\n}\n"]}
@@ -0,0 +1,80 @@
1
+ export declare class ContextSession {
2
+ private ctx;
3
+ private app;
4
+ private opts;
5
+ private session;
6
+ private externalKey;
7
+ private prevHash;
8
+ store: any;
9
+ /**
10
+ * context session constructor
11
+ * @api public
12
+ */
13
+ constructor(ctx: any, opts: any);
14
+ /**
15
+ * internal logic of `ctx.session`
16
+ * @return {Session} session object
17
+ *
18
+ * @api public
19
+ */
20
+ get(): any;
21
+ /**
22
+ * internal logic of `ctx.session=`
23
+ * @param {Object} val session object
24
+ *
25
+ * @api public
26
+ */
27
+ set(val: any): void;
28
+ /**
29
+ * init session from external store
30
+ * will be called in the front of session middleware
31
+ *
32
+ * @api public
33
+ */
34
+ initFromExternal(): Promise<void>;
35
+ /**
36
+ * init session from cookie
37
+ * @api private
38
+ */
39
+ initFromCookie(): void;
40
+ /**
41
+ * verify session(expired or )
42
+ * @param {Object} value session object
43
+ * @param {Object} key session externalKey(optional)
44
+ * @return {Boolean} valid
45
+ * @api private
46
+ */
47
+ valid(value: any, key?: any): boolean;
48
+ /**
49
+ * @param {String} event event name
50
+ * @param {Object} data event data
51
+ * @api private
52
+ */
53
+ emit(event: any, data: any): void;
54
+ /**
55
+ * create a new session and attach to ctx.sess
56
+ *
57
+ * @param {Object} [val] session data
58
+ * @param {String} [externalKey] session external key
59
+ * @api private
60
+ */
61
+ create(val?: any, externalKey?: any): void;
62
+ /**
63
+ * Commit the session changes or removal.
64
+ *
65
+ * @api public
66
+ */
67
+ commit(): Promise<void>;
68
+ _shouldSaveSession(): "" | "force" | "changed" | "rolling" | "renew";
69
+ /**
70
+ * remove session
71
+ * @api private
72
+ */
73
+ remove(): Promise<void>;
74
+ /**
75
+ * save session
76
+ * @api private
77
+ */
78
+ save(changed: any): Promise<void>;
79
+ }
80
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1,315 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContextSession = void 0;
4
+ const util_1 = require("util");
5
+ const util_2 = require("./util");
6
+ const session_1 = require("./session");
7
+ const debug = (0, util_1.debuglog)('session:context');
8
+ class ContextSession {
9
+ /**
10
+ * context session constructor
11
+ * @api public
12
+ */
13
+ constructor(ctx, opts) {
14
+ this.ctx = ctx;
15
+ this.app = ctx.app;
16
+ this.opts = Object.assign({}, opts);
17
+ this.store = this.opts.ContextStore
18
+ ? new this.opts.ContextStore(ctx)
19
+ : this.opts.store;
20
+ }
21
+ /**
22
+ * internal logic of `ctx.session`
23
+ * @return {Session} session object
24
+ *
25
+ * @api public
26
+ */
27
+ get() {
28
+ const session = this.session;
29
+ // already retrieved
30
+ if (session)
31
+ return session;
32
+ // unset
33
+ if (session === false)
34
+ return null;
35
+ // create an empty session or init from cookie
36
+ this.store ? this.create() : this.initFromCookie();
37
+ return this.session;
38
+ }
39
+ /**
40
+ * internal logic of `ctx.session=`
41
+ * @param {Object} val session object
42
+ *
43
+ * @api public
44
+ */
45
+ set(val) {
46
+ if (val === null) {
47
+ this.session = false;
48
+ return;
49
+ }
50
+ if (typeof val === 'object') {
51
+ // use the original `externalKey` if exists to avoid waste storage
52
+ this.create(val, this.externalKey);
53
+ return;
54
+ }
55
+ throw new Error('this.session can only be set as null or an object.');
56
+ }
57
+ /**
58
+ * init session from external store
59
+ * will be called in the front of session middleware
60
+ *
61
+ * @api public
62
+ */
63
+ async initFromExternal() {
64
+ debug('init from external');
65
+ const ctx = this.ctx;
66
+ const opts = this.opts;
67
+ let externalKey;
68
+ if (opts.externalKey) {
69
+ externalKey = opts.externalKey.get(ctx);
70
+ debug('get external key from custom %s', externalKey);
71
+ }
72
+ else {
73
+ externalKey = ctx.cookies.get(opts.key, opts);
74
+ debug('get external key from cookie %s', externalKey);
75
+ }
76
+ if (!externalKey) {
77
+ // create a new `externalKey`
78
+ this.create();
79
+ return;
80
+ }
81
+ const json = await this.store.get(externalKey, opts.maxAge, {
82
+ ctx,
83
+ rolling: opts.rolling,
84
+ });
85
+ if (!this.valid(json, externalKey)) {
86
+ // create a new `externalKey`
87
+ this.create();
88
+ return;
89
+ }
90
+ // create with original `externalKey`
91
+ this.create(json, externalKey);
92
+ this.prevHash = (0, util_2.hash)(this.session.toJSON());
93
+ }
94
+ /**
95
+ * init session from cookie
96
+ * @api private
97
+ */
98
+ initFromCookie() {
99
+ debug('init from cookie');
100
+ const ctx = this.ctx;
101
+ const opts = this.opts;
102
+ const cookie = ctx.cookies.get(opts.key, opts);
103
+ if (!cookie) {
104
+ this.create();
105
+ return;
106
+ }
107
+ let json;
108
+ debug('parse %s', cookie);
109
+ try {
110
+ json = opts.decode(cookie);
111
+ }
112
+ catch (err) {
113
+ // backwards compatibility:
114
+ // create a new session if parsing fails.
115
+ // new Buffer(string, 'base64') does not seem to crash
116
+ // when `string` is not base64-encoded.
117
+ // but `JSON.parse(string)` will crash.
118
+ debug('decode %j error: %s', cookie, err);
119
+ if (!(err instanceof SyntaxError)) {
120
+ // clean this cookie to ensure next request won't throw again
121
+ ctx.cookies.set(opts.key, '', opts);
122
+ // ctx.onerror will unset all headers, and set those specified in err
123
+ err.headers = {
124
+ 'set-cookie': ctx.response.get('set-cookie'),
125
+ };
126
+ throw err;
127
+ }
128
+ this.create();
129
+ return;
130
+ }
131
+ debug('parsed %j', json);
132
+ if (!this.valid(json)) {
133
+ this.create();
134
+ return;
135
+ }
136
+ // support access `ctx.session` before session middleware
137
+ this.create(json);
138
+ this.prevHash = (0, util_2.hash)(this.session.toJSON());
139
+ }
140
+ /**
141
+ * verify session(expired or )
142
+ * @param {Object} value session object
143
+ * @param {Object} key session externalKey(optional)
144
+ * @return {Boolean} valid
145
+ * @api private
146
+ */
147
+ valid(value, key) {
148
+ const ctx = this.ctx;
149
+ if (!value) {
150
+ this.emit('missed', { key, value, ctx });
151
+ return false;
152
+ }
153
+ if (value._expire && value._expire < Date.now()) {
154
+ debug('expired session');
155
+ this.emit('expired', { key, value, ctx });
156
+ return false;
157
+ }
158
+ const valid = this.opts.valid;
159
+ if (typeof valid === 'function' && !valid(ctx, value)) {
160
+ // valid session value fail, ignore this session
161
+ debug('invalid session');
162
+ this.emit('invalid', { key, value, ctx });
163
+ return false;
164
+ }
165
+ return true;
166
+ }
167
+ /**
168
+ * @param {String} event event name
169
+ * @param {Object} data event data
170
+ * @api private
171
+ */
172
+ emit(event, data) {
173
+ setImmediate(() => {
174
+ this.app.emit(`session:${event}`, data);
175
+ });
176
+ }
177
+ /**
178
+ * create a new session and attach to ctx.sess
179
+ *
180
+ * @param {Object} [val] session data
181
+ * @param {String} [externalKey] session external key
182
+ * @api private
183
+ */
184
+ create(val, externalKey) {
185
+ debug('create session with val: %j externalKey: %s', val, externalKey);
186
+ if (this.store) {
187
+ this.externalKey =
188
+ externalKey || (this.opts.genid && this.opts.genid(this.ctx));
189
+ }
190
+ this.session = new session_1.Session(this, val, this.externalKey);
191
+ }
192
+ /**
193
+ * Commit the session changes or removal.
194
+ *
195
+ * @api public
196
+ */
197
+ async commit() {
198
+ const session = this.session;
199
+ const opts = this.opts;
200
+ const ctx = this.ctx;
201
+ // not accessed
202
+ if (undefined === session)
203
+ return;
204
+ // removed
205
+ if (session === false) {
206
+ await this.remove();
207
+ return;
208
+ }
209
+ const reason = this._shouldSaveSession();
210
+ debug('should save session: %s', reason);
211
+ if (!reason)
212
+ return;
213
+ if (typeof opts.beforeSave === 'function') {
214
+ debug('before save');
215
+ opts.beforeSave(ctx, session);
216
+ }
217
+ const changed = reason === 'changed';
218
+ await this.save(changed);
219
+ }
220
+ _shouldSaveSession() {
221
+ const prevHash = this.prevHash;
222
+ const session = this.session;
223
+ // force save session when `session._requireSave` set
224
+ if (session._requireSave)
225
+ return 'force';
226
+ // do nothing if new and not populated
227
+ const json = session.toJSON();
228
+ if (!prevHash && !Object.keys(json).length)
229
+ return '';
230
+ // save if session changed
231
+ const changed = prevHash !== (0, util_2.hash)(json);
232
+ if (changed)
233
+ return 'changed';
234
+ // save if opts.rolling set
235
+ if (this.opts.rolling)
236
+ return 'rolling';
237
+ // save if opts.renew and session will expired
238
+ if (this.opts.renew) {
239
+ const expire = session._expire;
240
+ const maxAge = session.maxAge;
241
+ // renew when session will expired in maxAge / 2
242
+ if (expire && maxAge && expire - Date.now() < maxAge / 2)
243
+ return 'renew';
244
+ }
245
+ return '';
246
+ }
247
+ /**
248
+ * remove session
249
+ * @api private
250
+ */
251
+ async remove() {
252
+ // Override the default options so that we can properly expire the session cookies
253
+ const opts = Object.assign({}, this.opts, {
254
+ expires: util_2.COOKIE_EXP_DATE,
255
+ maxAge: false,
256
+ });
257
+ const ctx = this.ctx;
258
+ const key = opts.key;
259
+ const externalKey = this.externalKey;
260
+ if (externalKey) {
261
+ await this.store.destroy(externalKey, { ctx });
262
+ }
263
+ ctx.cookies.set(key, '', opts);
264
+ }
265
+ /**
266
+ * save session
267
+ * @api private
268
+ */
269
+ async save(changed) {
270
+ const opts = this.opts;
271
+ const key = opts.key;
272
+ const externalKey = this.externalKey;
273
+ let json = this.session.toJSON();
274
+ // set expire for check
275
+ let maxAge = opts.maxAge ? opts.maxAge : util_2.ONE_DAY;
276
+ if (maxAge === 'session') {
277
+ // do not set _expire in json if maxAge is set to 'session'
278
+ // also delete maxAge from options
279
+ opts.maxAge = undefined;
280
+ json._session = true;
281
+ }
282
+ else {
283
+ // set expire for check
284
+ json._expire = maxAge + Date.now();
285
+ json._maxAge = maxAge;
286
+ }
287
+ // save to external store
288
+ if (externalKey) {
289
+ debug('save %j to external key %s', json, externalKey);
290
+ if (typeof maxAge === 'number') {
291
+ // ensure store expired after cookie
292
+ maxAge += 10000;
293
+ }
294
+ await this.store.set(externalKey, json, maxAge, {
295
+ changed,
296
+ ctx: this.ctx,
297
+ rolling: opts.rolling,
298
+ });
299
+ if (opts.externalKey) {
300
+ opts.externalKey.set(this.ctx, externalKey);
301
+ }
302
+ else {
303
+ this.ctx.cookies.set(key, externalKey, opts);
304
+ }
305
+ return;
306
+ }
307
+ // save to cookie
308
+ debug('save %j to cookie', json);
309
+ json = opts.encode(json);
310
+ debug('save %s', json);
311
+ this.ctx.cookies.set(key, json, opts);
312
+ }
313
+ }
314
+ exports.ContextSession = ContextSession;
315
+ //# sourceMappingURL=context.js.map