@metamask/snaps-jest
Version:
A Jest preset for end-to-end testing MetaMask Snaps, including a Jest environment, and a set of Jest matchers
1 lines • 4.75 kB
Source Map (JSON)
{"version":3,"file":"server.cjs","sourceRoot":"","sources":["../../src/internals/server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,qDAIoC;AACpC,2CAAqD;AACrD,4DAA2D;AAC3D,2BAAoC;AAEpC,+BAA8C;AAE9C,yCAAsC;AAQtC;;;;;;;;;GASG;AACH,KAAK,UAAU,UAAU,CAAC,IAAY;IACpC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACxD,CAAC;IAED,IAAI,CAAC,CAAC,MAAM,IAAA,kBAAW,EAAC,IAAI,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CAAC,mBAAmB,IAAI,uBAAuB,CAAC,CAAC;IAClE,CAAC;IAED,MAAM,YAAY,GAAG,IAAA,cAAW,EAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC;IAC7D,MAAM,QAAQ,GAAiB,MAAM,aAAE;SACpC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;SAC9B,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAEpB,IAAA,2BAAoB,EAAC,QAAQ,CAAC,CAAC;IAC/B,MAAM,QAAQ,GAAG,IAAA,cAAW,EAAC,IAAI,EAAE,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAE1E,IAAI,CAAC,CAAC,MAAM,IAAA,aAAM,EAAC,QAAQ,CAAC,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CACb,SAAS,QAAQ,wEAAwE,CAC1F,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACI,KAAK,UAAU,WAAW,CAAC,OAAsB;IACtD,MAAM,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAE/B,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,mBAAU,EAAE,QAAQ,CAAC,CAAC;IACrD,MAAM,GAAG,GAAG,IAAA,iBAAO,GAAE,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,IAAA,gBAAa,EAAC,IAAA,cAAW,EAAC,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;AA5BD,kCA4BC","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"]}