@innovatorved/code-lab 0.1.7 → 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/dist/runtime.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * ============================================================
3
- * @innovatorved/code-lab v0.1.7 — Standalone Browser Code IDE & Virtual Runtime
3
+ * @innovatorved/code-lab v0.1.8 — Standalone Browser Code IDE & Virtual Runtime
4
4
  * ============================================================
5
5
  * Features:
6
6
  * - Sandboxed Web Worker Execution Engine with Multi-Instance Isolation & Watchdog Timer
@@ -16,6 +16,107 @@
16
16
  */
17
17
  (function (root) {
18
18
  'use strict';
19
+ var CodeLabLogger = (() => {
20
+ var __defProp = Object.defineProperty;
21
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
22
+ var __getOwnPropNames = Object.getOwnPropertyNames;
23
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
24
+ var __export = (target, all) => {
25
+ for (var name in all)
26
+ __defProp(target, name, { get: all[name], enumerable: true });
27
+ };
28
+ var __copyProps = (to, from, except, desc) => {
29
+ if (from && typeof from === "object" || typeof from === "function") {
30
+ for (let key of __getOwnPropNames(from))
31
+ if (!__hasOwnProp.call(to, key) && key !== except)
32
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
33
+ }
34
+ return to;
35
+ };
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+
38
+ // src/logger.ts
39
+ var logger_exports = {};
40
+ __export(logger_exports, {
41
+ CodeLabError: () => CodeLabError,
42
+ DEFAULT_LOGGER: () => DEFAULT_LOGGER,
43
+ LOG_PREFIX: () => LOG_PREFIX,
44
+ createLogger: () => createLogger,
45
+ default: () => logger_default,
46
+ isCodeLabError: () => isCodeLabError
47
+ });
48
+ var LOG_PREFIX = "[code-lab]";
49
+ var CodeLabError = class extends Error {
50
+ code;
51
+ options;
52
+ cause;
53
+ recoverable;
54
+ details;
55
+ context;
56
+ constructor(code, message, options = {}) {
57
+ super(message);
58
+ this.name = "CodeLabError";
59
+ this.code = code;
60
+ this.options = options;
61
+ this.cause = options.cause;
62
+ this.recoverable = options.recoverable !== false;
63
+ const meta = options.details ?? options.context;
64
+ if (meta !== void 0) {
65
+ this.details = meta;
66
+ this.context = meta;
67
+ }
68
+ Object.setPrototypeOf(this, new.target.prototype);
69
+ }
70
+ };
71
+ function isCodeLabError(value) {
72
+ return Boolean(
73
+ value && typeof value === "object" && (value instanceof CodeLabError || value.name === "CodeLabError" && typeof value.code === "string")
74
+ );
75
+ }
76
+ function createLogger(options = {}) {
77
+ const sink = options.logger ?? null;
78
+ const debugEnabled = options.debug === true;
79
+ const prefix = options.prefix || LOG_PREFIX;
80
+ const write = (level, args) => {
81
+ if (level === "debug" && !debugEnabled) return;
82
+ if (sink && typeof sink[level] === "function") {
83
+ let sinkError = null;
84
+ try {
85
+ sink[level](...args);
86
+ return;
87
+ } catch (err) {
88
+ sinkError = err;
89
+ }
90
+ }
91
+ const consoleObj = typeof console !== "undefined" ? console : null;
92
+ const fn = consoleObj ? consoleObj[level] : null;
93
+ if (typeof fn === "function") {
94
+ fn.call(consoleObj, prefix, ...args);
95
+ }
96
+ };
97
+ return {
98
+ debug: (...args) => write("debug", args),
99
+ info: (...args) => write("info", args),
100
+ warn: (...args) => write("warn", args),
101
+ error: (...args) => write("error", args)
102
+ };
103
+ }
104
+ var DEFAULT_LOGGER = createLogger();
105
+ var _loggerNamespace = {
106
+ LOG_PREFIX,
107
+ CodeLabError,
108
+ isCodeLabError,
109
+ createLogger,
110
+ DEFAULT_LOGGER
111
+ };
112
+ var _globalRoot = typeof globalThis !== "undefined" ? globalThis : null;
113
+ if (_globalRoot && !_globalRoot.CodeLabLogger) {
114
+ _globalRoot.CodeLabLogger = _loggerNamespace;
115
+ }
116
+ var logger_default = _loggerNamespace;
117
+ return __toCommonJS(logger_exports);
118
+ })();
119
+
19
120
  (function () {
20
121
  // @ts-nocheck
21
122
  'use strict';
@@ -39,6 +140,14 @@ if (typeof globalThis !== 'undefined' && typeof globalThis.Bun === 'undefined')
39
140
  */
40
141
 
41
142
  function __initVirtualRuntime(rootScope) {
143
+ const _runtimeLogger = (rootScope && rootScope.CodeLabLogger && typeof rootScope.CodeLabLogger.createLogger === 'function')
144
+ ? rootScope.CodeLabLogger.createLogger()
145
+ : {
146
+ debug() {},
147
+ info() {},
148
+ warn(msg, meta) { if (typeof console !== 'undefined' && console.warn) console.warn('[CodeLab]', msg, meta || ''); },
149
+ error(msg, meta) { if (typeof console !== 'undefined' && console.error) console.error('[CodeLab]', msg, meta || ''); }
150
+ };
42
151
  // =========================================================================
43
152
  // 1. UTF-8 / HEX / BASE64 / BASE64URL CODECS & VIRTUAL BUFFER
44
153
  // =========================================================================
@@ -1598,7 +1707,7 @@ function __initVirtualRuntime(rootScope) {
1598
1707
  },
1599
1708
  final(cb) {
1600
1709
  if (fd !== null) {
1601
- try { fsShim.closeSync(fd); } catch (_) {}
1710
+ try { fsShim.closeSync(fd); } catch (err) { _runtimeLogger.debug('vfs writeStream closeSync failed', err); }
1602
1711
  fd = null;
1603
1712
  }
1604
1713
  cb(null);
@@ -2118,7 +2227,9 @@ function __initVirtualRuntime(rootScope) {
2118
2227
  return pathname + search + hash;
2119
2228
  }
2120
2229
  return resolvedUrl.href;
2121
- } catch (_) {}
2230
+ } catch (err) {
2231
+ _runtimeLogger.debug('url.resolve fallback used', err);
2232
+ }
2122
2233
  }
2123
2234
  if (toStr.startsWith('/')) {
2124
2235
  const m = /^([a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^/?#]+)/.exec(fromStr);
@@ -2557,10 +2668,10 @@ function __initVirtualRuntime(rootScope) {
2557
2668
  for (const [port, entry] of Array.from(table.entries())) {
2558
2669
  if (entry.pid !== undefined && String(entry.pid) === pStr) {
2559
2670
  if (entry.server && typeof entry.server.close === 'function') {
2560
- try { entry.server.close(); } catch (_) {}
2671
+ try { entry.server.close(); } catch (err) { _runtimeLogger.debug('portTable server.close failed', err); }
2561
2672
  }
2562
2673
  if (typeof entry.stop === 'function') {
2563
- try { entry.stop(); } catch (_) {}
2674
+ try { entry.stop(); } catch (err) { _runtimeLogger.debug('portTable entry.stop failed', err); }
2564
2675
  }
2565
2676
  // Avoid a double 'close' emission when entry.server.close() already
2566
2677
  // unregistered the port through unregisterServer().
@@ -3197,7 +3308,7 @@ function __initVirtualRuntime(rootScope) {
3197
3308
  },
3198
3309
  closeAllServers() {
3199
3310
  for (const s of Array.from(activeServers.values())) {
3200
- try { s.close(); } catch (_) {}
3311
+ try { s.close(); } catch (err) { _runtimeLogger.debug('vhttp closeAllServers close failed', err); }
3201
3312
  }
3202
3313
  activeServers.clear();
3203
3314
  }
@@ -4023,7 +4134,7 @@ function __initVirtualRuntime(rootScope) {
4023
4134
 
4024
4135
  function closeAllServers() {
4025
4136
  for (const srv of servers.values()) {
4026
- try { srv.close(); } catch (_) {}
4137
+ try { srv.close(); } catch (err) { _runtimeLogger.debug('vnet closeAllServers close failed', err); }
4027
4138
  }
4028
4139
  servers.clear();
4029
4140
  }
@@ -5103,7 +5214,9 @@ function __initVirtualRuntime(rootScope) {
5103
5214
  try {
5104
5215
  const pkgJson = JSON.parse(content);
5105
5216
  for (const dep of Object.keys(pkgJson.dependencies || {})) addSpec(dep);
5106
- } catch (_) {}
5217
+ } catch (err) {
5218
+ _runtimeLogger.debug('preloadNpmPackages invalid package.json', err);
5219
+ }
5107
5220
  continue;
5108
5221
  }
5109
5222
  const reqRe = /\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
@@ -5132,7 +5245,9 @@ function __initVirtualRuntime(rootScope) {
5132
5245
  const meta = await res.json();
5133
5246
  deps = Object.keys((meta && meta.dependencies) || {});
5134
5247
  }
5135
- } catch (_) {}
5248
+ } catch (err) {
5249
+ _runtimeLogger.debug('preloadNpmPackages metadata fetch failed', { pkg, err });
5250
+ }
5136
5251
  _npmMetadataCache.set(pkg, deps);
5137
5252
  }));
5138
5253
  for (const pkg of pending) {
@@ -5150,7 +5265,7 @@ function __initVirtualRuntime(rootScope) {
5150
5265
  continue;
5151
5266
  }
5152
5267
  if (typeof onProgress === 'function') {
5153
- try { onProgress(pkg); } catch (_) {}
5268
+ try { onProgress(pkg); } catch (err) { _runtimeLogger.debug('preloadNpmPackages onProgress callback failed', err); }
5154
5269
  }
5155
5270
  if (typeof fetch === 'function') {
5156
5271
  try {
@@ -5160,7 +5275,9 @@ function __initVirtualRuntime(rootScope) {
5160
5275
  if (typeof existingCache.set === 'function') existingCache.set(pkg, text);
5161
5276
  loaded[pkg] = text;
5162
5277
  }
5163
- } catch (_) {}
5278
+ } catch (err) {
5279
+ _runtimeLogger.debug('preloadNpmPackages package fetch failed', { pkg, err });
5280
+ }
5164
5281
  }
5165
5282
  }
5166
5283
  return loaded;
@@ -5352,7 +5469,9 @@ function __initVirtualRuntime(rootScope) {
5352
5469
  const dir = VirtualPath.dirname(abs);
5353
5470
  if (!vfs.existsSync(dir)) vfs.mkdirSync(dir, { recursive: true });
5354
5471
  vfs.writeFileSync(abs, content);
5355
- } catch (_) {}
5472
+ } catch (err) {
5473
+ _runtimeLogger.warn('vfs initial file seed failed', { abs, err });
5474
+ }
5356
5475
  }
5357
5476
  }
