@1agh/maude 1.5.2 → 1.6.0

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.
@@ -65,6 +65,9 @@ export function scrubAgentEnv(
65
65
  if (value === undefined) continue;
66
66
  if (PROVIDER_REDIRECT_RE.test(key)) continue;
67
67
  if (GENERATION_KEY_CUSTODY_RE.test(key)) continue;
68
+ // The cell materializer's loopback credential is the studio's alone — an
69
+ // agent it spawns has no business filling the cache (Phase 1 review L3).
70
+ if (key === 'MAUDE_MATERIALIZE_TOKEN') continue;
68
71
  out[key] = value;
69
72
  }
70
73
  return out;
@@ -329,10 +329,13 @@ export function isSandboxArmed(env: NodeJS.ProcessEnv = process.env): boolean {
329
329
  export async function buildCanvasSandboxed({
330
330
  designRoot,
331
331
  canvasAbs,
332
+ assetUrlBase,
332
333
  env = process.env,
333
334
  }: {
334
335
  designRoot: string;
335
336
  canvasAbs: string;
337
+ /** BuildCanvasOptions.assetUrlBase, passed to the worker as argv[4]. */
338
+ assetUrlBase?: string;
336
339
  env?: NodeJS.ProcessEnv;
337
340
  }): Promise<SandboxBuildResult> {
338
341
  maybeRoll();
@@ -352,7 +355,7 @@ export async function buildCanvasSandboxed({
352
355
  );
353
356
 
354
357
  const started = Date.now();
355
- const result = await runWorker({ designRoot, canvasAbs, env });
358
+ const result = await runWorker({ designRoot, canvasAbs, assetUrlBase, env });
356
359
  const elapsed = Date.now() - started;
357
360
  counters.cpuMsTotal += elapsed;
358
361
  counters.durationsMs.push(elapsed);
@@ -374,23 +377,28 @@ export async function buildCanvasSandboxed({
374
377
  async function runWorker({
375
378
  designRoot,
376
379
  canvasAbs,
380
+ assetUrlBase,
377
381
  env,
378
382
  }: {
379
383
  designRoot: string;
380
384
  canvasAbs: string;
385
+ assetUrlBase?: string;
381
386
  env: NodeJS.ProcessEnv;
382
387
  }): Promise<SandboxBuildResult> {
383
388
  const bun = resolveBunPath(env);
384
389
  let child: Bun.Subprocess<'ignore', 'pipe', 'pipe'>;
385
390
  try {
386
- child = Bun.spawn([bun, workerScript(env), designRoot, canvasAbs], {
387
- // THE EMPTY ENVIRONMENT IS THE POINT — see workerEnv().
388
- env: workerEnv(env),
389
- cwd: designRoot,
390
- stdin: 'ignore',
391
- stdout: 'pipe',
392
- stderr: 'pipe',
393
- });
391
+ child = Bun.spawn(
392
+ [bun, workerScript(env), designRoot, canvasAbs, ...(assetUrlBase ? [assetUrlBase] : [])],
393
+ {
394
+ // THE EMPTY ENVIRONMENT IS THE POINT — see workerEnv().
395
+ env: workerEnv(env),
396
+ cwd: designRoot,
397
+ stdin: 'ignore',
398
+ stdout: 'pipe',
399
+ stderr: 'pipe',
400
+ }
401
+ );
394
402
  } catch (err) {
395
403
  return {
396
404
  ok: false,
@@ -36,7 +36,9 @@
36
36
 
37
37
  import { join } from 'node:path';
38
38
 
39
- const [, , designRoot, canvasAbs] = process.argv;
39
+ // The optional third argument is the URL the design root is served under
40
+ // (BuildCanvasOptions.assetUrlBase) — absent keeps the bundler's own handling.
41
+ const [, , designRoot, canvasAbs, assetUrlBase] = process.argv;
40
42
 
41
43
  /**
42
44
  * Where the build engine lives.
@@ -67,6 +69,7 @@ async function main() {
67
69
  const built = await buildCanvasModule(canvasAbs, source, {
68
70
  designRoot,
69
71
  restrictImportsTo: designRoot,
72
+ ...(assetUrlBase ? { assetUrlBase } : {}),
70
73
  });
71
74
  process.stdout.write(
72
75
  JSON.stringify({ ok: true, js: built.js, locator: built.locator, etag: built.etag })
@@ -92,6 +92,81 @@ export interface BuildCanvasOptions {
92
92
  * Set to the absolute design root to arm it. Absent (desktop) ⇒ unchanged.
93
93
  */
94
94
  restrictImportsTo?: string;
95
+ /**
96
+ * The URL the design root is served under (e.g. `/.design`) — cell
97
+ * materializer Task 9.
98
+ *
99
+ * When set, every CSS `url()` that resolves inside the design root is
100
+ * rewritten to `<assetUrlBase>/<rel>` and left EXTERNAL, so the browser
101
+ * fetches it through the static route instead of the bundler reading it:
102
+ *
103
+ * • On a cell the file may not be on disk at all (the disk is a cache; the
104
+ * bytes are in the bucket) — a bundler read made ONE missing photo fail
105
+ * the whole canvas with `Could not resolve`. The static route
106
+ * materializes it on demand.
107
+ * • Anywhere, Bun inlines a small url() as a data URI but emits a large one
108
+ * as a hashed `./name-<hash>.ext` output that nothing serves — so every
109
+ * CSS background photo and webfont past the inline limit was already a
110
+ * broken reference. The route serves the real file at its real path.
111
+ *
112
+ * Absent ⇒ the bundler's own handling, unchanged.
113
+ */
114
+ assetUrlBase?: string;
115
+ }
116
+
117
+ /**
118
+ * Rewrite CSS `url()` references inside the design root to served absolute
119
+ * URLs (see `BuildCanvasOptions.assetUrlBase`).
120
+ *
121
+ * Done at LOAD, on the stylesheet text, because Bun keeps an external url's
122
+ * ORIGINAL text in the output whatever path `onResolve` returns — and a
123
+ * relative url inlined into the canvas's `<style>` then resolves against the
124
+ * iframe document, not the stylesheet it came from. The rewritten absolute
125
+ * URLs are then marked external on resolve, so the bundler never reads them.
126
+ * Anything outside the root is left as written, for the allowlist to judge.
127
+ */
128
+ function cssUrlAssets(designRoot: string, base: string): import('bun').BunPlugin {
129
+ const prefix = base.replace(/\/+$/, '');
130
+ const real = (p: string) => {
131
+ try {
132
+ return realpathSync(p);
133
+ } catch {
134
+ return p;
135
+ }
136
+ };
137
+ const realRoot = real(path.resolve(designRoot));
138
+ const URL_TOKEN = /url\(\s*(['"]?)([^'")]+?)\1\s*\)/g;
139
+ const rewrite = (css: string, cssFile: string): string =>
140
+ css.replace(URL_TOKEN, (whole, quote: string, raw: string) => {
141
+ const ref = raw.trim();
142
+ // Schemes (data:, http:, blob:), fragments and root-absolute paths are
143
+ // not files beside this stylesheet.
144
+ if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith('#') || ref.startsWith('/')) {
145
+ return whole;
146
+ }
147
+ const bare = ref.replace(/[?#].*$/, '');
148
+ if (!bare || bare.endsWith('.css')) return whole; // @import is the bundler's
149
+ const abs = path.resolve(path.dirname(cssFile), bare);
150
+ const target = path.join(real(path.dirname(abs)), path.basename(abs));
151
+ if (!target.startsWith(realRoot + path.sep)) return whole; // the allowlist decides
152
+ const rel = path.relative(realRoot, target).split(path.sep).map(encodeURIComponent);
153
+ return `url(${quote}${prefix}/${rel.join('/')}${ref.slice(bare.length)}${quote})`;
154
+ });
155
+ return {
156
+ name: 'maude-css-url-assets',
157
+ setup(builder) {
158
+ builder.onLoad({ filter: /\.css$/ }, async (args: { path: string }) => ({
159
+ contents: rewrite(await Bun.file(args.path).text(), args.path),
160
+ loader: 'css',
161
+ }));
162
+ builder.onResolve({ filter: /.*/ }, (args: { path: string; importer: string }) => {
163
+ if (!args.importer?.endsWith('.css')) return null;
164
+ if (prefix && !args.path.startsWith(`${prefix}/`)) return null;
165
+ if (!prefix && !args.path.startsWith('/')) return null;
166
+ return { path: args.path, external: true };
167
+ });
168
+ },
169
+ };
95
170
  }
96
171
 
97
172
  /**
@@ -155,6 +230,9 @@ export async function buildCanvasModule(
155
230
  'process.env.NODE_ENV': '"production"',
156
231
  },
157
232
  plugins: [
233
+ ...(options.assetUrlBase && options.designRoot
234
+ ? [cssUrlAssets(options.designRoot, options.assetUrlBase)]
235
+ : []),
158
236
  {
159
237
  name: 'css-sources',
160
238
  setup(builder) {