@zubra/router 6.0.0-rc.0 → 6.0.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zubra/router",
3
- "version": "6.0.0-rc.0",
3
+ "version": "6.0.1",
4
4
  "description": "Serve Zubra components that say 'use route <path>' as pages, with a server-only load, for apps with no host framework.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -22,8 +22,8 @@
22
22
  "urlpattern-polyfill": "10.1.0"
23
23
  },
24
24
  "peerDependencies": {
25
- "@zubra/compiler": "^6.0.0-rc.0",
26
- "@zubra/server": "^6.0.0-rc.0"
25
+ "@zubra/compiler": "^6.0.0",
26
+ "@zubra/server": "^6.0.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@zubra/compiler": "file:../compiler",
@@ -43,5 +43,17 @@
43
43
  "type": "git",
44
44
  "url": "git+https://github.com/zubra-js/zubra.git",
45
45
  "directory": "router"
46
- }
46
+ },
47
+ "homepage": "https://github.com/zubra-js/zubra/tree/main/router#readme",
48
+ "bugs": {
49
+ "url": "https://github.com/zubra-js/zubra/issues"
50
+ },
51
+ "keywords": [
52
+ "zubra",
53
+ "web components",
54
+ "custom elements",
55
+ "router",
56
+ "routing",
57
+ "ssr"
58
+ ]
47
59
  }
package/src/answers.js CHANGED
@@ -1,7 +1,24 @@
1
1
  // What a `load` can return besides props. Marked with a registered symbol, so an answer made by
2
2
  // one copy of this module is recognised by another.
3
+ import { RouteError } from './table.js';
4
+
3
5
  export const ANSWER = Symbol.for('@zubra/router.answer');
4
6
 
7
+ /** Normalize local redirects and require explicit permission to leave the site. */
8
+ export function checkedLocation({ location, external }, url) {
9
+ if (/[\x00-\x1f\x7f]/.test(location)) throw new RouteError('redirect() location contains a control character');
10
+ if (/^[/\\]/.test(location)) return '/' + location.replace(/^[/\\]+/, '');
11
+ let target;
12
+ try {
13
+ target = new URL(location, url);
14
+ } catch {
15
+ throw new RouteError(`redirect() to ${JSON.stringify(location)}: not a URL`);
16
+ }
17
+ if (!['http:', 'https:'].includes(target.protocol)) throw new RouteError(`redirect() to a ${target.protocol} URL is refused`);
18
+ if (target.origin !== url.origin && !external) throw new RouteError(`redirect() to ${target.origin}, another site: say redirect(to, { external: true }) if that's meant`);
19
+ return target.origin === url.origin ? target.pathname + target.search + target.hash : target.href;
20
+ }
21
+
5
22
  /** `return notFound()`: the path has nothing behind it (no such farm). The response is a 404. */
6
23
  export const notFound = () => ({ [ANSWER]: 'notFound' });
7
24
 
package/src/index.js CHANGED
@@ -29,7 +29,7 @@ import { existsSync, mkdirSync, readdirSync, renameSync, statSync, writeFileSync
29
29
  import { dirname, join, relative, resolve, sep } from 'node:path';
30
30
  import { pathToFileURL } from 'node:url';
31
31
  import { findCompiler } from '@zubra/server';
32
- import { ANSWER } from './answers.js';
32
+ import { ANSWER, checkedLocation } from './answers.js';
33
33
  import { APP, buildTable, fromRoot, layoutMatcher, matcher, RouteError } from './table.js';
34
34
 
35
35
  export { notFound, redirect, show } from './answers.js';
@@ -395,25 +395,6 @@ function rewriteImports(code, to) {
395
395
  return code.replace(/(\bfrom\s*|\bimport\s*\(?\s*)(['"])(\.{1,2}\/[^'"]+)\2/g, (_, keyword, quote, specifier) => `${keyword}${quote}${to(specifier)}${quote}`);
396
396
  }
397
397
 
398
- /**
399
- * Where a `redirect()` sends the browser: on this site unless it says `{ external: true }`.
400
- * A path starting `//` or `/\` is another site to a browser, so it's written with one slash;
401
- * an absolute URL elsewhere (a `next` from the query string) is refused, and so is any scheme
402
- * that isn't http(s). `redirect(next)` after a sign-in can't send anyone off the site.
403
- */
404
- function checkedLocation({ location, external }, url) {
405
- if (/^[/\\]/.test(location)) return '/' + location.replace(/^[/\\]+/, '');
406
- let target;
407
- try {
408
- target = new URL(location, url);
409
- } catch {
410
- throw new RouteError(`redirect() to ${JSON.stringify(location)}: not a URL`);
411
- }
412
- if (!['http:', 'https:'].includes(target.protocol)) throw new RouteError(`redirect() to a ${target.protocol} URL is refused`);
413
- if (target.origin !== url.origin && !external) throw new RouteError(`redirect() to ${target.origin}, another site: say redirect(to, { external: true }) if that's meant`);
414
- return target.origin === url.origin ? target.pathname + target.search + target.hash : target.href;
415
- }
416
-
417
398
  // A Host header is a host and an optional port. Anything else (a path, a query, an empty one)
418
399
  // would move the URL's path away from the request's: `Host: site/admin` with `GET /public` would
419
400
  // read as `/admin/public`, past a proxy that allowed `/public`. That's refused (the callers answer