-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cosmic-swingset): new
scripts/clean-core-eval.js
- Loading branch information
1 parent
e1c48b9
commit b2b0b5a
Showing
7 changed files
with
143 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#! /usr/bin/env node | ||
import '@endo/init'; | ||
import * as farExports from '@endo/far'; | ||
import { isEntrypoint } from '../src/is-entrypoint.js'; | ||
|
||
export const compartmentEvaluate = code => { | ||
// const permit = true; | ||
// const powers = {}; | ||
const modules = {}; // TODO | ||
|
||
// Inspired by ../repl.js: | ||
const globals = harden({ | ||
...modules, | ||
...farExports, | ||
assert, | ||
console: { | ||
// Ensure we don't pollute stdout. | ||
debug: console.warn, | ||
log: console.warn, | ||
info: console.warn, | ||
warn: console.warn, | ||
error: console.error, | ||
}, | ||
}); | ||
|
||
// Evaluate the code in the context of the globals. | ||
const compartment = new Compartment(globals); | ||
harden(compartment.globalThis); | ||
return compartment.evaluate(code); | ||
}; | ||
|
||
export const htmlStartCommentPattern = new RegExp(`(${'<'})(!--)`, 'g'); | ||
export const htmlEndCommentPattern = new RegExp(`(--)(${'>'})`, 'g'); | ||
export const importPattern = new RegExp( | ||
'(^|[^.])\\bimport(\\s*(?:\\(|/[/*]))', | ||
'g', | ||
); | ||
|
||
// Neutralize HTML comments and import expressions. | ||
export const defangEvaluableCode = code => | ||
code | ||
.replace(importPattern, '$1import\\$2') // avoid SES_IMPORT_REJECTED | ||
.replace(htmlStartCommentPattern, '$1\\$2') // avoid SES_HTML_COMMENT_REJECTED | ||
.replace(htmlEndCommentPattern, '$1\\$2'); // avoid SES_HTML_COMMENT_REJECTED | ||
|
||
export const main = async (argv, { readFile, stdout }) => { | ||
const [_node, _script, fn] = argv; | ||
if (fn === undefined) { | ||
console.error(`Usage: ${_script} <filename>`); | ||
process.exit(1); | ||
} | ||
const text = await readFile(fn, 'utf-8'); | ||
const clean = defangEvaluableCode(text); | ||
const withURL = `${clean}\n//# sourceURL=${fn}\n`; | ||
|
||
// end-of-line whitespace disrupts YAML formatting | ||
const trimmed = withURL.replace(/[\r\t ]+$/gm, ''); | ||
compartmentEvaluate(trimmed); | ||
stdout.write(trimmed); | ||
}; | ||
|
||
if (isEntrypoint(import.meta.url)) { | ||
/* global process */ | ||
main([...process.argv], { | ||
readFile: (await import('fs/promises')).readFile, | ||
stdout: process.stdout, | ||
}).catch(err => { | ||
process.exitCode = 1; | ||
console.error(err); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Detect if this is run as a script. | ||
import url from 'url'; | ||
import process from 'process'; | ||
|
||
// FIXME: Should maybe be exported by '@endo/something'? | ||
export const isEntrypoint = href => | ||
String(href) === url.pathToFileURL(process.argv[1] ?? '/').href; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import '@endo/init'; | ||
import test from 'ava'; | ||
import { | ||
defangEvaluableCode, | ||
compartmentEvaluate, | ||
} from '../scripts/clean-core-eval.js'; | ||
|
||
test('defangEvaluableCode is working', t => { | ||
const samples = [ | ||
[''], | ||
[ | ||
`\n// <!-- foo\n\n`, | ||
`\n// <\\!-- foo\n\n`, | ||
{ message: /\(SES_HTML_COMMENT_REJECTED\)/ }, | ||
], | ||
[ | ||
`\n// --> foo\n\n`, | ||
`\n// --\\> foo\n\n`, | ||
{ message: /\(SES_HTML_COMMENT_REJECTED\)/ }, | ||
], | ||
[ | ||
`\n\`import('foo')\`\n`, | ||
`\n\`import\\('foo')\`\n`, | ||
{ message: /\(SES_IMPORT_REJECTED\)/ }, | ||
], | ||
[ | ||
`\n123; import('foo')\n`, | ||
`\n123; import\\('foo')\n`, | ||
{ message: /\(SES_IMPORT_REJECTED\)/ }, | ||
{ message: /Invalid or unexpected token/ }, | ||
], | ||
]; | ||
for (const [ | ||
code, | ||
transformed, | ||
originalExpected, | ||
defangedExpected, | ||
] of samples) { | ||
const defanged = defangEvaluableCode(code); | ||
if (originalExpected) { | ||
// Transform is necessary, and the original code throws. | ||
t.is(defanged, transformed); | ||
t.throws(() => compartmentEvaluate(code), originalExpected); | ||
} else { | ||
// No transform is necessary, and the code doesn't throw. | ||
t.is(defanged, code); | ||
t.notThrows(() => compartmentEvaluate(code)); | ||
} | ||
if (defangedExpected) { | ||
t.throws(() => compartmentEvaluate(defanged), defangedExpected); | ||
} else { | ||
// The transformed code doesn't throw. | ||
t.notThrows(() => compartmentEvaluate(defanged)); | ||
} | ||
} | ||
}); |
This file was deleted.
Oops, something went wrong.