@egdesk/next-api-plugin 1.5.115 → 1.5.116
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 +11 -1
- package/dist/canonical-visitor-redirect.js +72 -17
- package/dist/canonical-visitor-redirect.test.js +42 -3
- package/dist/generate-api-wrapper.js +6 -0
- package/dist/generate-helpers.d.ts +1 -1
- package/dist/generate-helpers.js +67 -13
- package/dist/generate-visitor-auth.js +79 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -25,9 +25,19 @@ export declare function isCanonicalRedirectEnabled(input: CanonicalRedirectInput
|
|
|
25
25
|
export declare function resolveEgdeskBasePathForRedirect(input: CanonicalRedirectInput): string;
|
|
26
26
|
export declare function stripBasePathFromPathname(pathname: string, basePath: string): string;
|
|
27
27
|
export declare function shouldSkipCanonicalRedirectPath(pathname: string, basePath?: string): boolean;
|
|
28
|
+
/** True when the URL is a shared-host tunnel coding path (/t/{id}/p/{project}/...). */
|
|
29
|
+
export declare function isTunnelPrefixedCodingUrl(pathname: string, basePath?: string): boolean;
|
|
30
|
+
export declare function isTunnelUrlCanonicalRedirectOptIn(input: CanonicalRedirectInput): boolean;
|
|
31
|
+
export type CanonicalVisitorRedirectResult = {
|
|
32
|
+
url: string;
|
|
33
|
+
/** 308 for permanent www → apex; 307 for other canonical redirects (avoid sticky 308 cache). */
|
|
34
|
+
status: number;
|
|
35
|
+
};
|
|
28
36
|
/**
|
|
29
|
-
* Returns
|
|
37
|
+
* Returns redirect URL and HTTP status, or null if no redirect.
|
|
30
38
|
*/
|
|
39
|
+
export declare function resolveCanonicalVisitorRedirect(input: CanonicalRedirectInput): CanonicalVisitorRedirectResult | null;
|
|
40
|
+
/** Returns absolute redirect URL, or null if no redirect. */
|
|
31
41
|
export declare function computeCanonicalVisitorRedirectTarget(input: CanonicalRedirectInput): string | null;
|
|
32
42
|
/** Source embedded into generated middleware.ts / proxy.ts (keep in sync with tests). */
|
|
33
43
|
export declare function canonicalVisitorRedirectMiddlewareSource(): string;
|
|
@@ -15,6 +15,9 @@ exports.isCanonicalRedirectEnabled = isCanonicalRedirectEnabled;
|
|
|
15
15
|
exports.resolveEgdeskBasePathForRedirect = resolveEgdeskBasePathForRedirect;
|
|
16
16
|
exports.stripBasePathFromPathname = stripBasePathFromPathname;
|
|
17
17
|
exports.shouldSkipCanonicalRedirectPath = shouldSkipCanonicalRedirectPath;
|
|
18
|
+
exports.isTunnelPrefixedCodingUrl = isTunnelPrefixedCodingUrl;
|
|
19
|
+
exports.isTunnelUrlCanonicalRedirectOptIn = isTunnelUrlCanonicalRedirectOptIn;
|
|
20
|
+
exports.resolveCanonicalVisitorRedirect = resolveCanonicalVisitorRedirect;
|
|
18
21
|
exports.computeCanonicalVisitorRedirectTarget = computeCanonicalVisitorRedirectTarget;
|
|
19
22
|
exports.canonicalVisitorRedirectMiddlewareSource = canonicalVisitorRedirectMiddlewareSource;
|
|
20
23
|
const TUNNEL_HOST_SUFFIXES = ['.egdesk.cloud'];
|
|
@@ -135,10 +138,25 @@ function shouldSkipCanonicalRedirectPath(pathname, basePath = '') {
|
|
|
135
138
|
return true;
|
|
136
139
|
return false;
|
|
137
140
|
}
|
|
141
|
+
/** True when the URL is a shared-host tunnel coding path (/t/{id}/p/{project}/...). */
|
|
142
|
+
function isTunnelPrefixedCodingUrl(pathname, basePath = '') {
|
|
143
|
+
if (/^\/t\/[^/]+\/p\/[^/]+(\/|$)/.test(pathname))
|
|
144
|
+
return true;
|
|
145
|
+
const stripped = stripBasePathFromPathname(pathname, basePath);
|
|
146
|
+
if (stripped !== pathname && /^\/t\/[^/]+\/p\/[^/]+(\/|$)/.test(stripped))
|
|
147
|
+
return true;
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
function isTunnelUrlCanonicalRedirectOptIn(input) {
|
|
151
|
+
const fromHeader = (input.headers['x-egdesk-canonical-redirect-tunnel-urls'] || '').trim();
|
|
152
|
+
if (fromHeader === '1' || fromHeader.toLowerCase() === 'true')
|
|
153
|
+
return true;
|
|
154
|
+
return runtimeEnv(input.getEnv, 'EGDESK_CANONICAL_REDIRECT_TUNNEL_URLS') === '1';
|
|
155
|
+
}
|
|
138
156
|
/**
|
|
139
|
-
* Returns
|
|
157
|
+
* Returns redirect URL and HTTP status, or null if no redirect.
|
|
140
158
|
*/
|
|
141
|
-
function
|
|
159
|
+
function resolveCanonicalVisitorRedirect(input) {
|
|
142
160
|
if (!isCanonicalRedirectEnabled(input))
|
|
143
161
|
return null;
|
|
144
162
|
const canonical = resolveCanonicalVisitorOrigin(input);
|
|
@@ -163,22 +181,36 @@ function computeCanonicalVisitorRedirectTarget(input) {
|
|
|
163
181
|
const canonicalHost = normalizeHostname(new URL(canonical).host);
|
|
164
182
|
if (requestHost === canonicalHost)
|
|
165
183
|
return null;
|
|
166
|
-
const
|
|
167
|
-
if (
|
|
184
|
+
const wwwAlias = isWwwAlternateOfCanonical(requestHost, canonical);
|
|
185
|
+
if (wwwAlias) {
|
|
186
|
+
// www → apex on the same project (custom domain alias).
|
|
187
|
+
}
|
|
188
|
+
else if (isKnownTunnelHostname(requestHost)) {
|
|
189
|
+
if (isTunnelPrefixedCodingUrl(input.pathname, basePath) &&
|
|
190
|
+
!isTunnelUrlCanonicalRedirectOptIn(input)) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
168
195
|
return null;
|
|
196
|
+
}
|
|
169
197
|
let path = stripBasePathFromPathname(input.pathname, basePath);
|
|
170
|
-
const
|
|
198
|
+
const url = new URL((path.startsWith('/') ? path : `/${path}`) + (input.search || ''), canonical).toString();
|
|
171
199
|
if (publicOrigin) {
|
|
172
200
|
try {
|
|
173
201
|
const current = new URL((path.startsWith('/') ? path : `/${path}`) + (input.search || ''), publicOrigin).toString();
|
|
174
|
-
if (current ===
|
|
202
|
+
if (current === url)
|
|
175
203
|
return null;
|
|
176
204
|
}
|
|
177
205
|
catch {
|
|
178
206
|
// ignore
|
|
179
207
|
}
|
|
180
208
|
}
|
|
181
|
-
return
|
|
209
|
+
return { url, status: wwwAlias ? 308 : 307 };
|
|
210
|
+
}
|
|
211
|
+
/** Returns absolute redirect URL, or null if no redirect. */
|
|
212
|
+
function computeCanonicalVisitorRedirectTarget(input) {
|
|
213
|
+
return resolveCanonicalVisitorRedirect(input)?.url ?? null;
|
|
182
214
|
}
|
|
183
215
|
/** Source embedded into generated middleware.ts / proxy.ts (keep in sync with tests). */
|
|
184
216
|
function canonicalVisitorRedirectMiddlewareSource() {
|
|
@@ -303,7 +335,20 @@ function shouldSkipCanonicalRedirectPath(pathname: string, basePath = ''): boole
|
|
|
303
335
|
return false;
|
|
304
336
|
}
|
|
305
337
|
|
|
306
|
-
function
|
|
338
|
+
function isTunnelPrefixedCodingUrl(pathname: string, basePath = ''): boolean {
|
|
339
|
+
if (/^\\/t\\/[^/]+\\/p\\/[^/]+(\\/|$)/.test(pathname)) return true;
|
|
340
|
+
const stripped = stripBasePathFromPathname(pathname, basePath);
|
|
341
|
+
if (stripped !== pathname && /^\\/t\\/[^/]+\\/p\\/[^/]+(\\/|$)/.test(stripped)) return true;
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function isTunnelUrlCanonicalRedirectOptIn(request: NextRequest): boolean {
|
|
346
|
+
const fromHeader = (request.headers.get('x-egdesk-canonical-redirect-tunnel-urls') || '').trim();
|
|
347
|
+
if (fromHeader === '1' || fromHeader.toLowerCase() === 'true') return true;
|
|
348
|
+
return runtimeEnv('EGDESK_CANONICAL_REDIRECT_TUNNEL_URLS') === '1';
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function resolveCanonicalVisitorRedirect(request: NextRequest): { url: string; status: number } | null {
|
|
307
352
|
if (!isCanonicalRedirectEnabled(request)) return null;
|
|
308
353
|
const canonical = resolveCanonicalVisitorOrigin(request);
|
|
309
354
|
if (!canonical) return null;
|
|
@@ -324,12 +369,22 @@ function computeCanonicalVisitorRedirectTarget(request: NextRequest): string | n
|
|
|
324
369
|
const canonicalHost = normalizeHostname(new URL(canonical).host);
|
|
325
370
|
if (requestHost === canonicalHost) return null;
|
|
326
371
|
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
372
|
+
const wwwAlias = isWwwAlternateOfCanonical(requestHost, canonical);
|
|
373
|
+
if (wwwAlias) {
|
|
374
|
+
// www → apex on the same project (custom domain alias).
|
|
375
|
+
} else if (isKnownTunnelHostname(requestHost)) {
|
|
376
|
+
if (
|
|
377
|
+
isTunnelPrefixedCodingUrl(pathname, basePath) &&
|
|
378
|
+
!isTunnelUrlCanonicalRedirectOptIn(request)
|
|
379
|
+
) {
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
} else {
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
330
385
|
|
|
331
386
|
let path = stripBasePathFromPathname(pathname, basePath);
|
|
332
|
-
const
|
|
387
|
+
const url = new URL(
|
|
333
388
|
(path.startsWith('/') ? path : \`/\${path}\`) + search,
|
|
334
389
|
canonical,
|
|
335
390
|
).toString();
|
|
@@ -340,19 +395,19 @@ function computeCanonicalVisitorRedirectTarget(request: NextRequest): string | n
|
|
|
340
395
|
(path.startsWith('/') ? path : \`/\${path}\`) + search,
|
|
341
396
|
publicOrigin,
|
|
342
397
|
).toString();
|
|
343
|
-
if (current ===
|
|
398
|
+
if (current === url) return null;
|
|
344
399
|
} catch {
|
|
345
400
|
// ignore
|
|
346
401
|
}
|
|
347
402
|
}
|
|
348
403
|
|
|
349
|
-
return
|
|
404
|
+
return { url, status: wwwAlias ? 308 : 307 };
|
|
350
405
|
}
|
|
351
406
|
|
|
352
407
|
function maybeRedirectToCanonicalVisitorSite(request: NextRequest): NextResponse | null {
|
|
353
|
-
const
|
|
354
|
-
if (!
|
|
355
|
-
return NextResponse.redirect(
|
|
408
|
+
const resolved = resolveCanonicalVisitorRedirect(request);
|
|
409
|
+
if (!resolved) return null;
|
|
410
|
+
return NextResponse.redirect(resolved.url, resolved.status);
|
|
356
411
|
}
|
|
357
412
|
`.trim();
|
|
358
413
|
}
|
|
@@ -30,8 +30,22 @@ function baseInput(overrides = {}) {
|
|
|
30
30
|
...rest,
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
|
-
(0, node_test_1.default)('GET HTML on tunnel
|
|
33
|
+
(0, node_test_1.default)('GET HTML on /t/.../p/... tunnel URL does not redirect without opt-in', () => {
|
|
34
34
|
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput());
|
|
35
|
+
strict_1.default.equal(target, null);
|
|
36
|
+
});
|
|
37
|
+
(0, node_test_1.default)('GET HTML on tunnel host redirects when tunnel URL opt-in is set', () => {
|
|
38
|
+
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
39
|
+
getEnv: (key) => {
|
|
40
|
+
const env = {
|
|
41
|
+
EGDESK_CANONICAL_REDIRECT_ENABLED: '1',
|
|
42
|
+
EGDESK_CANONICAL_VISITOR_ORIGIN: CANON,
|
|
43
|
+
EGDESK_BASE_PATH: '/t/demo/p/MyApp',
|
|
44
|
+
EGDESK_CANONICAL_REDIRECT_TUNNEL_URLS: '1',
|
|
45
|
+
};
|
|
46
|
+
return env[key];
|
|
47
|
+
},
|
|
48
|
+
}));
|
|
35
49
|
strict_1.default.equal(target, 'https://shop.example.com/dashboard');
|
|
36
50
|
});
|
|
37
51
|
(0, node_test_1.default)('canonical host via x-forwarded-host never redirects (loop guard)', () => {
|
|
@@ -44,7 +58,7 @@ function baseInput(overrides = {}) {
|
|
|
44
58
|
}));
|
|
45
59
|
strict_1.default.equal(target, null);
|
|
46
60
|
});
|
|
47
|
-
(0, node_test_1.default)('runtime canonical from gateway header
|
|
61
|
+
(0, node_test_1.default)('runtime canonical from gateway header does not redirect /t/.../p/... without opt-in', () => {
|
|
48
62
|
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
49
63
|
getEnv: () => undefined,
|
|
50
64
|
headers: {
|
|
@@ -55,7 +69,7 @@ function baseInput(overrides = {}) {
|
|
|
55
69
|
'x-egdesk-base-path': '/t/demo/p/MyApp',
|
|
56
70
|
},
|
|
57
71
|
}));
|
|
58
|
-
strict_1.default.equal(target,
|
|
72
|
+
strict_1.default.equal(target, null);
|
|
59
73
|
});
|
|
60
74
|
(0, node_test_1.default)('POST does not redirect', () => {
|
|
61
75
|
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({ method: 'POST' }));
|
|
@@ -90,6 +104,31 @@ function baseInput(overrides = {}) {
|
|
|
90
104
|
}));
|
|
91
105
|
strict_1.default.equal(target, 'https://shop.example.com/dashboard');
|
|
92
106
|
});
|
|
107
|
+
(0, node_test_1.default)('www alternate uses 308 (permanent apex)', () => {
|
|
108
|
+
const resolved = (0, canonical_visitor_redirect_1.resolveCanonicalVisitorRedirect)(baseInput({
|
|
109
|
+
headers: {
|
|
110
|
+
accept: 'text/html',
|
|
111
|
+
'x-forwarded-host': 'www.shop.example.com',
|
|
112
|
+
'x-forwarded-proto': 'https',
|
|
113
|
+
},
|
|
114
|
+
}));
|
|
115
|
+
strict_1.default.equal(resolved?.status, 308);
|
|
116
|
+
});
|
|
117
|
+
(0, node_test_1.default)('tunnel opt-in canonical redirect uses 307 (not sticky 308)', () => {
|
|
118
|
+
const resolved = (0, canonical_visitor_redirect_1.resolveCanonicalVisitorRedirect)(baseInput({
|
|
119
|
+
getEnv: (key) => {
|
|
120
|
+
const env = {
|
|
121
|
+
EGDESK_CANONICAL_REDIRECT_ENABLED: '1',
|
|
122
|
+
EGDESK_CANONICAL_VISITOR_ORIGIN: CANON,
|
|
123
|
+
EGDESK_BASE_PATH: '/t/demo/p/MyApp',
|
|
124
|
+
EGDESK_CANONICAL_REDIRECT_TUNNEL_URLS: '1',
|
|
125
|
+
};
|
|
126
|
+
return env[key];
|
|
127
|
+
},
|
|
128
|
+
}));
|
|
129
|
+
strict_1.default.equal(resolved?.url, 'https://shop.example.com/dashboard');
|
|
130
|
+
strict_1.default.equal(resolved?.status, 307);
|
|
131
|
+
});
|
|
93
132
|
(0, node_test_1.default)('untrusted random host does not redirect (open-redirect guard)', () => {
|
|
94
133
|
const target = (0, canonical_visitor_redirect_1.computeCanonicalVisitorRedirectTarget)(baseInput({
|
|
95
134
|
headers: {
|
|
@@ -89,6 +89,12 @@ function generateApiWrapper(projectPath) {
|
|
|
89
89
|
*/
|
|
90
90
|
|
|
91
91
|
export { apiFetch, getEgdeskBasePath } from '${helpersImport}';
|
|
92
|
+
|
|
93
|
+
/** NextAuth SessionProvider basePath (EGDesk tunnel prefix + /api/auth). */
|
|
94
|
+
export function getNextAuthBasePath(): string {
|
|
95
|
+
const root = getEgdeskBasePath();
|
|
96
|
+
return root ? \`\${root}/api/auth\` : '/api/auth';
|
|
97
|
+
}
|
|
92
98
|
`;
|
|
93
99
|
fs.writeFileSync(apiPath, apiContent.replace(/\r?\n/g, os.EOL), 'utf-8');
|
|
94
100
|
console.log(`✅ Generated ${apiPath}`);
|
|
@@ -33,5 +33,5 @@ export interface GenerateHelpersOptions {
|
|
|
33
33
|
apiKeyNames?: string[];
|
|
34
34
|
}
|
|
35
35
|
/** Bump when generated helper/middleware behavior changes; EGDesk regenerates stale projects. */
|
|
36
|
-
export declare const EGDESK_HELPERS_GENERATION_VERSION = "
|
|
36
|
+
export declare const EGDESK_HELPERS_GENERATION_VERSION = "3";
|
|
37
37
|
export declare function generateHelpers(projectPath: string, options?: GenerateHelpersOptions): void;
|
package/dist/generate-helpers.js
CHANGED
|
@@ -134,7 +134,7 @@ function ensureSchemaFile(projectPath, tables) {
|
|
|
134
134
|
};
|
|
135
135
|
}
|
|
136
136
|
/** Bump when generated helper/middleware behavior changes; EGDesk regenerates stale projects. */
|
|
137
|
-
exports.EGDESK_HELPERS_GENERATION_VERSION = '
|
|
137
|
+
exports.EGDESK_HELPERS_GENERATION_VERSION = '3';
|
|
138
138
|
function generateHelpers(projectPath, options = {}) {
|
|
139
139
|
const helperPath = path.join(projectPath, 'egdesk-helpers.ts');
|
|
140
140
|
const helperContent = `/**
|
|
@@ -171,6 +171,18 @@ export const EGDESK_HELPERS_GENERATION_VERSION = '${exports.EGDESK_HELPERS_GENER
|
|
|
171
171
|
* Resolve EGDesk tunnel basePath for client-side fetch.
|
|
172
172
|
* Next.js basePath does NOT apply to raw fetch() — prepend manually in production.
|
|
173
173
|
*/
|
|
174
|
+
function detectEgdeskBasePathFromPathname(pathname: string): string {
|
|
175
|
+
const parts = pathname.split('/').filter(Boolean);
|
|
176
|
+
if (parts[0] === 't' && parts.length >= 4 && parts[2] === 'p') {
|
|
177
|
+
return \`/\${parts.slice(0, 4).join('/')}\`;
|
|
178
|
+
}
|
|
179
|
+
return '';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function isLoopbackDevHost(hostname: string): boolean {
|
|
183
|
+
return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '[::1]';
|
|
184
|
+
}
|
|
185
|
+
|
|
174
186
|
export function getEgdeskBasePath(): string {
|
|
175
187
|
if (typeof window === 'undefined') {
|
|
176
188
|
return (
|
|
@@ -180,18 +192,26 @@ export function getEgdeskBasePath(): string {
|
|
|
180
192
|
);
|
|
181
193
|
}
|
|
182
194
|
|
|
195
|
+
const fromPath = detectEgdeskBasePathFromPathname(window.location.pathname);
|
|
196
|
+
if (fromPath) return fromPath;
|
|
197
|
+
|
|
183
198
|
const fromEnv =
|
|
184
199
|
(typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_BASE_PATH) || '';
|
|
185
|
-
if (fromEnv)
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
return \`/\${parts.slice(0, 4).join('/')}\`;
|
|
200
|
+
if (fromEnv) {
|
|
201
|
+
if (isLoopbackDevHost(window.location.hostname)) {
|
|
202
|
+
return '';
|
|
203
|
+
}
|
|
204
|
+
return fromEnv;
|
|
191
205
|
}
|
|
192
206
|
return '';
|
|
193
207
|
}
|
|
194
208
|
|
|
209
|
+
/** NextAuth SessionProvider basePath (includes /api/auth suffix). */
|
|
210
|
+
export function getNextAuthBasePath(): string {
|
|
211
|
+
const root = getEgdeskBasePath();
|
|
212
|
+
return root ? \`\${root}/api/auth\` : '/api/auth';
|
|
213
|
+
}
|
|
214
|
+
|
|
195
215
|
/**
|
|
196
216
|
* Fetch wrapper that prepends EGDesk basePath to relative URLs.
|
|
197
217
|
* Use for /api/* routes and /__*_proxy paths in client components.
|
|
@@ -792,7 +812,7 @@ function estimateBase64DecodedBytes(data: string): number {
|
|
|
792
812
|
return Math.floor((trimmed.length * 3) / 4);
|
|
793
813
|
}
|
|
794
814
|
|
|
795
|
-
function base64ToUint8Array(data: string): Uint8Array {
|
|
815
|
+
function base64ToUint8Array(data: string): Uint8Array<ArrayBuffer> {
|
|
796
816
|
const trimmed = String(data || '').replace(/^data:[^;]+;base64,/i, '').replace(/\\s/g, '');
|
|
797
817
|
const bin = atob(trimmed);
|
|
798
818
|
const out = new Uint8Array(bin.length);
|
|
@@ -1302,7 +1322,7 @@ export async function uploadVoiceTranscriptAudioFromBase64(
|
|
|
1302
1322
|
): Promise<VoiceTranscriptAudioUploadResult> {
|
|
1303
1323
|
assertVoiceTranscriptFilename(filename);
|
|
1304
1324
|
const bytes = base64ToUint8Array(data);
|
|
1305
|
-
const blob = new Blob([bytes], { type: options.mimeType || 'application/octet-stream' });
|
|
1325
|
+
const blob = new Blob([Uint8Array.from(bytes)], { type: options.mimeType || 'application/octet-stream' });
|
|
1306
1326
|
const file = new File([blob], filename, { type: options.mimeType || blob.type });
|
|
1307
1327
|
return uploadVoiceTranscriptAudioChunked(file, options);
|
|
1308
1328
|
}
|
|
@@ -1329,7 +1349,7 @@ export async function uploadFile(
|
|
|
1329
1349
|
const decodedBytes = estimateBase64DecodedBytes(data);
|
|
1330
1350
|
if (typeof window !== 'undefined' && decodedBytes > UPLOAD_CHUNKED_THRESHOLD_BYTES) {
|
|
1331
1351
|
const bytes = base64ToUint8Array(data);
|
|
1332
|
-
const blob = new Blob([bytes], { type: options.mimeType || 'application/octet-stream' });
|
|
1352
|
+
const blob = new Blob([Uint8Array.from(bytes)], { type: options.mimeType || 'application/octet-stream' });
|
|
1333
1353
|
const file = new File([blob], filename, { type: options.mimeType || blob.type });
|
|
1334
1354
|
return uploadFileChunked(tableName, rowId, columnName, file, options);
|
|
1335
1355
|
}
|
|
@@ -4202,9 +4222,43 @@ export async function trashDriveFile(fileId: string, preferOAuth?: boolean) {
|
|
|
4202
4222
|
return callDriveTool('drive_trash', { fileId, preferOAuth });
|
|
4203
4223
|
}
|
|
4204
4224
|
|
|
4205
|
-
/**
|
|
4206
|
-
|
|
4207
|
-
|
|
4225
|
+
/**
|
|
4226
|
+
* Find or create the top-level EGDesk Drive folder.
|
|
4227
|
+
* When folderName is set, find or create that folder directly under EGDesk (OAuth).
|
|
4228
|
+
*/
|
|
4229
|
+
export async function findOrCreateEgdeskFolder(folderName?: string) {
|
|
4230
|
+
const root = await callDriveTool('drive_find_or_create_egdesk_folder', {});
|
|
4231
|
+
if (!folderName?.trim()) {
|
|
4232
|
+
return root;
|
|
4233
|
+
}
|
|
4234
|
+
const name = folderName.trim();
|
|
4235
|
+
const egdeskFolderId =
|
|
4236
|
+
(root && typeof root === 'object' && 'folderId' in root && root.folderId) ||
|
|
4237
|
+
(root && typeof root === 'object' && 'id' in root && root.id) ||
|
|
4238
|
+
'';
|
|
4239
|
+
if (!egdeskFolderId) {
|
|
4240
|
+
throw new Error('findOrCreateEgdeskFolder: EGDesk root folder id missing');
|
|
4241
|
+
}
|
|
4242
|
+
const escapedName = name.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, "\\\\'");
|
|
4243
|
+
const preferOAuth = true;
|
|
4244
|
+
const listed = await callDriveTool(
|
|
4245
|
+
'drive_list_files',
|
|
4246
|
+
{
|
|
4247
|
+
folderId: String(egdeskFolderId),
|
|
4248
|
+
query: \`name = '\${escapedName}' and mimeType = 'application/vnd.google-apps.folder'\`,
|
|
4249
|
+
preferOAuth,
|
|
4250
|
+
},
|
|
4251
|
+
{ preferOAuth },
|
|
4252
|
+
);
|
|
4253
|
+
const files = listed?.files;
|
|
4254
|
+
if (Array.isArray(files) && files.length > 0 && files[0]?.id) {
|
|
4255
|
+
return files[0];
|
|
4256
|
+
}
|
|
4257
|
+
return callDriveTool('drive_create_folder', {
|
|
4258
|
+
name,
|
|
4259
|
+
parentId: String(egdeskFolderId),
|
|
4260
|
+
preferOAuth,
|
|
4261
|
+
});
|
|
4208
4262
|
}
|
|
4209
4263
|
|
|
4210
4264
|
/** Find or create EGDesk/Dev, Transactions, or Tax Invoices */
|
|
@@ -404,10 +404,78 @@ function resolveRemoteMcpRoot(configured: string, tunnelUrl: string): string {
|
|
|
404
404
|
return '';
|
|
405
405
|
}
|
|
406
406
|
|
|
407
|
+
function resolveLocalMcpRootFromEnv(configured: string, tunnelUrl: string): string {
|
|
408
|
+
if (configured && isLocalEgdeskUrl(configured)) return configured.replace(/\\/$/, '');
|
|
409
|
+
if (tunnelUrl && isLocalEgdeskUrl(tunnelUrl)) return tunnelUrl.replace(/\\/$/, '');
|
|
410
|
+
return 'http://localhost:8080';
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function isKnownGatewayHostname(hostname: string): boolean {
|
|
414
|
+
return hostname === 'tunneling-service.onrender.com' || hostname.endsWith('.egdesk.cloud');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Keep in sync with visitor-auth-origin.ts sanitizeVisitorOAuthBase */
|
|
418
|
+
function sanitizeVisitorOAuthBase(returnTo: string, egdeskPublicUrl: string): string {
|
|
419
|
+
const base = egdeskPublicUrl.replace(/\\/$/, '');
|
|
420
|
+
try {
|
|
421
|
+
const publicUrl = new URL(base);
|
|
422
|
+
const site = new URL(returnTo);
|
|
423
|
+
if (isKnownGatewayHostname(publicUrl.hostname)) return base;
|
|
424
|
+
if (!/^\\/t\\/[^/]+/.test(publicUrl.pathname)) return base;
|
|
425
|
+
if (
|
|
426
|
+
(site.protocol === 'http:' || site.protocol === 'https:') &&
|
|
427
|
+
!isDevSiteHostname(site.hostname)
|
|
428
|
+
) {
|
|
429
|
+
return site.origin;
|
|
430
|
+
}
|
|
431
|
+
return publicUrl.origin;
|
|
432
|
+
} catch {
|
|
433
|
+
return base;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function resolveLocalVisitorMcpRoot(egdeskPublicUrl: string): string {
|
|
438
|
+
const trimmed = egdeskPublicUrl.replace(/\\/$/, '');
|
|
439
|
+
try {
|
|
440
|
+
const parsed = new URL(trimmed);
|
|
441
|
+
if (isLoopbackHostname(parsed.hostname)) {
|
|
442
|
+
const port = parsed.port || '8080';
|
|
443
|
+
return \`http://localhost:\${port}\`;
|
|
444
|
+
}
|
|
445
|
+
} catch {
|
|
446
|
+
// ignore
|
|
447
|
+
}
|
|
448
|
+
return 'http://localhost:8080';
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Authorized redirect URI for BYO visitor GCP (gateway mode). Not the Next dev port (:3000/:4000).
|
|
453
|
+
* Keep in sync with visitor-auth-origin.ts resolveOperatorVisitorWebCallbackUri (gateway).
|
|
454
|
+
*/
|
|
455
|
+
export function resolveVisitorGcpRedirectUriToRegister(): string {
|
|
456
|
+
if (typeof window === 'undefined') {
|
|
457
|
+
throw new Error('resolveVisitorGcpRedirectUriToRegister() must run in the browser');
|
|
458
|
+
}
|
|
459
|
+
const returnTo = new URL(resolveVisitorAppPath('/auth/callback'), window.location.origin).toString();
|
|
460
|
+
const egdeskPublicUrl = resolveEgdeskPublicUrl();
|
|
461
|
+
if (isLoopbackHostname(new URL(returnTo).hostname)) {
|
|
462
|
+
return \`\${resolveLocalVisitorMcpRoot(egdeskPublicUrl)}/visitor-auth/callback\`;
|
|
463
|
+
}
|
|
464
|
+
if (isDevSiteHostname(new URL(returnTo).hostname)) {
|
|
465
|
+
const base = sanitizeVisitorOAuthBase(returnTo, egdeskPublicUrl);
|
|
466
|
+
if (isTunnelMcpRoot(base)) {
|
|
467
|
+
return \`\${base.replace(/\\/$/, '')}/visitor-auth/callback\`;
|
|
468
|
+
}
|
|
469
|
+
return \`\${resolveLocalVisitorMcpRoot(egdeskPublicUrl)}/visitor-auth/callback\`;
|
|
470
|
+
}
|
|
471
|
+
const sanitized = sanitizeVisitorOAuthBase(returnTo, egdeskPublicUrl);
|
|
472
|
+
return \`\${sanitized.replace(/\\/$/, '')}/visitor-auth/callback\`;
|
|
473
|
+
}
|
|
474
|
+
|
|
407
475
|
/**
|
|
408
476
|
* MCP root passed to visitor-auth start (egdeskPublicUrl).
|
|
409
477
|
*
|
|
410
|
-
* - Loopback dev →
|
|
478
|
+
* - Loopback dev → local MCP (:8080); platform OAuth still bounces via :54321 server-side.
|
|
411
479
|
* - LAN IP → tunnel MCP root from NEXT_PUBLIC_EGDESK_TUNNEL_URL or non-local API URL (required).
|
|
412
480
|
* - Tunnel/custom domain → tunnel MCP root; never window.location.origin on dev/LAN hosts.
|
|
413
481
|
*
|
|
@@ -423,7 +491,7 @@ export function resolveEgdeskPublicUrl(): string {
|
|
|
423
491
|
''
|
|
424
492
|
).replace(/\\/$/, '');
|
|
425
493
|
const remoteMcpRoot = resolveRemoteMcpRoot(configured, tunnelUrl);
|
|
426
|
-
const localMcpRoot = configured
|
|
494
|
+
const localMcpRoot = resolveLocalMcpRootFromEnv(configured, tunnelUrl);
|
|
427
495
|
|
|
428
496
|
if (typeof window === 'undefined') {
|
|
429
497
|
return remoteMcpRoot || localMcpRoot;
|
|
@@ -449,7 +517,7 @@ export function resolveEgdeskPublicUrl(): string {
|
|
|
449
517
|
);
|
|
450
518
|
}
|
|
451
519
|
if (isLoopbackHostname(hostname)) {
|
|
452
|
-
return
|
|
520
|
+
return localMcpRoot;
|
|
453
521
|
}
|
|
454
522
|
if (remoteMcpRoot) return remoteMcpRoot;
|
|
455
523
|
throw new Error(
|
|
@@ -530,6 +598,7 @@ export async function startVisitorGoogleLogin(options: {
|
|
|
530
598
|
egdeskPublicUrl,
|
|
531
599
|
forceConsent: options.forceConsent === true,
|
|
532
600
|
scopes,
|
|
601
|
+
visitorOrigin: window.location.origin,
|
|
533
602
|
};
|
|
534
603
|
if (options.gcp?.trim()) startArgs.gcp = options.gcp.trim();
|
|
535
604
|
if (options.handle?.trim()) startArgs.handle = options.handle.trim();
|
|
@@ -541,7 +610,11 @@ export async function startVisitorGoogleLogin(options: {
|
|
|
541
610
|
}
|
|
542
611
|
|
|
543
612
|
export async function exchangeVisitorAuthCode(code: string) {
|
|
544
|
-
const
|
|
613
|
+
const exchangeArgs: Record<string, unknown> = { code };
|
|
614
|
+
if (typeof window !== 'undefined') {
|
|
615
|
+
exchangeArgs.visitorOrigin = window.location.origin;
|
|
616
|
+
}
|
|
617
|
+
const result = await callVisitorAuth('exchange', exchangeArgs);
|
|
545
618
|
if (!result?.sessionId) {
|
|
546
619
|
throw new Error(result?.error || 'Failed to complete visitor login');
|
|
547
620
|
}
|
|
@@ -596,7 +669,8 @@ export async function getVisitorGcpStatus(handle: string) {
|
|
|
596
669
|
throw new Error('getVisitorGcpStatus() must run in the browser');
|
|
597
670
|
}
|
|
598
671
|
const sampleReturnTo = new URL(resolveVisitorAppPath('/auth/callback'), window.location.origin).toString();
|
|
599
|
-
|
|
672
|
+
const egdeskPublicUrl = resolveEgdeskPublicUrl();
|
|
673
|
+
return callVisitorAuth('gcp_status', { handle, sampleReturnTo, egdeskPublicUrl });
|
|
600
674
|
}
|
|
601
675
|
|
|
602
676
|
export async function forgetVisitorGcp(handle: string) {
|
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.123";
|
|
12
12
|
export interface SetupOptions {
|
|
13
13
|
egdeskUrl?: string;
|
|
14
14
|
/** Production EGDesk server URL — written to .env.production.local. Falls back to egdeskUrl. */
|
package/dist/index.js
CHANGED
|
@@ -46,7 +46,7 @@ exports.setupNextApiPlugin = setupNextApiPlugin;
|
|
|
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.123';
|
|
50
50
|
const fs = __importStar(require("fs"));
|
|
51
51
|
const path = __importStar(require("path"));
|
|
52
52
|
const setup_userdata_1 = require("./setup-userdata");
|