2022-01-03 16:20:46 +01:00
|
|
|
/**
|
|
|
|
* Uses the acorn.js library to parse a script's code into an AST and
|
|
|
|
* recursively walk through that AST to replace import urls with blobs
|
|
|
|
*/
|
|
|
|
import * as walk from "acorn-walk";
|
|
|
|
import { parse } from "acorn";
|
|
|
|
|
2024-07-14 23:47:10 +02:00
|
|
|
import { LoadedModule, type ScriptURL, type ScriptModule } from "./Script/LoadedModule";
|
|
|
|
import type { Script } from "./Script/Script";
|
|
|
|
import type { ScriptFilePath } from "./Paths/ScriptFilePath";
|
|
|
|
import { FileType, getFileType, getModuleScript, transformScript } from "./utils/ScriptTransformer";
|
2018-05-05 05:20:19 +02:00
|
|
|
|
2022-07-16 05:34:27 +02:00
|
|
|
// Acorn type def is straight up incomplete so we have to fill with our own.
|
|
|
|
export type Node = any;
|
|
|
|
|
2018-05-05 05:20:19 +02:00
|
|
|
// Makes a blob that contains the code of a given script.
|
2021-09-24 23:07:53 +02:00
|
|
|
function makeScriptBlob(code: string): Blob {
|
2021-09-05 01:09:30 +02:00
|
|
|
return new Blob([code], { type: "text/javascript" });
|
2018-05-05 05:20:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 13:45:23 +02:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// However, we need to be able to replace this implementation in tests. Ideally
|
|
|
|
// it would be fine, but Jest causes segfaults when using dynamic import: see
|
|
|
|
// https://github.com/nodejs/node/issues/35889 and
|
|
|
|
// https://github.com/facebook/jest/issues/11438
|
|
|
|
// 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 = {
|
2023-04-07 06:33:51 +02:00
|
|
|
doImport(url: ScriptURL): Promise<ScriptModule> {
|
2023-04-01 13:45:23 +02:00
|
|
|
return import(/*webpackIgnore:true*/ url);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-04-08 03:08:39 +02:00
|
|
|
// Maps code to LoadedModules, so we can reuse compiled code across servers,
|
|
|
|
// or possibly across files (if someone makes two copies of the same script,
|
|
|
|
// or changes a script and then changes it back).
|
|
|
|
// Modules can never be garbage collected by Javascript, so it's good to try
|
|
|
|
// to keep from making more than we need.
|
2023-05-05 09:55:59 +02:00
|
|
|
const moduleCache = new Map<string, WeakRef<LoadedModule>>();
|
2023-04-08 03:08:39 +02:00
|
|
|
const cleanup = new FinalizationRegistry((mapKey: string) => {
|
|
|
|
// A new entry can be created with the same key, before this callback is called.
|
|
|
|
if (moduleCache.get(mapKey)?.deref() === undefined) {
|
|
|
|
moduleCache.delete(mapKey);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
export function compile(script: Script, scripts: Map<ScriptFilePath, Script>): Promise<ScriptModule> {
|
2023-04-07 06:33:51 +02:00
|
|
|
// Return the module if it already exists
|
2023-04-08 03:08:39 +02:00
|
|
|
if (script.mod) return script.mod.module;
|
|
|
|
|
|
|
|
script.mod = generateLoadedModule(script, scripts, []);
|
|
|
|
return script.mod.module;
|
2021-10-15 18:47:43 +02:00
|
|
|
}
|
2021-10-15 05:39:30 +02:00
|
|
|
|
2023-04-07 06:33:51 +02:00
|
|
|
/** 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.
|
2019-06-02 21:21:08 +02:00
|
|
|
*
|
2023-04-08 03:08:39 +02:00
|
|
|
* This should only be called once the script has a LoadedModule. */
|
|
|
|
function addDependencyInfo(script: Script, seenStack: Script[]) {
|
|
|
|
if (!script.mod) throw new Error(`addDependencyInfo called without a LoadedModule (${script.filename})`);
|
|
|
|
if (seenStack.length) {
|
|
|
|
script.dependents.add(seenStack[seenStack.length - 1]);
|
|
|
|
for (const dependent of seenStack) dependent.dependencies.set(script.mod.url, script);
|
2022-08-20 00:21:31 +02:00
|
|
|
}
|
2023-04-08 03:08:39 +02:00
|
|
|
// Add self to dependencies (it's not part of the stack, since we don't want
|
|
|
|
// it in dependents.)
|
|
|
|
script.dependencies.set(script.mod.url, script);
|
2019-06-02 21:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-07 06:33:51 +02:00
|
|
|
* @param script the script that needs a URL assigned
|
|
|
|
* @param scripts array of other scripts on the server
|
2023-04-08 03:08:39 +02:00
|
|
|
* @param seenStack A stack of scripts that were higher up in the import tree in a recursive call.
|
2019-06-02 21:21:08 +02:00
|
|
|
*/
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
function generateLoadedModule(script: Script, scripts: Map<ScriptFilePath, Script>, seenStack: Script[]): LoadedModule {
|
2023-04-07 06:33:51 +02:00
|
|
|
// Early return for recursive calls where the script already has a URL
|
2023-04-08 03:08:39 +02:00
|
|
|
if (script.mod) {
|
|
|
|
addDependencyInfo(script, seenStack);
|
|
|
|
return script.mod;
|
2022-01-09 09:50:36 +01:00
|
|
|
}
|
2022-01-20 22:11:48 +01:00
|
|
|
|
2024-07-14 23:47:10 +02:00
|
|
|
let scriptCode;
|
|
|
|
const fileType = getFileType(script.filename);
|
|
|
|
switch (fileType) {
|
|
|
|
case FileType.JS:
|
|
|
|
scriptCode = script.code;
|
|
|
|
break;
|
|
|
|
case FileType.JSX:
|
|
|
|
case FileType.TS:
|
|
|
|
case FileType.TSX:
|
2024-07-18 08:23:18 +02:00
|
|
|
scriptCode = transformScript(script.code, fileType);
|
2024-07-14 23:47:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`Invalid file type: ${fileType}. Filename: ${script.filename}, server: ${script.server}.`);
|
|
|
|
}
|
|
|
|
if (!scriptCode) {
|
|
|
|
throw new Error(`Cannot transform script. Filename: ${script.filename}, server: ${script.server}.`);
|
|
|
|
}
|
|
|
|
|
2023-04-07 06:33:51 +02:00
|
|
|
// Inspired by: https://stackoverflow.com/a/43834063/91401
|
2024-07-14 23:47:10 +02:00
|
|
|
const ast = parse(scriptCode, { sourceType: "module", ecmaVersion: "latest", ranges: true });
|
2023-04-07 06:33:51 +02:00
|
|
|
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);
|
2024-07-14 23:47:10 +02:00
|
|
|
let newCode = scriptCode;
|
2023-04-07 06:33:51 +02:00
|
|
|
// Loop through each node and replace the script name with a blob url.
|
|
|
|
for (const node of importNodes) {
|
2024-07-14 23:47:10 +02:00
|
|
|
const importedScript = getModuleScript(node.filename, script.filename, scripts);
|
2023-04-07 06:33:51 +02:00
|
|
|
|
2023-04-08 03:08:39 +02:00
|
|
|
seenStack.push(script);
|
|
|
|
importedScript.mod = generateLoadedModule(importedScript, scripts, seenStack);
|
|
|
|
seenStack.pop();
|
|
|
|
newCode = newCode.substring(0, node.start) + importedScript.mod.url + newCode.substring(node.end);
|
2023-04-07 06:33:51 +02:00
|
|
|
}
|
2018-05-05 05:20:19 +02:00
|
|
|
|
2023-04-08 03:08:39 +02:00
|
|
|
const cachedMod = moduleCache.get(newCode)?.deref();
|
|
|
|
if (cachedMod) {
|
|
|
|
script.mod = cachedMod;
|
|
|
|
} else {
|
|
|
|
// Add an inline source-map to make debugging nicer. This won't be right
|
|
|
|
// in all cases, since we can share the same script across multiple
|
|
|
|
// servers; it will be listed under the first server it was compiled for.
|
|
|
|
// We don't include this in the cache key, so that other instances of the
|
|
|
|
// script dedupe properly.
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const adjustedCode = newCode + `\n//# sourceURL=${script.server}/${script.filename}`;
|
2023-04-08 03:08:39 +02:00
|
|
|
// At this point we have the full code and can construct a new blob / assign the URL.
|
2024-06-03 02:38:01 +02:00
|
|
|
|
2023-04-08 03:08:39 +02:00
|
|
|
const url = URL.createObjectURL(makeScriptBlob(adjustedCode)) as ScriptURL;
|
|
|
|
const module = config.doImport(url).catch((e) => {
|
|
|
|
script.invalidateModule();
|
|
|
|
console.error(`Error occurred while attempting to compile ${script.filename} on ${script.server}:`);
|
|
|
|
console.error(e);
|
|
|
|
throw e;
|
2023-05-05 09:55:59 +02:00
|
|
|
});
|
2023-04-08 03:08:39 +02:00
|
|
|
// We can *immediately* invalidate the Blob, because we've already started the fetch
|
|
|
|
// by starting the import. From now on, any imports using the blob's URL *must*
|
|
|
|
// directly return the module, without even attempting to fetch, due to the way
|
|
|
|
// modules work.
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
script.mod = new LoadedModule(url, module);
|
|
|
|
moduleCache.set(newCode, new WeakRef(script.mod));
|
|
|
|
cleanup.register(script.mod, newCode);
|
|
|
|
}
|
2018-05-13 08:06:44 +02:00
|
|
|
|
2023-04-08 03:08:39 +02:00
|
|
|
addDependencyInfo(script, seenStack);
|
|
|
|
return script.mod;
|
2018-05-05 05:20:19 +02:00
|
|
|
}
|