@malloy-publisher/server 0.0.203 → 0.0.205

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 (84) hide show
  1. package/build.ts +10 -1
  2. package/dist/app/api-doc.yaml +146 -0
  3. package/dist/app/assets/{EnvironmentPage-BVQ7glKP.js → EnvironmentPage-CAge6UHD.js} +1 -1
  4. package/dist/app/assets/HomePage-DhTe8qpa.js +1 -0
  5. package/dist/app/assets/{MainPage-bYOWcgDP.js → MainPage-CeTxxGex.js} +2 -2
  6. package/dist/app/assets/MaterializationsPage-CpDHB70t.js +1 -0
  7. package/dist/app/assets/ModelPage-D9sSMb75.js +1 -0
  8. package/dist/app/assets/PackagePage-LRqQWrFY.js +1 -0
  9. package/dist/app/assets/{RouteError-_J-EBz7W.js → RouteError-xT6kuCNw.js} +1 -1
  10. package/dist/app/assets/{WorkbookPage-Bjs9Nm-_.js → WorkbookPage-DsIh9svZ.js} +1 -1
  11. package/dist/app/assets/{core-BPLlx5VM.es-C2ARtwWI.js → core-C2sQrwVu.es-Bjem0hym.js} +1 -1
  12. package/dist/app/assets/{index-CqUWJELr.js → index-BdOZDcce.js} +2 -2
  13. package/dist/app/assets/index-DHHAcY5o.js +1812 -0
  14. package/dist/app/assets/index-RX3QOTde.js +455 -0
  15. package/dist/app/assets/index.umd-D2WH3D-f.js +2469 -0
  16. package/dist/app/index.html +1 -1
  17. package/dist/package_load_worker.mjs +392 -67
  18. package/dist/runtime/publisher.js +318 -0
  19. package/dist/server.mjs +982 -346
  20. package/package.json +15 -14
  21. package/scripts/bake-duckdb-extensions.js +104 -0
  22. package/src/controller/watch-mode.controller.ts +176 -46
  23. package/src/ducklake_version.spec.ts +43 -0
  24. package/src/ducklake_version.ts +26 -0
  25. package/src/errors.spec.ts +21 -0
  26. package/src/errors.ts +18 -1
  27. package/src/mcp/error_messages.spec.ts +35 -0
  28. package/src/mcp/error_messages.ts +14 -1
  29. package/src/mcp/handler_utils.ts +12 -0
  30. package/src/package_load/package_load_pool.ts +0 -5
  31. package/src/package_load/package_load_worker.ts +41 -99
  32. package/src/package_load/protocol.ts +1 -7
  33. package/src/runtime/publisher.js +318 -0
  34. package/src/server.ts +479 -2
  35. package/src/service/annotations.spec.ts +118 -0
  36. package/src/service/annotations.ts +91 -0
  37. package/src/service/authorize.spec.ts +132 -0
  38. package/src/service/authorize.ts +241 -0
  39. package/src/service/authorize_integration.spec.ts +932 -0
  40. package/src/service/compile_authorize.spec.ts +85 -0
  41. package/src/service/connection.ts +1 -1
  42. package/src/service/environment.ts +67 -9
  43. package/src/service/environment_store.ts +142 -11
  44. package/src/service/filter.spec.ts +14 -3
  45. package/src/service/filter.ts +5 -1
  46. package/src/service/filter_bypass.spec.ts +418 -0
  47. package/src/service/given.ts +37 -12
  48. package/src/service/givens_integration.spec.ts +34 -7
  49. package/src/service/materialization_service.ts +25 -20
  50. package/src/service/materialized_table_gc.spec.ts +6 -5
  51. package/src/service/materialized_table_gc.ts +2 -50
  52. package/src/service/model.spec.ts +203 -8
  53. package/src/service/model.ts +349 -155
  54. package/src/service/package.ts +17 -6
  55. package/src/service/package_worker_path.spec.ts +113 -0
  56. package/src/service/quoting.ts +0 -20
  57. package/src/service/restricted_mode.spec.ts +299 -0
  58. package/src/service/source_extraction.ts +226 -0
  59. package/src/storage/StorageManager.ts +73 -0
  60. package/src/storage/duckdb/DuckDBConnection.ts +70 -124
  61. package/tests/fixtures/authorize-compile/model.malloy +9 -0
  62. package/tests/fixtures/authorize-compile/publisher.json +4 -0
  63. package/tests/fixtures/html-pages-nopublic/model.malloy +1 -0
  64. package/tests/fixtures/html-pages-nopublic/publisher.json +5 -0
  65. package/tests/fixtures/html-pages-test/data.csv +3 -0
  66. package/tests/fixtures/html-pages-test/public/assets/app.css +3 -0
  67. package/tests/fixtures/html-pages-test/public/data.json +1 -0
  68. package/tests/fixtures/html-pages-test/public/index.html +9 -0
  69. package/tests/fixtures/html-pages-test/public/sub/page2.html +9 -0
  70. package/tests/fixtures/html-pages-test/publisher.json +5 -0
  71. package/tests/fixtures/html-pages-test/report.malloy +1 -0
  72. package/tests/integration/authorize/compile_authorize_http.integration.spec.ts +92 -0
  73. package/tests/integration/duckdb_storage/duckdb_storage.integration.spec.ts +138 -0
  74. package/tests/integration/html_pages/html_pages.integration.spec.ts +378 -0
  75. package/tests/integration/watch-mode/watch_mode.integration.spec.ts +421 -0
  76. package/tests/unit/duckdb/attached_databases.test.ts +111 -0
  77. package/tests/unit/duckdb/duckdb_connection.test.ts +181 -0
  78. package/tests/unit/duckdb/repositories.test.ts +208 -0
  79. package/dist/app/assets/HomePage-D9drXoZX.js +0 -1
  80. package/dist/app/assets/ModelPage-DT0gjNy1.js +0 -1
  81. package/dist/app/assets/PackagePage-N1ZBNJul.js +0 -1
  82. package/dist/app/assets/index-BeNwIeYQ.js +0 -454
  83. package/dist/app/assets/index-Dx7qi2LO.js +0 -1803
  84. package/dist/app/assets/index.umd-BXm2lnUO.js +0 -1145
