@ricsam/isolate-client 0.1.10 → 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.
@@ -397,6 +397,10 @@ async function createRuntime(state, options = {}, namespaceId) {
397
397
  callbacks.custom = registerCustomFunctions(state, options.customFunctions);
398
398
  }
399
399
  let playwrightHandler;
400
+ const browserConsoleLogs = [];
401
+ const networkRequests = [];
402
+ const networkResponses = [];
403
+ const pageListenerCleanups = [];
400
404
  if (options.playwright) {
401
405
  playwrightHandler = createPlaywrightHandler(options.playwright.page, {
402
406
  timeout: options.playwright.timeout,
@@ -408,65 +412,67 @@ async function createRuntime(state, options = {}, namespaceId) {
408
412
  const result2 = await playwrightHandler(op);
409
413
  return JSON.stringify(result2);
410
414
  });
411
- const hasOnEvent = !!options.playwright.onEvent;
412
- const hasConsoleHandler = options.playwright.console && options.console?.onEntry;
413
- let browserConsoleLogCallbackId;
414
- if (hasOnEvent || hasConsoleHandler) {
415
- browserConsoleLogCallbackId = registerEventCallback(state, (entry) => {
416
- const browserEntry = entry;
417
- if (options.playwright.onEvent) {
418
- options.playwright.onEvent({
419
- type: "browserConsoleLog",
420
- level: browserEntry.level,
421
- stdout: browserEntry.stdout,
422
- timestamp: browserEntry.timestamp
423
- });
424
- }
425
- if (options.playwright.console && options.console?.onEntry) {
426
- options.console.onEntry({
427
- type: "browserOutput",
428
- level: browserEntry.level,
429
- stdout: browserEntry.stdout,
430
- timestamp: browserEntry.timestamp
431
- });
432
- }
433
- });
434
- }
435
- let networkRequestCallbackId;
436
- if (hasOnEvent) {
437
- networkRequestCallbackId = registerEventCallback(state, (info) => {
438
- 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) {
439
448
  options.playwright.onEvent({
440
449
  type: "networkRequest",
441
- url: reqInfo.url,
442
- method: reqInfo.method,
443
- headers: reqInfo.headers,
444
- postData: reqInfo.postData,
445
- resourceType: reqInfo.resourceType,
446
- timestamp: reqInfo.timestamp
450
+ ...info
447
451
  });
448
- });
449
- }
450
- let networkResponseCallbackId;
451
- if (hasOnEvent) {
452
- networkResponseCallbackId = registerEventCallback(state, (info) => {
453
- 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) {
454
463
  options.playwright.onEvent({
455
464
  type: "networkResponse",
456
- url: resInfo.url,
457
- status: resInfo.status,
458
- statusText: resInfo.statusText,
459
- headers: resInfo.headers,
460
- timestamp: resInfo.timestamp
465
+ ...info
461
466
  });
462
- });
463
- }
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));
464
473
  callbacks.playwright = {
465
474
  handlerCallbackId,
466
- console: options.playwright.console && !options.console?.onEntry,
467
- onBrowserConsoleLogCallbackId: browserConsoleLogCallbackId,
468
- onNetworkRequestCallbackId: networkRequestCallbackId,
469
- onNetworkResponseCallbackId: networkResponseCallbackId
475
+ console: options.playwright.console && !options.console?.onEntry
470
476
  };
471
477
  }
472
478
  let testEnvironmentOption;
@@ -724,29 +730,23 @@ async function createRuntime(state, options = {}, namespaceId) {
724
730
  }
725
731
  };
726
732
  const playwrightHandle = {
727
- async getCollectedData() {
733
+ getCollectedData() {
728
734
  if (!playwrightEnabled) {
729
735
  throw new Error("Playwright not configured. Provide playwright.page in createRuntime options.");
730
736
  }
731
- const reqId = state.nextRequestId++;
732
- const req = {
733
- type: MessageType.GET_COLLECTED_DATA,
734
- requestId: reqId,
735
- isolateId
737
+ return {
738
+ browserConsoleLogs: [...browserConsoleLogs],
739
+ networkRequests: [...networkRequests],
740
+ networkResponses: [...networkResponses]
736
741
  };
737
- return sendRequest(state, req);
738
742
  },
739
- async clearCollectedData() {
743
+ clearCollectedData() {
740
744
  if (!playwrightEnabled) {
741
745
  throw new Error("Playwright not configured. Provide playwright.page in createRuntime options.");
742
746
  }
743
- const reqId = state.nextRequestId++;
744
- const req = {
745
- type: MessageType.CLEAR_COLLECTED_DATA,
746
- requestId: reqId,
747
- isolateId
748
- };
749
- await sendRequest(state, req);
747
+ browserConsoleLogs.length = 0;
748
+ networkRequests.length = 0;
749
+ networkResponses.length = 0;
750
750
  }
751
751
  };
752
752
  return {
@@ -773,6 +773,9 @@ async function createRuntime(state, options = {}, namespaceId) {
773
773
  await sendRequest(state, req);
774
774
  },
775
775
  dispose: async () => {
776
+ for (const cleanup of pageListenerCleanups) {
777
+ cleanup();
778
+ }
776
779
  isolateWsCallbacks.delete(isolateId);
777
780
  const reqId = state.nextRequestId++;
778
781
  const req = {
@@ -1279,4 +1282,4 @@ export {
1279
1282
  connect
1280
1283
  };
1281
1284
 
1282
- //# debugId=8CFFE73BFF63A58464756E2164756E21
1285
+ //# debugId=9932655385124AD564756E2164756E21