Giter VIP home page Giter VIP logo

browser-run's Introduction

browser-run

The easiest way of running code in a browser environment.

Bundles electronjs by default!

CI downloads

Usage

$ echo "console.log('Hey from ' + location); window.close()" | browser-run
Hey from http://localhost:53227/
$

Or use browser-run programmatically:

var run = require('browser-run');

var browser = run();
browser.pipe(process.stdout);
browser.end('console.log(location); window.close()');

Example with browserify

$ browserify main.js | browser-run

or

var browserify = require('browserify');
var browser = require('browser-run');

browserify('main.js').bundle().pipe(browser()).pipe(process.stdout);

CLI

$ browser-run --help
Run JavaScript in a browser.
Write code to stdin and receive console output on stdout.
Usage: browser-run [OPTIONS]

Options:
      --version  Show version number                                   [boolean]
  -b, --browser  Browser to use. Always available: electron. Available if
                 installed: chrome, firefox, ie, safari    [default: "electron"]
      --sandbox  Enable electron sandbox               [boolean] [default: true]
      --basedir  Set this if you need to require node modules in node mode
  -h, --help     Print help                                            [boolean]
  -p, --port     Starts listening on that port and waits for you to open a
                 browser
  -s, --static   Serve static assets from this directory
  -m, --mock     Path to code to handle requests for mocking a dynamic back-end
  -i, --input    Input type. Defaults to 'javascript', can be set to 'html'.
  -n, --node     Enable nodejs apis in electron

Custom html file

By using --input html or { input: 'html' } you can provide a custom html file for browser-run to use. Keep in mind though that it always needs to have <script src="/reporter.js"></script> above other script tags so browser-run is able to properly forward your console.logs etc to the terminal.

Dynamic back-end mock

By using --mock mock.js or { mock: 'mock.js'} you can provide a custom server-side implementation and handle all requests that are sent to paths beginning with /mock

mock.js needs to export a function that accepts req and res arguments for handling requests.

Example:

module.exports = function(req,res){
  if (req.url === '/mock/echo') {
    req.pipe(res)
  }
}

API

run([opts])

Returns a duplex stream and starts a webserver.

opts can be:

  • port: If speficied, no browser will be started, so you can point one yourself to http://localhost/<port>
  • browser: Browser to use. Defaults to electron. Available if installed:
    • chrome
    • firefox
    • ie
    • safari
  • static: Serve static files from this directory
  • mock: Path to code to handle requests for mocking a dynamic back-end
  • input: Input type. Defaults to javascript, can be set to html.
  • node: Enable nodejs integration in electron
  • sandbox: Enable electron sandbox. Default: true.
  • basedir: Set this if you need to require node modules in node mode

If only an empty string is written to it, an error will be thrown as there is nothing to execute.

If you call window.close() inside the script, the browser will exit.

run#stop()

Stop the underlying webserver.

Headless testing

In environments without a screen, you can use Xvfb to simulate one.

GitHub Actions

This is a full example to run npm test. Refer to the last 2 lines in the YAML config:

on:
  - pull_request
  - push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - run: npm install
    - run: xvfb-run npm test
      timeout-minutes: 5 # If the tests fails, the browser will hang open indefinitely

Travis

Add this to your travis.yml:

addons:
  apt:
    packages:
      - xvfb
install:
  - export DISPLAY=':99.0'
  - Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
  - npm install

Full example.

Any gnu/linux box

$ sudo apt-get install xvfb # or equivalent
$ export DISPLAY=':99.0'
$ Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
$ browser-run ...

Docker

There is also an example Docker image. Source

Installation

With npm do

$ npm install browser-run    # for library
$ npm install -g browser-run # for cli

Sponsors

This module is proudly supported by my Sponsors!

Do you want to support modules like this to improve their quality, stability and weigh in on new features? Then please consider donating to my Patreon. Not sure how much of my modules you're using? Try feross/thanks!

License

(MIT)

browser-run's People

Contributors

