-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from moonlight-mod/notnite/abstracted-fs
Abstract FS for browser
- Loading branch information
Showing
13 changed files
with
289 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { MoonlightBranch } from "@moonlight-mod/types"; | ||
import type { MoonbaseNatives, RepositoryManifest } from "./types"; | ||
import extractAsar from "@moonlight-mod/core/asar"; | ||
import { repoUrlFile } from "@moonlight-mod/types/constants"; | ||
|
||
export const githubRepo = "moonlight-mod/moonlight"; | ||
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 { | ||
async checkForMoonlightUpdate() { | ||
try { | ||
if (moonlightNode.branch === MoonlightBranch.STABLE) { | ||
const req = await fetch( | ||
`https://api.github.com/repos/${githubRepo}/releases/latest?_=${Date.now()}`, | ||
{ | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
} | ||
); | ||
const json: { name: string } = await req.json(); | ||
return json.name !== moonlightNode.version ? json.name : null; | ||
} else if (moonlightNode.branch === MoonlightBranch.NIGHTLY) { | ||
const req = await fetch(`${nightlyRefUrl}?_=${Date.now()}`, { | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
}); | ||
const ref = (await req.text()).split("\n")[0]; | ||
return ref !== moonlightNode.version ? ref : null; | ||
} | ||
|
||
return null; | ||
} catch (e) { | ||
logger.error("Error checking for moonlight update", e); | ||
return null; | ||
} | ||
}, | ||
|
||
async fetchRepositories(repos) { | ||
const ret: Record<string, RepositoryManifest[]> = {}; | ||
|
||
for (const repo of repos) { | ||
try { | ||
const req = await fetch(repo, { | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
}); | ||
const json = await req.json(); | ||
ret[repo] = json; | ||
} catch (e) { | ||
logger.error(`Error fetching repository ${repo}`, e); | ||
} | ||
} | ||
|
||
return ret; | ||
}, | ||
|
||
async installExtension(manifest, url, repo) { | ||
const req = await fetch(url, { | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
}); | ||
|
||
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); | ||
|
||
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); | ||
|
||
if (!(await fs.exists(fullDir))) await fs.mkdir(fullDir); | ||
await fs.writeFile(fs.join(dir, file), buf); | ||
} | ||
|
||
await fs.writeFileString(fs.join(dir, repoUrlFile), repo); | ||
}, | ||
|
||
async deleteExtension(id) { | ||
const dir = moonlightNode.getExtensionDir(id); | ||
await fs.rmdir(dir); | ||
}, | ||
|
||
getExtensionConfig(id, key) { | ||
const config = moonlightNode.config.extensions[id]; | ||
if (typeof config === "object") { | ||
return config.config?.[key]; | ||
} | ||
|
||
return undefined; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,2 @@ | ||
import { MoonbaseNatives, RepositoryManifest } from "./types"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import extractAsar from "@moonlight-mod/core/asar"; | ||
import { repoUrlFile } from "@moonlight-mod/types/constants"; | ||
import { githubRepo, userAgent, nightlyRefUrl } from "./consts"; | ||
import { MoonlightBranch } from "types/src"; | ||
|
||
const logger = moonlightNode.getLogger("moonbase"); | ||
|
||
async function checkForMoonlightUpdate() { | ||
try { | ||
if (moonlightNode.branch === MoonlightBranch.STABLE) { | ||
const req = await fetch( | ||
`https://api.github.com/repos/${githubRepo}/releases/latest?_=${Date.now()}`, | ||
{ | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
} | ||
); | ||
const json: { name: string } = await req.json(); | ||
return json.name !== moonlightNode.version ? json.name : null; | ||
} else if (moonlightNode.branch === MoonlightBranch.NIGHTLY) { | ||
const req = await fetch(`${nightlyRefUrl}?_=${Date.now()}`, { | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
}); | ||
const ref = (await req.text()).split("\n")[0]; | ||
return ref !== moonlightNode.version ? ref : null; | ||
} | ||
|
||
return null; | ||
} catch (e) { | ||
logger.error("Error checking for moonlight update", e); | ||
return null; | ||
} | ||
} | ||
|
||
async function fetchRepositories(repos: string[]) { | ||
const ret: Record<string, RepositoryManifest[]> = {}; | ||
|
||
for (const repo of repos) { | ||
try { | ||
const req = await fetch(repo, { | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
}); | ||
const json = await req.json(); | ||
ret[repo] = json; | ||
} catch (e) { | ||
logger.error(`Error fetching repository ${repo}`, e); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
async function installExtension( | ||
manifest: RepositoryManifest, | ||
url: string, | ||
repo: string | ||
) { | ||
const req = await fetch(url, { | ||
headers: { | ||
"User-Agent": userAgent | ||
} | ||
}); | ||
|
||
const dir = moonlightNode.getExtensionDir(manifest.id); | ||
// remake it in case of updates | ||
if (fs.existsSync(dir)) fs.rmdirSync(dir, { recursive: true }); | ||
fs.mkdirSync(dir, { recursive: true }); | ||
|
||
const buffer = await req.arrayBuffer(); | ||
const files = extractAsar(buffer); | ||
for (const [file, buf] of Object.entries(files)) { | ||
const nodeBuf = Buffer.from(buf); | ||
const fullFile = path.join(dir, file); | ||
const fullDir = path.dirname(fullFile); | ||
|
||
if (!fs.existsSync(fullDir)) fs.mkdirSync(fullDir, { recursive: true }); | ||
fs.writeFileSync(path.join(dir, file), nodeBuf); | ||
} | ||
|
||
fs.writeFileSync(path.join(dir, repoUrlFile), repo); | ||
} | ||
|
||
async function deleteExtension(id: string) { | ||
const dir = moonlightNode.getExtensionDir(id); | ||
fs.rmdirSync(dir, { recursive: true }); | ||
} | ||
|
||
function getExtensionConfig(id: string, key: string): any { | ||
const config = moonlightNode.config.extensions[id]; | ||
if (typeof config === "object") { | ||
return config.config?.[key]; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
const exports: MoonbaseNatives = { | ||
checkForMoonlightUpdate, | ||
fetchRepositories, | ||
installExtension, | ||
deleteExtension, | ||
getExtensionConfig | ||
}; | ||
|
||
module.exports = exports; | ||
import getNatives from "./native"; | ||
module.exports = getNatives(); |
Oops, something went wrong.