Skip to content

Commit

Permalink
Exposing FS to web is bad actually
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNite committed Oct 9, 2024
1 parent 749709f commit 01d483b
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 124 deletions.
4 changes: 2 additions & 2 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ window._moonlightBrowserInit = async () => {
}
});

window._moonlightBrowserFS = {
window.moonlightFS = {
async readFile(path) {
return new Uint8Array(await fs.readFile(path));
},
Expand Down Expand Up @@ -108,7 +108,7 @@ window._moonlightBrowserInit = async () => {
extensions,
processedExtensions,
nativesCache: {},
fs: window._moonlightBrowserFS,
isBrowser: true,

version: MOONLIGHT_VERSION,
branch: MOONLIGHT_BRANCH as MoonlightBranch,
Expand Down
21 changes: 12 additions & 9 deletions packages/core-extensions/src/moonbase/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const nightlyRefUrl = "https://moonlight-mod.github.io/moonlight/ref";
export const userAgent = `moonlight/${moonlightNode.version} (https://github.com/moonlight-mod/moonlight)`;

export default function getNatives(): MoonbaseNatives {
const fs = moonlightNode.fs;
const logger = moonlightNode.getLogger("moonbase/natives");

return {
Expand Down Expand Up @@ -71,25 +70,29 @@ export default function getNatives(): MoonbaseNatives {

const dir = moonlightNode.getExtensionDir(manifest.id);
// remake it in case of updates
if (await fs.exists(dir)) await fs.rmdir(dir);
await fs.mkdir(dir);
if (await moonlightFS.exists(dir)) await moonlightFS.rmdir(dir);
await moonlightFS.mkdir(dir);

const buffer = await req.arrayBuffer();
const files = extractAsar(buffer);
for (const [file, buf] of Object.entries(files)) {
const fullFile = fs.join(dir, file);
const fullDir = fs.dirname(fullFile);
const fullFile = moonlightFS.join(dir, file);
const fullDir = moonlightFS.dirname(fullFile);

if (!(await fs.exists(fullDir))) await fs.mkdir(fullDir);
await fs.writeFile(fs.join(dir, file), buf);
if (!(await moonlightFS.exists(fullDir)))
await moonlightFS.mkdir(fullDir);
await moonlightFS.writeFile(moonlightFS.join(dir, file), buf);
}

await fs.writeFileString(fs.join(dir, repoUrlFile), repo);
await moonlightFS.writeFileString(
moonlightFS.join(dir, repoUrlFile),
repo
);
},

async deleteExtension(id) {
const dir = moonlightNode.getExtensionDir(id);
await fs.rmdir(dir);
await moonlightFS.rmdir(dir);
},

getExtensionConfig(id, key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { mainRepo } from "@moonlight-mod/types/constants";
const logger = moonlight.getLogger("moonbase");

let natives: MoonbaseNatives = moonlight.getNatives("moonbase");
if (!natives) natives = getNatives();
if (moonlightNode.isBrowser) natives = getNatives();

class MoonbaseSettingsStore extends Store<any> {
private origConfig: Config;
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Config } from "@moonlight-mod/types";
import { getConfigPath } from "./util/data";
import * as constants from "@moonlight-mod/types/constants";
import getFS from "./fs";
import Logger from "./util/logger";

const logger = new Logger("core/config");
Expand All @@ -18,9 +17,11 @@ const defaultConfig: Config = {

export async function writeConfig(config: Config) {
try {
const fs = getFS();
const configPath = await getConfigPath();
await fs.writeFileString(configPath, JSON.stringify(config, null, 2));
await moonlightFS.writeFileString(
configPath,
JSON.stringify(config, null, 2)
);
} catch (e) {
logger.error("Failed to write config", e);
}
Expand All @@ -31,15 +32,15 @@ export async function readConfig(): Promise<Config> {
return moonlightNode.config;
}

const fs = getFS();

const configPath = await getConfigPath();
if (!(await fs.exists(configPath))) {
if (!(await moonlightFS.exists(configPath))) {
await writeConfig(defaultConfig);
return defaultConfig;
} else {
try {
let config: Config = JSON.parse(await fs.readFileString(configPath));
let config: Config = JSON.parse(
await moonlightFS.readFileString(configPath)
);
// Assign the default values if they don't exist (newly added)
config = { ...defaultConfig, ...config };
await writeConfig(config);
Expand Down
72 changes: 36 additions & 36 deletions packages/core/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ import {
ExtensionManifest,
DetectedExtension,
ExtensionLoadSource,
constants,
MoonlightFS
constants
} from "@moonlight-mod/types";
import { readConfig } from "./config";
import { getCoreExtensionsPath, getExtensionsPath } from "./util/data";
import Logger from "./util/logger";
import getFS from "./fs";

const logger = new Logger("core/extension");

async function findManifests(fs: MoonlightFS, dir: string): Promise<string[]> {
async function findManifests(dir: string): Promise<string[]> {
const ret = [];

if (await fs.exists(dir)) {
for (const file of await fs.readdir(dir)) {
const path = fs.join(dir, file);
if (await moonlightFS.exists(dir)) {
for (const file of await moonlightFS.readdir(dir)) {
const path = moonlightFS.join(dir, file);
if (file === "manifest.json") {
ret.push(path);
}

if (!(await fs.isFile(path))) {
ret.push(...(await findManifests(fs, path)));
if (!(await moonlightFS.isFile(path))) {
ret.push(...(await findManifests(path)));
}
}
}
Expand All @@ -32,50 +30,58 @@ async function findManifests(fs: MoonlightFS, dir: string): Promise<string[]> {
}

async function loadDetectedExtensions(
fs: MoonlightFS,
dir: string,
type: ExtensionLoadSource
): Promise<DetectedExtension[]> {
const ret: DetectedExtension[] = [];

const manifests = await findManifests(fs, dir);
const manifests = await findManifests(dir);
for (const manifestPath of manifests) {
try {
if (!(await fs.exists(manifestPath))) continue;
const dir = fs.dirname(manifestPath);
if (!(await moonlightFS.exists(manifestPath))) continue;
const dir = moonlightFS.dirname(manifestPath);

const manifest: ExtensionManifest = JSON.parse(
await fs.readFileString(manifestPath)
await moonlightFS.readFileString(manifestPath)
);

const webPath = fs.join(dir, "index.js");
const nodePath = fs.join(dir, "node.js");
const hostPath = fs.join(dir, "host.js");
const webPath = moonlightFS.join(dir, "index.js");
const nodePath = moonlightFS.join(dir, "node.js");
const hostPath = moonlightFS.join(dir, "host.js");

// if none exist (empty manifest) don't give a shit
if (!fs.exists(webPath) && !fs.exists(nodePath) && !fs.exists(hostPath)) {
if (
!moonlightFS.exists(webPath) &&
!moonlightFS.exists(nodePath) &&
!moonlightFS.exists(hostPath)
) {
continue;
}

const web = (await fs.exists(webPath))
? await fs.readFileString(webPath)
const web = (await moonlightFS.exists(webPath))
? await moonlightFS.readFileString(webPath)
: undefined;

let url: string | undefined = undefined;
const urlPath = fs.join(dir, constants.repoUrlFile);
if (type === ExtensionLoadSource.Normal && (await fs.exists(urlPath))) {
url = await fs.readFileString(urlPath);
const urlPath = moonlightFS.join(dir, constants.repoUrlFile);
if (
type === ExtensionLoadSource.Normal &&
(await moonlightFS.exists(urlPath))
) {
url = await moonlightFS.readFileString(urlPath);
}

const wpModules: Record<string, string> = {};
const wpModulesPath = fs.join(dir, "webpackModules");
if (await fs.exists(wpModulesPath)) {
const wpModulesFile = await fs.readdir(wpModulesPath);
const wpModulesPath = moonlightFS.join(dir, "webpackModules");
if (await moonlightFS.exists(wpModulesPath)) {
const wpModulesFile = await moonlightFS.readdir(wpModulesPath);

for (const wpModuleFile of wpModulesFile) {
if (wpModuleFile.endsWith(".js")) {
wpModules[wpModuleFile.replace(".js", "")] =
await fs.readFileString(fs.join(wpModulesPath, wpModuleFile));
await moonlightFS.readFileString(
moonlightFS.join(wpModulesPath, wpModuleFile)
);
}
}
}
Expand All @@ -91,8 +97,8 @@ async function loadDetectedExtensions(
web,
webPath: web != null ? webPath : undefined,
webpackModules: wpModules,
nodePath: (await fs.exists(nodePath)) ? nodePath : undefined,
hostPath: (await fs.exists(hostPath)) ? hostPath : undefined
nodePath: (await moonlightFS.exists(nodePath)) ? nodePath : undefined,
hostPath: (await moonlightFS.exists(hostPath)) ? hostPath : undefined
}
});
} catch (e) {
Expand All @@ -106,19 +112,16 @@ async function loadDetectedExtensions(
async function getExtensionsNative(): Promise<DetectedExtension[]> {
const config = await readConfig();
const res = [];
const fs = getFS();

res.push(
...(await loadDetectedExtensions(
fs,
getCoreExtensionsPath(),
ExtensionLoadSource.Core
))
);

res.push(
...(await loadDetectedExtensions(
fs,
await getExtensionsPath(),
ExtensionLoadSource.Normal
))
Expand All @@ -127,7 +130,6 @@ async function getExtensionsNative(): Promise<DetectedExtension[]> {
for (const devSearchPath of config.devSearchPaths ?? []) {
res.push(
...(await loadDetectedExtensions(
fs,
devSearchPath,
ExtensionLoadSource.Developer
))
Expand Down Expand Up @@ -176,11 +178,9 @@ async function getExtensionsBrowser(): Promise<DetectedExtension[]> {
});
}

const fs = getFS();
if (await fs.exists("/extensions")) {
if (await moonlightFS.exists("/extensions")) {
ret.push(
...(await loadDetectedExtensions(
fs,
"/extensions",
ExtensionLoadSource.Normal
))
Expand Down
92 changes: 43 additions & 49 deletions packages/core/src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
import { MoonlightFS } from "@moonlight-mod/types";
import type { MoonlightFS } from "@moonlight-mod/types";
import requireImport from "./util/import";

export default function getFS(): MoonlightFS {
browser: {
return window._moonlightBrowserFS!;
}
export default function createFS(): MoonlightFS {
const fs = requireImport("fs");
const path = requireImport("path");

nodeTarget: {
const fs = requireImport("fs");
const path = requireImport("path");
return {
async readFile(path) {
const file = fs.readFileSync(path);
return new Uint8Array(file);
},
async readFileString(path) {
return fs.readFileSync(path, "utf8");
},
async writeFile(path, data) {
fs.writeFileSync(path, Buffer.from(data));
},
async writeFileString(path, data) {
fs.writeFileSync(path, data, "utf8");
},
async unlink(path) {
fs.unlinkSync(path);
},

return {
async readFile(path) {
const file = fs.readFileSync(path);
return new Uint8Array(file);
},
async readFileString(path) {
return fs.readFileSync(path, "utf8");
},
async writeFile(path, data) {
fs.writeFileSync(path, Buffer.from(data));
},
async writeFileString(path, data) {
fs.writeFileSync(path, data, "utf8");
},
async unlink(path) {
fs.unlinkSync(path);
},
async readdir(path) {
return fs.readdirSync(path);
},
async mkdir(path) {
fs.mkdirSync(path, { recursive: true });
},
async rmdir(path) {
fs.rmSync(path, { recursive: true });
},

async readdir(path) {
return fs.readdirSync(path);
},
async mkdir(path) {
fs.mkdirSync(path, { recursive: true });
},
async rmdir(path) {
fs.rmSync(path, { recursive: true });
},
async exists(path) {
return fs.existsSync(path);
},
async isFile(path) {
return fs.statSync(path).isFile();
},

async exists(path) {
return fs.existsSync(path);
},
async isFile(path) {
return fs.statSync(path).isFile();
},

join(...parts) {
return path.join(...parts);
},
dirname(dir) {
return path.dirname(dir);
}
};
}
join(...parts) {
return path.join(...parts);
},
dirname(dir) {
return path.dirname(dir);
}
};
}
Loading

0 comments on commit 01d483b

Please sign in to comment.