Skip to content

Commit

Permalink
Add: convertToUrlString
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Nov 9, 2024
1 parent 301f792 commit 875abfd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
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

0 comments on commit 875abfd

Please sign in to comment.