ses 0.18.0 → 0.18.2
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/NEWS.md +17 -0
- package/README.md +10 -5
- package/SECURITY.md +1 -1
- package/dist/lockdown.cjs +319 -243
- package/dist/lockdown.mjs +319 -243
- package/dist/lockdown.umd.js +319 -243
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +319 -243
- package/dist/ses.mjs +319 -243
- package/dist/ses.umd.js +319 -243
- package/dist/ses.umd.min.js +1 -1
- package/index.js +1 -0
- package/package.json +15 -12
- package/src/commons.js +2 -0
- package/src/compartment-evaluate.js +2 -4
- package/src/compartment-shim.js +3 -3
- package/src/enablements.js +12 -0
- package/src/environment-options.js +10 -19
- package/src/error/assert.js +24 -16
- package/src/error/console.js +2 -6
- package/src/error/internal-types.js +3 -3
- package/src/error/types.js +76 -81
- package/src/get-anonymous-intrinsics.js +6 -4
- package/src/get-source-url.js +3 -0
- package/src/global-object.js +9 -9
- package/src/intrinsics.js +1 -1
- package/src/lockdown-shim.js +11 -13
- package/src/make-hardener.js +1 -1
- package/src/make-safe-evaluator.js +4 -4
- package/src/module-instance.js +29 -13
- package/src/module-link.js +2 -6
- package/src/module-load.js +58 -21
- package/src/scope-constants.js +2 -2
- package/src/sloppy-globals-scope-terminator.js +1 -0
- package/src/tame-domains.js +1 -0
- package/src/tame-function-constructors.js +3 -2
- package/src/tame-locale-methods.js +2 -2
- package/src/tame-math-object.js +2 -3
- package/src/transforms.js +15 -0
- package/src/whitelist-intrinsics.js +2 -2
- package/tools.js +25 -0
- package/{index.d.ts → types.d.ts} +2 -1
- package/CHANGELOG.md +0 -596
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "0.18.
|
|
3
|
+
"version": "0.18.2",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"main": "./dist/ses.cjs",
|
|
33
33
|
"module": "./index.js",
|
|
34
34
|
"unpkg": "./dist/ses.umd.js",
|
|
35
|
-
"types": "./
|
|
35
|
+
"types": "./types.d.ts",
|
|
36
36
|
"exports": {
|
|
37
37
|
".": {
|
|
38
38
|
"import": "./index.js",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"import": "./index.js",
|
|
43
43
|
"require": "./dist/ses.cjs"
|
|
44
44
|
},
|
|
45
|
+
"./tools.js": "./tools.js",
|
|
45
46
|
"./package.json": "./package.json"
|
|
46
47
|
},
|
|
47
48
|
"scripts": {
|
|
@@ -59,32 +60,33 @@
|
|
|
59
60
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
62
|
-
"@endo/compartment-mapper": "^0.8.
|
|
63
|
-
"@endo/eslint-config": "^0.5.
|
|
64
|
-
"@endo/static-module-record": "^0.7.
|
|
65
|
-
"@endo/test262-runner": "^0.1.
|
|
66
|
-
"ava": "^
|
|
63
|
+
"@endo/compartment-mapper": "^0.8.2",
|
|
64
|
+
"@endo/eslint-config": "^0.5.3",
|
|
65
|
+
"@endo/static-module-record": "^0.7.17",
|
|
66
|
+
"@endo/test262-runner": "^0.1.30",
|
|
67
|
+
"ava": "^5.2.0",
|
|
67
68
|
"babel-eslint": "^10.0.3",
|
|
68
69
|
"c8": "^7.7.3",
|
|
69
70
|
"eslint": "^7.32.0",
|
|
70
71
|
"eslint-config-airbnb-base": "^14.0.0",
|
|
71
72
|
"eslint-config-prettier": "^6.9.0",
|
|
72
73
|
"eslint-plugin-eslint-comments": "^3.1.2",
|
|
73
|
-
"eslint-plugin-import": "^2.
|
|
74
|
+
"eslint-plugin-import": "^2.27.5",
|
|
74
75
|
"eslint-plugin-prettier": "^3.4.1",
|
|
75
|
-
"prettier": "^
|
|
76
|
+
"prettier": "^2.8.0",
|
|
76
77
|
"sinon": "8.0.4",
|
|
77
78
|
"terser": "^4.8.0",
|
|
78
79
|
"tsd": "^0.24.1",
|
|
79
|
-
"typescript": "~4.
|
|
80
|
+
"typescript": "~4.9.5"
|
|
80
81
|
},
|
|
81
82
|
"files": [
|
|
82
83
|
"LICENSE*",
|
|
83
84
|
"NEWS*",
|
|
84
85
|
"SECURITY*",
|
|
85
86
|
"dist",
|
|
86
|
-
"
|
|
87
|
+
"types.d.ts",
|
|
87
88
|
"index.js",
|
|
89
|
+
"tools.js",
|
|
88
90
|
"src"
|
|
89
91
|
],
|
|
90
92
|
"private": false,
|
|
@@ -170,6 +172,7 @@
|
|
|
170
172
|
]
|
|
171
173
|
},
|
|
172
174
|
"prettier": {
|
|
175
|
+
"arrowParens": "avoid",
|
|
173
176
|
"trailingComma": "all",
|
|
174
177
|
"singleQuote": true
|
|
175
178
|
},
|
|
@@ -179,5 +182,5 @@
|
|
|
179
182
|
],
|
|
180
183
|
"timeout": "2m"
|
|
181
184
|
},
|
|
182
|
-
"gitHead": "
|
|
185
|
+
"gitHead": "0053227a37ee76ee8a3dce6059dd55140ed0ce6d"
|
|
183
186
|
}
|
package/src/commons.js
CHANGED
|
@@ -97,6 +97,7 @@ export const defineProperty = (object, prop, descriptor) => {
|
|
|
97
97
|
// Instead, to workaround the Safari bug
|
|
98
98
|
const result = originalDefineProperty(object, prop, descriptor);
|
|
99
99
|
if (result !== object) {
|
|
100
|
+
// See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_DEFINE_PROPERTY_FAILED_SILENTLY.md
|
|
100
101
|
throw TypeError(
|
|
101
102
|
`Please report that the original defineProperty silently failed to set ${stringifyJson(
|
|
102
103
|
String(prop),
|
|
@@ -279,6 +280,7 @@ export const FERAL_EVAL = eval;
|
|
|
279
280
|
export const FERAL_FUNCTION = Function;
|
|
280
281
|
|
|
281
282
|
export const noEvalEvaluate = () => {
|
|
283
|
+
// See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_NO_EVAL.md
|
|
282
284
|
throw new TypeError(
|
|
283
285
|
'Cannot eval with evalTaming set to "noEval" (SES_NO_EVAL)',
|
|
284
286
|
);
|
|
@@ -13,10 +13,8 @@ import {
|
|
|
13
13
|
import { makeSafeEvaluator } from './make-safe-evaluator.js';
|
|
14
14
|
|
|
15
15
|
export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
16
|
-
const {
|
|
17
|
-
|
|
18
|
-
__moduleShimLexicals__ = undefined,
|
|
19
|
-
} = options;
|
|
16
|
+
const { sloppyGlobalsMode = false, __moduleShimLexicals__ = undefined } =
|
|
17
|
+
options;
|
|
20
18
|
|
|
21
19
|
let safeEvaluate;
|
|
22
20
|
|
package/src/compartment-shim.js
CHANGED
|
@@ -96,10 +96,10 @@ export const CompartmentPrototype = {
|
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
98
|
* @param {string} source is a JavaScript program grammar construction.
|
|
99
|
-
* @param {
|
|
99
|
+
* @param {object} [options]
|
|
100
100
|
* @param {Array<Transform>} [options.transforms]
|
|
101
101
|
* @param {boolean} [options.sloppyGlobalsMode]
|
|
102
|
-
* @param {
|
|
102
|
+
* @param {object} [options.__moduleShimLexicals__]
|
|
103
103
|
* @param {boolean} [options.__evadeHtmlCommentTest__]
|
|
104
104
|
* @param {boolean} [options.__evadeImportExpressionTest__]
|
|
105
105
|
* @param {boolean} [options.__rejectSomeDirectEvalExpressions__]
|
|
@@ -180,7 +180,7 @@ defineProperties(InertCompartment, {
|
|
|
180
180
|
* @callback MakeCompartmentConstructor
|
|
181
181
|
* @param {MakeCompartmentConstructor} targetMakeCompartmentConstructor
|
|
182
182
|
* @param {Record<string, any>} intrinsics
|
|
183
|
-
* @param {(object:
|
|
183
|
+
* @param {(object: object) => void} markVirtualizedNativeFunction
|
|
184
184
|
* @returns {Compartment['constructor']}
|
|
185
185
|
*/
|
|
186
186
|
|
package/src/enablements.js
CHANGED
|
@@ -206,4 +206,16 @@ export const severeEnablements = {
|
|
|
206
206
|
* enablements just enable them all.
|
|
207
207
|
*/
|
|
208
208
|
'%TypedArrayPrototype%': '*',
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Needed to work with Immer before https://github.com/immerjs/immer/pull/914
|
|
212
|
+
* is accepted.
|
|
213
|
+
*/
|
|
214
|
+
'%MapPrototype%': '*',
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Needed to work with Immer before https://github.com/immerjs/immer/pull/914
|
|
218
|
+
* is accepted.
|
|
219
|
+
*/
|
|
220
|
+
'%SetPrototype%': '*',
|
|
209
221
|
};
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { arrayPush, freeze } from './commons.js';
|
|
6
6
|
import { assert } from './error/assert.js';
|
|
7
7
|
|
|
8
|
-
const {
|
|
8
|
+
const { Fail, quote: q } = assert;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* JavaScript module semantics resists attempts to parameterize a module's
|
|
@@ -55,7 +55,7 @@ const { details: X, Fail, quote: q } = assert;
|
|
|
55
55
|
* variables, if they are needed, and a way to catalog the names of all
|
|
56
56
|
* the environment variables that were captured.
|
|
57
57
|
*
|
|
58
|
-
* @param {
|
|
58
|
+
* @param {object} aGlobal
|
|
59
59
|
*/
|
|
60
60
|
export const makeEnvironmentCaptor = aGlobal => {
|
|
61
61
|
const capturedEnvironmentOptionNames = [];
|
|
@@ -70,19 +70,13 @@ export const makeEnvironmentCaptor = aGlobal => {
|
|
|
70
70
|
*/
|
|
71
71
|
const getEnvironmentOption = (optionName, defaultSetting) => {
|
|
72
72
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
73
|
-
|
|
74
|
-
optionName
|
|
75
|
-
'string',
|
|
76
|
-
X`Environment option name ${q(optionName)} must be a string.`,
|
|
77
|
-
);
|
|
73
|
+
typeof optionName === 'string' ||
|
|
74
|
+
Fail`Environment option name ${q(optionName)} must be a string.`;
|
|
78
75
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
'string',
|
|
82
|
-
X`Environment option default setting ${q(
|
|
76
|
+
typeof defaultSetting === 'string' ||
|
|
77
|
+
Fail`Environment option default setting ${q(
|
|
83
78
|
defaultSetting,
|
|
84
|
-
)} must be a string
|
|
85
|
-
);
|
|
79
|
+
)} must be a string.`;
|
|
86
80
|
|
|
87
81
|
/** @type {string} */
|
|
88
82
|
let setting = defaultSetting;
|
|
@@ -94,15 +88,12 @@ export const makeEnvironmentCaptor = aGlobal => {
|
|
|
94
88
|
arrayPush(capturedEnvironmentOptionNames, optionName);
|
|
95
89
|
const optionValue = globalEnv[optionName];
|
|
96
90
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
'string',
|
|
100
|
-
X`Environment option named ${q(
|
|
91
|
+
typeof optionValue === 'string' ||
|
|
92
|
+
Fail`Environment option named ${q(
|
|
101
93
|
optionName,
|
|
102
94
|
)}, if present, must have a corresponding string value, got ${q(
|
|
103
95
|
optionValue,
|
|
104
|
-
)}
|
|
105
|
-
);
|
|
96
|
+
)}`;
|
|
106
97
|
setting = optionValue;
|
|
107
98
|
}
|
|
108
99
|
}
|
package/src/error/assert.js
CHANGED
|
@@ -58,7 +58,7 @@ freeze(quote);
|
|
|
58
58
|
// /////////////////////////////////////////////////////////////////////////////
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
|
-
* @typedef {
|
|
61
|
+
* @typedef {object} HiddenDetails
|
|
62
62
|
*
|
|
63
63
|
* Captures the arguments passed to the `details` template string tag.
|
|
64
64
|
*
|
|
@@ -362,9 +362,11 @@ export { loggedErrorHandler };
|
|
|
362
362
|
*/
|
|
363
363
|
const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
364
364
|
const details = unredacted ? unredactedDetails : redactedDetails;
|
|
365
|
+
const assertFailedDetails = details`Check failed`;
|
|
366
|
+
|
|
365
367
|
/** @type {AssertFail} */
|
|
366
368
|
const fail = (
|
|
367
|
-
optDetails =
|
|
369
|
+
optDetails = assertFailedDetails,
|
|
368
370
|
ErrorConstructor = globalThis.Error,
|
|
369
371
|
) => {
|
|
370
372
|
const reason = makeError(optDetails, ErrorConstructor);
|
|
@@ -384,31 +386,37 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
384
386
|
/** @type {BaseAssert} */
|
|
385
387
|
function baseAssert(
|
|
386
388
|
flag,
|
|
387
|
-
optDetails =
|
|
388
|
-
ErrorConstructor =
|
|
389
|
+
optDetails = undefined,
|
|
390
|
+
ErrorConstructor = undefined,
|
|
389
391
|
) {
|
|
390
|
-
|
|
391
|
-
throw fail(optDetails, ErrorConstructor);
|
|
392
|
-
}
|
|
392
|
+
flag || fail(optDetails, ErrorConstructor);
|
|
393
393
|
}
|
|
394
394
|
|
|
395
395
|
/** @type {AssertEqual} */
|
|
396
396
|
const equal = (
|
|
397
397
|
actual,
|
|
398
398
|
expected,
|
|
399
|
-
optDetails =
|
|
400
|
-
ErrorConstructor =
|
|
399
|
+
optDetails = undefined,
|
|
400
|
+
ErrorConstructor = undefined,
|
|
401
401
|
) => {
|
|
402
|
-
|
|
402
|
+
is(actual, expected) ||
|
|
403
|
+
fail(
|
|
404
|
+
optDetails || details`Expected ${actual} is same as ${expected}`,
|
|
405
|
+
ErrorConstructor || RangeError,
|
|
406
|
+
);
|
|
403
407
|
};
|
|
404
408
|
freeze(equal);
|
|
405
409
|
|
|
406
410
|
/** @type {AssertTypeof} */
|
|
407
411
|
const assertTypeof = (specimen, typename, optDetails) => {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
)
|
|
412
|
+
// This will safely fall through if typename is not a string,
|
|
413
|
+
// which is what we want.
|
|
414
|
+
// eslint-disable-next-line valid-typeof
|
|
415
|
+
if (typeof specimen === typename) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
typeof typename === 'string' || Fail`${quote(typename)} must be a string`;
|
|
419
|
+
|
|
412
420
|
if (optDetails === undefined) {
|
|
413
421
|
// Like
|
|
414
422
|
// ```js
|
|
@@ -418,12 +426,12 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
418
426
|
// so it doesn't get quoted.
|
|
419
427
|
optDetails = details(['', ` must be ${an(typename)}`], specimen);
|
|
420
428
|
}
|
|
421
|
-
|
|
429
|
+
fail(optDetails, TypeError);
|
|
422
430
|
};
|
|
423
431
|
freeze(assertTypeof);
|
|
424
432
|
|
|
425
433
|
/** @type {AssertString} */
|
|
426
|
-
const assertString = (specimen, optDetails) =>
|
|
434
|
+
const assertString = (specimen, optDetails = undefined) =>
|
|
427
435
|
assertTypeof(specimen, 'string', optDetails);
|
|
428
436
|
|
|
429
437
|
// Note that "assert === baseAssert"
|
package/src/error/console.js
CHANGED
|
@@ -174,12 +174,8 @@ freeze(ErrorInfo);
|
|
|
174
174
|
|
|
175
175
|
/** @type {MakeCausalConsole} */
|
|
176
176
|
const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
177
|
-
const {
|
|
178
|
-
|
|
179
|
-
tagError,
|
|
180
|
-
takeMessageLogArgs,
|
|
181
|
-
takeNoteLogArgsArray,
|
|
182
|
-
} = loggedErrorHandler;
|
|
177
|
+
const { getStackString, tagError, takeMessageLogArgs, takeNoteLogArgsArray } =
|
|
178
|
+
loggedErrorHandler;
|
|
183
179
|
|
|
184
180
|
/**
|
|
185
181
|
* @param {ReadonlyArray<any>} logArgs
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* @typedef {
|
|
28
|
+
* @typedef {object} LoggedErrorHandler
|
|
29
29
|
*
|
|
30
30
|
* Used to parameterize `makeCausalConsole` to give it access to potentially
|
|
31
31
|
* hidden information to augment the logging of errors.
|
|
@@ -45,13 +45,13 @@
|
|
|
45
45
|
*/
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
* @typedef {
|
|
48
|
+
* @typedef {object} LoggingConsoleKit
|
|
49
49
|
* @property {VirtualConsole} loggingConsole
|
|
50
50
|
* @property {() => readonly LogRecord[]} takeLog
|
|
51
51
|
*/
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
|
-
* @typedef {
|
|
54
|
+
* @typedef {object} MakeLoggingConsoleKitOptions
|
|
55
55
|
* @property {boolean=} shouldResetForDebugging
|
|
56
56
|
*/
|
|
57
57
|
|
package/src/error/types.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* @typedef {
|
|
15
|
+
* @typedef {object} AssertMakeErrorOptions
|
|
16
16
|
* @property {string=} errorName
|
|
17
17
|
*/
|
|
18
18
|
|
|
@@ -34,8 +34,9 @@
|
|
|
34
34
|
*
|
|
35
35
|
* The `assert.fail` method.
|
|
36
36
|
*
|
|
37
|
-
* Fail an assertion, recording details to the console and
|
|
38
|
-
* raising an exception with
|
|
37
|
+
* Fail an assertion, recording full details to the console and
|
|
38
|
+
* raising an exception with a message in which `details` substitution values
|
|
39
|
+
* have been redacted.
|
|
39
40
|
*
|
|
40
41
|
* The optional `optDetails` can be a string for backwards compatibility
|
|
41
42
|
* with the nodejs assertion library.
|
|
@@ -59,51 +60,65 @@
|
|
|
59
60
|
*/
|
|
60
61
|
|
|
61
62
|
// Type all the overloads of the assertTypeof function.
|
|
62
|
-
// There may eventually be a better way to do this, but
|
|
63
|
-
// Typescript 4.0.
|
|
63
|
+
// There may eventually be a better way to do this, but
|
|
64
|
+
// thems the breaks with Typescript 4.0.
|
|
64
65
|
/**
|
|
65
66
|
* @callback AssertTypeofBigint
|
|
66
67
|
* @param {any} specimen
|
|
67
68
|
* @param {'bigint'} typename
|
|
68
69
|
* @param {Details=} optDetails
|
|
69
70
|
* @returns {asserts specimen is bigint}
|
|
70
|
-
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
/**
|
|
71
74
|
* @callback AssertTypeofBoolean
|
|
72
75
|
* @param {any} specimen
|
|
73
76
|
* @param {'boolean'} typename
|
|
74
77
|
* @param {Details=} optDetails
|
|
75
78
|
* @returns {asserts specimen is boolean}
|
|
76
|
-
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
/**
|
|
77
82
|
* @callback AssertTypeofFunction
|
|
78
83
|
* @param {any} specimen
|
|
79
84
|
* @param {'function'} typename
|
|
80
85
|
* @param {Details=} optDetails
|
|
81
86
|
* @returns {asserts specimen is Function}
|
|
82
|
-
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
83
90
|
* @callback AssertTypeofNumber
|
|
84
91
|
* @param {any} specimen
|
|
85
92
|
* @param {'number'} typename
|
|
86
93
|
* @param {Details=} optDetails
|
|
87
94
|
* @returns {asserts specimen is number}
|
|
88
|
-
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
/**
|
|
89
98
|
* @callback AssertTypeofObject
|
|
90
99
|
* @param {any} specimen
|
|
91
100
|
* @param {'object'} typename
|
|
92
101
|
* @param {Details=} optDetails
|
|
93
102
|
* @returns {asserts specimen is Record<any, any> | null}
|
|
94
|
-
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
/**
|
|
95
106
|
* @callback AssertTypeofString
|
|
96
107
|
* @param {any} specimen
|
|
97
108
|
* @param {'string'} typename
|
|
98
109
|
* @param {Details=} optDetails
|
|
99
110
|
* @returns {asserts specimen is string}
|
|
100
|
-
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
/**
|
|
101
114
|
* @callback AssertTypeofSymbol
|
|
102
115
|
* @param {any} specimen
|
|
103
116
|
* @param {'symbol'} typename
|
|
104
117
|
* @param {Details=} optDetails
|
|
105
118
|
* @returns {asserts specimen is symbol}
|
|
106
|
-
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
107
122
|
* @callback AssertTypeofUndefined
|
|
108
123
|
* @param {any} specimen
|
|
109
124
|
* @param {'undefined'} typename
|
|
@@ -127,14 +142,15 @@
|
|
|
127
142
|
* Assert an expected typeof result.
|
|
128
143
|
* @param {any} specimen The value to get the typeof
|
|
129
144
|
* @param {Details=} optDetails The details to throw
|
|
145
|
+
* @returns {asserts specimen is string}
|
|
130
146
|
*/
|
|
131
147
|
|
|
132
148
|
/**
|
|
133
149
|
* @callback AssertNote
|
|
134
150
|
* The `assert.note` method.
|
|
135
151
|
*
|
|
136
|
-
* Annotate
|
|
137
|
-
* augmented console
|
|
152
|
+
* Annotate an error with details, potentially to be used by an
|
|
153
|
+
* augmented console such as the causal console of `console.js`, to
|
|
138
154
|
* provide extra information associated with logged errors.
|
|
139
155
|
*
|
|
140
156
|
* @param {Error} error
|
|
@@ -157,45 +173,31 @@
|
|
|
157
173
|
*/
|
|
158
174
|
|
|
159
175
|
/**
|
|
160
|
-
* @typedef {
|
|
176
|
+
* @typedef {object} StringablePayload
|
|
161
177
|
* Holds the payload passed to quote so that its printed form is visible.
|
|
162
178
|
* @property {() => string} toString How to print the payload
|
|
163
179
|
*/
|
|
164
180
|
|
|
165
181
|
/**
|
|
166
182
|
* To "declassify" and quote a substitution value used in a
|
|
167
|
-
* details`...` template literal, enclose that substitution expression
|
|
168
|
-
* in a call to `quote`. This
|
|
169
|
-
* (as if with `JSON.stringify`)
|
|
183
|
+
* ``` details`...` ``` template literal, enclose that substitution expression
|
|
184
|
+
* in a call to `quote`. This makes the value appear quoted
|
|
185
|
+
* (as if with `JSON.stringify`) in the message of the thrown error. The
|
|
170
186
|
* payload itself is still passed unquoted to the console as it would be
|
|
171
187
|
* without `quote`.
|
|
172
188
|
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* want to reveal the sky's actual color, but we do want the thrown error's
|
|
176
|
-
* message to reveal what color the sky was supposed to be:
|
|
189
|
+
* For example, the following will reveal the expected sky color, but not the
|
|
190
|
+
* actual incorrect sky color, in the thrown error's message:
|
|
177
191
|
* ```js
|
|
178
|
-
*
|
|
179
|
-
* sky.color,
|
|
180
|
-
* color,
|
|
181
|
-
* details`${sky.color} should be ${quote(color)}`,
|
|
182
|
-
* );
|
|
192
|
+
* sky.color === expectedColor || Fail`${sky.color} should be ${quote(expectedColor)}`;
|
|
183
193
|
* ```
|
|
184
194
|
*
|
|
185
195
|
* // TODO Update SES-shim to new convention, where `details` is
|
|
186
196
|
* // renamed to `X` rather than `d`.
|
|
187
|
-
* The normal convention is to locally rename `quote` to `q`
|
|
188
|
-
* `details
|
|
189
|
-
* ```js
|
|
190
|
-
* const { details: d, quote: q } = assert;
|
|
191
|
-
* ```
|
|
192
|
-
* so the above example would then be
|
|
197
|
+
* The normal convention is to locally rename `details` to `d` and `quote` to `q`
|
|
198
|
+
* like `const { details: d, quote: q } = assert;`, so the above example would then be
|
|
193
199
|
* ```js
|
|
194
|
-
*
|
|
195
|
-
* sky.color,
|
|
196
|
-
* color,
|
|
197
|
-
* d`${sky.color} should be ${q(color)}`,
|
|
198
|
-
* );
|
|
200
|
+
* sky.color === expectedColor || Fail`${sky.color} should be ${q(expectedColor)}`;
|
|
199
201
|
* ```
|
|
200
202
|
*
|
|
201
203
|
* @callback AssertQuote
|
|
@@ -247,75 +249,68 @@
|
|
|
247
249
|
* ```js
|
|
248
250
|
* assert(sky.isBlue(), details`${sky.color} should be "blue"`);
|
|
249
251
|
* ```
|
|
252
|
+
* // TODO Update SES-shim to new convention, where `details` is
|
|
253
|
+
* // renamed to `X` rather than `d`.
|
|
254
|
+
* or following the normal convention to locally rename `details` to `d`
|
|
255
|
+
* and `quote` to `q` like `const { details: d, quote: q } = assert;`:
|
|
256
|
+
* ```js
|
|
257
|
+
* assert(sky.isBlue(), d`${sky.color} should be "blue"`);
|
|
258
|
+
* ```
|
|
259
|
+
* However, note that in most cases it is preferable to instead use the `Fail`
|
|
260
|
+
* template literal tag (which has the same input signature as `details`
|
|
261
|
+
* but automatically creates and throws an error):
|
|
262
|
+
* ```js
|
|
263
|
+
* sky.isBlue() || Fail`${sky.color} should be "blue"`;
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
250
266
|
* The details template tag returns a `DetailsToken` object that can print
|
|
251
267
|
* itself with the formatted message in two ways.
|
|
252
|
-
* It will report
|
|
253
|
-
*
|
|
268
|
+
* It will report full details to the console, but
|
|
269
|
+
* mask embedded substitution values with their typeof information in the thrown error
|
|
254
270
|
* to prevent revealing secrets up the exceptional path. In the example
|
|
255
271
|
* above, the thrown error may reveal only that `sky.color` is a string,
|
|
256
272
|
* whereas the same diagnostic printed to the console reveals that the
|
|
257
|
-
* sky was green.
|
|
273
|
+
* sky was green. This masking can be disabled for an individual substitution value
|
|
274
|
+
* using `quote`.
|
|
258
275
|
*
|
|
259
|
-
* The `raw`
|
|
260
|
-
*
|
|
276
|
+
* The `raw` property of an input template array is ignored, so a simple
|
|
277
|
+
* array of strings may be provided directly.
|
|
261
278
|
*/
|
|
262
279
|
|
|
263
280
|
/**
|
|
264
281
|
* @typedef {(template: TemplateStringsArray | string[], ...args: any) => never} FailTag
|
|
265
|
-
* The `Fail` tamplate tag supports replacing patterns like
|
|
266
|
-
* ```js
|
|
267
|
-
* assert(cond, X`...complaint...`);
|
|
268
|
-
* ```
|
|
269
|
-
* or
|
|
270
|
-
* ```js
|
|
271
|
-
* cond || assert.fail(X`...complaint...`);
|
|
272
|
-
* ```
|
|
273
|
-
* with patterns like
|
|
274
|
-
* ```js
|
|
275
|
-
* cond || Fail`...complaint...`;
|
|
276
|
-
* ```
|
|
277
282
|
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* patterns are not as powerful as the `assert(...)` call at enabling static
|
|
281
|
-
* reasoning. Of the `||`, again due to weaknesses in current TypeScript,
|
|
282
|
-
* the
|
|
283
|
+
* Use the `Fail` function as a template literal tag to efficiently
|
|
284
|
+
* create and throw a `details`-style error only when a condition is not satisfied.
|
|
283
285
|
* ```js
|
|
284
|
-
*
|
|
286
|
+
* condition || Fail`...complaint...`;
|
|
285
287
|
* ```
|
|
286
|
-
*
|
|
288
|
+
* This avoids the overhead of creating usually-unnecessary errors like
|
|
287
289
|
* ```js
|
|
288
|
-
*
|
|
290
|
+
* assert(condition, details`...complaint...`);
|
|
289
291
|
* ```
|
|
290
|
-
*
|
|
291
|
-
* return to the
|
|
292
|
+
* while improving readability over alternatives like
|
|
292
293
|
* ```js
|
|
293
|
-
* assert(
|
|
294
|
+
* condition || assert.fail(details`...complaint...`);
|
|
294
295
|
* ```
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
* ```
|
|
301
|
-
* because of the verbosity and loss of readability. Instead, until/unless
|
|
302
|
-
* https://github.com/microsoft/TypeScript/issues/51426 is fixed, for those
|
|
303
|
-
* new-style assertions where this loss of static reasoning is a problem,
|
|
296
|
+
*
|
|
297
|
+
* However, due to current weakness in TypeScript, static reasoning
|
|
298
|
+
* is less powerful with the `||` patterns than with an `assert` call.
|
|
299
|
+
* Until/unless https://github.com/microsoft/TypeScript/issues/51426 is fixed,
|
|
300
|
+
* for `||`-style assertions where this loss of static reasoning is a problem,
|
|
304
301
|
* instead express the assertion as
|
|
305
302
|
* ```js
|
|
306
|
-
* if (!
|
|
303
|
+
* if (!condition) {
|
|
307
304
|
* Fail`...complaint...`;
|
|
308
305
|
* }
|
|
309
306
|
* ```
|
|
310
307
|
* or, if needed,
|
|
311
308
|
* ```js
|
|
312
|
-
* if (!
|
|
313
|
-
* // `throw` is noop since `Fail` throws
|
|
309
|
+
* if (!condition) {
|
|
310
|
+
* // `throw` is noop since `Fail` throws, but it improves static analysis
|
|
314
311
|
* throw Fail`...complaint...`;
|
|
315
312
|
* }
|
|
316
313
|
* ```
|
|
317
|
-
* This avoid the TypeScript bugs that cause the loss of static reasoning,
|
|
318
|
-
* but with no loss of efficiency and little loss of readability.
|
|
319
314
|
*/
|
|
320
315
|
|
|
321
316
|
/**
|
|
@@ -353,7 +348,7 @@
|
|
|
353
348
|
// /////////////////////////////////////////////////////////////////////////////
|
|
354
349
|
|
|
355
350
|
/**
|
|
356
|
-
* @typedef {
|
|
351
|
+
* @typedef {object} VirtualConsole
|
|
357
352
|
* @property {Console['debug']} debug
|
|
358
353
|
* @property {Console['log']} log
|
|
359
354
|
* @property {Console['info']} info
|
|
@@ -20,7 +20,7 @@ import { InertCompartment } from './compartment-shim.js';
|
|
|
20
20
|
* Object.getConstructorOf()
|
|
21
21
|
* Helper function to improve readability, similar to Object.getPrototypeOf().
|
|
22
22
|
*
|
|
23
|
-
* @param {
|
|
23
|
+
* @param {object} obj
|
|
24
24
|
*/
|
|
25
25
|
function getConstructorOf(obj) {
|
|
26
26
|
return getPrototypeOf(obj).constructor;
|
|
@@ -38,15 +38,17 @@ function makeArguments() {
|
|
|
38
38
|
* Get the intrinsics not otherwise reachable by named own property
|
|
39
39
|
* traversal from the global object.
|
|
40
40
|
*
|
|
41
|
-
* @returns {
|
|
41
|
+
* @returns {object}
|
|
42
42
|
*/
|
|
43
43
|
export const getAnonymousIntrinsics = () => {
|
|
44
44
|
const InertFunction = FERAL_FUNCTION.prototype.constructor;
|
|
45
45
|
|
|
46
46
|
// 9.2.4.1 %ThrowTypeError%
|
|
47
47
|
|
|
48
|
-
const ThrowTypeError = getOwnPropertyDescriptor(
|
|
49
|
-
|
|
48
|
+
const ThrowTypeError = getOwnPropertyDescriptor(
|
|
49
|
+
makeArguments(),
|
|
50
|
+
'callee',
|
|
51
|
+
).get;
|
|
50
52
|
|
|
51
53
|
// 21.1.5.2 The %StringIteratorPrototype% Object
|
|
52
54
|
|