@bendyline/squisq 1.0.1 → 1.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/dist/MediaProvider-wpSe21B3.d.ts +66 -0
- package/dist/__tests__/contentContainer.test.d.ts +2 -0
- package/dist/__tests__/contentContainer.test.d.ts.map +1 -0
- package/dist/__tests__/contentContainer.test.js +234 -0
- package/dist/__tests__/contentContainer.test.js.map +1 -0
- package/dist/chunk-4CRBS35L.js +292 -0
- package/dist/chunk-4CRBS35L.js.map +1 -0
- package/dist/storage/ContentContainer.d.ts +87 -0
- package/dist/storage/ContentContainer.d.ts.map +1 -0
- package/dist/storage/ContentContainer.js +122 -0
- package/dist/storage/ContentContainer.js.map +1 -0
- package/dist/storage/MediaProviderFromContainer.d.ts +21 -0
- package/dist/storage/MediaProviderFromContainer.d.ts.map +1 -0
- package/dist/storage/MediaProviderFromContainer.js +79 -0
- package/dist/storage/MediaProviderFromContainer.js.map +1 -0
- package/dist/storage/index.d.ts +3 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +2 -0
- package/dist/storage/index.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/contentContainer.test.ts +282 -0
- package/src/storage/ContentContainer.ts +200 -0
- package/src/storage/MediaProviderFromContainer.ts +92 -0
- package/src/storage/index.ts +3 -0
- package/dist/chunk-VJN7UB2Z.js +0 -145
- package/dist/chunk-VJN7UB2Z.js.map +0 -1
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
|
2
|
+
import { MemoryContentContainer, findDocumentPath } from '../storage/ContentContainer';
|
|
3
|
+
import { createMediaProviderFromContainer } from '../storage/MediaProviderFromContainer';
|
|
4
|
+
import type { ContentEntry } from '../storage/ContentContainer';
|
|
5
|
+
|
|
6
|
+
// ============================================
|
|
7
|
+
// MemoryContentContainer
|
|
8
|
+
// ============================================
|
|
9
|
+
|
|
10
|
+
describe('MemoryContentContainer', () => {
|
|
11
|
+
let container: MemoryContentContainer;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
container = new MemoryContentContainer();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('readFile returns null for missing file', async () => {
|
|
18
|
+
expect(await container.readFile('missing.txt')).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('writeFile and readFile round-trip', async () => {
|
|
22
|
+
const data = new TextEncoder().encode('hello world');
|
|
23
|
+
await container.writeFile('test.txt', data, 'text/plain');
|
|
24
|
+
const result = await container.readFile('test.txt');
|
|
25
|
+
expect(result).not.toBeNull();
|
|
26
|
+
expect(new TextDecoder().decode(result!)).toBe('hello world');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('writeFile with ArrayBuffer', async () => {
|
|
30
|
+
const buf = new ArrayBuffer(4);
|
|
31
|
+
new Uint8Array(buf).set([1, 2, 3, 4]);
|
|
32
|
+
await container.writeFile('data.bin', buf);
|
|
33
|
+
const result = await container.readFile('data.bin');
|
|
34
|
+
expect(new Uint8Array(result!)).toEqual(new Uint8Array([1, 2, 3, 4]));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('writeFile overwrites existing file', async () => {
|
|
38
|
+
await container.writeFile('f.txt', new TextEncoder().encode('first'));
|
|
39
|
+
await container.writeFile('f.txt', new TextEncoder().encode('second'));
|
|
40
|
+
const result = await container.readFile('f.txt');
|
|
41
|
+
expect(new TextDecoder().decode(result!)).toBe('second');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('removeFile deletes a file', async () => {
|
|
45
|
+
await container.writeFile('f.txt', new TextEncoder().encode('data'));
|
|
46
|
+
await container.removeFile('f.txt');
|
|
47
|
+
expect(await container.readFile('f.txt')).toBeNull();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('removeFile is a no-op for missing file', async () => {
|
|
51
|
+
// Should not throw
|
|
52
|
+
await container.removeFile('nonexistent.txt');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('exists returns true for existing file', async () => {
|
|
56
|
+
await container.writeFile('f.txt', new TextEncoder().encode('data'));
|
|
57
|
+
expect(await container.exists('f.txt')).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('exists returns false for missing file', async () => {
|
|
61
|
+
expect(await container.exists('missing.txt')).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('listFiles returns all files', async () => {
|
|
65
|
+
await container.writeFile('a.txt', new TextEncoder().encode('a'), 'text/plain');
|
|
66
|
+
await container.writeFile('b.jpg', new Uint8Array([0xff, 0xd8]), 'image/jpeg');
|
|
67
|
+
const entries = await container.listFiles();
|
|
68
|
+
expect(entries).toHaveLength(2);
|
|
69
|
+
expect(entries.map((e) => e.path).sort()).toEqual(['a.txt', 'b.jpg']);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('listFiles with prefix filters results', async () => {
|
|
73
|
+
await container.writeFile('index.md', new TextEncoder().encode('# Hi'));
|
|
74
|
+
await container.writeFile('images/hero.jpg', new Uint8Array([0xff]));
|
|
75
|
+
await container.writeFile('images/bg.png', new Uint8Array([0x89]));
|
|
76
|
+
await container.writeFile('audio/clip.mp3', new Uint8Array([0x49]));
|
|
77
|
+
|
|
78
|
+
const imageEntries = await container.listFiles('images/');
|
|
79
|
+
expect(imageEntries).toHaveLength(2);
|
|
80
|
+
expect(imageEntries.map((e) => e.path).sort()).toEqual(['images/bg.png', 'images/hero.jpg']);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('listFiles reports correct size', async () => {
|
|
84
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
85
|
+
await container.writeFile('data.bin', data);
|
|
86
|
+
const entries = await container.listFiles();
|
|
87
|
+
expect(entries[0].size).toBe(5);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('listFiles guesses MIME type from extension', async () => {
|
|
91
|
+
await container.writeFile('photo.jpg', new Uint8Array([0xff]));
|
|
92
|
+
const entries = await container.listFiles();
|
|
93
|
+
expect(entries[0].mimeType).toBe('image/jpeg');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('listFiles uses provided MIME type over guess', async () => {
|
|
97
|
+
await container.writeFile('data.bin', new Uint8Array([0]), 'application/custom');
|
|
98
|
+
const entries = await container.listFiles();
|
|
99
|
+
expect(entries[0].mimeType).toBe('application/custom');
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// ============================================
|
|
104
|
+
// Document discovery
|
|
105
|
+
// ============================================
|
|
106
|
+
|
|
107
|
+
describe('getDocumentPath', () => {
|
|
108
|
+
let container: MemoryContentContainer;
|
|
109
|
+
|
|
110
|
+
beforeEach(() => {
|
|
111
|
+
container = new MemoryContentContainer();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('returns null for empty container', async () => {
|
|
115
|
+
expect(await container.getDocumentPath()).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('finds index.md', async () => {
|
|
119
|
+
await container.writeFile('index.md', new TextEncoder().encode('# Hello'));
|
|
120
|
+
expect(await container.getDocumentPath()).toBe('index.md');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('prefers index.md over doc.md', async () => {
|
|
124
|
+
await container.writeFile('doc.md', new TextEncoder().encode('doc'));
|
|
125
|
+
await container.writeFile('index.md', new TextEncoder().encode('index'));
|
|
126
|
+
expect(await container.getDocumentPath()).toBe('index.md');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('prefers doc.md over document.md', async () => {
|
|
130
|
+
await container.writeFile('document.md', new TextEncoder().encode('document'));
|
|
131
|
+
await container.writeFile('doc.md', new TextEncoder().encode('doc'));
|
|
132
|
+
expect(await container.getDocumentPath()).toBe('doc.md');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('prefers document.md over arbitrary .md', async () => {
|
|
136
|
+
await container.writeFile('readme.md', new TextEncoder().encode('readme'));
|
|
137
|
+
await container.writeFile('document.md', new TextEncoder().encode('document'));
|
|
138
|
+
expect(await container.getDocumentPath()).toBe('document.md');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('falls back to first .md at root', async () => {
|
|
142
|
+
await container.writeFile('my-story.md', new TextEncoder().encode('story'));
|
|
143
|
+
expect(await container.getDocumentPath()).toBe('my-story.md');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('ignores .md files in subdirectories', async () => {
|
|
147
|
+
await container.writeFile('docs/notes.md', new TextEncoder().encode('notes'));
|
|
148
|
+
expect(await container.getDocumentPath()).toBeNull();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('ignores non-md files', async () => {
|
|
152
|
+
await container.writeFile('data.json', new TextEncoder().encode('{}'));
|
|
153
|
+
await container.writeFile('hero.jpg', new Uint8Array([0xff]));
|
|
154
|
+
expect(await container.getDocumentPath()).toBeNull();
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('readDocument / writeDocument', () => {
|
|
159
|
+
let container: MemoryContentContainer;
|
|
160
|
+
|
|
161
|
+
beforeEach(() => {
|
|
162
|
+
container = new MemoryContentContainer();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('writeDocument creates index.md by default', async () => {
|
|
166
|
+
await container.writeDocument('# Hello');
|
|
167
|
+
expect(await container.getDocumentPath()).toBe('index.md');
|
|
168
|
+
expect(await container.readDocument()).toBe('# Hello');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('writeDocument uses custom filename', async () => {
|
|
172
|
+
await container.writeDocument('# Custom', 'story.md');
|
|
173
|
+
expect(await container.getDocumentPath()).toBe('story.md');
|
|
174
|
+
expect(await container.readDocument()).toBe('# Custom');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('readDocument returns null for empty container', async () => {
|
|
178
|
+
expect(await container.readDocument()).toBeNull();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ============================================
|
|
183
|
+
// findDocumentPath (standalone)
|
|
184
|
+
// ============================================
|
|
185
|
+
|
|
186
|
+
describe('findDocumentPath', () => {
|
|
187
|
+
it('returns null for empty list', () => {
|
|
188
|
+
expect(findDocumentPath([])).toBeNull();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('finds index.md from entry list', () => {
|
|
192
|
+
const entries: ContentEntry[] = [
|
|
193
|
+
{ path: 'images/hero.jpg', mimeType: 'image/jpeg', size: 100 },
|
|
194
|
+
{ path: 'index.md', mimeType: 'text/markdown', size: 50 },
|
|
195
|
+
];
|
|
196
|
+
expect(findDocumentPath(entries)).toBe('index.md');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('skips subdirectory .md files', () => {
|
|
200
|
+
const entries: ContentEntry[] = [
|
|
201
|
+
{ path: 'docs/guide.md', mimeType: 'text/markdown', size: 50 },
|
|
202
|
+
];
|
|
203
|
+
expect(findDocumentPath(entries)).toBeNull();
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// ============================================
|
|
208
|
+
// createMediaProviderFromContainer
|
|
209
|
+
// ============================================
|
|
210
|
+
|
|
211
|
+
describe('createMediaProviderFromContainer', () => {
|
|
212
|
+
let container: MemoryContentContainer;
|
|
213
|
+
let blobCounter = 0;
|
|
214
|
+
|
|
215
|
+
beforeEach(() => {
|
|
216
|
+
container = new MemoryContentContainer();
|
|
217
|
+
blobCounter = 0;
|
|
218
|
+
// Mock URL.createObjectURL / revokeObjectURL (not available in Node)
|
|
219
|
+
vi.stubGlobal('URL', {
|
|
220
|
+
...URL,
|
|
221
|
+
createObjectURL: () => `blob:mock-${++blobCounter}`,
|
|
222
|
+
revokeObjectURL: vi.fn(),
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
afterEach(() => {
|
|
227
|
+
vi.unstubAllGlobals();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('resolveUrl returns path as-is for missing file', async () => {
|
|
231
|
+
const provider = createMediaProviderFromContainer(container);
|
|
232
|
+
expect(await provider.resolveUrl('missing.jpg')).toBe('missing.jpg');
|
|
233
|
+
provider.dispose();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('resolveUrl returns blob URL for existing file', async () => {
|
|
237
|
+
await container.writeFile('hero.jpg', new Uint8Array([0xff, 0xd8]), 'image/jpeg');
|
|
238
|
+
const provider = createMediaProviderFromContainer(container);
|
|
239
|
+
const url = await provider.resolveUrl('hero.jpg');
|
|
240
|
+
expect(url).toMatch(/^blob:/);
|
|
241
|
+
provider.dispose();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('resolveUrl caches blob URLs', async () => {
|
|
245
|
+
await container.writeFile('hero.jpg', new Uint8Array([0xff, 0xd8]), 'image/jpeg');
|
|
246
|
+
const provider = createMediaProviderFromContainer(container);
|
|
247
|
+
const url1 = await provider.resolveUrl('hero.jpg');
|
|
248
|
+
const url2 = await provider.resolveUrl('hero.jpg');
|
|
249
|
+
expect(url1).toBe(url2);
|
|
250
|
+
provider.dispose();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('listMedia excludes markdown files', async () => {
|
|
254
|
+
await container.writeFile('index.md', new TextEncoder().encode('# Doc'));
|
|
255
|
+
await container.writeFile('hero.jpg', new Uint8Array([0xff]), 'image/jpeg');
|
|
256
|
+
await container.writeFile('clip.mp4', new Uint8Array([0x00]), 'video/mp4');
|
|
257
|
+
|
|
258
|
+
const provider = createMediaProviderFromContainer(container);
|
|
259
|
+
const media = await provider.listMedia();
|
|
260
|
+
expect(media).toHaveLength(2);
|
|
261
|
+
expect(media.map((m) => m.name).sort()).toEqual(['clip.mp4', 'hero.jpg']);
|
|
262
|
+
provider.dispose();
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('addMedia writes to container and returns path', async () => {
|
|
266
|
+
const provider = createMediaProviderFromContainer(container);
|
|
267
|
+
const path = await provider.addMedia('photo.png', new Uint8Array([0x89]), 'image/png');
|
|
268
|
+
expect(path).toBe('photo.png');
|
|
269
|
+
expect(await container.exists('photo.png')).toBe(true);
|
|
270
|
+
provider.dispose();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('removeMedia deletes from container', async () => {
|
|
274
|
+
await container.writeFile('hero.jpg', new Uint8Array([0xff]), 'image/jpeg');
|
|
275
|
+
const provider = createMediaProviderFromContainer(container);
|
|
276
|
+
// Resolve first to populate cache
|
|
277
|
+
await provider.resolveUrl('hero.jpg');
|
|
278
|
+
await provider.removeMedia('hero.jpg');
|
|
279
|
+
expect(await container.exists('hero.jpg')).toBe(false);
|
|
280
|
+
provider.dispose();
|
|
281
|
+
});
|
|
282
|
+
});
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ContentContainer — virtual file system for document containers.
|
|
3
|
+
*
|
|
4
|
+
* A Squisq document is more than just markdown: it's a container of files
|
|
5
|
+
* including the primary markdown document, images, audio, timing data, and
|
|
6
|
+
* other media. ContentContainer provides an abstract async file system
|
|
7
|
+
* interface for reading, writing, and listing these files.
|
|
8
|
+
*
|
|
9
|
+
* Paths are forward-slash separated strings relative to the container root
|
|
10
|
+
* (e.g., 'images/hero.jpg', 'index.md'). No leading slash.
|
|
11
|
+
*
|
|
12
|
+
* Implementations:
|
|
13
|
+
* - MemoryContentContainer — in-memory Map (for zip import, tests, transient use)
|
|
14
|
+
* - SlotContentContainer — backed by IndexedDB slot storage (in the site package)
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Metadata about a file in a ContentContainer.
|
|
19
|
+
*/
|
|
20
|
+
export interface ContentEntry {
|
|
21
|
+
/** Relative path within the container (e.g., 'images/hero.jpg') */
|
|
22
|
+
path: string;
|
|
23
|
+
/** MIME type (e.g., 'image/jpeg') */
|
|
24
|
+
mimeType: string;
|
|
25
|
+
/** File size in bytes */
|
|
26
|
+
size: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Abstract async file system for document containers.
|
|
31
|
+
*
|
|
32
|
+
* All paths are forward-slash separated, relative to the container root,
|
|
33
|
+
* with no leading slash. Example: 'images/hero.jpg', 'index.md'.
|
|
34
|
+
*/
|
|
35
|
+
export interface ContentContainer {
|
|
36
|
+
/** Read a file's binary content. Returns null if the file does not exist. */
|
|
37
|
+
readFile(path: string): Promise<ArrayBuffer | null>;
|
|
38
|
+
|
|
39
|
+
/** Write a file. Creates or overwrites. */
|
|
40
|
+
writeFile(path: string, data: ArrayBuffer | Uint8Array, mimeType?: string): Promise<void>;
|
|
41
|
+
|
|
42
|
+
/** Remove a file. No-op if the file does not exist. */
|
|
43
|
+
removeFile(path: string): Promise<void>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* List files in the container.
|
|
47
|
+
* @param prefix — Optional path prefix to filter by (e.g., 'images/')
|
|
48
|
+
*/
|
|
49
|
+
listFiles(prefix?: string): Promise<ContentEntry[]>;
|
|
50
|
+
|
|
51
|
+
/** Check whether a file exists. */
|
|
52
|
+
exists(path: string): Promise<boolean>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Find the primary markdown document path.
|
|
56
|
+
*
|
|
57
|
+
* Discovery order: index.md → doc.md → document.md → first *.md at root.
|
|
58
|
+
* Returns null if no markdown file is found at the root level.
|
|
59
|
+
*/
|
|
60
|
+
getDocumentPath(): Promise<string | null>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Convenience: read the primary markdown document as a UTF-8 string.
|
|
64
|
+
* Returns null if no markdown file is found.
|
|
65
|
+
*/
|
|
66
|
+
readDocument(): Promise<string | null>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Convenience: write a markdown document.
|
|
70
|
+
* @param markdown — The markdown content
|
|
71
|
+
* @param filename — Filename to use (defaults to 'index.md')
|
|
72
|
+
*/
|
|
73
|
+
writeDocument(markdown: string, filename?: string): Promise<void>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ============================================
|
|
77
|
+
// Well-known markdown filenames in priority order
|
|
78
|
+
// ============================================
|
|
79
|
+
|
|
80
|
+
const MARKDOWN_PRIORITY = ['index.md', 'doc.md', 'document.md'];
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Find the primary markdown path from a list of file entries.
|
|
84
|
+
* Exported for reuse by other ContentContainer implementations.
|
|
85
|
+
*/
|
|
86
|
+
export function findDocumentPath(entries: ContentEntry[]): string | null {
|
|
87
|
+
// Only consider root-level files (no '/' in path)
|
|
88
|
+
const rootFiles = entries.filter((e) => !e.path.includes('/'));
|
|
89
|
+
|
|
90
|
+
for (const name of MARKDOWN_PRIORITY) {
|
|
91
|
+
if (rootFiles.some((e) => e.path.toLowerCase() === name)) {
|
|
92
|
+
return name;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Fallback: first .md file at root
|
|
97
|
+
const firstMd = rootFiles.find((e) => e.path.toLowerCase().endsWith('.md'));
|
|
98
|
+
return firstMd?.path ?? null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ============================================
|
|
102
|
+
// MemoryContentContainer
|
|
103
|
+
// ============================================
|
|
104
|
+
|
|
105
|
+
interface MemoryFile {
|
|
106
|
+
data: ArrayBuffer;
|
|
107
|
+
mimeType: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* In-memory ContentContainer backed by a Map.
|
|
112
|
+
*
|
|
113
|
+
* Used for zip import (deserialize into memory), tests, and transient operations.
|
|
114
|
+
*/
|
|
115
|
+
export class MemoryContentContainer implements ContentContainer {
|
|
116
|
+
private files = new Map<string, MemoryFile>();
|
|
117
|
+
|
|
118
|
+
async readFile(path: string): Promise<ArrayBuffer | null> {
|
|
119
|
+
return this.files.get(path)?.data ?? null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async writeFile(path: string, data: ArrayBuffer | Uint8Array, mimeType?: string): Promise<void> {
|
|
123
|
+
const buffer = data instanceof ArrayBuffer ? data : data.slice().buffer;
|
|
124
|
+
this.files.set(path, {
|
|
125
|
+
data: buffer,
|
|
126
|
+
mimeType: mimeType ?? guessMimeType(path),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async removeFile(path: string): Promise<void> {
|
|
131
|
+
this.files.delete(path);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async listFiles(prefix?: string): Promise<ContentEntry[]> {
|
|
135
|
+
const entries: ContentEntry[] = [];
|
|
136
|
+
for (const [path, file] of this.files) {
|
|
137
|
+
if (prefix && !path.startsWith(prefix)) continue;
|
|
138
|
+
entries.push({
|
|
139
|
+
path,
|
|
140
|
+
mimeType: file.mimeType,
|
|
141
|
+
size: file.data.byteLength,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return entries;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async exists(path: string): Promise<boolean> {
|
|
148
|
+
return this.files.has(path);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async getDocumentPath(): Promise<string | null> {
|
|
152
|
+
return findDocumentPath(await this.listFiles());
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async readDocument(): Promise<string | null> {
|
|
156
|
+
const docPath = await this.getDocumentPath();
|
|
157
|
+
if (!docPath) return null;
|
|
158
|
+
const data = await this.readFile(docPath);
|
|
159
|
+
if (!data) return null;
|
|
160
|
+
return new TextDecoder().decode(data);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async writeDocument(markdown: string, filename?: string): Promise<void> {
|
|
164
|
+
const name = filename ?? 'index.md';
|
|
165
|
+
const data = new TextEncoder().encode(markdown);
|
|
166
|
+
await this.writeFile(name, data, 'text/markdown');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ============================================
|
|
171
|
+
// MIME type guessing
|
|
172
|
+
// ============================================
|
|
173
|
+
|
|
174
|
+
const EXTENSION_MIME_MAP: Record<string, string> = {
|
|
175
|
+
'.md': 'text/markdown',
|
|
176
|
+
'.txt': 'text/plain',
|
|
177
|
+
'.json': 'application/json',
|
|
178
|
+
'.jpg': 'image/jpeg',
|
|
179
|
+
'.jpeg': 'image/jpeg',
|
|
180
|
+
'.png': 'image/png',
|
|
181
|
+
'.gif': 'image/gif',
|
|
182
|
+
'.svg': 'image/svg+xml',
|
|
183
|
+
'.webp': 'image/webp',
|
|
184
|
+
'.avif': 'image/avif',
|
|
185
|
+
'.mp4': 'video/mp4',
|
|
186
|
+
'.webm': 'video/webm',
|
|
187
|
+
'.mp3': 'audio/mpeg',
|
|
188
|
+
'.wav': 'audio/wav',
|
|
189
|
+
'.ogg': 'audio/ogg',
|
|
190
|
+
'.css': 'text/css',
|
|
191
|
+
'.html': 'text/html',
|
|
192
|
+
'.js': 'application/javascript',
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
function guessMimeType(path: string): string {
|
|
196
|
+
const dot = path.lastIndexOf('.');
|
|
197
|
+
if (dot === -1) return 'application/octet-stream';
|
|
198
|
+
const ext = path.slice(dot).toLowerCase();
|
|
199
|
+
return EXTENSION_MIME_MAP[ext] ?? 'application/octet-stream';
|
|
200
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MediaProviderFromContainer — bridges ContentContainer to MediaProvider.
|
|
3
|
+
*
|
|
4
|
+
* Creates a MediaProvider that resolves relative paths by reading binary data
|
|
5
|
+
* from a ContentContainer and generating blob URLs. Blob URLs are cached and
|
|
6
|
+
* revoked on dispose().
|
|
7
|
+
*
|
|
8
|
+
* This allows any ContentContainer (memory, slot-backed, zip-loaded) to be
|
|
9
|
+
* used with existing rendering components (DocPlayer, ImageLayer, VideoLayer)
|
|
10
|
+
* that consume MediaProvider.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { MediaProvider, MediaEntry } from '../schemas/MediaProvider.js';
|
|
14
|
+
import type { ContentContainer } from './ContentContainer.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Create a MediaProvider backed by a ContentContainer.
|
|
18
|
+
*
|
|
19
|
+
* @param container — The ContentContainer to read/write media from
|
|
20
|
+
* @returns A MediaProvider that resolves paths to blob URLs
|
|
21
|
+
*/
|
|
22
|
+
export function createMediaProviderFromContainer(container: ContentContainer): MediaProvider {
|
|
23
|
+
const blobUrlCache = new Map<string, string>();
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
async resolveUrl(relativePath: string): Promise<string> {
|
|
27
|
+
const cached = blobUrlCache.get(relativePath);
|
|
28
|
+
if (cached) return cached;
|
|
29
|
+
|
|
30
|
+
const data = await container.readFile(relativePath);
|
|
31
|
+
if (!data) return relativePath;
|
|
32
|
+
|
|
33
|
+
const entries = await container.listFiles();
|
|
34
|
+
const entry = entries.find((e) => e.path === relativePath);
|
|
35
|
+
const mimeType = entry?.mimeType ?? 'application/octet-stream';
|
|
36
|
+
|
|
37
|
+
const blob = new Blob([data], { type: mimeType });
|
|
38
|
+
const url = URL.createObjectURL(blob);
|
|
39
|
+
blobUrlCache.set(relativePath, url);
|
|
40
|
+
return url;
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
async listMedia(): Promise<MediaEntry[]> {
|
|
44
|
+
const entries = await container.listFiles();
|
|
45
|
+
return entries
|
|
46
|
+
.filter((e) => !e.path.toLowerCase().endsWith('.md'))
|
|
47
|
+
.map((e) => ({
|
|
48
|
+
name: e.path,
|
|
49
|
+
mimeType: e.mimeType,
|
|
50
|
+
size: e.size,
|
|
51
|
+
}));
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
async addMedia(
|
|
55
|
+
name: string,
|
|
56
|
+
data: ArrayBuffer | Blob | Uint8Array,
|
|
57
|
+
mimeType: string,
|
|
58
|
+
): Promise<string> {
|
|
59
|
+
// Invalidate any cached blob URL for this path before overwriting
|
|
60
|
+
const cached = blobUrlCache.get(name);
|
|
61
|
+
if (cached) {
|
|
62
|
+
URL.revokeObjectURL(cached);
|
|
63
|
+
blobUrlCache.delete(name);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let buffer: ArrayBuffer | Uint8Array;
|
|
67
|
+
if (data instanceof Blob) {
|
|
68
|
+
buffer = await data.arrayBuffer();
|
|
69
|
+
} else {
|
|
70
|
+
buffer = data;
|
|
71
|
+
}
|
|
72
|
+
await container.writeFile(name, buffer, mimeType);
|
|
73
|
+
return name;
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
async removeMedia(relativePath: string): Promise<void> {
|
|
77
|
+
const cached = blobUrlCache.get(relativePath);
|
|
78
|
+
if (cached) {
|
|
79
|
+
URL.revokeObjectURL(cached);
|
|
80
|
+
blobUrlCache.delete(relativePath);
|
|
81
|
+
}
|
|
82
|
+
await container.removeFile(relativePath);
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
dispose(): void {
|
|
86
|
+
for (const url of blobUrlCache.values()) {
|
|
87
|
+
URL.revokeObjectURL(url);
|
|
88
|
+
}
|
|
89
|
+
blobUrlCache.clear();
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
package/src/storage/index.ts
CHANGED
|
@@ -3,3 +3,6 @@ export { MemoryStorageAdapter } from './MemoryStorageAdapter.js';
|
|
|
3
3
|
export { LocalStorageAdapter } from './LocalStorageAdapter.js';
|
|
4
4
|
export { LocalForageAdapter } from './LocalForageAdapter.js';
|
|
5
5
|
export type { LocalForageAdapterOptions } from './LocalForageAdapter.js';
|
|
6
|
+
export type { ContentContainer, ContentEntry } from './ContentContainer.js';
|
|
7
|
+
export { MemoryContentContainer, findDocumentPath } from './ContentContainer.js';
|
|
8
|
+
export { createMediaProviderFromContainer } from './MediaProviderFromContainer.js';
|