@metamask/snaps-cli 7.0.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/README.md +1 -0
  3. package/dist/builders.cjs +5 -0
  4. package/dist/builders.cjs.map +1 -1
  5. package/dist/builders.d.cts +5 -0
  6. package/dist/builders.d.cts.map +1 -1
  7. package/dist/builders.d.mts +5 -0
  8. package/dist/builders.d.mts.map +1 -1
  9. package/dist/builders.mjs +5 -0
  10. package/dist/builders.mjs.map +1 -1
  11. package/dist/commands/build/build.cjs +9 -5
  12. package/dist/commands/build/build.cjs.map +1 -1
  13. package/dist/commands/build/build.d.cts +8 -0
  14. package/dist/commands/build/build.d.cts.map +1 -1
  15. package/dist/commands/build/build.d.mts +8 -0
  16. package/dist/commands/build/build.d.mts.map +1 -1
  17. package/dist/commands/build/build.mjs +7 -3
  18. package/dist/commands/build/build.mjs.map +1 -1
  19. package/dist/commands/build/index.cjs +3 -0
  20. package/dist/commands/build/index.cjs.map +1 -1
  21. package/dist/commands/build/index.d.cts +1 -0
  22. package/dist/commands/build/index.d.cts.map +1 -1
  23. package/dist/commands/build/index.d.mts +1 -0
  24. package/dist/commands/build/index.d.mts.map +1 -1
  25. package/dist/commands/build/index.mjs +1 -0
  26. package/dist/commands/build/index.mjs.map +1 -1
  27. package/dist/commands/index.cjs +2 -0
  28. package/dist/commands/index.cjs.map +1 -1
  29. package/dist/commands/index.d.cts.map +1 -1
  30. package/dist/commands/index.d.mts.map +1 -1
  31. package/dist/commands/index.mjs +2 -0
  32. package/dist/commands/index.mjs.map +1 -1
  33. package/dist/commands/sandbox/index.cjs +17 -0
  34. package/dist/commands/sandbox/index.cjs.map +1 -0
  35. package/dist/commands/sandbox/index.d.cts +10 -0
  36. package/dist/commands/sandbox/index.d.cts.map +1 -0
  37. package/dist/commands/sandbox/index.d.mts +10 -0
  38. package/dist/commands/sandbox/index.d.mts.map +1 -0
  39. package/dist/commands/sandbox/index.mjs +12 -0
  40. package/dist/commands/sandbox/index.mjs.map +1 -0
  41. package/dist/commands/sandbox/sandbox.cjs +33 -0
  42. package/dist/commands/sandbox/sandbox.cjs.map +1 -0
  43. package/dist/commands/sandbox/sandbox.d.cts +14 -0
  44. package/dist/commands/sandbox/sandbox.d.cts.map +1 -0
  45. package/dist/commands/sandbox/sandbox.d.mts +14 -0
  46. package/dist/commands/sandbox/sandbox.d.mts.map +1 -0
  47. package/dist/commands/sandbox/sandbox.mjs +29 -0
  48. package/dist/commands/sandbox/sandbox.mjs.map +1 -0
  49. package/dist/commands/sandbox/server.cjs +27 -0
  50. package/dist/commands/sandbox/server.cjs.map +1 -0
  51. package/dist/commands/sandbox/server.d.cts +14 -0
  52. package/dist/commands/sandbox/server.d.cts.map +1 -0
  53. package/dist/commands/sandbox/server.d.mts +14 -0
  54. package/dist/commands/sandbox/server.d.mts.map +1 -0
  55. package/dist/commands/sandbox/server.mjs +25 -0
  56. package/dist/commands/sandbox/server.mjs.map +1 -0
  57. package/dist/webpack/server.cjs +83 -72
  58. package/dist/webpack/server.cjs.map +1 -1
  59. package/dist/webpack/server.d.cts +6 -1
  60. package/dist/webpack/server.d.cts.map +1 -1
  61. package/dist/webpack/server.d.mts +6 -1
  62. package/dist/webpack/server.d.mts.map +1 -1
  63. package/dist/webpack/server.mjs +63 -72
  64. package/dist/webpack/server.mjs.map +1 -1
  65. package/package.json +5 -4
@@ -4,11 +4,10 @@ function $importDefault(module) {
4
4
  }
5
5
  return module;
6
6
  }
