Skip to content

Commit 830fa53

Browse files
committed
fixup! make shim opt-in via process.env
1 parent d9085f6 commit 830fa53

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

packages/non-trapping-shim/README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Shim for Non-trapping Integrity Trait
1+
# Opt-in Shim for Non-trapping Integrity Trait
22

33
Emulates support for the non-trapping integrity trait from the
44
[Stabilize proposal](https://github.com/tc39/proposal-stabilize).
@@ -11,4 +11,11 @@ This package is currently organized internally as a ponyfill, and a shim based o
1111

1212
See https://github.com/endojs/endo/blob/master/packages/ses/docs/preparing-for-stabilize.md for guidance on how to prepare for the changes that will be introduced by this proposal.
1313

14-
TODO: More explanation.
14+
## Opt-in env-option `SES_NON_TRAPPING_SHIM`
15+
16+
To cope with various compat problems in linking code that uses or assumes this shim to code that does not, we have made this shim opt-in via the env-option `SES_NON_TRAPPING_SHIM`. This has two settings, `'enabled'` and the default `'disabled'`. As with all env options, this is represented at the property `process.env.SES_NON_TRAPPING_SHIM`, which typically represents the environment variable `SES_NON_TRAPPING_SHIM`. Thus, if nothing else sets `process.env.SES_NON_TRAPPING_SHIM`, you can opt-in at the shell level by
17+
```sh
18+
$ export SES_NON_TRAPPING_SHIM=enabled
19+
```
20+
21+
When not opted into, importing the shim has no effect.

packages/non-trapping-shim/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"module": "./index.js",
2121
"exports": {
2222
"./shim.js": "./shim.js",
23+
"./prepare-enable-shim.js": "./prepare-enable-shim.js",
2324
"./package.json": "./package.json"
2425
},
2526
"scripts": {
@@ -35,6 +36,9 @@
3536
"test:c8": "c8 $C8_OPTIONS ava --config=ava-nesm.config.js",
3637
"test:xs": "exit 0"
3738
},
39+
"dependencies": {
40+
"@endo/env-options": "workspace:^"
41+
},
3842
"devDependencies": {
3943
"ava": "^6.1.3",
4044
"c8": "^7.14.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* global globalThis */
2+
3+
// TODO consider adding env option setting APIs to @endo/env-options
4+
// TODO should set up globalThis.process.env if absent
5+
const env = (globalThis.process || {}).env || {};
6+
7+
env.SES_NON_TRAPPING_SHIM = 'enabled';

packages/non-trapping-shim/shim.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
/* global globalThis */
2+
import { getEnvironmentOption } from '@endo/env-options';
23
import { ReflectPlus, ObjectPlus, ProxyPlus } from './src/non-trapping-pony.js';
34

4-
globalThis.Reflect = ReflectPlus;
5+
const nonTrappingShimOption = getEnvironmentOption(
6+
'SES_NON_TRAPPING_SHIM',
7+
'disabled',
8+
['enabled'],
9+
);
510

6-
globalThis.Object = ObjectPlus;
7-
// eslint-disable-next-line no-extend-native
8-
Object.prototype.constructor = ObjectPlus;
11+
if (nonTrappingShimOption === 'enabled') {
12+
// TODO figure this out, either remove directive or change to
13+
// at-ts-expect-error.
14+
// @ts-ignore type of ReflectPlus vs Reflect, I think
15+
globalThis.Reflect = ReflectPlus;
916

10-
globalThis.Proxy = ProxyPlus;
17+
globalThis.Object = ObjectPlus;
18+
// eslint-disable-next-line no-extend-native
19+
Object.prototype.constructor = ObjectPlus;
20+
21+
globalThis.Proxy = ProxyPlus;
22+
}

packages/non-trapping-shim/test/non-trapping-shim.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../prepare-enable-shim.js';
12
// Uses 'ava' rather than @endo/ses-ava to avoid worries about cyclic
23
// dependencies. We will need similar tests is higher level packages, in order
34
// to test compat with ses and ses-ava.

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ __metadata:
706706
version: 0.0.0-use.local
707707
resolution: "@endo/non-trapping-shim@workspace:packages/non-trapping-shim"
708708
dependencies:
709+
"@endo/env-options": "workspace:^"
709710
ava: "npm:^6.1.3"
710711
c8: "npm:^7.14.0"
711712
tsd: "npm:^0.31.2"

0 commit comments

Comments
 (0)