@remix-run/cookie 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -57,7 +57,7 @@ let sessionCookie = createCookie('session', {
57
57
  secrets: ['secret1'],
58
58
  })
59
59
 
60
- console.log(sessionCookie.isSigned) // true
60
+ console.log(sessionCookie.signed) // true
61
61
 
62
62
  let response = new Response('Hello, world!', {
63
63
  headers: {
@@ -86,7 +86,7 @@ let response = new Response('Hello, world!', {
86
86
 
87
87
  ### Custom Encoding
88
88
 
89
- By default, the library will use `encodeURIComponent` and `decodeURIComponent` to encode and decode the cookie value. This is suitable for most use cases, but you can provide your own functions to customize the encoding and decoding of the cookie value.
89
+ By default, [`encodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) and [`decodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) are used to encode and decode the cookie value. This is suitable for most use cases, but you can provide your own functions to customize the encoding and decoding of the cookie value.
90
90
 
91
91
  ```tsx
92
92
  let sessionCookie = createCookie('session', {
@@ -27,7 +27,7 @@ export async function unsign(cookie, secret) {
27
27
  return false;
28
28
  }
29
29
  }
30
- async function createKey(secret, usages) {
30
+ function createKey(secret, usages) {
31
31
  return crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, usages);
32
32
  }
33
33
  function byteStringToArray(byteString) {
@@ -1,21 +1,24 @@
1
1
  import { type CookieProperties } from '@remix-run/headers';
2
+ /**
3
+ * Options for creating a cookie.
4
+ */
2
5
  export interface CookieOptions extends CookieProperties {
3
6
  /**
4
- * A function that decodes the cookie value.
5
- *
6
- * Defaults to `decodeURIComponent`, which decodes any URL-encoded sequences into their original
7
- * characters.
7
+ * A function that decodes the cookie value. Decodes any URL-encoded sequences into their
8
+ * original characters.
8
9
  *
9
10
  * See [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.1) for more details.
11
+ *
12
+ * @default decodeURIComponent
10
13
  */
11
14
  decode?: (value: string) => string;
12
15
  /**
13
- * A function that encodes the cookie value.
14
- *
15
- * Defaults to `encodeURIComponent`, which percent-encodes all characters that are not allowed
16
+ * A function that encodes the cookie value. Percent-encodes all characters that are not allowed
16
17
  * in a cookie value.
17
18
  *
18
19
  * See [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.1) for more details.
20
+ *
21
+ * @default encodeURIComponent
19
22
  */
20
23
  encode?: (value: string) => string;
21
24
  /**
@@ -37,8 +40,12 @@ type SameSiteValue = 'Strict' | 'Lax' | 'None';
37
40
  * Also supports cryptographic signing of the cookie value to ensure it's not tampered with, and
38
41
  * secret rotation to easily rotate secrets without breaking existing cookies.
39
42
  */
40
- export declare class Cookie {
43
+ export declare class Cookie implements CookieProperties {
41
44
  #private;
45
+ /**
46
+ * @param name The name of the cookie
47
+ * @param options Options for the cookie
48
+ */
42
49
  constructor(name: string, options?: CookieOptions);
43
50
  /**
44
51
  * The domain of the cookie.
@@ -56,6 +63,8 @@ export declare class Cookie {
56
63
  * True if the cookie is HTTP-only.
57
64
  *
58
65
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#httponly)
66
+ *
67
+ * @default false
59
68
  */
60
69
  get httpOnly(): boolean;
61
70
  /**
@@ -72,6 +81,7 @@ export declare class Cookie {
72
81
  get name(): string;
73
82
  /**
74
83
  * Extracts the value of this cookie from a `Cookie` header value.
84
+ *
75
85
  * @param headerValue The `Cookie` header to parse
76
86
  * @returns The value of this cookie, or `null` if it's not present
77
87
  */
@@ -80,31 +90,40 @@ export declare class Cookie {
80
90
  * True if the cookie is partitioned.
81
91
  *
82
92
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#partitioned)
93
+ *
94
+ * @default false
83
95
  */
84
96
  get partitioned(): boolean;
85
97
  /**
86
- * The path of the cookie. Defaults to `/`.
98
+ * The path of the cookie.
87
99
  *
88
100
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#pathpath-value)
101
+ *
102
+ * @default '/'
89
103
  */
90
104
  get path(): string;
91
105
  /**
92
- * The `SameSite` attribute of the cookie. Defaults to `Lax`.
106
+ * The `SameSite` attribute of the cookie.
93
107
  *
94
108
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value)
109
+ *
110
+ * @default 'Lax'
95
111
  */
96
112
  get sameSite(): SameSiteValue;
97
113
  /**
98
114
  * True if the cookie is secure (only sent over HTTPS).
99
115
  *
100
116
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#secure)
117
+ *
118
+ * @default false
101
119
  */
102
120
  get secure(): boolean;
103
121
  /**
104
122
  * Returns the value to use in a `Set-Cookie` header for this cookie.
123
+ *
105
124
  * @param value The value to serialize
106
- * @param props (optional) Additional properties to use when serializing the cookie
107
- * @returns The `Set-Cookie` header for this cookie
125
+ * @param props Additional properties to use when serializing the cookie
126
+ * @returns The `Set-Cookie` header value for this cookie
108
127
  */
109
128
  serialize(value: string, props?: CookieProperties): Promise<string>;
110
129
  /**
@@ -114,9 +133,10 @@ export declare class Cookie {
114
133
  }
115
134
  /**
116
135
  * Creates a new cookie object.
136
+ *
117
137
  * @param name The name of the cookie
118
- * @param options (optional) Additional options for the cookie
119
- * @returns A cookie object
138
+ * @param options Options for the cookie
139
+ * @returns A new `Cookie` object
120
140
  */
121
141
  export declare function createCookie(name: string, options?: CookieOptions): Cookie;
122
142
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../src/lib/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,gBAAgB,EACtB,MAAM,oBAAoB,CAAA;AAI3B,MAAM,WAAW,aAAc,SAAQ,gBAAgB;IACrD;;;;;;;OAOG;IACH,MA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AI,OAAO,CAEpB;IAED;;;;;;OAMG;IACG,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,CAgBxE;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,OAAO,CAEpB;CACF;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAE1E"}
@@ -21,6 +21,10 @@ export class Cookie {
21
21
  #path;
22
22
  #sameSite;
23
23
  #secure;
24
+ /**
25
+ * @param name The name of the cookie
26
+ * @param options Options for the cookie
27
+ */
24
28
  constructor(name, options) {
25
29
  let { decode = decodeURIComponent, encode = encodeURIComponent, secrets = [], domain, expires, httpOnly, maxAge, path = '/', partitioned, secure, sameSite = 'Lax', } = options ?? {};
26
30
  if (partitioned === true) {
@@ -61,6 +65,8 @@ export class Cookie {
61
65
  * True if the cookie is HTTP-only.
62
66
  *
63
67
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#httponly)
68
+ *
69
+ * @default false
64
70
  */
65
71
  get httpOnly() {
66
72
  return this.#httpOnly ?? false;
@@ -83,6 +89,7 @@ export class Cookie {
83
89
  }
84
90
  /**
85
91
  * Extracts the value of this cookie from a `Cookie` header value.
92
+ *
86
93
  * @param headerValue The `Cookie` header to parse
87
94
  * @returns The value of this cookie, or `null` if it's not present
88
95
  */
@@ -102,22 +109,28 @@ export class Cookie {
102
109
  * True if the cookie is partitioned.
103
110
  *
104
111
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#partitioned)
112
+ *
113
+ * @default false
105
114
  */
106
115
  get partitioned() {
107
116
  return this.#partitioned ?? false;
108
117
  }
109
118
  /**
110
- * The path of the cookie. Defaults to `/`.
119
+ * The path of the cookie.
111
120
  *
112
121
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#pathpath-value)
122
+ *
123
+ * @default '/'
113
124
  */
114
125
  get path() {
115
126
  return this.#path;
116
127
  }
117
128
  /**
118
- * The `SameSite` attribute of the cookie. Defaults to `Lax`.
129
+ * The `SameSite` attribute of the cookie.
119
130
  *
120
131
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value)
132
+ *
133
+ * @default 'Lax'
121
134
  */
122
135
  get sameSite() {
123
136
  return this.#sameSite;
@@ -126,15 +139,18 @@ export class Cookie {
126
139
  * True if the cookie is secure (only sent over HTTPS).
127
140
  *
128
141
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#secure)
142
+ *
143
+ * @default false
129
144
  */
130
145
  get secure() {
131
146
  return this.#secure ?? false;
132
147
  }
133
148
  /**
134
149
  * Returns the value to use in a `Set-Cookie` header for this cookie.
150
+ *
135
151
  * @param value The value to serialize
136
- * @param props (optional) Additional properties to use when serializing the cookie
137
- * @returns The `Set-Cookie` header for this cookie
152
+ * @param props Additional properties to use when serializing the cookie
153
+ * @returns The `Set-Cookie` header value for this cookie
138
154
  */
139
155
  async serialize(value, props) {
140
156
  let header = new SetCookieHeader({
@@ -161,9 +177,10 @@ export class Cookie {
161
177
  }
162
178
  /**
163
179
  * Creates a new cookie object.
180
+ *
164
181
  * @param name The name of the cookie
165
- * @param options (optional) Additional options for the cookie
166
- * @returns A cookie object
182
+ * @param options Options for the cookie
183
+ * @returns A new `Cookie` object
167
184
  */
168
185
  export function createCookie(name, options) {
169
186
  return new Cookie(name, options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remix-run/cookie",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "A toolkit for working with cookies in JavaScript",
5
5
  "author": "Michael Jackson <mjijackson@gmail.com>",
6
6
  "license": "MIT",
@@ -26,10 +26,11 @@
26
26
  "./package.json": "./package.json"
27
27
  },
28
28
  "devDependencies": {
29
- "@types/node": "^24.6.0"
29
+ "@types/node": "^24.6.0",
30
+ "@typescript/native-preview": "7.0.0-dev.20251125.1"
30
31
  },
31
- "peerDependencies": {
32
- "@remix-run/headers": "^0.17.1"
32
+ "dependencies": {
33
+ "@remix-run/headers": "^0.19.0"
33
34
  },
34
35
  "keywords": [
35
36
  "http",
@@ -39,9 +40,9 @@
39
40
  "set-cookie"
40
41
  ],
41
42
  "scripts": {
42
- "build": "tsc -p tsconfig.build.json",
43
+ "build": "tsgo -p tsconfig.build.json",
43
44
  "clean": "git clean -fdX",
44
- "test": "node --disable-warning=ExperimentalWarning --test './src/**/*.test.ts'",
45
- "typecheck": "tsc --noEmit"
45
+ "test": "node --disable-warning=ExperimentalWarning --test",
46
+ "typecheck": "tsgo --noEmit"
46
47
  }
47
48
  }
@@ -34,7 +34,7 @@ export async function unsign(cookie: string, secret: string): Promise<string | f
34
34
  }
35
35
  }
36
36
 
37
- async function createKey(secret: string, usages: CryptoKey['usages']): Promise<CryptoKey> {
37
+ function createKey(secret: string, usages: CryptoKey['usages']): Promise<CryptoKey> {
38
38
  return crypto.subtle.importKey(
39
39
  'raw',
40
40
  encoder.encode(secret),
package/src/lib/cookie.ts CHANGED
@@ -6,23 +6,26 @@ import {
6
6
 
7
7
  import { sign, unsign } from './cookie-signing.ts'
8
8
 
9
+ /**
10
+ * Options for creating a cookie.
11
+ */
9
12
  export interface CookieOptions extends CookieProperties {
10
13
  /**
11
- * A function that decodes the cookie value.
12
- *
13
- * Defaults to `decodeURIComponent`, which decodes any URL-encoded sequences into their original
14
- * characters.
14
+ * A function that decodes the cookie value. Decodes any URL-encoded sequences into their
15
+ * original characters.
15
16
  *
16
17
  * See [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.1) for more details.
18
+ *
19
+ * @default decodeURIComponent
17
20
  */
18
21
  decode?: (value: string) => string
19
22
  /**
20
- * A function that encodes the cookie value.
21
- *
22
- * Defaults to `encodeURIComponent`, which percent-encodes all characters that are not allowed
23
+ * A function that encodes the cookie value. Percent-encodes all characters that are not allowed
23
24
  * in a cookie value.
24
25
  *
25
26
  * See [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.1) for more details.
27
+ *
28
+ * @default encodeURIComponent
26
29
  */
27
30
  encode?: (value: string) => string
28
31
  /**
@@ -47,7 +50,7 @@ type Coder = (value: string) => string
47
50
  * Also supports cryptographic signing of the cookie value to ensure it's not tampered with, and
48
51
  * secret rotation to easily rotate secrets without breaking existing cookies.
49
52
  */
50
- export class Cookie {
53
+ export class Cookie implements CookieProperties {
51
54
  #name: string
52
55
  #decode: Coder
53
56
  #encode: Coder
@@ -61,6 +64,10 @@ export class Cookie {
61
64
  #sameSite: SameSiteValue
62
65
  #secure: boolean | undefined
63
66
 
67
+ /**
68
+ * @param name The name of the cookie
69
+ * @param options Options for the cookie
70
+ */
64
71
  constructor(name: string, options?: CookieOptions) {
65
72
  let {
66
73
  decode = decodeURIComponent,
@@ -118,6 +125,8 @@ export class Cookie {
118
125
  * True if the cookie is HTTP-only.
119
126
  *
120
127
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#httponly)
128
+ *
129
+ * @default false
121
130
  */
122
131
  get httpOnly(): boolean {
123
132
  return this.#httpOnly ?? false
@@ -143,6 +152,7 @@ export class Cookie {
143
152
 
144
153
  /**
145
154
  * Extracts the value of this cookie from a `Cookie` header value.
155
+ *
146
156
  * @param headerValue The `Cookie` header to parse
147
157
  * @returns The value of this cookie, or `null` if it's not present
148
158
  */
@@ -163,24 +173,30 @@ export class Cookie {
163
173
  * True if the cookie is partitioned.
164
174
  *
165
175
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#partitioned)
176
+ *
177
+ * @default false
166
178
  */
167
179
  get partitioned(): boolean {
168
180
  return this.#partitioned ?? false
169
181
  }
170
182
 
171
183
  /**
172
- * The path of the cookie. Defaults to `/`.
184
+ * The path of the cookie.
173
185
  *
174
186
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#pathpath-value)
187
+ *
188
+ * @default '/'
175
189
  */
176
190
  get path(): string {
177
191
  return this.#path
178
192
  }
179
193
 
180
194
  /**
181
- * The `SameSite` attribute of the cookie. Defaults to `Lax`.
195
+ * The `SameSite` attribute of the cookie.
182
196
  *
183
197
  * [MDN Reference](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value)
198
+ *
199
+ * @default 'Lax'
184
200
  */
185
201
  get sameSite(): SameSiteValue {
186
202
  return this.#sameSite
@@ -190,6 +206,8 @@ export class Cookie {
190
206
  * True if the cookie is secure (only sent over HTTPS).
191
207
  *
192
208
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#secure)
209
+ *
210
+ * @default false
193
211
  */
194
212
  get secure(): boolean {
195
213
  return this.#secure ?? false
@@ -197,9 +215,10 @@ export class Cookie {
197
215
 
198
216
  /**
199
217
  * Returns the value to use in a `Set-Cookie` header for this cookie.
218
+ *
200
219
  * @param value The value to serialize
201
- * @param props (optional) Additional properties to use when serializing the cookie
202
- * @returns The `Set-Cookie` header for this cookie
220
+ * @param props Additional properties to use when serializing the cookie
221
+ * @returns The `Set-Cookie` header value for this cookie
203
222
  */
204
223
  async serialize(value: string, props?: CookieProperties): Promise<string> {
205
224
  let header = new SetCookieHeader({
@@ -229,9 +248,10 @@ export class Cookie {
229
248
 
230
249
  /**
231
250
  * Creates a new cookie object.
251
+ *
232
252
  * @param name The name of the cookie
233
- * @param options (optional) Additional options for the cookie
234
- * @returns A cookie object
253
+ * @param options Options for the cookie
254
+ * @returns A new `Cookie` object
235
255
  */
236
256
  export function createCookie(name: string, options?: CookieOptions): Cookie {
237
257
  return new Cookie(name, options)