cuaHelperInstaller.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {
  2. createCuaHelperInstaller,
  3. defaultCuaHelperVerifierDependencies,
  4. type CuaHelperInstaller,
  5. type CuaHelperInstallerOptions,
  6. } from "@zcode/zcode-cua/broker/server";
  7. type CuaHelperInstallerFactory = (options: CuaHelperInstallerOptions) => CuaHelperInstaller;
  8. /**
  9. * macOS `lipo -archs` 对 Intel 二进制返回 `x86_64`,而 Node 的运行时架构名是 `x64`。
  10. * 两者表示同一架构;如果直接比较字符串,合法的 Intel Helper 会被误判为不可用。
  11. */
  12. export function normalizeCuaHelperArch(rawArch: string): string {
  13. switch (rawArch.trim().toLowerCase()) {
  14. case "amd64":
  15. case "x86_64":
  16. case "x64":
  17. return "x64";
  18. case "aarch64":
  19. case "arm64":
  20. return "arm64";
  21. default:
  22. return rawArch.trim();
  23. }
  24. }
  25. export function normalizeCuaHelperArchs(archs: readonly string[]): string[] {
  26. return [...new Set(archs.map(normalizeCuaHelperArch).filter(Boolean))];
  27. }
  28. export function canonicalizeCuaHelperInstallerOptions(
  29. options: CuaHelperInstallerOptions,
  30. ): CuaHelperInstallerOptions {
  31. const readExecutableArchs =
  32. options.dependencies?.readExecutableArchs ??
  33. defaultCuaHelperVerifierDependencies.readExecutableArchs;
  34. return {
  35. ...options,
  36. dependencies: {
  37. ...options.dependencies,
  38. readExecutableArchs: async (executablePath) =>
  39. normalizeCuaHelperArchs(await readExecutableArchs(executablePath)),
  40. },
  41. };
  42. }
  43. export function createCanonicalCuaHelperInstaller(
  44. options: CuaHelperInstallerOptions,
  45. createInstaller: CuaHelperInstallerFactory = createCuaHelperInstaller,
  46. ): CuaHelperInstaller {
  47. return createInstaller(canonicalizeCuaHelperInstallerOptions(options));
  48. }