@rangojs/router 0.0.0-experimental.51 → 0.0.0-experimental.52
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/dist/vite/index.js +17 -2
- package/package.json +1 -1
- package/src/context-var.ts +72 -2
package/dist/vite/index.js
CHANGED
|
@@ -1745,7 +1745,7 @@ import { resolve } from "node:path";
|
|
|
1745
1745
|
// package.json
|
|
1746
1746
|
var package_default = {
|
|
1747
1747
|
name: "@rangojs/router",
|
|
1748
|
-
version: "0.0.0-experimental.
|
|
1748
|
+
version: "0.0.0-experimental.52",
|
|
1749
1749
|
description: "Django-inspired RSC router with composable URL patterns",
|
|
1750
1750
|
keywords: [
|
|
1751
1751
|
"react",
|
|
@@ -3274,8 +3274,17 @@ function jsonParseExpression(value) {
|
|
|
3274
3274
|
}
|
|
3275
3275
|
|
|
3276
3276
|
// src/context-var.ts
|
|
3277
|
+
var NON_CACHEABLE_KEYS = /* @__PURE__ */ Symbol.for(
|
|
3278
|
+
"rango:non-cacheable-keys"
|
|
3279
|
+
);
|
|
3280
|
+
function getNonCacheableKeys(variables) {
|
|
3281
|
+
if (!variables[NON_CACHEABLE_KEYS]) {
|
|
3282
|
+
variables[NON_CACHEABLE_KEYS] = /* @__PURE__ */ new Set();
|
|
3283
|
+
}
|
|
3284
|
+
return variables[NON_CACHEABLE_KEYS];
|
|
3285
|
+
}
|
|
3277
3286
|
var FORBIDDEN_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
3278
|
-
function contextSet(variables, keyOrVar, value) {
|
|
3287
|
+
function contextSet(variables, keyOrVar, value, options) {
|
|
3279
3288
|
if (typeof keyOrVar === "string") {
|
|
3280
3289
|
if (FORBIDDEN_KEYS.has(keyOrVar)) {
|
|
3281
3290
|
throw new Error(
|
|
@@ -3283,8 +3292,14 @@ function contextSet(variables, keyOrVar, value) {
|
|
|
3283
3292
|
);
|
|
3284
3293
|
}
|
|
3285
3294
|
variables[keyOrVar] = value;
|
|
3295
|
+
if (options?.cache === false) {
|
|
3296
|
+
getNonCacheableKeys(variables).add(keyOrVar);
|
|
3297
|
+
}
|
|
3286
3298
|
} else {
|
|
3287
3299
|
variables[keyOrVar.key] = value;
|
|
3300
|
+
if (options?.cache === false) {
|
|
3301
|
+
getNonCacheableKeys(variables).add(keyOrVar.key);
|
|
3302
|
+
}
|
|
3288
3303
|
}
|
|
3289
3304
|
}
|
|
3290
3305
|
|
package/package.json
CHANGED
package/src/context-var.ts
CHANGED
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
* interface PaginationData { current: number; total: number }
|
|
13
13
|
* export const Pagination = createVar<PaginationData>();
|
|
14
14
|
*
|
|
15
|
+
* // Non-cacheable var — throws if set/get inside cache() or "use cache"
|
|
16
|
+
* export const User = createVar<UserData>({ cache: false });
|
|
17
|
+
*
|
|
15
18
|
* // handler
|
|
16
19
|
* ctx.set(Pagination, { current: 1, total: 4 });
|
|
17
20
|
*
|
|
@@ -23,18 +26,36 @@
|
|
|
23
26
|
export interface ContextVar<T> {
|
|
24
27
|
readonly __brand: "context-var";
|
|
25
28
|
readonly key: symbol;
|
|
29
|
+
/** When false, the var is non-cacheable — throws inside cache() / "use cache" */
|
|
30
|
+
readonly cache: boolean;
|
|
26
31
|
/** Phantom field to carry the type parameter. Never set at runtime. */
|
|
27
32
|
readonly __type?: T;
|
|
28
33
|
}
|
|
29
34
|
|
|
35
|
+
export interface ContextVarOptions {
|
|
36
|
+
/**
|
|
37
|
+
* When false, marks this variable as non-cacheable.
|
|
38
|
+
* Setting or getting this var inside a cache() boundary or "use cache"
|
|
39
|
+
* function will throw. Use for inherently request-specific data (user
|
|
40
|
+
* sessions, auth tokens, etc.) that must never be baked into cached segments.
|
|
41
|
+
*
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
cache?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
30
47
|
/**
|
|
31
48
|
* Create a typed context variable token.
|
|
32
49
|
*
|
|
33
50
|
* The returned object is used with ctx.set(token, value) and ctx.get(token)
|
|
34
51
|
* for compile-time-checked data flow between handlers, layouts, and middleware.
|
|
35
52
|
*/
|
|
36
|
-
export function createVar<T>(): ContextVar<T> {
|
|
37
|
-
return {
|
|
53
|
+
export function createVar<T>(options?: ContextVarOptions): ContextVar<T> {
|
|
54
|
+
return {
|
|
55
|
+
__brand: "context-var" as const,
|
|
56
|
+
key: Symbol(),
|
|
57
|
+
cache: options?.cache !== false,
|
|
58
|
+
};
|
|
38
59
|
}
|
|
39
60
|
|
|
40
61
|
/**
|
|
@@ -49,6 +70,36 @@ export function isContextVar(value: unknown): value is ContextVar<unknown> {
|
|
|
49
70
|
);
|
|
50
71
|
}
|
|
51
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Symbol used as a Set stored on the variables object to track
|
|
75
|
+
* which keys hold non-cacheable values (from write-level { cache: false }).
|
|
76
|
+
*/
|
|
77
|
+
const NON_CACHEABLE_KEYS: unique symbol = Symbol.for(
|
|
78
|
+
"rango:non-cacheable-keys",
|
|
79
|
+
) as any;
|
|
80
|
+
|
|
81
|
+
function getNonCacheableKeys(variables: any): Set<string | symbol> {
|
|
82
|
+
if (!variables[NON_CACHEABLE_KEYS]) {
|
|
83
|
+
variables[NON_CACHEABLE_KEYS] = new Set();
|
|
84
|
+
}
|
|
85
|
+
return variables[NON_CACHEABLE_KEYS];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check if a variable value is non-cacheable (either var-level or write-level).
|
|
90
|
+
*/
|
|
91
|
+
export function isNonCacheable(
|
|
92
|
+
variables: any,
|
|
93
|
+
keyOrVar: string | ContextVar<any>,
|
|
94
|
+
): boolean {
|
|
95
|
+
if (typeof keyOrVar !== "string" && !keyOrVar.cache) {
|
|
96
|
+
return true; // var-level policy
|
|
97
|
+
}
|
|
98
|
+
const key = typeof keyOrVar === "string" ? keyOrVar : keyOrVar.key;
|
|
99
|
+
const set = variables[NON_CACHEABLE_KEYS] as Set<string | symbol> | undefined;
|
|
100
|
+
return set?.has(key) ?? false; // write-level policy
|
|
101
|
+
}
|
|
102
|
+
|
|
52
103
|
/**
|
|
53
104
|
* Read a variable from the variables store.
|
|
54
105
|
* Accepts either a string key (legacy) or a ContextVar token (typed).
|
|
@@ -64,6 +115,17 @@ export function contextGet(
|
|
|
64
115
|
/** Keys that must never be used as string variable names */
|
|
65
116
|
const FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
66
117
|
|
|
118
|
+
export interface ContextSetOptions {
|
|
119
|
+
/**
|
|
120
|
+
* When false, marks this specific write as non-cacheable.
|
|
121
|
+
* "Least cacheable wins" — if either the var definition or this option
|
|
122
|
+
* says cache: false, the value is non-cacheable.
|
|
123
|
+
*
|
|
124
|
+
* @default true (inherits from createVar)
|
|
125
|
+
*/
|
|
126
|
+
cache?: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
67
129
|
/**
|
|
68
130
|
* Write a variable to the variables store.
|
|
69
131
|
* Accepts either a string key (legacy) or a ContextVar token (typed).
|
|
@@ -72,6 +134,7 @@ export function contextSet(
|
|
|
72
134
|
variables: any,
|
|
73
135
|
keyOrVar: string | ContextVar<any>,
|
|
74
136
|
value: any,
|
|
137
|
+
options?: ContextSetOptions,
|
|
75
138
|
): void {
|
|
76
139
|
if (typeof keyOrVar === "string") {
|
|
77
140
|
if (FORBIDDEN_KEYS.has(keyOrVar)) {
|
|
@@ -80,7 +143,14 @@ export function contextSet(
|
|
|
80
143
|
);
|
|
81
144
|
}
|
|
82
145
|
variables[keyOrVar] = value;
|
|
146
|
+
if (options?.cache === false) {
|
|
147
|
+
getNonCacheableKeys(variables).add(keyOrVar);
|
|
148
|
+
}
|
|
83
149
|
} else {
|
|
84
150
|
variables[keyOrVar.key] = value;
|
|
151
|
+
// Track write-level non-cacheable (var-level is checked via keyOrVar.cache)
|
|
152
|
+
if (options?.cache === false) {
|
|
153
|
+
getNonCacheableKeys(variables).add(keyOrVar.key);
|
|
154
|
+
}
|
|
85
155
|
}
|
|
86
156
|
}
|