@metamask/snaps-cli 6.7.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.
- package/CHANGELOG.md +45 -1
- package/README.md +12 -244
- package/dist/builders.cjs +5 -97
- package/dist/builders.cjs.map +1 -1
- package/dist/builders.d.cts +5 -94
- package/dist/builders.d.cts.map +1 -1
- package/dist/builders.d.mts +5 -94
- package/dist/builders.d.mts.map +1 -1
- package/dist/builders.mjs +5 -96
- package/dist/builders.mjs.map +1 -1
- package/dist/cli.cjs +4 -9
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.cts.map +1 -1
- package/dist/cli.d.mts.map +1 -1
- package/dist/cli.mjs +4 -16
- package/dist/cli.mjs.map +1 -1
- package/dist/commands/build/build.cjs +15 -11
- package/dist/commands/build/build.cjs.map +1 -1
- package/dist/commands/build/build.d.cts +8 -0
- package/dist/commands/build/build.d.cts.map +1 -1
- package/dist/commands/build/build.d.mts +8 -0
- package/dist/commands/build/build.d.mts.map +1 -1
- package/dist/commands/build/build.mjs +9 -5
- package/dist/commands/build/build.mjs.map +1 -1
- package/dist/commands/build/implementation.cjs.map +1 -1
- package/dist/commands/build/implementation.d.cts +2 -2
- package/dist/commands/build/implementation.d.cts.map +1 -1
- package/dist/commands/build/implementation.d.mts +2 -2
- package/dist/commands/build/implementation.d.mts.map +1 -1
- package/dist/commands/build/implementation.mjs.map +1 -1
- package/dist/commands/build/index.cjs +5 -14
- package/dist/commands/build/index.cjs.map +1 -1
- package/dist/commands/build/index.d.cts +1 -0
- package/dist/commands/build/index.d.cts.map +1 -1
- package/dist/commands/build/index.d.mts +1 -0
- package/dist/commands/build/index.d.mts.map +1 -1
- package/dist/commands/build/index.mjs +3 -14
- package/dist/commands/build/index.mjs.map +1 -1
- package/dist/commands/eval/__test__/webpack/bad/snap.config.cjs +0 -1
- package/dist/commands/eval/__test__/webpack/bad/snap.config.cjs.map +1 -1
- package/dist/commands/eval/__test__/webpack/bad/snap.config.d.cts.map +1 -1
- package/dist/commands/eval/__test__/webpack/bad/snap.config.d.mts.map +1 -1
- package/dist/commands/eval/__test__/webpack/bad/snap.config.mjs +0 -1
- package/dist/commands/eval/__test__/webpack/bad/snap.config.mjs.map +1 -1
- package/dist/commands/eval/__test__/webpack/good/snap.config.cjs +0 -1
- package/dist/commands/eval/__test__/webpack/good/snap.config.cjs.map +1 -1
- package/dist/commands/eval/__test__/webpack/good/snap.config.d.cts.map +1 -1
- package/dist/commands/eval/__test__/webpack/good/snap.config.d.mts.map +1 -1
- package/dist/commands/eval/__test__/webpack/good/snap.config.mjs +0 -1
- package/dist/commands/eval/__test__/webpack/good/snap.config.mjs.map +1 -1
- package/dist/commands/eval/__test__/webpack/snap.config.cjs +0 -1
- package/dist/commands/eval/__test__/webpack/snap.config.cjs.map +1 -1
- package/dist/commands/eval/__test__/webpack/snap.config.d.cts.map +1 -1
- package/dist/commands/eval/__test__/webpack/snap.config.d.mts.map +1 -1
- package/dist/commands/eval/__test__/webpack/snap.config.mjs +0 -1
- package/dist/commands/eval/__test__/webpack/snap.config.mjs.map +1 -1
- package/dist/commands/eval/eval.cjs +1 -1
- package/dist/commands/eval/eval.cjs.map +1 -1
- package/dist/commands/eval/eval.d.cts.map +1 -1
- package/dist/commands/eval/eval.d.mts.map +1 -1
- package/dist/commands/eval/eval.mjs +1 -1
- package/dist/commands/eval/eval.mjs.map +1 -1
- package/dist/commands/eval/index.cjs +1 -4
- package/dist/commands/eval/index.cjs.map +1 -1
- package/dist/commands/eval/index.d.cts.map +1 -1
- package/dist/commands/eval/index.d.mts.map +1 -1
- package/dist/commands/eval/index.mjs +1 -4
- package/dist/commands/eval/index.mjs.map +1 -1
- package/dist/commands/index.cjs +2 -0
- package/dist/commands/index.cjs.map +1 -1
- package/dist/commands/index.d.cts.map +1 -1
- package/dist/commands/index.d.mts.map +1 -1
- package/dist/commands/index.mjs +2 -0
- package/dist/commands/index.mjs.map +1 -1
- package/dist/commands/manifest/index.cjs +1 -4
- package/dist/commands/manifest/index.cjs.map +1 -1
- package/dist/commands/manifest/index.d.cts.map +1 -1
- package/dist/commands/manifest/index.d.mts.map +1 -1
- package/dist/commands/manifest/index.mjs +1 -4
- package/dist/commands/manifest/index.mjs.map +1 -1
- package/dist/commands/manifest/manifest.cjs +5 -6
- package/dist/commands/manifest/manifest.cjs.map +1 -1
- package/dist/commands/manifest/manifest.d.cts.map +1 -1
- package/dist/commands/manifest/manifest.d.mts.map +1 -1
- package/dist/commands/manifest/manifest.mjs +5 -6
- package/dist/commands/manifest/manifest.mjs.map +1 -1
- package/dist/commands/sandbox/index.cjs +17 -0
- package/dist/commands/sandbox/index.cjs.map +1 -0
- package/dist/commands/sandbox/index.d.cts +10 -0
- package/dist/commands/sandbox/index.d.cts.map +1 -0
- package/dist/commands/sandbox/index.d.mts +10 -0
- package/dist/commands/sandbox/index.d.mts.map +1 -0
- package/dist/commands/sandbox/index.mjs +12 -0
- package/dist/commands/sandbox/index.mjs.map +1 -0
- package/dist/commands/sandbox/sandbox.cjs +33 -0
- package/dist/commands/sandbox/sandbox.cjs.map +1 -0
- package/dist/commands/sandbox/sandbox.d.cts +14 -0
- package/dist/commands/sandbox/sandbox.d.cts.map +1 -0
- package/dist/commands/sandbox/sandbox.d.mts +14 -0
- package/dist/commands/sandbox/sandbox.d.mts.map +1 -0
- package/dist/commands/sandbox/sandbox.mjs +29 -0
- package/dist/commands/sandbox/sandbox.mjs.map +1 -0
- package/dist/commands/sandbox/server.cjs +27 -0
- package/dist/commands/sandbox/server.cjs.map +1 -0
- package/dist/commands/sandbox/server.d.cts +14 -0
- package/dist/commands/sandbox/server.d.cts.map +1 -0
- package/dist/commands/sandbox/server.d.mts +14 -0
- package/dist/commands/sandbox/server.d.mts.map +1 -0
- package/dist/commands/sandbox/server.mjs +25 -0
- package/dist/commands/sandbox/server.mjs.map +1 -0
- package/dist/commands/serve/index.cjs +2 -2
- package/dist/commands/serve/index.cjs.map +1 -1
- package/dist/commands/serve/index.d.cts.map +1 -1
- package/dist/commands/serve/index.d.mts.map +1 -1
- package/dist/commands/serve/index.mjs +2 -2
- package/dist/commands/serve/index.mjs.map +1 -1
- package/dist/commands/watch/implementation.cjs +2 -0
- package/dist/commands/watch/implementation.cjs.map +1 -1
- package/dist/commands/watch/implementation.d.cts +2 -2
- package/dist/commands/watch/implementation.d.cts.map +1 -1
- package/dist/commands/watch/implementation.d.mts +2 -2
- package/dist/commands/watch/implementation.d.mts.map +1 -1
- package/dist/commands/watch/implementation.mjs +2 -0
- package/dist/commands/watch/implementation.mjs.map +1 -1
- package/dist/commands/watch/index.cjs +2 -17
- package/dist/commands/watch/index.cjs.map +1 -1
- package/dist/commands/watch/index.d.cts.map +1 -1
- package/dist/commands/watch/index.d.mts.map +1 -1
- package/dist/commands/watch/index.mjs +2 -17
- package/dist/commands/watch/index.mjs.map +1 -1
- package/dist/commands/watch/watch.cjs +1 -1
- package/dist/commands/watch/watch.cjs.map +1 -1
- package/dist/commands/watch/watch.d.cts.map +1 -1
- package/dist/commands/watch/watch.d.mts.map +1 -1
- package/dist/commands/watch/watch.mjs +1 -1
- package/dist/commands/watch/watch.mjs.map +1 -1
- package/dist/config.cjs +11 -155
- package/dist/config.cjs.map +1 -1
- package/dist/config.d.cts +216 -314
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.mts +216 -314
- package/dist/config.d.mts.map +1 -1
- package/dist/config.mjs +13 -155
- package/dist/config.mjs.map +1 -1
- package/dist/main.cjs +0 -1
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +0 -1
- package/dist/main.mjs.map +1 -1
- package/dist/utils/index.cjs +0 -1
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +0 -1
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts +0 -1
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +0 -1
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/steps.cjs.map +1 -1
- package/dist/utils/steps.d.cts +1 -1
- package/dist/utils/steps.d.cts.map +1 -1
- package/dist/utils/steps.d.mts +1 -1
- package/dist/utils/steps.d.mts.map +1 -1
- package/dist/utils/steps.mjs.map +1 -1
- package/dist/webpack/compiler.cjs.map +1 -1
- package/dist/webpack/compiler.d.cts +2 -2
- package/dist/webpack/compiler.d.cts.map +1 -1
- package/dist/webpack/compiler.d.mts +2 -2
- package/dist/webpack/compiler.d.mts.map +1 -1
- package/dist/webpack/compiler.mjs.map +1 -1
- package/dist/webpack/config.cjs +1 -2
- package/dist/webpack/config.cjs.map +1 -1
- package/dist/webpack/config.d.cts +2 -2
- package/dist/webpack/config.d.cts.map +1 -1
- package/dist/webpack/config.d.mts +2 -2
- package/dist/webpack/config.d.mts.map +1 -1
- package/dist/webpack/config.mjs +1 -2
- package/dist/webpack/config.mjs.map +1 -1
- package/dist/webpack/loaders/function.cjs +7 -10
- package/dist/webpack/loaders/function.cjs.map +1 -1
- package/dist/webpack/loaders/function.d.cts +1 -0
- package/dist/webpack/loaders/function.d.cts.map +1 -1
- package/dist/webpack/loaders/function.d.mts +1 -0
- package/dist/webpack/loaders/function.d.mts.map +1 -1
- package/dist/webpack/loaders/function.mjs +6 -9
- package/dist/webpack/loaders/function.mjs.map +1 -1
- package/dist/webpack/loaders/index.cjs +1 -3
- package/dist/webpack/loaders/index.cjs.map +1 -1
- package/dist/webpack/loaders/index.d.cts +0 -1
- package/dist/webpack/loaders/index.d.cts.map +1 -1
- package/dist/webpack/loaders/index.d.mts +0 -1
- package/dist/webpack/loaders/index.d.mts.map +1 -1
- package/dist/webpack/loaders/index.mjs +0 -1
- package/dist/webpack/loaders/index.mjs.map +1 -1
- package/dist/webpack/loaders/wasm.cjs +0 -2
- package/dist/webpack/loaders/wasm.cjs.map +1 -1
- package/dist/webpack/loaders/wasm.d.cts.map +1 -1
- package/dist/webpack/loaders/wasm.d.mts.map +1 -1
- package/dist/webpack/loaders/wasm.mjs +0 -2
- package/dist/webpack/loaders/wasm.mjs.map +1 -1
- package/dist/webpack/plugins.cjs +15 -20
- package/dist/webpack/plugins.cjs.map +1 -1
- package/dist/webpack/plugins.d.cts +13 -3
- package/dist/webpack/plugins.d.cts.map +1 -1
- package/dist/webpack/plugins.d.mts +13 -3
- package/dist/webpack/plugins.d.mts.map +1 -1
- package/dist/webpack/plugins.mjs +4 -9
- package/dist/webpack/plugins.mjs.map +1 -1
- package/dist/webpack/server.cjs +83 -72
- package/dist/webpack/server.cjs.map +1 -1
- package/dist/webpack/server.d.cts +6 -1
- package/dist/webpack/server.d.cts.map +1 -1
- package/dist/webpack/server.d.mts +6 -1
- package/dist/webpack/server.d.mts.map +1 -1
- package/dist/webpack/server.mjs +63 -72
- package/dist/webpack/server.mjs.map +1 -1
- package/dist/webpack/utils.cjs +5 -21
- package/dist/webpack/utils.cjs.map +1 -1
- package/dist/webpack/utils.d.cts +6 -36
- package/dist/webpack/utils.d.cts.map +1 -1
- package/dist/webpack/utils.d.mts +6 -36
- package/dist/webpack/utils.d.mts.map +1 -1
- package/dist/webpack/utils.mjs +4 -18
- package/dist/webpack/utils.mjs.map +1 -1
- package/package.json +16 -40
- package/dist/commands/eval/__test__/browserify/bad/snap.config.cjs +0 -10
- package/dist/commands/eval/__test__/browserify/bad/snap.config.cjs.map +0 -1
- package/dist/commands/eval/__test__/browserify/bad/snap.config.d.cts +0 -4
- package/dist/commands/eval/__test__/browserify/bad/snap.config.d.cts.map +0 -1
- package/dist/commands/eval/__test__/browserify/bad/snap.config.d.mts +0 -4
- package/dist/commands/eval/__test__/browserify/bad/snap.config.d.mts.map +0 -1
- package/dist/commands/eval/__test__/browserify/bad/snap.config.mjs +0 -8
- package/dist/commands/eval/__test__/browserify/bad/snap.config.mjs.map +0 -1
- package/dist/commands/eval/__test__/browserify/good/snap.config.cjs +0 -10
- package/dist/commands/eval/__test__/browserify/good/snap.config.cjs.map +0 -1
- package/dist/commands/eval/__test__/browserify/good/snap.config.d.cts +0 -4
- package/dist/commands/eval/__test__/browserify/good/snap.config.d.cts.map +0 -1
- package/dist/commands/eval/__test__/browserify/good/snap.config.d.mts +0 -4
- package/dist/commands/eval/__test__/browserify/good/snap.config.d.mts.map +0 -1
- package/dist/commands/eval/__test__/browserify/good/snap.config.mjs +0 -8
- package/dist/commands/eval/__test__/browserify/good/snap.config.mjs.map +0 -1
- package/dist/utils/legacy.cjs +0 -53
- package/dist/utils/legacy.cjs.map +0 -1
- package/dist/utils/legacy.d.cts +0 -28
- package/dist/utils/legacy.d.cts.map +0 -1
- package/dist/utils/legacy.d.mts +0 -28
- package/dist/utils/legacy.d.mts.map +0 -1
- package/dist/utils/legacy.mjs +0 -47
- package/dist/utils/legacy.mjs.map +0 -1
- package/dist/webpack/loaders/browserify.cjs +0 -84
- package/dist/webpack/loaders/browserify.cjs.map +0 -1
- package/dist/webpack/loaders/browserify.d.cts +0 -18
- package/dist/webpack/loaders/browserify.d.cts.map +0 -1
- package/dist/webpack/loaders/browserify.d.mts +0 -18
- package/dist/webpack/loaders/browserify.d.mts.map +0 -1
- package/dist/webpack/loaders/browserify.mjs +0 -88
- package/dist/webpack/loaders/browserify.mjs.map +0 -1
package/dist/webpack/server.cjs
CHANGED
|
@@ -1,13 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
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
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
136
|
-
|
|
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 };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.cjs","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":"
|
|
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,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CAAC,CAAC;QACpD,CAAC;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;
|
|
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;
|
|
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"}
|
package/dist/webpack/server.mjs
CHANGED
|
@@ -4,11 +4,10 @@ function $importDefault(module) {
|
|
|
4
4
|
}
|
|
5
5
|
return module;
|
|
6
6
|
}
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
136
|
-
|
|
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,
|
|
1
|
+
{"version":3,"file":"server.mjs","sourceRoot":"","sources":["../../src/webpack/server.ts"],"names":[],"mappings":";;;;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E,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","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/dist/webpack/utils.cjs
CHANGED
|
@@ -10,10 +10,7 @@ const fs_1 = require("fs");
|
|
|
10
10
|
const module_1 = require("module");
|
|
11
11
|
const path_1 = require("path");
|
|
12
12
|
const strip_ansi_1 = __importDefault(require("strip-ansi/index.js"));
|
|
13
|
-
|
|
14
|
-
exports.BROWSERSLIST_FILE = (0, path_1.resolve)((0, path_1.dirname)(
|
|
15
|
-
// eslint-disable-next-line n/no-extraneous-require
|
|
16
|
-
require.resolve('@metamask/snaps-cli/package.json')), '.browserslistrc');
|
|
13
|
+
exports.BROWSERSLIST_FILE = (0, path_1.resolve)((0, path_1.dirname)(require.resolve('@metamask/snaps-cli/package.json')), '.browserslistrc');
|
|
17
14
|
exports.WEBPACK_FALLBACKS = {
|
|
18
15
|
assert: require.resolve('assert/'),
|
|
19
16
|
buffer: require.resolve('buffer/'),
|
|
@@ -50,28 +47,15 @@ exports.WEBPACK_FALLBACKS = {
|
|
|
50
47
|
* Get the default loader for JavaScript and TypeScript files, based on the
|
|
51
48
|
* config object.
|
|
52
49
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the
|
|
56
|
-
* `SWC` compiler, which is a much faster alternative to Babel and TypeScript's
|
|
57
|
-
* own compiler.
|
|
50
|
+
* We use the `swc-loader`, which is a Webpack loader that uses the `SWC`
|
|
51
|
+
* compiler, a much faster alternative to Babel and TypeScript's own compiler.
|
|
58
52
|
*
|
|
59
53
|
* @param config - The processed snap Webpack config.
|
|
60
|
-
* @param config.legacy - The legacy config object, if any.
|
|
61
54
|
* @param config.sourceMap - Whether to generate source maps.
|
|
62
55
|
* @see https://swc.rs/docs/usage/swc-loader
|
|
63
56
|
* @returns The default loader.
|
|
64
57
|
*/
|
|
65
|
-
async function getDefaultLoader({
|
|
66
|
-
if (legacy) {
|
|
67
|
-
/**
|
|
68
|
-
* If the snap uses the legacy config, we use the custom `browserify`
|
|
69
|
-
* loader. This uses the legacy Browserify config to transpile the code.
|
|
70
|
-
* This is necessary for backwards compatibility with the
|
|
71
|
-
* `bundlerCustomizer` function.
|
|
72
|
-
*/
|
|
73
|
-
return (0, loaders_1.getFunctionLoader)(loaders_1.browserify, legacy);
|
|
74
|
-
}
|
|
58
|
+
async function getDefaultLoader({ sourceMap }) {
|
|
75
59
|
const targets = await getBrowserslistTargets();
|
|
76
60
|
return {
|
|
77
61
|
/**
|
|
@@ -179,7 +163,7 @@ function getDevTool(sourceMap) {
|
|
|
179
163
|
if (sourceMap === 'inline') {
|
|
180
164
|
return 'inline-source-map';
|
|
181
165
|
}
|
|
182
|
-
if (sourceMap
|
|
166
|
+
if (sourceMap) {
|
|
183
167
|
return 'source-map';
|
|
184
168
|
}
|
|
185
169
|
return false;
|