Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions backend/appInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ test("url app info with manifest without name", async () => {

expect(appInfo.location).toEqual(location);
expect(appInfo.manifest).toEqual({
name: "No entry in manifest.toml (running from URL)",
name: "Missing name entry in manifest.toml",
sourceCodeUrl: "http://example.com",
manifestFound: true,
});
Expand All @@ -205,18 +205,12 @@ test("url app info with broken manifest", async () => {
fetch.isRedirect = () => false;

const location = getLocation("http://localhost:3000") as UrlLocation;
try {
await getAppInfoUrl(location, fetch);
} catch (e) {
if (e instanceof AppInfoError) {
expect(e.message).toEqual(
"Invalid manifest.toml, please check the format",
);
} else {
throw e;
}
}
expect.assertions(1);
const appInfo = await getAppInfoUrl(location, fetch);
expect(appInfo.manifest).toEqual({
name: "",
sourceCodeUrl: undefined,
manifestFound: false,
});
});

test("url app info with manifest and source code URL", async () => {
Expand Down
49 changes: 22 additions & 27 deletions backend/appInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export type AppInfo = {

export class AppInfoError extends Error {}

const MISSING_MANIFEST = {
name: undefined,
sourceCodeUrl: undefined,
manifestFound: false,
};

export async function getAppInfo(location: Location): Promise<AppInfo> {
if (location.type === "url") {
try {
Expand Down Expand Up @@ -72,19 +78,10 @@ async function getManifestInfoFromUrl(
}
const response = await fetch(url + "manifest.toml");
if (!response.ok) {
return {
name: "Unknown (running from URL)",
sourceCodeUrl: undefined,
manifestFound: false,
};
console.error("Missing manifest.toml (from URL)");
return { ...MISSING_MANIFEST, name: "Unknown (running from URL)" };
}
const body = await response.text();
const parsed = tomlParse(body);
return {
name: parsed.name || "No entry in manifest.toml (running from URL)",
sourceCodeUrl: parsed.source_code_url,
manifestFound: true,
};
return tomlParse(await response.text());
}

async function getIconInfoFromUrl(
Expand Down Expand Up @@ -117,26 +114,24 @@ function getManifestInfoFromDir(
): ManifestInfo {
const tomlBuffer = readFileBuffer(path.join(dir, "manifest.toml"));
if (tomlBuffer === null) {
return {
name: fallbackName,
sourceCodeUrl: undefined,
manifestFound: false,
};
console.error("Missing manifest.toml (from DIR)");
return { ...MISSING_MANIFEST, name: fallbackName };
}
const parsed = tomlParse(tomlBuffer.toString());
const name = parsed.name || fallbackName;
return {
name,
sourceCodeUrl: parsed.source_code_url,
manifestFound: true,
};
return tomlParse(tomlBuffer.toString(), fallbackName);
}

function tomlParse(s: string): any {
function tomlParse(s: string, fallbackName: string = ""): any {
try {
return toml.parse(s);
const parsed = toml.parse(s);
return {
name:
parsed.name || fallbackName || "Missing name entry in manifest.toml",
sourceCodeUrl: parsed.source_code_url || undefined,
manifestFound: true,
};
} catch (e) {
throw new AppInfoError("Invalid manifest.toml, please check the format");
console.error("Failed to parse manifest.toml, please check the format!");
return { ...MISSING_MANIFEST, name: fallbackName };
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"fix": "prettier --write .",
"check": "prettier --check .",
"cli": "node dist/backend/cli.js",
"test": "jest",
"test": "jest --silent",
"typecheck": "tsc --noEmit",
"build-backend": "tsc --project tsconfig-backend.json",
"build-frontend": "webpack --config webpack.prod.js",
Expand Down