Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

update packages and code #33

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,52 @@ class RedisSession {
}, options)

this.client = redis.createClient(this.options.store)
this.client.connect()
}

getSession (key) {
return new Promise((resolve, reject) => {
this.client.get(key, (err, json) => {
if (err) {
return reject(err)
async getSession (key) {
try {
const json = await this.client.get(key)
if (json) {
try {
const session = JSON.parse(json)
debug('session state', key, session)
return session
} catch (error) {
debug('Parse session state failed', error)
}
if (json) {
try {
const session = JSON.parse(json)
debug('session state', key, session)
resolve(session)
} catch (error) {
debug('Parse session state failed', error)
}
}
resolve({})
})
})
}
return {}
} catch (error) {
throw new Error(error)
}
}

clearSession (key) {
debug('clear session', key)
return new Promise((resolve, reject) => {
this.client.del(key, (err, json) => {
if (err) {
return reject(err)
}
resolve()
})
})
async clearSession (key) {
try {
debug('clear session', key)
await this.client.del(key)
return
} catch (error) {
throw new Error(error)
}
}

saveSession (key, session) {
if (!session || Object.keys(session).length === 0) {
return this.clearSession(key)
async saveSession (key, session) {
try {
if (!session || Object.keys(session).length === 0) {
return this.clearSession(key)
}
debug('save session', key, session)
if (this.options.ttl) {
await this.client.set(key, JSON.stringify(session), 'EX', this.options.ttl)
} else {
await this.client.set(key, JSON.stringify(session))
}
return {}
} catch (error) {
throw new Error(error)
}
debug('save session', key, session)
return new Promise((resolve, reject) => {
this.client.set(key, JSON.stringify(session), (err, json) => {
if (err) {
return reject(err)
}
if (this.options.ttl) {
debug('session ttl', session)
this.client.expire(key, this.options.ttl)
}
resolve({})
})
})
}

middleware () {
Expand All @@ -81,4 +76,9 @@ class RedisSession {
}
}

process.on('SIGINT', function () {
redisClient.quit()
console.log('redis client quit')
})

module.exports = RedisSession
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@
},
"homepage": "https://github.com/telegraf/telegraf-session-redis#readme",
"engines": {
"node": ">=6.2.1"
"node": "^12.20.0 || >=14.13.1"
},
"files": [
"lib/session.js",
"lib/session.d.ts"
],
"scripts": {
"lint-fix": "eslint . --fix",
"lint": "eslint .",
"test": "ava test"
"test": "ava test/*"
},
"dependencies": {
"@types/redis": "^2.8.6",
"debug": "^4.1.1",
"redis": "^2.6.0-1"
"debug": "^4.3.4",
"redis": "^4.5.1"
},
"peerDependencies": {
"telegraf": "^3.9.0"
"telegraf": "^4.0.0"
},
"devDependencies": {
"ava": "^1.1.0",
"eslint": "^5.4.0",
"ava": "^4.0.0",
"eslint": "^8.27.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-ava": "^5.1.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.0",
"eslint-plugin-ava": "^13.2.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-standard": "^4.0.0",
"telegraf": "^3.9.0"
"telegraf": "^4.11.0"
}
}
16 changes: 11 additions & 5 deletions test/session-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const Telegraf = require('telegraf')
const { Telegraf } = require('telegraf');
const test = require('ava')
const RedisSession = require('../lib/session')

function createBot (...args) {
const bot = new Telegraf(...args)
bot.botInfo = { id: 42, is_bot: true, username: 'bot', first_name: 'Bot' }
return bot
}

test.serial('should be defined', (t) => {
const app = new Telegraf()
const app = createBot()
const session = new RedisSession({
store: {
host: process.env.TELEGRAM_SESSION_HOST || '127.0.0.1',
Expand Down Expand Up @@ -38,7 +44,7 @@ test.serial('should retrieve and save session', (t) => {
})

test.serial('should handle existing session', (t) => {
const app = new Telegraf()
const app = createBot()
const session = new RedisSession({
store: {
host: process.env.TELEGRAM_SESSION_HOST || '127.0.0.1',
Expand All @@ -56,7 +62,7 @@ test.serial('should handle existing session', (t) => {
})

test.serial('should handle not existing session', (t) => {
const app = new Telegraf()
const app = createBot()
const session = new RedisSession({
store: {
host: process.env.TELEGRAM_SESSION_HOST || '127.0.0.1',
Expand All @@ -73,7 +79,7 @@ test.serial('should handle not existing session', (t) => {
})

test.serial('should handle session reset', (t) => {
const app = new Telegraf()
const app = createBot()
const session = new RedisSession({
store: {
host: process.env.TELEGRAM_SESSION_HOST || '127.0.0.1',
Expand Down