nowity 1.0.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 (192) hide show
  1. package/README.md +751 -0
  2. package/dist/api/contract.d.ts +186 -0
  3. package/dist/api/contract.d.ts.map +1 -0
  4. package/dist/api/contract.js +251 -0
  5. package/dist/api/errors.d.ts +50 -0
  6. package/dist/api/errors.d.ts.map +1 -0
  7. package/dist/api/errors.js +50 -0
  8. package/dist/api/index.d.ts +13 -0
  9. package/dist/api/index.d.ts.map +1 -0
  10. package/dist/api/index.js +32 -0
  11. package/dist/client/confirmed-store.d.ts +33 -0
  12. package/dist/client/confirmed-store.d.ts.map +1 -0
  13. package/dist/client/confirmed-store.js +123 -0
  14. package/dist/client/connection.d.ts +102 -0
  15. package/dist/client/connection.d.ts.map +1 -0
  16. package/dist/client/connection.js +285 -0
  17. package/dist/client/list.d.ts +102 -0
  18. package/dist/client/list.d.ts.map +1 -0
  19. package/dist/client/list.js +257 -0
  20. package/dist/client/live-query.d.ts +153 -0
  21. package/dist/client/live-query.d.ts.map +1 -0
  22. package/dist/client/live-query.js +244 -0
  23. package/dist/client/nowity-client.d.ts +188 -0
  24. package/dist/client/nowity-client.d.ts.map +1 -0
  25. package/dist/client/nowity-client.js +580 -0
  26. package/dist/client/null-transport.d.ts +16 -0
  27. package/dist/client/null-transport.d.ts.map +1 -0
  28. package/dist/client/null-transport.js +17 -0
  29. package/dist/client/overlay.d.ts +44 -0
  30. package/dist/client/overlay.d.ts.map +1 -0
  31. package/dist/client/overlay.js +90 -0
  32. package/dist/client/query-source.d.ts +78 -0
  33. package/dist/client/query-source.d.ts.map +1 -0
  34. package/dist/client/query-source.js +57 -0
  35. package/dist/client/ssr-source.d.ts +70 -0
  36. package/dist/client/ssr-source.d.ts.map +1 -0
  37. package/dist/client/ssr-source.js +134 -0
  38. package/dist/client/transport.d.ts +19 -0
  39. package/dist/client/transport.d.ts.map +1 -0
  40. package/dist/client/transport.js +2 -0
  41. package/dist/client/ws-transport.d.ts +30 -0
  42. package/dist/client/ws-transport.d.ts.map +1 -0
  43. package/dist/client/ws-transport.js +89 -0
  44. package/dist/client.d.ts +47 -0
  45. package/dist/client.d.ts.map +1 -0
  46. package/dist/client.js +80 -0
  47. package/dist/core/diff.d.ts +14 -0
  48. package/dist/core/diff.d.ts.map +1 -0
  49. package/dist/core/diff.js +56 -0
  50. package/dist/core/normalize.d.ts +16 -0
  51. package/dist/core/normalize.d.ts.map +1 -0
  52. package/dist/core/normalize.js +67 -0
  53. package/dist/core/protocol.d.ts +215 -0
  54. package/dist/core/protocol.d.ts.map +1 -0
  55. package/dist/core/protocol.js +14 -0
  56. package/dist/core/ssr-bridge.d.ts +69 -0
  57. package/dist/core/ssr-bridge.d.ts.map +1 -0
  58. package/dist/core/ssr-bridge.js +40 -0
  59. package/dist/core/types.d.ts +96 -0
  60. package/dist/core/types.d.ts.map +1 -0
  61. package/dist/core/types.js +17 -0
  62. package/dist/index.d.ts +47 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +98 -0
  65. package/dist/ironbean/client.d.ts +7 -0
  66. package/dist/ironbean/client.d.ts.map +1 -0
  67. package/dist/ironbean/client.js +38 -0
  68. package/dist/ironbean/server.d.ts +42 -0
  69. package/dist/ironbean/server.d.ts.map +1 -0
  70. package/dist/ironbean/server.js +62 -0
  71. package/dist/ironbean/tokens.d.ts +26 -0
  72. package/dist/ironbean/tokens.d.ts.map +1 -0
  73. package/dist/ironbean/tokens.js +23 -0
  74. package/dist/next/config.d.ts +7 -0
  75. package/dist/next/config.d.ts.map +1 -0
  76. package/dist/next/config.js +77 -0
  77. package/dist/next/live.d.ts +69 -0
  78. package/dist/next/live.d.ts.map +1 -0
  79. package/dist/next/live.js +70 -0
  80. package/dist/next/serve.d.ts +44 -0
  81. package/dist/next/serve.d.ts.map +1 -0
  82. package/dist/next/serve.js +107 -0
  83. package/dist/next/server.d.ts +62 -0
  84. package/dist/next/server.d.ts.map +1 -0
  85. package/dist/next/server.js +127 -0
  86. package/dist/react/connection-context.d.ts +4 -0
  87. package/dist/react/connection-context.d.ts.map +1 -0
  88. package/dist/react/connection-context.js +28 -0
  89. package/dist/react/grid-list.d.ts +34 -0
  90. package/dist/react/grid-list.d.ts.map +1 -0
  91. package/dist/react/grid-list.js +62 -0
  92. package/dist/react/hydrate.d.ts +22 -0
  93. package/dist/react/hydrate.d.ts.map +1 -0
  94. package/dist/react/hydrate.js +18 -0
  95. package/dist/react/index.d.ts +35 -0
  96. package/dist/react/index.d.ts.map +1 -0
  97. package/dist/react/index.js +51 -0
  98. package/dist/react/list.d.ts +31 -0
  99. package/dist/react/list.d.ts.map +1 -0
  100. package/dist/react/list.js +35 -0
  101. package/dist/react/prerendered.d.ts +33 -0
  102. package/dist/react/prerendered.d.ts.map +1 -0
  103. package/dist/react/prerendered.js +165 -0
  104. package/dist/react/provider.d.ts +64 -0
  105. package/dist/react/provider.d.ts.map +1 -0
  106. package/dist/react/provider.js +130 -0
  107. package/dist/react/refusals.d.ts +31 -0
  108. package/dist/react/refusals.d.ts.map +1 -0
  109. package/dist/react/refusals.js +51 -0
  110. package/dist/react/restricted.d.ts +76 -0
  111. package/dist/react/restricted.d.ts.map +1 -0
  112. package/dist/react/restricted.js +126 -0
  113. package/dist/react/use-list.d.ts +42 -0
  114. package/dist/react/use-list.d.ts.map +1 -0
  115. package/dist/react/use-list.js +56 -0
  116. package/dist/react/use-query.d.ts +66 -0
  117. package/dist/react/use-query.d.ts.map +1 -0
  118. package/dist/react/use-query.js +126 -0
  119. package/dist/react/virtual.d.ts +60 -0
  120. package/dist/react/virtual.d.ts.map +1 -0
  121. package/dist/react/virtual.js +131 -0
  122. package/dist/server/api/auth.d.ts +37 -0
  123. package/dist/server/api/auth.d.ts.map +1 -0
  124. package/dist/server/api/auth.js +54 -0
  125. package/dist/server/api/bridge.d.ts +28 -0
  126. package/dist/server/api/bridge.d.ts.map +1 -0
  127. package/dist/server/api/bridge.js +330 -0
  128. package/dist/server/api/collector.d.ts +25 -0
  129. package/dist/server/api/collector.d.ts.map +1 -0
  130. package/dist/server/api/collector.js +184 -0
  131. package/dist/server/api/configure.d.ts +31 -0
  132. package/dist/server/api/configure.d.ts.map +1 -0
  133. package/dist/server/api/configure.js +67 -0
  134. package/dist/server/api/execution.d.ts +77 -0
  135. package/dist/server/api/execution.d.ts.map +1 -0
  136. package/dist/server/api/execution.js +97 -0
  137. package/dist/server/api/list.d.ts +141 -0
  138. package/dist/server/api/list.d.ts.map +1 -0
  139. package/dist/server/api/list.js +281 -0
  140. package/dist/server/api/prerender.d.ts +32 -0
  141. package/dist/server/api/prerender.d.ts.map +1 -0
  142. package/dist/server/api/prerender.js +69 -0
  143. package/dist/server/api/read-keys.d.ts +128 -0
  144. package/dist/server/api/read-keys.d.ts.map +1 -0
  145. package/dist/server/api/read-keys.js +464 -0
  146. package/dist/server/api/read-set.d.ts +103 -0
  147. package/dist/server/api/read-set.d.ts.map +1 -0
  148. package/dist/server/api/read-set.js +210 -0
  149. package/dist/server/api/runtime.d.ts +36 -0
  150. package/dist/server/api/runtime.d.ts.map +1 -0
  151. package/dist/server/api/runtime.js +49 -0
  152. package/dist/server/api/tracked-client.d.ts +6 -0
  153. package/dist/server/api/tracked-client.d.ts.map +1 -0
  154. package/dist/server/api/tracked-client.js +56 -0
  155. package/dist/server/change-source/change-source.d.ts +41 -0
  156. package/dist/server/change-source/change-source.d.ts.map +1 -0
  157. package/dist/server/change-source/change-source.js +2 -0
  158. package/dist/server/change-source/outbox-change-source.d.ts +106 -0
  159. package/dist/server/change-source/outbox-change-source.d.ts.map +1 -0
  160. package/dist/server/change-source/outbox-change-source.js +237 -0
  161. package/dist/server/change-source/wal-change-source.d.ts +85 -0
  162. package/dist/server/change-source/wal-change-source.d.ts.map +1 -0
  163. package/dist/server/change-source/wal-change-source.js +193 -0
  164. package/dist/server/db.d.ts +51 -0
  165. package/dist/server/db.d.ts.map +1 -0
  166. package/dist/server/db.js +53 -0
  167. package/dist/server/evaluate.d.ts +39 -0
  168. package/dist/server/evaluate.d.ts.map +1 -0
  169. package/dist/server/evaluate.js +61 -0
  170. package/dist/server/invalidation-index.d.ts +64 -0
  171. package/dist/server/invalidation-index.d.ts.map +1 -0
  172. package/dist/server/invalidation-index.js +0 -0
  173. package/dist/server/mutator.d.ts +59 -0
  174. package/dist/server/mutator.d.ts.map +1 -0
  175. package/dist/server/mutator.js +77 -0
  176. package/dist/server/query.d.ts +113 -0
  177. package/dist/server/query.d.ts.map +1 -0
  178. package/dist/server/query.js +35 -0
  179. package/dist/server/setup.d.ts +30 -0
  180. package/dist/server/setup.d.ts.map +1 -0
  181. package/dist/server/setup.js +107 -0
  182. package/dist/server/sql/setup.sql +215 -0
  183. package/dist/server/ssr.d.ts +29 -0
  184. package/dist/server/ssr.d.ts.map +1 -0
  185. package/dist/server/ssr.js +43 -0
  186. package/dist/server/sync-engine.d.ts +256 -0
  187. package/dist/server/sync-engine.d.ts.map +1 -0
  188. package/dist/server/sync-engine.js +805 -0
  189. package/dist/server/ws-server.d.ts +56 -0
  190. package/dist/server/ws-server.d.ts.map +1 -0
  191. package/dist/server/ws-server.js +241 -0
  192. package/package.json +81 -0
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Overlay = void 0;
4
+ /**
5
+ * Copy-on-write layer over {@link ConfirmedStore} holding the effect of pending
6
+ * mutations.
7
+ *
8
+ * A rebase is: throw the overlay away, make a new one, replay every pending
9
+ * mutator into it. That is why mutators are named domain operations rather than
10
+ * precomputed patches — a patch cannot be meaningfully re-applied to state that
11
+ * has moved underneath it, an operation can.
12
+ */
13
+ class Overlay {
14
+ constructor(confirmed, idFieldOf) {
15
+ this.confirmed = confirmed;
16
+ this.idFieldOf = idFieldOf;
17
+ this.changes = new Map();
18
+ }
19
+ tx() {
20
+ return {
21
+ get: (entity, id) => this.get(entity, id),
22
+ all: (entity) => [...this.merged(entity).values()],
23
+ update: (entity, id, fields) => {
24
+ const current = this.get(entity, id);
25
+ if (!current)
26
+ return;
27
+ this.stage(entity, id, { ...current, ...fields });
28
+ },
29
+ put: (entity, row) => {
30
+ const id = String(row[this.idFieldOf(entity)]);
31
+ this.stage(entity, id, { ...row });
32
+ },
33
+ remove: (entity, id) => this.stage(entity, id, null),
34
+ };
35
+ }
36
+ get(entity, id) {
37
+ const staged = this.changes.get(entity);
38
+ if (staged?.has(id))
39
+ return staged.get(id) ?? undefined;
40
+ return this.confirmed.get(entity, id);
41
+ }
42
+ /**
43
+ * Rows a pending mutation created, per entity.
44
+ *
45
+ * Staged and not in the confirmed store: nothing on the server knows about
46
+ * them yet, which is exactly why no query result lists their ids. A window
47
+ * that knows its collection's ordering and membership can take them in
48
+ * meanwhile.
49
+ */
50
+ inserted() {
51
+ const out = new Map();
52
+ for (const [entity, staged] of this.changes) {
53
+ const ids = [];
54
+ for (const [id, row] of staged) {
55
+ if (row !== null && !this.confirmed.get(entity, id))
56
+ ids.push(id);
57
+ }
58
+ if (ids.length)
59
+ out.set(entity, ids);
60
+ }
61
+ return out;
62
+ }
63
+ /** Entities that either the server or a pending mutation knows about. */
64
+ entities() {
65
+ return [...new Set([...this.confirmed.entities(), ...this.changes.keys()])];
66
+ }
67
+ /** Confirmed rows with pending changes applied. */
68
+ merged(entity) {
69
+ const out = new Map(this.confirmed.rowsOf(entity));
70
+ const staged = this.changes.get(entity);
71
+ if (staged) {
72
+ for (const [id, row] of staged) {
73
+ if (row === null)
74
+ out.delete(id);
75
+ else
76
+ out.set(id, row);
77
+ }
78
+ }
79
+ return out;
80
+ }
81
+ stage(entity, id, row) {
82
+ let staged = this.changes.get(entity);
83
+ if (!staged) {
84
+ staged = new Map();
85
+ this.changes.set(entity, staged);
86
+ }
87
+ staged.set(id, row);
88
+ }
89
+ }
90
+ exports.Overlay = Overlay;
@@ -0,0 +1,78 @@
1
+ import type { HydrationPayload } from "../core/protocol";
2
+ import type { LiveQuery } from "./live-query";
3
+ /**
4
+ * A query that cannot be read right now.
5
+ *
6
+ * Thrown from `useQuery`, so it reaches an error boundary. `onReadable` is what
7
+ * makes that survivable: permissions are granted as well as revoked, and a
8
+ * boundary with no way to hear about it would leave "no access" on the screen
9
+ * long after access came back.
10
+ */
11
+ export interface QueryError extends Error {
12
+ onReadable?: (callback: () => void) => () => void;
13
+ }
14
+ /** One query, as much of it as a component needs. */
15
+ export interface QueryEntry {
16
+ /** The query and its arguments, by value — the identity of the subscription. */
17
+ readonly key: string;
18
+ /** No answer yet. False once there is one, whatever it says. */
19
+ readonly loading: boolean;
20
+ /** Why it cannot be read: refused, or broken. */
21
+ readonly problem: QueryError | null;
22
+ /** The rows, or the single row for a `findUnique`-shaped endpoint. */
23
+ readonly value: unknown;
24
+ /** Resolves when {@link loading} turns false — what a component suspends on. */
25
+ readonly settled: Promise<void>;
26
+ }
27
+ /** Where a component's queries are answered. */
28
+ export interface QuerySource {
29
+ /**
30
+ * This source answers once, and there is no second render to fill anything
31
+ * in — which is what a server render is.
32
+ *
33
+ * It matters to whoever draws a placeholder. In a browser "not here yet" can
34
+ * be a skeleton, because the row arrives and the component renders again; on
35
+ * a server the skeleton is final, and it is also what the browser would then
36
+ * *not* find in the markup it is hydrating. So a placeholder is a browser's
37
+ * privilege, and a server render waits instead.
38
+ */
39
+ readonly rendersOnce: boolean;
40
+ /** The shared entry for one query, created on first ask. */
41
+ ensure(name: string, args: unknown[]): QueryEntry;
42
+ /**
43
+ * Hold a subscription open for as long as a mounted component reads it.
44
+ *
45
+ * Called from an effect, so a server render never calls it at all — which is
46
+ * why a source that only ever renders once can leave both of these empty.
47
+ */
48
+ retain(entry: QueryEntry): void;
49
+ release(entry: QueryEntry): void;
50
+ /** The contract as methods that return promises, for writes and stray reads. */
51
+ contract<T extends object>(name: string): T;
52
+ /**
53
+ * Seed the store with what a server render already read.
54
+ *
55
+ * A payload is claimed by the first subscription for the same query and
56
+ * arguments, so a component that asks a moment later has its rows without a
57
+ * round trip — and the server's own snapshot replaces them once it arrives.
58
+ */
59
+ hydrate(payloads: HydrationPayload[]): void;
60
+ }
61
+ /**
62
+ * The current reason a query cannot be read, or null.
63
+ *
64
+ * Read from the live status rather than remembered, so a subscription that is
65
+ * revoked *after* it resolved reports it too — which is the case the initial
66
+ * promise cannot cover, because it has already settled.
67
+ */
68
+ export declare function problemOf(live: LiveQuery): QueryError | null;
69
+ /**
70
+ * What the caller gets: the live list, or the single row the method returned.
71
+ *
72
+ * `findUnique` and `findMany` are both "rows" on the wire, so cardinality is the
73
+ * server's answer rather than something read off the result — and `null` rather
74
+ * than `undefined` for the row that is not there, because undefined is what the
75
+ * hook already uses to mean "not allowed to see it".
76
+ */
77
+ export declare function valueOf(rows: readonly unknown[], single: boolean): unknown;
78
+ //# sourceMappingURL=query-source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-source.d.ts","sourceRoot":"","sources":["../../src/client/query-source.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C;;;;;;;GAOG;AACH,MAAM,WAAW,UAAW,SAAQ,KAAK;IACrC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;CACrD;AAED,qDAAqD;AACrD,MAAM,WAAW,UAAU;IACvB,gFAAgF;IAChF,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,gEAAgE;IAChE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,iDAAiD;IACjD,QAAQ,CAAC,OAAO,EAAE,UAAU,GAAG,IAAI,CAAC;IACpC,sEAAsE;IACtE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,gFAAgF;IAChF,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;CACnC;AAED,gDAAgD;AAChD,MAAM,WAAW,WAAW;IACxB;;;;;;;;;OASG;IACH,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,4DAA4D;IAC5D,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,UAAU,CAAC;IAClD;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IAChC,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IACjC,gFAAgF;IAChF,QAAQ,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC;IAC5C;;;;;;OAMG;IACH,OAAO,CAAC,QAAQ,EAAE,gBAAgB,EAAE,GAAG,IAAI,CAAC;CAC/C;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,SAAS,GAAG,UAAU,GAAG,IAAI,CAe5D;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAE1E"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.problemOf = problemOf;
4
+ exports.valueOf = valueOf;
5
+ /**
6
+ * What a component reads through, and the only thing it needs to know about.
7
+ *
8
+ * There are two of these. In a browser it is the {@link NowityConnection} — one
9
+ * socket, shared subscriptions, reference counts, a grace period for the render
10
+ * React threw away. While a page renders on the server it is
11
+ * {@link SsrQuerySource}, which has none of that because none of it applies: no
12
+ * socket, no second render, nothing to keep alive and nothing to collect.
13
+ *
14
+ * The interface is what makes that switch invisible above it. `useQuerySuspense`
15
+ * asks a source for an entry and reads three things off it, and the same three
16
+ * mean the same thing on both sides — which is the whole point of naming this at
17
+ * all rather than letting each half grow its own hook.
18
+ *
19
+ * What the two must *not* have separately is the answer to "what does a query
20
+ * result look like". That is the shaping below — status into a problem, rows into
21
+ * the value — and both sources call it, so a server-rendered result cannot come
22
+ * out different from the streamed one that replaces it a moment later.
23
+ */
24
+ const mobx_1 = require("mobx");
25
+ const errors_1 = require("../api/errors");
26
+ /**
27
+ * The current reason a query cannot be read, or null.
28
+ *
29
+ * Read from the live status rather than remembered, so a subscription that is
30
+ * revoked *after* it resolved reports it too — which is the case the initial
31
+ * promise cannot cover, because it has already settled.
32
+ */
33
+ function problemOf(live) {
34
+ const status = live.status;
35
+ if (status !== "revoked" && status !== "error")
36
+ return null;
37
+ const error = status === "revoked"
38
+ ? new errors_1.Forbidden(live.error ?? "Not authorized")
39
+ : new Error(live.error ?? "Query failed");
40
+ // Losing access is a state the data can come back from, and an error
41
+ // boundary cannot: once it has caught something it stays caught. So the
42
+ // error carries the way out — a boundary can wait on this and reset itself
43
+ // the moment the query is readable again.
44
+ error.onReadable = (callback) => (0, mobx_1.when)(() => live.status === "ready", callback);
45
+ return error;
46
+ }
47
+ /**
48
+ * What the caller gets: the live list, or the single row the method returned.
49
+ *
50
+ * `findUnique` and `findMany` are both "rows" on the wire, so cardinality is the
51
+ * server's answer rather than something read off the result — and `null` rather
52
+ * than `undefined` for the row that is not there, because undefined is what the
53
+ * hook already uses to mean "not allowed to see it".
54
+ */
55
+ function valueOf(rows, single) {
56
+ return single ? (rows[0] ?? null) : rows;
57
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Where a query is answered while the server renders the page.
3
+ *
4
+ * The obvious way to do this is to fake the socket: take the `subscribe` message
5
+ * the component's subscription produces, answer it with the `snapshot` message
6
+ * the socket would have sent, and let the browser's whole machinery run in a
7
+ * process that will forget it a millisecond later. It works, and it drags in
8
+ * everything that machinery exists for and none of which applies here —
9
+ * reference counts and a grace timer for a render React might throw away, an
10
+ * `autorun` per query waiting for changes that cannot arrive, an optimistic
11
+ * overlay rebased on every message with nothing pending to rebase.
12
+ *
13
+ * The timers are not free either: nothing retains a subscription on the server,
14
+ * because effects never run there, so every query starts its release timer
15
+ * immediately — and a render slower than the grace period has its entries
16
+ * collected underneath it, whereupon the retry opens a second subscription
17
+ * against a snapshot that is by then closed.
18
+ *
19
+ * So this is the same reading without any of it. What it keeps is the one thing
20
+ * worth keeping: the payload goes into the store through
21
+ * {@link NowityClient.hydrate}, exactly the path the browser applies a
22
+ * server-rendered payload through, and the value the component sees is shaped by
23
+ * the same {@link problemOf} and {@link valueOf} the socket-backed source uses.
24
+ * A refusal, a single row, an entity's identity — all of it is decided in one
25
+ * place, so the first paint and the snapshot that replaces it cannot disagree.
26
+ */
27
+ import type { HydrationPayload } from "../core/protocol";
28
+ import type { SsrReader } from "../core/ssr-bridge";
29
+ import { type QueryEntry, type QuerySource } from "./query-source";
30
+ export declare class SsrQuerySource implements QuerySource {
31
+ private readonly reader;
32
+ /** {@link QuerySource.rendersOnce} — this is the render that goes into the HTML. */
33
+ readonly rendersOnce = true;
34
+ /**
35
+ * A store with nowhere to send anything, which is all a render needs: the
36
+ * answers arrive from the reader, not from a socket, and the identity map and
37
+ * the diffing are what turn them into what a component reads.
38
+ */
39
+ private readonly store;
40
+ private readonly entries;
41
+ private kinds;
42
+ constructor(reader: SsrReader);
43
+ ensure(name: string, args: unknown[]): QueryEntry;
44
+ /** Nothing here outlives the render, so there is nothing to count or collect. */
45
+ retain(): void;
46
+ release(): void;
47
+ hydrate(payloads: HydrationPayload[]): void;
48
+ /**
49
+ * The contract as promises, for the components that hold a service they only
50
+ * call from an event handler — which is most of them.
51
+ *
52
+ * A read through it is the same subscription a hook would open, so asking
53
+ * twice costs one query. A write is refused rather than dropped: a render
54
+ * that mutates is a bug, and the render is over before any answer could
55
+ * arrive, so silence would leave a promise that never settles.
56
+ */
57
+ contract<T extends object>(name: string): T;
58
+ private call;
59
+ /**
60
+ * Query or write, from the manifest the reader carries.
61
+ *
62
+ * The same answer the socket's welcome gives the browser. A reader without a
63
+ * manifest cannot tell, and a read is the safe guess: it is refused by the
64
+ * collector if the endpoint does not exist, whereas guessing "write" would
65
+ * refuse a legitimate read.
66
+ */
67
+ private kindOf;
68
+ private answer;
69
+ }
70
+ //# sourceMappingURL=ssr-source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssr-source.d.ts","sourceRoot":"","sources":["../../src/client/ssr-source.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAiB,MAAM,kBAAkB,CAAC;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAKpD,OAAO,EAGH,KAAK,UAAU,EAEf,KAAK,WAAW,EACnB,MAAM,gBAAgB,CAAC;AA4CxB,qBAAa,cAAe,YAAW,WAAW;IAelC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAdnC,oFAAoF;IACpF,QAAQ,CAAC,WAAW,QAAQ;IAC5B;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAAC,KAAK,CAGnB;IACH,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+B;IACvD,OAAO,CAAC,KAAK,CAA0C;gBAE1B,MAAM,EAAE,SAAS;IAE9C,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,UAAU;IAUjD,iFAAiF;IACjF,MAAM,IAAI,IAAI;IACd,OAAO,IAAI,IAAI;IAEf,OAAO,CAAC,QAAQ,EAAE,gBAAgB,EAAE,GAAG,IAAI;IAI3C;;;;;;;;OAQG;IACH,QAAQ,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC;YAW7B,IAAI;IAsBlB;;;;;;;OAOG;IACH,OAAO,CAAC,MAAM;YAaA,MAAM;CASvB"}
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SsrQuerySource = void 0;
4
+ const nowity_client_1 = require("./nowity-client");
5
+ const null_transport_1 = require("./null-transport");
6
+ const query_source_1 = require("./query-source");
7
+ /** One query of one render: asked once, answered once, then read as many times as React retries. */
8
+ class SsrEntry {
9
+ constructor(key, answer) {
10
+ this.key = key;
11
+ this.live = null;
12
+ this.single = false;
13
+ this.failure = null;
14
+ // Settles, never rejects. A component suspends on this and React retries
15
+ // it either way, so carrying the failure on the entry rather than in the
16
+ // promise keeps one path: whatever is wrong is read off `problem`, at the
17
+ // point the component asks, the same as a refusal is.
18
+ this.settled = answer.then(({ live, single }) => {
19
+ this.live = live;
20
+ this.single = single;
21
+ }, (err) => {
22
+ this.failure = err instanceof Error ? err : new Error(String(err));
23
+ });
24
+ }
25
+ get loading() {
26
+ return !this.live && !this.failure;
27
+ }
28
+ get problem() {
29
+ return this.failure ?? (this.live ? (0, query_source_1.problemOf)(this.live) : null);
30
+ }
31
+ get value() {
32
+ return this.live ? (0, query_source_1.valueOf)(this.live.rows, this.single) : undefined;
33
+ }
34
+ }
35
+ class SsrQuerySource {
36
+ constructor(reader) {
37
+ this.reader = reader;
38
+ /** {@link QuerySource.rendersOnce} — this is the render that goes into the HTML. */
39
+ this.rendersOnce = true;
40
+ /**
41
+ * A store with nowhere to send anything, which is all a render needs: the
42
+ * answers arrive from the reader, not from a socket, and the identity map and
43
+ * the diffing are what turn them into what a component reads.
44
+ */
45
+ this.store = new nowity_client_1.NowityClient({
46
+ transport: new null_transport_1.NullTransport(),
47
+ clientId: "ssr",
48
+ });
49
+ this.entries = new Map();
50
+ this.kinds = null;
51
+ }
52
+ ensure(name, args) {
53
+ const key = (0, nowity_client_1.subscriptionKey)(name, args);
54
+ const existing = this.entries.get(key);
55
+ if (existing)
56
+ return existing;
57
+ const entry = new SsrEntry(key, this.answer(name, args));
58
+ this.entries.set(key, entry);
59
+ return entry;
60
+ }
61
+ /** Nothing here outlives the render, so there is nothing to count or collect. */
62
+ retain() { }
63
+ release() { }
64
+ hydrate(payloads) {
65
+ this.store.hydrate(payloads);
66
+ }
67
+ /**
68
+ * The contract as promises, for the components that hold a service they only
69
+ * call from an event handler — which is most of them.
70
+ *
71
+ * A read through it is the same subscription a hook would open, so asking
72
+ * twice costs one query. A write is refused rather than dropped: a render
73
+ * that mutates is a bug, and the render is over before any answer could
74
+ * arrive, so silence would leave a promise that never settles.
75
+ */
76
+ contract(name) {
77
+ const source = this;
78
+ return new Proxy({}, {
79
+ get(_target, method) {
80
+ if (typeof method !== "string")
81
+ return undefined;
82
+ return (...args) => source.call(`${name}.${method}`, args);
83
+ },
84
+ has: () => true,
85
+ });
86
+ }
87
+ async call(name, args) {
88
+ if (this.kindOf(name) === "list") {
89
+ throw new Error(`"${name}" is a @list endpoint, which is read a window at a time. ` +
90
+ `Use useList(...) for it.`);
91
+ }
92
+ if (this.kindOf(name) === "mutation") {
93
+ throw new Error(`"${name}" is a mutation, and a server render must not write. ` +
94
+ `Call it from an event handler, where the browser's connection ` +
95
+ `answers it.`);
96
+ }
97
+ const entry = this.ensure(name, args);
98
+ await entry.settled;
99
+ const problem = entry.problem;
100
+ if (problem)
101
+ throw problem;
102
+ return entry.value;
103
+ }
104
+ /**
105
+ * Query or write, from the manifest the reader carries.
106
+ *
107
+ * The same answer the socket's welcome gives the browser. A reader without a
108
+ * manifest cannot tell, and a read is the safe guess: it is refused by the
109
+ * collector if the endpoint does not exist, whereas guessing "write" would
110
+ * refuse a legitimate read.
111
+ */
112
+ kindOf(name) {
113
+ if (!this.kinds) {
114
+ this.kinds = new Map();
115
+ const manifest = this.reader.manifest();
116
+ for (const contract of manifest?.contracts ?? []) {
117
+ for (const endpoint of contract.endpoints) {
118
+ this.kinds.set(`${contract.name}.${endpoint.method}`, endpoint.kind);
119
+ }
120
+ }
121
+ }
122
+ return this.kinds.get(name);
123
+ }
124
+ async answer(name, args) {
125
+ const payload = await this.reader.read(name, args);
126
+ // The browser's own path, not an imitation of it: hydration is how a
127
+ // server-rendered payload becomes rows, and a subscribe is what claims
128
+ // one. Same code, so the render cannot see a shape the browser will not.
129
+ this.store.hydrate([payload]);
130
+ const live = this.store.subscribe(name, args);
131
+ return { live, single: this.store.isSingle(live.subId) === true };
132
+ }
133
+ }
134
+ exports.SsrQuerySource = SsrQuerySource;
@@ -0,0 +1,19 @@
1
+ import type { ClientMessage, ServerMessage } from "../core/protocol";
2
+ export interface TransportHandlers {
3
+ onOpen(): void;
4
+ onMessage(message: ServerMessage): void;
5
+ onClose(): void;
6
+ onError(error: Error): void;
7
+ }
8
+ /**
9
+ * How the client talks to the sync server.
10
+ *
11
+ * Reconnect policy lives in the client, not here: only the client knows what has
12
+ * to be re-established after a drop (subscriptions, unconfirmed mutations).
13
+ */
14
+ export interface Transport {
15
+ connect(handlers: TransportHandlers): void;
16
+ send(message: ClientMessage): void;
17
+ close(): void;
18
+ }
19
+ //# sourceMappingURL=transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../src/client/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAErE,MAAM,WAAW,iBAAiB;IAC9B,MAAM,IAAI,IAAI,CAAC;IACf,SAAS,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CAAC;IACxC,OAAO,IAAI,IAAI,CAAC;IAChB,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;CAC/B;AAED;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACtB,OAAO,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAC;IAC3C,IAAI,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CAAC;IACnC,KAAK,IAAI,IAAI,CAAC;CACjB"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,30 @@
1
+ import type { ClientMessage } from "../core/protocol";
2
+ import type { Transport, TransportHandlers } from "./transport";
3
+ export interface WebSocketTransportOptions {
4
+ url: string;
5
+ /**
6
+ * WebSocket implementation. Defaults to the global one in the browser; on
7
+ * Node pass the `ws` constructor.
8
+ */
9
+ WebSocketImpl?: any;
10
+ protocols?: string | string[];
11
+ }
12
+ /**
13
+ * WebSocket transport that works with both the browser API and Node's `ws`.
14
+ *
15
+ * Every listener is scoped to the socket that installed it. Without that, a
16
+ * `close` event from a socket that has already been replaced looks like a fresh
17
+ * disconnect and sends the client into a second reconnect — one that races with
18
+ * the first and can drop messages queued in between.
19
+ */
20
+ export declare class WebSocketTransport implements Transport {
21
+ private readonly opts;
22
+ private socket;
23
+ private handlers;
24
+ private queue;
25
+ constructor(opts: WebSocketTransportOptions);
26
+ connect(handlers: TransportHandlers): void;
27
+ send(message: ClientMessage): void;
28
+ close(): void;
29
+ }
30
+ //# sourceMappingURL=ws-transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ws-transport.d.ts","sourceRoot":"","sources":["../../src/client/ws-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAiB,MAAM,kBAAkB,CAAC;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAUhE,MAAM,WAAW,yBAAyB;IACtC,GAAG,EAAE,MAAM,CAAC;IACZ;;;OAGG;IACH,aAAa,CAAC,EAAE,GAAG,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CACjC;AAED;;;;;;;GAOG;AACH,qBAAa,kBAAmB,YAAW,SAAS;IAKpC,OAAO,CAAC,QAAQ,CAAC,IAAI;IAJjC,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,QAAQ,CAAkC;IAClD,OAAO,CAAC,KAAK,CAAuB;gBAEP,IAAI,EAAE,yBAAyB;IAE5D,OAAO,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI;IAmD1C,IAAI,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI;IAUlC,KAAK,IAAI,IAAI;CAShB"}
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketTransport = void 0;
4
+ /**
5
+ * WebSocket transport that works with both the browser API and Node's `ws`.
6
+ *
7
+ * Every listener is scoped to the socket that installed it. Without that, a
8
+ * `close` event from a socket that has already been replaced looks like a fresh
9
+ * disconnect and sends the client into a second reconnect — one that races with
10
+ * the first and can drop messages queued in between.
11
+ */
12
+ class WebSocketTransport {
13
+ constructor(opts) {
14
+ this.opts = opts;
15
+ this.socket = null;
16
+ this.handlers = null;
17
+ this.queue = [];
18
+ }
19
+ connect(handlers) {
20
+ this.handlers = handlers;
21
+ const Impl = this.opts.WebSocketImpl ?? globalThis.WebSocket;
22
+ if (!Impl) {
23
+ handlers.onError(new Error("No WebSocket implementation available"));
24
+ return;
25
+ }
26
+ const socket = new Impl(this.opts.url, this.opts.protocols);
27
+ this.socket = socket;
28
+ const isCurrent = () => this.socket === socket;
29
+ const bind = (event, listener) => {
30
+ if (socket.on)
31
+ socket.on(event, listener);
32
+ else
33
+ socket.addEventListener?.(event, listener);
34
+ };
35
+ bind("open", () => {
36
+ if (!isCurrent())
37
+ return;
38
+ // onOpen first: the client must get its `hello` in before anything that
39
+ // was queued while the socket was down, or the server would see
40
+ // messages with no session yet.
41
+ handlers.onOpen();
42
+ const queued = this.queue;
43
+ this.queue = [];
44
+ for (const message of queued)
45
+ this.send(message);
46
+ });
47
+ bind("message", (payload) => {
48
+ if (!isCurrent())
49
+ return;
50
+ const raw = typeof payload === "string" ? payload : (payload?.data ?? payload);
51
+ try {
52
+ handlers.onMessage(JSON.parse(String(raw)));
53
+ }
54
+ catch (err) {
55
+ handlers.onError(err);
56
+ }
57
+ });
58
+ bind("close", () => {
59
+ if (!isCurrent())
60
+ return;
61
+ this.socket = null;
62
+ handlers.onClose();
63
+ });
64
+ bind("error", (err) => {
65
+ if (!isCurrent())
66
+ return;
67
+ handlers.onError(err instanceof Error ? err : new Error(String(err?.message ?? err)));
68
+ });
69
+ }
70
+ send(message) {
71
+ const socket = this.socket;
72
+ // readyState 1 === OPEN in both implementations
73
+ if (!socket || socket.readyState !== 1) {
74
+ this.queue.push(message);
75
+ return;
76
+ }
77
+ socket.send(JSON.stringify(message));
78
+ }
79
+ close() {
80
+ const socket = this.socket;
81
+ // Detach first: the socket's own close event must not be mistaken for a
82
+ // disconnect of whatever connection comes next.
83
+ this.socket = null;
84
+ this.queue = [];
85
+ socket?.close();
86
+ this.handlers?.onClose();
87
+ }
88
+ }
89
+ exports.WebSocketTransport = WebSocketTransport;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * `nowity/client` — everything a browser is allowed to import.
3
+ *
4
+ * There are two entry points and one rule: a module that the browser bundles
5
+ * imports from here, and everything else imports from `nowity`. "The browser
6
+ * bundles it" means a `"use client"` component, and also the contract files both
7
+ * halves share — those are compiled into the client bundle even though the server
8
+ * implements them.
9
+ *
10
+ * The split is not tidiness. `nowity` reaches `pg`, `ws` and Prisma, so a client
11
+ * component that imported from it would drag sixty-odd server modules into the
12
+ * bundle, and native ones at that. Two entry points is the fewest that can state
13
+ * that boundary in a way a bundler enforces — one entry could only state it in a
14
+ * comment. `test/scenarios/16` is what keeps it honest.
15
+ *
16
+ * Contracts and errors are exported from both entries: they are the one thing
17
+ * genuinely shared, and the module behind them is the same instance either way,
18
+ * so a contract registered through one is the contract resolved through the
19
+ * other. A project with no React can therefore take them from `nowity` and never
20
+ * load this module at all.
21
+ */
22
+ export { api, contract, query, list, mutation, knownContracts, contractOf, resolveContracts, resetContracts, endpointName, pageQuery, countQuery, buildManifest, type ApiManifest, type ContractMeta, type EndpointKind, type EndpointMeta, type ListOptions, type ManifestContract, type ManifestEndpoint, type MutationOptions, type QueryOptions, } from "./api/contract";
23
+ export { Forbidden, Rejected, isForbidden, isRejected } from "./api/errors";
24
+ export { observer, useQuerySuspense, useList, GridList, List, LiveList, DEFAULT_PAGE_SIZE, useService, NowityProvider, SourceScope, useQuerySource,
25
+ /**
26
+ * Where a refused query is rendered — `<Suspense>`, for permissions. It is
27
+ * the other half of `useQuerySuspense` throwing rather than returning
28
+ * `undefined`, and unlike an error boundary it un-catches itself when access
29
+ * comes back.
30
+ */
31
+ Restricted, type ContractRef, type Suspended, type Listed, type UseListOptions, type RestrictedProps, type GridListProps, type ListProps, type NowityProviderProps, } from "./react/index";
32
+ export { NowityConnection, activeConnection, currentConnection, type NowityConnectionOptions, } from "./client/connection";
33
+ export type { QueryEntry, QueryError, QuerySource } from "./client/query-source";
34
+ /**
35
+ * The optimistic half of a write, which is the one thing about an endpoint that
36
+ * cannot be derived: a mutation's body is server code and never reaches the
37
+ * bundle, so an application that wants the change on screen before the server
38
+ * has answered states its effect here.
39
+ */
40
+ export type { ClientMutatorDef } from "./client/nowity-client";
41
+ export type { LocalTx } from "./client/overlay";
42
+ export { WebSocketTransport, type WebSocketTransportOptions } from "./client/ws-transport";
43
+ export { configureNowityClient, type NowityClientContainerConfig } from "./ironbean/client";
44
+ export { NowityClientToken } from "./ironbean/tokens";
45
+ export type { EntityId, Row } from "./core/types";
46
+ export type { HydrationPayload } from "./core/protocol";
47
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EACH,GAAG,EACH,QAAQ,EACR,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,cAAc,EACd,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,SAAS,EACT,UAAU,EACV,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,YAAY,GACpB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAQ5E,OAAO,EACH,QAAQ,EACR,gBAAgB,EAChB,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,QAAQ,EACR,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,WAAW,EACX,cAAc;AACd;;;;;GAKG;AACH,UAAU,EACV,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,MAAM,EACX,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,mBAAmB,GAC3B,MAAM,eAAe,CAAC;AAGvB,OAAO,EACH,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,KAAK,uBAAuB,GAC/B,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACjF;;;;;GAKG;AACH,YAAY,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,YAAY,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,KAAK,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAC3F,OAAO,EAAE,qBAAqB,EAAE,KAAK,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAC5F,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAGtD,YAAY,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC"}