@malloy-publisher/server 0.0.165 → 0.0.167

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/app/api-doc.yaml +107 -0
  2. package/dist/app/assets/{HomePage-QekMXs8r.js → HomePage-D76UaGFV.js} +1 -1
  3. package/dist/app/assets/{MainPage-DAyUfYba.js → MainPage-C9Fr5IN8.js} +1 -1
  4. package/dist/app/assets/{ModelPage-CrMryV1s.js → ModelPage-BkU6HAHA.js} +1 -1
  5. package/dist/app/assets/{PackagePage-DDaABD2A.js → PackagePage-BhE9Wi7b.js} +1 -1
  6. package/dist/app/assets/{ProjectPage-FAYUFGhL.js → ProjectPage-BatZLVap.js} +1 -1
  7. package/dist/app/assets/{RouteError-BKYctANX.js → RouteError-Bo5zJ8Xa.js} +1 -1
  8. package/dist/app/assets/{WorkbookPage-DZEVYGW3.js → WorkbookPage-D3rUQZj6.js} +1 -1
  9. package/dist/app/assets/{index-BvVmB5sv.js → index-BLxl0XLH.js} +71 -71
  10. package/dist/app/assets/{index-DWhjtyBB.js → index-hkABoiMV.js} +1 -1
  11. package/dist/app/assets/{index-CsC07BYd.js → index-lhDwptrQ.js} +1 -1
  12. package/dist/app/assets/{index.umd-DvM-lTQa.js → index.umd-BkXQ-YAe.js} +1 -1
  13. package/dist/app/index.html +1 -1
  14. package/dist/instrumentation.js +85955 -88560
  15. package/dist/server.js +197162 -106231
  16. package/package.json +2 -1
  17. package/src/controller/compile.controller.ts +35 -0
  18. package/src/controller/model.controller.ts +20 -9
  19. package/src/health.ts +8 -0
  20. package/src/instrumentation.ts +123 -34
  21. package/src/server.ts +44 -2
  22. package/src/service/connection.spec.ts +1226 -0
  23. package/src/service/connection.ts +114 -12
  24. package/src/service/db_utils.ts +19 -41
  25. package/src/service/gcs_s3_utils.ts +115 -40
  26. package/src/service/model.ts +5 -5
  27. package/src/service/project.ts +120 -1
  28. package/src/service/project_compile.spec.ts +197 -0
  29. package/src/service/project_store.ts +49 -21
  30. package/src/storage/StorageManager.ts +4 -3
  31. package/src/storage/duckdb/schema.ts +6 -5
  32. package/tests/harness/e2e.ts +4 -0
  33. package/tests/harness/mcp_test_setup.ts +6 -2
@@ -0,0 +1,197 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import { extractPreamble, extractPreambleFromSource } from "./project";
5
+
6
+ describe("extractPreambleFromSource", () => {
7
+ it("should extract pragmas and imports before a source definition", () => {
8
+ const content = [
9
+ "##! experimental.parameters",
10
+ 'import "utils.malloy"',
11
+ 'import { revenue } from "metrics.malloy"',
12
+ "",
13
+ 'source: my_source is duckdb.table("data.parquet")',
14
+ ' dimension: name is "test"',
15
+ ].join("\n");
16
+
17
+ const result = extractPreambleFromSource(content);
18
+ expect(result).toBe(
19
+ [
20
+ "##! experimental.parameters",
21
+ 'import "utils.malloy"',
22
+ 'import { revenue } from "metrics.malloy"',
23
+ ].join("\n"),
24
+ );
25
+ });
26
+
27
+ it("should extract pragmas and imports before a run statement", () => {
28
+ const content = [
29
+ 'import { my_source } from "model.malloy"',
30
+ "",
31
+ "run: my_source -> { aggregate: count() }",
32
+ ].join("\n");
33
+
34
+ const result = extractPreambleFromSource(content);
35
+ expect(result).toBe('import { my_source } from "model.malloy"');
36
+ });
37
+
38
+ it("should extract pragmas and imports before a query definition", () => {
39
+ const content = [
40
+ 'import { my_source } from "model.malloy"',
41
+ "",
42
+ "query: top_items is my_source -> { limit: 10 }",
43
+ ].join("\n");
44
+
45
+ const result = extractPreambleFromSource(content);
46
+ expect(result).toBe('import { my_source } from "model.malloy"');
47
+ });
48
+
49
+ it("should return empty string when file starts with source:", () => {
50
+ const content = 'source: my_source is duckdb.table("data.parquet")';
51
+
52
+ const result = extractPreambleFromSource(content);
53
+ expect(result).toBe("");
54
+ });
55
+
56
+ it("should return empty string when file starts with run:", () => {
57
+ const content = 'run: duckdb.sql("SELECT 1")';
58
+
59
+ const result = extractPreambleFromSource(content);
60
+ expect(result).toBe("");
61
+ });
62
+
63
+ it("should return empty string when file starts with query:", () => {
64
+ const content = 'query: q is duckdb.sql("SELECT 1")';
65
+
66
+ const result = extractPreambleFromSource(content);
67
+ expect(result).toBe("");
68
+ });
69
+
70
+ it("should preserve comments in the preamble", () => {
71
+ const content = [
72
+ "##! experimental.parameters",
73
+ "// This model defines revenue metrics",
74
+ 'import { revenue } from "metrics.malloy"',
75
+ "// Main source below",
76
+ 'source: my_source is duckdb.table("data.parquet")',
77
+ ].join("\n");
78
+
79
+ const result = extractPreambleFromSource(content);
80
+ expect(result).toBe(
81
+ [
82
+ "##! experimental.parameters",
83
+ "// This model defines revenue metrics",
84
+ 'import { revenue } from "metrics.malloy"',
85
+ "// Main source below",
86
+ ].join("\n"),
87
+ );
88
+ });
89
+
90
+ it("should handle multiple import blocks", () => {
91
+ const content = [
92
+ 'import { a } from "file_a.malloy"',
93
+ 'import { b } from "file_b.malloy"',
94
+ 'import { c, d } from "file_c.malloy"',
95
+ "",
96
+ "source: combined is a {",
97
+ " join_one: b on b.id = a.b_id",
98
+ "}",
99
+ ].join("\n");
100
+
101
+ const result = extractPreambleFromSource(content);
102
+ expect(result).toBe(
103
+ [
104
+ 'import { a } from "file_a.malloy"',
105
+ 'import { b } from "file_b.malloy"',
106
+ 'import { c, d } from "file_c.malloy"',
107
+ ].join("\n"),
108
+ );
109
+ });
110
+
111
+ it("should return all content when there are no source/query/run definitions", () => {
112
+ const content = [
113
+ "##! experimental.parameters",
114
+ 'import { revenue } from "metrics.malloy"',
115
+ "",
116
+ "// no definitions yet",
117
+ ].join("\n");
118
+
119
+ const result = extractPreambleFromSource(content);
120
+ expect(result).toBe(
121
+ [
122
+ "##! experimental.parameters",
123
+ 'import { revenue } from "metrics.malloy"',
124
+ "",
125
+ "// no definitions yet",
126
+ ].join("\n"),
127
+ );
128
+ });
129
+
130
+ it("should return empty string for empty input", () => {
131
+ expect(extractPreambleFromSource("")).toBe("");
132
+ });
133
+
134
+ it("should handle indented source/query/run definitions", () => {
135
+ const content = [
136
+ 'import "model.malloy"',
137
+ "",
138
+ ' source: indented is duckdb.table("data.parquet")',
139
+ ].join("\n");
140
+
141
+ // Indented definitions should still be detected (trimmed before check)
142
+ const result = extractPreambleFromSource(content);
143
+ expect(result).toBe('import "model.malloy"');
144
+ });
145
+
146
+ it("should not stop on 'source' appearing in comments or strings", () => {
147
+ const content = [
148
+ "// This file defines the source of truth",
149
+ 'import "model.malloy"',
150
+ 'source: my_source is duckdb.table("data.parquet")',
151
+ ].join("\n");
152
+
153
+ const result = extractPreambleFromSource(content);
154
+ expect(result).toBe(
155
+ [
156
+ "// This file defines the source of truth",
157
+ 'import "model.malloy"',
158
+ ].join("\n"),
159
+ );
160
+ });
161
+ });
162
+
163
+ describe("extractPreamble (file-based)", () => {
164
+ const testDir = path.join(process.cwd(), "test-temp-preamble");
165
+ const testModelPath = path.join(testDir, "test_model.malloy");
166
+
167
+ beforeEach(() => {
168
+ if (!fs.existsSync(testDir)) {
169
+ fs.mkdirSync(testDir, { recursive: true });
170
+ }
171
+ });
172
+
173
+ afterEach(() => {
174
+ if (fs.existsSync(testDir)) {
175
+ fs.rmSync(testDir, { recursive: true, force: true });
176
+ }
177
+ });
178
+
179
+ it("should read and extract preamble from a file", async () => {
180
+ const content = [
181
+ 'import { revenue } from "metrics.malloy"',
182
+ "",
183
+ 'source: my_source is duckdb.table("data.parquet")',
184
+ ].join("\n");
185
+ fs.writeFileSync(testModelPath, content);
186
+
187
+ const result = await extractPreamble(testModelPath);
188
+ expect(result).toBe('import { revenue } from "metrics.malloy"');
189
+ });
190
+
191
+ it("should return empty string when file does not exist", async () => {
192
+ const result = await extractPreamble(
193
+ path.join(testDir, "nonexistent.malloy"),
194
+ );
195
+ expect(result).toBe("");
196
+ });
197
+ });
@@ -196,7 +196,8 @@ export class ProjectStore {
196
196
  );
