motion-canvas-cache 0.1.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/LICENSE +21 -0
- package/README.md +367 -0
- package/dist/Cache.d.ts +156 -0
- package/dist/Cache.d.ts.map +1 -0
- package/dist/CacheUtils.d.ts +20 -0
- package/dist/CacheUtils.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +572 -0
- package/dist/index.js.map +1 -0
- package/dist/vite-plugin/plugin.d.ts +7 -0
- package/dist/vite-plugin/plugin.d.ts.map +1 -0
- package/dist/vite-plugin.js +164 -0
- package/dist/vite-plugin.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Patrick Rathje
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
# Motion Canvas Cache
|
|
2
|
+
|
|
3
|
+
A generic file caching library for Motion Canvas projects that provides both in-memory and server-side caching via Vite's HMR WebSocket.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Dual-layer caching**: In-memory cache for fast access + server-side cache for persistence
|
|
8
|
+
- **Simple API**: Single `cached()` function that handles everything
|
|
9
|
+
- **URL or Blob support**: Cache files from URLs or directly from Blob objects
|
|
10
|
+
- **Auto MIME type detection**: Automatically detects content type from response headers
|
|
11
|
+
- **Custom metadata**: Store arbitrary metadata alongside cached files
|
|
12
|
+
- **HMR persistence**: Cache persists across hot module reloads
|
|
13
|
+
- **Works standalone**: In-memory cache works even without the server plugin
|
|
14
|
+
- **Generic file support**: Supports any file type (audio, video, images, documents, etc.)
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install motion-canvas-cache
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### 1. Enable the Vite Plugin
|
|
25
|
+
|
|
26
|
+
Add the plugin to your `vite.config.ts`:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import {defineConfig} from 'vite';
|
|
30
|
+
import motionCanvas from '@motion-canvas/vite-plugin';
|
|
31
|
+
import {motionCanvasCachePlugin} from 'motion-canvas-cache';
|
|
32
|
+
|
|
33
|
+
export default defineConfig({
|
|
34
|
+
plugins: [
|
|
35
|
+
motionCanvas(),
|
|
36
|
+
motionCanvasCachePlugin({
|
|
37
|
+
cachePath: 'motion-canvas-cache', // Optional: default cache directory
|
|
38
|
+
maxFileSize: 50, // Optional: max file size in MB (default: 50)
|
|
39
|
+
}),
|
|
40
|
+
]
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. Use in Motion Canvas
|
|
45
|
+
|
|
46
|
+
Since Motion Canvas uses generator functions and you can't use `yield*` inside JSX, the pattern is:
|
|
47
|
+
|
|
48
|
+
1. **Preload assets** at the beginning of your scene using `yield cache()`
|
|
49
|
+
2. **Use synchronously** in JSX with `cached()`
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import {makeScene2D, Img, Audio} from '@motion-canvas/2d';
|
|
53
|
+
import {cache, cached} from 'motion-canvas-cache';
|
|
54
|
+
|
|
55
|
+
export default makeScene2D(function* (view) {
|
|
56
|
+
// 1. Preload all assets at the beginning using yield
|
|
57
|
+
yield cache('https://example.com/image.png', {
|
|
58
|
+
metadata: {width: 800, height: 600},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
yield cache('https://example.com/audio.mp3', {
|
|
62
|
+
metadata: {duration: 3.5},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// 2. Use synchronously in JSX - no yield needed!
|
|
66
|
+
view.add(
|
|
67
|
+
<Img src={cached('https://example.com/image.png')} />
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
view.add(
|
|
71
|
+
<Audio src={cached('https://example.com/audio.mp3')} />
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Alternative: Preload in variables first**
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
export default makeScene2D(function* (view) {
|
|
80
|
+
// Preload and store in variables
|
|
81
|
+
const imageUrl = yield cache('https://example.com/image.png');
|
|
82
|
+
const audioUrl = yield cache('https://example.com/audio.mp3');
|
|
83
|
+
|
|
84
|
+
// Use directly in JSX
|
|
85
|
+
view.add(<Img src={imageUrl} />);
|
|
86
|
+
view.add(<Audio src={audioUrl} />);
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 3. Conditional Loading
|
|
91
|
+
|
|
92
|
+
If you need to check if something is cached before loading:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
export default makeScene2D(function* (view) {
|
|
96
|
+
const url = 'https://example.com/image.png';
|
|
97
|
+
|
|
98
|
+
// Check if already cached (synchronous)
|
|
99
|
+
if (!cached(url)) {
|
|
100
|
+
// Not cached yet, load it
|
|
101
|
+
yield cache(url);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Now use it in JSX
|
|
105
|
+
view.add(<Img src={cached(url)} />);
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Advanced Usage
|
|
110
|
+
|
|
111
|
+
#### Cache with custom MIME type override
|
|
112
|
+
```typescript
|
|
113
|
+
export default makeScene2D(function* (view) {
|
|
114
|
+
// Preload with custom MIME type
|
|
115
|
+
yield cache('https://example.com/file', {
|
|
116
|
+
mimeType: 'audio/mpeg', // Override auto-detected MIME type
|
|
117
|
+
metadata: {duration: 5.2, title: 'My Audio'},
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Use in JSX
|
|
121
|
+
view.add(<Audio src={cached('https://example.com/file')} />);
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Cache a Blob directly
|
|
126
|
+
```typescript
|
|
127
|
+
import {Cache, CacheUtils, cached} from 'motion-canvas-cache';
|
|
128
|
+
|
|
129
|
+
export default makeScene2D(function* (view) {
|
|
130
|
+
const cache = Cache.getInstance();
|
|
131
|
+
const myBlob = new Blob(['content'], {type: 'text/plain'});
|
|
132
|
+
|
|
133
|
+
// Generate a cache key
|
|
134
|
+
const cacheKey = CacheUtils.generateCacheKey('my-content', ['text']);
|
|
135
|
+
|
|
136
|
+
// Cache the blob (use yield* in Motion Canvas)
|
|
137
|
+
yield* cache.cacheBlob(cacheKey, myBlob, {
|
|
138
|
+
source: 'user-generated',
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Use the cached result
|
|
142
|
+
const textUrl = cache.get(cacheKey).url;
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### Custom cache key options
|
|
147
|
+
```typescript
|
|
148
|
+
export default makeScene2D(function* (view) {
|
|
149
|
+
const baseUrl = 'https://api.example.com/audio';
|
|
150
|
+
|
|
151
|
+
// Preload different variants
|
|
152
|
+
yield cache(baseUrl, {
|
|
153
|
+
cacheKeyOptions: ['fast', 'lowquality'],
|
|
154
|
+
metadata: {speed: 'fast'},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
yield cache(baseUrl, {
|
|
158
|
+
cacheKeyOptions: ['slow', 'highquality'],
|
|
159
|
+
metadata: {speed: 'slow'},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Use them
|
|
163
|
+
const fastUrl = cached(baseUrl, {cacheKeyOptions: ['fast', 'lowquality']});
|
|
164
|
+
const slowUrl = cached(baseUrl, {cacheKeyOptions: ['slow', 'highquality']});
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Override cache key completely
|
|
169
|
+
```typescript
|
|
170
|
+
export default makeScene2D(function* (view) {
|
|
171
|
+
// Cache with custom key
|
|
172
|
+
yield cache('https://api.tts.com/generate?text=hello', {
|
|
173
|
+
cacheKey: 'my-custom-cache-key',
|
|
174
|
+
metadata: {duration: 2.5},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Retrieve with same custom key
|
|
178
|
+
const audioUrl = cached('https://api.tts.com/generate?text=hello', {
|
|
179
|
+
cacheKey: 'my-custom-cache-key',
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Note: If both cacheKey and cacheKeyOptions are provided,
|
|
183
|
+
// cacheKeyOptions will be ignored and a warning will be logged
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Use URL or Request objects
|
|
188
|
+
```typescript
|
|
189
|
+
export default makeScene2D(function* (view) {
|
|
190
|
+
// With URL object
|
|
191
|
+
const url = new URL('https://example.com/image.png');
|
|
192
|
+
yield cache(url);
|
|
193
|
+
view.add(<Img src={cached(url)} />);
|
|
194
|
+
|
|
195
|
+
// With Request object (for POST requests, custom headers, etc.)
|
|
196
|
+
const request = new Request('https://api.tts.com/generate', {
|
|
197
|
+
method: 'POST',
|
|
198
|
+
headers: {'Content-Type': 'application/json'},
|
|
199
|
+
body: JSON.stringify({text: 'Hello world'}),
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
yield cache(request, {
|
|
203
|
+
cacheKey: 'hello-world-audio',
|
|
204
|
+
metadata: {duration: 2.5},
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const audioUrl = cached(request, {cacheKey: 'hello-world-audio'});
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### Custom fetch options
|
|
212
|
+
```typescript
|
|
213
|
+
export default makeScene2D(function* (view) {
|
|
214
|
+
// Preload with fetch options
|
|
215
|
+
yield cache('https://api.tts.com/generate', {
|
|
216
|
+
fetchOptions: {
|
|
217
|
+
method: 'POST',
|
|
218
|
+
headers: {'Authorization': 'Bearer token123'},
|
|
219
|
+
body: JSON.stringify({text: 'Hello'}),
|
|
220
|
+
},
|
|
221
|
+
metadata: {duration: 3.5},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Use the cached result
|
|
225
|
+
const audioUrl = cached('https://api.tts.com/generate');
|
|
226
|
+
view.add(<Audio src={audioUrl} />);
|
|
227
|
+
});
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## How It Works
|
|
231
|
+
|
|
232
|
+
1. **First call**: When you call `cache()` with a URL:
|
|
233
|
+
- Checks in-memory cache first
|
|
234
|
+
- Checks server cache via HMR WebSocket
|
|
235
|
+
- If not cached, fetches from URL
|
|
236
|
+
- Stores in both memory and server cache
|
|
237
|
+
- Returns the cached URL (blob URL or server path)
|
|
238
|
+
|
|
239
|
+
2. **Subsequent calls**: Same cache key returns instantly from memory or server cache
|
|
240
|
+
|
|
241
|
+
3. **HMR updates**: Cache persists across hot module reloads for fast development
|
|
242
|
+
|
|
243
|
+
## API Reference
|
|
244
|
+
|
|
245
|
+
### `cache(input, options?)`
|
|
246
|
+
|
|
247
|
+
Async function to fetch and cache URLs. **Use with `yield` in Motion Canvas.**
|
|
248
|
+
|
|
249
|
+
**Parameters:**
|
|
250
|
+
- `input`: `string | URL | Request` - URL to fetch and cache
|
|
251
|
+
- `string` - URL as string
|
|
252
|
+
- `URL` - URL object
|
|
253
|
+
- `Request` - Request object (for POST, custom headers, etc.)
|
|
254
|
+
- `options`: `CachedOptions` (optional)
|
|
255
|
+
- `metadata?: Record<string, any>` - Custom metadata to store
|
|
256
|
+
- `mimeType?: string` - Override MIME type (auto-detected if not provided)
|
|
257
|
+
- `cacheKeyOptions?: string[]` - Additional options for cache key generation
|
|
258
|
+
- `cacheKey?: string` - Override the automatically generated cache key (if provided, `cacheKeyOptions` will be ignored)
|
|
259
|
+
- `fetchOptions?: RequestInit` - Additional options to pass to fetch() (method, headers, body, etc.)
|
|
260
|
+
|
|
261
|
+
**Returns:** `Promise<string>` - The cached URL (blob URL or server path) ready to use in Motion Canvas components
|
|
262
|
+
|
|
263
|
+
**Usage:** `const url = yield cache('https://...');`
|
|
264
|
+
|
|
265
|
+
### `cached(input, options?)`
|
|
266
|
+
|
|
267
|
+
Synchronously get a cached URL. **No `yield` needed - use directly in JSX!**
|
|
268
|
+
|
|
269
|
+
**Parameters:**
|
|
270
|
+
- Same as `cache()`
|
|
271
|
+
|
|
272
|
+
**Returns:** `string | null` - The cached URL if found, otherwise null
|
|
273
|
+
|
|
274
|
+
**Usage:** `const url = cached('https://...'); // No yield needed!`
|
|
275
|
+
|
|
276
|
+
### `Cache` class
|
|
277
|
+
|
|
278
|
+
Singleton cache manager.
|
|
279
|
+
|
|
280
|
+
**Methods:**
|
|
281
|
+
- `Cache.getInstance()` - Get the singleton instance
|
|
282
|
+
- `get(cacheKey)` - Get cached result
|
|
283
|
+
- `cacheResult(cacheKey, url, metadata?)` - Store URL in memory cache
|
|
284
|
+
- `cacheBlob(cacheKey, blob, metadata?, mimeType?)` - Cache a Blob with optional metadata and MIME type
|
|
285
|
+
- `checkServerCache(cacheKey)` - Check server cache
|
|
286
|
+
- `uploadToServer(cacheKey, data, mimeType, metadata?)` - Upload to server
|
|
287
|
+
|
|
288
|
+
### `CacheUtils` class
|
|
289
|
+
|
|
290
|
+
Utility functions for cache operations.
|
|
291
|
+
|
|
292
|
+
**Methods:**
|
|
293
|
+
- `CacheUtils.generateCacheKey(content, opts?)` - Generate 8-character hex cache key
|
|
294
|
+
- `CacheUtils.blobToDataUrl(blob)` - Convert Blob to base64 data URL
|
|
295
|
+
- `CacheUtils.streamToArrayBuffer(stream)` - Convert ReadableStream to ArrayBuffer
|
|
296
|
+
|
|
297
|
+
## Integration with Motion Canvas Narrator
|
|
298
|
+
|
|
299
|
+
This library was extracted from the motion-canvas-narrator project and can be reused there. Example integration:
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
// In your narrator provider (generator function)
|
|
303
|
+
import {cache, cached} from 'motion-canvas-cache';
|
|
304
|
+
|
|
305
|
+
function* generateAudio(text: string, voiceId: string, modelId: string) {
|
|
306
|
+
const url = 'https://api.example.com/tts';
|
|
307
|
+
|
|
308
|
+
// Check if already cached
|
|
309
|
+
const cachedUrl = cached(url, {
|
|
310
|
+
cacheKeyOptions: [text, voiceId, modelId],
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
if (cachedUrl) {
|
|
314
|
+
return cachedUrl;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Cache with audio duration metadata and return the cached URL
|
|
318
|
+
return yield cache(url, {
|
|
319
|
+
fetchOptions: {
|
|
320
|
+
method: 'POST',
|
|
321
|
+
body: JSON.stringify({text, voice: voiceId}),
|
|
322
|
+
},
|
|
323
|
+
metadata: {duration: 3.5},
|
|
324
|
+
cacheKeyOptions: [text, voiceId, modelId], // Unique per text/voice/model
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## Cache Directory Structure
|
|
330
|
+
|
|
331
|
+
Server-side cached files are stored as:
|
|
332
|
+
|
|
333
|
+
```
|
|
334
|
+
motion-canvas-cache/
|
|
335
|
+
├── 20fac170.mp3 # Cached file (8-char hash + extension)
|
|
336
|
+
├── 20fac170.meta.json # Metadata file
|
|
337
|
+
├── a1b2c3d4.png
|
|
338
|
+
├── a1b2c3d4.meta.json
|
|
339
|
+
└── ...
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Metadata file example:
|
|
343
|
+
```json
|
|
344
|
+
{
|
|
345
|
+
"cacheKey": "20fac170",
|
|
346
|
+
"mimeType": "audio/mpeg",
|
|
347
|
+
"fileSize": 55296,
|
|
348
|
+
"fileName": "20fac170.mp3",
|
|
349
|
+
"createdAt": "2024-01-01T12:00:00.000Z",
|
|
350
|
+
"duration": 3.5
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Cleanup
|
|
355
|
+
|
|
356
|
+
To clear the server cache, visit:
|
|
357
|
+
```
|
|
358
|
+
http://localhost:9000/__cache-cleanup
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
## Contributing
|
|
362
|
+
|
|
363
|
+
Contributions are welcome! Please open issues or pull requests.
|
|
364
|
+
|
|
365
|
+
## License
|
|
366
|
+
|
|
367
|
+
MIT
|
package/dist/Cache.d.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface ImportMeta {
|
|
3
|
+
hot?: {
|
|
4
|
+
on: (event: string, callback: (data: any) => void) => void;
|
|
5
|
+
send: (event: string, data: any) => void;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export interface CachedResult {
|
|
10
|
+
url: string;
|
|
11
|
+
metadata?: Record<string, any>;
|
|
12
|
+
}
|
|
13
|
+
type CacheKey = string;
|
|
14
|
+
/**
|
|
15
|
+
* Options for caching operations
|
|
16
|
+
*/
|
|
17
|
+
export interface CacheOptions {
|
|
18
|
+
cacheKey?: string;
|
|
19
|
+
metadata?: Record<string, any>;
|
|
20
|
+
mimeType?: string;
|
|
21
|
+
fetchOptions?: RequestInit;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Cache options with required cacheKey (used by cacheBlob and cacheArrayBuffer)
|
|
25
|
+
*/
|
|
26
|
+
export interface CacheOptionsWithKey extends Omit<CacheOptions, 'cacheKey' | 'fetchOptions'> {
|
|
27
|
+
cacheKey: string;
|
|
28
|
+
}
|
|
29
|
+
export declare class Cache {
|
|
30
|
+
private static instance;
|
|
31
|
+
private cache;
|
|
32
|
+
private pendingRequests;
|
|
33
|
+
private serverAvailablePromise;
|
|
34
|
+
private constructor();
|
|
35
|
+
static getInstance(): Cache;
|
|
36
|
+
private initializeHMR;
|
|
37
|
+
get(cacheKey: CacheKey): CachedResult | null;
|
|
38
|
+
/**
|
|
39
|
+
* Check if a cache key exists in memory cache (synchronous)
|
|
40
|
+
*
|
|
41
|
+
* @param cacheKey - The cache key to check
|
|
42
|
+
* @returns true if the key exists in memory cache
|
|
43
|
+
*/
|
|
44
|
+
has(cacheKey: CacheKey): boolean;
|
|
45
|
+
private set;
|
|
46
|
+
cacheResult(cacheKey: CacheKey, url: string, metadata?: Record<string, any>): void;
|
|
47
|
+
/**
|
|
48
|
+
* Cache a Blob (base caching method)
|
|
49
|
+
*
|
|
50
|
+
* @param blob - The Blob to cache
|
|
51
|
+
* @param options - Cache options (cacheKey is required, metadata, mimeType)
|
|
52
|
+
* @returns The blob URL that can be used to reference the cached blob
|
|
53
|
+
*/
|
|
54
|
+
cacheBlob(blob: Blob, options: CacheOptionsWithKey): Promise<string>;
|
|
55
|
+
/**
|
|
56
|
+
* Cache an ArrayBuffer (calls cacheBlob)
|
|
57
|
+
*
|
|
58
|
+
* @param arrayBuffer - The ArrayBuffer to cache
|
|
59
|
+
* @param options - Cache options (cacheKey is required, metadata, mimeType)
|
|
60
|
+
* @returns The blob URL that can be used to reference the cached data
|
|
61
|
+
*/
|
|
62
|
+
cacheArrayBuffer(arrayBuffer: ArrayBuffer, options: CacheOptionsWithKey): Promise<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Cache a URL by fetching and caching the response (calls cacheArrayBuffer)
|
|
65
|
+
*
|
|
66
|
+
* @param input - The URL to fetch and cache (string, URL object, or Request object)
|
|
67
|
+
* @param options - Cache options (cacheKey, metadata, mimeType, fetchOptions)
|
|
68
|
+
* @returns The blob URL that can be used to reference the cached data
|
|
69
|
+
*/
|
|
70
|
+
cacheUrl(input: string | URL, options?: CacheOptions): Promise<string>;
|
|
71
|
+
checkServerCache(cacheKey: CacheKey): Promise<CachedResult | null>;
|
|
72
|
+
uploadToServer(cacheKey: CacheKey, data: ArrayBuffer, mimeType: string, metadata?: Record<string, any>): Promise<void>;
|
|
73
|
+
streamToArrayBuffer(stream: ReadableStream): Promise<ArrayBuffer>;
|
|
74
|
+
}
|
|
75
|
+
export interface CachedOptions {
|
|
76
|
+
metadata?: Record<string, any>;
|
|
77
|
+
mimeType?: string;
|
|
78
|
+
cacheKeyOptions?: string[];
|
|
79
|
+
cacheKey?: string;
|
|
80
|
+
fetchOptions?: RequestInit;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Async function to fetch and cache a file from a URL
|
|
84
|
+
*
|
|
85
|
+
* @param input - URL to fetch (string, URL object, or Request object)
|
|
86
|
+
* @param options - Optional configuration
|
|
87
|
+
* - metadata: Custom metadata to store with the cached file
|
|
88
|
+
* - mimeType: Override MIME type (auto-detected if not provided)
|
|
89
|
+
* - cacheKeyOptions: Additional options for cache key generation
|
|
90
|
+
* - cacheKey: Override the automatically generated cache key
|
|
91
|
+
* - fetchOptions: Additional options to pass to fetch()
|
|
92
|
+
* @returns Promise that resolves to the cached URL (blob URL or server path)
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* // In Motion Canvas, use yield to await the promise
|
|
96
|
+
* export default makeScene2D(function* (view) {
|
|
97
|
+
* // Preload the image
|
|
98
|
+
* yield cache('https://example.com/image.png', {
|
|
99
|
+
* metadata: { width: 800, height: 600 },
|
|
100
|
+
* mimeType: 'image/png'
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* // Use synchronously in JSX
|
|
104
|
+
* view.add(<Img src={cached('https://example.com/image.png')} />);
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* // Cache with URL object
|
|
109
|
+
* const url = new URL('https://example.com/image.png');
|
|
110
|
+
* yield cache(url);
|
|
111
|
+
*
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* // Cache with fetch options
|
|
115
|
+
* yield cache('https://api.tts.com/generate', {
|
|
116
|
+
* fetchOptions: {
|
|
117
|
+
* method: 'POST',
|
|
118
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
119
|
+
* body: JSON.stringify({text: 'Hello'}),
|
|
120
|
+
* },
|
|
121
|
+
* metadata: { duration: 3.5 }
|
|
122
|
+
* });
|
|
123
|
+
*/
|
|
124
|
+
export declare function cache(input: string | URL, options?: CachedOptions): Promise<string>;
|
|
125
|
+
/**
|
|
126
|
+
* Synchronously get a cached URL (no async/yield needed)
|
|
127
|
+
*
|
|
128
|
+
* @param input - URL to check (string, URL object, or Request object)
|
|
129
|
+
* @param options - Optional configuration (same as cache function)
|
|
130
|
+
* @returns The cached URL if found, otherwise null
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* // Use directly in JSX without yield
|
|
134
|
+
* export default makeScene2D(function* (view) {
|
|
135
|
+
* // Preload first
|
|
136
|
+
* yield cache('https://example.com/image.png');
|
|
137
|
+
*
|
|
138
|
+
* // Use synchronously in JSX - no yield needed!
|
|
139
|
+
* view.add(<Img src={cached('https://example.com/image.png')} />);
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* // Conditional loading
|
|
144
|
+
* export default makeScene2D(function* (view) {
|
|
145
|
+
* const url = 'https://example.com/image.png';
|
|
146
|
+
*
|
|
147
|
+
* if (!cached(url)) {
|
|
148
|
+
* yield cache(url);
|
|
149
|
+
* }
|
|
150
|
+
*
|
|
151
|
+
* view.add(<Img src={cached(url)} />);
|
|
152
|
+
* });
|
|
153
|
+
*/
|
|
154
|
+
export declare function cached(input: string | URL, options?: CachedOptions): string | null;
|
|
155
|
+
export {};
|
|
156
|
+
//# sourceMappingURL=Cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Cache.d.ts","sourceRoot":"","sources":["../src/Cache.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare class CacheUtils {
|
|
2
|
+
/**
|
|
3
|
+
* Generates a cache key from content and options
|
|
4
|
+
*/
|
|
5
|
+
static generateCacheKey(content: string, opts?: string[]): string;
|
|
6
|
+
/**
|
|
7
|
+
* Simple hash function for generating cache keys
|
|
8
|
+
* Always returns exactly 8 hexadecimal characters
|
|
9
|
+
*/
|
|
10
|
+
private static simpleHash;
|
|
11
|
+
/**
|
|
12
|
+
* Converts a Blob to a data URL (base64 encoded)
|
|
13
|
+
*/
|
|
14
|
+
static blobToDataUrl(blob: Blob): Promise<string>;
|
|
15
|
+
/**
|
|
16
|
+
* Converts a ReadableStream to ArrayBuffer
|
|
17
|
+
*/
|
|
18
|
+
static streamToArrayBuffer(stream: ReadableStream): Promise<ArrayBuffer>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=CacheUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CacheUtils.d.ts","sourceRoot":"","sources":["../src/CacheUtils.ts"],"names":[],"mappings":"AAAA,qBAAa,UAAU;IACrB;;OAEG;WACW,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,MAAM,EAAO,GAAG,MAAM;IAK5E;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,UAAU;IAWzB;;OAEG;WACW,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC;IASxD;;OAEG;WACiB,mBAAmB,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC;CA2BtF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Cache, CachedResult, CacheOptions, CacheOptionsWithKey, cache, cached, CachedOptions } from './Cache';
|
|
2
|
+
export { CacheUtils } from './CacheUtils';
|
|
3
|
+
export { motionCanvasCachePlugin, MotionCanvasCachePluginOptions, } from './vite-plugin/plugin';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,EAAC,MAAM,SAAS,CAAC;AAC7G,OAAO,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC;AAGxC,OAAO,EACL,uBAAuB,EACvB,8BAA8B,GAC/B,MAAM,sBAAsB,CAAC"}
|