@ricsam/isolate-client 0.1.8 → 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 +24 -13
- package/dist/cjs/connection.cjs +37 -25
- package/dist/cjs/connection.cjs.map +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/mjs/connection.mjs +24 -25
- package/dist/mjs/connection.mjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/types/types.d.ts +7 -124
- package/package.json +1 -1
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) =>
|
|
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
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
|
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
|
},
|
|
@@ -333,7 +344,7 @@ const runtime = await client.createRuntime({
|
|
|
333
344
|
onEvent: (event) => {
|
|
334
345
|
// Unified event handler for all playwright events
|
|
335
346
|
if (event.type === "browserConsoleLog") {
|
|
336
|
-
console.log(`[browser:${event.level}]`,
|
|
347
|
+
console.log(`[browser:${event.level}]`, event.stdout);
|
|
337
348
|
} else if (event.type === "networkRequest") {
|
|
338
349
|
console.log(`[request] ${event.method} ${event.url}`);
|
|
339
350
|
} else if (event.type === "networkResponse") {
|
|
@@ -373,9 +384,9 @@ const runtime = await client.createRuntime({
|
|
|
373
384
|
console: {
|
|
374
385
|
onEntry: (entry) => {
|
|
375
386
|
if (entry.type === "output") {
|
|
376
|
-
console.log(`[sandbox:${entry.level}]`,
|
|
387
|
+
console.log(`[sandbox:${entry.level}]`, entry.stdout);
|
|
377
388
|
} else if (entry.type === "browserOutput") {
|
|
378
|
-
console.log(`[browser:${entry.level}]`,
|
|
389
|
+
console.log(`[browser:${entry.level}]`, entry.stdout);
|
|
379
390
|
}
|
|
380
391
|
},
|
|
381
392
|
},
|
package/dist/cjs/connection.cjs
CHANGED
|
@@ -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;
|
|
@@ -442,7 +456,7 @@ async function createRuntime(state, options = {}, namespaceId) {
|
|
|
442
456
|
options.playwright.onEvent({
|
|
443
457
|
type: "browserConsoleLog",
|
|
444
458
|
level: browserEntry.level,
|
|
445
|
-
|
|
459
|
+
stdout: browserEntry.stdout,
|
|
446
460
|
timestamp: browserEntry.timestamp
|
|
447
461
|
});
|
|
448
462
|
}
|
|
@@ -450,7 +464,7 @@ async function createRuntime(state, options = {}, namespaceId) {
|
|
|
450
464
|
options.console.onEntry({
|
|
451
465
|
type: "browserOutput",
|
|
452
466
|
level: browserEntry.level,
|
|
453
|
-
|
|
467
|
+
stdout: browserEntry.stdout,
|
|
454
468
|
timestamp: browserEntry.timestamp
|
|
455
469
|
});
|
|
456
470
|
}
|
|
@@ -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 (
|
|
903
|
-
const result = await callbacks.readFile(
|
|
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 (
|
|
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(
|
|
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 (
|
|
928
|
-
await callbacks.unlink(
|
|
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 (
|
|
935
|
-
return callbacks.readdir(
|
|
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 (
|
|
942
|
-
await callbacks.mkdir(
|
|
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 (
|
|
949
|
-
await callbacks.rmdir(
|
|
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 (
|
|
956
|
-
return callbacks.stat(
|
|
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
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
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=
|
|
1317
|
+
//# debugId=65FD012AB9411FDF64756E2164756E21
|