@@ -17,9 +17,6 @@ interface UnsealOptions {
1717 ttl ?: number ;
1818}
1919
20- /**
21- * Parse an iron-session seal to extract the version
22- */
2320function parseSeal ( seal : string ) : {
2421 sealWithoutVersion : string ;
2522 tokenVersion : number | null ;
@@ -31,15 +28,6 @@ function parseSeal(seal: string): {
3128 return { sealWithoutVersion, tokenVersion } ;
3229}
3330
34- /**
35- * Seal (encrypt) data in a format compatible with iron-session.
36- *
37- * @param data - The data to seal
38- * @param options - Sealing options
39- * @param options.password - Password for encryption (must be at least 32 characters)
40- * @param options.ttl - Time to live in seconds (default: 0 = no expiration)
41- * @returns The sealed string
42- */
4331export async function sealData (
4432 data : unknown ,
4533 { password, ttl = 0 } : SealOptions ,
@@ -51,30 +39,19 @@ export async function sealData(
5139
5240 const seal = await ironSeal ( data , passwordObj , {
5341 ...defaults ,
54- ttl : ttl * 1000 , // Convert seconds to milliseconds
55- encode : JSON . stringify , // Preserve v1 lenient serialization
42+ ttl : ttl * 1000 ,
43+ encode : JSON . stringify ,
5644 } ) ;
5745
58- // Add the version delimiter exactly like iron-session does
5946 return `${ seal } ${ VERSION_DELIMITER } ${ CURRENT_MAJOR_VERSION } ` ;
6047}
6148
62- /**
63- * Unseal (decrypt) data that was sealed with iron-session or sealData.
64- *
65- * @param encryptedData - The sealed string to decrypt
66- * @param options - Unsealing options
67- * @param options.password - Password for decryption
68- * @param options.ttl - Time to live in seconds (default: 0 = no expiration check)
69- * @returns The unsealed data, or empty object if unsealing fails
70- */
7149export async function unsealData < T = unknown > (
7250 encryptedData : string ,
7351 { password, ttl = 0 } : UnsealOptions ,
7452) : Promise < T > {
7553 const { sealWithoutVersion, tokenVersion } = parseSeal ( encryptedData ) ;
7654
77- // Format password as a map like iron-session expects
7855 const passwordMap = { 1 : password } ;
7956
8057 let data : unknown ;
@@ -85,7 +62,6 @@ export async function unsealData<T = unknown>(
8562 ttl : ttl * 1000 ,
8663 } ) ) ?? { } ;
8764 } catch ( error ) {
88- // Match iron-session's behavior: return empty object for known errors
8965 if (
9066 error instanceof Error &&
9167 / ^ ( E x p i r e d s e a l | B a d h m a c v a l u e | C a n n o t f i n d p a s s w o r d | I n c o r r e c t n u m b e r o f s e a l e d c o m p o n e n t s ) / . test (
@@ -97,11 +73,9 @@ export async function unsealData<T = unknown>(
9773 throw error ;
9874 }
9975
100- // Handle token version for backwards compatibility
10176 if ( tokenVersion === 2 ) {
10277 return data as T ;
10378 } else if ( tokenVersion !== null ) {
104- // For older token versions, extract the persistent property
10579 const record = data as Record < string , unknown > ;
10680 return ( record . persistent ?? data ) as T ;
10781 }
0 commit comments