ses 0.18.1 → 0.18.3
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 +15 -0
- package/README.md +9 -4
- package/SECURITY.md +1 -1
- package/dist/lockdown.cjs +753 -384
- package/dist/lockdown.mjs +753 -384
- package/dist/lockdown.umd.js +753 -384
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +753 -384
- package/dist/ses.mjs +753 -384
- package/dist/ses.umd.js +753 -384
- package/dist/ses.umd.min.js +1 -1
- package/index.js +1 -0
- package/package.json +16 -21
- package/src/commons.js +4 -0
- package/src/compartment-shim.js +3 -3
- package/src/enablements.js +13 -1
- package/src/environment-options.js +10 -19
- package/src/error/assert.js +28 -35
- package/src/error/internal-types.js +3 -3
- package/src/error/note-log-args.js +280 -0
- package/src/error/types.js +51 -70
- package/src/get-anonymous-intrinsics.js +2 -2
- 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 +14 -8
- package/src/make-hardener.js +1 -1
- package/src/make-safe-evaluator.js +4 -4
- package/src/module-instance.js +11 -5
- package/src/module-load.js +1 -1
- package/src/scope-constants.js +2 -2
- package/src/sloppy-globals-scope-terminator.js +1 -0
- package/src/strict-scope-terminator.js +2 -3
- package/src/tame-domains.js +1 -0
- package/src/tame-harden.js +23 -0
- package/src/tame-locale-methods.js +2 -2
- package/src/transforms.js +15 -0
- package/src/whitelist-intrinsics.js +2 -2
- package/src/whitelist.js +9 -1
- package/tools.js +25 -0
- package/{index.d.ts → types.d.ts} +1 -0
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.3",
|
|
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,31 @@
|
|
|
59
60
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
62
|
-
"@endo/compartment-mapper": "^0.8.
|
|
63
|
-
"@endo/
|
|
64
|
-
"@endo/
|
|
65
|
-
"
|
|
66
|
-
"ava": "^5.1.0",
|
|
63
|
+
"@endo/compartment-mapper": "^0.8.3",
|
|
64
|
+
"@endo/static-module-record": "^0.7.18",
|
|
65
|
+
"@endo/test262-runner": "^0.1.31",
|
|
66
|
+
"ava": "^5.2.0",
|
|
67
67
|
"babel-eslint": "^10.0.3",
|
|
68
68
|
"c8": "^7.7.3",
|
|
69
69
|
"eslint": "^7.32.0",
|
|
70
70
|
"eslint-config-airbnb-base": "^14.0.0",
|
|
71
|
-
"eslint-config-prettier": "^
|
|
71
|
+
"eslint-config-prettier": "^8.8.0",
|
|
72
72
|
"eslint-plugin-eslint-comments": "^3.1.2",
|
|
73
|
-
"eslint-plugin-import": "^2.
|
|
74
|
-
"
|
|
75
|
-
"prettier": "^2.8.0",
|
|
73
|
+
"eslint-plugin-import": "^2.27.5",
|
|
74
|
+
"prettier": "^2.8.5",
|
|
76
75
|
"sinon": "8.0.4",
|
|
77
|
-
"terser": "^
|
|
76
|
+
"terser": "^5.16.6",
|
|
78
77
|
"tsd": "^0.24.1",
|
|
79
|
-
"typescript": "~4.
|
|
78
|
+
"typescript": "~4.9.5"
|
|
80
79
|
},
|
|
81
80
|
"files": [
|
|
82
81
|
"LICENSE*",
|
|
83
82
|
"NEWS*",
|
|
84
83
|
"SECURITY*",
|
|
85
84
|
"dist",
|
|
86
|
-
"
|
|
85
|
+
"types.d.ts",
|
|
87
86
|
"index.js",
|
|
87
|
+
"tools.js",
|
|
88
88
|
"src"
|
|
89
89
|
],
|
|
90
90
|
"private": false,
|
|
@@ -93,7 +93,7 @@
|
|
|
93
93
|
},
|
|
94
94
|
"eslintConfig": {
|
|
95
95
|
"extends": [
|
|
96
|
-
"
|
|
96
|
+
"plugin:@endo/internal"
|
|
97
97
|
],
|
|
98
98
|
"rules": {
|
|
99
99
|
"no-restricted-globals": [
|
|
@@ -169,16 +169,11 @@
|
|
|
169
169
|
}
|
|
170
170
|
]
|
|
171
171
|
},
|
|
172
|
-
"prettier": {
|
|
173
|
-
"arrowParens": "avoid",
|
|
174
|
-
"trailingComma": "all",
|
|
175
|
-
"singleQuote": true
|
|
176
|
-
},
|
|
177
172
|
"ava": {
|
|
178
173
|
"files": [
|
|
179
174
|
"test/**/test-*.js"
|
|
180
175
|
],
|
|
181
176
|
"timeout": "2m"
|
|
182
177
|
},
|
|
183
|
-
"gitHead": "
|
|
178
|
+
"gitHead": "8141f58debaec420cbc2a7cb698e888566d430ba"
|
|
184
179
|
}
|
package/src/commons.js
CHANGED
|
@@ -59,6 +59,8 @@ export const {
|
|
|
59
59
|
getOwnPropertyNames,
|
|
60
60
|
getPrototypeOf,
|
|
61
61
|
is,
|
|
62
|
+
isFrozen,
|
|
63
|
+
isSealed,
|
|
62
64
|
isExtensible,
|
|
63
65
|
keys,
|
|
64
66
|
prototype: objectPrototype,
|
|
@@ -97,6 +99,7 @@ export const defineProperty = (object, prop, descriptor) => {
|
|
|
97
99
|
// Instead, to workaround the Safari bug
|
|
98
100
|
const result = originalDefineProperty(object, prop, descriptor);
|
|
99
101
|
if (result !== object) {
|
|
102
|
+
// See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_DEFINE_PROPERTY_FAILED_SILENTLY.md
|
|
100
103
|
throw TypeError(
|
|
101
104
|
`Please report that the original defineProperty silently failed to set ${stringifyJson(
|
|
102
105
|
String(prop),
|
|
@@ -279,6 +282,7 @@ export const FERAL_EVAL = eval;
|
|
|
279
282
|
export const FERAL_FUNCTION = Function;
|
|
280
283
|
|
|
281
284
|
export const noEvalEvaluate = () => {
|
|
285
|
+
// See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_NO_EVAL.md
|
|
282
286
|
throw new TypeError(
|
|
283
287
|
'Cannot eval with evalTaming set to "noEval" (SES_NO_EVAL)',
|
|
284
288
|
);
|
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
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
* <ul>
|
|
42
42
|
* <li>true, in which case this property is simply repaired. The
|
|
43
43
|
* value associated with that property is not traversed. For
|
|
44
|
-
*
|
|
44
|
+
* example, {@code "Function.prototype.name"} leads to true,
|
|
45
45
|
* meaning that the {@code "name"} property of {@code
|
|
46
46
|
* "Function.prototype"} should be repaired (which is needed
|
|
47
47
|
* when inheriting from @code{Function} and setting the subclass's
|
|
@@ -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
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
import { an, bestEffortStringify } from './stringify-utils.js';
|
|
37
37
|
import './types.js';
|
|
38
38
|
import './internal-types.js';
|
|
39
|
+
import { makeNoteLogArgsArrayKit } from './note-log-args.js';
|
|
39
40
|
|
|
40
41
|
// For our internal debugging purposes, uncomment
|
|
41
42
|
// const internalDebugConsole = console;
|
|
@@ -58,7 +59,7 @@ freeze(quote);
|
|
|
58
59
|
// /////////////////////////////////////////////////////////////////////////////
|
|
59
60
|
|
|
60
61
|
/**
|
|
61
|
-
* @typedef {
|
|
62
|
+
* @typedef {object} HiddenDetails
|
|
62
63
|
*
|
|
63
64
|
* Captures the arguments passed to the `details` template string tag.
|
|
64
65
|
*
|
|
@@ -251,17 +252,7 @@ freeze(makeError);
|
|
|
251
252
|
|
|
252
253
|
// /////////////////////////////////////////////////////////////////////////////
|
|
253
254
|
|
|
254
|
-
|
|
255
|
-
* @type {WeakMap<Error, LogArgs[]>}
|
|
256
|
-
*
|
|
257
|
-
* Maps from an error to an array of log args, where each log args is
|
|
258
|
-
* remembered as an annotation on that error. This can be used, for example,
|
|
259
|
-
* to keep track of additional causes of the error. The elements of any
|
|
260
|
-
* log args may include errors which are associated with further annotations.
|
|
261
|
-
* An augmented console, like the causal console of `console.js`, could
|
|
262
|
-
* then retrieve the graph of such annotations.
|
|
263
|
-
*/
|
|
264
|
-
const hiddenNoteLogArgsArrays = new WeakMap();
|
|
255
|
+
const { addLogArgs, takeLogArgsArray } = makeNoteLogArgsArrayKit();
|
|
265
256
|
|
|
266
257
|
/**
|
|
267
258
|
* @type {WeakMap<Error, NoteCallback[]>}
|
|
@@ -295,12 +286,7 @@ const note = (error, detailsNote) => {
|
|
|
295
286
|
callback(error, logArgs);
|
|
296
287
|
}
|
|
297
288
|
} else {
|
|
298
|
-
|
|
299
|
-
if (logArgsArray !== undefined) {
|
|
300
|
-
arrayPush(logArgsArray, logArgs);
|
|
301
|
-
} else {
|
|
302
|
-
weakmapSet(hiddenNoteLogArgsArrays, error, [logArgs]);
|
|
303
|
-
}
|
|
289
|
+
addLogArgs(error, logArgs);
|
|
304
290
|
}
|
|
305
291
|
};
|
|
306
292
|
freeze(note);
|
|
@@ -339,8 +325,7 @@ const loggedErrorHandler = {
|
|
|
339
325
|
return result;
|
|
340
326
|
},
|
|
341
327
|
takeNoteLogArgsArray: (error, callback) => {
|
|
342
|
-
const result =
|
|
343
|
-
weakmapDelete(hiddenNoteLogArgsArrays, error);
|
|
328
|
+
const result = takeLogArgsArray(error);
|
|
344
329
|
if (callback !== undefined) {
|
|
345
330
|
const callbacks = weakmapGet(hiddenNoteCallbackArrays, error);
|
|
346
331
|
if (callbacks) {
|
|
@@ -362,9 +347,11 @@ export { loggedErrorHandler };
|
|
|
362
347
|
*/
|
|
363
348
|
const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
364
349
|
const details = unredacted ? unredactedDetails : redactedDetails;
|
|
350
|
+
const assertFailedDetails = details`Check failed`;
|
|
351
|
+
|
|
365
352
|
/** @type {AssertFail} */
|
|
366
353
|
const fail = (
|
|
367
|
-
optDetails =
|
|
354
|
+
optDetails = assertFailedDetails,
|
|
368
355
|
ErrorConstructor = globalThis.Error,
|
|
369
356
|
) => {
|
|
370
357
|
const reason = makeError(optDetails, ErrorConstructor);
|
|
@@ -384,31 +371,37 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
384
371
|
/** @type {BaseAssert} */
|
|
385
372
|
function baseAssert(
|
|
386
373
|
flag,
|
|
387
|
-
optDetails =
|
|
388
|
-
ErrorConstructor =
|
|
374
|
+
optDetails = undefined,
|
|
375
|
+
ErrorConstructor = undefined,
|
|
389
376
|
) {
|
|
390
|
-
|
|
391
|
-
throw fail(optDetails, ErrorConstructor);
|
|
392
|
-
}
|
|
377
|
+
flag || fail(optDetails, ErrorConstructor);
|
|
393
378
|
}
|
|
394
379
|
|
|
395
380
|
/** @type {AssertEqual} */
|
|
396
381
|
const equal = (
|
|
397
382
|
actual,
|
|
398
383
|
expected,
|
|
399
|
-
optDetails =
|
|
400
|
-
ErrorConstructor =
|
|
384
|
+
optDetails = undefined,
|
|
385
|
+
ErrorConstructor = undefined,
|
|
401
386
|
) => {
|
|
402
|
-
|
|
387
|
+
is(actual, expected) ||
|
|
388
|
+
fail(
|
|
389
|
+
optDetails || details`Expected ${actual} is same as ${expected}`,
|
|
390
|
+
ErrorConstructor || RangeError,
|
|
391
|
+
);
|
|
403
392
|
};
|
|
404
393
|
freeze(equal);
|
|
405
394
|
|
|
406
395
|
/** @type {AssertTypeof} */
|
|
407
396
|
const assertTypeof = (specimen, typename, optDetails) => {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
)
|
|
397
|
+
// This will safely fall through if typename is not a string,
|
|
398
|
+
// which is what we want.
|
|
399
|
+
// eslint-disable-next-line valid-typeof
|
|
400
|
+
if (typeof specimen === typename) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
typeof typename === 'string' || Fail`${quote(typename)} must be a string`;
|
|
404
|
+
|
|
412
405
|
if (optDetails === undefined) {
|
|
413
406
|
// Like
|
|
414
407
|
// ```js
|
|
@@ -418,12 +411,12 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
418
411
|
// so it doesn't get quoted.
|
|
419
412
|
optDetails = details(['', ` must be ${an(typename)}`], specimen);
|
|
420
413
|
}
|
|
421
|
-
|
|
414
|
+
fail(optDetails, TypeError);
|
|
422
415
|
};
|
|
423
416
|
freeze(assertTypeof);
|
|
424
417
|
|
|
425
418
|
/** @type {AssertString} */
|
|
426
|
-
const assertString = (specimen, optDetails) =>
|
|
419
|
+
const assertString = (specimen, optDetails = undefined) =>
|
|
427
420
|
assertTypeof(specimen, 'string', optDetails);
|
|
428
421
|
|
|
429
422
|
// Note that "assert === baseAssert"
|
|
@@ -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
|
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/* eslint-disable @endo/no-polymorphic-call */
|
|
3
|
+
/* eslint-disable no-restricted-globals */
|
|
4
|
+
|
|
5
|
+
import './internal-types.js';
|
|
6
|
+
|
|
7
|
+
const { freeze } = Object;
|
|
8
|
+
const { isSafeInteger } = Number;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @template Data
|
|
12
|
+
* @typedef {object} DoublyLinkedCell
|
|
13
|
+
* A cell of a doubly-linked ring, i.e., a doubly-linked circular list.
|
|
14
|
+
* DoublyLinkedCells are not frozen, and so should be closely encapsulated by
|
|
15
|
+
* any abstraction that uses them.
|
|
16
|
+
* @property {DoublyLinkedCell<Data>} next
|
|
17
|
+
* @property {DoublyLinkedCell<Data>} prev
|
|
18
|
+
* @property {Data} data
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Makes a new self-linked cell. There are two reasons to do so:
|
|
23
|
+
* * To make the head sigil of a new initially-empty doubly-linked ring.
|
|
24
|
+
* * To make a non-sigil cell to be `spliceAfter`ed.
|
|
25
|
+
*
|
|
26
|
+
* @template Data
|
|
27
|
+
* @param {Data} data
|
|
28
|
+
* @returns {DoublyLinkedCell<Data>}
|
|
29
|
+
*/
|
|
30
|
+
const makeSelfCell = data => {
|
|
31
|
+
/** @type {Partial<DoublyLinkedCell<Data>>} */
|
|
32
|
+
const incompleteCell = {
|
|
33
|
+
next: undefined,
|
|
34
|
+
prev: undefined,
|
|
35
|
+
data,
|
|
36
|
+
};
|
|
37
|
+
const selfCell = /** @type {DoublyLinkedCell<Data>} */ (incompleteCell);
|
|
38
|
+
selfCell.next = selfCell;
|
|
39
|
+
selfCell.prev = selfCell;
|
|
40
|
+
// Not frozen!
|
|
41
|
+
return selfCell;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Splices a self-linked non-sigil cell into a ring after `prev`.
|
|
46
|
+
* `prev` could be the head sigil, or it could be some other non-sigil
|
|
47
|
+
* cell within a ring.
|
|
48
|
+
*
|
|
49
|
+
* @template Data
|
|
50
|
+
* @param {DoublyLinkedCell<Data>} prev
|
|
51
|
+
* @param {DoublyLinkedCell<Data>} selfCell
|
|
52
|
+
*/
|
|
53
|
+
const spliceAfter = (prev, selfCell) => {
|
|
54
|
+
if (prev === selfCell) {
|
|
55
|
+
throw TypeError('Cannot splice a cell into itself');
|
|
56
|
+
}
|
|
57
|
+
if (selfCell.next !== selfCell || selfCell.prev !== selfCell) {
|
|
58
|
+
throw TypeError('Expected self-linked cell');
|
|
59
|
+
}
|
|
60
|
+
const cell = selfCell;
|
|
61
|
+
// rename variable cause it isn't self-linked after this point.
|
|
62
|
+
|
|
63
|
+
const next = prev.next;
|
|
64
|
+
cell.prev = prev;
|
|
65
|
+
cell.next = next;
|
|
66
|
+
prev.next = cell;
|
|
67
|
+
next.prev = cell;
|
|
68
|
+
// Not frozen!
|
|
69
|
+
return cell;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @template Data
|
|
74
|
+
* @param {DoublyLinkedCell<Data>} cell
|
|
75
|
+
* No-op if the cell is self-linked.
|
|
76
|
+
*/
|
|
77
|
+
const spliceOut = cell => {
|
|
78
|
+
const { prev, next } = cell;
|
|
79
|
+
prev.next = next;
|
|
80
|
+
next.prev = prev;
|
|
81
|
+
cell.prev = cell;
|
|
82
|
+
cell.next = cell;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The LRUCacheMap is used within the implementation of `assert` and so
|
|
87
|
+
* at a layer below SES or harden. Thus, we give it a `WeakMap`-like interface
|
|
88
|
+
* rather than a `WeakMapStore`-like interface. To work before `lockdown`,
|
|
89
|
+
* the implementation must use `freeze` manually, but still exhaustively.
|
|
90
|
+
*
|
|
91
|
+
* It implements the WeakMap interface, and holds its keys weakly. Cached
|
|
92
|
+
* values are only held while the key is held by the user and the key/value
|
|
93
|
+
* bookkeeping cell has not been pushed off the end of the cache by `budget`
|
|
94
|
+
* number of more recently referenced cells. If the key is dropped by the user,
|
|
95
|
+
* the value will no longer be held by the cache, but the bookkeeping cell
|
|
96
|
+
* itself will stay in memory.
|
|
97
|
+
*
|
|
98
|
+
* @template {{}} K
|
|
99
|
+
* @template {unknown} V
|
|
100
|
+
* @param {number} keysBudget
|
|
101
|
+
* @returns {WeakMap<K,V>}
|
|
102
|
+
*/
|
|
103
|
+
export const makeLRUCacheMap = keysBudget => {
|
|
104
|
+
if (!isSafeInteger(keysBudget) || keysBudget < 0) {
|
|
105
|
+
throw new TypeError(
|
|
106
|
+
'keysBudget must be a safe non-negative integer number',
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
/** @typedef {DoublyLinkedCell<WeakMap<K, V> | undefined>} LRUCacheCell */
|
|
110
|
+
/** @type {WeakMap<K, LRUCacheCell>} */
|
|
111
|
+
const keyToCell = new WeakMap();
|
|
112
|
+
let size = 0; // `size` must remain <= `keysBudget`
|
|
113
|
+
// As a sigil, `head` uniquely is not in the `keyToCell` map.
|
|
114
|
+
/** @type {LRUCacheCell} */
|
|
115
|
+
const head = makeSelfCell(undefined);
|
|
116
|
+
|
|
117
|
+
const touchCell = key => {
|
|
118
|
+
const cell = keyToCell.get(key);
|
|
119
|
+
if (cell === undefined || cell.data === undefined) {
|
|
120
|
+
// Either the key was GCed, or the cell was condemned.
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
// Becomes most recently used
|
|
124
|
+
spliceOut(cell);
|
|
125
|
+
spliceAfter(head, cell);
|
|
126
|
+
return cell;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {K} key
|
|
131
|
+
*/
|
|
132
|
+
const has = key => touchCell(key) !== undefined;
|
|
133
|
+
freeze(has);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @param {K} key
|
|
137
|
+
*/
|
|
138
|
+
// TODO Change to the following line, once our tools don't choke on `?.`.
|
|
139
|
+
// See https://github.com/endojs/endo/issues/1514
|
|
140
|
+
// const get = key => touchCell(key)?.data?.get(key);
|
|
141
|
+
const get = key => {
|
|
142
|
+
const cell = touchCell(key);
|
|
143
|
+
return cell && cell.data && cell.data.get(key);
|
|
144
|
+
};
|
|
145
|
+
freeze(get);
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {K} key
|
|
149
|
+
* @param {V} value
|
|
150
|
+
*/
|
|
151
|
+
const set = (key, value) => {
|
|
152
|
+
if (keysBudget < 1) {
|
|
153
|
+
// eslint-disable-next-line no-use-before-define
|
|
154
|
+
return lruCacheMap; // Implements WeakMap.set
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let cell = touchCell(key);
|
|
158
|
+
if (cell === undefined) {
|
|
159
|
+
cell = makeSelfCell(undefined);
|
|
160
|
+
spliceAfter(head, cell); // start most recently used
|
|
161
|
+
}
|
|
162
|
+
if (!cell.data) {
|
|
163
|
+
// Either a fresh cell or a reused condemned cell.
|
|
164
|
+
size += 1;
|
|
165
|
+
// Add its data.
|
|
166
|
+
cell.data = new WeakMap();
|
|
167
|
+
// Advertise the cell for this key.
|
|
168
|
+
keyToCell.set(key, cell);
|
|
169
|
+
while (size > keysBudget) {
|
|
170
|
+
const condemned = head.prev;
|
|
171
|
+
spliceOut(condemned); // Drop least recently used
|
|
172
|
+
condemned.data = undefined;
|
|
173
|
+
size -= 1;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Update the data.
|
|
178
|
+
cell.data.set(key, value);
|
|
179
|
+
|
|
180
|
+
// eslint-disable-next-line no-use-before-define
|
|
181
|
+
return lruCacheMap; // Implements WeakMap.set
|
|
182
|
+
};
|
|
183
|
+
freeze(set);
|
|
184
|
+
|
|
185
|
+
// "delete" is a keyword.
|
|
186
|
+
/**
|
|
187
|
+
* @param {K} key
|
|
188
|
+
*/
|
|
189
|
+
const deleteIt = key => {
|
|
190
|
+
const cell = keyToCell.get(key);
|
|
191
|
+
if (cell === undefined) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
spliceOut(cell);
|
|
195
|
+
keyToCell.delete(key);
|
|
196
|
+
if (cell.data === undefined) {
|
|
197
|
+
// Already condemned.
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
cell.data = undefined;
|
|
202
|
+
size -= 1;
|
|
203
|
+
return true;
|
|
204
|
+
};
|
|
205
|
+
freeze(deleteIt);
|
|
206
|
+
|
|
207
|
+
const lruCacheMap = freeze({
|
|
208
|
+
has,
|
|
209
|
+
get,
|
|
210
|
+
set,
|
|
211
|
+
delete: deleteIt,
|
|
212
|
+
[Symbol.toStringTag]: 'LRUCacheMap',
|
|
213
|
+
});
|
|
214
|
+
return lruCacheMap;
|
|
215
|
+
};
|
|
216
|
+
freeze(makeLRUCacheMap);
|
|
217
|
+
|
|
218
|
+
const defaultLoggedErrorsBudget = 1000;
|
|
219
|
+
const defaultArgsPerErrorBudget = 100;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @param {number} [errorsBudget]
|
|
223
|
+
* @param {number} [argsPerErrorBudget]
|
|
224
|
+
*/
|
|
225
|
+
export const makeNoteLogArgsArrayKit = (
|
|
226
|
+
errorsBudget = defaultLoggedErrorsBudget,
|
|
227
|
+
argsPerErrorBudget = defaultArgsPerErrorBudget,
|
|
228
|
+
) => {
|
|
229
|
+
if (!isSafeInteger(argsPerErrorBudget) || argsPerErrorBudget < 1) {
|
|
230
|
+
throw new TypeError(
|
|
231
|
+
'argsPerErrorBudget must be a safe positive integer number',
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @type {WeakMap<Error, LogArgs[]>}
|
|
237
|
+
*
|
|
238
|
+
* Maps from an error to an array of log args, where each log args is
|
|
239
|
+
* remembered as an annotation on that error. This can be used, for example,
|
|
240
|
+
* to keep track of additional causes of the error. The elements of any
|
|
241
|
+
* log args may include errors which are associated with further annotations.
|
|
242
|
+
* An augmented console, like the causal console of `console.js`, could
|
|
243
|
+
* then retrieve the graph of such annotations.
|
|
244
|
+
*/
|
|
245
|
+
const noteLogArgsArrayMap = makeLRUCacheMap(errorsBudget);
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* @param {Error} error
|
|
249
|
+
* @param {LogArgs} logArgs
|
|
250
|
+
*/
|
|
251
|
+
const addLogArgs = (error, logArgs) => {
|
|
252
|
+
const logArgsArray = noteLogArgsArrayMap.get(error);
|
|
253
|
+
if (logArgsArray !== undefined) {
|
|
254
|
+
if (logArgsArray.length >= argsPerErrorBudget) {
|
|
255
|
+
logArgsArray.shift();
|
|
256
|
+
}
|
|
257
|
+
logArgsArray.push(logArgs);
|
|
258
|
+
} else {
|
|
259
|
+
noteLogArgsArrayMap.set(error, [logArgs]);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
freeze(addLogArgs);
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @param {Error} error
|
|
266
|
+
* @returns {LogArgs[] | undefined}
|
|
267
|
+
*/
|
|
268
|
+
const takeLogArgsArray = error => {
|
|
269
|
+
const result = noteLogArgsArrayMap.get(error);
|
|
270
|
+
noteLogArgsArrayMap.delete(error);
|
|
271
|
+
return result;
|
|
272
|
+
};
|
|
273
|
+
freeze(takeLogArgsArray);
|
|
274
|
+
|
|
275
|
+
return freeze({
|
|
276
|
+
addLogArgs,
|
|
277
|
+
takeLogArgsArray,
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
freeze(makeNoteLogArgsArrayKit);
|