mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-26 03:00:56 +02:00
SCRIPTS: Script modules are reused when they are imported (#461)
Also corrects some compile race conditions.
This commit is contained in:
+110
-151
@@ -5,8 +5,7 @@
|
||||
import * as walk from "acorn-walk";
|
||||
import { parse } from "acorn";
|
||||
|
||||
import { ScriptUrl } from "./Script/ScriptUrl";
|
||||
import { Script } from "./Script/Script";
|
||||
import { Script, ScriptURL } from "./Script/Script";
|
||||
import { areImportsEquals } from "./Terminal/DirectoryHelpers";
|
||||
import { ScriptModule } from "./Script/ScriptModule";
|
||||
|
||||
@@ -18,6 +17,24 @@ function makeScriptBlob(code: string): Blob {
|
||||
return new Blob([code], { type: "text/javascript" });
|
||||
}
|
||||
|
||||
const urlsToRevoke: ScriptURL[] = [];
|
||||
let activeCompilations = 0;
|
||||
/** Function to queue up revoking of script URLs. If there's no active compilation, just revoke it now. */
|
||||
export const queueUrlRevoke = (url: ScriptURL) => {
|
||||
if (!activeCompilations) return URL.revokeObjectURL(url);
|
||||
urlsToRevoke.push(url);
|
||||
};
|
||||
|
||||
/** Function to revoke any expired urls */
|
||||
function triggerURLRevokes() {
|
||||
if (activeCompilations === 0) {
|
||||
// Revoke all pending revoke URLS
|
||||
urlsToRevoke.forEach((url) => URL.revokeObjectURL(url));
|
||||
// Remove all url strings from array
|
||||
urlsToRevoke.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Webpack likes to turn the import into a require, which sort of
|
||||
// but not really behaves like import. So we use a "magic comment"
|
||||
// to disable that and leave it as a dynamic import.
|
||||
@@ -29,171 +46,113 @@ function makeScriptBlob(code: string): Blob {
|
||||
// import() is not a function, so it can't be replaced. We need this separate
|
||||
// config object to provide a hook point.
|
||||
export const config = {
|
||||
doImport(url: string): Promise<ScriptModule> {
|
||||
doImport(url: ScriptURL): Promise<ScriptModule> {
|
||||
return import(/*webpackIgnore:true*/ url);
|
||||
},
|
||||
};
|
||||
|
||||
export async function compile(script: Script, scripts: Script[]): Promise<ScriptModule> {
|
||||
//!shouldCompile ensures that script.module is non-null, hence the "as".
|
||||
if (!shouldCompile(script, scripts)) return script.module as Promise<ScriptModule>;
|
||||
script.queueCompile = true;
|
||||
//If we're already in the middle of compiling (script.module has not resolved yet), wait for the previous compilation to finish
|
||||
//If script.module is null, this does nothing.
|
||||
await script.module;
|
||||
//If multiple compiles were called on the same script before a compilation could be completed this ensures only one compilation is actually performed.
|
||||
if (!script.queueCompile) return script.module as Promise<ScriptModule>;
|
||||
script.queueCompile = false;
|
||||
const uurls = _getScriptUrls(script, scripts, []);
|
||||
const url = uurls[uurls.length - 1].url;
|
||||
if (script.url && script.url !== url) URL.revokeObjectURL(script.url);
|
||||
|
||||
if (script.dependencies.length > 0) script.dependencies.forEach((dep) => URL.revokeObjectURL(dep.url));
|
||||
script.url = uurls[uurls.length - 1].url;
|
||||
// The URL at the top is the one we want to import. It will
|
||||
// recursively import all the other modules in the urlStack.
|
||||
script.module = config.doImport(uurls[uurls.length - 1].url);
|
||||
script.dependencies = uurls;
|
||||
export function compile(script: Script, scripts: Script[]): Promise<ScriptModule> {
|
||||
// Return the module if it already exists
|
||||
if (script.module) return script.module;
|
||||
// While importing, use an existing url or generate a new one.
|
||||
if (!script.url) script.url = generateScriptUrl(script, scripts, []);
|
||||
activeCompilations++;
|
||||
script.module = config
|
||||
.doImport(script.url)
|
||||
.catch((e) => {
|
||||
script.invalidateModule();
|
||||
console.error(`Error occurred while attempting to compile ${script.filename} on ${script.server}:`);
|
||||
console.error(e);
|
||||
throw e;
|
||||
})
|
||||
.finally(() => {
|
||||
activeCompilations--;
|
||||
triggerURLRevokes();
|
||||
});
|
||||
return script.module;
|
||||
}
|
||||
|
||||
function isDependencyOutOfDate(filename: string, scripts: Script[], scriptModuleSequenceNumber: number): boolean {
|
||||
const depScript = scripts.find((s) => s.filename == filename);
|
||||
|
||||
// If the script is not present on the server, we should recompile, if only to get any necessary
|
||||
// compilation errors.
|
||||
if (!depScript) return true;
|
||||
|
||||
const depIsMoreRecent = depScript.moduleSequenceNumber > scriptModuleSequenceNumber;
|
||||
return depIsMoreRecent;
|
||||
}
|
||||
/** Returns whether we should compile the script parameter.
|
||||
/** Add the necessary dependency relationships for a script.
|
||||
* Dependents are used only for passing invalidation up an import tree, so only direct dependents need to be stored.
|
||||
* Direct and indirect dependents need to have the current url/script added to their dependency map for error text.
|
||||
*
|
||||
* @param {Script} script
|
||||
* @param {Script[]} scripts
|
||||
*/
|
||||
function shouldCompile(script: Script, scripts: Script[]): boolean {
|
||||
if (!script.module) return true;
|
||||
if (script.dependencies.some((dep) => isDependencyOutOfDate(dep.filename, scripts, script.moduleSequenceNumber))) {
|
||||
script.module = null;
|
||||
return true;
|
||||
* This should only be called once the script has an assigned URL. */
|
||||
function addDependencyInfo(script: Script, dependents: Script[]) {
|
||||
if (!script.url) throw new Error(`addDependencyInfo called without an assigned script URL (${script.filename})`);
|
||||
if (dependents.length) {
|
||||
script.dependents.add(dependents[dependents.length - 1]);
|
||||
for (const dependent of dependents) dependent.dependencies.set(script.url, script);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Gets a stack of blob urls, the top/right-most element being
|
||||
// the blob url for the named script on the named server.
|
||||
//
|
||||
// - script -- the script for whom we are getting a URL.
|
||||
// - scripts -- all the scripts available on this server
|
||||
// - seen -- The modules above this one -- to prevent mutual dependency.
|
||||
//
|
||||
// TODO 2.3 consider: Possibly reusing modules when imported in different locations. Per previous notes, this may
|
||||
// require a topo-sort then url-izing from leaf-most to root-most.
|
||||
/**
|
||||
* @param {Script} script
|
||||
* @param {Script[]} scripts
|
||||
* @param {Script[]} seen
|
||||
* @returns {ScriptUrl[]} All of the compiled scripts, with the final one
|
||||
* in the list containing the blob corresponding to
|
||||
* the script parameter.
|
||||
* @param script the script that needs a URL assigned
|
||||
* @param scripts array of other scripts on the server
|
||||
* @param dependents All scripts that were higher up in the import tree in a recursive call.
|
||||
*/
|
||||
// BUG: apparently seen is never consulted. Oops.
|
||||
function _getScriptUrls(script: Script, scripts: Script[], seen: Script[]): ScriptUrl[] {
|
||||
function generateScriptUrl(script: Script, scripts: Script[], dependents: Script[]): ScriptURL {
|
||||
// Early return for recursive calls where the script already has a URL
|
||||
if (script.url) {
|
||||
addDependencyInfo(script, dependents);
|
||||
return script.url;
|
||||
}
|
||||
|
||||
// Inspired by: https://stackoverflow.com/a/43834063/91401
|
||||
const urlStack: ScriptUrl[] = [];
|
||||
// Seen contains the dependents of the current script. Make sure we include that in the script dependents.
|
||||
for (const dependent of seen) {
|
||||
if (!script.dependents.some((s) => s.server === dependent.server && s.filename == dependent.filename)) {
|
||||
script.dependents.push({ server: dependent.server, filename: dependent.filename });
|
||||
}
|
||||
const ast = parse(script.code, { sourceType: "module", ecmaVersion: "latest", ranges: true });
|
||||
interface importNode {
|
||||
filename: string;
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
seen.push(script);
|
||||
try {
|
||||
// Replace every import statement with an import to a blob url containing
|
||||
// the corresponding script. E.g.
|
||||
//
|
||||
// import {foo} from "bar.js";
|
||||
//
|
||||
// becomes
|
||||
//
|
||||
// import {foo} from "blob://<uuid>"
|
||||
//
|
||||
// Where the blob URL contains the script content.
|
||||
const importNodes: importNode[] = [];
|
||||
// Walk the nodes of this tree and find any import declaration statements.
|
||||
walk.simple(ast, {
|
||||
ImportDeclaration(node: Node) {
|
||||
// Push this import onto the stack to replace
|
||||
if (!node.source) return;
|
||||
importNodes.push({
|
||||
filename: node.source.value,
|
||||
start: node.source.range[0] + 1,
|
||||
end: node.source.range[1] - 1,
|
||||
});
|
||||
},
|
||||
ExportNamedDeclaration(node: Node) {
|
||||
if (!node.source) return;
|
||||
importNodes.push({
|
||||
filename: node.source.value,
|
||||
start: node.source.range[0] + 1,
|
||||
end: node.source.range[1] - 1,
|
||||
});
|
||||
},
|
||||
ExportAllDeclaration(node: Node) {
|
||||
if (!node.source) return;
|
||||
importNodes.push({
|
||||
filename: node.source.value,
|
||||
start: node.source.range[0] + 1,
|
||||
end: node.source.range[1] - 1,
|
||||
});
|
||||
},
|
||||
});
|
||||
// Sort the nodes from last start index to first. This replaces the last import with a blob first,
|
||||
// preventing the ranges for other imports from being shifted.
|
||||
importNodes.sort((a, b) => b.start - a.start);
|
||||
let newCode = script.code;
|
||||
// Loop through each node and replace the script name with a blob url.
|
||||
for (const node of importNodes) {
|
||||
const filename = node.filename.startsWith("./") ? node.filename.substring(2) : node.filename;
|
||||
|
||||
// Parse the code into an ast tree
|
||||
const ast = parse(script.code, { sourceType: "module", ecmaVersion: "latest", ranges: true });
|
||||
interface importNode {
|
||||
filename: string;
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
const importNodes: importNode[] = [];
|
||||
// Walk the nodes of this tree and find any import declaration statements.
|
||||
walk.simple(ast, {
|
||||
ImportDeclaration(node: Node) {
|
||||
// Push this import onto the stack to replace
|
||||
if (!node.source) return;
|
||||
importNodes.push({
|
||||
filename: node.source.value,
|
||||
start: node.source.range[0] + 1,
|
||||
end: node.source.range[1] - 1,
|
||||
});
|
||||
},
|
||||
ExportNamedDeclaration(node: Node) {
|
||||
if (!node.source) return;
|
||||
importNodes.push({
|
||||
filename: node.source.value,
|
||||
start: node.source.range[0] + 1,
|
||||
end: node.source.range[1] - 1,
|
||||
});
|
||||
},
|
||||
ExportAllDeclaration(node: Node) {
|
||||
if (!node.source) return;
|
||||
importNodes.push({
|
||||
filename: node.source.value,
|
||||
start: node.source.range[0] + 1,
|
||||
end: node.source.range[1] - 1,
|
||||
});
|
||||
},
|
||||
});
|
||||
// Sort the nodes from last start index to first. This replaces the last import with a blob first,
|
||||
// preventing the ranges for other imports from being shifted.
|
||||
importNodes.sort((a, b) => b.start - a.start);
|
||||
let transformedCode = script.code;
|
||||
// Loop through each node and replace the script name with a blob url.
|
||||
for (const node of importNodes) {
|
||||
const filename = node.filename.startsWith("./") ? node.filename.substring(2) : node.filename;
|
||||
// Find the corresponding script.
|
||||
const importedScript = scripts.find((s) => areImportsEquals(s.filename, filename));
|
||||
if (!importedScript) continue;
|
||||
|
||||
// Find the corresponding script.
|
||||
const matchingScripts = scripts.filter((s) => areImportsEquals(s.filename, filename));
|
||||
if (matchingScripts.length === 0) continue;
|
||||
|
||||
const [importedScript] = matchingScripts;
|
||||
|
||||
const urls = _getScriptUrls(importedScript, scripts, seen);
|
||||
|
||||
// The top url in the stack is the replacement import file for this script.
|
||||
urlStack.push(...urls);
|
||||
const blob = urls[urls.length - 1].url;
|
||||
|
||||
// Replace the blob inside the import statement.
|
||||
transformedCode = transformedCode.substring(0, node.start) + blob + transformedCode.substring(node.end);
|
||||
}
|
||||
|
||||
// We automatically define a print function() in the NetscriptJS module so that
|
||||
// accidental calls to window.print() do not bring up the "print screen" dialog
|
||||
transformedCode += `\n//# sourceURL=${script.server}/${script.filename}`;
|
||||
|
||||
const blob = URL.createObjectURL(makeScriptBlob(transformedCode));
|
||||
// Push the blob URL onto the top of the stack.
|
||||
urlStack.push(new ScriptUrl(script.filename, blob, script.moduleSequenceNumber));
|
||||
return urlStack;
|
||||
} catch (err) {
|
||||
// If there is an error, we need to clean up the URLs.
|
||||
for (const url of urlStack) URL.revokeObjectURL(url.url);
|
||||
throw err;
|
||||
} finally {
|
||||
seen.pop();
|
||||
importedScript.url = generateScriptUrl(importedScript, scripts, [...dependents, script]);
|
||||
newCode = newCode.substring(0, node.start) + importedScript.url + newCode.substring(node.end);
|
||||
}
|
||||
|
||||
newCode += `\n//# sourceURL=${script.server}/${script.filename}`;
|
||||
|
||||
// At this point we have the full code and can construct a new blob / assign the URL.
|
||||
script.url = URL.createObjectURL(makeScriptBlob(newCode)) as ScriptURL;
|
||||
addDependencyInfo(script, dependents);
|
||||
return script.url;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user