New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

build-output-router

Package Overview
Dependencies
Maintainers
0
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

build-output-router - npm Package Compare versions

Comparing version 0.0.1-eaaa296 to 0.0.1-ed54790

index-B_hJqMhp.js

3

package.json
{
"name": "build-output-router",
"version": "0.0.1-eaaa296",
"version": "0.0.1-ed54790",
"description": "Router for the Build Output API (v3)",

@@ -43,2 +43,3 @@ "license": "MIT",

"vite-plugin-dts": "^4.2.2",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.1.1"

@@ -45,0 +46,0 @@ },

@@ -24,3 +24,3 @@ <p align="center">

> [!NOTE]
> It is recommended to lock the version you use to a specific patch release.
> [!WARNING]
> There _might_ be changes the library's public API. It is recommended to lock the version you use to a specific patch release.

@@ -1,2 +0,4 @@

import { ConfigMetadata, RequestContext, RoutesGroupedByPhase } from './types';
import { RoutesGroupedByPhase } from './types/build-output/index.ts';
import { RequestContext } from './types/request-context/index.ts';
import { ConfigMetadata } from './routes-matcher';
export declare class Router {

@@ -3,0 +5,0 @@ private routes;

@@ -1,8 +0,9 @@

import { RoutesMatcher as w } from "./routes-matcher.js";
import { isUrl as R, applySearchParams as g, applyHeaders as p } from "./utils/http.js";
import "./utils/pcre.js";
class S {
import { applyHeaders as p } from "./utils/http/apply-headers.js";
import { applySearchParams as w } from "./utils/http/apply-search-params.js";
import { isUrl as m } from "./utils/http/is-url.js";
import { RoutesMatcher as R } from "./routes-matcher.js";
class $ {
constructor(d, h) {
this.routes = d, this.configMetadata = h, this.fetch = async (t) => {
const o = new w(this.routes, t, this.configMetadata), s = await this.findMatch(o);
const o = new R(this.routes, t, this.configMetadata), s = await this.findMatch(o);
return this.generateResponse(t, s);

@@ -28,5 +29,5 @@ }, this.findMatch = async (t, o = "none", s = !1) => await t.run(o) === "error" || !s && t.status && t.status >= 400 ? this.findMatch(t, "error", !0) : {

e = new Response(c, { status: r });
else if (R(s)) {
else if (m(s)) {
const i = new URL(s);
g(i.searchParams, a), e = await fetch(i, t);
w(i.searchParams, a), e = await fetch(i, t);
} else

@@ -44,4 +45,4 @@ e = await ((f = o.get(s)) == null ? void 0 : f.fetch({ path: s, searchParams: a })) ?? new Response("Not Found", { status: 404 });

export {
S as Router
$ as Router
};
//# sourceMappingURL=router.js.map

@@ -1,3 +0,30 @@

import { ConfigMetadata, Phase, RequestContext, RoutesGroupedByPhase } from './types';
import { RoutingMatch } from './utils';
import { Phase, RoutesGroupedByPhase } from './types/build-output/index.ts';
import { RequestContext } from './types/request-context/index.ts';
import { WildCard } from './types/vercel-config/index.ts';
export type ConfigMetadata = {
locales: Set<string>;
wildcardConfig: WildCard[] | undefined;
};
export type RoutingMatch = {
path: string;
status: number | undefined;
headers: {
/**
* The headers present on a source route.
* Gets applied to the final response before the response headers from running a function.
*/
normal: Headers;
/**
* The *important* headers - the ones present on a source route that specifies `important: true`.
* Gets applied to the final response after the response headers from running a function.
*/
important: Headers;
/**
* Tracks if a location header is found, and what the value is, after running a middleware function.
*/
middlewareLocation?: string | null;
};
searchParams: URLSearchParams;
body: BodyInit | undefined | null;
};
export type CheckRouteStatus = 'skip' | 'next' | 'done' | 'error';

@@ -4,0 +31,0 @@ export type CheckPhaseStatus = Extract<CheckRouteStatus, 'error' | 'done'>;

@@ -1,5 +0,10 @@

import { applySearchParams as k, applyHeaders as R, isUrl as x, parseAcceptLanguage as g } from "./utils/http.js";
import { checkHasField as y } from "./utils/matcher.js";
import { matchPCRE as v, applyPCREMatches as L } from "./utils/pcre.js";
import { isLocaleTrailingSlashRegex as $, getNextPhase as P } from "./utils/routing.js";
import { applyHeaders as R } from "./utils/http/apply-headers.js";
import { applySearchParams as k } from "./utils/http/apply-search-params.js";
import { isUrl as x } from "./utils/http/is-url.js";
import { parseAcceptLanguage as g } from "./utils/http/parse-accept-language.js";
import { applyPCREMatches as v } from "./utils/pcre/apply-pcre-matches.js";
import { matchPCRE as L } from "./utils/pcre/match-pcre.js";
import { checkHasField as y } from "./utils/routing/check-has-field.js";
import { getNextPhase as P } from "./utils/routing/get-next-phase.js";
import { isLocaleTrailingSlashRegex as $ } from "./utils/routing/is-locale-trailing-slash-regex.js";
/*!

@@ -12,36 +17,36 @@ * cookie

var H = M;
function M(n, f) {
if (typeof n != "string")
function M(o, f) {
if (typeof o != "string")
throw new TypeError("argument str must be a string");
for (var u = {}, m = f || {}, w = m.decode || C, e = 0; e < n.length; ) {
var i = n.indexOf("=", e);
for (var u = {}, p = f || {}, w = p.decode || C, t = 0; t < o.length; ) {
var i = o.indexOf("=", t);
if (i === -1)
break;
var s = n.indexOf(";", e);
var s = o.indexOf(";", t);
if (s === -1)
s = n.length;
s = o.length;
else if (s < i) {
e = n.lastIndexOf(";", i - 1) + 1;
t = o.lastIndexOf(";", i - 1) + 1;
continue;
}
var r = n.slice(e, i).trim();
if (u[r] === void 0) {
var t = n.slice(i + 1, s).trim();
t.charCodeAt(0) === 34 && (t = t.slice(1, -1)), u[r] = D(t, w);
var a = o.slice(t, i).trim();
if (u[a] === void 0) {
var e = o.slice(i + 1, s).trim();
e.charCodeAt(0) === 34 && (e = e.slice(1, -1)), u[a] = D(e, w);
}
e = s + 1;
t = s + 1;
}
return u;
}
function C(n) {
return n.indexOf("%") !== -1 ? decodeURIComponent(n) : n;
function C(o) {
return o.indexOf("%") !== -1 ? decodeURIComponent(o) : o;
}
function D(n, f) {
function D(o, f) {
try {
return f(n);
return f(o);
} catch {
return n;
return o;
}
}
class O {
class N {
/**

@@ -59,46 +64,46 @@ * Creates a new instance of a request matcher.

*/
constructor(f, u, m) {
constructor(f, u, p) {
var w;
this.routes = f, this.ctx = u, this.checkRouteMatch = (e, { checkStatus: i, checkIntercept: s }) => {
var a, o;
const r = v(e.src, this.path, e.caseSensitive);
if (!r.match || e.methods && !e.methods.map((h) => h.toUpperCase()).includes(this.ctx.request.method.toUpperCase()))
this.routes = f, this.ctx = u, this.checkRouteMatch = (t, { checkStatus: i, checkIntercept: s }) => {
var r, n;
const a = L(t.src, this.path, t.caseSensitive);
if (!a.match || t.methods && !t.methods.map((h) => h.toUpperCase()).includes(this.ctx.request.method.toUpperCase()))
return;
const t = {
const e = {
url: this.url,
cookies: this.cookies,
headers: this.ctx.request.headers,
routeDest: e.dest
routeDest: t.dest
};
if (!((a = e.has) != null && a.find((h) => {
const c = y(h, t);
return c.newRouteDest && (t.routeDest = c.newRouteDest), !c.valid;
})) && !((o = e.missing) != null && o.find((h) => y(h, t).valid)) && !(i && e.status !== this.status)) {
if (s && e.dest) {
const h = /\/(\(\.+\))+/, c = h.test(e.dest), d = h.test(this.path);
if (!((r = t.has) != null && r.find((h) => {
const c = y(h, e);
return c.newRouteDest && (e.routeDest = c.newRouteDest), !c.valid;
})) && !((n = t.missing) != null && n.find((h) => y(h, e).valid)) && !(i && t.status !== this.status)) {
if (s && t.dest) {
const h = /\/(\(\.+\))+/, c = h.test(t.dest), d = h.test(this.path);
if (c && !d)
return;
}
return { routeMatch: r, routeDest: t.routeDest };
return { routeMatch: a, routeDest: e.routeDest };
}
}, this.processMiddlewareResp = (e) => {
const i = "x-middleware-override-headers", s = e.headers.get(i);
}, this.processMiddlewareResp = (t) => {
const i = "x-middleware-override-headers", s = t.headers.get(i);
if (s) {
const h = new Set(s.split(",").map((c) => c.trim()));
for (const c of h.keys()) {
const d = `x-middleware-request-${c}`, l = e.headers.get(d);
this.ctx.request.headers.get(c) !== l && (l ? this.ctx.request.headers.set(c, l) : this.ctx.request.headers.delete(c)), e.headers.delete(d);
const d = `x-middleware-request-${c}`, l = t.headers.get(d);
this.ctx.request.headers.get(c) !== l && (l ? this.ctx.request.headers.set(c, l) : this.ctx.request.headers.delete(c)), t.headers.delete(d);
}
e.headers.delete(i);
t.headers.delete(i);
}
const r = "x-middleware-rewrite", t = e.headers.get(r);
if (t) {
const h = new URL(t, this.url), c = this.url.hostname !== h.hostname;
this.path = c ? `${h}` : h.pathname, k(this.searchParams, h.searchParams), e.headers.delete(r);
const a = "x-middleware-rewrite", e = t.headers.get(a);
if (e) {
const h = new URL(e, this.url), c = this.url.hostname !== h.hostname;
this.path = c ? `${h}` : h.pathname, k(this.searchParams, h.searchParams), t.headers.delete(a);
}
const a = "x-middleware-next";
e.headers.get(a) ? e.headers.delete(a) : !t && !e.headers.has("location") ? (this.body = e.body, this.status = e.status) : e.headers.has("location") && e.status >= 300 && e.status < 400 && (this.status = e.status), R(this.ctx.request.headers, e.headers), R(this.headers.normal, e.headers), this.headers.middlewareLocation = e.headers.get("location");
}, this.runRouteMiddleware = async (e) => {
if (!e) return !0;
const i = e && this.ctx.assets.get(e);
const r = "x-middleware-next";
t.headers.get(r) ? t.headers.delete(r) : !e && !t.headers.has("location") ? (this.body = t.body, this.status = t.status) : t.headers.has("location") && t.status >= 300 && t.status < 400 && (this.status = t.status), R(this.ctx.request.headers, t.headers), R(this.headers.normal, t.headers), this.headers.middlewareLocation = t.headers.get("location");
}, this.runRouteMiddleware = async (t) => {
if (!t) return !0;
const i = t && this.ctx.assets.get(t);
if (!i || i.kind !== "middleware")

@@ -110,60 +115,60 @@ return this.status = 500, !1;

});
return this.middlewareInvoked.push(e), s.status === 500 ? (this.status = s.status, !1) : (this.processMiddlewareResp(s), !0);
}, this.applyRouteOverrides = (e) => {
e.override && (this.status = void 0, this.headers.normal = new Headers(), this.headers.important = new Headers());
}, this.applyRouteHeaders = (e, i, s) => {
e.headers && (R(this.headers.normal, e.headers, {
return this.middlewareInvoked.push(t), s.status === 500 ? (this.status = s.status, !1) : (this.processMiddlewareResp(s), !0);
}, this.applyRouteOverrides = (t) => {
t.override && (this.status = void 0, this.headers.normal = new Headers(), this.headers.important = new Headers());
}, this.applyRouteHeaders = (t, i, s) => {
t.headers && (R(this.headers.normal, t.headers, {
match: i,
captureGroupKeys: s
}), e.important && R(this.headers.important, e.headers, {
}), t.important && R(this.headers.important, t.headers, {
match: i,
captureGroupKeys: s
}));
}, this.applyRouteStatus = (e) => {
e.status && (this.status = e.status);
}, this.applyRouteDest = (e, i, s) => {
if (!e.dest) return this.path;
const r = this.path;
let t = e.dest;
this.wildcardMatch && /\$wildcard/.test(t) && (t = t.replace(/\$wildcard/g, this.wildcardMatch.value)), this.path = L(t, i, s);
const a = /\/index\.rsc$/i.test(this.path), o = /^\/(?:index)?$/i.test(r), h = /^\/__index\.prefetch\.rsc$/i.test(r);
a && !o && !h && (this.path = r);
}, this.applyRouteStatus = (t) => {
t.status && (this.status = t.status);
}, this.applyRouteDest = (t, i, s) => {
if (!t.dest) return this.path;
const a = this.path;
let e = t.dest;
this.wildcardMatch && /\$wildcard/.test(e) && (e = e.replace(/\$wildcard/g, this.wildcardMatch.value)), this.path = v(e, i, s);
const r = /\/index\.rsc$/i.test(this.path), n = /^\/(?:index)?$/i.test(a), h = /^\/__index\.prefetch\.rsc$/i.test(a);
r && !n && !h && (this.path = a);
const c = /\.rsc$/i.test(this.path), d = /\.prefetch\.rsc$/i.test(this.path), l = this.ctx.assets.has(this.path);
c && !d && !l && (this.path = this.path.replace(/\.rsc/i, ""));
const p = new URL(this.path, this.url);
return k(this.searchParams, p.searchParams), x(this.path) || (this.path = p.pathname), r;
}, this.applyLocaleRedirects = (e) => {
const m = new URL(this.path, this.url);
return k(this.searchParams, m.searchParams), x(this.path) || (this.path = m.pathname), a;
}, this.applyLocaleRedirects = (t) => {
var l;
if (!((l = e.locale) != null && l.redirect) || !/^\^(.)*$/.test(e.src) && e.src !== this.path || this.headers.normal.has("location")) return;
if (!((l = t.locale) != null && l.redirect) || !/^\^(.)*$/.test(t.src) && t.src !== this.path || this.headers.normal.has("location")) return;
const {
locale: { redirect: s, cookie: r }
} = e, t = r && this.cookies[r], a = g(t ?? ""), o = g(
locale: { redirect: s, cookie: a }
} = t, e = a && this.cookies[a], r = g(e ?? ""), n = g(
this.ctx.request.headers.get("accept-language") ?? ""
), d = [...a, ...o].map((p) => s[p]).filter(Boolean)[0];
), d = [...r, ...n].map((m) => s[m]).filter(Boolean)[0];
d && !this.path.startsWith(d) && (this.headers.normal.set("location", d), this.status = 307);
}, this.getLocaleFriendlyRoute = (e, i) => !this.locales || i !== "miss" ? e : $(e.src, this.locales) ? {
...e,
src: e.src.replace(/\/\(\.\*\)\$$/, "(?:/(.*))?$")
} : e, this.checkRoute = async (e, i) => {
const s = this.getLocaleFriendlyRoute(i, e), { routeMatch: r, routeDest: t } = this.checkRouteMatch(s, {
checkStatus: e === "error",
}, this.getLocaleFriendlyRoute = (t, i) => !this.locales || i !== "miss" ? t : $(t.src, this.locales) ? {
...t,
src: t.src.replace(/\/\(\.\*\)\$$/, "(?:/(.*))?$")
} : t, this.checkRoute = async (t, i) => {
const s = this.getLocaleFriendlyRoute(i, t), { routeMatch: a, routeDest: e } = this.checkRouteMatch(s, {
checkStatus: t === "error",
// The build output config correctly maps relevant request paths to be intercepts in the
// `none` phase, while the `rewrite` phase can contain entries that rewrite to an intercept
// that matches requests that are not actually intercepts, causing a 404.
checkIntercept: e === "rewrite"
}) ?? {}, a = { ...s, dest: t };
if (!(r != null && r.match) || a.middlewarePath && this.middlewareInvoked.includes(a.middlewarePath))
checkIntercept: t === "rewrite"
}) ?? {}, r = { ...s, dest: e };
if (!(a != null && a.match) || r.middlewarePath && this.middlewareInvoked.includes(r.middlewarePath))
return "skip";
const { match: o, captureGroupKeys: h } = r;
if (this.applyRouteOverrides(a), this.applyLocaleRedirects(a), !await this.runRouteMiddleware(a.middlewarePath)) return "error";
const { match: n, captureGroupKeys: h } = a;
if (this.applyRouteOverrides(r), this.applyLocaleRedirects(r), !await this.runRouteMiddleware(r.middlewarePath)) return "error";
if (this.body !== void 0 || this.headers.middlewareLocation)
return "done";
this.applyRouteHeaders(a, o, h), this.applyRouteStatus(a);
const d = this.applyRouteDest(a, o, h);
if (a.check && !x(this.path))
this.applyRouteHeaders(r, n, h), this.applyRouteStatus(r);
const d = this.applyRouteDest(r, n, h);
if (r.check && !x(this.path))
if (d === this.path) {
if (e !== "miss")
return this.checkPhase(P(e));
if (t !== "miss")
return this.checkPhase(P(t));
this.status = 404;
} else if (e === "miss") {
} else if (t === "miss") {
if (!this.ctx.assets.has(this.path) && !this.ctx.assets.has(this.path.replace(/\/$/, "")))

@@ -174,4 +179,4 @@ return this.checkPhase("filesystem");

return this.checkPhase("none");
return !a.continue || a.status && a.status >= 300 && a.status <= 399 ? "done" : "next";
}, this.checkPhase = async (e) => {
return !r.continue || r.status && r.status >= 300 && r.status <= 399 ? "done" : "next";
}, this.checkPhase = async (t) => {
if (this.checkPhaseCounter++ >= 50)

@@ -181,7 +186,7 @@ return console.error(`Routing encountered an infinite loop while checking ${this.url.pathname}`), this.status = 500, "error";

let i = !0;
for (const t of this.routes[e]) {
const a = await this.checkRoute(e, t);
if (a === "error")
for (const e of this.routes[t]) {
const r = await this.checkRoute(t, e);
if (r === "error")
return "error";
if (a === "done") {
if (r === "done") {
i = !1;

@@ -191,7 +196,7 @@ break;

}
if (e === "hit" || x(this.path) || this.headers.normal.has("location") || this.body)
if (t === "hit" || x(this.path) || this.headers.normal.has("location") || this.body)
return "done";
if (e === "none")
for (const t of this.locales) {
const a = new RegExp(`/${t}(/.*)`), o = this.path.match(a), h = o == null ? void 0 : o[1];
if (t === "none")
for (const e of this.locales) {
const r = new RegExp(`/${e}(/.*)`), n = this.path.match(r), h = n == null ? void 0 : n[1];
if (h && this.ctx.assets.has(h)) {

@@ -204,21 +209,21 @@ this.path = h;

if (!s && this.path.endsWith("/")) {
const t = this.path.replace(/\/$/, "");
s = this.ctx.assets.has(t), s && (this.path = t);
const e = this.path.replace(/\/$/, "");
s = this.ctx.assets.has(e), s && (this.path = e);
}
if (e === "miss" && !s) {
const t = !this.status || this.status < 400;
this.status = t ? 404 : this.status;
if (t === "miss" && !s) {
const e = !this.status || this.status < 400;
this.status = e ? 404 : this.status;
}
let r = "miss";
return s || e === "miss" || e === "error" ? r = "hit" : i && (r = P(e)), this.checkPhase(r);
}, this.run = async (e = "none") => {
let a = "miss";
return s || t === "miss" || t === "error" ? a = "hit" : i && (a = P(t)), this.checkPhase(a);
}, this.run = async (t = "none") => {
this.checkPhaseCounter = 0;
const i = await this.checkPhase(e);
const i = await this.checkPhase(t);
return this.headers.normal.has("location") && (!this.status || this.status < 300 || this.status >= 400) && (this.status = 307), i;
}, this.url = new URL(u.request.url), this.cookies = H(u.request.headers.get("cookie") || ""), this.path = this.url.pathname || "/", this.headers = { normal: new Headers(), important: new Headers() }, this.searchParams = new URLSearchParams(), k(this.searchParams, this.url.searchParams), this.checkPhaseCounter = 0, this.middlewareInvoked = [], this.wildcardMatch = (w = m.wildcardConfig) == null ? void 0 : w.find((e) => e.domain === this.url.hostname), this.locales = m.locales;
}, this.url = new URL(u.request.url), this.cookies = H(u.request.headers.get("cookie") || ""), this.path = this.url.pathname || "/", this.headers = { normal: new Headers(), important: new Headers() }, this.searchParams = new URLSearchParams(), k(this.searchParams, this.url.searchParams), this.checkPhaseCounter = 0, this.middlewareInvoked = [], this.wildcardMatch = (w = p.wildcardConfig) == null ? void 0 : w.find((t) => t.domain === this.url.hostname), this.locales = p.locales;
}
}
export {
O as RoutesMatcher
N as RoutesMatcher
};
//# sourceMappingURL=routes-matcher.js.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc