ses 0.15.22 → 0.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "0.15.22",
3
+ "version": "0.16.0",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -56,12 +56,12 @@
56
56
  "prepublish": "yarn run clean && yarn build",
57
57
  "qt": "ava",
58
58
  "test": "tsd && ava",
59
- "test:platform-compatability": "node test/package/test.cjs"
59
+ "test:platform-compatibility": "node test/package/test.cjs"
60
60
  },
61
61
  "devDependencies": {
62
- "@endo/compartment-mapper": "^0.7.12",
62
+ "@endo/compartment-mapper": "^0.7.14",
63
63
  "@endo/eslint-config": "^0.5.1",
64
- "@endo/static-module-record": "^0.7.11",
64
+ "@endo/static-module-record": "^0.7.13",
65
65
  "@endo/test262-runner": "^0.1.28",
66
66
  "ava": "^3.12.1",
67
67
  "babel-eslint": "^10.0.3",
@@ -179,5 +179,5 @@
179
179
  ],
180
180
  "timeout": "2m"
181
181
  },
182
- "gitHead": "44b17d92334592b2da54d91dba73f28db513412a"
182
+ "gitHead": "8da6dc1002417c0f18cd43b351f8f62d7010260c"
183
183
  }
package/src/commons.js CHANGED
@@ -66,6 +66,7 @@ export const {
66
66
  preventExtensions,
67
67
  setPrototypeOf,
68
68
  values,
69
+ fromEntries,
69
70
  } = Object;
70
71
 
