@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.
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/isolate-daemon",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "type": "commonjs"
5
5
  }
@@ -26,6 +26,12 @@ import {
26
26
  import {
27
27
  createInternalRuntime
28
28
  } from "@ricsam/isolate-runtime";
29
+ import {
30
+ transformEntryCode,
31
+ transformModuleCode,
32
+ contentHash,
33
+ mapErrorStack
34
+ } from "@ricsam/isolate-transform";
29
35
  function handleConnection(socket, state) {
30
36
  const connection = {
31
37
  socket,
@@ -587,7 +593,14 @@ async function handleEval(message, connection, state) {
587
593
  instance.lastActivity = Date.now();
588
594
  try {
589
595
  const filename = normalizeEntryFilename(message.filename);
590
- const mod = await instance.runtime.isolate.compileModule(message.code, {
596
+ const transformed = await transformEntryCode(message.code, filename);
597
+ if (transformed.sourceMap) {
598
+ if (!instance.sourceMaps) {
599
+ instance.sourceMaps = new Map;
600
+ }
601
+ instance.sourceMaps.set(filename, transformed.sourceMap);
602
+ }
603
+ const mod = await instance.runtime.isolate.compileModule(transformed.code, {
591
604
  filename
592
605
  });
593
606
  if (instance.moduleLoaderCallbackId) {
@@ -599,13 +612,25 @@ async function handleEval(message, connection, state) {
599
612
  throw new Error(`No module loader registered. Cannot import: ${specifier}`);
600
613
  });
601
614
  }
602
- const timeout = message.maxExecutionMs;
603
- await mod.evaluate(timeout ? { timeout } : undefined);
615
+ await mod.evaluate();
616
+ const ns = mod.namespace;
617
+ const runRef = await ns.get("default", { reference: true });
618
+ try {
619
+ await runRef.apply(undefined, [], {
620
+ result: { promise: true },
621
+ ...message.maxExecutionMs ? { timeout: message.maxExecutionMs } : {}
622
+ });
623
+ } finally {
624
+ runRef.release();
625
+ }
604
626
  await Promise.all(instance.pendingCallbacks);
605
627
  instance.pendingCallbacks.length = 0;
606
628
  sendOk(connection.socket, message.requestId, { value: undefined });
607
629
  } catch (err) {
608
630
  const error = err;
631
+ if (error.stack && instance.sourceMaps?.size) {
632
+ error.stack = mapErrorStack(error.stack, instance.sourceMaps);
633
+ }
609
634
  const isTimeoutError = error.message?.includes("Script execution timed out");
610
635
  sendError(connection.socket, message.requestId, isTimeoutError ? ErrorCode.ISOLATE_TIMEOUT : ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
611
636
  }
@@ -1370,7 +1395,19 @@ function createModuleResolver(instance, connection) {
1370
1395
  const importerResolveDir = path.posix.dirname(importerPath);
1371
1396
  const result = await invokeClientCallback(connection, instance.moduleLoaderCallbackId, [specifier, { path: importerPath, resolveDir: importerResolveDir }]);
1372
1397
  const { code, resolveDir } = result;
1373
- const mod = await instance.runtime.isolate.compileModule(code, {
1398
+ const hash = contentHash(code);
1399
+ const cacheKey = `${specifier}:${hash}`;
1400
+ const hashCached = instance.moduleCache?.get(cacheKey);
1401
+ if (hashCached)
1402
+ return hashCached;
1403
+ const transformed = await transformModuleCode(code, specifier);
1404
+ if (transformed.sourceMap) {
1405
+ if (!instance.sourceMaps) {
1406
+ instance.sourceMaps = new Map;
1407
+ }
1408
+ instance.sourceMaps.set(specifier, transformed.sourceMap);
1409
+ }
1410
+ const mod = await instance.runtime.isolate.compileModule(transformed.code, {
1374
1411
  filename: specifier
1375
1412
  });
1376
1413
  const resolvedPath = path.posix.join(resolveDir, path.posix.basename(specifier));
@@ -1378,6 +1415,7 @@ function createModuleResolver(instance, connection) {
1378
1415
  const resolver = createModuleResolver(instance, connection);
1379
1416
  await mod.instantiate(instance.runtime.context, resolver);
1380
1417
  instance.moduleCache?.set(specifier, mod);
1418
+ instance.moduleCache?.set(cacheKey, mod);
1381
1419
  return mod;
1382
1420
  };
1383
1421
  }
@@ -1520,6 +1558,10 @@ function handleCallbackStreamStart(message, connection) {
1520
1558
  resolver();
1521
1559
  }
1522
1560
  connection.callbackStreamReceivers.delete(message.streamId);
1561
+ sendMessage(connection.socket, {
1562
+ type: MessageType.CALLBACK_STREAM_CANCEL,
1563
+ streamId: message.streamId
1564
+ });
1523
1565
  return Promise.resolve();
1524
1566
  }
1525
1567
  });
@@ -1795,4 +1837,4 @@ export {
1795
1837
  handleConnection
1796
1838
  };
1797
1839
 
1798
- //# debugId=E68FD11623EE904064756E2164756E21
1840
+ //# debugId=514032B8427B94A564756E2164756E21