|
1 | | -import { deflateRawSync, inflateRawSync } from "node:zlib"; |
2 | 1 | import { |
3 | 2 | __descriptor, |
4 | | - $cursor, |
5 | 3 | KIND, |
| 4 | + NotImplementedError, |
6 | 5 | OPTIONS, |
7 | 6 | type Static, |
8 | 7 | type TSchema, |
9 | 8 | } from "@alepha/core"; |
10 | | -import { DateTimeProvider, type DurationLike } from "@alepha/datetime"; |
11 | | -import type { ServerRequest } from "@alepha/server"; |
| 9 | +import type { DurationLike } from "@alepha/datetime"; |
| 10 | + |
| 11 | +const KEY = "COOKIE"; |
12 | 12 |
|
13 | 13 | export interface CookieDescriptorOptions<T extends TSchema> { |
| 14 | + /** The schema for the cookie's value, used for validation and type safety. */ |
14 | 15 | schema: T; |
15 | 16 |
|
16 | | - name: string; |
| 17 | + /** The name of the cookie. */ |
| 18 | + name?: string; |
17 | 19 |
|
18 | | - path?: string; // default: "/" |
| 20 | + /** The cookie's path. Defaults to "/". */ |
| 21 | + path?: string; |
19 | 22 |
|
20 | | - ttl?: DurationLike; // map to maxAge |
| 23 | + /** Time-to-live for the cookie. Maps to `Max-Age`. */ |
| 24 | + ttl?: DurationLike; |
21 | 25 |
|
22 | | - secure?: boolean; // TODO: "auto" - secure=true if ctx.url.protocol === "https" |
| 26 | + /** If true, the cookie is only sent over HTTPS. Defaults to true in production. */ |
| 27 | + secure?: boolean; |
23 | 28 |
|
| 29 | + /** If true, the cookie cannot be accessed by client-side scripts. */ |
24 | 30 | httpOnly?: boolean; |
25 | 31 |
|
26 | | - sameSite?: "strict" | "lax" | "none"; // default: "lax" |
| 32 | + /** SameSite policy for the cookie. Defaults to "lax". */ |
| 33 | + sameSite?: "strict" | "lax" | "none"; |
27 | 34 |
|
| 35 | + /** The domain for the cookie. */ |
28 | 36 | domain?: string; |
29 | 37 |
|
| 38 | + /** If true, the cookie value will be compressed using zlib. */ |
30 | 39 | compress?: boolean; |
31 | 40 |
|
32 | | - encrypt?: boolean; // not implemented yet |
| 41 | + /** If true, the cookie value will be encrypted. Requires `COOKIE_SECRET` env var. */ |
| 42 | + encrypt?: boolean; |
33 | 43 |
|
34 | | - sign?: boolean; // not implemented yet |
| 44 | + /** If true, the cookie will be signed to prevent tampering. Requires `COOKIE_SECRET` env var. */ |
| 45 | + sign?: boolean; |
35 | 46 | } |
36 | 47 |
|
37 | 48 | export interface CookieDescriptor<T extends TSchema> { |
38 | | - [KIND]: "COOKIE"; |
39 | | - |
| 49 | + [KIND]: typeof KEY; |
40 | 50 | [OPTIONS]: CookieDescriptorOptions<T>; |
41 | 51 |
|
| 52 | + schema: T; |
| 53 | + |
| 54 | + /** Sets the cookie with the given value in the current request's response. */ |
42 | 55 | set: (value: Static<T>, options?: { cookies?: Cookies }) => void; |
43 | 56 |
|
| 57 | + /** Gets the cookie value from the current request. Returns undefined if not found or invalid. */ |
44 | 58 | get: (options?: { cookies?: Cookies }) => Static<T> | undefined; |
45 | 59 |
|
| 60 | + /** Deletes the cookie in the current request's response. */ |
46 | 61 | del: (options?: { cookies?: Cookies }) => void; |
47 | 62 | } |
48 | 63 |
|
| 64 | +/** |
| 65 | + * Declares a type-safe, configurable HTTP cookie. |
| 66 | + * This descriptor provides methods to get, set, and delete the cookie |
| 67 | + * within the server request/response cycle. |
| 68 | + */ |
49 | 69 | export const $cookie: { |
50 | 70 | <T extends TSchema>(options: CookieDescriptorOptions<T>): CookieDescriptor<T>; |
51 | 71 | [KIND]: string; |
52 | 72 | } = <T extends TSchema>( |
53 | 73 | options: CookieDescriptorOptions<T>, |
54 | 74 | ): CookieDescriptor<T> => { |
55 | | - __descriptor("COOKIE"); |
| 75 | + __descriptor(KEY); |
56 | 76 |
|
57 | | - const { context: alepha } = $cursor(); |
58 | | - |
59 | | - return { |
60 | | - [KIND]: "COOKIE", |
| 77 | + const api: Partial<CookieDescriptor<T>> = { |
| 78 | + [KIND]: KEY, |
61 | 79 | [OPTIONS]: options, |
62 | | - get: (opts: { cookies?: Cookies } = {}) => { |
63 | | - const cookies = |
64 | | - alepha.context.get<ServerRequest>("request")?.cookies ?? opts.cookies; |
65 | | - if (!cookies) { |
66 | | - throw new Error( |
67 | | - "Cookies not found in request context or options.cookies", |
68 | | - ); |
69 | | - } |
70 | | - |
71 | | - try { |
72 | | - if (cookies.req[options.name]) { |
73 | | - let value: string = decodeURIComponent(cookies.req[options.name]); |
74 | | - |
75 | | - if (options.compress) { |
76 | | - value = inflateRawSync(Buffer.from(value, "base64")).toString( |
77 | | - "utf8", |
78 | | - ); |
79 | | - } |
80 | | - |
81 | | - return alepha.parse(options.schema, JSON.parse(value)); |
82 | | - } |
83 | | - } catch (e) { |
84 | | - alepha.log.error(e); |
85 | | - cookies.res[options.name] = null; |
86 | | - } |
87 | | - |
88 | | - return undefined; |
| 80 | + schema: options.schema, |
| 81 | + set: () => { |
| 82 | + throw new NotImplementedError(KEY); |
89 | 83 | }, |
90 | | - |
91 | | - del: (opts: { cookies?: Cookies } = {}) => { |
92 | | - const cookies = |
93 | | - alepha.context.get<ServerRequest>("request")?.cookies ?? opts.cookies; |
94 | | - if (!cookies) { |
95 | | - throw new Error( |
96 | | - "Cookies not found in request context or options.cookies", |
97 | | - ); |
98 | | - } |
99 | | - |
100 | | - cookies.res[options.name] = null; |
| 84 | + get: () => { |
| 85 | + throw new NotImplementedError(KEY); |
101 | 86 | }, |
102 | | - |
103 | | - set: (data: Static<T>, opts: { cookies?: Cookies } = {}) => { |
104 | | - const cookies = |
105 | | - alepha.context.get<ServerRequest>("request")?.cookies ?? opts.cookies; |
106 | | - if (!cookies) { |
107 | | - throw new Error( |
108 | | - "Cookies not found in request context or options.cookies", |
109 | | - ); |
110 | | - } |
111 | | - |
112 | | - let value = JSON.stringify(alepha.parse(options.schema, data)); |
113 | | - |
114 | | - if (options.compress) { |
115 | | - value = deflateRawSync(value).toString("base64"); |
116 | | - } |
117 | | - |
118 | | - value = encodeURIComponent(value); |
119 | | - |
120 | | - const cookie: Cookie = { |
121 | | - value, |
122 | | - path: options.path ?? "/", |
123 | | - sameSite: options.sameSite ?? "lax", |
124 | | - secure: options.secure, |
125 | | - httpOnly: options.httpOnly, |
126 | | - domain: options.domain, |
127 | | - }; |
128 | | - |
129 | | - if (options.ttl) { |
130 | | - const dt = alepha.get(DateTimeProvider); |
131 | | - cookie.maxAge = dt.duration(options.ttl).as("seconds"); |
132 | | - } |
133 | | - |
134 | | - cookies.res[options.name] = cookie; |
| 87 | + del: () => { |
| 88 | + throw new NotImplementedError(KEY); |
135 | 89 | }, |
136 | 90 | }; |
| 91 | + |
| 92 | + return api as CookieDescriptor<T>; |
137 | 93 | }; |
138 | 94 |
|
139 | | -$cookie[KIND] = "COOKIE"; |
| 95 | +$cookie[KIND] = KEY; |
| 96 | + |
| 97 | +// --------------------------------------------------------------------------------------------------------------------- |
140 | 98 |
|
141 | 99 | export interface Cookies { |
142 | 100 | req: Record<string, string>; |
|
0 commit comments