An HTTP/1.1 client, written from scratch for Node.js
—
HTTP caching implementation with multiple storage backends and standards-compliant cache behavior following WHATWG CacheStorage specification.
Global cache storage instance providing access to named Cache instances.
/**
* Global cache storage instance
*/
const caches: CacheStorage;
/**
* Cache storage management system
*/
class CacheStorage {
/**
* Opens a named cache, creating it if it doesn't exist
* @param cacheName - Name of the cache to open
* @returns Promise resolving to Cache instance
*/
open(cacheName: string): Promise<Cache>;
/**
* Checks if a named cache exists
* @param cacheName - Name of the cache to check
* @returns Promise resolving to boolean
*/
has(cacheName: string): Promise<boolean>;
/**
* Deletes a named cache
* @param cacheName - Name of the cache to delete
* @returns Promise resolving to boolean indicating success
*/
delete(cacheName: string): Promise<boolean>;
/**
* Returns an array of cache names
* @returns Promise resolving to array of cache names
*/
keys(): Promise<string[]>;
}Usage Examples:
import { caches } from 'undici';
// Open a cache
const cache = await caches.open('api-responses');
// Store a response
const response = await fetch('https://api.example.com/data');
await cache.put('https://api.example.com/data', response.clone());
// Retrieve cached response
const cachedResponse = await cache.match('https://api.example.com/data');
if (cachedResponse) {
const data = await cachedResponse.json();
}
// Check if cache exists
const exists = await caches.has('api-responses');
// Delete cache
await caches.delete('api-responses');Individual cache instance for storing Request/Response pairs.
/**
* Individual cache instance
*/
class Cache {
/**
* Adds a request/response pair to the cache
* @param request - Request to use as key
* @param response - Response to cache
* @returns Promise resolving when stored
*/
put(request: RequestInfo, response: Response): Promise<void>;
/**
* Retrieves a cached response for the request
* @param request - Request to match
* @param options - Match options
* @returns Promise resolving to Response or undefined
*/
match(request: RequestInfo, options?: CacheQueryOptions): Promise<Response | undefined>;
/**
* Retrieves all cached responses matching the request
* @param request - Request to match (optional)
* @param options - Match options
* @returns Promise resolving to array of Responses
*/
matchAll(request?: RequestInfo, options?: CacheQueryOptions): Promise<Response[]>;
/**
* Adds a request to the cache, fetching the response
* @param request - Request to fetch and cache
* @returns Promise resolving when cached
*/
add(request: RequestInfo): Promise<void>;
/**
* Adds multiple requests to the cache
* @param requests - Array of requests to fetch and cache
* @returns Promise resolving when all are cached
*/
addAll(requests: RequestInfo[]): Promise<void>;
/**
* Deletes cached entries matching the request
* @param request - Request to match
* @param options - Match options
* @returns Promise resolving to boolean indicating if entries were deleted
*/
delete(request: RequestInfo, options?: CacheQueryOptions): Promise<boolean>;
/**
* Returns all cached request objects
* @param request - Request to match (optional)
* @param options - Match options
* @returns Promise resolving to array of Requests
*/
keys(request?: RequestInfo, options?: CacheQueryOptions): Promise<Request[]>;
}
interface CacheQueryOptions {
ignoreSearch?: boolean;
ignoreMethod?: boolean;
ignoreVary?: boolean;
}Multiple storage backend implementations for different persistence requirements.
const cacheStores: {
/**
* In-memory cache store implementation
*/
MemoryCacheStore: typeof MemoryCacheStore;
/**
* SQLite-based persistent cache store implementation
*/
SqliteCacheStore: typeof SqliteCacheStore;
};
/**
* In-memory cache implementation
*/
class MemoryCacheStore {
constructor(options?: MemoryCacheStoreOptions);
}
/**
* SQLite-based persistent cache implementation
*/
class SqliteCacheStore {
constructor(options?: SqliteCacheStoreOptions);
}
interface MemoryCacheStoreOptions {
maxSize?: number;
maxAge?: number;
}
interface SqliteCacheStoreOptions {
location?: string;
maxSize?: number;
}Usage Examples:
import { cacheStores } from 'undici';
// Create custom cache store
const memoryStore = new cacheStores.MemoryCacheStore({
maxSize: 100,
maxAge: 300000 // 5 minutes
});
const sqliteStore = new cacheStores.SqliteCacheStore({
location: './cache.db',
maxSize: 1000
});Install with Tessl CLI
npx tessl i tessl/npm-undici