ses 1.4.1 → 1.6.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 +68 -2
- package/README.md +209 -59
- package/dist/lockdown.cjs +10974 -10775
- package/dist/lockdown.mjs +10974 -10775
- package/dist/lockdown.umd.js +10974 -10775
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +10974 -10775
- package/dist/ses.mjs +10974 -10775
- package/dist/ses.umd.js +10974 -10775
- package/dist/ses.umd.min.js +1 -1
- package/dist/types.d.cts +567 -0
- package/package.json +31 -21
- package/src/commons.js +66 -0
- package/src/compartment.js +74 -52
- package/src/error/README.md +1 -1
- package/src/error/assert.js +112 -28
- package/src/error/fatal-assert.js +2 -0
- package/src/error/stringify-utils.js +2 -0
- package/src/error/tame-console.js +8 -8
- package/src/error/tame-error-constructor.js +49 -5
- package/src/error/tame-v8-error-constructor.js +6 -0
- package/src/error/types.js +1 -365
- package/src/global-object.js +8 -5
- package/src/lockdown.js +10 -6
- package/src/make-hardener.js +49 -21
- package/src/module-instance.js +10 -14
- package/src/module-link.js +29 -45
- package/src/module-load.js +458 -169
- package/src/permits.js +20 -2
- package/src/tame-symbol-constructor.js +4 -5
- package/types.d.ts +323 -34
package/NEWS.md
CHANGED
|
@@ -1,4 +1,70 @@
|
|
|
1
|
-
User-visible changes in
|
|
1
|
+
User-visible changes in `ses`:
|
|
2
|
+
|
|
3
|
+
# v1.6.0 (2024-07-30)
|
|
4
|
+
|
|
5
|
+
- *NOTICE*: This version introduces multiple features to converge upon a
|
|
6
|
+
more common standard for [Hardened JavaScript](https://hardenedjs.org).
|
|
7
|
+
All code should begin migrating to these usage patterns as the older
|
|
8
|
+
patterns are now deprecated and will not be supported in a future major
|
|
9
|
+
version of SES.
|
|
10
|
+
|
|
11
|
+
- To converge on a portable pattern for using `Compartment`, introduces an
|
|
12
|
+
`__options__` property for the first argument of the `Compartment`
|
|
13
|
+
constructor that must be `true` if present and indicates the object is the
|
|
14
|
+
options bag and not the global endowments. All code going forward should
|
|
15
|
+
include this flag until the next major version of SES, when we plan for it to
|
|
16
|
+
become vesgitial and drop support for three-argument `Compartment`
|
|
17
|
+
construction.
|
|
18
|
+
|
|
19
|
+
In the unlikely event that existing code names an endowment `__options__`,
|
|
20
|
+
that code will break and need to be adjusted to adopt this version.
|
|
21
|
+
Because we rate this unlikely, we have elected not to mark this with
|
|
22
|
+
a major version bump.
|
|
23
|
+
|
|
24
|
+
- Adds a `__noNamespaceBox__` option that aligns the behavior of the `import`
|
|
25
|
+
method on SES `Compartment` with the behavior of XS and the behavior we will
|
|
26
|
+
champion for compartment standards.
|
|
27
|
+
All use of `Compartment` should migrate to use this option as the standard
|
|
28
|
+
behavior will be enabled by default with the next major version of SES.
|
|
29
|
+
|
|
30
|
+
- Adds support for module descriptors better aligned with XS.
|
|
31
|
+
Compartments use module desriptors to load and link modules.
|
|
32
|
+
The importHook, importNowHook, and moduleMapHook all return module descriptors
|
|
33
|
+
(sometimes promises for module descriptors).
|
|
34
|
+
The modules option or argument to the Compatment constructor has module
|
|
35
|
+
descriptors for all its values.
|
|
36
|
+
- `{record, specifier, compartment}` should become `{source: record,
|
|
37
|
+
specifier, compartment}`.
|
|
38
|
+
- `{specifier, compartment}` should become `{source: specifier,
|
|
39
|
+
compartment}`.
|
|
40
|
+
- `{record: compartment.module(specifier)}` should become `{namespace:
|
|
41
|
+
specifier, compartment}`.
|
|
42
|
+
|
|
43
|
+
- When running transpiled code on Node, the SES error taming
|
|
44
|
+
gives line-numbers into the generated JavaScript, which often don't match the
|
|
45
|
+
the original lines. This happens even with the normal development-time
|
|
46
|
+
lockdown options setting,
|
|
47
|
+
```js
|
|
48
|
+
errorTaming: 'unsafe'
|
|
49
|
+
```
|
|
50
|
+
or setting the environment variable
|
|
51
|
+
```sh
|
|
52
|
+
$ export LOCKDOWN_ERROR_TAMING=unsafe
|
|
53
|
+
```
|
|
54
|
+
To get the original line numbers, this release
|
|
55
|
+
adds `'unsafe-debug'`. This `errorTaming: 'unsafe-debug'` setting
|
|
56
|
+
should be used ***during development only*** when you can
|
|
57
|
+
sacrifice more security for a better debugging experience, as explained at
|
|
58
|
+
[`errorTaming` Options](https://github.com/endojs/endo/blob/master/packages/ses/docs/lockdown.md#errortaming-options).
|
|
59
|
+
With this setting, when running transpiled code on Node (e.g. tests written
|
|
60
|
+
in TypeScript),
|
|
61
|
+
the stacktrace line-numbers point back into the original
|
|
62
|
+
source, as they do on Node without SES.
|
|
63
|
+
|
|
64
|
+
# v1.5.0 (2024-05-06)
|
|
65
|
+
|
|
66
|
+
- Adds `importNowHook` to the `Compartment` options. The compartment will invoke the hook whenever it encounters a missing dependency while running `compartmentInstance.importNow(specifier)`, which cannot use an asynchronous `importHook`.
|
|
67
|
+
- To support TypeScript v5.5, a CommonJS-specific type declaration file is now explicitly exported.
|
|
2
68
|
|
|
3
69
|
# v1.3.0 (2024-02-22)
|
|
4
70
|
|
|
@@ -387,7 +453,7 @@ User-visible changes in SES:
|
|
|
387
453
|
for an example of performing a scopeProxy leak workaround.
|
|
388
454
|
- Under the default `{errorTaming: 'safe'}` setting, the SES shim already redacts stack traces from error instances when it can (currently: v8, spiderMonkey, XS). The setting `{errorTaming: 'unsafe'}` suppresses that redaction, instead blabbing these stack traces on error instances via the expected `errorInstance.stack`.
|
|
389
455
|
|
|
390
|
-
The purpose of the `details` template literal tag (often spelled `X`
|
|
456
|
+
The purpose of the `details` template literal tag (often spelled `X`) together with the `quote` function (often spelled `q`) is to redact data from the error messages carried by error instances. With this release, the same `{errorTaming: 'unsafe'}` would suppress that redaction as well, so that all substitution values would act like they've been quoted. IOW, with this setting
|
|
391
457
|
|
|
392
458
|
```js
|
|
393
459
|
assert(false, X`literal part ${secretData} with ${q(publicData)}.`);
|
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# SES
|
|
2
2
|
|
|
3
|
-
SES is
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
SES is a [shim][define shim] for [Hardened JavaScript][] as [proposed][SES
|
|
4
|
+
proposal] to ECMA TC39.
|
|
5
|
+
SES stands for *fearless cooperation*.
|
|
6
6
|
Hardened JavaScript is highly compatible with ordinary JavaScript.
|
|
7
|
-
Most existing JavaScript libraries can run on
|
|
7
|
+
Most existing JavaScript libraries can run on Hardened JavaScript.
|
|
8
8
|
|
|
9
9
|
* **Compartments** Compartments are separate execution contexts: each one has
|
|
10
10
|
its own global object and global lexical scope.
|
|
@@ -24,6 +24,14 @@ compartments that have no excess authority in their global scope.
|
|
|
24
24
|
SES runs atop an ES6-compliant platform, enabling safe interaction of
|
|
25
25
|
mutually-suspicious code, using object-capability -style programming.
|
|
26
26
|
|
|
27
|
+
Agoric and MetaMask rely on Hardened JavaScript and this SES shim as part of
|
|
28
|
+
systems that sandbox third-party plugins or smart contracts and mitigate supply
|
|
29
|
+
chain attacks for production web applications, web extensions, and build
|
|
30
|
+
systems.
|
|
31
|
+
|
|
32
|
+
[](https://agoric.com/)
|
|
33
|
+
[](https://metamask.io/)
|
|
34
|
+
|
|
27
35
|
See https://github.com/Agoric/Jessie to see how SES fits into the various
|
|
28
36
|
flavors of confined JavaScript execution. And visit
|
|
29
37
|
https://ses-demo.agoric.app/demos/ for a demo.
|
|
@@ -161,7 +169,10 @@ function on `globalThis`.
|
|
|
161
169
|
import 'ses';
|
|
162
170
|
|
|
163
171
|
const c = new Compartment({
|
|
164
|
-
|
|
172
|
+
globals: {
|
|
173
|
+
print: harden(console.log),
|
|
174
|
+
},
|
|
175
|
+
__options__: true, // temporary migration affordance
|
|
165
176
|
});
|
|
166
177
|
|
|
167
178
|
c.evaluate(`
|
|
@@ -205,7 +216,10 @@ through:
|
|
|
205
216
|
* by assigning them to the compartment's `globalThis` after construction.
|
|
206
217
|
|
|
207
218
|
```js
|
|
208
|
-
const powerfulCompartment = new Compartment({
|
|
219
|
+
const powerfulCompartment = new Compartment({
|
|
220
|
+
globals: { Math },
|
|
221
|
+
__options__: true, // temporary migration affordance
|
|
222
|
+
});
|
|
209
223
|
powerfulCompartment.globalThis.Date = Date;
|
|
210
224
|
```
|
|
211
225
|
|
|
@@ -254,22 +268,31 @@ This can and should be frozen before running any dynamic code in that
|
|
|
254
268
|
compartment, yet is not strictly necessary if the compartment only
|
|
255
269
|
runs code from a single party.
|
|
256
270
|
|
|
257
|
-
|
|
258
271
|
### Modules
|
|
259
272
|
|
|
260
273
|
Any code executed within a compartment shares a set of module instances.
|
|
261
274
|
For modules to work within a compartment, the creator must provide
|
|
262
|
-
|
|
275
|
+
module descriptors.
|
|
276
|
+
A compartment can be configured with module descriptors, from highest to lowest
|
|
277
|
+
precedence:
|
|
278
|
+
|
|
279
|
+
- the `modules` map provided to the `Compartment` constructor,
|
|
280
|
+
- returned by a `moduleMapHook(specifier)` passed as an option to the
|
|
281
|
+
`Compartment` constructor.
|
|
282
|
+
- returned by either the `importHook(specifier)` or `importNowHook(specifier)`
|
|
283
|
+
option passed to the `Compartment` constructor. Calling
|
|
284
|
+
`compartment.import(specifier)` falls through to the `importHook` which may
|
|
285
|
+
return a promises, whereas `compartment.importNow(specifier)` falls through
|
|
286
|
+
to the synchronous `importNowHook`.
|
|
287
|
+
|
|
263
288
|
The `resolveHook` determines how the compartment will infer the full module
|
|
264
289
|
specifier for another module from a referrer module and the import specifier.
|
|
265
|
-
The `importHook` accepts a full specifier and asynchronously returns a
|
|
266
|
-
`StaticModuleRecord` for that module.
|
|
267
290
|
|
|
268
291
|
```js
|
|
269
292
|
import 'ses';
|
|
270
|
-
import {
|
|
293
|
+
import { ModuleSource } from '@endo/module-source';
|
|
271
294
|
|
|
272
|
-
const c1 = new Compartment({
|
|
295
|
+
const c1 = new Compartment({
|
|
273
296
|
name: "first compartment",
|
|
274
297
|
resolveHook: (moduleSpecifier, moduleReferrer) => {
|
|
275
298
|
return resolve(moduleSpecifier, moduleReferrer);
|
|
@@ -277,50 +300,98 @@ const c1 = new Compartment({}, {}, {
|
|
|
277
300
|
importHook: async moduleSpecifier => {
|
|
278
301
|
const moduleLocation = locate(moduleSpecifier);
|
|
279
302
|
const moduleText = await retrieve(moduleLocation);
|
|
280
|
-
return
|
|
303
|
+
return {
|
|
304
|
+
source: new ModuleSource(moduleText, moduleLocation);
|
|
305
|
+
};
|
|
281
306
|
},
|
|
307
|
+
__options__: true, // temporary migration affordance
|
|
282
308
|
});
|
|
283
309
|
```
|
|
284
310
|
|
|
285
|
-
> The
|
|
286
|
-
> provided by the shim because it entrains a full JavaScript parser that
|
|
287
|
-
> unnecessary performance penalty for the SES runtime.
|
|
288
|
-
> Instead, the SES shim accepts a compiled
|
|
311
|
+
> The Hardened JavaScript language specifies a global `ModuleSource`, but this
|
|
312
|
+
> is not provided by the shim because it entrains a full JavaScript parser that
|
|
313
|
+
> is an unnecessary performance penalty for the SES runtime.
|
|
314
|
+
> Instead, the SES shim accepts a pre-compiled module source duck-type that
|
|
289
315
|
> is tightly coupled to the shim implementation.
|
|
290
|
-
> Third party modules can provide suitable implementations and even move the
|
|
291
|
-
> compile step to build time instead of runtime.
|
|
292
316
|
|
|
293
317
|
A compartment can also link a module in another compartment.
|
|
294
|
-
Each compartment has a `module` function that accepts a module specifier
|
|
295
|
-
and returns the module exports namespace for that module.
|
|
296
|
-
The module exports namespace is not useful for inspecting the exports of the
|
|
297
|
-
module until that module has been imported, but it can be passed into the
|
|
298
|
-
module map of another Compartment, creating a link.
|
|
299
318
|
|
|
300
319
|
```js
|
|
301
|
-
const c2 = new Compartment({
|
|
302
|
-
'c1': c1.module('./main.js'),
|
|
303
|
-
}, {
|
|
320
|
+
const c2 = new Compartment({
|
|
304
321
|
name: "second compartment",
|
|
322
|
+
modules: {
|
|
323
|
+
'c1': {
|
|
324
|
+
source: './main.js',
|
|
325
|
+
compartment: c1,
|
|
326
|
+
},
|
|
327
|
+
},
|
|
305
328
|
resolveHook,
|
|
306
329
|
importHook,
|
|
330
|
+
__options__: true, // temporary migration affordance
|
|
307
331
|
});
|
|
308
332
|
```
|
|
309
333
|
|
|
310
|
-
|
|
334
|
+
#### Module Descriptors
|
|
335
|
+
|
|
336
|
+
Comparments can load and initialize module namespaces from module descriptors.
|
|
337
|
+
Like property descriptors, module descriptors are ordinary objects with various
|
|
338
|
+
forms.
|
|
339
|
+
|
|
340
|
+
##### Descriptors with `source` property
|
|
341
|
+
|
|
342
|
+
- If fhe value of the `source` property is a string, the parent compartment
|
|
343
|
+
loads the module but the compartment itself initializes the module.
|
|
344
|
+
|
|
345
|
+
- Otherwise, if the value of the `source` property is the module source, the
|
|
346
|
+
module is initialized from the module source.
|
|
347
|
+
|
|
348
|
+
- Otherwise, the value of the `source` property must be an object. The module
|
|
349
|
+
is loaded and initialized from the object according to the [virtual module
|
|
350
|
+
source](#VirtualModuleSource) pattern.
|
|
351
|
+
|
|
352
|
+
If the `importMeta` property is present, its value must be an object. The
|
|
353
|
+
default `importMeta` object is an empty object.
|
|
354
|
+
|
|
355
|
+
Compartments copy the `importMeta` object properties into the module
|
|
356
|
+
`import.meta` object like `Object.assign`.
|
|
357
|
+
|
|
358
|
+
If the `specifier` property is present, its value is coerced into a string and
|
|
359
|
+
becomes the referrer specifier of the module, on which all import specifiers
|
|
360
|
+
are resolved using the `resolveHook`.
|
|
361
|
+
|
|
362
|
+
##### Descriptors with `namespace` property
|
|
363
|
+
|
|
364
|
+
- If fhe value of the `namespace` property is a string, the descriptor shares a
|
|
365
|
+
module to be loaded and initialized by the compartment referred by the
|
|
366
|
+
`compartment` property.
|
|
367
|
+
|
|
368
|
+
- If the `compartment` property is present, its value must be a
|
|
369
|
+
compartment.
|
|
370
|
+
- If absent, the `compartment` property defaults to the compartment being
|
|
371
|
+
constructed in the `modules` option, or being hooked in the `loadHook`
|
|
372
|
+
and `loadNowHook` options.
|
|
373
|
+
|
|
374
|
+
- Otherwise, if the value of the `namespace` property is a module namepace, the
|
|
375
|
+
descriptor shares a module that is already available.
|
|
376
|
+
|
|
377
|
+
- Otherwise, the value of `namespace` property must be an object. The module is
|
|
378
|
+
loaded and initialized from the object according to the [virtual module
|
|
379
|
+
namespace](#VirtualModuleNamespace) pattern.
|
|
380
|
+
|
|
381
|
+
#### Redirects
|
|
311
382
|
|
|
312
383
|
If a compartment imports a module specified as `"./utility"` but actually
|
|
313
384
|
implemented by an alias like `"./utility/index.js"`, the `importHook` may
|
|
314
385
|
follow redirects, symbolic links, or search for candidates using its own logic
|
|
315
386
|
and return a module that has a different "response specifier" than the original
|
|
316
387
|
"request specifier".
|
|
317
|
-
The `importHook` may return an "alias" object with `
|
|
318
|
-
and `
|
|
388
|
+
The `importHook` may return an "alias" object with `source`, `compartment`,
|
|
389
|
+
and `specifier` properties.
|
|
319
390
|
|
|
320
|
-
- `
|
|
321
|
-
or a compiled
|
|
391
|
+
- `source` must be a module source, either a virtual module source
|
|
392
|
+
or a compiled module source.
|
|
322
393
|
- `compartment` is optional, to be specified if the alias transits to a
|
|
323
|
-
different compartment, and
|
|
394
|
+
the specified different compartment, and
|
|
324
395
|
- `specifier` is the full module specifier of the module in its compartment.
|
|
325
396
|
This defaults to the request specifier, which is only useful if the
|
|
326
397
|
compartment is different.
|
|
@@ -332,21 +403,26 @@ alias.
|
|
|
332
403
|
const importHook = async specifier => {
|
|
333
404
|
const candidates = [specifier, `${specifier}.js`, `${specifier}/index.js`];
|
|
334
405
|
for (const candidate of candidates) {
|
|
335
|
-
const
|
|
336
|
-
if (
|
|
337
|
-
return {
|
|
406
|
+
const source = await maybeImportSource(candidate);
|
|
407
|
+
if (source !== undefined) {
|
|
408
|
+
return {
|
|
409
|
+
source,
|
|
410
|
+
specifier: candidate,
|
|
411
|
+
compartment,
|
|
412
|
+
};
|
|
338
413
|
}
|
|
339
414
|
}
|
|
340
415
|
throw new Error(`Cannot find module ${specifier}`);
|
|
341
416
|
};
|
|
342
417
|
|
|
343
|
-
const compartment = new Compartment({
|
|
418
|
+
const compartment = new Compartment({
|
|
344
419
|
resolveHook,
|
|
345
420
|
importHook,
|
|
421
|
+
__options__: true, // temporary migration affordance
|
|
346
422
|
});
|
|
347
423
|
```
|
|
348
424
|
|
|
349
|
-
|
|
425
|
+
#### moduleMapHook
|
|
350
426
|
|
|
351
427
|
The module map above allows modules to be introduced to a compartment up-front.
|
|
352
428
|
Some modules cannot be known that early.
|
|
@@ -365,30 +441,87 @@ If the `moduleMapHook` returns `undefined`, the compartment proceeds to the
|
|
|
365
441
|
```js
|
|
366
442
|
const moduleMapHook = moduleSpecifier => {
|
|
367
443
|
if (moduleSpecifier === 'even') {
|
|
368
|
-
return
|
|
444
|
+
return {
|
|
445
|
+
source: './index.js',
|
|
446
|
+
compartment: even,
|
|
447
|
+
};
|
|
369
448
|
} else if (moduleSpecifier === 'odd') {
|
|
370
|
-
return
|
|
449
|
+
return {
|
|
450
|
+
source: './index.js',
|
|
451
|
+
compartment: odd,
|
|
452
|
+
};
|
|
371
453
|
}
|
|
372
454
|
};
|
|
373
455
|
|
|
374
|
-
const even = new Compartment({
|
|
456
|
+
const even = new Compartment({
|
|
375
457
|
resolveHook: nodeResolveHook,
|
|
376
458
|
importHook: makeImportHook('https://example.com/even'),
|
|
377
459
|
moduleMapHook,
|
|
460
|
+
__options__: true, // temporary migration affordance
|
|
378
461
|
});
|
|
379
462
|
|
|
380
|
-
const odd = new Compartment({
|
|
463
|
+
const odd = new Compartment({
|
|
381
464
|
resolveHook: nodeResolveHook,
|
|
382
465
|
importHook: makeImportHook('https://example.com/odd'),
|
|
383
466
|
moduleMapHook,
|
|
467
|
+
__options__: true, // temporary migration affordance
|
|
384
468
|
});
|
|
385
469
|
```
|
|
386
470
|
|
|
387
|
-
|
|
471
|
+
#### importNowHook
|
|
472
|
+
|
|
473
|
+
Additionally, an `importNowHook` may be provided that the compartment will use
|
|
474
|
+
as means to synchronously load modules not seen before in situations where
|
|
475
|
+
calling out to asynchronous `importHook` is not possible.
|
|
476
|
+
Specifically, when `compartmentInstance.importNow('specifier')` is called, the
|
|
477
|
+
compartment will first look up module records it's already aware of and call
|
|
478
|
+
`moduleMapHook` and if none of that is successful in finding a module record
|
|
479
|
+
matching the specifier, it will call `importNowHook` expecting to synchronously
|
|
480
|
+
receive the same record type as from `importHook` or throw if it cannot.
|
|
481
|
+
|
|
482
|
+
```js
|
|
483
|
+
import 'ses';
|
|
484
|
+
import { ModuleSource } from '@endo/module-source';
|
|
485
|
+
|
|
486
|
+
const compartment = new Compartment({
|
|
487
|
+
name: "first compartment",
|
|
488
|
+
modules: {
|
|
489
|
+
c: {
|
|
490
|
+
source: new ModuleSource(''),
|
|
491
|
+
},
|
|
492
|
+
},
|
|
493
|
+
resolveHook: (moduleSpecifier, moduleReferrer) => {
|
|
494
|
+
return resolve(moduleSpecifier, moduleReferrer);
|
|
495
|
+
},
|
|
496
|
+
importHook: async moduleSpecifier => {
|
|
497
|
+
const moduleLocation = locate(moduleSpecifier);
|
|
498
|
+
const moduleText = await retrieve(moduleLocation);
|
|
499
|
+
return {
|
|
500
|
+
source: new ModuleSource(moduleText, moduleLocation),
|
|
501
|
+
};
|
|
502
|
+
},
|
|
503
|
+
importNowHook: moduleSpecifier => {
|
|
504
|
+
const moduleLocation = locate(moduleSpecifier);
|
|
505
|
+
// Platform-specific synchronous read API can be used
|
|
506
|
+
const moduleText = fs.readFileSync(moduleLocation);
|
|
507
|
+
return {
|
|
508
|
+
source: new ModuleSource(moduleText, moduleLocation),
|
|
509
|
+
};
|
|
510
|
+
},
|
|
511
|
+
__options__: true, // temporary migration affordance
|
|
512
|
+
});
|
|
513
|
+
//... | importHook | importNowHook
|
|
514
|
+
await compartment.import('a'); //| called | not called
|
|
515
|
+
compartment.importNow('b'); //| not called | called
|
|
516
|
+
compartment.importNow('a'); //| not called | not called
|
|
517
|
+
compartment.importNow('c'); //| not called | not called
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
### <a name="VirtualModuleSource"></a> Virtual Module Source
|
|
388
521
|
|
|
389
522
|
To incorporate modules not implemented as JavaScript modules, third-parties may
|
|
390
|
-
implement a `
|
|
391
|
-
The
|
|
523
|
+
implement a `VirtualModuleSource` interface.
|
|
524
|
+
The object must have an `imports` array and an `execute` method.
|
|
392
525
|
The compartment will call `execute` with:
|
|
393
526
|
|
|
394
527
|
1. the proxied `exports` namespace object,
|
|
@@ -399,23 +532,27 @@ The compartment will call `execute` with:
|
|
|
399
532
|
specified `imports`.
|
|
400
533
|
|
|
401
534
|
:warning: A future breaking version may allow the `importNow` and the `execute`
|
|
402
|
-
method of
|
|
535
|
+
method of virtual module sources to return promises, to support
|
|
403
536
|
top-level await.
|
|
404
537
|
|
|
538
|
+
:warning: The virtual module source interface does not yet agree with the
|
|
539
|
+
[XS](https://www.moddable.com/hardening-xs) implementation of [Hardened
|
|
540
|
+
JavaScript](https://hardenedjs.org/).
|
|
541
|
+
|
|
405
542
|
### Compiled modules
|
|
406
543
|
|
|
407
|
-
Instead of the `
|
|
408
|
-
the SES shim uses compiled
|
|
409
|
-
These can be created with a `
|
|
410
|
-
like `@endo/
|
|
411
|
-
We omitted `
|
|
544
|
+
Instead of the `ModuleSource` constructor specified for the SES language,
|
|
545
|
+
the SES shim uses compiled module source records as a stand-in.
|
|
546
|
+
These can be created with a `ModuleSource` constructor from a package
|
|
547
|
+
like `@endo/module-source`.
|
|
548
|
+
We omitted `ModuleSource` from the SES shim because it entrains a heavy
|
|
412
549
|
dependency on a JavaScript parser.
|
|
413
|
-
The shim depends upon a `
|
|
550
|
+
The shim depends upon a `ModuleSource` constructor to analyze and
|
|
414
551
|
transform the source of a JavaScript module (known as an ESM or a `.mjs` file)
|
|
415
552
|
into a JavaScript program suitable for evaluation with `compartment.evaluate`
|
|
416
553
|
using a particular calling convention to initialize a module instance.
|
|
417
554
|
|
|
418
|
-
A compiled
|
|
555
|
+
A compiled module source record has the following shape:
|
|
419
556
|
|
|
420
557
|
- `imports` is a record that maps partial module specifiers to a list of
|
|
421
558
|
names imported from the corresponding module.
|
|
@@ -447,7 +584,7 @@ A compiled static module record has the following shape:
|
|
|
447
584
|
instead of the evaluation of the `__syncModuleProgram__` string. It will be
|
|
448
585
|
called with the initialization record described above. It is intended to be
|
|
449
586
|
used in environments where eval is not available. Sandboxing of the functor is
|
|
450
|
-
the responsibility of the author of the
|
|
587
|
+
the responsibility of the author of the ModuleSource.
|
|
451
588
|
- `__liveExportsMap__` is a record that maps import names or names in the lexical
|
|
452
589
|
scope of the module to export names, for variables that may change after
|
|
453
590
|
initialization. Any reexported name is assumed to possibly change.
|
|
@@ -472,7 +609,11 @@ not a module.
|
|
|
472
609
|
|
|
473
610
|
```js
|
|
474
611
|
const transforms = [addCodeCoverageInstrumentation];
|
|
475
|
-
const c = new Compartment({
|
|
612
|
+
const c = new Compartment({
|
|
613
|
+
globals: { console, coverage },
|
|
614
|
+
transforms,
|
|
615
|
+
__options__: true, // temporary migration affordance
|
|
616
|
+
});
|
|
476
617
|
c.evaluate('console.log("Hello");');
|
|
477
618
|
```
|
|
478
619
|
|
|
@@ -489,7 +630,7 @@ c.evaluate('console.log("Farewell, World!")', { transforms });
|
|
|
489
630
|
These transforms do not apply to modules.
|
|
490
631
|
To transform the source of a JavaScript module, the `importHook` must
|
|
491
632
|
intercept the source and transform it before passing it to the
|
|
492
|
-
`
|
|
633
|
+
`ModuleSource` constructor.
|
|
493
634
|
These are distinct because programs and modules have distinct grammar
|
|
494
635
|
productions.
|
|
495
636
|
|
|
@@ -510,8 +651,10 @@ instead of `transforms`.
|
|
|
510
651
|
|
|
511
652
|
```js
|
|
512
653
|
const __shimTransforms__ = [addCoverage];
|
|
513
|
-
const c = new Compartment({
|
|
654
|
+
const c = new Compartment({
|
|
655
|
+
globals: { console, coverage },
|
|
514
656
|
__shimTransforms__,
|
|
657
|
+
__options__: true, // temporary migration affordance
|
|
515
658
|
});
|
|
516
659
|
c.evaluate('console.log("Hello");');
|
|
517
660
|
```
|
|
@@ -618,11 +761,17 @@ abilities.
|
|
|
618
761
|
lockdown();
|
|
619
762
|
|
|
620
763
|
const promise = new Promise(resolve => {
|
|
621
|
-
const compartmentA = new Compartment(
|
|
764
|
+
const compartmentA = new Compartment({
|
|
765
|
+
globals: harden({ resolve }),
|
|
766
|
+
__options__: true, // temporary migration affordance
|
|
767
|
+
});
|
|
622
768
|
compartmentA.evaluate(programA);
|
|
623
769
|
});
|
|
624
770
|
|
|
625
|
-
const compartmentB = new Compartment(
|
|
771
|
+
const compartmentB = new Compartment({
|
|
772
|
+
globals: harden({ promise }),
|
|
773
|
+
__options__: true, // temporary migration affordance
|
|
774
|
+
});
|
|
626
775
|
compartmentB.evaluate(programB);
|
|
627
776
|
```
|
|
628
777
|
|
|
@@ -782,6 +931,7 @@ We find that library authors are generally amenable to making these small change
|
|
|
782
931
|
compatibility with any environment that protects itself from prototype pollution attacks by freezing
|
|
783
932
|
intrinsics, including `ses`.
|
|
784
933
|
|
|
934
|
+
[Hardened JavaScript]: https://hardenedjs.org/
|
|
785
935
|
[define shim]: https://en.wikipedia.org/wiki/Shim_(computing)
|
|
786
936
|
[Endo Matrix]: https://matrix.to/#/#endojs:matrix.org
|
|
787
937
|
[incompatibility tracking]: https://github.com/endojs/endo/issues/576
|