Enable parsing infoPlist in the browser

This commit is contained in:
Sam Carlton 2022-07-19 00:04:28 -05:00
parent f59ebd9056
commit f3b4bdcf3c

View file

@ -1,19 +1,22 @@
import { Buffer, Blob } from 'buffer' // import { Buffer, Blob } from 'buffer'
// import plist from 'plist' // import prettyBytes from 'pretty-bytes'
import * as plist from 'simple-plist'
import prettyBytes from 'pretty-bytes'
// import zip from '@zip.js/zip.js' // import zip from '@zip.js/zip.js'
import FileApi, { File } from 'file-api' // import FileApi from 'file-api'
import { isString, isNonEmptyString } from '~/helpers/check-types.js' import { isString, isNonEmptyString } from '~/helpers/check-types.js'
import { extractMachoMeta } from '~/helpers/scanner/parsers/macho.js' import { parsePlistBuffer } from '~/helpers/scanner/parsers/plist.js'
// import { extractMachoMeta } from '~/helpers/scanner/parsers/macho.js'
// For some reason inline 'import()' works better than 'import from' // For some reason inline 'import()' works better than 'import from'
// https://gildas-lormeau.github.io/zip.js/ // https://gildas-lormeau.github.io/zip.js/
const zip = await import('@zip.js/zip.js') const zip = await import('@zip.js/zip.js')
const bufferApi = await import('buffer')
// const FileApi = await import('file-api')
// const { parse: plistParse } = await import('simple-plist/dist/index.js')
// https://gildas-lormeau.github.io/zip.js/core-api.html#configuration // https://gildas-lormeau.github.io/zip.js/core-api.html#configuration
zip.configure({ zip.configure({
// Disable Web Workers for SSR since Node doesn't support them yet // Disable Web Workers for SSR since Node doesn't support them yet
@ -22,6 +25,17 @@ zip.configure({
}) })
function makeNodeFileBuffer ( buffer ) {
const fileBuffer = new bufferApi.Buffer.alloc( buffer.byteLength )
for (var i = 0; i < buffer.length; i++)
fileBuffer[i] = buffer[i];
// console.log( 'this.machoFileInstance', this.machoFileInstance.buffer.byteLength )
return fileBuffer
}
export class AppScan { export class AppScan {
constructor ({ constructor ({
fileLoader, fileLoader,
@ -101,8 +115,14 @@ export class AppScan {
async readFileBlob ( FileInstance ) { async readFileBlob ( FileInstance ) {
return new Promise( ( resolve, reject ) => { return new Promise( async ( resolve, reject ) => {
const fileReader = new zip.BlobReader( new Blob( FileInstance.arrayBuffer ) ) // Check if file is a Blob, typically in the Browser
// otherwise convert it to a Blob, like in Node
const FileBlob = FileInstance instanceof Blob ? FileInstance : new bufferApi.Blob( await FileInstance.arrayBuffer() )
console.log( 'FileBlob', await FileInstance.arrayBuffer() )
const fileReader = new zip.BlobReader( FileBlob )
// https://gildas-lormeau.github.io/zip.js/core-api.html#zip-reading // https://gildas-lormeau.github.io/zip.js/core-api.html#zip-reading
const zipReader = new zip.ZipReader( fileReader ) const zipReader = new zip.ZipReader( fileReader )
@ -196,19 +216,17 @@ export class AppScan {
throw new Error( 'More than one root info.plist found' ) throw new Error( 'More than one root info.plist found' )
} }
const infoBlob = await this.readFileEntryData( fileEntry, zip.Uint8ArrayWriter ) const infoUint8Array = await this.readFileEntryData( fileEntry, zip.Uint8ArrayWriter )
const infoBuffer = Buffer.from( infoBlob ) // console.log( 'infoUint8Array', infoUint8Array )
// const infoBuffer = await fileEntry.getData() const infoNodeBuffer = makeNodeFileBuffer( infoUint8Array )
// console.log( 'infoBuffer', Buffer.from( infoBuffer ) )
// Parse the Info.plist data // Parse the Info.plist data
this.infoPlist = plist.parse( infoBuffer ) this.infoPlist = await parsePlistBuffer( infoNodeBuffer )
this.sendMessage({ this.sendMessage({
message: ' Found Info.plist', message: ' Found Info.plist',
// data: this.infoPlist, data: this.infoPlist,
}) })
} }