mailwolt/resources/js/webserver/connector.js

47 lines
1.4 KiB
JavaScript

// resources/js/plugins/connector.js
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; // data-*
return {
host: host || null,
port: port ? Number(port) : null,
scheme: scheme || null,
path: path || null,
key: key || null,
};
}
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 || 'mailwolt-yhp47tbt1aebhr1fgvgj',
};
}
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 fromMeta = readMetaConfig();
const base = fromMeta ?? readViteEnv();
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 || 'mailwolt-yhp47tbt1aebhr1fgvgj',
});