1- import { isServer } from "solid-js/web" ;
2- import { StorageProps , StorageSignalProps , StorageWithOptions } from "./types.js" ;
3- import { addClearMethod } from "./tools.js" ;
4- import { createStorage , createStorageSignal } from "./storage.js" ;
5- import type { PageEvent } from "solid-start" ;
1+ import { isServer } from "solid-js/web" ;
2+ import { StorageProps , StorageSignalProps , StorageWithOptions } from "./types.js" ;
3+ import { addClearMethod } from "./tools.js" ;
4+ import { createStorage , createStorageSignal } from "./storage.js" ;
5+ import type { PageEvent } from "solid-start" ;
66
77export type CookieOptions = CookieProperties & {
88 getRequest ?: ( ( ) => Request ) | ( ( ) => PageEvent ) ;
@@ -17,27 +17,33 @@ type CookieProperties = {
1717 httpOnly ?: boolean ;
1818 maxAge ?: number ;
1919 sameSite ?: "None" | "Lax" | "Strict" ;
20- }
21-
22- const cookiePropertyKeys = [ "domain" , "expires" , "path" , "secure" , "httpOnly" , "maxAge" , "sameSite" ] as const ;
20+ } ;
2321
22+ const cookiePropertyKeys = [
23+ "domain" ,
24+ "expires" ,
25+ "path" ,
26+ "secure" ,
27+ "httpOnly" ,
28+ "maxAge" ,
29+ "sameSite" ,
30+ ] as const ;
2431
2532function serializeCookieOptions ( options ?: CookieOptions ) {
2633 if ( ! options ) {
2734 return "" ;
2835 }
2936 let memo = "" ;
3037 for ( const key in options ) {
31- if ( ! cookiePropertyKeys . includes ( key as keyof CookieProperties ) )
32- continue ;
38+ if ( ! cookiePropertyKeys . includes ( key as keyof CookieProperties ) ) continue ;
3339
3440 const value = options [ key as keyof CookieProperties ] ;
3541 memo +=
3642 value instanceof Date
3743 ? `; ${ key } =${ value . toUTCString ( ) } `
3844 : typeof value === "boolean"
39- ? `; ${ key } `
40- : `; ${ key } =${ value } ` ;
45+ ? `; ${ key } `
46+ : `; ${ key } =${ value } ` ;
4147 }
4248 return memo ;
4349}
5258} catch ( e ) {
5359 useRequest = ( ) => {
5460 // eslint-disable-next-line no-console
55- console . warn ( "It seems you attempt to use cookieStorage on the server without having solid-start installed or use vite." ) ;
61+ console . warn (
62+ "It seems you attempt to use cookieStorage on the server without having solid-start installed or use vite." ,
63+ ) ;
5664 return {
57- request : { headers : { get : ( ) => "" } } as unknown as Request ,
65+ request : { headers : { get : ( ) => "" } } as unknown as Request ,
5866 } as unknown as PageEvent ;
5967 } ;
6068}
@@ -81,35 +89,45 @@ try {
8189export const cookieStorage : StorageWithOptions < CookieOptions > = addClearMethod ( {
8290 _read : isServer
8391 ? ( options ?: CookieOptions ) => {
84- const eventOrRequest = options ?. getRequest ?.( ) || useRequest ( ) ;
85- const request = eventOrRequest && ( "request" in eventOrRequest ? eventOrRequest . request : eventOrRequest ) ;
86- let result = ""
87- if ( eventOrRequest . responseHeaders ) // Check if we really got a pageEvent
88- {
89- const responseHeaders = eventOrRequest . responseHeaders as Headers ;
90- result += responseHeaders . get ( "Set-Cookie" ) ?. split ( "," ) . map ( cookie => ! cookie . match ( / \\ w * \\ s * = \\ s * [ ^ ; ] + / ) ) . join ( ";" ) ?? "" ;
92+ const eventOrRequest = options ?. getRequest ?.( ) || useRequest ( ) ;
93+ const request =
94+ eventOrRequest && ( "request" in eventOrRequest ? eventOrRequest . request : eventOrRequest ) ;
95+ let result = "" ;
96+ if ( eventOrRequest . responseHeaders ) {
97+ // Check if we really got a pageEvent
98+ const responseHeaders = eventOrRequest . responseHeaders as Headers ;
99+ result +=
100+ responseHeaders
101+ . get ( "Set-Cookie" )
102+ ?. split ( "," )
103+ . map ( cookie => ! cookie . match ( / \\ w * \\ s * = \\ s * [ ^ ; ] + / ) )
104+ . join ( ";" ) ?? "" ;
105+ }
106+ return `${ result } ;${ request ?. headers ?. get ( "Cookie" ) ?? "" } ` ; // because first cookie will be preferred we don't have to worry about duplicates
91107 }
92- return `${ result } ;${ request ?. headers ?. get ( "Cookie" ) ?? "" } ` ; // because first cookie will be preferred we don't have to worry about duplicates
93- }
94108 : ( ) => document . cookie ,
95109 _write : isServer
96110 ? ( key : string , value : string , options ?: CookieOptions ) => {
97- if ( options ?. setCookie ) {
98- options ?. setCookie ?.( key , value , options )
99- return
111+ if ( options ?. setCookie ) {
112+ options ?. setCookie ?.( key , value , options ) ;
113+ return ;
114+ }
115+ const pageEvent : PageEvent = options ?. getRequest ?.( ) || useRequest ( ) ;
116+ if ( ! pageEvent . responseHeaders )
117+ // Check if we really got a pageEvent
118+ return ;
119+ const responseHeaders = pageEvent . responseHeaders as Headers ;
120+ const cookies =
121+ responseHeaders
122+ . get ( "Set-Cookie" )
123+ ?. split ( "," )
124+ . filter ( cookie => ! cookie . match ( `\\s*${ key } \\s*=` ) ) ?? [ ] ;
125+ cookies . push ( `${ key } =${ value } ${ serializeCookieOptions ( options ) } ` ) ;
126+ responseHeaders . set ( "Set-Cookie" , cookies . join ( "," ) ) ;
100127 }
101- const pageEvent : PageEvent = options ?. getRequest ?.( ) || useRequest ( ) ;
102- if ( ! pageEvent . responseHeaders ) // Check if we really got a pageEvent
103- return
104- const responseHeaders = pageEvent . responseHeaders as Headers ;
105- const cookies = responseHeaders . get ( "Set-Cookie" ) ?. split ( "," )
106- . filter ( ( cookie ) => ! cookie . match ( `\\s*${ key } \\s*=` ) ) ?? [ ] ;
107- cookies . push ( `${ key } =${ value } ${ serializeCookieOptions ( options ) } ` )
108- responseHeaders . set ( "Set-Cookie" , cookies . join ( "," ) )
109- }
110128 : ( key : string , value : string , options ?: CookieOptions ) => {
111- document . cookie = `${ key } =${ value } ${ serializeCookieOptions ( options ) } ` ;
112- } ,
129+ document . cookie = `${ key } =${ value } ${ serializeCookieOptions ( options ) } ` ;
130+ } ,
113131 getItem : ( key : string , options ?: CookieOptions ) =>
114132 deserializeCookieOptions ( cookieStorage . _read ( options ) , key ) ,
115133 setItem : ( key : string , value : string , options ?: CookieOptions ) => {
@@ -128,17 +146,19 @@ export const cookieStorage: StorageWithOptions<CookieOptions> = addClearMethod({
128146 }
129147 } ,
130148 removeItem : ( key : string , options ?: CookieOptions ) => {
131- cookieStorage . _write ( key , "deleted" , { ...options , expires : new Date ( 0 ) } ) ;
149+ cookieStorage . _write ( key , "deleted" , { ...options , expires : new Date ( 0 ) } ) ;
132150 } ,
133151 key : ( index : number , options ?: CookieOptions ) => {
134152 let key : string | null = null ;
135153 let count = 0 ;
136- cookieStorage . _read ( options ) . replace ( / (?: ^ | ; ) \s * ( .+ ?) \s * = \s * [ ^ ; ] + / g, ( _ : string , found : string ) => {
137- if ( ! key && found && count ++ === index ) {
138- key = found ;
139- }
140- return "" ;
141- } ) ;
154+ cookieStorage
155+ . _read ( options )
156+ . replace ( / (?: ^ | ; ) \s * ( .+ ?) \s * = \s * [ ^ ; ] + / g, ( _ : string , found : string ) => {
157+ if ( ! key && found && count ++ === index ) {
158+ key = found ;
159+ }
160+ return "" ;
161+ } ) ;
142162 return key ;
143163 } ,
144164 getLength : ( options ?: CookieOptions ) => {
@@ -150,8 +170,8 @@ export const cookieStorage: StorageWithOptions<CookieOptions> = addClearMethod({
150170 return length ;
151171 } ,
152172 get length ( ) {
153- return this . getLength ( )
154- }
173+ return this . getLength ( ) ;
174+ } ,
155175} ) ;
156176
157177/**
@@ -160,7 +180,7 @@ export const cookieStorage: StorageWithOptions<CookieOptions> = addClearMethod({
160180 */
161181export const createCookieStorage = < T , O = CookieOptions , A = StorageWithOptions < CookieOptions > > (
162182 props ?: Omit < StorageProps < T , A , O > , "api" > ,
163- ) => createStorage < O , T > ( { ...props , api : cookieStorage } as any ) ;
183+ ) => createStorage < O , T > ( { ...props , api : cookieStorage } as any ) ;
164184
165185/**
166186 * creates a reactive signal, but bound to document.cookie
@@ -174,4 +194,4 @@ export const createCookieStorageSignal = <
174194 key : string ,
175195 initialValue ?: T ,
176196 props ?: Omit < StorageSignalProps < T , A , O > , "api" > ,
177- ) => createStorageSignal < T , O > ( key , initialValue , { ...props , api : cookieStorage } as any ) ;
197+ ) => createStorageSignal < T , O > ( key , initialValue , { ...props , api : cookieStorage } as any ) ;
0 commit comments