forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cache: add docs, expose, types, type tests * cache: implement CacheStorage.prototype.match * fix(cache): use correct webidl converter in CacheStorage.prototype.match
- Loading branch information
1 parent
77292b4
commit ca3c666
Showing
10 changed files
with
124 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# CacheStorage | ||
|
||
Undici exposes a W3C spec-compliant implementation of [CacheStorage](https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage) and [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache). | ||
|
||
## Opening a Cache | ||
|
||
Undici exports a top-level CacheStorage instance. You can open a new Cache, or duplicate a Cache with an existing name, by using `CacheStorage.prototype.open`. If you open a Cache with the same name as an already-existing Cache, its list of cached Responses will be shared between both instances. | ||
|
||
```mjs | ||
import { caches } from 'undici' | ||
|
||
const cache_1 = await caches.open('v1') | ||
const cache_2 = await caches.open('v1') | ||
|
||
// Although .open() creates a new instance, | ||
assert(cache_1 !== cache_2) | ||
// The same Response is matched in both. | ||
assert.deepStrictEqual(await cache_1.match('/req'), await cache_2.match('/req')) | ||
``` | ||
|
||
## Deleting a Cache | ||
|
||
If a Cache is deleted, the cached Responses/Requests can still be used. | ||
|
||
```mjs | ||
const response = await cache_1.match('/req') | ||
await caches.delete('v1') | ||
|
||
await response.text() // the Response's body | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expectAssignable } from 'tsd' | ||
import { | ||
caches, | ||
CacheStorage, | ||
Cache, | ||
CacheQueryOptions, | ||
MultiCacheQueryOptions, | ||
RequestInfo, | ||
Request, | ||
Response | ||
} from '../..' | ||
|
||
declare const response: Response | ||
declare const request: Request | ||
declare const options: RequestInfo | ||
declare const cache: Cache | ||
|
||
expectAssignable<CacheStorage>(caches) | ||
expectAssignable<MultiCacheQueryOptions>({}) | ||
expectAssignable<MultiCacheQueryOptions>({ cacheName: 'v1' }) | ||
expectAssignable<MultiCacheQueryOptions>({ ignoreMethod: false, ignoreSearch: true }) | ||
|
||
expectAssignable<CacheQueryOptions>({}) | ||
expectAssignable<CacheQueryOptions>({ ignoreVary: false, ignoreMethod: true, ignoreSearch: true }) | ||
|
||
expectAssignable<Promise<Cache>>(caches.open('v1')) | ||
expectAssignable<Promise<Response | undefined>>(caches.match(options)) | ||
expectAssignable<Promise<Response | undefined>>(caches.match(request)) | ||
expectAssignable<Promise<boolean>>(caches.has('v1')) | ||
expectAssignable<Promise<boolean>>(caches.delete('v1')) | ||
expectAssignable<Promise<string[]>>(caches.keys()) | ||
|
||
expectAssignable<Promise<Response | undefined>>(cache.match(options)) | ||
expectAssignable<Promise<readonly Response[]>>(cache.matchAll('v1')) | ||
expectAssignable<Promise<boolean>>(cache.delete('v1')) | ||
expectAssignable<Promise<readonly Request[]>>(cache.keys()) | ||
expectAssignable<Promise<undefined>>(cache.add(options)) | ||
expectAssignable<Promise<undefined>>(cache.addAll([options])) | ||
expectAssignable<Promise<undefined>>(cache.put(options, response)) |
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