@malloy-publisher/server 0.0.198-dev → 0.0.198

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 (68) hide show
  1. package/README.docker.md +135 -20
  2. package/README.md +15 -0
  3. package/build.ts +16 -0
  4. package/dist/app/assets/EnvironmentPage-C7rtH4mC.js +1 -0
  5. package/dist/app/assets/HomePage-DwkH7OrS.js +1 -0
  6. package/dist/app/assets/MainPage-D38LtZDV.js +2 -0
  7. package/dist/app/assets/ModelPage-DOol8Mz7.js +1 -0
  8. package/dist/app/assets/PackagePage-0tgzA_kO.js +1 -0
  9. package/dist/app/assets/RouteError-BaMsOSly.js +1 -0
  10. package/dist/app/assets/WorkbookPage-Cx4SePkx.js +1 -0
  11. package/dist/app/assets/{core-w79IMXAG.es-Bd0UlzOL.js → core-CbsC6R_Y.es-Cwf6asf3.js} +14 -14
  12. package/dist/app/assets/index-DL6BZTuw.js +1803 -0
  13. package/dist/app/assets/{index-C513UodQ.js → index-DNofXMxi.js} +15 -15
  14. package/dist/app/assets/index-U38AyjJL.js +451 -0
  15. package/dist/app/assets/{index.umd-BMeMPq_9.js → index.umd-B68wGGkM.js} +1 -1
  16. package/dist/app/index.html +2 -3
  17. package/dist/default-publisher.config.json +23 -0
  18. package/dist/instrumentation.mjs +1 -3
  19. package/dist/server.mjs +1104 -567
  20. package/package.json +11 -12
  21. package/publisher.config.example.bigquery.json +33 -0
  22. package/publisher.config.example.duckdb.json +23 -0
  23. package/publisher.config.json +1 -11
  24. package/src/config.spec.ts +306 -0
  25. package/src/config.ts +222 -2
  26. package/src/controller/connection.controller.ts +1 -1
  27. package/src/controller/package.controller.ts +70 -29
  28. package/src/default-publisher.config.json +23 -0
  29. package/src/errors.spec.ts +42 -0
  30. package/src/errors.ts +21 -0
  31. package/src/logger.ts +1 -3
  32. package/src/mcp/tools/discovery_tools.ts +6 -2
  33. package/src/path_safety.spec.ts +158 -0
  34. package/src/path_safety.ts +140 -0
  35. package/src/pg_helpers.spec.ts +226 -0
  36. package/src/pg_helpers.ts +129 -0
  37. package/src/server-old.ts +3 -23
  38. package/src/server.ts +33 -0
  39. package/src/service/connection.spec.ts +6 -4
  40. package/src/service/connection.ts +8 -3
  41. package/src/service/connection_config.ts +2 -2
  42. package/src/service/environment.ts +619 -175
  43. package/src/service/environment_admission.spec.ts +180 -0
  44. package/src/service/environment_store.ts +22 -0
  45. package/src/service/manifest_service.spec.ts +7 -2
  46. package/src/service/manifest_service.ts +8 -2
  47. package/src/service/materialization_service.ts +14 -3
  48. package/src/service/package.ts +4 -3
  49. package/src/service/package_memory_governor.spec.ts +173 -0
  50. package/src/service/package_memory_governor.ts +233 -0
  51. package/src/service/package_race.spec.ts +208 -0
  52. package/src/storage/StorageManager.ts +71 -11
  53. package/src/storage/duckdb/schema.ts +41 -0
  54. package/src/utils.ts +11 -0
  55. package/tests/harness/rest_e2e.ts +2 -2
  56. package/tests/integration/legacy_routes/legacy_routes.integration.spec.ts +259 -0
  57. package/tests/unit/duckdb/attached_databases.test.ts +5 -5
  58. package/tests/unit/duckdb/legacy_schema_migration.test.ts +194 -0
  59. package/tests/unit/storage/StorageManager.test.ts +166 -0
  60. package/dist/app/assets/EnvironmentPage-1j6QDWAy.js +0 -1
  61. package/dist/app/assets/HomePage-DMop21VG.js +0 -1
  62. package/dist/app/assets/MainPage-BbE8ETz1.js +0 -2
  63. package/dist/app/assets/ModelPage-D2jvfe3t.js +0 -1
  64. package/dist/app/assets/PackagePage-BbnhGoD3.js +0 -1
  65. package/dist/app/assets/RouteError-D3LGEZ3i.js +0 -1
  66. package/dist/app/assets/WorkbookPage-DttVIj4u.js +0 -1
  67. package/dist/app/assets/index-5K9YjIxF.js +0 -456
  68. package/dist/app/assets/index-DIgzgp69.js +0 -1742
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@malloy-publisher/server",
3
3
  "description": "Malloy Publisher Server",
