-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(loader-utils): Preparing to migrate from FileProviders to Reada…
…bleFiles (#3000)
- Loading branch information
1 parent
48fda87
commit d9db370
Showing
20 changed files
with
248 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
modules/loader-utils/src/lib/file-provider/file-provider-interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Interface for providing file data | ||
*/ | ||
export interface FileProviderInterface { | ||
/** | ||
* Cleanup class data | ||
*/ | ||
destroy(): Promise<void>; | ||
/** | ||
* Gets an unsigned 8-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in bytes, from the start of the file where to read the data. | ||
*/ | ||
getUint8(offset: bigint): Promise<number>; | ||
|
||
/** | ||
* Gets an unsigned 16-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in bytes, from the start of the file where to read the data. | ||
*/ | ||
getUint16(offset: bigint): Promise<number>; | ||
|
||
/** | ||
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in bytes, from the file of the view where to read the data. | ||
*/ | ||
getUint32(offset: bigint): Promise<number>; | ||
|
||
/** | ||
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in byte, from the file of the view where to read the data. | ||
*/ | ||
getBigUint64(offset: bigint): Promise<bigint>; | ||
|
||
/** | ||
* returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive. | ||
* @param startOffset The offset, in bytes, from the start of the file where to start reading the data. | ||
* @param endOffset The offset, in bytes, from the start of the file where to end reading the data. | ||
*/ | ||
slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>; | ||
|
||
/** | ||
* the length (in bytes) of the data. | ||
*/ | ||
length: bigint; | ||
} | ||
|
||
/** | ||
* Check is the object has FileProvider members | ||
* @param fileProvider - tested object | ||
*/ | ||
export const isFileProvider = (fileProvider: unknown) => { | ||
return ( | ||
(fileProvider as FileProviderInterface)?.getUint8 && | ||
(fileProvider as FileProviderInterface)?.slice && | ||
(fileProvider as FileProviderInterface)?.length | ||
); | ||
}; |
120 changes: 95 additions & 25 deletions
120
modules/loader-utils/src/lib/file-provider/file-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,127 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
import {ReadableFile} from '../files/file'; | ||
import {FileProviderInterface} from './file-provider-interface'; | ||
|
||
/** | ||
* Interface for providing file data | ||
* Provides file data using range requests to the server | ||
* @deprecated - will be replaced with ReadableFile | ||
*/ | ||
export interface FileProvider { | ||
export class FileProvider implements FileProviderInterface { | ||
/** The File object from which data is provided */ | ||
private file: ReadableFile; | ||
private size: bigint; | ||
|
||
/** Create a new BrowserFile */ | ||
private constructor(file: ReadableFile, size: bigint | number) { | ||
this.file = file; | ||
this.size = BigInt(size); | ||
} | ||
|
||
static async create(file: ReadableFile): Promise<FileProvider> { | ||
return new FileProvider( | ||
file, | ||
file.bigsize > 0n | ||
? file.bigsize | ||
: file.size > 0n | ||
? file.size | ||
: (await file.stat?.())?.bigsize ?? 0n | ||
); | ||
} | ||
|
||
/** | ||
* Cleanup class data | ||
* Truncates the file descriptor. | ||
* @param length desired file lenght | ||
*/ | ||
destroy(): Promise<void>; | ||
async truncate(length: number): Promise<void> { | ||
throw new Error('file loaded via range requests cannot be changed'); | ||
} | ||
|
||
/** | ||
* Append data to a file. | ||
* @param buffer data to append | ||
*/ | ||
async append(buffer: Uint8Array): Promise<void> { | ||
throw new Error('file loaded via range requests cannot be changed'); | ||
} | ||
|
||
/** Close file */ | ||
async destroy(): Promise<void> { | ||
throw new Error('file loaded via range requests cannot be changed'); | ||
} | ||
|
||
/** | ||
* Gets an unsigned 8-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in bytes, from the start of the file where to read the data. | ||
*/ | ||
getUint8(offset: bigint): Promise<number>; | ||
async getUint8(offset: number | bigint): Promise<number> { | ||
const arrayBuffer = await this.file.read(offset, 1); | ||
const val = new Uint8Array(arrayBuffer).at(0); | ||
if (val === undefined) { | ||
throw new Error('something went wrong'); | ||
} | ||
return val; | ||
} | ||
|
||
/** | ||
* Gets an unsigned 16-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in bytes, from the start of the file where to read the data. | ||
*/ | ||
getUint16(offset: bigint): Promise<number>; | ||
async getUint16(offset: number | bigint): Promise<number> { | ||
const arrayBuffer = await this.file.read(offset, 2); | ||
const val = new Uint16Array(arrayBuffer).at(0); | ||
if (val === undefined) { | ||
throw new Error('something went wrong'); | ||
} | ||
return val; | ||
} | ||
|
||
/** | ||
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in bytes, from the file of the view where to read the data. | ||
* @param offset The offset, in bytes, from the start of the file where to read the data. | ||
*/ | ||
getUint32(offset: bigint): Promise<number>; | ||
async getUint32(offset: number | bigint): Promise<number> { | ||
const arrayBuffer = await this.file.read(offset, 4); | ||
const val = new Uint32Array(arrayBuffer).at(0); | ||
if (val === undefined) { | ||
throw new Error('something went wrong'); | ||
} | ||
return val; | ||
} | ||
|
||
/** | ||
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file. | ||
* @param offset The offset, in byte, from the file of the view where to read the data. | ||
* @param offset The offset, in bytes, from the start of the file where to read the data. | ||
*/ | ||
getBigUint64(offset: bigint): Promise<bigint>; | ||
async getBigUint64(offset: number | bigint): Promise<bigint> { | ||
const arrayBuffer = await this.file.read(offset, 8); | ||
const val = new BigInt64Array(arrayBuffer).at(0); | ||
if (val === undefined) { | ||
throw new Error('something went wrong'); | ||
} | ||
return val; | ||
} | ||
|
||
/** | ||
* returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive. | ||
* @param startOffset The offset, in bytes, from the start of the file where to start reading the data. | ||
* @param startOffset The offset, in byte, from the start of the file where to start reading the data. | ||
* @param endOffset The offset, in bytes, from the start of the file where to end reading the data. | ||
*/ | ||
slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>; | ||
async slice(startOffset: bigint | number, endOffset: bigint | number): Promise<ArrayBuffer> { | ||
const bigLength = BigInt(endOffset) - BigInt(startOffset); | ||
if (bigLength > Number.MAX_SAFE_INTEGER) { | ||
throw new Error('too big slice'); | ||
} | ||
const length = Number(bigLength); | ||
|
||
return await this.file.read(startOffset, length); | ||
} | ||
|
||
/** | ||
* the length (in bytes) of the data. | ||
*/ | ||
length: bigint; | ||
get length(): bigint { | ||
return this.size; | ||
} | ||
} | ||
|
||
/** | ||
* Check is the object has FileProvider members | ||
* @param fileProvider - tested object | ||
*/ | ||
export const isFileProvider = (fileProvider: unknown) => { | ||
return ( | ||
(fileProvider as FileProvider)?.getUint8 && | ||
(fileProvider as FileProvider)?.slice && | ||
(fileProvider as FileProvider)?.length | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
modules/loader-utils/test/lib/file-provider/file-provider.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import test from 'tape-promise/tape'; | ||
import {DATA_ARRAY} from '@loaders.gl/i3s/test/data/test.zip'; | ||
import {BlobFile, FileProvider} from '@loaders.gl/loader-utils'; | ||
|
||
export const getSignature = () => new Uint8Array([0x50, 0x4b, 0x03, 0x04]); | ||
|
||
const getProvider = () => { | ||
return FileProvider.create(new BlobFile(DATA_ARRAY.buffer)); | ||
}; | ||
|
||
test('FileProvider#slice', async (t) => { | ||
const provider = await getProvider(); | ||
const slice = await provider.slice(0, 4); | ||
t.deepEqual(new Uint8Array(slice), getSignature()); | ||
t.end(); | ||
}); | ||
|
||
test('FileProvider#getUint8', async (t) => { | ||
const provider = await getProvider(); | ||
t.equals(await provider.getUint8(0), 80); | ||
t.end(); | ||
}); | ||
|
||
test('FileProvider#getUint16', async (t) => { | ||
const provider = await getProvider(); | ||
t.equals(await provider.getUint16(0), 19280); | ||
t.end(); | ||
}); | ||
|
||
test('FileProvider#getUint32', async (t) => { | ||
const provider = await getProvider(); | ||
t.equals(await provider.getUint32(0), 67324752); | ||
t.end(); | ||
}); | ||
|
||
test('FileProvider#getBigUint64', async (t) => { | ||
const provider = await getProvider(); | ||
t.equals(await provider.getBigUint64(0), 563035920091984n); | ||
t.end(); | ||
}); |
Oops, something went wrong.