@malloy-publisher/server 0.0.198-dev → 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.
Files changed (87) hide show
  1. package/README.docker.md +135 -20
  2. package/README.md +15 -0
  3. package/build.ts +32 -1
  4. package/dist/app/api-doc.yaml +51 -0
  5. package/dist/app/assets/EnvironmentPage-Dpee_Kn6.js +1 -0
  6. package/dist/app/assets/HomePage-DLRWTNoL.js +1 -0
  7. package/dist/app/assets/MainPage-DsVt5QGM.js +2 -0
  8. package/dist/app/assets/ModelPage-AwAugZ37.js +1 -0
  9. package/dist/app/assets/PackagePage-XQ-EWGTC.js +1 -0
  10. package/dist/app/assets/RouteError-3Mv8JQw7.js +1 -0
  11. package/dist/app/assets/WorkbookPage-DHYYpcYc.js +1 -0
  12. package/dist/app/assets/{core-w79IMXAG.es-Bd0UlzOL.js → core-DfcpQGVP.es-DQggNOdX.js} +14 -14
  13. package/dist/app/assets/{index-C513UodQ.js → index-BUp81Qdm.js} +15 -15
  14. package/dist/app/assets/index-D1pdwrUW.js +1803 -0
  15. package/dist/app/assets/index-Dv5bF4Ii.js +451 -0
  16. package/dist/app/assets/{index.umd-BMeMPq_9.js → index.umd-CQH4LZU8.js} +1 -1
  17. package/dist/app/index.html +2 -3
  18. package/dist/default-publisher.config.json +23 -0
  19. package/dist/instrumentation.mjs +22 -3
  20. package/dist/server.mjs +1522 -651
  21. package/dist/service/schema_worker.mjs +61 -0
  22. package/package.json +11 -12
  23. package/publisher.config.example.bigquery.json +33 -0
  24. package/publisher.config.example.duckdb.json +23 -0
  25. package/publisher.config.json +1 -11
  26. package/src/config.spec.ts +306 -0
  27. package/src/config.ts +222 -2
  28. package/src/controller/compile.controller.ts +3 -1
  29. package/src/controller/connection.controller.ts +1 -1
  30. package/src/controller/model.controller.ts +8 -1
  31. package/src/controller/package.controller.ts +70 -29
  32. package/src/controller/query.controller.ts +3 -0
  33. package/src/default-publisher.config.json +23 -0
  34. package/src/errors.spec.ts +42 -0
  35. package/src/errors.ts +21 -0
  36. package/src/health.spec.ts +90 -0
  37. package/src/health.ts +73 -45
  38. package/src/instrumentation.ts +50 -0
  39. package/src/logger.ts +1 -3
  40. package/src/mcp/tools/discovery_tools.ts +6 -2
  41. package/src/mcp/tools/execute_query_tool.ts +12 -0
  42. package/src/path_safety.spec.ts +158 -0
  43. package/src/path_safety.ts +140 -0
  44. package/src/pg_helpers.spec.ts +226 -0
  45. package/src/pg_helpers.ts +129 -0
  46. package/src/server-old.ts +3 -23
  47. package/src/server.ts +54 -0
  48. package/src/service/connection.spec.ts +6 -4
  49. package/src/service/connection.ts +8 -3
  50. package/src/service/connection_config.ts +2 -2
  51. package/src/service/environment.ts +621 -176
  52. package/src/service/environment_admission.spec.ts +180 -0
  53. package/src/service/environment_store.ts +31 -0
  54. package/src/service/filter_integration.spec.ts +110 -0
  55. package/src/service/givens_integration.spec.ts +192 -0
  56. package/src/service/manifest_service.spec.ts +7 -2
  57. package/src/service/manifest_service.ts +8 -2
  58. package/src/service/materialization_service.ts +14 -3
  59. package/src/service/model.spec.ts +105 -0
  60. package/src/service/model.ts +91 -7
  61. package/src/service/package.spec.ts +11 -7
  62. package/src/service/package.ts +53 -56
  63. package/src/service/package_memory_governor.spec.ts +173 -0
  64. package/src/service/package_memory_governor.ts +233 -0
  65. package/src/service/package_race.spec.ts +208 -0
  66. package/src/service/process_stats_reporter.ts +169 -0
  67. package/src/service/schema_worker.ts +123 -0
  68. package/src/service/schema_worker_pool.ts +278 -0
  69. package/src/storage/StorageManager.ts +71 -11
  70. package/src/storage/duckdb/schema.ts +41 -0
  71. package/src/utils.ts +11 -0
  72. package/tests/harness/rest_e2e.ts +2 -2
  73. package/tests/integration/concurrent_environment/concurrent_environment.integration.spec.ts +235 -0
  74. package/tests/integration/concurrent_package/concurrent_package.integration.spec.ts +280 -0
  75. package/tests/integration/legacy_routes/legacy_routes.integration.spec.ts +259 -0
  76. package/tests/unit/duckdb/attached_databases.test.ts +5 -5
  77. package/tests/unit/duckdb/legacy_schema_migration.test.ts +194 -0
  78. package/tests/unit/storage/StorageManager.test.ts +166 -0
  79. package/dist/app/assets/EnvironmentPage-1j6QDWAy.js +0 -1
  80. package/dist/app/assets/HomePage-DMop21VG.js +0 -1
  81. package/dist/app/assets/MainPage-BbE8ETz1.js +0 -2
  82. package/dist/app/assets/ModelPage-D2jvfe3t.js +0 -1
  83. package/dist/app/assets/PackagePage-BbnhGoD3.js +0 -1
  84. package/dist/app/assets/RouteError-D3LGEZ3i.js +0 -1
  85. package/dist/app/assets/WorkbookPage-DttVIj4u.js +0 -1
  86. package/dist/app/assets/index-5K9YjIxF.js +0 -456
  87. package/dist/app/assets/index-DIgzgp69.js +0 -1742
