@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
@@ -0,0 +1,29 @@
1
+ import { startSandbox } from "./server.mjs";
2
+ import { success, executeSteps } from "../../utils/index.mjs";
3
+ import { steps as buildSteps } from "../build/index.mjs";
4
+ const steps = [
5
+ ...buildSteps,
6
+ {
7
+ name: 'Running sandbox.',
8
+ task: async ({ config, spinner }) => {
9
+ const { port } = await startSandbox(config);
10
+ success(`Sandbox running at http://localhost:${port}.`, spinner);
11
+ spinner.stop();
12
+ },
13
+ },
14
+ ];
15
+ /**
16
+ * Start the sandbox.
17
+ *
18
+ * @param config - The config object.
19
+ * @param options - The options object.
20
+ * @param options.build - Whether to build the Snap before starting the sandbox.
21
+ */
22
+ export async function sandboxHandler(config, { build = true }) {
23
+ await executeSteps(steps, {
24
+ analyze: false,
25
+ build,
26
+ config,
27
+ });
28
+ }
29
+ //# sourceMappingURL=sandbox.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sandbox.mjs","sourceRoot":"","sources":["../../../src/commands/sandbox/sandbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,qBAAiB;AAGxC,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,8BAAoB;AACpD,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,2BAAiB;AAS/C,MAAM,KAAK,GAA0B;IACnC,GAAG,UAAU;IACb;QACE,IAAI,EAAE,kBAAkB;QACxB,IAAI,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,EAAE;YAClC,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,YAAY,CAAC,MAAM,CAAC,CAAC;YAC5C,OAAO,CAAC,uCAAuC,IAAI,GAAG,EAAE,OAAO,CAAC,CAAC;YAEjE,OAAO,CAAC,IAAI,EAAE,CAAC;QACjB,CAAC;KACF;CACF,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,MAAuB,EACvB,EAAE,KAAK,GAAG,IAAI,EAAkB;IAEhC,MAAM,YAAY,CAAC,KAAK,EAAE;QACxB,OAAO,EAAE,KAAK;QACd,KAAK;QACL,MAAM;KACP,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { startSandbox } from './server';\nimport type { ProcessedConfig } from '../../config';\nimport type { Steps } from '../../utils';\nimport { success, executeSteps } from '../../utils';\nimport { steps as buildSteps } from '../build';\nimport type { BuildContext } from '../build/build';\n\ntype SandboxOptions = {\n build?: boolean;\n};\n\ntype SandboxContext = BuildContext;\n\nconst steps: Steps<SandboxContext> = [\n ...buildSteps,\n {\n name: 'Running sandbox.',\n task: async ({ config, spinner }) => {\n const { port } = await startSandbox(config);\n success(`Sandbox running at http://localhost:${port}.`, spinner);\n\n spinner.stop();\n },\n },\n];\n\n/**\n * Start the sandbox.\n *\n * @param config - The config object.\n * @param options - The options object.\n * @param options.build - Whether to build the Snap before starting the sandbox.\n */\nexport async function sandboxHandler(\n config: ProcessedConfig,\n { build = true }: SandboxOptions,\n) {\n await executeSteps(steps, {\n analyze: false,\n build,\n config,\n });\n}\n"]}
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.startSandbox = void 0;
4
+ const express_1 = require("express/index.js");
5
+ const path_1 = require("path");
6
+ const webpack_1 = require("../../webpack/index.cjs");
7
+ /**
8
+ * Start the sandbox.
9
+ *
10
+ * @param config - The config object.
11
+ * @returns The server instance.
12
+ */
13
+ async function startSandbox(config) {
14
+ const server = (0, webpack_1.getServer)(config, [
15
+ (app) => {
16
+ app.use('/__sandbox__', (0, express_1.static)((0, path_1.dirname)(require.resolve('@metamask/snaps-sandbox/dist/index.html'))));
17
+ app.get('/', (_request, response) => {
18
+ response.sendFile(require.resolve('@metamask/snaps-sandbox/dist/index.html'));
19
+ });
20
+ },
21
+ ]);
22
+ // If the `configPort` is `0`, the OS will choose a random port for us, so we
23
+ // need to get the port from the server after it starts.
24
+ return await server.listen(config.server.port);
25
+ }
26
+ exports.startSandbox = startSandbox;
27
+ //# sourceMappingURL=server.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.cjs","sourceRoot":"","sources":["../../../src/commands/sandbox/server.ts"],"names":[],"mappings":";;;AAAA,8CAAkD;AAClD,+BAA+B;AAG/B,qDAA0C;AAE1C;;;;;GAKG;AACI,KAAK,UAAU,YAAY,CAAC,MAAuB;IACxD,MAAM,MAAM,GAAG,IAAA,mBAAS,EAAC,MAAM,EAAE;QAC/B,CAAC,GAAG,EAAE,EAAE;YACN,GAAG,CAAC,GAAG,CACL,cAAc,EACd,IAAA,gBAAa,EACX,IAAA,cAAO,EAAC,OAAO,CAAC,OAAO,CAAC,yCAAyC,CAAC,CAAC,CACpE,CACF,CAAC;YAEF,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE;gBAClC,QAAQ,CAAC,QAAQ,CACf,OAAO,CAAC,OAAO,CAAC,yCAAyC,CAAC,CAC3D,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;IAEH,6EAA6E;IAC7E,wDAAwD;IACxD,OAAO,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AACjD,CAAC;AArBD,oCAqBC","sourcesContent":["import { static as expressStatic } from 'express';\nimport { dirname } from 'path';\n\nimport type { ProcessedConfig } from '../../config';\nimport { getServer } from '../../webpack';\n\n/**\n * Start the sandbox.\n *\n * @param config - The config object.\n * @returns The server instance.\n */\nexport async function startSandbox(config: ProcessedConfig) {\n const server = getServer(config, [\n (app) => {\n app.use(\n '/__sandbox__',\n expressStatic(\n dirname(require.resolve('@metamask/snaps-sandbox/dist/index.html')),\n ),\n );\n\n app.get('/', (_request, response) => {\n response.sendFile(\n require.resolve('@metamask/snaps-sandbox/dist/index.html'),\n );\n });\n },\n ]);\n\n // If the `configPort` is `0`, the OS will choose a random port for us, so we\n // need to get the port from the server after it starts.\n return await server.listen(config.server.port);\n}\n"]}
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import type { ProcessedConfig } from "../../config.cjs";
3
+ /**
4
+ * Start the sandbox.
5
+ *
6
+ * @param config - The config object.
7
+ * @returns The server instance.
8
+ */
9
+ export declare function startSandbox(config: ProcessedConfig): Promise<{
10
+ port: number;
11
+ server: import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>;
12
+ close: () => Promise<void>;
13
+ }>;
14
+ //# sourceMappingURL=server.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.cts","sourceRoot":"","sources":["../../../src/commands/sandbox/server.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AAGpD;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,MAAM,EAAE,eAAe;;;;GAqBzD"}
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import type { ProcessedConfig } from "../../config.mjs";
3
+ /**
4
+ * Start the sandbox.
5
+ *
6
+ * @param config - The config object.
7
+ * @returns The server instance.
8
+ */
9
+ export declare function startSandbox(config: ProcessedConfig): Promise<{
10
+ port: number;
11
+ server: import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>;
12
+ close: () => Promise<void>;
13
+ }>;
14
+ //# sourceMappingURL=server.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.mts","sourceRoot":"","sources":["../../../src/commands/sandbox/server.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AAGpD;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,MAAM,EAAE,eAAe;;;;GAqBzD"}
@@ -0,0 +1,25 @@
1
+ import { createRequire as $createRequire } from "module";
2
+ const $require = $createRequire(import.meta.url);
3
+ import { static as expressStatic } from "express/index.js";
4
+ import { dirname } from "path";
5
+ import { getServer } from "../../webpack/index.mjs";
6
+ /**
7
+ * Start the sandbox.
8
+ *
9
+ * @param config - The config object.
10
+ * @returns The server instance.
11
+ */
12
+ export async function startSandbox(config) {
13
+ const server = getServer(config, [
14
+ (app) => {
15
+ app.use('/__sandbox__', expressStatic(dirname($require.resolve('@metamask/snaps-sandbox/dist/index.html'))));
16
+ app.get('/', (_request, response) => {
17
+ response.sendFile($require.resolve('@metamask/snaps-sandbox/dist/index.html'));
18
+ });
19
+ },
20
+ ]);
21
+ // If the `configPort` is `0`, the OS will choose a random port for us, so we
22
+ // need to get the port from the server after it starts.
23
+ return await server.listen(config.server.port);
24
+ }
25
+ //# sourceMappingURL=server.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.mjs","sourceRoot":"","sources":["../../../src/commands/sandbox/server.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,MAAM,IAAI,aAAa,EAAE,yBAAgB;AAClD,OAAO,EAAE,OAAO,EAAE,aAAa;AAG/B,OAAO,EAAE,SAAS,EAAE,gCAAsB;AAE1C;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,MAAuB;IACxD,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE;QAC/B,CAAC,GAAG,EAAE,EAAE;YACN,GAAG,CAAC,GAAG,CACL,cAAc,EACd,aAAa,CACX,OAAO,kBAAiB,yCAAyC,EAAE,CACpE,CACF,CAAC;YAEF,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE;gBAClC,QAAQ,CAAC,QAAQ,kBACC,yCAAyC,EAC1D,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;IAEH,6EAA6E;IAC7E,wDAAwD;IACxD,OAAO,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import { static as expressStatic } from 'express';\nimport { dirname } from 'path';\n\nimport type { ProcessedConfig } from '../../config';\nimport { getServer } from '../../webpack';\n\n/**\n * Start the sandbox.\n *\n * @param config - The config object.\n * @returns The server instance.\n */\nexport async function startSandbox(config: ProcessedConfig) {\n const server = getServer(config, [\n (app) => {\n app.use(\n '/__sandbox__',\n expressStatic(\n dirname(require.resolve('@metamask/snaps-sandbox/dist/index.html')),\n ),\n );\n\n app.get('/', (_request, response) => {\n response.sendFile(\n require.resolve('@metamask/snaps-sandbox/dist/index.html'),\n );\n });\n },\n ]);\n\n // If the `configPort` is `0`, the OS will choose a random port for us, so we\n // need to get the port from the server after it starts.\n return await server.listen(config.server.port);\n}\n"]}
@@ -1,13 +1,32 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
4
24
  };
