@type-bridge/node 1.5.5 → 1.5.7

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.
@@ -0,0 +1,363 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypedReferenceError = exports.TypedMatchError = void 0;
4
+ exports.references = references;
5
+ exports.nativeCall = nativeCall;
6
+ exports.createBoundVar = createBoundVar;
7
+ exports.nativeSelectionHandle = nativeSelectionHandle;
8
+ exports.nativeBindingHandle = nativeBindingHandle;
9
+ exports.nativePredicateHandle = nativePredicateHandle;
10
+ exports.nativeQueryOrderHandle = nativeQueryOrderHandle;
11
+ const codec_js_1 = require("../codec.js");
12
+ const model_js_1 = require("../model.js");
13
+ const fieldReferenceStates = new WeakMap();
14
+ const roleReferenceStates = new WeakMap();
15
+ class FieldReferenceImpl {
16
+ constructor(state) {
17
+ fieldReferenceStates.set(this, state);
18
+ Object.freeze(this);
19
+ }
20
+ }
21
+ class RoleReferenceImpl {
22
+ constructor(state) {
23
+ roleReferenceStates.set(this, state);
24
+ Object.freeze(this);
25
+ }
26
+ }
27
+ /**
28
+ * Derive field/role references from a model's typed schema without accepting a
29
+ * public field or role name. Generated packages can re-export these exact
30
+ * values as model-specific constants.
31
+ */
32
+ function references(model) {
33
+ const fields = Object.create(null);
34
+ const roles = Object.create(null);
35
+ for (const [name, spec] of Object.entries(model.schema)) {
36
+ if (spec instanceof model_js_1.FieldSpec || spec instanceof model_js_1.ListFieldSpec) {
37
+ fields[name] = new FieldReferenceImpl({
38
+ owner: model,
39
+ ownerTypeName: model.typeName,
40
+ name,
41
+ spec,
42
+ attrType: spec.attrType,
43
+ });
44
+ }
45
+ else if (spec instanceof model_js_1.RoleSpec) {
46
+ roles[name] = new RoleReferenceImpl({
47
+ owner: model,
48
+ ownerTypeName: model.typeName,
49
+ name,
50
+ spec,
51
+ });
52
+ }
53
+ }
54
+ return Object.freeze({
55
+ fields: Object.freeze(fields),
56
+ roles: Object.freeze(roles),
57
+ });
58
+ }
59
+ class TypedMatchError extends Error {
60
+ category;
61
+ code;
62
+ path;
63
+ details;
64
+ name = "TypedMatchError";
65
+ constructor(category, code, message, path, details) {
66
+ super(message);
67
+ this.category = category;
68
+ this.code = code;
69
+ this.path = path;
70
+ this.details = details;
71
+ }
72
+ }
73
+ exports.TypedMatchError = TypedMatchError;
74
+ function isNativeMatchPayload(value) {
75
+ return ("category" in value &&
76
+ typeof value.category === "string" &&
77
+ "code" in value &&
78
+ typeof value.code === "string" &&
79
+ "message" in value &&
80
+ typeof value.message === "string" &&
81
+ "path" in value &&
82
+ Array.isArray(value.path) &&
83
+ "details" in value &&
84
+ typeof value.details === "object" &&
85
+ value.details !== null);
86
+ }
87
+ /** @internal Invoke one native transition and preserve structured match errors. */
88
+ function nativeCall(operation) {
89
+ try {
90
+ return operation();
91
+ }
92
+ catch (error) {
93
+ if (error instanceof Error) {
94
+ try {
95
+ const payload = JSON.parse(error.message);
96
+ if (isNativeMatchPayload(payload)) {
97
+ throw new TypedMatchError(payload.category, payload.code, payload.message, payload.path, payload.details);
98
+ }
99
+ }
100
+ catch (payloadError) {
101
+ if (payloadError instanceof TypedMatchError) {
102
+ throw payloadError;
103
+ }
104
+ }
105
+ }
106
+ throw error;
107
+ }
108
+ }
109
+ class TypedReferenceError extends TypeError {
110
+ name = "TypedReferenceError";
111
+ }
112
+ exports.TypedReferenceError = TypedReferenceError;
113
+ const boundVariableStates = new WeakMap();
114
+ const collectedStates = new WeakMap();
115
+ const boundFieldStates = new WeakMap();
116
+ const boundRoleStates = new WeakMap();
117
+ const predicateStates = new WeakMap();
118
+ const orderStates = new WeakMap();
119
+ class PredicateImpl {
120
+ constructor(handle) {
121
+ predicateStates.set(this, handle);
122
+ Object.freeze(this);
123
+ }
124
+ and(other) {
125
+ return new PredicateImpl(nativeCall(() => predicateHandle(this).and(predicateHandle(other))));
126
+ }
127
+ or(other) {
128
+ return new PredicateImpl(nativeCall(() => predicateHandle(this).or(predicateHandle(other))));
129
+ }
130
+ not() {
131
+ return new PredicateImpl(nativeCall(() => predicateHandle(this).not()));
132
+ }
133
+ }
134
+ class QueryOrderImpl {
135
+ constructor(handle) {
136
+ orderStates.set(this, handle);
137
+ Object.freeze(this);
138
+ }
139
+ }
140
+ class BoundFieldImpl {
141
+ constructor(handle, attrType) {
142
+ boundFieldStates.set(this, { handle, attrType });
143
+ Object.freeze(this);
144
+ }
145
+ eq(value) {
146
+ return this.compare("equal", value);
147
+ }
148
+ eqField(value) {
149
+ return this.compare("equal", value);
150
+ }
151
+ ne(value) {
152
+ return this.compare("not_equal", value);
153
+ }
154
+ gt(value) {
155
+ return this.compare("greater_than", value);
156
+ }
157
+ gte(value) {
158
+ return this.compare("greater_than_or_equal", value);
159
+ }
160
+ lt(value) {
161
+ return this.compare("less_than", value);
162
+ }
163
+ lte(value) {
164
+ return this.compare("less_than_or_equal", value);
165
+ }
166
+ contains(value) {
167
+ return this.compareString("contains", value);
168
+ }
169
+ startsWith(value) {
170
+ return this.compareString("starts_with", value);
171
+ }
172
+ endsWith(value) {
173
+ return this.compareString("ends_with", value);
174
+ }
175
+ regex(value) {
176
+ return this.compareString("regex", value);
177
+ }
178
+ asc(missing = "reject") {
179
+ return this.order("ascending", missing);
180
+ }
181
+ desc(missing = "reject") {
182
+ return this.order("descending", missing);
183
+ }
184
+ compare(comparison, value) {
185
+ const own = boundFieldState(this);
186
+ const other = boundFieldStates.get(value);
187
+ if (other !== undefined) {
188
+ return new PredicateImpl(nativeCall(() => own.handle.compareField(comparison, other.handle)));
189
+ }
190
+ if (!(value instanceof own.attrType)) {
191
+ throw new TypedReferenceError(`comparison value must be an instance of ${own.attrType.attrName}`);
192
+ }
193
+ const literal = value;
194
+ const lowered = (0, codec_js_1.lowerAttributeValue)(own.attrType.valueType, literal.value);
195
+ return new PredicateImpl(nativeCall(() => own.handle.compareValueJson(comparison, JSON.stringify(lowered))));
196
+ }
197
+ compareString(comparison, value) {
198
+ const own = boundFieldState(this);
199
+ return new PredicateImpl(nativeCall(() => own.handle.compareValueJson(comparison, JSON.stringify({ value_type: "string", value }))));
200
+ }
201
+ order(direction, missing) {
202
+ const own = boundFieldState(this);
203
+ return new QueryOrderImpl(nativeCall(() => own.handle.order(direction, missing)));
204
+ }
205
+ }
206
+ class BoundRoleImpl {
207
+ constructor(handle) {
208
+ boundRoleStates.set(this, handle);
209
+ Object.freeze(this);
210
+ }
211
+ connects(player) {
212
+ return new PredicateImpl(nativeCall(() => boundRoleHandle(this).connects(boundVariableState(player).handle)));
213
+ }
214
+ is(player) {
215
+ return this.connects(player);
216
+ }
217
+ }
218
+ class BoundVarImpl {
219
+ constructor(model, modelTypeName, handle) {
220
+ boundVariableStates.set(this, { model, modelTypeName, handle });
221
+ Object.freeze(this);
222
+ }
223
+ field(reference) {
224
+ const state = fieldReferenceStates.get(reference);
225
+ if (state === undefined) {
226
+ throw new TypedReferenceError("field reference was not created by references(model)");
227
+ }
228
+ const variable = boundVariableState(this);
229
+ const modelTypeName = stableBoundVariableTypeName(variable);
230
+ if (!Object.values(variable.model.schema).includes(state.spec)) {
231
+ throw new TypedReferenceError(`field reference '${state.name}' does not belong to ${modelTypeName}`);
232
+ }
233
+ const ownerTypeName = stableReferenceOwnerTypeName(state);
234
+ if (ownerTypeName === modelTypeName && state.owner !== variable.model) {
235
+ throw new TypedReferenceError("field reference owner does not match the bound model");
236
+ }
237
+ const handle = nativeCall(() => variable.handle.fieldOwnedBy(ownerTypeName, state.name));
238
+ return new BoundFieldImpl(handle, state.attrType);
239
+ }
240
+ role(reference) {
241
+ const state = roleReferenceStates.get(reference);
242
+ if (state === undefined) {
243
+ throw new TypedReferenceError("role reference was not created by references(model)");
244
+ }
245
+ const variable = boundVariableState(this);
246
+ const modelTypeName = stableBoundVariableTypeName(variable);
247
+ if (!Object.values(variable.model.schema).includes(state.spec)) {
248
+ throw new TypedReferenceError(`role reference '${state.name}' does not belong to ${modelTypeName}`);
249
+ }
250
+ const ownerTypeName = stableReferenceOwnerTypeName(state);
251
+ if (ownerTypeName === modelTypeName && state.owner !== variable.model) {
252
+ throw new TypedReferenceError("role reference owner does not match the bound model");
253
+ }
254
+ return new BoundRoleImpl(nativeCall(() => variable.handle.roleOwnedBy(ownerTypeName, state.name)));
255
+ }
256
+ collect() {
257
+ return new CollectedImpl(nativeCall(() => boundVariableState(this).handle.collect()));
258
+ }
259
+ }
260
+ class CollectedImpl {
261
+ constructor(handle) {
262
+ collectedStates.set(this, handle);
263
+ Object.freeze(this);
264
+ }
265
+ distinct(distinct = true) {
266
+ return new CollectedImpl(nativeCall(() => collectedHandle(this).distinct(distinct)));
267
+ }
268
+ orderBy(order) {
269
+ return new CollectedImpl(nativeCall(() => collectedHandle(this).orderBy(orderHandle(order))));
270
+ }
271
+ }
272
+ function stableReferenceOwnerTypeName(state) {
273
+ let currentTypeName;
274
+ try {
275
+ currentTypeName = state.owner.typeName;
276
+ }
277
+ catch {
278
+ throw new TypedReferenceError("reference owner type name changed after references(model)");
279
+ }
280
+ if (currentTypeName !== state.ownerTypeName) {
281
+ throw new TypedReferenceError("reference owner type name changed after references(model)");
282
+ }
283
+ return state.ownerTypeName;
284
+ }
285
+ function stableBoundVariableTypeName(state) {
286
+ let currentTypeName;
287
+ try {
288
+ currentTypeName = state.model.typeName;
289
+ }
290
+ catch {
291
+ throw new TypedReferenceError("bound variable model type name changed after QuerySession.var");
292
+ }
293
+ if (currentTypeName !== state.modelTypeName) {
294
+ throw new TypedReferenceError("bound variable model type name changed after QuerySession.var");
295
+ }
296
+ return state.modelTypeName;
297
+ }
298
+ function boundVariableState(variable) {
299
+ const state = boundVariableStates.get(variable);
300
+ if (state === undefined) {
301
+ throw new TypedReferenceError("bound variable was not created by QuerySession.var");
302
+ }
303
+ return state;
304
+ }
305
+ function boundFieldState(field) {
306
+ const state = boundFieldStates.get(field);
307
+ if (state === undefined) {
308
+ throw new TypedReferenceError("bound field was not created from a BoundVar");
309
+ }
310
+ return state;
311
+ }
312
+ function boundRoleHandle(role) {
313
+ const handle = boundRoleStates.get(role);
314
+ if (handle === undefined) {
315
+ throw new TypedReferenceError("bound role was not created from a BoundVar");
316
+ }
317
+ return handle;
318
+ }
319
+ function predicateHandle(predicate) {
320
+ const handle = predicateStates.get(predicate);
321
+ if (handle === undefined) {
322
+ throw new TypedReferenceError("predicate was not created by a bound reference");
323
+ }
324
+ return handle;
325
+ }
326
+ function orderHandle(order) {
327
+ const handle = orderStates.get(order);
328
+ if (handle === undefined) {
329
+ throw new TypedReferenceError("order was not created by a bound field");
330
+ }
331
+ return handle;
332
+ }
333
+ function collectedHandle(selection) {
334
+ const handle = collectedStates.get(selection);
335
+ if (handle === undefined) {
336
+ throw new TypedReferenceError("collection was not created by a BoundVar");
337
+ }
338
+ return handle;
339
+ }
340
+ /** @internal Construct a public variable around one opaque native binding. */
341
+ function createBoundVar(model, modelTypeName, handle) {
342
+ return new BoundVarImpl(model, modelTypeName, handle);
343
+ }
344
+ /** @internal Extract a fresh/native selection for the future Query facade. */
345
+ function nativeSelectionHandle(selection) {
346
+ const variable = boundVariableStates.get(selection);
347
+ if (variable !== undefined) {
348
+ return nativeCall(() => variable.handle.one());
349
+ }
350
+ return collectedHandle(selection);
351
+ }
352
+ /** @internal Extract the binding behind a session-owned variable. */
353
+ function nativeBindingHandle(variable) {
354
+ return boundVariableState(variable).handle;
355
+ }
356
+ /** @internal Extract a native predicate for the future Query facade. */
357
+ function nativePredicateHandle(predicate) {
358
+ return predicateHandle(predicate);
359
+ }
360
+ /** @internal Extract a native order for the future Query facade. */
361
+ function nativeQueryOrderHandle(order) {
362
+ return orderHandle(order);
363
+ }
@@ -0,0 +1,18 @@
1
+ import { loadNative } from "../native.js";
2
+ import { type Page } from "./page.js";
3
+ import { type QueryModelClass } from "./references.js";
4
+ type NativeModule = ReturnType<typeof loadNative>;
5
+ type NativeSession = InstanceType<NativeModule["NodeMatchSessionHandle"]>;
6
+ type NativeQueryHandle = ReturnType<NativeSession["query"]>;
7
+ type NativeResultHandle = ReturnType<NativeQueryHandle["executeFetchRowsOwned"]>;
8
+ /** @internal Materialize exactly one scalar/tuple/named row from a native proof. */
9
+ export declare function materializeValidatedOne(query: NativeQueryHandle, result: NativeResultHandle, models: ReadonlyMap<string, QueryModelClass>): unknown;
10
+ /** @internal Materialize frozen scalar/tuple/named rows from one native proof. */
11
+ export declare function materializeValidatedRows(query: NativeQueryHandle, result: NativeResultHandle, models: ReadonlyMap<string, QueryModelClass>): readonly unknown[];
12
+ /** @internal Materialize one immutable distinct-root page from a native proof. */
13
+ export declare function materializeValidatedPage(query: NativeQueryHandle, result: NativeResultHandle, models: ReadonlyMap<string, QueryModelClass>, expectedOffset: bigint, expectedLimit: bigint, includeTotal: boolean): Page<unknown>;
14
+ /** @internal Preserve one lossless distinct-root count. */
15
+ export declare function materializeValidatedCount(query: NativeQueryHandle, result: NativeResultHandle): bigint;
16
+ /** @internal Preserve one distinct-root existence proof. */
17
+ export declare function materializeValidatedExists(query: NativeQueryHandle, result: NativeResultHandle): boolean;
18
+ export {};