UNPKG

@metamask/snaps-simulation

Version:

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

94 lines 3.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFileToUpload = exports.getFileSize = exports.getContentType = exports.getSnapFile = void 0; const snaps_sdk_1 = require("@metamask/snaps-sdk"); const snaps_utils_1 = require("@metamask/snaps-utils"); const utils_1 = require("@metamask/utils"); const promises_1 = require("fs/promises"); const mime_1 = __importDefault(require("mime/index.js")); const path_1 = require("path"); /** * Get a statically defined Snap file from an array of files. * * @param files - The Snap files. * @param path - The file path. * @param encoding - The requested file encoding. * @returns The file in the requested encoding if found, otherwise null. */ async function getSnapFile(files, path, encoding = snaps_sdk_1.AuxiliaryFileEncoding.Base64) { const normalizedPath = (0, snaps_utils_1.normalizeRelative)(path); const base64 = files .find((file) => file.path === normalizedPath) ?.toString('base64'); if (!base64) { return null; } return await (0, snaps_utils_1.encodeAuxiliaryFile)(base64, encoding); } exports.getSnapFile = getSnapFile; /** * Get the content type of a file based on its extension. * * @param extension - The file extension. * @returns The content type of the file. If the content type cannot be inferred * from the extension, `application/octet-stream` is returned. */ function getContentType(extension) { return mime_1.default.getType(extension) ?? 'application/octet-stream'; } exports.getContentType = getContentType; /** * Get the size of a file, from a file path or a `Uint8Array`. * * @param file - The file to get the size of. This can be a path to a file or a * `Uint8Array` containing the file contents. If this is a path, the file is * resolved relative to the current working directory. * @returns The size of the file in bytes. */ async function getFileSize(file) { if (typeof file === 'string') { const { size } = await (0, promises_1.stat)((0, path_1.resolve)(process.cwd(), file)); return size; } return file.length; } exports.getFileSize = getFileSize; /** * Get a file object to upload, from a file path or a `Uint8Array`, with an * optional file name and content type. * * @param file - The file to upload. This can be a path to a file or a * `Uint8Array` containing the file contents. If this is a path, the file is * resolved relative to the current working directory. * @param options - The file options. * @param options.fileName - The name of the file. By default, this is * inferred from the file path if it's a path, and defaults to an empty string * if it's a `Uint8Array`. * @param options.contentType - The content type of the file. By default, this * is inferred from the file name if it's a path, and defaults to * `application/octet-stream` if it's a `Uint8Array` or the content type * cannot be inferred from the file name. * @returns The file object to upload. */ async function getFileToUpload(file, { fileName, contentType } = {}) { if (typeof file === 'string') { const buffer = await (0, promises_1.readFile)((0, path_1.resolve)(process.cwd(), file)); return { name: fileName ?? (0, path_1.basename)(file), size: buffer.byteLength, contentType: contentType ?? getContentType((0, path_1.extname)(file)), contents: (0, utils_1.bytesToBase64)(buffer), }; } return { name: fileName ?? '', size: file.length, contentType: contentType ?? 'application/octet-stream', contents: (0, utils_1.bytesToBase64)(file), }; } exports.getFileToUpload = getFileToUpload; //# sourceMappingURL=files.cjs.map