71
72
  export const {
@@ -81,22 +82,6 @@ export const { isInteger } = Number;
81
82
 
82
83
  export const { stringify: stringifyJson } = JSON;
83
84
 
84
- // At time of this writing, we still support Node 10 which doesn't have
85
- // `Object.fromEntries`. If it is absent, this should be an adequate
86
- // replacement.
87
- // By the terminology of https://ponyfoo.com/articles/polyfills-or-ponyfills
88
- // it is a ponyfill rather than a polyfill or shim because we do not
89
- // install it on `Object`.
90
- const objectFromEntries = entryPairs => {
91
- const result = {};
92
- for (const [prop, val] of entryPairs) {
93
- result[prop] = val;
94
- }
95
- return result;
96
- };
97
-
98
- export const fromEntries = Object.fromEntries || objectFromEntries;
99
-
100
85
  // Needed only for the Safari bug workaround below
101
86
  const { defineProperty: originalDefineProperty } = Object;
102
87
 
@@ -145,24 +130,19 @@ export const { prototype: promisePrototype } = Promise;
145
130
 
146
131
  export const typedArrayPrototype = getPrototypeOf(Uint8Array.prototype);
147
132
 
133
+ const { bind } = functionPrototype;
148
134
  /**
149
135
  * uncurryThis()
150
- * This form of uncurry uses Reflect.apply()
151
- *
152
- * The original uncurry uses:
153
- * const bind = Function.prototype.bind;
154
- * const uncurryThis = bind.bind(bind.call);
136
+ * Equivalent of: fn => (thisArg, ...args) => apply(fn, thisArg, args)
155
137
  *
156
138
  * See those reference for a complete explanation:
157
139
  * http://wiki.ecmascript.org/doku.php?id=conventions:safe_meta_programming
158
140
  * which only lives at
159
141
  * http://web.archive.org/web/20160805225710/http://wiki.ecmascript.org/doku.php?id=conventions:safe_meta_programming
160
142
  *
161
- * @template {Function} F
162
- * @param {F} fn
163
- * returns {(thisArg: ThisParameterType<F>, ...args: Parameters<F>) => ReturnType<F>}
143
+ * @type {<F extends (this: any, ...args: any[]) => any>(fn: F) => ((thisArg: ThisParameterType<F>, ...args: Parameters<F>) => ReturnType<F>)}
164
144
  */
165
- export const uncurryThis = fn => (thisArg, ...args) => apply(fn, thisArg, args);
145
+ export const uncurryThis = bind.bind(bind.call); // eslint-disable-line @endo/no-polymorphic-call
166
146
 
167
147
  export const objectHasOwnProperty = uncurryThis(objectPrototype.hasOwnProperty);
168
148
  //
@@ -170,8 +150,10 @@ export const arrayFilter = uncurryThis(arrayPrototype.filter);
170
150
  export const arrayForEach = uncurryThis(arrayPrototype.forEach);
171
151
  export const arrayIncludes = uncurryThis(arrayPrototype.includes);
172
152
  export const arrayJoin = uncurryThis(arrayPrototype.join);
153
+ /** @type {<T, U>(thisArg: readonly T[], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any) => U[]} */
173
154
  export const arrayMap = uncurryThis(arrayPrototype.map);
174
155
  export const arrayPop = uncurryThis(arrayPrototype.pop);
156
+ /** @type {<T>(thisArg: T[], ...items: T[]) => number} */
175
157
  export const arrayPush = uncurryThis(arrayPrototype.push);
176
158
  export const arraySlice = uncurryThis(arrayPrototype.slice);
177
159
  export const arraySome = uncurryThis(arrayPrototype.some);
@@ -199,9 +181,16 @@ export const stringEndsWith = uncurryThis(stringPrototype.endsWith);
199
181
  export const stringIncludes = uncurryThis(stringPrototype.includes);
200
182
  export const stringIndexOf = uncurryThis(stringPrototype.indexOf);
201
183
  export const stringMatch = uncurryThis(stringPrototype.match);
184
+ /**
185
+ * @type {
186
+ * ((thisArg: string, searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string) => string) |
187
+ * ((thisArg: string, searchValue: { [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string; }, replacer: (substring: string, ...args: any[]) => string) => string)
188
+ * }
189
+ */
202
190
  export const stringReplace = uncurryThis(stringPrototype.replace);
203
191
  export const stringSearch = uncurryThis(stringPrototype.search);
204
192
  export const stringSlice = uncurryThis(stringPrototype.slice);
193
+ /** @type {(thisArg: string, splitter: string | RegExp | { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number) => string[]} */
205
194
  export const stringSplit = uncurryThis(stringPrototype.split);
206
195
  export const stringStartsWith = uncurryThis(stringPrototype.startsWith);
207
196
  export const iterateString = uncurryThis(stringPrototype[iteratorSymbol]);
@@ -221,6 +210,7 @@ export const functionToString = uncurryThis(functionPrototype.toString);
221
210
  const { all } = Promise;
222
211
  export const promiseAll = promises => apply(all, Promise, [promises]);
223
212
  export const promiseCatch = uncurryThis(promisePrototype.catch);
213
+ /** @type {<T, TResult1 = T, TResult2 = never>(thisArg: T, onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null) => Promise<TResult1 | TResult2>} */
224
214
  export const promiseThen = uncurryThis(promisePrototype.then);
225
215
  //
226
216
  export const finalizationRegistryRegister =
@@ -154,7 +154,10 @@ export const CompartmentPrototype = {
154
154
  () => {
155
155
  // The namespace box is a contentious design and likely to be a breaking
156
156
  // change in an appropriately numbered future version.
157
- const namespace = compartmentImportNow(this, specifier);
157
+ const namespace = compartmentImportNow(
158
+ /** @type {Compartment} */ (this),
159
+ specifier,
160
+ );
158
161
  return { namespace };
159
162
  },
160
163
  );
@@ -177,7 +180,7 @@ export const CompartmentPrototype = {
177
180
 
178
181
  assertModuleHooks(this);
179
182
 
180
- return compartmentImportNow(this, specifier);
183
+ return compartmentImportNow(/** @type {Compartment} */ (this), specifier);
181
184
  },
182
185
  };
183
186
 
