Skip to content
nodebox / 0.0.37

nodebox 0.0.37

Install from the command line:
Learn more about npm packages
$ npm install @codesandbox/nodebox@0.0.37
Install via package.json:
"@codesandbox/nodebox": "0.0.37"

About this version

Node emulator

Getting started

Install

npm install @codesandbox/nodebox

Connect to the emulator instance

import { Nodebox } from '@codesandbox/nodebox';

async function usage() {
  const emulator = new Nodebox('https://...', {
    iframe: document.getElementById('my-frame'),
  });

  await emulator.connect();
}

See the full list of API for more methods.


API

connect()

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();

FS

fs.init()

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])
}

fs.readFile()

It reads asynchronously a content file.

await emulator.fs.readFile('/index.js').then((value) => value.toString());

await emulator.fs.readFile('/index.js', 'utf8');

fs.writeFile()

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');

fs.watch()

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;
  }
});

shell

shell.create()

It creates a new ShellProcess, which can perform some actions in this shell.

const shellProcess = channel.shell.create();

ShellProcess

shell.runCommand()

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']);
ShellProcess.state

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).

ShellProcess.kill

It terminates the Worker and closes the preview port.

await shellProcess.runCommand('node', ['src/index.js']);

await shellProcess.kill();

preview

preview.waitForPort()

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);

preview.getByShellId()

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.

Details


Assets

  • nodebox-0.0.37.tgz

Download activity

  • Total downloads 97
  • Last 30 days 0
  • Last week 0
  • Today 0