Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

実装:MinimumEngineManifest格納時の検証をZodで行う #1186

Merged
merged 10 commits into from
Feb 10, 2023
15 changes: 4 additions & 11 deletions src/background/engineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ElectronStoreType,
EngineDirValidationResult,
MinimumEngineManifest,
minimumEngineManifest,
} from "@/type/preload";

import log from "electron-log";
Expand Down Expand Up @@ -94,8 +95,8 @@ export class EngineManager {
}
let manifest: MinimumEngineManifest;
try {
manifest = JSON.parse(
fs.readFileSync(manifestPath, { encoding: "utf8" })
manifest = minimumEngineManifest.parse(
JSON.parse(fs.readFileSync(manifestPath, { encoding: "utf8" }))
);
} catch (e) {
return "manifestParseError";
Expand Down Expand Up @@ -447,19 +448,11 @@ export class EngineManager {
);
let manifestContent: MinimumEngineManifest;
try {
manifestContent = JSON.parse(manifest);
manifestContent = minimumEngineManifest.parse(JSON.parse(manifest));
} catch (e) {
return "invalidManifest";
}

if (
["name", "uuid", "port", "command", "icon"].some(
(key) => !(key in manifestContent)
)
) {
return "invalidManifest";
}

const engineInfos = this.fetchEngineInfos();
if (
engineInfos.some((engineInfo) => engineInfo.uuid === manifestContent.uuid)
Expand Down
19 changes: 12 additions & 7 deletions src/background/vvppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import log from "electron-log";
import { moveFile } from "move-file";
import { Extract } from "unzipper";
import { dialog } from "electron";
import { EngineInfo, MinimumEngineManifest } from "@/type/preload";
import {
EngineInfo,
minimumEngineManifest,
MinimumEngineManifest,
} from "@/type/preload";
import MultiStream from "multistream";
import glob, { glob as callbackGlob } from "glob";

Expand Down Expand Up @@ -147,13 +151,14 @@ export class VvppManager {
.on("close", resolve)
.on("error", reject);
});
// FIXME: バリデーションをかけるか、`validateEngineDir`で検査する
const manifest = JSON.parse(
await fs.promises.readFile(
path.join(outputDir, "engine_manifest.json"),
"utf-8"
const manifest: MinimumEngineManifest = minimumEngineManifest.parse(
JSON.parse(
await fs.promises.readFile(
path.join(outputDir, "engine_manifest.json"),
"utf-8"
)
)
) as MinimumEngineManifest;
);
return {
outputDir,
manifest,
Expand Down
24 changes: 18 additions & 6 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IpcRenderer, IpcRendererEvent, nativeTheme } from "electron";
import { IpcSOData } from "./ipc";
import { z } from "zod";
import { EngineManifest } from "@/openapi";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ'EngineManifest' is defined but never usedと出てたので消しちゃっても良さそうです!

Suggested change
import { EngineManifest } from "@/openapi";


export const isMac = process.platform === "darwin";
// ホットキーを追加したときは設定のマイグレーションが必要
Expand Down Expand Up @@ -280,12 +281,23 @@ export type DefaultStyleId = {
defaultStyleId: number;
};

export type MinimumEngineManifest = {
name: string;
uuid: string;
command: string;
port: string;
};
export const supportedFeaturesItemSchema = z.object({
type: z.string(),
value: z.boolean(),
name: z.string(),
});

export const minimumEngineManifest = z
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここもSchemaだと統一感あるかもです

Suggested change
export const minimumEngineManifest = z
export const minimumEngineManifestSchema = z

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、ここ忘れられてるかもです!

.object({
name: z.string(),
uuid: z.string(),
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
command: z.string(),
port: z.number(),
supported_features: z.record(z.string(), supportedFeaturesItemSchema), // FIXME:JSON側はsnake_caseなので合わせているが、camelCaseに修正する
})
.passthrough();

export type MinimumEngineManifest = z.infer<typeof minimumEngineManifest>;

export type EngineInfo = {
uuid: string;
Expand Down