@vercel/routing-utils 5.3.2 → 5.3.3

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/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { GetRoutesProps, NormalizedRoutes, Route, RouteWithHandle } from './types';
2
2
  export { appendRoutesToPhase } from './append';
3
3
  export { mergeRoutes } from './merge';
4
+ export { getOwnershipGuard, normalizeRoutePrefix, scopeRouteSourceToOwnership, } from './service-route-ownership';
4
5
  export * from './schemas';
5
6
  export { getCleanUrls, sourceToRegex } from './superstatic';
6
7
  export * from './types';
package/dist/index.js CHANGED
@@ -21,11 +21,14 @@ var src_exports = {};
21
21
  __export(src_exports, {
22
22
  appendRoutesToPhase: () => import_append.appendRoutesToPhase,
23
23
  getCleanUrls: () => import_superstatic2.getCleanUrls,
24
+ getOwnershipGuard: () => import_service_route_ownership.getOwnershipGuard,
24
25
  getTransformedRoutes: () => getTransformedRoutes,
25
26
  isHandler: () => isHandler,
26
27
  isValidHandleValue: () => isValidHandleValue,
27
28
  mergeRoutes: () => import_merge.mergeRoutes,
29
+ normalizeRoutePrefix: () => import_service_route_ownership.normalizeRoutePrefix,
28
30
  normalizeRoutes: () => normalizeRoutes,
31
+ scopeRouteSourceToOwnership: () => import_service_route_ownership.scopeRouteSourceToOwnership,
29
32
  sourceToRegex: () => import_superstatic2.sourceToRegex
30
33
  });
31
34
  module.exports = __toCommonJS(src_exports);
@@ -33,6 +36,7 @@ var import_url = require("url");
33
36
  var import_superstatic = require("./superstatic");
34
37
  var import_append = require("./append");
35
38
  var import_merge = require("./merge");
39
+ var import_service_route_ownership = require("./service-route-ownership");
36
40
  __reExport(src_exports, require("./schemas"), module.exports);
37
41
  var import_superstatic2 = require("./superstatic");
38
42
  __reExport(src_exports, require("./types"), module.exports);
@@ -366,11 +370,14 @@ function getTransformedRoutes(vercelConfig) {
366
370
  0 && (module.exports = {
367
371
  appendRoutesToPhase,
368
372
  getCleanUrls,
373
+ getOwnershipGuard,
369
374
  getTransformedRoutes,
370
375
  isHandler,
371
376
  isValidHandleValue,
372
377
  mergeRoutes,
378
+ normalizeRoutePrefix,
373
379
  normalizeRoutes,
380
+ scopeRouteSourceToOwnership,
374
381
  sourceToRegex,
375
382
  ...require("./schemas"),
376
383
  ...require("./types")
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Normalize a route prefix to always have a leading slash and no trailing slash
3
+ * unless it is root (`/`).
4
+ */
5
+ export declare function normalizeRoutePrefix(routePrefix: string): string;
6
+ /**
7
+ * Create a service ownership guard:
8
+ * - Root services exclude all non-root prefixes.
9
+ * - Non-root services are constrained to their prefix and exclude descendants.
10
+ */
11
+ export declare function getOwnershipGuard(ownerPrefix: string, allRoutePrefixes: string[]): string;
12
+ export declare function scopeRouteSourceToOwnership(source: string, ownershipGuard: string): string;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var service_route_ownership_exports = {};
20
+ __export(service_route_ownership_exports, {
21
+ getOwnershipGuard: () => getOwnershipGuard,
22
+ normalizeRoutePrefix: () => normalizeRoutePrefix,
23
+ scopeRouteSourceToOwnership: () => scopeRouteSourceToOwnership
24
+ });
25
+ module.exports = __toCommonJS(service_route_ownership_exports);
26
+ function normalizeRoutePrefix(routePrefix) {
27
+ let normalized = routePrefix.startsWith("/") ? routePrefix : `/${routePrefix}`;
28
+ if (normalized !== "/" && normalized.endsWith("/")) {
29
+ normalized = normalized.slice(0, -1);
30
+ }
31
+ return normalized || "/";
32
+ }
33
+ function escapeForRegex(value) {
34
+ return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
35
+ }
36
+ function toPrefixMatcher(routePrefix) {
37
+ return `${escapeForRegex(routePrefix)}(?:/|$)`;
38
+ }
39
+ function isDescendantPrefix(candidate, prefix) {
40
+ return candidate !== prefix && candidate.startsWith(`${prefix}/`);
41
+ }
42
+ function getOwnershipGuard(ownerPrefix, allRoutePrefixes) {
43
+ const owner = normalizeRoutePrefix(ownerPrefix);
44
+ const normalizedPrefixes = Array.from(
45
+ new Set(allRoutePrefixes.map(normalizeRoutePrefix))
46
+ );
47
+ const nonRootPrefixes = normalizedPrefixes.filter((prefix) => prefix !== "/").sort((a, b) => b.length - a.length);
48
+ if (owner === "/") {
49
+ return nonRootPrefixes.map((prefix) => `(?!${toPrefixMatcher(prefix)})`).join("");
50
+ }
51
+ const descendants = nonRootPrefixes.filter(
52
+ (prefix) => isDescendantPrefix(prefix, owner)
53
+ );
54
+ const positive = `(?=${toPrefixMatcher(owner)})`;
55
+ const negative = descendants.map((prefix) => `(?!${toPrefixMatcher(prefix)})`).join("");
56
+ return `${positive}${negative}`;
57
+ }
58
+ function scopeRouteSourceToOwnership(source, ownershipGuard) {
59
+ if (!ownershipGuard) {
60
+ return source;
61
+ }
62
+ const inner = source.startsWith("^") ? source.slice(1) : source;
63
+ return `^${ownershipGuard}(?:${inner})`;
64
+ }
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ getOwnershipGuard,
68
+ normalizeRoutePrefix,
69
+ scopeRouteSourceToOwnership
70
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/routing-utils",
3
- "version": "5.3.2",
3
+ "version": "5.3.3",
4
4
  "description": "Vercel routing utilities",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/jest": "27.4.1",
22
- "@types/node": "14.18.33",
22
+ "@types/node": "20.11.0",
23
23
  "ajv": "^6.12.3",
24
24
  "jest-junit": "16.0.0",
25
25
  "typescript": "4.9.5"