ses 1.4.1 → 1.5.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/types.d.ts CHANGED
@@ -95,12 +95,14 @@ export type ModuleMapHook = (
95
95
  moduleSpecifier: string,
96
96
  ) => string | ModuleExportsNamespace | void;
97
97
  export type ImportHook = (moduleSpecifier: string) => Promise<StaticModuleType>;
98
+ export type ImportNowHook = (moduleSpecifier: string) => StaticModuleType;
98
99
 
99
100
  export interface CompartmentOptions {
100
101
  name?: string;
101
102
  transforms?: Array<Transform>;
102
103
  moduleMapHook?: ModuleMapHook;
103
104
  importHook?: ImportHook;
105
+ importNowHook?: ImportNowHook;
104
106
  resolveHook?: ResolveHook;
105
107
  __shimTransforms__?: Array<Transform>;
106
108
  }
@@ -113,16 +115,49 @@ export interface EvaluateOptions {
113
115
  __rejectSomeDirectEvalExpressions__?: boolean;
114
116
  }
115
117
 
116
- // The DetailsToken is an empty object literal.
118
+ /**
119
+ * A call to the `details` template literal makes and returns a fresh details
120
+ * token, which is a frozen empty object associated with the arguments of that
121
+ * `details` template literal expression.
122
+ */
117
123
  export type DetailsToken = Record<any, never>;
124
+ /** Either a plain string, or made by the `details` template literal tag. */
118
125
  export type Details = string | DetailsToken;
119
126
 
120
127
  export interface AssertMakeErrorOptions {
128
+ /**
129
+ * Does not affect the error.name property. That remains determined by
130
+ * the constructor. Rather, the `errorName` determines how this error is
131
+ * identified in the causal console log's output.
132
+ */
121
133
  errorName?: string;
134
+
135
+ /**
136
+ * Discloses the error that caused this one, typically from a lower
137
+ * layer of abstraction. This is represented by a public `cause` data property
138
+ * on the error, not a hidden annotation.
139
+ */
122
140
  cause?: Error;
141
+
142
+ /**
143
+ * Normally only used when the ErrorConstuctor is `AggregateError`, to
144
+ * represent the set of prior errors aggregated together in this error,
145
+ * typically by `Promise.any`. But `makeError` allows it on any error.
146
+ * This is represented by a public `errors` data property on the error,
147
+ * not a hidden annotation.
148
+ */
123
149
  errors?: Error[];
150
+
151
+ /**
152
+ * Defaults to true. If true, `makeError` will apply `sanitizeError`
153
+ * to the error before returning it. See the comments on
154
+ * {@link sanitizeError}.
155
+ */
156
+ sanitize?: boolean;
124
157
  }
125
158
 
