ses 1.2.0 → 1.4.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/NEWS.md +20 -0
- package/dist/lockdown.cjs +397 -91
- package/dist/lockdown.mjs +397 -91
- package/dist/lockdown.umd.js +397 -91
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +397 -91
- package/dist/ses.mjs +397 -91
- package/dist/ses.umd.js +397 -91
- package/dist/ses.umd.min.js +1 -1
- package/index.js +1 -0
- package/package.json +10 -9
- package/src/commons.js +4 -0
- package/src/console-shim.js +50 -0
- package/src/enablements.js +6 -0
- package/src/error/README.md +9 -1
- package/src/error/assert.js +37 -9
- package/src/error/console.js +156 -17
- package/src/error/internal-types.js +6 -1
- package/src/error/tame-console.js +8 -8
- package/src/error/types.js +45 -27
- package/src/make-eval-function.js +1 -1
- package/src/permits.js +25 -7
- package/src/tame-regexp-constructor.js +13 -6
- package/types.d.ts +16 -4
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -66,13 +66,13 @@
|
|
|
66
66
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@endo/env-options": "^1.1.
|
|
69
|
+
"@endo/env-options": "^1.1.2"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@endo/compartment-mapper": "^1.1.
|
|
73
|
-
"@endo/static-module-record": "^1.0
|
|
74
|
-
"@endo/test262-runner": "^0.1.
|
|
75
|
-
"ava": "^
|
|
72
|
+
"@endo/compartment-mapper": "^1.1.3",
|
|
73
|
+
"@endo/static-module-record": "^1.1.0",
|
|
74
|
+
"@endo/test262-runner": "^0.1.35",
|
|
75
|
+
"ava": "^6.1.2",
|
|
76
76
|
"babel-eslint": "^10.0.3",
|
|
77
77
|
"c8": "^7.14.0",
|
|
78
78
|
"core-js": "^3.31.0",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"sinon": "^15.1.0",
|
|
86
86
|
"terser": "^5.16.6",
|
|
87
87
|
"tsd": "^0.28.1",
|
|
88
|
-
"typescript": "~5.
|
|
88
|
+
"typescript": "~5.4.2"
|
|
89
89
|
},
|
|
90
90
|
"files": [
|
|
91
91
|
"LICENSE*",
|
|
@@ -161,7 +161,8 @@
|
|
|
161
161
|
"isNaN",
|
|
162
162
|
"parseFloat",
|
|
163
163
|
"parseInt",
|
|
164
|
-
"unescape"
|
|
164
|
+
"unescape",
|
|
165
|
+
"AggregateError"
|
|
165
166
|
],
|
|
166
167
|
"@endo/no-polymorphic-call": "error"
|
|
167
168
|
},
|
|
@@ -188,5 +189,5 @@
|
|
|
188
189
|
"typeCoverage": {
|
|
189
190
|
"atLeast": 81.17
|
|
190
191
|
},
|
|
191
|
-
"gitHead": "
|
|
192
|
+
"gitHead": "36384eb5a4bbd11dd3d06a8045d620f8d6569590"
|
|
192
193
|
}
|
package/src/commons.js
CHANGED
|
@@ -47,6 +47,7 @@ export const {
|
|
|
47
47
|
ReferenceError,
|
|
48
48
|
SyntaxError,
|
|
49
49
|
TypeError,
|
|
50
|
+
AggregateError,
|
|
50
51
|
} = globalThis;
|
|
51
52
|
|
|
52
53
|
export const {
|
|
@@ -158,6 +159,9 @@ export const arrayIncludes = uncurryThis(arrayPrototype.includes);
|
|
|
158
159
|
export const arrayJoin = uncurryThis(arrayPrototype.join);
|
|
159
160
|
/** @type {<T, U>(thisArg: readonly T[], callbackfn: (value: T, index: number, array: T[]) => U, cbThisArg?: any) => U[]} */
|
|
160
161
|
export const arrayMap = /** @type {any} */ (uncurryThis(arrayPrototype.map));
|
|
162
|
+
export const arrayFlatMap = /** @type {any} */ (
|
|
163
|
+
uncurryThis(arrayPrototype.flatMap)
|
|
164
|
+
);
|
|
161
165
|
export const arrayPop = uncurryThis(arrayPrototype.pop);
|
|
162
166
|
/** @type {<T>(thisArg: T[], ...items: T[]) => number} */
|
|
163
167
|
export const arrayPush = uncurryThis(arrayPrototype.push);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { symbolFor, globalThis } from './commons.js';
|
|
2
|
+
import { defineCausalConsoleFromLogger } from './error/console.js';
|
|
3
|
+
import { loggedErrorHandler } from './error/assert.js';
|
|
4
|
+
|
|
5
|
+
// TODO possible additional exports. Some are privileged.
|
|
6
|
+
// export { loggedErrorHandler };
|
|
7
|
+
// export {
|
|
8
|
+
// makeCausalConsole,
|
|
9
|
+
// consoleLevelMethods,
|
|
10
|
+
// consoleOtherMethods,
|
|
11
|
+
// makeLoggingConsoleKit,
|
|
12
|
+
// filterConsole,
|
|
13
|
+
// pumpLogToConsole,
|
|
14
|
+
// } from './src/error/console.js';
|
|
15
|
+
// export { assertLogs, throwsAndLogs } from './src/error/throws-and-logs.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Makes a Console like the
|
|
19
|
+
* [SES causal `console`](https://github.com/endojs/endo/blob/master/packages/ses/src/error/README.md)
|
|
20
|
+
* but whose output is redirected to the supplied `logger` function.
|
|
21
|
+
*/
|
|
22
|
+
const makeCausalConsoleFromLoggerForSesAva =
|
|
23
|
+
defineCausalConsoleFromLogger(loggedErrorHandler);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
*`makeCausalConsoleFromLoggerForSesAva` is privileged because it exposes
|
|
27
|
+
* unredacted error info onto the `Logger` provided by the caller. It
|
|
28
|
+
* should not be made available to non-privileged code.
|
|
29
|
+
*
|
|
30
|
+
* Further, we consider this particular API choice to be experimental
|
|
31
|
+
* and may change in the future. It is currently only intended for use by
|
|
32
|
+
* `@endo/ses-ava`, with which it will be co-maintained.
|
|
33
|
+
*
|
|
34
|
+
* Thus, this `console-shim.js` makes `makeCausalConsoleFromLoggerForSesAva`
|
|
35
|
+
* available on `globalThis` which it *assumes* is the global of the start
|
|
36
|
+
* compartment and is therefore allowed to hold powers that should not be
|
|
37
|
+
* available in constructed compartments. It makes it available as the value of
|
|
38
|
+
* a global property named by a registered symbol named
|
|
39
|
+
* `MAKE_CAUSAL_CONSOLE_FROM_LOGGER_KEY_FOR_SES_AVA`.
|
|
40
|
+
*
|
|
41
|
+
* Anyone accessing this, including `@endo/ses-ava`, should feature test for
|
|
42
|
+
* this and be tolerant of its absence. It may indeed disappear from later
|
|
43
|
+
* versions of the ses-shim.
|
|
44
|
+
*/
|
|
45
|
+
const MAKE_CAUSAL_CONSOLE_FROM_LOGGER_KEY_FOR_SES_AVA = symbolFor(
|
|
46
|
+
'MAKE_CAUSAL_CONSOLE_FROM_LOGGER_KEY_FOR_SES_AVA',
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
globalThis[MAKE_CAUSAL_CONSOLE_FROM_LOGGER_KEY_FOR_SES_AVA] =
|
|
50
|
+
makeCausalConsoleFromLoggerForSesAva;
|
package/src/enablements.js
CHANGED
|
@@ -149,6 +149,12 @@ export const moderateEnablements = {
|
|
|
149
149
|
name: true, // set by "node 14"
|
|
150
150
|
},
|
|
151
151
|
|
|
152
|
+
// https://github.com/endojs/endo/issues/550
|
|
153
|
+
'%AggregateErrorPrototype%': {
|
|
154
|
+
message: true, // to match TypeErrorPrototype.message
|
|
155
|
+
name: true, // set by "node 14"?
|
|
156
|
+
},
|
|
157
|
+
|
|
152
158
|
'%PromisePrototype%': {
|
|
153
159
|
constructor: true, // set by "core-js"
|
|
154
160
|
},
|
package/src/error/README.md
CHANGED
|
@@ -6,7 +6,15 @@ Summary
|
|
|
6
6
|
* SES tames the global `console` and grants it the ability to reveal error annotations and stacks to the actual console.
|
|
7
7
|
* Both `assert` and `console` are powerful globals that SES does not implicitly carry into child compartments. When creating a child compartment, add `assert` to the compartment’s globals. Either add `console` too, or add a wrapper that annotates the console with a topic.
|
|
8
8
|
* SES hides annotations and stack traces by default. To reveal them, SES uses mechanisms like `process.on("uncaughtException")` in Node.js to catch the error and log it back to the `console` tamed by `lockdown`.
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
We refer to the enhanced `console`, installed by default by the ses shim, as the *causal console*, because the annotations it reveals are often used to show causality information. For example, with the [`TRACK_TURNS=enabled`](https://github.com/Agoric/agoric-sdk/blob/master/docs/env.md#track_turns) and [`DEBUG=track-turns`](https://github.com/Agoric/agoric-sdk/blob/master/docs/env.md#debug) environment options set
|
|
11
|
+
```sh
|
|
12
|
+
# in bash syntax
|
|
13
|
+
export DEBUG=track-turns
|
|
14
|
+
export TRACK_TURNS=enabled
|
|
15
|
+
```
|
|
16
|
+
the @endo/eventual-send package will use annotations to show where previous `E` operations (either eventual sends or `E.when`) in previous turns *locally in the same vat* caused the turn with the current error. This is sometimes called "deep asynchronous stacks".
|
|
17
|
+
* In the scope of the Agoric software ecosystem, this architecture will allow us to eventually introduce a more powerful distributed causal `console` that can meaningfully capture stack traces for a distributed debugger, based on the design of [Causeway](https://github.com/Agoric/agoric-sdk/issues/1318#issuecomment-662127549).
|
|
10
18
|
|
|
11
19
|
|
|
12
20
|
## Goals, non-goals, and partial goals
|
package/src/error/assert.js
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
arrayPush,
|
|
22
22
|
assign,
|
|
23
23
|
freeze,
|
|
24
|
+
defineProperty,
|
|
24
25
|
globalThis,
|
|
25
26
|
is,
|
|
26
27
|
isError,
|
|
@@ -33,6 +34,7 @@ import {
|
|
|
33
34
|
weakmapGet,
|
|
34
35
|
weakmapHas,
|
|
35
36
|
weakmapSet,
|
|
37
|
+
AggregateError,
|
|
36
38
|
} from '../commons.js';
|
|
37
39
|
import { an, bestEffortStringify } from './stringify-utils.js';
|
|
38
40
|
import './types.js';
|
|
@@ -257,8 +259,8 @@ const tagError = (err, optErrorName = err.name) => {
|
|
|
257
259
|
*/
|
|
258
260
|
const makeError = (
|
|
259
261
|
optDetails = redactedDetails`Assert failed`,
|
|
260
|
-
|
|
261
|
-
{ errorName = undefined } = {},
|
|
262
|
+
errConstructor = globalThis.Error,
|
|
263
|
+
{ errorName = undefined, cause = undefined, errors = undefined } = {},
|
|
262
264
|
) => {
|
|
263
265
|
if (typeof optDetails === 'string') {
|
|
264
266
|
// If it is a string, use it as the literal part of the template so
|
|
@@ -270,7 +272,29 @@ const makeError = (
|
|
|
270
272
|
throw TypeError(`unrecognized details ${quote(optDetails)}`);
|
|
271
273
|
}
|
|
272
274
|
const messageString = getMessageString(hiddenDetails);
|
|
273
|
-
const
|
|
275
|
+
const opts = cause && { cause };
|
|
276
|
+
let error;
|
|
277
|
+
if (
|
|
278
|
+
typeof AggregateError !== 'undefined' &&
|
|
279
|
+
errConstructor === AggregateError
|
|
280
|
+
) {
|
|
281
|
+
error = AggregateError(errors || [], messageString, opts);
|
|
282
|
+
} else {
|
|
283
|
+
error = /** @type {ErrorConstructor} */ (errConstructor)(
|
|
284
|
+
messageString,
|
|
285
|
+
opts,
|
|
286
|
+
);
|
|
287
|
+
if (errors !== undefined) {
|
|
288
|
+
// Since we need to tolerate `errors` on an AggregateError, may as
|
|
289
|
+
// well tolerate it on all errors.
|
|
290
|
+
defineProperty(error, 'errors', {
|
|
291
|
+
value: errors,
|
|
292
|
+
writable: true,
|
|
293
|
+
enumerable: false,
|
|
294
|
+
configurable: true,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}
|
|
274
298
|
weakmapSet(hiddenMessageLogArgs, error, getLogArgs(hiddenDetails));
|
|
275
299
|
if (errorName !== undefined) {
|
|
276
300
|
tagError(error, errorName);
|
|
@@ -382,9 +406,10 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
382
406
|
/** @type {AssertFail} */
|
|
383
407
|
const fail = (
|
|
384
408
|
optDetails = assertFailedDetails,
|
|
385
|
-
|
|
409
|
+
errConstructor = undefined,
|
|
410
|
+
options = undefined,
|
|
386
411
|
) => {
|
|
387
|
-
const reason = makeError(optDetails,
|
|
412
|
+
const reason = makeError(optDetails, errConstructor, options);
|
|
388
413
|
if (optRaise !== undefined) {
|
|
389
414
|
optRaise(reason);
|
|
390
415
|
}
|
|
@@ -402,9 +427,10 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
402
427
|
function baseAssert(
|
|
403
428
|
flag,
|
|
404
429
|
optDetails = undefined,
|
|
405
|
-
|
|
430
|
+
errConstructor = undefined,
|
|
431
|
+
options = undefined,
|
|
406
432
|
) {
|
|
407
|
-
flag || fail(optDetails,
|
|
433
|
+
flag || fail(optDetails, errConstructor, options);
|
|
408
434
|
}
|
|
409
435
|
|
|
410
436
|
/** @type {AssertEqual} */
|
|
@@ -412,12 +438,14 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
412
438
|
actual,
|
|
413
439
|
expected,
|
|
414
440
|
optDetails = undefined,
|
|
415
|
-
|
|
441
|
+
errConstructor = undefined,
|
|
442
|
+
options = undefined,
|
|
416
443
|
) => {
|
|
417
444
|
is(actual, expected) ||
|
|
418
445
|
fail(
|
|
419
446
|
optDetails || details`Expected ${actual} is same as ${expected}`,
|
|
420
|
-
|
|
447
|
+
errConstructor || RangeError,
|
|
448
|
+
options,
|
|
421
449
|
);
|
|
422
450
|
};
|
|
423
451
|
freeze(equal);
|
package/src/error/console.js
CHANGED
|
@@ -8,13 +8,17 @@
|
|
|
8
8
|
import {
|
|
9
9
|
WeakSet,
|
|
10
10
|
arrayFilter,
|
|
11
|
+
arrayFlatMap,
|
|
11
12
|
arrayMap,
|
|
13
|
+
arrayPop,
|
|
12
14
|
arrayPush,
|
|
13
15
|
defineProperty,
|
|
14
16
|
freeze,
|
|
15
17
|
fromEntries,
|
|
16
18
|
isError,
|
|
17
19
|
stringEndsWith,
|
|
20
|
+
stringIncludes,
|
|
21
|
+
stringSplit,
|
|
18
22
|
weaksetAdd,
|
|
19
23
|
weaksetHas,
|
|
20
24
|
} from '../commons.js';
|
|
@@ -46,8 +50,17 @@ import './internal-types.js';
|
|
|
46
50
|
|
|
47
51
|
/** @typedef {keyof VirtualConsole | 'profile' | 'profileEnd'} ConsoleProps */
|
|
48
52
|
|
|
49
|
-
/**
|
|
50
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Those console methods whose actual parameters are `(fmt?, ...args)`
|
|
55
|
+
* even if their TypeScript types claim otherwise.
|
|
56
|
+
*
|
|
57
|
+
* Each is paired with what we consider to be their log severity level.
|
|
58
|
+
* This is the same as the log severity of these on other
|
|
59
|
+
* platform console implementations when they all agree.
|
|
60
|
+
*
|
|
61
|
+
* @type {readonly [ConsoleProps, LogSeverity | undefined][]}
|
|
62
|
+
*/
|
|
63
|
+
export const consoleLevelMethods = freeze([
|
|
51
64
|
['debug', 'debug'], // (fmt?, ...args) verbose level on Chrome
|
|
52
65
|
['log', 'log'], // (fmt?, ...args) info level on Chrome
|
|
53
66
|
['info', 'info'], // (fmt?, ...args)
|
|
@@ -55,13 +68,22 @@ const consoleLevelMethods = freeze([
|
|
|
55
68
|
['error', 'error'], // (fmt?, ...args)
|
|
56
69
|
|
|
57
70
|
['trace', 'log'], // (fmt?, ...args)
|
|
58
|
-
['dirxml', 'log'], // (fmt?, ...args)
|
|
59
|
-
['group', 'log'], // (fmt?, ...args)
|
|
60
|
-
['groupCollapsed', 'log'], // (fmt?, ...args)
|
|
71
|
+
['dirxml', 'log'], // (fmt?, ...args) but TS typed (...data)
|
|
72
|
+
['group', 'log'], // (fmt?, ...args) but TS typed (...label)
|
|
73
|
+
['groupCollapsed', 'log'], // (fmt?, ...args) but TS typed (...label)
|
|
61
74
|
]);
|
|
62
75
|
|
|
63
|
-
/**
|
|
64
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Those console methods other than those already enumerated by
|
|
78
|
+
* `consoleLevelMethods`.
|
|
79
|
+
*
|
|
80
|
+
* Each is paired with what we consider to be their log severity level.
|
|
81
|
+
* This is the same as the log severity of these on other
|
|
82
|
+
* platform console implementations when they all agree.
|
|
83
|
+
*
|
|
84
|
+
* @type {readonly [ConsoleProps, LogSeverity | undefined][]}
|
|
85
|
+
*/
|
|
86
|
+
export const consoleOtherMethods = freeze([
|
|
65
87
|
['assert', 'error'], // (value, fmt?, ...args)
|
|
66
88
|
['timeLog', 'log'], // (label?, ...args) no fmt string
|
|
67
89
|
|
|
@@ -85,7 +107,7 @@ const consoleOtherMethods = freeze([
|
|
|
85
107
|
]);
|
|
86
108
|
|
|
87
109
|
/** @type {readonly [ConsoleProps, LogSeverity | undefined][]} */
|
|
88
|
-
|
|
110
|
+
const consoleWhitelist = freeze([
|
|
89
111
|
...consoleLevelMethods,
|
|
90
112
|
...consoleOtherMethods,
|
|
91
113
|
]);
|
|
@@ -117,10 +139,10 @@ export const consoleWhitelist = freeze([
|
|
|
117
139
|
* ]);
|
|
118
140
|
*/
|
|
119
141
|
|
|
120
|
-
//
|
|
142
|
+
// //////////////////////////// Logging Console ////////////////////////////////
|
|
121
143
|
|
|
122
144
|
/** @type {MakeLoggingConsoleKit} */
|
|
123
|
-
const makeLoggingConsoleKit = (
|
|
145
|
+
export const makeLoggingConsoleKit = (
|
|
124
146
|
loggedErrorHandler,
|
|
125
147
|
{ shouldResetForDebugging = false } = {},
|
|
126
148
|
) => {
|
|
@@ -161,19 +183,35 @@ const makeLoggingConsoleKit = (
|
|
|
161
183
|
return freeze({ loggingConsole: typedLoggingConsole, takeLog });
|
|
162
184
|
};
|
|
163
185
|
freeze(makeLoggingConsoleKit);
|
|
164
|
-
export { makeLoggingConsoleKit };
|
|
165
186
|
|
|
166
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Makes the same calls on a `baseConsole` that were made on a
|
|
189
|
+
* `loggingConsole` to produce this `log`. In this way, a logging console
|
|
190
|
+
* can be used as a buffer to delay the application of these calls to a
|
|
191
|
+
* `baseConsole`.
|
|
192
|
+
*
|
|
193
|
+
* @param {LogRecord[]} log
|
|
194
|
+
* @param {VirtualConsole} baseConsole
|
|
195
|
+
*/
|
|
196
|
+
export const pumpLogToConsole = (log, baseConsole) => {
|
|
197
|
+
for (const [name, ...args] of log) {
|
|
198
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
199
|
+
baseConsole[name](...args);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
// //////////////////////////// Causal Console /////////////////////////////////
|
|
167
203
|
|
|
168
204
|
/** @type {ErrorInfo} */
|
|
169
205
|
const ErrorInfo = {
|
|
170
206
|
NOTE: 'ERROR_NOTE:',
|
|
171
207
|
MESSAGE: 'ERROR_MESSAGE:',
|
|
208
|
+
CAUSE: 'cause:',
|
|
209
|
+
ERRORS: 'errors:',
|
|
172
210
|
};
|
|
173
211
|
freeze(ErrorInfo);
|
|
174
212
|
|
|
175
213
|
/** @type {MakeCausalConsole} */
|
|
176
|
-
const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
214
|
+
export const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
177
215
|
if (!baseConsole) {
|
|
178
216
|
return undefined;
|
|
179
217
|
}
|
|
@@ -308,6 +346,14 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
308
346
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
309
347
|
baseConsole[severity](stackString);
|
|
310
348
|
// Show the other annotations on error
|
|
349
|
+
if (error.cause) {
|
|
350
|
+
logErrorInfo(severity, error, ErrorInfo.CAUSE, [error.cause], subErrors);
|
|
351
|
+
}
|
|
352
|
+
// @ts-expect-error AggregateError has an `errors` property.
|
|
353
|
+
if (error.errors) {
|
|
354
|
+
// @ts-expect-error AggregateError has an `errors` property.
|
|
355
|
+
logErrorInfo(severity, error, ErrorInfo.ERRORS, error.errors, subErrors);
|
|
356
|
+
}
|
|
311
357
|
for (const noteLogArgs of noteLogArgsArray) {
|
|
312
358
|
logErrorInfo(severity, error, ErrorInfo.NOTE, noteLogArgs, subErrors);
|
|
313
359
|
}
|
|
@@ -352,12 +398,106 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
|
|
|
352
398
|
return /** @type {VirtualConsole} */ (freeze(causalConsole));
|
|
353
399
|
};
|
|
354
400
|
freeze(makeCausalConsole);
|
|
355
|
-
export { makeCausalConsole };
|
|
356
401
|
|
|
357
|
-
|
|
402
|
+
/**
|
|
403
|
+
* @typedef {(...args: unknown[]) => void} Logger
|
|
404
|
+
*/
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* This is a rather horrible kludge to indent the output to a logger in
|
|
408
|
+
* the case where some arguments are strings containing newlines. Part of
|
|
409
|
+
* the problem is that console-like loggers, including the one in ava,
|
|
410
|
+
* join the string arguments of the log message with a space.
|
|
411
|
+
* Because of this, there's an extra space at the beginning of each of
|
|
412
|
+
* the split lines. So this kludge compensated by putting an extra empty
|
|
413
|
+
* string at the beginning, so that the logger will add the same extra
|
|
414
|
+
* joiner.
|
|
415
|
+
* TODO: Fix this horrible kludge, and indent in a sane manner.
|
|
416
|
+
*
|
|
417
|
+
* @param {string} str
|
|
418
|
+
* @param {string} sep
|
|
419
|
+
* @param {string[]} indents
|
|
420
|
+
* @returns {string[]}
|
|
421
|
+
*/
|
|
422
|
+
const indentAfterAllSeps = (str, sep, indents) => {
|
|
423
|
+
const [firstLine, ...restLines] = stringSplit(str, sep);
|
|
424
|
+
const indentedRest = arrayFlatMap(restLines, line => [sep, ...indents, line]);
|
|
425
|
+
return ['', firstLine, ...indentedRest];
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* @param {LoggedErrorHandler} loggedErrorHandler
|
|
430
|
+
*/
|
|
431
|
+
export const defineCausalConsoleFromLogger = loggedErrorHandler => {
|
|
432
|
+
/**
|
|
433
|
+
* Implement the `VirtualConsole` API badly by turning all calls into
|
|
434
|
+
* calls on `tlogger`. We need to do this to have `console` logging
|
|
435
|
+
* turn into calls to Ava's `t.log`, so these console log messages
|
|
436
|
+
* are output in the right place.
|
|
437
|
+
*
|
|
438
|
+
* @param {Logger} tlogger
|
|
439
|
+
* @returns {VirtualConsole}
|
|
440
|
+
*/
|
|
441
|
+
const makeCausalConsoleFromLogger = tlogger => {
|
|
442
|
+
const indents = [];
|
|
443
|
+
const logWithIndent = (...args) => {
|
|
444
|
+
if (indents.length > 0) {
|
|
445
|
+
args = arrayFlatMap(args, arg =>
|
|
446
|
+
typeof arg === 'string' && stringIncludes(arg, '\n')
|
|
447
|
+
? indentAfterAllSeps(arg, '\n', indents)
|
|
448
|
+
: [arg],
|
|
449
|
+
);
|
|
450
|
+
args = [...indents, ...args];
|
|
451
|
+
}
|
|
452
|
+
return tlogger(...args);
|
|
453
|
+
};
|
|
454
|
+
const makeNamed = (name, fn) =>
|
|
455
|
+
({ [name]: (...args) => fn(...args) })[name];
|
|
456
|
+
|
|
457
|
+
const baseConsole = fromEntries([
|
|
458
|
+
...arrayMap(consoleLevelMethods, ([name]) => [
|
|
459
|
+
name,
|
|
460
|
+
makeNamed(name, logWithIndent),
|
|
461
|
+
]),
|
|
462
|
+
...arrayMap(consoleOtherMethods, ([name]) => [
|
|
463
|
+
name,
|
|
464
|
+
makeNamed(name, (...args) => logWithIndent(name, ...args)),
|
|
465
|
+
]),
|
|
466
|
+
]);
|
|
467
|
+
// https://console.spec.whatwg.org/#grouping
|
|
468
|
+
for (const name of ['group', 'groupCollapsed']) {
|
|
469
|
+
if (baseConsole[name]) {
|
|
470
|
+
baseConsole[name] = makeNamed(name, (...args) => {
|
|
471
|
+
if (args.length >= 1) {
|
|
472
|
+
// Prefix the logged data with "group" or "groupCollapsed".
|
|
473
|
+
logWithIndent(...args);
|
|
474
|
+
}
|
|
475
|
+
// A single space is enough;
|
|
476
|
+
// the host console will separate them with additional spaces.
|
|
477
|
+
arrayPush(indents, ' ');
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (baseConsole.groupEnd) {
|
|
482
|
+
baseConsole.groupEnd = makeNamed('groupEnd', (...args) => {
|
|
483
|
+
arrayPop(indents);
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
harden(baseConsole);
|
|
487
|
+
const causalConsole = makeCausalConsole(
|
|
488
|
+
/** @type {VirtualConsole} */ (baseConsole),
|
|
489
|
+
loggedErrorHandler,
|
|
490
|
+
);
|
|
491
|
+
return /** @type {VirtualConsole} */ (causalConsole);
|
|
492
|
+
};
|
|
493
|
+
return freeze(makeCausalConsoleFromLogger);
|
|
494
|
+
};
|
|
495
|
+
freeze(defineCausalConsoleFromLogger);
|
|
496
|
+
|
|
497
|
+
// ///////////////////////// Filter Console ////////////////////////////////////
|
|
358
498
|
|
|
359
499
|
/** @type {FilterConsole} */
|
|
360
|
-
const filterConsole = (baseConsole, filter, _topic = undefined) => {
|
|
500
|
+
export const filterConsole = (baseConsole, filter, _topic = undefined) => {
|
|
361
501
|
// TODO do something with optional topic string
|
|
362
502
|
const whitelist = arrayFilter(
|
|
363
503
|
consoleWhitelist,
|
|
@@ -381,4 +521,3 @@ const filterConsole = (baseConsole, filter, _topic = undefined) => {
|
|
|
381
521
|
return /** @type {VirtualConsole} */ (freeze(filteringConsole));
|
|
382
522
|
};
|
|
383
523
|
freeze(filterConsole);
|
|
384
|
-
export { filterConsole };
|
|
@@ -58,14 +58,14 @@ export const tameConsole = (
|
|
|
58
58
|
typeof globalThis.console !== 'undefined'
|
|
59
59
|
? globalThis.console
|
|
60
60
|
: typeof globalThis.print === 'function'
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
? // Make a good-enough console for eshost (including only functions that
|
|
62
|
+
// log at a specific level with no special argument interpretation).
|
|
63
|
+
// https://console.spec.whatwg.org/#logging
|
|
64
|
+
(p => freeze({ debug: p, log: p, info: p, warn: p, error: p }))(
|
|
65
|
+
// eslint-disable-next-line no-undef
|
|
66
|
+
wrapLogger(globalThis.print),
|
|
67
|
+
)
|
|
68
|
+
: undefined
|
|
69
69
|
);
|
|
70
70
|
|
|
71
71
|
// Upgrade a log-only console (as in `eshost -h SpiderMonkey`).
|