@malloy-publisher/server 0.0.198-dev1 → 0.0.198-dev2

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.
@@ -1,251 +0,0 @@
1
- /**
2
- * Wire protocol between the main thread (CompileWorkerPool) and the
3
- * compile worker threads. Messages flow in both directions over the
4
- * worker_threads MessagePort:
5
- *
6
- * main ──▶ worker: CompileJobRequest (start a compile)
7
- * worker ──▶ main: CompileJobResult (success)
8
- * worker ──▶ main: CompileJobError (failure)
9
- *
10
- * worker ──▶ main: SchemaForTablesRequest (proxy schema fetch)
11
- * worker ──▶ main: SchemaForSqlRequest (proxy SQL block schema)
12
- * main ──▶ worker: SchemaForTablesResponse / SchemaForSqlResponse
13
- *
14
- * main ──▶ worker: ShutdownRequest (graceful drain & exit)
15
- *
16
- * The protocol intentionally uses plain structured-clonable POJOs so
17
- * `parentPort.postMessage` and `worker.postMessage` can transfer them
18
- * via V8's structured clone — much cheaper than JSON.stringify for
19
- * the multi-MB `modelDef` payloads that come back from compile.
20
- *
21
- * All requests are correlated by an opaque `requestId` string so the
22
- * receiver can match responses without relying on FIFO ordering.
23
- */
24
-
25
- import type {
26
- Annotation,
27
- SQLSourceDef,
28
- TableSourceDef,
29
- } from "@malloydata/malloy";
30
-
31
- // ──────────────────────────────────────────────────────────────────────
32
- // Direction: main ──▶ worker (compile job)
33
- // ──────────────────────────────────────────────────────────────────────
34
-
35
- /**
36
- * Connection metadata the worker needs to construct a stub
37
- * `InfoConnection`. Resolved lazily — the worker asks the main thread
38
- * for these on the first `lookupConnection(name)` call (see
39
- * {@link ConnectionMetadataRequest}). We don't ship the full list
40
- * upfront because the caller layer doesn't always know it; Malloy
41
- * sees connection names only as `connection.table('...')`
42
- * references inside the model.
43
- */
44
- export interface ConnectionMetadata {
45
- name: string;
46
- dialectName: string;
47
- digest: string;
48
- }
49
-
50
- export interface CompileJobRequest {
51
- type: "compile";
52
- requestId: string;
53
- /** Absolute path to the package directory on disk. */
54
- packagePath: string;
55
- /** Path of the model file relative to `packagePath`. */
56
- modelPath: string;
57
- /** Name of the default connection (e.g. "duckdb"), or null. */
58
- defaultConnectionName: string | null;
59
- /** Optional row-build manifest passed through to the Runtime. */
60
- buildManifest?: unknown;
61
- }
62
-
63
- // ──────────────────────────────────────────────────────────────────────
64
- // Direction: worker ──▶ main (compile result)
65
- // ──────────────────────────────────────────────────────────────────────
66
-
67
- /**
68
- * Wire shape of a successful compile. Mirrors the fields the
69
- * server's `Model` constructor needs to fully describe a `.malloy`
70
- * file without holding a `ModelMaterializer` reference.
71
- *
72
- * The materializer itself is intentionally NOT shipped back — it
73
- * binds to a Runtime that holds live native connection handles and
74
- * cannot cross a worker_threads boundary. The main thread builds
75
- * its own materializer lazily on the first query (see
76
- * `Model.ensureMaterializer`).
77
- */
78
- export interface CompileJobResult {
79
- type: "compile-result";
80
- requestId: string;
81
- /** Whatever `await modelMaterializer.getModel()`._modelDef returned. */
82
- modelDef: unknown;
83
- /** Source-info entries (from imports + local sources). */
84
- sourceInfos: unknown[];
85
- /** Pre-extracted API source descriptors. */
86
- sources: unknown[];
87
- /** Pre-extracted API query descriptors. */
88
- queries: unknown[];
89
- /** Parsed `#(filter)` map, keyed by source name. */
90
- filterMap: Array<[string, unknown[]]>;
91
- /** Givens declared on the model, already in API shape so the main
92
- * thread can stash them on the `Model` without further conversion. */
93
- givens?: unknown[];
94
- /** Accumulated dataStyles (from HackyDataStylesAccumulator). */
95
- dataStyles: unknown;
96
- /** Wall-clock ms inside the worker for the actual compile. */
97
- compileDurationMs: number;
98
- }
99
-
100
- export interface CompileJobError {
101
- type: "compile-error";
102
- requestId: string;
103
- /** Serialized error — the main thread reconstructs an Error. */
104
- error: SerializedError;
105
- }
106
-
107
- /**
108
- * Error wire-shape. We cannot transfer Error instances directly
109
- * across postMessage cleanly (Bun/Node behaviour diverges on stack
110
- * propagation), so we ship a structured payload and reconstitute on
111
- * the main thread.
112
- */
113
- export interface SerializedError {
114
- name: string;
115
- message: string;
116
- stack?: string;
117
- /** Set when the error originated as a Malloy `MalloyError`. */
118
- malloyProblems?: unknown[];
119
- /** Set when the error originated as `ModelCompilationError`. */
120
- isCompilationError?: boolean;
121
- }
122
-
123
- // ──────────────────────────────────────────────────────────────────────
124
- // Direction: worker ──▶ main (proxy connection metadata)
125
- // ──────────────────────────────────────────────────────────────────────
126
-
127
- export interface ConnectionMetadataRequest {
128
- type: "connection-metadata";
129
- requestId: string;
130
- jobId: string;
131
- connectionName: string;
132
- }
133
-
134
- export interface ConnectionMetadataResponse {
135
- type: "connection-metadata-response";
136
- requestId: string;
137
- ok: true;
138
- metadata: ConnectionMetadata;
139
- }
140
-
141
- // ──────────────────────────────────────────────────────────────────────
142
- // Direction: worker ──▶ main (proxy schema fetches)
143
- // ──────────────────────────────────────────────────────────────────────
144
-
145
- export interface SchemaForTablesRequest {
146
- type: "schema-for-tables";
147
- requestId: string;
148
- /** Job this RPC belongs to (so main routes to the right config). */
149
- jobId: string;
150
- connectionName: string;
151
- tables: Record<string, string>;
152
- options: {
153
- refreshTimestamp?: number;
154
- modelAnnotation?: Annotation;
155
- };
156
- }
157
-
158
- export interface SchemaForTablesResponse {
159
- type: "schema-for-tables-response";
160
- requestId: string;
161
- ok: true;
162
- schemas: Record<string, TableSourceDef>;
163
- errors: Record<string, string>;
164
- }
165
-
166
- export interface SchemaForSqlRequest {
167
- type: "schema-for-sql";
168
- requestId: string;
169
- jobId: string;
170
- connectionName: string;
171
- sentence: unknown;
172
- options: {
173
- refreshTimestamp?: number;
174
- modelAnnotation?: Annotation;
175
- };
176
- }
177
-
178
- export interface SchemaForSqlResponse {
179
- type: "schema-for-sql-response";
180
- requestId: string;
181
- ok: true;
182
- structDef?: SQLSourceDef;
183
- error?: string;
184
- }
185
-
186
- export interface RpcErrorResponse {
187
- type: "rpc-error";
188
- requestId: string;
189
- ok: false;
190
- error: SerializedError;
191
- }
192
-
193
- // ──────────────────────────────────────────────────────────────────────
194
- // Direction: worker ──▶ main (file read for imports)
195
- // ──────────────────────────────────────────────────────────────────────
196
-
197
- /**
198
- * Workers read most files directly via fs (they run in the same
199
- * filesystem namespace). This RPC exists for the rare case where the
200
- * package URL reader has host-specific behaviour (e.g. virtual files,
201
- * remote URLs) — we delegate back to the main thread's URL reader so
202
- * compile semantics stay identical to the in-process path.
203
- */
204
- export interface ReadUrlRequest {
205
- type: "read-url";
206
- requestId: string;
207
- jobId: string;
208
- url: string;
209
- }
210
-
211
- export interface ReadUrlResponse {
212
- type: "read-url-response";
213
- requestId: string;
214
- ok: true;
215
- contents: string;
216
- invalidationKey?: string | number | null;
217
- }
218
-
219
- // ──────────────────────────────────────────────────────────────────────
220
- // Lifecycle
221
- // ──────────────────────────────────────────────────────────────────────
222
-
223
- export interface ShutdownRequest {
224
- type: "shutdown";
225
- }
226
-
227
- export interface ReadyMessage {
228
- type: "ready";
229
- }
230
-
231
- // ──────────────────────────────────────────────────────────────────────
232
- // Union types for routing
233
- // ──────────────────────────────────────────────────────────────────────
234
-
235
- export type MainToWorkerMessage =
236
- | CompileJobRequest
237
- | ConnectionMetadataResponse
238
- | SchemaForTablesResponse
239
- | SchemaForSqlResponse
240
- | ReadUrlResponse
241
- | RpcErrorResponse
242
- | ShutdownRequest;
243
-
244
- export type WorkerToMainMessage =
245
- | CompileJobResult
246
- | CompileJobError
247
- | ConnectionMetadataRequest
248
- | SchemaForTablesRequest
249
- | SchemaForSqlRequest
250
- | ReadUrlRequest
251
- | ReadyMessage;
@@ -1,125 +0,0 @@
1
- /**
2
- * Integration test: exercise `Model.create` with the worker pool
3
- * enabled (MALLOY_COMPILE_WORKERS=1).
4
- *
5
- * Validates that the worker-compile path:
6
- * - produces a Model with a populated modelDef + sources + queries
7
- * - defers materializer construction (none until first query)
8
- * - falls back to in-process compile for notebooks
9
- * - falls through to in-process compile when the worker pool fails
10
- *
11
- * Kept separate from `model.spec.ts` so the existing tests keep
12
- * running on the in-process path without paying worker startup cost.
13
- */
14
- import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test";
15
- import * as fs from "fs";
16
- import * as os from "os";
17
- import * as path from "path";
18
- import { __setCompilePoolForTests } from "../compile/compile_pool";
19
- import { Model } from "./model";
20
-
21
- const ORIGINAL_ENV = process.env.MALLOY_COMPILE_WORKERS;
22
-
23
- describe("Model.create via worker pool", () => {
24
- let tempDir: string;
25
-
26
- beforeAll(() => {
27
- process.env.MALLOY_COMPILE_WORKERS = "1";
28
- });
29
-
30
- afterAll(async () => {
31
- if (ORIGINAL_ENV === undefined) {
32
- delete process.env.MALLOY_COMPILE_WORKERS;
33
- } else {
34
- process.env.MALLOY_COMPILE_WORKERS = ORIGINAL_ENV;
35
- }
36
- await __setCompilePoolForTests(null);
37
- });
38
-
39
- afterEach(() => {
40
- if (tempDir) {
41
- fs.rmSync(tempDir, { recursive: true, force: true });
42
- tempDir = "";
43
- }
44
- });
45
-
46
- it("compiles a .malloy file via worker and returns a usable Model", async () => {
47
- const { DuckDBConnection } = await import("@malloydata/db-duckdb");
48
- tempDir = fs.mkdtempSync(
49
- path.join(os.tmpdir(), "publisher-model-worker-"),
50
- );
51
- fs.writeFileSync(
52
- path.join(tempDir, "trivial.malloy"),
53
- `source: nums is duckdb.sql("select 1 as a") extend {
54
- measure: total is a.sum()
55
- }`,
56
- );
57
-
58
- const duckdb = new DuckDBConnection("duckdb", ":memory:");
59
- try {
60
- const model = await Model.create(
61
- "test-pkg",
62
- tempDir,
63
- "trivial.malloy",
64
- new Map([["duckdb", duckdb]]),
65
- );
66
-
67
- expect(model).toBeInstanceOf(Model);
68
- const apiModel = await model.getModel();
69
- expect(apiModel.type).toBe("source");
70
- expect(apiModel.modelDef).toBeDefined();
71
- expect(apiModel.modelDef!.length).toBeGreaterThan(10);
72
- // Single source `nums` from the worker-extracted ApiSource[]
73
- expect(apiModel.sources?.[0]?.name).toBe("nums");
74
- } finally {
75
- await duckdb.close();
76
- }
77
- });
78
-
79
- it("propagates compilation errors as ModelCompilationError", async () => {
80
- const { DuckDBConnection } = await import("@malloydata/db-duckdb");
81
- const { ModelCompilationError } = await import("../errors");
82
- tempDir = fs.mkdtempSync(
83
- path.join(os.tmpdir(), "publisher-model-worker-"),
84
- );
85
- fs.writeFileSync(
86
- path.join(tempDir, "broken.malloy"),
87
- `source: nums is duckdb.sql("select 1 as a") extend {
88
- measure: total is THIS_FUNC_DOES_NOT_EXIST(a)
89
- }`,
90
- );
91
-
92
- const duckdb = new DuckDBConnection("duckdb", ":memory:");
93
- try {
94
- const model = await Model.create(
95
- "test-pkg",
96
- tempDir,
97
- "broken.malloy",
98
- new Map([["duckdb", duckdb]]),
99
- );
100
- // Either the Model surfaces with `compilationError` populated
101
- // (returned by the worker, re-wrapped on the main thread) or
102
- // getModel() throws — both are equivalent under the existing
103
- // error contract; we accept either.
104
- try {
105
- await model.getModel();
106
- // If getModel didn't throw, the compile error should be
107
- // visible via the Model's `compilationError` field.
108
- expect(
109
- (model as unknown as { compilationError?: Error })
110
- .compilationError,
111
- ).toBeDefined();
112
- } catch (err) {
113
- expect(err).toBeInstanceOf(Error);
114
- // Compile errors come back as ModelCompilationError
115
- // (worker serializes MalloyError with
116
- // isCompilationError=true; pool re-wraps).
117
- expect(
118
- err instanceof ModelCompilationError || err instanceof Error,
119
- ).toBe(true);
120
- }
121
- } finally {
122
- await duckdb.close();
123
- }
124
- });
125
- });