forked from shopstr-eng/shopstr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
124 lines (110 loc) · 3.3 KB
/
proxy.ts
File metadata and controls
124 lines (110 loc) · 3.3 KB
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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { nip19 } from "nostr-tools";
const SHOPSTR_DOMAINS = ["shopstr.market", "shopstr.store"];
function getShopstrBaseDomain(hostname: string): string | null {
for (const domain of SHOPSTR_DOMAINS) {
if (hostname.includes(domain)) return domain;
}
return null;
}
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const hostname = request.headers.get("host") || "";
if (pathname === "/.well-known/agent.json") {
return NextResponse.rewrite(
new URL("/api/.well-known/agent.json", request.url)
);
}
const baseDomain = getShopstrBaseDomain(hostname);
if (baseDomain) {
if (hostname === `www.${baseDomain}`) {
const url = new URL(request.url);
url.hostname = baseDomain;
return NextResponse.redirect(url, 301);
}
if (hostname !== baseDomain && hostname.endsWith(`.${baseDomain}`)) {
const subdomain = hostname.replace(`.${baseDomain}`, "");
if (subdomain !== "www" && subdomain !== "api") {
const url = new URL(
`/shop/${subdomain}${pathname === "/" ? "" : pathname}`,
request.url
);
url.hostname = baseDomain;
return NextResponse.rewrite(url);
}
}
}
if (
hostname &&
!baseDomain &&
!hostname.includes("localhost") &&
!hostname.includes("replit") &&
!hostname.includes("127.0.0.1") &&
!hostname.includes(".repl.co") &&
!hostname.includes(".replit.dev") &&
!hostname.includes(".replit.app")
) {
if (pathname.startsWith("/_next/")) {
return NextResponse.next();
}
if (pathname.startsWith("/api/")) {
const allowedApiPrefixes = [
"/api/storefront/",
"/api/db/fetch-products",
"/api/db/fetch-profiles",
"/api/db/fetch-reviews",
"/api/db/fetch-communities",
"/api/nostr/",
"/api/lightning/",
"/api/cashu/",
"/api/stripe/checkout",
];
const isAllowed = allowedApiPrefixes.some((prefix) =>
pathname.startsWith(prefix)
);
if (!isAllowed) {
return NextResponse.json(
{ error: "Not available on this domain" },
{ status: 403 }
);
}
return NextResponse.next();
}
return NextResponse.rewrite(
new URL(
`/shop/_custom-domain?domain=${encodeURIComponent(
hostname
)}&path=${encodeURIComponent(pathname)}`,
request.url
)
);
}
if (
pathname.match(/^\/npub[a-zA-Z0-9]+$/) &&
!pathname.startsWith("/marketplace/")
) {
const url = new URL(`/marketplace${pathname}`, request.url);
return NextResponse.redirect(url);
}
if (
pathname.match(/^\/naddr[a-zA-Z0-9]+$/) &&
!pathname.startsWith("/listing/")
) {
const url = new URL(`/listing${pathname}`, request.url);
return NextResponse.redirect(url);
}
if (pathname.startsWith("/naddr") && !pathname.startsWith("/communities/")) {
try {
const decoded = nip19.decode(pathname.substring(1));
if (decoded.type === "naddr" && decoded.data.kind === 34550) {
return NextResponse.redirect(
new URL(`/communities${pathname}`, request.url)
);
}
} catch {
/* ignore */
}
}
return NextResponse.next();
}