197
197
  } catch (error) {
198
198
  markNotReady();
199
- logger.error("Error initializing project store", { error });
199
+ const errorData = this.extractErrorDataFromError(error);
200
+ logger.error("Error initializing project store", errorData);
200
201
  process.exit(1);
201
202
  }
202
203
  }
@@ -533,7 +534,7 @@ export class ProjectStore {
533
534
  PUBLISHER_DATA_DIR,
534
535
  );
535
536
  logger.info(
536
- `Re init: Cleaning up upload documents path ${uploadDocsPath}`,
537
+ `Reinitialization mode: Cleaning up upload documents path ${uploadDocsPath}`,
537
538
  );
538
539
  try {
539
540
  await fs.promises.rm(uploadDocsPath, {
@@ -1025,11 +1026,10 @@ export class ProjectStore {
1025
1026
  }
1026
1027
  }
1027
1028
  } catch (error) {
1029
+ const errorData = this.extractErrorDataFromError(error);
1028
1030
  logger.error(
1029
1031
  `Failed to download or mount location "${groupedLocation}"`,
1030
- {
1031
- error,
1032
- },
1032
+ errorData,
1033
1033
  );
1034
1034
  throw new PackageNotFoundError(
1035
1035
  `Failed to download or mount location: ${groupedLocation}`,
@@ -1075,9 +1075,11 @@ export class ProjectStore {
1075
1075
  );
1076
1076
  return;
1077
1077
  } catch (error) {
1078
- logger.error(`Failed to download GCS directory "${location}"`, {
1079
- error,
1080
- });
1078
+ const errorData = this.extractErrorDataFromError(error);
1079
+ logger.error(
1080
+ `Failed to download GCS directory "${location}"`,
1081
+ errorData,
1082
+ );
1081
1083
  throw new PackageNotFoundError(
1082
1084
  `Failed to download GCS directory: ${location}`,
1083
1085
  );
@@ -1093,9 +1095,11 @@ export class ProjectStore {
1093
1095
  await this.downloadGitHubDirectory(location, targetPath);
1094
1096
  return;
1095
1097
  } catch (error) {
1096
- logger.error(`Failed to clone GitHub repository "${location}"`, {
1097
- error,
1098
- });
1098
+ const errorData = this.extractErrorDataFromError(error);
1099
+ logger.error(
1100
+ `Failed to clone GitHub repository "${location}"`,
1101
+ errorData,
1102
+ );
1099
1103
  throw new PackageNotFoundError(
1100
1104
  `Failed to clone GitHub repository: ${location}`,
1101
1105
  );
@@ -1111,9 +1115,11 @@ export class ProjectStore {
1111
1115
  await this.downloadS3Directory(location, projectName, targetPath);
1112
1116
  return;
1113
1117
  } catch (error) {
1114
- logger.error(`Failed to download S3 directory "${location}"`, {
1115
- error,
1116
- });
1118
+ const errorData = this.extractErrorDataFromError(error);
1119
+ logger.error(
1120
+ `Failed to download S3 directory "${location}"`,
1121
+ errorData,
1122
+ );
1117
1123
  throw new PackageNotFoundError(
1118
1124
  `Failed to download S3 directory: ${location}`,
1119
1125
  );
@@ -1137,9 +1143,11 @@ export class ProjectStore {
1137
1143
  );
1138
1144
  return;
1139
1145
  } catch (error) {
1140
- logger.error(`Failed to mount local directory "${packagePath}"`, {
1141
- error,
1142
- });
1146
+ const errorData = this.extractErrorDataFromError(error);
1147
+ logger.error(
1148
+ `Failed to mount local directory "${packagePath}"`,
1149
+ errorData,
1150
+ );
1143
1151
  throw new PackageNotFoundError(
1144
1152
  `Failed to mount local directory: ${packagePath}`,
1145
1153
  );
@@ -1332,10 +1340,11 @@ export class ProjectStore {
1332
1340
  await new Promise<void>((resolve, reject) => {
1333
1341
  simpleGit().clone(repoUrl, absoluteDirPath, {}, (err) => {
1334
1342
  if (err) {
1335
- console.error(err);
1336
- logger.error(`Failed to clone GitHub repository "${repoUrl}"`, {
1337
- error: err,
1338
- });
1343
+ const errorData = this.extractErrorDataFromError(err);
1344
+ logger.error(
1345
+ `Failed to clone GitHub repository "${repoUrl}"`,
1346
+ errorData,
1347
+ );
1339
1348
  reject(err);
1340
1349
  }
1341
1350
  resolve();
@@ -1396,4 +1405,23 @@ export class ProjectStore {
1396
1405
 
1397
1406
  // https://github.com/credibledata/malloy-samples/imdb/publisher.json -> ${absoluteDirPath}/publisher.json
1398
1407
  }
1408
+
1409
+ private extractErrorDataFromError(error: unknown): {
1410
+ error: string;
1411
+ stack?: string;
1412
+ task?: unknown;
1413
+ } {
1414
+ const errorMessage =
1415
+ error instanceof Error ? error.message : String(error);
1416
+ const errorData: { error: string; stack?: string; task?: unknown } = {
1417
+ error: errorMessage,
1418
+ };
1419
+ if (error instanceof Error && logger.level === "debug") {
1420
+ errorData.stack = error.stack;
1421
+ }
1422
+ if (error && typeof error === "object" && "task" in error) {
1423
+ errorData.task = (error as { task?: unknown }).task;
1424
+ }
1425
+ return errorData;
1426
+ }
1399
1427
  }
@@ -1,3 +1,4 @@
1
+ import { logger } from "../logger";
1
2
  import { DatabaseConnection, ResourceRepository } from "./DatabaseInterface";
2
3
  import { DuckDBConnection } from "./duckdb/DuckDBConnection";
3
4
  import { DuckDBRepository } from "./duckdb/DuckDBRepository";
@@ -37,11 +38,11 @@ export class StorageManager {
37
38
 
38
39
  async initialize(reinit: boolean = false): Promise<void> {
39
40
  if (reinit) {
40
- console.log(
41
- "RE-INITIALIZATION MODE: Database will be dropped and recreated",
41
+ logger.info(
42
+ "Reinitialization mode: Database will be dropped and recreated",
42
43
  );
43
44
  } else {
44
- console.log("NORMAL MODE: Loading from existing database");
45
+ logger.info("Normal mode: Loading from existing database");
45
46
  }
46
47
 
47
48
  switch (this.config.type) {
@@ -1,3 +1,4 @@
1
+ import { logger } from "../../logger";
1
2
  import { DuckDBConnection } from "./DuckDBConnection";
2
3
 
3
4
  export async function initializeSchema(
@@ -11,12 +12,12 @@ export async function initializeSchema(
11
12
  }
12
13
 
13
14
  if (force) {
14
- console.log(
15
+ logger.info(
15
16
  "Reinitializing database schema dropping and recreating all tables",
16
17
  );
17
18
  await dropAllTables(db);
18
19
  } else {
19
- console.log("Creating database schema for the first time...");
20
+ logger.info("Creating database schema for the first time...");
20
21
  }
21
22
 
22
23
  // Projects table
@@ -75,14 +76,14 @@ export async function initializeSchema(
75
76
  async function dropAllTables(db: DuckDBConnection): Promise<void> {
76
77
  const tables = ["packages", "connections", "projects"];
77
78
 
78
- console.log("Dropping tables:", tables.join(", "));
79
+ logger.info("Dropping tables:", tables.join(", "));
79
80
 
80
81
  for (const table of tables) {
81
82
  try {
82
83
  await db.run(`DROP TABLE IF EXISTS ${table} `);
83
- console.log(`Dropped table: ${table}`);
84
+ logger.info(`Dropped table: ${table}`);
84
85
  } catch (err) {
85
- console.warn(` Warning: Could not drop table ${table}:`, err);
86
+ logger.warn(` Warning: Could not drop table ${table}:`, err);
86
87
  }
87
88
  }
88
89
  }
@@ -7,6 +7,7 @@ import type {
7
7
  } from "@modelcontextprotocol/sdk/types.js";
8
8
  import http from "http";
9
9
  import path from "path";
10
+ import { fileURLToPath } from "url";
10
11
  import { URL } from "url";
11
12
 
12
13
  /**
@@ -30,6 +31,9 @@ export async function startE2E(): Promise<E2EEnv & { stop(): Promise<void> }> {
30
31
  // 1. Set SERVER_ROOT so ProjectStore loader finds publisher.config.json
31
32
  //--------------------------------------------------------------------------
32
33
  originalServerRoot = process.env.SERVER_ROOT;
34
+ // Use import.meta.url for cross-platform compatibility (works on Windows)
35
+ const __filename = fileURLToPath(import.meta.url);
36
+ const __dirname = path.dirname(__filename);
33
37
  const serverPackageDir = path.resolve(__dirname, "../../../"); // packages/server
34
38
  process.env.SERVER_ROOT = serverPackageDir;
35
39
 
@@ -8,6 +8,7 @@ import {
8
8
  import http from "http";
9
9
  import { AddressInfo } from "net";
10
10
  import path from "path";
11
+ import { fileURLToPath } from "url";
11
12
  import { URL } from "url";
12
13
 
13
14
  // --- Real Server Import ---
@@ -31,8 +32,11 @@ export async function setupE2ETestEnvironment(): Promise<McpE2ETestEnvironment>
31
32
  // --- Store and Set SERVER_ROOT Env Var ---
32
33
  // The ProjectStore relies on SERVER_ROOT to find publisher.config.json.
33
34
  originalServerRoot = process.env.SERVER_ROOT; // Store original value
34
- // Resolve the path to 'packages/server' based on the location of this file (__dirname)
35
- const serverPackageDir = path.resolve(__dirname, "../../"); // Go up two levels from .../packages/server/src/__tests__
35
+ // Resolve the path to 'packages/server' based on the location of this file
36
+ // Use import.meta.url for cross-platform compatibility (works on Windows)
37
+ const __filename = fileURLToPath(import.meta.url);
38
+ const __dirname = path.dirname(__filename);
39
+ const serverPackageDir = path.resolve(__dirname, "../../"); // Go up two levels from .../packages/server/tests/harness
36
40
  process.env.SERVER_ROOT = serverPackageDir;
37
41
  console.log(
38
42
  `[E2E Test Setup] Temporarily set SERVER_ROOT=${process.env.SERVER_ROOT}`,