5358
5477
  const vhttp = createVirtualHttp(resourceTracker, activePortTable);
@@ -5797,7 +5916,7 @@ function __initVirtualRuntime(rootScope) {
5797
5916
  exitCode = Number(code) || 0;
5798
5917
  if (!hasExited) {
5799
5918
  hasExited = true;
5800
- try { sandboxProcess.emit('exit', exitCode); } catch (_) {}
5919
+ try { sandboxProcess.emit('exit', exitCode); } catch (err) { _runtimeLogger.debug('process exit listener threw', err); }
5801
5920
  }
5802
5921
  if (syncExecDepth > 0) {
5803
5922
  throw new ProcessExitSignal(exitCode);
@@ -5830,8 +5949,8 @@ function __initVirtualRuntime(rootScope) {
5830
5949
  resourceTracker.inFlightHttp.clear();
5831
5950
 
5832
5951
  if (!isBackground) {
5833
- try { vhttp.closeAllServers(); } catch (_) {}
5834
- try { vnet.closeAllServers(); } catch (_) {}
5952
+ try { vhttp.closeAllServers(); } catch (err) { _runtimeLogger.debug('SIGINT vhttp closeAllServers failed', err); }
5953
+ try { vnet.closeAllServers(); } catch (err) { _runtimeLogger.debug('SIGINT vnet closeAllServers failed', err); }
5835
5954
  }
5836
5955
  };
5837
5956
 
@@ -5941,10 +6060,10 @@ function __initVirtualRuntime(rootScope) {
5941
6060
  sandboxConsole.log(`✗ ${fullName}\n ${e && e.stack ? e.stack : e}`);
5942
6061
  } finally {
5943
6062
  for (const ae of hooksSnapshot.afterEachList) {
5944
- try { await ae(); } catch (_) {}
6063
+ try { await ae(); } catch (err) { _runtimeLogger.debug('node:test afterEach hook failed', err); }
5945
6064
  }
5946
6065
  for (const af of hooksSnapshot.afterToRun) {
5947
- try { await af(); } catch (_) {}
6066
+ try { await af(); } catch (err) { _runtimeLogger.debug('node:test after hook failed', err); }
5948
6067
  }
5949
6068
  hooksSnapshot.afterToRun.length = 0;
5950
6069
  }
@@ -5992,7 +6111,7 @@ function __initVirtualRuntime(rootScope) {
5992
6111
  const afters = [...suiteCtx.afterCallbacks];
5993
6112
  const cleanupTask = nodeTestChain.then(async () => {
5994
6113
  for (const af of afters) {
5995
- try { await af(); } catch (_) {}
6114
+ try { await af(); } catch (err) { _runtimeLogger.debug('node:test suite after hook failed', err); }
5996
6115
  }
5997
6116
  });
5998
6117
  nodeTestChain = cleanupTask.catch(() => {});
@@ -6038,7 +6157,7 @@ function __initVirtualRuntime(rootScope) {
6038
6157
  getEntriesByName: (n, t) => (entry.name === n && (!t || entry.entryType === t) ? [entry] : []),
6039
6158
  getEntriesByType: (t) => (entry.entryType === t ? [entry] : [])
6040
6159
  };
6041
- try { obs._cb(list, obs); } catch (_) {}
6160
+ try { obs._cb(list, obs); } catch (err) { _runtimeLogger.debug('PerformanceObserver callback failed', err); }
6042
6161
  }
6043
6162
  }
6044
6163
  }
@@ -6143,7 +6262,7 @@ function __initVirtualRuntime(rootScope) {
6143
6262
  getEntriesByName: (n, t) => buffered.filter((e) => e.name === n && (!t || e.entryType === t)),
6144
6263
  getEntriesByType: (t) => buffered.filter((e) => e.entryType === t)
6145
6264
  };
6146
- queueMicrotask(() => { try { this._cb(list, this); } catch (_) {} });
6265
+ queueMicrotask(() => { try { this._cb(list, this); } catch (err) { _runtimeLogger.debug('PerformanceObserver buffered callback failed', err); } });
6147
6266
  }
6148
6267
  }
6149
6268
  }
@@ -7414,7 +7533,9 @@ function __initVirtualRuntime(rootScope) {
7414
7533
  const resolvedEntry = resolveFileOrDirCandidates(VirtualPath.resolve(base, entryRel));
7415
7534
  if (resolvedEntry) return resolvedEntry;
7416
7535
  }
7417
- } catch (_) {}
7536
+ } catch (err) {
7537
+ _runtimeLogger.debug('resolveFileOrDirCandidates package.json parse failed', err);
7538
+ }
7418
7539
  }
7419
7540
  const indexCandidates = ['index.js', 'index.ts', 'index.mjs', 'index.cjs', 'index.json'];