5
25
  Object.defineProperty(exports, "__esModule", { value: true });
6
26
  exports.getServer = exports.getAllowedPaths = void 0;
7
27
  const node_1 = require("@metamask/snaps-utils/node");
8
- const http_1 = require("http");
28
+ const express_1 = __importStar(require("express/index.js"));
9
29
  const path_1 = require("path");
10
- const serve_handler_1 = __importDefault(require("serve-handler"));
11
30
  /**
12
31
  * Get the relative path from one path to another.
13
32
  *
@@ -46,6 +65,22 @@ function getAllowedPaths(config, manifest) {
46
65
  ];
47
66
  }
48
67
  exports.getAllowedPaths = getAllowedPaths;
68
+ /**
69
+ * Get whether the request path is allowed. This is used to check if the request
70
+ * path is in the list of allowed paths for the static server.
71
+ *
72
+ * @param request - The request object.
73
+ * @param config - The config object.
74
+ * @returns A promise that resolves to `true` if the path is allowed, or
75
+ * `false` if it is not.
76
+ */
77
+ async function isAllowedPath(request, config) {
78
+ const manifestPath = (0, path_1.join)(config.server.root, node_1.NpmSnapFileNames.Manifest);
79
+ const { result } = await (0, node_1.readJsonFile)(manifestPath);
80
+ const allowedPaths = getAllowedPaths(config, result);
81
+ const path = request.path.slice(1);
82
+ return allowedPaths.some((allowedPath) => path === allowedPath);
83
+ }
49
84
  /**
50
85
  * Get a static server for development purposes.
51
86
  *
@@ -54,61 +89,38 @@ exports.getAllowedPaths = getAllowedPaths;
54
89
  * difficult to customize.
55
90
  *
56
91
  * @param config - The config object.
92
+ * @param middleware - An array of middleware functions to run before serving
93
+ * the static files.
57
94
  * @returns An object with a `listen` method that returns a promise that
58
95
  * resolves when the server is listening.
59
96
  */