apaleslimghost avatar btakita avatar cezaraugusto avatar dependabot[bot] avatar eklem avatar fanatid avatar fregante avatar gomesalexandre avatar greenkeeper[bot] avatar greenkeeperio-bot avatar jcblw avatar jgravois avatar juliangruber avatar kumavis avatar naugtur avatar peteruithoven avatar rtsao avatar rubennorte avatar zeke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

browser-run's Issues

stack traces & source maps?

Is there a way to have stack traces that correspond with your source files?

Has anybody successfully used source maps with browser-run?

TypeError: Cannot call method 'pipe' of undefined

TypeError: Cannot call method 'pipe' of undefined
    at /Users/damon/Workspace/base-api-client/node_modules/tape-run/node_modules/browser-run/index.js:91:55
    at process._tickCallback (node.js:442:13)
    at Function.Module.runMain (module.js:499:11)
    at startup (node.js:119:16)
    at node.js:929:3

Encountered this with OS X, Node v0.10.36, NPM 2.14., PhantomJS 1.9.8. Occurs with default "electron" too.

It looks like the error is on line 91, where browser.setEncoding('utf8') doesn't return browser:

if (browser.pipe) browser.setEncoding('utf8').pipe(output);

`browser-launcher` is no longer maintained

substack hasn't updated browser-launcher for 2 years; bugs aren't getting fixed. So I can't run this, for example, with Chrome on Windows.

I suggest using another package such as browser-launcher2 instead.

Consider removing electron

Hi, browser-run is awesome – thanks for building it!
I have a request, would it be possible to remove the default browser and make the package smaller? Electron alone is ~41MB and not everyone needs it.

An in-range update of source-map-support is breaking the build 🚨

Version 0.4.13 of source-map-support just got published.

Branch Build failing 🚨
Dependency source-map-support
Current Version 0.4.12
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

As source-map-support is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪


Status Details
  • dependency-ci Dependencies checked Details

  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 4 commits .

  • 1dfbc24 0.4.13
  • 4290140 Update compiled file
  • 7fdde65 Merge pull request #176 from christophehurpeau/patch-1
  • 3061a49 fix: browser regression from #169 when getResponseHeader throws

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Unicode characters are encoded incorrectly and/or cause parse errors – tape-run

Some of the unicode characters are creating falsy-negatives when running using tape-run, but they're working correctly with browser-run. 2 different cases.

Using ächarcode: 228 – causes SyntaxError: Parse error and hangs process.

Using cyrillic eg. тест – encodes incorrectly giving теÑ�Ñ‚ as a result instead of тест which causes given test fail.

Simple tape test using Backbone:

test("routes with unicode", function (t) {
    location.replace('http://example.com#search/тест');
    Backbone.history.checkUrl();
    t.equal(router.query, "тест");
});

results:

  ---
    operator: equal
    expected: 'те�т'
    actual:   'тест'
  ...

if you write simple test:

test("unicode", function (t) {
    t.equal("тест", "тест");
});

it will of course pass

  ---
    operator: equal
    expected: 'те�т'
    expected: 'те�т'
  ...

Multiple arguments to window.console methods not printed to command line

In reporter.js, it seems like only the first argument is written to the command line.