package/src/config.ts CHANGED
@@ -1,9 +1,68 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
+ import { fileURLToPath } from "url";
3
4
  import { components } from "./api";
4
5
  import { API_PREFIX, PUBLISHER_CONFIG_NAME } from "./constants";
5
6
  import { logger } from "./logger";
6
7
 
8
+ /**
9
+ * Path to the publisher.config.json file shipped inside the published
10
+ * package. Used as a last-resort fallback so `npx @malloy-publisher/server`
11
+ * with no args still boots with the DuckDB-only sample packages.
12
+ *
13
+ * The file is copied next to the running module by `build.ts` at production
14
+ * build time. In a source/dev checkout it lives alongside this file.
15
+ */
16
+ const BUNDLED_DEFAULT_CONFIG_PATH = path.join(
17
+ path.dirname(fileURLToPath(import.meta.url)),
18
+ "default-publisher.config.json",
19
+ );
20
+
21
+ /**
22
+ * Decide which `publisher.config.json` to read.
23
+ *
24
+ * Precedence:
25
+ * 1. `--config <path>` (surfaced via `process.env.PUBLISHER_CONFIG_PATH`)
26
+ * 2. `<serverRoot>/publisher.config.json`
27
+ * 3. The bundled default shipped inside the package — ONLY when
28
+ * `process.env.PUBLISHER_USE_BUNDLED_DEFAULT === "true"`. server.ts
29
+ * sets that flag when the user passed neither `--server_root` nor
30
+ * `--config`, so `npx @malloy-publisher/server` with zero args
31
+ * boots into something usable. Callers that construct an
32
+ * EnvironmentStore programmatically (tests, embeds) don't get
33
+ * surprise filesystem fallbacks they didn't ask for.
34
+ *
35
+ * Returns `null` if step 1 was requested but the file doesn't exist —
36
+ * that's an explicit user mistake and the caller should surface it as
37
+ * an error rather than silently falling back.
38
+ */
39
+ function resolvePublisherConfigPath(serverRoot: string): {
40
+ path: string;
41
+ isBundledDefault: boolean;
42
+ } | null {
43
+ const explicitPath = process.env.PUBLISHER_CONFIG_PATH;
44
+ if (explicitPath && explicitPath.length > 0) {
45
+ if (!fs.existsSync(explicitPath)) {
46
+ return null;
47
+ }
48
+ return { path: explicitPath, isBundledDefault: false };
49
+ }
50
+
51
+ const serverRootPath = path.join(serverRoot, PUBLISHER_CONFIG_NAME);
52
+ if (fs.existsSync(serverRootPath)) {
53
+ return { path: serverRootPath, isBundledDefault: false };
54
+ }
55
+
56
+ if (
57
+ process.env.PUBLISHER_USE_BUNDLED_DEFAULT === "true" &&
58
+ fs.existsSync(BUNDLED_DEFAULT_CONFIG_PATH)
59
+ ) {
60
+ return { path: BUNDLED_DEFAULT_CONFIG_PATH, isBundledDefault: true };
61
+ }
62
+
63
+ return null;
64
+ }
65
+
7
66
  type FilesystemPath = `./${string}` | `../${string}` | `/${string}`;
