hono-sessions 0.3.6 → 0.4.0

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,6 +7,7 @@ Hono Sessions is currently tested on these runtimes:
7
7
 
8
8
  - Deno
9
9
  - Cloudflare Workers
10
+ - Cloudflare Pages
10
11
  - Bun
11
12
  - Node (v20+)
12
13
 
@@ -156,7 +157,7 @@ export default {
156
157
  }
157
158
  ```
158
159
 
159
- ### Cloudflare Workers
160
+ ### Cloudflare Workers / Pages
160
161
 
161
162
  ```ts
162
163
  import { Hono } from 'hono'
package/esm/deps.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export { nanoid } from 'nanoid/async';
2
1
  export type { Context, MiddlewareHandler } from 'hono';
3
2
  export { createMiddleware } from 'hono/factory';
4
3
  export { getCookie, setCookie } from 'hono/cookie';
package/esm/deps.js CHANGED
@@ -1,4 +1,3 @@
1
- export { nanoid } from 'nanoid/async';
2
1
  export { createMiddleware } from 'hono/factory';
3
2
  export { getCookie, setCookie } from 'hono/cookie';
4
3
  export * as Iron from 'iron-webcrypto';
@@ -1,2 +1,16 @@
1
+ /**
2
+ * Encrypt a string or object value
3
+ *
4
+ * @param password Random string at least 32 characters long
5
+ * @param payload String or object to encrypt
6
+ * @returns A promise of the encrypted string
7
+ */
1
8
  export declare function encrypt(password: string, payload: object | string): Promise<string>;
9
+ /**
10
+ * Decrypt an encrypted payload
11
+ *
12
+ * @param password Random string at least 32 characters long
13
+ * @param encrypted Encrypted string
14
+ * @returns Promise of the unencrypted value (string or object in most cases)
15
+ */
2
16
  export declare function decrypt(password: string, encrypted: string): Promise<unknown>;
package/esm/src/Crypto.js CHANGED
@@ -1,7 +1,21 @@
1
1
  import { Iron } from '../deps.js';
2
+ /**
3
+ * Encrypt a string or object value
4
+ *
5
+ * @param password Random string at least 32 characters long
6
+ * @param payload String or object to encrypt
7
+ * @returns A promise of the encrypted string
8
+ */
2
9
  export async function encrypt(password, payload) {
3
10
  return await Iron.seal(globalThis.crypto, payload, password, Iron.defaults);
4
11
  }
12
+ /**
13
+ * Decrypt an encrypted payload
14
+ *
15
+ * @param password Random string at least 32 characters long
16
+ * @param encrypted Encrypted string
17
+ * @returns Promise of the unencrypted value (string or object in most cases)
18
+ */
5
19
  export async function decrypt(password, encrypted) {
6
20
  return await Iron.unseal(globalThis.crypto, encrypted, { default: password }, Iron.defaults);
7
21
  }
@@ -9,5 +9,6 @@ interface SessionOptions {
9
9
  cookieOptions?: CookieOptions;
10
10
  sessionCookieName?: string;
11
11
  }
12
+ /** Function that returns a Hono-compatible session middleware */
12
13
  export declare function sessionMiddleware(options: SessionOptions): MiddlewareHandler<any, any, {}>;
13
14
  export {};
@@ -1,7 +1,7 @@
1
- import { nanoid } from '../deps.js';
2
1
  import { getCookie, setCookie, createMiddleware } from '../deps.js';
3
2
  import CookieStore from './store/CookieStore.js';
4
3
  import { Session, encrypt, decrypt } from '../mod.js';
