noxid-cli 0.2.0

The Noxid compiler command line: check, build, test, adapt, and the agent surface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
class NoxidRouteQueryError extends Error {
  constructor(code, route, field, type, values) {
    super(`${code}: query field ${field} on ${route}`);
    this.name = "NoxidRouteQueryError";
    this.code = code;
    this.route = route;
    this.field = field;
    this.type = type;
    this.values = Object.freeze([...values]);
  }
}

function normalizeBasePath(value) {
  if (!value || value === "/") return "/";
  return value.endsWith("/") ? value.slice(0, -1) : value;
}

function isWithinBase(pathname, basePath) {
  return basePath === "/" || pathname === basePath || pathname.startsWith(`${basePath}/`);
}

function stripBasePath(pathname, basePath) {
  if (basePath === "/") return pathname;
  if (pathname === basePath) return "/";
  return pathname.startsWith(`${basePath}/`) ? pathname.slice(basePath.length) : null;
}

function resolveNavigation(value, basePath) {
  const raw = typeof value === "string" ? value : value.href;
  const url = new URL(raw, location.href);
  if (url.origin !== location.origin || isWithinBase(url.pathname, basePath)) return url;
  if (typeof raw === "string" && raw.startsWith("/") && !raw.startsWith("//")) {
    url.pathname = `${basePath}${url.pathname}`;
  }
  return url;
}

function pathSegments(pathname) {
  const normalized = pathname.length > 1 ? pathname.replace(/\/+$/, "") : pathname;
  if (normalized === "/") return [];
  return normalized.slice(1).split("/").map((segment) => decodeURIComponent(segment));
}

function patternSegments(pattern) {
  if (pattern === "/") return [];
  return pattern.slice(1).split("/");
}

function convertScalar(value, type) {
  if (type === "String") return value;
  if (type === "Int") {
    if (!/^-?\d+$/.test(value)) return null;
    const parsed = Number(value);
    return Number.isSafeInteger(parsed) ? parsed : null;
  }
  if (type === "Boolean") {
    if (value === "true") return true;
    if (value === "false") return false;
  }
  return null;
}

export function parseRouteQuery(route, source) {
  const search = source instanceof URL ? source.searchParams : source;
  const query = {};
  for (const field of route.query ?? []) {
    const values = search.getAll(field.name);
    if (values.length === 0) {
      if (field.required) {
        throw new NoxidRouteQueryError("ROUTE_QUERY_REQUIRED", route.id, field.name, field.type, values);
      }
      query[field.name] = null;
      continue;
    }
    if (values.length > 1) {
      throw new NoxidRouteQueryError("ROUTE_QUERY_MULTIPLE", route.id, field.name, field.type, values);
    }
    const converted = convertScalar(values[0], field.type);
    if (converted === null) {
      throw new NoxidRouteQueryError("ROUTE_QUERY_INVALID", route.id, field.name, field.type, values);
    }
    query[field.name] = converted;
  }
  return Object.freeze(query);
}

export function matchRoute(route, pathname) {
  const actual = pathSegments(pathname);
  if (actual.some((segment) => segment === "." || segment === ".." || /[\u0000-\u001f\u007f]/.test(segment))) return null;
  const expected = patternSegments(route.pattern);
  const catchAllIndex = expected.findIndex(
    (segment) => segment.startsWith("{*") && segment.endsWith("}"),
  );
  if (catchAllIndex === -1 && actual.length !== expected.length) return null;
  if (catchAllIndex !== -1 && (catchAllIndex !== expected.length - 1 || actual.length < expected.length)) {
    return null;
  }
  const parameters = new Map((route.parameters ?? []).map((parameter) => [parameter.name, parameter]));
  const params = Object.create(null);
  for (let index = 0; index < expected.length; index += 1) {
    const segment = expected[index];
    if (segment.startsWith("{*") && segment.endsWith("}")) {
      const name = segment.slice(2, -1);
      const parameter = parameters.get(name);
      if (!parameter?.catchAll) return null;
      params[name] = Object.freeze(actual.slice(index));
      break;
    }
    if (!segment.startsWith("{") || !segment.endsWith("}")) {
      if (segment !== actual[index]) return null;
      continue;
    }
    const name = segment.slice(1, -1);
    const parameter = parameters.get(name);
    if (!parameter || parameter.catchAll) return null;
    const converted = convertScalar(actual[index], parameter.type);
    if (converted === null) return null;
    params[name] = converted;
  }
  return Object.freeze(params);
}