@@ -0,0 +1,91 @@
1
+ import { Annotations, type ModelDef } from "@malloydata/malloy";
2
+
3
+ /**
4
+ * The raw IR annotation bundle. Workaround: `@malloydata/malloy` exports the
5
+ * `Annotations` view but not the underlying `AnnotationsDef` type, so we
6
+ * recover it from the view's constructor parameter. Replace with a direct
7
+ * `import type { AnnotationsDef }` once malloy exports it.
8
+ */
9
+ export type AnnotationsDef = NonNullable<
10
+ ConstructorParameters<typeof Annotations>[0]
11
+ >;
12
+
13
+ /**
14
+ * True if a route belongs to Malloy's own reserved namespace rather than an
15
+ * application. Malloy claims the empty route (`''` — MOTLY tags / render
16
+ * config) and the whole punctuation-sigil namespace (`!` `@` `"` `:`, and
17
+ * any punct-only route — Form 2 reserves all of them). Everything else is a
18
+ * bracketed app route (`#(doc)`, `#<label>`, …).
19
+ *
20
+ * TODO: this route classification belongs in `@malloydata/malloy` core,
21
+ * beside `parsePrefix` — otherwise every consumer reinvents it. Remove when
22
+ * core exports an equivalent.
23
+ */
24
+ export function isReservedRoute(route: string): boolean {
25
+ return route === "" || !/[\p{L}\p{N}]/u.test(route);
26
+ }
27
+
28
+ /**
29
+ * The model (`##`) annotation bundle for one model, folded across its
30
+ * import/extend lineage.
31
+ *
32
+ * Workaround: malloy 0.0.405 moved model annotations off `ModelDef.annotation`
33
+ * and onto `ModelDef.modelAnnotations` (a `modelID → {ownNotes, inheritsFrom}`
34
+ * registry), folded by the `getModelAnnotations` helper — which malloy does
35
+ * NOT export from its public barrel. We replicate it (matching
36
+ * `@malloydata/malloy/dist/model/annotation_utils.js`): a post-order DFS over
37
+ * `inheritsFrom` (cycle-safe, each model emitted once at its most-ancestral
38
+ * slot) yields ancestral-first / local-last order, folded into an
39
+ * `AnnotationsDef` whose `inherits` chain runs most-ancestral-deepest / local
40
+ * at the top.
41
+ *
42
+ * A model that contributes no `##` of its own adds NO link to the chain (we
43
+ * skip empty `ownNotes`), so `.notes` returns the nearest ancestor that
44
+ * actually has notes — not an empty local node. This matters because `.notes`
45
+ * feeds file-level `##(authorize)` enforcement: an imported model's
46
+ * `##(authorize)` must still flow into an importing file that declares no `##`
47
+ * of its own. We also copy only `notes`/`blockNotes` rather than spreading
48
+ * `ownNotes`, whose own `inherits` would otherwise leak in. Replace with a
49
+ * direct `import { getModelAnnotations }` once malloy exports it.
50
+ */
51
+ export function modelAnnotations(modelDef: ModelDef): AnnotationsDef {
52
+ const registry = modelDef.modelAnnotations ?? {};
53
+ const visited = new Set<string>();
54
+ const order: string[] = [];
55
+ const visit = (id: string): void => {
56
+ if (visited.has(id)) return;
57
+ visited.add(id);
58
+ const entry = registry[id];
59
+ if (!entry) return;
60
+ for (const dep of entry.inheritsFrom) visit(dep);
61
+ order.push(id); // post-order: ancestors precede the model itself
62
+ };
63
+ visit(modelDef.modelID);
64
+
65
+ // Fold most-ancestral → local so the local model lands at the top of the
66
+ // resulting `inherits` chain. Models with no own notes add no link.
67
+ let folded: AnnotationsDef | undefined;
68
+ for (const id of order) {
69
+ const own = registry[id].ownNotes;
70
+ if (!own.notes?.length && !own.blockNotes?.length) continue;
71
+ folded = {
72
+ notes: own.notes,
73
+ blockNotes: own.blockNotes,
74
+ inherits: folded,
75
+ };
76
+ }
77
+ return folded ?? {};
78
+ }
79
+
80
+ /**
81
+ * Every annotation text on an entity — its own `notes` and `blockNotes`
82
+ * plus everything inherited from its ancestors. All of an entity's
83
+ * annotations apply; none are dropped by source location. Returns
84
+ * `undefined`, not `[]`, when empty, to match the optional API shape.
85
+ */
86
+ export function annotationTexts(
87
+ annote: AnnotationsDef | undefined,
88
+ ): string[] | undefined {
89
+ const texts = new Annotations(annote).texts();
90
+ return texts.length > 0 ? texts : undefined;
91
+ }
@@ -0,0 +1,132 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import {
3
+ collectAuthorizeExprs,
4
+ isProbeTrue,
5
+ parseAuthorizeAnnotation,
6
+ } from "./authorize";
7
+
8
+ describe("isProbeTrue", () => {
9
+ it("grants only on a genuine true / 1 / 'true'", () => {
10
+ expect(isProbeTrue(true)).toBe(true);
11
+ expect(isProbeTrue(1)).toBe(true);
12
+ expect(isProbeTrue("true")).toBe(true);
13
+ });
14
+
15
+ it("denies on anything else (fail closed)", () => {
16
+ for (const v of [false, 0, "false", "", null, undefined, {}, "TRUE", 2]) {
17
+ expect(isProbeTrue(v)).toBe(false);
18
+ }
19
+ });
20
+ });
21
+
22
+ describe("parseAuthorizeAnnotation", () => {
23
+ it("parses a source-level #(authorize) expression", () => {
24
+ expect(parseAuthorizeAnnotation(`#(authorize) "$ROLE = 'analyst'"`)).toBe(
25
+ "$ROLE = 'analyst'",
26
+ );
27
+ });
28
+
29
+ it("parses a file-level ##(authorize) expression", () => {
30
+ expect(parseAuthorizeAnnotation(`##(authorize) "$ROLE = 'admin'"`)).toBe(
31
+ "$ROLE = 'admin'",
32
+ );
33
+ });
34
+
35
+ it("tolerates the trailing newline Malloy keeps on note text", () => {
36
+ expect(
37
+ parseAuthorizeAnnotation(`#(authorize) "$REGION = 'us-west'"\n`),
38
+ ).toBe("$REGION = 'us-west'");
39
+ });
40
+
41
+ it("preserves inner single quotes (Malloy string literals)", () => {
42
+ expect(
43
+ parseAuthorizeAnnotation(`#(authorize) "$TENANT in ['a', 'b']"`),
44
+ ).toBe("$TENANT in ['a', 'b']");
45
+ });
46
+
47
+ it("unescapes escaped inner double quotes", () => {
48
+ expect(parseAuthorizeAnnotation(`#(authorize) "$NAME = \\"foo\\""`)).toBe(
49
+ `$NAME = "foo"`,
50
+ );
51
+ });
52
+
53
+ it("handles a constant false gate", () => {
54
+ expect(parseAuthorizeAnnotation(`#(authorize) "false"`)).toBe("false");
55
+ });
56
+
57
+ it("returns null for non-authorize annotations", () => {
58
+ expect(
59
+ parseAuthorizeAnnotation(`#(filter) dimension=x type=equal`),
60
+ ).toBeNull();
61
+ expect(parseAuthorizeAnnotation(`##! experimental.givens`)).toBeNull();
62
+ expect(parseAuthorizeAnnotation(`## just a doc comment`)).toBeNull();
63
+ expect(parseAuthorizeAnnotation(`# plain`)).toBeNull();
64
+ expect(parseAuthorizeAnnotation(``)).toBeNull();
65
+ });
66
+
67
+ it("throws when the body is not quoted", () => {
68
+ expect(() =>
69
+ parseAuthorizeAnnotation(`#(authorize) $ROLE = 'analyst'`),
70
+ ).toThrow(/double-quoted/);
71
+ });
72
+
73
+ it("throws on mismatched / unterminated quotes", () => {
74
+ expect(() =>
75
+ parseAuthorizeAnnotation(`#(authorize) "$ROLE = 'analyst'`),
76
+ ).toThrow(/mismatched quotes/);
77
+ });
78
+
79
+ it("throws on an empty expression body", () => {
80
+ expect(() => parseAuthorizeAnnotation(`#(authorize) ""`)).toThrow(
81
+ /empty expression/,
82
+ );
83
+ });
84
+
85
+ it("throws on content after the closing quote", () => {
86
+ expect(() =>
87
+ parseAuthorizeAnnotation(`#(authorize) "$ROLE = 'a'" extra`),
88
+ ).toThrow(/unexpected content/);
89
+ });
90
+
91
+ it("throws when the prefix has no body", () => {
92
+ expect(() => parseAuthorizeAnnotation(`#(authorize)`)).toThrow(
93
+ /double-quoted/,
94
+ );
95
+ });
96
+ });
97
+
98
+ describe("collectAuthorizeExprs", () => {
99
+ it("collects authorize expressions in declaration order", () => {
100
+ expect(
101
+ collectAuthorizeExprs([
102
+ `##(authorize) "$ROLE = 'admin'"`,
103
+ `#(filter) dimension=x type=equal`,
104
+ `#(authorize) "$REGION = 'us-west'"`,
105
+ ]),
106
+ ).toEqual(["$ROLE = 'admin'", "$REGION = 'us-west'"]);
107
+ });
108
+
109
+ it("returns [] when there are no authorize annotations", () => {
110
+ expect(
111
+ collectAuthorizeExprs([`#(filter) dimension=x type=equal`, `## doc`]),
112
+ ).toEqual([]);
113
+ });
114
+
115
+ it("keeps duplicate gates (no dedup — OR semantics)", () => {
116
+ expect(
117
+ collectAuthorizeExprs([
118
+ `#(authorize) "$ROLE = 'admin'"`,
119
+ `#(authorize) "$ROLE = 'admin'"`,
120
+ ]),
121
+ ).toEqual(["$ROLE = 'admin'", "$ROLE = 'admin'"]);
122
+ });
123
+
124
+ it("propagates the throw from a malformed authorize annotation", () => {
125
+ expect(() =>
126
+ collectAuthorizeExprs([
127
+ `#(authorize) "$ROLE = 'admin'"`,
128
+ `#(authorize) "unterminated`,
129
+ ]),
130
+ ).toThrow(/mismatched quotes/);
131
+ });
132
+ });
@@ -0,0 +1,241 @@
1
+ /**
2
+ * `#(authorize)` / `##(authorize)` annotation parsing.
3
+ *
4
+ * Annotation format:
5
+ * #(authorize) "<malloy-bool-expr>" — source-level, on a single source
6
+ * ##(authorize) "<malloy-bool-expr>" — file/model-level, applies to the file
7
+ *
8
+ * The body is a single double-quoted Malloy boolean expression that references
9
+ * declared givens (`$NAME`), e.g. `#(authorize) "$ROLE = 'analyst'"`. The
10
+ * expression itself routinely contains single quotes (Malloy string literals),
11
+ * so we cannot reuse filter.ts's whitespace tokenizer — we unwrap exactly one
12
+ * layer of double quotes and hand the inner expression back untouched.
13
+ *
14
+ * This module parses, collects, and (at compile time) validates authorize
15
+ * annotations. Evaluating the expression — the actual access gate — lands in a
16
+ * later PR and reuses `buildAuthorizeProbe`. Kept light so it bundles cleanly
17
+ * into the package-load worker (the only non-type import is the shared
18
+ * `ModelCompilationError`).
19
+ */
20
+
21
+ import { type GivenValue } from "@malloydata/malloy";
22
+ import { ModelCompilationError } from "../errors";
23
+
24
+ const SOURCE_PREFIX = "#(authorize)";
25
+ const FILE_PREFIX = "##(authorize)";
26
+
27
+ /** source name → effective authorize expressions (file-level then source-level). */
28
+ export type AuthorizeMap = Map<string, string[]>;
29
+
30
+ /**
31
+ * Build the synthetic probe query that evaluates a source's authorize
32
+ * expressions. Each expression becomes a boolean `select` column over a
33
+ * one-row, warehouse-independent DuckDB source (the `"duckdb"` sandbox is
34
+ * registered for every package, so this never touches the model's real
35
+ * warehouse). Compiling this probe validates the expressions against the
36
+ * model's `given:` block (unknown givens and source-field references surface as
37
+ * compile errors); running it evaluates the gate. The reserved dummy column
38
+ * name is deliberately obscure so a real authorize expression is unlikely to
39
+ * collide with it — a bare field reference in an expression is meant to fail.
40
+ */
41
+ export function buildAuthorizeProbe(exprs: string[]): string {
42
+ const selects = exprs
43
+ .map((expr, i) => `__auth_${i} is (${expr})`)
44
+ .join("\n ");
45
+ return `run: duckdb.sql("SELECT 1 AS __authorize_probe_row") -> {
46
+ select:
47
+ ${selects}
48
+ limit: 1
49
+ }`;
50
+ }
51
+
52
+ /**
53
+ * Strict, fail-closed truthiness for a probe result cell. DuckDB (the probe's
54
+ * connection) returns a native boolean, but normalize defensively: only a real
55
+ * `true` / SQL `1` / `"true"` grants access. null, undefined, `0`, `false`, a
56
+ * missing cell — anything else — denies.
57
+ */
58
+ export function isProbeTrue(cell: unknown): boolean {
59
+ return cell === true || cell === 1 || cell === "true";
60
+ }
61
+
62
+ /** Minimal materializer surface needed to compile (not run) the probe. */
63
+ interface AuthorizeProbeCompiler {
64
+ loadQuery(query: string): { getPreparedQuery(): Promise<unknown> };
65
+ }
66
+
67
+ /** Minimal materializer surface needed to run the probe (the runtime gate). */
68
+ interface AuthorizeProbeExecutor {
69
+ loadQuery(query: string): {
70
+ run(opts: {
71
+ rowLimit?: number;
72
+ givens?: Record<string, GivenValue>;
73
+ }): Promise<{ data: { value: ReadonlyArray<Record<string, unknown>> } }>;
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Evaluate a source's authorize disjunction against the supplied givens.
79
+ * Returns true if ANY expression evaluates true (OR semantics).
80
+ *
81
+ * Each expression is probed INDEPENDENTLY rather than batched into one query.
82
+ * That is what preserves OR semantics: an expression that can't be evaluated —
83
+ * e.g. it references a given the request didn't supply, so Malloy throws "no
84
+ * value" — counts as "does not grant" (false), and the next disjunct is still
85
+ * tried. Batching them would let a missing given in one unused branch throw and
86
+ * sink the whole request (denying an admin who matched a different branch).
87
+ *
88
+ * Per-branch failures are swallowed to false (fail closed at the branch level);
89
+ * access is granted only if some branch genuinely returns true. Short-circuits
90
+ * on the first true. Returns false (→ caller denies) if none grant.
91
+ */
92
+ export async function evaluateAuthorize(
93
+ executor: AuthorizeProbeExecutor,
94
+ exprs: string[],
95
+ givens: Record<string, GivenValue>,
96
+ ): Promise<boolean> {
97
+ for (const expr of exprs) {
98
+ try {
99
+ const result = await executor
100
+ .loadQuery(buildAuthorizeProbe([expr]))
101
+ .run({ rowLimit: 1, givens });
102
+ const row = result?.data?.value?.[0];
103
+ if (row && isProbeTrue(row.__auth_0)) {
104
+ return true;
105
+ }
106
+ } catch {
107
+ // This disjunct can't be evaluated (e.g. a referenced given has no
108
+ // value) — treat as not-granting and try the next. It does not fail
109
+ // the whole request, which is what keeps OR semantics intact.
110
+ continue;
111
+ }
112
+ }
113
+ return false;
114
+ }
115
+
116
+ /** A source plus its effective authorize expressions. */
117
+ interface SourceWithAuthorize {
118
+ name?: string;
119
+ authorize?: string[];
120
+ }
121
+
122
+ /**
123
+ * Translation-time validation. For each source carrying authorize expressions,
124
+ * compile the probe (no givens, no DB execution) so unknown givens and
125
+ * source-field references surface as compile errors at model load instead of
126
+ * first request. Type mismatches such as `$ROLE = 5` are NOT Malloy compile
127
+ * errors, so they are not caught here — they fail closed at the runtime gate.
128
+ *
129
+ * Throws `ModelCompilationError` naming the source on the first invalid
130
+ * annotation. Shared by `Model.create` and the package-load worker so both
131
+ * compile paths validate identically.
132
+ */
133
+ export async function validateAuthorizeProbes(
134
+ compiler: AuthorizeProbeCompiler,
135
+ sources: readonly SourceWithAuthorize[],
136
+ ): Promise<void> {
137
+ for (const source of sources) {
138
+ const exprs = source.authorize;
139
+ if (!exprs || exprs.length === 0) continue;
140
+ try {
141
+ await compiler
142
+ .loadQuery(buildAuthorizeProbe(exprs))
143
+ .getPreparedQuery();
144
+ } catch (err) {
145
+ const detail = err instanceof Error ? err.message : String(err);
146
+ throw new ModelCompilationError({
147
+ message: `Invalid #(authorize) annotation on source "${source.name ?? "(unnamed)"}" [${exprs.join(" | ")}]: ${detail}`,
148
+ });
149
+ }
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Parse a single annotation string into its authorize expression.
155
+ *
156
+ * Returns the inner expression for a well-formed `#(authorize)` / `##(authorize)`
157
+ * annotation, `null` if the string is not an authorize annotation at all, and
158
+ * throws if it looks like one but is malformed (missing quotes, mismatched
159
+ * quotes, or an empty body). The throw is what later compile-time validation
160
+ * turns into a model-load error.
161
+ */
162
+ export function parseAuthorizeAnnotation(annotation: string): string | null {
163
+ const trimmed = annotation.trim();
164
+
165
+ let body: string;
166
+ // Check the file-level prefix first — `##(authorize)` also starts with `#`.
167
+ if (trimmed.startsWith(FILE_PREFIX)) {
168
+ body = trimmed.slice(FILE_PREFIX.length).trim();
169
+ } else if (trimmed.startsWith(SOURCE_PREFIX)) {
170
+ body = trimmed.slice(SOURCE_PREFIX.length).trim();
171
+ } else {
172
+ return null;
173
+ }
174
+
175
+ return unwrapQuotedExpression(body);
176
+ }
177
+
178
+ /**
179
+ * Extract authorize expressions from a list of annotation strings, preserving
180
+ * declaration order. Non-authorize annotations are ignored; there is no dedup —
181
+ * every authorize annotation is an independent gate (OR semantics), so repeats
182
+ * are kept. Propagates the throw from a malformed authorize annotation.
183
+ */
184
+ export function collectAuthorizeExprs(annotations: string[]): string[] {
185
+ const exprs: string[] = [];
186
+ for (const annotation of annotations) {
187
+ const expr = parseAuthorizeAnnotation(annotation);
188
+ if (expr !== null) {
189
+ exprs.push(expr);
190
+ }
191
+ }
192
+ return exprs;
193
+ }
194
+
195
+ /**
196
+ * Strip exactly one layer of wrapping double quotes off the annotation body and
197
+ * return the inner expression. Inner single quotes are part of the expression
198
+ * and pass through untouched; `\"` and `\\` inside the string are unescaped.
199
+ */
200
+ function unwrapQuotedExpression(body: string): string {
201
+ if (body.length < 2 || body[0] !== '"') {
202
+ throw new Error(
203
+ `authorize annotation expression must be a double-quoted string, got: ${body || "(empty)"}`,
204
+ );
205
+ }
206
+
207
+ let expr = "";
208
+ let i = 1;
209
+ let closed = false;
210
+ for (; i < body.length; i++) {
211
+ const ch = body[i];
212
+ if (ch === "\\" && i + 1 < body.length) {
213
+ const next = body[i + 1];
214
+ if (next === '"' || next === "\\") {
215
+ expr += next;
216
+ i++;
217
+ continue;
218
+ }
219
+ }
220
+ if (ch === '"') {
221
+ closed = true;
222
+ i++;
223
+ break;
224
+ }
225
+ expr += ch;
226
+ }
227
+
228
+ if (!closed) {
229
+ throw new Error(`authorize annotation has mismatched quotes: ${body}`);
230
+ }
231
+ const rest = body.slice(i).trim();
232
+ if (rest.length > 0) {
233
+ throw new Error(
234
+ `authorize annotation has unexpected content after the expression: ${rest}`,
235
+ );
236
+ }
237
+ if (expr.trim().length === 0) {
238
+ throw new Error("authorize annotation has an empty expression body");
239
+ }
240
+ return expr;
241
+ }