Skip to content

Commit 7e44e60

Browse files
fix(deps): update dependency iron-session to v8 (#1557)
Bump major version for iron-session
1 parent 8de3208 commit 7e44e60

4 files changed

Lines changed: 80 additions & 149 deletions

File tree

packages/wallet/backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"hash-wasm": "^4.11.0",
2626
"helmet": "^7.1.0",
2727
"ioredis": "^5.4.1",
28-
"iron-session": "^6.3.1",
28+
"iron-session": "^8.0.3",
2929
"knex": "^3.1.0",
3030
"moment": "^2.30.1",
3131
"node-cache": "^5.1.2",
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
import type { Response } from 'express'
22
import type { IronSession } from 'iron-session'
33

4-
declare module 'iron-session' {
5-
interface IronSessionData {
6-
id: string
7-
user: UserSessionData
8-
}
9-
}
104
declare global {
115
// eslint-disable-next-line @typescript-eslint/no-explicit-any
126
type Send<ResBody = any, T = Response<ResBody>> = (body?: ResBody) => T
137

14-
type UserSessionData = {
8+
interface UserSessionData {
159
id: string
1610
email: string
1711
needsWallet: boolean
1812
needsIDProof: boolean
1913
}
2014

21-
// eslint-disable-next-line @typescript-eslint/prefer-namespace-keyword
22-
declare module Express {
15+
interface IronSessionData {
16+
id: string
17+
user: UserSessionData
18+
}
19+
20+
namespace Express {
2321
interface Request {
24-
session: IronSession
22+
session: IronSession<IronSessionData>
2523
}
2624
}
2725

@@ -36,3 +34,9 @@ declare global {
3634
toJSON(): string
3735
}
3836
}
37+
38+
declare module 'http' {
39+
interface IncomingMessage {
40+
session: IronSession<IronSessionData>
41+
}
42+
}
Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { env } from '@/config/env'
22
import type { NextFunction, Request, Response } from 'express'
3-
import { IronSessionOptions } from 'iron-session'
4-
import { ironSession } from 'iron-session/express'
3+
import {
4+
type SessionOptions,
5+
type IronSession,
6+
getIronSession
7+
} from 'iron-session'
58

6-
export const SESSION_OPTIONS: IronSessionOptions = {
9+
export const SESSION_OPTIONS: SessionOptions = {
710
password: env.COOKIE_PASSWORD,
811
cookieName: env.COOKIE_NAME,
912
cookieOptions: {
@@ -15,8 +18,50 @@ export const SESSION_OPTIONS: IronSessionOptions = {
1518
ttl: env.COOKIE_TTL
1619
} as const
1720

21+
// Utility from a previous version of iron-session.
22+
// https://github.com/vvo/iron-session/blob/v6.3.1/src/getPropertyDescriptorForReqSession.ts
23+
function getPropertyDescriptorForReqSession(
24+
session: IronSession<IronSessionData>
25+
): PropertyDescriptor {
26+
return {
27+
enumerable: true,
28+
get() {
29+
return session
30+
},
31+
set(value) {
32+
const keys = Object.keys(value)
33+
const currentKeys = Object.keys(session)
34+
35+
currentKeys.forEach((key) => {
36+
if (!keys.includes(key)) {
37+
// @ts-expect-error unknown keys
38+
delete session[key]
39+
}
40+
})
41+
42+
keys.forEach((key) => {
43+
// @ts-expect-error unknown keys
44+
session[key] = value[key]
45+
})
46+
}
47+
}
48+
}
49+
1850
export const withSession: (
1951
req: Request,
2052
res: Response,
2153
next: NextFunction
22-
) => Promise<void> = ironSession(SESSION_OPTIONS)
54+
) => Promise<void> = async (req, res, next) => {
55+
const session = await getIronSession<IronSessionData>(
56+
req,
57+
res,
58+
SESSION_OPTIONS
59+
)
60+
Object.defineProperty(
61+
req,
62+
'session',
63+
getPropertyDescriptorForReqSession(session)
64+
)
65+
66+
next()
67+
}

0 commit comments

Comments
 (0)