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passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n//               similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n *   interface DataMap {\n *     // `file.data.name` is typed as `string`\n *     name: string\n *   }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-object-type\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n  | string\n  | Uint8Array\n  | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n  value: Value;\n  path?: string;\n  data?: Data;\n  result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n  constructor(value?: Compatible<Result>) {\n    let options: Options | undefined;\n    if (typeof value === 'string' || value instanceof Uint8Array) {\n      options = { value };\n    } else {\n      options = value;\n    }\n\n    this.value = options?.value ?? '';\n    // This situations happens when there's no .result used,\n    // we expect the file to have default generic in that situation:\n    // VirtualFile<unknown> which will handle undefined properly\n    //\n    // While not 100% type safe, it'll be way less frustrating to work with.\n    // The alternative would be to have VirtualFile.result be Result | undefined\n    // and that would result in needing to branch out and check in all situations.\n    //\n    // In short, optimizing for most common use case.\n    this.result = options?.result ?? (undefined as any);\n    this.data = options?.data ?? {};\n    this.path = options?.path ?? '/';\n  }\n\n  value: Value;\n\n  result: Result;\n\n  data: Data;\n\n  path: string;\n\n  get size() {\n    return typeof this.value === 'string'\n      ? this.value.length\n      : this.value.byteLength;\n  }\n\n  toString(encoding?: string) {\n    if (typeof this.value === 'string') {\n      assert(encoding === undefined, 'Tried to encode string.');\n      return this.value;\n    } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n      return bytesToHex(this.value);\n    } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n      // For large files, this is quite slow, instead use `encodeBase64()`\n      // TODO: Use @metamask/utils for this\n      return base64.encode(this.value);\n    }\n    const decoder = new TextDecoder(encoding);\n    return decoder.decode(this.value);\n  }\n\n  clone() {\n    const vfile = new VirtualFile<Result>();\n    if (typeof this.value === 'string') {\n      vfile.value = this.value;\n    } else {\n      // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n      vfile.value = this.value.slice(0);\n    }\n    vfile.result = deepClone(this.result);\n    vfile.data = deepClone(this.data);\n    vfile.path = this.path;\n    return vfile;\n  }\n}\n"]}