@ricsam/isolate-runtime 0.1.16 → 0.1.18

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.
@@ -4,38 +4,59 @@ var __defProp = Object.defineProperty;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ function __accessProp(key) {
8
+ return this[key];
9
+ }
10
+ var __toESMCache_node;
11
+ var __toESMCache_esm;
7
12
  var __toESM = (mod, isNodeMode, target) => {
13
+ var canCache = mod != null && typeof mod === "object";
14
+ if (canCache) {
15
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
+ var cached = cache.get(mod);
17
+ if (cached)
18
+ return cached;
19
+ }
8
20
  target = mod != null ? __create(__getProtoOf(mod)) : {};
9
21
  const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
22
  for (let key of __getOwnPropNames(mod))
11
23
  if (!__hasOwnProp.call(to, key))
12
24
  __defProp(to, key, {
13
- get: () => mod[key],
25
+ get: __accessProp.bind(mod, key),
14
26
  enumerable: true
15
27
  });
28
+ if (canCache)
29
+ cache.set(mod, to);
16
30
  return to;
17
31
  };
18
- var __moduleCache = /* @__PURE__ */ new WeakMap;
19
32
  var __toCommonJS = (from) => {
20
- var entry = __moduleCache.get(from), desc;
33
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
21
34
  if (entry)
22
35
  return entry;
23
36
  entry = __defProp({}, "__esModule", { value: true });
24
- if (from && typeof from === "object" || typeof from === "function")
25
- __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
26
- get: () => from[key],
27
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
28
- }));
37
+ if (from && typeof from === "object" || typeof from === "function") {
38
+ for (var key of __getOwnPropNames(from))
39
+ if (!__hasOwnProp.call(entry, key))
40
+ __defProp(entry, key, {
41
+ get: __accessProp.bind(from, key),
42
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
43
+ });
44
+ }
29
45
  __moduleCache.set(from, entry);
30
46
  return entry;
31
47
  };
48
+ var __moduleCache;
49
+ var __returnValue = (v) => v;
50
+ function __exportSetter(name, newValue) {
51
+ this[name] = __returnValue.bind(null, newValue);
52
+ }
32
53
  var __export = (target, all) => {
33
54
  for (var name in all)
34
55
  __defProp(target, name, {
35
56
  get: all[name],
36
57
  enumerable: true,
37
58
  configurable: true,
38
- set: (newValue) => all[name] = () => newValue
59
+ set: __exportSetter.bind(all, name)
39
60
  });
40
61
  };
41
62
 
@@ -670,29 +691,36 @@ function createLocalCustomFunctionsMarshalOptions() {
670
691
  }
