Skip to content

Commit

Permalink
Merge pull request #117 from salute-developers/issue/115
Browse files Browse the repository at this point in the history
BREAKING: Добавлена автовалидация жизни сессии в StorageAdapter
  • Loading branch information
evgeniysemin authored Dec 6, 2023
2 parents 54a55a1 + 42c1e61 commit 66ad4e3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"subject-case": [0]
"subject-case": [0],
"type-enum": [2, "always", ["build", "ci", "chore", "feat", "fix", "docs", "style", "perf", "refactor", "test", "revert", "BREAKING"]],
"type-case": [2, "always", ["lower-case", "upper-case"]]
}
}
2 changes: 1 addition & 1 deletion packages/scenario/src/lib/types/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SaluteSession } from './salute';

export interface SaluteSessionStorage {
resolve: (id: string) => Promise<SaluteSession>;
save: (params: { id: string; session: SaluteSession; lifetime?: number }) => Promise<void>;
save: (params: { id: string; session: SaluteSession }) => Promise<void>;
reset: (id: string) => Promise<void>;
validate?: () => Promise<void>;
}
18 changes: 15 additions & 3 deletions packages/storage-adapter-memory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ import { SaluteSession, SaluteSessionStorage } from '@salutejs/scenario';
export class SaluteMemoryStorage implements SaluteSessionStorage {
private sessions: Record<string, SaluteSession> = {};

private lifetime: number;

constructor({ lifetime = 60 * 60 * 1000 }: { lifetime?: number } = {}) {
this.lifetime = lifetime;

setInterval(this.validate.bind(this), 60 * 1000);
}

async resolve(id: string) {
return Promise.resolve(
this.sessions[id] || {
path: [],
variables: {},
slotFilling: false,
state: {},
expires: this.lifetime > 0 ? Date.now() + this.lifetime : undefined,
},
);
}

async save({ id, session, lifetime = 0 }: { id: string; session: SaluteSession; lifetime?: number }) {
async save({ id, session }: { id: string; session: SaluteSession }) {
this.sessions[id] = session;
if (lifetime > 0) {
this.sessions[id].expires = Date.now() + lifetime;

if (this.lifetime > 0) {
this.sessions[id].expires = Date.now() + this.lifetime;
}

return Promise.resolve();
Expand All @@ -29,6 +39,7 @@ export class SaluteMemoryStorage implements SaluteSessionStorage {
variables: {},
slotFilling: false,
state: {},
expires: this.lifetime > 0 ? Date.now() + this.lifetime : undefined,
};

return Promise.resolve();
Expand All @@ -37,6 +48,7 @@ export class SaluteMemoryStorage implements SaluteSessionStorage {
async validate() {
Object.keys(this.sessions).forEach((sessionId) => {
const { expires } = this.sessions[sessionId];

if (expires && expires < Date.now()) {
delete this.sessions[sessionId];
}
Expand Down

0 comments on commit 66ad4e3

Please sign in to comment.