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.
- package/README.md +751 -0
- package/dist/api/contract.d.ts +186 -0
- package/dist/api/contract.d.ts.map +1 -0
- package/dist/api/contract.js +251 -0
- package/dist/api/errors.d.ts +50 -0
- package/dist/api/errors.d.ts.map +1 -0
- package/dist/api/errors.js +50 -0
- package/dist/api/index.d.ts +13 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +32 -0
- package/dist/client/confirmed-store.d.ts +33 -0
- package/dist/client/confirmed-store.d.ts.map +1 -0
- package/dist/client/confirmed-store.js +123 -0
- package/dist/client/connection.d.ts +102 -0
- package/dist/client/connection.d.ts.map +1 -0
- package/dist/client/connection.js +285 -0
- package/dist/client/list.d.ts +102 -0
- package/dist/client/list.d.ts.map +1 -0
- package/dist/client/list.js +257 -0
- package/dist/client/live-query.d.ts +153 -0
- package/dist/client/live-query.d.ts.map +1 -0
- package/dist/client/live-query.js +244 -0
- package/dist/client/nowity-client.d.ts +188 -0
- package/dist/client/nowity-client.d.ts.map +1 -0
- package/dist/client/nowity-client.js +580 -0
- package/dist/client/null-transport.d.ts +16 -0
- package/dist/client/null-transport.d.ts.map +1 -0
- package/dist/client/null-transport.js +17 -0
- package/dist/client/overlay.d.ts +44 -0
- package/dist/client/overlay.d.ts.map +1 -0
- package/dist/client/overlay.js +90 -0
- package/dist/client/query-source.d.ts +78 -0
- package/dist/client/query-source.d.ts.map +1 -0
- package/dist/client/query-source.js +57 -0
- package/dist/client/ssr-source.d.ts +70 -0
- package/dist/client/ssr-source.d.ts.map +1 -0
- package/dist/client/ssr-source.js +134 -0
- package/dist/client/transport.d.ts +19 -0
- package/dist/client/transport.d.ts.map +1 -0
- package/dist/client/transport.js +2 -0
- package/dist/client/ws-transport.d.ts +30 -0
- package/dist/client/ws-transport.d.ts.map +1 -0
- package/dist/client/ws-transport.js +89 -0
- package/dist/client.d.ts +47 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +80 -0
- package/dist/core/diff.d.ts +14 -0
- package/dist/core/diff.d.ts.map +1 -0
- package/dist/core/diff.js +56 -0
- package/dist/core/normalize.d.ts +16 -0
- package/dist/core/normalize.d.ts.map +1 -0
- package/dist/core/normalize.js +67 -0
- package/dist/core/protocol.d.ts +215 -0
- package/dist/core/protocol.d.ts.map +1 -0
- package/dist/core/protocol.js +14 -0
- package/dist/core/ssr-bridge.d.ts +69 -0
- package/dist/core/ssr-bridge.d.ts.map +1 -0
- package/dist/core/ssr-bridge.js +40 -0
- package/dist/core/types.d.ts +96 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +17 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +98 -0
- package/dist/ironbean/client.d.ts +7 -0
- package/dist/ironbean/client.d.ts.map +1 -0
- package/dist/ironbean/client.js +38 -0
- package/dist/ironbean/server.d.ts +42 -0
- package/dist/ironbean/server.d.ts.map +1 -0
- package/dist/ironbean/server.js +62 -0
- package/dist/ironbean/tokens.d.ts +26 -0
- package/dist/ironbean/tokens.d.ts.map +1 -0
- package/dist/ironbean/tokens.js +23 -0
- package/dist/next/config.d.ts +7 -0
- package/dist/next/config.d.ts.map +1 -0
- package/dist/next/config.js +77 -0
- package/dist/next/live.d.ts +69 -0
- package/dist/next/live.d.ts.map +1 -0
- package/dist/next/live.js +70 -0
- package/dist/next/serve.d.ts +44 -0
- package/dist/next/serve.d.ts.map +1 -0
- package/dist/next/serve.js +107 -0
- package/dist/next/server.d.ts +62 -0
- package/dist/next/server.d.ts.map +1 -0
- package/dist/next/server.js +127 -0
- package/dist/react/connection-context.d.ts +4 -0
- package/dist/react/connection-context.d.ts.map +1 -0
- package/dist/react/connection-context.js +28 -0
- package/dist/react/grid-list.d.ts +34 -0
- package/dist/react/grid-list.d.ts.map +1 -0
- package/dist/react/grid-list.js +62 -0
- package/dist/react/hydrate.d.ts +22 -0
- package/dist/react/hydrate.d.ts.map +1 -0
- package/dist/react/hydrate.js +18 -0
- package/dist/react/index.d.ts +35 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +51 -0
- package/dist/react/list.d.ts +31 -0
- package/dist/react/list.d.ts.map +1 -0
- package/dist/react/list.js +35 -0
- package/dist/react/prerendered.d.ts +33 -0
- package/dist/react/prerendered.d.ts.map +1 -0
- package/dist/react/prerendered.js +165 -0
- package/dist/react/provider.d.ts +64 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +130 -0
- package/dist/react/refusals.d.ts +31 -0
- package/dist/react/refusals.d.ts.map +1 -0
- package/dist/react/refusals.js +51 -0
- package/dist/react/restricted.d.ts +76 -0
- package/dist/react/restricted.d.ts.map +1 -0
- package/dist/react/restricted.js +126 -0
- package/dist/react/use-list.d.ts +42 -0
- package/dist/react/use-list.d.ts.map +1 -0
- package/dist/react/use-list.js +56 -0
- package/dist/react/use-query.d.ts +66 -0
- package/dist/react/use-query.d.ts.map +1 -0
- package/dist/react/use-query.js +126 -0
- package/dist/react/virtual.d.ts +60 -0
- package/dist/react/virtual.d.ts.map +1 -0
- package/dist/react/virtual.js +131 -0
- package/dist/server/api/auth.d.ts +37 -0
- package/dist/server/api/auth.d.ts.map +1 -0
- package/dist/server/api/auth.js +54 -0
- package/dist/server/api/bridge.d.ts +28 -0
- package/dist/server/api/bridge.d.ts.map +1 -0
- package/dist/server/api/bridge.js +330 -0
- package/dist/server/api/collector.d.ts +25 -0
- package/dist/server/api/collector.d.ts.map +1 -0
- package/dist/server/api/collector.js +184 -0
- package/dist/server/api/configure.d.ts +31 -0
- package/dist/server/api/configure.d.ts.map +1 -0
- package/dist/server/api/configure.js +67 -0
- package/dist/server/api/execution.d.ts +77 -0
- package/dist/server/api/execution.d.ts.map +1 -0
- package/dist/server/api/execution.js +97 -0
- package/dist/server/api/list.d.ts +141 -0
- package/dist/server/api/list.d.ts.map +1 -0
- package/dist/server/api/list.js +281 -0
- package/dist/server/api/prerender.d.ts +32 -0
- package/dist/server/api/prerender.d.ts.map +1 -0
- package/dist/server/api/prerender.js +69 -0
- package/dist/server/api/read-keys.d.ts +128 -0
- package/dist/server/api/read-keys.d.ts.map +1 -0
- package/dist/server/api/read-keys.js +464 -0
- package/dist/server/api/read-set.d.ts +103 -0
- package/dist/server/api/read-set.d.ts.map +1 -0
- package/dist/server/api/read-set.js +210 -0
- package/dist/server/api/runtime.d.ts +36 -0
- package/dist/server/api/runtime.d.ts.map +1 -0
- package/dist/server/api/runtime.js +49 -0
- package/dist/server/api/tracked-client.d.ts +6 -0
- package/dist/server/api/tracked-client.d.ts.map +1 -0
- package/dist/server/api/tracked-client.js +56 -0
- package/dist/server/change-source/change-source.d.ts +41 -0
- package/dist/server/change-source/change-source.d.ts.map +1 -0
- package/dist/server/change-source/change-source.js +2 -0
- package/dist/server/change-source/outbox-change-source.d.ts +106 -0
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -0
- package/dist/server/change-source/outbox-change-source.js +237 -0
- package/dist/server/change-source/wal-change-source.d.ts +85 -0
- package/dist/server/change-source/wal-change-source.d.ts.map +1 -0
- package/dist/server/change-source/wal-change-source.js +193 -0
- package/dist/server/db.d.ts +51 -0
- package/dist/server/db.d.ts.map +1 -0
- package/dist/server/db.js +53 -0
- package/dist/server/evaluate.d.ts +39 -0
- package/dist/server/evaluate.d.ts.map +1 -0
- package/dist/server/evaluate.js +61 -0
- package/dist/server/invalidation-index.d.ts +64 -0
- package/dist/server/invalidation-index.d.ts.map +1 -0
- package/dist/server/invalidation-index.js +0 -0
- package/dist/server/mutator.d.ts +59 -0
- package/dist/server/mutator.d.ts.map +1 -0
- package/dist/server/mutator.js +77 -0
- package/dist/server/query.d.ts +113 -0
- package/dist/server/query.d.ts.map +1 -0
- package/dist/server/query.js +35 -0
- package/dist/server/setup.d.ts +30 -0
- package/dist/server/setup.d.ts.map +1 -0
- package/dist/server/setup.js +107 -0
- package/dist/server/sql/setup.sql +215 -0
- package/dist/server/ssr.d.ts +29 -0
- package/dist/server/ssr.d.ts.map +1 -0
- package/dist/server/ssr.js +43 -0
- package/dist/server/sync-engine.d.ts +256 -0
- package/dist/server/sync-engine.d.ts.map +1 -0
- package/dist/server/sync-engine.js +805 -0
- package/dist/server/ws-server.d.ts +56 -0
- package/dist/server/ws-server.d.ts.map +1 -0
- package/dist/server/ws-server.js +241 -0
- package/package.json +81 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `nowity/client` — everything a browser is allowed to import.
|
|
4
|
+
*
|
|
5
|
+
* There are two entry points and one rule: a module that the browser bundles
|
|
6
|
+
* imports from here, and everything else imports from `nowity`. "The browser
|
|
7
|
+
* bundles it" means a `"use client"` component, and also the contract files both
|
|
8
|
+
* halves share — those are compiled into the client bundle even though the server
|
|
9
|
+
* implements them.
|
|
10
|
+
*
|
|
11
|
+
* The split is not tidiness. `nowity` reaches `pg`, `ws` and Prisma, so a client
|
|
12
|
+
* component that imported from it would drag sixty-odd server modules into the
|
|
13
|
+
* bundle, and native ones at that. Two entry points is the fewest that can state
|
|
14
|
+
* that boundary in a way a bundler enforces — one entry could only state it in a
|
|
15
|
+
* comment. `test/scenarios/16` is what keeps it honest.
|
|
16
|
+
*
|
|
17
|
+
* Contracts and errors are exported from both entries: they are the one thing
|
|
18
|
+
* genuinely shared, and the module behind them is the same instance either way,
|
|
19
|
+
* so a contract registered through one is the contract resolved through the
|
|
20
|
+
* other. A project with no React can therefore take them from `nowity` and never
|
|
21
|
+
* load this module at all.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.NowityClientToken = exports.configureNowityClient = exports.WebSocketTransport = exports.currentConnection = exports.activeConnection = exports.NowityConnection = exports.Restricted = exports.useQuerySource = exports.SourceScope = exports.NowityProvider = exports.useService = exports.DEFAULT_PAGE_SIZE = exports.LiveList = exports.List = exports.GridList = exports.useList = exports.useQuerySuspense = exports.observer = exports.isRejected = exports.isForbidden = exports.Rejected = exports.Forbidden = exports.buildManifest = exports.countQuery = exports.pageQuery = exports.endpointName = exports.resetContracts = exports.resolveContracts = exports.contractOf = exports.knownContracts = exports.mutation = exports.list = exports.query = exports.contract = exports.api = void 0;
|
|
25
|
+
// ------------------------------------------------------------------- contracts
|
|
26
|
+
var contract_1 = require("./api/contract");
|
|
27
|
+
Object.defineProperty(exports, "api", { enumerable: true, get: function () { return contract_1.api; } });
|
|
28
|
+
Object.defineProperty(exports, "contract", { enumerable: true, get: function () { return contract_1.contract; } });
|
|
29
|
+
Object.defineProperty(exports, "query", { enumerable: true, get: function () { return contract_1.query; } });
|
|
30
|
+
Object.defineProperty(exports, "list", { enumerable: true, get: function () { return contract_1.list; } });
|
|
31
|
+
Object.defineProperty(exports, "mutation", { enumerable: true, get: function () { return contract_1.mutation; } });
|
|
32
|
+
Object.defineProperty(exports, "knownContracts", { enumerable: true, get: function () { return contract_1.knownContracts; } });
|
|
33
|
+
Object.defineProperty(exports, "contractOf", { enumerable: true, get: function () { return contract_1.contractOf; } });
|
|
34
|
+
Object.defineProperty(exports, "resolveContracts", { enumerable: true, get: function () { return contract_1.resolveContracts; } });
|
|
35
|
+
Object.defineProperty(exports, "resetContracts", { enumerable: true, get: function () { return contract_1.resetContracts; } });
|
|
36
|
+
Object.defineProperty(exports, "endpointName", { enumerable: true, get: function () { return contract_1.endpointName; } });
|
|
37
|
+
Object.defineProperty(exports, "pageQuery", { enumerable: true, get: function () { return contract_1.pageQuery; } });
|
|
38
|
+
Object.defineProperty(exports, "countQuery", { enumerable: true, get: function () { return contract_1.countQuery; } });
|
|
39
|
+
Object.defineProperty(exports, "buildManifest", { enumerable: true, get: function () { return contract_1.buildManifest; } });
|
|
40
|
+
var errors_1 = require("./api/errors");
|
|
41
|
+
Object.defineProperty(exports, "Forbidden", { enumerable: true, get: function () { return errors_1.Forbidden; } });
|
|
42
|
+
Object.defineProperty(exports, "Rejected", { enumerable: true, get: function () { return errors_1.Rejected; } });
|
|
43
|
+
Object.defineProperty(exports, "isForbidden", { enumerable: true, get: function () { return errors_1.isForbidden; } });
|
|
44
|
+
Object.defineProperty(exports, "isRejected", { enumerable: true, get: function () { return errors_1.isRejected; } });
|
|
45
|
+
// ------------------------------------------------------------------------ react
|
|
46
|
+
//
|
|
47
|
+
// Routed through one `"use client"` barrel rather than taken from each module.
|
|
48
|
+
// A server component importing `<NowityProvider>` from here has to meet a
|
|
49
|
+
// boundary, not a module the RSC layer then evaluates — and the contracts above
|
|
50
|
+
// must *not* be behind that boundary, because both halves call them for real.
|
|
51
|
+
var index_1 = require("./react/index");
|
|
52
|
+
Object.defineProperty(exports, "observer", { enumerable: true, get: function () { return index_1.observer; } });
|
|
53
|
+
Object.defineProperty(exports, "useQuerySuspense", { enumerable: true, get: function () { return index_1.useQuerySuspense; } });
|
|
54
|
+
Object.defineProperty(exports, "useList", { enumerable: true, get: function () { return index_1.useList; } });
|
|
55
|
+
Object.defineProperty(exports, "GridList", { enumerable: true, get: function () { return index_1.GridList; } });
|
|
56
|
+
Object.defineProperty(exports, "List", { enumerable: true, get: function () { return index_1.List; } });
|
|
57
|
+
Object.defineProperty(exports, "LiveList", { enumerable: true, get: function () { return index_1.LiveList; } });
|
|
58
|
+
Object.defineProperty(exports, "DEFAULT_PAGE_SIZE", { enumerable: true, get: function () { return index_1.DEFAULT_PAGE_SIZE; } });
|
|
59
|
+
Object.defineProperty(exports, "useService", { enumerable: true, get: function () { return index_1.useService; } });
|
|
60
|
+
Object.defineProperty(exports, "NowityProvider", { enumerable: true, get: function () { return index_1.NowityProvider; } });
|
|
61
|
+
Object.defineProperty(exports, "SourceScope", { enumerable: true, get: function () { return index_1.SourceScope; } });
|
|
62
|
+
Object.defineProperty(exports, "useQuerySource", { enumerable: true, get: function () { return index_1.useQuerySource; } });
|
|
63
|
+
/**
|
|
64
|
+
* Where a refused query is rendered — `<Suspense>`, for permissions. It is
|
|
65
|
+
* the other half of `useQuerySuspense` throwing rather than returning
|
|
66
|
+
* `undefined`, and unlike an error boundary it un-catches itself when access
|
|
67
|
+
* comes back.
|
|
68
|
+
*/
|
|
69
|
+
Object.defineProperty(exports, "Restricted", { enumerable: true, get: function () { return index_1.Restricted; } });
|
|
70
|
+
// ------------------------------------------------------------------------ store
|
|
71
|
+
var connection_1 = require("./client/connection");
|
|
72
|
+
Object.defineProperty(exports, "NowityConnection", { enumerable: true, get: function () { return connection_1.NowityConnection; } });
|
|
73
|
+
Object.defineProperty(exports, "activeConnection", { enumerable: true, get: function () { return connection_1.activeConnection; } });
|
|
74
|
+
Object.defineProperty(exports, "currentConnection", { enumerable: true, get: function () { return connection_1.currentConnection; } });
|
|
75
|
+
var ws_transport_1 = require("./client/ws-transport");
|
|
76
|
+
Object.defineProperty(exports, "WebSocketTransport", { enumerable: true, get: function () { return ws_transport_1.WebSocketTransport; } });
|
|
77
|
+
var client_1 = require("./ironbean/client");
|
|
78
|
+
Object.defineProperty(exports, "configureNowityClient", { enumerable: true, get: function () { return client_1.configureNowityClient; } });
|
|
79
|
+
var tokens_1 = require("./ironbean/tokens");
|
|
80
|
+
Object.defineProperty(exports, "NowityClientToken", { enumerable: true, get: function () { return tokens_1.NowityClientToken; } });
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EntityId, Row, SetDiff } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Field-level diff of two versions of the same result set.
|
|
4
|
+
*
|
|
5
|
+
* This is the naive incremental view maintenance from M3 of the design doc: the
|
|
6
|
+
* query is re-executed and its result compared to the previous one. It is
|
|
7
|
+
* O(rows) per change instead of O(changed rows), but it is correct for *any*
|
|
8
|
+
* query — including rows that enter or leave the result without their own data
|
|
9
|
+
* changing — which is the property that matters. Optimise only against
|
|
10
|
+
* measurements.
|
|
11
|
+
*/
|
|
12
|
+
export declare function diffRowSets(prev: Map<EntityId, Row>, next: Map<EntityId, Row>): SetDiff;
|
|
13
|
+
export declare function indexById(rows: Row[], idField: string): Map<EntityId, Row>;
|
|
14
|
+
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../src/core/diff.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAY,OAAO,EAAE,MAAM,SAAS,CAAC;AAEhE;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,GAAG,OAAO,CAmCvF;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,CAM1E"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.diffRowSets = diffRowSets;
|
|
4
|
+
exports.indexById = indexById;
|
|
5
|
+
const normalize_1 = require("./normalize");
|
|
6
|
+
/**
|
|
7
|
+
* Field-level diff of two versions of the same result set.
|
|
8
|
+
*
|
|
9
|
+
* This is the naive incremental view maintenance from M3 of the design doc: the
|
|
10
|
+
* query is re-executed and its result compared to the previous one. It is
|
|
11
|
+
* O(rows) per change instead of O(changed rows), but it is correct for *any*
|
|
12
|
+
* query — including rows that enter or leave the result without their own data
|
|
13
|
+
* changing — which is the property that matters. Optimise only against
|
|
14
|
+
* measurements.
|
|
15
|
+
*/
|
|
16
|
+
function diffRowSets(prev, next) {
|
|
17
|
+
const upsert = [];
|
|
18
|
+
const remove = [];
|
|
19
|
+
for (const [id, nextRow] of next) {
|
|
20
|
+
const prevRow = prev.get(id);
|
|
21
|
+
if (prevRow === undefined) {
|
|
22
|
+
upsert.push({ id, fields: { ...nextRow } });
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const fields = {};
|
|
26
|
+
let changed = false;
|
|
27
|
+
for (const key of Object.keys(nextRow)) {
|
|
28
|
+
if (!(0, normalize_1.valuesEqual)(prevRow[key], nextRow[key])) {
|
|
29
|
+
fields[key] = nextRow[key];
|
|
30
|
+
changed = true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// A field that disappeared from the projection is reported as null
|
|
34
|
+
// rather than left stale.
|
|
35
|
+
for (const key of Object.keys(prevRow)) {
|
|
36
|
+
if (!(key in nextRow)) {
|
|
37
|
+
fields[key] = null;
|
|
38
|
+
changed = true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (changed)
|
|
42
|
+
upsert.push({ id, fields });
|
|
43
|
+
}
|
|
44
|
+
for (const id of prev.keys()) {
|
|
45
|
+
if (!next.has(id))
|
|
46
|
+
remove.push(id);
|
|
47
|
+
}
|
|
48
|
+
return { upsert, remove };
|
|
49
|
+
}
|
|
50
|
+
function indexById(rows, idField) {
|
|
51
|
+
const map = new Map();
|
|
52
|
+
for (const row of rows) {
|
|
53
|
+
map.set(String(row[idField]), row);
|
|
54
|
+
}
|
|
55
|
+
return map;
|
|
56
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EntityId, Row } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Make a value comparable and JSON-transportable.
|
|
4
|
+
*
|
|
5
|
+
* Query results arrive either from `pg` (Date, BigInt, Buffer) or from Prisma
|
|
6
|
+
* (Date, Decimal, BigInt). Patches are computed by comparing consecutive query
|
|
7
|
+
* results, so values must have a canonical representation — otherwise two
|
|
8
|
+
* equal timestamps read on two different runs would look like a change and the
|
|
9
|
+
* client would get a patch on every event.
|
|
10
|
+
*/
|
|
11
|
+
export declare function normalizeValue(value: unknown): unknown;
|
|
12
|
+
export declare function normalizeRow(row: Row): Row;
|
|
13
|
+
/** Structural equality over already-normalised values. */
|
|
14
|
+
export declare function valuesEqual(a: unknown, b: unknown): boolean;
|
|
15
|
+
export declare function idOf(row: Row, idField: string): EntityId;
|
|
16
|
+
//# sourceMappingURL=normalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/core/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAuBtD;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAM1C;AAED,0DAA0D;AAC1D,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAM3D;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,CAMxD"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeValue = normalizeValue;
|
|
4
|
+
exports.normalizeRow = normalizeRow;
|
|
5
|
+
exports.valuesEqual = valuesEqual;
|
|
6
|
+
exports.idOf = idOf;
|
|
7
|
+
/**
|
|
8
|
+
* Make a value comparable and JSON-transportable.
|
|
9
|
+
*
|
|
10
|
+
* Query results arrive either from `pg` (Date, BigInt, Buffer) or from Prisma
|
|
11
|
+
* (Date, Decimal, BigInt). Patches are computed by comparing consecutive query
|
|
12
|
+
* results, so values must have a canonical representation — otherwise two
|
|
13
|
+
* equal timestamps read on two different runs would look like a change and the
|
|
14
|
+
* client would get a patch on every event.
|
|
15
|
+
*/
|
|
16
|
+
function normalizeValue(value) {
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
return null;
|
|
19
|
+
if (value === null)
|
|
20
|
+
return null;
|
|
21
|
+
if (value instanceof Date)
|
|
22
|
+
return value.toISOString();
|
|
23
|
+
if (typeof value === "bigint")
|
|
24
|
+
return value.toString();
|
|
25
|
+
if (Buffer.isBuffer(value))
|
|
26
|
+
return value.toString("base64");
|
|
27
|
+
if (Array.isArray(value))
|
|
28
|
+
return value.map(normalizeValue);
|
|
29
|
+
if (typeof value === "object") {
|
|
30
|
+
// Prisma Decimal and friends expose toJSON / toString.
|
|
31
|
+
const asAny = value;
|
|
32
|
+
if (typeof asAny.toJSON === "function")
|
|
33
|
+
return normalizeValue(asAny.toJSON());
|
|
34
|
+
const out = {};
|
|
35
|
+
for (const key of Object.keys(value).sort()) {
|
|
36
|
+
out[key] = normalizeValue(value[key]);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
function normalizeRow(row) {
|
|
43
|
+
const out = {};
|
|
44
|
+
for (const key of Object.keys(row)) {
|
|
45
|
+
out[key] = normalizeValue(row[key]);
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
/** Structural equality over already-normalised values. */
|
|
50
|
+
function valuesEqual(a, b) {
|
|
51
|
+
if (a === b)
|
|
52
|
+
return true;
|
|
53
|
+
if (a === null || b === null)
|
|
54
|
+
return false;
|
|
55
|
+
if (typeof a !== typeof b)
|
|
56
|
+
return false;
|
|
57
|
+
if (typeof a !== "object")
|
|
58
|
+
return false;
|
|
59
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
60
|
+
}
|
|
61
|
+
function idOf(row, idField) {
|
|
62
|
+
const raw = row[idField];
|
|
63
|
+
if (raw === null || raw === undefined) {
|
|
64
|
+
throw new Error(`Row is missing id field "${idField}": ${JSON.stringify(row)}`);
|
|
65
|
+
}
|
|
66
|
+
return String(raw);
|
|
67
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { EntityId, Row, RowPatch } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Wire protocol between the sync server and the client.
|
|
4
|
+
*
|
|
5
|
+
* Two invariants encoded here:
|
|
6
|
+
* - patches travel **per transaction** (`ServerTx`), never per row, so the
|
|
7
|
+
* client can apply a whole transaction atomically;
|
|
8
|
+
* - every snapshot and every patch carries the authorization `epoch` of its
|
|
9
|
+
* scope, so a client can drop a patch that was computed before a permission
|
|
10
|
+
* change it has already observed.
|
|
11
|
+
*/
|
|
12
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
13
|
+
export interface ClientHello {
|
|
14
|
+
t: "hello";
|
|
15
|
+
v: number;
|
|
16
|
+
clientId: string;
|
|
17
|
+
}
|
|
18
|
+
export interface ClientSubscribe {
|
|
19
|
+
t: "subscribe";
|
|
20
|
+
subId: string;
|
|
21
|
+
query: string;
|
|
22
|
+
args: unknown;
|
|
23
|
+
}
|
|
24
|
+
export interface ClientUnsubscribe {
|
|
25
|
+
t: "unsubscribe";
|
|
26
|
+
subId: string;
|
|
27
|
+
}
|
|
28
|
+
export interface ClientMutate {
|
|
29
|
+
t: "mutate";
|
|
30
|
+
mutationId: number;
|
|
31
|
+
name: string;
|
|
32
|
+
args: unknown;
|
|
33
|
+
}
|
|
34
|
+
export type ClientMessage = ClientHello | ClientSubscribe | ClientUnsubscribe | ClientMutate;
|
|
35
|
+
/**
|
|
36
|
+
* A query result rendered on the server for the first paint.
|
|
37
|
+
*
|
|
38
|
+
* Shaped like a snapshot on purpose: the client applies it through the same code
|
|
39
|
+
* path as a streamed one, so hydrated data cannot behave differently from live
|
|
40
|
+
* data.
|
|
41
|
+
*/
|
|
42
|
+
export interface HydrationPayload {
|
|
43
|
+
query: string;
|
|
44
|
+
args: unknown;
|
|
45
|
+
entity: string;
|
|
46
|
+
single?: boolean;
|
|
47
|
+
ids: EntityId[];
|
|
48
|
+
rows: Row[];
|
|
49
|
+
extra?: Record<string, Row[]>;
|
|
50
|
+
/** How the collection is ordered, when the endpoint's own query says so. */
|
|
51
|
+
order?: OrderKey[];
|
|
52
|
+
/** Which rows belong here, when the browser can decide that itself. */
|
|
53
|
+
filter?: RowFilter;
|
|
54
|
+
scope: string | null;
|
|
55
|
+
epoch: number;
|
|
56
|
+
/** The session was not allowed to see this query; `rows` is empty. */
|
|
57
|
+
denied?: boolean;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* One key of a collection's ordering, as the endpoint declared it.
|
|
61
|
+
*
|
|
62
|
+
* Sent so the browser can place a row it has changed locally, before the server
|
|
63
|
+
* has said where it goes. It is derived from the query's own `orderBy` rather
|
|
64
|
+
* than written down a second time — the ordering has one author, and this is a
|
|
65
|
+
* copy of what it said, not a restatement of it.
|
|
66
|
+
*/
|
|
67
|
+
export interface OrderKey {
|
|
68
|
+
field: string;
|
|
69
|
+
dir: "asc" | "desc";
|
|
70
|
+
/**
|
|
71
|
+
* What kind of value this key holds, from the schema.
|
|
72
|
+
*
|
|
73
|
+
* The browser needs it because a value on the wire has lost its type: a
|
|
74
|
+
* timestamp arrives as a string and is indistinguishable from a name. Told
|
|
75
|
+
* that it is a date, the browser compares it as one; told that it is text, it
|
|
76
|
+
* declines to compare it at all, because collation belongs to the database.
|
|
77
|
+
*/
|
|
78
|
+
type: "number" | "date" | "boolean" | "text";
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Which rows belong to a collection, when that is a question the browser can
|
|
82
|
+
* answer for itself.
|
|
83
|
+
*
|
|
84
|
+
* A flat map of field to value — `{ projectId: "archive" }`, or nothing at all
|
|
85
|
+
* for a collection with no filter. It is sent so that a row created *here* can
|
|
86
|
+
* be shown here, before the server has confirmed it: something has to decide
|
|
87
|
+
* whether the new row belongs in this result, and the alternative is an
|
|
88
|
+
* application writing the query's condition out a second time.
|
|
89
|
+
*
|
|
90
|
+
* Absent means "not expressible", not "everything". A `where` with an `OR`, a
|
|
91
|
+
* comparison or a nested relation yields nothing, and a locally created row then
|
|
92
|
+
* waits for the server like it always did — the browser must not guess at
|
|
93
|
+
* membership it cannot compute.
|
|
94
|
+
*/
|
|
95
|
+
export type RowFilter = Record<string, string | number | boolean | null>;
|
|
96
|
+
/** Entity shape the client needs to resolve relations through the identity map. */
|
|
97
|
+
export interface WireEntity {
|
|
98
|
+
idField?: string;
|
|
99
|
+
relations?: Record<string, {
|
|
100
|
+
entity: string;
|
|
101
|
+
localKey?: string;
|
|
102
|
+
}>;
|
|
103
|
+
}
|
|
104
|
+
export interface ServerWelcome {
|
|
105
|
+
t: "welcome";
|
|
106
|
+
v: number;
|
|
107
|
+
/** Highest mutation of this client the server has already processed. */
|
|
108
|
+
lastMutationId: number;
|
|
109
|
+
/**
|
|
110
|
+
* The server's API shape: which endpoints exist and which of them are
|
|
111
|
+
* queries rather than writes.
|
|
112
|
+
*
|
|
113
|
+
* The client cannot know this from its own types — a decorator leaves no
|
|
114
|
+
* trace in a `.d.ts` — and it is what lets a call to a method the server does
|
|
115
|
+
* not have fail immediately instead of hanging on a subscription that will
|
|
116
|
+
* never be answered.
|
|
117
|
+
*/
|
|
118
|
+
api?: {
|
|
119
|
+
contracts: Array<{
|
|
120
|
+
name: string;
|
|
121
|
+
endpoints: Array<{
|
|
122
|
+
method: string;
|
|
123
|
+
kind: string;
|
|
124
|
+
}>;
|
|
125
|
+
}>;
|
|
126
|
+
entities?: Record<string, WireEntity>;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** Full result of a live query. Replaces whatever the client held for `subId`. */
|
|
130
|
+
export interface SubSnapshot {
|
|
131
|
+
subId: string;
|
|
132
|
+
entity: string;
|
|
133
|
+
/**
|
|
134
|
+
* The endpoint returned one row rather than a list.
|
|
135
|
+
*
|
|
136
|
+
* Cardinality is a property of the method's return type, and a decorator
|
|
137
|
+
* leaves no trace the client can read, so the server states it here. Without
|
|
138
|
+
* it `findUnique`-shaped endpoints would resolve to a one-element array.
|
|
139
|
+
*/
|
|
140
|
+
single?: boolean;
|
|
141
|
+
/** Ordered ids of the primary entity, as returned by the query. */
|
|
142
|
+
ids: EntityId[];
|
|
143
|
+
rows: Row[];
|
|
144
|
+
/** Side entities (relation targets) keyed by entity name. */
|
|
145
|
+
extra?: Record<string, Row[]>;
|
|
146
|
+
/**
|
|
147
|
+
* How this result is ordered, when the endpoint's query declared it.
|
|
148
|
+
*
|
|
149
|
+
* Only on the snapshot: an ordering is a property of the query, and a query
|
|
150
|
+
* does not change while a subscription is open.
|
|
151
|
+
*/
|
|
152
|
+
order?: OrderKey[];
|
|
153
|
+
/** Which rows belong here, when the browser can decide that itself. */
|
|
154
|
+
filter?: RowFilter;
|
|
155
|
+
scope: string | null;
|
|
156
|
+
epoch: number;
|
|
157
|
+
reason: SnapshotReason;
|
|
158
|
+
}
|
|
159
|
+
export interface ServerSnapshot extends SubSnapshot {
|
|
160
|
+
t: "snapshot";
|
|
161
|
+
lastMutationId: number;
|
|
162
|
+
}
|
|
163
|
+
export type SnapshotReason = "initial" | "permission-change";
|
|
164
|
+
export interface SubRevoked {
|
|
165
|
+
subId: string;
|
|
166
|
+
reason: string;
|
|
167
|
+
epoch: number;
|
|
168
|
+
}
|
|
169
|
+
export interface SubPatch {
|
|
170
|
+
subId: string;
|
|
171
|
+
entity: string;
|
|
172
|
+
epoch: number;
|
|
173
|
+
/** New ordered id list, sent only when the ordering actually changed. */
|
|
174
|
+
ids?: EntityId[];
|
|
175
|
+
upsert: RowPatch[];
|
|
176
|
+
remove: EntityId[];
|
|
177
|
+
extraUpsert?: Record<string, RowPatch[]>;
|
|
178
|
+
extraRemove?: Record<string, EntityId[]>;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Everything one database transaction did to this client's subscriptions.
|
|
182
|
+
*
|
|
183
|
+
* One message, applied inside one MobX action. A transaction that both changes
|
|
184
|
+
* permissions and data therefore cannot be observed half-applied: the
|
|
185
|
+
* re-snapshotted subscription and the patched one land together.
|
|
186
|
+
*/
|
|
187
|
+
export interface ServerTx {
|
|
188
|
+
t: "tx";
|
|
189
|
+
position: string;
|
|
190
|
+
/**
|
|
191
|
+
* Mutations up to (and including) this id are reflected in the data carried
|
|
192
|
+
* by this message. The client may drop them from its pending queue.
|
|
193
|
+
*/
|
|
194
|
+
lastMutationId: number;
|
|
195
|
+
patches: SubPatch[];
|
|
196
|
+
/** Subscriptions re-snapshotted because their authorization epoch moved. */
|
|
197
|
+
snapshots?: SubSnapshot[];
|
|
198
|
+
/** Subscriptions the client may no longer hold. */
|
|
199
|
+
revoked?: SubRevoked[];
|
|
200
|
+
}
|
|
201
|
+
export interface ServerMutationResult {
|
|
202
|
+
t: "mutation-result";
|
|
203
|
+
mutationId: number;
|
|
204
|
+
ok: boolean;
|
|
205
|
+
error?: string;
|
|
206
|
+
lastMutationId: number;
|
|
207
|
+
}
|
|
208
|
+
export interface ServerError {
|
|
209
|
+
t: "error";
|
|
210
|
+
subId?: string;
|
|
211
|
+
code: string;
|
|
212
|
+
message: string;
|
|
213
|
+
}
|
|
214
|
+
export type ServerMessage = ServerWelcome | ServerSnapshot | ServerTx | ServerMutationResult | ServerError;
|
|
215
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/core/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEvD;;;;;;;;;GASG;AAEH,eAAO,MAAM,gBAAgB,IAAI,CAAC;AAIlC,MAAM,WAAW,WAAW;IACxB,CAAC,EAAE,OAAO,CAAC;IACX,CAAC,EAAE,MAAM,CAAC;IACV,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC5B,CAAC,EAAE,WAAW,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAC9B,CAAC,EAAE,aAAa,CAAC;IACjB,KAAK,EAAE,MA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AAc,GACd,QAAQ,GACR,oBAAoB,GACpB,WAAW,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROTOCOL_VERSION = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Wire protocol between the sync server and the client.
|
|
6
|
+
*
|
|
7
|
+
* Two invariants encoded here:
|
|
8
|
+
* - patches travel **per transaction** (`ServerTx`), never per row, so the
|
|
9
|
+
* client can apply a whole transaction atomically;
|
|
10
|
+
* - every snapshot and every patch carries the authorization `epoch` of its
|
|
11
|
+
* scope, so a client can drop a patch that was computed before a permission
|
|
12
|
+
* change it has already observed.
|
|
13
|
+
*/
|
|
14
|
+
exports.PROTOCOL_VERSION = 1;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one thing a client bundle is allowed to know about a server render.
|
|
3
|
+
*
|
|
4
|
+
* Collecting a page's reads instead of being told them needs the *client*
|
|
5
|
+
* components — the ones that actually call `useQuerySuspense` — to reach the
|
|
6
|
+
* request's open snapshot while Next renders them to HTML. They cannot import it:
|
|
7
|
+
* a `"use client"` module is compiled for the browser too, and an import of
|
|
8
|
+
* anything that reaches `pg` would follow it there.
|
|
9
|
+
*
|
|
10
|
+
* So nothing is imported. A server component puts a reader in this registry and
|
|
11
|
+
* passes its id down as an ordinary string prop; the client component looks the
|
|
12
|
+
* id up and finds either a reader (it is rendering on the server) or nothing (it
|
|
13
|
+
* is in a browser, where the socket does that job). The registry lives on
|
|
14
|
+
* `globalThis` because the two halves are compiled into different bundles and
|
|
15
|
+
* share no module instance.
|
|
16
|
+
*
|
|
17
|
+
* The id is per render, never per user: two requests rendering at once each hold
|
|
18
|
+
* their own reader, bound to their own session and their own snapshot.
|
|
19
|
+
*/
|
|
20
|
+
import type { HydrationPayload, ServerWelcome } from "./protocol";
|
|
21
|
+
/**
|
|
22
|
+
* A page's open snapshot, as much of it as the browser bundle may see.
|
|
23
|
+
*
|
|
24
|
+
* Every method is plain data in and plain data out — no snapshot, no session, no
|
|
25
|
+
* database handle crosses this line.
|
|
26
|
+
*/
|
|
27
|
+
export interface SsrReader {
|
|
28
|
+
/** The API shape, as the socket would announce it in its welcome. */
|
|
29
|
+
manifest(): ServerWelcome["api"] | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Run one query against this render's snapshot.
|
|
32
|
+
*
|
|
33
|
+
* Answers in the shape the socket answers in — including a refusal, which is
|
|
34
|
+
* a payload marked `denied` rather than a rejection, because a page that may
|
|
35
|
+
* not see one of its panels still renders the rest.
|
|
36
|
+
*/
|
|
37
|
+
read(query: string, args: unknown): Promise<HydrationPayload>;
|
|
38
|
+
/**
|
|
39
|
+
* Everything read so far, once the render has stopped asking for more.
|
|
40
|
+
*
|
|
41
|
+
* Resolves when no query is in flight and none has started for a turn of the
|
|
42
|
+
* event loop — the render has gone quiet. It is a heuristic and it is allowed
|
|
43
|
+
* to be: a payload that misses the cut is not lost data, it is one query the
|
|
44
|
+
* browser asks the socket for.
|
|
45
|
+
*/
|
|
46
|
+
collected(): Promise<HydrationPayload[]>;
|
|
47
|
+
}
|
|
48
|
+
export declare function registerSsrReader(id: string, reader: SsrReader): void;
|
|
49
|
+
export declare function forgetSsrReader(id: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* The reader for this render, or null in a browser.
|
|
52
|
+
*
|
|
53
|
+
* Null is not a failure: it is the ordinary answer everywhere except inside a
|
|
54
|
+
* server render, and it is what tells the client half to use the socket.
|
|
55
|
+
*/
|
|
56
|
+
export declare function ssrReader(id: string | undefined): SsrReader | null;
|
|
57
|
+
/** Where the inline script leaves its payloads for the browser to pick up. */
|
|
58
|
+
export declare const SSR_PAYLOAD_GLOBAL = "__NOWITY_SSR__";
|
|
59
|
+
/**
|
|
60
|
+
* A read that arrived after the render was declared finished.
|
|
61
|
+
*
|
|
62
|
+
* Quiet is a heuristic, so this is a race that can happen: a component asks for a
|
|
63
|
+
* query a turn later than the collector waited. It is not an error anyone should
|
|
64
|
+
* see — the query simply stays unanswered here and the browser opens it on the
|
|
65
|
+
* socket like any other — so it is marked, and the transport says nothing rather
|
|
66
|
+
* than reporting a failure the page would have to render.
|
|
67
|
+
*/
|
|
68
|
+
export declare const SSR_CLOSED = "nowity/ssr-closed";
|
|
69
|
+
//# sourceMappingURL=ssr-bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssr-bridge.d.ts","sourceRoot":"","sources":["../../src/core/ssr-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAElE;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACtB,qEAAqE;IACrE,QAAQ,IAAI,aAAa,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAC7C;;;;;;OAMG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC9D;;;;;;;OAOG;IACH,SAAS,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAAC;CAC5C;AASD,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,GAAG,IAAI,CAErE;AAED,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAEhD;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,IAAI,CAGlE;AAED,8EAA8E;AAC9E,eAAO,MAAM,kBAAkB,mBAAmB,CAAC;AAEnD;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SSR_CLOSED = exports.SSR_PAYLOAD_GLOBAL = void 0;
|
|
4
|
+
exports.registerSsrReader = registerSsrReader;
|
|
5
|
+
exports.forgetSsrReader = forgetSsrReader;
|
|
6
|
+
exports.ssrReader = ssrReader;
|
|
7
|
+
const REGISTRY = Symbol.for("nowity.ssrReaders");
|
|
8
|
+
function registry() {
|
|
9
|
+
const global = globalThis;
|
|
10
|
+
return (global[REGISTRY] ?? (global[REGISTRY] = new Map()));
|
|
11
|
+
}
|
|
12
|
+
function registerSsrReader(id, reader) {
|
|
13
|
+
registry().set(id, reader);
|
|
14
|
+
}
|
|
15
|
+
function forgetSsrReader(id) {
|
|
16
|
+
registry().delete(id);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The reader for this render, or null in a browser.
|
|
20
|
+
*
|
|
21
|
+
* Null is not a failure: it is the ordinary answer everywhere except inside a
|
|
22
|
+
* server render, and it is what tells the client half to use the socket.
|
|
23
|
+
*/
|
|
24
|
+
function ssrReader(id) {
|
|
25
|
+
if (!id || typeof window !== "undefined")
|
|
26
|
+
return null;
|
|
27
|
+
return registry().get(id) ?? null;
|
|
28
|
+
}
|
|
29
|
+
/** Where the inline script leaves its payloads for the browser to pick up. */
|
|
30
|
+
exports.SSR_PAYLOAD_GLOBAL = "__NOWITY_SSR__";
|
|
31
|
+
/**
|
|
32
|
+
* A read that arrived after the render was declared finished.
|
|
33
|
+
*
|
|
34
|
+
* Quiet is a heuristic, so this is a race that can happen: a component asks for a
|
|
35
|
+
* query a turn later than the collector waited. It is not an error anyone should
|
|
36
|
+
* see — the query simply stays unanswered here and the browser opens it on the
|
|
37
|
+
* socket like any other — so it is marked, and the transport says nothing rather
|
|
38
|
+
* than reporting a failure the page would have to render.
|
|
39
|
+
*/
|
|
40
|
+
exports.SSR_CLOSED = "nowity/ssr-closed";
|