7
- import { logError, NpmSnapFileNames, readJsonFile } from "@metamask/snaps-utils/node";
8
- import { createServer } from "http";
7
+ import { NpmSnapFileNames, readJsonFile } from "@metamask/snaps-utils/node";
8
+ import $express, { static as expressStatic } from "express/index.js";
9
+ const express = $importDefault($express);
9
10
  import { join, relative, resolve as resolvePath, sep, posix } from "path";
10
- import $serveMiddleware from "serve-handler";
11
- const serveMiddleware = $importDefault($serveMiddleware);
12
11
  /**
13
12
  * Get the relative path from one path to another.
14
13
  *
@@ -46,6 +45,22 @@ export function getAllowedPaths(config, manifest) {
46
45
  ...otherFiles,
47
46
  ];
48
47
  }
48
+ /**
49
+ * Get whether the request path is allowed. This is used to check if the request
50
+ * path is in the list of allowed paths for the static server.
51
+ *
52
+ * @param request - The request object.
53
+ * @param config - The config object.
54
+ * @returns A promise that resolves to `true` if the path is allowed, or
55
+ * `false` if it is not.
56
+ */
57
+ async function isAllowedPath(request, config) {
58
+ const manifestPath = join(config.server.root, NpmSnapFileNames.Manifest);
59
+ const { result } = await readJsonFile(manifestPath);
60
+ const allowedPaths = getAllowedPaths(config, result);
61
+ const path = request.path.slice(1);
62
+ return allowedPaths.some((allowedPath) => path === allowedPath);
63
+ }
49
64
  /**
50
65
  * Get a static server for development purposes.
51
66
  *
@@ -54,61 +69,38 @@ export function getAllowedPaths(config, manifest) {
54
69
  * difficult to customize.
55
70
  *
56
71
  * @param config - The config object.
72
+ * @param middleware - An array of middleware functions to run before serving
73
+ * the static files.
57
74
  * @returns An object with a `listen` method that returns a promise that
58
75
  * resolves when the server is listening.
59
76
  */