7420
7541
  for (const idx of indexCandidates) {
@@ -7484,7 +7605,9 @@ function __initVirtualRuntime(rootScope) {
7484
7605
  if (found) return found;
7485
7606
  }
7486
7607
  }
7487
- } catch (_) {}
7608
+ } catch (err) {
7609
+ _runtimeLogger.debug('resolveModulePath subpath package.json parse failed', err);
7610
+ }
7488
7611
  }
7489
7612
  const foundSub = resolveFileOrDirCandidates(VirtualPath.join(pkgRoot, subpath));
7490
7613
  if (foundSub) return foundSub;
@@ -7526,7 +7649,9 @@ function __initVirtualRuntime(rootScope) {
7526
7649
  if (isLocal && activePortTable) {
7527
7650
  return await activePortTable.dispatchRequest(urlStr, finalInit);
7528
7651
  }
7529
- } catch (_) {}
7652
+ } catch (err) {
7653
+ _runtimeLogger.debug('virtualFetch local URL dispatch skipped', err);
7654
+ }
7530
7655
 
7531
7656
  if (typeof globalThis.fetch === 'function') {
7532
7657
  return await globalThis.fetch(urlOrReq, init);
@@ -7693,7 +7818,9 @@ function __initVirtualRuntime(rootScope) {
7693
7818
  if (err instanceof SyntaxError && (/\bawait\b/.test(body) || /await/i.test(err.message))) {
7694
7819
  try {
7695
7820
  return new AsyncFunction(...argNames, body);
7696
- } catch (_) {}
7821
+ } catch (asyncErr) {
7822
+ _runtimeLogger.debug('AsyncFunction compilation fallback failed', asyncErr);
7823
+ }
7697
7824
  }
7698
7825
  if (
7699
7826
  err &&
@@ -7747,7 +7874,9 @@ function __initVirtualRuntime(rootScope) {
7747
7874
  if (signal && onAbort) {
7748
7875
  try {
7749
7876
  signal.removeEventListener('abort', onAbort);
7750
- } catch (_) {}
7877
+ } catch (err) {
7878
+ _runtimeLogger.debug('raceWithAbort removeEventListener failed', err);
7879
+ }
7751
7880
  }
7752
7881
  };
7753
7882
 
@@ -7906,11 +8035,11 @@ function __initVirtualRuntime(rootScope) {
7906
8035
  }
7907
8036
  } finally {
7908
8037
  if (signal && signalListener) {
7909
- try { signal.removeEventListener('abort', signalListener); } catch (_) {}
8038
+ try { signal.removeEventListener('abort', signalListener); } catch (err) { _runtimeLogger.debug('executeInVirtualRuntime removeEventListener failed', err); }
7910
8039
  }
7911
8040
  if (!hasExited) {
7912
8041
  hasExited = true;
7913
- try { sandboxProcess.emit('exit', exitCode); } catch (_) {}
8042
+ try { sandboxProcess.emit('exit', exitCode); } catch (err) { _runtimeLogger.debug('executeInVirtualRuntime exit listener failed', err); }
7914
8043
  }
7915
8044
  for (const rawId of resourceTracker.allTimers) {
7916
8045
  clearTimeout(rawId);
@@ -8018,7 +8147,16 @@ function createWorkerRuntimeSource() {
8018
8147
  return `'use strict';\nself.__virtualRuntime = (${__initVirtualRuntime.toString()})(self);\n(${__workerMessageLoop.toString()})(self);\n`;
8019
8148
  }
8020
8149
 
8021
- const _hostRuntime = __initVirtualRuntime(typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : global));
8150
+ const _hostScope = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : global);
8151
+ const _hostLogger = (_hostScope && _hostScope.CodeLabLogger && typeof _hostScope.CodeLabLogger.createLogger === 'function')
8152
+ ? _hostScope.CodeLabLogger.createLogger()
8153
+ : {
8154
+ debug() {},
8155
+ info() {},
8156
+ warn(msg, meta) { if (typeof console !== 'undefined' && console.warn) console.warn('[CodeLab]', msg, meta || ''); },
8157
+ error(msg, meta) { if (typeof console !== 'undefined' && console.error) console.error('[CodeLab]', msg, meta || ''); }
8158
+ };
8159
+ const _hostRuntime = __initVirtualRuntime(_hostScope);
8022
8160
  _hostRuntime.createWorkerRuntimeSource = createWorkerRuntimeSource;
