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

Refactor: エンジンの URL を組み立てる部分を関数に切り出す #2351

Merged
merged 2 commits into from
Nov 16, 2024
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
12 changes: 7 additions & 5 deletions src/backend/electron/manager/RuntimeInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import log from "electron-log/main";
import type { AltPortInfos } from "@/store/type";
import { EngineId, EngineInfo } from "@/type/preload";
import { writeFileSafely } from "@/backend/electron/fileHelper";
import { createEngineUrl } from "@/domain/url";

/**
* ランタイム情報書き出しに必要なEngineInfo
Expand Down Expand Up @@ -85,13 +86,14 @@ export class RuntimeInfoManager {
const altPort: string | undefined =
this.altportInfos[engineInfo.uuid];
const port = altPort ?? engineInfo.defaultPort;
// NOTE: URLを正規化する
const url = new URL(`${engineInfo.protocol}//${engineInfo.hostname}`);
url.port = port;
return {
uuid: engineInfo.uuid,
// NOTE: URLインターフェースは"pathname"が空文字でも"/"を付けるので手動で結合する。
url: `${url.origin}${engineInfo.pathname}`,
url: createEngineUrl({
protocol: engineInfo.protocol,
hostname: engineInfo.hostname,
port,
pathname: engineInfo.pathname,
}),
name: engineInfo.name,
};
}),
Expand Down
18 changes: 18 additions & 0 deletions src/domain/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** エンジンのURLを構成するパラメータ */
export type EngineUrlParams = {
protocol: string; // `http:`など
hostname: string; // `example.com`など
port: string; // `50021`など。空文字列もありえる。
pathname: string; // `/engine`など。空文字列もありえる。
};

/**
* URLを構成するパラメータから、VOICEVOXエディタが初期から想定しているURL文字列を組み立てる。
* pathnameが空文字の場合は末尾にスラッシュを付与しない、などがビルトインのURLクラスと異なる。
*/
export function createEngineUrl(params: EngineUrlParams) {
const { protocol, hostname, port, pathname } = params;
const url = new URL(`${protocol}//${hostname}`); // NOTE: URLを正規化する
url.port = port;
return `${url.origin}${pathname}`; // NOTE: URLインターフェースは"pathname"が空文字でも"/"を付けるので手動で結合する。
}
12 changes: 7 additions & 5 deletions src/store/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ProxyStoreState, ProxyStoreTypes, EditorAudioQuery } from "./type";
import { createPartialStore } from "./vuex";
import { createEngineUrl } from "@/domain/url";
import {
IEngineConnectorFactory,
OpenAPIEngineConnectorFactory,
Expand All @@ -22,12 +23,13 @@ const proxyStoreCreator = (_engineFactory: IEngineConnectorFactory) => {

const altPort: string | undefined = state.altPortInfos[engineId];
const port = altPort ?? engineInfo.defaultPort;
// NOTE: URLを正規化する
const url = new URL(`${engineInfo.protocol}//${engineInfo.hostname}`);
url.port = port;
// NOTE: URLインターフェースは"pathname"が空文字でも"/"を付けるので手動で結合する。
const instance = _engineFactory.instance(
`${url.origin}${engineInfo.pathname}`,
createEngineUrl({
protocol: engineInfo.protocol,
hostname: engineInfo.hostname,
port,
pathname: engineInfo.pathname,
}),
);
return Promise.resolve({
invoke: (v) => (arg) =>
Expand Down
Loading