Files
BridgeSwarm/native-host/discord/load-discord.js
T
snxraven 693814f46b
CI / Build & Test (push) Failing after 52s
Add Discordjs
2026-09-04 22:10:35 -04:00

140 lines
3.9 KiB
JavaScript

/**
* Load vendored bare-discord-js. CJS only — the ESM entry uses node:module.
* Apply WHATWG WS + FormData before requiring the package.
*/
'use strict';
const { takePackedDiscordJs, unwrapDiscordModule } = require('./registry.js');
let discordJsCache;
let discordJsTried = false;
let discordJsLastError = '';
function getBareDiscordJsLoadError() {
return discordJsLastError;
}
function noteDiscordLoadError(err) {
const msg =
err && typeof err === 'object' && err.message
? String(err.message || err)
: String(err || 'unknown error');
discordJsLastError = msg.slice(0, 800);
return discordJsLastError;
}
function bareRuntime() {
if (typeof globalThis.Bare !== 'undefined') return true;
const v = globalThis.process && globalThis.process.versions;
return Boolean(v && typeof v.bare === 'string');
}
function normalizeDiscordToken(raw) {
if (typeof raw !== 'string') return '';
let t = raw.trim();
if (t.charCodeAt(0) === 0xfeff) t = t.slice(1);
t = t.replace(/[\u200B-\u200D\uFEFF]/g, '');
return t.trim();
}
function applyBareProcessEmitWarning() {
const proc = globalThis.process;
if (!proc || typeof proc.emitWarning === 'function') return;
proc.emitWarning = function emitWarning(warning, type, code) {
const msg = warning instanceof Error ? warning.message : String(warning);
const name = typeof type === 'string' ? type : 'Warning';
const id = typeof code === 'string' ? code : '';
const line = id ? name + ' [' + id + ']: ' + msg : name + ': ' + msg;
try {
if (typeof proc.emit === 'function') proc.emit('warning', warning);
} catch (_) {}
try {
console.error(line);
} catch (_) {}
};
}
function applyBareTlsCompat() {
const proc = globalThis.process;
if (!proc || !proc.env) return;
if (!proc.env.NODE_TLS_REJECT_UNAUTHORIZED) {
proc.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
try {
const req = globalThis.require;
if (typeof req !== 'function') return;
const bareHttps = req('bare-https');
if (!bareHttps || typeof bareHttps.Agent !== 'function') return;
const insecureAgent = new bareHttps.Agent({ rejectUnauthorized: false });
bareHttps.globalAgent = insecureAgent;
if (bareHttps.Agent) bareHttps.Agent.global = insecureAgent;
} catch (_) {}
}
function applyAdapters() {
applyBareProcessEmitWarning();
if (bareRuntime()) applyBareTlsCompat();
try {
const ws = require('../vendor/bare-discord-js/src/adapters/whatwg-ws.cjs');
if (typeof ws.installBareOsDiscordGatewayWs === 'function') {
ws.installBareOsDiscordGatewayWs();
}
} catch (_) {}
try {
const fd = require('../vendor/bare-discord-js/src/adapters/form-data.cjs');
if (typeof fd.installBareOsFormDataGlobal === 'function') {
fd.installBareOsFormDataGlobal();
}
} catch (_) {}
}
function loadBareDiscordJsSync() {
applyAdapters();
if (discordJsTried) return discordJsCache;
discordJsTried = true;
discordJsLastError = '';
let lastErr = null;
try {
const packed = unwrapDiscordModule(takePackedDiscordJs());
if (packed) {
discordJsCache = packed;
return discordJsCache;
}
} catch (err) {
lastErr = err;
}
try {
const mod = require('bare-discord-js');
const discord = unwrapDiscordModule(mod);
if (discord) {
discordJsCache = discord;
return discordJsCache;
}
if (!lastErr) lastErr = new Error('require("bare-discord-js") returned no Client');
} catch (err) {
lastErr = err;
}
noteDiscordLoadError(lastErr || 'vendored bare-discord-js unresolved');
try {
if (process.stderr) {
process.stderr.write(
'[bridge-swarm-host] BridgeSwarm.DiscordJS: failed to load bare-discord-js: ' +
discordJsLastError +
'\n'
);
}
} catch (_) {}
discordJsCache = undefined;
return undefined;
}
module.exports = {
loadBareDiscordJsSync,
getBareDiscordJsLoadError,
normalizeDiscordToken,
unwrapDiscordModule,
};