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

feat(endo-exec): new package #1225

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@
"@endo/promise-kit": "^0.2.53",
"@endo/where": "^0.2.11",
"commander": "^5.0.0",
"endo-exec": "^0.1.0",
"import-meta-resolve": "^2.0.3",
"ses": "^0.18.1"
},
"devDependencies": {
20 changes: 19 additions & 1 deletion packages/cli/src/endo.js
Original file line number Diff line number Diff line change
@@ -9,8 +9,9 @@ import fs from 'fs';
import path from 'path';
import url from 'url';
import crypto from 'crypto';
import { spawn } from 'child_process';
import { fork, spawn } from 'child_process';
import os from 'os';
import { moduleResolve } from 'import-meta-resolve';

import { Command } from 'commander';
import { makePromiseKit } from '@endo/promise-kit';
@@ -89,6 +90,23 @@ export const main = async rawArgs => {
process.stdout.write(`${cachePath}\n`);
});

program
.command('exec -- <script> [args...]')
.action(async (script, args, _cmd) => {
const u = moduleResolve('endo-exec/endo-exec.cjs', import.meta.url);
const endoExec = url.fileURLToPath(u);
return new Promise((resolve, reject) => {
const cp = fork(endoExec, [script, ...args], {
stdio: 'inherit',
});
cp.on('error', reject);
cp.on('close', code => {
process.exitCode = code;
resolve(code);
});
});
});

program.command('start').action(async _cmd => {
await start();
});
43 changes: 43 additions & 0 deletions packages/endo-exec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Endo Exec

Importing `endo-exec` declares that if module executed directly as a script, it
will run in a Hardened JS start compartment. It opts for compatibility with
legacy code when possible.

NOTE: if you want precise control over how Endo is initialized, especially if
you are writing an application that needs to use mixed Compartments (both
trusted libraries and untrusted code), you should NOT use this package.
Instead, have a look at `@endo/init`.

Import `endo-exec` like:

```js
#! /usr/bin/env node
import 'endo-exec';

console.log('Hello, Endo world!');

Object.prototype.monkeyPatch = 53;
// (TypeError#1)
// TypeError#1: Cannot add property monkeyPatch, object is not extensible
// at main (file:///Users/michael/agoric/endo/packages/syrup/a.js:6:32)
// at /Users/michael/agoric/endo/packages/endo-exec/endo-exec.cjs:22:15
```

Or if you want to have your script be importable without side-effects (ocap
discipline), then avoid top-level module state and export a `main` entrypoint
that can be executed on demand.

