Skip to content
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
4 changes: 3 additions & 1 deletion benchmark/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ function createServer (sessionPlugin, cookiePlugin, storeType) {

if (storeType === 'redis') {
if (!redisClient) {
redisClient = new Redis()
redisClient = new Redis({
clientInfoTag: fastifySession.getDefaultClientInfoTag()
})
}
store = new RedisStore({ client: redisClient })
} else if (storeType === 'file') {
Expand Down
3 changes: 2 additions & 1 deletion examples/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const fastify = Fastify()

const store = new RedisStore({
client: new Redis({
enableAutoPipelining: true
enableAutoPipelining: true,
clientInfoTag: fastifySession.getDefaultClientInfoTag()
})
})

Expand Down
15 changes: 15 additions & 0 deletions lib/fastifySession.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ const idGenerator = require('./idGenerator')()
const Store = require('./store')
const Session = require('./session')

/**
* Get the default clientInfoTag for identifying the framework in Redis CLIENT SETINFO.
* This can be used when creating Redis clients for session stores.
* @returns {string} The client info tag (e.g., "fastify-session_v11.1.1")
*/
function getDefaultClientInfoTag () {
try {
const version = require('../package.json').version
return `fastify-session_v${version}`
} catch {
return 'fastify-session'
}
}

function fastifySession (fastify, options, next) {
const error = checkOptions(options)
if (error) {
Expand Down Expand Up @@ -276,3 +290,4 @@ module.exports.fastifySession = fastifySession

module.exports.Store = Store
module.exports.MemoryStore = Store
module.exports.getDefaultClientInfoTag = getDefaultClientInfoTag
44 changes: 44 additions & 0 deletions test/clientInfoTag.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const test = require('node:test')
const fastifySession = require('..')

test('getDefaultClientInfoTag should return version tag', (t) => {
t.plan(1)
const tag = fastifySession.getDefaultClientInfoTag()
t.assert.match(tag, /^fastify-session_v\d+\.\d+\.\d+$/)
})

test('getDefaultClientInfoTag should be accessible from module', (t) => {
t.plan(1)
t.assert.strictEqual(typeof fastifySession.getDefaultClientInfoTag, 'function')
})

test('getDefaultClientInfoTag should fallback to fastify-session when package.json version is unavailable', (t) => {
t.plan(1)

// Mock require to simulate missing package.json
const Module = require('module')
const originalRequire = Module.prototype.require

Module.prototype.require = function (id) {
if (id === '../package.json') {
throw new Error('Cannot find module')
}
return originalRequire.apply(this, arguments)
}

// Clear the require cache
delete require.cache[require.resolve('..')]

const fastifySessionMocked = require('..')
const tag = fastifySessionMocked.getDefaultClientInfoTag()

// Restore original require
Module.prototype.require = originalRequire

// Clear cache again
delete require.cache[require.resolve('..')]

t.assert.strictEqual(tag, 'fastify-session')
})
9 changes: 9 additions & 0 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare module 'fastify' {
type FastifySession = FastifyPluginCallback<fastifySession.FastifySessionOptions> & {
Store: fastifySession.MemoryStore,
MemoryStore: fastifySession.MemoryStore,
getDefaultClientInfoTag: () => string,
}

type Callback = (err?: any) => void
Expand Down Expand Up @@ -201,6 +202,14 @@ declare namespace fastifySession {

export const Store: MemoryStore

/**
* Get the default clientInfoTag for identifying the framework in Redis CLIENT SETINFO.
* This can be used when creating Redis clients for session stores.
* @link https://redis.io/docs/latest/commands/client-setinfo/
* @returns The client info tag (e.g., "fastify-session_v11.1.1")
*/
export function getDefaultClientInfoTag (): string

export const fastifySession: FastifySession
export { fastifySession as default }
}
Expand Down
9 changes: 9 additions & 0 deletions types/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare module 'fastify' {

expectType<SessionStore>(plugin.Store)
expectType<SessionStore>(plugin.MemoryStore)
expectType<string>(plugin.getDefaultClientInfoTag())

const secret = 'ABCDEFGHIJKLNMOPQRSTUVWXYZ012345'

Expand Down Expand Up @@ -63,6 +64,14 @@ app.register(plugin, {
secret,
store: new RedisStore({ client: new Redis() })
})
app.register(plugin, {
secret,
store: new RedisStore({
client: new Redis({
clientInfoTag: plugin.getDefaultClientInfoTag()
})
})
})
app.register(plugin, {
secret,
store: MongoStore.create({ mongoUrl: 'mongodb://connection-string' })
Expand Down