ws.write(msg + '\n');

    console[method] = function (msg) {
      ws.write(msg + '\n');
      if (old) old.apply(console, arguments);
      if (msg instanceof Error && typeof JSON != 'undefined') {
        ws.write(JSON.stringify(msg) + '\n'); 
      }

No output

When I run the example echo "console.log('Hey from ' + location); window.close()" | browser-run
I get no output, but also no error message. How can I debug this?

Browser-run causing tape-run tests to hang

This is probably related to #33 or #28. It took me a while to track down the solution so I'm making this issue to hopefully help others searching for the answer.

I had a package.json file with something like this:

{
  ...
  "devDependencies": {
    "tape-run": "^1.1.0"
  }
  ...
}

When I ran my tests they'd hang unexpectedly causing timeouts on Travis CI. I had to specify an older version of browser-run to get them to run again:

{
  ...
  "devDependencies": {
    "browser-run": "2.5.0",
    "tape-run": "^1.1.0"
  }
  ...
}

For more details:

https://travis-ci.org/haydn/json-api-store/builds/83237200

Use local PhantomJS?

Currently the package requires a globally installed PhantomJS. There are situations where you'd like to pin the version to specific projects. It's also easier for people to use because it can get installed with other dependencies when npm install is called.

browser-run 2.6.1 broken

Given the following file:

import test from 'tape';

test('teardown', function (t) {
  window.close();
  t.end();
});

and this test command browserify ./test/index.js -t [ babelify ] | browser-run

browser-run 2.6.0 produces the following output:

TAP version 13
# teardown

1..0
# tests 0
# pass  0

# ok

but browser run 2.6.1 produces this:

TAP version 13
# teardown

1..0
# tests 0
# pass  0

# ok

TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909
  http://localhost:60578/reporter.js:5909 in _onXHRProgress
  http://localhost:60578/reporter.js:5694 in _onXHRProgress
  http://localhost:60578/reporter.js:5644 in onreadystatechange
TypeError: Type error
TypeError: Type error

  http://localhost:60578/reporter.js:29
TypeError: Type error

  http://localhost:60578/reporter.js:5909

and then proceeds to hang.

Usage on GitHub Actions with xvfb

I tried to install/configure xvfb on GitHub actions to use browser-run/tape-run there, but I was not successful.

If anyone finds a solution, please advise. This is the simplest configuration that's only missing xvfb

on: push
jobs:
  run:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/setup-node@v1
    - run: npm install browser-run
    - run: echo 'console.log(9001); window.close()' | browser-run

I wrote all of this and it exits with 0 but browser-run doesn't output anything. The code is not run at all, invalid JS will also result in browser-run exiting successfully.

on: push
jobs:
  run:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/setup-node@v1
    - run: sudo apt-get install xvfb
    - run: export DISPLAY=':99.0'
    - run: Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
    - run: npm install browser-run
    - run: npm install electron # Without this, Electron complains it's not installed correctly
    - run: echo 'console.log(9001); window.close()' | ./node_modules/.bin/browser-run

Use browserify as a dev-dependency

browser-run is the perfect tool to launch tests in a continuous integration environment, like Jenkins and Travis. We could save time, bandwidth and disk space (halt of the size of the installed package, 22 of 45 MB, corresponds to browserify) if instead of requiring browserify as a runtime dependency to bundle the browser/reporter.js file, it would be included that way in the published package.

I also think the package utf8-stream is only used in that file, so it could also be installed as a devDependency only.

Example package.json:

{
  "scripts": {
    "prepublish": "browserify browser/reporter.js -o dist/reporter.js"
  },
  "devDependencies": {
    "browserify": "~5.9.3",
    "utf8-stream": "0.0.0"
  }
}

If you agree I can create a PR ;)

Autoclose?

When using the cli, is there anyway to automatically close browsers after some task "finished"?
Would it be possible to call something in javascript that browser-run picks up and have it stop itself?

I'm using browser-run to run unit testing and when their done I'd like it to automatically close the browser again.
(It now closes the browser when it stop the browser-run process (with ctrl+c)).

Cannot run unpublished version

This commit:
e161817

Introduces a requirement that browser-run can only work if it's installed from npm (or any copy of published version, where /static/reporter.js is generated.
Figuring it out took me a while.

I'm not PRing a fix to switch from prepublish to a postinstall because I hope to discuss it first. Since browserify is in devDependencies, is it ok to assume it's there when postinstall is running? Sounds risky.

For people trying to test what they contribute instead of just PRing it, let's at least add an error message that explains what happened when /static/reporter.js can't be found

browser-run 2.6.0 broken

It fails with:

