npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

browser-host

v0.0.5

Published

The **browser-host** utility creates a browser environment for executing code. Something that you miss to test browser specific things. For example, to test `fetch`, `AbortController` and other things.

Downloads

15

Readme

browser-host

The browser-host utility creates a browser environment for executing code. Something that you miss to test browser specific things. For example, to test fetch, AbortController and other things.

Example

write a test:

const test = require('tape');

test('equality test', function (t) {
  t.plan(1);
  t.equal(1, 1);
});

run your test in a headless browser:

$ browserify test.js | browser-host

TAP version 13
# equality test
ok 1 should be equal

1..1
# tests 1
# pass  1

# ok

Note that if you use any external modules, probably you need to bundle your test file via browserify or any other bundler first in order to run its contents in the browser environment.

Usage

$ browser-host [entry] [options]

Description:

  The browser-host utility reads file and executes its contents (javascript code)
  in the browser environment. The environment is created
  by headless chrome browser (via puppeteer). If file is a signle dash ('-')
  or absent, browser-host reads from the standard input.

Options:

  -h, --help      print usage
  -b, --browser   run Chrome / Chromium browser in non-headless mode
  --html          print resulting html only
  --mock          path to module to handle requests for mocking a dynamic back-end

Examples:

  $ echo "console.log('hello, world!')" | browser-host
  $ echo "console.log('hello, world!')" > a.js; browser-host a.js

Dynamic backend mock

Using --mock mock.js you can add a custom server-side implementation to provide a stable behavior for your requests. All paths should start from /mock.

mock.js should be a plain commonjs module and export a function that accepts req and res arguments. Note that browser-host uses a plain http module to create a server.

Example:

module.exports = function mock(req, res) {
  const pathname = req.url.split('?').shift();

  if (pathname === '/mock/weather') {
    const r = JSON.stringify({ weather: 'is ok' });

    res.writeHead(200, { 'content-type': 'application/json' });
    res.write(r);
    res.end();
    return;
  }

  res.writeHead(404, { 'content-type': 'text/plain' });
  res.write('404 not found');
  res.end();
}

Install

Install via package manager (npm or yarn):

yarn add browser-host --dev

License

MIT License

Copyright (c) 2019 Aleksey Litvinov