ses 1.0.1 → 1.2.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 +25 -1
- package/README.md +1 -1
- package/dist/lockdown.cjs +962 -453
- package/dist/lockdown.mjs +962 -453
- package/dist/lockdown.umd.js +962 -453
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +962 -453
- package/dist/ses.mjs +962 -453
- package/dist/ses.umd.js +962 -453
- package/dist/ses.umd.min.js +1 -1
- package/package.json +15 -11
- package/src/compartment.js +13 -4
- package/src/enable-property-overrides.js +40 -36
- package/src/enablements.js +15 -0
- package/src/error/README.md +1 -1
- package/src/error/fatal-assert.js +1 -1
- package/src/error/note-log-args.js +1 -205
- package/src/error/tame-console.js +72 -51
- package/src/lockdown.js +43 -12
- package/src/make-lru-cachemap.js +221 -0
- package/src/module-instance.js +10 -10
- package/src/module-proxy.js +42 -14
- package/src/permits-intrinsics.js +29 -15
- package/src/permits.js +67 -2
- package/src/tame-domains.js +4 -6
- package/src/tame-faux-data-properties.js +210 -0
- package/types.d.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"homepage": "https://github.com/Agoric/SES-shim/tree/master/packages/ses#readme",
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
26
|
-
"url": "git+https://github.com/endojs/endo.git"
|
|
26
|
+
"url": "git+https://github.com/endojs/endo.git",
|
|
27
|
+
"directory": "packages/ses"
|
|
27
28
|
},
|
|
28
29
|
"bugs": {
|
|
29
30
|
"url": "https://github.com/endojs/endo/issues"
|
|
@@ -45,6 +46,9 @@
|
|
|
45
46
|
"require": "./dist/ses.cjs"
|
|
46
47
|
},
|
|
47
48
|
"./tools.js": "./tools.js",
|
|
49
|
+
"./assert-shim.js": "./assert-shim.js",
|
|
50
|
+
"./lockdown-shim.js": "./lockdown-shim.js",
|
|
51
|
+
"./compartment-shim.js": "./compartment-shim.js",
|
|
48
52
|
"./package.json": "./package.json"
|
|
49
53
|
},
|
|
50
54
|
"scripts": {
|
|
@@ -52,9 +56,9 @@
|
|
|
52
56
|
"clean": "rm -rf dist",
|
|
53
57
|
"cover": "c8 ava",
|
|
54
58
|
"demo": "python3 -m http.server",
|
|
55
|
-
"lint": "yarn lint:types && yarn lint:
|
|
59
|
+
"lint": "yarn lint:types && yarn lint:eslint",
|
|
56
60
|
"lint-fix": "eslint --fix .",
|
|
57
|
-
"lint:
|
|
61
|
+
"lint:eslint": "eslint .",
|
|
58
62
|
"lint:types": "tsc",
|
|
59
63
|
"prepare": "yarn run clean && yarn build",
|
|
60
64
|
"qt": "ava",
|
|
@@ -62,12 +66,12 @@
|
|
|
62
66
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
63
67
|
},
|
|
64
68
|
"dependencies": {
|
|
65
|
-
"@endo/env-options": "^1.
|
|
69
|
+
"@endo/env-options": "^1.1.1"
|
|
66
70
|
},
|
|
67
71
|
"devDependencies": {
|
|
68
|
-
"@endo/compartment-mapper": "^1.
|
|
69
|
-
"@endo/static-module-record": "^1.0.
|
|
70
|
-
"@endo/test262-runner": "^0.1.
|
|
72
|
+
"@endo/compartment-mapper": "^1.1.1",
|
|
73
|
+
"@endo/static-module-record": "^1.0.3",
|
|
74
|
+
"@endo/test262-runner": "^0.1.33",
|
|
71
75
|
"ava": "^5.3.0",
|
|
72
76
|
"babel-eslint": "^10.0.3",
|
|
73
77
|
"c8": "^7.14.0",
|
|
@@ -81,7 +85,7 @@
|
|
|
81
85
|
"sinon": "^15.1.0",
|
|
82
86
|
"terser": "^5.16.6",
|
|
83
87
|
"tsd": "^0.28.1",
|
|
84
|
-
"typescript": "~5.
|
|
88
|
+
"typescript": "~5.3.3"
|
|
85
89
|
},
|
|
86
90
|
"files": [
|
|
87
91
|
"LICENSE*",
|
|
@@ -182,7 +186,7 @@
|
|
|
182
186
|
"timeout": "2m"
|
|
183
187
|
},
|
|
184
188
|
"typeCoverage": {
|
|
185
|
-
"atLeast": 81.
|
|
189
|
+
"atLeast": 81.17
|
|
186
190
|
},
|
|
187
|
-
"gitHead": "
|
|
191
|
+
"gitHead": "f731c5c12f8d185dbf2daf53ec6a57e5ac56e4e9"
|
|
188
192
|
}
|
package/src/compartment.js
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
defineProperties,
|
|
12
12
|
entries,
|
|
13
13
|
promiseThen,
|
|
14
|
+
toStringTagSymbol,
|
|
14
15
|
weakmapGet,
|
|
15
16
|
weakmapSet,
|
|
16
17
|
} from './commons.js';
|
|
@@ -105,10 +106,6 @@ export const CompartmentPrototype = {
|
|
|
105
106
|
return compartmentEvaluate(compartmentFields, source, options);
|
|
106
107
|
},
|
|
107
108
|
|
|
108
|
-
toString() {
|
|
109
|
-
return '[object Compartment]';
|
|
110
|
-
},
|
|
111
|
-
|
|
112
109
|
module(specifier) {
|
|
113
110
|
if (typeof specifier !== 'string') {
|
|
114
111
|
throw TypeError('first argument of module() must be a string');
|
|
@@ -168,6 +165,18 @@ export const CompartmentPrototype = {
|
|
|
168
165
|
},
|
|
169
166
|
};
|
|
170
167
|
|
|
168
|
+
// This causes `String(new Compartment())` to evaluate to `[object Compartment]`.
|
|
169
|
+
// The descriptor follows the conventions of other globals with @@toStringTag
|
|
170
|
+
// properties, e.g. Math.
|
|
171
|
+
defineProperties(CompartmentPrototype, {
|
|
172
|
+
[toStringTagSymbol]: {
|
|
173
|
+
value: 'Compartment',
|
|
174
|
+
writable: false,
|
|
175
|
+
enumerable: false,
|
|
176
|
+
configurable: true,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
171
180
|
defineProperties(InertCompartment, {
|
|
172
181
|
prototype: { value: CompartmentPrototype },
|
|
173
182
|
});
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
defineProperty,
|
|
12
12
|
getOwnPropertyDescriptor,
|
|
13
13
|
getOwnPropertyDescriptors,
|
|
14
|
-
getOwnPropertyNames,
|
|
15
14
|
isObject,
|
|
16
15
|
objectHasOwnProperty,
|
|
17
16
|
ownKeys,
|
|
@@ -88,9 +87,43 @@ export default function enablePropertyOverrides(
|
|
|
88
87
|
if ('value' in desc && desc.configurable) {
|
|
89
88
|
const { value } = desc;
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
const isDebug = setHas(debugProperties, prop);
|
|
91
|
+
|
|
92
|
+
// We use concise method syntax to be `this` sensitive, but still
|
|
93
|
+
// omit a prototype property or [[Construct]] behavior.
|
|
94
|
+
// @ts-expect-error We know there is an accessor descriptor there
|
|
95
|
+
const { get: getter, set: setter } = getOwnPropertyDescriptor(
|
|
96
|
+
{
|
|
97
|
+
get [prop]() {
|
|
98
|
+
return value;
|
|
99
|
+
},
|
|
100
|
+
set [prop](newValue) {
|
|
101
|
+
if (obj === this) {
|
|
102
|
+
throw TypeError(
|
|
103
|
+
`Cannot assign to read only property '${String(
|
|
104
|
+
prop,
|
|
105
|
+
)}' of '${path}'`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (objectHasOwnProperty(this, prop)) {
|
|
109
|
+
this[prop] = newValue;
|
|
110
|
+
} else {
|
|
111
|
+
if (isDebug) {
|
|
112
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
113
|
+
console.error(TypeError(`Override property ${prop}`));
|
|
114
|
+
}
|
|
115
|
+
defineProperty(this, prop, {
|
|
116
|
+
value: newValue,
|
|
117
|
+
writable: true,
|
|
118
|
+
enumerable: true,
|
|
119
|
+
configurable: true,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
prop,
|
|
125
|
+
);
|
|
126
|
+
|
|
94
127
|
defineProperty(getter, 'originalValue', {
|
|
95
128
|
value,
|
|
96
129
|
writable: false,
|
|
@@ -98,32 +131,6 @@ export default function enablePropertyOverrides(
|
|
|
98
131
|
configurable: false,
|
|
99
132
|
});
|
|
100
133
|
|
|
101
|
-
const isDebug = setHas(debugProperties, prop);
|
|
102
|
-
|
|
103
|
-
function setter(newValue) {
|
|
104
|
-
if (obj === this) {
|
|
105
|
-
throw TypeError(
|
|
106
|
-
`Cannot assign to read only property '${String(
|
|
107
|
-
prop,
|
|
108
|
-
)}' of '${path}'`,
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
if (objectHasOwnProperty(this, prop)) {
|
|
112
|
-
this[prop] = newValue;
|
|
113
|
-
} else {
|
|
114
|
-
if (isDebug) {
|
|
115
|
-
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
116
|
-
console.error(TypeError(`Override property ${prop}`));
|
|
117
|
-
}
|
|
118
|
-
defineProperty(this, prop, {
|
|
119
|
-
value: newValue,
|
|
120
|
-
writable: true,
|
|
121
|
-
enumerable: true,
|
|
122
|
-
configurable: true,
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
134
|
defineProperty(obj, prop, {
|
|
128
135
|
get: getter,
|
|
129
136
|
set: setter,
|
|
@@ -148,12 +155,11 @@ export default function enablePropertyOverrides(
|
|
|
148
155
|
}
|
|
149
156
|
// TypeScript does not allow symbols to be used as indexes because it
|
|
150
157
|
// cannot recokon types of symbolized properties.
|
|
151
|
-
// @ts-ignore
|
|
152
158
|
arrayForEach(ownKeys(descs), prop => enable(path, obj, prop, descs[prop]));
|
|
153
159
|
}
|
|
154
160
|
|
|
155
161
|
function enableProperties(path, obj, plan) {
|
|
156
|
-
for (const prop of
|
|
162
|
+
for (const prop of ownKeys(plan)) {
|
|
157
163
|
const desc = getOwnPropertyDescriptor(obj, prop);
|
|
158
164
|
if (!desc || desc.get || desc.set) {
|
|
159
165
|
// No not a value property, nothing to do.
|
|
@@ -161,10 +167,8 @@ export default function enablePropertyOverrides(
|
|
|
161
167
|
continue;
|
|
162
168
|
}
|
|
163
169
|
|
|
164
|
-
//
|
|
165
|
-
//
|
|
166
|
-
// with `String(..)` anyway just as good hygiene, since these paths are just
|
|
167
|
-
// for diagnostic purposes.
|
|
170
|
+
// In case `prop` is a symbol, we first coerce it with `String`,
|
|
171
|
+
// purely for diagnostic purposes.
|
|
168
172
|
const subPath = `${path}.${String(prop)}`;
|
|
169
173
|
const subPlan = plan[prop];
|
|
170
174
|
|
package/src/enablements.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { toStringTagSymbol, iteratorSymbol } from './commons.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @file Exports {@code enablements}, a recursively defined
|
|
3
5
|
* JSON record defining the optimum set of intrinsics properties
|
|
@@ -74,6 +76,13 @@ export const minEnablements = {
|
|
|
74
76
|
'%ErrorPrototype%': {
|
|
75
77
|
name: true, // set by "precond", "ava", "node-fetch"
|
|
76
78
|
},
|
|
79
|
+
'%IteratorPrototype%': {
|
|
80
|
+
toString: true,
|
|
81
|
+
// https://github.com/tc39/proposal-iterator-helpers
|
|
82
|
+
constructor: true,
|
|
83
|
+
// https://github.com/tc39/proposal-iterator-helpers
|
|
84
|
+
[toStringTagSymbol]: true,
|
|
85
|
+
},
|
|
77
86
|
};
|
|
78
87
|
|
|
79
88
|
/**
|
|
@@ -88,6 +97,8 @@ export const moderateEnablements = {
|
|
|
88
97
|
'%ArrayPrototype%': {
|
|
89
98
|
toString: true,
|
|
90
99
|
push: true, // set by "Google Analytics"
|
|
100
|
+
concat: true, // set by mobx generated code (old TS compiler?)
|
|
101
|
+
[iteratorSymbol]: true, // set by mobx generated code (old TS compiler?)
|
|
91
102
|
},
|
|
92
103
|
|
|
93
104
|
// Function.prototype has no 'prototype' property to enable.
|
|
@@ -152,6 +163,10 @@ export const moderateEnablements = {
|
|
|
152
163
|
|
|
153
164
|
'%IteratorPrototype%': {
|
|
154
165
|
toString: true,
|
|
166
|
+
// https://github.com/tc39/proposal-iterator-helpers
|
|
167
|
+
constructor: true,
|
|
168
|
+
// https://github.com/tc39/proposal-iterator-helpers
|
|
169
|
+
[toStringTagSymbol]: true,
|
|
155
170
|
},
|
|
156
171
|
};
|
|
157
172
|
|
package/src/error/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Summary
|
|
|
5
5
|
* To that end, SES introduces an `assert` global with functions that add to errors annotations that will be hidden from callers. SES also tames the `Error` constructor to hide the `stack` to parent callers when possible (currently: v8, SpiderMonkey, XS).
|
|
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
|
-
* SES hides annotations and stack traces by default. To reveal them,
|
|
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
|
* In the scope of the Agoric software ecosystem, this architecture will allow us to eventually introduce a more powerful distributed `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
10
|
|
|
11
11
|
|
|
@@ -10,7 +10,7 @@ let abandon;
|
|
|
10
10
|
// below). Currently it only checks for the `process.abort` or `process.exit`
|
|
11
11
|
// found on Node. It should also sniff for a vat terminating function expected
|
|
12
12
|
// to be found within the start compartment of SwingSet vats. What else?
|
|
13
|
-
if (typeof process === 'object') {
|
|
13
|
+
if (typeof process === 'object' && process) {
|
|
14
14
|
abandon = process.abort || process.exit;
|
|
15
15
|
}
|
|
16
16
|
let raise;
|
|
@@ -2,216 +2,12 @@
|
|
|
2
2
|
/* eslint-disable @endo/no-polymorphic-call */
|
|
3
3
|
/* eslint-disable no-restricted-globals */
|
|
4
4
|
|
|
5
|
+
import { makeLRUCacheMap } from '../make-lru-cachemap.js';
|
|
5
6
|
import './internal-types.js';
|
|
6
7
|
|
|
7
8
|
const { freeze } = Object;
|
|
8
9
|
const { isSafeInteger } = Number;
|
|
9
10
|
|
|
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 TypeError('keysBudget must be a safe non-negative integer number');
|
|
106
|
-
}
|
|
107
|
-
/** @typedef {DoublyLinkedCell<WeakMap<K, V> | undefined>} LRUCacheCell */
|
|
108
|
-
/** @type {WeakMap<K, LRUCacheCell>} */
|
|
109
|
-
const keyToCell = new WeakMap();
|
|
110
|
-
let size = 0; // `size` must remain <= `keysBudget`
|
|
111
|
-
// As a sigil, `head` uniquely is not in the `keyToCell` map.
|
|
112
|
-
/** @type {LRUCacheCell} */
|
|
113
|
-
const head = makeSelfCell(undefined);
|
|
114
|
-
|
|
115
|
-
const touchCell = key => {
|
|
116
|
-
const cell = keyToCell.get(key);
|
|
117
|
-
if (cell === undefined || cell.data === undefined) {
|
|
118
|
-
// Either the key was GCed, or the cell was condemned.
|
|
119
|
-
return undefined;
|
|
120
|
-
}
|
|
121
|
-
// Becomes most recently used
|
|
122
|
-
spliceOut(cell);
|
|
123
|
-
spliceAfter(head, cell);
|
|
124
|
-
return cell;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* @param {K} key
|
|
129
|
-
*/
|
|
130
|
-
const has = key => touchCell(key) !== undefined;
|
|
131
|
-
freeze(has);
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* @param {K} key
|
|
135
|
-
*/
|
|
136
|
-
// UNTIL https://github.com/endojs/endo/issues/1514
|
|
137
|
-
// Prefer: const get = key => touchCell(key)?.data?.get(key);
|
|
138
|
-
const get = key => {
|
|
139
|
-
const cell = touchCell(key);
|
|
140
|
-
return cell && cell.data && cell.data.get(key);
|
|
141
|
-
};
|
|
142
|
-
freeze(get);
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* @param {K} key
|
|
146
|
-
* @param {V} value
|
|
147
|
-
*/
|
|
148
|
-
const set = (key, value) => {
|
|
149
|
-
if (keysBudget < 1) {
|
|
150
|
-
// eslint-disable-next-line no-use-before-define
|
|
151
|
-
return lruCacheMap; // Implements WeakMap.set
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
let cell = touchCell(key);
|
|
155
|
-
if (cell === undefined) {
|
|
156
|
-
cell = makeSelfCell(undefined);
|
|
157
|
-
spliceAfter(head, cell); // start most recently used
|
|
158
|
-
}
|
|
159
|
-
if (!cell.data) {
|
|
160
|
-
// Either a fresh cell or a reused condemned cell.
|
|
161
|
-
size += 1;
|
|
162
|
-
// Add its data.
|
|
163
|
-
cell.data = new WeakMap();
|
|
164
|
-
// Advertise the cell for this key.
|
|
165
|
-
keyToCell.set(key, cell);
|
|
166
|
-
while (size > keysBudget) {
|
|
167
|
-
const condemned = head.prev;
|
|
168
|
-
spliceOut(condemned); // Drop least recently used
|
|
169
|
-
condemned.data = undefined;
|
|
170
|
-
size -= 1;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Update the data.
|
|
175
|
-
cell.data.set(key, value);
|
|
176
|
-
|
|
177
|
-
// eslint-disable-next-line no-use-before-define
|
|
178
|
-
return lruCacheMap; // Implements WeakMap.set
|
|
179
|
-
};
|
|
180
|
-
freeze(set);
|
|
181
|
-
|
|
182
|
-
// "delete" is a keyword.
|
|
183
|
-
/**
|
|
184
|
-
* @param {K} key
|
|
185
|
-
*/
|
|
186
|
-
const deleteIt = key => {
|
|
187
|
-
const cell = keyToCell.get(key);
|
|
188
|
-
if (cell === undefined) {
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
spliceOut(cell);
|
|
192
|
-
keyToCell.delete(key);
|
|
193
|
-
if (cell.data === undefined) {
|
|
194
|
-
// Already condemned.
|
|
195
|
-
return false;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
cell.data = undefined;
|
|
199
|
-
size -= 1;
|
|
200
|
-
return true;
|
|
201
|
-
};
|
|
202
|
-
freeze(deleteIt);
|
|
203
|
-
|
|
204
|
-
const lruCacheMap = freeze({
|
|
205
|
-
has,
|
|
206
|
-
get,
|
|
207
|
-
set,
|
|
208
|
-
delete: deleteIt,
|
|
209
|
-
[Symbol.toStringTag]: 'LRUCacheMap',
|
|
210
|
-
});
|
|
211
|
-
return lruCacheMap;
|
|
212
|
-
};
|
|
213
|
-
freeze(makeLRUCacheMap);
|
|
214
|
-
|
|
215
11
|
const defaultLoggedErrorsBudget = 1000;
|
|
216
12
|
const defaultArgsPerErrorBudget = 100;
|
|
217
13
|
|