@vizejs/router 0.803.0 → 0.805.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.
package/README.md CHANGED
@@ -24,6 +24,12 @@ const match = router.match("/users/42/settings?from=ssr");
24
24
  router.resolve("user", { id: "42" });
25
25
  ```
26
26
 
27
+ A string location is an in-app reference. `match`, `navigate`, and the activation URL given to
28
+ `hydrateRouteState` report no match when it resolves to another origin (`//host/path`, an absolute
29
+ URL) or cannot be parsed or decoded; pass a `URL` object for an absolute URL such as a request URL.
30
+ History adapters receive the normalised path, query, and hash, never the caller's raw string.
31
+ `resolve` rejects a param that is empty or a `.`/`..` segment, so a built path names its own route.
32
+
27
33
  Route components are declared as `.vue` specifiers. JavaScript render-function component sources are
28
34
  not part of this package surface.
29
35
 
package/dist/index.mjs CHANGED
@@ -1,8 +1,37 @@
1
+ //#region src/location.ts
2
+ const PLACEHOLDER_ORIGIN = "https://vize.local";
3
+ /**
4
+ * Parse a location for matching, or return undefined when it cannot name a route.
5
+ *
6
+ * A string is an in-app reference: once resolved it has to stay on the placeholder
7
+ * origin, so `//host/path`, `\\host/path` and absolute URLs do not match. A pathname
8
+ * that starts with `//` is refused as well, because written back on its own it
9
+ * would be read as a host.
10
+ */
11
+ function parseLocation(input) {
12
+ let url;
13
+ if (input instanceof URL) url = input;
14
+ else {
15
+ try {
16
+ url = new URL(input, PLACEHOLDER_ORIGIN);
17
+ } catch {
18
+ return;
19
+ }
20
+ if (url.origin !== PLACEHOLDER_ORIGIN) return void 0;
21
+ }
22
+ return url.pathname.startsWith("//") ? void 0 : url;
23
+ }
24
+ /** Path, query and hash of a parsed location: the form a history adapter receives. */
25
+ function locationPath(url) {
26
+ return `${url.pathname}${url.search}${url.hash}`;
27
+ }
28
+ //#endregion
1
29
  //#region src/runtime.ts
