Repository: electron-userland/electron-builder
File: packages/app-builder-lib/src/targets/AppxCapabilities.ts (TypeScript)

Code before:

class MobileCapability extends NSBaseCapability{

  constructor(name:string) {
    super("mobile", "http://schemas.microsoft.com/appx/manifest/mobile/windows10", "Capability", name);
  }
}

// class IOTCapability extends NSBaseCapability{
//
//   constructor(name:string) {
//     super('iot', '?????', 'Capability', name);
//   }
// }

class ResCapCapability extends NSBaseCapability{

  constructor(name:string) {
    super("rescap", "http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities", "Capability", name);
  }
}

// order matters
// see https://learn.microsoft.com/en-us/uwp/schemas/appxpackage/uapmanifestschema/element-capabilities

export const CAPABILITIES = [


  new CommonCapability("internetClient"),
  new CommonCapability("internetClientServer"),
  new CommonCapability("privateNetworkClientServer"),
  new CommonCapability("codeGeneration"), // ???
  new CommonCapability("allJoyn"), // ???
  new CommonCapability("backgroundMediaPlayback"),
  new CommonCapability("remoteSystem"),
  new CommonCapability("spatialPerception"),
  new CommonCapability("userDataTasks"),
  new CommonCapability("userNotificationListener"),


  new UAPCapability("musicLibrary"),
  new UAPCapability("picturesLibrary"),
  new UAPCapability("videosLibrary"),
  new UAPCapability("removableStorage"),
  new UAPCapability("appointments"),
  new UAPCapability("contacts"),
  new UAPCapability("phoneCall"),
  new UAPCapability("phoneCallHistoryPublic"),
  new UAPCapability("userAccountInformation"),
  new UAPCapability("voipCall"),
  new UAPCapability("objects3D"),

Review comment (security):
This seems wildly overcomplicated/over-engineered to me. Instead of classes for each object that only are using `super`. Why not just use a `Map<CompatibilityType, string>`?

Here's the rewritten code of what I'm thinking is more optimized and it dramatically reduces the number of classes/super being used.
```
export interface Capability {
  readonly nsAlias: string | null;
  readonly nsURI: string | null;
  readonly name: string;
  toXMLString(): string;
}

type CapabilityConfig = {
  nsAlias: string | null;
  nsURI: string | null;
  elementName: string;
};

class AppxCapability implements Capability {
  constructor(
    public readonly nsAlias: string | null,
    public readonly nsURI: string | null,
    private readonly elementName: string,
    public readonly name: string
  ) {}

  toXMLString(): string {
    const tagName = this.nsAlias ? `${this.nsAlias}:${this.elementName}` : this.elementName;
    return `<${tagName} Name="${this.name}"/>`;
  }
}

// Capability type configurations
const CAPABILITY_TYPES = {
  common: {
    nsAlias: null,
    nsURI: "http://schemas.microsoft.com/appx/manifest/foundation/windows10",
    elementName: "Capability",
  },
  device: {
    nsAlias: null,
    nsURI: "http://schemas.microsoft.com/appx/manifest/foundation/windows10",
    elementName: "DeviceCapability",
  },
  uap: {
    nsAlias: "uap",
    nsURI: "http://schemas.microsoft.com/appx/manifest/uap/windows10",
    elementName: "Capability",
  },
  uap6: {
    nsAlias: "uap6",
    nsURI: "http://schemas.microsoft.com/appx/manifest/uap/windows10/6",
    elementName: "Capability",
  },
  uap7: {
    nsAlias: "uap7",
    nsURI: "http://schemas.microsoft.com/appx/manifest/uap/windows10/7",
    elementName: "Capability",
  },
  uap11: {
    nsAlias: "uap11",
    nsURI: "http://schemas.microsoft.com/appx/manifest/uap/windows10/11",
    elementName: "Capability",
  },
  mobile: {
    nsAlias: "mobile",
    nsURI: "http://schemas.microsoft.com/appx/manifest/mobile/windows10",
    elementName: "Capability",
  },
  rescap: {
    nsAlias: "rescap",
    nsURI: "http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities",
    elementName: "Capability",
  },
} as const;

type CapabilityType = keyof typeof CAPABILITY_TYPES;

// Map of capability types to their capability names (grouped by type)
const CAPABILITY_MAP = new Map<CapabilityType, string[]>([
  // Common capabilities
  ["common", [
    "internetClient",
    "internetClientServer",
    "privateNetworkClientServer",
    "codeGeneration",
    "allJoyn",
    "backgroundMediaPlayback",
    "remoteSystem",
    "spatialPerception",
    "userDataTasks",
    "userNotificationListener",
  ]],

  // UAP capabilities
  ["uap", [
    "musicLibrary",
    "picturesLibrary",
    "videosLibrary",
    "removableStorage",
    "appointments",
    "contacts",
    "phoneCall",
    "phoneCallHistoryPublic",
    "userAccountInformation",
    "voipCall",
    "objects3D",
    "chat",
    "blockedChatMessages",
    "enterpriseAuthentication",
    "sharedUserCertificates",
    "documentsLibrary",
  ]],

  // Device capabilities
  ["device", [
    "location",
    "microphone",
    "webcam",
    "proximity",
    "pointOfService",
    "wiFiControl",
    "radios",
    "optical",
    "activity",
    "humanPresence",
    "serialcommunication",
    "gazeInput",
    "lowLevel",
    "packageQuery",
  ]],

  // Mobile capabilities
  ["mobile", [
    "recordedCallsFolder",
  ]],

  // Restricted capabilities
  ["rescap", [
    "enterpriseDataPolicy",
    "appCaptureSettings",
    "cellularDeviceControl",
    "cellularDeviceIdentity",
    "cellularMessaging",
    "deviceUnlock",
    "dualSimTiles",
    "enterpriseDeviceLockdown",
    "inputInjectionBrokered",
    "inputObservation",
    "inputSuppression",
    "networkingVpnProvider",
    "packageManagement",
    "screenDuplication",
    "userPrincipalName",
    "walletSystem",
    "locationHistory",
    "confirmAppClose",
    "phoneCallHistory",
    "appointmentsSystem",
    "chatSystem",
    "contactsSystem",
    "email",
    "emailSystem",
    "phoneCallHistorySystem",
    "smsSend",
    "userDataSystem",
    "previewStore",
    "firstSignInSettings",
    "teamEditionExperience",
    "remotePassportAuthentication",
    "previewUiComposition",
    "secureAssessment",
    "networkConnectionManagerProvisioning",
    "networkDataPlanProvisioning",
    "slapiQueryLicenseValue",
    "extendedBackgroundTaskTime",
    "extendedExecutionBackgroundAudio",
    "extendedExecutionCritical",
    "extendedExecutionUnconstrained",
    "deviceManagementDmAccount",
    "deviceManagementFoundation",
    "deviceManagementWapSecurityPolicies",
    "deviceManagementEmailAccount",
    "packagePolicySystem",
    "gameList",
    "xboxAccessoryManagement",
    "cortanaSpeechAccessory",
    "accessoryManager",
    "interopServices",
    "inputForegroundObservation",
    "oemDeployment",
    "oemPublicDirectory",
    "appLicensing",
    "locationSystem",
    "userDataAccountsProvider",
    "previewPenWorkspace",
    "secondaryAuthenticationFactor",
    "storeLicenseManagement",
    "userSystemId",
    "targetedContent",
    "uiAutomation",
    "gameBarServices",
    "appCaptureServices",
    "appBroadcastServices",
    "audioDeviceConfiguration",
    "backgroundMediaRecording",
    "previewInkWorkspace",
    "startScreenManagement",
    "cortanaPermissions",
    "allAppMods",
    "expandedResources",
    "protectedApp",
    "gameMonitor",
    "appDiagnostics",
    "devicePortalProvider",
    "enterpriseCloudSSO",
    "backgroundVoIP",
    "oneProcessVoIP",
    "developmentModeNetwork",
    "broadFileSystemAccess",
    "smbios",
    "runFullTrust",
    "allowElevation",
    "teamEditionDeviceCredential",
    "teamEditionView",
    "cameraProcessingExtension",
    "networkDataUsageManagement",
    "phoneLineTransportManagement",
    "unvirtualizedResources",
    "modifiableApp",
    "packageWriteRedirectionCompatibilityShim",
    "customInstallActions",
    "packagedServices",
    "localSystemServices",
    "backgroundSpatialPerception",
    "uiAccess",
  ]],

  // UAP6 capabilities
  ["uap6", [
    "graphicsCapture",
  ]],

  // UAP7 capabilities
  ["uap7", [
    "globalMediaControl",
  ]],

  // UAP11 capabilities
  ["uap11", [
    "graphicsCaptureWithoutBorder",
    "graphicsCaptureProgrammatic",
  ]],
]);

// Factory function to create capabilities
function createCapability(type: CapabilityType, name: string): Capability {
  const config = CAPABILITY_TYPES[type];
  return new AppxCapability(config.nsAlias, config.nsURI, config.elementName, name);
}

// Export ordered list of all capabilities (order matters per Microsoft docs)
export const CAPABILITIES: Capability[] = Array.from(CAPABILITY_MAP.entries()).flatMap(
  ([type, names]) => names.map(name => createCapability(type, name))
);
```
```

Code after:
      "internetClient",
      "internetClientServer",
      "privateNetworkClientServer",
      "codeGeneration",
      "allJoyn",
      "backgroundMediaPlayback",
      "remoteSystem",
      "spatialPerception",
      "userDataTasks",
      "userNotificationListener",
    ],
  ],

  // UAP capabilities
  [
    "uap",
    [
      "musicLibrary",
      "picturesLibrary",
      "videosLibrary",
      "removableStorage",
      "appointments",
      "contacts",
      "phoneCall",
      "phoneCallHistoryPublic",
      "userAccountInformation",
      "voipCall",
      "objects3D",
      "chat",
      "blockedChatMessages",
      "enterpriseAuthentication",
      "sharedUserCertificates",
      "documentsLibrary",
    ],
  ],

  // Mobile capabilities
  ["mobile", ["recordedCallsFolder"]],

  // Restricted capabilities
  [
    "rescap",
    [
      "enterpriseDataPolicy",
      "appCaptureSettings",
      "cellularDeviceControl",
      "cellularDeviceIdentity",
      "cellularMessaging",
      "deviceUnlock",
      "dualSimTiles",
      "enterpriseDeviceLockdown",