Socket
Socket
Sign inDemoInstall

bx

Package Overview
Dependencies
391
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bx

Command line tool to run JavaScript, TypeScript or HTML files in the browser.


Version published
Weekly downloads
3
decreased by-82.35%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

🌐 bx - a browser runner CI

With Node.js, Deno or Bun there are so many JavaScript environments to choose from. However, nothing is as good as the browser environment. bx gives you an execution runtime for the browser.

Install

No install needed, just run it directly via npx, e.g.:

npx bx "console.log(navigator.userAgent)"

Usage

With bx you can easily run scripts (JS or TS) within different browser environments:

> echo "console.log(navigator.userAgent)" &> script.js
> npx bx ./script.js
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

You can easily switch browsers via the --browserName parameter:

> npx bx ./script.js --browserName firefox
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0

It even allows you to run .html files, e.g. given this file:

<script type="module">
  console.log(document.querySelector("b").textContent);
</script>
<b>Hello World!</b>

Running this with bx results in:

> npx bx ./html.html
Hello World!

Run Programmatically

You can also run bx programmatically, e.g. to hydrate components within the browser. For example, to hydrate a Lit component through a Koa server, you can run this script:

import path from "node:path";
import Koa from "koa";

import { run } from "bx";

const __dirname = path.dirname(new URL(import.meta.url).pathname);
const app = new Koa();

app.use(async (ctx) => {
  if (ctx.path === "/favicon.ico") {
    return;
  }

  ctx.body = await run(async () => {
    /**
     * runs in the browser
     */
    const { render } = await import("@lit-labs/ssr");
    const { html } = await import("lit");
    await import("./component.ts");

    const dom = await render(html`<simple-greeting></simple-greeting>`);
    return Array.from(dom).join("\n");
  }, {
    browserName: "chrome",
    rootDir: __dirname,
  });
});

app.listen(3000);
console.log("Server running at http://localhost:3000/");

Session Management

If you like to speed up your execution, you can create browser sessions on your system and run scripts through them immediately without having to spin up the browser. You can create a session via:

# create a session with random session name, e.g. "chrome-1"
npx bx session --browserName chrome
# create a session with custom name
npx bx session --browserName chrome --name chrome

You can now run scripts faster by providing a session name:

npx bx ./script.ts --sessionName chrome

To view all opened sessions, run:

npx bx session

Kill specific or all sessions via:

npx bx session --kill chrome
npx bx session --killAll

Keywords

FAQs

Last updated on 26 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc