@ricsam/isolate-daemon 0.1.10 → 0.1.12

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.
@@ -46,12 +46,14 @@ __export(exports_connection, {
46
46
  });
47
47
  module.exports = __toCommonJS(exports_connection);
48
48
  var import_node_crypto = require("node:crypto");
49
+ var import_node_path = __toESM(require("node:path"));
49
50
  var import_isolated_vm = __toESM(require("isolated-vm"));
50
51
  var import_isolate_protocol = require("@ricsam/isolate-protocol");
51
52
  var import_callback_fs_handler = require("./callback-fs-handler.cjs");
52
53
  var import_isolate_test_environment = require("@ricsam/isolate-test-environment");
53
54
  var import_isolate_playwright = require("@ricsam/isolate-playwright");
54
55
  var import_isolate_runtime = require("@ricsam/isolate-runtime");
56
+ var import_isolate_transform = require("@ricsam/isolate-transform");
55
57
  function handleConnection(socket, state) {
56
58
  const connection = {
57
59
  socket,
@@ -434,7 +436,7 @@ async function handleCreateRuntime(message, connection, state) {
434
436
  }
435
437
  },
436
438
  fs: {
437
- getDirectory: async (path) => {
439
+ getDirectory: async (path2) => {
438
440
  const conn = callbackContext.connection;
439
441
  if (!conn) {
440
442
  throw new Error("FS callbacks not available");
@@ -443,7 +445,7 @@ async function handleCreateRuntime(message, connection, state) {
443
445
  connection: conn,
444
446
  callbackContext,
445
447
  invokeClientCallback,
446
- basePath: path
448
+ basePath: path2
447
449
  });
448
450
  }
449
451
  }
@@ -467,6 +469,7 @@ async function handleCreateRuntime(message, connection, state) {
467
469
  if (moduleLoaderCallback) {
468
470
  instance.moduleLoaderCallbackId = moduleLoaderCallback.callbackId;
469
471
  instance.moduleCache = new Map;
472
+ instance.moduleToFilename = new Map;
470
473
  }
471
474
  if (customCallbacks) {
472
475
  await setupCustomFunctions(runtime.context, customCallbacks, connection, instance);
@@ -611,10 +614,19 @@ async function handleEval(message, connection, state) {
611
614
  }
612
615
  instance.lastActivity = Date.now();
613
616
  try {
614
- const mod = await instance.runtime.isolate.compileModule(message.code, {
615
- filename: message.filename ?? "<eval>"
617
+ const filename = import_isolate_protocol.normalizeEntryFilename(message.filename);
618
+ const transformed = await import_isolate_transform.transformEntryCode(message.code, filename);
619
+ if (transformed.sourceMap) {
620
+ if (!instance.sourceMaps) {
621
+ instance.sourceMaps = new Map;
622
+ }
623
+ instance.sourceMaps.set(filename, transformed.sourceMap);
624
+ }
625
+ const mod = await instance.runtime.isolate.compileModule(transformed.code, {
626
+ filename
616
627
  });
617
628
  if (instance.moduleLoaderCallbackId) {
629
+ instance.moduleToFilename?.set(mod, filename);
618
630
  const resolver = createModuleResolver(instance, connection);
619
631
  await mod.instantiate(instance.runtime.context, resolver);
620
632
  } else {
@@ -622,13 +634,25 @@ async function handleEval(message, connection, state) {
622
634
  throw new Error(`No module loader registered. Cannot import: ${specifier}`);
623
635
  });
624
636
  }
625
- const timeout = message.maxExecutionMs;
626
- await mod.evaluate(timeout ? { timeout } : undefined);
637
+ await mod.evaluate();
638
+ const ns = mod.namespace;
639
+ const runRef = await ns.get("default", { reference: true });
640
+ try {
641
+ await runRef.apply(undefined, [], {
642
+ result: { promise: true },
643
+ ...message.maxExecutionMs ? { timeout: message.maxExecutionMs } : {}
644
+ });
645
+ } finally {
646
+ runRef.release();
647
+ }
627
648
  await Promise.all(instance.pendingCallbacks);
628
649
  instance.pendingCallbacks.length = 0;
629
650
  sendOk(connection.socket, message.requestId, { value: undefined });
630
651
  } catch (err) {
631
652
  const error = err;
653
+ if (error.stack && instance.sourceMaps?.size) {
654
+ error.stack = import_isolate_transform.mapErrorStack(error.stack, instance.sourceMaps);
655
+ }
632
656
  const isTimeoutError = error.message?.includes("Script execution timed out");
633
657
  sendError(connection.socket, message.requestId, isTimeoutError ? import_isolate_protocol.ErrorCode.ISOLATE_TIMEOUT : import_isolate_protocol.ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
634
658
  }
@@ -1382,20 +1406,38 @@ async function setupCustomFunctions(context, customCallbacks, connection, instan
1382
1406
  }
1383
1407
  }
1384
1408
  function createModuleResolver(instance, connection) {
1385
- return async (specifier, _referrer) => {
1409
+ return async (specifier, referrer) => {
1386
1410
  const cached = instance.moduleCache?.get(specifier);
1387
1411
  if (cached)
1388
1412
  return cached;
1389
1413
  if (!instance.moduleLoaderCallbackId) {
1390
1414
  throw new Error(`Module not found: ${specifier}`);
1391
1415
  }
1392
- const code = await invokeClientCallback(connection, instance.moduleLoaderCallbackId, [specifier]);
1393
- const mod = await instance.runtime.isolate.compileModule(code, {
1416
+ const importerPath = instance.moduleToFilename?.get(referrer) ?? "<unknown>";
1417
+ const importerResolveDir = import_node_path.default.posix.dirname(importerPath);
1418
+ const result = await invokeClientCallback(connection, instance.moduleLoaderCallbackId, [specifier, { path: importerPath, resolveDir: importerResolveDir }]);
1419
+ const { code, resolveDir } = result;
1420
+ const hash = import_isolate_transform.contentHash(code);
1421
+ const cacheKey = `${specifier}:${hash}`;
1422
+ const hashCached = instance.moduleCache?.get(cacheKey);
1423
+ if (hashCached)
1424
+ return hashCached;
1425
+ const transformed = await import_isolate_transform.transformModuleCode(code, specifier);
1426
+ if (transformed.sourceMap) {
1427
+ if (!instance.sourceMaps) {
1428
+ instance.sourceMaps = new Map;
1429
+ }
1430
+ instance.sourceMaps.set(specifier, transformed.sourceMap);
1431
+ }
1432
+ const mod = await instance.runtime.isolate.compileModule(transformed.code, {
1394
1433
  filename: specifier
1395
1434
  });
1435
+ const resolvedPath = import_node_path.default.posix.join(resolveDir, import_node_path.default.posix.basename(specifier));
1436
+ instance.moduleToFilename?.set(mod, resolvedPath);
1396
1437
  const resolver = createModuleResolver(instance, connection);
1397
1438
  await mod.instantiate(instance.runtime.context, resolver);
1398
1439
  instance.moduleCache?.set(specifier, mod);
1440
+ instance.moduleCache?.set(cacheKey, mod);
1399
1441
  return mod;
1400
1442
  };
1401
1443
  }
@@ -1538,6 +1580,10 @@ function handleCallbackStreamStart(message, connection) {
1538
1580
  resolver();
1539
1581
  }
1540
1582
  connection.callbackStreamReceivers.delete(message.streamId);
1583
+ sendMessage(connection.socket, {
1584
+ type: import_isolate_protocol.MessageType.CALLBACK_STREAM_CANCEL,
1585
+ streamId: message.streamId
1586
+ });
1541
1587
  return Promise.resolve();
1542
1588
  }
1543
1589
  });
@@ -1810,4 +1856,4 @@ async function handleClearCollectedData(message, connection, state) {
1810
1856
  }
1811
1857
  }
1812
1858
 
1813
- //# debugId=DE5E35C70F14FD3964756E2164756E21
1859
+ //# debugId=32017BAA615D918C64756E2164756E21