ResourceLoader.js 1.5 KB

1234567
  1. const ResourceLoader={};export default ResourceLoader;let _lastStreamId=0;const _boundStreams={};const _bindOutputStream=function(stream){_boundStreams[++_lastStreamId]=stream;return _lastStreamId;};const _discardOutputStream=function(id){_boundStreams[id].close();delete _boundStreams[id];};export const streamWrite=function(id,chunk){_boundStreams[id].write(chunk);};export function load(url,headers,callback){const stream=new Common.StringOutputStream();loadAsStream(url,headers,stream,mycallback);function mycallback(statusCode,headers){callback(statusCode,headers,stream.data());}}
  2. export const loadAsStream=function(url,headers,stream,callback){const streamId=_bindOutputStream(stream);const parsedURL=new Common.ParsedURL(url);if(parsedURL.isDataURL()){loadXHR(url).then(dataURLDecodeSuccessful).catch(dataURLDecodeFailed);return;}
  3. const rawHeaders=[];if(headers){for(const key in headers){rawHeaders.push(key+': '+headers[key]);}}
  4. Host.InspectorFrontendHost.loadNetworkResource(url,rawHeaders.join('\r\n'),streamId,finishedCallback);function finishedCallback(response){if(callback){callback(response.statusCode,response.headers||{});}
  5. _discardOutputStream(streamId);}
  6. function dataURLDecodeSuccessful(text){streamWrite(streamId,text);finishedCallback(({statusCode:200}));}
  7. function dataURLDecodeFailed(){finishedCallback(({statusCode:404}));}};self.Host=self.Host||{};Host=Host||{};Host.ResourceLoader=ResourceLoader;Host.ResourceLoader.streamWrite=streamWrite;Host.ResourceLoader.load=load;Host.ResourceLoader.loadAsStream=loadAsStream;