@@ -165,22 +165,27 @@ export const severeEnablements = {
165
165
  ...moderateEnablements,
166
166
 
167
167
  /**
168
- * Rollup(as used at least by vega) and webpack
168
+ * Rollup (as used at least by vega) and webpack
169
169
  * (as used at least by regenerator) both turn exports into assignments
170
170
  * to a big `exports` object that inherits directly from
171
- * `Object.prototype`.Some of the exported names we've seen include
171
+ * `Object.prototype`. Some of the exported names we've seen include
172
172
  * `hasOwnProperty`, `constructor`, and `toString`. But the strategy used
173
- * by rollup and webpack means potentionally turns any exported name
174
- * into an assignment rejected by the override mistake.That's why
175
- * we take the extreme step of enabling everything on`Object.prototype`.
173
+ * by rollup and webpack potentionally turns any exported name
174
+ * into an assignment rejected by the override mistake. That's why
175
+ * the `severe` enablements takes the extreme step of enabling
176
+ * everything on `Object.prototype`.
176
177
  *
177
178
  * In addition, code doing inheritance manually will often override
178
179
  * the `constructor` property on the new prototype by assignment. We've
179
180
  * seen this several times.
180
181
  *
181
182
  * The cost of enabling all these is that they create a miserable debugging
182
- * experience. https://github.com/Agoric/agoric-sdk/issues/2324 explains
183
- * how it confused the Node console.
183
+ * experience specifically on Node.
184
+ * https://github.com/Agoric/agoric-sdk/issues/2324
185
+ * explains how it confused the Node console.
186
+ *
187
+ * (TODO Reexamine the vscode situation. I think it may have improved
188
+ * since the following paragraph was written.)
184
189
  *
185
190
  * The vscode debugger's object inspector shows the own data properties of
186
191
  * an object, which is typically what you want, but also shows both getter
@@ -197,8 +202,8 @@ export const severeEnablements = {
197
202
  * The widely used Buffer defined at https://github.com/feross/buffer
198
203
  * on initialization, manually creates the equivalent of a subclass of
199
204
  * `TypedArray`, which it then initializes by assignment. These assignments
200
- * include enough of the `TypeArray` methods that here, we just enable
201
- * them all.
205
+ * include enough of the `TypeArray` methods that here, the `severe`
206
+ * enablements just enable them all.
202
207
  */
203
208
  '%TypedArrayPrototype%': '*',
204
209
  };
@@ -322,9 +322,9 @@ const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
322
322
  const levelMethod = (...logArgs) => {
323
323
  const subErrors = [];
324
324
  const argTags = extractErrorArgs(logArgs, subErrors);
325
- // @ts-ignore
326
325
  // eslint-disable-next-line @endo/no-polymorphic-call
327
326
  baseConsole[level](...argTags);
327
+ // @ts-expect-error ConsoleProp vs LogSeverity mismatch
328
328
  logSubErrors(level, subErrors);
329
329
  };
330
330
  defineProperty(levelMethod, 'name', { value: level });
@@ -19,7 +19,7 @@ import {
19
19
  /**
20
20
  * Joins English terms with commas and an optional conjunction.
21
21
  *
22
- * @param {string[]} terms
22
+ * @param {(string | StringablePayload)[]} terms
23
23
  * @param {"and" | "or"} conjunction
24
24
  */
25
25
  export const enJoin = (terms, conjunction) => {
@@ -6,6 +6,7 @@ import {
6
6
  defineProperties,
7
7
  setPrototypeOf,
8
8
  getOwnPropertyDescriptor,
9
+ defineProperty,
9
10
  } from '../commons.js';
10
11
  import { NativeErrors } from '../whitelist.js';
11
12
  import { tameV8ErrorConstructor } from './tame-v8-error-constructor.js';
@@ -137,6 +138,39 @@ export default function tameErrorConstructor(
137
138
  },
138
139
  });
139
140
 
141
+ if (platform === 'v8') {
142
+ // `SharedError.prepareStackTrace`, if it exists, must also be
143
+ // powerless. However, from what we've heard, depd expects to be able to
144
+ // assign to it without the assignment throwing. It is normally a function
145
+ // that returns a stack string to be magically added to error objects.
146
+ // However, as long as we're adding a lenient standin, we may as well
147
+ // accommodate any who expect to get a function they can call and get
148
+ // a string back. This prepareStackTrace is a do-nothing function that
149
+ // always returns the empty string.
150
+ defineProperties(SharedError, {
151
+ prepareStackTrace: {
152
+ get() {
153
+ return () => '';
154
+ },
155
+ set(_prepareFn) {
156
+ // do nothing
157
+ },
158
+ enumerable: false,
159
+ configurable: true,
160
+ },
161
+ captureStackTrace: {
162
+ value: (errorish, _constructorOpt) => {
163
+ defineProperty(errorish, 'stack', {
164
+ value: '',
165
+ });
166
+ },
167
+ writable: false,
168
+ enumerable: false,
169
+ configurable: true,
170
+ },
171
+ });
172
+ }
173
+
140
174
  let initialGetStackString = tamedMethods.getStackString;
