@innovatorved/code-lab 0.3.2 → 0.5.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/ARCHITECTURE.md +5 -3
- package/CHANGELOG.md +41 -0
- package/README.md +53 -175
- package/dist/apps/web/src/index.d.ts +34 -0
- package/dist/code-lab-sql.css +482 -0
- package/dist/code-lab-sql.d.ts +179 -0
- package/dist/code-lab-sql.esm.js +542 -0
- package/dist/code-lab-sql.umd.js +531 -0
- package/dist/code-lab.css +1799 -98
- package/dist/code-lab.d.ts +110 -15
- package/dist/code-lab.esm.js +49678 -23253
- package/dist/code-lab.js +49648 -23251
- package/dist/code-lab.umd.js +75766 -0
- package/dist/diagnostics.d.ts +3 -0
- package/dist/extensions/cpp.esm.js +19504 -3788
- package/dist/extensions/cpp.js +19501 -3785
- package/dist/extensions/guided-lab.esm.js +19401 -3725
- package/dist/extensions/guided-lab.js +19400 -3724
- package/dist/extensions/python.d.ts +1 -0
- package/dist/extensions/python.esm.js +19597 -3788
- package/dist/extensions/python.js +19597 -3788
- package/dist/extensions/umbrella.d.ts +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/modular-kernel.d.ts +11 -0
- package/dist/packages/commands/src/index.d.ts +96 -0
- package/dist/packages/core/src/di.d.ts +36 -0
- package/dist/packages/core/src/disposable.d.ts +27 -0
- package/dist/packages/core/src/errors.d.ts +69 -0
- package/dist/packages/core/src/events.d.ts +38 -0
- package/dist/packages/core/src/index.d.ts +6 -0
- package/dist/packages/core/src/path.d.ts +14 -0
- package/dist/packages/core/src/registry.d.ts +23 -0
- package/dist/packages/editor/src/index.d.ts +118 -0
- package/dist/packages/fs/src/backends.d.ts +37 -0
- package/dist/packages/fs/src/index.d.ts +3 -0
- package/dist/packages/fs/src/types.d.ts +40 -0
- package/dist/packages/fs/src/vfs-service.d.ts +33 -0
- package/dist/packages/preview/src/index.d.ts +34 -0
- package/dist/packages/runtime/src/index.d.ts +47 -0
- package/dist/packages/settings/src/builtin-schemas.d.ts +15 -0
- package/dist/packages/settings/src/configuration-service.d.ts +107 -0
- package/dist/packages/settings/src/index.d.ts +4 -0
- package/dist/packages/settings/src/jsonc.d.ts +20 -0
- package/dist/packages/settings/src/schema.d.ts +28 -0
- package/dist/packages/sql/src/completions.d.ts +8 -0
- package/dist/packages/sql/src/diagnostics.d.ts +2 -0
- package/dist/packages/sql/src/formatter.d.ts +2 -0
- package/dist/packages/sql/src/index.d.ts +7 -0
- package/dist/packages/sql/src/sql-editor.d.ts +2 -0
- package/dist/packages/sql/src/styles.d.ts +2 -0
- package/dist/packages/sql/src/tokenizer.d.ts +13 -0
- package/dist/packages/sql/src/types.d.ts +130 -0
- package/dist/packages/terminal/src/index.d.ts +54 -0
- package/dist/packages/ui/src/index.d.ts +4 -0
- package/dist/packages/ui/src/observability.d.ts +22 -0
- package/dist/packages/ui/src/plugin-registry.d.ts +90 -0
- package/dist/packages/ui/src/settings-dialog.d.ts +53 -0
- package/dist/packages/ui/src/themes.d.ts +40 -0
- package/dist/preview-sw.js +53 -12
- package/dist/runtime.d.ts +3 -3
- package/dist/runtime.esm.js +467 -44
- package/dist/runtime.js +465 -44
- package/dist/schemas.d.ts +234 -814
- package/dist/schemas.esm.js +19401 -3725
- package/dist/schemas.js +19404 -3728
- package/dist/scm.d.ts +92 -11
- package/dist/shell.d.ts +1 -1
- package/dist/shell.esm.js +1300 -151
- package/dist/shell.js +1300 -151
- package/dist/storage.d.ts +47 -0
- package/dist/terminal.esm.js +62 -3
- package/dist/terminal.js +62 -3
- package/dist/workbench/diff-editor.d.ts +1 -0
- package/dist/workbench/index.d.ts +1 -1
- package/dist/workbench/styles.d.ts +1 -1
- package/package.json +38 -9
- package/packages/commands/README.md +7 -0
- package/packages/commands/package.json +16 -0
- package/packages/commands/src/index.ts +467 -0
- package/packages/commands/tsconfig.json +22 -0
- package/packages/core/README.md +12 -0
- package/packages/core/package.json +13 -0
- package/packages/core/src/di.ts +147 -0
- package/packages/core/src/disposable.ts +112 -0
- package/packages/core/src/errors.ts +205 -0
- package/packages/core/src/events.ts +157 -0
- package/packages/core/src/index.ts +49 -0
- package/packages/core/src/path.ts +118 -0
- package/packages/core/src/registry.ts +61 -0
- package/packages/core/tsconfig.json +19 -0
- package/packages/editor/README.md +9 -0
- package/packages/editor/package.json +18 -0
- package/packages/editor/src/index.ts +344 -0
- package/packages/editor/tsconfig.json +24 -0
- package/packages/fs/README.md +7 -0
- package/packages/fs/package.json +16 -0
- package/packages/fs/src/backends.ts +301 -0
- package/packages/fs/src/index.ts +18 -0
- package/packages/fs/src/types.ts +44 -0
- package/packages/fs/src/vfs-service.ts +257 -0
- package/packages/fs/tsconfig.json +22 -0
- package/packages/preview/README.md +3 -0
- package/packages/preview/package.json +16 -0
- package/packages/preview/src/index.ts +125 -0
- package/packages/preview/tsconfig.json +22 -0
- package/packages/runtime/README.md +3 -0
- package/packages/runtime/package.json +17 -0
- package/packages/runtime/src/index.ts +134 -0
- package/packages/runtime/tsconfig.json +23 -0
- package/packages/settings/README.md +7 -0
- package/packages/settings/package.json +16 -0
- package/packages/settings/src/builtin-schemas.ts +246 -0
- package/packages/settings/src/configuration-service.ts +609 -0
- package/packages/settings/src/index.ts +38 -0
- package/packages/settings/src/jsonc.ts +189 -0
- package/packages/settings/src/schema.ts +164 -0
- package/packages/settings/tsconfig.json +22 -0
- package/packages/sql/README.md +12 -0
- package/packages/sql/package.json +13 -0
- package/packages/sql/src/completions.ts +359 -0
- package/packages/sql/src/diagnostics.ts +200 -0
- package/packages/sql/src/formatter.ts +271 -0
- package/packages/sql/src/index.ts +54 -0
- package/packages/sql/src/sql-editor.ts +802 -0
- package/packages/sql/src/styles.ts +501 -0
- package/packages/sql/src/tokenizer.ts +457 -0
- package/packages/sql/src/types.ts +148 -0
- package/packages/sql/tsconfig.json +19 -0
- package/packages/terminal/README.md +3 -0
- package/packages/terminal/package.json +16 -0
- package/packages/terminal/src/index.ts +183 -0
- package/packages/terminal/tsconfig.json +22 -0
- package/packages/ui/README.md +8 -0
- package/packages/ui/package.json +18 -0
- package/packages/ui/src/index.ts +40 -0
- package/packages/ui/src/observability.ts +175 -0
- package/packages/ui/src/plugin-registry.ts +289 -0
- package/packages/ui/src/settings-dialog.ts +575 -0
- package/packages/ui/src/themes.ts +267 -0
- package/packages/ui/tsconfig.json +24 -0
- package/src/code-lab.css +1799 -98
- package/src/diagnostics.ts +139 -7
- package/src/extensions/cpp.ts +88 -45
- package/src/extensions/python.ts +186 -53
- package/src/extensions/umbrella.ts +5 -0
- package/src/index.ts +1 -1
- package/src/modular-kernel.ts +11 -0
- package/src/preview-sw.js +52 -11
- package/src/runtime.ts +466 -46
- package/src/schemas.ts +62 -62
- package/src/scm.ts +781 -58
- package/src/shell.ts +1299 -151
- package/src/storage.ts +336 -3
- package/src/terminal.ts +60 -2
- package/src/workbench/diff-editor.ts +66 -6
- package/src/workbench/icons.ts +12 -2
- package/src/workbench/styles.ts +25 -19
- package/src/workbench.ts +17059 -6673
- package/dist/runtime/index.d.ts +0 -4
- package/dist/shell/coreutils/compilers-cli.d.ts +0 -24
- package/dist/shell/index.d.ts +0 -4
- package/src/runtime/index.ts +0 -5
- package/src/shell/coreutils/compilers-cli.ts +0 -103
- package/src/shell/index.ts +0 -5
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { PosixPath } from '@code-lab/core';
|
|
2
|
+
import type { IFsBackend } from './types';
|
|
3
|
+
|
|
4
|
+
export class MemoryBackend implements IFsBackend {
|
|
5
|
+
public readonly name = 'memory' as const;
|
|
6
|
+
private readonly store = new Map<string, Uint8Array>();
|
|
7
|
+
|
|
8
|
+
public constructor(initial?: Readonly<Record<string, string | Uint8Array>>) {
|
|
9
|
+
if (initial) {
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
for (const [rawPath, data] of Object.entries(initial)) {
|
|
12
|
+
const clean = PosixPath.normalize(rawPath.startsWith('/') ? rawPath : '/' + rawPath);
|
|
13
|
+
this.store.set(clean, typeof data === 'string' ? encoder.encode(data) : new Uint8Array(data));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public async loadAll(): Promise<Map<string, Uint8Array>> {
|
|
19
|
+
const out = new Map<string, Uint8Array>();
|
|
20
|
+
for (const [k, v] of this.store.entries()) {
|
|
21
|
+
out.set(k, new Uint8Array(v));
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async writeEntry(path: string, bytes: Uint8Array): Promise<void> {
|
|
27
|
+
this.store.set(path, new Uint8Array(bytes));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public async deleteEntry(path: string): Promise<void> {
|
|
31
|
+
this.store.delete(path);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async clear(): Promise<void> {
|
|
35
|
+
this.store.clear();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class IndexedDbBackend implements IFsBackend {
|
|
40
|
+
public readonly name = 'indexeddb' as const;
|
|
41
|
+
private readonly dbName: string;
|
|
42
|
+
private readonly storeName = 'files';
|
|
43
|
+
private readonly fallback = new MemoryBackend();
|
|
44
|
+
|
|
45
|
+
public constructor(namespace = 'codelab-vfs') {
|
|
46
|
+
this.dbName = `codelab-fs-${namespace}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private openDb(): Promise<IDBDatabase | null> {
|
|
50
|
+
if (typeof indexedDB === 'undefined') {
|
|
51
|
+
return Promise.resolve(null);
|
|
52
|
+
}
|
|
53
|
+
return new Promise((resolve) => {
|
|
54
|
+
try {
|
|
55
|
+
const req = indexedDB.open(this.dbName, 1);
|
|
56
|
+
req.onupgradeneeded = () => {
|
|
57
|
+
const db = req.result;
|
|
58
|
+
if (!db.objectStoreNames.contains(this.storeName)) {
|
|
59
|
+
db.createObjectStore(this.storeName);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
req.onsuccess = () => resolve(req.result);
|
|
63
|
+
req.onerror = () => resolve(null);
|
|
64
|
+
} catch {
|
|
65
|
+
resolve(null);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public async loadAll(): Promise<Map<string, Uint8Array>> {
|
|
71
|
+
const db = await this.openDb();
|
|
72
|
+
if (!db) return this.fallback.loadAll();
|
|
73
|
+
return new Promise((resolve) => {
|
|
74
|
+
try {
|
|
75
|
+
const tx = db.transaction(this.storeName, 'readonly');
|
|
76
|
+
const store = tx.objectStore(this.storeName);
|
|
77
|
+
const out = new Map<string, Uint8Array>();
|
|
78
|
+
const cursorReq = store.openCursor();
|
|
79
|
+
cursorReq.onsuccess = () => {
|
|
80
|
+
const cursor = cursorReq.result;
|
|
81
|
+
if (cursor) {
|
|
82
|
+
const key = String(cursor.key);
|
|
83
|
+
const val = cursor.value;
|
|
84
|
+
if (val instanceof Uint8Array) {
|
|
85
|
+
out.set(key, new Uint8Array(val));
|
|
86
|
+
} else if (val instanceof ArrayBuffer) {
|
|
87
|
+
out.set(key, new Uint8Array(val));
|
|
88
|
+
} else if (typeof val === 'string') {
|
|
89
|
+
out.set(key, new TextEncoder().encode(val));
|
|
90
|
+
}
|
|
91
|
+
cursor.continue();
|
|
92
|
+
} else {
|
|
93
|
+
db.close();
|
|
94
|
+
resolve(out);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
cursorReq.onerror = () => {
|
|
98
|
+
db.close();
|
|
99
|
+
resolve(out);
|
|
100
|
+
};
|
|
101
|
+
} catch {
|
|
102
|
+
db.close();
|
|
103
|
+
resolve(this.fallback.loadAll());
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public async writeEntry(path: string, bytes: Uint8Array): Promise<void> {
|
|
109
|
+
await this.fallback.writeEntry(path, bytes);
|
|
110
|
+
const db = await this.openDb();
|
|
111
|
+
if (!db) return;
|
|
112
|
+
await new Promise<void>((resolve) => {
|
|
113
|
+
try {
|
|
114
|
+
const tx = db.transaction(this.storeName, 'readwrite');
|
|
115
|
+
tx.objectStore(this.storeName).put(new Uint8Array(bytes), path);
|
|
116
|
+
tx.oncomplete = () => {
|
|
117
|
+
db.close();
|
|
118
|
+
resolve();
|
|
119
|
+
};
|
|
120
|
+
tx.onerror = () => {
|
|
121
|
+
db.close();
|
|
122
|
+
resolve();
|
|
123
|
+
};
|
|
124
|
+
} catch {
|
|
125
|
+
db.close();
|
|
126
|
+
resolve();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public async deleteEntry(path: string): Promise<void> {
|
|
132
|
+
await this.fallback.deleteEntry(path);
|
|
133
|
+
const db = await this.openDb();
|
|
134
|
+
if (!db) return;
|
|
135
|
+
await new Promise<void>((resolve) => {
|
|
136
|
+
try {
|
|
137
|
+
const tx = db.transaction(this.storeName, 'readwrite');
|
|
138
|
+
tx.objectStore(this.storeName).delete(path);
|
|
139
|
+
tx.oncomplete = () => {
|
|
140
|
+
db.close();
|
|
141
|
+
resolve();
|
|
142
|
+
};
|
|
143
|
+
tx.onerror = () => {
|
|
144
|
+
db.close();
|
|
145
|
+
resolve();
|
|
146
|
+
};
|
|
147
|
+
} catch {
|
|
148
|
+
db.close();
|
|
149
|
+
resolve();
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public async clear(): Promise<void> {
|
|
155
|
+
await this.fallback.clear();
|
|
156
|
+
const db = await this.openDb();
|
|
157
|
+
if (!db) return;
|
|
158
|
+
await new Promise<void>((resolve) => {
|
|
159
|
+
try {
|
|
160
|
+
const tx = db.transaction(this.storeName, 'readwrite');
|
|
161
|
+
tx.objectStore(this.storeName).clear();
|
|
162
|
+
tx.oncomplete = () => {
|
|
163
|
+
db.close();
|
|
164
|
+
resolve();
|
|
165
|
+
};
|
|
166
|
+
tx.onerror = () => {
|
|
167
|
+
db.close();
|
|
168
|
+
resolve();
|
|
169
|
+
};
|
|
170
|
+
} catch {
|
|
171
|
+
db.close();
|
|
172
|
+
resolve();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
interface OpfsWritable {
|
|
179
|
+
write(data: Uint8Array): Promise<void>;
|
|
180
|
+
close(): Promise<void>;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
interface OpfsFileHandle {
|
|
184
|
+
readonly kind: 'file';
|
|
185
|
+
getFile(): Promise<{ arrayBuffer(): Promise<ArrayBuffer> }>;
|
|
186
|
+
createWritable(): Promise<OpfsWritable>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
interface OpfsDirectoryHandle {
|
|
190
|
+
readonly kind: 'directory';
|
|
191
|
+
getDirectoryHandle(name: string, opts?: { create?: boolean }): Promise<OpfsDirectoryHandle>;
|
|
192
|
+
getFileHandle(name: string, opts?: { create?: boolean }): Promise<OpfsFileHandle>;
|
|
193
|
+
removeEntry(name: string, opts?: { recursive?: boolean }): Promise<void>;
|
|
194
|
+
entries(): AsyncIterableIterator<[string, OpfsDirectoryHandle | OpfsFileHandle]>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export class OpfsBackend implements IFsBackend {
|
|
198
|
+
public readonly name = 'opfs' as const;
|
|
199
|
+
public lastError: unknown = null;
|
|
200
|
+
private readonly rootDirName: string;
|
|
201
|
+
private readonly fallback = new MemoryBackend();
|
|
202
|
+
private readonly onError?: (err: unknown) => void;
|
|
203
|
+
|
|
204
|
+
public constructor(namespace = 'codelab', options: { onError?: (err: unknown) => void } = {}) {
|
|
205
|
+
this.rootDirName = `codelab-${namespace}`;
|
|
206
|
+
this.onError = options.onError;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private async getNamespaceRoot(): Promise<OpfsDirectoryHandle | null> {
|
|
210
|
+
if (
|
|
211
|
+
typeof navigator === 'undefined' ||
|
|
212
|
+
!navigator.storage ||
|
|
213
|
+
typeof navigator.storage.getDirectory !== 'function'
|
|
214
|
+
) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
const root = (await navigator.storage.getDirectory()) as unknown as OpfsDirectoryHandle;
|
|
219
|
+
return await root.getDirectoryHandle(this.rootDirName, { create: true });
|
|
220
|
+
} catch {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
public async loadAll(): Promise<Map<string, Uint8Array>> {
|
|
226
|
+
const root = await this.getNamespaceRoot();
|
|
227
|
+
if (!root) return this.fallback.loadAll();
|
|
228
|
+
const out = new Map<string, Uint8Array>();
|
|
229
|
+
const walk = async (dir: OpfsDirectoryHandle, prefix: string): Promise<void> => {
|
|
230
|
+
for await (const [name, handle] of dir.entries()) {
|
|
231
|
+
const full = `${prefix}/${name}`;
|
|
232
|
+
if (handle.kind === 'directory') {
|
|
233
|
+
await walk(handle, full);
|
|
234
|
+
} else {
|
|
235
|
+
const file = await handle.getFile();
|
|
236
|
+
const buf = await file.arrayBuffer();
|
|
237
|
+
out.set(full, new Uint8Array(buf));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
try {
|
|
242
|
+
await walk(root, '');
|
|
243
|
+
return out;
|
|
244
|
+
} catch {
|
|
245
|
+
return this.fallback.loadAll();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
public async writeEntry(path: string, bytes: Uint8Array): Promise<void> {
|
|
250
|
+
await this.fallback.writeEntry(path, bytes);
|
|
251
|
+
const root = await this.getNamespaceRoot();
|
|
252
|
+
if (!root) return;
|
|
253
|
+
try {
|
|
254
|
+
const segments = path.split('/').filter(Boolean);
|
|
255
|
+
if (segments.length === 0) return;
|
|
256
|
+
let dir = root;
|
|
257
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
258
|
+
dir = await dir.getDirectoryHandle(segments[i], { create: true });
|
|
259
|
+
}
|
|
260
|
+
const fileHandle = await dir.getFileHandle(segments[segments.length - 1], { create: true });
|
|
261
|
+
const writable = await fileHandle.createWritable();
|
|
262
|
+
await writable.write(bytes);
|
|
263
|
+
await writable.close();
|
|
264
|
+
} catch (err) {
|
|
265
|
+
this.lastError = err;
|
|
266
|
+
this.onError?.(err);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
public async deleteEntry(path: string): Promise<void> {
|
|
271
|
+
await this.fallback.deleteEntry(path);
|
|
272
|
+
const root = await this.getNamespaceRoot();
|
|
273
|
+
if (!root) return;
|
|
274
|
+
try {
|
|
275
|
+
const segments = path.split('/').filter(Boolean);
|
|
276
|
+
if (segments.length === 0) return;
|
|
277
|
+
let dir = root;
|
|
278
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
279
|
+
dir = await dir.getDirectoryHandle(segments[i]);
|
|
280
|
+
}
|
|
281
|
+
await dir.removeEntry(segments[segments.length - 1], { recursive: true });
|
|
282
|
+
} catch (err) {
|
|
283
|
+
this.lastError = err;
|
|
284
|
+
this.onError?.(err);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
public async clear(): Promise<void> {
|
|
289
|
+
await this.fallback.clear();
|
|
290
|
+
const root = await this.getNamespaceRoot();
|
|
291
|
+
if (!root) return;
|
|
292
|
+
try {
|
|
293
|
+
for await (const [name] of root.entries()) {
|
|
294
|
+
await root.removeEntry(name, { recursive: true });
|
|
295
|
+
}
|
|
296
|
+
} catch (err) {
|
|
297
|
+
this.lastError = err;
|
|
298
|
+
this.onError?.(err);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type FsEntryKind,
|
|
3
|
+
type FsStat,
|
|
4
|
+
type FsChangeEvent,
|
|
5
|
+
type IFsBackend,
|
|
6
|
+
type IFileSystemService,
|
|
7
|
+
IFileSystemServiceId
|
|
8
|
+
} from './types';
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
MemoryBackend,
|
|
12
|
+
IndexedDbBackend,
|
|
13
|
+
OpfsBackend
|
|
14
|
+
} from './backends';
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
FileSystemService
|
|
18
|
+
} from './vfs-service';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createServiceId, type Event, type IDisposable } from '@code-lab/core';
|
|
2
|
+
|
|
3
|
+
export type FsEntryKind = 'file' | 'directory';
|
|
4
|
+
|
|
5
|
+
export interface FsStat {
|
|
6
|
+
readonly kind: FsEntryKind;
|
|
7
|
+
readonly size: number;
|
|
8
|
+
readonly mtimeMs: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface FsChangeEvent {
|
|
12
|
+
readonly type: 'created' | 'updated' | 'deleted' | 'renamed';
|
|
13
|
+
readonly path: string;
|
|
14
|
+
readonly oldPath?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface IFsBackend {
|
|
18
|
+
readonly name: 'memory' | 'indexeddb' | 'opfs';
|
|
19
|
+
loadAll(): Promise<Map<string, Uint8Array>>;
|
|
20
|
+
writeEntry(path: string, bytes: Uint8Array): Promise<void>;
|
|
21
|
+
deleteEntry(path: string): Promise<void>;
|
|
22
|
+
clear(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface IFileSystemService extends IDisposable {
|
|
26
|
+
readonly backendName: string;
|
|
27
|
+
readonly onDidChangeFile: Event<FsChangeEvent>;
|
|
28
|
+
hydrate(): Promise<void>;
|
|
29
|
+
exists(path: string): boolean;
|
|
30
|
+
stat(path: string): FsStat;
|
|
31
|
+
readFile(path: string): string;
|
|
32
|
+
readFileBytes(path: string): Uint8Array;
|
|
33
|
+
writeFile(path: string, content: string | Uint8Array): void;
|
|
34
|
+
deleteFile(path: string, options?: { recursive?: boolean }): void;
|
|
35
|
+
rename(oldPath: string, newPath: string): void;
|
|
36
|
+
mkdir(path: string): void;
|
|
37
|
+
readdir(path: string): string[];
|
|
38
|
+
toRecord(prefix?: string): Record<string, string>;
|
|
39
|
+
loadRecord(files: Readonly<Record<string, string>>, prefix?: string): void;
|
|
40
|
+
flush(): Promise<void>;
|
|
41
|
+
switchBackend(backend: IFsBackend): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const IFileSystemServiceId = createServiceId<IFileSystemService>('code-lab.fs');
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { CodeLabError, Emitter, type Event, PosixPath } from '@code-lab/core';
|
|
2
|
+
import { MemoryBackend } from './backends';
|
|
3
|
+
import type { FsChangeEvent, FsStat, IFileSystemService, IFsBackend } from './types';
|
|
4
|
+
|
|
5
|
+
interface FileNode {
|
|
6
|
+
bytes: Uint8Array;
|
|
7
|
+
mtimeMs: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
const decoder = new TextDecoder('utf-8');
|
|
12
|
+
|
|
13
|
+
export class FileSystemService implements IFileSystemService {
|
|
14
|
+
private backend: IFsBackend;
|
|
15
|
+
private readonly files = new Map<string, FileNode>();
|
|
16
|
+
private readonly dirs = new Set<string>(['/', '/workspace']);
|
|
17
|
+
private readonly changeEmitter = new Emitter<FsChangeEvent>({ name: 'fs.change' });
|
|
18
|
+
private pendingChain: Promise<void> = Promise.resolve();
|
|
19
|
+
private disposed = false;
|
|
20
|
+
|
|
21
|
+
public constructor(backend?: IFsBackend) {
|
|
22
|
+
this.backend = backend ?? new MemoryBackend();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public get backendName(): string {
|
|
26
|
+
return this.backend.name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public get onDidChangeFile(): Event<FsChangeEvent> {
|
|
30
|
+
return this.changeEmitter.event;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private normalize(rawPath: string): string {
|
|
34
|
+
return PosixPath.normalize(rawPath.startsWith('/') ? rawPath : `/workspace/${rawPath}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private ensureParentDirs(normPath: string): void {
|
|
38
|
+
const parts = normPath.split('/').filter(Boolean);
|
|
39
|
+
let current = '';
|
|
40
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
41
|
+
current += '/' + parts[i];
|
|
42
|
+
this.dirs.add(current);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private schedulePersist(op: () => Promise<void>): void {
|
|
47
|
+
this.pendingChain = this.pendingChain.then(op, op);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async hydrate(): Promise<void> {
|
|
51
|
+
const loaded = await this.backend.loadAll();
|
|
52
|
+
for (const [rawPath, bytes] of loaded.entries()) {
|
|
53
|
+
const norm = this.normalize(rawPath);
|
|
54
|
+
this.ensureParentDirs(norm);
|
|
55
|
+
this.files.set(norm, { bytes: new Uint8Array(bytes), mtimeMs: Date.now() });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public exists(path: string): boolean {
|
|
60
|
+
const norm = this.normalize(path);
|
|
61
|
+
return this.dirs.has(norm) || this.files.has(norm);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public stat(path: string): FsStat {
|
|
65
|
+
const norm = this.normalize(path);
|
|
66
|
+
if (this.dirs.has(norm)) {
|
|
67
|
+
return { kind: 'directory', size: 0, mtimeMs: Date.now() };
|
|
68
|
+
}
|
|
69
|
+
const file = this.files.get(norm);
|
|
70
|
+
if (!file) {
|
|
71
|
+
throw new CodeLabError('ERR_FS_NOT_FOUND', `ENOENT: no such file or directory, stat '${path}'`, {
|
|
72
|
+
context: { path: norm }
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return { kind: 'file', size: file.bytes.byteLength, mtimeMs: file.mtimeMs };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public readFile(path: string): string {
|
|
79
|
+
return decoder.decode(this.readFileBytes(path));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public readFileBytes(path: string): Uint8Array {
|
|
83
|
+
const norm = this.normalize(path);
|
|
84
|
+
if (this.dirs.has(norm)) {
|
|
85
|
+
throw new CodeLabError('ERR_FS_IS_DIR', `EISDIR: illegal operation on a directory, read '${path}'`);
|
|
86
|
+
}
|
|
87
|
+
const file = this.files.get(norm);
|
|
88
|
+
if (!file) {
|
|
89
|
+
throw new CodeLabError('ERR_FS_NOT_FOUND', `ENOENT: no such file or directory, open '${path}'`);
|
|
90
|
+
}
|
|
91
|
+
return new Uint8Array(file.bytes);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public writeFile(path: string, content: string | Uint8Array): void {
|
|
95
|
+
const norm = this.normalize(path);
|
|
96
|
+
if (this.dirs.has(norm)) {
|
|
97
|
+
throw new CodeLabError('ERR_FS_IS_DIR', `EISDIR: illegal operation on a directory, write '${path}'`);
|
|
98
|
+
}
|
|
99
|
+
this.ensureParentDirs(norm);
|
|
100
|
+
const existed = this.files.has(norm);
|
|
101
|
+
const bytes = typeof content === 'string' ? encoder.encode(content) : new Uint8Array(content);
|
|
102
|
+
this.files.set(norm, { bytes, mtimeMs: Date.now() });
|
|
103
|
+
const snapshot = new Uint8Array(bytes);
|
|
104
|
+
this.schedulePersist(() => this.backend.writeEntry(norm, snapshot));
|
|
105
|
+
this.changeEmitter.fire({ type: existed ? 'updated' : 'created', path: norm });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public deleteFile(path: string, options: { recursive?: boolean } = {}): void {
|
|
109
|
+
const norm = this.normalize(path);
|
|
110
|
+
if (this.files.has(norm)) {
|
|
111
|
+
this.files.delete(norm);
|
|
112
|
+
this.schedulePersist(() => this.backend.deleteEntry(norm));
|
|
113
|
+
this.changeEmitter.fire({ type: 'deleted', path: norm });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (this.dirs.has(norm)) {
|
|
117
|
+
if (!options.recursive) {
|
|
118
|
+
const children = this.readdir(norm);
|
|
119
|
+
if (children.length > 0) {
|
|
120
|
+
throw new CodeLabError('ERR_INVALID_ARGUMENT', `Directory not empty: '${path}'`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const prefix = norm === '/' ? '/' : norm + '/';
|
|
124
|
+
for (const fileKey of Array.from(this.files.keys())) {
|
|
125
|
+
if (fileKey.startsWith(prefix)) {
|
|
126
|
+
this.files.delete(fileKey);
|
|
127
|
+
this.schedulePersist(() => this.backend.deleteEntry(fileKey));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
for (const dirKey of Array.from(this.dirs)) {
|
|
131
|
+
if (dirKey !== '/' && (dirKey === norm || dirKey.startsWith(prefix))) {
|
|
132
|
+
this.dirs.delete(dirKey);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
this.changeEmitter.fire({ type: 'deleted', path: norm });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
throw new CodeLabError('ERR_FS_NOT_FOUND', `ENOENT: no such file or directory, unlink '${path}'`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public rename(oldPath: string, newPath: string): void {
|
|
142
|
+
const src = this.normalize(oldPath);
|
|
143
|
+
const dst = this.normalize(newPath);
|
|
144
|
+
if (src === dst) return;
|
|
145
|
+
if (this.files.has(src)) {
|
|
146
|
+
const node = this.files.get(src)!;
|
|
147
|
+
this.files.delete(src);
|
|
148
|
+
this.ensureParentDirs(dst);
|
|
149
|
+
const copy = new Uint8Array(node.bytes);
|
|
150
|
+
this.files.set(dst, { bytes: copy, mtimeMs: Date.now() });
|
|
151
|
+
this.schedulePersist(async () => {
|
|
152
|
+
await this.backend.deleteEntry(src);
|
|
153
|
+
await this.backend.writeEntry(dst, copy);
|
|
154
|
+
});
|
|
155
|
+
this.changeEmitter.fire({ type: 'renamed', path: dst, oldPath: src });
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (this.dirs.has(src)) {
|
|
159
|
+
const prefix = src + '/';
|
|
160
|
+
this.dirs.delete(src);
|
|
161
|
+
this.dirs.add(dst);
|
|
162
|
+
this.ensureParentDirs(dst);
|
|
163
|
+
for (const d of Array.from(this.dirs)) {
|
|
164
|
+
if (d.startsWith(prefix)) {
|
|
165
|
+
this.dirs.delete(d);
|
|
166
|
+
this.dirs.add(dst + d.slice(src.length));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
for (const [k, node] of Array.from(this.files.entries())) {
|
|
170
|
+
if (k.startsWith(prefix)) {
|
|
171
|
+
const mapped = dst + k.slice(src.length);
|
|
172
|
+
this.files.delete(k);
|
|
173
|
+
const copy = new Uint8Array(node.bytes);
|
|
174
|
+
this.files.set(mapped, { bytes: copy, mtimeMs: Date.now() });
|
|
175
|
+
this.schedulePersist(async () => {
|
|
176
|
+
await this.backend.deleteEntry(k);
|
|
177
|
+
await this.backend.writeEntry(mapped, copy);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
this.changeEmitter.fire({ type: 'renamed', path: dst, oldPath: src });
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
throw new CodeLabError('ERR_FS_NOT_FOUND', `ENOENT: no such file or directory, rename '${oldPath}'`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public mkdir(path: string): void {
|
|
188
|
+
const norm = this.normalize(path);
|
|
189
|
+
if (this.files.has(norm)) {
|
|
190
|
+
throw new CodeLabError('ERR_FS_EXISTS', `EEXIST: file already exists, mkdir '${path}'`);
|
|
191
|
+
}
|
|
192
|
+
this.ensureParentDirs(norm + '/placeholder');
|
|
193
|
+
this.dirs.add(norm);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public readdir(path: string): string[] {
|
|
197
|
+
const norm = this.normalize(path);
|
|
198
|
+
if (!this.dirs.has(norm)) {
|
|
199
|
+
throw new CodeLabError('ERR_FS_NOT_DIR', `ENOTDIR: not a directory, scandir '${path}'`);
|
|
200
|
+
}
|
|
201
|
+
const prefix = norm === '/' ? '/' : norm + '/';
|
|
202
|
+
const children = new Set<string>();
|
|
203
|
+
for (const d of this.dirs) {
|
|
204
|
+
if (d !== norm && d.startsWith(prefix)) {
|
|
205
|
+
const seg = d.slice(prefix.length).split('/')[0];
|
|
206
|
+
if (seg) children.add(seg);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
for (const f of this.files.keys()) {
|
|
210
|
+
if (f.startsWith(prefix)) {
|
|
211
|
+
const rest = f.slice(prefix.length);
|
|
212
|
+
if (!rest.includes('/')) children.add(rest);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return Array.from(children).sort();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public toRecord(prefix = '/workspace'): Record<string, string> {
|
|
219
|
+
const cleanPrefix = PosixPath.normalize(prefix);
|
|
220
|
+
const out: Record<string, string> = {};
|
|
221
|
+
for (const [k, node] of this.files.entries()) {
|
|
222
|
+
if (cleanPrefix === '/') {
|
|
223
|
+
out[k.replace(/^\/+/, '')] = decoder.decode(node.bytes);
|
|
224
|
+
} else if (k.startsWith(cleanPrefix + '/')) {
|
|
225
|
+
out[k.slice(cleanPrefix.length + 1)] = decoder.decode(node.bytes);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
public loadRecord(files: Readonly<Record<string, string>>, prefix = '/workspace'): void {
|
|
232
|
+
const cleanPrefix = PosixPath.normalize(prefix);
|
|
233
|
+
for (const [rel, content] of Object.entries(files)) {
|
|
234
|
+
const target = rel.startsWith('/') ? rel : `${cleanPrefix}/${rel}`;
|
|
235
|
+
this.writeFile(target, content);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
public async flush(): Promise<void> {
|
|
240
|
+
await this.pendingChain;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
public async switchBackend(nextBackend: IFsBackend): Promise<void> {
|
|
244
|
+
await this.flush();
|
|
245
|
+
this.backend = nextBackend;
|
|
246
|
+
await this.backend.clear();
|
|
247
|
+
for (const [path, node] of this.files.entries()) {
|
|
248
|
+
await this.backend.writeEntry(path, new Uint8Array(node.bytes));
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
public dispose(): void {
|
|
253
|
+
if (this.disposed) return;
|
|
254
|
+
this.disposed = true;
|
|
255
|
+
this.changeEmitter.dispose();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022", "DOM"],
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noImplicitAny": true,
|
|
9
|
+
"strictNullChecks": true,
|
|
10
|
+
"noUnusedLocals": true,
|
|
11
|
+
"noUnusedParameters": true,
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"noFallthroughCasesInSwitch": true,
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
"paths": {
|
|
18
|
+
"@code-lab/core": ["../core/src/index.ts"]
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"include": ["src/**/*.ts"]
|
|
22
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@code-lab/preview",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Isolated web preview service, iframe sandbox enforcer, and CSP builder for CodeLab.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@code-lab/core": "workspace:*"
|
|
13
|
+
},
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|