@fougere/state-cookie 0.0.0-stage → 0.26.0-alpha.0
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/LICENSE +21 -0
- package/README.md +15 -2
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -3
- package/src/index.ts +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fougere contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fougere/state-cookie
|
|
2
|
+
> A caller's state, kept in a cookie
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
An op reads and writes the caller's state by name (`state: State`); `state:` in `fougere.config.ts`
|
|
5
|
+
says how each name is kept. Importing this package makes `cookie` one of the ways:
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import '@fougere/state-cookie';
|
|
9
|
+
|
|
10
|
+
export default defineFougere({
|
|
11
|
+
state: { cart: { cookie: { maxAge: 604800 } } }, // HttpOnly, SameSite=Lax, Path=/ unless said otherwise
|
|
12
|
+
});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Core signs every value before it leaves (`FOUGERE_STATE_KEY`): the caller can read its cookie, and a
|
|
16
|
+
changed one reads as absent. Every HTTP door carries it — REST, GraphQL, and the envelope a page calls.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Format, type Accepted } from '@fougere/schema';
|
|
2
|
+
/** What `state: { cart: { cookie: { … } } }` may say — what a `Set-Cookie` line says, by the same names. */
|
|
3
|
+
declare const COOKIE: Format<{
|
|
4
|
+
domain?: string | undefined;
|
|
5
|
+
httpOnly?: boolean | undefined;
|
|
6
|
+
maxAge?: number | undefined;
|
|
7
|
+
path?: string | undefined;
|
|
8
|
+
sameSite?: "lax" | "none" | "strict" | undefined;
|
|
9
|
+
secure?: boolean | undefined;
|
|
10
|
+
}>;
|
|
11
|
+
export type CookieOptions = Accepted<typeof COOKIE>;
|
|
12
|
+
declare module '@fougere/core' {
|
|
13
|
+
interface FougereStates {
|
|
14
|
+
cookie: CookieOptions;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,KAAK,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAExD,4GAA4G;AAC5G,QAAA,MAAM,MAAM;;;;;;;EAOD,CAAC;AAEZ,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC,OAAO,MAAM,CAAC,CAAC;AAEpD,OAAO,QAAQ,eAAe,CAAC;IAC7B,UAAU,aAAa;QACrB,MAAM,EAAE,aAAa,CAAC;KACvB;CACF"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** `cookie` — a caller's state kept in a cookie, registered at import. */
|
|
2
|
+
import { States } from '@fougere/core';
|
|
3
|
+
import { Format } from '@fougere/schema';
|
|
4
|
+
/** What `state: { cart: { cookie: { … } } }` may say — what a `Set-Cookie` line says, by the same names. */
|
|
5
|
+
const COOKIE = Format.of('state/cookie')
|
|
6
|
+
.key('maxAge', Format.count)
|
|
7
|
+
.key('path', Format.text)
|
|
8
|
+
.key('domain', Format.text)
|
|
9
|
+
.key('sameSite', Format.tokens(['lax', 'strict', 'none']))
|
|
10
|
+
.key('secure', Format.flag)
|
|
11
|
+
.key('httpOnly', Format.flag)
|
|
12
|
+
.closed();
|
|
13
|
+
/** The values a `Cookie` header carries, by name. */
|
|
14
|
+
function cookiesOf(header) {
|
|
15
|
+
return new Map((header ?? '').split(';').flatMap((pair) => {
|
|
16
|
+
const at = pair.indexOf('=');
|
|
17
|
+
return at <= 0 ? [] : [[pair.slice(0, at).trim(), pair.slice(at + 1).trim()]];
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* One `Set-Cookie` line. A script on the page may not read it and a request from another site does
|
|
22
|
+
* not carry it, unless the entry says otherwise; with no `maxAge` it lasts as long as the browser.
|
|
23
|
+
*/
|
|
24
|
+
function lineOf(name, token, options) {
|
|
25
|
+
const { path = '/', sameSite = 'lax', httpOnly = true, secure, domain, maxAge } = options;
|
|
26
|
+
return [
|
|
27
|
+
`${name}=${token ?? ''}`,
|
|
28
|
+
`Path=${path}`,
|
|
29
|
+
`SameSite=${sameSite[0].toUpperCase()}${sameSite.slice(1)}`,
|
|
30
|
+
...(httpOnly ? ['HttpOnly'] : []),
|
|
31
|
+
...(secure || sameSite === 'none' ? ['Secure'] : []),
|
|
32
|
+
...(domain ? [`Domain=${domain}`] : []),
|
|
33
|
+
...(token === undefined ? ['Max-Age=0'] : maxAge !== undefined ? [`Max-Age=${maxAge}`] : []),
|
|
34
|
+
].join('; ');
|
|
35
|
+
}
|
|
36
|
+
States.register('cookie', {
|
|
37
|
+
over: 'http',
|
|
38
|
+
format: COOKIE,
|
|
39
|
+
read: (carrier, names) => {
|
|
40
|
+
const cookies = cookiesOf(carrier.request.get('cookie'));
|
|
41
|
+
return new Map(names.flatMap(({ name }) => (cookies.has(name) ? [[name, cookies.get(name)]] : [])));
|
|
42
|
+
},
|
|
43
|
+
write: (carrier, written) => {
|
|
44
|
+
for (const { name, token, options } of written) {
|
|
45
|
+
carrier.response.append('set-cookie', lineOf(name, token, options));
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEvC,OAAO,EAAE,MAAM,EAAiB,MAAM,iBAAiB,CAAC;AAExD,4GAA4G;AAC5G,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC,cAAc,CAAC;KACrC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,KAAK,CAAC;KAC3B,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC;KACxB,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC;KAC1B,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAU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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fougere/state-cookie",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.26.0-alpha.0",
|
|
4
|
+
"description": "A caller's state kept in a cookie — what `state: { cart: { cookie: {} } }` names, signed by core, carried by every HTTP door.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"fougere",
|
|
7
|
+
"typescript",
|
|
8
|
+
"state",
|
|
9
|
+
"cookie"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/chok/fougere.git",
|
|
15
|
+
"directory": "packages/state/cookie"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./dist/index.js"
|
|
20
|
+
],
|
|
21
|
+
"main": "dist/index.js",
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"src"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@fougere/core": "0.26.0-alpha.0",
|
|
36
|
+
"@fougere/http": "0.26.0-alpha.0",
|
|
37
|
+
"@fougere/schema": "0.26.0-alpha.0"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "rm -rf dist && tsc",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"test:watch": "vitest",
|
|
46
|
+
"typecheck": "tsc --noEmit -p tsconfig.test.json"
|
|
47
|
+
}
|
|
6
48
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** `cookie` — a caller's state kept in a cookie, registered at import. */
|
|
2
|
+
import { States } from '@fougere/core';
|
|
3
|
+
import type { HttpCarrier } from '@fougere/http';
|
|
4
|
+
import { Format, type Accepted } from '@fougere/schema';
|
|
5
|
+
|
|
6
|
+
/** What `state: { cart: { cookie: { … } } }` may say — what a `Set-Cookie` line says, by the same names. */
|
|
7
|
+
const COOKIE = Format.of('state/cookie')
|
|
8
|
+
.key('maxAge', Format.count)
|
|
9
|
+
.key('path', Format.text)
|
|
10
|
+
.key('domain', Format.text)
|
|
11
|
+
.key('sameSite', Format.tokens(['lax', 'strict', 'none'] as const))
|
|
12
|
+
.key('secure', Format.flag)
|
|
13
|
+
.key('httpOnly', Format.flag)
|
|
14
|
+
.closed();
|
|
15
|
+
|
|
16
|
+
export type CookieOptions = Accepted<typeof COOKIE>;
|
|
17
|
+
|
|
18
|
+
declare module '@fougere/core' {
|
|
19
|
+
interface FougereStates {
|
|
20
|
+
cookie: CookieOptions;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The values a `Cookie` header carries, by name. */
|
|
25
|
+
function cookiesOf(header: string | null): Map<string, string> {
|
|
26
|
+
return new Map((header ?? '').split(';').flatMap((pair) => {
|
|
27
|
+
const at = pair.indexOf('=');
|
|
28
|
+
|
|
29
|
+
return at <= 0 ? [] : [[pair.slice(0, at).trim(), pair.slice(at + 1).trim()] as const];
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* One `Set-Cookie` line. A script on the page may not read it and a request from another site does
|
|
35
|
+
* not carry it, unless the entry says otherwise; with no `maxAge` it lasts as long as the browser.
|
|
36
|
+
*/
|
|
37
|
+
function lineOf(name: string, token: string | undefined, options: CookieOptions): string {
|
|
38
|
+
const { path = '/', sameSite = 'lax', httpOnly = true, secure, domain, maxAge } = options;
|
|
39
|
+
|
|
40
|
+
return [
|
|
41
|
+
`${name}=${token ?? ''}`,
|
|
42
|
+
`Path=${path}`,
|
|
43
|
+
`SameSite=${sameSite[0]!.toUpperCase()}${sameSite.slice(1)}`,
|
|
44
|
+
...(httpOnly ? ['HttpOnly'] : []),
|
|
45
|
+
...(secure || sameSite === 'none' ? ['Secure'] : []),
|
|
46
|
+
...(domain ? [`Domain=${domain}`] : []),
|
|
47
|
+
...(token === undefined ? ['Max-Age=0'] : maxAge !== undefined ? [`Max-Age=${maxAge}`] : []),
|
|
48
|
+
].join('; ');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
States.register('cookie', {
|
|
52
|
+
over: 'http',
|
|
53
|
+
format: COOKIE,
|
|
54
|
+
read: (carrier, names) => {
|
|
55
|
+
const cookies = cookiesOf((carrier as HttpCarrier).request.get('cookie'));
|
|
56
|
+
|
|
57
|
+
return new Map(names.flatMap(({ name }) => (cookies.has(name) ? [[name, cookies.get(name)!] as const] : [])));
|
|
58
|
+
},
|
|
59
|
+
write: (carrier, written) => {
|
|
60
|
+
for (const { name, token, options } of written) {
|
|
61
|
+
(carrier as HttpCarrier).response.append('set-cookie', lineOf(name, token, options as CookieOptions));
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
});
|