diff --git a/examples/experimental/slpk-in-browser/src/app.tsx b/examples/experimental/slpk-in-browser/src/app.tsx index 65e21d4caa..4f5df3abb7 100644 --- a/examples/experimental/slpk-in-browser/src/app.tsx +++ b/examples/experimental/slpk-in-browser/src/app.tsx @@ -11,9 +11,8 @@ import {MapController, FlyToInterpolator, MapViewState} from '@deck.gl/core/type import {COORDINATE_SYSTEM, I3SLoader, parseSLPKArchive} from '@loaders.gl/i3s'; import {Tileset3D} from '@loaders.gl/tiles'; import {ControlPanel} from './components/control-panel'; -import {BrowserFile} from './browser-file'; import {ZipFileSystem} from '@loaders.gl/zip'; -import {LoaderWithParser} from '@loaders.gl/loader-utils'; +import {BlobFile, FileProvider, LoaderWithParser} from '@loaders.gl/loader-utils'; import CustomTile3DLayer from './custom-tile-3d-layer'; export const TRANSITION_DURAITON = 4000; @@ -49,7 +48,7 @@ export default function App() { } const createFileSystem = async (file: File) => { - const fileProvider = new BrowserFile(file); + const fileProvider = await FileProvider.create(new BlobFile(file)); const archive = await parseSLPKArchive(fileProvider, undefined, file.name); const fileSystem = new ZipFileSystem(archive); setFileSystem(fileSystem); diff --git a/examples/experimental/slpk-in-browser/src/browser-file.ts b/examples/experimental/slpk-in-browser/src/browser-file.ts deleted file mode 100644 index de36ec1f84..0000000000 --- a/examples/experimental/slpk-in-browser/src/browser-file.ts +++ /dev/null @@ -1,132 +0,0 @@ -// loaders.gl -// SPDX-License-Identifier: MIT -// Copyright (c) vis.gl contributors -import {FileProviderInterface} from '@loaders.gl/loader-utils'; - -/** - * Provides file data using node fs library - * @deprecated - will be replaced with ReadableFile - */ -export class BrowserFile implements FileProviderInterface { - /** The File object from which data is provided */ - private file: File; - - /** Create a new BrowserFile */ - constructor(file: File) { - this.file = file; - } - /** - * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive. - * @param start The offset, in byte, from the start of the file where to start reading the data. - * @param lenght Length of read data - */ - private async getBytesFromFile(start: number, lenght: number): Promise { - let reader = new FileReader(); - reader.readAsArrayBuffer(this.file.slice(start, start + lenght)); - return new Promise((res, rej) => { - reader.onload = function() { - const arrayBuffer = reader.result - if (!arrayBuffer || typeof arrayBuffer === 'string') { - rej(new Error('something went wrong')); - } else { - res(arrayBuffer) - } - } - }) - } - - /** - * Truncates the file descriptor. - * @param length desired file lenght - */ - async truncate(length: number): Promise { - throw new Error("file loaded in browser cannot be changed"); - } - - /** - * Append data to a file. - * @param buffer data to append - */ - async append(buffer: Uint8Array): Promise { - throw new Error("file loaded in browser cannot be changed"); - } - - /** Close file */ - async destroy(): Promise { - throw new Error("file loaded in browser 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. - */ - async getUint8(offset: number | bigint): Promise { - const arrayBuffer = await this.getBytesFromFile(Number(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. - */ - async getUint16(offset: number | bigint): Promise { - const arrayBuffer = await this.getBytesFromFile(Number(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 start of the file where to read the data. - */ - async getUint32(offset: number | bigint): Promise { - const arrayBuffer = await this.getBytesFromFile(Number(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 bytes, from the start of the file where to read the data. - */ - async getBigUint64(offset: number | bigint): Promise { - const arrayBuffer = await this.getBytesFromFile(Number(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 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. - */ - async slice(startOffset: bigint, endOffset: bigint): Promise { - const bigLength = endOffset - startOffset; - if (bigLength > Number.MAX_SAFE_INTEGER) { - throw new Error('too big slice'); - } - const length = Number(bigLength); - - return await this.getBytesFromFile(Number(startOffset), length); - } - - /** - * the length (in bytes) of the data. - */ - get length(): bigint { - return BigInt(this.file.size); - } -} diff --git a/examples/experimental/slpk-in-browser/vite.config.ts b/examples/experimental/slpk-in-browser/vite.config.ts index b3a7da53e7..abf0685ea9 100644 --- a/examples/experimental/slpk-in-browser/vite.config.ts +++ b/examples/experimental/slpk-in-browser/vite.config.ts @@ -14,7 +14,7 @@ const getAliases = async (frameworkName, frameworkRootDir) => { // https://vitejs.dev/config/ export default defineConfig(async () => ({ - resolve: {alias: await getAliases('@loaders.gl', `${__dirname}/../../..`)}, + resolve: {alias: await getAliases('@loaders.gl', `${__dirname}/../..`)}, server: {open: true} })) diff --git a/modules/loader-utils/src/lib/files/blob-file.ts b/modules/loader-utils/src/lib/files/blob-file.ts index 7a54d7debf..88f157c4a1 100644 --- a/modules/loader-utils/src/lib/files/blob-file.ts +++ b/modules/loader-utils/src/lib/files/blob-file.ts @@ -30,8 +30,10 @@ export class BlobFile implements ReadableFile { }; } - async read(start: number, length: number): Promise { - const arrayBuffer = await this.handle.slice(start, start + length).arrayBuffer(); + async read(start?: number | bigint, length?: number): Promise { + const arrayBuffer = await this.handle + .slice(Number(start), Number(start) + Number(length)) + .arrayBuffer(); return arrayBuffer; } } diff --git a/modules/loader-utils/src/lib/files/http-file.ts b/modules/loader-utils/src/lib/files/http-file.ts index 57b13ba48f..8cc046cb8b 100644 --- a/modules/loader-utils/src/lib/files/http-file.ts +++ b/modules/loader-utils/src/lib/files/http-file.ts @@ -30,7 +30,7 @@ export class HttpFile implements ReadableFile { }; } - async read(offset: number | bigint, length: number): Promise { + async read(offset: number | bigint = 0, length: number = 0): Promise { const response = await this.fetchRange(offset, length); const arrayBuffer = await response.arrayBuffer(); return arrayBuffer; diff --git a/modules/loader-utils/src/lib/files/node-file-facade.ts b/modules/loader-utils/src/lib/files/node-file-facade.ts index 9ca124ba80..eb813bec76 100644 --- a/modules/loader-utils/src/lib/files/node-file-facade.ts +++ b/modules/loader-utils/src/lib/files/node-file-facade.ts @@ -25,7 +25,7 @@ export class NodeFileFacade implements ReadableFile, WritableFile { throw new Error('Can\'t instantiate NodeFile. Make sure to import @loaders.gl/polyfills first.'); } /** Read data */ - async read(start?: number | bigint, end?: number | bigint): Promise { + async read(start?: number | bigint, length?: number): Promise { throw NOT_IMPLEMENTED; } /** Write to file. The number of bytes written will be returned */