API: dynamic imports with ns.dynamicImport() (#2036)

This commit is contained in:
Shy
2025-04-10 01:49:01 +00:00
committed by GitHub
parent cdb74e74dd
commit d28a06e764
5 changed files with 90 additions and 0 deletions
+13
View File
@@ -112,6 +112,8 @@ import { assertFunctionWithNSContext } from "./Netscript/TypeAssertion";
import { Router } from "./ui/GameRoot";
import { Page } from "./ui/Router";
import { canAccessBitNodeFeature, validBitNodes } from "./BitNode/BitNodeUtils";
import { compile } from "./NetscriptJSEvaluator";
import { Script } from "./Script/Script";
export const enums: NSEnums = {
CityName,
@@ -1836,6 +1838,17 @@ export const ns: InternalAPI<NSFull> = {
printRaw: (ctx) => (value) => {
ctx.workerScript.print(wrapUserNode(value));
},
dynamicImport: (ctx) => async (value) => {
const path = helpers.scriptPath(ctx, "path", value);
const server = helpers.getServer(ctx, ctx.workerScript.hostname);
const script = server.getContentFile(path);
if (!script) throw helpers.errorMessage(ctx, `Script was not found\nPath: ${path}`);
//We validated the path as ScriptFilePath and made sure script is not null
//Script **must** be a script at this point
return compile(script as Script, server.scripts);
},
flags: Flags,
heart: { break: () => () => Player.karma },
...NetscriptExtra(),