function attachStyle(target, revision = "") {
  if (!target?.css) return { link: null, loaded: Promise.resolve() };
  const link = document.createElement("link");
  link.rel = "stylesheet";
  link.href = `${target.css}${revision}`;
  link.dataset.noxidRouteStyle = target.component;
  const loaded = new Promise((resolve, reject) => {
    link.addEventListener("load", resolve, { once: true });
    link.addEventListener("error", () => reject(new Error(`ROUTE_STYLE_LOAD_FAILED: ${target.css}`)), { once: true });
  });
  document.head.appendChild(link);
  return { link, loaded };
}

async function loadTargets(targets, signal, revision = "") {
  const styles = targets.map((target) => attachStyle(target, revision));
  try {
    const modules = await Promise.all(targets.map((target) => target.load()));
    await Promise.all(styles.map((style) => style.loaded));
    if (signal.aborted) throw signal.reason;
    return {
      loaded: targets.map((target, index) => ({ target, module: modules[index] })),
      styles: styles.flatMap((style) => (style.link ? [style.link] : [])),
    };
  } catch (error) {
    styles.forEach((style) => style.link?.remove());
    throw error;
  }
}

function renderFailure(root, code, message) {
  const section = document.createElement("section");
  section.setAttribute("role", "alert");
  section.dataset.noxidRouteError = code;
  const heading = document.createElement("h1");
  heading.textContent = code === "NOT_FOUND" ? "Page not found" : "Page failed to load";
  const detail = document.createElement("p");
  detail.textContent = message;
  section.append(heading, detail);
  root.replaceChildren(section);
}

function applyRouteMetadata(metadata, defaultTitle) {
  document.title = metadata?.title ?? defaultTitle;
  let description = document.head.querySelector("meta[data-noxid-route-description]");
  if (metadata?.description) {
    if (!description) {
      description = document.createElement("meta");
      description.name = "description";
      description.dataset.noxidRouteDescription = "";
      document.head.appendChild(description);
    }
    description.content = metadata.description;
  } else {
    description?.remove();
  }
}

function disposeView(view) {
  view?.instance?.dispose();
  view?.styles?.forEach((link) => link.remove());
}

function runtimeOptionsFor(route, params, query, url, path, basePath, streams, prefetchRoute, host) {
  return Object.freeze({
    ...(host.runtimeOptions ?? {}),
    route: Object.freeze({ id: route.id, pattern: route.pattern, path, urlPath: url.pathname, basePath, params, query }),
    middleware: Object.freeze({ applied: Object.freeze([]) }),
    streams,
    executeBoundary: typeof host.executeBoundary === "function"
      ? host.executeBoundary.bind(host)
      : host.runtimeOptions?.executeBoundary,
    prefetchRoute,
  });
}