Uncaught SyntaxError: Unexpected token o
SourceMapConsumer @ reporter.js:7159
mapSourcePosition @ reporter.js:8993
wrapCallSite @ reporter.js:9152
(anonymous function) @ reporter.js:9187
prepareStackTrace @ reporter.js:9186
window.onerror @ reporter.js:14

2.5.0 works

Allow getUserMedia?

That would be great, so that I can run headless tests on code that uses the getUserMedia API

doesn't work with js code that contains a </script>

I have an issue with browser-run where it will spit out a SyntaxError: Invalid or unexpected token. Originally I thought it had to do with #135, however after a lot of head bashing and finally running the .source... file in the browser directly I realized that the problem was a closing script tag located in the script.

This is an issue with injecting JavaScript directly into HTML as the browser is going to interpret a </script> literally, even if it appears in a string or something.

I don't know the best way to fix this, but perhaps the script can be encoded in the HTML file and then decoded and eval'd on the browser? Curious on your thoughts. Thanks!

Windows issues

I try to use tape-run but can't because I have many issues on my Windows box:

  1. electron is not supported on my (old) Windows box. So I need to switch to another browser
  2. real browsers detection fail too due to bugs in browser-launcher which is not maintained anymore!

Can I suggest moving to browser-launcher2 instead of browser-launcher, so that bugs are fixed?

Doesn't work with Chrome on MacOS

cat dist-test/specs.js | browser-run --static dist-test --browser chrome

doesn't work correctly because it doesn't open chrome with the generated url, so the tests are not executed

I tried a command similar to the one executed by the browser-launcher internally, and it turned out that it simply doesn't work the way it should on MacOS:

open --new -a '/Applications/Google Chrome.app' http://localhost:57630

This command opens chrome, but with empty tab instead of the url specified: http://localhost:57630

System Configuration:

MacOS Sierra 10.12.2
Google Chrome - Version 55.0.2883.95 (64-bit)

TypeError: Arguments to path.join must be strings

echo "console.log('Hey there from ' + document.location)" | browser-run

path.js:360
throw new TypeError('Arguments to path.join must be strings');
^
TypeError: Arguments to path.join must be strings
at path.js:360:15
at Array.filter (native)
at Object.exports.join (path.js:358:36)
at /usr/local/lib/node_modules/browser-run/node_modules/browser-launcher/lib/darwin/chrome.js:22:23
at /usr/local/lib/node_modules/browser-run/node_modules/browser-launcher/lib/darwin/chrome.js:16:9
at /usr/local/lib/node_modules/browser-run/node_modules/browser-launcher/lib/darwin/util.js:16:9
at ChildProcess.exithandler (child_process.js:635:7)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:743:16)
at Socket. (child_process.js:956:11)

System: OS X 10.8.5. both Canary and Firefox are installed

Issues to many xhr

Hi,

I am using tape-run to run mobx tests and I saw that in safari and chrome not all tests results are sent back to the node http server, therefore tests never finishes.

tape_run_bug
Taken from mobxjs/mobx#590

After some researching, I found out that for each console.log we issue POST request and it might be that we issue lots of requests and browser resources are exhausted.

I can submit a pull request that will use websocket-stream (or socket-io so we can support old browsers?) to fix that.

Ability to add additional script tags to html

I'm testing a set of interdependent libraries using browserify. I'd like to load multiple <script> tags into <head>.

Right now, static/index.html is loaded. I'd like the ability to use a custom html file and/or have a custom handler for GET /.

Support import in scripts

Trying to come up with node ES module, working both in browser/node.
It uses import.
cat test.js | browser-run gives

SyntaxError: Unexpected identifier
  at http://localhost:55133/bundle.js:1:8 

Would that be reasonable to detect if input code uses import (see is-module) and include script as <script type="module"></script>?

update browserify dep

It looks like earlier versions of browserify (<=4.2.0) included a potentially vulnerable version of the syntax-error module (see advisory).

Steps to reproduce:

$ git clone [email protected]:juliangruber/browser-run.git .
$ npm i
$ npm shrinkwrap --dev
wrote npm-shrinkwrap.json

