33 * License, v. 2.0. If a copy of the MPL was not distributed with this
44 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
55
6+ /* eslint-env node */
7+
68require ( "babel-register" ) ;
79
810const path = require ( "path" ) ;
@@ -27,6 +29,7 @@ const {
2729const isDevelopment = require ( "devtools-environment" ) . isDevelopment ;
2830const { handleLaunchRequest } = require ( "./server/launch" ) ;
2931const NODE_VERSION = require ( "../package.json" ) . engines . node ;
32+ const mime = require ( "mime-types" ) ;
3033let root ;
3134
3235function httpOrHttpsGet ( url , onResponse ) {
@@ -38,11 +41,12 @@ function httpOrHttpsGet(url, onResponse) {
3841 response . emit ( "statusCode" , new Error ( response . statusCode ) ) ;
3942 return onResponse ( "{}" ) ;
4043 }
41- let body = "" ;
42- response . on ( "data" , ( d ) => {
43- body += d ;
44+ const contentType = response . headers [ "content-type" ] ;
45+ let body = Buffer . alloc ( 0 ) ;
46+ response . on ( "data" , ( data ) => {
47+ body = Buffer . concat ( [ body , data ] ) ;
4448 } ) ;
45- response . on ( "end" , ( ) => onResponse ( body ) ) ;
49+ response . on ( "end" , ( ) => onResponse ( { body, contentType } ) ) ;
4650
4751 return undefined ;
4852 } ) ;
@@ -80,13 +84,20 @@ function handleNetworkRequest(req, res) {
8084 const url = req . query . url ;
8185 if ( url . indexOf ( "file://" ) === 0 ) {
8286 const _path = url . replace ( "file://" , "" ) ;
83- res . json ( JSON . parse ( fs . readFileSync ( _path , "utf8" ) ) ) ;
87+ const mimeType = mime . lookup ( _path ) ;
88+ if ( mimeType ) {
89+ res . set ( "Content-Type" , mimeType ) ;
90+ }
91+ res . send ( fs . readFileSync ( _path ) ) ;
8492 } else {
85- const httpReq = httpOrHttpsGet ( req . query . url , body => {
93+ const httpReq = httpOrHttpsGet ( req . query . url , ( { body, contentType } ) => {
94+ if ( contentType ) {
95+ res . set ( "Content-Type" , contentType ) ;
96+ }
8697 try {
8798 res . send ( body ) ;
8899 } catch ( e ) {
89- res . status ( 500 ) . send ( "Malformed json" ) ;
100+ res . status ( 500 ) . send ( `Network error: ${ e } ` ) ;
90101 }
91102 } ) ;
92103
0 commit comments