-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
- require() pauses the current computation, finds and loads the file with a new AsyncRun, then resumes with the result - makeRequireAsync() returns a promise to deal with the delayed/async result - Everything gets stopified, no control over stopifying some modules and not others, or some functions and not others
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
"sha.js": "^2.4.8", | ||
"source-map": "^0.5.6", | ||
"stacktrace-js": "^1.3.1", | ||
"stopify": "^0.5.4-elementaryJS", | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jpolitz
Author
Member
|
||
"strip-ansi": "^4.0.0", | ||
"tsify": "^4.0.1", | ||
"websocket": "^1.0.23", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,72 @@ | ||
const assert = require('assert'); | ||
const browserFS = window['BrowserFS'].BFSRequire('fs'); | ||
const path = window['BrowserFS'].BFSRequire('path'); | ||
const stopify = require('stopify'); | ||
window['stopify'] = stopify; | ||
|
||
const nodeModules = { | ||
'assert': assert | ||
}; | ||
|
||
function makeRequireAsync(basePath : string) { | ||
let cwd = basePath; | ||
let currentRunner = null; | ||
|
||
function requireAsyncMain(importPath : string) { | ||
return new Promise(function (resolve, reject) { | ||
if(importPath in nodeModules) { | ||
return nodeModules[importPath]; | ||
} | ||
const oldWd = cwd; | ||
const nextPath = path.join(cwd, importPath); | ||
cwd = path.parse(nextPath).dir; | ||
if(!browserFS.existsSync(nextPath)) { | ||
throw new Error("Path did not exist in requireSync: " + nextPath); | ||
} | ||
const contents = String(browserFS.readFileSync(nextPath)); | ||
const runner = stopify.stopifyLocally("(function() { " + String(contents) + "})()"); | ||
const module = {exports: false}; | ||
runner.g = { stopify, require: requireAsync, module }; | ||
runner.path = nextPath; | ||
currentRunner = runner; | ||
runner.run((result) => { | ||
cwd = oldWd; | ||
const toReturn = module.exports ? module.exports : result; | ||
resolve(toReturn); | ||
}); | ||
}); | ||
} | ||
|
||
function requireAsync(importPath : string) { | ||
if(importPath in nodeModules) { | ||
return nodeModules[importPath]; | ||
} | ||
const oldWd = cwd; | ||
const nextPath = path.join(cwd, importPath); | ||
cwd = path.parse(nextPath).dir; | ||
if(!browserFS.existsSync(nextPath)) { | ||
throw new Error("Path did not exist in requireSync: " + nextPath); | ||
} | ||
const contents = String(browserFS.readFileSync(nextPath)); | ||
const runner = stopify.stopifyLocally("(function() { " + String(contents) + "})()"); | ||
This comment has been minimized.
Sorry, something went wrong.
arjunguha
Member
|
||
const module = {exports: false}; | ||
runner.g = { stopify, require: requireAsync, module, console }; | ||
runner.path = nextPath; | ||
currentRunner.pauseImmediate(() => { | ||
const oldRunner = currentRunner; | ||
currentRunner = runner; | ||
runner.run((result) => { | ||
cwd = oldWd; | ||
const toReturn = module.exports ? module.exports : result.value; | ||
currentRunner = oldRunner; | ||
oldRunner.continueImmediate({ type: 'normal', value: toReturn }); | ||
}); | ||
}); | ||
} | ||
|
||
return requireAsyncMain; | ||
} | ||
|
||
function makeRequire(basePath : string) { | ||
var cwd = basePath; | ||
/* | ||
|
@@ -40,5 +101,4 @@ function makeRequire(basePath : string) { | |
return requireSync; | ||
} | ||
|
||
window['makeRequire'] = makeRequire; | ||
module.exports = { makeRequire }; | ||
module.exports = { makeRequire, makeRequireAsync }; |
1 comment
on commit d4df431
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here’s the issue Arjun’s talking about for reference: nuprl/Stopify#418
There is a more recent release (as of 2 weeks ago) that has all the changes on this hacked up branch, which are now in master.