671
692
  function createModuleResolver(state) {
672
693
  return async (specifier, referrer) => {
673
- const staticCached = state.staticModuleCache.get(specifier);
694
+ const importerPath = state.moduleToFilename.get(referrer) ?? "<unknown>";
695
+ const importerResolveDir = import_node_path.default.posix.dirname(importerPath);
696
+ const importerStack = state.moduleImportChain.get(importerPath) ?? [];
697
+ const resolvedSpecifier = specifier.startsWith(".") ? import_node_path.default.posix.normalize(import_node_path.default.posix.join(importerResolveDir, specifier)) : specifier;
698
+ state.specifierToImporter.set(resolvedSpecifier, importerPath);
699
+ const staticCached = state.staticModuleCache.get(resolvedSpecifier);
674
700
  if (staticCached)
675
701
  return staticCached;
676
- const cached = state.moduleCache.get(specifier);
702
+ const cached = state.moduleCache.get(resolvedSpecifier);
677
703
  if (cached)
678
704
  return cached;
679
705
  if (!state.moduleLoader) {
680
706
  throw new Error(`No module loader registered. Cannot import: ${specifier}`);
681
707
  }
682
- const importerPath = state.moduleToFilename.get(referrer) ?? "<unknown>";
683
- const importerResolveDir = import_node_path.default.posix.dirname(importerPath);
684
708
  const result = await state.moduleLoader(specifier, {
685
709
  path: importerPath,
686
710
  resolveDir: importerResolveDir
687
711
  });
688
712
  const { code, resolveDir } = result;
713
+ if (result.filename.includes("/")) {
714
+ throw new Error(`moduleLoader returned a filename with slashes: "${result.filename}". ` + `filename must be a basename (e.g. "utils.js"), not a path.`);
715
+ }
716
+ const resolvedFilename = import_node_path.default.posix.join(resolveDir, result.filename);
689
717
  const hash = import_isolate_transform.contentHash(code);
690
- const cacheKey = `${specifier}:${hash}`;
718
+ const cacheKey = `${resolvedSpecifier}:${hash}`;
691
719
  const inFlightKey = `${result.static ? "static" : "dynamic"}:${cacheKey}`;
692
- const staticCachedAfterLoad = state.staticModuleCache.get(specifier);
720
+ const staticCachedAfterLoad = state.staticModuleCache.get(resolvedSpecifier);
693
721
  if (staticCachedAfterLoad)
694
722
  return staticCachedAfterLoad;
695
- const cachedAfterLoad = state.moduleCache.get(specifier);
723
+ const cachedAfterLoad = state.moduleCache.get(resolvedSpecifier);
696
724
  if (cachedAfterLoad)
697
725
  return cachedAfterLoad;
698
726
  const hashCached = state.moduleCache.get(cacheKey);
@@ -706,41 +734,51 @@ function createModuleResolver(state) {
706
734
  try {
707
735
  let transformed = state.transformCache.get(hash);
708
736
  if (!transformed) {
709
- transformed = await import_isolate_transform.transformModuleCode(code, specifier);
737
+ transformed = await import_isolate_transform.transformModuleCode(code, resolvedSpecifier);
710
738
  state.transformCache.set(hash, transformed);
711
739
  }
712
740
  if (transformed.sourceMap) {
713
- state.sourceMaps.set(specifier, transformed.sourceMap);
741
+ state.sourceMaps.set(resolvedSpecifier, transformed.sourceMap);
714
742
  }
715
743
  mod = await state.isolate.compileModule(transformed.code, {
716
- filename: specifier
744
+ filename: resolvedSpecifier
717
745
  });
718
- const resolvedPath = import_node_path.default.posix.join(resolveDir, import_node_path.default.posix.basename(specifier));
719
- state.moduleToFilename.set(mod, resolvedPath);
746
+ state.moduleToFilename.set(mod, resolvedFilename);
747
+ state.moduleImportChain.set(resolvedFilename, [...importerStack, importerPath]);
720
748
  if (result.static) {
721
- state.staticModuleCache.set(specifier, mod);
749
+ state.staticModuleCache.set(resolvedSpecifier, mod);
722
750
  } else {
723
- state.moduleCache.set(specifier, mod);
751
+ state.moduleCache.set(resolvedSpecifier, mod);
724
752
  state.moduleCache.set(cacheKey, mod);
725
753
  }
726
754
  return mod;
727
755
  } catch (err) {
756
+ const error = err instanceof Error ? err : new Error(String(err));
757
+ error.message = `Failed to compile module "${resolvedSpecifier}" (imported by "${importerPath}"):
758
+ ${error.message}`;
759
+ if (importerStack.length > 0) {
760
+ error.message += `
761
+
762
+ Import chain:
763
+ ${[...importerStack, importerPath].join(`
764
+ -> `)}`;
765
+ }
728
766
  if (mod) {
729
767
  state.moduleToFilename.delete(mod);
730
768
  if (result.static) {
731
- if (state.staticModuleCache.get(specifier) === mod) {
732
- state.staticModuleCache.delete(specifier);
769
+ if (state.staticModuleCache.get(resolvedSpecifier) === mod) {
770
+ state.staticModuleCache.delete(resolvedSpecifier);
733
771
  }
734
772
  } else {
735
- if (state.moduleCache.get(specifier) === mod) {
736
- state.moduleCache.delete(specifier);
773
+ if (state.moduleCache.get(resolvedSpecifier) === mod) {
774
+ state.moduleCache.delete(resolvedSpecifier);
737
775
  }
738
776
  if (state.moduleCache.get(cacheKey) === mod) {
739
777
  state.moduleCache.delete(cacheKey);
740
778
  }
741
779
  }
742
780
  }
743
- throw err;
781
+ throw error;
744
782
  }
745
783
  })();
746
784
  state.moduleLoadsInFlight.set(inFlightKey, loadPromise);
@@ -778,6 +816,8 @@ async function createRuntime(options) {
778
816
  transformCache: new Map,
779
817
  moduleToFilename: new Map,
780
818
  sourceMaps: new Map,
819
+ moduleImportChain: new Map,
820
+ specifierToImporter: new Map,
781
821
  pendingCallbacks: [],
782
822
  evalChain: Promise.resolve(),
783
823
  moduleLoader: opts.moduleLoader,
@@ -1023,8 +1063,53 @@ async function createRuntime(options) {
1023
1063
  filename
1024
1064
  });
1025
1065
  state.moduleToFilename.set(mod, filename);
1066
+ state.moduleImportChain.set(filename, []);
1026
1067
  const resolver = createModuleResolver(state);
1027
- await mod.instantiate(state.context, resolver);
1068
+ try {
1069
+ await mod.instantiate(state.context, resolver);
1070
+ } catch (err) {
1071
+ const error = err instanceof Error ? err : new Error(String(err));
1072
+ const specifierMatch = error.message.match(/The requested module '([^']+)'/);
1073
+ const exportMatch = error.message.match(/export named '([^']+)'/);
1074
+ const failingSpecifier = specifierMatch?.[1];
1075
+ const failingExport = exportMatch?.[1];
1076
+ const importerFile = failingSpecifier ? state.specifierToImporter.get(failingSpecifier) : undefined;
1077
+ const details = [];
1078
+ if (importerFile) {
1079
+ const chain = state.moduleImportChain.get(importerFile) ?? [];
1080
+ const fullChain = [...chain, importerFile];
1081
+ if (failingSpecifier)
1082
+ fullChain.push(failingSpecifier);
1083
+ const trimmed = fullChain.length > 12 ? fullChain.slice(-12) : fullChain;
1084
+ const prefix = fullChain.length > 12 ? ` ...
1085
+ ` : "";
1086
+ details.push(`Import chain:
1087
+ ` + prefix + trimmed.map((p) => ` ${p}`).join(`
1088
+ -> `));
1089
+ } else if (failingSpecifier) {
1090
+ for (const [modPath, chain] of state.moduleImportChain) {
1091
+ if (modPath.includes(failingSpecifier) || modPath.endsWith(failingSpecifier)) {
1092
+ const fullChain = [...chain, modPath];
1093
+ const trimmed = fullChain.length > 12 ? fullChain.slice(-12) : fullChain;
1094
+ details.push(`Import chain:
1095
+ ` + trimmed.map((p) => ` ${p}`).join(`
1096
+ -> `));
1097
+ break;
1098
+ }
1099
+ }
1100
+ }
1101
+ if (failingExport && failingSpecifier) {
1102
+ details.push(`Hint: If '${failingExport}' is a TypeScript type/interface, use \`import type\` to prevent it from being resolved at runtime:
1103
+ ` + ` import type { ${failingExport} } from '${failingSpecifier}';`);
1104
+ }
1105
+ const suffix = details.length > 0 ? `
1106
+
1107
+ ` + details.join(`
1108
+
1109
+ `) : "";
1110
+ error.message = `Module instantiation failed: ${error.message}${suffix}`;
1111
+ throw error;
1112
+ }
1028
1113
  await mod.evaluate();
1029
1114
  const ns = mod.namespace;
1030
1115
  const runRef = await ns.get("default", { reference: true });
@@ -1059,6 +1144,8 @@ async function createRuntime(options) {
1059
1144
  state.moduleCache.clear();
1060
1145
  state.moduleLoadsInFlight.clear();
1061
1146
  state.moduleToFilename.clear();
1147
+ state.moduleImportChain.clear();
1148
+ state.specifierToImporter.clear();
1062
1149
  state.sourceMaps.clear();
1063
1150
  },
1064
1151
  async dispose() {
@@ -1083,4 +1170,4 @@ async function createRuntime(options) {
1083
1170
  };
1084
1171
  }
1085
1172
 
1086
- //# debugId=9159E7F9BC462BBB64756E2164756E21
1173
+ //# debugId=2027548ECE80ECC164756E2164756E21