windows-trash-execution.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Recoverable Windows delete metadata carried in-process from the permission
  3. * checker to the local Bash implementation. Only literal trash targets cross
  4. * this seam; the runtime owns the launcher, script path and environment.
  5. */
  6. const WINDOWS_TRASH_EXECUTION = Symbol.for('@mavis/permission/windows-trash-execution');
  7. export interface WindowsTrashExecution {
  8. readonly targets: readonly string[];
  9. }
  10. type WindowsTrashExecutionCarrier = {
  11. [WINDOWS_TRASH_EXECUTION]?: WindowsTrashExecution;
  12. };
  13. export function withWindowsTrashExecution<T extends Record<string, unknown>>(
  14. input: T,
  15. targets: readonly string[],
  16. ): T {
  17. if (targets.length === 0 || !targets.every((target) => typeof target === 'string' && target)) {
  18. throw new TypeError('Windows trash execution requires at least one literal target.');
  19. }
  20. const effectiveInput = { ...input } as T & WindowsTrashExecutionCarrier;
  21. Object.defineProperty(effectiveInput, WINDOWS_TRASH_EXECUTION, {
  22. value: Object.freeze({ targets: Object.freeze([...targets]) }),
  23. enumerable: true,
  24. configurable: false,
  25. writable: false,
  26. });
  27. return effectiveInput;
  28. }
  29. export function readWindowsTrashExecution(input: unknown): WindowsTrashExecution | undefined {
  30. if (!input || typeof input !== 'object' || Array.isArray(input)) return undefined;
  31. const value = (input as WindowsTrashExecutionCarrier)[WINDOWS_TRASH_EXECUTION];
  32. if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined;
  33. if (
  34. !Array.isArray(value.targets) ||
  35. value.targets.length === 0 ||
  36. !value.targets.every((target) => typeof target === 'string' && target)
  37. ) {
  38. return undefined;
  39. }
  40. return value;
  41. }