@tschk/moonshine-next 0.4.4 → 0.4.49
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 +21 -2
- package/package.json +5 -5
- package/src/headers.ts +141 -8
package/README.md
CHANGED
|
@@ -133,8 +133,10 @@ Be aware of these before migrating — none of them are stubbed to silently
|
|
|
133
133
|
- **No file-system routing conventions.** `layout.tsx`, `loading.tsx`,
|
|
134
134
|
`error.tsx`, `template.tsx`, route groups, parallel and intercepting routes
|
|
135
135
|
are not interpreted. Use the moonshine router.
|
|
136
|
-
- **
|
|
137
|
-
|
|
136
|
+
- **Bounded cookie mutation.** `cookies().set()` and `.delete()` use the opt-in
|
|
137
|
+
request response-header scope on Bun/Node. `headers()` has no response mutation
|
|
138
|
+
API, and `draftMode()` always reports disabled. Streaming Server Actions and
|
|
139
|
+
Next's render-phase restrictions are not implemented.
|
|
138
140
|
- **Other `next/*` modules are absent**, including `next/server`
|
|
139
141
|
(`NextRequest` / `NextResponse`), `next/og`, `next/cache`, and
|
|
140
142
|
`next/navigation`'s `unstable_*` APIs. Route Handlers take and return plain
|
|
@@ -149,3 +151,20 @@ Request helpers require `requestContext: true` in Moonshine config (enabled by
|
|
|
149
151
|
Next adoption). Bun/Node deployment and preview inject the runner from the same
|
|
150
152
|
compiled bundle as the route modules; a separately imported context cannot scope
|
|
151
153
|
an already-bundled route. Edge request context is currently unsupported.
|
|
154
|
+
|
|
155
|
+
`cookies()` retains synchronous reads (`await cookies()` also works). With
|
|
156
|
+
`requestContext: true`, loaders, actions and buffered rendering can call
|
|
157
|
+
`set(name, value, options)` or `set({name, value, ...options})`. Values are
|
|
158
|
+
percent-encoded; invalid names and attributes reject before changing headers or
|
|
159
|
+
same-request reads. The default path is `/`. Each write appends a separate
|
|
160
|
+
`Set-Cookie`; later writes to the same name update the shared request-local jar.
|
|
161
|
+
Ordinary incoming cookies still read without a response scope; mutation requires
|
|
162
|
+
one. This does not install a Next runtime.
|
|
163
|
+
|
|
164
|
+
`delete(name)` expires the root-path cookie. Use `delete({name, path, domain})`
|
|
165
|
+
for another scope; the incoming Cookie header cannot reveal a cookie's original
|
|
166
|
+
path or domain. Deletion leaves an empty tombstone in same-request reads; the
|
|
167
|
+
browser applies expiration on receipt. Signed/encrypted sessions, domain ownership
|
|
168
|
+
checks, cookie prefix policies, streaming and Next cache invalidation are outside
|
|
169
|
+
this API. Standard expiry, age, security, SameSite, priority and partitioned
|
|
170
|
+
attributes are serialized; browser acceptance still follows browser policy.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tschk/moonshine-next",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.49",
|
|
4
4
|
"description": "Next.js API surface reimplemented on moonshine — drop Next, keep the imports",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"type": "module",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"./shaders": "./src/shaders.ts"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@tschk/moonshine": "^0.4.
|
|
25
|
-
"@tschk/moonshine-router": "^0.4.
|
|
26
|
-
"@tschk/moonshine-shaders": "^0.4.
|
|
27
|
-
"@tschk/moonshine-framework": "^0.4.
|
|
24
|
+
"@tschk/moonshine": "^0.4.48",
|
|
25
|
+
"@tschk/moonshine-router": "^0.4.48",
|
|
26
|
+
"@tschk/moonshine-shaders": "^0.4.48",
|
|
27
|
+
"@tschk/moonshine-framework": "^0.4.48"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"react": ">=18",
|
package/src/headers.ts
CHANGED
|
@@ -8,7 +8,10 @@
|
|
|
8
8
|
* import { headers, cookies } from "@tschk/moonshine-next/headers";
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
getRequest,
|
|
13
|
+
getResponseHeaders,
|
|
14
|
+
} from "@tschk/moonshine-framework/request";
|
|
12
15
|
export { runWithRequest } from "@tschk/moonshine-framework/request";
|
|
13
16
|
|
|
14
17
|
function current(): Request {
|
|
@@ -47,19 +50,149 @@ function parseCookies(header: string | null): Map<string, string> {
|
|
|
47
50
|
return jar;
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
export
|
|
53
|
+
export type CookieOptions = {
|
|
54
|
+
domain?: string;
|
|
55
|
+
path?: string;
|
|
56
|
+
expires?: Date;
|
|
57
|
+
maxAge?: number;
|
|
58
|
+
secure?: boolean;
|
|
59
|
+
httpOnly?: boolean;
|
|
60
|
+
sameSite?: boolean | "lax" | "strict" | "none";
|
|
61
|
+
priority?: "low" | "medium" | "high";
|
|
62
|
+
partitioned?: boolean;
|
|
63
|
+
};
|
|
64
|
+
export type CookieStore = {
|
|
51
65
|
get(name: string): CookieEntry | undefined;
|
|
52
|
-
getAll(): CookieEntry[];
|
|
66
|
+
getAll(name?: string): CookieEntry[];
|
|
53
67
|
has(name: string): boolean;
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
set(name: string, value: string, options?: CookieOptions): void;
|
|
69
|
+
set(cookie: CookieEntry & CookieOptions): void;
|
|
70
|
+
delete(
|
|
71
|
+
cookie:
|
|
72
|
+
string | ({ name: string } & Pick<CookieOptions, "path" | "domain">),
|
|
73
|
+
): void;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const responseJars = new WeakMap<Headers, Map<string, string>>();
|
|
77
|
+
function serializeCookie(
|
|
78
|
+
name: string,
|
|
79
|
+
value: string,
|
|
80
|
+
options: CookieOptions,
|
|
81
|
+
): string {
|
|
82
|
+
if (typeof name !== "string" || !/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name))
|
|
83
|
+
throw new TypeError("Invalid cookie name");
|
|
84
|
+
if (typeof value !== "string")
|
|
85
|
+
throw new TypeError("Cookie value must be a string");
|
|
86
|
+
const allowed = [
|
|
87
|
+
"domain",
|
|
88
|
+
"path",
|
|
89
|
+
"expires",
|
|
90
|
+
"maxAge",
|
|
91
|
+
"secure",
|
|
92
|
+
"httpOnly",
|
|
93
|
+
"sameSite",
|
|
94
|
+
"priority",
|
|
95
|
+
"partitioned",
|
|
96
|
+
];
|
|
97
|
+
if (
|
|
98
|
+
!options ||
|
|
99
|
+
typeof options !== "object" ||
|
|
100
|
+
Object.keys(options).some((key) => !allowed.includes(key))
|
|
101
|
+
)
|
|
102
|
+
throw new TypeError("Unsupported cookie options");
|
|
103
|
+
for (const key of ["secure", "httpOnly", "partitioned"] as const)
|
|
104
|
+
if (options[key] !== undefined && typeof options[key] !== "boolean")
|
|
105
|
+
throw new TypeError("Cookie flags must be boolean");
|
|
106
|
+
const parts = [name + "=" + encodeURIComponent(value)];
|
|
107
|
+
if (options.domain !== undefined) {
|
|
108
|
+
if (
|
|
109
|
+
typeof options.domain !== "string" ||
|
|
110
|
+
!/^\.?(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)*$/.test(
|
|
111
|
+
options.domain,
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
throw new TypeError("Invalid cookie domain");
|
|
115
|
+
parts.push("Domain=" + options.domain);
|
|
116
|
+
}
|
|
117
|
+
const path = options.path ?? "/";
|
|
118
|
+
if (typeof path !== "string" || !/^[\x20-\x3a\x3c-\x7e]*$/.test(path))
|
|
119
|
+
throw new TypeError("Invalid cookie path");
|
|
120
|
+
if (path) parts.push("Path=" + path);
|
|
121
|
+
if (options.expires !== undefined) {
|
|
122
|
+
if (
|
|
123
|
+
!(options.expires instanceof Date) ||
|
|
124
|
+
!Number.isFinite(options.expires.getTime())
|
|
125
|
+
)
|
|
126
|
+
throw new TypeError("Invalid cookie expiry");
|
|
127
|
+
parts.push("Expires=" + options.expires.toUTCString());
|
|
128
|
+
}
|
|
129
|
+
if (options.maxAge !== undefined) {
|
|
130
|
+
if (!Number.isSafeInteger(options.maxAge))
|
|
131
|
+
throw new TypeError("Invalid cookie maxAge");
|
|
132
|
+
parts.push("Max-Age=" + options.maxAge);
|
|
133
|
+
}
|
|
134
|
+
if (options.secure) parts.push("Secure");
|
|
135
|
+
if (options.httpOnly) parts.push("HttpOnly");
|
|
136
|
+
if (options.sameSite !== undefined && options.sameSite !== false) {
|
|
137
|
+
const policy = options.sameSite === true ? "strict" : options.sameSite;
|
|
138
|
+
if (!["lax", "strict", "none"].includes(policy))
|
|
139
|
+
throw new TypeError("Invalid cookie sameSite");
|
|
140
|
+
parts.push("SameSite=" + policy[0]!.toUpperCase() + policy.slice(1));
|
|
141
|
+
}
|
|
142
|
+
if (options.priority !== undefined) {
|
|
143
|
+
if (!["low", "medium", "high"].includes(options.priority))
|
|
144
|
+
throw new TypeError("Invalid cookie priority");
|
|
145
|
+
parts.push(
|
|
146
|
+
"Priority=" +
|
|
147
|
+
options.priority[0]!.toUpperCase() +
|
|
148
|
+
options.priority.slice(1),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
if (options.partitioned) parts.push("Partitioned");
|
|
152
|
+
return parts.join("; ");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function cookies(): CookieStore {
|
|
156
|
+
const request = current();
|
|
157
|
+
const sink = getResponseHeaders();
|
|
158
|
+
let jar = sink && responseJars.get(sink);
|
|
159
|
+
if (!jar) {
|
|
160
|
+
jar = parseCookies(request.headers.get("cookie"));
|
|
161
|
+
if (sink) responseJars.set(sink, jar);
|
|
162
|
+
}
|
|
163
|
+
const values = jar;
|
|
164
|
+
function set(
|
|
165
|
+
name: string | (CookieEntry & CookieOptions),
|
|
166
|
+
value?: string,
|
|
167
|
+
options: CookieOptions = {},
|
|
168
|
+
): void {
|
|
169
|
+
if (!sink)
|
|
170
|
+
throw new Error("Cookie mutation requires a response request scope");
|
|
171
|
+
if (typeof name !== "string") {
|
|
172
|
+
const { name: key, value: next, ...rest } = name;
|
|
173
|
+
set(key, next, rest);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const serialized = serializeCookie(name, value!, options);
|
|
177
|
+
sink.append("set-cookie", serialized);
|
|
178
|
+
values.set(name, value!);
|
|
179
|
+
}
|
|
56
180
|
return {
|
|
57
181
|
get: (name) => {
|
|
58
|
-
const value =
|
|
182
|
+
const value = values.get(name);
|
|
59
183
|
return value === undefined ? undefined : { name, value };
|
|
60
184
|
},
|
|
61
|
-
getAll: () =>
|
|
62
|
-
|
|
185
|
+
getAll: (filter) =>
|
|
186
|
+
[...values]
|
|
187
|
+
.filter(([key]) => filter === undefined || key === filter)
|
|
188
|
+
.map(([name, value]) => ({ name, value })),
|
|
189
|
+
has: (name) => values.has(name),
|
|
190
|
+
set,
|
|
191
|
+
delete: (cookie) => {
|
|
192
|
+
const { name, ...options } =
|
|
193
|
+
typeof cookie === "string" ? { name: cookie } : cookie;
|
|
194
|
+
set(name, "", { ...options, expires: new Date(0), maxAge: 0 });
|
|
195
|
+
},
|
|
63
196
|
};
|
|
64
197
|
}
|
|
65
198
|
|