async function acquirePrefetchLoaders(targets, initialProps, runtimeOptions, signal) {
  const props = { ...initialProps };
  const loaders = targets.flatMap((target) => (target.loaders ?? []).map((loader) => ({ target, loader })));
  if (loaders.length > 0 && typeof runtimeOptions.executeBoundary !== "function") {
    throw Object.assign(new Error("Route prefetch loaders require an execution boundary"), { code: "ROUTE_PREFETCH_LOADER_UNAVAILABLE" });
  }
  const stages = [...new Set(loaders.map(({ loader }) => loader.stage ?? 0))].sort((left, right) => left - right);
  for (const stage of stages) {
    const results = await Promise.all(loaders.filter(({ loader }) => (loader.stage ?? 0) === stage).map(async ({ target, loader }) => {
      if (signal.aborted) throw signal.reason ?? new DOMException("Route prefetch aborted", "AbortError");
      const inputs = (loader.arguments ?? []).map((argument) => Object.freeze({
        id: argument.id,
        name: argument.name,
        type: argument.type,
        value: props[argument.sourceName],
      }));
      const value = await runtimeOptions.executeBoundary(Object.freeze({
        id: loader.action,
        loader: loader.id,
        kind: "route-loader",
        component: target.component,
        action: loader.actionName,
        execution: loader.execution,
        arguments: Object.freeze(inputs),
        result: Object.freeze(loader.result),
        signal,
      }));
      return Object.freeze({ name: loader.name, value });
    }));
    for (const result of results) props[result.name] = result.value;
  }
  return Object.freeze(props);
}

function mountLoadedTargets(root, loaded, props, runtimeOptions) {
  let targetRoot = root;
  let parentOwner = null;
  let rootInstance = null;
  try {
    for (const { target, module } of loaded) {
      const mount = module?.[`mount${target.component}`];
      if (typeof mount !== "function") throw new Error(`ROUTE_MOUNT_MISSING: ${target.component}`);
      const instance = mount(targetRoot, props, {}, parentOwner, runtimeOptions);
      rootInstance ??= instance;
      parentOwner = instance.owner;
      if (target.layout) {
        const outlet = targetRoot.querySelector("outlet");
        if (!outlet) throw new Error(`ROUTE_LAYOUT_OUTLET_MISSING: ${target.component}`);
        targetRoot = outlet;
      }
    }
    return rootInstance;
  } catch (error) {
    rootInstance?.dispose();
    throw error;
  }
}

