@malloy-publisher/server 0.0.198 → 0.0.200
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.
- package/build.ts +30 -1
- package/dist/app/api-doc.yaml +127 -111
- package/dist/app/assets/{EnvironmentPage-C7rtH4mC.js → EnvironmentPage-CgKNjySu.js} +1 -1
- package/dist/app/assets/HomePage-BPIpMBjW.js +1 -0
- package/dist/app/assets/{MainPage-D38LtZDV.js → MainPage-CAwb8U82.js} +2 -2
- package/dist/app/assets/{ModelPage-DOol8Mz7.js → ModelPage-C0Uevsw9.js} +1 -1
- package/dist/app/assets/{PackagePage-0tgzA_kO.js → PackagePage-Cu-u9k1g.js} +1 -1
- package/dist/app/assets/{RouteError-BaMsOSly.js → RouteError-DVwPh2Ql.js} +1 -1
- package/dist/app/assets/{WorkbookPage-Cx4SePkx.js → WorkbookPage-DW38R2Zv.js} +1 -1
- package/dist/app/assets/{core-CbsC6R_Y.es-Cwf6asf3.js → core-C0vCMRDQ.es-D_ytHhjS.js} +10 -10
- package/dist/app/assets/{index-DL6BZTuw.js → index-BGdcKsFF.js} +1 -1
- package/dist/app/assets/{index-DNofXMxi.js → index-CTx4v4_3.js} +1 -1
- package/dist/app/assets/index-DE6d5jEy.js +452 -0
- package/dist/app/assets/{index.umd-B68wGGkM.js → index.umd-C1Mi1uRm.js} +1 -1
- package/dist/app/index.html +1 -1
- package/dist/instrumentation.mjs +57 -36
- package/dist/package_load_worker.mjs +12213 -0
- package/dist/server.mjs +4198 -3648
- package/package.json +2 -3
- package/src/config.spec.ts +246 -0
- package/src/config.ts +121 -1
- package/src/constants.ts +84 -1
- package/src/controller/compile.controller.ts +3 -1
- package/src/controller/connection.controller.spec.ts +803 -0
- package/src/controller/connection.controller.ts +207 -20
- package/src/controller/model.controller.ts +19 -1
- package/src/controller/query.controller.ts +22 -6
- package/src/controller/watch-mode.controller.ts +11 -2
- package/src/errors.spec.ts +44 -0
- package/src/errors.ts +34 -0
- package/src/health.spec.ts +90 -0
- package/src/health.ts +88 -45
- package/src/heap_check.spec.ts +144 -0
- package/src/heap_check.ts +144 -0
- package/src/instrumentation.ts +50 -0
- package/src/mcp/handler_utils.ts +14 -0
- package/src/mcp/tools/execute_query_tool.ts +52 -10
- package/src/oom_guards.integration.spec.ts +261 -0
- package/src/package_load/package_load_pool.spec.ts +252 -0
- package/src/package_load/package_load_pool.ts +920 -0
- package/src/package_load/package_load_worker.ts +980 -0
- package/src/package_load/protocol.ts +336 -0
- package/src/path_safety.ts +9 -3
- package/src/query_cap_metrics.spec.ts +89 -0
- package/src/query_cap_metrics.ts +115 -0
- package/src/query_concurrency.spec.ts +247 -0
- package/src/query_concurrency.ts +236 -0
- package/src/query_param_utils.ts +18 -0
- package/src/query_timeout.spec.ts +224 -0
- package/src/query_timeout.ts +178 -0
- package/src/server-old.ts +21 -1
- package/src/server.ts +61 -57
- package/src/service/connection.ts +8 -2
- package/src/service/db_utils.spec.ts +1 -1
- package/src/service/environment.ts +85 -4
- package/src/service/environment_admission.spec.ts +165 -1
- package/src/service/environment_store.spec.ts +103 -0
- package/src/service/environment_store.ts +98 -26
- package/src/service/filter_integration.spec.ts +110 -0
- package/src/service/given.ts +80 -0
- package/src/service/givens_integration.spec.ts +192 -0
- package/src/service/model.spec.ts +298 -3
- package/src/service/model.ts +362 -23
- package/src/service/model_limits.spec.ts +181 -0
- package/src/service/model_limits.ts +110 -0
- package/src/service/package.spec.ts +12 -6
- package/src/service/package.ts +263 -146
- package/src/service/package_worker_path.spec.ts +196 -0
- package/src/service/path_injection.spec.ts +39 -0
- package/src/stream_helpers.spec.ts +280 -0
- package/src/stream_helpers.ts +162 -0
- package/src/test_helpers/metrics_harness.ts +126 -0
- package/tests/integration/concurrent_package/concurrent_package.integration.spec.ts +280 -0
- package/dist/app/assets/HomePage-DwkH7OrS.js +0 -1
- package/dist/app/assets/index-U38AyjJL.js +0 -451
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { PayloadTooLargeError } from "../errors";
|
|
4
|
+
import {
|
|
5
|
+
assertWithinModelResponseLimits,
|
|
6
|
+
resolveModelQueryRowLimit,
|
|
7
|
+
} from "./model_limits";
|
|
8
|
+
|
|
9
|
+
describe("resolveModelQueryRowLimit", () => {
|
|
10
|
+
it("uses the user's LIMIT when set and below the maxRows ceiling", () => {
|
|
11
|
+
expect(
|
|
12
|
+
resolveModelQueryRowLimit(500, {
|
|
13
|
+
defaultLimit: 1000,
|
|
14
|
+
maxRows: 100_000,
|
|
15
|
+
}),
|
|
16
|
+
).toBe(500);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("falls back to defaultLimit when the user's LIMIT is undefined", () => {
|
|
20
|
+
expect(
|
|
21
|
+
resolveModelQueryRowLimit(undefined, {
|
|
22
|
+
defaultLimit: 1000,
|
|
23
|
+
maxRows: 100_000,
|
|
24
|
+
}),
|
|
25
|
+
).toBe(1000);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("falls back to defaultLimit when the user's LIMIT is 0 (Malloy returns 0 for 'no limit')", () => {
|
|
29
|
+
// Malloy's PreparedResult returns 0 from `resultExplore.limit` when the
|
|
30
|
+
// query has no LIMIT clause; treat that as "no user limit".
|
|
31
|
+
expect(
|
|
32
|
+
resolveModelQueryRowLimit(0, {
|
|
33
|
+
defaultLimit: 1000,
|
|
34
|
+
maxRows: 100_000,
|
|
35
|
+
}),
|
|
36
|
+
).toBe(1000);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("clamps a too-high user LIMIT to the maxRows + 1 sentinel", () => {
|
|
40
|
+
expect(
|
|
41
|
+
resolveModelQueryRowLimit(1_000_000, {
|
|
42
|
+
defaultLimit: 1000,
|
|
43
|
+
maxRows: 100_000,
|
|
44
|
+
}),
|
|
45
|
+
).toBe(100_001);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("clamps a too-high defaultLimit to the maxRows + 1 sentinel", () => {
|
|
49
|
+
// Operator misconfigured DEFAULT > MAX; the hard cap still wins.
|
|
50
|
+
expect(
|
|
51
|
+
resolveModelQueryRowLimit(undefined, {
|
|
52
|
+
defaultLimit: 1_000_000,
|
|
53
|
+
maxRows: 50,
|
|
54
|
+
}),
|
|
55
|
+
).toBe(51);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("returns the requested limit unchanged when maxRows is 0 (cap disabled)", () => {
|
|
59
|
+
expect(
|
|
60
|
+
resolveModelQueryRowLimit(1_000_000, {
|
|
61
|
+
defaultLimit: 1000,
|
|
62
|
+
maxRows: 0,
|
|
63
|
+
}),
|
|
64
|
+
).toBe(1_000_000);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("returns the default unchanged when maxRows is 0 and no user limit", () => {
|
|
68
|
+
expect(
|
|
69
|
+
resolveModelQueryRowLimit(undefined, {
|
|
70
|
+
defaultLimit: 1000,
|
|
71
|
+
maxRows: 0,
|
|
72
|
+
}),
|
|
73
|
+
).toBe(1000);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("rejects negative user limits by treating them as 'no limit'", () => {
|
|
77
|
+
// Defensive — shouldn't happen in practice, but a -1 from a malformed
|
|
78
|
+
// PreparedResult shouldn't propagate as a negative rowLimit to the driver.
|
|
79
|
+
expect(
|
|
80
|
+
resolveModelQueryRowLimit(-1, {
|
|
81
|
+
defaultLimit: 1000,
|
|
82
|
+
maxRows: 100_000,
|
|
83
|
+
}),
|
|
84
|
+
).toBe(1000);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("assertWithinModelResponseLimits", () => {
|
|
89
|
+
it("does not throw when both counts are below their caps", () => {
|
|
90
|
+
expect(() =>
|
|
91
|
+
assertWithinModelResponseLimits(
|
|
92
|
+
500,
|
|
93
|
+
1_000,
|
|
94
|
+
{ maxRows: 1000, maxBytes: 10_000 },
|
|
95
|
+
"model_query",
|
|
96
|
+
),
|
|
97
|
+
).not.toThrow();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("does not throw when row count equals the cap exactly (sentinel hasn't fired)", () => {
|
|
101
|
+
expect(() =>
|
|
102
|
+
assertWithinModelResponseLimits(
|
|
103
|
+
1000,
|
|
104
|
+
1_000,
|
|
105
|
+
{ maxRows: 1000, maxBytes: 10_000 },
|
|
106
|
+
"model_query",
|
|
107
|
+
),
|
|
108
|
+
).not.toThrow();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("throws PayloadTooLargeError with the row-cap message on row overflow", () => {
|
|
112
|
+
expect(() =>
|
|
113
|
+
assertWithinModelResponseLimits(
|
|
114
|
+
1001,
|
|
115
|
+
1_000,
|
|
116
|
+
{ maxRows: 1000, maxBytes: 10_000 },
|
|
117
|
+
"model_query",
|
|
118
|
+
),
|
|
119
|
+
).toThrow(PayloadTooLargeError);
|
|
120
|
+
expect(() =>
|
|
121
|
+
assertWithinModelResponseLimits(
|
|
122
|
+
1001,
|
|
123
|
+
1_000,
|
|
124
|
+
{ maxRows: 1000, maxBytes: 10_000 },
|
|
125
|
+
"model_query",
|
|
126
|
+
),
|
|
127
|
+
).toThrow("more than 1000 rows");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("throws PayloadTooLargeError with the byte-cap message on byte overflow", () => {
|
|
131
|
+
expect(() =>
|
|
132
|
+
assertWithinModelResponseLimits(
|
|
133
|
+
10,
|
|
134
|
+
50_000,
|
|
135
|
+
{ maxRows: 1000, maxBytes: 10_000 },
|
|
136
|
+
"model_query",
|
|
137
|
+
),
|
|
138
|
+
).toThrow(PayloadTooLargeError);
|
|
139
|
+
expect(() =>
|
|
140
|
+
assertWithinModelResponseLimits(
|
|
141
|
+
10,
|
|
142
|
+
50_000,
|
|
143
|
+
{ maxRows: 1000, maxBytes: 10_000 },
|
|
144
|
+
"model_query",
|
|
145
|
+
),
|
|
146
|
+
).toThrow("exceeded 10000 bytes");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("prefers the row-cap message when both caps would have fired (row check runs first)", () => {
|
|
150
|
+
expect(() =>
|
|
151
|
+
assertWithinModelResponseLimits(
|
|
152
|
+
2000,
|
|
153
|
+
50_000,
|
|
154
|
+
{ maxRows: 1000, maxBytes: 10_000 },
|
|
155
|
+
"model_query",
|
|
156
|
+
),
|
|
157
|
+
).toThrow("more than 1000 rows");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("disables row cap when maxRows is 0", () => {
|
|
161
|
+
expect(() =>
|
|
162
|
+
assertWithinModelResponseLimits(
|
|
163
|
+
1_000_000,
|
|
164
|
+
1_000,
|
|
165
|
+
{ maxRows: 0, maxBytes: 10_000 },
|
|
166
|
+
"model_query",
|
|
167
|
+
),
|
|
168
|
+
).not.toThrow();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("disables byte cap when maxBytes is 0", () => {
|
|
172
|
+
expect(() =>
|
|
173
|
+
assertWithinModelResponseLimits(
|
|
174
|
+
10,
|
|
175
|
+
1_000_000_000,
|
|
176
|
+
{ maxRows: 1000, maxBytes: 0 },
|
|
177
|
+
"model_query",
|
|
178
|
+
),
|
|
179
|
+
).not.toThrow();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory guards for the Malloy model-query path (the `runnable.run`
|
|
3
|
+
* flow used by `getQueryResults` and notebook cell execution).
|
|
4
|
+
*
|
|
5
|
+
* Two layered defenses:
|
|
6
|
+
*
|
|
7
|
+
* 1. {@link resolveModelQueryRowLimit} — compute the effective
|
|
8
|
+
* `rowLimit` to push down to `runnable.run`. The user's Malloy
|
|
9
|
+
* `LIMIT` clause wins when present; otherwise the operator-
|
|
10
|
+
* tunable default ({@link getDefaultQueryRowLimit}) fills in.
|
|
11
|
+
* Either way the result is clamped to `maxRows + 1` so the
|
|
12
|
+
* database itself stops producing rows when a user-supplied
|
|
13
|
+
* `LIMIT 1_000_000` would otherwise blow up the process.
|
|
14
|
+
*
|
|
15
|
+
* 2. {@link assertWithinModelResponseLimits} — post-run overflow
|
|
16
|
+
* detection. If the connector returned `maxRows + 1` rows
|
|
17
|
+
* (the sentinel) or the JSON-serialized response exceeds the
|
|
18
|
+
* byte cap, throw `PayloadTooLargeError` so the caller sees a
|
|
19
|
+
* clean HTTP 413.
|
|
20
|
+
*
|
|
21
|
+
* Caveat on the byte cap: this path runs `runnable.run` (buffered),
|
|
22
|
+
* not `runStream`, so by the time we measure bytes the result has
|
|
23
|
+
* already been materialized in memory. The byte cap here is loud-
|
|
24
|
+
* failure detection — it surfaces oversize responses with a 413
|
|
25
|
+
* instead of letting the client receive a half-transmitted payload
|
|
26
|
+
* — not OOM prevention. True prevention requires streaming +
|
|
27
|
+
* `Result` reconstruction from `DataRecord`s, which is out of scope
|
|
28
|
+
* for this step (the model-query streaming path entangles with
|
|
29
|
+
* Malloy's `Result` schema metadata in non-trivial ways).
|
|
30
|
+
*
|
|
31
|
+
* Both helpers are pure so they can be unit-tested without spinning
|
|
32
|
+
* up a model runtime; the caller injects the env-derived limits.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { PayloadTooLargeError } from "../errors";
|
|
36
|
+
import {
|
|
37
|
+
recordQueryCapExceeded,
|
|
38
|
+
type QueryCapSource,
|
|
39
|
+
} from "../query_cap_metrics";
|
|
40
|
+
|
|
41
|
+
export interface ResolveRowLimitConfig {
|
|
42
|
+
/**
|
|
43
|
+
* Result of {@link getDefaultQueryRowLimit}. Applied when the
|
|
44
|
+
* user's Malloy query doesn't carry a `LIMIT` clause.
|
|
45
|
+
*/
|
|
46
|
+
defaultLimit: number;
|
|
47
|
+
/**
|
|
48
|
+
* Result of {@link getMaxQueryRows}. The effective row limit is
|
|
49
|
+
* clamped to `maxRows + 1` so a sentinel-count overflow check can
|
|
50
|
+
* distinguish "ran right up to the cap" from "would have
|
|
51
|
+
* overflowed". A value of `0` disables the cap.
|
|
52
|
+
*/
|
|
53
|
+
maxRows: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Compute the `rowLimit` to pass to `runnable.run`. The +1 sentinel
|
|
58
|
+
* mirrors the Step 1 / Step 2 patterns on the connection-query path
|
|
59
|
+
* so behavior is uniform across all query surfaces.
|
|
60
|
+
*/
|
|
61
|
+
export function resolveModelQueryRowLimit(
|
|
62
|
+
userLimit: number | undefined,
|
|
63
|
+
{ defaultLimit, maxRows }: ResolveRowLimitConfig,
|
|
64
|
+
): number {
|
|
65
|
+
const requested = userLimit && userLimit > 0 ? userLimit : defaultLimit;
|
|
66
|
+
if (maxRows <= 0) return requested;
|
|
67
|
+
return Math.min(requested, maxRows + 1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ModelResponseLimitsConfig {
|
|
71
|
+
/** Result of {@link getMaxQueryRows}. `0` disables the row cap. */
|
|
72
|
+
maxRows: number;
|
|
73
|
+
/** Result of {@link getMaxResponseBytes}. `0` disables the byte cap. */
|
|
74
|
+
maxBytes: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Throw {@link PayloadTooLargeError} (HTTP 413) when a model-query
|
|
79
|
+
* response exceeds either configured cap. `rowCount` should be the
|
|
80
|
+
* raw row count Malloy actually fetched (typically
|
|
81
|
+
* `result._queryResult.data.rawData.length`); `serializedBytes`
|
|
82
|
+
* should be the byte length of the JSON-stringified response that
|
|
83
|
+
* would otherwise be returned to the client.
|
|
84
|
+
*
|
|
85
|
+
* Row check uses the `> maxRows` sentinel (not `>= maxRows`), since
|
|
86
|
+
* {@link resolveModelQueryRowLimit} asked the connector for
|
|
87
|
+
* `maxRows + 1` and we want to fail only when that sentinel fires.
|
|
88
|
+
*/
|
|
89
|
+
export function assertWithinModelResponseLimits(
|
|
90
|
+
rowCount: number,
|
|
91
|
+
serializedBytes: number,
|
|
92
|
+
{ maxRows, maxBytes }: ModelResponseLimitsConfig,
|
|
93
|
+
source: QueryCapSource,
|
|
94
|
+
): void {
|
|
95
|
+
if (maxRows > 0 && rowCount > maxRows) {
|
|
96
|
+
// Tick the counter *before* throwing so it reflects the
|
|
97
|
+
// event even if a downstream `catch` swallows the error
|
|
98
|
+
// (notebook handlers and MCP tools both do this in places).
|
|
99
|
+
recordQueryCapExceeded("rows", source);
|
|
100
|
+
throw new PayloadTooLargeError(
|
|
101
|
+
`Query returned more than ${maxRows} rows. Refine the query (add a LIMIT or more selective WHERE) or raise PUBLISHER_MAX_QUERY_ROWS.`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
if (maxBytes > 0 && serializedBytes > maxBytes) {
|
|
105
|
+
recordQueryCapExceeded("bytes", source);
|
|
106
|
+
throw new PayloadTooLargeError(
|
|
107
|
+
`Query response exceeded ${maxBytes} bytes (was ${serializedBytes}). Project fewer columns, add a LIMIT, or raise PUBLISHER_MAX_RESPONSE_BYTES.`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { DuckDBConnection } from "@malloydata/db-duckdb";
|
|
2
|
+
import "@malloydata/db-duckdb/native";
|
|
1
3
|
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
2
4
|
import { Stats } from "fs";
|
|
3
5
|
import fs from "fs/promises";
|
|
@@ -11,7 +13,7 @@ import { Package } from "./package";
|
|
|
11
13
|
type PartialModel = Pick<Model, "getPath">;
|
|
12
14
|
|
|
13
15
|
describe("service/package", () => {
|
|
14
|
-
const testPackageDirectory = "testPackage";
|
|
16
|
+
const testPackageDirectory = resolve("testPackage");
|
|
15
17
|
|
|
16
18
|
beforeEach(async () => {
|
|
17
19
|
await fs.mkdir(testPackageDirectory, { recursive: true });
|
|
@@ -98,11 +100,7 @@ describe("service/package", () => {
|
|
|
98
100
|
testPackageDirectory,
|
|
99
101
|
new Map(),
|
|
100
102
|
),
|
|
101
|
-
).rejects.
|
|
102
|
-
new PackageNotFoundError(
|
|
103
|
-
"Package manifest for testPackage does not exist.",
|
|
104
|
-
),
|
|
105
|
-
);
|
|
103
|
+
).rejects.toBeInstanceOf(PackageNotFoundError);
|
|
106
104
|
});
|
|
107
105
|
it(
|
|
108
106
|
"should return a Package object if the package exists",
|
|
@@ -340,10 +338,18 @@ describe("service/package", () => {
|
|
|
340
338
|
it("should return the size of the database file", async () => {
|
|
341
339
|
sinon.stub(fs, "stat").resolves({ size: 13 } as Stats);
|
|
342
340
|
|
|
341
|
+
// `getDatabaseInfo` now requires the caller to pass in the
|
|
342
|
+
// shared DuckDB connection (resolved once by `readDatabases`
|
|
343
|
+
// off the package's MalloyConfig). For this isolated unit
|
|
344
|
+
// test we mint a fresh ephemeral one — production paths
|
|
345
|
+
// reuse a single connection per package via `Package.create`.
|
|
346
|
+
const conn = new DuckDBConnection("duckdb");
|
|
347
|
+
|
|
343
348
|
// @ts-expect-error Accessing private static method for testing
|
|
344
349
|
const info = await Package.getDatabaseInfo(
|
|
345
350
|
testPackageDirectory,
|
|
346
351
|
"database.csv",
|
|
352
|
+
conn,
|
|
347
353
|
);
|
|
348
354
|
|
|
349
355
|
expect(info).toEqual({
|