4
- "version": "0.0.198-dev",
4
+ "version": "0.0.198",
5
5
  "main": "dist/server.mjs",
6
6
  "bin": {
7
7
  "malloy-publisher": "dist/server.mjs"
@@ -34,16 +34,16 @@
34
34
  "@azure/identity": "^4.13.0",
35
35
  "@azure/storage-blob": "^12.26.0",
36
36
  "@google-cloud/storage": "^7.16.0",
37
- "@malloydata/db-bigquery": "^0.0.383",
38
- "@malloydata/db-databricks": "^0.0.383",
39
- "@malloydata/db-duckdb": "^0.0.383",
40
- "@malloydata/db-mysql": "^0.0.383",
41
- "@malloydata/db-postgres": "^0.0.383",
42
- "@malloydata/db-snowflake": "^0.0.383",
43
- "@malloydata/db-trino": "^0.0.383",
44
- "@malloydata/malloy": "^0.0.383",
45
- "@malloydata/malloy-sql": "^0.0.383",
46
- "@malloydata/render-validator": "^0.0.383",
37
+ "@malloydata/db-bigquery": "^0.0.394",
38
+ "@malloydata/db-databricks": "^0.0.394",
39
+ "@malloydata/db-duckdb": "^0.0.394",
40
+ "@malloydata/db-mysql": "^0.0.394",
41
+ "@malloydata/db-postgres": "^0.0.394",
42
+ "@malloydata/db-snowflake": "^0.0.394",
43
+ "@malloydata/db-trino": "^0.0.394",
44
+ "@malloydata/malloy": "^0.0.394",
45
+ "@malloydata/malloy-sql": "^0.0.394",
46
+ "@malloydata/render-validator": "^0.0.394",
47
47
  "@modelcontextprotocol/sdk": "^1.13.2",
48
48
  "@opentelemetry/api": "^1.9.0",
49
49
  "@opentelemetry/auto-instrumentations-node": "^0.57.0",
@@ -68,7 +68,6 @@
68
68
  "node-cron": "^3.0.3",
69
69
  "recursive-readdir": "^2.2.3",
70
70
  "simple-git": "^3.28.0",
71
- "trino": "^1.1.1",
72
71
  "uuid": "^11.0.3"
73
72
  },
74
73
  "devDependencies": {
@@ -0,0 +1,33 @@
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
+ "name": "bigquery-hackernews",
21
+ "location": "https://github.com/credibledata/malloy-samples/tree/main/bigquery-hackernews"
22
+ }
23
+ ],
24
+ "connections": [
25
+ {
26
+ "name": "bigquery",
27
+ "type": "bigquery",
28
+ "bigqueryConnection": {}
29
+ }
30
+ ]
31
+ }
32
+ ]
33
+ }
@@ -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
+ }
@@ -15,19 +15,9 @@
15
15
  {
16
16
  "name": "faa",
17
17
  "location": "https://github.com/credibledata/malloy-samples/tree/main/faa"
18
- },
19
- {
20
- "name": "bigquery-hackernews",
21
- "location": "https://github.com/credibledata/malloy-samples/tree/main/bigquery-hackernews"
22
18
  }
23
19
  ],
24
- "connections": [
25
- {
26
- "name": "bigquery",
27
- "type": "bigquery",
28
- "bigqueryConnection": {}
29
- }
30
- ]
20
+ "connections": []
31
21
  }
32
22
  ]
33
23
  }
@@ -856,3 +856,309 @@ describe("Config Environment Variable Substitution", () => {
856
856
  });
857
857
  });
858
858
  });
