@randajan/koa-io-session 0.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
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present Jan Randa
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,52 @@
1
+ # @randajan/koa-io-session
2
+
3
+ [![NPM](https://img.shields.io/npm/v/@randajan/koa-io-session.svg)](https://www.npmjs.com/package/@randajan/koa-io-session) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
4
+
5
+ Simple bridge between `koa-session` and `socket.io`. Shares a unified session across HTTP and WebSocket using a session store.
6
+
7
+ ---
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm i @randajan/koa-io-session
13
+ ```
14
+
15
+ ---
16
+
17
+ ## Quick use
18
+
19
+ ```js
20
+ import Koa from "koa";
21
+ import http from "http";
22
+ import { Server } from "socket.io";
23
+ import { attachSession } from "@randajan/koa-io-session";
24
+
25
+ const app = new Koa();
26
+ const server = http.createServer(app.callback());
27
+ const io = new Server(server);
28
+
29
+ attachSession(app, io, {
30
+ key: "koa:sess",
31
+ signed: true,
32
+ maxAge: 86400000
33
+ });
34
+
35
+ io.on("connection", socket => {
36
+ console.log("session ID:", socket.sessionId);
37
+ console.log("session data:", socket.session);
38
+ });
39
+ ```
40
+
41
+ ---
42
+
43
+ ## Socket helpers
44
+
45
+ - `socket.sessionId` → session ID from cookies
46
+ - `socket.session` → session object from store
47
+
48
+ ---
49
+
50
+ ## License
51
+
52
+ MIT © [randajan](https://github.com/randajan)
@@ -0,0 +1,97 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.js
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ attachSession: () => attachSession,
33
+ default: () => index_default
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_crypto = __toESM(require("crypto"), 1);
37
+ var import_koa_session = __toESM(require("koa-session"), 1);
38
+
39
+ // src/SessionStore.js
40
+ var import_props = require("@randajan/props");
41
+ var SessionStore = class {
42
+ constructor() {
43
+ (0, import_props.solid)(this, "_recs", /* @__PURE__ */ new Map());
44
+ }
45
+ get(sid) {
46
+ const rec = this._recs.get(sid);
47
+ if (!rec) {
48
+ return;
49
+ }
50
+ if (Date.now() < rec.expiresAt) {
51
+ return rec.session;
52
+ }
53
+ this.destroy(sid);
54
+ }
55
+ set(sid, session2, maxAge) {
56
+ const expiresAt = Date.now() + maxAge;
57
+ this._recs.set(sid, { session: session2, expiresAt, maxAge });
58
+ }
59
+ destroy(sid) {
60
+ this._recs.delete(sid);
61
+ }
62
+ };
63
+
64
+ // src/index.js
65
+ var import_props2 = require("@randajan/props");
66
+ var uid = (len = 12) => import_crypto.default.randomBytes(len).toString("base64url").slice(0, len);
67
+ var attachSession = (app, io, opt = {}) => {
68
+ if (!app.keys) {
69
+ app.keys = Array(6).fill().map(() => uid(12));
70
+ }
71
+ if (!opt.key) {
72
+ opt.key = uid(12);
73
+ }
74
+ if (!opt.store) {
75
+ opt.store = new SessionStore();
76
+ }
77
+ const { key, store } = opt;
78
+ const signed = "signed" in opt ? !!opt.signed : true;
79
+ delete opt.signed;
80
+ app.use((0, import_koa_session.default)(opt, app));
81
+ app.use(async (ctx, next) => {
82
+ ctx.session.active = true;
83
+ await next();
84
+ });
85
+ io.use(async (socket, next) => {
86
+ if (!socket.handshake.headers.cookie) {
87
+ return next(new Error("no cookie"));
88
+ }
89
+ const ctx = app.createContext(socket.request, socket.response);
90
+ const sid = ctx.cookies.get(key, { signed });
91
+ (0, import_props2.virtual)(socket, "sessionId", (_) => sid);
92
+ (0, import_props2.virtual)(socket, "session", (_) => store.get(sid));
93
+ await next();
94
+ });
95
+ };
96
+ var index_default = attachSession;
97
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.js", "../../src/SessionStore.js"],
4
+ "sourcesContent": ["import crypto from \"crypto\";\nimport session from \"koa-session\";\nimport { SessionStore } from \"./SessionStore\";\nimport { virtual } from \"@randajan/props\";\n\nconst uid = (len = 12) => crypto.randomBytes(len).toString(\"base64url\").slice(0, len);\n\nexport const attachSession = (app, io, opt = {}) => {\n if (!app.keys) { app.keys = Array(6).fill().map(() => uid(12)); }\n\n if (!opt.key) { opt.key = uid(12); }\n if (!opt.store) { opt.store = new SessionStore(); }\n\n const { key, store } = opt;\n\n const signed = (\"signed\" in opt) ? !!opt.signed : true;\n delete opt.signed;\n\n app.use(session(opt, app));\n app.use(async (ctx, next) => {\n ctx.session.active = true; //idk why but without this it doesnt work :)\n await next();\n });\n \n io.use(async (socket, next) => {\n \n if (!socket.handshake.headers.cookie) { return next(new Error('no cookie')); }\n \n const ctx = app.createContext(socket.request, socket.response);\n const sid = ctx.cookies.get(key, { signed });\n \n virtual(socket, \"sessionId\", _=>sid);\n virtual(socket, \"session\", _=>store.get(sid));\n \n await next();\n });\n};\n\n\n\nexport default attachSession;", "import { solid } from \"@randajan/props\";\r\n\r\n\r\nexport class SessionStore {\r\n constructor() {\r\n solid(this, \"_recs\", new Map());\r\n }\r\n\r\n get(sid) {\r\n const rec = this._recs.get(sid);\r\n if (!rec) { return; }\r\n if (Date.now() < rec.expiresAt) { return rec.session; }\r\n this.destroy(sid);\r\n }\r\n\r\n set(sid, session, maxAge) {\r\n const expiresAt = Date.now() + maxAge;\r\n this._recs.set(sid, { session, expiresAt, maxAge });\r\n }\r\n\r\n destroy(sid) {\r\n this._recs.delete(sid);\r\n }\r\n};"],
5
+ "mappings": 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MAAI,QAAI,mBAAAC,SAAQ,KAAK,GAAG,CAAC;AACzB,MAAI,IAAI,OAAO,KAAK,SAAS;AACzB,QAAI,QAAQ,SAAS;AACrB,UAAM,KAAK;AAAA,EACf,CAAC;AAED,KAAG,IAAI,OAAO,QAAQ,SAAS;AAE3B,QAAI,CAAC,OAAO,UAAU,QAAQ,QAAQ;AAAE,aAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAAA,IAAG;AAE7E,UAAM,MAAM,IAAI,cAAc,OAAO,SAAS,OAAO,QAAQ;AAC7D,UAAM,MAAM,IAAI,QAAQ,IAAI,KAAK,EAAE,OAAO,CAAC;AAE3C,+BAAQ,QAAQ,aAAa,OAAG,GAAG;AACnC,+BAAQ,QAAQ,WAAW,OAAG,MAAM,IAAI,GAAG,CAAC;AAE5C,UAAM,KAAK;AAAA,EACf,CAAC;AACL;AAIA,IAAO,gBAAQ;",
6
+ "names": ["session", "import_props", "crypto", "session"]
7
+ }
@@ -0,0 +1,67 @@
1
+ // src/index.js
2
+ import crypto from "crypto";
3
+ import session from "koa-session";
4
+
5
+ // src/SessionStore.js
6
+ import { solid } from "@randajan/props";
7
+ var SessionStore = class {
8
+ constructor() {
9
+ solid(this, "_recs", /* @__PURE__ */ new Map());
10
+ }
11
+ get(sid) {
12
+ const rec = this._recs.get(sid);
13
+ if (!rec) {
14
+ return;
15
+ }
16
+ if (Date.now() < rec.expiresAt) {
17
+ return rec.session;
18
+ }
19
+ this.destroy(sid);
20
+ }
21
+ set(sid, session2, maxAge) {
22
+ const expiresAt = Date.now() + maxAge;
23
+ this._recs.set(sid, { session: session2, expiresAt, maxAge });
24
+ }
25
+ destroy(sid) {
26
+ this._recs.delete(sid);
27
+ }
28
+ };
29
+
30
+ // src/index.js
31
+ import { virtual } from "@randajan/props";
32
+ var uid = (len = 12) => crypto.randomBytes(len).toString("base64url").slice(0, len);
33
+ var attachSession = (app, io, opt = {}) => {
34
+ if (!app.keys) {
35
+ app.keys = Array(6).fill().map(() => uid(12));
36
+ }
37
+ if (!opt.key) {
38
+ opt.key = uid(12);
39
+ }
40
+ if (!opt.store) {
41
+ opt.store = new SessionStore();
42
+ }
43
+ const { key, store } = opt;
44
+ const signed = "signed" in opt ? !!opt.signed : true;
45
+ delete opt.signed;
46
+ app.use(session(opt, app));
47
+ app.use(async (ctx, next) => {
48
+ ctx.session.active = true;
49
+ await next();
50
+ });
51
+ io.use(async (socket, next) => {
52
+ if (!socket.handshake.headers.cookie) {
53
+ return next(new Error("no cookie"));
54
+ }
55
+ const ctx = app.createContext(socket.request, socket.response);
56
+ const sid = ctx.cookies.get(key, { signed });
57
+ virtual(socket, "sessionId", (_) => sid);
58
+ virtual(socket, "session", (_) => store.get(sid));
59
+ await next();
60
+ });
61
+ };
62
+ var index_default = attachSession;
63
+ export {
64
+ attachSession,
65
+ index_default as default
66
+ };
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.js", "../../src/SessionStore.js"],
4
+ "sourcesContent": ["import crypto from \"crypto\";\nimport session from \"koa-session\";\nimport { SessionStore } from \"./SessionStore\";\nimport { virtual } from \"@randajan/props\";\n\nconst uid = (len = 12) => crypto.randomBytes(len).toString(\"base64url\").slice(0, len);\n\nexport const attachSession = (app, io, opt = {}) => {\n if (!app.keys) { app.keys = Array(6).fill().map(() => uid(12)); }\n\n if (!opt.key) { opt.key = uid(12); }\n if (!opt.store) { opt.store = new SessionStore(); }\n\n const { key, store } = opt;\n\n const signed = (\"signed\" in opt) ? !!opt.signed : true;\n delete opt.signed;\n\n app.use(session(opt, app));\n app.use(async (ctx, next) => {\n ctx.session.active = true; //idk why but without this it doesnt work :)\n await next();\n });\n \n io.use(async (socket, next) => {\n \n if (!socket.handshake.headers.cookie) { return next(new Error('no cookie')); }\n \n const ctx = app.createContext(socket.request, socket.response);\n const sid = ctx.cookies.get(key, { signed });\n \n virtual(socket, \"sessionId\", _=>sid);\n virtual(socket, \"session\", _=>store.get(sid));\n \n await next();\n });\n};\n\n\n\nexport default attachSession;", "import { solid } from \"@randajan/props\";\r\n\r\n\r\nexport class SessionStore {\r\n constructor() {\r\n solid(this, \"_recs\", new Map());\r\n }\r\n\r\n get(sid) {\r\n const rec = this._recs.get(sid);\r\n if (!rec) { return; }\r\n if (Date.now() < rec.expiresAt) { return rec.session; }\r\n this.destroy(sid);\r\n }\r\n\r\n set(sid, session, maxAge) {\r\n const expiresAt = Date.now() + maxAge;\r\n this._recs.set(sid, { session, expiresAt, maxAge });\r\n }\r\n\r\n destroy(sid) {\r\n this._recs.delete(sid);\r\n }\r\n};"],
5
+ "mappings": ";AAAA,OAAO,YAAY;AACnB,OAAO,aAAa;;;ACDpB,SAAS,aAAa;AAGf,IAAM,eAAN,MAAmB;AAAA,EACtB,cAAc;AACV,UAAM,MAAM,SAAS,oBAAI,IAAI,CAAC;AAAA,EAClC;AAAA,EAEA,IAAI,KAAK;AACL,UAAM,MAAM,KAAK,MAAM,IAAI,GAAG;AAC9B,QAAI,CAAC,KAAK;AAAE;AAAA,IAAQ;AACpB,QAAI,KAAK,IAAI,IAAI,IAAI,WAAW;AAAE,aAAO,IAAI;AAAA,IAAS;AACtD,SAAK,QAAQ,GAAG;AAAA,EACpB;AAAA,EAEA,IAAI,KAAKA,UAAS,QAAQ;AACtB,UAAM,YAAY,KAAK,IAAI,IAAI;AAC/B,SAAK,MAAM,IAAI,KAAK,EAAE,SAAAA,UAAS,WAAW,OAAO,CAAC;AAAA,EACtD;AAAA,EAEA,QAAQ,KAAK;AACT,SAAK,MAAM,OAAO,GAAG;AAAA,EACzB;AACJ;;;ADpBA,SAAS,eAAe;AAExB,IAAM,MAAM,CAAC,MAAM,OAAO,OAAO,YAAY,GAAG,EAAE,SAAS,WAAW,EAAE,MAAM,GAAG,GAAG;AAE7E,IAAM,gBAAgB,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM;AAChD,MAAI,CAAC,IAAI,MAAM;AAAE,QAAI,OAAO,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,MAAM,IAAI,EAAE,CAAC;AAAA,EAAG;AAEhE,MAAI,CAAC,IAAI,KAAK;AAAE,QAAI,MAAM,IAAI,EAAE;AAAA,EAAG;AACnC,MAAI,CAAC,IAAI,OAAO;AAAE,QAAI,QAAQ,IAAI,aAAa;AAAA,EAAG;AAElD,QAAM,EAAE,KAAK,MAAM,IAAI;AAEvB,QAAM,SAAU,YAAY,MAAO,CAAC,CAAC,IAAI,SAAS;AAClD,SAAO,IAAI;AAEX,MAAI,IAAI,QAAQ,KAAK,GAAG,CAAC;AACzB,MAAI,IAAI,OAAO,KAAK,SAAS;AACzB,QAAI,QAAQ,SAAS;AACrB,UAAM,KAAK;AAAA,EACf,CAAC;AAED,KAAG,IAAI,OAAO,QAAQ,SAAS;AAE3B,QAAI,CAAC,OAAO,UAAU,QAAQ,QAAQ;AAAE,aAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAAA,IAAG;AAE7E,UAAM,MAAM,IAAI,cAAc,OAAO,SAAS,OAAO,QAAQ;AAC7D,UAAM,MAAM,IAAI,QAAQ,IAAI,KAAK,EAAE,OAAO,CAAC;AAE3C,YAAQ,QAAQ,aAAa,OAAG,GAAG;AACnC,YAAQ,QAAQ,WAAW,OAAG,MAAM,IAAI,GAAG,CAAC;AAE5C,UAAM,KAAK;AAAA,EACf,CAAC;AACL;AAIA,IAAO,gBAAQ;",
6
+ "names": ["session"]
7
+ }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@randajan/koa-io-session",
3
+ "version": "0.0.3",
4
+ "description": "Simple bridge between koa-session and socket.io. Shares a unified session across HTTP and WebSocket using a common session store.",
5
+ "repository": "randajan/koa-io-session",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.js",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/esm/index.mjs",
12
+ "require": "./dist/cjs/index.cjs"
13
+ }
14
+ },
15
+ "license": "MIT",
16
+ "devDependencies": {
17
+ "@randajan/simple-lib": "^3.2.0"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "keywords": [
25
+ "koa",
26
+ "socket.io",
27
+ "session",
28
+ "koa-session",
29
+ "websocket",
30
+ "shared-session",
31
+ "middleware",
32
+ "bridge",
33
+ "auth",
34
+ "realtime"
35
+ ],
36
+ "homepage": "https://github.com/randajan/koa-io-session",
37
+ "bugs": {
38
+ "url": "https://github.com/randajan/koa-io-session/issues"
39
+ },
40
+ "author": {
41
+ "name": "Jan Randa",
42
+ "email": "jnranda@gmail.com",
43
+ "url": "https://www.linkedin.com/in/randajan/"
44
+ },
45
+ "dependencies": {
46
+ "@randajan/props": "^0.1.4",
47
+ "crypto": "^1.0.1",
48
+ "koa-session": "^7.0.2"
49
+ }
50
+ }