159
+ // TODO inline overloading
160
+
126
161
  type AssertTypeofBigint = (
127
162
  specimen: any,
128
163
  typeName: 'bigint',
@@ -173,62 +208,282 @@ export type AssertTypeof = AssertTypeofBigint &
173
208
  AssertTypeofSymbol &
174
209
  AssertTypeofUndefined;
175
210
 
176
- interface ToStringable {
211
+ interface StringablePayload {
177
212
  toString(): string;
178
213
  }
179
214
 
215
+ /**
216
+ * TypeScript does not treat `AggregateErrorConstructor` as a subtype of
217
+ * `ErrorConstructor`, which makes sense because their constructors
218
+ * have incompatible signatures. However, we want to parameterize some
219
+ * operations by any error constructor, including possible `AggregateError`.
220
+ * So we introduce `GenericErrorConstructor` as a common supertype. Any call
221
+ * to it to make an instance must therefore first case split on whether the
222
+ * constructor is an AggregateErrorConstructor or a normal ErrorConstructor.
223
+ */
180
224
  export type GenericErrorConstructor =
181
225
  | ErrorConstructor
182
226
  | AggregateErrorConstructor;
183
227
 
228
+ /**
229
+ * To make an `assert` which terminates some larger unit of computation
230
+ * like a transaction, vat, or process, call `makeAssert` with a `Raise`
231
+ * callback, where that callback actually performs that larger termination.
232
+ * If possible, the callback should also report its `reason` parameter as
233
+ * the alleged reason for the termination.
234
+ */
184
235
  export type Raise = (reason: Error) => void;
236
+
237
+ /**
238
+ * Makes and returns an `assert` function object that shares the bookkeeping
239
+ * state defined by this module with other `assert` function objects made by
240
+ * `makeAssert`. This state is per-module-instance and is exposed by the
241
+ * `loggedErrorHandler` above. We refer to `assert` as a "function object"
242
+ * because it can be called directly as a function, but also has methods that
243
+ * can be called.
244
+ *
245
+ * If `optRaise` is provided, the returned `assert` function object will call
246
+ * `optRaise(reason)` before throwing the error. This enables `optRaise` to
247
+ * engage in even more violent termination behavior, like terminating the vat,
248
+ * that prevents execution from reaching the following throw. However, if
249
+ * `optRaise` returns normally, which would be unusual, the throw following
250
+ * `optRaise(reason)` would still happen.
251
+ */
185
252
  // Behold: recursion.
186
253
  // eslint-disable-next-line no-use-before-define
187
254
  export type MakeAssert = (raise?: Raise, unredacted?: boolean) => Assert;
188
255
 
189
- export interface AssertionFunctions {
190
- (
191
- value: any,
192
- details?: Details,
193
- errConstructor?: GenericErrorConstructor,
194
- options?: AssertMakeErrorOptions,
195
- ): asserts value;
256
+ export type BaseAssert = (
257
+ /** The truthy/falsy value we're testing */
258
+ flag: any,
259
+ /** The details of what was asserted */
260
+ details?: Details,
261
+ /** An optional alternate error constructor to use */
262
+ errConstructor?: GenericErrorConstructor,
263
+ options?: AssertMakeErrorOptions,
264
+ ) => asserts flag;
265
+
266
+ export interface AssertionFunctions extends BaseAssert {
196
267
  typeof: AssertTypeof;
268
+
269
+ /**
270
+ * The `assert.equal` method
271
+ *
272
+ * Assert that two values must be `Object.is`.
273
+ */
197
274
  equal(
198
- left: any,
199
- right: any,
275
+ /** What we received */
276
+ actual: any,
277
+ /** What we wanted */
278
+ expected: any,
279
+ /** The details of what was asserted */
200
280
  details?: Details,
281
+ /** An optional alternate error constructor to use */
201
282
  errConstructor?: GenericErrorConstructor,
202
283
  options?: AssertMakeErrorOptions,
203
284
  ): void;
204
- string(specimen: any, details?: Details): asserts specimen is string;
285
+
286
+ /**
287
+ * The `assert.string` method.
288
+ *
289
+ * `assert.string(v)` is equivalent to `assert.typeof(v, 'string')`. We
290
+ * special case this one because it is the most frequently used.
291
+ *
292
+ * Assert an expected typeof result.
293
+ */
294
+ string(
295
+ specimen: any,
296
+ /** The details of what was asserted */
297
+ details?: Details,
298
+ ): asserts specimen is string;
299
+
300
+ /**
301
+ * The `assert.fail` method.
302
+ *
303
+ * Fail an assertion, recording full details to the console and
304
+ * raising an exception with a message in which `details` substitution values
305
+ * have been redacted.
306
+ *
307
+ * The optional `optDetails` can be a string for backwards compatibility
308
+ * with the nodejs assertion library.
309
+ */
205
310
  fail(
311
+ /** The details of what was asserted */
206
312
  details?: Details,
313
+ /** An optional alternate error constructor to use */
207
314
  errConstructor?: GenericErrorConstructor,
208
315
  options?: AssertMakeErrorOptions,
209
316
  ): never;
210
317
  }
211
318
 
212
319
  export interface AssertionUtilities {
320
+ /**
321
+ * Aka the `makeError` function as imported from `@endo/errors`
322
+ *
323
+ * Recording unredacted details for the console.
324
+ */
213
325
  error(
326
+ /** The details of what was asserted */
214
327
  details?: Details,
328
+ /** An optional alternate error constructor to use */
215
329
  errConstructor?: GenericErrorConstructor,
216
330
  options?: AssertMakeErrorOptions,
217
331
  ): Error;
332
+
333
+ /**
334
+ * Aka the `annotateError` function as imported from `@endo/errors`
335
+ *
336
+ * Annotate an error with details, potentially to be used by an
337
+ * augmented console such as the causal console of `console.js`, to
338
+ * provide extra information associated with logged errors.
339
+ */
218
340
  note(error: Error, details: Details): void;
341
+
342
+ /**
343
+ * Use the `details` function as a template literal tag to create
344
+ * informative error messages. The assertion functions take such messages
345
+ * as optional arguments:
346
+ * ```js
347
+ * assert(sky.isBlue(), details`${sky.color} should be "blue"`);
348
+ * ```
349
+ * // TODO Update SES-shim to new convention, where `details` is
350
+ * // renamed to `X` rather than `d`.
351
+ * or following the normal convention to locally rename `details` to `d`
352
+ * and `quote` to `q` like `const { details: d, quote: q } = assert;`:
353
+ * ```js
354
+ * assert(sky.isBlue(), d`${sky.color} should be "blue"`);
355
+ * ```
356
+ * However, note that in most cases it is preferable to instead use the `Fail`
357
+ * template literal tag (which has the same input signature as `details`
358
+ * but automatically creates and throws an error):
359
+ * ```js
360
+ * sky.isBlue() || Fail`${sky.color} should be "blue"`;
361
+ * ```
362
+ *
363
+ * The details template tag returns a `DetailsToken` object that can print
364
+ * itself with the formatted message in two ways.
365
+ * It will report full details to the console, but
366
+ * mask embedded substitution values with their typeof information in the thrown error
367
+ * to prevent revealing secrets up the exceptional path. In the example
368
+ * above, the thrown error may reveal only that `sky.color` is a string,
369
+ * whereas the same diagnostic printed to the console reveals that the
370
+ * sky was green. This masking can be disabled for an individual substitution value
371
+ * using `quote`.
372
+ *
373
+ * The `raw` property of an input template array is ignored, so a simple
374
+ * array of strings may be provided directly.
375
+ */
219
376
  details(
220
377
  template: TemplateStringsArray | string[],
221
378
  ...args: any
222
379
  ): DetailsToken;
380
+
381
+ /**
382
+ * Use the `Fail` function as a template literal tag to efficiently
383
+ * create and throw a `details`-style error only when a condition is not satisfied.
384
+ * ```js
385
+ * condition || Fail`...complaint...`;
386
+ * ```
387
+ * This avoids the overhead of creating usually-unnecessary errors like
388
+ * ```js
389
+ * assert(condition, details`...complaint...`);
390
+ * ```
391
+ * while improving readability over alternatives like
392
+ * ```js
393
+ * condition || assert.fail(details`...complaint...`);
394
+ * ```
395
+ *
396
+ * However, due to current weakness in TypeScript, static reasoning
397
+ * is less powerful with the `||` patterns than with an `assert` call.
398
+ * Until/unless https://github.com/microsoft/TypeScript/issues/51426 is fixed,
399
+ * for `||`-style assertions where this loss of static reasoning is a problem,
400
+ * instead express the assertion as
401
+ * ```js
402
+ * if (!condition) {
403
+ * Fail`...complaint...`;
404
+ * }
405
+ * ```
406
+ * or, if needed,
407
+ * ```js
408
+ * if (!condition) {
409
+ * // `throw` is noop since `Fail` throws, but it improves static analysis
410
+ * throw Fail`...complaint...`;
411
+ * }
412
+ * ```
413
+ */
223
414
  Fail(template: TemplateStringsArray | string[], ...args: any): never;
224
- quote(payload: any, spaces?: string | number): ToStringable;
225
- bare(payload: any, spaces?: string | number): ToStringable;
415
+
416
+ /**
417
+ * To "declassify" and quote a substitution value used in a
418
+ * ``` details`...` ``` template literal, enclose that substitution expression
419
+ * in a call to `quote`. This makes the value appear quoted
420
+ * (as if with `JSON.stringify`) in the message of the thrown error. The
421
+ * payload itself is still passed unquoted to the console as it would be
422
+ * without `quote`.
423
+ *
424
+ * For example, the following will reveal the expected sky color, but not the
425
+ * actual incorrect sky color, in the thrown error's message:
426
+ * ```js
427
+ * sky.color === expectedColor || Fail`${sky.color} should be ${quote(expectedColor)}`;
428
+ * ```
429
+ *
430
+ * // TODO Update SES-shim to new convention, where `details` is
431
+ * // renamed to `X` rather than `d`.
432
+ * The normal convention is to locally rename `details` to `d` and `quote` to `q`
433
+ * like `const { details: d, quote: q } = assert;`, so the above example would then be
434
+ * ```js
435
+ * sky.color === expectedColor || Fail`${sky.color} should be ${q(expectedColor)}`;
436
+ * ```
437
+ */
438
+ quote(
439
+ /** What to declassify */
440
+ payload: any,
441
+ spaces?: string | number,
442
+ ): /** The declassified and quoted payload */ StringablePayload;
443
+
444
+ /**
445
+ * Embed a string directly into error details without wrapping punctuation.
446
+ * To avoid injection attacks that exploit quoting confusion, this must NEVER
447
+ * be used with data that is possibly attacker-controlled.
448
+ * As a further safeguard, we fall back to quoting any input that is not a
449
+ * string of sufficiently word-like parts separated by isolated spaces (rather
450
+ * than throwing an exception, which could hide the original problem for which
451
+ * explanatory details are being constructed---i.e., ``` assert.details`...` ```
452
+ * should never be the source of a new exception, nor should an attempt to
453
+ * render its output, although we _could_ instead decide to handle the latter
454
+ * by inline replacement similar to that of `bestEffortStringify` for producing
455
+ * rendered messages like `(an object) was tagged "[Unsafe bare string]"`).
456
+ */
457
+ bare(
458
+ /** What to declassify */
459
+ payload: any,
460
+ spaces?: string | number,
461
+ ): /** The declassified payload without quotes (beware confusion hazard) */
462
+ StringablePayload;
226
463
  }
227
464
 
228
465
  export interface DeprecatedAssertionUtilities {
229
466
  makeAssert: MakeAssert;
230
467
  }
231
468
 
469
+ /**
470
+ * assert that expr is truthy, with an optional details to describe
471
+ * the assertion. It is a tagged template literal like
472
+ * ```js
473
+ * assert(expr, details`....`);`
474
+ * ```
475
+ *
476
+ * The literal portions of the template are assumed non-sensitive, as
477
+ * are the `typeof` types of the substitution values. These are
478
+ * assembled into the thrown error message. The actual contents of the
479
+ * substitution values are assumed sensitive, to be revealed to
480
+ * the console only. We assume only the virtual platform's owner can read
481
+ * what is written to the console, where the owner is in a privileged
482
+ * position over computation running on that platform.
483
+ *
484
+ * The optional `optDetails` can be a string for backwards compatibility
485
+ * with the nodejs assertion library.
486
+ */
232
487
  export type Assert = AssertionFunctions &
233
488
  AssertionUtilities &
234
489
  DeprecatedAssertionUtilities;