2
30
  const ROUTER_ERROR = {
3
31
  duplicateName: "VIZE_ROUTER_DUPLICATE_NAME",
4
32
  duplicatePath: "VIZE_ROUTER_DUPLICATE_PATH",
5
33
  invalidComponent: "VIZE_ROUTER_COMPONENT_NOT_VUE",
34
+ invalidParam: "VIZE_ROUTER_INVALID_PARAM",
6
35
  invalidPath: "VIZE_ROUTER_INVALID_PATH",
7
36
  missingParam: "VIZE_ROUTER_MISSING_PARAM",
8
37
  unknownRoute: "VIZE_ROUTER_UNKNOWN_ROUTE"
@@ -27,14 +56,13 @@ function createRouterMatcher(routes) {
27
56
  return {
28
57
  routes,
29
58
  match(input) {
30
- const url = toUrl(input);
59
+ const url = parseLocation(input);
60
+ if (url === void 0) return void 0;
31
61
  for (const candidate of compiled) {
32
62
  const matched = candidate.pattern.exec(url.pathname);
33
63
  if (!matched) continue;
34
- const params = Object.fromEntries(candidate.params.flatMap((name, index) => {
35
- const value = matched[index + 1];
36
- return value == null ? [] : [[name, decodeURIComponent(value)]];
37
- }));
64
+ const params = decodeParams(candidate.params, matched);
65
+ if (params === void 0) return void 0;
38
66
  return {
39
67
  route: candidate.route,
40
68
  name: candidate.route.name,
@@ -154,6 +182,18 @@ function compilePath(pathPattern) {
154
182
  params
155
183
  };
156
184
  }
185
+ /** Decode captured params, or return undefined when one is not valid percent-encoding. */
186
+ function decodeParams(names, matched) {
187
+ try {
188
+ return Object.fromEntries(names.flatMap((name, index) => {
189
+ const value = matched[index + 1];
190
+ return value == null ? [] : [[name, decodeURIComponent(value)]];
191
+ }));
192
+ } catch (error) {
193
+ if (error instanceof URIError) return void 0;
194
+ throw error;
195
+ }
196
+ }
157
197
  function buildPath(pathPattern, params) {
158
198
  if (pathPattern === "/") return "/";
159
199
  return `/${pathPattern.split("/").filter(Boolean).flatMap((segment) => {
@@ -161,15 +201,20 @@ function buildPath(pathPattern, params) {
161
201
  const optional = segment.endsWith("?");
162
202
  const name = segment.slice(segment.startsWith(":") ? 1 : 1, optional ? -1 : void 0);
163
203
  const value = params[name];
164
- if (value == null) {
204
+ const segments = value == null ? [] : segment.startsWith("*") ? catchAllSegments(value) : [value];
205
+ if (!segments[0]) {
165
206
  if (optional) return [];
166
207
  throw new Error(`[${ROUTER_ERROR.missingParam}] Missing route param ${name}`);
167
208
  }
168
- return [segment.startsWith("*") ? value.split("/").map(encodeURIComponent).join("/") : encodeURIComponent(value)];
209
+ if (segments.some((item) => item === "." || item === "..")) throw new Error(`[${ROUTER_ERROR.invalidParam}] Route param ${name} cannot contain a "." or ".." segment`);
210
+ return [segments.map(encodeURIComponent).join("/")];
169
211
  }).join("/")}`;
170
212
  }
171
- function toUrl(input) {
172
- return input instanceof URL ? input : new URL(input, "https://vize.local");
213
+ /** Split a catch-all value into segments, dropping the empty ones a leading `/` produces. */
214
+ function catchAllSegments(value) {
215
+ const segments = value.split("/");
216
+ const first = segments.findIndex((segment) => segment !== "");
217
+ return first === -1 ? [] : segments.slice(first);
173
218
  }
174
219
  function parseQuery(searchParams) {
175
220
  const query = /* @__PURE__ */ new Map();
@@ -282,11 +327,17 @@ function createRouter(routes, options = {}) {
282
327
  active?.abort(/* @__PURE__ */ new Error("Navigation superseded"));
283
328
  const controller = new AbortController();
284
329
  active = controller;
285
- const initial = resolveLocation(matcher, location);
330
+ let initial;
331
+ let unresolved;
332
+ try {
333
+ initial = resolveLocation(matcher, location);
334
+ } catch (error) {
335
+ unresolved = error;
336
+ }
286
337
  if (!initial) {
287
338
  const result = {
288
339
  status: "error",
289
- error: /* @__PURE__ */ new Error(`No route matches ${locationLabel(location)}`)
340
+ error: unresolved ?? /* @__PURE__ */ new Error(`No route matches ${locationLabel(location)}`)
290
341
  };
291
342
  publish({
292
343
  ...state,
@@ -387,14 +438,15 @@ function createRouter(routes, options = {}) {
387
438
  };
388
439
  }
389
440
  function resolveLocation(matcher, location) {
390
- const url = isNamedLocation(location) ? matcher.resolve(location.name, location.params, {
441
+ const url = parseLocation(isNamedLocation(location) ? matcher.resolve(location.name, location.params, {
391
442
  ...location.query === void 0 ? {} : { query: location.query },
392
443
  ...location.hash === void 0 ? {} : { hash: location.hash }
393
- }) : location instanceof URL ? `${location.pathname}${location.search}${location.hash}` : location;
444
+ }) : location);
445
+ if (url === void 0) return void 0;
394
446
  const match = matcher.match(url);
395
447
  return match === void 0 ? void 0 : {
396
448
  match,
397
- url
449
+ url: locationPath(url)
398
450
  };
399
451
  }
400
452
  function isNamedLocation(location) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizejs/router",
3
- "version": "0.803.0",
3
+ "version": "0.805.0",
4
4
  "description": "Strongly typed SSR router foundations for Vize applications",
5
5
  "keywords": [
6
6
  "router",