@innovatorved/code-lab 0.1.4 → 0.1.6
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 +2 -1
- package/README.md +4 -2
- package/dist/code-lab.css +49 -0
- package/dist/code-lab.d.ts +3 -1
- package/dist/code-lab.esm.js +1472 -87
- package/dist/code-lab.js +1462 -86
- package/dist/runtime.d.ts +1 -0
- package/dist/runtime.esm.js +783 -32
- package/dist/runtime.js +740 -21
- package/dist/schemas.esm.js +4191 -0
- package/dist/schemas.js +4207 -0
- package/dist/shell.esm.js +657 -64
- package/dist/shell.js +657 -64
- package/dist/terminal.esm.js +7 -1
- package/package.json +9 -4
- package/src/code-lab.css +49 -0
- package/src/runtime.js +740 -21
- package/src/runtime.ts +741 -21
- package/src/shell.js +657 -64
- package/src/shell.ts +657 -64
- package/src/workbench.js +64 -1
- package/src/workbench.ts +64 -1
package/dist/runtime.esm.js
CHANGED
|
@@ -827,9 +827,78 @@ function __initVirtualRuntime(rootScope) {
|
|
|
827
827
|
crc = CRC32_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
|
|
828
828
|
}
|
|
829
829
|
return (crc ^ 0xffffffff) >>> 0;
|
|
830
|
+
},
|
|
831
|
+
// The full Node zlib constant surface. Packages such as axios read these at
|
|
832
|
+
// module-load time (e.g. `zlib.constants.Z_SYNC_FLUSH`) even when they never
|
|
833
|
+
// actually compress, so a missing table breaks the whole module graph.
|
|
834
|
+
constants: {
|
|
835
|
+
Z_NO_FLUSH: 0, Z_PARTIAL_FLUSH: 1, Z_SYNC_FLUSH: 2, Z_FULL_FLUSH: 3, Z_FINISH: 4,
|
|
836
|
+
Z_BLOCK: 5, Z_TREES: 6, Z_OK: 0, Z_STREAM_END: 1, Z_NEED_DICT: 2, Z_ERRNO: -1,
|
|
837
|
+
Z_STREAM_ERROR: -2, Z_DATA_ERROR: -3, Z_MEM_ERROR: -4, Z_BUF_ERROR: -5,
|
|
838
|
+
Z_VERSION_ERROR: -6, Z_NO_COMPRESSION: 0, Z_BEST_SPEED: 1, Z_BEST_COMPRESSION: 9,
|
|
839
|
+
Z_DEFAULT_COMPRESSION: -1, Z_FILTERED: 1, Z_HUFFMAN_ONLY: 2, Z_RLE: 3, Z_FIXED: 4,
|
|
840
|
+
Z_DEFAULT_STRATEGY: 0, ZLIB_VERNUM: 0x1310, DEFLATE: 1, INFLATE: 2, GZIP: 3,
|
|
841
|
+
GUNZIP: 4, DEFLATERAW: 5, INFLATERAW: 6, UNZIP: 7, BROTLI_DECODE: 8,
|
|
842
|
+
BROTLI_ENCODE: 9, Z_MIN_WINDOWBITS: 8, Z_MAX_WINDOWBITS: 15,
|
|
843
|
+
Z_DEFAULT_WINDOWBITS: 15, Z_MIN_CHUNK: 64, Z_MAX_CHUNK: Infinity,
|
|
844
|
+
Z_DEFAULT_CHUNK: 16384, Z_MIN_MEMLEVEL: 1, Z_MAX_MEMLEVEL: 9,
|
|
845
|
+
Z_DEFAULT_MEMLEVEL: 8, Z_MIN_LEVEL: -1, Z_MAX_LEVEL: 9, Z_DEFAULT_LEVEL: -1
|
|
830
846
|
}
|
|
831
847
|
};
|
|
832
848
|
|
|
849
|
+
// Compression: delegate to the host's real zlib when the runtime is embedded in
|
|
850
|
+
// Node (same process). In a pure browser worker there is no synchronous zlib, so
|
|
851
|
+
// the async APIs use CompressionStream/DecompressionStream and the *Sync variants
|
|
852
|
+
// throw a descriptive error instead of silently corrupting data.
|
|
853
|
+
(() => {
|
|
854
|
+
let hostZlib = null;
|
|
855
|
+
try { hostZlib = (typeof require === 'function') ? require('zlib') : null; } catch (_) { hostZlib = null; }
|
|
856
|
+
const names = [
|
|
857
|
+
'gzip', 'gunzip', 'deflate', 'inflate', 'deflateRaw', 'inflateRaw', 'unzip',
|
|
858
|
+
'gzipSync', 'gunzipSync', 'deflateSync', 'inflateSync', 'deflateRawSync', 'inflateRawSync', 'unzipSync',
|
|
859
|
+
'createGzip', 'createGunzip', 'createDeflate', 'createInflate', 'createDeflateRaw', 'createInflateRaw'
|
|
860
|
+
];
|
|
861
|
+
if (hostZlib) {
|
|
862
|
+
for (const n of names) {
|
|
863
|
+
if (typeof hostZlib[n] === 'function') VirtualZlib[n] = hostZlib[n].bind(hostZlib);
|
|
864
|
+
}
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
const asyncVariant = (format, kind) => (data, optsOrCb, maybeCb) => {
|
|
868
|
+
const cb = typeof optsOrCb === 'function' ? optsOrCb : (typeof maybeCb === 'function' ? maybeCb : null);
|
|
869
|
+
const p = (async () => {
|
|
870
|
+
if (typeof CompressionStream === 'undefined' || typeof DecompressionStream === 'undefined' || typeof Response === 'undefined') {
|
|
871
|
+
throw new Error('zlib.' + kind + ' is not supported in this environment');
|
|
872
|
+
}
|
|
873
|
+
const decompress = /^(gunzip|unzip|inflate)/.test(kind);
|
|
874
|
+
const stream = decompress ? new DecompressionStream(format) : new CompressionStream(format);
|
|
875
|
+
const writer = stream.writable.getWriter();
|
|
876
|
+
const input = data instanceof Uint8Array ? data : VirtualBuffer.from(data);
|
|
877
|
+
writer.write(input);
|
|
878
|
+
writer.close();
|
|
879
|
+
const ab = await new Response(stream.readable).arrayBuffer();
|
|
880
|
+
return VirtualBuffer.from(ab);
|
|
881
|
+
})();
|
|
882
|
+
if (cb) { p.then((out) => cb(null, out), (err) => cb(err)); return undefined; }
|
|
883
|
+
return p;
|
|
884
|
+
};
|
|
885
|
+
VirtualZlib.gzip = asyncVariant('gzip', 'gzip');
|
|
886
|
+
VirtualZlib.gunzip = asyncVariant('gzip', 'gunzip');
|
|
887
|
+
VirtualZlib.unzip = asyncVariant('gzip', 'unzip');
|
|
888
|
+
VirtualZlib.deflate = asyncVariant('deflate', 'deflate');
|
|
889
|
+
VirtualZlib.inflate = asyncVariant('deflate', 'inflate');
|
|
890
|
+
VirtualZlib.deflateRaw = asyncVariant('deflate-raw', 'deflateRaw');
|
|
891
|
+
VirtualZlib.inflateRaw = asyncVariant('deflate-raw', 'inflateRaw');
|
|
892
|
+
const unsupported = (kind) => () => { throw new Error('zlib.' + kind + ' is not supported in a browser worker (use the async API)'); };
|
|
893
|
+
VirtualZlib.gzipSync = unsupported('gzipSync');
|
|
894
|
+
VirtualZlib.gunzipSync = unsupported('gunzipSync');
|
|
895
|
+
VirtualZlib.deflateSync = unsupported('deflateSync');
|
|
896
|
+
VirtualZlib.inflateSync = unsupported('inflateSync');
|
|
897
|
+
VirtualZlib.deflateRawSync = unsupported('deflateRawSync');
|
|
898
|
+
VirtualZlib.inflateRawSync = unsupported('inflateRawSync');
|
|
899
|
+
VirtualZlib.unzipSync = unsupported('unzipSync');
|
|
900
|
+
})();
|
|
901
|
+
|
|
833
902
|
// =========================================================================
|
|
834
903
|
// 4. PATH & OS SHIMS
|
|
835
904
|
// =========================================================================
|
|
@@ -3972,14 +4041,316 @@ function __initVirtualRuntime(rootScope) {
|
|
|
3972
4041
|
};
|
|
3973
4042
|
}
|
|
3974
4043
|
|
|
4044
|
+
// =========================================================================
|
|
4045
|
+
// 7b. JSX / TSX SUPPORT
|
|
4046
|
+
// -------------------------------------------------------------------------
|
|
4047
|
+
// Rewrites JSX syntax into classic `createElement(...)` calls. This runs
|
|
4048
|
+
// BEFORE the string-masking pass so that JSX text (which routinely contains
|
|
4049
|
+
// quotes/apostrophes, e.g. <p>Don't</p>) is captured verbatim, while genuine
|
|
4050
|
+
// JS strings, comments and template literals are skipped untouched.
|
|
4051
|
+
// =========================================================================
|
|
4052
|
+
function detectJsxSyntax(source, filename) {
|
|
4053
|
+
if (/\.(jsx|tsx)$/i.test(filename || '')) return true;
|
|
4054
|
+
if (!source || source.indexOf('<') === -1) return false;
|
|
4055
|
+
if (/@jsx\b/.test(source)) return true;
|
|
4056
|
+
if (source.indexOf('<>') !== -1) return true;
|
|
4057
|
+
if (/<\/[A-Za-z]/.test(source)) return true;
|
|
4058
|
+
return false;
|
|
4059
|
+
}
|
|
4060
|
+
|
|
4061
|
+
function readJsxPragma(source) {
|
|
4062
|
+
let pragma = 'React.createElement';
|
|
4063
|
+
let frag = null;
|
|
4064
|
+
const m = /@jsx\s+([A-Za-z_$][\w$.]*)/.exec(source);
|
|
4065
|
+
if (m) pragma = m[1];
|
|
4066
|
+
const fm = /@jsxFrag\s+([A-Za-z_$][\w$.]*)/.exec(source);
|
|
4067
|
+
if (fm) frag = fm[1];
|
|
4068
|
+
if (!frag) frag = pragma === 'React.createElement' ? 'React.Fragment' : 'Fragment';
|
|
4069
|
+
return { pragma, frag };
|
|
4070
|
+
}
|
|
4071
|
+
|
|
4072
|
+
const JSX_WS_RE = /\s/;
|
|
4073
|
+
const JSX_NAME_START_RE = /[A-Za-z_$]/;
|
|
4074
|
+
const JSX_NAME_CHAR_RE = /[A-Za-z0-9_$:.\-]/;
|
|
4075
|
+
|
|
4076
|
+
function jsxThrow(message) {
|
|
4077
|
+
const err = new Error(message);
|
|
4078
|
+
err.jsxParse = true;
|
|
4079
|
+
throw err;
|
|
4080
|
+
}
|
|
4081
|
+
|
|
4082
|
+
// Returns the exclusive end index if a JS string / template / comment starts
|
|
4083
|
+
// at `i`, otherwise -1. Regexes are deliberately ignored.
|
|
4084
|
+
function jsxSkipJsLiteral(source, i) {
|
|
4085
|
+
const ch = source[i];
|
|
4086
|
+
const next = source[i + 1];
|
|
4087
|
+
const len = source.length;
|
|
4088
|
+
if (ch === '/' && next === '/') {
|
|
4089
|
+
const e = source.indexOf('\n', i);
|
|
4090
|
+
return e === -1 ? len : e;
|
|
4091
|
+
}
|
|
4092
|
+
if (ch === '/' && next === '*') {
|
|
4093
|
+
const e = source.indexOf('*/', i + 2);
|
|
4094
|
+
return e === -1 ? len : e + 2;
|
|
4095
|
+
}
|
|
4096
|
+
if (ch === '"' || ch === "'") {
|
|
4097
|
+
let j = i + 1;
|
|
4098
|
+
while (j < len) {
|
|
4099
|
+
const c = source[j];
|
|
4100
|
+
if (c === '\\') { j += 2; continue; }
|
|
4101
|
+
if (c === ch) { j++; break; }
|
|
4102
|
+
if (c === '\n') break;
|
|
4103
|
+
j++;
|
|
4104
|
+
}
|
|
4105
|
+
return j;
|
|
4106
|
+
}
|
|
4107
|
+
if (ch === '`') {
|
|
4108
|
+
let j = i + 1;
|
|
4109
|
+
let depth = 0;
|
|
4110
|
+
while (j < len) {
|
|
4111
|
+
const c = source[j];
|
|
4112
|
+
if (c === '\\') { j += 2; continue; }
|
|
4113
|
+
if (c === '$' && source[j + 1] === '{') { depth++; j += 2; continue; }
|
|
4114
|
+
if (c === '}' && depth > 0) { depth--; j++; continue; }
|
|
4115
|
+
if (c === '`' && depth === 0) { j++; break; }
|
|
4116
|
+
j++;
|
|
4117
|
+
}
|
|
4118
|
+
return j;
|
|
4119
|
+
}
|
|
4120
|
+
return -1;
|
|
4121
|
+
}
|
|
4122
|
+
|
|
4123
|
+
function jsxReadName(source, i) {
|
|
4124
|
+
if (!JSX_NAME_START_RE.test(source[i] || '')) jsxThrow('jsx: expected a tag name');
|
|
4125
|
+
let j = i;
|
|
4126
|
+
while (j < source.length && JSX_NAME_CHAR_RE.test(source[j])) j++;
|
|
4127
|
+
return { name: source.slice(i, j), next: j };
|
|
4128
|
+
}
|
|
4129
|
+
|
|
4130
|
+
function jsxSkipWs(source, i) {
|
|
4131
|
+
while (i < source.length && JSX_WS_RE.test(source[i])) i++;
|
|
4132
|
+
return i;
|
|
4133
|
+
}
|
|
4134
|
+
|
|
4135
|
+
function normalizeJsxText(raw) {
|
|
4136
|
+
if (!raw) return '';
|
|
4137
|
+
let t = raw.replace(/\r/g, '');
|
|
4138
|
+
if (t.indexOf('\n') !== -1) {
|
|
4139
|
+
t = t.split('\n').map((s) => s.replace(/[ \t]+/g, ' ').trim()).filter(Boolean).join(' ');
|
|
4140
|
+
} else if (!/\S/.test(t)) {
|
|
4141
|
+
return '';
|
|
4142
|
+
} else {
|
|
4143
|
+
t = t.replace(/[ \t]+/g, ' ');
|
|
4144
|
+
}
|
|
4145
|
+
return t ? JSON.stringify(t) : '';
|
|
4146
|
+
}
|
|
4147
|
+
|
|
4148
|
+
// Reads a balanced `{ ... }` expression container, skipping nested JSX and
|
|
4149
|
+
// JS literals. Returns the raw inner source (later re-transformed) plus the
|
|
4150
|
+
// index just past the closing brace.
|
|
4151
|
+
function jsxReadBraced(source, i, opts) {
|
|
4152
|
+
let depth = 0;
|
|
4153
|
+
let j = i;
|
|
4154
|
+
while (j < source.length) {
|
|
4155
|
+
const lit = jsxSkipJsLiteral(source, j);
|
|
4156
|
+
if (lit !== -1) { j = lit; continue; }
|
|
4157
|
+
const c = source[j];
|
|
4158
|
+
if (c === '{') { depth++; j++; continue; }
|
|
4159
|
+
if (c === '}') {
|
|
4160
|
+
depth--;
|
|
4161
|
+
j++;
|
|
4162
|
+
if (depth === 0) return { inner: source.slice(i + 1, j - 1), next: j };
|
|
4163
|
+
continue;
|
|
4164
|
+
}
|
|
4165
|
+
if (c === '<' && (JSX_NAME_START_RE.test(source[j + 1] || '') || source[j + 1] === '>')) {
|
|
4166
|
+
try {
|
|
4167
|
+
const el = jsxParseElement(source, j, opts);
|
|
4168
|
+
j = el.next;
|
|
4169
|
+
continue;
|
|
4170
|
+
} catch (e) {
|
|
4171
|
+
if (!e || !e.jsxParse) throw e;
|
|
4172
|
+
}
|
|
4173
|
+
}
|
|
4174
|
+
j++;
|
|
4175
|
+
}
|
|
4176
|
+
jsxThrow('jsx: unterminated expression container');
|
|
4177
|
+
}
|
|
4178
|
+
|
|
4179
|
+
function jsxParseElement(source, i, opts) {
|
|
4180
|
+
const o = opts || readJsxPragma(source);
|
|
4181
|
+
let j = i + 1;
|
|
4182
|
+
let isFragment = false;
|
|
4183
|
+
let typeExpr;
|
|
4184
|
+
if (source[j] === '>') {
|
|
4185
|
+
isFragment = true;
|
|
4186
|
+
typeExpr = o.frag;
|
|
4187
|
+
j++;
|
|
4188
|
+
} else {
|
|
4189
|
+
const nm = jsxReadName(source, j);
|
|
4190
|
+
j = nm.next;
|
|
4191
|
+
const raw = nm.name;
|
|
4192
|
+
const isReference = raw.indexOf('.') !== -1 || raw.indexOf(':') !== -1 || /^[A-Z_$]/.test(raw);
|
|
4193
|
+
if (isReference) {
|
|
4194
|
+
typeExpr = raw.replace(/:/g, '.');
|
|
4195
|
+
} else {
|
|
4196
|
+
typeExpr = JSON.stringify(raw);
|
|
4197
|
+
}
|
|
4198
|
+
}
|
|
4199
|
+
|
|
4200
|
+
const props = [];
|
|
4201
|
+
let selfClosing = false;
|
|
4202
|
+
if (!isFragment) {
|
|
4203
|
+
for (;;) {
|
|
4204
|
+
j = jsxSkipWs(source, j);
|
|
4205
|
+
if (j >= source.length) jsxThrow('jsx: unexpected end of input inside a tag');
|
|
4206
|
+
const c = source[j];
|
|
4207
|
+
if (c === '/') {
|
|
4208
|
+
if (source[j + 1] !== '>') jsxThrow('jsx: malformed self-closing tag');
|
|
4209
|
+
j += 2;
|
|
4210
|
+
selfClosing = true;
|
|
4211
|
+
break;
|
|
4212
|
+
}
|
|
4213
|
+
if (c === '>') { j++; break; }
|
|
4214
|
+
if (c === '{') {
|
|
4215
|
+
const br = jsxReadBraced(source, j, o);
|
|
4216
|
+
const inner = br.inner.trim();
|
|
4217
|
+
if (inner.slice(0, 3) !== '...') jsxThrow('jsx: only a spread {...} may appear where an attribute is expected');
|
|
4218
|
+
props.push({ spread: jsxTransform(inner.slice(3), o) });
|
|
4219
|
+
j = br.next;
|
|
4220
|
+
continue;
|
|
4221
|
+
}
|
|
4222
|
+
if (!JSX_NAME_START_RE.test(c)) jsxThrow('jsx: malformed attribute');
|
|
4223
|
+
const am = jsxReadName(source, j);
|
|
4224
|
+
const attrName = am.name;
|
|
4225
|
+
j = jsxSkipWs(source, am.next);
|
|
4226
|
+
let valueExpr = 'true';
|
|
4227
|
+
if (source[j] === '=') {
|
|
4228
|
+
j = jsxSkipWs(source, j + 1);
|
|
4229
|
+
const vc = source[j];
|
|
4230
|
+
if (vc === '"' || vc === "'") {
|
|
4231
|
+
let k = j + 1;
|
|
4232
|
+
while (k < source.length && source[k] !== vc) {
|
|
4233
|
+
if (source[k] === '\\') k++;
|
|
4234
|
+
k++;
|
|
4235
|
+
}
|
|
4236
|
+
if (k >= source.length) jsxThrow('jsx: unterminated attribute string');
|
|
4237
|
+
valueExpr = JSON.stringify(source.slice(j + 1, k));
|
|
4238
|
+
j = k + 1;
|
|
4239
|
+
} else if (vc === '{') {
|
|
4240
|
+
const br = jsxReadBraced(source, j, o);
|
|
4241
|
+
valueExpr = jsxTransform(br.inner, o) || 'undefined';
|
|
4242
|
+
j = br.next;
|
|
4243
|
+
} else if (vc === '<') {
|
|
4244
|
+
const el = jsxParseElement(source, j, o);
|
|
4245
|
+
valueExpr = el.code;
|
|
4246
|
+
j = el.next;
|
|
4247
|
+
} else {
|
|
4248
|
+
jsxThrow('jsx: unsupported attribute value');
|
|
4249
|
+
}
|
|
4250
|
+
}
|
|
4251
|
+
const propKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(attrName) ? attrName : JSON.stringify(attrName);
|
|
4252
|
+
props.push({ key: propKey, value: valueExpr });
|
|
4253
|
+
}
|
|
4254
|
+
}
|
|
4255
|
+
|
|
4256
|
+
const children = [];
|
|
4257
|
+
if (!selfClosing) {
|
|
4258
|
+
for (;;) {
|
|
4259
|
+
if (j >= source.length) jsxThrow('jsx: missing closing tag');
|
|
4260
|
+
const c = source[j];
|
|
4261
|
+
if (c === '<' && source[j + 1] === '/') {
|
|
4262
|
+
j = jsxSkipWs(source, j + 2);
|
|
4263
|
+
if (source[j] === '>') { j++; break; }
|
|
4264
|
+
const cm = jsxReadName(source, j);
|
|
4265
|
+
j = jsxSkipWs(source, cm.next);
|
|
4266
|
+
if (source[j] !== '>') jsxThrow('jsx: malformed closing tag');
|
|
4267
|
+
j++;
|
|
4268
|
+
break;
|
|
4269
|
+
}
|
|
4270
|
+
if (c === '<') {
|
|
4271
|
+
const el = jsxParseElement(source, j, o);
|
|
4272
|
+
children.push(el.code);
|
|
4273
|
+
j = el.next;
|
|
4274
|
+
continue;
|
|
4275
|
+
}
|
|
4276
|
+
if (c === '{') {
|
|
4277
|
+
const br = jsxReadBraced(source, j, o);
|
|
4278
|
+
const inner = jsxTransform(br.inner, o);
|
|
4279
|
+
if (inner && inner.trim()) children.push(inner);
|
|
4280
|
+
j = br.next;
|
|
4281
|
+
continue;
|
|
4282
|
+
}
|
|
4283
|
+
let k = j;
|
|
4284
|
+
while (k < source.length && source[k] !== '<' && source[k] !== '{') k++;
|
|
4285
|
+
const text = normalizeJsxText(source.slice(j, k));
|
|
4286
|
+
if (text) children.push(text);
|
|
4287
|
+
j = k;
|
|
4288
|
+
}
|
|
4289
|
+
}
|
|
4290
|
+
|
|
4291
|
+
let propsCode = 'null';
|
|
4292
|
+
if (props.length) {
|
|
4293
|
+
const parts = props.map((p) => (p.spread !== undefined ? '...(' + p.spread + ')' : p.key + ': ' + p.value));
|
|
4294
|
+
propsCode = '{ ' + parts.join(', ') + ' }';
|
|
4295
|
+
}
|
|
4296
|
+
|
|
4297
|
+
const args = [typeExpr, propsCode].concat(children);
|
|
4298
|
+
return { code: o.pragma + '(' + args.join(', ') + ')', next: j };
|
|
4299
|
+
}
|
|
4300
|
+
|
|
4301
|
+
function jsxTransform(source, opts) {
|
|
4302
|
+
const o = opts || readJsxPragma(source);
|
|
4303
|
+
let i = 0;
|
|
4304
|
+
let out = '';
|
|
4305
|
+
while (i < source.length) {
|
|
4306
|
+
const lit = jsxSkipJsLiteral(source, i);
|
|
4307
|
+
if (lit !== -1) { out += source.slice(i, lit); i = lit; continue; }
|
|
4308
|
+
const ch = source[i];
|
|
4309
|
+
if (ch === '<' && (JSX_NAME_START_RE.test(source[i + 1] || '') || source[i + 1] === '>')) {
|
|
4310
|
+
try {
|
|
4311
|
+
const el = jsxParseElement(source, i, o);
|
|
4312
|
+
out += el.code;
|
|
4313
|
+
i = el.next;
|
|
4314
|
+
continue;
|
|
4315
|
+
} catch (e) {
|
|
4316
|
+
if (!e || !e.jsxParse) throw e;
|
|
4317
|
+
}
|
|
4318
|
+
}
|
|
4319
|
+
out += ch;
|
|
4320
|
+
i++;
|
|
4321
|
+
}
|
|
4322
|
+
return out;
|
|
4323
|
+
}
|
|
4324
|
+
|
|
3975
4325
|
// =========================================================================
|
|
3976
4326
|
// 8. FAST TYPESCRIPT & ESM-TO-CJS TRANSPILER
|
|
3977
4327
|
// =========================================================================
|
|
3978
|
-
function transpileTS(source, filename = 'module.ts') {
|
|
4328
|
+
function transpileTS(source, filename = 'module.ts', opts = {}) {
|
|
4329
|
+
// `keepEsm` leaves `import`/`export` statements intact so the output can run
|
|
4330
|
+
// natively in a browser as an ES module (used by the built-in preview server).
|
|
4331
|
+
const keepEsm = Boolean(opts && opts.keepEsm);
|
|
3979
4332
|
const isTS = /\.(ts|tsx|mts|cts)$/i.test(filename)
|
|
3980
|
-
||
|
|
3981
|
-
const hasESM = /^\s*(import|export)\
|
|
3982
|
-
|
|
4333
|
+
|| /^[ \t]*(?:export\s+)?(?:abstract\s+class\s+[A-Za-z_$]|declare\s+(?:const|let|var|function|class|namespace|module|global)\b|interface\s+[A-Za-z_$][A-Za-z0-9_$]*|type\s+[A-Za-z_$][A-Za-z0-9_$]*\s*(?:<[^>]*>)?\s*=|enum\s+[A-Za-z_$][A-Za-z0-9_$]*\s*\{|namespace\s+[A-Za-z_$][A-Za-z0-9_$]*\s*\{)/m.test(source);
|
|
4334
|
+
const hasESM = /^\s*(?:import|export)\b/m.test(source)
|
|
4335
|
+
|| /\bexport\s*\{/.test(source)
|
|
4336
|
+
|| /\bimport\s*\{/.test(source)
|
|
4337
|
+
|| /\bexport\s+default\b/.test(source);
|
|
4338
|
+
const hasJsx = detectJsxSyntax(source, filename);
|
|
4339
|
+
if (!isTS && !hasESM && !hasJsx) return source;
|
|
4340
|
+
|
|
4341
|
+
// JSX/TSX must be rewritten before masking so JSX text is kept verbatim.
|
|
4342
|
+
if (hasJsx) {
|
|
4343
|
+
const { pragma, frag } = readJsxPragma(source);
|
|
4344
|
+
const needsReact = pragma === 'React.createElement' && !/\bReact\b/.test(source);
|
|
4345
|
+
const transformed = jsxTransform(source, { pragma, frag });
|
|
4346
|
+
if (transformed !== source) {
|
|
4347
|
+
source = transformed;
|
|
4348
|
+
// The classic runtime needs a React binding available to the JSX output.
|
|
4349
|
+
if (needsReact) {
|
|
4350
|
+
source = (keepEsm ? 'import React from "react";\n' : 'const React = require("react");\n') + source;
|
|
4351
|
+
}
|
|
4352
|
+
}
|
|
4353
|
+
}
|
|
3983
4354
|
|
|
3984
4355
|
// Step 1: Mask strings, template literals, regex literals, and comments so transforms never corrupt literals
|
|
3985
4356
|
const placeholders = [];
|
|
@@ -4542,10 +4913,10 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4542
4913
|
} while (code !== prevCode);
|
|
4543
4914
|
}
|
|
4544
4915
|
|
|
4545
|
-
if (hasESM || isTS) {
|
|
4916
|
+
if ((hasESM || isTS) && !keepEsm) {
|
|
4546
4917
|
// Convert ES module imports (including multi-line `{ ... }`)
|
|
4547
|
-
code = code.replace(
|
|
4548
|
-
code = code.replace(
|
|
4918
|
+
code = code.replace(/\bimport\s*\*\s*as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*from\s*([^\n;]+);?/gm, 'const $1 = require($2);');
|
|
4919
|
+
code = code.replace(/\bimport\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*,\s*\{([\s\S]*?)\}\s*from\s*([^\n;]+);?/gm, (_, defId, specifiers, mod) => {
|
|
4549
4920
|
const cleaned = specifiers
|
|
4550
4921
|
.split(',')
|
|
4551
4922
|
.map((s) => s.trim())
|
|
@@ -4556,7 +4927,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4556
4927
|
return `const ${defId} = (function(m){ return m && m.__esModule ? m.default : m; })(require(${mTrim}));` +
|
|
4557
4928
|
(cleaned ? ` const { ${cleaned} } = require(${mTrim});` : '');
|
|
4558
4929
|
});
|
|
4559
|
-
code = code.replace(
|
|
4930
|
+
code = code.replace(/\bimport\s*\{([\s\S]*?)\}\s*from\s*([^\n;]+);?/gm, (_, specifiers, mod) => {
|
|
4560
4931
|
const cleaned = specifiers
|
|
4561
4932
|
.split(',')
|
|
4562
4933
|
.map((s) => s.trim())
|
|
@@ -4565,42 +4936,48 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4565
4936
|
.join(', ');
|
|
4566
4937
|
return cleaned ? `const { ${cleaned} } = require(${mod.trim()});` : '';
|
|
4567
4938
|
});
|
|
4568
|
-
code = code.replace(
|
|
4569
|
-
|
|
4939
|
+
code = code.replace(/\bimport\s+([A-Za-z_$][A-Za-z0-9_$]*)\s+from\s+([^\n;]+);?/gm, 'const $1 = (function(m){ return m && m.__esModule ? m.default : m; })(require($2));');
|
|
4940
|
+
// Dynamic `import("pkg")` — resolve through the CJS loader instead of the
|
|
4941
|
+
// host's dynamic import, which cannot see the virtual filesystem.
|
|
4942
|
+
code = code.replace(/\bimport\s*\(\s*(\x00__LIT_\d+__\x00)\s*\)/g, 'Promise.resolve().then(() => require($1))');
|
|
4943
|
+
// Side-effect import `import"pkg"` / `import "pkg"`. By this point the
|
|
4944
|
+
// specifier is literal-masked, and minified modules emit no whitespace
|
|
4945
|
+
// between `import` and the string, so `\s*` (not `\s+`) is required.
|
|
4946
|
+
code = code.replace(/\bimport\s*(\x00__LIT_\d+__\x00)\s*;?/gm, 'require($1);');
|
|
4570
4947
|
|
|
4571
4948
|
// Convert ES module exports
|
|
4572
4949
|
const exportedNames = [];
|
|
4573
4950
|
let defaultExportName = null;
|
|
4574
4951
|
let hasEsmExport = false;
|
|
4575
4952
|
|
|
4576
|
-
code = code.replace(
|
|
4953
|
+
code = code.replace(/\bexport\s*\*\s*as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*from\s*([^\n;]+);?/gm, (_, ns, mod) => {
|
|
4577
4954
|
hasEsmExport = true;
|
|
4578
4955
|
return `exports.${ns} = require(${mod.trim()});`;
|
|
4579
4956
|
});
|
|
4580
|
-
code = code.replace(
|
|
4957
|
+
code = code.replace(/\bexport\s*\*\s*from\s*([^\n;]+);?/gm, (_, mod) => {
|
|
4581
4958
|
hasEsmExport = true;
|
|
4582
4959
|
return `Object.assign(exports, require(${mod.trim()}));`;
|
|
4583
4960
|
});
|
|
4584
|
-
code = code.replace(
|
|
4961
|
+
code = code.replace(/\bexport\s+default\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
|
|
4585
4962
|
hasEsmExport = true;
|
|
4586
4963
|
defaultExportName = name;
|
|
4587
4964
|
return `${kw} ${name}`;
|
|
4588
4965
|
});
|
|
4589
|
-
code = code.replace(
|
|
4966
|
+
code = code.replace(/\bexport\s+default\s+/gm, () => {
|
|
4590
4967
|
hasEsmExport = true;
|
|
4591
4968
|
return 'exports.default = ';
|
|
4592
4969
|
});
|
|
4593
|
-
code = code.replace(
|
|
4970
|
+
code = code.replace(/\bexport\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
|
|
4594
4971
|
hasEsmExport = true;
|
|
4595
4972
|
exportedNames.push(name);
|
|
4596
4973
|
return `${kw} ${name}`;
|
|
4597
4974
|
});
|
|
4598
|
-
code = code.replace(
|
|
4975
|
+
code = code.replace(/\bexport\s+(const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
|
|
4599
4976
|
hasEsmExport = true;
|
|
4600
4977
|
exportedNames.push(name);
|
|
4601
4978
|
return `${kw} ${name}`;
|
|
4602
4979
|
});
|
|
4603
|
-
code = code.replace(
|
|
4980
|
+
code = code.replace(/\bexport\s*\{([^}]+)\}\s*from\s*([^\n;]+);?/gm, (_, list, mod) => {
|
|
4604
4981
|
hasEsmExport = true;
|
|
4605
4982
|
const mTrim = mod.trim();
|
|
4606
4983
|
return list.split(',').map((item) => {
|
|
@@ -4612,7 +4989,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4612
4989
|
return `exports.${exported} = require(${mTrim}).${local};`;
|
|
4613
4990
|
}).join(' ');
|
|
4614
4991
|
});
|
|
4615
|
-
code = code.replace(
|
|
4992
|
+
code = code.replace(/\bexport\s*\{([^}]+)\}\s*;?/gm, (_, list) => {
|
|
4616
4993
|
hasEsmExport = true;
|
|
4617
4994
|
return list.split(',').map((item) => {
|
|
4618
4995
|
const trimmed = item.trim();
|
|
@@ -4699,8 +5076,12 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4699
5076
|
const builtinSet = new Set([
|
|
4700
5077
|
'fs', 'fs/promises', 'path', 'os', 'crypto', 'zlib', 'http', 'https', 'net',
|
|
4701
5078
|
'dns', 'dns/promises', 'string_decoder', 'child_process', 'process',
|
|
4702
|
-
'assert', 'assert/strict', 'events', 'util', 'url', 'querystring',
|
|
4703
|
-
'stream', 'stream/promises', '
|
|
5079
|
+
'assert', 'assert/strict', 'events', 'util', 'util/types', 'url', 'querystring',
|
|
5080
|
+
'stream', 'stream/promises', 'stream/web', 'stream/consumers', 'test', 'perf_hooks',
|
|
5081
|
+
'timers', 'timers/promises', 'buffer', 'tty', 'tls', 'dgram', 'readline', 'readline/promises',
|
|
5082
|
+
'async_hooks', 'worker_threads', 'v8', 'vm', 'module', 'constants', 'domain',
|
|
5083
|
+
'diagnostics_channel', 'http2', 'cluster', 'repl', 'inspector', 'trace_events',
|
|
5084
|
+
'wasi', 'console',
|
|
4704
5085
|
'bun', 'bun:test', 'bun:sqlite'
|
|
4705
5086
|
]);
|
|
4706
5087
|
|
|
@@ -6610,6 +6991,285 @@ function __initVirtualRuntime(rootScope) {
|
|
|
6610
6991
|
})();
|
|
6611
6992
|
|
|
6612
6993
|
// Built-in Node module table
|
|
6994
|
+
// ── Additional Node builtin shims ────────────────────────────────────────
|
|
6995
|
+
// Widely-used packages require these at load time (e.g. `debug` requires
|
|
6996
|
+
// `tty`). Without an entry `require('tty')` throws MODULE_NOT_FOUND and the
|
|
6997
|
+
// whole app fails to start. Implementations are deliberately minimal but
|
|
6998
|
+
// shape-compatible; anything that genuinely cannot work in the sandbox
|
|
6999
|
+
// throws an explicit, descriptive error rather than silently misbehaving.
|
|
7000
|
+
const VirtualTty = {
|
|
7001
|
+
isatty() { return false; },
|
|
7002
|
+
ReadStream: class TtyReadStream extends EventEmitter {},
|
|
7003
|
+
WriteStream: class TtyWriteStream extends EventEmitter {
|
|
7004
|
+
constructor(fd) { super(); this.fd = fd; this.columns = 80; this.rows = 24; this.isTTY = false; }
|
|
7005
|
+
getColorDepth() { return 1; }
|
|
7006
|
+
hasColors() { return false; }
|
|
7007
|
+
cursorTo() { return true; }
|
|
7008
|
+
clearLine() { return true; }
|
|
7009
|
+
moveCursor() { return true; }
|
|
7010
|
+
}
|
|
7011
|
+
};
|
|
7012
|
+
|
|
7013
|
+
function makeReadlineInterface() {
|
|
7014
|
+
const iface = new EventEmitter();
|
|
7015
|
+
iface.terminal = false;
|
|
7016
|
+
iface.line = '';
|
|
7017
|
+
iface.cursor = 0;
|
|
7018
|
+
iface.question = (query, cb) => { if (typeof cb === 'function') queueMicrotask(() => cb('')); };
|
|
7019
|
+
iface.prompt = () => {};
|
|
7020
|
+
iface.setPrompt = () => {};
|
|
7021
|
+
iface.getPrompt = () => '';
|
|
7022
|
+
iface.write = () => {};
|
|
7023
|
+
iface.pause = () => iface;
|
|
7024
|
+
iface.resume = () => iface;
|
|
7025
|
+
iface.close = () => { queueMicrotask(() => iface.emit('close')); return iface; };
|
|
7026
|
+
iface[Symbol.asyncIterator] = async function* () {};
|
|
7027
|
+
return iface;
|
|
7028
|
+
}
|
|
7029
|
+
const VirtualReadline = {
|
|
7030
|
+
createInterface: makeReadlineInterface,
|
|
7031
|
+
Interface: class ReadlineInterface extends EventEmitter {},
|
|
7032
|
+
clearLine: () => true,
|
|
7033
|
+
clearScreenDown: () => true,
|
|
7034
|
+
cursorTo: () => true,
|
|
7035
|
+
moveCursor: () => true,
|
|
7036
|
+
emitKeypressEvents: () => {}
|
|
7037
|
+
};
|
|
7038
|
+
const VirtualReadlinePromises = {
|
|
7039
|
+
createInterface: () => {
|
|
7040
|
+
const iface = makeReadlineInterface();
|
|
7041
|
+
iface.question = () => Promise.resolve('');
|
|
7042
|
+
return iface;
|
|
7043
|
+
},
|
|
7044
|
+
Interface: class ReadlinePromisesInterface extends EventEmitter {}
|
|
7045
|
+
};
|
|
7046
|
+
|
|
7047
|
+
const VirtualAsyncHooks = {
|
|
7048
|
+
AsyncLocalStorage: class AsyncLocalStorage {
|
|
7049
|
+
constructor() { this._store = undefined; }
|
|
7050
|
+
getStore() { return this._store; }
|
|
7051
|
+
run(store, fn, ...args) {
|
|
7052
|
+
const prev = this._store;
|
|
7053
|
+
this._store = store;
|
|
7054
|
+
try { return fn(...args); } finally { this._store = prev; }
|
|
7055
|
+
}
|
|
7056
|
+
exit(fn, ...args) {
|
|
7057
|
+
const prev = this._store;
|
|
7058
|
+
this._store = undefined;
|
|
7059
|
+
try { return fn(...args); } finally { this._store = prev; }
|
|
7060
|
+
}
|
|
7061
|
+
enterWith(store) { this._store = store; }
|
|
7062
|
+
disable() { this._store = undefined; }
|
|
7063
|
+
},
|
|
7064
|
+
AsyncResource: class AsyncResource {
|
|
7065
|
+
constructor(type) { this.type = type || 'AsyncResource'; }
|
|
7066
|
+
runInAsyncScope(fn, thisArg, ...args) { return fn.apply(thisArg, args); }
|
|
7067
|
+
emitDestroy() { return this; }
|
|
7068
|
+
asyncId() { return 0; }
|
|
7069
|
+
triggerAsyncId() { return 0; }
|
|
7070
|
+
static bind(fn) { return fn; }
|
|
7071
|
+
},
|
|
7072
|
+
createHook() { return { enable() {}, disable() {} }; },
|
|
7073
|
+
executionAsyncId() { return 0; },
|
|
7074
|
+
triggerAsyncId() { return 0; },
|
|
7075
|
+
executionAsyncResource() { return null; }
|
|
7076
|
+
};
|
|
7077
|
+
|
|
7078
|
+
const VirtualWorkerThreads = {
|
|
7079
|
+
isMainThread: true,
|
|
7080
|
+
threadId: 0,
|
|
7081
|
+
parentPort: null,
|
|
7082
|
+
workerData: null,
|
|
7083
|
+
resourceLimits: {},
|
|
7084
|
+
SHARE_ENV: Symbol('nodejs.worker_threads.SHARE_ENV'),
|
|
7085
|
+
receiveMessageOnPort() { return undefined; },
|
|
7086
|
+
Worker: class Worker extends EventEmitter {
|
|
7087
|
+
constructor(filename) {
|
|
7088
|
+
super();
|
|
7089
|
+
this.threadId = -1;
|
|
7090
|
+
this.filename = filename;
|
|
7091
|
+
queueMicrotask(() => this.emit('error', new Error('worker_threads.Worker is not supported in the virtual runtime.')));
|
|
7092
|
+
}
|
|
7093
|
+
postMessage() {}
|
|
7094
|
+
terminate() { return Promise.resolve(0); }
|
|
7095
|
+
ref() { return this; }
|
|
7096
|
+
unref() { return this; }
|
|
7097
|
+
},
|
|
7098
|
+
MessageChannel: class MessageChannel {},
|
|
7099
|
+
MessagePort: class MessagePort extends EventEmitter {}
|
|
7100
|
+
};
|
|
7101
|
+
|
|
7102
|
+
const VirtualV8 = {
|
|
7103
|
+
serialize(value) { return VirtualBuffer.from(JSON.stringify(value === undefined ? null : value), 'utf8'); },
|
|
7104
|
+
deserialize(buf) { try { return JSON.parse(String(buf)); } catch (_) { return undefined; } },
|
|
7105
|
+
getHeapStatistics() {
|
|
7106
|
+
return {
|
|
7107
|
+
total_heap_size: 0, total_heap_size_executable: 0, total_physical_size: 0,
|
|
7108
|
+
total_available_size: 0, used_heap_size: 0, heap_size_limit: 0,
|
|
7109
|
+
malloced_memory: 0, peak_malloced_memory: 0, does_zap_garbage: 0,
|
|
7110
|
+
number_of_native_contexts: 1, number_of_detached_contexts: 0
|
|
7111
|
+
};
|
|
7112
|
+
},
|
|
7113
|
+
getHeapSpaceStatistics() { return []; },
|
|
7114
|
+
setFlagsFromString() {},
|
|
7115
|
+
cachedDataVersionTag() { return 0; },
|
|
7116
|
+
Serializer: class Serializer {
|
|
7117
|
+
constructor() { this._value = undefined; }
|
|
7118
|
+
writeHeader() {}
|
|
7119
|
+
writeValue(value) { this._value = value; }
|
|
7120
|
+
releaseBuffer() { return VirtualBuffer.from(JSON.stringify(this._value === undefined ? null : this._value), 'utf8'); }
|
|
7121
|
+
},
|
|
7122
|
+
Deserializer: class Deserializer {
|
|
7123
|
+
constructor(buffer) { this._buffer = buffer; }
|
|
7124
|
+
readHeader() { return true; }
|
|
7125
|
+
readValue() { try { return JSON.parse(String(this._buffer)); } catch (_) { return undefined; } }
|
|
7126
|
+
}
|
|
7127
|
+
};
|
|
7128
|
+
|
|
7129
|
+
const VirtualVm = {
|
|
7130
|
+
createContext(sandbox) { return sandbox && typeof sandbox === 'object' ? sandbox : {}; },
|
|
7131
|
+
isContext() { return false; },
|
|
7132
|
+
createScript() { throw new Error('vm code compilation is not supported in the virtual runtime.'); },
|
|
7133
|
+
runInContext() { throw new Error('vm.runInContext is not supported in the virtual runtime.'); },
|
|
7134
|
+
runInNewContext() { throw new Error('vm.runInNewContext is not supported in the virtual runtime.'); },
|
|
7135
|
+
runInThisContext() { throw new Error('vm.runInThisContext is not supported in the virtual runtime.'); },
|
|
7136
|
+
compileFunction() { throw new Error('vm.compileFunction is not supported in the virtual runtime.'); },
|
|
7137
|
+
Script: class Script {
|
|
7138
|
+
runInThisContext() { throw new Error('vm.Script is not supported in the virtual runtime.'); }
|
|
7139
|
+
runInContext() { throw new Error('vm.Script is not supported in the virtual runtime.'); }
|
|
7140
|
+
runInNewContext() { throw new Error('vm.Script is not supported in the virtual runtime.'); }
|
|
7141
|
+
}
|
|
7142
|
+
};
|
|
7143
|
+
|
|
7144
|
+
const VirtualDomain = {
|
|
7145
|
+
create() {
|
|
7146
|
+
const d = new EventEmitter();
|
|
7147
|
+
d.members = [];
|
|
7148
|
+
d.add = () => d;
|
|
7149
|
+
d.remove = () => d;
|
|
7150
|
+
d.bind = (fn) => fn;
|
|
7151
|
+
d.intercept = (fn) => fn;
|
|
7152
|
+
d.run = (fn, ...args) => fn(...args);
|
|
7153
|
+
d.dispose = () => d;
|
|
7154
|
+
return d;
|
|
7155
|
+
},
|
|
7156
|
+
createDomain() { return VirtualDomain.create(); }
|
|
7157
|
+
};
|
|
7158
|
+
|
|
7159
|
+
const VirtualDiagnosticsChannel = {
|
|
7160
|
+
_channels: new Map(),
|
|
7161
|
+
channel(name) {
|
|
7162
|
+
const key = String(name);
|
|
7163
|
+
if (!VirtualDiagnosticsChannel._channels.has(key)) {
|
|
7164
|
+
const ch = new EventEmitter();
|
|
7165
|
+
ch.name = key;
|
|
7166
|
+
ch.hasSubscribers = false;
|
|
7167
|
+
ch.subscribe = () => { ch.hasSubscribers = true; };
|
|
7168
|
+
ch.unsubscribe = () => { ch.hasSubscribers = false; };
|
|
7169
|
+
VirtualDiagnosticsChannel._channels.set(key, ch);
|
|
7170
|
+
}
|
|
7171
|
+
return VirtualDiagnosticsChannel._channels.get(key);
|
|
7172
|
+
},
|
|
7173
|
+
subscribe(name, cb) {
|
|
7174
|
+
const ch = VirtualDiagnosticsChannel.channel(name);
|
|
7175
|
+
ch.subscribe();
|
|
7176
|
+
if (typeof cb === 'function') ch.on('message', cb);
|
|
7177
|
+
},
|
|
7178
|
+
unsubscribe(name) { VirtualDiagnosticsChannel.channel(name).unsubscribe(); },
|
|
7179
|
+
Channel: class Channel extends EventEmitter {}
|
|
7180
|
+
};
|
|
7181
|
+
|
|
7182
|
+
const notSupported = (what) => () => { throw new Error(what + ' is not supported in the virtual runtime.'); };
|
|
7183
|
+
const VirtualTls = {
|
|
7184
|
+
DEFAULT_MIN_VERSION: 'TLSv1.2',
|
|
7185
|
+
DEFAULT_MAX_VERSION: 'TLSv1.3',
|
|
7186
|
+
rootCertificates: [],
|
|
7187
|
+
createSecureContext() { return { context: {} }; },
|
|
7188
|
+
SecureContext: class SecureContext {},
|
|
7189
|
+
TLSSocket: class TLSSocket extends EventEmitter {},
|
|
7190
|
+
Server: class TLSServer extends EventEmitter {},
|
|
7191
|
+
checkServerIdentity() { return undefined; },
|
|
7192
|
+
connect: notSupported('tls.connect'),
|
|
7193
|
+
createServer: notSupported('tls.createServer')
|
|
7194
|
+
};
|
|
7195
|
+
const VirtualDgram = {
|
|
7196
|
+
createSocket(type) {
|
|
7197
|
+
const sock = new EventEmitter();
|
|
7198
|
+
sock.type = type || 'udp4';
|
|
7199
|
+
sock.bind = (...args) => { const cb = args[args.length - 1]; if (typeof cb === 'function') queueMicrotask(cb); return sock; };
|
|
7200
|
+
sock.close = (cb) => { queueMicrotask(() => { sock.emit('close'); if (typeof cb === 'function') cb(); }); return sock; };
|
|
7201
|
+
sock.send = notSupported('dgram.send');
|
|
7202
|
+
sock.address = () => ({ address: '0.0.0.0', family: 'IPv4', port: 0 });
|
|
7203
|
+
sock.setBroadcast = () => sock;
|
|
7204
|
+
sock.setTTL = () => sock;
|
|
7205
|
+
sock.addMembership = () => {};
|
|
7206
|
+
sock.dropMembership = () => {};
|
|
7207
|
+
return sock;
|
|
7208
|
+
},
|
|
7209
|
+
Socket: class Socket extends EventEmitter {}
|
|
7210
|
+
};
|
|
7211
|
+
const VirtualHttp2 = {
|
|
7212
|
+
constants: { NGHTTP2_REFUSED_STREAM: 7, NGHTTP2_CANCEL: 8 },
|
|
7213
|
+
connect: notSupported('http2.connect'),
|
|
7214
|
+
createServer: notSupported('http2.createServer'),
|
|
7215
|
+
createSecureServer: notSupported('http2.createSecureServer')
|
|
7216
|
+
};
|
|
7217
|
+
const VirtualCluster = {
|
|
7218
|
+
isPrimary: true,
|
|
7219
|
+
isMaster: true,
|
|
7220
|
+
isWorker: false,
|
|
7221
|
+
workers: {},
|
|
7222
|
+
fork: notSupported('cluster.fork'),
|
|
7223
|
+
setupPrimary: notSupported('cluster.setupPrimary'),
|
|
7224
|
+
setupMaster: notSupported('cluster.setupMaster'),
|
|
7225
|
+
disconnect: () => {}
|
|
7226
|
+
};
|
|
7227
|
+
const VirtualRepl = { start: notSupported('repl.start') };
|
|
7228
|
+
const VirtualInspector = {
|
|
7229
|
+
open: () => null,
|
|
7230
|
+
close: () => {},
|
|
7231
|
+
url: () => '',
|
|
7232
|
+
Session: class Session { connect() {} disconnect() {} post() {} }
|
|
7233
|
+
};
|
|
7234
|
+
const VirtualTraceEvents = {
|
|
7235
|
+
createTracing: () => ({ enable() {}, disable() {} }),
|
|
7236
|
+
getEnabledCategories: () => ''
|
|
7237
|
+
};
|
|
7238
|
+
const VirtualWasi = {
|
|
7239
|
+
WASI: class WASI { constructor() { throw new Error('wasi is not supported in the virtual runtime.'); } }
|
|
7240
|
+
};
|
|
7241
|
+
|
|
7242
|
+
const VirtualStreamWeb = {
|
|
7243
|
+
ReadableStream: typeof ReadableStream !== 'undefined' ? ReadableStream : undefined,
|
|
7244
|
+
WritableStream: typeof WritableStream !== 'undefined' ? WritableStream : undefined,
|
|
7245
|
+
TransformStream: typeof TransformStream !== 'undefined' ? TransformStream : undefined
|
|
7246
|
+
};
|
|
7247
|
+
async function consumeStream(kind, stream) {
|
|
7248
|
+
const chunks = [];
|
|
7249
|
+
if (stream && typeof stream[Symbol.asyncIterator] === 'function') {
|
|
7250
|
+
for await (const chunk of stream) chunks.push(chunk);
|
|
7251
|
+
} else if (stream && typeof stream.on === 'function') {
|
|
7252
|
+
await new Promise((resolve, reject) => {
|
|
7253
|
+
stream.on('data', (c) => chunks.push(c));
|
|
7254
|
+
stream.on('end', resolve);
|
|
7255
|
+
stream.on('error', reject);
|
|
7256
|
+
});
|
|
7257
|
+
}
|
|
7258
|
+
const bufs = chunks.map((c) => (typeof c === 'string' ? VirtualBuffer.from(c) : VirtualBuffer.from(c)));
|
|
7259
|
+
const buf = VirtualBuffer.concat(bufs);
|
|
7260
|
+
if (kind === 'buffer') return buf;
|
|
7261
|
+
if (kind === 'arrayBuffer') return buf.buffer ? buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) : new ArrayBuffer(0);
|
|
7262
|
+
if (kind === 'text') return buf.toString('utf8');
|
|
7263
|
+
if (kind === 'json') return JSON.parse(buf.toString('utf8'));
|
|
7264
|
+
return buf;
|
|
7265
|
+
}
|
|
7266
|
+
const VirtualStreamConsumers = {
|
|
7267
|
+
text: (s) => consumeStream('text', s),
|
|
7268
|
+
json: (s) => consumeStream('json', s),
|
|
7269
|
+
buffer: (s) => consumeStream('buffer', s),
|
|
7270
|
+
arrayBuffer: (s) => consumeStream('arrayBuffer', s)
|
|
7271
|
+
};
|
|
7272
|
+
|
|
6613
7273
|
const builtinModules = {
|
|
6614
7274
|
fs: vfs,
|
|
6615
7275
|
'node:fs': vfs,
|
|
@@ -6670,9 +7330,59 @@ function __initVirtualRuntime(rootScope) {
|
|
|
6670
7330
|
bun: BunObj,
|
|
6671
7331
|
'node:bun': BunObj,
|
|
6672
7332
|
'bun:test': bunTestModule,
|
|
6673
|
-
'bun:sqlite': { Database: SqliteDatabase }
|
|
7333
|
+
'bun:sqlite': { Database: SqliteDatabase },
|
|
7334
|
+
tty: VirtualTty,
|
|
7335
|
+
tls: VirtualTls,
|
|
7336
|
+
dgram: VirtualDgram,
|
|
7337
|
+
readline: VirtualReadline,
|
|
7338
|
+
'readline/promises': VirtualReadlinePromises,
|
|
7339
|
+
async_hooks: VirtualAsyncHooks,
|
|
7340
|
+
worker_threads: VirtualWorkerThreads,
|
|
7341
|
+
v8: VirtualV8,
|
|
7342
|
+
vm: VirtualVm,
|
|
7343
|
+
domain: VirtualDomain,
|
|
7344
|
+
diagnostics_channel: VirtualDiagnosticsChannel,
|
|
7345
|
+
http2: VirtualHttp2,
|
|
7346
|
+
cluster: VirtualCluster,
|
|
7347
|
+
repl: VirtualRepl,
|
|
7348
|
+
inspector: VirtualInspector,
|
|
7349
|
+
trace_events: VirtualTraceEvents,
|
|
7350
|
+
wasi: VirtualWasi,
|
|
7351
|
+
constants: vfs.constants,
|
|
7352
|
+
'util/types': VirtualUtil.types,
|
|
7353
|
+
'stream/web': VirtualStreamWeb,
|
|
7354
|
+
'stream/consumers': VirtualStreamConsumers
|
|
6674
7355
|
};
|
|
6675
7356
|
|
|
7357
|
+
// Auto-alias every builtin under the `node:` prefix (node:tty, node:readline…)
|
|
7358
|
+
// so both spellings resolve identically without hand-maintaining two lists.
|
|
7359
|
+
for (const __builtinName of Object.keys(builtinModules)) {
|
|
7360
|
+
if (!__builtinName.startsWith('node:') && !__builtinName.startsWith('bun:')) {
|
|
7361
|
+
builtinModules['node:' + __builtinName] = builtinModules[__builtinName];
|
|
7362
|
+
}
|
|
7363
|
+
}
|
|
7364
|
+
|
|
7365
|
+
// `module` must reference the now-complete builtin table, so it is attached
|
|
7366
|
+
// after the object literal above.
|
|
7367
|
+
const VirtualModule = {
|
|
7368
|
+
builtinModules: Object.keys(builtinModules),
|
|
7369
|
+
Module: class Module {},
|
|
7370
|
+
isBuiltin(name) { return Object.prototype.hasOwnProperty.call(builtinModules, String(name)); },
|
|
7371
|
+
createRequire(from) {
|
|
7372
|
+
const base = typeof from === 'string' ? from : '/workspace';
|
|
7373
|
+
const dir = /\.(?:js|mjs|cjs|ts|json)$/.test(base) ? VirtualPath.dirname(base) : base;
|
|
7374
|
+
const req = (id) => {
|
|
7375
|
+
if (Object.prototype.hasOwnProperty.call(builtinModules, id)) return builtinModules[id];
|
|
7376
|
+
return loadModule(resolveModulePath(id, dir), false);
|
|
7377
|
+
};
|
|
7378
|
+
req.resolve = (id) => resolveModulePath(id, dir);
|
|
7379
|
+
req.cache = {};
|
|
7380
|
+
return req;
|
|
7381
|
+
}
|
|
7382
|
+
};
|
|
7383
|
+
builtinModules.module = VirtualModule;
|
|
7384
|
+
builtinModules['node:module'] = VirtualModule;
|
|
7385
|
+
|
|
6676
7386
|
const moduleCache = new Map();
|
|
6677
7387
|
let mainModule = null;
|
|
6678
7388
|
|
|
@@ -7237,6 +7947,14 @@ function __initVirtualRuntime(rootScope) {
|
|
|
7237
7947
|
};
|
|
7238
7948
|
}
|
|
7239
7949
|
|
|
7950
|
+
// Transpile a source file so it can run natively in a browser as an ES module:
|
|
7951
|
+
// JSX is compiled to the classic runtime (auto-importing React when needed) and
|
|
7952
|
+
// TypeScript annotations are stripped, but `import`/`export` are preserved so an
|
|
7953
|
+
// import map can resolve bare specifiers to CDN ESM builds.
|
|
7954
|
+
function transpileForBrowser(source, filename = 'module.js') {
|
|
7955
|
+
return transpileTS(source, filename, { keepEsm: true });
|
|
7956
|
+
}
|
|
7957
|
+
|
|
7240
7958
|
return {
|
|
7241
7959
|
VirtualBuffer,
|
|
7242
7960
|
VirtualCrypto,
|
|
@@ -7256,6 +7974,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
7256
7974
|
createVirtualHttp,
|
|
7257
7975
|
createVirtualNet,
|
|
7258
7976
|
transpileTS,
|
|
7977
|
+
transpileForBrowser,
|
|
7259
7978
|
preloadNpmPackages,
|
|
7260
7979
|
createVirtualPortTable,
|
|
7261
7980
|
executeInVirtualRuntime
|
|
@@ -7530,26 +8249,58 @@ if (typeof module !== 'undefined' && module.exports) {
|
|
|
7530
8249
|
})();
|
|
7531
8250
|
|
|
7532
8251
|
const CodeLabRuntime = _root.CodeLabRuntime;
|
|
7533
|
-
const
|
|
7534
|
-
const
|
|
7535
|
-
const
|
|
7536
|
-
const
|
|
7537
|
-
const
|
|
8252
|
+
const VirtualBuffer = CodeLabRuntime.VirtualBuffer;
|
|
8253
|
+
const VirtualCrypto = CodeLabRuntime.VirtualCrypto;
|
|
8254
|
+
const VirtualZlib = CodeLabRuntime.VirtualZlib;
|
|
8255
|
+
const VirtualPath = CodeLabRuntime.VirtualPath;
|
|
8256
|
+
const VirtualOs = CodeLabRuntime.VirtualOs;
|
|
8257
|
+
const VirtualAssert = CodeLabRuntime.VirtualAssert;
|
|
8258
|
+
const EventEmitter = CodeLabRuntime.EventEmitter;
|
|
8259
|
+
const VirtualUtil = CodeLabRuntime.VirtualUtil;
|
|
8260
|
+
const VirtualUrl = CodeLabRuntime.VirtualUrl;
|
|
8261
|
+
const VirtualQuerystring = CodeLabRuntime.VirtualQuerystring;
|
|
8262
|
+
const VirtualStream = CodeLabRuntime.VirtualStream;
|
|
8263
|
+
const VirtualStreamPromises = CodeLabRuntime.VirtualStreamPromises;
|
|
8264
|
+
const VirtualStringDecoder = CodeLabRuntime.VirtualStringDecoder;
|
|
8265
|
+
const VirtualDns = CodeLabRuntime.VirtualDns;
|
|
7538
8266
|
const createVirtualFs = CodeLabRuntime.createVirtualFs;
|
|
7539
8267
|
const createVirtualHttp = CodeLabRuntime.createVirtualHttp;
|
|
7540
8268
|
const createVirtualNet = CodeLabRuntime.createVirtualNet;
|
|
8269
|
+
const transpileTS = CodeLabRuntime.transpileTS;
|
|
8270
|
+
const transpileForBrowser = CodeLabRuntime.transpileForBrowser;
|
|
8271
|
+
const preloadNpmPackages = CodeLabRuntime.preloadNpmPackages;
|
|
7541
8272
|
const createVirtualPortTable = CodeLabRuntime.createVirtualPortTable;
|
|
8273
|
+
const executeInVirtualRuntime = CodeLabRuntime.executeInVirtualRuntime;
|
|
8274
|
+
const createWorkerRuntimeSource = CodeLabRuntime.createWorkerRuntimeSource;
|
|
8275
|
+
const cancelWorkerExecution = CodeLabRuntime.cancelWorkerExecution;
|
|
8276
|
+
const executeInWorker = CodeLabRuntime.executeInWorker;
|
|
7542
8277
|
|
|
7543
8278
|
export {
|
|
7544
8279
|
CodeLabRuntime,
|
|
7545
|
-
|
|
7546
|
-
|
|
7547
|
-
|
|
7548
|
-
|
|
7549
|
-
|
|
8280
|
+
VirtualBuffer,
|
|
8281
|
+
VirtualCrypto,
|
|
8282
|
+
VirtualZlib,
|
|
8283
|
+
VirtualPath,
|
|
8284
|
+
VirtualOs,
|
|
8285
|
+
VirtualAssert,
|
|
8286
|
+
EventEmitter,
|
|
8287
|
+
VirtualUtil,
|
|
8288
|
+
VirtualUrl,
|
|
8289
|
+
VirtualQuerystring,
|
|
8290
|
+
VirtualStream,
|
|
8291
|
+
VirtualStreamPromises,
|
|
8292
|
+
VirtualStringDecoder,
|
|
8293
|
+
VirtualDns,
|
|
7550
8294
|
createVirtualFs,
|
|
7551
8295
|
createVirtualHttp,
|
|
7552
8296
|
createVirtualNet,
|
|
7553
|
-
|
|
8297
|
+
transpileTS,
|
|
8298
|
+
transpileForBrowser,
|
|
8299
|
+
preloadNpmPackages,
|
|
8300
|
+
createVirtualPortTable,
|
|
8301
|
+
executeInVirtualRuntime,
|
|
8302
|
+
createWorkerRuntimeSource,
|
|
8303
|
+
cancelWorkerExecution,
|
|
8304
|
+
executeInWorker
|
|
7554
8305
|
};
|
|
7555
8306
|
export default CodeLabRuntime;
|