@pgxsinkit/server 0.6.1 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/circuits/compile.d.ts +1 -1
- package/dist/circuits/compile.d.ts.map +1 -1
- package/dist/circuits/edge.d.ts.map +1 -1
- package/dist/circuits/subscribe.d.ts +16 -0
- package/dist/circuits/subscribe.d.ts.map +1 -1
- package/dist/events/consumer.d.ts.map +1 -1
- package/dist/events/pgmq-queue.d.ts.map +1 -1
- package/dist/events/route.d.ts.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +94 -24
- package/dist/index.js.map +12 -12
- package/dist/mutations/plpgsql-apply.d.ts.map +1 -1
- package/dist/mutations/route.d.ts.map +1 -1
- package/dist/operations-log/writer.d.ts +1 -1
- package/dist/operations-log/writer.d.ts.map +1 -1
- package/package.json +2 -2
- package/skills/deploying/SKILL.md +1 -1
- package/src/circuits/compile.ts +21 -24
- package/src/circuits/edge.ts +1 -1
- package/src/circuits/subscribe.ts +109 -8
- package/src/cli/generate.ts +1 -1
- package/src/events/consumer.ts +2 -2
- package/src/events/pgmq-queue.ts +3 -3
- package/src/events/route.ts +5 -5
- package/src/index.ts +13 -2
- package/src/mutations/plpgsql-apply.ts +4 -4
- package/src/mutations/route.ts +4 -4
- package/src/operations-log/writer.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plpgsql-apply.d.ts","sources":["../../src/mutations/plpgsql-apply.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,KAAK,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGnH,OAAO,
|
|
1
|
+
{"version":3,"file":"plpgsql-apply.d.ts","sources":["../../src/mutations/plpgsql-apply.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,KAAK,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGnH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,oBAAoB,EAC3B,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,OAAO,EACnB,UAAU,EAAE,OAAO,EACnB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAKnC,mBAAmB,EAAE,MAAM,EAC3B,OAAO,GAAE;IACP,cAAc,CAAC,EAAE,MAAM,CAAC;CACpB,GACL,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA4B7B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sources":["../../src/mutations/route.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"route.d.ts","sources":["../../src/mutations/route.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,qBAAqB,CAAC;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAI7E,OAAO,KAAK,EAEV,oBAAoB,EACpB,SAAS,EAGT,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EAClB,MAAM,sBAAsB,CAAC;AAU9B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAEnE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAY9C;;;;;;;GAOG;AACH,MAAM,MAAM,uBAAuB,GAAG,YAAY,GAAG,aAAa,CAAC;AAEnE,QAAA,MAAM,EAAE,kBAAkB,EAAE,0BAA0B,EAAE;;;;;;;;;;;;;CAItD,CAAC;AAIH,eAAO,MAAM,kBAAkB,YAAI,gBAAgB,CAAU,CAAC;AAC9D,oGAAoG;AACpG,eAAO,MAAM,0BAA0B,YAAI,qBAAqB,CAAU,CAAC;AAE3E,wBAAgB,qBAAqB,CAAC,SAAS,SAAS,iBAAiB,EACvE,EAAE,EAAE,eAAe,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,SAAS,CAAC,CAAC,EACnE,QAAQ,EAAE,SAAS,EACnB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,SAAS,CAAC,CAAC,CAUlF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OperationsLogConfig, OpsLogEntry, SqlExecutor } from "./types";
|
|
2
2
|
export declare function logOperation(executor: SqlExecutor, config: OperationsLogConfig, entry: OpsLogEntry): Promise<void>;
|
|
3
3
|
export declare function logOperationSafely(executor: SqlExecutor, config: OperationsLogConfig, entry: OpsLogEntry): Promise<void>;
|
|
4
4
|
//# sourceMappingURL=writer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writer.d.ts","sources":["../../src/operations-log/writer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"writer.d.ts","sources":["../../src/operations-log/writer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAkB7E,wBAAsB,YAAY,CAChC,QAAQ,EAAE,WAAW,EACrB,MAAM,EAAE,mBAAmB,EAC3B,KAAK,EAAE,WAAW,GACjB,OAAO,CAAC,IAAI,CAAC,CAmBf;AAED,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,WAAW,EACrB,MAAM,EAAE,mBAAmB,EAC3B,KAAK,EAAE,WAAW,GACjB,OAAO,CAAC,IAAI,CAAC,CAMf"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgxsinkit/server",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"tanstack-intent"
|
|
6
6
|
],
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"build": "bun --cwd ../.. run build:public-packages"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@pgxsinkit/contracts": "0.6.
|
|
35
|
+
"@pgxsinkit/contracts": "0.6.3"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"drizzle-orm": "rc"
|
package/src/circuits/compile.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getTableName, getTableSchema } from "drizzle-orm";
|
|
1
2
|
import { getTableConfig } from "drizzle-orm/pg-core";
|
|
2
3
|
|
|
3
4
|
import {
|
|
@@ -6,10 +7,10 @@ import {
|
|
|
6
7
|
isAndPredicate,
|
|
7
8
|
isIsNullPredicate,
|
|
8
9
|
isLeafPredicate,
|
|
9
|
-
readShapeTier,
|
|
10
10
|
type JwtClaims,
|
|
11
11
|
type Predicate,
|
|
12
12
|
type PredicateValue,
|
|
13
|
+
readShapeTier,
|
|
13
14
|
type ShapeSpec,
|
|
14
15
|
type SyncTableEntry,
|
|
15
16
|
type SyncTableRegistry,
|
|
@@ -46,30 +47,29 @@ export type CompiledShapeRequest =
|
|
|
46
47
|
* Resolve the entry a request selects by its **`shapeKey`** — not by table name, since several
|
|
47
48
|
* shapes (a read projection and its owner) legitimately read one physical table under distinct keys.
|
|
48
49
|
* Schema qualification is significant. Returns undefined when nothing declares that key, and the
|
|
49
|
-
* caller fails closed.
|
|
50
|
+
* caller fails closed. An ambiguous key is a registry configuration error.
|
|
50
51
|
*/
|
|
51
52
|
export function resolveEntryByShapeKey(registry: SyncTableRegistry, shapeKey: string): SyncTableEntry | undefined {
|
|
52
|
-
|
|
53
|
+
const matches = Object.values(registry).filter((entry) => entry?.shape?.shapeKey === shapeKey);
|
|
54
|
+
if (matches.length > 1) {
|
|
55
|
+
throw new Error(`shapeKey "${shapeKey}" is declared by multiple registry entries`);
|
|
56
|
+
}
|
|
57
|
+
return matches[0];
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
/**
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
*/
|
|
63
|
-
function resolveShapeTarget(shape: ShapeSpec): { table: string } | { refusal: string } {
|
|
60
|
+
/** The physical table the engine reads. Keep public requests in their existing fingerprint form. */
|
|
61
|
+
function resolveShapeTarget(entry: SyncTableEntry, shape: ShapeSpec): string {
|
|
62
|
+
const schema = getTableSchema(entry.table);
|
|
63
|
+
// A dot in a Drizzle identifier is literal but the native API treats it as a table separator.
|
|
64
|
+
if (getTableName(entry.table).includes(".") || schema?.includes(".")) {
|
|
65
|
+
throw new Error(`shape "${shape.shapeKey}" has a dot in a Postgres schema or table identifier`);
|
|
66
|
+
}
|
|
64
67
|
const target = shape.physicalTable ?? shape.tableName;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
`shape "${shape.shapeKey}" reads "${target}", but the engine introspects the public schema ` +
|
|
71
|
-
`only and names tables bare — a non-public target has no shape it can be created under.`,
|
|
72
|
-
};
|
|
68
|
+
const separator = target.indexOf(".");
|
|
69
|
+
if (separator !== -1) {
|
|
70
|
+
return target.startsWith("public.") && target.lastIndexOf(".") === separator ? target.slice(separator + 1) : target;
|
|
71
|
+
}
|
|
72
|
+
return schema === undefined || schema === "public" ? target : `${schema}.${target}`;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -110,9 +110,6 @@ export function compileShapeRequest(registry: SyncTableRegistry, request: ShapeR
|
|
|
110
110
|
return { outcome: "deny", reason: `no shape declares shapeKey "${request.shapeKey}"` };
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
const target = resolveShapeTarget(shape);
|
|
114
|
-
if ("refusal" in target) return { outcome: "deny", reason: target.refusal };
|
|
115
|
-
|
|
116
113
|
const tier = readShapeTier(shape);
|
|
117
114
|
const subject =
|
|
118
115
|
tier === "shared" ? scopePredicate(shape, request.scope) : privatePredicate(shape, request.claims, request.params);
|
|
@@ -129,7 +126,7 @@ export function compileShapeRequest(registry: SyncTableRegistry, request: ShapeR
|
|
|
129
126
|
outcome: "create",
|
|
130
127
|
tier,
|
|
131
128
|
request: {
|
|
132
|
-
table:
|
|
129
|
+
table: resolveShapeTarget(entry, shape),
|
|
133
130
|
...(where !== undefined ? { where } : {}),
|
|
134
131
|
...(columns !== undefined ? { columns } : {}),
|
|
135
132
|
},
|
package/src/circuits/edge.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { getTableConfig } from "drizzle-orm/pg-core";
|
|
|
3
3
|
import type { PredicateValue, RowTransform, RowTransformContext, SyncTableRegistry } from "@pgxsinkit/contracts";
|
|
4
4
|
|
|
5
5
|
import { resolveEntryByShapeKey } from "./compile";
|
|
6
|
-
import { findGrant,
|
|
6
|
+
import { findGrant, type StreamGrant, verifyStreamToken } from "./stream-token";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* The live entitlement set the edge checks a shared-tier read against (ADR-0055 decision 7).
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
|
-
readShapeTier,
|
|
3
2
|
type JwtClaims,
|
|
4
3
|
type PredicateValue,
|
|
4
|
+
readShapeTier,
|
|
5
5
|
type SyncTableEntry,
|
|
6
6
|
type SyncTableRegistry,
|
|
7
7
|
} from "@pgxsinkit/contracts";
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
|
+
type CompiledShapeRequest,
|
|
10
11
|
compileShapeRequest,
|
|
11
12
|
fingerprintShapeRequest,
|
|
12
13
|
resolveEntryByShapeKey,
|
|
13
|
-
type CompiledShapeRequest,
|
|
14
14
|
} from "./compile";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { DEFAULT_STREAM_TOKEN_TTL_SECONDS, mintStreamToken,
|
|
15
|
+
import { type EntitlementSet, EntitlementsUnavailableError } from "./edge";
|
|
16
|
+
import { type CircuitsEngineClient, CircuitsEngineError } from "./engine-client";
|
|
17
|
+
import { DEFAULT_STREAM_TOKEN_TTL_SECONDS, mintStreamToken, type StreamGrant, verifyStreamToken } from "./stream-token";
|
|
18
18
|
import type { CircuitsShapeHandle, CreateShapeRequest } from "./wire";
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -55,6 +55,28 @@ export interface SubscribeResult {
|
|
|
55
55
|
denied: DeniedSubscription[];
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** Operator context for a shape-registration refusal at subscribe. */
|
|
59
|
+
export interface SubscribeEngineErrorDiagnostic {
|
|
60
|
+
/** HTTP status from the engine. The subscribe response itself is a 503. */
|
|
61
|
+
status: number;
|
|
62
|
+
reason:
|
|
63
|
+
| "unknown_table"
|
|
64
|
+
| "unknown_column"
|
|
65
|
+
| "engine_booting"
|
|
66
|
+
| "engine_degraded"
|
|
67
|
+
| "request_refused"
|
|
68
|
+
| "engine_unavailable";
|
|
69
|
+
/** Registry key and configured source table of the failed registration. */
|
|
70
|
+
shapeKey: string;
|
|
71
|
+
table: string;
|
|
72
|
+
/** Registered keys among the first 16 requests, deduplicated. */
|
|
73
|
+
batchShapeKeys: readonly string[];
|
|
74
|
+
batchSize: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Reports sanitized operator context. An asynchronous report does not delay the response. */
|
|
78
|
+
export type SubscribeEngineErrorReporter = (diagnostic: SubscribeEngineErrorDiagnostic) => void | Promise<void>;
|
|
79
|
+
|
|
58
80
|
export interface SubscribeOptions {
|
|
59
81
|
registry: SyncTableRegistry;
|
|
60
82
|
engine: CircuitsEngineClient;
|
|
@@ -73,6 +95,70 @@ export interface SubscribeOptions {
|
|
|
73
95
|
ttlSeconds?: number;
|
|
74
96
|
/** Deployment-supplied runtime params, handed to `rowFilter.customPredicate` as its second argument. */
|
|
75
97
|
params?: Record<string, unknown>;
|
|
98
|
+
/** Reports engine refusals. The HTTP handler defaults this to `console.error`. */
|
|
99
|
+
onSubscribeEngineError?: SubscribeEngineErrorReporter;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function subscribeEngineRefusalReason(error: CircuitsEngineError): SubscribeEngineErrorDiagnostic["reason"] {
|
|
103
|
+
// Engine errors can quote predicate values. Classify a small JSON body using fixed reasons and
|
|
104
|
+
// keep every byte from the response itself out of the diagnostic.
|
|
105
|
+
if ((error.status === 400 || error.status === 503) && error.body.length <= 1024) {
|
|
106
|
+
try {
|
|
107
|
+
const body = JSON.parse(error.body) as { error?: unknown } | null;
|
|
108
|
+
if (typeof body?.error === "string") {
|
|
109
|
+
if (error.status === 400) {
|
|
110
|
+
if (body.error.startsWith("unknown table '")) return "unknown_table";
|
|
111
|
+
if (body.error.startsWith("unknown column '")) return "unknown_column";
|
|
112
|
+
} else {
|
|
113
|
+
if (body.error.startsWith("engine is still booting")) return "engine_booting";
|
|
114
|
+
if (body.error.startsWith("degraded: subquery membership effects were lost")) return "engine_degraded";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} catch {
|
|
118
|
+
// Upstream proxies can return non-JSON responses. The status is still useful.
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return error.status >= 400 && error.status < 500 ? "request_refused" : "engine_unavailable";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function boundedDiagnosticLabel(value: string): string {
|
|
125
|
+
const limit = 128;
|
|
126
|
+
const label = value.slice(0, limit).replace(/[\p{C}\p{Zl}\p{Zp}]/gu, "?");
|
|
127
|
+
return value.length > limit ? `${label}…` : label;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function reportSubscribeEngineError(
|
|
131
|
+
options: SubscribeOptions,
|
|
132
|
+
error: CircuitsEngineError,
|
|
133
|
+
shapeKey: string,
|
|
134
|
+
table: string,
|
|
135
|
+
batch: readonly SubscriptionRequest[],
|
|
136
|
+
): void {
|
|
137
|
+
const report = options.onSubscribeEngineError;
|
|
138
|
+
if (!report) return;
|
|
139
|
+
try {
|
|
140
|
+
const entries = Object.values(options.registry);
|
|
141
|
+
const keys = new Set<string>();
|
|
142
|
+
for (const request of batch.slice(0, 16)) {
|
|
143
|
+
if (
|
|
144
|
+
typeof request?.shapeKey === "string" &&
|
|
145
|
+
entries.some((entry) => entry?.shape?.shapeKey === request.shapeKey)
|
|
146
|
+
) {
|
|
147
|
+
keys.add(boundedDiagnosticLabel(request.shapeKey));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const diagnostic: SubscribeEngineErrorDiagnostic = {
|
|
151
|
+
status: error.status,
|
|
152
|
+
reason: subscribeEngineRefusalReason(error),
|
|
153
|
+
shapeKey: boundedDiagnosticLabel(shapeKey),
|
|
154
|
+
table: boundedDiagnosticLabel(table),
|
|
155
|
+
batchShapeKeys: [...keys],
|
|
156
|
+
batchSize: batch.length,
|
|
157
|
+
};
|
|
158
|
+
void Promise.resolve(report(diagnostic)).catch(() => {});
|
|
159
|
+
} catch {
|
|
160
|
+
// Diagnostics must preserve the retryable response even if the host reporter throws.
|
|
161
|
+
}
|
|
76
162
|
}
|
|
77
163
|
|
|
78
164
|
/**
|
|
@@ -339,8 +425,15 @@ export async function subscribeToShapes(
|
|
|
339
425
|
// two joins, and one id could only ever release one of them.
|
|
340
426
|
const claim = crypto.randomUUID();
|
|
341
427
|
|
|
342
|
-
|
|
343
|
-
|
|
428
|
+
let handle: CircuitsShapeHandle;
|
|
429
|
+
try {
|
|
430
|
+
handle = await options.engine.createShape({ ...compiled.request, subscription: claim });
|
|
431
|
+
} catch (error) {
|
|
432
|
+
if (error instanceof CircuitsEngineError) {
|
|
433
|
+
reportSubscribeEngineError(options, error, request.shapeKey, compiled.request.table, requests);
|
|
434
|
+
}
|
|
435
|
+
throw error;
|
|
436
|
+
}
|
|
344
437
|
assertClaimRecorded(handle, claim);
|
|
345
438
|
// Checked per create rather than once at boot: the lease window is the engine's to report, and
|
|
346
439
|
// this is the only place it is reported. A misconfigured pairing must surface as a 503 on the
|
|
@@ -390,6 +483,14 @@ export function createSubscribeHandler(
|
|
|
390
483
|
resolveAuthClaims?: (request: Request) => Promise<JwtClaims | null> | JwtClaims | null;
|
|
391
484
|
},
|
|
392
485
|
) {
|
|
486
|
+
const subscribeOptions: SubscribeOptions = {
|
|
487
|
+
...options,
|
|
488
|
+
onSubscribeEngineError:
|
|
489
|
+
options.onSubscribeEngineError ??
|
|
490
|
+
((diagnostic) => {
|
|
491
|
+
console.error("[pgxsinkit] subscribe shape registration failed", diagnostic);
|
|
492
|
+
}),
|
|
493
|
+
};
|
|
393
494
|
return async function handleSubscribe(request: Request): Promise<Response> {
|
|
394
495
|
let body: { subscriptions?: SubscriptionRequest[] };
|
|
395
496
|
try {
|
|
@@ -418,7 +519,7 @@ export function createSubscribeHandler(
|
|
|
418
519
|
|
|
419
520
|
let result: SubscribeResult;
|
|
420
521
|
try {
|
|
421
|
-
result = await subscribeToShapes(
|
|
522
|
+
result = await subscribeToShapes(subscribeOptions, claims, body.subscriptions, Math.floor(Date.now() / 1000));
|
|
422
523
|
} catch (error) {
|
|
423
524
|
// An engine that could not answer is an OUTAGE, and 503 is what keeps the client retrying.
|
|
424
525
|
// Relaying it as a per-subscription denial would tell the client it lost entitlement, and a
|
package/src/cli/generate.ts
CHANGED
|
@@ -35,9 +35,9 @@ import type { SyncTableRegistry } from "@pgxsinkit/contracts";
|
|
|
35
35
|
import { eventLaneDdlFingerprint, eventLaneStreamNames, renderEventLaneMigration } from "../events/ddl";
|
|
36
36
|
import { renderPgxsinkitUtilitiesMigration } from "../migrations/utilities";
|
|
37
37
|
import {
|
|
38
|
+
type ApplyFunctionRenderOptions,
|
|
38
39
|
buildPlpgsqlBatchFunctionDdl,
|
|
39
40
|
expectedApplyFingerprint,
|
|
40
|
-
type ApplyFunctionRenderOptions,
|
|
41
41
|
} from "../mutations/plpgsql-apply";
|
|
42
42
|
|
|
43
43
|
/** Exported for direct unit coverage of the flag surface (repeatable/comma-separated `--grant-execute-to`). */
|
package/src/events/consumer.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
-
getSyncRegistryStreams,
|
|
3
2
|
type EventQueueMessage,
|
|
3
|
+
getSyncRegistryStreams,
|
|
4
4
|
type StampedEvent,
|
|
5
5
|
type SyncTableRegistry,
|
|
6
6
|
} from "@pgxsinkit/contracts";
|
|
7
7
|
|
|
8
8
|
import {
|
|
9
|
-
MalformedEventQueueMessageError,
|
|
10
9
|
type DeliveredEventMessage,
|
|
11
10
|
type EventQueue,
|
|
12
11
|
type EventQueueReceipt,
|
|
12
|
+
MalformedEventQueueMessageError,
|
|
13
13
|
} from "./queue";
|
|
14
14
|
|
|
15
15
|
/**
|
package/src/events/pgmq-queue.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { sql } from "drizzle-orm";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { type EventQueueMessage, eventQueueMessageSchema } from "@pgxsinkit/contracts";
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
assertEventQueueReceipts,
|
|
7
|
-
eventStreamQueueName,
|
|
8
|
-
MalformedEventQueueMessageError,
|
|
9
7
|
type DeadLetteredEventMessage,
|
|
10
8
|
type DeliveredEventMessage,
|
|
11
9
|
type EventQueue,
|
|
12
10
|
type EventQueueExecutor,
|
|
13
11
|
type EventQueueReadOptions,
|
|
14
12
|
type EventQueueReceipt,
|
|
13
|
+
eventStreamQueueName,
|
|
14
|
+
MalformedEventQueueMessageError,
|
|
15
15
|
} from "./queue";
|
|
16
16
|
|
|
17
17
|
/**
|
package/src/events/route.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
batchEventRequestSchema,
|
|
3
|
-
getSyncRegistryStreams,
|
|
4
|
-
isZodSchema,
|
|
5
|
-
MAX_EVENT_PAYLOAD_BYTES,
|
|
6
|
-
MAX_EVENT_REQUEST_BYTES,
|
|
7
|
-
MAX_EVENTS_PER_BATCH,
|
|
8
3
|
type EventAck,
|
|
9
4
|
type EventEnvelope,
|
|
10
5
|
type EventQueueMessage,
|
|
11
6
|
type EventStreamRegistry,
|
|
7
|
+
getSyncRegistryStreams,
|
|
8
|
+
isZodSchema,
|
|
12
9
|
type JwtClaims,
|
|
10
|
+
MAX_EVENT_PAYLOAD_BYTES,
|
|
11
|
+
MAX_EVENT_REQUEST_BYTES,
|
|
12
|
+
MAX_EVENTS_PER_BATCH,
|
|
13
13
|
type StampedEvent,
|
|
14
14
|
type SyncTableRegistry,
|
|
15
15
|
} from "@pgxsinkit/contracts";
|
package/src/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
createSubscribeHandler,
|
|
21
21
|
refreshPath,
|
|
22
22
|
releasePath,
|
|
23
|
+
type SubscribeEngineErrorReporter,
|
|
23
24
|
subscribePath,
|
|
24
25
|
} from "./circuits/subscribe";
|
|
25
26
|
import { createPgmqEventQueue } from "./events/pgmq-queue";
|
|
@@ -33,7 +34,7 @@ import {
|
|
|
33
34
|
} from "./mutations/route";
|
|
34
35
|
import { ensureOperationsLogSchema } from "./operations-log/ddl";
|
|
35
36
|
import type { OperationsLogConfig } from "./operations-log/types";
|
|
36
|
-
import {
|
|
37
|
+
import { type CorsScope, FetchRouter } from "./router";
|
|
37
38
|
|
|
38
39
|
const defaultAllowedOrigins = ["http://localhost:5173", "http://localhost:5174"];
|
|
39
40
|
|
|
@@ -111,6 +112,8 @@ export interface CreateSyncServerOptions<
|
|
|
111
112
|
barrierMaxAgeSeconds?: number;
|
|
112
113
|
/** Optional per-request extra params passed to the private tier's row filters. */
|
|
113
114
|
resolveShapeParams?: (request: Request) => Record<string, unknown> | undefined;
|
|
115
|
+
/** Reports sanitized shape-registration refusals from subscribe. Defaults to `console.error`. */
|
|
116
|
+
onSubscribeEngineError?: SubscribeEngineErrorReporter;
|
|
114
117
|
};
|
|
115
118
|
operationsLog?: {
|
|
116
119
|
enabled?: boolean;
|
|
@@ -346,7 +349,13 @@ export function createSyncServer<
|
|
|
346
349
|
resolveAuthClaims,
|
|
347
350
|
};
|
|
348
351
|
|
|
349
|
-
router.post(
|
|
352
|
+
router.post(
|
|
353
|
+
subscribePath,
|
|
354
|
+
createSubscribeHandler({
|
|
355
|
+
...subscribeOptions,
|
|
356
|
+
...(readPath.onSubscribeEngineError ? { onSubscribeEngineError: readPath.onSubscribeEngineError } : {}),
|
|
357
|
+
}),
|
|
358
|
+
);
|
|
350
359
|
router.post(refreshPath, createRefreshHandler(subscribeOptions));
|
|
351
360
|
// The close half of subscribe. Every grant subscribe handed out is a named claim on an engine
|
|
352
361
|
// shape, and a live claim blocks dormancy and eviction — so without this route every shape waits
|
|
@@ -570,6 +579,8 @@ export type {
|
|
|
570
579
|
DeniedSubscription,
|
|
571
580
|
GrantedSubscription,
|
|
572
581
|
RefreshResult,
|
|
582
|
+
SubscribeEngineErrorDiagnostic,
|
|
583
|
+
SubscribeEngineErrorReporter,
|
|
573
584
|
SubscribeOptions,
|
|
574
585
|
SubscribeResult,
|
|
575
586
|
SubscriptionRequest,
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { getTableName, sql } from "drizzle-orm";
|
|
2
|
-
import {
|
|
2
|
+
import { type AnyPgTable, getTableConfig, type PgAsyncDatabase, type PgQueryResultHKT } from "drizzle-orm/pg-core";
|
|
3
3
|
import { getColumns } from "drizzle-orm/utils";
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
+
type BatchMutationRequest,
|
|
6
7
|
buildRoleGuardedStatement,
|
|
7
8
|
CLOCK_US_CALL_SQL_TEXT,
|
|
8
9
|
escapeSqlLiteral as toSqlLiteral,
|
|
9
10
|
getProjectedColumns,
|
|
10
11
|
hashString,
|
|
11
12
|
isManagedFieldGuarded,
|
|
12
|
-
quoteIdentifier as quoteIdent,
|
|
13
|
-
resolveServerVersionColumnName,
|
|
14
|
-
type BatchMutationRequest,
|
|
15
13
|
type ManagedFieldApplyOn,
|
|
14
|
+
quoteIdentifier as quoteIdent,
|
|
16
15
|
type RegistryRelations,
|
|
16
|
+
resolveServerVersionColumnName,
|
|
17
17
|
type SyncTableEntry,
|
|
18
18
|
type SyncTableRegistry,
|
|
19
19
|
} from "@pgxsinkit/contracts";
|
package/src/mutations/route.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { and, eq, sql
|
|
2
|
-
import {
|
|
1
|
+
import { and, type AnyRelations, eq, sql } from "drizzle-orm";
|
|
2
|
+
import { type AnyPgTable, getTableConfig } from "drizzle-orm/pg-core";
|
|
3
3
|
import type { PgAsyncDatabase, PgQueryResultHKT } from "drizzle-orm/pg-core";
|
|
4
4
|
import { getColumns } from "drizzle-orm/utils";
|
|
5
5
|
import { createSchemaFactory } from "drizzle-orm/zod";
|
|
@@ -28,11 +28,11 @@ import { logOperation, logOperationSafely } from "../operations-log/writer";
|
|
|
28
28
|
import type { FetchHandler } from "../router";
|
|
29
29
|
import { readSqlState } from "../sql-state";
|
|
30
30
|
import {
|
|
31
|
+
type ApplyFunctionRenderOptions,
|
|
31
32
|
executePlpgsqlBatch,
|
|
32
33
|
expectedApplyFingerprint,
|
|
33
|
-
resolveApplyFunctionSchema,
|
|
34
|
-
type ApplyFunctionRenderOptions,
|
|
35
34
|
type MutationConflict,
|
|
35
|
+
resolveApplyFunctionSchema,
|
|
36
36
|
verifyRlsAuthHelpers,
|
|
37
37
|
} from "./plpgsql-apply";
|
|
38
38
|
import type { TransactionClient } from "./types";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { operationsLogTable } from "./schema";
|
|
2
|
-
import type {
|
|
2
|
+
import type { OperationsLogConfig, OpsLogEntry, SqlExecutor } from "./types";
|
|
3
3
|
|
|
4
4
|
function toJsonValue(value: unknown): unknown {
|
|
5
5
|
if (value === undefined) {
|