Skip to content

Commit

Permalink
fix: unpack modules in Android Release Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Sep 1, 2023
1 parent 3c91eac commit 120bae9
Showing 1 changed file with 47 additions and 37 deletions.
84 changes: 47 additions & 37 deletions packages/fiber/src/native/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,43 +78,52 @@ if (Platform.OS !== 'web') {
* Generates an asset based on input type.
*/
async function getAsset(input: string | number): Promise<Asset> {
switch (typeof input) {
case 'string':
if (input.startsWith('data:')) {
const [header, data] = input.split(',')
const [, type] = header.split('/')

const localUri = fs.cacheDirectory + uuidv4() + `.${type}`
await fs.writeAsStringAsync(localUri, data, { encoding: fs.EncodingType.Base64 })

return { localUri } as Asset
} else if (input.startsWith('blob:')) {
const blob = await new Promise<Blob>((res, rej) => {
const xhr = new XMLHttpRequest()
xhr.open('GET', input)
xhr.responseType = 'blob'
xhr.onload = () => res(xhr.response)
xhr.onerror = rej
xhr.send()
})

const data = await new Promise<string>((res, rej) => {
const reader = new FileReader()
reader.onload = () => res(reader.result as string)
reader.onerror = rej
reader.readAsText(blob)
})

const localUri = `data:${blob.type};base64,${data}`

return getAsset(localUri)
}
return Asset.fromURI(input).downloadAsync()
case 'number':
return Asset.fromModule(input).downloadAsync()
default:
throw new Error('R3F: Invalid asset! Must be a URI or module.')
if (typeof input === 'string') {
// Unpack Blobs from react-native BlobManager
if (input.startsWith('blob:')) {
const blob = await new Promise<Blob>((res, rej) => {
const xhr = new XMLHttpRequest()
xhr.open('GET', input as string)
xhr.responseType = 'blob'
xhr.onload = () => res(xhr.response)
xhr.onerror = rej
xhr.send()
})

const data = await new Promise<string>((res, rej) => {
const reader = new FileReader()
reader.onload = () => res(reader.result as string)
reader.onerror = rej
reader.readAsText(blob)
})

input = `data:${blob.type};base64,${data}`
}

// Create safe URI for JSI
if (input.startsWith('data:')) {
const [header, data] = input.split(',')
const [, type] = header.split('/')

const localUri = fs.cacheDirectory + uuidv4() + `.${type}`
await fs.writeAsStringAsync(localUri, data, { encoding: fs.EncodingType.Base64 })

return { localUri } as Asset
}
}

// Download bundler module or external URL
const asset = Asset.fromModule(input)

// Unpack assets in Android Release Mode
if (!asset.uri.includes(':')) {
const localUri = `${fs.cacheDirectory}ExponentAsset-${asset.hash}.${asset.type}`
await fs.copyAsync({ from: asset.uri, to: localUri })
return { localUri } as Asset
}

// Otherwise, resolve from registry
return asset.downloadAsync()
}

// Don't pre-process urls, let expo-asset generate an absolute URL
Expand All @@ -141,9 +150,10 @@ if (Platform.OS !== 'web') {
height: asset.height,
}
texture.flipY = true
// texture.unpackAlignment = 1
texture.unpackAlignment = 1
texture.needsUpdate = true

// Force non-DOM upload for EXGL fast paths
// @ts-ignore
texture.isDataTexture = true

Expand Down

0 comments on commit 120bae9

Please sign in to comment.