file.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. interface NodeFileOptions extends BlobPropertyBag {
  2. lastModified?: number;
  3. }
  4. // Create a minimal File polyfill for Node.js environments
  5. // This is needed because Zod 4.x checks for File API on initialization
  6. class NodeFile {
  7. readonly name: string;
  8. readonly lastModified: number;
  9. readonly webkitRelativePath: string;
  10. readonly size: number;
  11. readonly type: string;
  12. constructor(fileBits: BlobPart[], fileName: string, options: NodeFileOptions = {}) {
  13. this.name = fileName;
  14. this.webkitRelativePath = "";
  15. this.lastModified = typeof options.lastModified === "number" ? options.lastModified : Date.now();
  16. this.size = 0;
  17. this.type = options.type || "";
  18. }
  19. // Stub methods to satisfy interface
  20. arrayBuffer(): Promise<ArrayBuffer> {
  21. return Promise.resolve(new ArrayBuffer(0));
  22. }
  23. bytes(): Promise<Uint8Array> {
  24. return Promise.resolve(new Uint8Array(0));
  25. }
  26. slice(): Blob {
  27. return new Blob([]);
  28. }
  29. stream(): ReadableStream {
  30. return new ReadableStream();
  31. }
  32. text(): Promise<string> {
  33. return Promise.resolve("");
  34. }
  35. }
  36. export function ensureFilePolyfill(): void {
  37. // Apply polyfill if File is not already defined
  38. if (typeof globalThis !== "undefined" && typeof globalThis.File === "undefined") {
  39. // Use type assertion to bypass strict type checking
  40. // This polyfill only needs to satisfy Zod's runtime checks, not full File API
  41. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  42. (globalThis as any).File = NodeFile;
  43. }
  44. }
  45. // Execute immediately to ensure File is available before Zod initialization
  46. ensureFilePolyfill();