TeaSpeak-Client/modules/renderer/RequireProxy.ts

149 lines
4.5 KiB
TypeScript
Raw Normal View History

import * as path from "path";
import {remote} from "electron";
import * as electron from "electron";
import * as os from "os";
const Module = require("module");
interface ModuleOverride {
name?: string,
test: string | RegExp | ((request: string) => boolean);
callback: (this: string, request: string, parent?: NodeJS.Module) => any;
}
const overrides: ModuleOverride[] = [];
function proxied_load(request: string, parent?: NodeJS.Module) {
for(const override of overrides) {
let test_satisfied = false;
if(typeof override.test === "string") {
test_satisfied = override.test === request;
} else if(typeof override.test === "function") {
test_satisfied = override.test(request);
} else if(typeof override === "object") {
if(override.test instanceof RegExp)
test_satisfied = !!request.match(override.test);
}
if(test_satisfied) {
//console.log("Using override %s for %s", override.name || "unnamed", request);
return override.callback.apply(this, arguments);
}
}
//console.log("No override found for %s", request);
return proxied_load.original_load.apply(this, arguments);
}
function shared_backend_loader(request: string) {
if(!request.startsWith("tc-backend/"))
throw "invalid target";
if(!backend_root)
throw "backend is not available in this context";
const target = request.substr(11);
return require(path.join(backend_root, target));
}
namespace proxied_load {
export let original_load: typeof Module.require;
}
let backend_root: string;
2020-10-01 04:56:22 -04:00
let app_module: string;
export function initialize(backend_root_: string, app_module_: string) {
backend_root = backend_root_;
2020-10-01 04:56:22 -04:00
app_module = app_module_;
proxied_load.original_load = Module._load;
Module._load = proxied_load;
window["backend-loader"] = {
require: shared_backend_loader
};
}
overrides.push({
name: "tc-loader",
test: "tc-loader",
callback: () => window["loader"]
});
overrides.push({
name: "native loader",
test: /^tc-native\/[a-zA-Z_-]+$/,
callback: request => {
const name = request.substr(10);
const file_mapping = {
connection: "teaclient_connection.node",
ppt: "teaclient_ppt.node",
dns: "teaclient_dns.node"
};
if(typeof file_mapping[name] !== "string")
throw "unknown native module";
const app_path = (remote || electron).app.getAppPath();
2020-04-04 08:27:42 -04:00
let target_path;
if(app_path.endsWith(".asar")) {
2020-04-04 13:58:22 -04:00
target_path = path.join(path.dirname(app_path), "natives", file_mapping[name]);
2020-04-04 08:27:42 -04:00
} else {
/* from source code */
target_path = path.join(app_path, "native", "build", os.platform() + "_" + os.arch(), file_mapping[name]);
}
return require(target_path);
}
});
2020-08-09 09:03:52 -04:00
function resolveModuleMapping(context: string, resource: string) {
2020-10-01 04:56:22 -04:00
if(context.endsWith("/")) {
2020-08-09 09:03:52 -04:00
context = context.substring(0, context.length - 1);
2020-10-01 04:56:22 -04:00
}
2020-08-09 09:03:52 -04:00
const loader = require("tc-loader");
2020-10-01 04:56:22 -04:00
const mapping = loader.module_mapping().find(e => e.application === app_module); //FIXME: Variable name!
if(!mapping) {
debugger;
throw "missing ui module mapping";
}
2020-08-09 09:03:52 -04:00
const entries = mapping.modules.filter(e => e.context === context);
if(!entries.length) {
debugger;
throw "unknown target path";
}
const entry = entries.find(e => path.basename(e.resource, path.extname(e.resource)) === resource);
if(!entry) {
if(resource.indexOf(".") === -1 && !resource.endsWith("/"))
return resolveModuleMapping(context + "/" + resource, "index");
throw "unknown import (" + context + "/" + resource + ")";
}
return entry.id;
}
overrides.push({
name: "svg sprites",
test: /^svg-sprites\/.*/,
callback: request => {
const entryId = resolveModuleMapping("svg-sprites", request.substring("svg-sprites/".length));
return window["shared-require"](entryId);
}
});
overrides.push({
name: "shared loader",
test: /^tc-shared\/.*/,
callback: request => {
if(request.endsWith("/"))
return require(request + "index");
2020-08-09 09:03:52 -04:00
let contextPath = path.dirname(request.substr(10));
const entryId = resolveModuleMapping("shared/js/" + (contextPath === "." ? "" : contextPath), path.basename(request, path.extname(request)));
return window["shared-require"](entryId);
}
});