nodebox 0.0.29
Install from the command line:
Learn more about npm packages
$ npm install @codesandbox/nodebox@0.0.29
Install via package.json:
"@codesandbox/nodebox": "0.0.29"
About this version
npm install @codesandbox/node-emulator
import { NodeEmulator } from '@codesandbox/node-emulator';
async function usage() {
const emulator = new NodeEmulator('https://...', {
iframe: document.getElementById('my-frame'),
});
await emulator.connect();
}
See the full list of API for more methods.
Connects to a deployed Node emulator instance. Returns a Promise once the connection is established and the emulator iframe has established a message channel with the consumer.
await emulator.connect();
Populates the File system with the initial state of files. Returns a Promise once the files are populated and the worker is finished.
await emulator.fs.init({
// Each key in this object is a relative path to the file,
// and each value if an ArrayBuffer of the file's contents.
'/src/index.js': new Uint8Array([1, 2, 3])
}
It reads asynchronously a content file.
await emulator.fs.readFile("/index.js").then(value => value.toString())
await emulator.fs.readFile("/index.js", "utf8")
It writes asynchronously a content to a file, which will replace the file if it already exists.
await emulator.fs.writeFile("/index.js", new Uint8Array([1, 2, 3]))
await emulator.fs.writeFile("/index.js", "Hello World", "utf8")
It asynchronously watches for changes on a file or a directory given a Glob matcher. Each change event it returns the filename and an event type that could be: rename, delete, create or change.
await emulator.fs.watch("/**/*", (evt) => {
switch (evt.type) {
case 'create':
case 'remove':
case 'change':
console.log(evt.type, evt.path);
break;
case 'rename':
console.log(evt.type, evt.oldPath, evt.newPath);
break;
case 'close':
console.log('Watcher closed');
break;
}
})
It creates a new ShellProcess
, which can perform some actions in this shell.
const shellProcess = channel.shell.create();
Executes a given command. Returns a Promise that resolves to an object with the shell information representing a shell process running in the worker.
const { id: shellId } = await shellProcess.runCommand('node', ['index.js']);
It returns running
when a worker is running (but not necessarily a preview opened) and idle
when the process has been exited or restarted (but running
after successfully initializing again).
It terminates the Worker and closes the preview port.
await shellProcess.runCommand('node', ['src/index.js']);
await shellProcess.exit()
It returns an URL preview for a given port. The second argument is a timeout value, which will trigger rejecting the promise if the port fails (it defaults to 10s).
const { url, sourceShellId } = await emulator.preview.waitForPort(3000)
It returns an URL preview for a given shell id. The second argument is a timeout value, which will trigger rejecting the promise if the port fails (it defaults to 10s).
const { id } = await emulator.shell.runCommand('node', ['src/index.js']);
const { url, port } = await emulator.preview.getByShellId(id)
At the moment, Node emulator only supports evaluating JavaScript modules.