@midwayjs/session 3.0.0-beta.16 → 3.0.1

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 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
 
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,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,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
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Session model.
3
+ */
4
+ import { ISession } from '../interface';
5
+ export declare class Session implements ISession {
6
+ private _sessCtx;
7
+ private _ctx;
8
+ private _externalKey;
9
+ _requireSave: any;
10
+ isNew: boolean;
11
+ /**
12
+ * Session constructor
13
+ * @param sessionContext
14
+ * @param {Object} obj
15
+ * @param externalKey
16
+ */
17
+ constructor(sessionContext: any, obj: any, externalKey: any);
18
+ /**
19
+ * JSON representation of the session.
20
+ *
21
+ * @return {Object}
22
+ * @api public
23
+ */
24
+ toJSON(): {};
25
+ /**
26
+ * Return how many values there are in the session object.
27
+ * Used to see if it's "populated".
28
+ *
29
+ * @return {Number}
30
+ * @api public
31
+ */
32
+ get length(): number;
33
+ /**
34
+ * populated flag, which is just a boolean alias of .length.
35
+ *
36
+ * @return {Boolean}
37
+ * @api public
38
+ */
39
+ get populated(): boolean;
40
+ /**
41
+ * get session maxAge
42
+ *
43
+ * @return {Number}
44
+ * @api public
45
+ */
46
+ get maxAge(): any;
47
+ /**
48
+ * set session maxAge
49
+ *
50
+ * @api public
51
+ * @param val
52
+ */
53
+ set maxAge(val: any);
54
+ /**
55
+ * get session external key
56
+ * only exist if opts.store present
57
+ */
58
+ get externalKey(): any;
59
+ /**
60
+ * save this session no matter whether it is populated
61
+ *
62
+ * @api public
63
+ */
64
+ save(): void;
65
+ /**
66
+ * commit this session's headers if autoCommit is set to false
67
+ *
68
+ * @api public
69
+ */
70
+ manuallyCommit(): Promise<void>;
71
+ }
72
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Session = void 0;
4
+ class Session {
5
+ /**
6
+ * Session constructor
7
+ * @param sessionContext
8
+ * @param {Object} obj
9
+ * @param externalKey
10
+ */
11
+ constructor(sessionContext, obj, externalKey) {
12
+ this.isNew = true;
13
+ this._sessCtx = sessionContext;
14
+ this._ctx = sessionContext.ctx;
15
+ this._externalKey = externalKey;
16
+ if (!obj) {
17
+ this.isNew = true;
18
+ }
19
+ else {
20
+ for (const k in obj) {
21
+ // restore maxAge from store
22
+ if (k === '_maxAge')
23
+ this._ctx.sessionOptions.maxAge = obj._maxAge;
24
+ else if (k === '_session')
25
+ this._ctx.sessionOptions.maxAge = 'session';
26
+ else
27
+ this[k] = obj[k];
28
+ }
29
+ }
30
+ }
31
+ /**
32
+ * JSON representation of the session.
33
+ *
34
+ * @return {Object}
35
+ * @api public
36
+ */
37
+ toJSON() {
38
+ const obj = {};
39
+ Object.keys(this).forEach(key => {
40
+ if (key === 'isNew')
41
+ return;
42
+ if (key[0] === '_')
43
+ return;
44
+ obj[key] = this[key];
45
+ });
46
+ return obj;
47
+ }
48
+ /**
49
+ * Return how many values there are in the session object.
50
+ * Used to see if it's "populated".
51
+ *
52
+ * @return {Number}
53
+ * @api public
54
+ */
55
+ get length() {
56
+ return Object.keys(this.toJSON()).length;
57
+ }
58
+ /**
59
+ * populated flag, which is just a boolean alias of .length.
60
+ *
61
+ * @return {Boolean}
62
+ * @api public
63
+ */
64
+ get populated() {
65
+ return !!this.length;
66
+ }
67
+ /**
68
+ * get session maxAge
69
+ *
70
+ * @return {Number}
71
+ * @api public
72
+ */
73
+ get maxAge() {
74
+ return this._ctx.sessionOptions.maxAge;
75
+ }
76
+ /**
77
+ * set session maxAge
78
+ *
79
+ * @api public
80
+ * @param val
81
+ */
82
+ set maxAge(val) {
83
+ this._ctx.sessionOptions.maxAge = val;
84
+ // maxAge changed, must save to cookie and store
85
+ this._requireSave = true;
86
+ }
87
+ /**
88
+ * get session external key
89
+ * only exist if opts.store present
90
+ */
91
+ get externalKey() {
92
+ return this._externalKey;
93
+ }
94
+ /**
95
+ * save this session no matter whether it is populated
96
+ *
97
+ * @api public
98
+ */
99
+ save() {
100
+ this._requireSave = true;
101
+ }
102
+ /**
103
+ * commit this session's headers if autoCommit is set to false
104
+ *
105
+ * @api public
106
+ */
107
+ async manuallyCommit() {
108
+ await this._sessCtx.commit();
109
+ }
110
+ }
111
+ exports.Session = Session;
112
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1,7 @@
1
+ import { SessionStore } from '../interface';
2
+ export declare class SessionStoreManager {
3
+ private sessionStore;
4
+ setSessionStore(sessionStore: any): void;
5
+ getSessionStore(): SessionStore;
6
+ }
7
+ //# sourceMappingURL=store.d.ts.map
@@ -6,11 +6,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
7
  };
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.SessionStoreManager = exports.SessionStore = void 0;
9
+ exports.SessionStoreManager = void 0;
10
10
  const decorator_1 = require("@midwayjs/decorator");
