@decocms/start 2.25.0 → 2.27.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/.agents/skills/deco-to-tanstack-migration/SKILL.md +1 -0
- package/.agents/skills/deco-to-tanstack-migration/references/platform-hooks-factories.md +84 -2
- package/.agents/skills/deco-to-tanstack-migration/references/post-migration-cleanup.md +1 -0
- package/MIGRATION_TOOLING_PLAN.md +149 -5
- package/package.json +7 -1
- package/scripts/migrate/post-cleanup/rules.ts +26 -0
- package/scripts/migrate/post-cleanup/runner.test.ts +44 -0
- package/src/sdk/cn.test.ts +34 -0
- package/src/sdk/cn.ts +28 -0
- package/src/sdk/cookie.test.ts +108 -0
- package/src/sdk/cookie.ts +90 -0
- package/src/sdk/encoding.test.ts +71 -0
- package/src/sdk/encoding.ts +47 -0
- package/src/sdk/http.test.ts +71 -0
- package/src/sdk/http.ts +124 -0
- package/src/sdk/useScript.test.ts +77 -2
- package/src/sdk/useScript.ts +48 -8
- package/src/sdk/useSuggestions.test.ts +230 -0
- package/src/sdk/useSuggestions.ts +188 -0
package/src/sdk/cookie.ts
CHANGED
|
@@ -37,3 +37,93 @@ export function decodeCookie(cookieValue: string): any {
|
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Server-side cookie helpers — Web-platform / Workers-friendly.
|
|
43
|
+
//
|
|
44
|
+
// These mirror the surface area of Deno's `@std/http/cookie`, which deco
|
|
45
|
+
// storefronts depended on heavily before TanStack/Workers migration. Sites
|
|
46
|
+
// can now import from "@decocms/start/sdk/cookie" instead of shipping a
|
|
47
|
+
// per-site shim or pulling JSR.
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
export interface Cookie {
|
|
51
|
+
name: string;
|
|
52
|
+
value: string;
|
|
53
|
+
expires?: Date | number;
|
|
54
|
+
maxAge?: number;
|
|
55
|
+
domain?: string;
|
|
56
|
+
path?: string;
|
|
57
|
+
secure?: boolean;
|
|
58
|
+
httpOnly?: boolean;
|
|
59
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Parse all cookies from a Request's `Cookie` header into a plain object.
|
|
64
|
+
* Returns `{}` when no cookies are present. Values are URL-decoded.
|
|
65
|
+
*
|
|
66
|
+
* Equivalent to `getCookies(req.headers)` from `@std/http/cookie`.
|
|
67
|
+
*/
|
|
68
|
+
export function getCookies(headers: Headers): Record<string, string> {
|
|
69
|
+
const cookie = headers.get("cookie");
|
|
70
|
+
if (!cookie) return {};
|
|
71
|
+
const out: Record<string, string> = {};
|
|
72
|
+
for (const pair of cookie.split(/;\s*/)) {
|
|
73
|
+
const eq = pair.indexOf("=");
|
|
74
|
+
if (eq === -1) continue;
|
|
75
|
+
const name = pair.slice(0, eq).trim();
|
|
76
|
+
if (!name) continue;
|
|
77
|
+
const value = pair.slice(eq + 1).trim();
|
|
78
|
+
try {
|
|
79
|
+
out[name] = decodeURIComponent(value);
|
|
80
|
+
} catch {
|
|
81
|
+
out[name] = value;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Serialize a cookie spec and append a `Set-Cookie` header to a Response's
|
|
89
|
+
* `Headers`. Equivalent to `setCookie(headers, cookie)` from `@std/http/cookie`.
|
|
90
|
+
*
|
|
91
|
+
* Note: this uses `headers.append`, not `set`, so multiple cookies stack
|
|
92
|
+
* correctly (a single `Set-Cookie` header cannot represent multiple cookies).
|
|
93
|
+
*/
|
|
94
|
+
export function setResponseCookie(headers: Headers, cookie: Cookie): void {
|
|
95
|
+
const parts = [`${cookie.name}=${cookie.value}`];
|
|
96
|
+
if (cookie.expires !== undefined) {
|
|
97
|
+
const date = cookie.expires instanceof Date
|
|
98
|
+
? cookie.expires
|
|
99
|
+
: new Date(cookie.expires);
|
|
100
|
+
parts.push(`Expires=${date.toUTCString()}`);
|
|
101
|
+
}
|
|
102
|
+
if (cookie.maxAge !== undefined) parts.push(`Max-Age=${cookie.maxAge}`);
|
|
103
|
+
if (cookie.domain) parts.push(`Domain=${cookie.domain}`);
|
|
104
|
+
if (cookie.path) parts.push(`Path=${cookie.path}`);
|
|
105
|
+
if (cookie.secure) parts.push("Secure");
|
|
106
|
+
if (cookie.httpOnly) parts.push("HttpOnly");
|
|
107
|
+
if (cookie.sameSite) parts.push(`SameSite=${cookie.sameSite}`);
|
|
108
|
+
headers.append("Set-Cookie", parts.join("; "));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Append a delete instruction (`Max-Age=0` + epoch `Expires`) for a cookie.
|
|
113
|
+
* `path` and `domain` should match the original `setResponseCookie` call to
|
|
114
|
+
* actually clear the cookie in the browser.
|
|
115
|
+
*/
|
|
116
|
+
export function deleteResponseCookie(
|
|
117
|
+
headers: Headers,
|
|
118
|
+
name: string,
|
|
119
|
+
attributes: { path?: string; domain?: string } = {},
|
|
120
|
+
): void {
|
|
121
|
+
setResponseCookie(headers, {
|
|
122
|
+
name,
|
|
123
|
+
value: "",
|
|
124
|
+
expires: new Date(0),
|
|
125
|
+
maxAge: 0,
|
|
126
|
+
path: attributes.path,
|
|
127
|
+
domain: attributes.domain,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
decodeBase64,
|
|
4
|
+
decodeBase64Url,
|
|
5
|
+
encodeBase64,
|
|
6
|
+
encodeBase64Url,
|
|
7
|
+
} from "./encoding";
|
|
8
|
+
|
|
9
|
+
describe("encodeBase64 / decodeBase64", () => {
|
|
10
|
+
it("round-trips ASCII strings", () => {
|
|
11
|
+
const data = "hello, world";
|
|
12
|
+
const b64 = encodeBase64(data);
|
|
13
|
+
expect(b64).toBe("aGVsbG8sIHdvcmxk");
|
|
14
|
+
const decoded = new TextDecoder().decode(decodeBase64(b64));
|
|
15
|
+
expect(decoded).toBe(data);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("round-trips multi-byte UTF-8", () => {
|
|
19
|
+
const data = "São Paulo — café ☕";
|
|
20
|
+
const b64 = encodeBase64(data);
|
|
21
|
+
const decoded = new TextDecoder().decode(decodeBase64(b64));
|
|
22
|
+
expect(decoded).toBe(data);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("accepts Uint8Array input", () => {
|
|
26
|
+
const bytes = new Uint8Array([1, 2, 3, 4, 5]);
|
|
27
|
+
const b64 = encodeBase64(bytes);
|
|
28
|
+
expect(decodeBase64(b64)).toEqual(bytes);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("accepts ArrayBuffer input", () => {
|
|
32
|
+
const buf = new Uint8Array([255, 254, 253]).buffer;
|
|
33
|
+
const b64 = encodeBase64(buf);
|
|
34
|
+
expect(Array.from(decodeBase64(b64))).toEqual([255, 254, 253]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("handles inputs larger than the chunking window", () => {
|
|
38
|
+
// 0x8000 + 7 bytes — forces the chunking branch.
|
|
39
|
+
const bytes = new Uint8Array(0x8000 + 7);
|
|
40
|
+
for (let i = 0; i < bytes.length; i++) bytes[i] = i & 0xff;
|
|
41
|
+
const b64 = encodeBase64(bytes);
|
|
42
|
+
expect(decodeBase64(b64)).toEqual(bytes);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("encodeBase64Url / decodeBase64Url", () => {
|
|
47
|
+
it("uses URL-safe alphabet and strips padding", () => {
|
|
48
|
+
// Inputs that produce '+', '/', and padding under standard base64.
|
|
49
|
+
const bytes = new Uint8Array([251, 255, 191, 251, 239, 254]);
|
|
50
|
+
const standard = encodeBase64(bytes);
|
|
51
|
+
const url = encodeBase64Url(bytes);
|
|
52
|
+
// Every '+' becomes '-', '/' becomes '_', trailing '=' is stripped.
|
|
53
|
+
expect(url).toBe(
|
|
54
|
+
standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""),
|
|
55
|
+
);
|
|
56
|
+
expect(url).not.toContain("+");
|
|
57
|
+
expect(url).not.toContain("/");
|
|
58
|
+
expect(url).not.toContain("=");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("round-trips through the URL-safe pair", () => {
|
|
62
|
+
const data = new Uint8Array(64);
|
|
63
|
+
for (let i = 0; i < data.length; i++) data[i] = (i * 7) & 0xff;
|
|
64
|
+
expect(decodeBase64Url(encodeBase64Url(data))).toEqual(data);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("decodes inputs missing their padding", () => {
|
|
68
|
+
// 'a' = 0x61. encodeBase64('a') = 'YQ==', URL form drops to 'YQ'.
|
|
69
|
+
expect(new TextDecoder().decode(decodeBase64Url("YQ"))).toBe("a");
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-platform base64 / base64url helpers.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the surface area of Deno's `@std/encoding/base64` so deco
|
|
5
|
+
* storefronts on TanStack/Workers can drop the per-site shim.
|
|
6
|
+
*
|
|
7
|
+
* All implementations use the global `btoa` / `atob` (available in Workers,
|
|
8
|
+
* browsers, and Node 16+) so there is zero runtime dependency.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function toBytes(data: ArrayBuffer | Uint8Array | string): Uint8Array {
|
|
12
|
+
if (typeof data === "string") return new TextEncoder().encode(data);
|
|
13
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
14
|
+
return data;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function encodeBase64(data: ArrayBuffer | Uint8Array | string): string {
|
|
18
|
+
const bytes = toBytes(data);
|
|
19
|
+
// Build the binary string in chunks to avoid blowing the call stack on
|
|
20
|
+
// large inputs (`String.fromCharCode(...bytes)` spreads the entire array).
|
|
21
|
+
let bin = "";
|
|
22
|
+
const CHUNK = 0x8000;
|
|
23
|
+
for (let i = 0; i < bytes.byteLength; i += CHUNK) {
|
|
24
|
+
bin += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
|
|
25
|
+
}
|
|
26
|
+
return btoa(bin);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function decodeBase64(b64: string): Uint8Array {
|
|
30
|
+
const bin = atob(b64);
|
|
31
|
+
const out = new Uint8Array(bin.length);
|
|
32
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function encodeBase64Url(data: ArrayBuffer | Uint8Array | string): string {
|
|
37
|
+
return encodeBase64(data)
|
|
38
|
+
.replace(/\+/g, "-")
|
|
39
|
+
.replace(/\//g, "_")
|
|
40
|
+
.replace(/=+$/, "");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function decodeBase64Url(b64url: string): Uint8Array {
|
|
44
|
+
const padded = b64url.replace(/-/g, "+").replace(/_/g, "/");
|
|
45
|
+
const padLen = (4 - (padded.length % 4)) % 4;
|
|
46
|
+
return decodeBase64(padded + "=".repeat(padLen));
|
|
47
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { HttpError, STATUS_CODE, UserAgent } from "./http";
|
|
3
|
+
|
|
4
|
+
describe("STATUS_CODE", () => {
|
|
5
|
+
it("exposes common codes with the IANA-canonical names", () => {
|
|
6
|
+
expect(STATUS_CODE.OK).toBe(200);
|
|
7
|
+
expect(STATUS_CODE.MovedPermanently).toBe(301);
|
|
8
|
+
expect(STATUS_CODE.NotFound).toBe(404);
|
|
9
|
+
expect(STATUS_CODE.TooManyRequests).toBe(429);
|
|
10
|
+
expect(STATUS_CODE.InternalServerError).toBe(500);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("is readonly at the type level", () => {
|
|
14
|
+
// @ts-expect-error — assigning into the const map should not type-check.
|
|
15
|
+
STATUS_CODE.OK = 999;
|
|
16
|
+
// Even though the assignment is allowed at runtime (frozen-by-convention),
|
|
17
|
+
// we just want the type to flag it. The value is whatever JS allows.
|
|
18
|
+
expect(typeof STATUS_CODE.OK).toBe("number");
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe("UserAgent", () => {
|
|
23
|
+
it("accepts a string and exposes it via toString", () => {
|
|
24
|
+
const ua = new UserAgent("Mozilla/5.0 (test)");
|
|
25
|
+
expect(ua.toString()).toBe("Mozilla/5.0 (test)");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("treats null as an empty UA string", () => {
|
|
29
|
+
const ua = new UserAgent(null);
|
|
30
|
+
expect(ua.toString()).toBe("");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("exposes empty browser/os/cpu/device/engine accessors", () => {
|
|
34
|
+
const ua = new UserAgent("anything");
|
|
35
|
+
expect(ua.browser).toEqual({});
|
|
36
|
+
expect(ua.os).toEqual({});
|
|
37
|
+
expect(ua.cpu).toEqual({});
|
|
38
|
+
expect(ua.device).toEqual({});
|
|
39
|
+
expect(ua.engine).toEqual({});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("HttpError", () => {
|
|
44
|
+
it("captures status and message", () => {
|
|
45
|
+
const err = new HttpError(404, "Missing");
|
|
46
|
+
expect(err).toBeInstanceOf(Error);
|
|
47
|
+
expect(err.name).toBe("HttpError");
|
|
48
|
+
expect(err.status).toBe(404);
|
|
49
|
+
expect(err.message).toBe("Missing");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("defaults the message from the status when not provided", () => {
|
|
53
|
+
const err = new HttpError(503);
|
|
54
|
+
expect(err.message).toBe("HTTP 503");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("preserves an optional body payload for downstream handling", () => {
|
|
58
|
+
const body = { code: "rate_limited" };
|
|
59
|
+
const err = new HttpError(429, "Too Many Requests", body);
|
|
60
|
+
expect(err.body).toEqual(body);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("supports `instanceof` discrimination", () => {
|
|
64
|
+
const err = new HttpError(304);
|
|
65
|
+
function isNotModified(e: unknown): e is HttpError {
|
|
66
|
+
return e instanceof HttpError && e.status === 304;
|
|
67
|
+
}
|
|
68
|
+
expect(isNotModified(err)).toBe(true);
|
|
69
|
+
expect(isNotModified(new Error("nope"))).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
});
|
package/src/sdk/http.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP constants and small typed helpers — replaces the parts of Deno's
|
|
3
|
+
* `@std/http` (other than cookies, which live in `./cookie.ts`) that deco
|
|
4
|
+
* storefronts touch.
|
|
5
|
+
*
|
|
6
|
+
* Currently exposes:
|
|
7
|
+
* - `STATUS_CODE` — full IANA status-code map (parity with @std/http).
|
|
8
|
+
* - `UserAgent` — minimal class with the same shape; does not parse
|
|
9
|
+
* the UA string (sites only used `.toString()` and
|
|
10
|
+
* basic browser/os accessors in dev). Replace with a
|
|
11
|
+
* real parser like `ua-parser-js` if you actually
|
|
12
|
+
* depend on the parsed fields.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const STATUS_CODE = {
|
|
16
|
+
Continue: 100,
|
|
17
|
+
SwitchingProtocols: 101,
|
|
18
|
+
Processing: 102,
|
|
19
|
+
EarlyHints: 103,
|
|
20
|
+
OK: 200,
|
|
21
|
+
Created: 201,
|
|
22
|
+
Accepted: 202,
|
|
23
|
+
NonAuthoritativeInfo: 203,
|
|
24
|
+
NoContent: 204,
|
|
25
|
+
ResetContent: 205,
|
|
26
|
+
PartialContent: 206,
|
|
27
|
+
MultiStatus: 207,
|
|
28
|
+
AlreadyReported: 208,
|
|
29
|
+
IMUsed: 226,
|
|
30
|
+
MultipleChoices: 300,
|
|
31
|
+
MovedPermanently: 301,
|
|
32
|
+
Found: 302,
|
|
33
|
+
SeeOther: 303,
|
|
34
|
+
NotModified: 304,
|
|
35
|
+
UseProxy: 305,
|
|
36
|
+
TemporaryRedirect: 307,
|
|
37
|
+
PermanentRedirect: 308,
|
|
38
|
+
BadRequest: 400,
|
|
39
|
+
Unauthorized: 401,
|
|
40
|
+
PaymentRequired: 402,
|
|
41
|
+
Forbidden: 403,
|
|
42
|
+
NotFound: 404,
|
|
43
|
+
MethodNotAllowed: 405,
|
|
44
|
+
NotAcceptable: 406,
|
|
45
|
+
ProxyAuthRequired: 407,
|
|
46
|
+
RequestTimeout: 408,
|
|
47
|
+
Conflict: 409,
|
|
48
|
+
Gone: 410,
|
|
49
|
+
LengthRequired: 411,
|
|
50
|
+
PreconditionFailed: 412,
|
|
51
|
+
ContentTooLarge: 413,
|
|
52
|
+
URITooLong: 414,
|
|
53
|
+
UnsupportedMediaType: 415,
|
|
54
|
+
RangeNotSatisfiable: 416,
|
|
55
|
+
ExpectationFailed: 417,
|
|
56
|
+
Teapot: 418,
|
|
57
|
+
MisdirectedRequest: 421,
|
|
58
|
+
UnprocessableEntity: 422,
|
|
59
|
+
Locked: 423,
|
|
60
|
+
FailedDependency: 424,
|
|
61
|
+
TooEarly: 425,
|
|
62
|
+
UpgradeRequired: 426,
|
|
63
|
+
PreconditionRequired: 428,
|
|
64
|
+
TooManyRequests: 429,
|
|
65
|
+
RequestHeaderFieldsTooLarge: 431,
|
|
66
|
+
UnavailableForLegalReasons: 451,
|
|
67
|
+
InternalServerError: 500,
|
|
68
|
+
NotImplemented: 501,
|
|
69
|
+
BadGateway: 502,
|
|
70
|
+
ServiceUnavailable: 503,
|
|
71
|
+
GatewayTimeout: 504,
|
|
72
|
+
HTTPVersionNotSupported: 505,
|
|
73
|
+
VariantAlsoNegotiates: 506,
|
|
74
|
+
InsufficientStorage: 507,
|
|
75
|
+
LoopDetected: 508,
|
|
76
|
+
NotExtended: 510,
|
|
77
|
+
NetworkAuthenticationRequired: 511,
|
|
78
|
+
} as const;
|
|
79
|
+
|
|
80
|
+
export type StatusCode = typeof STATUS_CODE[keyof typeof STATUS_CODE];
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Minimal stand-in for Deno's `@std/http`'s `UserAgent`. Captures the raw
|
|
84
|
+
* UA string and exposes the same field shape; does NOT parse. Replace with
|
|
85
|
+
* a real parser when you start depending on parsed fields.
|
|
86
|
+
*/
|
|
87
|
+
export class UserAgent {
|
|
88
|
+
ua: string;
|
|
89
|
+
browser: { name?: string; version?: string };
|
|
90
|
+
os: { name?: string; version?: string };
|
|
91
|
+
device: { vendor?: string; model?: string; type?: string };
|
|
92
|
+
cpu: { architecture?: string };
|
|
93
|
+
engine: { name?: string; version?: string };
|
|
94
|
+
|
|
95
|
+
constructor(ua: string | null) {
|
|
96
|
+
this.ua = ua ?? "";
|
|
97
|
+
this.browser = {};
|
|
98
|
+
this.os = {};
|
|
99
|
+
this.device = {};
|
|
100
|
+
this.cpu = {};
|
|
101
|
+
this.engine = {};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
toString(): string {
|
|
105
|
+
return this.ua;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Lightweight HTTP error class. Drop-in for the `HttpError` shape that
|
|
111
|
+
* `deco-cx/apps` exposes — sites use it as `error instanceof HttpError &&
|
|
112
|
+
* error.status === 304` and similar.
|
|
113
|
+
*/
|
|
114
|
+
export class HttpError extends Error {
|
|
115
|
+
status: number;
|
|
116
|
+
body?: unknown;
|
|
117
|
+
|
|
118
|
+
constructor(status: number, message?: string, body?: unknown) {
|
|
119
|
+
super(message ?? `HTTP ${status}`);
|
|
120
|
+
this.name = "HttpError";
|
|
121
|
+
this.status = status;
|
|
122
|
+
this.body = body;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
HTMX_LEGACY_URL,
|
|
4
|
+
inlineScript,
|
|
5
|
+
usePartialSection,
|
|
6
|
+
useScript,
|
|
7
|
+
useSection,
|
|
8
|
+
} from "./useScript";
|
|
3
9
|
|
|
4
10
|
describe("inlineScript", () => {
|
|
5
11
|
it("returns dangerouslySetInnerHTML with the provided string", () => {
|
|
@@ -51,3 +57,72 @@ describe("useScript", () => {
|
|
|
51
57
|
expect(result).toMatch(/^\(.*\)\(\)$/);
|
|
52
58
|
});
|
|
53
59
|
});
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// Legacy HTMX stubs — useSection / usePartialSection
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
describe("useSection / usePartialSection (legacy HTMX stubs)", () => {
|
|
66
|
+
let warnSpy: ReturnType<typeof vi.spyOn>;
|
|
67
|
+
let prevNodeEnv: string | undefined;
|
|
68
|
+
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
// Reset the dedup set so each test sees a fresh warning fire.
|
|
71
|
+
delete (globalThis as any).__DECO_LEGACY_HTMX_WARNED;
|
|
72
|
+
prevNodeEnv = process.env.NODE_ENV;
|
|
73
|
+
process.env.NODE_ENV = "development";
|
|
74
|
+
warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
afterEach(() => {
|
|
78
|
+
warnSpy.mockRestore();
|
|
79
|
+
if (prevNodeEnv === undefined) delete process.env.NODE_ENV;
|
|
80
|
+
else process.env.NODE_ENV = prevNodeEnv;
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("useSection returns a stable placeholder URL instead of throwing", () => {
|
|
84
|
+
expect(useSection()).toBe(HTMX_LEGACY_URL);
|
|
85
|
+
expect(useSection({ props: { x: 1 } })).toBe(HTMX_LEGACY_URL);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("usePartialSection returns the same placeholder URL", () => {
|
|
89
|
+
expect(usePartialSection()).toBe(HTMX_LEGACY_URL);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("warns on first call (in development)", () => {
|
|
93
|
+
useSection();
|
|
94
|
+
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
95
|
+
expect(warnSpy.mock.calls[0][0]).toMatch(/useSection/);
|
|
96
|
+
expect(warnSpy.mock.calls[0][0]).toMatch(/were removed/);
|
|
97
|
+
expect(warnSpy.mock.calls[0][0]).toMatch(/htmx-residue/);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("dedups warnings: a second call to the same stub is silent", () => {
|
|
101
|
+
useSection();
|
|
102
|
+
useSection();
|
|
103
|
+
useSection();
|
|
104
|
+
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("tracks warnings per-stub: useSection and usePartialSection warn independently", () => {
|
|
108
|
+
useSection();
|
|
109
|
+
usePartialSection();
|
|
110
|
+
expect(warnSpy).toHaveBeenCalledTimes(2);
|
|
111
|
+
const messages = warnSpy.mock.calls.map((c) => c[0] as string);
|
|
112
|
+
expect(messages.some((m) => m.includes("useSection"))).toBe(true);
|
|
113
|
+
expect(messages.some((m) => m.includes("usePartialSection"))).toBe(true);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("does NOT warn in production", () => {
|
|
117
|
+
process.env.NODE_ENV = "production";
|
|
118
|
+
useSection();
|
|
119
|
+
usePartialSection();
|
|
120
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("returns the placeholder URL even in production (still safe to embed)", () => {
|
|
124
|
+
process.env.NODE_ENV = "production";
|
|
125
|
+
expect(useSection()).toBe(HTMX_LEGACY_URL);
|
|
126
|
+
expect(usePartialSection()).toBe(HTMX_LEGACY_URL);
|
|
127
|
+
});
|
|
128
|
+
});
|
package/src/sdk/useScript.ts
CHANGED
|
@@ -150,21 +150,61 @@ export function inlineScript(js: string) {
|
|
|
150
150
|
* See: deco-to-tanstack-migration skill, "useComponent / partial sections"
|
|
151
151
|
* section, for the per-pattern recipes.
|
|
152
152
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
153
|
+
* ## SSR-safe stub behavior (since 2.27)
|
|
154
|
+
*
|
|
155
|
+
* Earlier versions threw on call. That broke SSR for the *entire page* if a
|
|
156
|
+
* single section still imported `useSection` — even sections that the user
|
|
157
|
+
* never interacts with (e.g. legacy login form on the homepage). React's
|
|
158
|
+
* error boundary would catch the throw and degrade the whole route to
|
|
159
|
+
* client rendering.
|
|
160
|
+
*
|
|
161
|
+
* The current behavior:
|
|
162
|
+
* - returns a stable placeholder URL (`HTMX_LEGACY_URL`) so it can be
|
|
163
|
+
* embedded in `hx-get` / `hx-post` attributes without crashing the
|
|
164
|
+
* SSR pass,
|
|
165
|
+
* - logs a deduped `console.warn` (in dev only) per call site so the
|
|
166
|
+
* migration signal stays loud,
|
|
167
|
+
* - the `htmx-residue` audit rule still catalogues every call site for
|
|
168
|
+
* systematic rewrite.
|
|
169
|
+
*
|
|
170
|
+
* This is a deliberate trade-off: SSR success > strict-throw enforcement.
|
|
171
|
+
* Audit + skill docs do the enforcement instead.
|
|
156
172
|
*/
|
|
157
173
|
const DEPRECATION_MESSAGE =
|
|
158
174
|
"[@decocms/start] useSection / usePartialSection were removed. " +
|
|
159
175
|
"The Fresh/Deno HTMX partial-section pattern does not apply on " +
|
|
160
176
|
"TanStack Start / Cloudflare Workers. Replace call-sites with " +
|
|
161
177
|
"createServerFn + useMutation, or local React state. See the " +
|
|
162
|
-
"deco-to-tanstack-migration skill for per-pattern recipes."
|
|
178
|
+
"deco-to-tanstack-migration skill for per-pattern recipes. " +
|
|
179
|
+
"Run `deco-post-cleanup` and look for rule [9] htmx-residue to find " +
|
|
180
|
+
"every site call-site that still depends on this.";
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Stable placeholder URL returned by the legacy `useSection` /
|
|
184
|
+
* `usePartialSection` stubs. Hitting this URL surfaces a clear error.
|
|
185
|
+
* Exported so frameworks/tests can match against it.
|
|
186
|
+
*/
|
|
187
|
+
export const HTMX_LEGACY_URL = "/__deco_legacy_htmx_section__";
|
|
188
|
+
|
|
189
|
+
function warnLegacyHtmx(name: string) {
|
|
190
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (typeof (globalThis as any).__DECO_LEGACY_HTMX_WARNED === "undefined") {
|
|
194
|
+
(globalThis as any).__DECO_LEGACY_HTMX_WARNED = new Set<string>();
|
|
195
|
+
}
|
|
196
|
+
const set = (globalThis as any).__DECO_LEGACY_HTMX_WARNED as Set<string>;
|
|
197
|
+
if (set.has(name)) return;
|
|
198
|
+
set.add(name);
|
|
199
|
+
console.warn(`[${name}] ${DEPRECATION_MESSAGE}`);
|
|
200
|
+
}
|
|
163
201
|
|
|
164
|
-
export function usePartialSection(_props?: Record<string, unknown>):
|
|
165
|
-
|
|
202
|
+
export function usePartialSection(_props?: Record<string, unknown>): string {
|
|
203
|
+
warnLegacyHtmx("usePartialSection");
|
|
204
|
+
return HTMX_LEGACY_URL;
|
|
166
205
|
}
|
|
167
206
|
|
|
168
|
-
export function useSection(_props?: Record<string, unknown>):
|
|
169
|
-
|
|
207
|
+
export function useSection(_props?: Record<string, unknown>): string {
|
|
208
|
+
warnLegacyHtmx("useSection");
|
|
209
|
+
return HTMX_LEGACY_URL;
|
|
170
210
|
}
|