60
- 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 = (0, path_1.join)(config.server.root, node_1.NpmSnapFileNames.Manifest);
71
- const { result } = await (0, node_1.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 (0, serve_handler_1.default)(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 = (0, http_1.createServer)((request, response) => {
104
- getResponse(request, response).catch(
105
- /* istanbul ignore next */
106
- (error) => {
107
- (0, node_1.logError)(error);
108
- response.statusCode = 500;
97
+ function getServer(config, middleware = []) {
98
+ const app = (0, express_1.default)();
99
+ // Run "middleware" functions before serving the static files.
100
+ middleware.forEach((fn) => fn(app));
101
+ // Check for allowed paths in the request URL.
102
+ app.use((request, response, next) => {
103
+ isAllowedPath(request, config)
104
+ .then((allowed) => {
105
+ if (allowed) {
106
+ // eslint-disable-next-line promise/no-callback-in-promise
107
+ next();
108
+ return;
109
+ }
110
+ response.status(404);
109
111
  response.end();
110
- });
112
+ })
113
+ // eslint-disable-next-line promise/no-callback-in-promise
114
+ .catch(next);
111
115
  });
116
+ // Serve the static files.
117
+ app.use((0, express_1.static)(config.server.root, {
118
+ dotfiles: 'deny',
119
+ setHeaders: (res) => {
120
+ res.setHeader('Cache-Control', 'no-cache');
121
+ res.setHeader('Access-Control-Allow-Origin', '*');
122
+ },
123
+ }));
112
124
  /**
113
125
  * Start the server on the port specified in the config.
114
126
  *
@@ -120,25 +132,24 @@ function getServer(config) {
120
132
  */
121
133
  const listen = async (port = config.server.port) => {
122
134
  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
- });
135
+ // eslint-disable-next-line consistent-return
136
+ const server = app.listen(port, (error) => {
137
+ if (error) {
138
+ return reject(error);
139
+ }
140
+ const close = async () => {
141
+ await new Promise((resolveClose, rejectClose) => {
142
+ server.close((closeError) => {
143
+ if (closeError) {
144
+ return rejectClose(closeError);
145
+ }
146
+ return resolveClose();
133
147
  });
134
- };
135
- const address = server.address();
136
- resolve({ port: address.port, server, close });
137
- });
138
- }
139
- catch (listenError) {
140
- reject(listenError);
141
- }
148
+ });
149
+ };
150
+ const address = server.address();
151
+ resolve({ port: address.port, server, close });
152
+ });
142
153
  });
