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
11 changes: 9 additions & 2 deletions benchmark/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

const { RedisStore } = require('connect-redis')
const Fastify = require('fastify')
const Redis = require('ioredis')
const { createClient } = require('redis')
const fileStoreFactory = require('session-file-store')
const { isMainThread } = require('node:worker_threads')

const fastifySession = require('..')
const fastifyCookie = require('@fastify/cookie')

let redisClient
let redisClientConnecting

function createServer (sessionPlugin, cookiePlugin, storeType) {
let requestCounter = 0
let store

if (storeType === 'redis') {
if (!redisClient) {
redisClient = new Redis()
redisClient = createClient()
redisClientConnecting = redisClient.connect()
}
store = new RedisStore({ client: redisClient })
} else if (storeType === 'file') {
Expand Down Expand Up @@ -55,6 +57,11 @@ function testFunction (sessionPlugin, cookiePlugin, storeType) {
const server = createServer(sessionPlugin, cookiePlugin, storeType)

return async function () {
if (redisClientConnecting) {
await redisClientConnecting
redisClientConnecting = null
}

const { headers } = await server.inject('/')
const setCookieHeader = headers['set-cookie']

Expand Down
13 changes: 6 additions & 7 deletions examples/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
const Fastify = require('fastify')
const fastifySession = require('..')
const fastifyCookie = require('@fastify/cookie')
const Redis = require('ioredis')
const { createClient } = require('redis')
const { RedisStore } = require('connect-redis')

const fastify = Fastify()

const redisClient = createClient()
redisClient.connect().catch(console.error)

const store = new RedisStore({
client: new Redis({
enableAutoPipelining: true
})
client: redisClient
})

fastify.register(fastifyCookie, {})
Expand All @@ -27,8 +28,6 @@ fastify.get('/', (request, reply) => {
})

const response = fastify.inject('/')
response.then(v => console.log(`

autocannon -p 10 -H "Cookie=${decodeURIComponent(v.headers['set-cookie'])}" http://127.0.0.1:3000`))
response.then(v => console.log(`\n\nautocannon -p 10 -H "Cookie=${decodeURIComponent(v.headers['set-cookie'])}" http://127.0.0.1:3000`))

fastify.listen({ port: 3000 })
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "c8 --100 node --test",
"test:typescript": "tstyche",
"redis": "docker run -p 6379:6379 --rm redis",
"benchmark": "node benchmark/bench.js",
"lint": "eslint",
"lint:fix": "eslint --fix"
Expand Down Expand Up @@ -65,12 +66,12 @@
"@types/node": "^26.0.1",
"c8": "^12.0.0",
"connect-mongo": "^6.0.0",
"connect-redis": "^8.0.0",
"connect-redis": "^9.0.0",
"cronometro": "^6.0.3",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"ioredis": "^5.3.2",
"neostandard": "^0.13.0",
"redis": "^6.0.0",
"session-file-store": "^1.5.0",
"tstyche": "^7.0.0"
},
Expand Down
70 changes: 47 additions & 23 deletions types/index.tst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import fastify, {
type FastifyRequest,
type Session
} from 'fastify'
import Redis from 'ioredis'
import { createClient } from 'redis'
import { expect } from 'tstyche'
import fastifySession, { type CookieOptions, MemoryStore, type SessionStore } from '..'
import fastifySession, {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR should focus on upgrading the connect-redis package and removing unnecessary formatting changes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR should focus on upgrading the connect-redis package and removing unnecessary formatting changes.

If I need to make any changes let me know

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert unrelated lint changes plz, you can open new PRs if necessary for certain plugins.
I just made this change on core:
fastify/fastify@d267263

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes made are solely for the update to connect-redis-v9; everyone has their own configurations in their editors or IDEs. Before committing, I always run npm run lint.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes made are solely for the update to connect-redis-v9;

Nope.

everyone has their own configurations in their editors or IDEs.

If your IDE mess with the current stylistics changes, it's your responsability to fix it before pushing.

I always run npm run lint.

Our current linting config doesn't enforce some stylistic choices, so your changes are not reverted by the linter.
But it creates noise in the PR review, it's ok because this one is small but in general this is a feedback you'll receive any time you add unrelated stylistic changes (I had the same in the past).

Image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll check. In the editor, I have two vertical lines showing 80 and 120. If I remember correctly, I locked it to 80 columns. I'm sure that's why it shifted some of the lines. 😆

type CookieOptions,
MemoryStore,
type SessionStore
} from '..'

const plugin = fastifySession

Expand All @@ -25,7 +29,7 @@ declare module 'fastify' {
user?: {
id: number;
};
foo: string
foo: string;
}
}

Expand Down Expand Up @@ -62,7 +66,7 @@ app.register(plugin, {
})
app.register(plugin, {
secret,
store: new RedisStore({ client: new Redis() })
store: new RedisStore({ client: createClient() })
})
app.register(plugin, {
secret,
Expand All @@ -77,19 +81,29 @@ app.register(plugin, {
idGenerator: () => Date.now() + ''
})
app.register(plugin, {
secret,
secret
})
app.register(plugin, {
secret,
idGenerator: (request) => `${request === undefined ? 'null' : request.ip}-${Date.now()}`
idGenerator: (request) =>
`${request === undefined ? 'null' : request.ip}-${Date.now()}`
})

expect(app.register).type.not.toBeCallableWith(plugin)
expect(app.register).type.not.toBeCallableWith(plugin, {})

expect(app.decryptSession).type.not.toBeInstantiableWith<[string]>()
app.decryptSession<{ hello: 'world' }>('sessionId', { hello: 'world' }, () => ({}))
app.decryptSession<{ hello: 'world' }>('sessionId', { hello: 'world' }, { domain: '/' }, () => ({}))
app.decryptSession<{ hello: 'world' }>(
'sessionId',
{ hello: 'world' },
() => ({})
)
app.decryptSession<{ hello: 'world' }>(
'sessionId',
{ hello: 'world' },
{ domain: '/' },
() => ({})
)
app.decryptSession('sessionId', {}, () => ({}))
app.decryptSession('sessionId', {}, { domain: '/' }, () => ({}))

Expand Down Expand Up @@ -136,21 +150,25 @@ app.route({
expect(request.session.regenerate(['foo'])).type.toBe<Promise<void>>()
expect(request.session.save()).type.toBe<Promise<void>>()

expect(request.session.options).type.not.toBeCallableWith({ keyNotInCookieOptions: true })
expect(request.session.options).type.not.toBeCallableWith({
keyNotInCookieOptions: true
})
expect(request.session.options).type.not.toBeCallableWith({ signed: true })

expect(request.session.options({})).type.toBe<void>()
expect(request.session.options({
domain: 'example.com',
expires: new Date(),
httpOnly: true,
maxAge: 1000,
partitioned: true,
path: '/',
sameSite: 'lax',
priority: 'low',
secure: 'auto'
})).type.toBe<void>()
expect(
request.session.options({
domain: 'example.com',
expires: new Date(),
httpOnly: true,
maxAge: 1000,
partitioned: true,
path: '/',
sameSite: 'lax',
priority: 'low',
secure: 'auto'
})
).type.toBe<void>()
}
})

Expand All @@ -169,8 +187,12 @@ const app2 = fastify()
app2.register(fastifySession, { secret: 'DizIzSecret' })

app2.get('/', async function (request) {
expect(request.session.get('foo')).type.toBeAssignableTo<string | undefined>()
expect(request.session.get('foo')).type.not.toBeAssignableTo<number | undefined>()
expect(request.session.get('foo')).type.toBeAssignableTo<
string | undefined
>()
expect(request.session.get('foo')).type.not.toBeAssignableTo<
number | undefined
>()

expect(request.session.set('foo', 'bar')).type.toBe<void>()

Expand All @@ -183,5 +205,7 @@ app2.get('/', async function (request) {
expect(request.session.set).type.not.toBeCallableWith('not exist', 'abc')

expect(request.session.get<any>('not exist')).type.toBe<any>()
expect(request.session.set<any>('not exist', 'abc')).type.toBeAssignableTo<any>()
expect(
request.session.set<any>('not exist', 'abc')
).type.toBeAssignableTo<any>()
})
Loading