4
+ /** Function that returns a Hono-compatible session middleware */
5
5
  export function sessionMiddleware(options) {
6
6
  const store = options.store;
7
7
  const encryptionKey = options.encryptionKey;
@@ -67,7 +67,7 @@ export function sessionMiddleware(options) {
67
67
  await store.createSession(c, defaultData);
68
68
  }
69
69
  else {
70
- sid = await nanoid(21);
70
+ sid = globalThis.crypto.randomUUID();
71
71
  await store.createSession(sid, defaultData);
72
72
  }
73
73
  session.setCache(defaultData);
@@ -96,7 +96,7 @@ export function sessionMiddleware(options) {
96
96
  shouldRotateSessionKey;
97
97
  if (shouldRecreateSessionForNonCookieStore) {
98
98
  await store.deleteSession(sid);
99
- sid = await nanoid(21);
99
+ sid = globalThis.crypto.randomUUID();
100
100
  await store.createSession(sid, session.getCache());
101
101
  setCookie(c, sessionCookieName, encryptionKey ? await encrypt(encryptionKey, sid) : sid, cookieOptions);
102
102
  }
@@ -2,12 +2,18 @@ interface SessionDataEntry {
2
2
  value: unknown;
3
3
  flash: boolean;
4
4
  }
5
+ /**
6
+ * Interface for specifying the necessary data for a session entry
7
+ */
5
8
  export interface SessionData {
6
9
  _data: Record<string, SessionDataEntry>;
7
10
  _expire: string | null;
8
11
  _delete: boolean;
9
12
  _accessed: string | null;
10
13
  }
14
+ /**
15
+ * Session class with methods for interacting with the session
16
+ */
11
17
  export declare class Session {
12
18
  private cache;
13
19
  constructor();
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Session class with methods for interacting with the session
3
+ */
1
4
  export class Session {
2
5
  constructor() {
3
6
  Object.defineProperty(this, "cache", {
@@ -5,6 +5,9 @@ interface CookieStoreOptions {
5
5
  cookieOptions?: CookieOptions;
6
6
  sessionCookieName: string;
7
7
  }
8
+ /**
9
+ * Cookie storage driver class
10
+ */
8
11
  declare class CookieStore {
9
12
  encryptionKey: string | null | undefined;
10
13
  cookieOptions: CookieOptions | undefined;
@@ -1,5 +1,8 @@
1
1
  import { getCookie, setCookie } from '../../deps.js';
2
2
  import { encrypt, decrypt } from '../../mod.js';
3
+ /**
4
+ * Cookie storage driver class
5
+ */
3
6
  class CookieStore {
4
7
  constructor(options) {
5
8
  Object.defineProperty(this, "encryptionKey", {
@@ -1,5 +1,8 @@
1
1
  import Store from './Store.js';
2
2
  import { SessionData } from '../../mod.js';
3
+ /**
4
+ * Memory storage driver class
5
+ */
3
6
  declare class MemoryStore implements Store {
4
7
  private data;
5
8
  constructor();
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Memory storage driver class
3
+ */
1
4
  class MemoryStore {
2
5
  constructor() {
3
6
  Object.defineProperty(this, "data", {
@@ -1,4 +1,7 @@
1
1
  import { SessionData } from "../../mod.js";
2
+ /**
3
+ * Interface for required methods in session storage drivers
4
+ */
2
5
  export default interface Store {
3
6
  getSessionById(sessionId?: string): SessionData | null | undefined | Promise<SessionData | null | undefined>;
4
7
  createSession(sessionId: string, initialData: SessionData): Promise<void> | void;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/mod.js",
3
3
  "main": "./script/mod.js",
4
4
  "name": "hono-sessions",
5
- "version": "0.3.6",
5
+ "version": "0.4.0",
6
6
  "description": "Cookie-based sessions for Hono web framework",
7
7
  "license": "MIT",
8
8
  "repository": {
@@ -28,7 +28,6 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "hono": "3.12.8",
31
- "iron-webcrypto": "0.10.1",
32
- "nanoid": "4.0.0"
31
+ "iron-webcrypto": "0.10.1"
33
32
  }
34
33
  }
package/script/deps.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export { nanoid } from 'nanoid/async';
2
1
  export type { Context, MiddlewareHandler } from 'hono';
3
2
  export { createMiddleware } from 'hono/factory';
4
3
  export { getCookie, setCookie } from 'hono/cookie';
package/script/deps.js CHANGED
@@ -23,9 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.Iron = exports.setCookie = exports.getCookie = exports.createMiddleware = exports.nanoid = void 0;
27
- var async_1 = require("nanoid/async");
28
- Object.defineProperty(exports, "nanoid", { enumerable: true, get: function () { return async_1.nanoid; } });
26
+ exports.Iron = exports.setCookie = exports.getCookie = exports.createMiddleware = void 0;
29
27
  var factory_1 = require("hono/factory");
30
28
  Object.defineProperty(exports, "createMiddleware", { enumerable: true, get: function () { return factory_1.createMiddleware; } });
31
29
  var cookie_1 = require("hono/cookie");
@@ -1,2 +1,16 @@
1
+ /**
2
+ * Encrypt a string or object value
3
+ *
4
+ * @param password Random string at least 32 characters long
5
+ * @param payload String or object to encrypt
6
+ * @returns A promise of the encrypted string
7
+ */
1
8
  export declare function encrypt(password: string, payload: object | string): Promise<string>;
9
+ /**
10
+ * Decrypt an encrypted payload
11
+ *
12
+ * @param password Random string at least 32 characters long
13
+ * @param encrypted Encrypted string
14
+ * @returns Promise of the unencrypted value (string or object in most cases)
15
+ */
2
16
  export declare function decrypt(password: string, encrypted: string): Promise<unknown>;
@@ -2,10 +2,24 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.decrypt = exports.encrypt = void 0;
4
4
  const deps_js_1 = require("../deps.js");
5
+ /**
6
+ * Encrypt a string or object value
7
+ *
8
+ * @param password Random string at least 32 characters long
9
+ * @param payload String or object to encrypt
10
+ * @returns A promise of the encrypted string
11
+ */
5
12
  async function encrypt(password, payload) {
6
13
  return await deps_js_1.Iron.seal(globalThis.crypto, payload, password, deps_js_1.Iron.defaults);
7
14
  }
8
15
  exports.encrypt = encrypt;
16
+ /**
17
+ * Decrypt an encrypted payload
18
+ *
19
+ * @param password Random string at least 32 characters long
20
+ * @param encrypted Encrypted string
21
+ * @returns Promise of the unencrypted value (string or object in most cases)
22
+ */
9
23
  async function decrypt(password, encrypted) {
10
24
  return await deps_js_1.Iron.unseal(globalThis.crypto, encrypted, { default: password }, deps_js_1.Iron.defaults);
11
25
  }
@@ -9,5 +9,6 @@ interface SessionOptions {
9
9
  cookieOptions?: CookieOptions;
10
10
  sessionCookieName?: string;
11
11
  }
12
+ /** Function that returns a Hono-compatible session middleware */
12
13
  export declare function sessionMiddleware(options: SessionOptions): MiddlewareHandler<any, any, {}>;
13
14
  export {};
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.sessionMiddleware = void 0;
7
7
  const deps_js_1 = require("../deps.js");
8
- const deps_js_2 = require("../deps.js");
9
8
  const CookieStore_js_1 = __importDefault(require("./store/CookieStore.js"));
10
9
  const mod_js_1 = require("../mod.js");
10
+ /** Function that returns a Hono-compatible session middleware */
11
11
  function sessionMiddleware(options) {
12
12
  const store = options.store;
13
13
  const encryptionKey = options.encryptionKey;
@@ -26,12 +26,12 @@ function sessionMiddleware(options) {
26
26
  store.cookieOptions = cookieOptions;
27
27
  }
28
28
  }
29
- const middleware = (0, deps_js_2.createMiddleware)(async (c, next) => {
29
+ const middleware = (0, deps_js_1.createMiddleware)(async (c, next) => {
30
30
  const session = new mod_js_1.Session;
31
31
  let sid = '';
32
32
  let session_data;
33
33
  let createNewSession = false;
34
- const sessionCookie = (0, deps_js_2.getCookie)(c, sessionCookieName);
34
+ const sessionCookie = (0, deps_js_1.getCookie)(c, sessionCookieName);
35
35
  if (sessionCookie) { // If there is a session cookie present...
36
36
  if (store instanceof CookieStore_js_1.default) {
37
37
  session_data = await store.getSession(c);
@@ -73,13 +73,13 @@ function sessionMiddleware(options) {
73
73
  await store.createSession(c, defaultData);
74
74
  }
75
75
  else {
76
- sid = await (0, deps_js_1.nanoid)(21);
76
+ sid = globalThis.crypto.randomUUID();
77
77
  await store.createSession(sid, defaultData);
78
78
  }
79
79
  session.setCache(defaultData);
80
80
  }
81
81
  if (!(store instanceof CookieStore_js_1.default)) {
82
- (0, deps_js_2.setCookie)(c, sessionCookieName, encryptionKey ? await (0, mod_js_1.encrypt)(encryptionKey, sid) : sid, cookieOptions);
82
+ (0, deps_js_1.setCookie)(c, sessionCookieName, encryptionKey ? await (0, mod_js_1.encrypt)(encryptionKey, sid) : sid, cookieOptions);
83
83
  }
84
84
  session.updateAccess();
85
85
  c.set('session', session);
@@ -102,9 +102,9 @@ function sessionMiddleware(options) {
102
102
  shouldRotateSessionKey;
103
103
  if (shouldRecreateSessionForNonCookieStore) {
104
104
  await store.deleteSession(sid);
105
- sid = await (0, deps_js_1.nanoid)(21);
105
+ sid = globalThis.crypto.randomUUID();
106
106
  await store.createSession(sid, session.getCache());
107
- (0, deps_js_2.setCookie)(c, sessionCookieName, encryptionKey ? await (0, mod_js_1.encrypt)(encryptionKey, sid) : sid, cookieOptions);
107
+ (0, deps_js_1.setCookie)(c, sessionCookieName, encryptionKey ? await (0, mod_js_1.encrypt)(encryptionKey, sid) : sid, cookieOptions);
108
108
  }
109
109
  /*
110
110
  * We skip session data persistence if it was just deleted.
@@ -2,12 +2,18 @@ interface SessionDataEntry {
2
2
  value: unknown;
3
3
  flash: boolean;
4
4
  }
5
+ /**
6
+ * Interface for specifying the necessary data for a session entry
7
+ */
5
8
  export interface SessionData {
6
9
  _data: Record<string, SessionDataEntry>;
7
10
  _expire: string | null;
8
11
  _delete: boolean;
9
12
  _accessed: string | null;
10
13
  }
14
+ /**
15
+ * Session class with methods for interacting with the session
16
+ */
11
17
  export declare class Session {
12
18
  private cache;
13
19
  constructor();
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Session = void 0;
4
+ /**
5
+ * Session class with methods for interacting with the session
6
+ */
4
7
  class Session {
5
8
  constructor() {
6
9
  Object.defineProperty(this, "cache", {
@@ -5,6 +5,9 @@ interface CookieStoreOptions {
5
5
  cookieOptions?: CookieOptions;
6
6
  sessionCookieName: string;
7
7
  }
8
+ /**
9
+ * Cookie storage driver class
10
+ */
8
11
  declare class CookieStore {
9
12
  encryptionKey: string | null | undefined;
10
13
  cookieOptions: CookieOptions | undefined;
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const deps_js_1 = require("../../deps.js");
4
4
  const mod_js_1 = require("../../mod.js");
5
+ /**
6
+ * Cookie storage driver class
7
+ */
5
8
  class CookieStore {
6
9
  constructor(options) {
7
10
  Object.defineProperty(this, "encryptionKey", {
@@ -1,5 +1,8 @@
1
1
  import Store from './Store.js';
2
2
  import { SessionData } from '../../mod.js';
3
+ /**
4
+ * Memory storage driver class
5
+ */
3
6
  declare class MemoryStore implements Store {
4
7
  private data;
5
8
  constructor();
@@ -1,5 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Memory storage driver class
5
+ */
3
6
  class MemoryStore {
4
7
  constructor() {
5
8
  Object.defineProperty(this, "data", {
@@ -1,4 +1,7 @@
1
1
  import { SessionData } from "../../mod.js";
2
+ /**
3
+ * Interface for required methods in session storage drivers
4
+ */
2
5
  export default interface Store {
3
6
  getSessionById(sessionId?: string): SessionData | null | undefined | Promise<SessionData | null | undefined>;
4
7
  createSession(sessionId: string, initialData: SessionData): Promise<void> | void;