UNPKG

@remotion/renderer

Version:

Render Remotion videos using Node.js or Bun

254 lines (253 loc) • 11.1 kB
"use strict"; /** * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.NetworkManager = exports.NetworkManagerEmittedEvents = void 0; const EventEmitter_1 = require("./EventEmitter"); const handle_failed_resource_1 = require("./handle-failed-resource"); const HTTPRequest_1 = require("./HTTPRequest"); const HTTPResponse_1 = require("./HTTPResponse"); const NetworkEventManager_1 = require("./NetworkEventManager"); exports.NetworkManagerEmittedEvents = { Request: Symbol('NetworkManager.Request'), }; class NetworkManager extends EventEmitter_1.EventEmitter { #client; #frameManager; #networkEventManager = new NetworkEventManager_1.NetworkEventManager(); #indent; #logLevel; constructor(client, frameManager, indent, logLevel) { super(); this.#client = client; this.#frameManager = frameManager; this.#indent = indent; this.#logLevel = logLevel; this.#client.on('Fetch.requestPaused', this.#onRequestPaused.bind(this)); this.#client.on('Network.requestWillBeSent', this.#onRequestWillBeSent.bind(this)); this.#client.on('Network.requestServedFromCache', this.#onRequestServedFromCache.bind(this)); this.#client.on('Network.responseReceived', this.#onResponseReceived.bind(this)); this.#client.on('Network.loadingFinished', this.#onLoadingFinished.bind(this)); this.#client.on('Network.loadingFailed', this.#onLoadingFailed.bind(this)); this.#client.on('Network.responseReceivedExtraInfo', this.#onResponseReceivedExtraInfo.bind(this)); } async initialize() { await this.#client.send('Network.enable'); } #onRequestWillBeSent(event) { this.#onRequest(event, undefined); } /** * CDP may send a Fetch.requestPaused without or before a * Network.requestWillBeSent * * CDP may send multiple Fetch.requestPaused * for the same Network.requestWillBeSent. */ #onRequestPaused(event) { const { networkId: networkRequestId, requestId: fetchRequestId } = event; if (!networkRequestId) { return; } const requestWillBeSentEvent = (() => { const _requestWillBeSentEvent = this.#networkEventManager.getRequestWillBeSent(networkRequestId); // redirect requests have the same `requestId`, if (_requestWillBeSentEvent && (_requestWillBeSentEvent.request.url !== event.request.url || _requestWillBeSentEvent.request.method !== event.request.method)) { this.#networkEventManager.forgetRequestWillBeSent(networkRequestId); return; } return _requestWillBeSentEvent; })(); if (requestWillBeSentEvent) { this.#patchRequestEventHeaders(requestWillBeSentEvent, event); this.#onRequest(requestWillBeSentEvent, fetchRequestId); } else { this.#networkEventManager.storeRequestPaused(networkRequestId, event); } } #patchRequestEventHeaders(requestWillBeSentEvent, requestPausedEvent) { requestWillBeSentEvent.request.headers = { ...requestWillBeSentEvent.request.headers, // includes extra headers, like: Accept, Origin ...requestPausedEvent.request.headers, }; } #onRequest(event, fetchRequestId) { if (event.redirectResponse) { // We want to emit a response and requestfinished for the // redirectResponse, but we can't do so unless we have a // responseExtraInfo ready to pair it up with. If we don't have any // responseExtraInfos saved in our queue, they we have to wait until // the next one to emit response and requestfinished, *and* we should // also wait to emit this Request too because it should come after the // response/requestfinished. let redirectResponseExtraInfo = null; if (event.redirectHasExtraInfo) { redirectResponseExtraInfo = this.#networkEventManager .getResponseExtraInfo(event.requestId) .shift(); if (!redirectResponseExtraInfo) { this.#networkEventManager.queueRedirectInfo(event.requestId, { event, fetchRequestId, }); return; } } const _request = this.#networkEventManager.getRequest(event.requestId); // If we connect late to the target, we could have missed the // requestWillBeSent event. if (_request) { this.#handleRequestRedirect(_request, event.redirectResponse, redirectResponseExtraInfo); } } const frame = event.frameId ? this.#frameManager.frame(event.frameId) : null; const request = new HTTPRequest_1.HTTPRequest(frame, event); this.#networkEventManager.storeRequest(event.requestId, request); this.emit(exports.NetworkManagerEmittedEvents.Request, request); } #onRequestServedFromCache(event) { const request = this.#networkEventManager.getRequest(event.requestId); if (request) { request._fromMemoryCache = true; } } #handleRequestRedirect(request, responsePayload, extraInfo) { const response = new HTTPResponse_1.HTTPResponse(responsePayload, extraInfo); request._response = response; this.#forgetRequest(request, false); } #emitResponseEvent(responseReceived, extraInfo) { const request = this.#networkEventManager.getRequest(responseReceived.requestId); // FileUpload sends a response without a matching request. if (!request) { return; } const response = new HTTPResponse_1.HTTPResponse(responseReceived.response, extraInfo); request._response = response; } #onResponseReceived(event) { const request = this.#networkEventManager.getRequest(event.requestId); let extraInfo = null; if (request && !request._fromMemoryCache && event.hasExtraInfo) { extraInfo = this.#networkEventManager .getResponseExtraInfo(event.requestId) .shift(); if (!extraInfo) { // Wait until we get the corresponding ExtraInfo event. this.#networkEventManager.queueEventGroup(event.requestId, { responseReceivedEvent: event, }); return; } } this.#emitResponseEvent(event, extraInfo); } #onResponseReceivedExtraInfo(event) { // We may have skipped a redirect response/request pair due to waiting for // this ExtraInfo event. If so, continue that work now that we have the // request. const redirectInfo = this.#networkEventManager.takeQueuedRedirectInfo(event.requestId); if (redirectInfo) { this.#networkEventManager .getResponseExtraInfo(event.requestId) .push(event); this.#onRequest(redirectInfo.event, redirectInfo.fetchRequestId); return; } // We may have skipped response and loading events because we didn't have // this ExtraInfo event yet. If so, emit those events now. const queuedEvents = this.#networkEventManager.getQueuedEventGroup(event.requestId); if (queuedEvents) { this.#networkEventManager.forgetQueuedEventGroup(event.requestId); this.#emitResponseEvent(queuedEvents.responseReceivedEvent, event); if (queuedEvents.loadingFinishedEvent) { this.#emitLoadingFinished(queuedEvents.loadingFinishedEvent); } if (queuedEvents.loadingFailedEvent) { this.#emitLoadingFailed(queuedEvents.loadingFailedEvent); } return; } // Wait until we get another event that can use this ExtraInfo event. this.#networkEventManager.getResponseExtraInfo(event.requestId).push(event); } #forgetRequest(request, events) { const requestId = request._requestId; this.#networkEventManager.forgetRequest(requestId); if (events) { this.#networkEventManager.forget(requestId); } } #onLoadingFinished(event) { // If the response event for this request is still waiting on a // corresponding ExtraInfo event, then wait to emit this event too. const queuedEvents = this.#networkEventManager.getQueuedEventGroup(event.requestId); if (queuedEvents) { queuedEvents.loadingFinishedEvent = event; } else { this.#emitLoadingFinished(event); } } #emitLoadingFinished(event) { const request = this.#networkEventManager.getRequest(event.requestId); // For certain requestIds we never receive requestWillBeSent event. // @see https://crbug.com/750469 if (!request) { return; } this.#forgetRequest(request, true); } #onLoadingFailed(event) { // If the response event for this request is still waiting on a // corresponding ExtraInfo event, then wait to emit this event too. const queuedEvents = this.#networkEventManager.getQueuedEventGroup(event.requestId); if (queuedEvents) { queuedEvents.loadingFailedEvent = event; } else { this.#emitLoadingFailed(event); } } #emitLoadingFailed(event) { const request = this.#networkEventManager.getRequest(event.requestId); // For certain requestIds we never receive requestWillBeSent event. // @see https://crbug.com/750469 if (!request) { return; } if (event.canceled) { this.#forgetRequest(request, true); return; } const extraInfo = this.#networkEventManager.getResponseExtraInfo(event.requestId); (0, handle_failed_resource_1.handleFailedResource)({ extraInfo, event, indent: this.#indent, logLevel: this.#logLevel, request, }); this.#forgetRequest(request, true); } } exports.NetworkManager = NetworkManager;