juliangruber / browser-run

Run code inside a browser from the command line
447 stars 62 forks source link

Adding in the ability to serve static resources #10

Closed jcblw closed 9 years ago

jcblw commented 9 years ago

This is allows you to serve up static resources using the same server that browser-run is using.

example usage

var run = require( '../' );

var browser = run( {
    static: true
} );
browser.pipe( process.stdout );
browser.write(
    'var img = document.createElement( \'img\' ); ' + 
    'img.onload = function(){ console.log( \'img loaded\' )}; ' +
    'img.onerror = function( err ){ console.log( \'img errored\', err )}; ' +  
    'img.src = \'./examples/foo.png\'; ' +
    'document.body.appendChild( img );' 
);
browser.end();

If the resource is there then it will log "image loaded"

Also I was thinking on how we could test this let me know if you have any ideas

once this is merged I think I can have a pr ready for tape-run to be able to pass this flag.

juliangruber commented 9 years ago

two things:

1) for security and compatibility reasons we should be using a module like st, ecstatic or serve 2) for the js api, static should take a string of the path to server, only the bin should rely on process.cwd(). libs should have as few side effects as possible

jcblw commented 9 years ago

Ok everything seems to be fixed up, the example is slightly different.

var run = require( '../' );

var browser = run( {
    static: process.cwd()
} );
browser.pipe( process.stdout );
browser.write(
    'var img = document.createElement( \'img\' ); ' + 
    'img.onload = function(){ console.log( \'img loaded\' )}; ' +
    'img.onerror = function( err ){ console.log( \'img errored\', err )}; ' +  
    'img.src = \'./examples/foo.png\'; ' +
    'document.body.appendChild( img );' 
);
browser.end();

Also whats going on with the test? Is this something I should be concerned with, seems as though there is a missing .travis.yml

juliangruber commented 9 years ago

thanks! published as 0.5.0