@@ -10,91 +10,19 @@ import {
1010} from "../../shared/utils.js" ;
1111
1212/**
13- * @returns {Record<string,string> }
14- */
15- function parseCookies ( ) {
16- /** @type {Record<string, string> } */
17- const out = { } ;
18- if ( ! document . cookie ) return out ;
19-
20- const parts = document . cookie . split ( "; " ) ;
21- for ( const part of parts ) {
22- const eq = part . indexOf ( "=" ) ;
23- if ( eq === - 1 ) continue ; // skip malformed cookie
24- const key = decodeURIComponent ( part . substring ( 0 , eq ) ) ;
25- const val = decodeURIComponent ( part . substring ( eq + 1 ) ) ;
26- out [ key ] = val ;
27- }
28- return out ;
29- }
30-
31- /** Utility: stringify options */
32- /**
33- *
34- * @param {ng.CookieOptions } opts
35- * @returns {string }
13+ * Service provider that creates a {@link ng.CookieService $cookie} service.
14+ * @type {ng.ServiceProvider }
3615 */
37- /**
38- * Build cookie options string from an options object.
39- * Safely validates types for path, domain, expires, secure, and samesite.
40- *
41- * @param {ng.CookieOptions } opts
42- * @returns {string }
43- * @throws {TypeError } if any of options are invalid
44- */
45- function buildOptions ( opts = { } ) {
46- const parts = [ ] ;
47-
48- // Path
49- if ( isDefined ( opts . path ) ) {
50- if ( ! isString ( opts . path ) ) throw new TypeError ( `badarg:path ${ opts . path } ` ) ;
51- parts . push ( `path=${ opts . path } ` ) ;
52- }
53-
54- // Domain
55- if ( isDefined ( opts . domain ) ) {
56- if ( ! isString ( opts . domain ) )
57- throw new TypeError ( `badarg:domain ${ opts . domain } ` ) ;
58- parts . push ( `domain=${ opts . domain } ` ) ;
59- }
60-
61- // Expires
62- if ( opts . expires != null ) {
63- let expDate ;
64-
65- if ( opts . expires instanceof Date ) {
66- expDate = opts . expires ;
67- } else if ( isNumber ( opts . expires ) || isString ( opts . expires ) ) {
68- expDate = new Date ( opts . expires ) ;
69- } else {
70- throw new TypeError ( `badarg:expires ${ String ( opts . expires ) } ` ) ;
71- }
72-
73- if ( isNaN ( expDate . getTime ( ) ) ) {
74- throw new TypeError ( `badarg:expires ${ String ( opts . expires ) } ` ) ;
75- }
76-
77- parts . push ( `expires=${ expDate . toUTCString ( ) } ` ) ;
78- }
79-
80- // Secure
81- if ( opts . secure ) {
82- parts . push ( "secure" ) ;
83- }
84-
85- // SameSite
86- if ( isDefined ( opts . samesite ) ) {
87- if ( ! isString ( opts . samesite ) )
88- throw new TypeError ( `badarg:samesite ${ opts . samesite } ` ) ;
89- const s = opts . samesite . toLowerCase ( ) ;
90- if ( ! [ "lax" , "strict" , "none" ] . includes ( s ) ) {
91- throw new TypeError ( `badarg:samesite ${ opts . samesite } ` ) ;
92- }
93- parts . push ( `samesite=${ s } ` ) ;
16+ export class CookieProvider {
17+ constructor ( ) {
18+ this . defaults = { } ;
9419 }
9520
96- // Join all parts with semicolons
97- return parts . length ? ";" + parts . join ( ";" ) : "" ;
21+ $get = [
22+ $injectTokens . $exceptionHandler ,
23+ /** @param {ng.ExceptionHandlerService } $exceptionHandler */
24+ ( $exceptionHandler ) => new CookieService ( this . defaults , $exceptionHandler ) ,
25+ ] ;
9826}
9927
10028/**
@@ -221,14 +149,86 @@ export class CookieService {
221149 }
222150}
223151
224- export class CookieProvider {
225- constructor ( ) {
226- this . defaults = { } ;
152+ /*----------Helpers----------*/
153+
154+ /**
155+ * @returns {Record<string,string> }
156+ */
157+ function parseCookies ( ) {
158+ /** @type {Record<string, string> } */
159+ const out = { } ;
160+ if ( ! document . cookie ) return out ;
161+
162+ const parts = document . cookie . split ( "; " ) ;
163+ for ( const part of parts ) {
164+ const eq = part . indexOf ( "=" ) ;
165+ if ( eq === - 1 ) continue ; // skip malformed cookie
166+ const key = decodeURIComponent ( part . substring ( 0 , eq ) ) ;
167+ const val = decodeURIComponent ( part . substring ( eq + 1 ) ) ;
168+ out [ key ] = val ;
169+ }
170+ return out ;
171+ }
172+
173+ /**
174+ * Build cookie options string from an options object.
175+ * Safely validates types for path, domain, expires, secure, and samesite.
176+ *
177+ * @param {ng.CookieOptions } opts
178+ * @returns {string }
179+ * @throws {TypeError } if any of options are invalid
180+ */
181+ function buildOptions ( opts = { } ) {
182+ const parts = [ ] ;
183+
184+ // Path
185+ if ( isDefined ( opts . path ) ) {
186+ if ( ! isString ( opts . path ) ) throw new TypeError ( `badarg:path ${ opts . path } ` ) ;
187+ parts . push ( `path=${ opts . path } ` ) ;
227188 }
228189
229- $get = [
230- $injectTokens . $exceptionHandler ,
231- /** @param {ng.ExceptionHandlerService } $exceptionHandler */
232- ( $exceptionHandler ) => new CookieService ( this . defaults , $exceptionHandler ) ,
233- ] ;
190+ // Domain
191+ if ( isDefined ( opts . domain ) ) {
192+ if ( ! isString ( opts . domain ) )
193+ throw new TypeError ( `badarg:domain ${ opts . domain } ` ) ;
194+ parts . push ( `domain=${ opts . domain } ` ) ;
195+ }
196+
197+ // Expires
198+ if ( opts . expires != null ) {
199+ let expDate ;
200+
201+ if ( opts . expires instanceof Date ) {
202+ expDate = opts . expires ;
203+ } else if ( isNumber ( opts . expires ) || isString ( opts . expires ) ) {
204+ expDate = new Date ( opts . expires ) ;
205+ } else {
206+ throw new TypeError ( `badarg:expires ${ String ( opts . expires ) } ` ) ;
207+ }
208+
209+ if ( isNaN ( expDate . getTime ( ) ) ) {
210+ throw new TypeError ( `badarg:expires ${ String ( opts . expires ) } ` ) ;
211+ }
212+
213+ parts . push ( `expires=${ expDate . toUTCString ( ) } ` ) ;
214+ }
215+
216+ // Secure
217+ if ( opts . secure ) {
218+ parts . push ( "secure" ) ;
219+ }
220+
221+ // SameSite
222+ if ( isDefined ( opts . samesite ) ) {
223+ if ( ! isString ( opts . samesite ) )
224+ throw new TypeError ( `badarg:samesite ${ opts . samesite } ` ) ;
225+ const s = opts . samesite . toLowerCase ( ) ;
226+ if ( ! [ "lax" , "strict" , "none" ] . includes ( s ) ) {
227+ throw new TypeError ( `badarg:samesite ${ opts . samesite } ` ) ;
228+ }
229+ parts . push ( `samesite=${ s } ` ) ;
230+ }
231+
232+ // Join all parts with semicolons
233+ return parts . length ? ";" + parts . join ( ";" ) : "" ;
234234}
0 commit comments