Repository: puppeteer/puppeteer
File: packages/puppeteer-core/src/bidi/Connection.ts (TypeScript)

Code before:
        case 'event':
          if (isCdpEvent(object)) {
            cdpSessions
              .get(object.params.session)
              ?.emit(object.params.event, object.params.params);
            return;
          }
          this.#maybeEmitOnContext(object);
          // SAFETY: We know the method and parameter still match here.
          this.emit(
            object.method,
            object.params as BidiEvents[keyof BidiEvents]
          );
          return;
      }
    }
    // Even if the response in not in BiDi protocol format but `id` is provided, reject
    // the callback.
    if ('id' in object) {
      this.#callbacks.reject(
        (object as {id: number}).id,
        `Protocol Error. Message is not in BiDi protocol format: '${message}'`,
        object.message
      );
    }
    debugError(object);
  }

  #maybeEmitOnContext(event: Bidi.ChromiumBidi.Event) {
    let context: BrowsingContext | undefined;
    // Context specific events
    if ('context' in event.params && event.params.context !== null) {
      context = this.#browsingContexts.get(event.params.context);
      // `log.entryAdded` specific context
    } else if (
      'source' in event.params &&
      event.params.source.context !== undefined
    ) {
      context = this.#browsingContexts.get(event.params.source.context);
    }
    context?.emit(event.method, event.params);
  }

  registerBrowsingContexts(context: BrowsingContext): void {
    this.#browsingContexts.set(context.id, context);
  }

  getBrowsingContext(contextId: string): BrowsingContext {
    const currentContext = this.#browsingContexts.get(contextId);
    if (!currentContext) {
      throw new Error(`BrowsingContext ${contextId} does not exist.`);

Review comment (suggestion):
Can you give more info here why this is needed? 
From my understanding we should always `type` to be populated from the spec perspective.

Code after:
          }
          this.#callbacks.reject(
            object.id,
            createProtocolError(object),
            object.message
          );
          return;
        case 'event':
          if (isCdpEvent(object)) {
            cdpSessions
              .get(object.params.session)
              ?.emit(object.params.event, object.params.params);
            return;
          }
          this.#maybeEmitOnContext(object);
          // SAFETY: We know the method and parameter still match here.
          this.emit(
            object.method,
            object.params as BidiEvents[keyof BidiEvents]
          );
          return;
      }
    }
    // Even if the response in not in BiDi protocol format but `id` is provided, reject
    // the callback. This can happen if the endpoint supports CDP instead of BiDi.
    if ('id' in object) {
      this.#callbacks.reject(
        (object as {id: number}).id,
        `Protocol Error. Message is not in BiDi protocol format: '${message}'`,
        object.message
      );
    }
    debugError(object);
  }

  #maybeEmitOnContext(event: Bidi.ChromiumBidi.Event) {
    let context: BrowsingContext | undefined;
    // Context specific events
    if ('context' in event.params && event.params.context !== null) {
      context = this.#browsingContexts.get(event.params.context);
      // `log.entryAdded` specific context
    } else if (
      'source' in event.params &&
      event.params.source.context !== undefined
    ) {
      context = this.#browsingContexts.get(event.params.source.context);
    }
    context?.emit(event.method, event.params);
  }

  registerBrowsingContexts(context: BrowsingContext): void {