8
67
  type GcsPath = `gs://${string}`;
9
68
  type ApiConnection = components["schemas"]["Connection"];
@@ -40,6 +99,132 @@ export type ProcessedPublisherConfig = {
40
99
  environments: ProcessedEnvironment[];
41
100
  };
42
101
 
102
+ /**
103
+ * Tunables for {@link PackageMemoryGovernor}. All values are sourced
104
+ * from environment variables at startup; see {@link getMemoryGovernorConfig}
105
+ * for parsing and defaults.
106
+ *
107
+ * The governor is admission control only: it polls process RSS on
108
+ * `checkIntervalMs` and toggles a single `isBackpressured` flag using
109
+ * a low/high-water hysteresis band. It does NOT evict, unload, or
110
+ * interrupt already-loaded packages — recovery is left to the kernel
111
+ * reclaiming pages as in-flight traffic completes.
112
+ */
113
+ export interface MemoryGovernorConfig {
114
+ /** Hard ceiling for process RSS in bytes (the OOM-relevant figure). */
115
+ maxMemoryBytes: number;
116
+ /** Fraction of `maxMemoryBytes` at which the governor activates back-pressure (new package loads start returning HTTP 503). Must be in (0, 1) and strictly greater than `lowWaterFraction`. */
117
+ highWaterFraction: number;
118
+ /** Fraction of `maxMemoryBytes` at which the governor clears back-pressure (new package loads admitted again). Must be in (0, 1) and strictly less than `highWaterFraction`; the gap is the hysteresis band that prevents flap. */
119
+ lowWaterFraction: number;
120
+ /** Polling cadence for the RSS sampler, in milliseconds. */
121
+ checkIntervalMs: number;
122
+ /** When true, RSS crossings flip the back-pressure flag. When false, the governor still samples and emits metrics but never rejects requests — useful for a monitoring-only rollout before enabling the 503 behaviour. */
123
+ backpressureEnabled: boolean;
124
+ }
125
+
126
+ const DEFAULT_HIGH_WATER_FRACTION = 0.8;
127
+ const DEFAULT_LOW_WATER_FRACTION = 0.7;
128
+ const DEFAULT_CHECK_INTERVAL_MS = 5_000;
129
+ const MIN_CHECK_INTERVAL_MS = 100;
130
+
131
+ function parseIntEnv(name: string): number | undefined {
132
+ const raw = process.env[name];
133
+ if (raw === undefined || raw.trim() === "") return undefined;
134
+ const value = Number.parseInt(raw, 10);
135
+ if (!Number.isFinite(value) || String(value) !== raw.trim()) {
136
+ throw new Error(
137
+ `Invalid value for ${name}: expected a base-10 integer, got "${raw}"`,
138
+ );
139
+ }
140
+ return value;
141
+ }
142
+
143
+ function parseFloatEnv(name: string): number | undefined {
144
+ const raw = process.env[name];
145
+ if (raw === undefined || raw.trim() === "") return undefined;
146
+ const value = Number.parseFloat(raw);
147
+ if (!Number.isFinite(value)) {
148
+ throw new Error(
149
+ `Invalid value for ${name}: expected a finite number, got "${raw}"`,
150
+ );
151
+ }
152
+ return value;
153
+ }
154
+
155
+ function parseBoolEnv(name: string): boolean | undefined {
156
+ const raw = process.env[name];
157
+ if (raw === undefined || raw.trim() === "") return undefined;
158
+ const normalised = raw.trim().toLowerCase();
159
+ if (["1", "true", "yes", "on"].includes(normalised)) return true;
160
+ if (["0", "false", "no", "off"].includes(normalised)) return false;
161
+ throw new Error(
162
+ `Invalid value for ${name}: expected a boolean (true/false), got "${raw}"`,
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Parse memory-governor settings from environment variables and return
168
+ * either a fully-validated config or `null` when the feature is
169
+ * disabled. The feature is disabled iff `PUBLISHER_MAX_MEMORY_BYTES`
170
+ * is unset or set to `0`.
171
+ *
172
+ * Throws at startup on malformed input so a typo in a k8s manifest
173
+ * surfaces as a loud failure rather than silently disabling the cap.
174
+ */
175
+ export const getMemoryGovernorConfig = (): MemoryGovernorConfig | null => {
176
+ const maxMemoryBytes = parseIntEnv("PUBLISHER_MAX_MEMORY_BYTES");
177
+ if (maxMemoryBytes === undefined || maxMemoryBytes === 0) {
178
+ return null;
179
+ }
180
+ if (maxMemoryBytes < 0) {
181
+ throw new Error(
182
+ `PUBLISHER_MAX_MEMORY_BYTES must be a positive integer (got ${maxMemoryBytes})`,
183
+ );
184
+ }
185
+
186
+ const highWaterFraction =
187
+ parseFloatEnv("PUBLISHER_MEMORY_HIGH_WATER_FRACTION") ??
188
+ DEFAULT_HIGH_WATER_FRACTION;
189
+ const lowWaterFraction =
190
+ parseFloatEnv("PUBLISHER_MEMORY_LOW_WATER_FRACTION") ??
191
+ DEFAULT_LOW_WATER_FRACTION;
192
+ const checkIntervalMs =
193
+ parseIntEnv("PUBLISHER_MEMORY_CHECK_INTERVAL_MS") ??
194
+ DEFAULT_CHECK_INTERVAL_MS;
195
+ const backpressureEnabled =
196
+ parseBoolEnv("PUBLISHER_MEMORY_BACKPRESSURE") ?? true;
197
+
198
+ if (highWaterFraction <= 0 || highWaterFraction >= 1) {
199
+ throw new Error(
200
+ `PUBLISHER_MEMORY_HIGH_WATER_FRACTION must be in (0, 1) (got ${highWaterFraction})`,
201
+ );
202
+ }
203
+ if (lowWaterFraction <= 0 || lowWaterFraction >= 1) {
204
+ throw new Error(
205
+ `PUBLISHER_MEMORY_LOW_WATER_FRACTION must be in (0, 1) (got ${lowWaterFraction})`,
206
+ );
207
+ }
208
+ if (lowWaterFraction >= highWaterFraction) {
209
+ throw new Error(
210
+ `PUBLISHER_MEMORY_LOW_WATER_FRACTION (${lowWaterFraction}) must be strictly less than PUBLISHER_MEMORY_HIGH_WATER_FRACTION (${highWaterFraction})`,
211
+ );
212
+ }
213
+ if (checkIntervalMs < MIN_CHECK_INTERVAL_MS) {
214
+ throw new Error(
215
+ `PUBLISHER_MEMORY_CHECK_INTERVAL_MS must be >= ${MIN_CHECK_INTERVAL_MS} (got ${checkIntervalMs})`,
216
+ );
217
+ }
218
+
219
+ return {
220
+ maxMemoryBytes,
221
+ highWaterFraction,
222
+ lowWaterFraction,
223
+ checkIntervalMs,
224
+ backpressureEnabled,
225
+ };
226
+ };
227
+
43
228
  function substituteEnvVars(value: string): string {
44
229
  const envVarPattern = /\$\{([A-Z_][A-Z0-9_]*)\}/g;
45
230
 
@@ -77,13 +262,30 @@ function processConfigValue(value: unknown): unknown {
77
262
  }
78
263
 
79
264
  export const getPublisherConfig = (serverRoot: string): PublisherConfig => {
80
- const publisherConfigPath = path.join(serverRoot, PUBLISHER_CONFIG_NAME);
81
- if (!fs.existsSync(publisherConfigPath)) {
265
+ const resolved = resolvePublisherConfigPath(serverRoot);
266
+ if (!resolved) {
267
+ if (
268
+ process.env.PUBLISHER_CONFIG_PATH &&
269
+ process.env.PUBLISHER_CONFIG_PATH.length > 0
270
+ ) {
271
+ // Explicit --config was given but the path didn't exist. Loud
272
+ // failure here so a typo in the flag doesn't silently boot the
273
+ // server with an empty environment list.
274
+ logger.error(
275
+ `--config path not found: ${process.env.PUBLISHER_CONFIG_PATH}. Using default empty config.`,
276
+ );
277
+ }
82
278
  return {
83
279
  frozenConfig: false,
84
280
  environments: [],
85
281
  };
86
282
  }
283
+ const publisherConfigPath = resolved.path;
284
+ if (resolved.isBundledDefault) {
285
+ logger.info(
286
+ `No publisher.config.json found at ${path.join(serverRoot, PUBLISHER_CONFIG_NAME)}; falling back to bundled DuckDB-only default. Pass --config <path> or place a config in the server root to override.`,
287
+ );
288
+ }
87
289
 
88
290
  let rawConfig: unknown;
89
291
  try {
@@ -108,6 +310,24 @@ export const getPublisherConfig = (serverRoot: string): PublisherConfig => {
108
310
  // Process environment variables in config values
109
311
  const processedConfig = processConfigValue(rawConfig);
110
312
 
313
+ // TODO: Remove this during projects cleanup
314
+ // Back-compat: the top-level key was renamed `projects` → `environments`.
315
+ // If a config still uses the old key, accept it once with a deprecation
316
+ // warning so existing on-disk configs don't silently parse as empty.
317
+ if (
318
+ processedConfig &&
319
+ typeof processedConfig === "object" &&
320
+ !("environments" in processedConfig) &&
321
+ "projects" in processedConfig
322
+ ) {
323
+ logger.warn(
324
+ `${PUBLISHER_CONFIG_NAME} uses deprecated "projects" key; rename to "environments".`,
325
+ );
326
+ (processedConfig as Record<string, unknown>).environments = (
327
+ processedConfig as Record<string, unknown>
328
+ ).projects;
329
+ }
330
+
111
331
  if (
112
332
  processedConfig &&
113
333
  typeof processedConfig === "object" &&
@@ -1,4 +1,4 @@
1
- import type { LogMessage } from "@malloydata/malloy";
1
+ import type { GivenValue, LogMessage } from "@malloydata/malloy";
2
2
  import { EnvironmentStore } from "../service/environment_store";
3
3
 
4
4
  export class CompileController {
@@ -14,6 +14,7 @@ export class CompileController {
14
14
  modelName: string,
15
15
  source: string,
16
16
  includeSql: boolean = false,
17
+ givens?: Record<string, GivenValue>,
17
18
  ): Promise<{ status: string; problems: LogMessage[]; sql?: string }> {
18
19
  const environment = await this.environmentStore.getEnvironment(
19
20
  environmentName,
@@ -24,6 +25,7 @@ export class CompileController {
24
25
  modelName,
25
26
  source,
26
27
  includeSql,
28
+ givens,
27
29
  );
28
30
 
29
31
  // Determine overall status based on presence of errors
@@ -91,7 +91,7 @@ function validateAdminAuthoredConnection(
91
91
  ): void {
92
92
  if (connectionName === "duckdb" || connectionConfig.name === "duckdb") {
93
93
  throw new BadRequestError(
94
- "DuckDB connection name cannot be 'duckdb'; it is reserved for Publisher package sandboxes.",
94
+ "Connection name 'duckdb' is reserved for per-package sandboxes. Choose a different name for environment-level DuckDB connections (e.g. 'shared_duckdb').",
95
95
  );
96
96
  }
97
97
 
@@ -2,6 +2,7 @@ import { components } from "../api";
2
2
  import { ModelNotFoundError } from "../errors";
3
3
  import { EnvironmentStore } from "../service/environment_store";
4
4
  import type { FilterParams } from "../service/filter";
5
+ import type { GivenValue } from "@malloydata/malloy";
5
6
 
6
7
  type ApiNotebook = components["schemas"]["Notebook"];
7
8
  type ApiModel = components["schemas"]["Model"];
@@ -97,6 +98,7 @@ export class ModelController {
97
98
  cellIndex: number,
98
99
  filterParams?: FilterParams,
99
100
  bypassFilters?: boolean,
101
+ givens?: Record<string, GivenValue>,
100
102
  ): Promise<{
101
103
  type: "code" | "markdown";
102
104
  text: string;
@@ -117,6 +119,11 @@ export class ModelController {
117
119
  throw new ModelNotFoundError(`${notebookPath} is a model`);
118
120
  }
119
121
 
120
- return model.executeNotebookCell(cellIndex, filterParams, bypassFilters);
122
+ return model.executeNotebookCell(
123
+ cellIndex,
124
+ filterParams,
125
+ bypassFilters,
126
+ givens,
127
+ );
121
128
  }
122
129
  }
@@ -1,6 +1,4 @@
1
- import * as path from "path";
2
1
  import { components } from "../api";
3
- import { PUBLISHER_DATA_DIR } from "../constants";
4
2
  import { BadRequestError, FrozenConfigError } from "../errors";
5
3
  import { logger } from "../logger";
6
4
  import { EnvironmentStore } from "../service/environment_store";
@@ -37,15 +35,38 @@ export class PackageController {
37
35
  environmentName,
38
36
  false,
39
37
  );
40
- const _package = await environment.getPackage(packageName, reload);
41
- const packageLocation = _package.getPackageMetadata().location;
42
- if (reload && packageLocation) {
43
- await this.downloadPackage(
44
- environmentName,
45
- packageName,
46
- packageLocation,
47
- );
38
+
39
+ if (reload) {
40
+ // Resolve the package's source location from the currently-cached
41
+ // metadata WITHOUT triggering a stale-state reload. If a `location`
42
+ // is set, route the reload through `installPackage` so that
43
+ // download-then-load happens atomically; otherwise fall back to an
44
+ // in-place reload of the existing on-disk content.
45
+ let location: string | undefined;
46
+ try {
47
+ const cached = await environment.getPackage(packageName, false);
48
+ location = cached.getPackageMetadata().location;
49
+ } catch {
50
+ // Not previously loaded — nothing to reinstall from.
51
+ }
52
+ if (location) {
53
+ const reinstalled = await environment.installPackage(
54
+ packageName,
55
+ (stagingPath) =>
56
+ this.downloadInto(
57
+ environmentName,
58
+ packageName,
59
+ location,
60
+ stagingPath,
61
+ ),
62
+ );
63
+ return reinstalled.getPackageMetadata();
64
+ }
65
+ const _package = await environment.getPackage(packageName, true);
66
+ return _package.getPackageMetadata();
48
67
  }
68
+
69
+ const _package = await environment.getPackage(packageName, false);
49
70
  return _package.getPackageMetadata();
50
71
  }
51
72
 
@@ -60,21 +81,32 @@ export class PackageController {
60
81
  if (!body.name) {
61
82
  throw new BadRequestError("Package name is required");
62
83
  }
84
+ const packageName = body.name;
63
85
  const environment = await this.environmentStore.getEnvironment(
64
86
  environmentName,
65
87
  false,
66
88
  );
89
+ let result;
67
90
  if (body.location) {
68
- await this.downloadPackage(environmentName, body.name, body.location);
91
+ const bodyLocation = body.location;
92
+ result = await environment.installPackage(packageName, (stagingPath) =>
93
+ this.downloadInto(
94
+ environmentName,
95
+ packageName,
96
+ bodyLocation,
97
+ stagingPath,
98
+ ),
99
+ );
100
+ } else {
101
+ result = await environment.addPackage(packageName);
69
102
  }
70
- const result = await environment.addPackage(body.name);
71
103
  await this.environmentStore.addPackageToDatabase(
72
104
  environmentName,
73
- body.name,
105
+ packageName,
74
106
  );
75
107
 
76
108
  if (options?.autoLoadManifest === true) {
77
- await this.tryLoadExistingManifest(environmentName, body.name);
109
+ await this.tryLoadExistingManifest(environmentName, packageName);
78
110
  }
79
111
 
80
112
  return result;
@@ -151,12 +183,20 @@ export class PackageController {
151
183
  false,
152
184
  );
153
185
  if (body.location) {
154
- await this.downloadPackage(
155
- environmentName,
156
- packageName,
157
- body.location,
186
+ // Re-install: stream the new content into a staging dir (no lock)
187
+ // and atomically swap it in (under the lock).
188
+ const bodyLocation = body.location;
189
+ await environment.installPackage(packageName, (stagingPath) =>
190
+ this.downloadInto(
191
+ environmentName,
192
+ packageName,
193
+ bodyLocation,
194
+ stagingPath,
195
+ ),
158
196
  );
159
197
  }
198
+ // Apply metadata changes (publisher.json) under the same per-package
199
+ // mutex via `Environment.updatePackage`.
160
200
  const result = await environment.updatePackage(packageName, body);
161
201
  await this.environmentStore.addPackageToDatabase(
162
202
  environmentName,
@@ -166,17 +206,18 @@ export class PackageController {
166
206
  return result;
167
207
  }
168
208
 
169
- private async downloadPackage(
209
+ /**
210
+ * Run the right downloader for the given location into `targetPath`.
211
+ * This used to point at the canonical package directory, but the
212
+ * install pipeline now passes a sibling staging dir so the long-running
213
+ * download doesn't hold the per-package mutex.
214
+ */
215
+ private async downloadInto(
170
216
  environmentName: string,
171
217
  packageName: string,
172
218
  packageLocation: string,
219
+ targetPath: string,
173
220
  ) {
174
- const absoluteTargetPath = path.join(
175
- this.environmentStore.serverRootPath,
176
- PUBLISHER_DATA_DIR,
177
- environmentName,
178
- packageName,
179
- );
180
221
  const isCompressedFile = packageLocation.endsWith(".zip");
181
222
  if (
182
223
  packageLocation.startsWith("https://") ||
@@ -184,20 +225,20 @@ export class PackageController {
184
225
  ) {
185
226
  await this.environmentStore.downloadGitHubDirectory(
186
227
  packageLocation,
187
- absoluteTargetPath,
228
+ targetPath,
188
229
  );
189
230
  } else if (packageLocation.startsWith("gs://")) {
190
231
  await this.environmentStore.downloadGcsDirectory(
191
232
  packageLocation,
192
233
  environmentName,
193
- absoluteTargetPath,
234
+ targetPath,
194
235
  isCompressedFile,
195
236
  );
196
237
  } else if (packageLocation.startsWith("s3://")) {
197
238
  await this.environmentStore.downloadS3Directory(
198
239
  packageLocation,
199
240
  environmentName,
200
- absoluteTargetPath,
241
+ targetPath,
201
242
  isCompressedFile,
202
243
  );
203
244
  }
@@ -207,7 +248,7 @@ export class PackageController {
207
248
  // so we need to mount them on the right place.
208
249
  await this.environmentStore.mountLocalDirectory(
209
250
  packageLocation,
210
- absoluteTargetPath,
251
+ targetPath,
211
252
  environmentName,
212
253
  packageName,
213
254
  );
@@ -4,6 +4,7 @@ import { API_PREFIX } from "../constants";
4
4
  import { ModelNotFoundError } from "../errors";
5
5
  import { EnvironmentStore } from "../service/environment_store";
6
6
  import type { FilterParams } from "../service/filter";
7
+ import type { GivenValue } from "@malloydata/malloy";
7
8
 
8
9
  type ApiQuery = components["schemas"]["QueryResult"];
9
10
 
@@ -32,6 +33,7 @@ export class QueryController {
32
33
  compactJson: boolean = false,
33
34
  filterParams?: FilterParams,
34
35
  bypassFilters?: boolean,
36
+ givens?: Record<string, GivenValue>,
35
37
  ): Promise<ApiQuery> {
36
38
  const environment = await this.environmentStore.getEnvironment(
37
39
  environmentName,
@@ -49,6 +51,7 @@ export class QueryController {
49
51
  query,
50
52
  filterParams,
51
53
  bypassFilters,
54
+ givens,
52
55
  );
53
56
  const renderLogs = validateRenderTags(result);
54
57
  return {
@@ -0,0 +1,23 @@
1
+ {
2
+ "frozenConfig": false,
3
+ "environments": [
4
+ {
5
+ "name": "malloy-samples",
6
+ "packages": [
7
+ {
8
+ "name": "ecommerce",
9
+ "location": "https://github.com/credibledata/malloy-samples/tree/main/ecommerce"
10
+ },
11
+ {
12
+ "name": "imdb",
13
+ "location": "https://github.com/credibledata/malloy-samples/tree/main/imdb"
14
+ },
15
+ {
16
+ "name": "faa",
17
+ "location": "https://github.com/credibledata/malloy-samples/tree/main/faa"
18
+ }
19
+ ],
20
+ "connections": []
21
+ }
22
+ ]
23
+ }
@@ -0,0 +1,42 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import {
3
+ BadRequestError,
4
+ ConnectionAuthError,
5
+ ConnectionError,
6
+ internalErrorToHttpError,
7
+ } from "./errors";
8
+
9
+ describe("internalErrorToHttpError", () => {
10
+ it("maps ConnectionAuthError to 422", () => {
11
+ const { status, json } = internalErrorToHttpError(
12
+ new ConnectionAuthError("creds rejected for db_x"),
13
+ );
14
+ expect(status).toBe(422);
15
+ expect(json).toEqual({
16
+ code: 422,
17
+ message: "creds rejected for db_x",
18
+ });
19
+ });
20
+
21
+ it("maps BadRequestError to 400", () => {
22
+ const { status, json } = internalErrorToHttpError(
23
+ new BadRequestError("bad input"),
24
+ );
25
+ expect(status).toBe(400);
26
+ expect(json).toEqual({ code: 400, message: "bad input" });
27
+ });
28
+
29
+ it("maps ConnectionError to 502 (distinct from auth, still retryable)", () => {
30
+ const { status, json } = internalErrorToHttpError(
31
+ new ConnectionError("upstream broken"),
32
+ );
33
+ expect(status).toBe(502);
34
+ expect(json).toEqual({ code: 502, message: "upstream broken" });
35
+ });
36
+
37
+ it("falls through to 500 for unrecognized errors", () => {
38
+ const { status, json } = internalErrorToHttpError(new Error("boom"));
39
+ expect(status).toBe(500);
40
+ expect(json.message).toBe("boom");
41
+ });
42
+ });
package/src/errors.ts CHANGED
@@ -16,6 +16,8 @@ export function internalErrorToHttpError(error: Error) {
16
16
  return httpError(400, error.message);
17
17
  } else if (error instanceof ConnectionNotFoundError) {
18
18
  return httpError(404, error.message);
19
+ } else if (error instanceof ConnectionAuthError) {
20
+ return httpError(422, error.message);
19
21
  } else if (error instanceof ModelCompilationError) {
20
22
  return httpError(424, error.message);
21
23
  } else if (error instanceof ConnectionError) {
@@ -26,6 +28,8 @@ export function internalErrorToHttpError(error: Error) {
26
28
  return httpError(409, error.message);
27
29
  } else if (error instanceof InvalidStateTransitionError) {
28
30
  return httpError(409, error.message);
31
+ } else if (error instanceof ServiceUnavailableError) {
32
+ return httpError(503, error.message);
29
33
  } else {
30
34
  return httpError(500, error.message);
31
35
  }
@@ -83,6 +87,12 @@ export class ConnectionError extends Error {
83
87
  }
84
88
  }
85
89
 
90
+ export class ConnectionAuthError extends Error {
91
+ constructor(message: string) {
92
+ super(message);
93
+ }
94
+ }
95
+
86
96
  export class ModelCompilationError extends Error {
87
97
  constructor(error: MalloyError) {
88
98
  super(error.message);
@@ -114,3 +124,14 @@ export class InvalidStateTransitionError extends Error {
114
124
  super(message);
115
125
  }
116
126
  }
127
+
128
+ /**
129
+ * Thrown when the publisher is temporarily refusing a request to keep
130
+ * RSS under the configured `PUBLISHER_MAX_MEMORY_BYTES` cap. Mapped to
131
+ * HTTP 503 so an upstream proxy / client can retry with back-off.
132
+ */
133
+ export class ServiceUnavailableError extends Error {
134
+ constructor(message: string) {
135
+ super(message);
136
+ }
137
+ }