@ricsam/isolate-client 0.1.9 → 0.1.10

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.
package/README.md CHANGED
@@ -85,7 +85,10 @@ const namespace = client.createNamespace("tenant-123");
85
85
  // Create a runtime in this namespace
86
86
  const runtime = await namespace.createRuntime({
87
87
  memoryLimitMB: 128,
88
- moduleLoader: async (name) => loadModule(name),
88
+ moduleLoader: async (name, importer) => {
89
+ const code = loadModule(name);
90
+ return { code, resolveDir: importer.resolveDir };
91
+ },
89
92
  });
90
93
 
91
94
  console.log(runtime.reused); // false - first time
@@ -145,21 +148,29 @@ interface Namespace {
145
148
 
146
149
  ## Module Loader
147
150
 
148
- Register a custom module loader to handle dynamic `import()` calls:
151
+ Register a custom module loader to handle dynamic `import()` calls. The loader receives the module specifier and importer info, and returns an object with the source code and `resolveDir` (used to resolve nested relative imports):
149
152
 
150
153
  ```typescript
151
154
  const runtime = await client.createRuntime({
152
- moduleLoader: async (moduleName: string) => {
155
+ moduleLoader: async (moduleName: string, importer) => {
156
+ // importer.path = resolved path of importing module
157
+ // importer.resolveDir = directory for relative resolution
153
158
  if (moduleName === "@/db") {
154
- return `
155
- export async function getUser(id) {
156
- const response = await fetch("/api/users/" + id);
157
- return response.json();
158
- }
159
- `;
159
+ return {
160
+ code: `
161
+ export async function getUser(id) {
162
+ const response = await fetch("/api/users/" + id);
163
+ return response.json();
164
+ }
165
+ `,
166
+ resolveDir: "/modules",
167
+ };
160
168
  }
161
169
  if (moduleName === "@/config") {
162
- return `export const API_KEY = "sk-xxx";`;
170
+ return {
171
+ code: `export const API_KEY = "sk-xxx";`,
172
+ resolveDir: "/modules",
173
+ };
163
174
  }
164
175
  throw new Error(`Unknown module: ${moduleName}`);
165
176
  },
@@ -1,7 +1,20 @@
1
+ var __create = Object.create;
2
+ var __getProtoOf = Object.getPrototypeOf;
1
3
  var __defProp = Object.defineProperty;
2
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
3
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
5
18
  var __moduleCache = /* @__PURE__ */ new WeakMap;
6
19
  var __toCommonJS = (from) => {
7
20
  var entry = __moduleCache.get(from), desc;
@@ -33,6 +46,7 @@ __export(exports_connection, {
33
46
  });
34
47
  module.exports = __toCommonJS(exports_connection);
35
48
  var import_node_net = require("node:net");
49
+ var import_node_path = __toESM(require("node:path"));
36
50
  var import_isolate_protocol = require("@ricsam/isolate-protocol");
37
51
  var import_client = require("@ricsam/isolate-playwright/client");
38
52
  var DEFAULT_TIMEOUT = 30000;
@@ -899,15 +913,15 @@ function registerFsCallbacks(state, callbacks) {
899
913
  const registrations = {};
900
914
  if (callbacks.readFile) {
901
915
  const callbackId = state.nextCallbackId++;
902
- state.callbacks.set(callbackId, async (path) => {
903
- const result = await callbacks.readFile(path);
916
+ state.callbacks.set(callbackId, async (path2) => {
917
+ const result = await callbacks.readFile(path2);
904
918
  return new Uint8Array(result);
905
919
  });
906
920
  registrations.readFile = { callbackId, name: "readFile", type: "async" };
907
921
  }
908
922
  if (callbacks.writeFile) {
909
923
  const callbackId = state.nextCallbackId++;
910
- state.callbacks.set(callbackId, async (path, data) => {
924
+ state.callbacks.set(callbackId, async (path2, data) => {
911
925
  let buffer;
912
926
  if (data instanceof Uint8Array) {
913
927
  buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
@@ -918,42 +932,42 @@ function registerFsCallbacks(state, callbacks) {
918
932
  } else {
919
933
  buffer = new ArrayBuffer(0);
920
934
  }
921
- await callbacks.writeFile(path, buffer);
935
+ await callbacks.writeFile(path2, buffer);
922
936
  });
923
937
  registrations.writeFile = { callbackId, name: "writeFile", type: "async" };
924
938
  }
925
939
  if (callbacks.unlink) {
926
940
  const callbackId = state.nextCallbackId++;
927
- state.callbacks.set(callbackId, async (path) => {
928
- await callbacks.unlink(path);
941
+ state.callbacks.set(callbackId, async (path2) => {
942
+ await callbacks.unlink(path2);
929
943
  });
930
944
  registrations.unlink = { callbackId, name: "unlink", type: "async" };
931
945
  }
932
946
  if (callbacks.readdir) {
933
947
  const callbackId = state.nextCallbackId++;
934
- state.callbacks.set(callbackId, async (path) => {
935
- return callbacks.readdir(path);
948
+ state.callbacks.set(callbackId, async (path2) => {
949
+ return callbacks.readdir(path2);
936
950
  });
937
951
  registrations.readdir = { callbackId, name: "readdir", type: "async" };
938
952
  }
939
953
  if (callbacks.mkdir) {
940
954
  const callbackId = state.nextCallbackId++;
941
- state.callbacks.set(callbackId, async (path, options) => {
942
- await callbacks.mkdir(path, options);
955
+ state.callbacks.set(callbackId, async (path2, options) => {
956
+ await callbacks.mkdir(path2, options);
943
957
  });
944
958
  registrations.mkdir = { callbackId, name: "mkdir", type: "async" };
945
959
  }
946
960
  if (callbacks.rmdir) {
947
961
  const callbackId = state.nextCallbackId++;
948
- state.callbacks.set(callbackId, async (path) => {
949
- await callbacks.rmdir(path);
962
+ state.callbacks.set(callbackId, async (path2) => {
963
+ await callbacks.rmdir(path2);
950
964
  });
951
965
  registrations.rmdir = { callbackId, name: "rmdir", type: "async" };
952
966
  }
953
967
  if (callbacks.stat) {
954
968
  const callbackId = state.nextCallbackId++;
955
- state.callbacks.set(callbackId, async (path) => {
956
- return callbacks.stat(path);
969
+ state.callbacks.set(callbackId, async (path2) => {
970
+ return callbacks.stat(path2);
957
971
  });
958
972
  registrations.stat = { callbackId, name: "stat", type: "async" };
959
973
  }
@@ -968,15 +982,13 @@ function registerFsCallbacks(state, callbacks) {
968
982
  }
969
983
  function registerModuleLoaderCallback(state, callback) {
970
984
  const callbackId = state.nextCallbackId++;
971
- state.callbacks.set(callbackId, async (moduleName) => {
985
+ state.callbacks.set(callbackId, async (moduleName, importer) => {
972
986
  const specifier = moduleName;
973
- const cached = state.moduleSourceCache.get(specifier);
974
- if (cached !== undefined) {
975
- return cached;
976
- }
977
- const source = await callback(specifier);
978
- state.moduleSourceCache.set(specifier, source);
979
- return source;
987
+ const importerInfo = importer;
988
+ const result = await callback(specifier, importerInfo);
989
+ const resolvedPath = import_node_path.default.posix.join(result.resolveDir, import_node_path.default.posix.basename(specifier));
990
+ state.moduleSourceCache.set(resolvedPath, result.code);
991
+ return result;
980
992
  });
981
993
  return { callbackId, name: "moduleLoader", type: "async" };
982
994
  }
@@ -1302,4 +1314,4 @@ async function sendBodyStream(state, streamId, body) {
1302
1314
  }
1303
1315
  }
1304
1316
 
1305
- //# debugId=88982DC84A5B7DE464756E2164756E21
1317
+ //# debugId=65FD012AB9411FDF64756E2164756E21