@metamask/snaps-utils 1.0.2 → 2.0.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 +33 -223
- package/dist/cjs/array.js +23 -0
- package/dist/cjs/array.js.map +1 -0
- package/dist/cjs/caveats.js +36 -0
- package/dist/cjs/caveats.js.map +1 -0
- package/dist/cjs/checksum.js +38 -0
- package/dist/cjs/checksum.js.map +1 -0
- package/dist/cjs/cronjob.js +84 -0
- package/dist/cjs/cronjob.js.map +1 -0
- package/dist/cjs/deep-clone.js +22 -0
- package/dist/cjs/deep-clone.js.map +1 -0
- package/dist/cjs/default-endowments.js +49 -0
- package/dist/cjs/default-endowments.js.map +1 -0
- package/dist/cjs/entropy.js +23 -0
- package/dist/cjs/entropy.js.map +1 -0
- package/dist/cjs/enum.js +16 -0
- package/dist/cjs/enum.js.map +1 -0
- package/dist/cjs/errors.js +19 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/{eval-worker.js → cjs/eval-worker.js} +22 -21
- package/dist/cjs/eval-worker.js.map +1 -0
- package/dist/cjs/eval.js +77 -0
- package/dist/cjs/eval.js.map +1 -0
- package/dist/cjs/fs.js +126 -0
- package/dist/cjs/fs.js.map +1 -0
- package/dist/cjs/handlers.js +80 -0
- package/dist/cjs/handlers.js.map +1 -0
- package/dist/cjs/icon.js +37 -0
- package/dist/cjs/icon.js.map +1 -0
- package/dist/cjs/iframe.js +59 -0
- package/dist/cjs/iframe.js.map +1 -0
- package/dist/cjs/index.browser.js +43 -0
- package/dist/cjs/index.browser.js.map +1 -0
- package/dist/cjs/index.executionenv.js +24 -0
- package/dist/cjs/index.executionenv.js.map +1 -0
- package/dist/cjs/index.js +48 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/json-rpc.js +46 -0
- package/dist/cjs/json-rpc.js.map +1 -0
- package/dist/cjs/json.js +16 -0
- package/dist/cjs/json.js.map +1 -0
- package/dist/cjs/logging.js +40 -0
- package/dist/cjs/logging.js.map +1 -0
- package/dist/cjs/manifest/index.browser.js +20 -0
- package/dist/cjs/manifest/index.browser.js.map +1 -0
- package/dist/cjs/manifest/index.js +21 -0
- package/dist/cjs/manifest/index.js.map +1 -0
- package/dist/cjs/manifest/manifest.js +239 -0
- package/dist/cjs/manifest/manifest.js.map +1 -0
- package/dist/cjs/manifest/validation.js +189 -0
- package/dist/cjs/manifest/validation.js.map +1 -0
- package/dist/cjs/mock.js +128 -0
- package/dist/cjs/mock.js.map +1 -0
- package/dist/cjs/namespace.js +132 -0
- package/dist/cjs/namespace.js.map +1 -0
- package/dist/cjs/npm.js +81 -0
- package/dist/cjs/npm.js.map +1 -0
- package/dist/cjs/path.js +21 -0
- package/dist/cjs/path.js.map +1 -0
- package/dist/cjs/post-process.js +328 -0
- package/dist/cjs/post-process.js.map +1 -0
- package/dist/cjs/snaps.js +230 -0
- package/dist/cjs/snaps.js.map +1 -0
- package/dist/cjs/strings.js +21 -0
- package/dist/cjs/strings.js.map +1 -0
- package/dist/cjs/structs.js +163 -0
- package/dist/cjs/structs.js.map +1 -0
- package/dist/cjs/types.js +109 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/validation.js +22 -0
- package/dist/cjs/validation.js.map +1 -0
- package/dist/cjs/versions.js +47 -0
- package/dist/cjs/versions.js.map +1 -0
- package/dist/cjs/virtual-file/VirtualFile.js +85 -0
- package/dist/cjs/virtual-file/VirtualFile.js.map +1 -0
- package/dist/cjs/virtual-file/index.browser.js +20 -0
- package/dist/cjs/virtual-file/index.browser.js.map +1 -0
- package/dist/cjs/virtual-file/index.js +21 -0
- package/dist/cjs/virtual-file/index.js.map +1 -0
- package/dist/cjs/virtual-file/toVirtualFile.js +33 -0
- package/dist/cjs/virtual-file/toVirtualFile.js.map +1 -0
- package/dist/{array.js → esm/array.js} +3 -7
- package/dist/esm/array.js.map +1 -0
- package/dist/esm/caveats.js +26 -0
- package/dist/esm/caveats.js.map +1 -0
- package/dist/esm/checksum.js +36 -0
- package/dist/esm/checksum.js.map +1 -0
- package/dist/esm/cronjob.js +66 -0
- package/dist/esm/cronjob.js.map +1 -0
- package/dist/esm/deep-clone.js +7 -0
- package/dist/esm/deep-clone.js.map +1 -0
- package/dist/{default-endowments.js → esm/default-endowments.js} +3 -6
- package/dist/esm/default-endowments.js.map +1 -0
- package/dist/esm/entropy.js +6 -0
- package/dist/esm/entropy.js.map +1 -0
- package/dist/esm/enum.js +12 -0
- package/dist/esm/enum.js.map +1 -0
- package/dist/esm/errors.js +17 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/eval-worker.js +47 -0
- package/dist/esm/eval-worker.js.map +1 -0
- package/dist/esm/eval.js +65 -0
- package/dist/esm/eval.js.map +1 -0
- package/dist/{fs.js → esm/fs.js} +33 -56
- package/dist/esm/fs.js.map +1 -0
- package/dist/esm/handlers.js +59 -0
- package/dist/esm/handlers.js.map +1 -0
- package/dist/esm/icon.js +11 -0
- package/dist/esm/icon.js.map +1 -0
- package/dist/{iframe.js → esm/iframe.js} +16 -18
- package/dist/esm/iframe.js.map +1 -0
- package/dist/esm/index.browser.js +26 -0
- package/dist/esm/index.browser.js.map +1 -0
- package/dist/esm/index.executionenv.js +7 -0
- package/dist/esm/index.executionenv.js.map +1 -0
- package/dist/esm/index.js +31 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/json-rpc.js +39 -0
- package/dist/esm/json-rpc.js.map +1 -0
- package/dist/esm/json.js +17 -0
- package/dist/esm/json.js.map +1 -0
- package/dist/{logging.js → esm/logging.js} +6 -14
- package/dist/esm/logging.js.map +1 -0
- package/dist/esm/manifest/index.browser.js +3 -0
- package/dist/esm/manifest/index.browser.js.map +1 -0
- package/dist/esm/manifest/index.js +4 -0
- package/dist/esm/manifest/index.js.map +1 -0
- package/dist/{manifest → esm/manifest}/manifest.js +101 -115
- package/dist/esm/manifest/manifest.js.map +1 -0
- package/dist/esm/manifest/validation.js +155 -0
- package/dist/esm/manifest/validation.js.map +1 -0
- package/dist/{mock.js → esm/mock.js} +42 -40
- package/dist/esm/mock.js.map +1 -0
- package/dist/esm/namespace.js +112 -0
- package/dist/esm/namespace.js.map +1 -0
- package/dist/esm/npm.js +70 -0
- package/dist/esm/npm.js.map +1 -0
- package/dist/esm/path.js +17 -0
- package/dist/esm/path.js.map +1 -0
- package/dist/{post-process.js → esm/post-process.js} +110 -99
- package/dist/esm/post-process.js.map +1 -0
- package/dist/esm/snaps.js +215 -0
- package/dist/esm/snaps.js.map +1 -0
- package/dist/esm/strings.js +11 -0
- package/dist/esm/strings.js.map +1 -0
- package/dist/esm/structs.js +230 -0
- package/dist/esm/structs.js.map +1 -0
- package/dist/esm/types.js +80 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/validation.js +17 -0
- package/dist/esm/validation.js.map +1 -0
- package/dist/{versions.js → esm/versions.js} +15 -18
- package/dist/esm/versions.js.map +1 -0
- package/dist/{virtual-file → esm/virtual-file}/VirtualFile.js +47 -33
- package/dist/esm/virtual-file/VirtualFile.js.map +1 -0
- package/dist/esm/virtual-file/index.browser.js +3 -0
- package/dist/esm/virtual-file/index.browser.js.map +1 -0
- package/dist/esm/virtual-file/index.js +4 -0
- package/dist/esm/virtual-file/index.js.map +1 -0
- package/dist/esm/virtual-file/toVirtualFile.js +26 -0
- package/dist/esm/virtual-file/toVirtualFile.js.map +1 -0
- package/dist/{caveats.d.ts → types/caveats.d.ts} +5 -5
- package/dist/{cronjob.d.ts → types/cronjob.d.ts} +1 -1
- package/dist/types/enum.d.ts +30 -0
- package/dist/types/errors.d.ts +10 -0
- package/dist/{eval.d.ts → types/eval.d.ts} +9 -1
- package/dist/{fs.d.ts → types/fs.d.ts} +2 -2
- package/dist/types/handlers.d.ts +196 -0
- package/dist/types/icon.d.ts +4 -0
- package/dist/{iframe.d.ts → types/iframe.d.ts} +3 -2
- package/dist/{index.browser.d.ts → types/index.browser.d.ts} +5 -1
- package/dist/{index.d.ts → types/index.d.ts} +5 -1
- package/dist/{json-rpc.d.ts → types/json-rpc.d.ts} +2 -2
- package/dist/types/json.d.ts +13 -0
- package/dist/{manifest → types/manifest}/manifest.d.ts +6 -4
- package/dist/{manifest → types/manifest}/validation.d.ts +40 -76
- package/dist/types/namespace.d.ts +128 -0
- package/dist/{npm.d.ts → types/npm.d.ts} +2 -3
- package/dist/{snaps.d.ts → types/snaps.d.ts} +17 -9
- package/dist/types/strings.d.ts +8 -0
- package/dist/types/structs.d.ts +158 -0
- package/dist/{types.d.ts → types/types.d.ts} +15 -22
- package/dist/types/validation.d.ts +8 -0
- package/dist/{versions.d.ts → types/versions.d.ts} +1 -1
- package/package.json +66 -46
- package/dist/array.js.map +0 -1
- package/dist/caveats.js +0 -35
- package/dist/caveats.js.map +0 -1
- package/dist/checksum.js +0 -42
- package/dist/checksum.js.map +0 -1
- package/dist/cronjob.js +0 -71
- package/dist/cronjob.js.map +0 -1
- package/dist/deep-clone.js +0 -9
- package/dist/deep-clone.js.map +0 -1
- package/dist/default-endowments.js.map +0 -1
- package/dist/entropy.js +0 -8
- package/dist/entropy.js.map +0 -1
- package/dist/eval-worker.js.map +0 -1
- package/dist/eval.js +0 -27
- package/dist/eval.js.map +0 -1
- package/dist/fs.js.map +0 -1
- package/dist/handlers.d.ts +0 -105
- package/dist/handlers.js +0 -3
- package/dist/handlers.js.map +0 -1
- package/dist/iframe.js.map +0 -1
- package/dist/iframe.test.browser.js +0 -15
- package/dist/iframe.test.browser.js.map +0 -1
- package/dist/index.browser.js +0 -37
- package/dist/index.browser.js.map +0 -1
- package/dist/index.executionenv.js +0 -22
- package/dist/index.executionenv.js.map +0 -1
- package/dist/index.js +0 -42
- package/dist/index.js.map +0 -1
- package/dist/json-rpc.js +0 -46
- package/dist/json-rpc.js.map +0 -1
- package/dist/json.d.ts +0 -9
- package/dist/json.js +0 -18
- package/dist/json.js.map +0 -1
- package/dist/logging.js.map +0 -1
- package/dist/manifest/index.browser.js +0 -18
- package/dist/manifest/index.browser.js.map +0 -1
- package/dist/manifest/index.js +0 -19
- package/dist/manifest/index.js.map +0 -1
- package/dist/manifest/manifest.js.map +0 -1
- package/dist/manifest/validation.js +0 -141
- package/dist/manifest/validation.js.map +0 -1
- package/dist/mock.js.map +0 -1
- package/dist/namespace.d.ts +0 -275
- package/dist/namespace.js +0 -225
- package/dist/namespace.js.map +0 -1
- package/dist/notification.d.ts +0 -66
- package/dist/notification.js +0 -58
- package/dist/notification.js.map +0 -1
- package/dist/npm.js +0 -74
- package/dist/npm.js.map +0 -1
- package/dist/path.js +0 -21
- package/dist/path.js.map +0 -1
- package/dist/post-process.js.map +0 -1
- package/dist/snaps.js +0 -202
- package/dist/snaps.js.map +0 -1
- package/dist/types.js +0 -103
- package/dist/types.js.map +0 -1
- package/dist/versions.js.map +0 -1
- package/dist/virtual-file/VirtualFile.js.map +0 -1
- package/dist/virtual-file/index.browser.js +0 -18
- package/dist/virtual-file/index.browser.js.map +0 -1
- package/dist/virtual-file/index.js +0 -19
- package/dist/virtual-file/index.js.map +0 -1
- package/dist/virtual-file/toVirtualFile.js +0 -30
- package/dist/virtual-file/toVirtualFile.js.map +0 -1
- /package/dist/{array.d.ts → types/array.d.ts} +0 -0
- /package/dist/{checksum.d.ts → types/checksum.d.ts} +0 -0
- /package/dist/{deep-clone.d.ts → types/deep-clone.d.ts} +0 -0
- /package/dist/{default-endowments.d.ts → types/default-endowments.d.ts} +0 -0
- /package/dist/{entropy.d.ts → types/entropy.d.ts} +0 -0
- /package/dist/{eval-worker.d.ts → types/eval-worker.d.ts} +0 -0
- /package/dist/{iframe.test.browser.d.ts → types/iframe.test.browser.d.ts} +0 -0
- /package/dist/{index.executionenv.d.ts → types/index.executionenv.d.ts} +0 -0
- /package/dist/{logging.d.ts → types/logging.d.ts} +0 -0
- /package/dist/{manifest → types/manifest}/index.browser.d.ts +0 -0
- /package/dist/{manifest → types/manifest}/index.d.ts +0 -0
- /package/dist/{mock.d.ts → types/mock.d.ts} +0 -0
- /package/dist/{path.d.ts → types/path.d.ts} +0 -0
- /package/dist/{post-process.d.ts → types/post-process.d.ts} +0 -0
- /package/dist/{virtual-file → types/virtual-file}/VirtualFile.d.ts +0 -0
- /package/dist/{virtual-file → types/virtual-file}/index.browser.d.ts +0 -0
- /package/dist/{virtual-file → types/virtual-file}/index.d.ts +0 -0
- /package/dist/{virtual-file → types/virtual-file}/toVirtualFile.d.ts +0 -0
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.postProcessBundle = exports.PostProcessWarning = void 0;
|
|
4
1
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var PostProcessWarning;
|
|
8
|
-
(function
|
|
9
|
-
PostProcessWarning["UnsafeMathRandom"] =
|
|
10
|
-
})(PostProcessWarning
|
|
2
|
+
import { transformSync, template } from '@babel/core';
|
|
3
|
+
import { binaryExpression, isUnaryExpression, isUpdateExpression, stringLiteral, templateElement, templateLiteral } from '@babel/types';
|
|
4
|
+
export var PostProcessWarning;
|
|
5
|
+
(function(PostProcessWarning) {
|
|
6
|
+
PostProcessWarning["UnsafeMathRandom"] = '`Math.random` was detected in the bundle. This is not a secure source of randomness.';
|
|
7
|
+
})(PostProcessWarning || (PostProcessWarning = {}));
|
|
11
8
|
// The RegEx below consists of multiple groups joined by a boolean OR.
|
|
12
9
|
// Each part consists of two groups which capture a part of each string
|
|
13
10
|
// which needs to be split up, e.g., `<!--` is split into `<!` and `--`.
|
|
14
11
|
const TOKEN_REGEX = /(<!)(--)|(--)(>)|(import)(\(.*?\))/gu;
|
|
15
12
|
// An empty template element, i.e., a part of a template literal without any
|
|
16
13
|
// value ("").
|
|
17
|
-
const EMPTY_TEMPLATE_ELEMENT =
|
|
18
|
-
|
|
14
|
+
const EMPTY_TEMPLATE_ELEMENT = templateElement({
|
|
15
|
+
raw: '',
|
|
16
|
+
cooked: ''
|
|
17
|
+
});
|
|
18
|
+
const evalWrapper = template.statement(`
|
|
19
19
|
(1, REF)(ARGS)
|
|
20
20
|
`);
|
|
21
|
-
const objectEvalWrapper =
|
|
21
|
+
const objectEvalWrapper = template.statement(`
|
|
22
22
|
(1, OBJECT.REF)
|
|
23
23
|
`);
|
|
24
|
-
const regeneratorRuntimeWrapper =
|
|
24
|
+
const regeneratorRuntimeWrapper = template.statement(`
|
|
25
25
|
var regeneratorRuntime;
|
|
26
26
|
`);
|
|
27
27
|
/**
|
|
@@ -35,13 +35,11 @@ const regeneratorRuntimeWrapper = core_1.template.statement(`
|
|
|
35
35
|
* @returns The string split into an array, in a way that it can be joined
|
|
36
36
|
* together to form the same string, but with the tokens separated into single
|
|
37
37
|
* array elements.
|
|
38
|
-
*/
|
|
39
|
-
function breakTokens(value) {
|
|
38
|
+
*/ function breakTokens(value) {
|
|
40
39
|
const tokens = value.split(TOKEN_REGEX);
|
|
41
|
-
return
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
.filter((token) => token !== '' && token !== undefined));
|
|
40
|
+
return tokens// TODO: The `split` above results in some values being `undefined`.
|
|
41
|
+
// There may be a better solution to avoid having to filter those out.
|
|
42
|
+
.filter((token)=>token !== '' && token !== undefined);
|
|
45
43
|
}
|
|
46
44
|
/**
|
|
47
45
|
* Breaks up tokens that would otherwise result in SES errors. The tokens are
|
|
@@ -53,47 +51,59 @@ function breakTokens(value) {
|
|
|
53
51
|
* @param value - The string value to break up.
|
|
54
52
|
* @returns The string split into a tuple consisting of the new template
|
|
55
53
|
* elements and string literal expressions.
|
|
56
|
-
*/
|
|
57
|
-
function breakTokensTemplateLiteral(value) {
|
|
54
|
+
*/ function breakTokensTemplateLiteral(value) {
|
|
58
55
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
59
56
|
// @ts-ignore `matchAll` is not available in ES2017, but this code
|
|
60
57
|
// should only be used in environments where the function is supported.
|
|
61
58
|
const matches = Array.from(value.matchAll(TOKEN_REGEX));
|
|
62
59
|
if (matches.length > 0) {
|
|
63
|
-
const output = matches.reduce(([elements, expressions], rawMatch, index, values)
|
|
64
|
-
const [, first, last] = rawMatch.filter((raw)
|
|
60
|
+
const output = matches.reduce(([elements, expressions], rawMatch, index, values)=>{
|
|
61
|
+
const [, first, last] = rawMatch.filter((raw)=>raw !== undefined);
|
|
65
62
|
// Slice the text in front of the match, which does not need to be
|
|
66
63
|
// broken up.
|
|
67
|
-
const prefix = value.slice(index === 0
|
|
68
|
-
? 0
|
|
69
|
-
: values[index - 1].index + values[index - 1][0].length, rawMatch.index);
|
|
64
|
+
const prefix = value.slice(index === 0 ? 0 : values[index - 1].index + values[index - 1][0].length, rawMatch.index);
|
|
70
65
|
return [
|
|
71
66
|
[
|
|
72
67
|
...elements,
|
|
73
|
-
|
|
68
|
+
templateElement({
|
|
74
69
|
raw: getRawTemplateValue(prefix),
|
|
75
|
-
cooked: prefix
|
|
70
|
+
cooked: prefix
|
|
76
71
|
}),
|
|
77
|
-
EMPTY_TEMPLATE_ELEMENT
|
|
72
|
+
EMPTY_TEMPLATE_ELEMENT
|
|
78
73
|
],
|
|
79
|
-
[
|
|
74
|
+
[
|
|
75
|
+
...expressions,
|
|
76
|
+
stringLiteral(first),
|
|
77
|
+
stringLiteral(last)
|
|
78
|
+
]
|
|
80
79
|
];
|
|
81
|
-
}, [
|
|
80
|
+
}, [
|
|
81
|
+
[],
|
|
82
|
+
[]
|
|
83
|
+
]);
|
|
82
84
|
// Add the text after the last match to the output.
|
|
83
85
|
const lastMatch = matches[matches.length - 1];
|
|
84
86
|
const suffix = value.slice(lastMatch.index + lastMatch[0].length);
|
|
85
87
|
return [
|
|
86
88
|
[
|
|
87
89
|
...output[0],
|
|
88
|
-
|
|
90
|
+
templateElement({
|
|
91
|
+
raw: getRawTemplateValue(suffix),
|
|
92
|
+
cooked: suffix
|
|
93
|
+
})
|
|
89
94
|
],
|
|
90
|
-
output[1]
|
|
95
|
+
output[1]
|
|
91
96
|
];
|
|
92
97
|
}
|
|
93
98
|
// If there are no matches, simply return the original value.
|
|
94
99
|
return [
|
|
95
|
-
[
|
|
96
|
-
|
|
100
|
+
[
|
|
101
|
+
templateElement({
|
|
102
|
+
raw: getRawTemplateValue(value),
|
|
103
|
+
cooked: value
|
|
104
|
+
})
|
|
105
|
+
],
|
|
106
|
+
[]
|
|
97
107
|
];
|
|
98
108
|
}
|
|
99
109
|
/**
|
|
@@ -103,8 +113,7 @@ function breakTokensTemplateLiteral(value) {
|
|
|
103
113
|
* @see https://github.com/babel/babel/issues/9242#issuecomment-532529613
|
|
104
114
|
* @param value - The cooked string to get the raw string for.
|
|
105
115
|
* @returns The value as raw value.
|
|
106
|
-
*/
|
|
107
|
-
function getRawTemplateValue(value) {
|
|
116
|
+
*/ function getRawTemplateValue(value) {
|
|
108
117
|
return value.replace(/\\|`|\$\{/gu, '\\$&');
|
|
109
118
|
}
|
|
110
119
|
/**
|
|
@@ -130,23 +139,19 @@ function getRawTemplateValue(value) {
|
|
|
130
139
|
* map will be generated instead.
|
|
131
140
|
* @returns An object containing the modified code, and source map, or null if
|
|
132
141
|
* the provided code is null.
|
|
133
|
-
*/
|
|
134
|
-
function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps, inputSourceMap, } = {}) {
|
|
142
|
+
*/ export function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps, inputSourceMap } = {}) {
|
|
135
143
|
const warnings = new Set();
|
|
136
|
-
const pre = ({ ast })
|
|
137
|
-
ast.comments?.forEach((comment)
|
|
144
|
+
const pre = ({ ast })=>{
|
|
145
|
+
ast.comments?.forEach((comment)=>{
|
|
138
146
|
// Break up tokens that could be parsed as HTML comment terminators. The
|
|
139
147
|
// regular expressions below are written strangely so as to avoid the
|
|
140
148
|
// appearance of such tokens in our source code. For reference:
|
|
141
149
|
// https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md
|
|
142
|
-
comment.value = comment.value
|
|
143
|
-
.replace(new RegExp(`<!${'--'}`, 'gu'), '< !--')
|
|
144
|
-
.replace(new RegExp(`${'--'}>`, 'gu'), '-- >')
|
|
145
|
-
.replace(/import(\(.*\))/gu, 'import\\$1');
|
|
150
|
+
comment.value = comment.value.replace(new RegExp(`<!${'--'}`, 'gu'), '< !--').replace(new RegExp(`${'--'}>`, 'gu'), '-- >').replace(/import(\(.*\))/gu, 'import\\$1');
|
|
146
151
|
});
|
|
147
152
|
};
|
|
148
153
|
const visitor = {
|
|
149
|
-
FunctionExpression(path) {
|
|
154
|
+
FunctionExpression (path) {
|
|
150
155
|
const { node } = path;
|
|
151
156
|
// Browserify provides the `Buffer` global as an argument to modules that
|
|
152
157
|
// use it, but this does not work in SES. Since we pass in `Buffer` as an
|
|
@@ -159,61 +164,52 @@ function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps,
|
|
|
159
164
|
// TODO: Since we're working on the AST level, we could check the scope
|
|
160
165
|
// of the function expression, and possibly prevent false positives?
|
|
161
166
|
if (node.type === 'FunctionExpression' && node.extra?.parenthesized) {
|
|
162
|
-
node.params = node.params.filter((param)
|
|
167
|
+
node.params = node.params.filter((param)=>!(param.type === 'Identifier' && param.name === 'Buffer'));
|
|
163
168
|
}
|
|
164
169
|
},
|
|
165
|
-
CallExpression(path) {
|
|
170
|
+
CallExpression (path) {
|
|
166
171
|
const { node } = path;
|
|
167
172
|
// Replace `eval(foo)` with `(1, eval)(foo)`.
|
|
168
173
|
if (node.callee.type === 'Identifier' && node.callee.name === 'eval') {
|
|
169
174
|
path.replaceWith(evalWrapper({
|
|
170
175
|
REF: node.callee,
|
|
171
|
-
ARGS: node.arguments
|
|
176
|
+
ARGS: node.arguments
|
|
172
177
|
}));
|
|
173
178
|
}
|
|
174
179
|
// Detect the use of `Math.random()` and add a warning.
|
|
175
|
-
if (node.callee.type === 'MemberExpression' &&
|
|
176
|
-
node.callee.object.type === 'Identifier' &&
|
|
177
|
-
node.callee.object.name === 'Math' &&
|
|
178
|
-
node.callee.property.type === 'Identifier' &&
|
|
179
|
-
node.callee.property.name === 'random') {
|
|
180
|
+
if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier' && node.callee.object.name === 'Math' && node.callee.property.type === 'Identifier' && node.callee.property.name === 'random') {
|
|
180
181
|
warnings.add(PostProcessWarning.UnsafeMathRandom);
|
|
181
182
|
}
|
|
182
183
|
},
|
|
183
|
-
MemberExpression(path) {
|
|
184
|
+
MemberExpression (path) {
|
|
184
185
|
const { node } = path;
|
|
185
186
|
// Replace `object.eval(foo)` with `(1, object.eval)(foo)`.
|
|
186
|
-
if (node.property.type === 'Identifier' &&
|
|
187
|
-
|
|
188
|
-
// We only apply this to MemberExpressions that are the callee of CallExpression
|
|
189
|
-
path.parent.type === 'CallExpression' &&
|
|
190
|
-
path.parent.callee === node) {
|
|
187
|
+
if (node.property.type === 'Identifier' && node.property.name === 'eval' && // We only apply this to MemberExpressions that are the callee of CallExpression
|
|
188
|
+
path.parent.type === 'CallExpression' && path.parent.callee === node) {
|
|
191
189
|
path.replaceWith(objectEvalWrapper({
|
|
192
190
|
OBJECT: node.object,
|
|
193
|
-
REF: node.property
|
|
191
|
+
REF: node.property
|
|
194
192
|
}));
|
|
195
193
|
}
|
|
196
194
|
},
|
|
197
|
-
Identifier(path) {
|
|
195
|
+
Identifier (path) {
|
|
198
196
|
const { node } = path;
|
|
199
197
|
// Insert `regeneratorRuntime` global if it's used in the code.
|
|
200
198
|
if (node.name === 'regeneratorRuntime') {
|
|
201
|
-
const program = path.findParent((parent)
|
|
199
|
+
const program = path.findParent((parent)=>parent.node.type === 'Program');
|
|
202
200
|
// We know that `program` is a Program node here, but this keeps
|
|
203
201
|
// TypeScript happy.
|
|
204
202
|
if (program?.node.type === 'Program') {
|
|
205
203
|
const body = program.node.body[0];
|
|
206
204
|
// This stops it from inserting `regeneratorRuntime` multiple times.
|
|
207
|
-
if (body.type === 'VariableDeclaration' &&
|
|
208
|
-
body.declarations[0].id.name ===
|
|
209
|
-
'regeneratorRuntime') {
|
|
205
|
+
if (body.type === 'VariableDeclaration' && body.declarations[0].id.name === 'regeneratorRuntime') {
|
|
210
206
|
return;
|
|
211
207
|
}
|
|
212
208
|
program?.node.body.unshift(regeneratorRuntimeWrapper());
|
|
213
209
|
}
|
|
214
210
|
}
|
|
215
211
|
},
|
|
216
|
-
TemplateLiteral(path) {
|
|
212
|
+
TemplateLiteral (path) {
|
|
217
213
|
const { node } = path;
|
|
218
214
|
// This checks if the template literal was visited before. Without this,
|
|
219
215
|
// it would cause an infinite loop resulting in a stack overflow. We can't
|
|
@@ -226,7 +222,7 @@ function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps,
|
|
|
226
222
|
// For reference:
|
|
227
223
|
// - https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md
|
|
228
224
|
// - https://github.com/MetaMask/snaps-monorepo/issues/505
|
|
229
|
-
const [replacementQuasis, replacementExpressions] = node.quasis.reduce(([elements, expressions], quasi, index)
|
|
225
|
+
const [replacementQuasis, replacementExpressions] = node.quasis.reduce(([elements, expressions], quasi, index)=>{
|
|
230
226
|
// Note: Template literals have two variants, "cooked" and "raw". Here
|
|
231
227
|
// we use the cooked version.
|
|
232
228
|
// https://exploringjs.com/impatient-js/ch_template-literals.html#template-strings-cooked-vs-raw
|
|
@@ -234,23 +230,35 @@ function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps,
|
|
|
234
230
|
// Only update the node if something changed.
|
|
235
231
|
if (tokens[0].length <= 1) {
|
|
236
232
|
return [
|
|
237
|
-
[
|
|
238
|
-
|
|
233
|
+
[
|
|
234
|
+
...elements,
|
|
235
|
+
quasi
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
...expressions,
|
|
239
|
+
node.expressions[index]
|
|
240
|
+
]
|
|
239
241
|
];
|
|
240
242
|
}
|
|
241
243
|
return [
|
|
242
|
-
[
|
|
244
|
+
[
|
|
245
|
+
...elements,
|
|
246
|
+
...tokens[0]
|
|
247
|
+
],
|
|
243
248
|
[
|
|
244
249
|
...expressions,
|
|
245
250
|
...tokens[1],
|
|
246
|
-
node.expressions[index]
|
|
247
|
-
]
|
|
251
|
+
node.expressions[index]
|
|
252
|
+
]
|
|
248
253
|
];
|
|
249
|
-
}, [
|
|
250
|
-
|
|
254
|
+
}, [
|
|
255
|
+
[],
|
|
256
|
+
[]
|
|
257
|
+
]);
|
|
258
|
+
path.replaceWith(templateLiteral(replacementQuasis, replacementExpressions.filter((expression)=>expression !== undefined)));
|
|
251
259
|
path.setData('visited', true);
|
|
252
260
|
},
|
|
253
|
-
StringLiteral(path) {
|
|
261
|
+
StringLiteral (path) {
|
|
254
262
|
const { node } = path;
|
|
255
263
|
// Break up tokens that could be parsed as HTML comment terminators, or
|
|
256
264
|
// `import()` statements.
|
|
@@ -262,25 +270,29 @@ function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps,
|
|
|
262
270
|
if (tokens.length <= 1) {
|
|
263
271
|
return;
|
|
264
272
|
}
|
|
265
|
-
const replacement = tokens
|
|
266
|
-
.slice(1)
|
|
267
|
-
.reduce((acc, value) => (0, types_1.binaryExpression)('+', acc, (0, types_1.stringLiteral)(value)), (0, types_1.stringLiteral)(tokens[0]));
|
|
273
|
+
const replacement = tokens.slice(1).reduce((acc, value)=>binaryExpression('+', acc, stringLiteral(value)), stringLiteral(tokens[0]));
|
|
268
274
|
path.replaceWith(replacement);
|
|
269
275
|
path.skip();
|
|
270
276
|
},
|
|
271
|
-
BinaryExpression(path) {
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
277
|
+
BinaryExpression (path) {
|
|
278
|
+
const { node } = path;
|
|
279
|
+
const errorMessage = 'Using HTML comments (`<!--` and `-->`) as operators is not allowed. The behaviour of ' + 'these comments is ambiguous, and differs per browser and environment. If you want ' + 'to use them as operators, break them up into separate characters, i.e., `a-- > b` ' + 'and `a < ! --b`.';
|
|
280
|
+
if (node.operator === '<' && isUnaryExpression(node.right) && isUpdateExpression(node.right.argument) && node.right.argument.operator === '--' && node.left.end && node.right.argument.argument.start) {
|
|
281
|
+
const expression = code.slice(node.left.end, node.right.argument.argument.start);
|
|
282
|
+
if (expression.includes('<!--')) {
|
|
283
|
+
throw new Error(errorMessage);
|
|
284
|
+
}
|
|
279
285
|
}
|
|
280
|
-
|
|
286
|
+
if (node.operator === '>' && isUpdateExpression(node.left) && node.left.operator === '--' && node.left.argument.end && node.right.start) {
|
|
287
|
+
const expression = code.slice(node.left.argument.end, node.right.start);
|
|
288
|
+
if (expression.includes('-->')) {
|
|
289
|
+
throw new Error(errorMessage);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
281
293
|
};
|
|
282
294
|
try {
|
|
283
|
-
const file =
|
|
295
|
+
const file = transformSync(code, {
|
|
284
296
|
// Prevent Babel from searching for a config file.
|
|
285
297
|
configFile: false,
|
|
286
298
|
parserOpts: {
|
|
@@ -288,7 +300,7 @@ function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps,
|
|
|
288
300
|
strictMode: true,
|
|
289
301
|
// If this is disabled, the AST does not include any comments. This is
|
|
290
302
|
// useful for performance reasons, and we use it for stripping comments.
|
|
291
|
-
attachComment: !stripComments
|
|
303
|
+
attachComment: !stripComments
|
|
292
304
|
},
|
|
293
305
|
// By default, Babel optimises bundles that exceed 500 KB, but that
|
|
294
306
|
// results in characters which look like HTML comments, which breaks SES.
|
|
@@ -299,11 +311,11 @@ function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps,
|
|
|
299
311
|
inputSourceMap,
|
|
300
312
|
sourceMaps,
|
|
301
313
|
plugins: [
|
|
302
|
-
()
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
]
|
|
314
|
+
()=>({
|
|
315
|
+
pre,
|
|
316
|
+
visitor
|
|
317
|
+
})
|
|
318
|
+
]
|
|
307
319
|
});
|
|
308
320
|
if (!file?.code) {
|
|
309
321
|
throw new Error('Bundled code is empty.');
|
|
@@ -311,12 +323,11 @@ function postProcessBundle(code, { stripComments = true, sourceMap: sourceMaps,
|
|
|
311
323
|
return {
|
|
312
324
|
code: file.code,
|
|
313
325
|
sourceMap: file.map,
|
|
314
|
-
warnings: Array.from(warnings)
|
|
326
|
+
warnings: Array.from(warnings)
|
|
315
327
|
};
|
|
316
|
-
}
|
|
317
|
-
catch (error) {
|
|
328
|
+
} catch (error) {
|
|
318
329
|
throw new Error(`Failed to post process code:\n${error.message}`);
|
|
319
330
|
}
|
|
320
331
|
}
|
|
321
|
-
|
|
332
|
+
|
|
322
333
|
//# sourceMappingURL=post-process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/post-process.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/no-shadow\nimport type { Node, Visitor, PluginObj } from '@babel/core';\nimport { transformSync, template } from '@babel/core';\nimport type { Expression, Identifier, TemplateElement } from '@babel/types';\nimport {\n binaryExpression,\n isUnaryExpression,\n isUpdateExpression,\n stringLiteral,\n templateElement,\n templateLiteral,\n} from '@babel/types';\n\n/**\n * Source map declaration taken from `@babel/core`. Babel doesn't export the\n * type for this, so it's copied from the source code instead here.\n */\nexport type SourceMap = {\n version: number;\n sources: string[];\n names: string[];\n sourceRoot?: string | undefined;\n sourcesContent?: string[] | undefined;\n mappings: string;\n file: string;\n};\n\n/**\n * The post process options.\n *\n * @property stripComments - Whether to strip comments. Defaults to `true`.\n * @property sourceMap - Whether to generate a source map for the modified code.\n * See also `inputSourceMap`.\n * @property inputSourceMap - The source map for the input code. When provided,\n * the source map will be used to generate a source map for the modified code.\n * This ensures that the source map is correct for the modified code, and still\n * points to the original source. If not provided, a new source map will be\n * generated instead.\n */\nexport type PostProcessOptions = {\n stripComments?: boolean;\n sourceMap?: boolean | 'inline';\n inputSourceMap?: SourceMap;\n};\n\n/**\n * The post processed bundle output.\n *\n * @property code - The modified code.\n * @property sourceMap - The source map for the modified code, if the source map\n * option was enabled.\n * @property warnings - Any warnings that occurred during the post-processing.\n */\nexport type PostProcessedBundle = {\n code: string;\n sourceMap?: SourceMap | null;\n warnings: PostProcessWarning[];\n};\n\nexport enum PostProcessWarning {\n UnsafeMathRandom = '`Math.random` was detected in the bundle. This is not a secure source of randomness.',\n}\n\n// The RegEx below consists of multiple groups joined by a boolean OR.\n// Each part consists of two groups which capture a part of each string\n// which needs to be split up, e.g., `<!--` is split into `<!` and `--`.\nconst TOKEN_REGEX = /(<!)(--)|(--)(>)|(import)(\\(.*?\\))/gu;\n\n// An empty template element, i.e., a part of a template literal without any\n// value (\"\").\nconst EMPTY_TEMPLATE_ELEMENT = templateElement({ raw: '', cooked: '' });\n\nconst evalWrapper = template.statement(`\n (1, REF)(ARGS)\n`);\n\nconst objectEvalWrapper = template.statement(`\n (1, OBJECT.REF)\n`);\n\nconst regeneratorRuntimeWrapper = template.statement(`\n var regeneratorRuntime;\n`);\n\n/**\n * Breaks up tokens that would otherwise result in SES errors. The tokens are\n * broken up in a non-destructive way where possible. Currently works with:\n * - HTML comment tags `<!--` and `-->`, broken up into `<!`, `--`, and `--`,\n * `>`.\n * - `import(n)` statements, broken up into `import`, `(n)`.\n *\n * @param value - The string value to break up.\n * @returns The string split into an array, in a way that it can be joined\n * together to form the same string, but with the tokens separated into single\n * array elements.\n */\nfunction breakTokens(value: string): string[] {\n const tokens = value.split(TOKEN_REGEX);\n return (\n tokens\n // TODO: The `split` above results in some values being `undefined`.\n // There may be a better solution to avoid having to filter those out.\n .filter((token) => token !== '' && token !== undefined)\n );\n}\n\n/**\n * Breaks up tokens that would otherwise result in SES errors. The tokens are\n * broken up in a non-destructive way where possible. Currently works with:\n * - HTML comment tags `<!--` and `-->`, broken up into `<!`, `--`, and `--`,\n * `>`.\n * - `import(n)` statements, broken up into `import`, `(n)`.\n *\n * @param value - The string value to break up.\n * @returns The string split into a tuple consisting of the new template\n * elements and string literal expressions.\n */\nfunction breakTokensTemplateLiteral(\n value: string,\n): [TemplateElement[], Expression[]] {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore `matchAll` is not available in ES2017, but this code\n // should only be used in environments where the function is supported.\n const matches: RegExpMatchArray[] = Array.from(value.matchAll(TOKEN_REGEX));\n\n if (matches.length > 0) {\n const output = matches.reduce<[TemplateElement[], Expression[]]>(\n ([elements, expressions], rawMatch, index, values) => {\n const [, first, last] = rawMatch.filter((raw) => raw !== undefined);\n\n // Slice the text in front of the match, which does not need to be\n // broken up.\n const prefix = value.slice(\n index === 0\n ? 0\n : (values[index - 1].index as number) + values[index - 1][0].length,\n rawMatch.index,\n );\n\n return [\n [\n ...elements,\n templateElement({\n raw: getRawTemplateValue(prefix),\n cooked: prefix,\n }),\n EMPTY_TEMPLATE_ELEMENT,\n ],\n [...expressions, stringLiteral(first), stringLiteral(last)],\n ];\n },\n [[], []],\n );\n\n // Add the text after the last match to the output.\n const lastMatch = matches[matches.length - 1];\n const suffix = value.slice(\n (lastMatch.index as number) + lastMatch[0].length,\n );\n\n return [\n [\n ...output[0],\n templateElement({ raw: getRawTemplateValue(suffix), cooked: suffix }),\n ],\n output[1],\n ];\n }\n\n // If there are no matches, simply return the original value.\n return [\n [templateElement({ raw: getRawTemplateValue(value), cooked: value })],\n [],\n ];\n}\n\n/**\n * Get a raw template literal value from a cooked value. This adds a backslash\n * before every '`', '\\' and '${' characters.\n *\n * @see https://github.com/babel/babel/issues/9242#issuecomment-532529613\n * @param value - The cooked string to get the raw string for.\n * @returns The value as raw value.\n */\nfunction getRawTemplateValue(value: string) {\n return value.replace(/\\\\|`|\\$\\{/gu, '\\\\$&');\n}\n\n/**\n * Post process code with AST such that it can be evaluated in SES.\n *\n * Currently:\n * - Makes all direct calls to eval indirect.\n * - Handles certain Babel-related edge cases.\n * - Removes the `Buffer` provided by Browserify.\n * - Optionally removes comments.\n * - Breaks up tokens that would otherwise result in SES errors, such as HTML\n * comment tags `<!--` and `-->` and `import(n)` statements.\n *\n * @param code - The code to post process.\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to `true`.\n * @param options.sourceMap - Whether to generate a source map for the modified\n * code. See also `inputSourceMap`.\n * @param options.inputSourceMap - The source map for the input code. When\n * provided, the source map will be used to generate a source map for the\n * modified code. This ensures that the source map is correct for the modified\n * code, and still points to the original source. If not provided, a new source\n * map will be generated instead.\n * @returns An object containing the modified code, and source map, or null if\n * the provided code is null.\n */\nexport function postProcessBundle(\n code: string,\n {\n stripComments = true,\n sourceMap: sourceMaps,\n inputSourceMap,\n }: Partial<PostProcessOptions> = {},\n): PostProcessedBundle {\n const warnings = new Set<PostProcessWarning>();\n\n const pre: PluginObj['pre'] = ({ ast }) => {\n ast.comments?.forEach((comment) => {\n // Break up tokens that could be parsed as HTML comment terminators. The\n // regular expressions below are written strangely so as to avoid the\n // appearance of such tokens in our source code. For reference:\n // https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md\n comment.value = comment.value\n .replace(new RegExp(`<!${'--'}`, 'gu'), '< !--')\n .replace(new RegExp(`${'--'}>`, 'gu'), '-- >')\n .replace(/import(\\(.*\\))/gu, 'import\\\\$1');\n });\n };\n\n const visitor: Visitor<Node> = {\n FunctionExpression(path) {\n const { node } = path;\n\n // Browserify provides the `Buffer` global as an argument to modules that\n // use it, but this does not work in SES. Since we pass in `Buffer` as an\n // endowment, we can simply remove the argument.\n //\n // Note that this only removes `Buffer` from a wrapped function\n // expression, e.g., `(function (Buffer) { ... })`. Regular functions\n // are not affected.\n //\n // TODO: Since we're working on the AST level, we could check the scope\n // of the function expression, and possibly prevent false positives?\n if (node.type === 'FunctionExpression' && node.extra?.parenthesized) {\n node.params = node.params.filter(\n (param) => !(param.type === 'Identifier' && param.name === 'Buffer'),\n );\n }\n },\n\n CallExpression(path) {\n const { node } = path;\n\n // Replace `eval(foo)` with `(1, eval)(foo)`.\n if (node.callee.type === 'Identifier' && node.callee.name === 'eval') {\n path.replaceWith(\n evalWrapper({\n REF: node.callee,\n ARGS: node.arguments,\n }),\n );\n }\n\n // Detect the use of `Math.random()` and add a warning.\n if (\n node.callee.type === 'MemberExpression' &&\n node.callee.object.type === 'Identifier' &&\n node.callee.object.name === 'Math' &&\n node.callee.property.type === 'Identifier' &&\n node.callee.property.name === 'random'\n ) {\n warnings.add(PostProcessWarning.UnsafeMathRandom);\n }\n },\n\n MemberExpression(path) {\n const { node } = path;\n\n // Replace `object.eval(foo)` with `(1, object.eval)(foo)`.\n if (\n node.property.type === 'Identifier' &&\n node.property.name === 'eval' &&\n // We only apply this to MemberExpressions that are the callee of CallExpression\n path.parent.type === 'CallExpression' &&\n path.parent.callee === node\n ) {\n path.replaceWith(\n objectEvalWrapper({\n OBJECT: node.object,\n REF: node.property,\n }),\n );\n }\n },\n\n Identifier(path) {\n const { node } = path;\n\n // Insert `regeneratorRuntime` global if it's used in the code.\n if (node.name === 'regeneratorRuntime') {\n const program = path.findParent(\n (parent) => parent.node.type === 'Program',\n );\n\n // We know that `program` is a Program node here, but this keeps\n // TypeScript happy.\n if (program?.node.type === 'Program') {\n const body = program.node.body[0];\n\n // This stops it from inserting `regeneratorRuntime` multiple times.\n if (\n body.type === 'VariableDeclaration' &&\n (body.declarations[0].id as Identifier).name ===\n 'regeneratorRuntime'\n ) {\n return;\n }\n\n program?.node.body.unshift(regeneratorRuntimeWrapper());\n }\n }\n },\n\n TemplateLiteral(path) {\n const { node } = path;\n\n // This checks if the template literal was visited before. Without this,\n // it would cause an infinite loop resulting in a stack overflow. We can't\n // skip the path here, because we need to visit the children of the node.\n if (path.getData('visited')) {\n return;\n }\n\n // Break up tokens that could be parsed as HTML comment terminators, or\n // `import()` statements.\n // For reference:\n // - https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md\n // - https://github.com/MetaMask/snaps-monorepo/issues/505\n const [replacementQuasis, replacementExpressions] = node.quasis.reduce<\n [TemplateElement[], Expression[]]\n >(\n ([elements, expressions], quasi, index) => {\n // Note: Template literals have two variants, \"cooked\" and \"raw\". Here\n // we use the cooked version.\n // https://exploringjs.com/impatient-js/ch_template-literals.html#template-strings-cooked-vs-raw\n const tokens = breakTokensTemplateLiteral(\n quasi.value.cooked as string,\n );\n\n // Only update the node if something changed.\n if (tokens[0].length <= 1) {\n return [\n [...elements, quasi],\n [...expressions, node.expressions[index] as Expression],\n ];\n }\n\n return [\n [...elements, ...tokens[0]],\n [\n ...expressions,\n ...tokens[1],\n node.expressions[index] as Expression,\n ],\n ];\n },\n [[], []],\n );\n\n path.replaceWith(\n templateLiteral(\n replacementQuasis,\n replacementExpressions.filter(\n (expression) => expression !== undefined,\n ),\n ) as Node,\n );\n\n path.setData('visited', true);\n },\n\n StringLiteral(path) {\n const { node } = path;\n\n // Break up tokens that could be parsed as HTML comment terminators, or\n // `import()` statements.\n // For reference:\n // - https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md\n // - https://github.com/MetaMask/snaps-monorepo/issues/505\n const tokens = breakTokens(node.value);\n\n // Only update the node if the string literal was broken up.\n if (tokens.length <= 1) {\n return;\n }\n\n const replacement = tokens\n .slice(1)\n .reduce<Expression>(\n (acc, value) => binaryExpression('+', acc, stringLiteral(value)),\n stringLiteral(tokens[0]),\n );\n\n path.replaceWith(replacement as Node);\n path.skip();\n },\n\n BinaryExpression(path) {\n const { node } = path;\n\n const errorMessage =\n 'Using HTML comments (`<!--` and `-->`) as operators is not allowed. The behaviour of ' +\n 'these comments is ambiguous, and differs per browser and environment. If you want ' +\n 'to use them as operators, break them up into separate characters, i.e., `a-- > b` ' +\n 'and `a < ! --b`.';\n\n if (\n node.operator === '<' &&\n isUnaryExpression(node.right) &&\n isUpdateExpression(node.right.argument) &&\n node.right.argument.operator === '--' &&\n node.left.end &&\n node.right.argument.argument.start\n ) {\n const expression = code.slice(\n node.left.end,\n node.right.argument.argument.start,\n );\n\n if (expression.includes('<!--')) {\n throw new Error(errorMessage);\n }\n }\n\n if (\n node.operator === '>' &&\n isUpdateExpression(node.left) &&\n node.left.operator === '--' &&\n node.left.argument.end &&\n node.right.start\n ) {\n const expression = code.slice(node.left.argument.end, node.right.start);\n\n if (expression.includes('-->')) {\n throw new Error(errorMessage);\n }\n }\n },\n };\n\n try {\n const file = transformSync(code, {\n // Prevent Babel from searching for a config file.\n configFile: false,\n\n parserOpts: {\n // Strict mode isn't enabled by default, so we need to enable it here.\n strictMode: true,\n\n // If this is disabled, the AST does not include any comments. This is\n // useful for performance reasons, and we use it for stripping comments.\n attachComment: !stripComments,\n },\n\n // By default, Babel optimises bundles that exceed 500 KB, but that\n // results in characters which look like HTML comments, which breaks SES.\n compact: false,\n\n // This configures Babel to generate a new source map from the existing\n // source map if specified. If `sourceMap` is `true` but an input source\n // map is not provided, a new source map will be generated instead.\n inputSourceMap,\n sourceMaps,\n\n plugins: [\n () => ({\n pre,\n visitor,\n }),\n ],\n });\n\n if (!file?.code) {\n throw new Error('Bundled code is empty.');\n }\n\n return {\n code: file.code,\n sourceMap: file.map,\n warnings: Array.from(warnings),\n };\n } catch (error) {\n throw new Error(`Failed to post process code:\\n${error.message}`);\n }\n}\n"],"names":["transformSync","template","binaryExpression","isUnaryExpression","isUpdateExpression","stringLiteral","templateElement","templateLiteral","PostProcessWarning","UnsafeMathRandom","TOKEN_REGEX","EMPTY_TEMPLATE_ELEMENT","raw","cooked","evalWrapper","statement","objectEvalWrapper","regeneratorRuntimeWrapper","breakTokens","value","tokens","split","filter","token","undefined","breakTokensTemplateLiteral","matches","Array","from","matchAll","length","output","reduce","elements","expressions","rawMatch","index","values","first","last","prefix","slice","getRawTemplateValue","lastMatch","suffix","replace","postProcessBundle","code","stripComments","sourceMap","sourceMaps","inputSourceMap","warnings","Set","pre","ast","comments","forEach","comment","RegExp","visitor","FunctionExpression","path","node","type","extra","parenthesized","params","param","name","CallExpression","callee","replaceWith","REF","ARGS","arguments","object","property","add","MemberExpression","parent","OBJECT","Identifier","program","findParent","body","declarations","id","unshift","TemplateLiteral","getData","replacementQuasis","replacementExpressions","quasis","quasi","expression","setData","StringLiteral","replacement","acc","skip","BinaryExpression","errorMessage","operator","right","argument","left","end","start","includes","Error","file","configFile","parserOpts","strictMode","attachComment","compact","plugins","map","error","message"],"mappings":"AAAA,wDAAwD;AAExD,SAASA,aAAa,EAAEC,QAAQ,QAAQ,cAAc;AAEtD,SACEC,gBAAgB,EAChBC,iBAAiB,EACjBC,kBAAkB,EAClBC,aAAa,EACbC,eAAe,EACfC,eAAe,QACV,eAAe;WAgDf;UAAKC,kBAAkB;IAAlBA,mBACVC,sBAAmB;GADTD,uBAAAA;AAIZ,sEAAsE;AACtE,uEAAuE;AACvE,wEAAwE;AACxE,MAAME,cAAc;AAEpB,4EAA4E;AAC5E,cAAc;AACd,MAAMC,yBAAyBL,gBAAgB;IAAEM,KAAK;IAAIC,QAAQ;AAAG;AAErE,MAAMC,cAAcb,SAASc,SAAS,CAAC,CAAC;;AAExC,CAAC;AAED,MAAMC,oBAAoBf,SAASc,SAAS,CAAC,CAAC;;AAE9C,CAAC;AAED,MAAME,4BAA4BhB,SAASc,SAAS,CAAC,CAAC;;AAEtD,CAAC;AAED;;;;;;;;;;;CAWC,GACD,SAASG,YAAYC,KAAa;IAChC,MAAMC,SAASD,MAAME,KAAK,CAACX;IAC3B,OACEU,MACE,oEAAoE;IACpE,sEAAsE;KACrEE,MAAM,CAAC,CAACC,QAAUA,UAAU,MAAMA,UAAUC;AAEnD;AAEA;;;;;;;;;;CAUC,GACD,SAASC,2BACPN,KAAa;IAEb,6DAA6D;IAC7D,kEAAkE;IAClE,uEAAuE;IACvE,MAAMO,UAA8BC,MAAMC,IAAI,CAACT,MAAMU,QAAQ,CAACnB;IAE9D,IAAIgB,QAAQI,MAAM,GAAG,GAAG;QACtB,MAAMC,SAASL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