$ # sudo npm i nsp -g
$ nsp shrinkwrap
Name          Installed   Patched  Vulnerable Dependency
syntax-error    0.0.1    >= 1.1.1  browser-run > browserify

For giggles, here's a list of all the outdated modules, via npm outdated, in case you want to update them all at once:

$ npm outdated --depth 0 | sort
Package     Current  Wanted  Latest  Location
browserify   2.13.3  2.13.3   5.9.3  browserify
duplexer      0.0.3   0.0.3   0.1.1  duplexer
optimist      0.3.7   0.3.7   0.6.1  optimist
through       2.2.7   2.2.7   2.3.4  through

Tests fail on OS X with Node 5: spawn phantomjs ENOENT

~/forks/browser-run master
❯ t

> [email protected] test /Users/zeke/forks/browser-run
> make test

test/close.js ......................................... 2/2 967ms
test/empty.js ......................................... 1/1 300ms
test/error.js ......................................... 1/1 469ms
test/phantom.js ....................................... 0/3 30s
  phantomjs
  not ok Error: spawn phantomjs ENOENT
    at:
      line: 890
      column: 11
      file: util.js
      function: exports._errnoException
    stack: |
      exports._errnoException (util.js:890:11)
      _combinedTickCallback (internal/process/next_tick.js:74:11)
      process._tickCallback (internal/process/next_tick.js:98:9)
    code: ENOENT
    errno: ENOENT
    syscall: spawn phantomjs
    path: phantomjs
    spawnargs:
      - '/var/folders/nj/9554mvgj07xdh61jbyhkbtq80000gn/T/phantomjs-stream:4f43af18'
    test: phantomjs
    message: 'Error: spawn phantomjs ENOENT'

  phantomjs
  not ok missing test
  not ok timeout!
    signal: SIGTERM
    handles:
      - type: Server
        events:
          - request
          - connection
          - clientError
        connectionKey: '6::::0'

test/spawn.js ......................................... 1/1 1s
test/stream.js ........................................ 1/1 966ms
total ................................................. 6/9


  6 passing (34s)
  3 failing

make: *** [test] Error 1
npm ERR! Test failed.  See above for more details.

~/forks/browser-run master 36s
❯ node -v
v5.10.1

phantomjs processes aren't killed correctly

After running it many times, I observed that unclosed processed are piling up.

Running ps aux | grep node returned a lot of these:

ashnur    5010  0.0  0.4 1384812 17308 pts/5   Sl   10:43   0:00 /usr/lib/node_modules/mocha-run/node_modules/browser-run/node_modules/phantomjs/lib/phantom/bin/phantomjs /usr/lib/node_modules/mocha-run/node_modules/browser-run/script/run.js 60492

Fail while running with default electron browser

I'm using next command:

echo "console.log('Hey from ' + location); window.close()" | browser-run

The Electron starts but with an error:

A JavaScript error occurred in the main process.
Uncaught Exception:
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:404:25)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object. (/Users/activeobject/.nvm/versions/node/v5.1.1/lib/node_modules/browser-run/node_modules/electron-stream/lib/runner.js:1:90)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)

It seems that electron-stream module has been published with raw sources.
I'm using [email protected].

Works in everything EXCEPT Electron

I don't know what I could be getting wrong in my setup, but I can't seem to make this work using Electron.

Running with cat build/index.html | browser-run --input html --static build

Folder structure is

build/
  bundle.js
  index.html

css/
  ... source files ...

html/
  ... source files ...

js/
  ... source files ...

node_modules/
  ... node modules ...

package.json
webpack.config.js

index.html is

<div id="app"></div>
<script type="text/javascript" src="/reporter.js"></script>
<script type="text/javascript" src="/bundle.js"></script>

I've stripped down my bundle so that the only code going into it is some babel runtime code and:

import test from 'tape'

test.onFinish(function(){
  window.close();
});