859
+
860
+ // TODO: Remove this during projects cleanup
861
+ describe("Config legacy 'projects' key back-compat", () => {
862
+ const testServerRoot = path.join(process.cwd(), "test-temp-legacy-config");
863
+ const configPath = path.join(testServerRoot, PUBLISHER_CONFIG_NAME);
864
+
865
+ beforeEach(() => {
866
+ if (!fs.existsSync(testServerRoot)) {
867
+ fs.mkdirSync(testServerRoot, { recursive: true });
868
+ }
869
+ });
870
+
871
+ afterEach(() => {
872
+ if (fs.existsSync(configPath)) {
873
+ fs.unlinkSync(configPath);
874
+ }
875
+ if (fs.existsSync(testServerRoot)) {
876
+ fs.rmdirSync(testServerRoot, { recursive: true });
877
+ }
878
+ });
879
+
880
+ it("reads from legacy 'projects' key when 'environments' is absent", async () => {
881
+ // Pre-rename on-disk shape: top-level key is `projects`, not
882
+ // `environments`. Without back-compat this silently parses as empty.
883
+ const legacyConfig = {
884
+ frozenConfig: false,
885
+ projects: [
886
+ {
887
+ name: "legacy-env",
888
+ packages: [
889
+ {
890
+ name: "p1",
891
+ location: "./packages/p1",
892
+ },
893
+ ],
894
+ },
895
+ ],
896
+ };
897
+
898
+ fs.writeFileSync(configPath, JSON.stringify(legacyConfig, null, 2));
899
+
900
+ // Spy on logger.warn so we can assert the deprecation message fired.
901
+ const { logger } = await import("./logger");
902
+ const originalWarn = logger.warn;
903
+ const warnings: string[] = [];
904
+ logger.warn = ((msg: unknown, ..._rest: unknown[]) => {
905
+ warnings.push(typeof msg === "string" ? msg : String(msg));
906
+ return logger;
907
+ }) as typeof logger.warn;
908
+
909
+ try {
910
+ const result = getPublisherConfig(testServerRoot);
911
+
912
+ expect(result.environments.length).toBe(1);
913
+ expect(result.environments[0].name).toBe("legacy-env");
914
+ expect(result.environments[0].packages[0].name).toBe("p1");
915
+
916
+ expect(
917
+ warnings.some((w) => w.includes('uses deprecated "projects" key')),
918
+ ).toBe(true);
919
+ } finally {
920
+ logger.warn = originalWarn;
921
+ }
922
+ });
923
+
924
+ it("prefers the new 'environments' key when both are present", async () => {
925
+ const config = {
926
+ frozenConfig: false,
927
+ environments: [
928
+ {
929
+ name: "new-env",
930
+ packages: [{ name: "p1", location: "./packages/p1" }],
931
+ },
932
+ ],
933
+ projects: [
934
+ {
935
+ name: "should-be-ignored",
936
+ packages: [{ name: "p2", location: "./packages/p2" }],
937
+ },
938
+ ],
939
+ };
940
+
941
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
942
+
943
+ const { logger } = await import("./logger");
944
+ const originalWarn = logger.warn;
945
+ const warnings: string[] = [];
946
+ logger.warn = ((msg: unknown, ..._rest: unknown[]) => {
947
+ warnings.push(typeof msg === "string" ? msg : String(msg));
948
+ return logger;
949
+ }) as typeof logger.warn;
950
+
951
+ try {
952
+ const result = getPublisherConfig(testServerRoot);
953
+
954
+ expect(result.environments.length).toBe(1);
955
+ expect(result.environments[0].name).toBe("new-env");
956
+
957
+ // No deprecation warning should fire when `environments` is present.
958
+ expect(
959
+ warnings.some((w) => w.includes('uses deprecated "projects" key')),
960
+ ).toBe(false);
961
+ } finally {
962
+ logger.warn = originalWarn;
963
+ }
964
+ });
965
+ });
966
+
967
+ describe("Committed example configs", () => {
968
+ const serverDir = path.resolve(__dirname, "..");
969
+
970
+ it.each([
971
+ ["publisher.config.json", false],
972
+ ["publisher.config.example.duckdb.json", false],
973
+ ["publisher.config.example.bigquery.json", true],
974
+ ])(
975
+ "%s parses as a valid PublisherConfig",
976
+ (filename, expectsBigQueryConnection) => {
977
+ const filePath = path.join(serverDir, filename);
978
+ expect(fs.existsSync(filePath)).toBe(true);
979
+
980
+ const raw = fs.readFileSync(filePath, "utf-8");
981
+ const parsed = JSON.parse(raw) as PublisherConfig;
982
+
983
+ expect(parsed).toHaveProperty("environments");
984
+ expect(Array.isArray(parsed.environments)).toBe(true);
985
+ expect(parsed.environments.length).toBeGreaterThan(0);
986
+
987
+ const env = parsed.environments[0];
988
+ expect(env.name).toBeTruthy();
989
+ expect(Array.isArray(env.packages)).toBe(true);
990
+ expect(env.packages.length).toBeGreaterThan(0);
991
+
992
+ if (expectsBigQueryConnection) {
993
+ expect(
994
+ (env.connections ?? []).some((c) => c.type === "bigquery"),
995
+ ).toBe(true);
996
+ expect(
997
+ env.packages.some((p) => p.name === "bigquery-hackernews"),
998
+ ).toBe(true);
999
+ } else {
1000
+ expect(
1001
+ env.packages.some((p) => p.name === "bigquery-hackernews"),
1002
+ ).toBe(false);
1003
+ }
1004
+ },
1005
+ );
1006
+ });
1007
+
1008
+ describe("Config path resolution (--config and bundled default)", () => {
1009
+ const emptyServerRoot = path.join(process.cwd(), "test-empty-server-root");
1010
+ const customConfigPath = path.join(
1011
+ process.cwd(),
1012
+ "test-custom-publisher.config.json",
1013
+ );
1014
+
1015
+ beforeEach(() => {
1016
+ if (!fs.existsSync(emptyServerRoot)) {
1017
+ fs.mkdirSync(emptyServerRoot, { recursive: true });
1018
+ }
1019
+ delete process.env.PUBLISHER_CONFIG_PATH;
1020
+ delete process.env.PUBLISHER_USE_BUNDLED_DEFAULT;
1021
+ });
1022
+
1023
+ afterEach(() => {
1024
+ delete process.env.PUBLISHER_CONFIG_PATH;
1025
+ delete process.env.PUBLISHER_USE_BUNDLED_DEFAULT;
1026
+ if (fs.existsSync(customConfigPath)) {
1027
+ fs.unlinkSync(customConfigPath);
1028
+ }
1029
+ if (fs.existsSync(emptyServerRoot)) {
1030
+ fs.rmSync(emptyServerRoot, { recursive: true, force: true });
1031
+ }
1032
+ });
1033
+
1034
+ it("falls back to the bundled default config when serverRoot has no publisher.config.json and bundled-default opt-in is set", () => {
1035
+ // The bundled default is opt-in (server.ts sets the env var when
1036
+ // the user passed neither --server_root nor --config) so embedded
1037
+ // callers don't get a surprise filesystem read.
1038
+ process.env.PUBLISHER_USE_BUNDLED_DEFAULT = "true";
1039
+ const result = getPublisherConfig(emptyServerRoot);
1040
+
1041
+ expect(result.environments.length).toBeGreaterThan(0);
1042
+ const env = result.environments[0];
1043
+ expect(env.name).toBe("malloy-samples");
1044
+ expect(env.packages.some((p) => p.name === "ecommerce")).toBe(true);
1045
+ expect(env.packages.some((p) => p.name === "bigquery-hackernews")).toBe(
1046
+ false,
1047
+ );
1048
+ });
1049
+
1050
+ it("does NOT fall back to the bundled default when opt-in flag is unset (programmatic construction)", () => {
1051
+ // No PUBLISHER_USE_BUNDLED_DEFAULT, no PUBLISHER_CONFIG_PATH, no
1052
+ // file in emptyServerRoot — result should be the original empty
1053
+ // shape, preserving prior behavior for embeds and tests.
1054
+ const result = getPublisherConfig(emptyServerRoot);
1055
+ expect(result.environments).toEqual([]);
1056
+ });
1057
+
1058
+ it("honors PUBLISHER_CONFIG_PATH (set by --config) over the server root", () => {
1059
+ const customConfig = {
1060
+ frozenConfig: false,
1061
+ environments: [
1062
+ { name: "custom-env", packages: [{ name: "foo", location: "/x" }] },
1063
+ ],
1064
+ };
1065
+ fs.writeFileSync(customConfigPath, JSON.stringify(customConfig));
1066
+ process.env.PUBLISHER_CONFIG_PATH = customConfigPath;
1067
+
1068
+ const result = getPublisherConfig(emptyServerRoot);
1069
+ expect(result.environments[0].name).toBe("custom-env");
1070
+ });
1071
+
1072
+ it("returns empty environments (not the bundled default) when --config points at a missing file", () => {
1073
+ process.env.PUBLISHER_CONFIG_PATH = path.join(
1074
+ process.cwd(),
1075
+ "does-not-exist.json",
1076
+ );
1077
+ // Even with bundled-default opt-in, --config with a missing target
1078
+ // is a user error and we don't paper over it.
1079
+ process.env.PUBLISHER_USE_BUNDLED_DEFAULT = "true";
1080
+ const result = getPublisherConfig(emptyServerRoot);
1081
+ expect(result.environments).toEqual([]);
1082
+ });
1083
+ });
1084
+
1085
+ describe("getMemoryGovernorConfig", () => {
1086
+ const GOVERNOR_ENV_VARS = [
1087
+ "PUBLISHER_MAX_MEMORY_BYTES",
1088
+ "PUBLISHER_MEMORY_HIGH_WATER_FRACTION",
1089
+ "PUBLISHER_MEMORY_LOW_WATER_FRACTION",
1090
+ "PUBLISHER_MEMORY_CHECK_INTERVAL_MS",
1091
+ "PUBLISHER_MEMORY_BACKPRESSURE",
1092
+ ];
1093
+
1094
+ beforeEach(() => {
1095
+ for (const v of GOVERNOR_ENV_VARS) delete process.env[v];
1096
+ });
1097
+ afterEach(() => {
1098
+ for (const v of GOVERNOR_ENV_VARS) delete process.env[v];
1099
+ });
1100
+
1101
+ it("returns null when PUBLISHER_MAX_MEMORY_BYTES is unset", async () => {
1102
+ const { getMemoryGovernorConfig } = await import("./config");
1103
+ expect(getMemoryGovernorConfig()).toBeNull();
1104
+ });
1105
+
1106
+ it("parses defaults when only PUBLISHER_MAX_MEMORY_BYTES is set", async () => {
1107
+ process.env.PUBLISHER_MAX_MEMORY_BYTES = String(2 * 1024 * 1024 * 1024);
1108
+ const { getMemoryGovernorConfig } = await import("./config");
1109
+ const cfg = getMemoryGovernorConfig();
1110
+ expect(cfg).not.toBeNull();
1111
+ expect(cfg!.maxMemoryBytes).toBe(2 * 1024 * 1024 * 1024);
1112
+ expect(cfg!.backpressureEnabled).toBe(true);
1113
+ expect(cfg!.highWaterFraction).toBeGreaterThan(cfg!.lowWaterFraction);
1114
+ });
1115
+
1116
+ it("honours fraction and interval overrides", async () => {
1117
+ process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
1118
+ process.env.PUBLISHER_MEMORY_HIGH_WATER_FRACTION = "0.85";
1119
+ process.env.PUBLISHER_MEMORY_LOW_WATER_FRACTION = "0.7";
1120
+ process.env.PUBLISHER_MEMORY_CHECK_INTERVAL_MS = "10000";
1121
+ process.env.PUBLISHER_MEMORY_BACKPRESSURE = "false";
1122
+ const { getMemoryGovernorConfig } = await import("./config");
1123
+ const cfg = getMemoryGovernorConfig();
1124
+ expect(cfg).not.toBeNull();
1125
+ expect(cfg!.highWaterFraction).toBe(0.85);
1126
+ expect(cfg!.lowWaterFraction).toBe(0.7);
1127
+ expect(cfg!.checkIntervalMs).toBe(10000);
1128
+ expect(cfg!.backpressureEnabled).toBe(false);
1129
+ });
1130
+
1131
+ it("treats PUBLISHER_MAX_MEMORY_BYTES=0 as disabled (returns null)", async () => {
1132
+ process.env.PUBLISHER_MAX_MEMORY_BYTES = "0";
1133
+ const { getMemoryGovernorConfig } = await import("./config");
1134
+ expect(getMemoryGovernorConfig()).toBeNull();
1135
+ });
1136
+
1137
+ it("rejects a negative PUBLISHER_MAX_MEMORY_BYTES", async () => {
1138
+ process.env.PUBLISHER_MAX_MEMORY_BYTES = "-1";
1139
+ const { getMemoryGovernorConfig } = await import("./config");
1140
+ expect(() => getMemoryGovernorConfig()).toThrow();
1141
+ });
1142
+
1143
+ it("rejects low >= high", async () => {
1144
+ process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
1145
+ process.env.PUBLISHER_MEMORY_HIGH_WATER_FRACTION = "0.7";
1146
+ process.env.PUBLISHER_MEMORY_LOW_WATER_FRACTION = "0.8";
1147
+ const { getMemoryGovernorConfig } = await import("./config");
1148
+ expect(() => getMemoryGovernorConfig()).toThrow();
1149
+ });
1150
+
1151
+ it("rejects an out-of-range fraction", async () => {
1152
+ process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
1153
+ process.env.PUBLISHER_MEMORY_HIGH_WATER_FRACTION = "1.5";
1154
+ const { getMemoryGovernorConfig } = await import("./config");
1155
+ expect(() => getMemoryGovernorConfig()).toThrow();
1156
+ });
1157
+
1158
+ it("rejects a check interval below the safety floor", async () => {
1159
+ process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
1160
+ process.env.PUBLISHER_MEMORY_CHECK_INTERVAL_MS = "10";
1161
+ const { getMemoryGovernorConfig } = await import("./config");
1162
+ expect(() => getMemoryGovernorConfig()).toThrow();
1163
+ });
1164
+ });
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" &&