Skip to content

Commit

Permalink
Use http server for serving local files, see phetsims/phet-io#1664
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed May 25, 2020
1 parent 855cfac commit 9d74fe9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion js/grunt/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ module.exports = function( grunt ) {

const filePath = `${buildDirectory}/${new Date().toLocaleString().split( ', ' ).join( '_' ).split( '/' ).join( '-' ).split( ' ' ).join( '_' ).split( ':' ).join( '.' )}.json`;

const api = await generatePhetioAPI( repo, buildLocal.localTestingURL );
const api = await generatePhetioAPI( repo, false );
writeFile( filePath, api );
} )
);
Expand Down
2 changes: 1 addition & 1 deletion js/grunt/phet-io/copySupplementalPhetioFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ module.exports = async ( repo, version, simulationDisplayName, packageObject, bu
await handleJSDOC( buildDir );

if ( generatePhetioAPIFile ) {
const fullAPI = await generatePhetioAPI( repo, buildLocal.localTestingURL );
const fullAPI = await generatePhetioAPI( repo, true );
grunt.file.write( `${buildDir}${repo}-phet-io-api.json`, fullAPI );
}
};
Expand Down
52 changes: 46 additions & 6 deletions js/grunt/phet-io/generatePhetioAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,53 @@

'use strict';

const http = require( 'http' );
const fs = require( 'fs' );
const puppeteer = require( 'puppeteer' );

/**
* @param {string} repo
* @param {string} localTestingURL - the browser url to access the root of phet repos
* @param {boolean} [fromBuiltVersion] - if the built file should be used to generate the API (otherwise uses unbuilt)
*/
module.exports = async ( repo, localTestingURL ) => {
localTestingURL = localTestingURL.endsWith( '/' ) ? localTestingURL : `${localTestingURL}/`;
module.exports = async ( repo, fromBuiltVersion = false ) => {

return new Promise( async ( resolve, reject ) => {
// Consider using https://github.com/cloudhead/node-static or reading https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/
const server = http.createServer( ( req, res ) => {

// Trim query string
const tail = req.url.indexOf( '?' ) >= 0 ? req.url.substring( 0, req.url.indexOf( '?' ) ) : req.url;
const path = `${process.cwd()}/..${tail}`;

// See https://gist.github.com/aolde/8104861
const mimeTypes = {
html: 'text/html',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
png: 'image/png',
js: 'text/javascript',
css: 'text/css',
gif: 'image/gif',
mp3: 'audio/mpeg',
wav: 'audio/wav'
};
const mimeType = mimeTypes[ path.split( '.' ).pop() ] || 'text/plain';

fs.readFile( path, function( err, data ) {
if ( err ) {
res.writeHead( 404 );
res.end( JSON.stringify( err ) );
}
else {
res.writeHead( 200, { 'Content-Type': mimeType } );
res.end( data );
}
} );
} );
server.listen( 0 );

const port = server.address().port;

return new Promise( async ( resolve, reject ) => {

const browser = await puppeteer.launch();
const page = await browser.newPage();
Expand All @@ -44,10 +80,14 @@ module.exports = async ( repo, localTestingURL ) => {
page.on( 'pageerror', msg => reject( msg ) );

try {
await page.goto( `${localTestingURL}${repo}/${repo}_en.html?brand=phet-io&phetioStandalone&phetioPrintAPI` );
const relativePath = fromBuiltVersion ?
`build/phet-io/${repo}_all_phet-io.html` :
`${repo}_en.html`;
const url = `http://localhost:${port}/${repo}/${relativePath}?brand=phet-io&phetioStandalone&phetioPrintAPI`;
await page.goto( url );
}
catch( e ) {
reject( e );
}
} );
};
};

0 comments on commit 9d74fe9

Please sign in to comment.