@ricsam/isolate-daemon 0.1.11 → 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.
@@ -53,6 +53,7 @@ var import_callback_fs_handler = require("./callback-fs-handler.cjs");
53
53
  var import_isolate_test_environment = require("@ricsam/isolate-test-environment");
54
54
  var import_isolate_playwright = require("@ricsam/isolate-playwright");
55
55
  var import_isolate_runtime = require("@ricsam/isolate-runtime");
56
+ var import_isolate_transform = require("@ricsam/isolate-transform");
56
57
  function handleConnection(socket, state) {
57
58
  const connection = {
58
59
  socket,
@@ -614,7 +615,14 @@ async function handleEval(message, connection, state) {
614
615
  instance.lastActivity = Date.now();
615
616
  try {
616
617
  const filename = import_isolate_protocol.normalizeEntryFilename(message.filename);
617
- const mod = await instance.runtime.isolate.compileModule(message.code, {
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, {
618
626
  filename
619
627
  });
620
628
  if (instance.moduleLoaderCallbackId) {
@@ -626,13 +634,25 @@ async function handleEval(message, connection, state) {
626
634
  throw new Error(`No module loader registered. Cannot import: ${specifier}`);
627
635
  });
628
636
  }
629
- const timeout = message.maxExecutionMs;
630
- 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
+ }
631
648
  await Promise.all(instance.pendingCallbacks);
632
649
  instance.pendingCallbacks.length = 0;
633
650
  sendOk(connection.socket, message.requestId, { value: undefined });
634
651
  } catch (err) {
635
652
  const error = err;
653
+ if (error.stack && instance.sourceMaps?.size) {
654
+ error.stack = import_isolate_transform.mapErrorStack(error.stack, instance.sourceMaps);
655
+ }
636
656
  const isTimeoutError = error.message?.includes("Script execution timed out");
637
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 });
638
658
  }
@@ -1397,7 +1417,19 @@ function createModuleResolver(instance, connection) {
1397
1417
  const importerResolveDir = import_node_path.default.posix.dirname(importerPath);
1398
1418
  const result = await invokeClientCallback(connection, instance.moduleLoaderCallbackId, [specifier, { path: importerPath, resolveDir: importerResolveDir }]);
1399
1419
  const { code, resolveDir } = result;
1400
- const mod = await instance.runtime.isolate.compileModule(code, {
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, {
1401
1433
  filename: specifier
1402
1434
  });
1403
1435
  const resolvedPath = import_node_path.default.posix.join(resolveDir, import_node_path.default.posix.basename(specifier));
@@ -1405,6 +1437,7 @@ function createModuleResolver(instance, connection) {
1405
1437
  const resolver = createModuleResolver(instance, connection);
1406
1438
  await mod.instantiate(instance.runtime.context, resolver);
1407
1439
  instance.moduleCache?.set(specifier, mod);
1440
+ instance.moduleCache?.set(cacheKey, mod);
1408
1441
  return mod;
1409
1442
  };
1410
1443
  }
@@ -1547,6 +1580,10 @@ function handleCallbackStreamStart(message, connection) {
1547
1580
  resolver();
1548
1581
  }
1549
1582
  connection.callbackStreamReceivers.delete(message.streamId);
1583
+ sendMessage(connection.socket, {
1584
+ type: import_isolate_protocol.MessageType.CALLBACK_STREAM_CANCEL,
1585
+ streamId: message.streamId
1586
+ });
1550
1587
  return Promise.resolve();
1551
1588
  }
1552
1589
  });
@@ -1819,4 +1856,4 @@ async function handleClearCollectedData(message, connection, state) {
1819
1856
  }
1820
1857
  }
1821
1858
 
1822
- //# debugId=6F23033E4A4DC7E564756E2164756E21
1859
+ //# debugId=32017BAA615D918C64756E2164756E21