logManager.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.LogManager = void 0;
  4. const protocol_js_1 = require("../../../protocol/protocol.js");
  5. const logHelper_js_1 = require("./logHelper.js");
  6. /** Converts CDP StackTrace object to BiDi StackTrace object. */
  7. function getBidiStackTrace(cdpStackTrace) {
  8. const stackFrames = cdpStackTrace?.callFrames.map((callFrame) => {
  9. return {
  10. columnNumber: callFrame.columnNumber,
  11. functionName: callFrame.functionName,
  12. lineNumber: callFrame.lineNumber,
  13. url: callFrame.url,
  14. };
  15. });
  16. return stackFrames ? { callFrames: stackFrames } : undefined;
  17. }
  18. function getLogLevel(consoleApiType) {
  19. if (['assert', 'error'].includes(consoleApiType)) {
  20. return 'error';
  21. }
  22. if (['debug', 'trace'].includes(consoleApiType)) {
  23. return 'debug';
  24. }
  25. if (['warn', 'warning'].includes(consoleApiType)) {
  26. return 'warn';
  27. }
  28. return 'info';
  29. }
  30. class LogManager {
  31. #eventManager;
  32. #realmStorage;
  33. #cdpTarget;
  34. constructor(cdpTarget, realmStorage, eventManager) {
  35. this.#cdpTarget = cdpTarget;
  36. this.#realmStorage = realmStorage;
  37. this.#eventManager = eventManager;
  38. }
  39. static create(cdpTarget, realmStorage, eventManager) {
  40. const logManager = new LogManager(cdpTarget, realmStorage, eventManager);
  41. logManager.#initialize();
  42. return logManager;
  43. }
  44. #initialize() {
  45. this.#initializeLogEntryAddedEventListener();
  46. }
  47. #initializeLogEntryAddedEventListener() {
  48. this.#cdpTarget.cdpClient.on('Runtime.consoleAPICalled', (params) => {
  49. // Try to find realm by `cdpSessionId` and `executionContextId`,
  50. // if provided.
  51. const realm = this.#realmStorage.findRealm({
  52. cdpSessionId: this.#cdpTarget.cdpSessionId,
  53. executionContextId: params.executionContextId,
  54. });
  55. const argsPromise = realm === undefined
  56. ? Promise.resolve(params.args)
  57. : // Properly serialize arguments if possible.
  58. Promise.all(params.args.map((arg) => {
  59. return realm.serializeCdpObject(arg, 'none');
  60. }));
  61. this.#eventManager.registerPromiseEvent(argsPromise.then((args) => ({
  62. method: protocol_js_1.Log.EventNames.LogEntryAddedEvent,
  63. params: {
  64. level: getLogLevel(params.type),
  65. source: {
  66. realm: realm?.realmId ?? 'UNKNOWN',
  67. context: realm?.browsingContextId ?? 'UNKNOWN',
  68. },
  69. text: (0, logHelper_js_1.getRemoteValuesText)(args, true),
  70. timestamp: Math.round(params.timestamp),
  71. stackTrace: getBidiStackTrace(params.stackTrace),
  72. type: 'console',
  73. // Console method is `warn`, not `warning`.
  74. method: params.type === 'warning' ? 'warn' : params.type,
  75. args,
  76. },
  77. })), realm?.browsingContextId ?? 'UNKNOWN', protocol_js_1.Log.EventNames.LogEntryAddedEvent);
  78. });
  79. this.#cdpTarget.cdpClient.on('Runtime.exceptionThrown', (params) => {
  80. // Try to find realm by `cdpSessionId` and `executionContextId`,
  81. // if provided.
  82. const realm = this.#realmStorage.findRealm({
  83. cdpSessionId: this.#cdpTarget.cdpSessionId,
  84. executionContextId: params.exceptionDetails.executionContextId,
  85. });
  86. // Try all the best to get the exception text.
  87. const textPromise = (async () => {
  88. if (!params.exceptionDetails.exception) {
  89. return params.exceptionDetails.text;
  90. }
  91. if (realm === undefined) {
  92. return JSON.stringify(params.exceptionDetails.exception);
  93. }
  94. return realm.stringifyObject(params.exceptionDetails.exception);
  95. })();
  96. this.#eventManager.registerPromiseEvent(textPromise.then((text) => ({
  97. method: protocol_js_1.Log.EventNames.LogEntryAddedEvent,
  98. params: {
  99. level: 'error',
  100. source: {
  101. realm: realm?.realmId ?? 'UNKNOWN',
  102. context: realm?.browsingContextId ?? 'UNKNOWN',
  103. },
  104. text,
  105. timestamp: Math.round(params.timestamp),
  106. stackTrace: getBidiStackTrace(params.exceptionDetails.stackTrace),
  107. type: 'javascript',
  108. },
  109. })), realm?.browsingContextId ?? 'UNKNOWN', protocol_js_1.Log.EventNames.LogEntryAddedEvent);
  110. });
  111. }
  112. }
  113. exports.LogManager = LogManager;
  114. //# sourceMappingURL=logManager.js.map