@carno.js/live 1.8.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.
Files changed (199) hide show
  1. package/LICENSE +21 -0
  2. package/dist/LiveEngine.d.ts +89 -0
  3. package/dist/LiveEngine.js +520 -0
  4. package/dist/LivePlugin.d.ts +55 -0
  5. package/dist/LivePlugin.js +165 -0
  6. package/dist/LiveService.d.ts +32 -0
  7. package/dist/LiveService.js +51 -0
  8. package/dist/auth/authorizer.d.ts +33 -0
  9. package/dist/auth/authorizer.js +32 -0
  10. package/dist/bus/InProcessBus.d.ts +8 -0
  11. package/dist/bus/InProcessBus.js +29 -0
  12. package/dist/bus/InvalidationBus.d.ts +15 -0
  13. package/dist/bus/InvalidationBus.js +2 -0
  14. package/dist/bus/PgNotifyBus.d.ts +55 -0
  15. package/dist/bus/PgNotifyBus.js +138 -0
  16. package/dist/client/angular.d.ts +19 -0
  17. package/dist/client/angular.js +71 -0
  18. package/dist/client/core.d.ts +111 -0
  19. package/dist/client/core.js +337 -0
  20. package/dist/client/hydrate.d.ts +19 -0
  21. package/dist/client/hydrate.js +65 -0
  22. package/dist/client/optimistic.d.ts +17 -0
  23. package/dist/client/optimistic.js +2 -0
  24. package/dist/client/react.d.ts +22 -0
  25. package/dist/client/react.js +69 -0
  26. package/dist/client/transport.d.ts +133 -0
  27. package/dist/client/transport.js +431 -0
  28. package/dist/client/vanilla.d.ts +54 -0
  29. package/dist/client/vanilla.js +111 -0
  30. package/dist/client/vue.d.ts +12 -0
  31. package/dist/client/vue.js +35 -0
  32. package/dist/config.d.ts +32 -0
  33. package/dist/config.js +21 -0
  34. package/dist/decorators/Live.d.ts +4 -0
  35. package/dist/decorators/Live.js +17 -0
  36. package/dist/emitters/AppEmitter.d.ts +20 -0
  37. package/dist/emitters/AppEmitter.js +59 -0
  38. package/dist/emitters/pg-listener.d.ts +50 -0
  39. package/dist/emitters/pg-listener.js +120 -0
  40. package/dist/emitters/pg-notify-emitter.d.ts +59 -0
  41. package/dist/emitters/pg-notify-emitter.js +105 -0
  42. package/dist/emitters/pg-trigger-sql.d.ts +27 -0
  43. package/dist/emitters/pg-trigger-sql.js +107 -0
  44. package/dist/emitters/statement-keys.d.ts +16 -0
  45. package/dist/emitters/statement-keys.js +182 -0
  46. package/dist/graph/DependencyGraph.d.ts +27 -0
  47. package/dist/graph/DependencyGraph.js +118 -0
  48. package/dist/graph/SubscriptionRegistry.d.ts +22 -0
  49. package/dist/graph/SubscriptionRegistry.js +92 -0
  50. package/dist/graph/dep-key.d.ts +14 -0
  51. package/dist/graph/dep-key.js +37 -0
  52. package/dist/graph/types.d.ts +13 -0
  53. package/dist/graph/types.js +2 -0
  54. package/dist/http/etag.d.ts +41 -0
  55. package/dist/http/etag.js +109 -0
  56. package/dist/index.d.ts +56 -0
  57. package/dist/index.js +111 -0
  58. package/dist/metadata.d.ts +16 -0
  59. package/dist/metadata.js +4 -0
  60. package/dist/observability.d.ts +25 -0
  61. package/dist/observability.js +44 -0
  62. package/dist/patch/PatchEngine.d.ts +20 -0
  63. package/dist/patch/PatchEngine.js +180 -0
  64. package/dist/patch/types.d.ts +33 -0
  65. package/dist/patch/types.js +2 -0
  66. package/dist/resource/ResourceRegistry.d.ts +32 -0
  67. package/dist/resource/ResourceRegistry.js +126 -0
  68. package/dist/resource/dependency-context.d.ts +25 -0
  69. package/dist/resource/dependency-context.js +45 -0
  70. package/dist/resource/instance-id.d.ts +17 -0
  71. package/dist/resource/instance-id.js +61 -0
  72. package/dist/resource/prefetch.d.ts +20 -0
  73. package/dist/resource/prefetch.js +31 -0
  74. package/dist/resource/route-executor.d.ts +13 -0
  75. package/dist/resource/route-executor.js +86 -0
  76. package/dist/resource/types.d.ts +26 -0
  77. package/dist/resource/types.js +2 -0
  78. package/dist/runtime.d.ts +41 -0
  79. package/dist/runtime.js +47 -0
  80. package/dist/shared/canonical.d.ts +14 -0
  81. package/dist/shared/canonical.js +60 -0
  82. package/dist/shared/descriptor.d.ts +44 -0
  83. package/dist/shared/descriptor.js +19 -0
  84. package/dist/shared/hash.d.ts +12 -0
  85. package/dist/shared/hash.js +29 -0
  86. package/dist/shared/inputs.d.ts +19 -0
  87. package/dist/shared/inputs.js +2 -0
  88. package/dist/shared/protocol.d.ts +83 -0
  89. package/dist/shared/protocol.js +9 -0
  90. package/dist/transport/FanTransport.d.ts +18 -0
  91. package/dist/transport/FanTransport.js +29 -0
  92. package/dist/transport/LiveGateway.d.ts +16 -0
  93. package/dist/transport/LiveGateway.js +126 -0
  94. package/dist/transport/SocketTransport.d.ts +18 -0
  95. package/dist/transport/SocketTransport.js +40 -0
  96. package/dist/transport/SseTransport.d.ts +30 -0
  97. package/dist/transport/SseTransport.js +92 -0
  98. package/dist/transport/scope-resolver.d.ts +20 -0
  99. package/dist/transport/scope-resolver.js +17 -0
  100. package/dist/transport/sse-routes.d.ts +24 -0
  101. package/dist/transport/sse-routes.js +78 -0
  102. package/package.json +92 -0
  103. package/src/LiveEngine.ts +730 -0
  104. package/src/LivePlugin.ts +253 -0
  105. package/src/LiveService.ts +47 -0
  106. package/src/auth/authorizer.ts +56 -0
  107. package/src/bus/InProcessBus.ts +28 -0
  108. package/src/bus/InvalidationBus.ts +17 -0
  109. package/src/bus/PgNotifyBus.ts +188 -0
  110. package/src/client/angular.ts +115 -0
  111. package/src/client/core.ts +492 -0
  112. package/src/client/hydrate.ts +80 -0
  113. package/src/client/optimistic.ts +19 -0
  114. package/src/client/react.ts +113 -0
  115. package/src/client/transport.ts +568 -0
  116. package/src/client/vanilla.ts +165 -0
  117. package/src/client/vue.ts +67 -0
  118. package/src/config.ts +49 -0
  119. package/src/decorators/Live.ts +16 -0
  120. package/src/emitters/AppEmitter.ts +69 -0
  121. package/src/emitters/pg-listener.ts +171 -0
  122. package/src/emitters/pg-notify-emitter.ts +143 -0
  123. package/src/emitters/pg-trigger-sql.ts +111 -0
  124. package/src/emitters/statement-keys.ts +229 -0
  125. package/src/graph/DependencyGraph.ts +147 -0
  126. package/src/graph/SubscriptionRegistry.ts +109 -0
  127. package/src/graph/dep-key.ts +41 -0
  128. package/src/graph/types.ts +15 -0
  129. package/src/http/etag.ts +144 -0
  130. package/src/index.ts +81 -0
  131. package/src/metadata.ts +21 -0
  132. package/src/observability.ts +54 -0
  133. package/src/patch/PatchEngine.ts +235 -0
  134. package/src/patch/types.ts +39 -0
  135. package/src/resource/ResourceRegistry.ts +178 -0
  136. package/src/resource/dependency-context.ts +48 -0
  137. package/src/resource/instance-id.ts +65 -0
  138. package/src/resource/prefetch.ts +49 -0
  139. package/src/resource/route-executor.ts +134 -0
  140. package/src/resource/types.ts +35 -0
  141. package/src/runtime.ts +69 -0
  142. package/src/shared/canonical.ts +63 -0
  143. package/src/shared/descriptor.ts +44 -0
  144. package/src/shared/hash.ts +31 -0
  145. package/src/shared/inputs.ts +20 -0
  146. package/src/shared/protocol.ts +96 -0
  147. package/src/transport/FanTransport.ts +34 -0
  148. package/src/transport/LiveGateway.ts +124 -0
  149. package/src/transport/SocketTransport.ts +43 -0
  150. package/src/transport/SseTransport.ts +112 -0
  151. package/src/transport/scope-resolver.ts +25 -0
  152. package/src/transport/sse-routes.ts +94 -0
  153. package/test/acceptance-fase-2.test.ts +248 -0
  154. package/test/acceptance-fase-3.test.ts +216 -0
  155. package/test/acceptance.test.ts +306 -0
  156. package/test/angular-adapter.test.ts +151 -0
  157. package/test/app-emitter.test.ts +112 -0
  158. package/test/authorization.test.ts +175 -0
  159. package/test/client-core.test.ts +287 -0
  160. package/test/client-transport.test.ts +154 -0
  161. package/test/dependency-graph.test.ts +83 -0
  162. package/test/etag.test.ts +193 -0
  163. package/test/fan-transport.test.ts +62 -0
  164. package/test/happydom.ts +24 -0
  165. package/test/inputs-body.test.ts +81 -0
  166. package/test/instance-id.test.ts +104 -0
  167. package/test/live-engine.test.ts +593 -0
  168. package/test/live-post.test.ts +81 -0
  169. package/test/metrics.test.ts +131 -0
  170. package/test/optimistic.test.ts +166 -0
  171. package/test/orm-integration.test.ts +75 -0
  172. package/test/patch-engine.test.ts +121 -0
  173. package/test/pg-bus.test.ts +162 -0
  174. package/test/pg-listen-probe.test.ts +56 -0
  175. package/test/pg-listener.test.ts +134 -0
  176. package/test/pg-notify-emitter.test.ts +113 -0
  177. package/test/pg-notify-integration.test.ts +88 -0
  178. package/test/polling-auth.test.ts +169 -0
  179. package/test/prefetch.test.ts +137 -0
  180. package/test/react-adapter.test.tsx +83 -0
  181. package/test/react-rerender.test.tsx +131 -0
  182. package/test/resource-registry-helper.ts +35 -0
  183. package/test/resource-registry.test.ts +144 -0
  184. package/test/route-pipeline.test.ts +139 -0
  185. package/test/shared.test.ts +53 -0
  186. package/test/sse-routes.test.ts +204 -0
  187. package/test/sse-transport.test.ts +91 -0
  188. package/test/statement-keys.test.ts +126 -0
  189. package/test/subscription-registry.test.ts +61 -0
  190. package/test/transport-ladder.test.ts +293 -0
  191. package/test/transport.test.ts +274 -0
  192. package/test/types/optimistic-types.ts +34 -0
  193. package/test/use-live-action.test.ts +127 -0
  194. package/test/use-live.test.ts +109 -0
  195. package/test/vanilla-adapter.test.ts +200 -0
  196. package/test/vue-adapter.test.ts +122 -0
  197. package/tsconfig.json +15 -0
  198. package/tsconfig.tsbuildinfo +1 -0
  199. package/tsconfig.types.json +10 -0
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSseRoutes = createSseRoutes;
4
+ const runtime_1 = require("../runtime");
5
+ const LiveGateway_1 = require("./LiveGateway");
6
+ // Captured at load so a later happy-dom register cannot replace the
7
+ // constructor Bun.serve requires of a route handler.
8
+ const NativeResponse = Response;
9
+ const SSE_HEADERS = {
10
+ 'Content-Type': 'text/event-stream',
11
+ 'Cache-Control': 'no-cache, no-transform',
12
+ Connection: 'keep-alive',
13
+ // Nginx buffers proxied responses by default, which turns a live stream
14
+ // into a stream that arrives all at once, at the end.
15
+ 'X-Accel-Buffering': 'no'
16
+ };
17
+ /**
18
+ * The two halves of the SSE transport, as HTTP.
19
+ *
20
+ * `GET streamPath` opens the downstream and names the connection; every
21
+ * client message goes up through `POST controlPath` and into the same
22
+ * `handleMessage` the WebSocket gateway uses. There is no second protocol
23
+ * here, and there must never be one.
24
+ *
25
+ * Handlers registered through `Carno.route()` receive a Bun `Request`, not a
26
+ * `Context` -- the docstring on that method says otherwise, the runtime does
27
+ * not.
28
+ */
29
+ function createSseRoutes(options) {
30
+ const { transport, streamPath, controlPath } = options;
31
+ const stream = (request) => {
32
+ // Unguessable on purpose: the id is a bearer for this connection, and
33
+ // whoever holds it can subscribe as it.
34
+ const connectionId = `sse:${crypto.randomUUID()}`;
35
+ try {
36
+ const body = transport.open(connectionId);
37
+ const runtime = (0, runtime_1.getLiveRuntime)();
38
+ // Until a `hello` arrives, the connection is its own principal:
39
+ // safe, shares nothing. Same rule as the gateway's onOpen.
40
+ runtime.scopes.set(connectionId, { principal: connectionId });
41
+ runtime.handshakes.delete(connectionId);
42
+ // Cancelling the client reader does not always reach the stream's
43
+ // `cancel`; aborting the request does, and is what a closed
44
+ // EventSource looks like on the wire.
45
+ request.signal.addEventListener('abort', () => {
46
+ transport.close(connectionId);
47
+ try {
48
+ (0, LiveGateway_1.dropLiveConnection)(connectionId);
49
+ }
50
+ catch {
51
+ // closeLiveRuntime nulls the runtime before dispose.
52
+ }
53
+ });
54
+ return new NativeResponse(body, { status: 200, headers: SSE_HEADERS });
55
+ }
56
+ catch (error) {
57
+ return new NativeResponse(error.message, { status: 503 });
58
+ }
59
+ };
60
+ const control = async (request) => {
61
+ let payload;
62
+ try {
63
+ payload = await request.json();
64
+ }
65
+ catch {
66
+ return new NativeResponse('malformed body', { status: 400 });
67
+ }
68
+ if (typeof payload.cid !== 'string' || !payload.message) {
69
+ return new NativeResponse('cid and message are required', { status: 400 });
70
+ }
71
+ if (!transport.owns(payload.cid)) {
72
+ return new NativeResponse('unknown connection', { status: 404 });
73
+ }
74
+ await (0, LiveGateway_1.handleMessage)(payload.cid, JSON.stringify(payload.message));
75
+ return new NativeResponse(null, { status: 204 });
76
+ };
77
+ return { streamPath, controlPath, stream, control };
78
+ }
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "@carno.js/live",
3
+ "version": "1.8.0",
4
+ "description": "Server-owned reactive state for Carno.js: live resources, dependency-graph invalidation, and framework-native client stores",
5
+ "type": "commonjs",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "require": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "./client": {
16
+ "types": "./dist/client/core.d.ts",
17
+ "require": "./dist/client/core.js",
18
+ "default": "./dist/client/core.js"
19
+ },
20
+ "./react": {
21
+ "types": "./dist/client/react.d.ts",
22
+ "require": "./dist/client/react.js",
23
+ "default": "./dist/client/react.js"
24
+ },
25
+ "./vanilla": {
26
+ "types": "./dist/client/vanilla.d.ts",
27
+ "require": "./dist/client/vanilla.js",
28
+ "default": "./dist/client/vanilla.js"
29
+ },
30
+ "./angular": {
31
+ "types": "./dist/client/angular.d.ts",
32
+ "require": "./dist/client/angular.js",
33
+ "default": "./dist/client/angular.js"
34
+ },
35
+ "./vue": {
36
+ "types": "./dist/client/vue.d.ts",
37
+ "require": "./dist/client/vue.js",
38
+ "default": "./dist/client/vue.js"
39
+ }
40
+ },
41
+ "scripts": {
42
+ "compile": "rm -rf ./dist tsconfig.tsbuildinfo && tsc --build --force",
43
+ "build": "tsc --build --force",
44
+ "test": "bun test",
45
+ "prepublishOnly": "bun run build"
46
+ },
47
+ "peerDependencies": {
48
+ "@angular/core": ">=18.0.0",
49
+ "@carno.js/core": "^1.7.0",
50
+ "@carno.js/orm": "^1.7.0",
51
+ "@carno.js/websocket": "^1.7.0",
52
+ "react": ">=18.0.0",
53
+ "vue": ">=3.4.0"
54
+ },
55
+ "peerDependenciesMeta": {
56
+ "@carno.js/orm": {
57
+ "optional": true
58
+ },
59
+ "react": {
60
+ "optional": true
61
+ },
62
+ "@angular/core": {
63
+ "optional": true
64
+ },
65
+ "vue": {
66
+ "optional": true
67
+ }
68
+ },
69
+ "devDependencies": {
70
+ "@angular/core": "^18.2.0",
71
+ "@happy-dom/global-registrator": "^20.0.0",
72
+ "@testing-library/dom": "^10.4.0",
73
+ "@testing-library/react": "^16.1.0",
74
+ "@types/react": "^18.3.12",
75
+ "react": "^18.3.1",
76
+ "react-dom": "^18.3.1",
77
+ "vue": "^3.5.0"
78
+ },
79
+ "keywords": [
80
+ "carno",
81
+ "carno.js",
82
+ "live",
83
+ "realtime",
84
+ "reactive",
85
+ "websocket"
86
+ ],
87
+ "license": "MIT",
88
+ "publishConfig": {
89
+ "access": "public"
90
+ },
91
+ "gitHead": "4eacc5852543c296787a2f8b92795a1775de44fa"
92
+ }