mailwolt/resources/js/webserver/connector.js

82 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// function fromWindow() {
// const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:'
// return {
// host: (typeof window !== 'undefined' && window.location.hostname) || '127.0.0.1',
// port: isHttps ? 443 : 80,
// scheme: isHttps ? 'https' : 'http',
// path: '/ws',
// key: import.meta?.env?.VITE_REVERB_APP_KEY || ''
// }
// }
// const meta = readMetaConfig()
// const fallback = readViteEnv()
// const base = meta ?? fallback ?? fromWindow()
//
// // harte Korrektur: NIEMALS 8080 nach außen geben
// if (Number(base.port) === 8080 && (base.scheme || 'https').startsWith('http')) {
// base.port = (base.scheme === 'https') ? 443 : 80
// }
// Liest Konfig aus <meta name="reverb" ...>
function readMetaConfig() {
const el = (typeof document !== 'undefined')
? document.querySelector('meta[name="reverb"]')
: null;
if (!el) return null;
const { host, port, scheme, path, key } = el.dataset;
return {
host: host || null,
port: port ? Number(port) : null,
scheme: scheme || null,
path: path || null,
key: key || null,
};
}
// Fallback für DEV/HMR (Vite-Umgebungsvariablen)
function readViteEnv() {
const env = (typeof import.meta !== 'undefined' && import.meta.env) ? import.meta.env : {};
return {
host: env.VITE_REVERB_HOST || env.APP_HOST || '127.0.0.1',
port: env.VITE_REVERB_PORT ? Number(env.VITE_REVERB_PORT) : 443,
scheme: (env.VITE_REVERB_SCHEME || 'https').toLowerCase(),
path: env.VITE_REVERB_PATH || '/ws',
key: env.VITE_REVERB_APP_KEY || '',
};
}
function fromWindow() {
const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:';
return {
host: (typeof window !== 'undefined' && window.location.hostname) || '127.0.0.1',
port: isHttps ? 443 : 80,
scheme: isHttps ? 'https' : 'http',
path: '/ws',
key: '',
};
}
function normalize(cfg) {
const c = { ...cfg };
if (!c.path || typeof c.path !== 'string') c.path = '/ws';
if (!c.path.startsWith('/')) c.path = '/' + c.path;
c.wsScheme = (c.scheme === 'http' ? 'ws' : 'wss');
return c;
}
const meta = readMetaConfig();
const fallback = readViteEnv();
const base = meta ?? fallback ?? fromWindow();
// Niemals Port 8080 an den Browser geben nach außen 80/443 nutzen
if (Number(base.port) === 8080 && (base.scheme || 'https').startsWith('http')) {
base.port = (base.scheme === 'https') ? 443 : 80;
}
export const wsConfig = normalize({
host: base.host || '127.0.0.1',
port: base.port || 443,
scheme: base.scheme || 'https',
path: base.path || '/ws',
key: base.key || '',
});