@cnc_cbz/usefultools-plugin-official 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/js-runner.mjs +68 -94
  2. package/package.json +1 -1
@@ -39573,108 +39573,82 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
39573
39573
  }
39574
39574
  });
39575
39575
  }
39576
+ function serialize(v2, depth = 0) {
39577
+ var _a3, _b2;
39578
+ if (v2 === null) return { t: "null", p: "null" };
39579
+ if (v2 === void 0) return { t: "undefined", p: "undefined" };
39580
+ const tp2 = typeof v2;
39581
+ if (tp2 === "string") return { t: "string", p: `"${v2}"` };
39582
+ if (tp2 === "number" || tp2 === "boolean") return { t: tp2, p: String(v2) };
39583
+ if (tp2 === "function") return { t: "function", p: `ƒ ${v2.name || "anonymous"}()` };
39584
+ if (tp2 === "symbol") return { t: "symbol", p: v2.toString() };
39585
+ if (v2 instanceof Error) return { t: "error", p: v2.stack || v2.message || String(v2) };
39586
+ if (v2 instanceof Date) return { t: "date", p: v2.toISOString() };
39587
+ if (v2 instanceof RegExp) return { t: "regexp", p: v2.toString() };
39588
+ if (depth > 4) return { t: Array.isArray(v2) ? "array" : "object", p: Array.isArray(v2) ? "[...]" : "{...}" };
39589
+ if (Array.isArray(v2)) {
39590
+ const entries = [];
39591
+ for (let i = 0; i < v2.length && i < 100; i++) entries.push({ k: String(i), v: serialize(v2[i], depth + 1) });
39592
+ return { t: "array", p: `Array(${v2.length})`, e: entries };
39593
+ }
39594
+ if (tp2 === "object") {
39595
+ const keys2 = Object.keys(v2);
39596
+ const entries = [];
39597
+ for (let i = 0; i < keys2.length && i < 50; i++) {
39598
+ try {
39599
+ entries.push({ k: keys2[i], v: serialize(v2[keys2[i]], depth + 1) });
39600
+ } catch {
39601
+ entries.push({ k: keys2[i], v: { t: "error", p: "<getter error>" } });
39602
+ }
39603
+ }
39604
+ const name2 = ((_a3 = v2.constructor) == null ? void 0 : _a3.name) !== "Object" ? (_b2 = v2.constructor) == null ? void 0 : _b2.name : "";
39605
+ let preview = name2 ? `${name2} {…}` : "{…}";
39606
+ if (keys2.length <= 5) {
39607
+ preview = (name2 ? `${name2} {` : "{") + keys2.slice(0, 5).map((k2) => `${k2}: …`).join(", ") + "}";
39608
+ }
39609
+ return { t: "object", p: preview, e: entries };
39610
+ }
39611
+ return { t: "string", p: String(v2) };
39612
+ }
39576
39613
  async function runCode() {
39577
39614
  var _a3;
39578
39615
  if (running.value) return;
39579
39616
  running.value = true;
39580
39617
  logs.value = [];
39581
39618
  const userCode = ((_a3 = editorView.value) == null ? void 0 : _a3.state.doc.toString()) || "";
39582
- const urls = cdnUrls.value.filter((u) => u.trim());
39583
- const sandboxHtml = `
39584
- <!DOCTYPE html><html><head>
39585
- ${urls.map((u) => `<script src="${u}"><\/script>`).join("\n")}
39586
- </head><body><script>
39587
- // 递归序列化值为结构化树
39588
- function _ser(v, depth) {
39589
- if (depth === undefined) depth = 0;
39590
- if (v === null) return { t: 'null', p: 'null' };
39591
- if (v === undefined) return { t: 'undefined', p: 'undefined' };
39592
- var tp = typeof v;
39593
- if (tp === 'string') return { t: 'string', p: '"' + v + '"' };
39594
- if (tp === 'number' || tp === 'boolean') return { t: tp, p: String(v) };
39595
- if (tp === 'function') return { t: 'function', p: 'ƒ ' + (v.name || 'anonymous') + '()' };
39596
- if (tp === 'symbol') return { t: 'symbol', p: v.toString() };
39597
- if (v instanceof Error) return { t: 'error', p: v.stack || v.message || String(v) };
39598
- if (v instanceof Date) return { t: 'date', p: v.toISOString() };
39599
- if (v instanceof RegExp) return { t: 'regexp', p: v.toString() };
39600
- if (depth > 4) return { t: typeof v === 'object' ? (Array.isArray(v) ? 'array' : 'object') : tp, p: Array.isArray(v) ? '[...]' : '{...}' };
39601
- if (Array.isArray(v)) {
39602
- var entries = [];
39603
- for (var i = 0; i < v.length && i < 100; i++) entries.push({ k: String(i), v: _ser(v[i], depth + 1) });
39604
- return { t: 'array', p: 'Array(' + v.length + ')', e: entries };
39605
- }
39606
- if (tp === 'object') {
39607
- var keys = Object.keys(v), entries = [];
39608
- for (var i = 0; i < keys.length && i < 50; i++) {
39609
- try { entries.push({ k: keys[i], v: _ser(v[keys[i]], depth + 1) }); } catch(e) { entries.push({ k: keys[i], v: { t: 'error', p: '<getter error>' } }); }
39610
- }
39611
- var name = v.constructor && v.constructor.name !== 'Object' ? v.constructor.name : '';
39612
- var preview = name ? name + ' {…}' : '{…}';
39613
- if (keys.length <= 5) {
39614
- preview = name ? name + ' {' : '{';
39615
- preview += keys.slice(0, 5).map(function(k) { return k + ': …' }).join(', ');
39616
- preview += '}';
39617
- }
39618
- return { t: 'object', p: preview, e: entries };
39619
- }
39620
- return { t: 'string', p: String(v) };
39621
- }
39622
-
39623
- const _post = (type, args) => {
39624
- window.parent.postMessage({ __jsRunner: true, type, args: args.map(a => _ser(a)) }, '*');
39625
- };
39626
- console.log = (...a) => _post('log', a);
39627
- console.warn = (...a) => _post('warn', a);
39628
- console.error = (...a) => _post('error', a);
39629
- console.info = (...a) => _post('info', a);
39630
-
39631
- window.onerror = (msg, src, line, col, err) => {
39632
- _post('error', [err || msg]);
39633
- };
39634
- window.onunhandledrejection = (e) => {
39635
- _post('error', [e.reason || 'Unhandled Promise Rejection']);
39636
- };
39637
-
39638
- try {
39639
- ${userCode}
39640
- } catch(e) {
39641
- _post('error', [e]);
39642
- }
39643
-
39644
- window.parent.postMessage({ __jsRunner: true, type: '__done' }, '*');
39645
- <\/script></body></html>`;
39646
- const handler = (e) => {
39647
- var _a4;
39648
- if ((_a4 = e.data) == null ? void 0 : _a4.__jsRunner) {
39649
- if (e.data.type === "__done") {
39650
- running.value = false;
39651
- window.removeEventListener("message", handler);
39652
- iframe == null ? void 0 : iframe.remove();
39653
- return;
39654
- }
39655
- logs.value.push({
39656
- type: e.data.type,
39657
- args: e.data.args || [],
39658
- timestamp: now()
39659
- });
39660
- scrollToBottom();
39661
- }
39619
+ const pushLog = (type, ...args) => {
39620
+ logs.value.push({ type, args: args.map((a3) => serialize(a3)), timestamp: now() });
39621
+ scrollToBottom();
39662
39622
  };
39663
- window.addEventListener("message", handler);
39664
- const iframe = document.createElement("iframe");
39665
- iframe.style.display = "none";
39666
- iframe.setAttribute("sandbox", "allow-scripts");
39667
- document.body.appendChild(iframe);
39668
- iframe.srcdoc = sandboxHtml;
39669
- setTimeout(() => {
39670
- if (running.value) {
39671
- running.value = false;
39672
- window.removeEventListener("message", handler);
39673
- iframe == null ? void 0 : iframe.remove();
39674
- logs.value.push({ type: "error", args: [{ t: "error", p: "⏱ 执行超时(10秒)" }], timestamp: now() });
39675
- scrollToBottom();
39623
+ const sandboxConsole = {
39624
+ log: (...a3) => pushLog("log", ...a3),
39625
+ warn: (...a3) => pushLog("warn", ...a3),
39626
+ error: (...a3) => pushLog("error", ...a3),
39627
+ info: (...a3) => pushLog("info", ...a3)
39628
+ };
39629
+ let timer = null;
39630
+ let timedOut = false;
39631
+ try {
39632
+ const timeoutPromise = new Promise((_2, reject) => {
39633
+ timer = setTimeout(() => {
39634
+ timedOut = true;
39635
+ reject(new Error("⏱ 执行超时(10秒)"));
39636
+ }, 1e4);
39637
+ });
39638
+ const AsyncFunction = Object.getPrototypeOf(async function() {
39639
+ }).constructor;
39640
+ const fn2 = new AsyncFunction("console", userCode);
39641
+ await Promise.race([fn2(sandboxConsole), timeoutPromise]);
39642
+ } catch (e) {
39643
+ if (timedOut) {
39644
+ pushLog("error", "⏱ 执行超时(10秒)");
39645
+ } else {
39646
+ pushLog("error", e);
39676
39647
  }
39677
- }, 1e4);
39648
+ } finally {
39649
+ if (timer) clearTimeout(timer);
39650
+ running.value = false;
39651
+ }
39678
39652
  }
39679
39653
  const logStyles = {
39680
39654
  log: "text-gray-200",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cnc_cbz/usefultools-plugin-official",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "description": "UsefulTools 官方工具插件包",
6
6
  "keywords": [