feat: 支持配置 safeDomain 用来支持一个网站多域名通信问题

This commit is contained in:
jeasonnow
2023-06-25 15:22:46 +08:00
parent 80ff9fae0f
commit 3c97825b54
4 changed files with 30 additions and 19 deletions

1
bin/cli.ts vendored
View File

@@ -31,6 +31,7 @@ program
.option('--multi-arch', 'Only for Mac, supports both Intel and M1', DEFAULT.multiArch) .option('--multi-arch', 'Only for Mac, supports both Intel and M1', DEFAULT.multiArch)
.option('--targets <string>', 'Only for Linux, option "deb" or "appimage"', DEFAULT.targets) .option('--targets <string>', 'Only for Linux, option "deb" or "appimage"', DEFAULT.targets)
.option('--inject [injects...]', 'inject .js or .css for this app', DEFAULT.inject) .option('--inject [injects...]', 'inject .js or .css for this app', DEFAULT.inject)
.option('--safe-domain [domains...]', 'domains that can call window.__TAURI__ and use ipc', DEFAULT.safeDomain)
.option('--debug', 'Debug mode', DEFAULT.debug) .option('--debug', 'Debug mode', DEFAULT.debug)
.version(packageJson.version, '-v, --version', 'Output the current version') .version(packageJson.version, '-v, --version', 'Output the current version')
.action(async (url: string, options: PakeCliOptions) => { .action(async (url: string, options: PakeCliOptions) => {

1
bin/defaults.ts vendored
View File

@@ -16,4 +16,5 @@ export const DEFAULT_PAKE_OPTIONS: PakeCliOptions = {
systemTrayIcon: '', systemTrayIcon: '',
debug: false, debug: false,
inject: [], inject: [],
safeDomain: [],
}; };

44
bin/helpers/merge.ts vendored
View File

@@ -21,6 +21,7 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
name, name,
resizable = true, resizable = true,
inject, inject,
safeDomain,
} = options; } = options;
const { platform } = process; const { platform } = process;
@@ -59,9 +60,7 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
const filesToCopyBack = ['cli.js', 'about_pake.html']; const filesToCopyBack = ['cli.js', 'about_pake.html'];
await Promise.all( await Promise.all(
filesToCopyBack.map(file => filesToCopyBack.map(file => fsExtra.copy(path.join(distBakDir, file), path.join(distDir, file))),
fsExtra.copy(path.join(distBakDir, file), path.join(distDir, file)),
),
); );
} }
@@ -70,11 +69,24 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
} else { } else {
tauriConf.pake.windows[0].url_type = 'web'; tauriConf.pake.windows[0].url_type = 'web';
// Set the secure domain for calling window.__TAURI__ to the application domain that has been set. // Set the secure domain for calling window.__TAURI__ to the application domain that has been set.
tauriConf.tauri.security.dangerousRemoteDomainIpcAccess = [{ tauriConf.tauri.security.dangerousRemoteDomainIpcAccess = [
domain: new URL(url).hostname, {
windows: ["pake"], domain: new URL(url).hostname,
enableTauriAPI: true, windows: ['pake'],
}]; enableTauriAPI: true,
},
];
}
if (safeDomain.length > 0) {
tauriConf.tauri.security.dangerousRemoteDomainIpcAccess = [
...tauriConf.tauri.security.dangerousRemoteDomainIpcAccess,
...safeDomain.map(domain => ({
domain,
windows: ['pake'],
enableTauriAPI: true,
})),
];
} }
const platformMap: PlatformMap = { const platformMap: PlatformMap = {
@@ -96,12 +108,9 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
delete tauriConf.tauri.bundle.deb.files; delete tauriConf.tauri.bundle.deb.files;
const validTargets = ['all', 'deb', 'appimage']; const validTargets = ['all', 'deb', 'appimage'];
if (validTargets.includes(options.targets)) { if (validTargets.includes(options.targets)) {
tauriConf.tauri.bundle.targets = tauriConf.tauri.bundle.targets = options.targets === 'all' ? ['deb', 'appimage'] : [options.targets];
options.targets === 'all' ? ['deb', 'appimage'] : [options.targets];
} else { } else {
logger.warn( logger.warn(`✼ The target must be one of ${validTargets.join(', ')}, the default 'deb' will be used.`);
`✼ The target must be one of ${validTargets.join(', ')}, the default 'deb' will be used.`,
);
} }
} }
@@ -160,10 +169,7 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
// 需要判断图标格式默认只支持ico和png两种 // 需要判断图标格式默认只支持ico和png两种
let iconExt = path.extname(systemTrayIcon).toLowerCase(); let iconExt = path.extname(systemTrayIcon).toLowerCase();
if (iconExt == '.png' || iconExt == '.ico') { if (iconExt == '.png' || iconExt == '.ico') {
const trayIcoPath = path.join( const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`);
npmDirectory,
`src-tauri/png/${name.toLowerCase()}${iconExt}`,
);
trayIconPath = `png/${name.toLowerCase()}${iconExt}`; trayIconPath = `png/${name.toLowerCase()}${iconExt}`;
await fsExtra.copy(systemTrayIcon, trayIcoPath); await fsExtra.copy(systemTrayIcon, trayIcoPath);
} else { } else {
@@ -181,12 +187,12 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
// inject js or css files // inject js or css files
if (inject?.length > 0) { if (inject?.length > 0) {
if (!inject.every(item => item.endsWith('.css') || item.endsWith('.js'))) { if (!inject.every(item => item.endsWith('.css') || item.endsWith('.js'))) {
logger.error("The injected file must be in either CSS or JS format."); logger.error('The injected file must be in either CSS or JS format.');
return; return;
} }
const files = inject.map(relativePath => path.join(process.cwd(), relativePath)); const files = inject.map(relativePath => path.join(process.cwd(), relativePath));
tauriConf.pake.inject = files; tauriConf.pake.inject = files;
combineFiles(files, path.join(npmDirectory, `src-tauri/src/inject/_INJECT_.js`)); await combineFiles(files, path.join(npmDirectory, `src-tauri/src/inject/_INJECT_.js`));
} }
// Save config file. // Save config file.

3
bin/types.ts vendored
View File

@@ -50,6 +50,9 @@ export interface PakeCliOptions {
/** 需要注入页面的外部脚本 */ /** 需要注入页面的外部脚本 */
inject: string[]; inject: string[];
/* the domain that can use ipc or tauri javascript sdk */
safeDomain: string[];
} }
export interface PakeAppOptions extends PakeCliOptions { export interface PakeAppOptions extends PakeCliOptions {