ses 0.14.2 → 0.15.1
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 +64 -0
- package/NEWS.md +60 -1
- package/README.md +0 -1
- package/dist/lockdown.cjs +1769 -992
- package/dist/lockdown.mjs +1769 -992
- package/dist/lockdown.umd.js +1769 -992
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +1769 -992
- package/dist/ses.mjs +1769 -992
- package/dist/ses.umd.js +1769 -992
- package/dist/ses.umd.min.js +1 -1
- package/index.d.ts +2 -1
- package/index.js +6 -13
- package/package.json +24 -11
- package/src/commons.js +44 -9
- package/src/compartment-evaluate.js +102 -0
- package/src/compartment-shim.js +78 -106
- package/src/enable-property-overrides.js +5 -4
- package/src/enablements.js +7 -2
- package/src/environment-options.js +125 -0
- package/src/error/assert.js +51 -39
- package/src/error/console.js +32 -14
- package/src/error/fatal-assert.js +2 -0
- package/src/error/stringify-utils.js +26 -3
- package/src/error/tame-console.js +12 -4
- package/src/error/tame-error-constructor.js +18 -17
- package/src/error/tame-v8-error-constructor.js +30 -14
- package/src/get-anonymous-intrinsics.js +11 -8
- package/src/get-source-url.js +4 -4
- package/src/global-object.js +33 -26
- package/src/intrinsics.js +76 -67
- package/src/lockdown-shim.js +111 -119
- package/src/make-eval-function.js +3 -5
- package/src/make-function-constructor.js +3 -4
- package/src/make-safe-evaluator.js +117 -0
- package/src/module-instance.js +31 -17
- package/src/module-link.js +11 -4
- package/src/module-load.js +169 -44
- package/src/module-proxy.js +12 -4
- package/src/scope-constants.js +6 -3
- package/src/scope-handler.js +41 -17
- package/src/tame-date-constructor.js +8 -2
- package/src/tame-domains.js +46 -0
- package/src/tame-function-constructors.js +1 -0
- package/src/tame-locale-methods.js +6 -6
- package/src/tame-math-object.js +2 -2
- package/src/tame-regexp-constructor.js +7 -7
- package/src/transforms.js +13 -6
- package/src/whitelist-intrinsics.js +9 -3
- package/src/evaluate.js +0 -98
package/src/enablements.js
CHANGED
|
@@ -103,34 +103,39 @@ export const moderateEnablements = {
|
|
|
103
103
|
'%ErrorPrototype%': {
|
|
104
104
|
constructor: true, // set by "fast-json-patch", "node-fetch"
|
|
105
105
|
message: true,
|
|
106
|
-
name: true, // set by "precond", "ava", "node-fetch"
|
|
106
|
+
name: true, // set by "precond", "ava", "node-fetch", "node 14"
|
|
107
107
|
toString: true, // set by "bluebird"
|
|
108
108
|
},
|
|
109
109
|
|
|
110
110
|
'%TypeErrorPrototype%': {
|
|
111
111
|
constructor: true, // set by "readable-stream"
|
|
112
112
|
message: true, // set by "tape"
|
|
113
|
-
name: true, // set by "readable-stream"
|
|
113
|
+
name: true, // set by "readable-stream", "node 14"
|
|
114
114
|
},
|
|
115
115
|
|
|
116
116
|
'%SyntaxErrorPrototype%': {
|
|
117
117
|
message: true, // to match TypeErrorPrototype.message
|
|
118
|
+
name: true, // set by "node 14"
|
|
118
119
|
},
|
|
119
120
|
|
|
120
121
|
'%RangeErrorPrototype%': {
|
|
121
122
|
message: true, // to match TypeErrorPrototype.message
|
|
123
|
+
name: true, // set by "node 14"
|
|
122
124
|
},
|
|
123
125
|
|
|
124
126
|
'%URIErrorPrototype%': {
|
|
125
127
|
message: true, // to match TypeErrorPrototype.message
|
|
128
|
+
name: true, // set by "node 14"
|
|
126
129
|
},
|
|
127
130
|
|
|
128
131
|
'%EvalErrorPrototype%': {
|
|
129
132
|
message: true, // to match TypeErrorPrototype.message
|
|
133
|
+
name: true, // set by "node 14"
|
|
130
134
|
},
|
|
131
135
|
|
|
132
136
|
'%ReferenceErrorPrototype%': {
|
|
133
137
|
message: true, // to match TypeErrorPrototype.message
|
|
138
|
+
name: true, // set by "node 14"
|
|
134
139
|
},
|
|
135
140
|
|
|
136
141
|
'%PromisePrototype%': {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
// environment-options needs to be imported quite early, and so should
|
|
4
|
+
// avoid importing from anything but commons.js and assert.js
|
|
5
|
+
import { arrayPush, arraySlice, freeze } from './commons.js';
|
|
6
|
+
import { assert } from './error/assert.js';
|
|
7
|
+
|
|
8
|
+
const { details: X, quote: q } = assert;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* JavaScript module semantics resists attempts to parameterize a module's
|
|
12
|
+
* initialization behavior. A module initializes in order according to
|
|
13
|
+
* the path by which it is first imported, and then the initialized module
|
|
14
|
+
* is reused by all the other times it is imported. Compartments give us
|
|
15
|
+
* the opportunity to bind the same import name to different imported
|
|
16
|
+
* modules, depending on the package/compartment doing the import. Compartments
|
|
17
|
+
* also address the difficulty of parameterizing a module's initialization
|
|
18
|
+
* logic, but not in a pleasant manner.
|
|
19
|
+
*
|
|
20
|
+
* A pleasant parameterization would be for a static module to be function-like
|
|
21
|
+
* with explicit parameters, and for the parameterization to be like
|
|
22
|
+
* calling the static module with parameters in order to derive from it a
|
|
23
|
+
* module instance. Compartments instead lets us parameterize the meaning
|
|
24
|
+
* of a module instance derived from a static module according to the
|
|
25
|
+
* three namespaces provided by the JavaScript semantics, affecting the
|
|
26
|
+
* meaning of a module instance.
|
|
27
|
+
* * The global variable namespaces.
|
|
28
|
+
* * The global scope, aliased to properties of the global object.
|
|
29
|
+
* This is necessarily compartment-wide, and therefore in our
|
|
30
|
+
* recommened usage pattern, package-wide.
|
|
31
|
+
* * The global lexical scope. The SES-shim compartments support
|
|
32
|
+
* these both compartment-wide as well as per-module. But it is
|
|
33
|
+
* not yet clear what we will propose in the Compartment proposal.
|
|
34
|
+
* * The import namespace.
|
|
35
|
+
* * The host hooks.
|
|
36
|
+
*
|
|
37
|
+
* This `environment-options.js` module looks for a setting of an
|
|
38
|
+
* `optionName` parameter rooted in the global scope. If follows the Node
|
|
39
|
+
* precedent for finding Unix environment variable settings, looking for a
|
|
40
|
+
* global `process` object holding an `env` object,
|
|
41
|
+
* optionally holding a property named for the `optionName` whose value is the
|
|
42
|
+
* configuration setting of that option. For example, for the optionName
|
|
43
|
+
* `ALLOW_IMPLICIT_REMOTABLES` it would look in
|
|
44
|
+
* `globalThis.process.env.ALLOW_IMPLICIT_REMOTABLES`.
|
|
45
|
+
*
|
|
46
|
+
* If setting is either absent or `undefined`, that indicates that
|
|
47
|
+
* this configuration option should have its default behavior, whatever that is.
|
|
48
|
+
* Otherwise, reflecting Unix environment variables, the setting must be a
|
|
49
|
+
* string. This also helps ensure that this channel is used only to pass data,
|
|
50
|
+
* not authority beyond the ability to read this global state.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* makeEnvironmentCaptor provides a mechanism for getting environment
|
|
55
|
+
* variables, if they are needed, and a way to catalog the names of all
|
|
56
|
+
* the environment variables that were captured.
|
|
57
|
+
*
|
|
58
|
+
* @param {Object} aGlobal
|
|
59
|
+
*/
|
|
60
|
+
export const makeEnvironmentCaptor = aGlobal => {
|
|
61
|
+
const capturedEnvironmentOptionNames = [];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Gets an environment option by name and returns the option value or the
|
|
65
|
+
* given default.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} optionName
|
|
68
|
+
* @param {string} defaultSetting
|
|
69
|
+
* @returns {string}
|
|
70
|
+
*/
|
|
71
|
+
const getEnvironmentOption = (optionName, defaultSetting) => {
|
|
72
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
73
|
+
assert.typeof(
|
|
74
|
+
optionName,
|
|
75
|
+
'string',
|
|
76
|
+
X`Environment option name ${q(optionName)} must be a string.`,
|
|
77
|
+
);
|
|
78
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
79
|
+
assert.typeof(
|
|
80
|
+
defaultSetting,
|
|
81
|
+
'string',
|
|
82
|
+
X`Environment option default setting ${q(
|
|
83
|
+
defaultSetting,
|
|
84
|
+
)} must be a string.`,
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
/** @type {string} */
|
|
88
|
+
let setting = defaultSetting;
|
|
89
|
+
const globalProcess = aGlobal.process;
|
|
90
|
+
if (globalProcess && typeof globalProcess === 'object') {
|
|
91
|
+
const globalEnv = globalProcess.env;
|
|
92
|
+
if (globalEnv && typeof globalEnv === 'object') {
|
|
93
|
+
if (optionName in globalEnv) {
|
|
94
|
+
arrayPush(capturedEnvironmentOptionNames, optionName);
|
|
95
|
+
const optionValue = globalEnv[optionName];
|
|
96
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
97
|
+
assert.typeof(
|
|
98
|
+
optionValue,
|
|
99
|
+
'string',
|
|
100
|
+
X`Environment option named ${q(
|
|
101
|
+
optionName,
|
|
102
|
+
)}, if present, must have a corresponding string value, got ${q(
|
|
103
|
+
optionValue,
|
|
104
|
+
)}`,
|
|
105
|
+
);
|
|
106
|
+
setting = optionValue;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
assert(
|
|
111
|
+
setting === undefined || typeof setting === 'string',
|
|
112
|
+
X`Environment option value ${q(setting)}, if present, must be a string.`,
|
|
113
|
+
);
|
|
114
|
+
return setting;
|
|
115
|
+
};
|
|
116
|
+
freeze(getEnvironmentOption);
|
|
117
|
+
|
|
118
|
+
const getCapturedEnvironmentOptionNames = () => {
|
|
119
|
+
return freeze(arraySlice(capturedEnvironmentOptionNames));
|
|
120
|
+
};
|
|
121
|
+
freeze(getCapturedEnvironmentOptionNames);
|
|
122
|
+
|
|
123
|
+
return { getEnvironmentOption, getCapturedEnvironmentOptionNames };
|
|
124
|
+
};
|
|
125
|
+
freeze(makeEnvironmentCaptor);
|
package/src/error/assert.js
CHANGED
|
@@ -12,15 +12,25 @@
|
|
|
12
12
|
// module should not be observably impure.
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
|
-
Error,
|
|
16
15
|
RangeError,
|
|
17
16
|
TypeError,
|
|
18
17
|
WeakMap,
|
|
18
|
+
arrayJoin,
|
|
19
|
+
arrayMap,
|
|
20
|
+
arrayPop,
|
|
21
|
+
arrayPush,
|
|
19
22
|
assign,
|
|
20
23
|
freeze,
|
|
21
24
|
globalThis,
|
|
22
25
|
is,
|
|
26
|
+
isError,
|
|
27
|
+
stringIndexOf,
|
|
28
|
+
stringReplace,
|
|
29
|
+
stringSlice,
|
|
30
|
+
stringStartsWith,
|
|
31
|
+
weakmapDelete,
|
|
23
32
|
weakmapGet,
|
|
33
|
+
weakmapHas,
|
|
24
34
|
weakmapSet,
|
|
25
35
|
} from '../commons.js';
|
|
26
36
|
import { an, bestEffortStringify } from './stringify-utils.js';
|
|
@@ -40,7 +50,7 @@ const quote = (payload, spaces = undefined) => {
|
|
|
40
50
|
const result = freeze({
|
|
41
51
|
toString: freeze(() => bestEffortStringify(payload, spaces)),
|
|
42
52
|
});
|
|
43
|
-
declassifiers
|
|
53
|
+
weakmapSet(declassifiers, result, payload);
|
|
44
54
|
return result;
|
|
45
55
|
};
|
|
46
56
|
freeze(quote);
|
|
@@ -73,16 +83,16 @@ const getMessageString = ({ template, args }) => {
|
|
|
73
83
|
for (let i = 0; i < args.length; i += 1) {
|
|
74
84
|
const arg = args[i];
|
|
75
85
|
let argStr;
|
|
76
|
-
if (declassifiers
|
|
86
|
+
if (weakmapHas(declassifiers, arg)) {
|
|
77
87
|
argStr = `${arg}`;
|
|
78
|
-
} else if (arg
|
|
88
|
+
} else if (isError(arg)) {
|
|
79
89
|
argStr = `(${an(arg.name)})`;
|
|
80
90
|
} else {
|
|
81
91
|
argStr = `(${an(typeof arg)})`;
|
|
82
92
|
}
|
|
83
|
-
parts
|
|
93
|
+
arrayPush(parts, argStr, template[i + 1]);
|
|
84
94
|
}
|
|
85
|
-
return parts
|
|
95
|
+
return arrayJoin(parts, '');
|
|
86
96
|
};
|
|
87
97
|
|
|
88
98
|
/**
|
|
@@ -99,7 +109,7 @@ const getMessageString = ({ template, args }) => {
|
|
|
99
109
|
*/
|
|
100
110
|
const DetailsTokenProto = freeze({
|
|
101
111
|
toString() {
|
|
102
|
-
const hiddenDetails = hiddenDetailsMap
|
|
112
|
+
const hiddenDetails = weakmapGet(hiddenDetailsMap, this);
|
|
103
113
|
if (hiddenDetails === undefined) {
|
|
104
114
|
return '[Not a DetailsToken]';
|
|
105
115
|
}
|
|
@@ -125,7 +135,7 @@ const redactedDetails = (template, ...args) => {
|
|
|
125
135
|
// possible. Hence we store what we've got with little processing, postponing
|
|
126
136
|
// all the work to happen only if needed, for example, if an assertion fails.
|
|
127
137
|
const detailsToken = freeze({ __proto__: DetailsTokenProto });
|
|
128
|
-
hiddenDetailsMap
|
|
138
|
+
weakmapSet(hiddenDetailsMap, detailsToken, { template, args });
|
|
129
139
|
return detailsToken;
|
|
130
140
|
};
|
|
131
141
|
freeze(redactedDetails);
|
|
@@ -144,7 +154,9 @@ freeze(redactedDetails);
|
|
|
144
154
|
* @type {DetailsTag}
|
|
145
155
|
*/
|
|
146
156
|
const unredactedDetails = (template, ...args) => {
|
|
147
|
-
args = args
|
|
157
|
+
args = arrayMap(args, arg =>
|
|
158
|
+
weakmapHas(declassifiers, arg) ? arg : quote(arg),
|
|
159
|
+
);
|
|
148
160
|
return redactedDetails(template, ...args);
|
|
149
161
|
};
|
|
150
162
|
freeze(unredactedDetails);
|
|
@@ -158,20 +170,20 @@ const getLogArgs = ({ template, args }) => {
|
|
|
158
170
|
const logArgs = [template[0]];
|
|
159
171
|
for (let i = 0; i < args.length; i += 1) {
|
|
160
172
|
let arg = args[i];
|
|
161
|
-
if (declassifiers
|
|
162
|
-
arg = declassifiers
|
|
173
|
+
if (weakmapHas(declassifiers, arg)) {
|
|
174
|
+
arg = weakmapGet(declassifiers, arg);
|
|
163
175
|
}
|
|
164
176
|
// Remove the extra spaces (since console.error puts them
|
|
165
177
|
// between each cause).
|
|
166
|
-
const priorWithoutSpace = (logArgs
|
|
178
|
+
const priorWithoutSpace = stringReplace(arrayPop(logArgs) || '', / $/, '');
|
|
167
179
|
if (priorWithoutSpace !== '') {
|
|
168
|
-
logArgs
|
|
180
|
+
arrayPush(logArgs, priorWithoutSpace);
|
|
169
181
|
}
|
|
170
|
-
const nextWithoutSpace = template[i + 1]
|
|
171
|
-
logArgs
|
|
182
|
+
const nextWithoutSpace = stringReplace(template[i + 1], /^ /, '');
|
|
183
|
+
arrayPush(logArgs, arg, nextWithoutSpace);
|
|
172
184
|
}
|
|
173
185
|
if (logArgs[logArgs.length - 1] === '') {
|
|
174
|
-
logArgs
|
|
186
|
+
arrayPop(logArgs);
|
|
175
187
|
}
|
|
176
188
|
return logArgs;
|
|
177
189
|
};
|
|
@@ -214,7 +226,7 @@ const tagError = (err, optErrorName = err.name) => {
|
|
|
214
226
|
*/
|
|
215
227
|
const makeError = (
|
|
216
228
|
optDetails = redactedDetails`Assert failed`,
|
|
217
|
-
ErrorConstructor = Error,
|
|
229
|
+
ErrorConstructor = globalThis.Error,
|
|
218
230
|
{ errorName = undefined } = {},
|
|
219
231
|
) => {
|
|
220
232
|
if (typeof optDetails === 'string') {
|
|
@@ -222,13 +234,13 @@ const makeError = (
|
|
|
222
234
|
// it doesn't get quoted.
|
|
223
235
|
optDetails = redactedDetails([optDetails]);
|
|
224
236
|
}
|
|
225
|
-
const hiddenDetails = hiddenDetailsMap
|
|
237
|
+
const hiddenDetails = weakmapGet(hiddenDetailsMap, optDetails);
|
|
226
238
|
if (hiddenDetails === undefined) {
|
|
227
|
-
throw new
|
|
239
|
+
throw new TypeError(`unrecognized details ${quote(optDetails)}`);
|
|
228
240
|
}
|
|
229
241
|
const messageString = getMessageString(hiddenDetails);
|
|
230
242
|
const error = new ErrorConstructor(messageString);
|
|
231
|
-
hiddenMessageLogArgs
|
|
243
|
+
weakmapSet(hiddenMessageLogArgs, error, getLogArgs(hiddenDetails));
|
|
232
244
|
if (errorName !== undefined) {
|
|
233
245
|
tagError(error, errorName);
|
|
234
246
|
}
|
|
@@ -272,22 +284,22 @@ const note = (error, detailsNote) => {
|
|
|
272
284
|
// it doesn't get quoted.
|
|
273
285
|
detailsNote = redactedDetails([detailsNote]);
|
|
274
286
|
}
|
|
275
|
-
const hiddenDetails = hiddenDetailsMap
|
|
287
|
+
const hiddenDetails = weakmapGet(hiddenDetailsMap, detailsNote);
|
|
276
288
|
if (hiddenDetails === undefined) {
|
|
277
|
-
throw new
|
|
289
|
+
throw new TypeError(`unrecognized details ${quote(detailsNote)}`);
|
|
278
290
|
}
|
|
279
291
|
const logArgs = getLogArgs(hiddenDetails);
|
|
280
|
-
const callbacks = hiddenNoteCallbackArrays
|
|
292
|
+
const callbacks = weakmapGet(hiddenNoteCallbackArrays, error);
|
|
281
293
|
if (callbacks !== undefined) {
|
|
282
294
|
for (const callback of callbacks) {
|
|
283
295
|
callback(error, logArgs);
|
|
284
296
|
}
|
|
285
297
|
} else {
|
|
286
|
-
const logArgsArray = hiddenNoteLogArgsArrays
|
|
298
|
+
const logArgsArray = weakmapGet(hiddenNoteLogArgsArrays, error);
|
|
287
299
|
if (logArgsArray !== undefined) {
|
|
288
|
-
logArgsArray
|
|
300
|
+
arrayPush(logArgsArray, logArgs);
|
|
289
301
|
} else {
|
|
290
|
-
hiddenNoteLogArgsArrays
|
|
302
|
+
weakmapSet(hiddenNoteLogArgsArrays, error, [logArgs]);
|
|
291
303
|
}
|
|
292
304
|
}
|
|
293
305
|
};
|
|
@@ -306,11 +318,11 @@ const defaultGetStackString = error => {
|
|
|
306
318
|
return '';
|
|
307
319
|
}
|
|
308
320
|
const stackString = `${error.stack}`;
|
|
309
|
-
const pos = stackString
|
|
310
|
-
if (stackString
|
|
321
|
+
const pos = stringIndexOf(stackString, '\n');
|
|
322
|
+
if (stringStartsWith(stackString, ' ') || pos === -1) {
|
|
311
323
|
return stackString;
|
|
312
324
|
}
|
|
313
|
-
return stackString
|
|
325
|
+
return stringSlice(stackString, pos + 1); // exclude the initial newline
|
|
314
326
|
};
|
|
315
327
|
|
|
316
328
|
/** @type {LoggedErrorHandler} */
|
|
@@ -320,21 +332,21 @@ const loggedErrorHandler = {
|
|
|
320
332
|
resetErrorTagNum: () => {
|
|
321
333
|
errorTagNum = 0;
|
|
322
334
|
},
|
|
323
|
-
getMessageLogArgs: error => hiddenMessageLogArgs
|
|
335
|
+
getMessageLogArgs: error => weakmapGet(hiddenMessageLogArgs, error),
|
|
324
336
|
takeMessageLogArgs: error => {
|
|
325
|
-
const result = hiddenMessageLogArgs
|
|
326
|
-
hiddenMessageLogArgs
|
|
337
|
+
const result = weakmapGet(hiddenMessageLogArgs, error);
|
|
338
|
+
weakmapDelete(hiddenMessageLogArgs, error);
|
|
327
339
|
return result;
|
|
328
340
|
},
|
|
329
341
|
takeNoteLogArgsArray: (error, callback) => {
|
|
330
|
-
const result = hiddenNoteLogArgsArrays
|
|
331
|
-
hiddenNoteLogArgsArrays
|
|
342
|
+
const result = weakmapGet(hiddenNoteLogArgsArrays, error);
|
|
343
|
+
weakmapDelete(hiddenNoteLogArgsArrays, error);
|
|
332
344
|
if (callback !== undefined) {
|
|
333
|
-
const callbacks = hiddenNoteCallbackArrays
|
|
345
|
+
const callbacks = weakmapGet(hiddenNoteCallbackArrays, error);
|
|
334
346
|
if (callbacks) {
|
|
335
|
-
callbacks
|
|
347
|
+
arrayPush(callbacks, callback);
|
|
336
348
|
} else {
|
|
337
|
-
hiddenNoteCallbackArrays
|
|
349
|
+
weakmapSet(hiddenNoteCallbackArrays, error, [callback]);
|
|
338
350
|
}
|
|
339
351
|
}
|
|
340
352
|
return result || [];
|
|
@@ -353,7 +365,7 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
353
365
|
/** @type {AssertFail} */
|
|
354
366
|
const fail = (
|
|
355
367
|
optDetails = details`Assert failed`,
|
|
356
|
-
ErrorConstructor = Error,
|
|
368
|
+
ErrorConstructor = globalThis.Error,
|
|
357
369
|
) => {
|
|
358
370
|
const reason = makeError(optDetails, ErrorConstructor);
|
|
359
371
|
if (optRaise !== undefined) {
|
|
@@ -370,7 +382,7 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
370
382
|
function baseAssert(
|
|
371
383
|
flag,
|
|
372
384
|
optDetails = details`Check failed`,
|
|
373
|
-
ErrorConstructor = Error,
|
|
385
|
+
ErrorConstructor = globalThis.Error,
|
|
374
386
|
) {
|
|
375
387
|
if (!flag) {
|
|
376
388
|
throw fail(optDetails, ErrorConstructor);
|
package/src/error/console.js
CHANGED
|
@@ -6,11 +6,15 @@
|
|
|
6
6
|
// normally commented out.
|
|
7
7
|
|
|
8
8
|
import {
|
|
9
|
-
Error,
|
|
10
9
|
WeakSet,
|
|
10
|
+
arrayFilter,
|
|
11
|
+
arrayMap,
|
|
12
|
+
arrayPush,
|
|
11
13
|
defineProperty,
|
|
12
14
|
freeze,
|
|
13
15
|
fromEntries,
|
|
16
|
+
isError,
|
|
17
|
+
stringEndsWith,
|
|
14
18
|
weaksetAdd,
|
|
15
19
|
weaksetHas,
|
|
16
20
|
} from '../commons.js';
|
|
@@ -121,6 +125,7 @@ const makeLoggingConsoleKit = (
|
|
|
121
125
|
{ shouldResetForDebugging = false } = {},
|
|
122
126
|
) => {
|
|
123
127
|
if (shouldResetForDebugging) {
|
|
128
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
124
129
|
loggedErrorHandler.resetErrorTagNum();
|
|
125
130
|
}
|
|
126
131
|
|
|
@@ -128,7 +133,7 @@ const makeLoggingConsoleKit = (
|
|
|
128
133
|
let logArray = [];
|
|
129
134
|
|
|
130
135
|
const loggingConsole = fromEntries(
|
|
131
|
-
consoleWhitelist
|
|
136
|
+
arrayMap(consoleWhitelist, ([name, _]) => {
|
|
132
137
|
// Use an arrow function so that it doesn't come with its own name in
|
|
133
138
|
// its printed form. Instead, we're hoping that tooling uses only
|
|
134
139
|
// the `.name` property set below.
|
|
@@ -136,7 +141,7 @@ const makeLoggingConsoleKit = (
|
|
|
136
141
|
* @param {...any} args
|
|
137
142
|
*/
|
|
138
143
|
const method = (...args) => {
|
|
139
|
-
logArray
|
|
144
|
+
arrayPush(logArray, [name, ...args]);
|
|
140
145
|
};
|
|
141
146
|
defineProperty(method, 'name', { value: name });
|
|
142
147
|
return [name, freeze(method)];
|
|
@@ -191,9 +196,9 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
191
196
|
* @returns {any}
|
|
192
197
|
*/
|
|
193
198
|
const extractErrorArgs = (logArgs, subErrorsSink) => {
|
|
194
|
-
const argTags = logArgs
|
|
195
|
-
if (arg
|
|
196
|
-
subErrorsSink
|
|
199
|
+
const argTags = arrayMap(logArgs, arg => {
|
|
200
|
+
if (isError(arg)) {
|
|
201
|
+
arrayPush(subErrorsSink, arg);
|
|
197
202
|
return `(${tagError(arg)})`;
|
|
198
203
|
}
|
|
199
204
|
return arg;
|
|
@@ -212,6 +217,7 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
212
217
|
const errorName =
|
|
213
218
|
kind === ErrorInfo.MESSAGE ? `${errorTag}:` : `${errorTag} ${kind}`;
|
|
214
219
|
const argTags = extractErrorArgs(logArgs, subErrorsSink);
|
|
220
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
215
221
|
baseConsole[BASE_CONSOLE_LEVEL](errorName, ...argTags);
|
|
216
222
|
};
|
|
217
223
|
|
|
@@ -240,6 +246,7 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
240
246
|
if (optTag !== undefined) {
|
|
241
247
|
label = `${label} under ${optTag}`;
|
|
242
248
|
}
|
|
249
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
243
250
|
baseConsole.group(label);
|
|
244
251
|
try {
|
|
245
252
|
for (const subError of subErrors) {
|
|
@@ -247,6 +254,7 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
247
254
|
logError(subError);
|
|
248
255
|
}
|
|
249
256
|
} finally {
|
|
257
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
250
258
|
baseConsole.groupEnd();
|
|
251
259
|
}
|
|
252
260
|
};
|
|
@@ -278,6 +286,7 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
278
286
|
if (messageLogArgs === undefined) {
|
|
279
287
|
// If there is no message log args, then just show the message that
|
|
280
288
|
// the error itself carries.
|
|
289
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
281
290
|
baseConsole[BASE_CONSOLE_LEVEL](`${errorTag}:`, error.message);
|
|
282
291
|
} else {
|
|
283
292
|
// If there is one, we take it to be strictly more informative than the
|
|
@@ -289,10 +298,11 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
289
298
|
if (
|
|
290
299
|
typeof stackString === 'string' &&
|
|
291
300
|
stackString.length >= 1 &&
|
|
292
|
-
!stackString
|
|
301
|
+
!stringEndsWith(stackString, '\n')
|
|
293
302
|
) {
|
|
294
303
|
stackString += '\n';
|
|
295
304
|
}
|
|
305
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
296
306
|
baseConsole[BASE_CONSOLE_LEVEL](stackString);
|
|
297
307
|
// Show the other annotations on error
|
|
298
308
|
for (const noteLogArgs of noteLogArgsArray) {
|
|
@@ -302,7 +312,7 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
302
312
|
logSubErrors(subErrors, errorTag);
|
|
303
313
|
};
|
|
304
314
|
|
|
305
|
-
const levelMethods = consoleLevelMethods
|
|
315
|
+
const levelMethods = arrayMap(consoleLevelMethods, ([level, _]) => {
|
|
306
316
|
/**
|
|
307
317
|
* @param {...any} logArgs
|
|
308
318
|
*/
|
|
@@ -310,21 +320,24 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
310
320
|
const subErrors = [];
|
|
311
321
|
const argTags = extractErrorArgs(logArgs, subErrors);
|
|
312
322
|
// @ts-ignore
|
|
323
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
313
324
|
baseConsole[level](...argTags);
|
|
314
325
|
logSubErrors(subErrors);
|
|
315
326
|
};
|
|
316
327
|
defineProperty(levelMethod, 'name', { value: level });
|
|
317
328
|
return [level, freeze(levelMethod)];
|
|
318
329
|
});
|
|
319
|
-
const otherMethodNames =
|
|
330
|
+
const otherMethodNames = arrayFilter(
|
|
331
|
+
consoleOtherMethods,
|
|
320
332
|
([name, _]) => name in baseConsole,
|
|
321
333
|
);
|
|
322
|
-
const otherMethods = otherMethodNames
|
|
334
|
+
const otherMethods = arrayMap(otherMethodNames, ([name, _]) => {
|
|
323
335
|
/**
|
|
324
336
|
* @param {...any} args
|
|
325
337
|
*/
|
|
326
338
|
const otherMethod = (...args) => {
|
|
327
339
|
// @ts-ignore
|
|
340
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
328
341
|
baseConsole[name](...args);
|
|
329
342
|
return undefined;
|
|
330
343
|
};
|
|
@@ -333,7 +346,7 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
333
346
|
});
|
|
334
347
|
|
|
335
348
|
const causalConsole = fromEntries([...levelMethods, ...otherMethods]);
|
|
336
|
-
return freeze(causalConsole);
|
|
349
|
+
return /** @type {VirtualConsole} */ (freeze(causalConsole));
|
|
337
350
|
};
|
|
338
351
|
freeze(makeCausalConsole);
|
|
339
352
|
export { makeCausalConsole };
|
|
@@ -343,21 +356,26 @@ export { makeCausalConsole };
|
|
|
343
356
|
/** @type {FilterConsole} */
|
|
344
357
|
const filterConsole = (baseConsole, filter, _topic = undefined) => {
|
|
345
358
|
// TODO do something with optional topic string
|
|
346
|
-
const
|
|
347
|
-
|
|
359
|
+
const whitelist = arrayFilter(
|
|
360
|
+
consoleWhitelist,
|
|
361
|
+
([name, _]) => name in baseConsole,
|
|
362
|
+
);
|
|
363
|
+
const methods = arrayMap(whitelist, ([name, severity]) => {
|
|
348
364
|
/**
|
|
349
365
|
* @param {...any} args
|
|
350
366
|
*/
|
|
351
367
|
const method = (...args) => {
|
|
368
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
352
369
|
if (severity === undefined || filter.canLog(severity)) {
|
|
353
370
|
// @ts-ignore
|
|
371
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
354
372
|
baseConsole[name](...args);
|
|
355
373
|
}
|
|
356
374
|
};
|
|
357
375
|
return [name, freeze(method)];
|
|
358
376
|
});
|
|
359
377
|
const filteringConsole = fromEntries(methods);
|
|
360
|
-
return freeze(filteringConsole);
|
|
378
|
+
return /** @type {VirtualConsole} */ (freeze(filteringConsole));
|
|
361
379
|
};
|
|
362
380
|
freeze(filterConsole);
|
|
363
381
|
export { filterConsole };
|
|
@@ -18,7 +18,9 @@ if (typeof abandon === 'function') {
|
|
|
18
18
|
/** @param {Error} reason */
|
|
19
19
|
raise = reason => {
|
|
20
20
|
// Check `console` each time `raise` is called.
|
|
21
|
+
// eslint-disable-next-line no-restricted-globals
|
|
21
22
|
if (typeof console === 'object' && typeof console.error === 'function') {
|
|
23
|
+
// eslint-disable-next-line @endo/no-polymorphic-call, no-restricted-globals
|
|
22
24
|
console.error('Failed because:', reason);
|
|
23
25
|
}
|
|
24
26
|
abandon(1);
|
|
@@ -1,19 +1,42 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
Error,
|
|
5
4
|
Set,
|
|
6
5
|
String,
|
|
6
|
+
arrayJoin,
|
|
7
|
+
arraySlice,
|
|
7
8
|
freeze,
|
|
8
9
|
is,
|
|
10
|
+
isError,
|
|
9
11
|
setAdd,
|
|
10
12
|
setHas,
|
|
11
|
-
stringStartsWith,
|
|
12
13
|
stringIncludes,
|
|
14
|
+
stringStartsWith,
|
|
13
15
|
stringifyJson,
|
|
14
16
|
toStringTagSymbol,
|
|
15
17
|
} from '../commons.js';
|
|
16
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Joins English terms with commas and an optional conjunction.
|
|
21
|
+
*
|
|
22
|
+
* @param {string[]} terms
|
|
23
|
+
* @param {"and" | "or"} conjunction
|
|
24
|
+
*/
|
|
25
|
+
export const enJoin = (terms, conjunction) => {
|
|
26
|
+
if (terms.length === 0) {
|
|
27
|
+
return '(none)';
|
|
28
|
+
} else if (terms.length === 1) {
|
|
29
|
+
return terms[0];
|
|
30
|
+
} else if (terms.length === 2) {
|
|
31
|
+
const [first, second] = terms;
|
|
32
|
+
return `${first} ${conjunction} ${second}`;
|
|
33
|
+
} else {
|
|
34
|
+
return `${arrayJoin(arraySlice(terms, 0, -1), ', ')}, ${conjunction} ${
|
|
35
|
+
terms[terms.length - 1]
|
|
36
|
+
}`;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
17
40
|
/**
|
|
18
41
|
* Prepend the correct indefinite article onto a noun, typically a typeof
|
|
19
42
|
* result, e.g., "an object" vs. "a number"
|
|
@@ -70,7 +93,7 @@ const bestEffortStringify = (payload, spaces = undefined) => {
|
|
|
70
93
|
return '[Seen]';
|
|
71
94
|
}
|
|
72
95
|
setAdd(seenSet, val);
|
|
73
|
-
if (val
|
|
96
|
+
if (isError(val)) {
|
|
74
97
|
return `[${val.name}: ${val.message}]`;
|
|
75
98
|
}
|
|
76
99
|
if (toStringTagSymbol in val) {
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { TypeError, globalThis } from '../commons.js';
|
|
4
4
|
import { loggedErrorHandler as defaultHandler } from './assert.js';
|
|
5
5
|
import { makeCausalConsole } from './console.js';
|
|
6
6
|
import './types.js';
|
|
7
7
|
import './internal-types.js';
|
|
8
8
|
|
|
9
|
+
// eslint-disable-next-line no-restricted-globals
|
|
9
10
|
const originalConsole = console;
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -24,7 +25,7 @@ export const tameConsole = (
|
|
|
24
25
|
optGetStackString = undefined,
|
|
25
26
|
) => {
|
|
26
27
|
if (consoleTaming !== 'safe' && consoleTaming !== 'unsafe') {
|
|
27
|
-
throw new
|
|
28
|
+
throw new TypeError(`unrecognized consoleTaming ${consoleTaming}`);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
if (consoleTaming === 'unsafe') {
|
|
@@ -54,11 +55,16 @@ export const tameConsole = (
|
|
|
54
55
|
|
|
55
56
|
// Node.js
|
|
56
57
|
if (errorTrapping !== 'none' && globalThis.process !== undefined) {
|
|
58
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
57
59
|
globalThis.process.on('uncaughtException', error => {
|
|
60
|
+
// causalConsole is born frozen so not vulnerable to method tampering.
|
|
61
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
58
62
|
causalConsole.error(error);
|
|
59
63
|
if (errorTrapping === 'platform' || errorTrapping === 'exit') {
|
|
64
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
60
65
|
globalThis.process.exit(globalThis.process.exitCode || -1);
|
|
61
66
|
} else if (errorTrapping === 'abort') {
|
|
67
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
62
68
|
globalThis.process.abort();
|
|
63
69
|
}
|
|
64
70
|
});
|
|
@@ -66,10 +72,12 @@ export const tameConsole = (
|
|
|
66
72
|
|
|
67
73
|
// Browser
|
|
68
74
|
if (errorTrapping !== 'none' && globalThis.window !== undefined) {
|
|
75
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
69
76
|
globalThis.window.addEventListener('error', event => {
|
|
77
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
70
78
|
event.preventDefault();
|
|
71
|
-
|
|
72
|
-
causalConsole.error(
|
|
79
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
80
|
+
causalConsole.error(event.error);
|
|
73
81
|
if (errorTrapping === 'exit' || errorTrapping === 'abort') {
|
|
74
82
|
globalThis.window.location.href = `about:blank`;
|
|
75
83
|
}
|