Comparing version 1.3.1 to 1.3.2
@@ -111,4 +111,5 @@ import { Context } from '../context'; | ||
* | ||
* Consider using third-party session middleware instead of rolling your own | ||
* implementation of this. | ||
* Consider using a [known storage | ||
* adapter](https://grammy.dev/plugins/session.html#known-storage-adapters) | ||
* instead of rolling your own implementation of this. | ||
* | ||
@@ -198,2 +199,33 @@ * The default implementation will store session in memory. The data will be | ||
export declare function lazySession<S, C extends Context>(options?: SessionOptions<S>): MiddlewareFn<C & LazySessionFlavor<S>>; | ||
/** | ||
* The memory session storage is a built-in storage adapter that saves your | ||
* session data in RAM using a regular JavaScript `Map` object. If you use this | ||
* storage adapter, all sessions will be lost when your process terminates or | ||
* restarts. Hence, you should only use it for short-lived data that is not | ||
* important to persist. | ||
* | ||
* This class is used as default if you do not provide a storage adapter, e.g. | ||
* to your database. | ||
* | ||
* This storage adapter features expiring sessions. When instatiating this class | ||
* yourself, you can pass a time to live in milliseconds that will be used for | ||
* each session object. If a session for a user expired, the session data will | ||
* be discarded on its first read, and a fresh session object as returned by the | ||
* `inital` option (or undefined) will be put into place. | ||
*/ | ||
export declare class MemorySessionStorage<S> implements StorageAdapter<S> { | ||
private readonly timeToLive; | ||
private readonly storage; | ||
/** | ||
* Constructs a new memory session storage with the given time to live. Note | ||
* that this storage adapter will not store your data permanently. | ||
* | ||
* @param timeToLive TTL in milliseconds, default is `Infinity` | ||
*/ | ||
constructor(timeToLive?: number); | ||
read(key: string): S | undefined; | ||
write(key: string, value: S): void; | ||
private addExpiryDate; | ||
delete(key: string): void; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.lazySession = exports.session = void 0; | ||
exports.MemorySessionStorage = exports.lazySession = exports.session = void 0; | ||
const platform_1 = require("../platform"); | ||
@@ -53,3 +53,4 @@ const debug = platform_1.debug('grammy:session'); | ||
const getSessionKey = (_a = options === null || options === void 0 ? void 0 : options.getSessionKey) !== null && _a !== void 0 ? _a : defaultGetSessionKey; | ||
const storage = (_b = options === null || options === void 0 ? void 0 : options.storage) !== null && _b !== void 0 ? _b : new MemorySessionStorage(); | ||
const storage = (_b = options === null || options === void 0 ? void 0 : options.storage) !== null && _b !== void 0 ? _b : (debug('Storing session data in memory, all data will be lost when the bot restarts.'), | ||
new MemorySessionStorage()); | ||
return async (ctx, next) => { | ||
@@ -117,3 +118,4 @@ var _a, _b; | ||
const getSessionKey = (_a = options === null || options === void 0 ? void 0 : options.getSessionKey) !== null && _a !== void 0 ? _a : defaultGetSessionKey; | ||
const storage = (_b = options === null || options === void 0 ? void 0 : options.storage) !== null && _b !== void 0 ? _b : new MemorySessionStorage(); | ||
const storage = (_b = options === null || options === void 0 ? void 0 : options.storage) !== null && _b !== void 0 ? _b : (debug('Storing session data in memory, all data will be lost when the bot restarts.'), | ||
new MemorySessionStorage()); | ||
return async (ctx, next) => { | ||
@@ -178,7 +180,28 @@ const key = await getSessionKey(ctx); | ||
} | ||
/** | ||
* The memory session storage is a built-in storage adapter that saves your | ||
* session data in RAM using a regular JavaScript `Map` object. If you use this | ||
* storage adapter, all sessions will be lost when your process terminates or | ||
* restarts. Hence, you should only use it for short-lived data that is not | ||
* important to persist. | ||
* | ||
* This class is used as default if you do not provide a storage adapter, e.g. | ||
* to your database. | ||
* | ||
* This storage adapter features expiring sessions. When instatiating this class | ||
* yourself, you can pass a time to live in milliseconds that will be used for | ||
* each session object. If a session for a user expired, the session data will | ||
* be discarded on its first read, and a fresh session object as returned by the | ||
* `inital` option (or undefined) will be put into place. | ||
*/ | ||
class MemorySessionStorage { | ||
/** | ||
* Constructs a new memory session storage with the given time to live. Note | ||
* that this storage adapter will not store your data permanently. | ||
* | ||
* @param timeToLive TTL in milliseconds, default is `Infinity` | ||
*/ | ||
constructor(timeToLive = Infinity) { | ||
this.timeToLive = timeToLive; | ||
this.storage = new Map(); | ||
debug('Storing session data in memory, all data will be lost when the bot restarts.'); | ||
} | ||
@@ -212,1 +235,2 @@ read(key) { | ||
} | ||
exports.MemorySessionStorage = MemorySessionStorage; |
{ | ||
"name": "grammy", | ||
"description": "grammY core package ported to Node.js", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"author": "KnorpelSenf", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
457414
8493