141
175
  if (platform === 'v8') {
142
176
  initialGetStackString = tameV8ErrorConstructor(
@@ -63,7 +63,7 @@ export const makeRejectionHandlers = reportReason => {
63
63
  * @param {ReasonId} heldReasonId
64
64
  */
65
65
  const finalizeDroppedPromise = heldReasonId => {
66
- if (mapHas(idToReason)) {
66
+ if (mapHas(idToReason, heldReasonId)) {
67
67
  const reason = mapGet(idToReason, heldReasonId);
68
68
  removeReasonId(heldReasonId);
69
69
  reportReason(reason);
@@ -66,40 +66,52 @@ const getTypedArrayToStringTag = typedArrayToStringTag.get;
66
66
  assert(getTypedArrayToStringTag);
67
67
 
68
68
  // Exported for tests.
69
- /** @param {unknown} object */
69
+ /**
70
+ * Duplicates packages/marshal/src/helpers/passStyle-helpers.js to avoid a dependency.
71
+ *
72
+ * @param {unknown} object
73
+ */
70
74
  export const isTypedArray = object => {
71
75
  // The object must pass a brand check or toStringTag will return undefined.
72
76
  const tag = apply(getTypedArrayToStringTag, object, []);
73
77
  return tag !== undefined;
74
78
  };
75
79
 
80
+ /**
81
+ * Tests if a property key is an integer-valued canonical numeric index.
82
+ * https://tc39.es/ecma262/#sec-canonicalnumericindexstring
83
+ *
84
+ * @param {string | symbol} propertyKey
85
+ */
86
+ const isCanonicalIntegerIndexString = propertyKey => {
87
+ const n = +String(propertyKey);
88
+ return isInteger(n) && String(n) === propertyKey;
89
+ };
90
+
76
91
  /**
77
92
  * @template T
78
93
  * @param {ArrayLike<T>} array
79
94
  */
80
95
  const freezeTypedArray = array => {
81
- const descs = getOwnPropertyDescriptors(array);
82
-
83
96
  preventExtensions(array);
84
97
 
85
98
  // Downgrade writable expandos to readonly, even if non-configurable.
86
- arrayForEach(ownKeys(descs), (/** @type {string | symbol} */ name) => {
87
- const desc = descs[/** @type {string} */ (name)];
88
- // The numbered properties are writable and non-configurable,
89
- // and cannot be made non-writable by defineProperty.
90
- // This is a strange behavior intrinsic to TypedArrays, but no more harmful
91
- // than the mutability of properties of a hardened Map or Set,
92
- // so we carve out this exceptional behavior.
93
- //
94
- // TypedArrays are integer-indexed exotic objects, so indexed properties
95
- // outside the range of 0 to the typed array's length are disallowed.
96
- // Assignment to these indexes silently fails and defining an indexed
97
- // property throws an error.
98
- // So, we only need to make non-index properties non-writable and
99
- // non-configurable.
99
+ // We get each descriptor individually rather than using
100
+ // getOwnPropertyDescriptors in order to fail safe when encountering
101
+ // an obscure GraalJS issue where getOwnPropertyDescriptor returns
102
+ // undefined for a property that does exist.
103
+ arrayForEach(ownKeys(array), (/** @type {string | symbol} */ name) => {
104
+ const desc = getOwnPropertyDescriptor(array, name);
105
+ assert(desc);
106
+ // TypedArrays are integer-indexed exotic objects, which define special
107
+ // treatment for property names in canonical numeric form:
108
+ // integers in range are permanently writable and non-configurable.
100
109
  // https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects
101
- const number = +String(name);
102
- if (!isInteger(number)) {
110
+ //
111
+ // This is analogous to the data of a hardened Map or Set,
112
+ // so we carve out this exceptional behavior but make all other
113
+ // properties non-configurable.
114
+ if (!isCanonicalIntegerIndexString(name)) {
103
115
  defineProperty(array, name, {
104
116
  ...desc,
105
117
  writable: false,