@egdesk/next-api-plugin 1.5.114 → 1.5.115
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/canonical-visitor-redirect.d.ts +33 -0
- package/dist/canonical-visitor-redirect.js +358 -0
- package/dist/canonical-visitor-redirect.test.d.ts +1 -0
- package/dist/canonical-visitor-redirect.test.js +102 -0
- package/dist/generate-helpers.d.ts +2 -0
- package/dist/generate-helpers.js +19 -10
- package/dist/generate-middleware.js +7 -0
- package/dist/generate-proxy.js +7 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -2
- package/package.json +2 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure logic for canonical custom-domain redirects (middleware / proxy codegen).
|
|
3
|
+
* Unit-tested here; the same logic is embedded in generated middleware.ts / proxy.ts.
|
|
4
|
+
*/
|
|
5
|
+
export type CanonicalRedirectHeaders = Record<string, string | undefined>;
|
|
6
|
+
export type CanonicalRedirectInput = {
|
|
7
|
+
method: string;
|
|
8
|
+
pathname: string;
|
|
9
|
+
search: string;
|
|
10
|
+
nextUrlHost: string;
|
|
11
|
+
headers: CanonicalRedirectHeaders;
|
|
12
|
+
getEnv: (key: string) => string | undefined;
|
|
13
|
+
};
|
|
14
|
+
export declare function originFromEnvValue(value?: string | null): string | null;
|
|
15
|
+
export declare function runtimeEnv(getEnv: (key: string) => string | undefined, key: string): string | undefined;
|
|
16
|
+
export declare function resolvePublicRequestOrigin(input: {
|
|
17
|
+
headers: CanonicalRedirectHeaders;
|
|
18
|
+
nextUrlHost: string;
|
|
19
|
+
}): string | null;
|
|
20
|
+
export declare function normalizeHostname(host: string): string;
|
|
21
|
+
export declare function isKnownTunnelHostname(hostname: string): boolean;
|
|
22
|
+
export declare function isWwwAlternateOfCanonical(requestHost: string, canonicalOrigin: string): boolean;
|
|
23
|
+
export declare function resolveCanonicalVisitorOrigin(input: CanonicalRedirectInput): string | null;
|
|
24
|
+
export declare function isCanonicalRedirectEnabled(input: CanonicalRedirectInput): boolean;
|
|
25
|
+
export declare function resolveEgdeskBasePathForRedirect(input: CanonicalRedirectInput): string;
|
|
26
|
+
export declare function stripBasePathFromPathname(pathname: string, basePath: string): string;
|
|
27
|
+
export declare function shouldSkipCanonicalRedirectPath(pathname: string, basePath?: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Returns absolute redirect URL, or null if no redirect.
|
|
30
|
+
*/
|
|
31
|
+
export declare function computeCanonicalVisitorRedirectTarget(input: CanonicalRedirectInput): string | null;
|
|
32
|
+
/** Source embedded into generated middleware.ts / proxy.ts (keep in sync with tests). */
|
|
33
|
+
export declare function canonicalVisitorRedirectMiddlewareSource(): string;
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Pure logic for canonical custom-domain redirects (middleware / proxy codegen).
|
|
4
|
+
* Unit-tested here; the same logic is embedded in generated middleware.ts / proxy.ts.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.originFromEnvValue = originFromEnvValue;
|
|
8
|
+
exports.runtimeEnv = runtimeEnv;
|
|
9
|
+
exports.resolvePublicRequestOrigin = resolvePublicRequestOrigin;
|
|
10
|
+
exports.normalizeHostname = normalizeHostname;
|
|
11
|
+
exports.isKnownTunnelHostname = isKnownTunnelHostname;
|
|
12
|
+
exports.isWwwAlternateOfCanonical = isWwwAlternateOfCanonical;
|
|
13
|
+
exports.resolveCanonicalVisitorOrigin = resolveCanonicalVisitorOrigin;
|
|
14
|
+
exports.isCanonicalRedirectEnabled = isCanonicalRedirectEnabled;
|
|
15
|
+
exports.resolveEgdeskBasePathForRedirect = resolveEgdeskBasePathForRedirect;
|
|
16
|
+
exports.stripBasePathFromPathname = stripBasePathFromPathname;
|
|
17
|
+
exports.shouldSkipCanonicalRedirectPath = shouldSkipCanonicalRedirectPath;
|
|
18
|
+
exports.computeCanonicalVisitorRedirectTarget = computeCanonicalVisitorRedirectTarget;
|
|
19
|
+
exports.canonicalVisitorRedirectMiddlewareSource = canonicalVisitorRedirectMiddlewareSource;
|
|
20
|
+
const TUNNEL_HOST_SUFFIXES = ['.egdesk.cloud'];
|
|
21
|
+
const TUNNEL_HOSTS = new Set(['tunneling-service.onrender.com', 'egdesk.cloud', 'www.egdesk.cloud']);
|
|
22
|
+
function originFromEnvValue(value) {
|
|
23
|
+
const raw = (value || '').trim();
|
|
24
|
+
if (!raw)
|
|
25
|
+
return null;
|
|
26
|
+
try {
|
|
27
|
+
return new URL(raw).origin;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
try {
|
|
31
|
+
return new URL(`https://${raw}`).origin;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function runtimeEnv(getEnv, key) {
|
|
39
|
+
try {
|
|
40
|
+
return getEnv(key);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function headerFirst(value) {
|
|
47
|
+
if (!value)
|
|
48
|
+
return undefined;
|
|
49
|
+
const first = value.split(',')[0]?.trim();
|
|
50
|
+
return first || undefined;
|
|
51
|
+
}
|
|
52
|
+
function resolvePublicRequestOrigin(input) {
|
|
53
|
+
const host = headerFirst(input.headers['x-forwarded-host']) ||
|
|
54
|
+
headerFirst(input.headers['host']) ||
|
|
55
|
+
input.nextUrlHost;
|
|
56
|
+
if (!host)
|
|
57
|
+
return null;
|
|
58
|
+
const proto = headerFirst(input.headers['x-forwarded-proto']) ||
|
|
59
|
+
(host.includes('localhost') || host.startsWith('127.0.0.1') ? 'http' : 'https');
|
|
60
|
+
try {
|
|
61
|
+
return new URL(`${proto}://${host}`).origin;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function normalizeHostname(host) {
|
|
68
|
+
return host.toLowerCase().replace(/:\d+$/, '');
|
|
69
|
+
}
|
|
70
|
+
function isKnownTunnelHostname(hostname) {
|
|
71
|
+
const h = normalizeHostname(hostname);
|
|
72
|
+
if (TUNNEL_HOSTS.has(h))
|
|
73
|
+
return true;
|
|
74
|
+
return TUNNEL_HOST_SUFFIXES.some((suffix) => h.endsWith(suffix));
|
|
75
|
+
}
|
|
76
|
+
function isWwwAlternateOfCanonical(requestHost, canonicalOrigin) {
|
|
77
|
+
try {
|
|
78
|
+
const canonicalHost = new URL(canonicalOrigin).hostname.toLowerCase();
|
|
79
|
+
if (canonicalHost.startsWith('www.'))
|
|
80
|
+
return false;
|
|
81
|
+
const bare = canonicalHost.replace(/^www\./, '');
|
|
82
|
+
const h = normalizeHostname(requestHost);
|
|
83
|
+
return h === `www.${bare}`;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function resolveCanonicalVisitorOrigin(input) {
|
|
90
|
+
const fromHeader = originFromEnvValue(input.headers['x-egdesk-canonical-visitor-origin']);
|
|
91
|
+
if (fromHeader)
|
|
92
|
+
return fromHeader;
|
|
93
|
+
const fromRuntime = runtimeEnv(input.getEnv, 'EGDESK_CANONICAL_VISITOR_ORIGIN') ||
|
|
94
|
+
runtimeEnv(input.getEnv, 'NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN');
|
|
95
|
+
return originFromEnvValue(fromRuntime);
|
|
96
|
+
}
|
|
97
|
+
function isCanonicalRedirectEnabled(input) {
|
|
98
|
+
const fromHeader = (input.headers['x-egdesk-canonical-redirect-enabled'] || '').trim();
|
|
99
|
+
if (fromHeader === '1' || fromHeader.toLowerCase() === 'true')
|
|
100
|
+
return true;
|
|
101
|
+
return runtimeEnv(input.getEnv, 'EGDESK_CANONICAL_REDIRECT_ENABLED') === '1';
|
|
102
|
+
}
|
|
103
|
+
function isHtmlNavigation(input) {
|
|
104
|
+
const accept = input.headers['accept'] || '';
|
|
105
|
+
const secFetchDest = input.headers['sec-fetch-dest'];
|
|
106
|
+
if (accept.includes('text/html') || accept.includes('application/xhtml+xml'))
|
|
107
|
+
return true;
|
|
108
|
+
if (!secFetchDest)
|
|
109
|
+
return true;
|
|
110
|
+
return secFetchDest === 'document' || secFetchDest === 'iframe';
|
|
111
|
+
}
|
|
112
|
+
function resolveEgdeskBasePathForRedirect(input) {
|
|
113
|
+
const fromHeader = (input.headers['x-egdesk-base-path'] || '').trim();
|
|
114
|
+
if (fromHeader)
|
|
115
|
+
return fromHeader.replace(/\/$/, '');
|
|
116
|
+
return (runtimeEnv(input.getEnv, 'EGDESK_BASE_PATH') ||
|
|
117
|
+
runtimeEnv(input.getEnv, 'NEXT_PUBLIC_EGDESK_BASE_PATH') ||
|
|
118
|
+
'').replace(/\/$/, '');
|
|
119
|
+
}
|
|
120
|
+
function stripBasePathFromPathname(pathname, basePath) {
|
|
121
|
+
if (!basePath)
|
|
122
|
+
return pathname;
|
|
123
|
+
if (pathname === basePath || pathname.startsWith(`${basePath}/`)) {
|
|
124
|
+
return pathname.slice(basePath.length) || '/';
|
|
125
|
+
}
|
|
126
|
+
return pathname;
|
|
127
|
+
}
|
|
128
|
+
function shouldSkipCanonicalRedirectPath(pathname, basePath = '') {
|
|
129
|
+
const path = stripBasePathFromPathname(pathname, basePath);
|
|
130
|
+
if (path.startsWith('/api/'))
|
|
131
|
+
return true;
|
|
132
|
+
if (path.startsWith('/_next/'))
|
|
133
|
+
return true;
|
|
134
|
+
if (path.includes('__'))
|
|
135
|
+
return true;
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Returns absolute redirect URL, or null if no redirect.
|
|
140
|
+
*/
|
|
141
|
+
function computeCanonicalVisitorRedirectTarget(input) {
|
|
142
|
+
if (!isCanonicalRedirectEnabled(input))
|
|
143
|
+
return null;
|
|
144
|
+
const canonical = resolveCanonicalVisitorOrigin(input);
|
|
145
|
+
if (!canonical)
|
|
146
|
+
return null;
|
|
147
|
+
if (input.method !== 'GET')
|
|
148
|
+
return null;
|
|
149
|
+
const basePath = resolveEgdeskBasePathForRedirect(input);
|
|
150
|
+
if (shouldSkipCanonicalRedirectPath(input.pathname, basePath))
|
|
151
|
+
return null;
|
|
152
|
+
if (!isHtmlNavigation(input))
|
|
153
|
+
return null;
|
|
154
|
+
const publicOrigin = resolvePublicRequestOrigin({
|
|
155
|
+
headers: input.headers,
|
|
156
|
+
nextUrlHost: input.nextUrlHost,
|
|
157
|
+
});
|
|
158
|
+
if (publicOrigin && publicOrigin === canonical)
|
|
159
|
+
return null;
|
|
160
|
+
const requestHost = normalizeHostname(headerFirst(input.headers['x-forwarded-host']) ||
|
|
161
|
+
headerFirst(input.headers['host']) ||
|
|
162
|
+
input.nextUrlHost);
|
|
163
|
+
const canonicalHost = normalizeHostname(new URL(canonical).host);
|
|
164
|
+
if (requestHost === canonicalHost)
|
|
165
|
+
return null;
|
|
166
|
+
const redirectable = isKnownTunnelHostname(requestHost) || isWwwAlternateOfCanonical(requestHost, canonical);
|
|
167
|
+
if (!redirectable)
|
|
168
|
+
return null;
|
|
169
|
+
let path = stripBasePathFromPathname(input.pathname, basePath);
|
|
170
|
+
const target = new URL((path.startsWith('/') ? path : `/${path}`) + (input.search || ''), canonical).toString();
|
|
171
|
+
if (publicOrigin) {
|
|
172
|
+
try {
|
|
173
|
+
const current = new URL((path.startsWith('/') ? path : `/${path}`) + (input.search || ''), publicOrigin).toString();
|
|
174
|
+
if (current === target)
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
// ignore
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return target;
|
|
182
|
+
}
|
|
183
|
+
/** Source embedded into generated middleware.ts / proxy.ts (keep in sync with tests). */
|
|
184
|
+
function canonicalVisitorRedirectMiddlewareSource() {
|
|
185
|
+
return `
|
|
186
|
+
function originFromEnvValue(value?: string | null): string | null {
|
|
187
|
+
const raw = (value || '').trim();
|
|
188
|
+
if (!raw) return null;
|
|
189
|
+
try {
|
|
190
|
+
return new URL(raw).origin;
|
|
191
|
+
} catch {
|
|
192
|
+
try {
|
|
193
|
+
return new URL(\`https://\${raw}\`).origin;
|
|
194
|
+
} catch {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function runtimeEnv(key: string): string | undefined {
|
|
201
|
+
try {
|
|
202
|
+
if (typeof process === 'undefined' || !process.env) return undefined;
|
|
203
|
+
return process.env[key];
|
|
204
|
+
} catch {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function headerFirst(value?: string | null): string | undefined {
|
|
210
|
+
if (!value) return undefined;
|
|
211
|
+
const first = value.split(',')[0]?.trim();
|
|
212
|
+
return first || undefined;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function resolvePublicRequestOrigin(request: NextRequest): string | null {
|
|
216
|
+
const host =
|
|
217
|
+
headerFirst(request.headers.get('x-forwarded-host')) ||
|
|
218
|
+
headerFirst(request.headers.get('host')) ||
|
|
219
|
+
request.nextUrl.host;
|
|
220
|
+
if (!host) return null;
|
|
221
|
+
const proto =
|
|
222
|
+
headerFirst(request.headers.get('x-forwarded-proto')) ||
|
|
223
|
+
(host.includes('localhost') || host.startsWith('127.0.0.1') ? 'http' : 'https');
|
|
224
|
+
try {
|
|
225
|
+
return new URL(\`\${proto}://\${host}\`).origin;
|
|
226
|
+
} catch {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function normalizeHostname(host: string): string {
|
|
232
|
+
return host.toLowerCase().replace(/:\\d+$/, '');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function isKnownTunnelHostname(hostname: string): boolean {
|
|
236
|
+
const h = normalizeHostname(hostname);
|
|
237
|
+
if (
|
|
238
|
+
h === 'tunneling-service.onrender.com' ||
|
|
239
|
+
h === 'egdesk.cloud' ||
|
|
240
|
+
h === 'www.egdesk.cloud'
|
|
241
|
+
) {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
return h.endsWith('.egdesk.cloud');
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function isWwwAlternateOfCanonical(requestHost: string, canonicalOrigin: string): boolean {
|
|
248
|
+
try {
|
|
249
|
+
const canonicalHost = new URL(canonicalOrigin).hostname.toLowerCase();
|
|
250
|
+
if (canonicalHost.startsWith('www.')) return false;
|
|
251
|
+
const bare = canonicalHost.replace(/^www\\./, '');
|
|
252
|
+
const h = normalizeHostname(requestHost);
|
|
253
|
+
return h === \`www.\${bare}\`;
|
|
254
|
+
} catch {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function resolveCanonicalVisitorOrigin(request: NextRequest): string | null {
|
|
260
|
+
const fromHeader = originFromEnvValue(request.headers.get('x-egdesk-canonical-visitor-origin'));
|
|
261
|
+
if (fromHeader) return fromHeader;
|
|
262
|
+
const fromRuntime =
|
|
263
|
+
runtimeEnv('EGDESK_CANONICAL_VISITOR_ORIGIN') ||
|
|
264
|
+
runtimeEnv('NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN');
|
|
265
|
+
return originFromEnvValue(fromRuntime);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function isCanonicalRedirectEnabled(request: NextRequest): boolean {
|
|
269
|
+
const fromHeader = (request.headers.get('x-egdesk-canonical-redirect-enabled') || '').trim();
|
|
270
|
+
if (fromHeader === '1' || fromHeader.toLowerCase() === 'true') return true;
|
|
271
|
+
return runtimeEnv('EGDESK_CANONICAL_REDIRECT_ENABLED') === '1';
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function isHtmlNavigation(request: NextRequest): boolean {
|
|
275
|
+
const accept = request.headers.get('accept') || '';
|
|
276
|
+
const secFetchDest = request.headers.get('sec-fetch-dest');
|
|
277
|
+
if (accept.includes('text/html') || accept.includes('application/xhtml+xml')) return true;
|
|
278
|
+
if (!secFetchDest) return true;
|
|
279
|
+
return secFetchDest === 'document' || secFetchDest === 'iframe';
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function resolveEgdeskBasePathForRedirect(request: NextRequest): string {
|
|
283
|
+
const fromHeader = (request.headers.get('x-egdesk-base-path') || '').trim();
|
|
284
|
+
if (fromHeader) return fromHeader.replace(/\\/$/, '');
|
|
285
|
+
return (
|
|
286
|
+
runtimeEnv('EGDESK_BASE_PATH') || runtimeEnv('NEXT_PUBLIC_EGDESK_BASE_PATH') || ''
|
|
287
|
+
).replace(/\\/$/, '');
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function stripBasePathFromPathname(pathname: string, basePath: string): string {
|
|
291
|
+
if (!basePath) return pathname;
|
|
292
|
+
if (pathname === basePath || pathname.startsWith(basePath + '/')) {
|
|
293
|
+
return pathname.slice(basePath.length) || '/';
|
|
294
|
+
}
|
|
295
|
+
return pathname;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function shouldSkipCanonicalRedirectPath(pathname: string, basePath = ''): boolean {
|
|
299
|
+
const path = stripBasePathFromPathname(pathname, basePath);
|
|
300
|
+
if (path.startsWith('/api/')) return true;
|
|
301
|
+
if (path.startsWith('/_next/')) return true;
|
|
302
|
+
if (path.includes('__')) return true;
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function computeCanonicalVisitorRedirectTarget(request: NextRequest): string | null {
|
|
307
|
+
if (!isCanonicalRedirectEnabled(request)) return null;
|
|
308
|
+
const canonical = resolveCanonicalVisitorOrigin(request);
|
|
309
|
+
if (!canonical) return null;
|
|
310
|
+
if (request.method !== 'GET') return null;
|
|
311
|
+
const { pathname, search } = request.nextUrl;
|
|
312
|
+
const basePath = resolveEgdeskBasePathForRedirect(request);
|
|
313
|
+
if (shouldSkipCanonicalRedirectPath(pathname, basePath)) return null;
|
|
314
|
+
if (!isHtmlNavigation(request)) return null;
|
|
315
|
+
|
|
316
|
+
const publicOrigin = resolvePublicRequestOrigin(request);
|
|
317
|
+
if (publicOrigin && publicOrigin === canonical) return null;
|
|
318
|
+
|
|
319
|
+
const requestHost = normalizeHostname(
|
|
320
|
+
headerFirst(request.headers.get('x-forwarded-host')) ||
|
|
321
|
+
headerFirst(request.headers.get('host')) ||
|
|
322
|
+
request.nextUrl.host,
|
|
323
|
+
);
|
|
324
|
+
const canonicalHost = normalizeHostname(new URL(canonical).host);
|
|
325
|
+
if (requestHost === canonicalHost) return null;
|
|
326
|
+
|
|
327
|
+
const redirectable =
|
|
328
|
+
isKnownTunnelHostname(requestHost) || isWwwAlternateOfCanonical(requestHost, canonical);
|
|
329
|
+
if (!redirectable) return null;
|
|
330
|
+
|
|
331
|
+
let path = stripBasePathFromPathname(pathname, basePath);
|
|
332
|
+
const target = new URL(
|
|
333
|
+
(path.startsWith('/') ? path : \`/\${path}\`) + search,
|
|
334
|
+
canonical,
|
|
335
|
+
).toString();
|
|
336
|
+
|
|
337
|
+
if (publicOrigin) {
|
|
338
|
+
try {
|
|
339
|
+
const current = new URL(
|
|
340
|
+
(path.startsWith('/') ? path : \`/\${path}\`) + search,
|
|
341
|
+
publicOrigin,
|
|
342
|
+
).toString();
|
|
343
|
+
if (current === target) return null;
|
|
344
|
+
} catch {
|
|
345
|
+
// ignore
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return target;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function maybeRedirectToCanonicalVisitorSite(request: NextRequest): NextResponse | null {
|
|
353
|
+
const target = computeCanonicalVisitorRedirectTarget(request);
|
|
354
|
+
if (!target) return null;
|
|
355
|
+
return NextResponse.redirect(target, 308);
|
|
356
|
+
}
|
|
357
|
+
`.trim();
|
|
358
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
7
|
+
const node_test_1 = __importDefault(require("node:test"));
|
|
8
|
+
const canonical_visitor_redirect_1 = require("./canonical-visitor-redirect");
|
|
9
|
+
const CANON = 'https://shop.example.com';
|
|
10
|
+
const TUNNEL = 'tunneling-service.onrender.com';
|
|
11
|
+
function baseInput(overrides = {}) {
|
|
12
|
+
const env = {
|
|
13
|
+
EGDESK_CANONICAL_REDIRECT_ENABLED: '1',
|
|
14
|
+
EGDESK_CANONICAL_VISITOR_ORIGIN: CANON,
|
|
15
|
+
EGDESK_BASE_PATH: '/t/demo/p/MyApp',
|
|
16
|
+
};
|
|
17
|
+
const getEnv = overrides.getEnv ?? ((key) => env[key]);
|
|
18
|
+
const { getEnv: _drop, ...rest } = overrides;
|
|
19
|
+
return {
|
|
20
|
+
method: 'GET',
|
|
21
|
+
pathname: '/t/demo/p/MyApp/dashboard',
|
|
22
|
+
search: '',
|
|
23
|
+
nextUrlHost: 'localhost:3000',
|
|
24
|
+
headers: {
|
|
25
|
+
accept: 'text/html',
|
|
26
|
+
'x-forwarded-host': TUNNEL,
|
|
27
|
+
'x-forwarded-proto': 'https',
|
|
28
|
+
},
|
|
29
|
+
getEnv,
|
|
30
|
+
...rest,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
(0, node_test_1.default)('GET HTML on tunnel host redirects to canonical with basePath stripped', () => {
|
|
34
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput());
|
|
35
|
+
strict_1.default.equal(target, 'https://shop.example.com/dashboard');
|
|
36
|
+
});
|
|
37
|
+
(0, node_test_1.default)('canonical host via x-forwarded-host never redirects (loop guard)', () => {
|
|
38
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
39
|
+
headers: {
|
|
40
|
+
accept: 'text/html',
|
|
41
|
+
'x-forwarded-host': 'shop.example.com',
|
|
42
|
+
'x-forwarded-proto': 'https',
|
|
43
|
+
},
|
|
44
|
+
}));
|
|
45
|
+
strict_1.default.equal(target, null);
|
|
46
|
+
});
|
|
47
|
+
(0, node_test_1.default)('runtime canonical from gateway header when env unset at build time', () => {
|
|
48
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
49
|
+
getEnv: () => undefined,
|
|
50
|
+
headers: {
|
|
51
|
+
accept: 'text/html',
|
|
52
|
+
'x-forwarded-host': TUNNEL,
|
|
53
|
+
'x-egdesk-canonical-visitor-origin': CANON,
|
|
54
|
+
'x-egdesk-canonical-redirect-enabled': '1',
|
|
55
|
+
'x-egdesk-base-path': '/t/demo/p/MyApp',
|
|
56
|
+
},
|
|
57
|
+
}));
|
|
58
|
+
strict_1.default.equal(target, 'https://shop.example.com/dashboard');
|
|
59
|
+
});
|
|
60
|
+
(0, node_test_1.default)('POST does not redirect', () => {
|
|
61
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({ method: 'POST' }));
|
|
62
|
+
strict_1.default.equal(target, null);
|
|
63
|
+
});
|
|
64
|
+
(0, node_test_1.default)('/api does not redirect', () => {
|
|
65
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({ pathname: '/t/demo/p/MyApp/api/agent/gas-bridge' }));
|
|
66
|
+
strict_1.default.equal(target, null);
|
|
67
|
+
});
|
|
68
|
+
(0, node_test_1.default)('/_next does not redirect', () => {
|
|
69
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({ pathname: '/t/demo/p/MyApp/_next/static/chunk.js' }));
|
|
70
|
+
strict_1.default.equal(target, null);
|
|
71
|
+
});
|
|
72
|
+
(0, node_test_1.default)('__proxy paths do not redirect', () => {
|
|
73
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({ pathname: '/t/demo/p/MyApp/__user_data_proxy' }));
|
|
74
|
+
strict_1.default.equal(target, null);
|
|
75
|
+
});
|
|
76
|
+
(0, node_test_1.default)('redirect disabled (tunnel-only) does not redirect', () => {
|
|
77
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
78
|
+
getEnv: (key) => (key === 'EGDESK_CANONICAL_VISITOR_ORIGIN' ? CANON : undefined),
|
|
79
|
+
headers: { accept: 'text/html', 'x-forwarded-host': TUNNEL },
|
|
80
|
+
}));
|
|
81
|
+
strict_1.default.equal(target, null);
|
|
82
|
+
});
|
|
83
|
+
(0, node_test_1.default)('www alternate redirects to apex canonical', () => {
|
|
84
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
85
|
+
headers: {
|
|
86
|
+
accept: 'text/html',
|
|
87
|
+
'x-forwarded-host': 'www.shop.example.com',
|
|
88
|
+
'x-forwarded-proto': 'https',
|
|
89
|
+
},
|
|
90
|
+
}));
|
|
91
|
+
strict_1.default.equal(target, 'https://shop.example.com/dashboard');
|
|
92
|
+
});
|
|
93
|
+
(0, node_test_1.default)('untrusted random host does not redirect (open-redirect guard)', () => {
|
|
94
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
95
|
+
headers: {
|
|
96
|
+
accept: 'text/html',
|
|
97
|
+
'x-forwarded-host': 'evil.example.net',
|
|
98
|
+
'x-forwarded-proto': 'https',
|
|
99
|
+
},
|
|
100
|
+
}));
|
|
101
|
+
strict_1.default.equal(target, null);
|
|
102
|
+
});
|
|
@@ -32,4 +32,6 @@ export interface GenerateHelpersOptions {
|
|
|
32
32
|
/** Google/Gemini API key names saved in EGDesk AI Keys Manager */
|
|
33
33
|
apiKeyNames?: string[];
|
|
34
34
|
}
|
|
35
|
+
/** Bump when generated helper/middleware behavior changes; EGDesk regenerates stale projects. */
|
|
36
|
+
export declare const EGDESK_HELPERS_GENERATION_VERSION = "2";
|
|
35
37
|
export declare function generateHelpers(projectPath: string, options?: GenerateHelpersOptions): void;
|
package/dist/generate-helpers.js
CHANGED
|
@@ -38,6 +38,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
38
38
|
};
|
|
39
39
|
})();
|
|
40
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.EGDESK_HELPERS_GENERATION_VERSION = void 0;
|
|
41
42
|
exports.formatEgdeskSchemaFileContent = formatEgdeskSchemaFileContent;
|
|
42
43
|
exports.generateSchemaFile = generateSchemaFile;
|
|
43
44
|
exports.ensureSchemaFile = ensureSchemaFile;
|
|
@@ -132,11 +133,15 @@ function ensureSchemaFile(projectPath, tables) {
|
|
|
132
133
|
tableCount: userTables.length,
|
|
133
134
|
};
|
|
134
135
|
}
|
|
136
|
+
/** Bump when generated helper/middleware behavior changes; EGDesk regenerates stale projects. */
|
|
137
|
+
exports.EGDESK_HELPERS_GENERATION_VERSION = '2';
|
|
135
138
|
function generateHelpers(projectPath, options = {}) {
|
|
136
139
|
const helperPath = path.join(projectPath, 'egdesk-helpers.ts');
|
|
137
140
|
const helperContent = `/**
|
|
138
141
|
* EGDesk Helper Functions for Next.js
|
|
139
142
|
*
|
|
143
|
+
* Generation version: ${exports.EGDESK_HELPERS_GENERATION_VERSION} (EGDesk regenerates when stale)
|
|
144
|
+
*
|
|
140
145
|
* Type-safe helpers for EGDesk user data, Google Workspace (Sheets / Drive / Docs / Slides MCP + sync, Gmail, Apps Script),
|
|
141
146
|
* FinanceHub (banks, cards, Hometax, promissory notes),
|
|
142
147
|
* internal knowledge / business identity / company research (MCP), company research docs (cited tool-result blocks), Korean law (MCP), Bizinfo grants (MCP), NPS workplace headcount (MCP), and browser recorder replay / live browse.
|
|
@@ -159,6 +164,9 @@ function generateHelpers(projectPath, options = {}) {
|
|
|
159
164
|
|
|
160
165
|
import { EGDESK_CONFIG } from './egdesk.config';
|
|
161
166
|
|
|
167
|
+
/** @internal Used by EGDesk to detect stale generated helpers. */
|
|
168
|
+
export const EGDESK_HELPERS_GENERATION_VERSION = '${exports.EGDESK_HELPERS_GENERATION_VERSION}';
|
|
169
|
+
|
|
162
170
|
/**
|
|
163
171
|
* Resolve EGDesk tunnel basePath for client-side fetch.
|
|
164
172
|
* Next.js basePath does NOT apply to raw fetch() — prepend manually in production.
|
|
@@ -263,14 +271,18 @@ function originFromValue(value?: string | null): string | null {
|
|
|
263
271
|
}
|
|
264
272
|
}
|
|
265
273
|
|
|
266
|
-
/** Browser origin, then visitorOrigin, then
|
|
274
|
+
/** Browser origin, then visitorOrigin, runtime canonical env, then NEXT_PUBLIC_* (build-time fallback). */
|
|
267
275
|
function resolveVisitorSiteOrigin(options: WorkspaceVisitorCallOptions = {}): string | null {
|
|
268
276
|
if (typeof window !== 'undefined') return window.location.origin;
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
''
|
|
273
|
-
|
|
277
|
+
const explicit = originFromValue(options.visitorOrigin);
|
|
278
|
+
if (explicit) return explicit;
|
|
279
|
+
const fromRuntime =
|
|
280
|
+
typeof process !== 'undefined'
|
|
281
|
+
? process.env?.EGDESK_CANONICAL_VISITOR_ORIGIN ||
|
|
282
|
+
process.env?.NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN ||
|
|
283
|
+
process.env?.NEXT_PUBLIC_SITE_URL
|
|
284
|
+
: '';
|
|
285
|
+
return originFromValue(fromRuntime);
|
|
274
286
|
}
|
|
275
287
|
|
|
276
288
|
function buildWorkspaceVisitorHeaders(options: WorkspaceVisitorCallOptions = {}): Record<string, string> {
|
|
@@ -288,11 +300,8 @@ function buildWorkspaceVisitorHeaders(options: WorkspaceVisitorCallOptions = {})
|
|
|
288
300
|
if (origin) {
|
|
289
301
|
headers['Origin'] = origin;
|
|
290
302
|
headers['X-Visitor-Origin'] = origin;
|
|
291
|
-
} else if (typeof window === 'undefined') {
|
|
292
|
-
throw new Error(
|
|
293
|
-
'asVisitor is set but no site origin is available. Pass visitorOrigin matching startVisitorGoogleLogin, or set NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN.',
|
|
294
|
-
);
|
|
295
303
|
}
|
|
304
|
+
// Server-side without Origin: EGDesk MCP binds the session from stored audience (token-only callers).
|
|
296
305
|
return headers;
|
|
297
306
|
}
|
|
298
307
|
|
|
@@ -43,6 +43,7 @@ exports.generateMiddleware = generateMiddleware;
|
|
|
43
43
|
const fs = __importStar(require("fs"));
|
|
44
44
|
const path = __importStar(require("path"));
|
|
45
45
|
const os = __importStar(require("os"));
|
|
46
|
+
const canonical_visitor_redirect_1 = require("./canonical-visitor-redirect");
|
|
46
47
|
/**
|
|
47
48
|
* Detect if project uses src/ directory structure
|
|
48
49
|
*/
|
|
@@ -121,6 +122,7 @@ function workspaceUpstreamHeaders(request: NextRequest): Record<string, string>
|
|
|
121
122
|
const origin =
|
|
122
123
|
request.headers.get('x-visitor-origin') ||
|
|
123
124
|
request.headers.get('origin') ||
|
|
125
|
+
process.env.EGDESK_CANONICAL_VISITOR_ORIGIN ||
|
|
124
126
|
process.env.NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN ||
|
|
125
127
|
process.env.NEXT_PUBLIC_SITE_URL ||
|
|
126
128
|
request.nextUrl.origin;
|
|
@@ -172,7 +174,12 @@ function visitorAuthToolFromProxyBody(body: string): string {
|
|
|
172
174
|
}
|
|
173
175
|
}
|
|
174
176
|
|
|
177
|
+
${(0, canonical_visitor_redirect_1.canonicalVisitorRedirectMiddlewareSource)()}
|
|
178
|
+
|
|
175
179
|
export async function middleware(request: NextRequest) {
|
|
180
|
+
const canonicalRedirect = maybeRedirectToCanonicalVisitorSite(request);
|
|
181
|
+
if (canonicalRedirect) return canonicalRedirect;
|
|
182
|
+
|
|
176
183
|
const { pathname } = request.nextUrl;
|
|
177
184
|
|
|
178
185
|
// Intercept __user_data_proxy requests
|
package/dist/generate-proxy.js
CHANGED
|
@@ -45,6 +45,7 @@ exports.generateProxy = generateProxy;
|
|
|
45
45
|
const fs = __importStar(require("fs"));
|
|
46
46
|
const path = __importStar(require("path"));
|
|
47
47
|
const os = __importStar(require("os"));
|
|
48
|
+
const canonical_visitor_redirect_1 = require("./canonical-visitor-redirect");
|
|
48
49
|
/**
|
|
49
50
|
* Detect if project uses src/ directory structure
|
|
50
51
|
*/
|
|
@@ -160,6 +161,8 @@ function visitorForwardHeaders(request: NextRequest): Record<string, string> {
|
|
|
160
161
|
}
|
|
161
162
|
|
|
162
163
|
/** Forward opt-in visitor session headers to Workspace MCP tool calls. */
|
|
164
|
+
${(0, canonical_visitor_redirect_1.canonicalVisitorRedirectMiddlewareSource)()}
|
|
165
|
+
|
|
163
166
|
function workspaceForwardHeaders(request: NextRequest): Record<string, string> {
|
|
164
167
|
const headers = buildEgdeskHeaders();
|
|
165
168
|
const authorization = request.headers.get('authorization');
|
|
@@ -170,6 +173,7 @@ function workspaceForwardHeaders(request: NextRequest): Record<string, string> {
|
|
|
170
173
|
const origin =
|
|
171
174
|
request.headers.get('x-visitor-origin') ||
|
|
172
175
|
request.headers.get('origin') ||
|
|
176
|
+
process.env.EGDESK_CANONICAL_VISITOR_ORIGIN ||
|
|
173
177
|
process.env.NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN ||
|
|
174
178
|
process.env.NEXT_PUBLIC_SITE_URL ||
|
|
175
179
|
request.nextUrl.origin;
|
|
@@ -180,6 +184,9 @@ function workspaceForwardHeaders(request: NextRequest): Record<string, string> {
|
|
|
180
184
|
}
|
|
181
185
|
|
|
182
186
|
export async function proxy(request: NextRequest) {
|
|
187
|
+
const canonicalRedirect = maybeRedirectToCanonicalVisitorSite(request);
|
|
188
|
+
if (canonicalRedirect) return canonicalRedirect;
|
|
189
|
+
|
|
183
190
|
const { pathname } = request.nextUrl;
|
|
184
191
|
|
|
185
192
|
// Intercept __user_data_proxy requests
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* The minimum EGDesk app version this plugin version is compatible with.
|
|
9
9
|
* The app checks this at setup time and warns on mismatch.
|
|
10
10
|
*/
|
|
11
|
-
export declare const COMPATIBLE_APP_VERSION = "1.4.
|
|
11
|
+
export declare const COMPATIBLE_APP_VERSION = "1.4.122";
|
|
12
12
|
export interface SetupOptions {
|
|
13
13
|
egdeskUrl?: string;
|
|
14
14
|
/** Production EGDesk server URL — written to .env.production.local. Falls back to egdeskUrl. */
|
|
@@ -41,7 +41,7 @@ export { discoverTables, generateConfigFile, loadTablesFromSchemaFile, updateEnv
|
|
|
41
41
|
export { generateMiddleware } from './generate-middleware';
|
|
42
42
|
export { generateProxy } from './generate-proxy';
|
|
43
43
|
export { generateChunkedUploadRoutes, generateVoiceTranscriptChunkedUploadRoutes, } from './generate-chunked-upload-routes';
|
|
44
|
-
export { generateHelpers, generateSchemaFile, ensureSchemaFile, formatEgdeskSchemaFileContent } from './generate-helpers';
|
|
44
|
+
export { generateHelpers, generateSchemaFile, ensureSchemaFile, formatEgdeskSchemaFileContent, EGDESK_HELPERS_GENERATION_VERSION, } from './generate-helpers';
|
|
45
45
|
export type { SchemaTableDefinition, EnsureSchemaFileResult, GenerateHelpersOptions } from './generate-helpers';
|
|
46
46
|
export { generateApiWrapper } from './generate-api-wrapper';
|
|
47
47
|
export { generateVisitorAuth, ensureVisitorAuthConfig } from './generate-visitor-auth';
|
package/dist/index.js
CHANGED
|
@@ -39,14 +39,14 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
39
39
|
};
|
|
40
40
|
})();
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.ensureVisitorAuthConfig = exports.generateVisitorAuth = exports.generateApiWrapper = exports.formatEgdeskSchemaFileContent = exports.ensureSchemaFile = exports.generateSchemaFile = exports.generateHelpers = exports.generateVoiceTranscriptChunkedUploadRoutes = exports.generateChunkedUploadRoutes = exports.generateProxy = exports.generateMiddleware = exports.updateEnvFiles = exports.loadTablesFromSchemaFile = exports.generateConfigFile = exports.discoverTables = exports.COMPATIBLE_APP_VERSION = void 0;
|
|
42
|
+
exports.ensureVisitorAuthConfig = exports.generateVisitorAuth = exports.generateApiWrapper = exports.EGDESK_HELPERS_GENERATION_VERSION = exports.formatEgdeskSchemaFileContent = exports.ensureSchemaFile = exports.generateSchemaFile = exports.generateHelpers = exports.generateVoiceTranscriptChunkedUploadRoutes = exports.generateChunkedUploadRoutes = exports.generateProxy = exports.generateMiddleware = exports.updateEnvFiles = exports.loadTablesFromSchemaFile = exports.generateConfigFile = exports.discoverTables = exports.COMPATIBLE_APP_VERSION = void 0;
|
|
43
43
|
exports.detectNextUseProxy = detectNextUseProxy;
|
|
44
44
|
exports.setupNextApiPlugin = setupNextApiPlugin;
|
|
45
45
|
/**
|
|
46
46
|
* The minimum EGDesk app version this plugin version is compatible with.
|
|
47
47
|
* The app checks this at setup time and warns on mismatch.
|
|
48
48
|
*/
|
|
49
|
-
exports.COMPATIBLE_APP_VERSION = '1.4.
|
|
49
|
+
exports.COMPATIBLE_APP_VERSION = '1.4.122';
|
|
50
50
|
const fs = __importStar(require("fs"));
|
|
51
51
|
const path = __importStar(require("path"));
|
|
52
52
|
const setup_userdata_1 = require("./setup-userdata");
|
|
@@ -250,6 +250,7 @@ Object.defineProperty(exports, "generateHelpers", { enumerable: true, get: funct
|
|
|
250
250
|
Object.defineProperty(exports, "generateSchemaFile", { enumerable: true, get: function () { return generate_helpers_2.generateSchemaFile; } });
|
|
251
251
|
Object.defineProperty(exports, "ensureSchemaFile", { enumerable: true, get: function () { return generate_helpers_2.ensureSchemaFile; } });
|
|
252
252
|
Object.defineProperty(exports, "formatEgdeskSchemaFileContent", { enumerable: true, get: function () { return generate_helpers_2.formatEgdeskSchemaFileContent; } });
|
|
253
|
+
Object.defineProperty(exports, "EGDESK_HELPERS_GENERATION_VERSION", { enumerable: true, get: function () { return generate_helpers_2.EGDESK_HELPERS_GENERATION_VERSION; } });
|
|
253
254
|
var generate_api_wrapper_2 = require("./generate-api-wrapper");
|
|
254
255
|
Object.defineProperty(exports, "generateApiWrapper", { enumerable: true, get: function () { return generate_api_wrapper_2.generateApiWrapper; } });
|
|
255
256
|
var generate_visitor_auth_2 = require("./generate-visitor-auth");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@egdesk/next-api-plugin",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.115",
|
|
4
4
|
"description": "Next.js plugin for EGDesk database proxy integration",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "tsc",
|
|
19
|
+
"test": "npm run build && node --test dist/canonical-visitor-redirect.test.js",
|
|
19
20
|
"dev": "tsc --watch",
|
|
20
21
|
"prepublishOnly": "npm run build"
|
|
21
22
|
},
|