```js
#! /usr/bin/env node
import 'endo-exec';

import { promises as fs } from 'fs';

/** @type {import('endo-exec').Main} */
export const main = async ([script, file]) => {
console.log('Hello from', script);
await fs.readFile(file);
console.log(`Here's the file`, file);
};
```
4 changes: 4 additions & 0 deletions packages/endo-exec/bin/endo-exec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /usr/bin/env bash
real0=$(readlink -f "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")
thisdir=$(cd "$(dirname -- "$real0")/.." >/dev/null && pwd -P)
exec node "$thisdir/endo-exec.cjs" ${1+"$@"}
1 change: 1 addition & 0 deletions packages/endo-exec/bin/endo-exec.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@node %~dp0\..\endo-exec.cjs %*
32 changes: 32 additions & 0 deletions packages/endo-exec/endo-exec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node
(async () => {
// We use the legacy mode for maximum compatibility.
await import('@endo/init/legacy.js');

// Trim off the Node.js interpreter name.
const [_nodeJS, endoExec, ...args] = process.argv;

const script = endoExec.endsWith('endo-exec.cjs') ? args.shift() : endoExec;
assert(script, `Usage: ${endoExec} SCRIPT [ARGS...]`);

const url = await import('url');
const mod = new URL(script, url.pathToFileURL('./')).href;

// Execute the `main` import if there is one.
const { main } = await import(mod);
let resultP;
if (typeof main === 'function') {
resultP = main(harden([script, ...args]));
}

const result = await resultP;
if (Number.isSafeInteger(result)) {
// Specify an exit code.
process.exitCode = result;
}
})().catch(error => {
console.error(error);
if (process.exitCode === 0) {
process.exitCode = 1;
}
});
5 changes: 5 additions & 0 deletions packages/endo-exec/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Ensure endo is initialized synchronously.
import '@endo/init/legacy.js';

// Asynchronously run the exported main function.
import './endo-exec.cjs';
37 changes: 37 additions & 0 deletions packages/endo-exec/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "endo-exec",
"version": "0.4.0",
"description": "Endo script executor",
"keywords": [],
"author": "Endo contributors",
"license": "Apache-2.0",
"homepage": "https://github.com/endojs/endo/tree/master/packages/cli#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/endojs/endo.git"
},
"bugs": {
"url": "https://github.com/endojs/endo/issues"
},
"type": "module",
"exports": {
".": "./index.js",
"./endo-exec.cjs": "./endo-exec.cjs"
},
"main": "./endo-exec.cjs",
"bin": "./bin/endo-exec",
"scripts": {
"build": "exit 0",
"lint": "exit 0",
"lint-fix": "exit 0",
"test": "exit 0"
},
"dependencies": {
"@endo/init": "^0.5.42",
"global": "^4.4.0"
},
"devDependencies": {},
"publishConfig": {
"access": "public"
}
}
2 changes: 2 additions & 0 deletions packages/endo-exec/scripts/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /usr/bin/env endo-exec
console.log('Hello, Endo world!');
7 changes: 7 additions & 0 deletions packages/endo-exec/scripts/zx-head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#! /usr/bin/env endo-exec
import 'zx/globals';

export const main = async ([script, file]) => {
echo`Hello, world (from ${script})!`;
await $`head -5 ${file}`;
};
36 changes: 35 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -4916,6 +4916,11 @@ dom-serializer@^1.0.1:
domhandler "^4.2.0"
entities "^2.0.0"

dom-walk@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.2.tgz#0c548bef048f4d1f2a97249002236060daa3fd84"
integrity sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==

domain-browser@^1.1.1, domain-browser@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda"
@@ -6381,6 +6386,14 @@ global-prefix@^3.0.0:
kind-of "^6.0.2"
which "^1.3.1"

global@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/global/-/global-4.4.0.tgz#3e7b105179006a323ed71aafca3e9c57a5cc6406"
integrity sha512-wv/LAoHdRE3BeTGz53FAamhGlPLhlssK45usmGFThIi4XqnBmjKQ16u+RNbP7WvigRZDxUsM0J3gcQ5yicaL0w==
dependencies:
min-document "^2.19.0"
process "^0.11.10"

globals@^11.1.0:
version "11.12.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
@@ -6883,6 +6896,13 @@ import-local@^3.0.2:
pkg-dir "^4.2.0"
resolve-cwd "^3.0.0"

import-meta-resolve@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/import-meta-resolve/-/import-meta-resolve-2.0.3.tgz#117f3dc9a6415e82c32530545e1cf91be5c0a5d1"
integrity sha512-fpAppnBpZ3ymQ/dPP97TNsco1HB5+V9SYJ3chY50PP8xn4U/w+Y6ovWBmTImB/prmGsTjzPh8pQYY+EVBlr9mw==
dependencies:
builtins "^5.0.0"

imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
@@ -8480,6 +8500,13 @@ mimic-fn@^4.0.0:
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-4.0.0.tgz#60a90550d5cb0b239cca65d893b1a53b29871ecc"
integrity sha512-vqiC06CuhBTUdZH+RYl8sFrL096vA45Ok5ISO6sE/Mr1jRbGH4Csnhi8f3wKVl7x8mO4Au7Ir9D3Oyv1VYMFJw==

min-document@^2.19.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/min-document/-/min-document-2.19.0.tgz#7bd282e3f5842ed295bb748cdd9f1ffa2c824685"
integrity sha512-9Wy1B3m3f66bPPmU5hdA4DR4PB2OfDU/+GS3yAB7IQozE3tqXaVv2zOjgla7MEGSRv95+ILmOuvhLkOK6wJtCQ==
dependencies:
dom-walk "^0.1.0"

min-indent@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
@@ -11192,7 +11219,14 @@ semver@^6.0.0, semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.0.0, semver@^7.1.1, semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8:
semver@^7.0.0, semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.7:
version "7.3.7"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==
dependencies:
lru-cache "^6.0.0"

semver@^7.1.1, semver@^7.3.5, semver@^7.3.8:
version "7.3.8"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798"
integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==