test('test ok', function (t) {
  t.ok(true)
  t.end()
})

If I use the -b or --port options, everything works as expected. If I leave those options off, Electron tries to run but just outputs SyntaxError: Unexpected token < and immediately exits.

Adding --basedir build causes Electron to create a file called .source.[timestamp].html and the contents of that file are:

<body><script><div id="app"></div>
<script type="text/javascript" src="/reporter.js"></script>
<script type="text/javascript" src="/bundle.js"></script>
</script></body>

However, nothing is output and Electron just hangs until I hit Ctrl-C.

Meanwhile, using a browser (-b or --port), I get exactly the output I expect:

TAP version 13
# okay
ok 1 should be truthy

1..1
# tests 1
# pass  1

# ok

Node postinstall script fails

So when I try installing: sudo npm install -g browser-run
It errors out on my system. (MacOS, 10.12.6)

/usr/local/bin/browser-run -> /usr/local/lib/node_modules/browser-run/bin/bin.js

> [email protected] postinstall /usr/local/lib/node_modules/browser-run/node_modules/electron
> node install.js

/usr/local/lib/node_modules/browser-run/node_modules/electron/install.js:48
  throw err

Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/browser-run/node_modules/electron/.electron'
    at Error (native)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/snau/.npm/_logs/2017-10-12T16_25_11_282Z-debug.log

Any idea why this is happening?

An in-range update of electron-stream is breaking the build 🚨

Version 4.1.1 of electron-stream just got published.

Branch Build failing 🚨
Dependency electron-stream
Current Version 4.1.0
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

As electron-stream is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪


Status Details
  • dependency-ci Dependencies checked Details

  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 2 commits .

  • 5a06bb1 4.1.1
  • cee7635 rename .nodeIntegration to .node, keep an alias

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Example with watch

Is there some way of getting this to work while watching for file changes?

Currently I'm killing the process, then starting a new one on each change, but it feels a bit brute. Any ideas?

Std stream not working correctly

I'm on windows but using git for windows bash

While
browserify -t babelify src/test.js | browser-run -p 8080
works just fine

$ browserify -t babelify src/test.js -o lib/script-compiled.js
$ cat lib/script-compiled.js | browser-run -p 8080

Does not. neither does

$ browser-run -p 8080 < lib/script-compiled.js

I can't figure why this strange behaviour might be happening

Firefox: Unhandled stream error in pipe.

Has anyone tried running tests on Travis in Firefox? It works correctly with Electron but with Firefox I get this error:

internal/streams/legacy.js:57
      throw er; // Unhandled stream error in pipe.
      ^
Unexpected data: 

And if I add the MOZ_HEADLESS=1 env as suggested by Travis’s docs, I get this:

internal/streams/legacy.js:57
      throw er; // Unhandled stream error in pipe.
      ^
Unexpected data: *** You are running in headless mode.

This is my Travis config:

language: node_js
node_js:
  - stable
env:
  - MOZ_HEADLESS=1
addons:
  firefox: latest
  apt:
    packages:
      - xvfb
install:
  - export DISPLAY=':99.0'
  - Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
  - npm install

Full failing Travis log: https://travis-ci.com/bfred-it/insert-text-textarea/builds/108077120

Browsers other than electron don't close

I noticed when using tape-run that Firefox closed but Chrome stayed open (even though the tests completed anyway)

However this doesn't close the tab and window either:

echo "console.log('Hey from ' + location); window.close()" | browser-run --browser firefox

...nor any other browser I tested: chrome and safari.

The interesting part is that this closed the tab once, but on a second call the tab and window stayed open:

echo "var run = require('browser-run');
  var browser = run({browser: 'chrome'});
  browser.pipe(process.stdout);
  browser.end('console.log(location); window.close()');" | node

I'm on macOS 10.15.2, Node v12.14.1

Generating coverage reports?

It would be great to use tape-run to generate a code coverage report on the browserify output using Istanbul. Would have opened this issue there but here seems more appropriate.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.