import { type OPFSAccessMode } from "./opfs_store"; export type { OPFSAccessMode } from "./opfs_store"; export interface TensorCacheEntry { name: string; shape: Array; dtype: string; format: "f32-to-bf16" | "raw"; byteOffset: number; nbytes: number; } export interface TensorShardEntry { dataPath: string; format: "raw-shard"; nbytes: number; records: Array; } /** * Return a borrowed view of one tensor record within a shard. * * The returned view aliases the shard and is only valid for as long as the * shard data remains alive. Tensor-cache decoding consumes it synchronously. */ export declare function getTensorCacheRecordBytes(shardData: ArrayBuffer | Uint8Array, record: Pick): Uint8Array; /** * Common Interface for the artifact cache */ export interface ArtifactCacheTemplate { /** * Retrieve data object that corresponds to `url` from cache. If data object does not exist in * cache, fetch the data and then add to cache. * * @param url The url to the data to be cached. * @param storetype This field is required so that `ArtifactIndexedDBCache` can store the * actual data object (see `addToCache()`), while `ArtifactCache` which uses the Cache API can * return the actual data object rather than the request. There are two options: * 1. "json": returns equivalent to `fetch(url).json()` * 2. "arraybuffer": returns equivalent to `fetch(url).arraybuffer()` * @param signal An optional AbortSignal allowing user to abort the fetching before its completion. * @return The data object (i.e. users do not need to call `.json()` or `.arraybuffer()`). * * Note: This is an async function. */ fetchWithCache(url: string, storetype?: string, signal?: AbortSignal): Promise; /** * Fetch data from url and add into cache. If already exists in cache, should return instantly. * * @param url The url to the data to be cached. * @param storetype Only applies to `ArtifactIndexedDBCache`. Since `indexedDB` stores the actual * @param signal An optional AbortSignal to abort data retrival. * data rather than a request, we specify `storagetype`. There are two options: * 1. "json": IndexedDB stores `fetch(url).json()` * 2. "arraybuffer": IndexedDB stores `fetch(url).arrayBuffer()` * * Note: This is an async function. */ addToCache(url: string, storetype?: string, signal?: AbortSignal): Promise; /** * check if cache has all keys in Cache * * Note: This is an async function. */ hasAllKeys(keys: string[]): Promise; /** * Delete url in cache if url exists * * Note: This is an async function. */ deleteInCache(url: string): Promise; } export type ArtifactCacheType = "cache" | "indexeddb" | "cross-origin" | "opfs"; export interface TensorCacheAccessOptions { cacheScope?: string; cacheType?: ArtifactCacheType; artifactCache?: ArtifactCacheTemplate; opfsAccessMode?: OPFSAccessMode; } type StoreType = string | undefined; type RequestLike = string | URL | Request | { url?: string; }; interface CrossOriginHashDescriptor { algorithm: string; value: string; } interface CrossOriginStorageHandle { getFile(): Promise; createWritable(): Promise; } interface CrossOriginStorageGetFileHandleOptions { create?: boolean; origins?: string[] | string | undefined; } interface CrossOriginStorageWritable { write(data: Blob): Promise; close(): Promise; } interface CrossOriginStorageAPI { getFileHandle(descriptor: CrossOriginHashDescriptor, options?: CrossOriginStorageGetFileHandleOptions): Promise; } declare global { interface Navigator { crossOriginStorage?: CrossOriginStorageAPI; } interface WorkerNavigator { crossOriginStorage?: CrossOriginStorageAPI; } } declare class CrossOriginStorage { private hashCache; constructor(); static isAvailable(): boolean; match(request: RequestLike): Promise; put(request: RequestLike, response: Response): Promise; delete(_request: RequestLike): Promise; private getApi; private normalizeRequest; private persistHashEntry; private loadPersistedHashEntry; private resolveHashDescriptor; private getFileHash; private extractHashFromPointer; private getBlobHash; } /** * Cache to store model related data, implemented with the Cache API. */ export declare class ArtifactCache implements ArtifactCacheTemplate { private scope; private cache?; constructor(scope: string); /** * Convert the Response object to the expected storetype instead */ responseTostoretype(response: Response, storetype?: string): Promise; /** * fetch the corresponding url object in response or stored object format * @param url url * @param storetype the storage type for indexedDB * @param signal an optional abort signal to abort fetching * @returns response in json, arraybuffer or pure response format */ fetchWithCache(url: string, storetype?: string, signal?: AbortSignal): Promise; addToCache(url: string, storetype?: string, signal?: AbortSignal): Promise; /** * Determine if all keys exist in the cache * @param keys the url key list of the strings * @returns boolean value indicate if all keys are in cache */ hasAllKeys(keys: string[]): Promise; /** * Delete the corresponding url object in cache * @param url the corresponding url object to be deleted */ deleteInCache(url: string): Promise; } /** * Cache by IndexedDB to support caching model data */ export declare class ArtifactIndexedDBCache implements ArtifactCacheTemplate { private dbName?; private dbVersion; private db; constructor(dbName: string); /** * Init the indexed DB database if it is not initialized. */ private initDB; /** * Check if current url object is in indexedDB or not * @param url the url link * @returns boolean indicate if url object in indexedDB */ private isUrlInDB; asyncGetHelper(url: string): Promise; fetchWithCache(url: string, storetype?: string, signal?: AbortSignal): Promise; addToIndexedDB(url: string, response: any, storetype?: string): Promise; addToCache(url: string, storetype?: string, signal?: AbortSignal): Promise; hasAllKeys(keys: string[]): Promise; deleteInCache(url: string): Promise; } /** * Cache by Origin Private File System (OPFS). */ export declare class ArtifactOPFSCache implements ArtifactCacheTemplate { private readonly store; constructor(scope: string, accessMode?: OPFSAccessMode); static isAvailable(): boolean; fetchWithCache(url: string, storetype?: string, signal?: AbortSignal): Promise; private readFromCache; addToCache(url: string, _storetype?: string, signal?: AbortSignal): Promise; hasAllKeys(keys: string[]): Promise; deleteInCache(url: string): Promise; private responseToStoreType; } /** * Cache by cross-origin storage extension. */ export declare class ArtifactCrossOriginStorageCache implements ArtifactCacheTemplate { private storage; constructor(_scope: string, storage?: CrossOriginStorage); fetchWithCache(url: string, storetype?: StoreType, signal?: AbortSignal): Promise; addToCache(url: string, _storetype?: StoreType, signal?: AbortSignal): Promise; hasAllKeys(keys: string[]): Promise; deleteInCache(url: string): Promise; private responseToStoreType; } export declare function createArtifactCache(scope: string, options?: TensorCacheAccessOptions): ArtifactCacheTemplate; /** * Function to check if NDarray is in Cache or not * * @param tensorCacheUrl The cache url which links to the Tensor * @param cacheScope The scope identifier of the cache * @param cacheType The type of the cache: "cache", "indexedDB", "cross-origin", or "opfs" * @returns the result if the cache has Tensor */ export declare function hasTensorInCache(tensorCacheUrl: string, options?: TensorCacheAccessOptions): Promise; export declare function hasTensorInCache(tensorCacheUrl: string, cacheScope?: string, cacheType?: string): Promise; /** * Given cacheUrl, search up items to delete based on cacheUrl/tensor-cache.json * * @param cacheUrl The cacheUrl for the items * @param cacheScope The scope identifier of the cache * @param cacheType The type of the cache: "cache", "indexedDB", "cross-origin", or "opfs" */ export declare function deleteTensorCache(cacheUrl: string, options?: TensorCacheAccessOptions): Promise; export declare function deleteTensorCache(cacheUrl: string, cacheScope?: string, cacheType?: string): Promise;