'use strict'; const assert = require('assert'); const urlJoin = require('url-join'); const asyncHandler = require('../lib/express-async-handler'); const RouteTimeoutAbortError = require('../lib/errors/route-timeout-abort-error'); const UserClosedConnectionAbortError = require('../lib/errors/user-closed-connection-abort-error'); const { getSerializableSpans, getActiveTraceId } = require('../tracing/tracing-middleware'); const { SemanticAttributes } = require('@opentelemetry/semantic-conventions'); const sanitizeHtml = require('../lib/sanitize-html'); const renderPageHtml = require('../hydrogen-render/render-page-html'); const config = require('../lib/config'); const basePath = config.get('basePath'); assert(basePath); const requestTimeoutMs = config.get('requestTimeoutMs'); assert(requestTimeoutMs); // Based off of the `connect-timeout` middleware, // https://github.com/expressjs/timeout/blob/f2f520f335f2f2ae255d4778e908e8d38e3a4e68/index.js async function timeoutMiddleware(req, res, next) { req.abortController = new AbortController(); req.abortSignal = req.abortController.signal; const timeoutId = setTimeout(() => { // Signal to downstream middlewares/routes that they should stop processing/fetching // things since we timed out (downstream consumers need to respect `req.abortSignal`) req.abortController.abort( new RouteTimeoutAbortError( `Timed out after ${requestTimeoutMs}ms while trying to respond to route ${req.originalUrl}` ) ); const traceId = getActiveTraceId(); const serializableSpans = getSerializableSpans(); let humanReadableSpans; if (serializableSpans.length > 0) { humanReadableSpans = serializableSpans.map((serializableSpan) => { const method = serializableSpan.attributes[SemanticAttributes.HTTP_METHOD]; const url = serializableSpan.attributes[SemanticAttributes.HTTP_TARGET] || serializableSpan.attributes[SemanticAttributes.HTTP_URL]; const statusCode = serializableSpan.attributes[SemanticAttributes.HTTP_STATUS_CODE]; let durationString = `request is still running (${ Date.now() - serializableSpan.startTimeInMs }ms so far)`; if (serializableSpan.durationInMs) { durationString = `took ${serializableSpan.durationInMs}ms`; } return `
Server was unable to respond in time (${requestTimeoutMs / 1000}s)