@innovatorved/code-lab 0.4.0 → 0.6.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 +4 -4
- package/CHANGELOG.md +36 -0
- package/README.md +6 -30
- package/dist/code-lab-sql.esm.js +1 -1
- package/dist/code-lab-sql.umd.js +2 -2
- package/dist/code-lab.css +32 -5818
- package/dist/code-lab.d.ts +1 -1
- package/dist/code-lab.esm.js +4655 -64400
- package/dist/code-lab.js +4540 -64286
- package/dist/code-lab.umd.js +4540 -64286
- package/dist/diagnostics.d.ts +3 -0
- package/dist/extensions/cpp.d.ts +2 -2
- package/dist/extensions/cpp.esm.js +1804 -14725
- package/dist/extensions/cpp.js +1804 -14725
- package/dist/extensions/guided-lab.esm.js +2938 -16127
- package/dist/extensions/guided-lab.js +2938 -16127
- package/dist/extensions/python.d.ts +2 -2
- package/dist/extensions/python.esm.js +1765 -14698
- package/dist/extensions/python.js +1765 -14698
- package/dist/index.d.ts +1 -1
- package/dist/packages/settings/src/configuration-service.d.ts +7 -0
- package/dist/packages/settings/src/index.d.ts +1 -1
- package/dist/packages/ui/src/index.d.ts +2 -2
- package/dist/packages/ui/src/plugin-registry.d.ts +1 -0
- package/dist/packages/ui/src/settings-dialog.d.ts +31 -3
- package/dist/packages/ui/src/themes.d.ts +5 -0
- package/dist/preview-sw.js +4 -188
- package/dist/runtime.esm.js +109 -10284
- package/dist/runtime.js +107 -10284
- package/dist/schemas.d.ts +2 -2
- package/dist/schemas.esm.js +2938 -16127
- package/dist/schemas.js +2941 -16130
- package/dist/scm.d.ts +296 -14
- package/dist/shell.esm.js +811 -9178
- package/dist/shell.js +811 -9178
- package/dist/storage.d.ts +56 -0
- package/dist/terminal.esm.js +20 -1347
- package/dist/terminal.js +20 -1347
- package/dist/workbench/command-palette.d.ts +16 -1
- package/dist/workbench/diff-editor.d.ts +27 -1
- package/dist/workbench/icons.d.ts +6 -2
- package/dist/workbench/index.d.ts +33 -1
- package/dist/workbench/intellisense.d.ts +194 -0
- package/dist/workbench/styles.d.ts +1 -1
- package/dist/workbench/xterm-adapter.d.ts +122 -0
- package/package.json +8 -2
- package/packages/commands/package.json +1 -1
- package/packages/core/package.json +1 -1
- package/packages/editor/package.json +1 -1
- package/packages/fs/package.json +1 -1
- package/packages/preview/package.json +1 -1
- package/packages/runtime/package.json +1 -1
- package/packages/settings/package.json +1 -1
- package/packages/settings/src/builtin-schemas.ts +17 -6
- package/packages/settings/src/configuration-service.ts +72 -6
- package/packages/settings/src/index.ts +1 -0
- package/packages/sql/package.json +1 -1
- package/packages/terminal/package.json +1 -1
- package/packages/ui/package.json +1 -1
- package/packages/ui/src/index.ts +4 -0
- package/packages/ui/src/plugin-registry.ts +17 -2
- package/packages/ui/src/settings-dialog.ts +393 -99
- package/packages/ui/src/themes.ts +183 -24
- package/src/code-lab.css +2763 -444
- package/src/diagnostics.ts +139 -7
- package/src/extensions/cpp.ts +337 -71
- package/src/extensions/python.ts +375 -75
- package/src/index.ts +1 -1
- package/src/runtime.ts +272 -43
- package/src/schemas.ts +2 -2
- package/src/scm.ts +2172 -74
- package/src/shell.ts +1999 -193
- package/src/storage.ts +401 -5
- package/src/terminal.ts +148 -12
- package/src/workbench/command-palette.ts +289 -48
- package/src/workbench/diff-editor.ts +240 -78
- package/src/workbench/icons.ts +250 -46
- package/src/workbench/index.ts +7 -1
- package/src/workbench/intellisense.ts +1859 -0
- package/src/workbench/styles.ts +25 -19
- package/src/workbench/xterm-adapter.ts +1566 -0
- package/src/workbench.ts +13446 -3881
package/src/storage.ts
CHANGED
|
@@ -17,6 +17,9 @@ export interface StoredProject {
|
|
|
17
17
|
id: string;
|
|
18
18
|
name: string;
|
|
19
19
|
files: Record<string, string>;
|
|
20
|
+
binaryFiles?: Record<string, Uint8Array | string>;
|
|
21
|
+
gitFiles?: Record<string, Uint8Array | string>;
|
|
22
|
+
gitMeta?: Record<string, unknown>;
|
|
20
23
|
updatedAt: number;
|
|
21
24
|
createdAt?: number;
|
|
22
25
|
entry?: string;
|
|
@@ -38,21 +41,354 @@ export interface WorkspaceStorageOptions {
|
|
|
38
41
|
preferredBackend?: StorageBackendType;
|
|
39
42
|
}
|
|
40
43
|
|
|
44
|
+
export type WorkspaceKind = 'blank' | 'repo' | 'demo' | 'custom';
|
|
45
|
+
|
|
46
|
+
export interface WorkspaceMeta {
|
|
47
|
+
labId: string;
|
|
48
|
+
title: string;
|
|
49
|
+
kind: WorkspaceKind;
|
|
50
|
+
repo?: string;
|
|
51
|
+
updatedAt: number;
|
|
52
|
+
fileCount?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const WORKSPACE_REGISTRY_KEY = 'codelab:workspace-registry:v1';
|
|
56
|
+
|
|
41
57
|
const DEFAULT_DB_NAME = 'codelab_workspace_db';
|
|
42
58
|
const DEFAULT_STORE_NAME = 'projects';
|
|
43
59
|
const DEFAULT_LS_PREFIX = 'codelab:project:';
|
|
60
|
+
const WORKBENCH_STATE_PREFIX = 'sd-codelab-v1:';
|
|
61
|
+
const LEGACY_WORKBENCH_PREFIX = 'codelab_';
|
|
62
|
+
|
|
63
|
+
const memoryRegistry: Map<string, WorkspaceMeta> = new Map();
|
|
64
|
+
const memoryOrIdbNonEmptyProjects: Set<string> = new Set();
|
|
65
|
+
let registryWriteFailed = false;
|
|
66
|
+
let lastBlankTs = 0;
|
|
67
|
+
let blankSeq = 0;
|
|
68
|
+
|
|
69
|
+
function getSafeLocalStorage(): Storage | null {
|
|
70
|
+
try {
|
|
71
|
+
if (typeof globalThis === 'undefined' || !('localStorage' in globalThis)) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const ls = globalThis.localStorage;
|
|
75
|
+
if (
|
|
76
|
+
!ls ||
|
|
77
|
+
typeof ls.getItem !== 'function' ||
|
|
78
|
+
typeof ls.setItem !== 'function' ||
|
|
79
|
+
typeof ls.removeItem !== 'function'
|
|
80
|
+
) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return ls;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
const _ignored = err;
|
|
86
|
+
void _ignored;
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isPlainRecord(val: unknown): val is Record<string, unknown> {
|
|
92
|
+
return Boolean(val && typeof val === 'object' && !Array.isArray(val));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function normalizeWorkspaceMeta(raw: unknown, existing?: WorkspaceMeta): WorkspaceMeta | null {
|
|
96
|
+
if (!isPlainRecord(raw)) return null;
|
|
97
|
+
const labId = typeof raw.labId === 'string' ? raw.labId.trim() : '';
|
|
98
|
+
if (!labId) return null;
|
|
99
|
+
|
|
100
|
+
const rawKind = raw.kind;
|
|
101
|
+
const kind: WorkspaceKind =
|
|
102
|
+
rawKind === 'blank' || rawKind === 'repo' || rawKind === 'demo' || rawKind === 'custom'
|
|
103
|
+
? rawKind
|
|
104
|
+
: (existing?.kind ?? 'custom');
|
|
105
|
+
|
|
106
|
+
const title =
|
|
107
|
+
typeof raw.title === 'string' && raw.title.trim()
|
|
108
|
+
? raw.title.trim()
|
|
109
|
+
: (existing?.title || labId);
|
|
110
|
+
|
|
111
|
+
const updatedAt =
|
|
112
|
+
typeof raw.updatedAt === 'number' && Number.isFinite(raw.updatedAt)
|
|
113
|
+
? raw.updatedAt
|
|
114
|
+
: Date.now();
|
|
115
|
+
|
|
116
|
+
const repo =
|
|
117
|
+
typeof raw.repo === 'string'
|
|
118
|
+
? (raw.repo.trim() || undefined)
|
|
119
|
+
: existing?.repo;
|
|
120
|
+
|
|
121
|
+
const fileCount =
|
|
122
|
+
typeof raw.fileCount === 'number' && Number.isFinite(raw.fileCount) && raw.fileCount >= 0
|
|
123
|
+
? Math.floor(raw.fileCount)
|
|
124
|
+
: existing?.fileCount;
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
labId,
|
|
128
|
+
title,
|
|
129
|
+
kind,
|
|
130
|
+
updatedAt,
|
|
131
|
+
...(repo !== undefined ? { repo } : {}),
|
|
132
|
+
...(fileCount !== undefined ? { fileCount } : {})
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function readRegistryEntries(): Map<string, WorkspaceMeta> {
|
|
137
|
+
const ls = getSafeLocalStorage();
|
|
138
|
+
if (ls && !registryWriteFailed) {
|
|
139
|
+
try {
|
|
140
|
+
const raw = ls.getItem(WORKSPACE_REGISTRY_KEY);
|
|
141
|
+
if (raw === null) {
|
|
142
|
+
memoryRegistry.clear();
|
|
143
|
+
return new Map();
|
|
144
|
+
}
|
|
145
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
146
|
+
if (!Array.isArray(parsed)) {
|
|
147
|
+
return new Map();
|
|
148
|
+
}
|
|
149
|
+
const map = new Map<string, WorkspaceMeta>();
|
|
150
|
+
for (const entry of parsed) {
|
|
151
|
+
const normalized = normalizeWorkspaceMeta(entry);
|
|
152
|
+
if (!normalized) continue;
|
|
153
|
+
const prev = map.get(normalized.labId);
|
|
154
|
+
if (!prev || normalized.updatedAt >= prev.updatedAt) {
|
|
155
|
+
map.set(normalized.labId, normalized);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
memoryRegistry.clear();
|
|
159
|
+
for (const [k, v] of map.entries()) {
|
|
160
|
+
memoryRegistry.set(k, { ...v });
|
|
161
|
+
}
|
|
162
|
+
return map;
|
|
163
|
+
} catch (err) {
|
|
164
|
+
const _ignored = err;
|
|
165
|
+
void _ignored;
|
|
166
|
+
return new Map();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const out = new Map<string, WorkspaceMeta>();
|
|
171
|
+
for (const [k, v] of memoryRegistry.entries()) {
|
|
172
|
+
out.set(k, { ...v });
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function persistRegistryEntries(map: Map<string, WorkspaceMeta>): void {
|
|
178
|
+
const sorted = Array.from(map.values()).sort((a, b) => b.updatedAt - a.updatedAt);
|
|
179
|
+
memoryRegistry.clear();
|
|
180
|
+
for (const item of sorted) {
|
|
181
|
+
memoryRegistry.set(item.labId, { ...item });
|
|
182
|
+
}
|
|
183
|
+
const ls = getSafeLocalStorage();
|
|
184
|
+
if (ls) {
|
|
185
|
+
try {
|
|
186
|
+
ls.setItem(WORKSPACE_REGISTRY_KEY, JSON.stringify(sorted));
|
|
187
|
+
registryWriteFailed = false;
|
|
188
|
+
} catch (err) {
|
|
189
|
+
const _ignored = err;
|
|
190
|
+
void _ignored;
|
|
191
|
+
registryWriteFailed = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function listWorkspaces(): WorkspaceMeta[] {
|
|
197
|
+
return Array.from(readRegistryEntries().values()).sort((a, b) => b.updatedAt - a.updatedAt);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function getWorkspaceMeta(labId: string): WorkspaceMeta | null {
|
|
201
|
+
if (typeof labId !== 'string') return null;
|
|
202
|
+
const trimmed = labId.trim();
|
|
203
|
+
if (!trimmed) return null;
|
|
204
|
+
const found = readRegistryEntries().get(trimmed);
|
|
205
|
+
return found ? { ...found } : null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function upsertWorkspaceMeta(meta: WorkspaceMeta): void {
|
|
209
|
+
if (!isPlainRecord(meta)) return;
|
|
210
|
+
const trimmedId = typeof meta.labId === 'string' ? meta.labId.trim() : '';
|
|
211
|
+
if (!trimmedId) return;
|
|
212
|
+
const map = readRegistryEntries();
|
|
213
|
+
const normalized = normalizeWorkspaceMeta(meta, map.get(trimmedId));
|
|
214
|
+
if (!normalized) return;
|
|
215
|
+
map.set(normalized.labId, normalized);
|
|
216
|
+
persistRegistryEntries(map);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function removeWorkspaceMeta(labId: string): void {
|
|
220
|
+
if (typeof labId !== 'string') return;
|
|
221
|
+
const trimmed = labId.trim();
|
|
222
|
+
if (!trimmed) return;
|
|
223
|
+
const map = readRegistryEntries();
|
|
224
|
+
if (map.delete(trimmed)) {
|
|
225
|
+
persistRegistryEntries(map);
|
|
226
|
+
} else {
|
|
227
|
+
memoryRegistry.delete(trimmed);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function payloadHasSavedFiles(rawJson: string | null): boolean {
|
|
232
|
+
if (!rawJson) return false;
|
|
233
|
+
try {
|
|
234
|
+
const parsed = JSON.parse(rawJson) as unknown;
|
|
235
|
+
if (!isPlainRecord(parsed)) return false;
|
|
236
|
+
|
|
237
|
+
if (isPlainRecord(parsed.files) && Object.keys(parsed.files).length > 0) {
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (isPlainRecord(parsed.customFiles)) {
|
|
242
|
+
for (const val of Object.values(parsed.customFiles)) {
|
|
243
|
+
if (typeof val === 'string') {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
if (isPlainRecord(val) && Object.keys(val).length > 0) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
} catch (err) {
|
|
252
|
+
const _ignored = err;
|
|
253
|
+
void _ignored;
|
|
254
|
+
}
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function checkHasWorkspaceData(
|
|
259
|
+
labId: string,
|
|
260
|
+
extraPrefix?: string,
|
|
261
|
+
instanceMemory?: ReadonlyMap<string, StoredProject>
|
|
262
|
+
): boolean {
|
|
263
|
+
if (typeof labId !== 'string') return false;
|
|
264
|
+
const trimmed = labId.trim();
|
|
265
|
+
if (!trimmed) return false;
|
|
266
|
+
|
|
267
|
+
if (instanceMemory) {
|
|
268
|
+
const proj = instanceMemory.get(trimmed);
|
|
269
|
+
if (proj && isPlainRecord(proj.files) && Object.keys(proj.files).length > 0) {
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (memoryOrIdbNonEmptyProjects.has(trimmed)) {
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const ls = getSafeLocalStorage();
|
|
279
|
+
if (ls) {
|
|
280
|
+
try {
|
|
281
|
+
if (payloadHasSavedFiles(ls.getItem(`${WORKBENCH_STATE_PREFIX}${trimmed}`))) {
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
if (payloadHasSavedFiles(ls.getItem(`${LEGACY_WORKBENCH_PREFIX}${trimmed}`))) {
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
if (payloadHasSavedFiles(ls.getItem(`${DEFAULT_LS_PREFIX}${trimmed}`))) {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
if (
|
|
291
|
+
typeof extraPrefix === 'string' &&
|
|
292
|
+
extraPrefix &&
|
|
293
|
+
extraPrefix !== DEFAULT_LS_PREFIX &&
|
|
294
|
+
payloadHasSavedFiles(ls.getItem(`${extraPrefix}${trimmed}`))
|
|
295
|
+
) {
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
} catch (err) {
|
|
299
|
+
const _ignored = err;
|
|
300
|
+
void _ignored;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function hasWorkspaceData(labId: string): boolean {
|
|
308
|
+
return checkHasWorkspaceData(labId);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function createBlankWorkspaceId(): string {
|
|
312
|
+
const now = Date.now();
|
|
313
|
+
if (now === lastBlankTs) {
|
|
314
|
+
blankSeq += 1;
|
|
315
|
+
} else {
|
|
316
|
+
lastBlankTs = now;
|
|
317
|
+
blankSeq = 0;
|
|
318
|
+
}
|
|
319
|
+
const rand = Math.random().toString(36).slice(2, 6).padEnd(4, '0');
|
|
320
|
+
const suffix = blankSeq > 0 ? `${blankSeq.toString(36)}${rand}` : rand;
|
|
321
|
+
return `blank-workspace-${now}-${suffix}`;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export function encodeBase64Bytes(bytes: Uint8Array): string {
|
|
325
|
+
if (typeof Buffer !== 'undefined') {
|
|
326
|
+
return Buffer.from(bytes).toString('base64');
|
|
327
|
+
}
|
|
328
|
+
let binary = '';
|
|
329
|
+
const chunk = 0x8000;
|
|
330
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
331
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
332
|
+
}
|
|
333
|
+
return typeof btoa === 'function' ? btoa(binary) : binary;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function decodeBase64Bytes(b64: string): Uint8Array {
|
|
337
|
+
if (typeof Buffer !== 'undefined') {
|
|
338
|
+
const buf = Buffer.from(b64, 'base64');
|
|
339
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
340
|
+
}
|
|
341
|
+
if (typeof atob === 'function') {
|
|
342
|
+
const bin = atob(b64);
|
|
343
|
+
const out = new Uint8Array(bin.length);
|
|
344
|
+
for (let i = 0; i < bin.length; i++) {
|
|
345
|
+
out[i] = bin.charCodeAt(i);
|
|
346
|
+
}
|
|
347
|
+
return out;
|
|
348
|
+
}
|
|
349
|
+
return new Uint8Array(0);
|
|
350
|
+
}
|
|
44
351
|
|
|
45
|
-
function
|
|
352
|
+
function cloneByteMap(
|
|
353
|
+
map: Record<string, Uint8Array | string> | undefined,
|
|
354
|
+
serializeBase64 = false
|
|
355
|
+
): Record<string, Uint8Array | string> | undefined {
|
|
356
|
+
if (!map || typeof map !== 'object') return undefined;
|
|
357
|
+
const out: Record<string, Uint8Array | string> = {};
|
|
358
|
+
let count = 0;
|
|
359
|
+
for (const [k, v] of Object.entries(map)) {
|
|
360
|
+
if (v instanceof Uint8Array) {
|
|
361
|
+
out[k] = serializeBase64 ? encodeBase64Bytes(v) : new Uint8Array(v);
|
|
362
|
+
count++;
|
|
363
|
+
} else if (ArrayBuffer.isView(v)) {
|
|
364
|
+
const view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
|
|
365
|
+
out[k] = serializeBase64 ? encodeBase64Bytes(view) : new Uint8Array(view);
|
|
366
|
+
count++;
|
|
367
|
+
} else if (typeof v === 'string') {
|
|
368
|
+
out[k] = serializeBase64 ? v : decodeBase64Bytes(v);
|
|
369
|
+
count++;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return count > 0 ? out : undefined;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function cloneProject(project: StoredProject, serializeBase64 = false): StoredProject {
|
|
46
376
|
const filesCopy: Record<string, string> = {};
|
|
47
377
|
if (project.files && typeof project.files === 'object') {
|
|
48
378
|
for (const [k, v] of Object.entries(project.files)) {
|
|
49
379
|
filesCopy[k] = String(v);
|
|
50
380
|
}
|
|
51
381
|
}
|
|
382
|
+
const binaryCopy = cloneByteMap(project.binaryFiles, serializeBase64);
|
|
383
|
+
const gitCopy = cloneByteMap(project.gitFiles, serializeBase64);
|
|
384
|
+
const gitMetaCopy = isPlainRecord(project.gitMeta) ? { ...project.gitMeta } : undefined;
|
|
52
385
|
return {
|
|
53
386
|
id: String(project.id),
|
|
54
387
|
name: String(project.name || project.id),
|
|
55
388
|
files: filesCopy,
|
|
389
|
+
...(binaryCopy ? { binaryFiles: binaryCopy } : {}),
|
|
390
|
+
...(gitCopy ? { gitFiles: gitCopy } : {}),
|
|
391
|
+
...(gitMetaCopy ? { gitMeta: gitMetaCopy } : {}),
|
|
56
392
|
updatedAt: typeof project.updatedAt === 'number' ? project.updatedAt : Date.now(),
|
|
57
393
|
...(project.createdAt !== undefined ? { createdAt: project.createdAt } : {}),
|
|
58
394
|
...(project.entry !== undefined ? { entry: project.entry } : {}),
|
|
@@ -95,6 +431,14 @@ function isIndexedDbAvailable(): boolean {
|
|
|
95
431
|
}
|
|
96
432
|
|
|
97
433
|
export class WorkspaceStorage {
|
|
434
|
+
public static readonly WORKSPACE_REGISTRY_KEY = WORKSPACE_REGISTRY_KEY;
|
|
435
|
+
public static readonly listWorkspaces = listWorkspaces;
|
|
436
|
+
public static readonly getWorkspaceMeta = getWorkspaceMeta;
|
|
437
|
+
public static readonly upsertWorkspaceMeta = upsertWorkspaceMeta;
|
|
438
|
+
public static readonly removeWorkspaceMeta = removeWorkspaceMeta;
|
|
439
|
+
public static readonly hasWorkspaceData = hasWorkspaceData;
|
|
440
|
+
public static readonly createBlankWorkspaceId = createBlankWorkspaceId;
|
|
441
|
+
|
|
98
442
|
private readonly dbName: string;
|
|
99
443
|
private readonly storeName: string;
|
|
100
444
|
private readonly storagePrefix: string;
|
|
@@ -161,6 +505,7 @@ export class WorkspaceStorage {
|
|
|
161
505
|
return false;
|
|
162
506
|
}
|
|
163
507
|
const normalized = cloneProject(project);
|
|
508
|
+
const hasFiles = Object.keys(normalized.files).length > 0;
|
|
164
509
|
// Always keep memory mirror hot for resilience
|
|
165
510
|
this.memoryStore.set(normalized.id, normalized);
|
|
166
511
|
|
|
@@ -182,7 +527,11 @@ export class WorkspaceStorage {
|
|
|
182
527
|
resolve(false);
|
|
183
528
|
}
|
|
184
529
|
});
|
|
185
|
-
if (ok)
|
|
530
|
+
if (ok) {
|
|
531
|
+
if (hasFiles) memoryOrIdbNonEmptyProjects.add(normalized.id);
|
|
532
|
+
else memoryOrIdbNonEmptyProjects.delete(normalized.id);
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
186
535
|
this.downgradeFromIdb();
|
|
187
536
|
}
|
|
188
537
|
}
|
|
@@ -190,7 +539,9 @@ export class WorkspaceStorage {
|
|
|
190
539
|
if (this.backendType === 'localstorage') {
|
|
191
540
|
try {
|
|
192
541
|
const key = `${this.storagePrefix}${normalized.id}`;
|
|
193
|
-
|
|
542
|
+
const serializable = cloneProject(normalized, true);
|
|
543
|
+
globalThis.localStorage.setItem(key, JSON.stringify(serializable));
|
|
544
|
+
memoryOrIdbNonEmptyProjects.delete(normalized.id);
|
|
194
545
|
return true;
|
|
195
546
|
} catch (err) {
|
|
196
547
|
// QuotaExceededError or disabled storage -> fallback to memory
|
|
@@ -200,6 +551,8 @@ export class WorkspaceStorage {
|
|
|
200
551
|
}
|
|
201
552
|
}
|
|
202
553
|
|
|
554
|
+
if (hasFiles) memoryOrIdbNonEmptyProjects.add(normalized.id);
|
|
555
|
+
else memoryOrIdbNonEmptyProjects.delete(normalized.id);
|
|
203
556
|
return true;
|
|
204
557
|
}
|
|
205
558
|
|
|
@@ -320,6 +673,7 @@ export class WorkspaceStorage {
|
|
|
320
673
|
|
|
321
674
|
public async deleteProject(id: string): Promise<boolean> {
|
|
322
675
|
if (!id || typeof id !== 'string') return false;
|
|
676
|
+
memoryOrIdbNonEmptyProjects.delete(id);
|
|
323
677
|
let existed = this.memoryStore.delete(id);
|
|
324
678
|
|
|
325
679
|
if (this.backendType === 'indexeddb') {
|
|
@@ -365,13 +719,55 @@ export class WorkspaceStorage {
|
|
|
365
719
|
|
|
366
720
|
return existed;
|
|
367
721
|
}
|
|
722
|
+
|
|
723
|
+
public listWorkspaces(): WorkspaceMeta[] {
|
|
724
|
+
return listWorkspaces();
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
public getWorkspaceMeta(labId: string): WorkspaceMeta | null {
|
|
728
|
+
return getWorkspaceMeta(labId);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
public upsertWorkspaceMeta(meta: WorkspaceMeta): void {
|
|
732
|
+
upsertWorkspaceMeta(meta);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
public removeWorkspaceMeta(labId: string): void {
|
|
736
|
+
removeWorkspaceMeta(labId);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
public hasWorkspaceData(labId: string): boolean {
|
|
740
|
+
return checkHasWorkspaceData(labId, this.storagePrefix, this.memoryStore);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
public createBlankWorkspaceId(): string {
|
|
744
|
+
return createBlankWorkspaceId();
|
|
745
|
+
}
|
|
368
746
|
}
|
|
369
747
|
|
|
370
748
|
export function createWorkspaceStorage(options: WorkspaceStorageOptions = {}): WorkspaceStorage {
|
|
371
749
|
return new WorkspaceStorage(options);
|
|
372
750
|
}
|
|
373
751
|
|
|
374
|
-
export
|
|
752
|
+
export const CodeLabWorkspaceStorage = {
|
|
753
|
+
WORKSPACE_REGISTRY_KEY,
|
|
375
754
|
WorkspaceStorage,
|
|
376
|
-
createWorkspaceStorage
|
|
755
|
+
createWorkspaceStorage,
|
|
756
|
+
listWorkspaces,
|
|
757
|
+
getWorkspaceMeta,
|
|
758
|
+
upsertWorkspaceMeta,
|
|
759
|
+
removeWorkspaceMeta,
|
|
760
|
+
hasWorkspaceData,
|
|
761
|
+
createBlankWorkspaceId,
|
|
762
|
+
encodeBase64Bytes,
|
|
763
|
+
decodeBase64Bytes
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
if (typeof globalThis !== 'undefined') {
|
|
767
|
+
const g = globalThis as unknown as Record<string, unknown>;
|
|
768
|
+
g.CodeLabWorkspaceStorage = CodeLabWorkspaceStorage;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
export default {
|
|
772
|
+
...CodeLabWorkspaceStorage
|
|
377
773
|
};
|