pulse-framework 0.1.53__py3-none-any.whl → 0.1.55__py3-none-any.whl
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.
- pulse/__init__.py +3 -3
- pulse/app.py +34 -20
- pulse/code_analysis.py +38 -0
- pulse/codegen/codegen.py +18 -50
- pulse/codegen/templates/route.py +100 -56
- pulse/component.py +24 -6
- pulse/components/for_.py +17 -2
- pulse/cookies.py +38 -2
- pulse/env.py +4 -4
- pulse/hooks/init.py +174 -14
- pulse/hooks/state.py +105 -0
- pulse/js/__init__.py +12 -9
- pulse/js/obj.py +79 -0
- pulse/js/pulse.py +112 -0
- pulse/js/react.py +457 -0
- pulse/messages.py +13 -13
- pulse/proxy.py +18 -5
- pulse/render_session.py +282 -266
- pulse/renderer.py +36 -73
- pulse/serializer.py +5 -2
- pulse/transpiler/__init__.py +13 -0
- pulse/transpiler/assets.py +66 -0
- pulse/transpiler/builtins.py +0 -20
- pulse/transpiler/dynamic_import.py +131 -0
- pulse/transpiler/emit_context.py +49 -0
- pulse/transpiler/errors.py +29 -11
- pulse/transpiler/function.py +36 -5
- pulse/transpiler/imports.py +33 -27
- pulse/transpiler/js_module.py +73 -20
- pulse/transpiler/modules/pulse/tags.py +35 -15
- pulse/transpiler/nodes.py +121 -36
- pulse/transpiler/py_module.py +1 -1
- pulse/transpiler/react_component.py +4 -11
- pulse/transpiler/transpiler.py +32 -26
- pulse/user_session.py +10 -0
- pulse_framework-0.1.55.dist-info/METADATA +196 -0
- {pulse_framework-0.1.53.dist-info → pulse_framework-0.1.55.dist-info}/RECORD +39 -32
- pulse/hooks/states.py +0 -285
- pulse_framework-0.1.53.dist-info/METADATA +0 -18
- {pulse_framework-0.1.53.dist-info → pulse_framework-0.1.55.dist-info}/WHEEL +0 -0
- {pulse_framework-0.1.53.dist-info → pulse_framework-0.1.55.dist-info}/entry_points.txt +0 -0
pulse/js/react.py
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
"""
|
|
2
|
+
JavaScript React module.
|
|
3
|
+
|
|
4
|
+
Usage:
|
|
5
|
+
from pulse.js.react import useState, useEffect, useRef
|
|
6
|
+
state, setState = useState(0) # -> const [state, setState] = useState(0)
|
|
7
|
+
useEffect(lambda: print("hi"), []) # -> useEffect(() => console.log("hi"), [])
|
|
8
|
+
ref = useRef(None) # -> const ref = useRef(null)
|
|
9
|
+
|
|
10
|
+
# Also available as namespace:
|
|
11
|
+
import pulse.js.react as React
|
|
12
|
+
React.useState(0) # -> React.useState(0)
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import ast as _ast
|
|
16
|
+
from collections.abc import Callable as _Callable
|
|
17
|
+
from typing import TYPE_CHECKING as _TYPE_CHECKING
|
|
18
|
+
from typing import Any as _Any
|
|
19
|
+
from typing import Protocol as _Protocol
|
|
20
|
+
from typing import TypeVar as _TypeVar
|
|
21
|
+
from typing import override as _override
|
|
22
|
+
|
|
23
|
+
from pulse.component import component as _component
|
|
24
|
+
from pulse.transpiler import Import as _Import
|
|
25
|
+
from pulse.transpiler.errors import TranspileError as _TranspileError
|
|
26
|
+
from pulse.transpiler.function import Constant as _Constant
|
|
27
|
+
from pulse.transpiler.js_module import JsModule
|
|
28
|
+
from pulse.transpiler.nodes import Call as _Call
|
|
29
|
+
from pulse.transpiler.nodes import Expr as _Expr
|
|
30
|
+
from pulse.transpiler.nodes import Jsx as _Jsx
|
|
31
|
+
from pulse.transpiler.nodes import Node as _PulseNode
|
|
32
|
+
from pulse.transpiler.vdom import VDOMNode as _VDOMNode
|
|
33
|
+
|
|
34
|
+
if _TYPE_CHECKING:
|
|
35
|
+
from pulse.transpiler.transpiler import Transpiler as _Transpiler
|
|
36
|
+
|
|
37
|
+
# Type variables for hooks
|
|
38
|
+
T = _TypeVar("T")
|
|
39
|
+
T_co = _TypeVar("T_co", covariant=True)
|
|
40
|
+
T_contra = _TypeVar("T_contra", contravariant=True)
|
|
41
|
+
S = _TypeVar("S")
|
|
42
|
+
A = _TypeVar("A")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# =============================================================================
|
|
46
|
+
# React Types
|
|
47
|
+
# =============================================================================
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class RefObject(_Protocol[T_co]):
|
|
51
|
+
"""Type for useRef return value."""
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def current(self) -> T_co: ...
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class MutableRefObject(_Protocol[T]):
|
|
58
|
+
"""Type for useRef return value with mutable current."""
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def current(self) -> T: ...
|
|
62
|
+
|
|
63
|
+
@current.setter
|
|
64
|
+
def current(self, value: T) -> None: ...
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class Dispatch(_Protocol[T_contra]):
|
|
68
|
+
"""Type for setState/dispatch functions."""
|
|
69
|
+
|
|
70
|
+
def __call__(self, action: T_contra, /) -> None: ...
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class TransitionStartFunction(_Protocol):
|
|
74
|
+
"""Type for startTransition callback."""
|
|
75
|
+
|
|
76
|
+
def __call__(self, callback: _Callable[[], None], /) -> None: ...
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class Context(_Protocol[T_co]):
|
|
80
|
+
"""Type for React Context."""
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def Provider(self) -> _Any: ...
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def Consumer(self) -> _Any: ...
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class ReactNode(_Protocol):
|
|
90
|
+
"""Type for React children."""
|
|
91
|
+
|
|
92
|
+
...
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class ReactElement(_Protocol):
|
|
96
|
+
"""Type for React element."""
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def type(self) -> _Any: ...
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def props(self) -> _Any: ...
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def key(self) -> str | None: ...
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# =============================================================================
|
|
109
|
+
# State Hooks
|
|
110
|
+
# =============================================================================
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def useState(
|
|
114
|
+
initial_state: S | _Callable[[], S],
|
|
115
|
+
) -> tuple[S, Dispatch[S | _Callable[[S], S]]]:
|
|
116
|
+
"""Returns a stateful value and a function to update it.
|
|
117
|
+
|
|
118
|
+
Example:
|
|
119
|
+
count, set_count = useState(0)
|
|
120
|
+
set_count(count + 1)
|
|
121
|
+
set_count(lambda prev: prev + 1)
|
|
122
|
+
"""
|
|
123
|
+
...
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def useReducer(
|
|
127
|
+
reducer: _Callable[[S, A], S],
|
|
128
|
+
initial_arg: S,
|
|
129
|
+
init: _Callable[[S], S] | None = None,
|
|
130
|
+
) -> tuple[S, Dispatch[A]]:
|
|
131
|
+
"""An alternative to useState for complex state logic.
|
|
132
|
+
|
|
133
|
+
Example:
|
|
134
|
+
def reducer(state, action):
|
|
135
|
+
if action['type'] == 'increment':
|
|
136
|
+
return {'count': state['count'] + 1}
|
|
137
|
+
return state
|
|
138
|
+
|
|
139
|
+
state, dispatch = useReducer(reducer, {'count': 0})
|
|
140
|
+
dispatch({'type': 'increment'})
|
|
141
|
+
"""
|
|
142
|
+
...
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
# =============================================================================
|
|
146
|
+
# Effect Hooks
|
|
147
|
+
# =============================================================================
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def useEffect(
|
|
151
|
+
effect: _Callable[[], None | _Callable[[], None]],
|
|
152
|
+
deps: list[_Any] | None = None,
|
|
153
|
+
) -> None:
|
|
154
|
+
"""Accepts a function that contains imperative, possibly effectful code.
|
|
155
|
+
|
|
156
|
+
Example:
|
|
157
|
+
useEffect(lambda: print("mounted"), [])
|
|
158
|
+
useEffect(lambda: (print("update"), lambda: print("cleanup"))[-1], [dep])
|
|
159
|
+
"""
|
|
160
|
+
...
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def useLayoutEffect(
|
|
164
|
+
effect: _Callable[[], None | _Callable[[], None]],
|
|
165
|
+
deps: list[_Any] | None = None,
|
|
166
|
+
) -> None:
|
|
167
|
+
"""Like useEffect, but fires synchronously after all DOM mutations.
|
|
168
|
+
|
|
169
|
+
Example:
|
|
170
|
+
useLayoutEffect(lambda: measure_element(), [])
|
|
171
|
+
"""
|
|
172
|
+
...
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def useInsertionEffect(
|
|
176
|
+
effect: _Callable[[], None | _Callable[[], None]],
|
|
177
|
+
deps: list[_Any] | None = None,
|
|
178
|
+
) -> None:
|
|
179
|
+
"""Like useLayoutEffect, but fires before any DOM mutations.
|
|
180
|
+
Use for CSS-in-JS libraries.
|
|
181
|
+
"""
|
|
182
|
+
...
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
# =============================================================================
|
|
186
|
+
# Ref Hooks
|
|
187
|
+
# =============================================================================
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def useRef(initial_value: T) -> MutableRefObject[T]:
|
|
191
|
+
"""Returns a mutable ref object.
|
|
192
|
+
|
|
193
|
+
Example:
|
|
194
|
+
input_ref = useRef(None)
|
|
195
|
+
# In JSX: <input ref={input_ref} />
|
|
196
|
+
input_ref.current.focus()
|
|
197
|
+
"""
|
|
198
|
+
...
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def useImperativeHandle(
|
|
202
|
+
ref: RefObject[T] | _Callable[[T | None], None] | None,
|
|
203
|
+
create_handle: _Callable[[], T],
|
|
204
|
+
deps: list[_Any] | None = None,
|
|
205
|
+
) -> None:
|
|
206
|
+
"""Customizes the instance value exposed to parent components when using ref."""
|
|
207
|
+
...
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# =============================================================================
|
|
211
|
+
# Performance Hooks
|
|
212
|
+
# =============================================================================
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def useMemo(factory: _Callable[[], T], deps: list[_Any]) -> T:
|
|
216
|
+
"""Returns a memoized value.
|
|
217
|
+
|
|
218
|
+
Example:
|
|
219
|
+
expensive = useMemo(lambda: compute_expensive(a, b), [a, b])
|
|
220
|
+
"""
|
|
221
|
+
...
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def useCallback(callback: T, deps: list[_Any]) -> T:
|
|
225
|
+
"""Returns a memoized callback.
|
|
226
|
+
|
|
227
|
+
Example:
|
|
228
|
+
handle_click = useCallback(lambda e: print(e), [])
|
|
229
|
+
"""
|
|
230
|
+
...
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def useDeferredValue(value: T) -> T:
|
|
234
|
+
"""Defers updating a part of the UI. Returns a deferred version of the value."""
|
|
235
|
+
...
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def useTransition() -> tuple[bool, TransitionStartFunction]:
|
|
239
|
+
"""Returns a stateful value for pending state and a function to start transition.
|
|
240
|
+
|
|
241
|
+
Example:
|
|
242
|
+
is_pending, start_transition = useTransition()
|
|
243
|
+
start_transition(lambda: set_state(new_value))
|
|
244
|
+
"""
|
|
245
|
+
...
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
# =============================================================================
|
|
249
|
+
# Context Hooks
|
|
250
|
+
# =============================================================================
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def useContext(context: Context[T]) -> T:
|
|
254
|
+
"""Returns the current context value for the given context.
|
|
255
|
+
|
|
256
|
+
Example:
|
|
257
|
+
theme = useContext(ThemeContext)
|
|
258
|
+
"""
|
|
259
|
+
...
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
# =============================================================================
|
|
263
|
+
# Other Hooks
|
|
264
|
+
# =============================================================================
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def useId() -> str:
|
|
268
|
+
"""Generates a unique ID that is stable across server and client.
|
|
269
|
+
|
|
270
|
+
Example:
|
|
271
|
+
id = useId()
|
|
272
|
+
# <label htmlFor={id}>Name</label>
|
|
273
|
+
# <input id={id} />
|
|
274
|
+
"""
|
|
275
|
+
...
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def useDebugValue(value: T, format_fn: _Callable[[T], _Any] | None = None) -> None:
|
|
279
|
+
"""Displays a label in React DevTools for custom hooks."""
|
|
280
|
+
...
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def useSyncExternalStore(
|
|
284
|
+
subscribe: _Callable[[_Callable[[], None]], _Callable[[], None]],
|
|
285
|
+
get_snapshot: _Callable[[], T],
|
|
286
|
+
get_server_snapshot: _Callable[[], T] | None = None,
|
|
287
|
+
) -> T:
|
|
288
|
+
"""Subscribe to an external store.
|
|
289
|
+
|
|
290
|
+
Example:
|
|
291
|
+
width = useSyncExternalStore(
|
|
292
|
+
subscribe_to_resize,
|
|
293
|
+
lambda: window.innerWidth
|
|
294
|
+
)
|
|
295
|
+
"""
|
|
296
|
+
...
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
# =============================================================================
|
|
300
|
+
# React Components and Elements
|
|
301
|
+
# =============================================================================
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def createElement(
|
|
305
|
+
type: _Any,
|
|
306
|
+
props: dict[str, _Any] | None = None,
|
|
307
|
+
*children: _Any,
|
|
308
|
+
) -> ReactElement:
|
|
309
|
+
"""Creates a React element."""
|
|
310
|
+
...
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def cloneElement(
|
|
314
|
+
element: ReactElement,
|
|
315
|
+
props: dict[str, _Any] | None = None,
|
|
316
|
+
*children: _Any,
|
|
317
|
+
) -> ReactElement:
|
|
318
|
+
"""Clones and returns a new React element."""
|
|
319
|
+
...
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def isValidElement(obj: _Any) -> bool:
|
|
323
|
+
"""Checks if the object is a React element."""
|
|
324
|
+
...
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def memo(component: T, are_equal: _Callable[[_Any, _Any], bool] | None = None) -> T:
|
|
328
|
+
"""Memoizes a component to skip re-rendering when props are unchanged."""
|
|
329
|
+
...
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def forwardRef(
|
|
333
|
+
render: _Callable[[_Any, _Any], ReactElement | None],
|
|
334
|
+
) -> _Callable[..., ReactElement | None]:
|
|
335
|
+
"""Lets your component expose a DOM node to a parent component with a ref."""
|
|
336
|
+
...
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
class _LazyComponentFactory(_Expr):
|
|
340
|
+
"""React.lazy binding that works both at definition time and in @javascript.
|
|
341
|
+
|
|
342
|
+
This Expr represents React's lazy function. It can be:
|
|
343
|
+
- Called at Python definition time: lazy(factory) → Jsx(Constant(...))
|
|
344
|
+
- Used as a reference in @javascript: some_fn(lazy) → some_fn(lazy)
|
|
345
|
+
- Called inside @javascript: lazy(factory) → creates Constant+Jsx
|
|
346
|
+
|
|
347
|
+
Usage:
|
|
348
|
+
# At definition time (Python executes this)
|
|
349
|
+
LazyChart = lazy(Import("Chart", "./Chart", lazy=True))
|
|
350
|
+
|
|
351
|
+
# As reference in transpiled code
|
|
352
|
+
@javascript
|
|
353
|
+
def foo():
|
|
354
|
+
return higher_order_fn(lazy) # → higher_order_fn(lazy)
|
|
355
|
+
|
|
356
|
+
# Called in transpiled code
|
|
357
|
+
@javascript
|
|
358
|
+
def bar():
|
|
359
|
+
LazyComp = lazy(factory) # → const LazyComp_1 = lazy(factory)
|
|
360
|
+
return LazyComp()
|
|
361
|
+
"""
|
|
362
|
+
|
|
363
|
+
__slots__: tuple[str, ...] = ("_lazy_import",)
|
|
364
|
+
_lazy_import: _Import | None
|
|
365
|
+
|
|
366
|
+
def __init__(self) -> None:
|
|
367
|
+
# Defer Import creation to avoid polluting global import registry at module load
|
|
368
|
+
self._lazy_import = None
|
|
369
|
+
|
|
370
|
+
@property
|
|
371
|
+
def _import(self) -> _Import:
|
|
372
|
+
"""Lazily create the React.lazy import."""
|
|
373
|
+
if self._lazy_import is None:
|
|
374
|
+
self._lazy_import = _Import("lazy", "react")
|
|
375
|
+
return self._lazy_import
|
|
376
|
+
|
|
377
|
+
def _create_lazy_component(self, factory: _Expr) -> _Jsx:
|
|
378
|
+
"""Create a lazy-loaded component from a factory expression.
|
|
379
|
+
|
|
380
|
+
Args:
|
|
381
|
+
factory: An Expr that evaluates to a dynamic import factory
|
|
382
|
+
|
|
383
|
+
Returns:
|
|
384
|
+
A Jsx-wrapped lazy component
|
|
385
|
+
"""
|
|
386
|
+
lazy_call = _Call(self._import, [factory])
|
|
387
|
+
const = _Constant(lazy_call, lazy_call)
|
|
388
|
+
return _Jsx(const)
|
|
389
|
+
|
|
390
|
+
@_override
|
|
391
|
+
def emit(self, out: list[str]) -> None:
|
|
392
|
+
"""Emit as reference to the lazy import."""
|
|
393
|
+
self._import.emit(out)
|
|
394
|
+
|
|
395
|
+
@_override
|
|
396
|
+
def render(self) -> _VDOMNode:
|
|
397
|
+
raise TypeError("lazy cannot be rendered to VDOM")
|
|
398
|
+
|
|
399
|
+
@_override
|
|
400
|
+
def transpile_call(
|
|
401
|
+
self,
|
|
402
|
+
args: list[_ast.expr],
|
|
403
|
+
keywords: list[_ast.keyword],
|
|
404
|
+
ctx: "_Transpiler",
|
|
405
|
+
) -> _Expr:
|
|
406
|
+
"""Handle lazy(factory) calls in @javascript functions."""
|
|
407
|
+
if keywords:
|
|
408
|
+
raise _TranspileError("lazy() does not accept keyword arguments")
|
|
409
|
+
if len(args) != 1:
|
|
410
|
+
raise _TranspileError("lazy() takes exactly 1 argument")
|
|
411
|
+
|
|
412
|
+
factory = ctx.emit_expr(args[0])
|
|
413
|
+
return self._create_lazy_component(factory)
|
|
414
|
+
|
|
415
|
+
@_override
|
|
416
|
+
def __call__(self, factory: _Import) -> _Jsx: # pyright: ignore[reportIncompatibleMethodOverride]
|
|
417
|
+
"""Python-time call: create a lazy-loaded component.
|
|
418
|
+
|
|
419
|
+
Args:
|
|
420
|
+
factory: An Import with lazy=True that generates a dynamic import factory
|
|
421
|
+
|
|
422
|
+
Returns:
|
|
423
|
+
A Jsx-wrapped lazy component that can be used as LazyChart(props)[children]
|
|
424
|
+
"""
|
|
425
|
+
return self._create_lazy_component(factory)
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
# Singleton instance - use as: lazy(Import(...))
|
|
429
|
+
lazy: _LazyComponentFactory = _LazyComponentFactory()
|
|
430
|
+
# Register so transpiler can resolve it from closure
|
|
431
|
+
_Expr.register(lazy, lazy)
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def createContext(default_value: T) -> Context[T]:
|
|
435
|
+
"""Creates a Context object."""
|
|
436
|
+
...
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
# =============================================================================
|
|
440
|
+
# Components (stub declarations become Jsx-wrapped imports)
|
|
441
|
+
# =============================================================================
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
@_component
|
|
445
|
+
def Suspense(
|
|
446
|
+
*, fallback: ReactNode | _PulseNode | None = None, name: str | None = None
|
|
447
|
+
) -> ReactElement:
|
|
448
|
+
"""Lets you display a fallback while its children are loading."""
|
|
449
|
+
...
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
# =============================================================================
|
|
453
|
+
# Registration
|
|
454
|
+
# =============================================================================
|
|
455
|
+
|
|
456
|
+
# React is a namespace module where each hook is a named import
|
|
457
|
+
JsModule.register(name="React", src="react", kind="namespace", values="named_import")
|
pulse/messages.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from typing import Any, Literal, NotRequired, TypedDict
|
|
2
2
|
|
|
3
3
|
from pulse.routing import RouteInfo
|
|
4
|
-
from pulse.transpiler.vdom import VDOM, VDOMOperation
|
|
4
|
+
from pulse.transpiler.vdom import VDOM, VDOMNode, VDOMOperation
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
# ====================
|
|
@@ -80,12 +80,12 @@ class ServerChannelResponseMessage(TypedDict):
|
|
|
80
80
|
|
|
81
81
|
|
|
82
82
|
class ServerJsExecMessage(TypedDict):
|
|
83
|
-
"""Execute JavaScript
|
|
83
|
+
"""Execute JavaScript expression on the client."""
|
|
84
84
|
|
|
85
85
|
type: Literal["js_exec"]
|
|
86
86
|
path: str
|
|
87
87
|
id: str
|
|
88
|
-
|
|
88
|
+
expr: VDOMNode
|
|
89
89
|
|
|
90
90
|
|
|
91
91
|
# ====================
|
|
@@ -98,20 +98,20 @@ class ClientCallbackMessage(TypedDict):
|
|
|
98
98
|
args: list[Any]
|
|
99
99
|
|
|
100
100
|
|
|
101
|
-
class
|
|
102
|
-
type: Literal["
|
|
101
|
+
class ClientAttachMessage(TypedDict):
|
|
102
|
+
type: Literal["attach"]
|
|
103
103
|
path: str
|
|
104
104
|
routeInfo: RouteInfo
|
|
105
105
|
|
|
106
106
|
|
|
107
|
-
class
|
|
108
|
-
type: Literal["
|
|
107
|
+
class ClientUpdateMessage(TypedDict):
|
|
108
|
+
type: Literal["update"]
|
|
109
109
|
path: str
|
|
110
110
|
routeInfo: RouteInfo
|
|
111
111
|
|
|
112
112
|
|
|
113
|
-
class
|
|
114
|
-
type: Literal["
|
|
113
|
+
class ClientDetachMessage(TypedDict):
|
|
114
|
+
type: Literal["detach"]
|
|
115
115
|
path: str
|
|
116
116
|
|
|
117
117
|
|
|
@@ -165,9 +165,9 @@ ServerMessage = (
|
|
|
165
165
|
|
|
166
166
|
ClientPulseMessage = (
|
|
167
167
|
ClientCallbackMessage
|
|
168
|
-
|
|
|
169
|
-
|
|
|
170
|
-
|
|
|
168
|
+
| ClientAttachMessage
|
|
169
|
+
| ClientUpdateMessage
|
|
170
|
+
| ClientDetachMessage
|
|
171
171
|
| ClientApiResultMessage
|
|
172
172
|
| ClientJsResultMessage
|
|
173
173
|
)
|
|
@@ -193,5 +193,5 @@ class Directives(TypedDict):
|
|
|
193
193
|
|
|
194
194
|
|
|
195
195
|
class Prerender(TypedDict):
|
|
196
|
-
views: dict[str, ServerInitMessage | None]
|
|
196
|
+
views: dict[str, ServerInitMessage | ServerNavigateToMessage | None]
|
|
197
197
|
directives: Directives
|
pulse/proxy.py
CHANGED
|
@@ -15,6 +15,9 @@ from starlette.responses import PlainTextResponse, Response
|
|
|
15
15
|
from starlette.websockets import WebSocket, WebSocketDisconnect
|
|
16
16
|
from websockets.typing import Subprotocol
|
|
17
17
|
|
|
18
|
+
from pulse.context import PulseContext
|
|
19
|
+
from pulse.cookies import parse_cookie_header
|
|
20
|
+
|
|
18
21
|
logger = logging.getLogger(__name__)
|
|
19
22
|
|
|
20
23
|
|
|
@@ -108,11 +111,7 @@ class ReactProxy:
|
|
|
108
111
|
)
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
#
|
|
112
|
-
# We'll accept without subprotocol initially, then update if target accepts one
|
|
113
|
-
await websocket.accept()
|
|
114
|
-
|
|
115
|
-
# Connect to target WebSocket server
|
|
114
|
+
# Connect to target WebSocket server first to negotiate subprotocol
|
|
116
115
|
try:
|
|
117
116
|
async with websockets.connect(
|
|
118
117
|
target_url,
|
|
@@ -120,6 +119,9 @@ class ReactProxy:
|
|
|
120
119
|
subprotocols=subprotocols,
|
|
121
120
|
ping_interval=None, # Let the target server handle ping/pong
|
|
122
121
|
) as target_ws:
|
|
122
|
+
# Accept client connection with the negotiated subprotocol
|
|
123
|
+
await websocket.accept(subprotocol=target_ws.subprotocol)
|
|
124
|
+
|
|
123
125
|
# Forward messages bidirectionally
|
|
124
126
|
async def forward_client_to_target():
|
|
125
127
|
try:
|
|
@@ -190,6 +192,17 @@ class ReactProxy:
|
|
|
190
192
|
|
|
191
193
|
# Extract headers, skip host header (will be set by httpx)
|
|
192
194
|
headers = {k: v for k, v in request.headers.items() if k.lower() != "host"}
|
|
195
|
+
ctx = PulseContext.get()
|
|
196
|
+
session = ctx.session
|
|
197
|
+
if session is not None:
|
|
198
|
+
session_cookie = session.get_cookie_value(ctx.app.cookie.name)
|
|
199
|
+
if session_cookie:
|
|
200
|
+
existing = parse_cookie_header(headers.get("cookie"))
|
|
201
|
+
if existing.get(ctx.app.cookie.name) != session_cookie:
|
|
202
|
+
existing[ctx.app.cookie.name] = session_cookie
|
|
203
|
+
headers["cookie"] = "; ".join(
|
|
204
|
+
f"{key}={value}" for key, value in existing.items()
|
|
205
|
+
)
|
|
193
206
|
|
|
194
207
|
try:
|
|
195
208
|
# Build request
|