11
- class SessionStore {
12
- }
13
- exports.SessionStore = SessionStore;
14
11
  let SessionStoreManager = class SessionStoreManager {
15
12
  setSessionStore(sessionStore) {
16
13
  this.sessionStore = sessionStore;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Decode the base64 cookie value to an object.
3
+ *
4
+ * @param {String} string
5
+ * @return {Object}
6
+ * @api private
7
+ */
8
+ export declare function decode(string: any): any;
9
+ /**
10
+ * Encode an object into a base64-encoded JSON string.
11
+ *
12
+ * @param {Object} body
13
+ * @return {String}
14
+ * @api private
15
+ */
16
+ export declare function encode(body: any): string;
17
+ export declare function hash(sess: any): number;
18
+ export declare const COOKIE_EXP_DATE: Date;
19
+ export declare const ONE_DAY: number;
20
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ONE_DAY = exports.COOKIE_EXP_DATE = exports.hash = exports.encode = exports.decode = void 0;
4
+ const crc_1 = require("crc");
5
+ /**
6
+ * Decode the base64 cookie value to an object.
7
+ *
8
+ * @param {String} string
9
+ * @return {Object}
10
+ * @api private
11
+ */
12
+ function decode(string) {
13
+ const body = Buffer.from(string, 'base64').toString('utf8');
14
+ return JSON.parse(body);
15
+ }
16
+ exports.decode = decode;
17
+ /**
18
+ * Encode an object into a base64-encoded JSON string.
19
+ *
20
+ * @param {Object} body
21
+ * @return {String}
22
+ * @api private
23
+ */
24
+ function encode(body) {
25
+ body = JSON.stringify(body);
26
+ return Buffer.from(body).toString('base64');
27
+ }
28
+ exports.encode = encode;
29
+ function hash(sess) {
30
+ return (0, crc_1.crc32)(JSON.stringify(sess));
31
+ }
32
+ exports.hash = hash;
33
+ exports.COOKIE_EXP_DATE = new Date('Thu, 01 Jan 1970 00:00:00 GMT');
34
+ exports.ONE_DAY = 24 * 60 * 60 * 1000;
35
+ //# sourceMappingURL=util.js.map
@@ -1,10 +1,10 @@
1
1
  import { IMiddleware } from '@midwayjs/core';
2
- import { SessionStoreManager } from '../store';
2
+ import { SessionStoreManager } from '../lib/store';
3
3
  export declare class SessionMiddleware implements IMiddleware<any, any> {
4
4
  sessionConfig: any;
5
5
  logger: any;
6
6
  sessionStoreManager: SessionStoreManager;
7
- resolve(app: any): any;
7
+ resolve(app: any): (ctx: any, next: any) => Promise<void>;
8
8
  static getName(): string;
9
9
  }
10
10
  //# sourceMappingURL=session.d.ts.map
@@ -11,8 +11,112 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.SessionMiddleware = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
- const koaSession = require("koa-session");
15
- const store_1 = require("../store");
14
+ const store_1 = require("../lib/store");
15
+ const util_1 = require("../lib/util");
16
+ const assert = require("assert");
17
+ const context_1 = require("../lib/context");
18
+ const CONTEXT_SESSION = Symbol('context#contextSession');
19
+ const _CONTEXT_SESSION = Symbol('context#_contextSession');
20
+ /**
21
+ * format and check session options
22
+ * @param {Object} opts session options
23
+ * @return {Object} new session options
24
+ *
25
+ * @api private
26
+ */
27
+ function formatOpts(opts) {
28
+ opts = opts || {};
29
+ // key
30
+ opts.key = opts.key || 'koa.sess';
31
+ // back-compat maxage
32
+ if (!('maxAge' in opts))
33
+ opts.maxAge = opts.maxage;
34
+ // defaults
35
+ if (opts.overwrite == null)
36
+ opts.overwrite = true;
37
+ if (opts.httpOnly == null)
38
+ opts.httpOnly = true;
39
+ // delete null sameSite config
40
+ if (opts.sameSite == null)
41
+ delete opts.sameSite;
42
+ if (opts.signed == null)
43
+ opts.signed = true;
44
+ if (opts.autoCommit == null)
45
+ opts.autoCommit = true;
46
+ // setup encoding/decoding
47
+ if (typeof opts.encode !== 'function') {
48
+ opts.encode = util_1.encode;
49
+ }
50
+ if (typeof opts.decode !== 'function') {
51
+ opts.decode = util_1.decode;
52
+ }
53
+ const store = opts.store;
54
+ if (store) {
55
+ assert(decorator_1.Types.isFunction(store.get), 'store.get must be function');
56
+ assert(decorator_1.Types.isFunction(store.set), 'store.set must be function');
57
+ assert(decorator_1.Types.isFunction(store.destroy), 'store.destroy must be function');
58
+ }
59
+ const externalKey = opts.externalKey;
60
+ if (externalKey) {
61
+ assert(decorator_1.Types.isFunction(externalKey.get), 'externalKey.get must be function');
62
+ assert(decorator_1.Types.isFunction(externalKey.set), 'externalKey.set must be function');
63
+ }
64
+ const ContextStore = opts.ContextStore;
65
+ if (ContextStore) {
66
+ assert(decorator_1.Types.isClass(ContextStore), 'ContextStore must be a class');
67
+ assert(decorator_1.Types.isFunction(ContextStore.prototype.get), 'ContextStore.prototype.get must be function');
68
+ assert(decorator_1.Types.isFunction(ContextStore.prototype.set), 'ContextStore.prototype.set must be function');
69
+ assert(decorator_1.Types.isFunction(ContextStore.prototype.destroy), 'ContextStore.prototype.destroy must be function');
70
+ }
71
+ if (!opts.genid) {
72
+ if (opts.prefix) {
73
+ opts.genid = () => `${opts.prefix}${decorator_1.Utils.randomUUID()}`;
74
+ }
75
+ else {
76
+ opts.genid = decorator_1.Utils.randomUUID;
77
+ }
78
+ }
79
+ return opts;
80
+ }
81
+ /**
82
+ * extend context prototype, add session properties
83
+ *
84
+ * @param {Object} context koa's context prototype
85
+ * @param {Object} opts session options
86
+ *
87
+ * @api private
88
+ */
89
+ function extendContext(context, opts) {
90
+ // eslint-disable-next-line no-prototype-builtins
91
+ if (context.hasOwnProperty(CONTEXT_SESSION)) {
92
+ return;
93
+ }
94
+ Object.defineProperties(context, {
95
+ [CONTEXT_SESSION]: {
96
+ get() {
97
+ if (this[_CONTEXT_SESSION]) {
98
+ return this[_CONTEXT_SESSION];
99
+ }
100
+ this[_CONTEXT_SESSION] = new context_1.ContextSession(this, opts);
101
+ return this[_CONTEXT_SESSION];
102
+ },
103
+ },
104
+ session: {
105
+ get() {
106
+ return this[CONTEXT_SESSION].get();
107
+ },
108
+ set(val) {
109
+ this[CONTEXT_SESSION].set(val);
110
+ },
111
+ configurable: true,
112
+ },
113
+ sessionOptions: {
114
+ get() {
115
+ return this[CONTEXT_SESSION].opts;
116
+ },
117
+ },
118
+ });
119
+ }
16
120
  let SessionMiddleware = class SessionMiddleware {
17
121
  resolve(app) {
18
122
  if (!this.sessionConfig.httpOnly) {
@@ -22,7 +126,22 @@ let SessionMiddleware = class SessionMiddleware {
22
126
  if (store) {
23
127
  this.sessionConfig.store = store;
24
128
  }
25
- return koaSession(this.sessionConfig, app);
129
+ const opts = formatOpts(this.sessionConfig);
130
+ extendContext(app.context, opts);
131
+ return async function session(ctx, next) {
132
+ const sess = ctx[CONTEXT_SESSION];
133
+ if (sess.store) {
134
+ await sess.initFromExternal();
135
+ }
136
+ try {
137
+ await next();
138
+ }
139
+ finally {
140
+ if (opts.autoCommit) {
141
+ await sess.commit();
142
+ }
143
+ }
144
+ };
26
145
  }
27
146
  static getName() {
28
147
  return 'session';
package/index.d.ts CHANGED
@@ -1,12 +1,20 @@
1
- import { opts } from 'koa-session';
1
+ import { SessionOptions, ISession } from './dist';
2
2
  export * from './dist/index';
3
3
 
4
4
  declare module '@midwayjs/core/dist/interface' {
5
5
  interface MidwayConfig {
6
- session: Partial<
7
- opts & {
8
- enable: boolean;
9
- }
10
- >;
6
+ session?: PowerPartial<SessionOptions>;
7
+ }
8
+ }
9
+
10
+ declare module '@midwayjs/koa/dist/interface' {
11
+ interface Context {
12
+ session?: ISession;
13
+ }
14
+ }
15
+
16
+ declare module '@midwayjs/faas/dist/interface' {
17
+ interface Context {
18
+ session?: ISession;
11
19
  }
12
20
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/session",
3
3
  "description": "midway session component for koa and faas",
4
- "version": "3.0.0-beta.16",
4
+ "version": "3.0.1",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -10,13 +10,13 @@
10
10
  "index.d.ts"
11
11
  ],
12
12
  "devDependencies": {
13
- "@midwayjs/core": "^3.0.0-beta.16",
14
- "@midwayjs/decorator": "^3.0.0-beta.16",
15
- "@midwayjs/mock": "^3.0.0-beta.16",
16
- "@types/koa-session": "5.10.4"
13
+ "@midwayjs/core": "^3.0.1",
14
+ "@midwayjs/decorator": "^3.0.0",
15
+ "@midwayjs/mock": "^3.0.1"
17
16
  },
18
17
  "dependencies": {
19
- "koa-session": "^6.2.0"
18
+ "@midwayjs/cookies": "1.0.1",
19
+ "crc": "4.1.0"
20
20
  },
21
21
  "keywords": [
22
22
  "midway",
@@ -29,8 +29,8 @@
29
29
  "license": "MIT",
30
30
  "scripts": {
31
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",
32
+ "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
33
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
34
34
  "ci": "npm run test",
35
35
  "lint": "mwts check"
36
36
  },
@@ -41,5 +41,5 @@
41
41
  "type": "git",
42
42
  "url": "https://github.com/midwayjs/midway.git"
43
43
  },
44
- "gitHead": "43a32745ac45164dfef1494cba979f9652a6692b"
44
+ "gitHead": "f345b4ed0392e5c3b9e815438ef0a377ad6da076"
45
45
  }
package/dist/store.d.ts DELETED
@@ -1,11 +0,0 @@
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