143
154
  };
144
155
  return { listen };
@@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AAE,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;AArGD,8BAqGC","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.cjs","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,qDAA4E;AAE5E,4DAA2D;AAG3D,+BAA0E;AAI1E;;;;;;;;;GASG;AACH,SAAS,eAAe,CAAC,IAAY,EAAE,EAAU;IAC/C,OAAO,IAAA,eAAQ,EAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,UAAG,CAAC,CAAC,IAAI,CAAC,YAAK,CAAC,GAAG,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,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,IAAA,cAAW,EAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CACtC,CACF,IAAI,EAAE,CAAC;IAEV,MAAM,iBAAiB,GACrB,QA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;KACF,CAAC,CACH,CAAC;IAEF;;;;;;;;OAQG;IACH,MAAM,MAAM,GAAG,KAAK,EAAE,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE;QACjD,OAAO,IAAI,OAAO,CAIf,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrB,6CAA6C;YAC7C,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;gBACxC,IAAI,KAAK,EAAE,CAAC;oBACV,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;gBACvB,CAAC;gBAED,MAAM,KAAK,GAAG,KAAK,IAAI,EAAE;oBACvB,MAAM,IAAI,OAAO,CAAO,CAAC,YAAY,EAAE,WAAW,EAAE,EAAE;wBACpD,MAAM,CAAC,KAAK,CAAC,CAAC,UAAU,EAAE,EAAE;4BAC1B,IAAI,UAAU,EAAE,CAAC;gCACf,OAAO,WAAW,CAAC,UAAU,CAAC,CAAC;4BACjC,CAAC;4BAED,OAAO,YAAY,EAAE,CAAC;wBACxB,CAAC,CAAC,CAAC;oBACL,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC;gBAEF,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAiB,CAAC;gBAChD,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;YACjD,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,CAAC;AACpB,CAAC;AA7ED,8BA6EC","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"]}
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import type { SnapManifest } from "@metamask/snaps-utils";
3
+ import type { Express } from "express/index.js";
3
4
  import type { Server } from "http";
4
5
  import type { ProcessedConfig } from "../config.cjs";
5
6
  /**
@@ -11,6 +12,7 @@ import type { ProcessedConfig } from "../config.cjs";
11
12
  * @returns An array of allowed paths.
12
13
  */
