@innovatorved/code-lab 0.1.6 → 0.1.8
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 +34 -16
- package/README.md +17 -7
- package/dist/code-lab.css +26 -0
- package/dist/code-lab.d.ts +271 -1
- package/dist/code-lab.esm.js +2909 -166
- package/dist/code-lab.js +2869 -164
- package/dist/csp.d.ts +86 -0
- package/dist/diagnostics.d.ts +43 -0
- package/dist/extensions/guided-lab.esm.js +66 -0
- package/dist/extensions/guided-lab.js +66 -0
- package/dist/html.d.ts +24 -0
- package/dist/index.d.ts +9 -1
- package/dist/logger.d.ts +60 -0
- package/dist/preview-sw.js +425 -11
- package/dist/runtime.d.ts +6 -27
- package/dist/runtime.esm.js +171 -31
- package/dist/runtime.js +171 -31
- package/dist/schemas.d.ts +366 -25
- package/dist/schemas.esm.js +73 -0
- package/dist/schemas.js +73 -0
- package/dist/scm.d.ts +77 -0
- package/dist/search.d.ts +75 -0
- package/dist/shell.d.ts +18 -47
- package/dist/shell.esm.js +236 -37
- package/dist/shell.js +236 -37
- package/dist/storage.d.ts +57 -0
- package/dist/terminal.d.ts +11 -87
- package/dist/terminal.esm.js +124 -7
- package/dist/terminal.js +124 -7
- package/dist/zip.d.ts +42 -0
- package/package.json +11 -3
- package/src/code-lab.css +26 -0
- package/src/csp.ts +396 -0
- package/src/diagnostics.ts +116 -0
- package/src/html.ts +88 -0
- package/src/index.ts +39 -1
- package/src/logger.ts +139 -0
- package/src/preview-sw.js +97 -11
- package/src/runtime.ts +132 -105
- package/src/schemas.ts +94 -1
- package/src/scm.ts +268 -0
- package/src/search.ts +389 -0
- package/src/shell.ts +410 -369
- package/src/storage.ts +377 -0
- package/src/terminal.ts +898 -918
- package/src/workbench.ts +926 -89
- package/src/zip.ts +326 -0
- package/src/runtime.js +0 -8229
- package/src/shell.js +0 -7257
- package/src/terminal.js +0 -1046
- package/src/workbench.js +0 -7353
package/src/logger.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab — Shared Leveled Logger & Typed Error Hierarchy
|
|
3
|
+
* ============================================================================
|
|
4
|
+
* Bundled as `CodeLabLogger` into the runtime, shell, terminal, and workbench
|
|
5
|
+
* bundles so no subsystem needs empty `catch` blocks or ad-hoc console calls.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const LOG_PREFIX = '[code-lab]';
|
|
9
|
+
|
|
10
|
+
export type CodeLabLogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
11
|
+
|
|
12
|
+
export interface CodeLabLogSink {
|
|
13
|
+
debug?: (...args: unknown[]) => void;
|
|
14
|
+
info?: (...args: unknown[]) => void;
|
|
15
|
+
warn?: (...args: unknown[]) => void;
|
|
16
|
+
error?: (...args: unknown[]) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CodeLabLoggerOptions {
|
|
20
|
+
logger?: CodeLabLogSink | null;
|
|
21
|
+
debug?: boolean;
|
|
22
|
+
prefix?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface CodeLabLogger {
|
|
26
|
+
debug: (...args: unknown[]) => void;
|
|
27
|
+
info: (...args: unknown[]) => void;
|
|
28
|
+
warn: (...args: unknown[]) => void;
|
|
29
|
+
error: (...args: unknown[]) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type CodeLabErrorCode =
|
|
33
|
+
| 'E_SAVE'
|
|
34
|
+
| 'E_PERSISTENCE'
|
|
35
|
+
| 'E_INSTALL'
|
|
36
|
+
| 'E_PREVIEW'
|
|
37
|
+
| 'E_RUNTIME'
|
|
38
|
+
| 'E_SHELL'
|
|
39
|
+
| 'E_CLONE'
|
|
40
|
+
| 'E_CONFIG';
|
|
41
|
+
|
|
42
|
+
export interface CodeLabErrorOptions {
|
|
43
|
+
cause?: unknown;
|
|
44
|
+
recoverable?: boolean;
|
|
45
|
+
details?: Record<string, unknown>;
|
|
46
|
+
context?: Record<string, unknown>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Structured error surfaced through `wb.on('error', ...)` and `wb.on('save-state', ...)`.
|
|
51
|
+
*/
|
|
52
|
+
export class CodeLabError extends Error {
|
|
53
|
+
readonly code: CodeLabErrorCode;
|
|
54
|
+
readonly options: CodeLabErrorOptions;
|
|
55
|
+
override readonly cause?: unknown;
|
|
56
|
+
readonly recoverable: boolean;
|
|
57
|
+
readonly details?: Record<string, unknown>;
|
|
58
|
+
readonly context?: Record<string, unknown>;
|
|
59
|
+
|
|
60
|
+
constructor(code: CodeLabErrorCode, message: string, options: CodeLabErrorOptions = {}) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.name = 'CodeLabError';
|
|
63
|
+
this.code = code;
|
|
64
|
+
this.options = options;
|
|
65
|
+
this.cause = options.cause;
|
|
66
|
+
this.recoverable = options.recoverable !== false;
|
|
67
|
+
const meta = options.details ?? options.context;
|
|
68
|
+
if (meta !== undefined) {
|
|
69
|
+
this.details = meta;
|
|
70
|
+
this.context = meta;
|
|
71
|
+
}
|
|
72
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function isCodeLabError(value: unknown): value is CodeLabError {
|
|
77
|
+
return Boolean(
|
|
78
|
+
value &&
|
|
79
|
+
typeof value === 'object' &&
|
|
80
|
+
(value instanceof CodeLabError ||
|
|
81
|
+
((value as { name?: unknown }).name === 'CodeLabError' &&
|
|
82
|
+
typeof (value as { code?: unknown }).code === 'string'))
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Small leveled logger. Hosts can pass `options.logger` to route diagnostics
|
|
88
|
+
* into their own telemetry; `options.debug` enables verbose output. Errors and
|
|
89
|
+
* warnings are never silently discarded.
|
|
90
|
+
*/
|
|
91
|
+
export function createLogger(options: CodeLabLoggerOptions = {}): CodeLabLogger {
|
|
92
|
+
const sink = options.logger ?? null;
|
|
93
|
+
const debugEnabled = options.debug === true;
|
|
94
|
+
const prefix = options.prefix || LOG_PREFIX;
|
|
95
|
+
|
|
96
|
+
const write = (level: CodeLabLogLevel, args: unknown[]): void => {
|
|
97
|
+
if (level === 'debug' && !debugEnabled) return;
|
|
98
|
+
if (sink && typeof sink[level] === 'function') {
|
|
99
|
+
let sinkError: unknown = null;
|
|
100
|
+
try {
|
|
101
|
+
(sink[level] as (...a: unknown[]) => void)(...args);
|
|
102
|
+
return;
|
|
103
|
+
} catch (err) {
|
|
104
|
+
// A broken host logger must not break the IDE; fall through to console.
|
|
105
|
+
sinkError = err;
|
|
106
|
+
}
|
|
107
|
+
void sinkError;
|
|
108
|
+
}
|
|
109
|
+
const consoleObj = typeof console !== 'undefined' ? console : null;
|
|
110
|
+
const fn = consoleObj ? consoleObj[level] : null;
|
|
111
|
+
if (typeof fn === 'function') {
|
|
112
|
+
fn.call(consoleObj, prefix, ...args);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
debug: (...args: unknown[]) => write('debug', args),
|
|
118
|
+
info: (...args: unknown[]) => write('info', args),
|
|
119
|
+
warn: (...args: unknown[]) => write('warn', args),
|
|
120
|
+
error: (...args: unknown[]) => write('error', args)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const DEFAULT_LOGGER: CodeLabLogger = createLogger();
|
|
125
|
+
|
|
126
|
+
const _loggerNamespace = {
|
|
127
|
+
LOG_PREFIX,
|
|
128
|
+
CodeLabError,
|
|
129
|
+
isCodeLabError,
|
|
130
|
+
createLogger,
|
|
131
|
+
DEFAULT_LOGGER
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const _globalRoot = typeof globalThis !== 'undefined' ? (globalThis as Record<string, unknown>) : null;
|
|
135
|
+
if (_globalRoot && !_globalRoot.CodeLabLogger) {
|
|
136
|
+
_globalRoot.CodeLabLogger = _loggerNamespace;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default _loggerNamespace;
|
package/src/preview-sw.js
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
* This enables "pop out to tab" Web Preview for virtual HTTP / Bun servers that
|
|
9
9
|
* exist only inside the workbench's in-memory runtime (no real OS socket).
|
|
10
10
|
*
|
|
11
|
+
* The Content-Security-Policy, the storage shims and the violation reporter all
|
|
12
|
+
* come from `src/csp.ts`, which the build prepends to this file. The workbench
|
|
13
|
+
* embeds the same module, so the two can never drift.
|
|
14
|
+
*
|
|
11
15
|
* Zero dependencies. Plain Service Worker script (no module imports) so it can
|
|
12
16
|
* be registered directly from any host origin.
|
|
13
17
|
*/
|
|
@@ -16,6 +20,32 @@
|
|
|
16
20
|
|
|
17
21
|
const PREVIEW_PREFIX = '/__preview/';
|
|
18
22
|
const DISPATCH_TIMEOUT_MS = 15000;
|
|
23
|
+
const MAX_CONFIG_LENGTH = 8192;
|
|
24
|
+
|
|
25
|
+
// Policy configured by the workbench via postMessage. The worker cannot read the
|
|
26
|
+
// host's options, so the host pushes the resolved policy after registering.
|
|
27
|
+
let configuredCsp = null;
|
|
28
|
+
|
|
29
|
+
function cspModule() {
|
|
30
|
+
// `CodeLabCsp` is declared by the prepended csp bundle (src/csp.ts).
|
|
31
|
+
return typeof CodeLabCsp !== 'undefined' ? CodeLabCsp : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function previewPolicy() {
|
|
35
|
+
if (configuredCsp) return configuredCsp;
|
|
36
|
+
const csp = cspModule();
|
|
37
|
+
if (!csp) return "default-src 'self'; object-src 'none'";
|
|
38
|
+
// The workbench pushes its resolved policy on registration, so this branch
|
|
39
|
+
// only runs in the brief window before that message arrives. Use the
|
|
40
|
+
// documented default (`balanced`) rather than a stricter one, otherwise the
|
|
41
|
+
// very first preview of a CDN-based app can be blocked by a race.
|
|
42
|
+
return csp.buildPreviewPolicy({ hostOrigin: self.location.origin, mode: 'balanced' });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function previewShims() {
|
|
46
|
+
const csp = cspModule();
|
|
47
|
+
return csp ? csp.buildPreviewShims() : '';
|
|
48
|
+
}
|
|
19
49
|
|
|
20
50
|
self.addEventListener('install', () => {
|
|
21
51
|
self.skipWaiting();
|
|
@@ -25,6 +55,25 @@ self.addEventListener('activate', (event) => {
|
|
|
25
55
|
event.waitUntil(self.clients.claim());
|
|
26
56
|
});
|
|
27
57
|
|
|
58
|
+
self.addEventListener('message', (event) => {
|
|
59
|
+
const data = event && event.data;
|
|
60
|
+
if (!data || data.type !== 'codelab:config') return;
|
|
61
|
+
const csp = typeof data.csp === 'string' ? data.csp.trim() : '';
|
|
62
|
+
if (!csp || csp.length > MAX_CONFIG_LENGTH) return;
|
|
63
|
+
configuredCsp = csp;
|
|
64
|
+
// Acknowledge so the host can wait for confirmation before loading a frame.
|
|
65
|
+
try {
|
|
66
|
+
if (event.ports && event.ports[0]) {
|
|
67
|
+
event.ports[0].postMessage({ type: 'codelab:config-ready' });
|
|
68
|
+
} else if (event.source && typeof event.source.postMessage === 'function') {
|
|
69
|
+
event.source.postMessage({ type: 'codelab:config-ready' });
|
|
70
|
+
}
|
|
71
|
+
} catch (err) {
|
|
72
|
+
// The client may have navigated away between postMessage and reply.
|
|
73
|
+
console.warn('[code-lab] preview-sw: config acknowledgement failed', err);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
28
77
|
function parsePreviewPath(pathname) {
|
|
29
78
|
const match = pathname.match(/^\/__preview\/(\d+)(\/.*)?$/);
|
|
30
79
|
if (!match) return null;
|
|
@@ -32,15 +81,46 @@ function parsePreviewPath(pathname) {
|
|
|
32
81
|
}
|
|
33
82
|
|
|
34
83
|
function previewErrorResponse(status, title, message) {
|
|
35
|
-
const
|
|
84
|
+
const csp = previewPolicy();
|
|
85
|
+
const body = `<!doctype html><meta charset="utf-8">` +
|
|
86
|
+
`<meta http-equiv="Content-Security-Policy" content="${csp}">` +
|
|
87
|
+
`<title>${title}</title>` +
|
|
36
88
|
`<body style="font-family:system-ui,-apple-system,sans-serif;padding:32px;color:#666;background:#0a0a0a">` +
|
|
37
89
|
`<h1 style="color:#e5e5e5;font-size:18px">${title}</h1><p>${message}</p></body>`;
|
|
38
90
|
return new Response(body, {
|
|
39
91
|
status,
|
|
40
|
-
headers: {
|
|
92
|
+
headers: {
|
|
93
|
+
'Content-Type': 'text/html; charset=utf-8',
|
|
94
|
+
'Content-Security-Policy': csp,
|
|
95
|
+
'X-Content-Type-Options': 'nosniff'
|
|
96
|
+
}
|
|
41
97
|
});
|
|
42
98
|
}
|
|
43
99
|
|
|
100
|
+
/** Inject the policy, a base URL and the sandbox shims into an HTML response. */
|
|
101
|
+
function injectPreviewHead(html, port) {
|
|
102
|
+
const csp = previewPolicy();
|
|
103
|
+
const shims = previewShims();
|
|
104
|
+
const tags = [];
|
|
105
|
+
const hasCsp = /<meta[^>]+http-equiv=["']?content-security-policy/i.test(html);
|
|
106
|
+
const hasBase = /<base\b/i.test(html);
|
|
107
|
+
const hasShims = html.indexOf('data-codelab-preview-shims') !== -1;
|
|
108
|
+
|
|
109
|
+
if (!hasCsp) tags.push(`<meta http-equiv="Content-Security-Policy" content="${csp}">`);
|
|
110
|
+
if (!hasBase) tags.push(`<base href="${PREVIEW_PREFIX}${port}/">`);
|
|
111
|
+
if (!hasShims && shims) tags.push(shims);
|
|
112
|
+
if (tags.length === 0) return html;
|
|
113
|
+
|
|
114
|
+
const injection = tags.join('');
|
|
115
|
+
if (/<head\b[^>]*>/i.test(html)) {
|
|
116
|
+
return html.replace(/<head\b[^>]*>/i, (match) => match + injection);
|
|
117
|
+
}
|
|
118
|
+
if (/<html\b[^>]*>/i.test(html)) {
|
|
119
|
+
return html.replace(/<html\b[^>]*>/i, (match) => `${match}<head>${injection}</head>`);
|
|
120
|
+
}
|
|
121
|
+
return `<head>${injection}</head>${html}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
44
124
|
async function proxyVirtualRequest(event, port, targetPath) {
|
|
45
125
|
const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
|
|
46
126
|
if (!clients || !clients.length) {
|
|
@@ -61,13 +141,18 @@ async function proxyVirtualRequest(event, port, targetPath) {
|
|
|
61
141
|
for (const [key, value] of request.headers.entries()) {
|
|
62
142
|
headers[key.toLowerCase()] = value;
|
|
63
143
|
}
|
|
64
|
-
} catch (
|
|
144
|
+
} catch (err) {
|
|
145
|
+
// Opaque or unsupported header collection; forward without request headers.
|
|
146
|
+
console.warn('[code-lab] preview-sw: could not read request headers', err);
|
|
147
|
+
}
|
|
65
148
|
|
|
66
149
|
let body = null;
|
|
67
150
|
if (!['GET', 'HEAD'].includes(method)) {
|
|
68
151
|
try {
|
|
69
152
|
body = await request.arrayBuffer();
|
|
70
|
-
} catch (
|
|
153
|
+
} catch (err) {
|
|
154
|
+
console.warn('[code-lab] preview-sw: could not read request body', err);
|
|
155
|
+
}
|
|
71
156
|
}
|
|
72
157
|
|
|
73
158
|
const response = await new Promise((resolve) => {
|
|
@@ -102,13 +187,12 @@ async function proxyVirtualRequest(event, port, targetPath) {
|
|
|
102
187
|
const responseHeaders = new Headers((response && response.headers) || {});
|
|
103
188
|
const contentType = responseHeaders.get('content-type') || '';
|
|
104
189
|
|
|
105
|
-
// Inject a <base> so relative asset URLs in HTML responses
|
|
106
|
-
// `/__preview/:port/` namespace rather than the host
|
|
190
|
+
// Inject the policy and a <base> so relative asset URLs in HTML responses
|
|
191
|
+
// resolve under the `/__preview/:port/` namespace rather than the host root.
|
|
107
192
|
if (/text\/html/i.test(contentType) && typeof responseBody === 'string') {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
193
|
+
responseBody = injectPreviewHead(responseBody, port);
|
|
194
|
+
responseHeaders.set('Content-Security-Policy', previewPolicy());
|
|
195
|
+
responseHeaders.set('X-Content-Type-Options', 'nosniff');
|
|
112
196
|
}
|
|
113
197
|
|
|
114
198
|
return new Response(responseBody, {
|
|
@@ -122,7 +206,9 @@ self.addEventListener('fetch', (event) => {
|
|
|
122
206
|
let url;
|
|
123
207
|
try {
|
|
124
208
|
url = new URL(event.request.url);
|
|
125
|
-
} catch (
|
|
209
|
+
} catch (err) {
|
|
210
|
+
// Non-standard request URL; let the network handle it.
|
|
211
|
+
console.warn('[code-lab] preview-sw: unparsable request URL', err);
|
|
126
212
|
return;
|
|
127
213
|
}
|
|
128
214
|
|