@ricsam/isolate-client 0.1.9 → 0.1.11

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,6 @@
1
1
  // packages/isolate-client/src/connection.ts
2
2
  import { connect as netConnect } from "node:net";
3
+ import path from "node:path";
3
4
  import {
4
5
  createFrameParser,
5
6
  buildFrame,
@@ -396,6 +397,10 @@ async function createRuntime(state, options = {}, namespaceId) {
396
397
  callbacks.custom = registerCustomFunctions(state, options.customFunctions);
397
398
  }
398
399
  let playwrightHandler;
400
+ const browserConsoleLogs = [];
401
+ const networkRequests = [];
402
+ const networkResponses = [];
403
+ const pageListenerCleanups = [];
399
404
  if (options.playwright) {
400
405
  playwrightHandler = createPlaywrightHandler(options.playwright.page, {
401
406
  timeout: options.playwright.timeout,
@@ -407,65 +412,67 @@ async function createRuntime(state, options = {}, namespaceId) {
407
412
  const result2 = await playwrightHandler(op);
408
413
  return JSON.stringify(result2);
409
414
  });
410
- const hasOnEvent = !!options.playwright.onEvent;
411
- const hasConsoleHandler = options.playwright.console && options.console?.onEntry;
412
- let browserConsoleLogCallbackId;
413
- if (hasOnEvent || hasConsoleHandler) {
414
- browserConsoleLogCallbackId = registerEventCallback(state, (entry) => {
415
- const browserEntry = entry;
416
- if (options.playwright.onEvent) {
417
- options.playwright.onEvent({
418
- type: "browserConsoleLog",
419
- level: browserEntry.level,
420
- stdout: browserEntry.stdout,
421
- timestamp: browserEntry.timestamp
422
- });
423
- }
424
- if (options.playwright.console && options.console?.onEntry) {
425
- options.console.onEntry({
426
- type: "browserOutput",
427
- level: browserEntry.level,
428
- stdout: browserEntry.stdout,
429
- timestamp: browserEntry.timestamp
430
- });
431
- }
432
- });
433
- }
434
- let networkRequestCallbackId;
435
- if (hasOnEvent) {
436
- networkRequestCallbackId = registerEventCallback(state, (info) => {
437
- const reqInfo = info;
415
+ const page = options.playwright.page;
416
+ const onConsole = (msg) => {
417
+ const entry = {
418
+ level: msg.type(),
419
+ stdout: msg.text(),
420
+ timestamp: Date.now()
421
+ };
422
+ browserConsoleLogs.push(entry);
423
+ if (options.playwright.onEvent) {
424
+ options.playwright.onEvent({
425
+ type: "browserConsoleLog",
426
+ ...entry
427
+ });
428
+ }
429
+ if (options.playwright.console && options.console?.onEntry) {
430
+ options.console.onEntry({
431
+ type: "browserOutput",
432
+ ...entry
433
+ });
434
+ } else if (options.playwright.console) {
435
+ const prefix = entry.level === "error" ? "[browser:error]" : "[browser]";
436
+ console.log(prefix, entry.stdout);
437
+ }
438
+ };
439
+ const onRequest = (request2) => {
440
+ const info = {
441
+ url: request2.url(),
442
+ method: request2.method(),
443
+ headers: request2.headers(),
444
+ timestamp: Date.now()
445
+ };
446
+ networkRequests.push(info);
447
+ if (options.playwright.onEvent) {
438
448
  options.playwright.onEvent({
439
449
  type: "networkRequest",
440
- url: reqInfo.url,
441
- method: reqInfo.method,
442
- headers: reqInfo.headers,
443
- postData: reqInfo.postData,
444
- resourceType: reqInfo.resourceType,
445
- timestamp: reqInfo.timestamp
450
+ ...info
446
451
  });
447
- });
448
- }
449
- let networkResponseCallbackId;
450
- if (hasOnEvent) {
451
- networkResponseCallbackId = registerEventCallback(state, (info) => {
452
- const resInfo = info;
452
+ }
453
+ };
454
+ const onResponse = (response) => {
455
+ const info = {
456
+ url: response.url(),
457
+ status: response.status(),
458
+ headers: response.headers(),
459
+ timestamp: Date.now()
460
+ };
461
+ networkResponses.push(info);
462
+ if (options.playwright.onEvent) {
453
463
  options.playwright.onEvent({
454
464
  type: "networkResponse",
455
- url: resInfo.url,
456
- status: resInfo.status,
457
- statusText: resInfo.statusText,
458
- headers: resInfo.headers,
459
- timestamp: resInfo.timestamp
465
+ ...info
460
466
  });
461
- });
462
- }
467
+ }
468
+ };
469
+ page.on("console", onConsole);
470
+ page.on("request", onRequest);
471
+ page.on("response", onResponse);
472
+ pageListenerCleanups.push(() => page.removeListener("console", onConsole), () => page.removeListener("request", onRequest), () => page.removeListener("response", onResponse));
463
473
  callbacks.playwright = {
464
474
  handlerCallbackId,
465
- console: options.playwright.console && !options.console?.onEntry,
466
- onBrowserConsoleLogCallbackId: browserConsoleLogCallbackId,
467
- onNetworkRequestCallbackId: networkRequestCallbackId,
468
- onNetworkResponseCallbackId: networkResponseCallbackId
475
+ console: options.playwright.console && !options.console?.onEntry
469
476
  };
470
477
  }
471
478
  let testEnvironmentOption;
@@ -723,29 +730,23 @@ async function createRuntime(state, options = {}, namespaceId) {
723
730
  }
724
731
  };
725
732
  const playwrightHandle = {
726
- async getCollectedData() {
733
+ getCollectedData() {
727
734
  if (!playwrightEnabled) {
728
735
  throw new Error("Playwright not configured. Provide playwright.page in createRuntime options.");
729
736
  }
730
- const reqId = state.nextRequestId++;
731
- const req = {
732
- type: MessageType.GET_COLLECTED_DATA,
733
- requestId: reqId,
734
- isolateId
737
+ return {
738
+ browserConsoleLogs: [...browserConsoleLogs],
739
+ networkRequests: [...networkRequests],
740
+ networkResponses: [...networkResponses]
735
741
  };
736
- return sendRequest(state, req);
737
742
  },
738
- async clearCollectedData() {
743
+ clearCollectedData() {
739
744
  if (!playwrightEnabled) {
740
745
  throw new Error("Playwright not configured. Provide playwright.page in createRuntime options.");
741
746
  }
742
- const reqId = state.nextRequestId++;
743
- const req = {
744
- type: MessageType.CLEAR_COLLECTED_DATA,
745
- requestId: reqId,
746
- isolateId
747
- };
748
- await sendRequest(state, req);
747
+ browserConsoleLogs.length = 0;
748
+ networkRequests.length = 0;
749
+ networkResponses.length = 0;
749
750
  }
750
751
  };
751
752
  return {
@@ -772,6 +773,9 @@ async function createRuntime(state, options = {}, namespaceId) {
772
773
  await sendRequest(state, req);
773
774
  },
774
775
  dispose: async () => {
776
+ for (const cleanup of pageListenerCleanups) {
777
+ cleanup();
778
+ }
775
779
  isolateWsCallbacks.delete(isolateId);
776
780
  const reqId = state.nextRequestId++;
777
781
  const req = {
@@ -874,15 +878,15 @@ function registerFsCallbacks(state, callbacks) {
874
878
  const registrations = {};
875
879
  if (callbacks.readFile) {
876
880
  const callbackId = state.nextCallbackId++;
877
- state.callbacks.set(callbackId, async (path) => {
878
- const result = await callbacks.readFile(path);
881
+ state.callbacks.set(callbackId, async (path2) => {
882
+ const result = await callbacks.readFile(path2);
879
883
  return new Uint8Array(result);
880
884
  });
881
885
  registrations.readFile = { callbackId, name: "readFile", type: "async" };
882
886
  }
883
887
  if (callbacks.writeFile) {
884
888
  const callbackId = state.nextCallbackId++;
885
- state.callbacks.set(callbackId, async (path, data) => {
889
+ state.callbacks.set(callbackId, async (path2, data) => {
886
890
  let buffer;
887
891
  if (data instanceof Uint8Array) {
888
892
  buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
@@ -893,42 +897,42 @@ function registerFsCallbacks(state, callbacks) {
893
897
  } else {
894
898
  buffer = new ArrayBuffer(0);
895
899
  }
896
- await callbacks.writeFile(path, buffer);
900
+ await callbacks.writeFile(path2, buffer);
897
901
  });
898
902
  registrations.writeFile = { callbackId, name: "writeFile", type: "async" };
899
903
  }
900
904
  if (callbacks.unlink) {
901
905
  const callbackId = state.nextCallbackId++;
902
- state.callbacks.set(callbackId, async (path) => {
903
- await callbacks.unlink(path);
906
+ state.callbacks.set(callbackId, async (path2) => {
907
+ await callbacks.unlink(path2);
904
908
  });
905
909
  registrations.unlink = { callbackId, name: "unlink", type: "async" };
906
910
  }
907
911
  if (callbacks.readdir) {
908
912
  const callbackId = state.nextCallbackId++;
909
- state.callbacks.set(callbackId, async (path) => {
910
- return callbacks.readdir(path);
913
+ state.callbacks.set(callbackId, async (path2) => {
914
+ return callbacks.readdir(path2);
911
915
  });
912
916
  registrations.readdir = { callbackId, name: "readdir", type: "async" };
913
917
  }
914
918
  if (callbacks.mkdir) {
915
919
  const callbackId = state.nextCallbackId++;
916
- state.callbacks.set(callbackId, async (path, options) => {
917
- await callbacks.mkdir(path, options);
920
+ state.callbacks.set(callbackId, async (path2, options) => {
921
+ await callbacks.mkdir(path2, options);
918
922
  });
919
923
  registrations.mkdir = { callbackId, name: "mkdir", type: "async" };
920
924
  }
921
925
  if (callbacks.rmdir) {
922
926
  const callbackId = state.nextCallbackId++;
923
- state.callbacks.set(callbackId, async (path) => {
924
- await callbacks.rmdir(path);
927
+ state.callbacks.set(callbackId, async (path2) => {
928
+ await callbacks.rmdir(path2);
925
929
  });
926
930
  registrations.rmdir = { callbackId, name: "rmdir", type: "async" };
927
931
  }
928
932
  if (callbacks.stat) {
929
933
  const callbackId = state.nextCallbackId++;
930
- state.callbacks.set(callbackId, async (path) => {
931
- return callbacks.stat(path);
934
+ state.callbacks.set(callbackId, async (path2) => {
935
+ return callbacks.stat(path2);
932
936
  });
933
937
  registrations.stat = { callbackId, name: "stat", type: "async" };
934
938
  }
@@ -943,15 +947,13 @@ function registerFsCallbacks(state, callbacks) {
943
947
  }
944
948
  function registerModuleLoaderCallback(state, callback) {
945
949
  const callbackId = state.nextCallbackId++;
946
- state.callbacks.set(callbackId, async (moduleName) => {
950
+ state.callbacks.set(callbackId, async (moduleName, importer) => {
947
951
  const specifier = moduleName;
948
- const cached = state.moduleSourceCache.get(specifier);
949
- if (cached !== undefined) {
950
- return cached;
951
- }
952
- const source = await callback(specifier);
953
- state.moduleSourceCache.set(specifier, source);
954
- return source;
952
+ const importerInfo = importer;
953
+ const result = await callback(specifier, importerInfo);
954
+ const resolvedPath = path.posix.join(result.resolveDir, path.posix.basename(specifier));
955
+ state.moduleSourceCache.set(resolvedPath, result.code);
956
+ return result;
955
957
  });
956
958
  return { callbackId, name: "moduleLoader", type: "async" };
957
959
  }
@@ -1280,4 +1282,4 @@ export {
1280
1282
  connect
1281
1283
  };
1282
1284
 
1283
- //# debugId=3C4C993A49BD89A964756E2164756E21
1285
+ //# debugId=9932655385124AD564756E2164756E21