8023
8161
 
8024
8162
  let _cachedWorkerUrl = null;
@@ -8042,7 +8180,7 @@ function _ensureWarmWorker() {
8042
8180
 
8043
8181
  function _destroyWarmWorker() {
8044
8182
  if (_warmWorker) {
8045
- try { _warmWorker.terminate(); } catch (_) {}
8183
+ try { _warmWorker.terminate(); } catch (err) { _hostLogger.debug('warmWorker.terminate failed', err); }
8046
8184
  _warmWorker = null;
8047
8185
  }
8048
8186
  _warmWorkerBusy = false;
@@ -8097,7 +8235,7 @@ _hostRuntime.executeInWorker = async function executeInWorker(options = {}) {
8097
8235
 
8098
8236
  function releaseWorker(terminateWorker) {
8099
8237
  if (options.signal && abortListener) {
8100
- try { options.signal.removeEventListener('abort', abortListener); } catch (_) {}
8238
+ try { options.signal.removeEventListener('abort', abortListener); } catch (err) { _hostLogger.debug('executeInWorker removeEventListener failed', err); }
8101
8239
  abortListener = null;
8102
8240
  }
8103
8241
  _activeWorkerJobs.delete(requestId);
@@ -8112,7 +8250,7 @@ _hostRuntime.executeInWorker = async function executeInWorker(options = {}) {
8112
8250
  }
8113
8251
  }
8114
8252
  } else {
8115
- try { worker.terminate(); } catch (_) {}
8253
+ try { worker.terminate(); } catch (err) { _hostLogger.debug('worker.terminate failed', err); }
8116
8254
  }
8117
8255
  }
8118
8256