UNPKG

@metamask/snaps-simulation

Version:

A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment

1 lines 5.24 kB
{"version":3,"file":"files.cjs","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":";;;;;;AACA,mDAA4D;AAE5D,uDAA+E;AAC/E,2CAAgD;AAChD,0CAA6C;AAC7C,yDAAwB;AACxB,+BAAkD;AAIlD;;;;;;;GAOG;AACI,KAAK,UAAU,WAAW,CAC/B,KAAoB,EACpB,IAAY,EACZ,WAAkC,iCAAqB,CAAC,MAAM;IAE9D,MAAM,cAAc,GAAG,IAAA,+BAAiB,EAAC,IAAI,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,KAAK;SACjB,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC;QAC7C,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAEvB,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,MAAM,IAAA,iCAAmB,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AACrD,CAAC;AAfD,kCAeC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,SAAiB;IAC9C,OAAO,cAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,0BAA0B,CAAC;AAC/D,CAAC;AAFD,wCAEC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,WAAW,CAAC,IAAyB;IACzD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAA,eAAI,EAAC,IAAA,cAAO,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;QAC1D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,IAAI,CAAC,MAAM,CAAC;AACrB,CAAC;AAPD,kCAOC;AAED;;;;;;;;;;;;;;;;GAgBG;AACI,KAAK,UAAU,eAAe,CACnC,IAAyB,EACzB,EAAE,QAAQ,EAAE,WAAW,KAAkB,EAAE;IAE3C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,MAAM,GAAG,MAAM,IAAA,mBAAQ,EAAC,IAAA,cAAO,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;QAE5D,OAAO;YACL,IAAI,EAAE,QAAQ,IAAI,IAAA,eAAQ,EAAC,IAAI,CAAC;YAChC,IAAI,EAAE,MAAM,CAAC,UAAU;YACvB,WAAW,EAAE,WAAW,IAAI,cAAc,CAAC,IAAA,cAAO,EAAC,IAAI,CAAC,CAAC;YACzD,QAAQ,EAAE,IAAA,qBAAa,EAAC,MAAM,CAAC;SAChC,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,QAAQ,IAAI,EAAE;QACpB,IAAI,EAAE,IAAI,CAAC,MAAM;QACjB,WAAW,EAAE,WAAW,IAAI,0BAA0B;QACtD,QAAQ,EAAE,IAAA,qBAAa,EAAC,IAAI,CAAC;KAC9B,CAAC;AACJ,CAAC;AArBD,0CAqBC","sourcesContent":["import type { File } from '@metamask/snaps-sdk';\nimport { AuxiliaryFileEncoding } from '@metamask/snaps-sdk';\nimport type { VirtualFile } from '@metamask/snaps-utils';\nimport { encodeAuxiliaryFile, normalizeRelative } from '@metamask/snaps-utils';\nimport { bytesToBase64 } from '@metamask/utils';\nimport { readFile, stat } from 'fs/promises';\nimport mime from 'mime';\nimport { basename, extname, resolve } from 'path';\n\nimport type { FileOptions } from './types';\n\n/**\n * Get a statically defined Snap file from an array of files.\n *\n * @param files - The Snap files.\n * @param path - The file path.\n * @param encoding - The requested file encoding.\n * @returns The file in the requested encoding if found, otherwise null.\n */\nexport async function getSnapFile(\n files: VirtualFile[],\n path: string,\n encoding: AuxiliaryFileEncoding = AuxiliaryFileEncoding.Base64,\n) {\n const normalizedPath = normalizeRelative(path);\n const base64 = files\n .find((file) => file.path === normalizedPath)\n ?.toString('base64');\n\n if (!base64) {\n return null;\n }\n\n return await encodeAuxiliaryFile(base64, encoding);\n}\n\n/**\n * Get the content type of a file based on its extension.\n *\n * @param extension - The file extension.\n * @returns The content type of the file. If the content type cannot be inferred\n * from the extension, `application/octet-stream` is returned.\n */\nexport function getContentType(extension: string) {\n return mime.getType(extension) ?? 'application/octet-stream';\n}\n\n/**\n * Get the size of a file, from a file path or a `Uint8Array`.\n *\n * @param file - The file to get the size of. This can be a path to a file or a\n * `Uint8Array` containing the file contents. If this is a path, the file is\n * resolved relative to the current working directory.\n * @returns The size of the file in bytes.\n */\nexport async function getFileSize(file: string | Uint8Array) {\n if (typeof file === 'string') {\n const { size } = await stat(resolve(process.cwd(), file));\n return size;\n }\n\n return file.length;\n}\n\n/**\n * Get a file object to upload, from a file path or a `Uint8Array`, with an\n * optional file name and content type.\n *\n * @param file - The file to upload. This can be a path to a file or a\n * `Uint8Array` containing the file contents. If this is a path, the file is\n * resolved relative to the current working directory.\n * @param options - The file options.\n * @param options.fileName - The name of the file. By default, this is\n * inferred from the file path if it's a path, and defaults to an empty string\n * if it's a `Uint8Array`.\n * @param options.contentType - The content type of the file. By default, this\n * is inferred from the file name if it's a path, and defaults to\n * `application/octet-stream` if it's a `Uint8Array` or the content type\n * cannot be inferred from the file name.\n * @returns The file object to upload.\n */\nexport async function getFileToUpload(\n file: string | Uint8Array,\n { fileName, contentType }: FileOptions = {},\n): Promise<File> {\n if (typeof file === 'string') {\n const buffer = await readFile(resolve(process.cwd(), file));\n\n return {\n name: fileName ?? basename(file),\n size: buffer.byteLength,\n contentType: contentType ?? getContentType(extname(file)),\n contents: bytesToBase64(buffer),\n };\n }\n\n return {\n name: fileName ?? '',\n size: file.length,\n contentType: contentType ?? 'application/octet-stream',\n contents: bytesToBase64(file),\n };\n}\n"]}