export function startRouter({ root, routes, host = {}, streams = Object.freeze({}), basePath: configuredBasePath = "/", defaultTitle: configuredDefaultTitle = document.title }) {
  const basePath = normalizeBasePath(configuredBasePath);
  const defaultTitle = configuredDefaultTitle || document.title;
  let current = null;
  let transition = null;
  let pending = null;
  let sequence = 0;
  const prefetches = new Map();

  function commitHistory(url, options) {
    if (!options.popstate) history[options.replace ? "replaceState" : "pushState"]({}, "", url);
  }

  function prefetchFailure(code, message, route = null, cause = null) {
    return Object.assign(new Error(message), {
      code,
      semanticId: route?.id ?? null,
      route: route?.id ?? null,
      cause,
    });
  }

  function prefetchRoute(value) {
    let url;
    try {
      url = resolveNavigation(value, basePath);
    } catch (cause) {
      return Promise.reject(prefetchFailure("ROUTE_PREFETCH_URL_INVALID", "Route prefetch requires a valid URL", null, cause));
    }
    if (url.origin !== location.origin) {
      return Promise.reject(prefetchFailure("ROUTE_PREFETCH_EXTERNAL", "Route prefetch only supports same-origin URLs"));
    }
    if (!isWithinBase(url.pathname, basePath)) {
      return Promise.reject(prefetchFailure("ROUTE_PREFETCH_OUTSIDE_BASE", `Route prefetch URL is outside ${basePath}`));
    }
    url.hash = "";
    const key = `${url.pathname}${url.search}`;
    const existing = prefetches.get(key);
    if (existing) return existing.promise;
    const controller = new AbortController();
    const work = (async () => {
      const applicationPath = stripBasePath(url.pathname, basePath);
      const matched = routes
        .map((route) => ({ route, params: matchRoute(route, applicationPath) }))
        .find((item) => item.params !== null);
      if (!matched) throw prefetchFailure("ROUTE_PREFETCH_NOT_FOUND", `No route matches ${applicationPath}`);
      let query;
      try { query = parseRouteQuery(matched.route, url); }
      catch (cause) {
        throw prefetchFailure("ROUTE_PREFETCH_QUERY_INVALID", `Route prefetch query is invalid for ${matched.route.id}`, matched.route, cause);
      }
      let modules;
      try { modules = await Promise.all(matched.route.targets.map((target) => target.load())); }
      catch (cause) {
        throw prefetchFailure("ROUTE_PREFETCH_MODULE_FAILED", `Route prefetch could not load modules for ${matched.route.id}`, matched.route, cause);
      }
      const runtimeOptions = runtimeOptionsFor(matched.route, matched.params, query, url, applicationPath, basePath, streams, prefetchRoute, host);
      let props = Object.freeze({ ...matched.params, ...query });
      try { props = await acquirePrefetchLoaders(matched.route.targets, props, runtimeOptions, controller.signal); }
      catch (cause) {
        if (cause?.code === "ROUTE_PREFETCH_LOADER_UNAVAILABLE") throw cause;
        throw prefetchFailure("ROUTE_PREFETCH_LOADER_FAILED", `Route prefetch loaders failed for ${matched.route.id}`, matched.route, cause);
      }
      try {
        for (let index = 0; index < matched.route.targets.length; index += 1) {
          const target = matched.route.targets[index];
          const prefetch = modules[index]?.[`__noxidPrefetch${target.component}`];
          if (typeof prefetch === "function") await prefetch(props, runtimeOptions);
        }
      } catch (cause) {
        throw prefetchFailure("ROUTE_PREFETCH_RESOURCE_FAILED", `Route resource prefetch failed for ${matched.route.id}`, matched.route, cause);
      }
      return Object.freeze({ routeId: matched.route.id, url: key });
    })();
    const promise = work.catch((cause) => {
      if (typeof cause?.code === "string" && cause.code.startsWith("ROUTE_PREFETCH_")) throw cause;
      throw prefetchFailure("ROUTE_PREFETCH_FAILED", "Route prefetch failed", null, cause);
    });
    prefetches.set(key, { promise, controller });
    void promise.finally(() => {
      if (prefetches.get(key)?.promise === promise) prefetches.delete(key);
    }).catch(() => {});
    return promise;
  }

  async function showRouteError(error, context, runtimeOptions, options) {
    if (context.signal.aborted) return false;
    const code = typeof error?.code === "string" ? error.code : "ROUTE_NAVIGATION_FAILED";
    const message = typeof error?.message === "string" ? error.message : String(error);
    commitHistory(context.url, options);
    if (context.route.error) {
      try {
        const boundary = await loadTargets([context.route.error], context.signal, options.revision);
        const instance = mountLoadedTargets(root, boundary.loaded, { code, message }, runtimeOptions);
        current = { route: context.route, instance, styles: boundary.styles, url: context.url };
        pending = null;
        return false;
      } catch (boundaryError) {
        if (context.signal.aborted) return false;
        renderFailure(root, "ROUTE_ERROR_BOUNDARY_FAILED", String(boundaryError?.message ?? boundaryError));
        pending = null;
        return false;
      }
    }
    renderFailure(root, code, message);
    pending = null;
    return false;
  }

  async function navigate(value, options = {}) {
    const url = resolveNavigation(value, basePath);
    if (url.origin !== location.origin || !isWithinBase(url.pathname, basePath)) {
      location.assign(url.href);
      return false;
    }
    const applicationPath = stripBasePath(url.pathname, basePath);
    pending?.abort(new DOMException("Navigation superseded", "AbortError"));
    disposeView(transition);
    transition = null;
    const controller = new AbortController();
    pending = controller;
    const navigation = ++sequence;
    const matched = routes
      .map((route) => ({ route, params: matchRoute(route, applicationPath) }))
      .find((item) => item.params !== null);
    if (!matched) {
      applyRouteMetadata(null, defaultTitle);
      commitHistory(url, options);
      disposeView(current);
      current = null;
      renderFailure(root, "NOT_FOUND", `No route matches ${applicationPath}`);
      pending = null;
      return false;
    }
    const baseContext = { route: matched.route, params: matched.params, url, path: applicationPath, basePath, signal: controller.signal };
    let query;
    try {
      query = parseRouteQuery(matched.route, url);
    } catch (error) {
      if (controller.signal.aborted) return false;
      const emptyQuery = Object.freeze({});
      const context = { ...baseContext, query: emptyQuery, props: matched.params };
      const runtimeOptions = runtimeOptionsFor(matched.route, matched.params, emptyQuery, url, applicationPath, basePath, streams, prefetchRoute, host);
      applyRouteMetadata(null, defaultTitle);
      disposeView(current);
      current = null;
      return showRouteError(error, context, runtimeOptions, options);
    }
    const routeProps = Object.freeze({ ...matched.params, ...query });
    const context = { ...baseContext, query, props: routeProps };
    const runtimeOptions = runtimeOptionsFor(matched.route, matched.params, query, url, applicationPath, basePath, streams, prefetchRoute, host);
    applyRouteMetadata(matched.route.metadata, defaultTitle);
    let loadingView = null;
    let routeStyles = [];
    let routeInstance = null;
    try {
      if (matched.route.loading) {
        disposeView(current);
        current = null;
        const loading = await loadTargets([matched.route.loading], controller.signal, options.revision);
        if (navigation !== sequence || controller.signal.aborted) {
          loading.styles.forEach((link) => link.remove());
          return false;
        }
        loadingView = { instance: mountLoadedTargets(root, loading.loaded, {}, runtimeOptions), styles: loading.styles };
        transition = loadingView;
      }
      const routeAssets = await loadTargets(matched.route.targets, controller.signal, options.revision);
      routeStyles = routeAssets.styles;
      if (navigation !== sequence || controller.signal.aborted) {
        if (loadingView && transition === loadingView) { disposeView(loadingView); transition = null; }
        routeStyles.forEach((link) => link.remove());
        return false;
      }
      if (loadingView) {
        disposeView(loadingView);
        if (transition === loadingView) transition = null;
      } else disposeView(current);
      current = null;
      routeInstance = mountLoadedTargets(root, routeAssets.loaded, routeProps, runtimeOptions);
      current = { route: matched.route, instance: routeInstance, styles: routeStyles, url };
      commitHistory(url, options);
      pending = null;
      return true;
    } catch (error) {
      routeInstance?.dispose();
      routeStyles.forEach((link) => link.remove());
      if (loadingView && transition === loadingView) { disposeView(loadingView); transition = null; }
      if (navigation !== sequence || controller.signal.aborted) return false;
      disposeView(current);
      current = null;
      return showRouteError(error, context, runtimeOptions, options);
    }
  }

  function onClick(event) {
    if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
    const anchor = event.target.closest?.("a[href]");
    if (!anchor || anchor.target || anchor.hasAttribute("download")) return;
    const url = new URL(anchor.href, location.href);
    if (url.origin !== location.origin || !isWithinBase(url.pathname, basePath)) return;
    event.preventDefault();
    void navigate(anchor.getAttribute("href"));
  }

  const onPopState = () => void navigate(location.href, { popstate: true, replace: true });
  document.addEventListener("click", onClick);
  window.addEventListener("popstate", onPopState);
  void navigate(location.href, { popstate: true, replace: true });

  return Object.freeze({
    navigate,
    refresh(revision) {
      return navigate(location.href, { popstate: true, replace: true, revision: `?noxid-hmr=${encodeURIComponent(revision)}` });
    },
    basePath,
    current: () => current?.route ?? null,
    dispose() {
      pending?.abort(new DOMException("Router disposed", "AbortError"));
      for (const entry of prefetches.values()) entry.controller.abort(new DOMException("Router disposed", "AbortError"));
      prefetches.clear();
      document.removeEventListener("click", onClick);
      window.removeEventListener("popstate", onPopState);
      disposeView(transition);
      transition = null;
      disposeView(current);
      current = null;
      applyRouteMetadata(null, defaultTitle);
    },
  });
}