@innovatorved/code-lab 0.7.0 → 0.7.2

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.
Files changed (159) hide show
  1. package/ARCHITECTURE.md +2 -2
  2. package/CHANGELOG.md +30 -0
  3. package/README.md +6 -6
  4. package/dist/code-lab-sql.esm.js +19 -19
  5. package/dist/code-lab-sql.umd.js +20 -20
  6. package/dist/code-lab.css +1 -1
  7. package/dist/code-lab.d.ts +47 -38
  8. package/dist/code-lab.esm.js +1183 -1909
  9. package/dist/code-lab.js +1182 -1906
  10. package/dist/extensions/cpp.esm.js +132 -8760
  11. package/dist/extensions/cpp.js +124 -8770
  12. package/dist/extensions/guided-lab.esm.js +33 -6836
  13. package/dist/extensions/guided-lab.js +33 -6854
  14. package/dist/extensions/javascript.esm.js +1 -36
  15. package/dist/extensions/javascript.js +1 -58
  16. package/dist/extensions/python.esm.js +113 -8122
  17. package/dist/extensions/python.js +113 -8140
  18. package/dist/extensions/typescript.esm.js +1 -55
  19. package/dist/extensions/typescript.js +1 -77
  20. package/dist/index.d.ts +1 -1
  21. package/dist/logger.d.ts +4 -20
  22. package/dist/modular-kernel.d.ts +0 -5
  23. package/dist/packages/core/src/errors.d.ts +5 -1
  24. package/dist/packages/fs/src/backends.d.ts +0 -27
  25. package/dist/packages/fs/src/index.d.ts +1 -1
  26. package/dist/packages/ui/src/html.d.ts +1 -0
  27. package/dist/packages/ui/src/index.d.ts +1 -0
  28. package/dist/{apps/web/src/index.d.ts → packages/ui/src/kernel.d.ts} +3 -11
  29. package/dist/packages/ui/src/observability.d.ts +1 -1
  30. package/dist/packages/ui/src/settings-dialog.d.ts +0 -1
  31. package/dist/preview-sw.js +1 -1
  32. package/dist/runtime.d.ts +3 -0
  33. package/dist/runtime.esm.js +78 -78
  34. package/dist/runtime.js +78 -78
  35. package/dist/schemas.d.ts +10 -1
  36. package/dist/schemas.esm.js +33 -6815
  37. package/dist/schemas.js +33 -6831
  38. package/dist/shell.esm.js +277 -285
  39. package/dist/shell.js +277 -285
  40. package/dist/terminal.esm.js +9 -7
  41. package/dist/terminal.js +7 -7
  42. package/dist/workbench/command-palette.d.ts +0 -4
  43. package/dist/workbench/editor.d.ts +0 -4
  44. package/dist/workbench/index.d.ts +0 -22
  45. package/dist/workbench/intellisense.d.ts +1 -8
  46. package/dist/workbench/xterm-adapter.d.ts +0 -1
  47. package/package.json +4 -13
  48. package/dist/code-lab.umd.js +0 -6117
  49. package/dist/packages/editor/src/index.d.ts +0 -118
  50. package/dist/packages/preview/src/index.d.ts +0 -34
  51. package/dist/packages/runtime/src/index.d.ts +0 -47
  52. package/dist/packages/terminal/src/index.d.ts +0 -54
  53. package/dist/workbench/drawer.d.ts +0 -18
  54. package/dist/workbench/file-tree.d.ts +0 -11
  55. package/dist/workbench/guided-pane.d.ts +0 -11
  56. package/dist/workbench/styles.d.ts +0 -4
  57. package/dist/workbench/toolbar.d.ts +0 -17
  58. package/packages/commands/README.md +0 -7
  59. package/packages/commands/package.json +0 -16
  60. package/packages/commands/src/index.ts +0 -467
  61. package/packages/commands/tsconfig.json +0 -22
  62. package/packages/core/README.md +0 -12
  63. package/packages/core/package.json +0 -13
  64. package/packages/core/src/di.ts +0 -147
  65. package/packages/core/src/disposable.ts +0 -112
  66. package/packages/core/src/errors.ts +0 -205
  67. package/packages/core/src/events.ts +0 -157
  68. package/packages/core/src/index.ts +0 -49
  69. package/packages/core/src/path.ts +0 -118
  70. package/packages/core/src/registry.ts +0 -61
  71. package/packages/core/tsconfig.json +0 -19
  72. package/packages/editor/README.md +0 -9
  73. package/packages/editor/package.json +0 -18
  74. package/packages/editor/src/index.ts +0 -344
  75. package/packages/editor/tsconfig.json +0 -24
  76. package/packages/fs/README.md +0 -7
  77. package/packages/fs/package.json +0 -16
  78. package/packages/fs/src/backends.ts +0 -301
  79. package/packages/fs/src/index.ts +0 -18
  80. package/packages/fs/src/types.ts +0 -44
  81. package/packages/fs/src/vfs-service.ts +0 -257
  82. package/packages/fs/tsconfig.json +0 -22
  83. package/packages/preview/README.md +0 -3
  84. package/packages/preview/package.json +0 -16
  85. package/packages/preview/src/index.ts +0 -125
  86. package/packages/preview/tsconfig.json +0 -22
  87. package/packages/runtime/README.md +0 -3
  88. package/packages/runtime/package.json +0 -17
  89. package/packages/runtime/src/index.ts +0 -134
  90. package/packages/runtime/tsconfig.json +0 -23
  91. package/packages/settings/README.md +0 -7
  92. package/packages/settings/package.json +0 -16
  93. package/packages/settings/src/builtin-schemas.ts +0 -262
  94. package/packages/settings/src/configuration-service.ts +0 -609
  95. package/packages/settings/src/index.ts +0 -38
  96. package/packages/settings/src/jsonc.ts +0 -189
  97. package/packages/settings/src/schema.ts +0 -164
  98. package/packages/settings/tsconfig.json +0 -22
  99. package/packages/sql/README.md +0 -12
  100. package/packages/sql/package.json +0 -13
  101. package/packages/sql/src/completions.ts +0 -359
  102. package/packages/sql/src/diagnostics.ts +0 -200
  103. package/packages/sql/src/formatter.ts +0 -271
  104. package/packages/sql/src/index.ts +0 -54
  105. package/packages/sql/src/sql-editor.ts +0 -802
  106. package/packages/sql/src/styles.ts +0 -524
  107. package/packages/sql/src/tokenizer.ts +0 -457
  108. package/packages/sql/src/types.ts +0 -148
  109. package/packages/sql/tsconfig.json +0 -19
  110. package/packages/terminal/README.md +0 -3
  111. package/packages/terminal/package.json +0 -16
  112. package/packages/terminal/src/index.ts +0 -183
  113. package/packages/terminal/tsconfig.json +0 -22
  114. package/packages/ui/README.md +0 -8
  115. package/packages/ui/package.json +0 -18
  116. package/packages/ui/src/index.ts +0 -40
  117. package/packages/ui/src/observability.ts +0 -173
  118. package/packages/ui/src/plugin-registry.ts +0 -289
  119. package/packages/ui/src/settings-dialog.ts +0 -939
  120. package/packages/ui/src/themes.ts +0 -333
  121. package/packages/ui/tsconfig.json +0 -24
  122. package/src/code-lab.css +0 -8584
  123. package/src/csp.ts +0 -590
  124. package/src/diagnostics.ts +0 -287
  125. package/src/extensions/cpp.ts +0 -2389
  126. package/src/extensions/guided-lab.ts +0 -64
  127. package/src/extensions/javascript.ts +0 -54
  128. package/src/extensions/python.ts +0 -1737
  129. package/src/extensions/typescript.ts +0 -85
  130. package/src/extensions/umbrella.ts +0 -5
  131. package/src/html.ts +0 -88
  132. package/src/index.ts +0 -129
  133. package/src/logger.ts +0 -139
  134. package/src/modular-kernel.ts +0 -11
  135. package/src/preview-sw.js +0 -296
  136. package/src/runtime/test-harness.ts +0 -57
  137. package/src/runtime.ts +0 -10480
  138. package/src/sample.html +0 -304
  139. package/src/schemas.ts +0 -366
  140. package/src/scm.ts +0 -2453
  141. package/src/search.ts +0 -433
  142. package/src/shell.ts +0 -10987
  143. package/src/storage.ts +0 -773
  144. package/src/terminal.ts +0 -1400
  145. package/src/workbench/command-palette.ts +0 -381
  146. package/src/workbench/diff-editor.ts +0 -470
  147. package/src/workbench/drawer.ts +0 -157
  148. package/src/workbench/editor.ts +0 -49
  149. package/src/workbench/file-tree.ts +0 -51
  150. package/src/workbench/guided-pane.ts +0 -43
  151. package/src/workbench/icons.ts +0 -255
  152. package/src/workbench/index.ts +0 -44
  153. package/src/workbench/intellisense.ts +0 -1871
  154. package/src/workbench/share.ts +0 -128
  155. package/src/workbench/styles.ts +0 -211
  156. package/src/workbench/toolbar.ts +0 -182
  157. package/src/workbench/xterm-adapter.ts +0 -1571
  158. package/src/workbench.ts +0 -22132
  159. package/src/zip.ts +0 -348
@@ -1,301 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
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';
@@ -1,44 +0,0 @@
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');
@@ -1,257 +0,0 @@
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
- }
@@ -1,22 +0,0 @@
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
- }
@@ -1,3 +0,0 @@
1
- # `@code-lab/preview`
2
-
3
- Layer 2 (`services`) web preview coordinator (`IPreviewService`, `PreviewService`), strict iframe sandbox policy (`SAFE_IFRAME_SANDBOX` — never `allow-same-origin`), URL scheme sanitizer, and CSP header generator (`buildPreviewCsp`).
@@ -1,16 +0,0 @@
1
- {
2
- "name": "@code-lab/preview",
3
- "version": "0.7.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
- }