@randajan/koa-io-session 0.0.4 → 0.0.5

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
@@ -1,52 +1,64 @@
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
- const store = 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
-
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
+ const store = 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
+ ## Production notes
51
+
52
+ - **Stable signing keys**: Provide your own `app.keys` and a fixed `opt.key` (cookie name).
53
+ Using randomly generated values on every server restart will invalidate existing signed cookies and force users to log in again.
54
+ - **Persistent stores for production**: The bundled in‑memory store works only for local development because all sessions disappear when the process restarts.
55
+ Configure a persistent store such as Redis, DynamoDB, or SQL for real deployments.
56
+ - **Proxy deep‑mutation limitation**: The session proxy tracks changes only on top‑level properties.
57
+ If you mutate nested objects you must either replace the whole object or use immutable updates so that changes are picked up and persisted.
58
+ - **Middleware order matters**: Call `attachSession` *before* any middleware (Router, authentication, etc.) that expects `ctx.session` to exist.
59
+
60
+
61
+
62
+ ## License
63
+
52
64
  MIT © [randajan](https://github.com/randajan)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@randajan/koa-io-session",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Simple bridge between koa-session and socket.io. Shares a unified session across HTTP and WebSocket using a common session store.",
5
5
  "repository": "randajan/koa-io-session",
6
6
  "type": "module",
@@ -1,98 +0,0 @@
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
- return opt.store;
96
- };
97
- var index_default = attachSession;
98
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
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 return opt.store;\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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAmB;AACnB,yBAAoB;;;ACDpB,mBAAsB;AAGf,IAAM,eAAN,MAAmB;AAAA,EACtB,cAAc;AACV,4BAAM,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,IAAAC,gBAAwB;AAExB,IAAM,MAAM,CAAC,MAAM,OAAO,cAAAC,QAAO,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,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;AAED,SAAO,IAAI;AACf;AAIA,IAAO,gBAAQ;",
6
- "names": ["session", "import_props", "crypto", "session"]
7
- }
@@ -1,68 +0,0 @@
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
- return opt.store;
62
- };
63
- var index_default = attachSession;
64
- export {
65
- attachSession,
66
- index_default as default
67
- };
68
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
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 return opt.store;\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;A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