BinaryResourceViewFactory.js 3.0 KB

12345678910111213141516171819202122
  1. export class BinaryResourceViewFactory{constructor(base64content,contentUrl,resourceType){this._base64content=base64content;this._contentUrl=contentUrl;this._resourceType=resourceType;this._arrayPromise=null;this._hexPromise=null;this._utf8Promise=null;}
  2. async _fetchContentAsArray(){if(!this._arrayPromise){this._arrayPromise=new Promise(async resolve=>{const fetchResponse=await fetch('data:;base64,'+this._base64content);resolve(new Uint8Array(await fetchResponse.arrayBuffer()));});}
  3. return await this._arrayPromise;}
  4. async hex(){if(!this._hexPromise){this._hexPromise=new Promise(async resolve=>{const content=await this._fetchContentAsArray();const hexString=BinaryResourceViewFactory.uint8ArrayToHexString(content);resolve({content:hexString,isEncoded:false});});}
  5. return this._hexPromise;}
  6. async base64(){return{content:this._base64content,isEncoded:true};}
  7. async utf8(){if(!this._utf8Promise){this._utf8Promise=new Promise(async resolve=>{const content=await this._fetchContentAsArray();const utf8String=new TextDecoder('utf8').decode(content);resolve({content:utf8String,isEncoded:false});});}
  8. return this._utf8Promise;}
  9. createBase64View(){return new SourceFrame.ResourceSourceFrame(Common.StaticContentProvider.fromString(this._contentUrl,this._resourceType,this._base64content),false,{lineNumbers:false,lineWrapping:true});}
  10. createHexView(){const hexViewerContentProvider=new Common.StaticContentProvider(this._contentUrl,this._resourceType,async()=>{const contentAsArray=await this._fetchContentAsArray();const content=BinaryResourceViewFactory.uint8ArrayToHexViewer(contentAsArray);return{content,isEncoded:false};});return new SourceFrame.ResourceSourceFrame(hexViewerContentProvider,false,{lineNumbers:false,lineWrapping:false});}
  11. createUtf8View(){const utf8fn=this.utf8.bind(this);const utf8ContentProvider=new Common.StaticContentProvider(this._contentUrl,this._resourceType,utf8fn);return new SourceFrame.ResourceSourceFrame(utf8ContentProvider,false,{lineNumbers:true,lineWrapping:true});}
  12. static uint8ArrayToHexString(uint8Array){let output='';for(let i=0;i<uint8Array.length;i++){output+=BinaryResourceViewFactory.numberToHex(uint8Array[i],2);}
  13. return output;}
  14. static numberToHex(number,padding){let hex=number.toString(16);while(hex.length<padding){hex='0'+hex;}
  15. return hex;}
  16. static uint8ArrayToHexViewer(array){let output='';let line=0;while((line*16)<array.length){const lineArray=array.slice(line*16,(line+1)*16);output+=BinaryResourceViewFactory.numberToHex(line,8)+':';let hexColsPrinted=0;for(let i=0;i<lineArray.length;i++){if(i%2===0){output+=' ';hexColsPrinted++;}
  17. output+=BinaryResourceViewFactory.numberToHex(lineArray[i],2);hexColsPrinted+=2;}
  18. while(hexColsPrinted<42){output+=' ';hexColsPrinted++;}
  19. for(let i=0;i<lineArray.length;i++){const code=lineArray[i];if(code>=32&&code<=126){output+=String.fromCharCode(code);}else{output+='.';}}
  20. output+='\n';line++;}
  21. return output;}}
  22. self.SourceFrame=self.SourceFrame||{};SourceFrame=SourceFrame||{};SourceFrame.BinaryResourceViewFactory=BinaryResourceViewFactory;