@amazon/vinyl-cache-manager 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +271 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +251 -0
- package/dist/index.js.map +7 -0
- package/dist/types/current/cacheManager/CacheControl.d.ts +67 -0
- package/dist/types/current/cacheManager/CacheControl.d.ts.map +1 -0
- package/dist/types/current/cacheManager/CacheControl.js +47 -0
- package/dist/types/current/cacheManager/CacheManager.d.ts +119 -0
- package/dist/types/current/cacheManager/CacheManager.d.ts.map +1 -0
- package/dist/types/current/cacheManager/CacheManager.js +221 -0
- package/dist/types/current/index.d.ts +4 -0
- package/dist/types/current/index.d.ts.map +1 -0
- package/dist/types/current/index.js +3 -0
- package/dist/types/current/tsconfig.tsbuildinfo +1 -0
- package/dist/types/ts3.7/cacheManager/CacheControl.d.ts +67 -0
- package/dist/types/ts3.7/cacheManager/CacheManager.d.ts +119 -0
- package/dist/types/ts3.7/index.d.ts +4 -0
- package/dist/types/ts4.1/cacheManager/CacheControl.d.ts +67 -0
- package/dist/types/ts4.1/cacheManager/CacheManager.d.ts +119 -0
- package/dist/types/ts4.1/index.d.ts +4 -0
- package/dist/types/ts4.7/cacheManager/CacheControl.d.ts +67 -0
- package/dist/types/ts4.7/cacheManager/CacheManager.d.ts +119 -0
- package/dist/types/ts4.7/index.d.ts +4 -0
- package/package.json +81 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { CacheControl } from './CacheControl';
|
|
2
|
+
export declare const DEFAULT_CACHE_NAME = "offlineCache";
|
|
3
|
+
export declare const DEFAULT_PURGE_AFTER_STALE: number;
|
|
4
|
+
/**
|
|
5
|
+
* If the cache control options set the onlyIfCached directive and the request is not cached, an error will be thrown.
|
|
6
|
+
*/
|
|
7
|
+
export declare const NOT_CACHED_ERROR = "Not cached";
|
|
8
|
+
export type CacheManagerOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* The name of the cache key.
|
|
11
|
+
*/
|
|
12
|
+
readonly name?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The Fetch from network method. Defaults to window.fetch.
|
|
15
|
+
*/
|
|
16
|
+
readonly requestFromNetwork?: Fetch;
|
|
17
|
+
};
|
|
18
|
+
type Fetch = (request: RequestInfo) => Promise<Response>;
|
|
19
|
+
export type CacheEntryOptions = {
|
|
20
|
+
/**
|
|
21
|
+
* If set, the response will be cached under this identifier, not based on the request.
|
|
22
|
+
* This is required for caching POST requests.
|
|
23
|
+
*/
|
|
24
|
+
readonly cacheKey?: string | null | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Cache control directive defaults.
|
|
27
|
+
* These will be used unless overridden by the response headers or the cache control overrides.
|
|
28
|
+
*
|
|
29
|
+
* CacheControl directives will be overwritten in the order:
|
|
30
|
+
* - cacheControlDefaults
|
|
31
|
+
* - response headers
|
|
32
|
+
* - cacheControlOverrides
|
|
33
|
+
*/
|
|
34
|
+
readonly cacheControlDefaults?: CacheControl;
|
|
35
|
+
/**
|
|
36
|
+
* Allows the client to set cache control overrides. This object is merged with the actual cache control header.
|
|
37
|
+
*
|
|
38
|
+
* For example, to allow cached items to be used 1 day after they are stale while asynchronously revalidating
|
|
39
|
+
* the cache, set `{ staleWhileRevalidate: 24 * 60 * 60 }`
|
|
40
|
+
*
|
|
41
|
+
* These overrides do not change the header cached, only how the cached header is read.
|
|
42
|
+
* If noStore is true, the entry will not be cached.
|
|
43
|
+
*/
|
|
44
|
+
readonly cacheControlOverrides?: CacheControl;
|
|
45
|
+
/**
|
|
46
|
+
* If set, checks the response if it should be used. This affects whether to cache and whether to use the
|
|
47
|
+
* cached fallback.
|
|
48
|
+
*/
|
|
49
|
+
readonly responseIsValid?: (response: Response) => Promise<boolean>;
|
|
50
|
+
};
|
|
51
|
+
export type CacheCleanOptions = {
|
|
52
|
+
/**
|
|
53
|
+
* The number of seconds after an entry becomes stale before it should be removed from the cache.
|
|
54
|
+
* Default: 7 * 24 * 60 * 60 (7 days).
|
|
55
|
+
* If the cached entry has a stale-while-revalidate cache control directive, this will be ignored.
|
|
56
|
+
*/
|
|
57
|
+
readonly purgeAfterStale?: number;
|
|
58
|
+
};
|
|
59
|
+
export interface CacheManagerDeps {
|
|
60
|
+
/**
|
|
61
|
+
* The cache storages api.
|
|
62
|
+
* Default: window.caches
|
|
63
|
+
*/
|
|
64
|
+
readonly caches?: CacheStorage;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a Headers object
|
|
67
|
+
*/
|
|
68
|
+
readonly headersFactory?: (headersInit?: HeadersInit) => Headers;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a Response object
|
|
71
|
+
*/
|
|
72
|
+
readonly responseFactory?: (body?: BodyInit | null, init?: ResponseInit) => Response;
|
|
73
|
+
}
|
|
74
|
+
export declare class CacheManager {
|
|
75
|
+
readonly name: string;
|
|
76
|
+
private readonly caches;
|
|
77
|
+
private cache;
|
|
78
|
+
private readonly fetch;
|
|
79
|
+
/**
|
|
80
|
+
* If set, when asynchronous caching has failed, this callback will be invoked.
|
|
81
|
+
* This is not invoked when a promise rejects during an operation.
|
|
82
|
+
*/
|
|
83
|
+
onError?: (error: Error) => void;
|
|
84
|
+
constructor(options?: CacheManagerOptions, deps?: CacheManagerDeps);
|
|
85
|
+
private readonly errorHandler;
|
|
86
|
+
/**
|
|
87
|
+
* Caches a response with added expiry headers.
|
|
88
|
+
*/
|
|
89
|
+
put(cacheId: RequestInfo, response: Response): Promise<Response>;
|
|
90
|
+
/**
|
|
91
|
+
* Deletes the request from this cache.
|
|
92
|
+
*
|
|
93
|
+
* @param cacheId The request or cache id used as the cache key.
|
|
94
|
+
*/
|
|
95
|
+
delete(cacheId: RequestInfo): Promise<boolean>;
|
|
96
|
+
/**
|
|
97
|
+
* Resolves to true if the request is cached.
|
|
98
|
+
*
|
|
99
|
+
* @param cacheId
|
|
100
|
+
*/
|
|
101
|
+
has(cacheId: RequestInfo): Promise<boolean>;
|
|
102
|
+
get(request: RequestInfo, options?: CacheEntryOptions): Promise<Response>;
|
|
103
|
+
/**
|
|
104
|
+
* Returns a Response if it exists in cache.
|
|
105
|
+
*
|
|
106
|
+
* @param request
|
|
107
|
+
*/
|
|
108
|
+
private getCached;
|
|
109
|
+
/**
|
|
110
|
+
* @returns a promise that resolves to true if the Cache object is found and deleted, and false otherwise.
|
|
111
|
+
*/
|
|
112
|
+
clear(): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* Cleans stale items from the cache.
|
|
115
|
+
*/
|
|
116
|
+
clean(options?: CacheCleanOptions): Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
export {};
|
|
119
|
+
//# sourceMappingURL=CacheManager.d.ts.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents all known Cache-Control directives.
|
|
3
|
+
*
|
|
4
|
+
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
5
|
+
*/
|
|
6
|
+
export type CacheControl = {
|
|
7
|
+
/**
|
|
8
|
+
* The maximum amount of time (in seconds) a resource is considered fresh.
|
|
9
|
+
* Example: `max-age=3600` (1 hour)
|
|
10
|
+
*/
|
|
11
|
+
readonly maxAge?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Requires the resource to be revalidated with the origin server before serving it from the cache.
|
|
14
|
+
* Example: `no-cache`
|
|
15
|
+
*/
|
|
16
|
+
readonly noCache?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Prevents the resource from being cached by any mechanism.
|
|
19
|
+
* Example: `no-store`
|
|
20
|
+
*/
|
|
21
|
+
readonly noStore?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Ensures that stale responses cannot be used without successful revalidation with the origin server.
|
|
24
|
+
* Example: `must-revalidate`
|
|
25
|
+
*/
|
|
26
|
+
readonly mustRevalidate?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Indicates the resource is cacheable by both shared and private caches.
|
|
29
|
+
* Example: `public`
|
|
30
|
+
*/
|
|
31
|
+
readonly public?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Indicates the resource is cacheable only by the user's browser or private caches, not shared caches.
|
|
34
|
+
* Example: `private`
|
|
35
|
+
*/
|
|
36
|
+
readonly private?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Indicates that the resource will not change, so it does not need to be revalidated.
|
|
39
|
+
* Example: `immutable`
|
|
40
|
+
*/
|
|
41
|
+
readonly immutable?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Allows serving stale responses for the specified duration (in seconds) while asynchronously revalidating the
|
|
44
|
+
* cache.
|
|
45
|
+
* Example: `stale-while-revalidate=60`
|
|
46
|
+
*/
|
|
47
|
+
readonly staleWhileRevalidate?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Allows serving stale responses (in seconds) if the origin server is unavailable or returns an error.
|
|
50
|
+
* Example: `stale-if-error=86400` (1 day)
|
|
51
|
+
*/
|
|
52
|
+
readonly staleIfError?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Indicates the client only wants a response from the cache and will not make a network request.
|
|
55
|
+
* Example: `only-if-cached`
|
|
56
|
+
*/
|
|
57
|
+
readonly onlyIfCached?: boolean;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Parses a Cache-Control header string into a structured object.
|
|
61
|
+
* Handles known directives with optional numeric values.
|
|
62
|
+
*
|
|
63
|
+
* @param str - The Cache-Control header string to parse.
|
|
64
|
+
* @returns A CacheControl object representing the parsed directives.
|
|
65
|
+
*/
|
|
66
|
+
export declare function parseCacheControl(str: string | null | undefined): CacheControl;
|
|
67
|
+
//# sourceMappingURL=CacheControl.d.ts.map
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { CacheControl } from './CacheControl';
|
|
2
|
+
export declare const DEFAULT_CACHE_NAME = "offlineCache";
|
|
3
|
+
export declare const DEFAULT_PURGE_AFTER_STALE: number;
|
|
4
|
+
/**
|
|
5
|
+
* If the cache control options set the onlyIfCached directive and the request is not cached, an error will be thrown.
|
|
6
|
+
*/
|
|
7
|
+
export declare const NOT_CACHED_ERROR = "Not cached";
|
|
8
|
+
export type CacheManagerOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* The name of the cache key.
|
|
11
|
+
*/
|
|
12
|
+
readonly name?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The Fetch from network method. Defaults to window.fetch.
|
|
15
|
+
*/
|
|
16
|
+
readonly requestFromNetwork?: Fetch;
|
|
17
|
+
};
|
|
18
|
+
type Fetch = (request: RequestInfo) => Promise<Response>;
|
|
19
|
+
export type CacheEntryOptions = {
|
|
20
|
+
/**
|
|
21
|
+
* If set, the response will be cached under this identifier, not based on the request.
|
|
22
|
+
* This is required for caching POST requests.
|
|
23
|
+
*/
|
|
24
|
+
readonly cacheKey?: string | null | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Cache control directive defaults.
|
|
27
|
+
* These will be used unless overridden by the response headers or the cache control overrides.
|
|
28
|
+
*
|
|
29
|
+
* CacheControl directives will be overwritten in the order:
|
|
30
|
+
* - cacheControlDefaults
|
|
31
|
+
* - response headers
|
|
32
|
+
* - cacheControlOverrides
|
|
33
|
+
*/
|
|
34
|
+
readonly cacheControlDefaults?: CacheControl;
|
|
35
|
+
/**
|
|
36
|
+
* Allows the client to set cache control overrides. This object is merged with the actual cache control header.
|
|
37
|
+
*
|
|
38
|
+
* For example, to allow cached items to be used 1 day after they are stale while asynchronously revalidating
|
|
39
|
+
* the cache, set `{ staleWhileRevalidate: 24 * 60 * 60 }`
|
|
40
|
+
*
|
|
41
|
+
* These overrides do not change the header cached, only how the cached header is read.
|
|
42
|
+
* If noStore is true, the entry will not be cached.
|
|
43
|
+
*/
|
|
44
|
+
readonly cacheControlOverrides?: CacheControl;
|
|
45
|
+
/**
|
|
46
|
+
* If set, checks the response if it should be used. This affects whether to cache and whether to use the
|
|
47
|
+
* cached fallback.
|
|
48
|
+
*/
|
|
49
|
+
readonly responseIsValid?: (response: Response) => Promise<boolean>;
|
|
50
|
+
};
|
|
51
|
+
export type CacheCleanOptions = {
|
|
52
|
+
/**
|
|
53
|
+
* The number of seconds after an entry becomes stale before it should be removed from the cache.
|
|
54
|
+
* Default: 7 * 24 * 60 * 60 (7 days).
|
|
55
|
+
* If the cached entry has a stale-while-revalidate cache control directive, this will be ignored.
|
|
56
|
+
*/
|
|
57
|
+
readonly purgeAfterStale?: number;
|
|
58
|
+
};
|
|
59
|
+
export interface CacheManagerDeps {
|
|
60
|
+
/**
|
|
61
|
+
* The cache storages api.
|
|
62
|
+
* Default: window.caches
|
|
63
|
+
*/
|
|
64
|
+
readonly caches?: CacheStorage;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a Headers object
|
|
67
|
+
*/
|
|
68
|
+
readonly headersFactory?: (headersInit?: HeadersInit) => Headers;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a Response object
|
|
71
|
+
*/
|
|
72
|
+
readonly responseFactory?: (body?: BodyInit | null, init?: ResponseInit) => Response;
|
|
73
|
+
}
|
|
74
|
+
export declare class CacheManager {
|
|
75
|
+
readonly name: string;
|
|
76
|
+
private readonly caches;
|
|
77
|
+
private cache;
|
|
78
|
+
private readonly fetch;
|
|
79
|
+
/**
|
|
80
|
+
* If set, when asynchronous caching has failed, this callback will be invoked.
|
|
81
|
+
* This is not invoked when a promise rejects during an operation.
|
|
82
|
+
*/
|
|
83
|
+
onError?: (error: Error) => void;
|
|
84
|
+
constructor(options?: CacheManagerOptions, deps?: CacheManagerDeps);
|
|
85
|
+
private readonly errorHandler;
|
|
86
|
+
/**
|
|
87
|
+
* Caches a response with added expiry headers.
|
|
88
|
+
*/
|
|
89
|
+
put(cacheId: RequestInfo, response: Response): Promise<Response>;
|
|
90
|
+
/**
|
|
91
|
+
* Deletes the request from this cache.
|
|
92
|
+
*
|
|
93
|
+
* @param cacheId The request or cache id used as the cache key.
|
|
94
|
+
*/
|
|
95
|
+
delete(cacheId: RequestInfo): Promise<boolean>;
|
|
96
|
+
/**
|
|
97
|
+
* Resolves to true if the request is cached.
|
|
98
|
+
*
|
|
99
|
+
* @param cacheId
|
|
100
|
+
*/
|
|
101
|
+
has(cacheId: RequestInfo): Promise<boolean>;
|
|
102
|
+
get(request: RequestInfo, options?: CacheEntryOptions): Promise<Response>;
|
|
103
|
+
/**
|
|
104
|
+
* Returns a Response if it exists in cache.
|
|
105
|
+
*
|
|
106
|
+
* @param request
|
|
107
|
+
*/
|
|
108
|
+
private getCached;
|
|
109
|
+
/**
|
|
110
|
+
* @returns a promise that resolves to true if the Cache object is found and deleted, and false otherwise.
|
|
111
|
+
*/
|
|
112
|
+
clear(): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* Cleans stale items from the cache.
|
|
115
|
+
*/
|
|
116
|
+
clean(options?: CacheCleanOptions): Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
export {};
|
|
119
|
+
//# sourceMappingURL=CacheManager.d.ts.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents all known Cache-Control directives.
|
|
3
|
+
*
|
|
4
|
+
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
5
|
+
*/
|
|
6
|
+
export type CacheControl = {
|
|
7
|
+
/**
|
|
8
|
+
* The maximum amount of time (in seconds) a resource is considered fresh.
|
|
9
|
+
* Example: `max-age=3600` (1 hour)
|
|
10
|
+
*/
|
|
11
|
+
readonly maxAge?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Requires the resource to be revalidated with the origin server before serving it from the cache.
|
|
14
|
+
* Example: `no-cache`
|
|
15
|
+
*/
|
|
16
|
+
readonly noCache?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Prevents the resource from being cached by any mechanism.
|
|
19
|
+
* Example: `no-store`
|
|
20
|
+
*/
|
|
21
|
+
readonly noStore?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Ensures that stale responses cannot be used without successful revalidation with the origin server.
|
|
24
|
+
* Example: `must-revalidate`
|
|
25
|
+
*/
|
|
26
|
+
readonly mustRevalidate?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Indicates the resource is cacheable by both shared and private caches.
|
|
29
|
+
* Example: `public`
|
|
30
|
+
*/
|
|
31
|
+
readonly public?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Indicates the resource is cacheable only by the user's browser or private caches, not shared caches.
|
|
34
|
+
* Example: `private`
|
|
35
|
+
*/
|
|
36
|
+
readonly private?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Indicates that the resource will not change, so it does not need to be revalidated.
|
|
39
|
+
* Example: `immutable`
|
|
40
|
+
*/
|
|
41
|
+
readonly immutable?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Allows serving stale responses for the specified duration (in seconds) while asynchronously revalidating the
|
|
44
|
+
* cache.
|
|
45
|
+
* Example: `stale-while-revalidate=60`
|
|
46
|
+
*/
|
|
47
|
+
readonly staleWhileRevalidate?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Allows serving stale responses (in seconds) if the origin server is unavailable or returns an error.
|
|
50
|
+
* Example: `stale-if-error=86400` (1 day)
|
|
51
|
+
*/
|
|
52
|
+
readonly staleIfError?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Indicates the client only wants a response from the cache and will not make a network request.
|
|
55
|
+
* Example: `only-if-cached`
|
|
56
|
+
*/
|
|
57
|
+
readonly onlyIfCached?: boolean;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Parses a Cache-Control header string into a structured object.
|
|
61
|
+
* Handles known directives with optional numeric values.
|
|
62
|
+
*
|
|
63
|
+
* @param str - The Cache-Control header string to parse.
|
|
64
|
+
* @returns A CacheControl object representing the parsed directives.
|
|
65
|
+
*/
|
|
66
|
+
export declare function parseCacheControl(str: string | null | undefined): CacheControl;
|
|
67
|
+
//# sourceMappingURL=CacheControl.d.ts.map
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { type CacheControl } from './CacheControl';
|
|
2
|
+
export declare const DEFAULT_CACHE_NAME = "offlineCache";
|
|
3
|
+
export declare const DEFAULT_PURGE_AFTER_STALE: number;
|
|
4
|
+
/**
|
|
5
|
+
* If the cache control options set the onlyIfCached directive and the request is not cached, an error will be thrown.
|
|
6
|
+
*/
|
|
7
|
+
export declare const NOT_CACHED_ERROR = "Not cached";
|
|
8
|
+
export type CacheManagerOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* The name of the cache key.
|
|
11
|
+
*/
|
|
12
|
+
readonly name?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The Fetch from network method. Defaults to window.fetch.
|
|
15
|
+
*/
|
|
16
|
+
readonly requestFromNetwork?: Fetch;
|
|
17
|
+
};
|
|
18
|
+
type Fetch = (request: RequestInfo) => Promise<Response>;
|
|
19
|
+
export type CacheEntryOptions = {
|
|
20
|
+
/**
|
|
21
|
+
* If set, the response will be cached under this identifier, not based on the request.
|
|
22
|
+
* This is required for caching POST requests.
|
|
23
|
+
*/
|
|
24
|
+
readonly cacheKey?: string | null | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Cache control directive defaults.
|
|
27
|
+
* These will be used unless overridden by the response headers or the cache control overrides.
|
|
28
|
+
*
|
|
29
|
+
* CacheControl directives will be overwritten in the order:
|
|
30
|
+
* - cacheControlDefaults
|
|
31
|
+
* - response headers
|
|
32
|
+
* - cacheControlOverrides
|
|
33
|
+
*/
|
|
34
|
+
readonly cacheControlDefaults?: CacheControl;
|
|
35
|
+
/**
|
|
36
|
+
* Allows the client to set cache control overrides. This object is merged with the actual cache control header.
|
|
37
|
+
*
|
|
38
|
+
* For example, to allow cached items to be used 1 day after they are stale while asynchronously revalidating
|
|
39
|
+
* the cache, set `{ staleWhileRevalidate: 24 * 60 * 60 }`
|
|
40
|
+
*
|
|
41
|
+
* These overrides do not change the header cached, only how the cached header is read.
|
|
42
|
+
* If noStore is true, the entry will not be cached.
|
|
43
|
+
*/
|
|
44
|
+
readonly cacheControlOverrides?: CacheControl;
|
|
45
|
+
/**
|
|
46
|
+
* If set, checks the response if it should be used. This affects whether to cache and whether to use the
|
|
47
|
+
* cached fallback.
|
|
48
|
+
*/
|
|
49
|
+
readonly responseIsValid?: (response: Response) => Promise<boolean>;
|
|
50
|
+
};
|
|
51
|
+
export type CacheCleanOptions = {
|
|
52
|
+
/**
|
|
53
|
+
* The number of seconds after an entry becomes stale before it should be removed from the cache.
|
|
54
|
+
* Default: 7 * 24 * 60 * 60 (7 days).
|
|
55
|
+
* If the cached entry has a stale-while-revalidate cache control directive, this will be ignored.
|
|
56
|
+
*/
|
|
57
|
+
readonly purgeAfterStale?: number;
|
|
58
|
+
};
|
|
59
|
+
export interface CacheManagerDeps {
|
|
60
|
+
/**
|
|
61
|
+
* The cache storages api.
|
|
62
|
+
* Default: window.caches
|
|
63
|
+
*/
|
|
64
|
+
readonly caches?: CacheStorage;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a Headers object
|
|
67
|
+
*/
|
|
68
|
+
readonly headersFactory?: (headersInit?: HeadersInit) => Headers;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a Response object
|
|
71
|
+
*/
|
|
72
|
+
readonly responseFactory?: (body?: BodyInit | null, init?: ResponseInit) => Response;
|
|
73
|
+
}
|
|
74
|
+
export declare class CacheManager {
|
|
75
|
+
readonly name: string;
|
|
76
|
+
private readonly caches;
|
|
77
|
+
private cache;
|
|
78
|
+
private readonly fetch;
|
|
79
|
+
/**
|
|
80
|
+
* If set, when asynchronous caching has failed, this callback will be invoked.
|
|
81
|
+
* This is not invoked when a promise rejects during an operation.
|
|
82
|
+
*/
|
|
83
|
+
onError?: (error: Error) => void;
|
|
84
|
+
constructor(options?: CacheManagerOptions, deps?: CacheManagerDeps);
|
|
85
|
+
private readonly errorHandler;
|
|
86
|
+
/**
|
|
87
|
+
* Caches a response with added expiry headers.
|
|
88
|
+
*/
|
|
89
|
+
put(cacheId: RequestInfo, response: Response): Promise<Response>;
|
|
90
|
+
/**
|
|
91
|
+
* Deletes the request from this cache.
|
|
92
|
+
*
|
|
93
|
+
* @param cacheId The request or cache id used as the cache key.
|
|
94
|
+
*/
|
|
95
|
+
delete(cacheId: RequestInfo): Promise<boolean>;
|
|
96
|
+
/**
|
|
97
|
+
* Resolves to true if the request is cached.
|
|
98
|
+
*
|
|
99
|
+
* @param cacheId
|
|
100
|
+
*/
|
|
101
|
+
has(cacheId: RequestInfo): Promise<boolean>;
|
|
102
|
+
get(request: RequestInfo, options?: CacheEntryOptions): Promise<Response>;
|
|
103
|
+
/**
|
|
104
|
+
* Returns a Response if it exists in cache.
|
|
105
|
+
*
|
|
106
|
+
* @param request
|
|
107
|
+
*/
|
|
108
|
+
private getCached;
|
|
109
|
+
/**
|
|
110
|
+
* @returns a promise that resolves to true if the Cache object is found and deleted, and false otherwise.
|
|
111
|
+
*/
|
|
112
|
+
clear(): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* Cleans stale items from the cache.
|
|
115
|
+
*/
|
|
116
|
+
clean(options?: CacheCleanOptions): Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
export {};
|
|
119
|
+
//# sourceMappingURL=CacheManager.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amazon/vinyl-cache-manager",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A mediator for the Cache API",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/amazonmusic/vinyl.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "UNLICENSED",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"types": "./dist/types/ts3.7/index.d.ts",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": [
|
|
15
|
+
"test/**/*"
|
|
16
|
+
],
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/*",
|
|
19
|
+
"docs/*"
|
|
20
|
+
],
|
|
21
|
+
"typesVersions": {
|
|
22
|
+
">=5.4": {
|
|
23
|
+
"*": [
|
|
24
|
+
"./dist/types/current/index.d.ts"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
">=4.7": {
|
|
28
|
+
"*": [
|
|
29
|
+
"./dist/types/ts4.7/index.d.ts"
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
">=4.1": {
|
|
33
|
+
"*": [
|
|
34
|
+
"./dist/types/ts4.1/index.d.ts"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
">=3.7": {
|
|
38
|
+
"*": [
|
|
39
|
+
"./dist/types/ts3.7/index.d.ts"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"types@>=5.4": "./dist/types/current/index.d.ts",
|
|
46
|
+
"types@>=4.7": "./dist/types/ts4.7/index.d.ts",
|
|
47
|
+
"require": "./dist/index.cjs",
|
|
48
|
+
"import": "./dist/index.js"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"analyzeExports": "tsx buildSrc/analyzeExports.ts",
|
|
53
|
+
"analyzeExports:write": "tsx buildSrc/analyzeExports.ts --write",
|
|
54
|
+
"clean": "rimraf --glob dist .eslintcache \"**/*.tsbuildinfo\"",
|
|
55
|
+
"indexFiles:check": "tsx buildSrc/writeIndexFiles.ts --check",
|
|
56
|
+
"indexFiles:write": "tsx buildSrc/writeIndexFiles.ts",
|
|
57
|
+
"build": "tsx buildSrc/build.ts",
|
|
58
|
+
"build:release": "tsx buildSrc/build.ts --release",
|
|
59
|
+
"prettier:check": "prettier . --ignore-path ../../.prettierignore --cache --check",
|
|
60
|
+
"prettier:write": "prettier . --ignore-path ../../.prettierignore --cache --write",
|
|
61
|
+
"eslint:check": "eslint . --cache --max-warnings=0",
|
|
62
|
+
"eslint:write": "eslint . --cache --fix",
|
|
63
|
+
"lint": "npm run indexFiles:check && npm run eslint:check && npm run prettier:check",
|
|
64
|
+
"lint:write": "npm run indexFiles:write && npm run eslint:write && npm run prettier:write",
|
|
65
|
+
"validate": "tsc --project test/unit && npm run lint && npm run test",
|
|
66
|
+
"test": "tsx --tsconfig ../../tsconfig.flat.json ../../node_modules/jasmine/bin/jasmine --config=test/unit/jasmine.config.json",
|
|
67
|
+
"test:coverage": "c8 npm run test",
|
|
68
|
+
"watch": "tsx buildSrc/build.ts --watch",
|
|
69
|
+
"watch:release": "tsx buildSrc/build.ts --watch --release"
|
|
70
|
+
},
|
|
71
|
+
"devDependencies": {
|
|
72
|
+
"@amazon/vinyl-build-utils": "^1.0.0",
|
|
73
|
+
"@amazon/vinyl-jasmine-wrapper": "^1.0.0"
|
|
74
|
+
},
|
|
75
|
+
"dependencies": {
|
|
76
|
+
"@amazon/vinyl-util": "^1.0.0"
|
|
77
|
+
},
|
|
78
|
+
"browserslist": [
|
|
79
|
+
"defaults, chrome 52, firefox 54, edge 15, safari 8"
|
|
80
|
+
]
|
|
81
|
+
}
|