Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ses): Replace Rollup with Endo bundle #715

Merged
merged 2 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import test from 'tape-promise/tape';
import path from 'path';

const runBrowserTests = async (t, indexFile) => {
const browser = await puppeteer.launch();
const browser = await puppeteer.launch({
// debug:
// { headless: false }
});

let numTests;
let numPass;
Expand Down Expand Up @@ -62,7 +65,7 @@ const testBundler = (bundlerName, indexFile) => {
t.notEqual(numTests, undefined);
t.equal(numTests, numPass);
})
.catch(e => t.isNot(e, e, 'unexpected exception'))
.catch(e => t.fail(`Unexpected exception ${e}`))
.finally(() => t.end());
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>Test</title>
<meta charset="utf8">
</head>
<body>
<script src="../../../ses/dist/ses.umd.js"></script>
Expand Down
7 changes: 7 additions & 0 deletions packages/ses/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ User-visible changes in SES:
The constructor must perform the module analysis and transform the source,
and present this duck-type to the Compartment `importHook`.
This relieves SES of a dependency on Babel and simplifies its API.
- *BREAKING CHANGE* The UMD distribution of SES must have the UTF-8 charset.
The prior versions were accidentally ASCII, so SES would have worked
in any web page, regardless of the charset.
To remedy this, be sure to include `<head><meta charset="utf-8"></head>` in
the containing page (a general best-practice for web pages) or specifically
use `<script charset="utf-8" src="ses.umd.min.js">` to address the single
file.
- Relaxes the censorship of `import` and `eval` in programs evaluated
under SES to specifically allow the use of `something.import()` or
`something.eval()` methods.
Expand Down
7 changes: 7 additions & 0 deletions packages/ses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ npm install ses

## Usage

SES is a shim that can be run in most engines, either as an ESM module `ses` or
as a `<script>` tag.
For a script tag, the content encoding charset must be UTF-8, either by virtue
of `<head><meta charset="utf-8"></head>` (a general best practice for all HTML
files) or specifically `<script src="node_modules/ses/dist/ses.umd.min.js"
charset="utf-8">`.

### Lockdown

SES introduces the `lockdown()` function.
Expand Down
6 changes: 4 additions & 2 deletions packages/ses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
"test": "yarn build && yarn ava",
"qt": "yarn ava",
"test:platform-compatability": "node test/package/test.cjs",
"build": "rollup --config rollup.config.js && (cd dist; cp ses.cjs lockdown.cjs; cp ses.mjs lockdown.mjs; cp ses.umd.js lockdown.umd.js; cp ses.umd.min.js lockdown.umd.min.js)",
"build": "mkdir -p dist && node scripts/bundle.js",
"demo": "http-server -o /demos"
},
"dependencies": {
"@agoric/make-hardener": "^0.1.2"
},
"devDependencies": {
"@agoric/babel-standalone": "^7.9.5",
"@agoric/compartment-mapper": "^0.2.4",
"@agoric/eslint-config": "^0.1.0",
"@agoric/test262-runner": "~0.1.0",
"@endo/static-module-record": "^0.1.0",
Expand All @@ -66,7 +67,8 @@
"http-server": "^0.12.1",
"prettier": "^1.19.1",
"rollup-plugin-terser": "^5.1.3",
"sinon": "8.0.4"
"sinon": "8.0.4",
"terser": "^4.8.0"
},
"ava": {
"files": [
Expand Down
38 changes: 0 additions & 38 deletions packages/ses/rollup.config.js

This file was deleted.

37 changes: 37 additions & 0 deletions packages/ses/scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import '../index.js';
import fs from 'fs';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should adopt some convention that we can check, for making sources of static authority stand out in our source files. Just noting. No change suggested at this time. Attn @dckc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be a script that we run with Endo instead of Node, and it should be counted a design goal for Endo to accommodate injection of an attenuated FS and a decent DX for granting that capability to the script. For example, there might be some reasonable default filesystem attenuation for any Endo script run in the context of developer tools for a package, or at least a convenient piece of boilerplate we can add to package.json to grant that limited authority.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"DX"?

import { makeBundle } from '@agoric/compartment-mapper';
import terser from 'terser';

const resolve = (rel, abs) => new URL(rel, abs).toString();
const root = resolve('..', import.meta.url);

const read = async location => fs.promises.readFile(new URL(location).pathname);
const write = async (target, content) => {
const location = resolve(target, root);
await fs.promises.writeFile(new URL(location).pathname, content);
};

(async () => {
const bundle = await makeBundle(
read,
resolve('../index.js', import.meta.url),
);
const { code: terse } = terser.minify(bundle, {
mangle: false,
keep_classnames: true,
});

console.log(`Bundle size: ${bundle.length} bytes`);
console.log(`Minified bundle size: ${terse.length} bytes`);

await write('dist/ses.cjs', bundle);
await write('dist/ses.mjs', bundle);
await write('dist/ses.umd.js', bundle);
await write('dist/ses.umd.min.js', terse);

await write('dist/lockdown.cjs', bundle);
await write('dist/lockdown.mjs', bundle);
await write('dist/lockdown.umd.js', bundle);
await write('dist/lockdown.umd.min.js', terse);
Comment on lines +28 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that there are only two, but with eight names, is amusing. No change suggested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. This is the effect of architectural drift in this case. Before I changed the SES shim to effect its API strictly using globals, there was a difference between the CJS, MJS, and UMD treatments. Rollup continued to emphasize the difference in its configuration options, but the difference amounted to nothing except a vestigial SES global introduced by the UMD version. The lockdown versions were of course briefly different. We’re retaining all of these only to ease the migration path. In the end, we really only need ses.cjs (with the CJS extension strictly to navigate RESM to NESM portability) and ses.umd.min.js.

})();