60
- export function getServer(config) {
61
- /**
62
- * Get the response for a request. This is extracted into a function so that
63
- * we can easily catch errors and send a 500 response.
64
- *
65
- * @param request - The request.
66
- * @param response - The response.
67
- * @returns A promise that resolves when the response is sent.
68
- */
69
- async function getResponse(request, response) {
70
- const manifestPath = join(config.server.root, NpmSnapFileNames.Manifest);
71
- const { result } = await readJsonFile(manifestPath);
72
- const allowedPaths = getAllowedPaths(config, result);
73
- const pathname = request.url &&
74
- request.headers.host &&
75
- new URL(request.url, `http://${request.headers.host}`).pathname;
76
- const path = pathname?.slice(1);
77
- const allowed = allowedPaths.some((allowedPath) => path === allowedPath);
78
- if (!allowed) {
79
- response.statusCode = 404;
80
- response.end();
81
- return;
82
- }
83
- await serveMiddleware(request, response, {
84
- public: config.server.root,
85
- directoryListing: false,
86
- headers: [
87
- {
88
- source: '**/*',
89
- headers: [
90
- {
91
- key: 'Cache-Control',
92
- value: 'no-cache',
93
- },
94
- {
95
- key: 'Access-Control-Allow-Origin',
96
- value: '*',
97
- },
98
- ],
99
- },
100
- ],
101
- });
102
- }
103
- const server = createServer((request, response) => {
104
- getResponse(request, response).catch(
105
- /* istanbul ignore next */
106
- (error) => {
107
- logError(error);
108
- response.statusCode = 500;
77
+ export function getServer(config, middleware = []) {
78
+ const app = express();
79
+ // Run "middleware" functions before serving the static files.
80
+ middleware.forEach((fn) => fn(app));
81
+ // Check for allowed paths in the request URL.
82
+ app.use((request, response, next) => {
83
+ isAllowedPath(request, config)
84
+ .then((allowed) => {
85
+ if (allowed) {
86
+ // eslint-disable-next-line promise/no-callback-in-promise
87
+ next();
88
+ return;
89
+ }
90
+ response.status(404);
109
91
  response.end();
110
- });
92
+ })
93
+ // eslint-disable-next-line promise/no-callback-in-promise
94
+ .catch(next);
111
95
  });
96
+ // Serve the static files.
97
+ app.use(expressStatic(config.server.root, {
98
+ dotfiles: 'deny',
99
+ setHeaders: (res) => {
100
+ res.setHeader('Cache-Control', 'no-cache');
101
+ res.setHeader('Access-Control-Allow-Origin', '*');
102
+ },
103
+ }));
112
104
  /**
113
105
  * Start the server on the port specified in the config.
114
106
  *
@@ -120,25 +112,24 @@ export function getServer(config) {
120
112
  */
121
113
  const listen = async (port = config.server.port) => {
122
114
  return new Promise((resolve, reject) => {
123
- try {
124
- server.listen(port, () => {
125
- const close = async () => {
126
- await new Promise((resolveClose, rejectClose) => {
127
- server.close((closeError) => {
128
- if (closeError) {
129
- return rejectClose(closeError);
130
- }
131
- return resolveClose();
132
- });
115
+ // eslint-disable-next-line consistent-return
116
+ const server = app.listen(port, (error) => {
117
+ if (error) {
118
+ return reject(error);
119
+ }
120
+ const close = async () => {
121
+ await new Promise((resolveClose, rejectClose) => {
122
+ server.close((closeError) => {
123
+ if (closeError) {
124
+ return rejectClose(closeError);
125
+ }
126
+ return resolveClose();
133
127
  });
134
- };
135
- const address = server.address();
136
- resolve({ port: address.port, server, close });
137
- });
138
- }
139
- catch (listenError) {
140
- reject(listenError);
141
- }
128
+ });
129
+ };
130
+ const address = server.address();
131
+ resolve({ port: address.port, server, close });
132
+ });
142
133
  });
143
134
  };
144
135
  return { listen };
@@ -1 +1 @@
1
- {"version":3,"file":"server.mjs","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EACL,QAAQ,EACR,gBAAgB,EAChB,YAAY,EACb,mCAAmC;AAEpC,OAAO,EAAE,YAAY,EAAE,aAAa;AAEpC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,IAAI,WAAW,EAAE,GAAG,EAAE,KAAK,EAAE,aAAa;AAC1E,OAAO,gBAAe,sBAAsB;;AAI5C;;;;;;;;;GASG;AACH,SAAS,eAAe,CAAC,IAAY,EAAE,EAAU;IAC/C,OAAO,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAC7B,MAAuB,EACvB,QAAsB;IAEtB,MAAM,cAAc,GAClB,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAClC,eAAe,CACb,MAAM,CAAC,MAAM,CAAC,IAAI,EAClB,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CACtC,CACF,IAAI,EAAE,CAAC;IAEV,MAAM,iBAAiB,GACrB,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,YAAY,EAAE,EAAE,CAC5C,eAAe,CACb,MAAM,CAAC,MAAM,CAAC,IAAI,EAClB,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAC9C,CACF,IAAI,EAAE,CAAC;IA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AI,CAAC;gBACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;oBACvB,MAAM,KAAK,GAAG,KAAK,IAAI,EAAE;wBACvB,MAAM,IAAI,OAAO,CAAO,CAAC,YAAY,EAAE,WAAW,EAAE,EAAE;4BACpD,MAAM,CAAC,KAAK,CAAC,CAAC,UAAU,EAAE,EAAE;gCAC1B,IAAI,UAAU,EAAE,CAAC;oCACf,OAAO,WAAW,CAAC,UAAU,CAAC,CAAC;gCACjC,CAAC;gCAED,OAAO,YAAY,EAAE,CAAC;4BACxB,CAAC,CAAC,CAAC;wBACL,CAAC,CAAC,CAAC;oBACL,CAAC,CAAC;oBAEF,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAiB,CAAC;oBAChD,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;gBACjD,CAAC,CAAC,CAAC;YACL,CAAC;YAAC,OAAO,WAAW,EAAE,CAAC;gBACrB,MAAM,CAAC,WAAW,CAAC,CAAC;YACtB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,CAAC;AACpB,CAAC","sourcesContent":["import type { SnapManifest } from '@metamask/snaps-utils';\nimport {\n logError,\n NpmSnapFileNames,\n readJsonFile,\n} from '@metamask/snaps-utils/node';\nimport type { IncomingMessage, Server, ServerResponse } from 'http';\nimport { createServer } from 'http';\nimport type { AddressInfo } from 'net';\nimport { join, relative, resolve as resolvePath, sep, posix } from 'path';\nimport serveMiddleware from 'serve-handler';\n\nimport type { ProcessedConfig } from '../config';\n\n/**\n * Get the relative path from one path to another.\n *\n * Note: This is a modified version of `path.relative` that uses Posix\n * separators for URL-compatibility.\n *\n * @param from - The path to start from.\n * @param to - The path to end at.\n * @returns The relative path.\n */\nfunction getRelativePath(from: string, to: string) {\n return relative(from, to).split(sep).join(posix.sep);\n}\n\n/**\n * Get the allowed paths for the static server. This includes the output file,\n * the manifest file, and any auxiliary/localization files.\n *\n * @param config - The config object.\n * @param manifest - The Snap manifest object.\n * @returns An array of allowed paths.\n */\nexport function getAllowedPaths(\n config: ProcessedConfig,\n manifest: SnapManifest,\n) {\n const auxiliaryFiles =\n manifest.source.files?.map((file) =>\n getRelativePath(\n config.server.root,\n resolvePath(config.server.root, file),\n ),\n ) ?? [];\n\n const localizationFiles =\n manifest.source.locales?.map((localization) =>\n getRelativePath(\n config.server.root,\n resolvePath(config.server.root, localization),\n ),\n ) ?? [];\n\n const otherFiles = manifest.source.location.npm.iconPath\n ? [\n getRelativePath(\n config.server.root,\n resolvePath(\n config.server.root,\n manifest.source.location.npm.iconPath,\n ),\n ),\n ]\n : [];\n\n return [\n getRelativePath(\n config.server.root,\n resolvePath(\n config.server.root,\n config.output.path,\n config.output.filename,\n ),\n ),\n getRelativePath(\n config.server.root,\n resolvePath(config.server.root, NpmSnapFileNames.Manifest),\n ),\n ...auxiliaryFiles,\n ...localizationFiles,\n ...otherFiles,\n ];\n}\n\n/**\n * Get a static server for development purposes.\n *\n * Note: We're intentionally not using `webpack-dev-server` here because it\n * adds a lot of extra stuff to the output that we don't need, and it's\n * difficult to customize.\n *\n * @param config - The config object.\n * @returns An object with a `listen` method that returns a promise that\n * resolves when the server is listening.\n */\nexport function getServer(config: ProcessedConfig) {\n /**\n * Get the response for a request. This is extracted into a function so that\n * we can easily catch errors and send a 500 response.\n *\n * @param request - The request.\n * @param response - The response.\n * @returns A promise that resolves when the response is sent.\n */\n async function getResponse(\n request: IncomingMessage,\n response: ServerResponse,\n ) {\n const manifestPath = join(config.server.root, NpmSnapFileNames.Manifest);\n const { result } = await readJsonFile<SnapManifest>(manifestPath);\n const allowedPaths = getAllowedPaths(config, result);\n\n const pathname =\n request.url &&\n request.headers.host &&\n new URL(request.url, `http://${request.headers.host}`).pathname;\n const path = pathname?.slice(1);\n const allowed = allowedPaths.some((allowedPath) => path === allowedPath);\n\n if (!allowed) {\n response.statusCode = 404;\n response.end();\n return;\n }\n\n await serveMiddleware(request, response, {\n public: config.server.root,\n directoryListing: false,\n headers: [\n {\n source: '**/*',\n headers: [\n {\n key: 'Cache-Control',\n value: 'no-cache',\n },\n {\n key: 'Access-Control-Allow-Origin',\n value: '*',\n },\n ],\n },\n ],\n });\n }\n\n const server = createServer((request, response) => {\n getResponse(request, response).catch(\n /* istanbul ignore next */\n (error) => {\n logError(error);\n response.statusCode = 500;\n response.end();\n },\n );\n });\n\n /**\n * Start the server on the port specified in the config.\n *\n * @param port - The port to listen on.\n * @returns A promise that resolves when the server is listening. The promise\n * resolves to an object with the port and the server instance. Note that if\n * the `config.server.port` is `0`, the OS will choose a random port for us,\n * so we need to get the port from the server after it starts.\n */\n const listen = async (port = config.server.port) => {\n return new Promise<{\n port: number;\n server: Server;\n close: () => Promise<void>;\n }>((resolve, reject) => {\n try {\n server.listen(port, () => {\n const close = async () => {\n await new Promise<void>((resolveClose, rejectClose) => {\n server.close((closeError) => {\n if (closeError) {\n return rejectClose(closeError);\n }\n\n return resolveClose();\n });\n });\n };\n\n const address = server.address() as AddressInfo;\n resolve({ port: address.port, server, close });\n });\n } catch (listenError) {\n reject(listenError);\n }\n });\n };\n\n return { listen };\n}\n"]}
1
+ {"version":3,"file":"server.mjs","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,mCAAmC;AAE5E,OAAO,UAAS,EAAE,MAAM,IAAI,aAAa,EAAE,yBAAgB;;AAG3D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,IAAI,WAAW,EAAE,GAAG,EAAE,KAAK,EAAE,aAAa;AAI1E;;;;;;;;;GASG;AACH,SAAS,eAAe,CAAC,IAAY,EAAE,EAAU;IAC/C,OAAO,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAA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CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,CAAC;AACpB,CAAC","sourcesContent":["import type { SnapManifest } from '@metamask/snaps-utils';\nimport { NpmSnapFileNames, readJsonFile } from '@metamask/snaps-utils/node';\nimport type { Express, Request } from 'express';\nimport express, { static as expressStatic } from 'express';\nimport type { Server } from 'http';\nimport type { AddressInfo } from 'net';\nimport { join, relative, resolve as resolvePath, sep, posix } from 'path';\n\nimport type { ProcessedConfig } from '../config';\n\n/**\n * Get the relative path from one path to another.\n *\n * Note: This is a modified version of `path.relative` that uses Posix\n * separators for URL-compatibility.\n *\n * @param from - The path to start from.\n * @param to - The path to end at.\n * @returns The relative path.\n */\nfunction getRelativePath(from: string, to: string) {\n return relative(from, to).split(sep).join(posix.sep);\n}\n\n/**\n * Get the allowed paths for the static server. This includes the output file,\n * the manifest file, and any auxiliary/localization files.\n *\n * @param config - The config object.\n * @param manifest - The Snap manifest object.\n * @returns An array of allowed paths.\n */\nexport function getAllowedPaths(\n config: ProcessedConfig,\n manifest: SnapManifest,\n) {\n const auxiliaryFiles =\n manifest.source.files?.map((file) =>\n getRelativePath(\n config.server.root,\n resolvePath(config.server.root, file),\n ),\n ) ?? [];\n\n const localizationFiles =\n manifest.source.locales?.map((localization) =>\n getRelativePath(\n config.server.root,\n resolvePath(config.server.root, localization),\n ),\n ) ?? [];\n\n const otherFiles = manifest.source.location.npm.iconPath\n ? [\n getRelativePath(\n config.server.root,\n resolvePath(\n config.server.root,\n manifest.source.location.npm.iconPath,\n ),\n ),\n ]\n : [];\n\n return [\n getRelativePath(\n config.server.root,\n resolvePath(\n config.server.root,\n config.output.path,\n config.output.filename,\n ),\n ),\n getRelativePath(\n config.server.root,\n resolvePath(config.server.root, NpmSnapFileNames.Manifest),\n ),\n ...auxiliaryFiles,\n ...localizationFiles,\n ...otherFiles,\n ];\n}\n\n/**\n * Get whether the request path is allowed. This is used to check if the request\n * path is in the list of allowed paths for the static server.\n *\n * @param request - The request object.\n * @param config - The config object.\n * @returns A promise that resolves to `true` if the path is allowed, or\n * `false` if it is not.\n */\nasync function isAllowedPath(request: Request, config: ProcessedConfig) {\n const manifestPath = join(config.server.root, NpmSnapFileNames.Manifest);\n const { result } = await readJsonFile<SnapManifest>(manifestPath);\n const allowedPaths = getAllowedPaths(config, result);\n\n const path = request.path.slice(1);\n return allowedPaths.some((allowedPath) => path === allowedPath);\n}\n\ntype Middleware = (app: Express) => void;\n\n/**\n * Get a static server for development purposes.\n *\n * Note: We're intentionally not using `webpack-dev-server` here because it\n * adds a lot of extra stuff to the output that we don't need, and it's\n * difficult to customize.\n *\n * @param config - The config object.\n * @param middleware - An array of middleware functions to run before serving\n * the static files.\n * @returns An object with a `listen` method that returns a promise that\n * resolves when the server is listening.\n */\nexport function getServer(\n config: ProcessedConfig,\n middleware: Middleware[] = [],\n) {\n const app = express();\n\n // Run \"middleware\" functions before serving the static files.\n middleware.forEach((fn) => fn(app));\n\n // Check for allowed paths in the request URL.\n app.use((request, response, next) => {\n isAllowedPath(request, config)\n .then((allowed) => {\n if (allowed) {\n // eslint-disable-next-line promise/no-callback-in-promise\n next();\n return;\n }\n\n response.status(404);\n response.end();\n })\n // eslint-disable-next-line promise/no-callback-in-promise\n .catch(next);\n });\n\n // Serve the static files.\n app.use(\n expressStatic(config.server.root, {\n dotfiles: 'deny',\n setHeaders: (res) => {\n res.setHeader('Cache-Control', 'no-cache');\n res.setHeader('Access-Control-Allow-Origin', '*');\n },\n }),\n );\n\n /**\n * Start the server on the port specified in the config.\n *\n * @param port - The port to listen on.\n * @returns A promise that resolves when the server is listening. The promise\n * resolves to an object with the port and the server instance. Note that if\n * the `config.server.port` is `0`, the OS will choose a random port for us,\n * so we need to get the port from the server after it starts.\n */\n const listen = async (port = config.server.port) => {\n return new Promise<{\n port: number;\n server: Server;\n close: () => Promise<void>;\n }>((resolve, reject) => {\n // eslint-disable-next-line consistent-return\n const server = app.listen(port, (error) => {\n if (error) {\n return reject(error);\n }\n\n const close = async () => {\n await new Promise<void>((resolveClose, rejectClose) => {\n server.close((closeError) => {\n if (closeError) {\n return rejectClose(closeError);\n }\n\n return resolveClose();\n });\n });\n };\n\n const address = server.address() as AddressInfo;\n resolve({ port: address.port, server, close });\n });\n });\n };\n\n return { listen };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-cli",
3
- "version": "7.0.0",
3
+ "version": "7.1.0",
4
4
  "description": "A CLI for developing MetaMask Snaps",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -64,11 +64,12 @@
64
64
  "test:watch": "jest --watch"
65
65
  },
66
66
  "dependencies": {
67
+ "@metamask/snaps-sandbox": "^1.0.0",
67
68
  "@metamask/snaps-sdk": "^6.22.0",
68
69
  "@metamask/snaps-utils": "^9.2.0",
69
70
  "@metamask/snaps-webpack-plugin": "^4.2.1",
70
71
  "@metamask/superstruct": "^3.2.1",
71
- "@metamask/utils": "^11.2.0",
72
+ "@metamask/utils": "^11.4.0",
72
73
  "@swc/core": "1.3.78",
73
74
  "assert": "^2.0.0",
74
75
  "browserify-zlib": "^0.2.0",
@@ -79,6 +80,7 @@
79
80
  "crypto-browserify": "^3.12.0",
80
81
  "domain-browser": "^4.22.0",
81
82
  "events": "^3.3.0",
83
+ "express": "^5.1.0",
82
84
  "fork-ts-checker-webpack-plugin": "^9.0.2",
83
85
  "https-browserify": "^1.0.0",
84
86
  "ora": "^5.4.1",
@@ -89,7 +91,6 @@
89
91
  "querystring-es3": "^0.2.1",
90
92
  "readable-stream": "^3.6.2",
91
93
  "semver": "^7.5.4",
92
- "serve-handler": "^6.1.5",
93
94
  "stream-browserify": "^3.0.0",
94
95
  "stream-http": "^3.2.0",
95
96
  "string_decoder": "^1.3.0",
@@ -111,9 +112,9 @@
111
112
  "@metamask/auto-changelog": "^5.0.1",
112
113
  "@swc/jest": "^0.2.26",
113
114
  "@ts-bridge/cli": "^0.6.1",
115
+ "@types/express": "^5.0.1",
114
116
  "@types/jest": "^27.5.1",
115
117
  "@types/node": "18.14.2",
116
- "@types/serve-handler": "^6.1.0",
117
118
  "@types/yargs": "^17.0.24",
118
119
  "cross-fetch": "^3.1.5",
119
120
  "deepmerge": "^4.2.2",