Files
matrix-bot/scripts/test-network-guard.cjs

19 lines
871 B
JavaScript

const blocked = () => { throw new Error('Network access is disabled in test subprocesses'); };
require('node:http').request = blocked;
require('node:http').get = blocked;
require('node:https').request = blocked;
require('node:https').get = blocked;
require('node:net').Socket.prototype.connect = blocked;
require('node:tls').connect = blocked;
global.fetch = blocked;
const fs = require('node:fs');
const exists = fs.existsSync.bind(fs);
const read = fs.readFileSync.bind(fs);
const privateConfiguration = path => typeof path === 'string' &&
(/(^|\/)\.env(?:\..*)?$/.test(path) || /(^|\/)config\/bots\.yaml$/.test(path));
fs.existsSync = path => privateConfiguration(path) ? false : exists(path);
fs.readFileSync = (path, ...args) => {
if (privateConfiguration(path)) throw new Error('Runtime configuration is unavailable in tests');
return read(path, ...args);
};