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AM,CAAC,KAAK,UAAU,WAAW,CAAC,OAAsB;IACtD,MAAM,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAE/B,MAAM,GAAG,GAAG,kBAAkB,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IACrD,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;IAEtB,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QACnC,QAAQ,CAAC,MAAM,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAC;QACpD,QAAQ,CAAC,MAAM,CAAC,kCAAkC,EAAE,MAAM,CAAC,CAAC;QAC5D,QAAQ,CAAC,MAAM,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAC;QAChE,QAAQ,CAAC,MAAM,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAC;QAEhE,IAAI,EAAE,CAAC;IACT,CAAC,CAAC,CAAC;IAEH,GAAG,CAAC,GAAG,CAAC,aAAa,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEjE,OAAO,MAAM,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACnD,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;YAChD,IAAI,KAAK,EAAE,CAAC;gBACV,GAAG,CAAC,KAAK,CAAC,CAAC;gBACX,MAAM,CAAC,KAAK,CAAC,CAAC;gBACd,OAAO;YACT,CAAC;YAED,OAAO,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { SnapManifest } from '@metamask/snaps-utils';\nimport {\n  assertIsSnapManifest,\n  isDirectory,\n  isFile,\n} from '@metamask/snaps-utils/node';\nimport { createModuleLogger } from '@metamask/utils';\nimport express, { static as expressStatic } from 'express';\nimport { promises as fs } from 'fs';\nimport type { Server } from 'http';\nimport { resolve as pathResolve } from 'path';\n\nimport { rootLogger } from './logger';\nimport type { SnapsEnvironmentOptions } from '../options';\n\nexport type ServerOptions = Required<\n  // We need a double `Required` for the type to be inferred correctly.\n  Required<SnapsEnvironmentOptions>['server']\n>;\n\n/**\n * Check that:\n *\n * - The root directory exists.\n * - The root directory contains a `snap.manifest.json` file.\n * - The file path in the manifest exists.\n *\n * @param root - The root directory.\n * @throws If any of the checks fail.\n */\nasync function assertRoot(root: string) {\n  if (!root) {\n    throw new Error('You must specify a root directory.');\n  }\n\n  if (!(await isDirectory(root, false))) {\n    throw new Error(`Root directory \"${root}\" is not a directory.`);\n  }\n\n  const manifestPath = pathResolve(root, 'snap.manifest.json');\n  const manifest: SnapManifest = await fs\n    .readFile(manifestPath, 'utf8')\n    .then(JSON.parse);\n\n  assertIsSnapManifest(manifest);\n  const filePath = pathResolve(root, manifest.source.location.npm.filePath);\n\n  if (!(await isFile(filePath))) {\n    throw new Error(\n      `File \"${filePath}\" does not exist, or is not a file. Did you forget to build your snap?`,\n    );\n  }\n}\n\n/**\n * Start an HTTP server on `localhost` with a random port. This is used to serve\n * the static files for the environment.\n *\n * @param options - The options to use.\n * @param options.port - The port to use for the server.\n * @param options.root - The root directory to serve from the server.\n * @returns The HTTP server.\n */\nexport async function startServer(options: ServerOptions) {\n  await assertRoot(options.root);\n\n  const log = createModuleLogger(rootLogger, 'server');\n  const app = express();\n\n  app.use((_request, response, next) => {\n    response.header('Access-Control-Allow-Origin', '*');\n    response.header('Access-Control-Allow-Credentials', 'true');\n    response.header('Access-Control-Allow-Methods', 'GET, OPTIONS');\n    response.header('Access-Control-Allow-Headers', 'Content-Type');\n\n    next();\n  });\n\n  app.use(expressStatic(pathResolve(process.cwd(), options.root)));\n\n  return await new Promise<Server>((resolve, reject) => {\n    const server = app.listen(options.port, (error) => {\n      if (error) {\n        log(error);\n        reject(error);\n        return;\n      }\n\n      resolve(server);\n    });\n  });\n}\n"]}