Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(native): parse file response type #3086

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/fiber/src/native/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,19 @@ export function polyfills() {
.then(async (uri) => {
const base64 = await fs.readAsStringAsync(uri, { encoding: fs.EncodingType.Base64 })
const data = Buffer.from(base64, 'base64')
onLoad?.(data.buffer)

switch (this.responseType) {
case 'arrayBuffer':
return onLoad?.(data.buffer)
case 'blob':
// @ts-ignore
return onLoad?.(new Blob([data.buffer]))
// case 'document':
case 'json':
return onLoad?.(JSON.parse(THREE.LoaderUtils.decodeText(data)))
default:
return onLoad?.(THREE.LoaderUtils.decodeText(data))
}
})
.catch((error) => {
onError?.(error)
Expand Down
8 changes: 6 additions & 2 deletions packages/fiber/tests/native/polyfills.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ describe('polyfills', () => {

it('loads files via the file system', async () => {
const asset = 1
const file = await new THREE.FileLoader().loadAsync(asset as any)
const loader = new THREE.FileLoader()
loader.setResponseType('arrayBuffer')
const file = await loader.loadAsync(asset as any)
expect(typeof (file as ArrayBuffer).byteLength).toBe('number') // TODO: ArrayBuffer instanceof
})

it('loads files via http', async () => {
const file = await new THREE.FileLoader().loadAsync('https://example.com/test.png')
const loader = new THREE.FileLoader()
loader.setResponseType('arrayBuffer')
const file = await loader.loadAsync('https://example.com/test.png')
expect(typeof (file as ArrayBuffer).byteLength).toBe('number') // TODO: ArrayBuffer instanceof
})
})