13
14
  export declare function getAllowedPaths(config: ProcessedConfig, manifest: SnapManifest): string[];
15
+ type Middleware = (app: Express) => void;
14
16
  /**
15
17
  * Get a static server for development purposes.
16
18
  *
@@ -19,14 +21,17 @@ export declare function getAllowedPaths(config: ProcessedConfig, manifest: SnapM
19
21
  * difficult to customize.
20
22
  *
21
23
  * @param config - The config object.
24
+ * @param middleware - An array of middleware functions to run before serving
25
+ * the static files.
22
26
  * @returns An object with a `listen` method that returns a promise that
23
27
  * resolves when the server is listening.
24
28
  */
25
- export declare function getServer(config: ProcessedConfig): {
29
+ export declare function getServer(config: ProcessedConfig, middleware?: Middleware[]): {
26
30
  listen: (port?: number) => Promise<{
27
31
  port: number;
28
32
  server: Server;
29
33
  close: () => Promise<void>;
30
34
  }>;
31
35
  };
36
+ export {};
32
37
  //# sourceMappingURL=server.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.cts","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAM1D,OAAO,KAAK,EAAmB,MAAM,EAAkB,aAAa;AAMpE,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAkB;AAgBjD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,YAAY,YA+CvB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,eAAe;;cAyErC,MAAM;gBACJ,MAAM;eACP,MAAM,QAAQ,IAAI,CAAC;;EA0B/B"}
1
+ {"version":3,"file":"server.d.cts","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,OAAO,EAAW,yBAAgB;AAEhD,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa;AAInC,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAkB;AAgBjD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,YAAY,YA+CvB;AAoBD,KAAK,UAAU,GAAG,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAC;AAEzC;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,eAAe,EACvB,UAAU,GAAE,UAAU,EAAO;;cA8CnB,MAAM;gBACJ,MAAM;eACP,MAAM,QAAQ,IAAI,CAAC;;EA2B/B"}
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import type { SnapManifest } from "@metamask/snaps-utils";
3
+ import type { Express } from "express/index.js";
3
4
  import type { Server } from "http";
4
5
  import type { ProcessedConfig } from "../config.mjs";
5
6
  /**
@@ -11,6 +12,7 @@ import type { ProcessedConfig } from "../config.mjs";
11
12
  * @returns An array of allowed paths.
12
13
  */
13
14
  export declare function getAllowedPaths(config: ProcessedConfig, manifest: SnapManifest): string[];
15
+ type Middleware = (app: Express) => void;
14
16
  /**
15
17
  * Get a static server for development purposes.
16
18
  *
@@ -19,14 +21,17 @@ export declare function getAllowedPaths(config: ProcessedConfig, manifest: SnapM
19
21
  * difficult to customize.
20
22
  *
21
23
  * @param config - The config object.
24
+ * @param middleware - An array of middleware functions to run before serving
25
+ * the static files.
22
26
  * @returns An object with a `listen` method that returns a promise that
23
27
  * resolves when the server is listening.
24
28
  */
25
- export declare function getServer(config: ProcessedConfig): {
29
+ export declare function getServer(config: ProcessedConfig, middleware?: Middleware[]): {
26
30
  listen: (port?: number) => Promise<{
27
31
  port: number;
28
32
  server: Server;
29
33
  close: () => Promise<void>;
30
34
  }>;
31
35
  };
36
+ export {};
32
37
  //# sourceMappingURL=server.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.mts","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAM1D,OAAO,KAAK,EAAmB,MAAM,EAAkB,aAAa;AAMpE,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAkB;AAgBjD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,YAAY,YA+CvB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,eAAe;;cAyErC,MAAM;gBACJ,MAAM;eACP,MAAM,QAAQ,IAAI,CAAC;;EA0B/B"}
1
+ {"version":3,"file":"server.d.mts","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,OAAO,EAAW,yBAAgB;AAEhD,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa;AAInC,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAkB;AAgBjD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,YAAY,YA+CvB;AAoBD,KAAK,UAAU,GAAG,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAC;AAEzC;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,eAAe,EACvB,UAAU,GAAE,UAAU,EAAO;;cA8CnB,MAAM;gBACJ,MAAM;eACP,MAAM,QAAQ,IAAI,CAAC;;EA2B/B"}