|
1 | 1 | import { createInflate, inflateSync } from 'node:zlib' |
2 | 2 | import https from 'node:https' |
3 | 3 | import type express from 'express' |
4 | | -import type { Request as PlaywrightRequest } from '@playwright/test' |
5 | 4 | import createBusboy from 'busboy' |
6 | 5 | import type { BrowserProfileEvent, BrowserProfilerTrace } from '@datadog/browser-rum/src/types/profiling' |
7 | 6 | import type { BrowserSegment, BrowserSegmentMetadata } from '@datadog/browser-rum/src/types/sessionReplay' |
8 | 7 | import type { LogsEvent } from '@datadog/browser-logs/src/logsEvent.types' |
9 | 8 | import type { RumEvent } from '@datadog/browser-rum-core/src/rumEvent.types' |
10 | 9 | import type { TelemetryEvent } from '@datadog/browser-core/src/domain/telemetry/telemetryEvent.types' |
11 | 10 |
|
12 | | -export type IncomingRequest = express.Request | PlaywrightRequest |
13 | | - |
14 | | -function isPlaywrightRequest(req: IncomingRequest): req is PlaywrightRequest { |
15 | | - return typeof (req as { url: unknown }).url === 'function' |
16 | | -} |
17 | | - |
18 | 11 | interface BaseIntakeRequest { |
19 | 12 | isBridge: boolean |
20 | 13 | encoding: string | null |
@@ -100,24 +93,19 @@ export function createIntakeProxyMiddleware(options: IntakeProxyOptions): expres |
100 | 93 | } |
101 | 94 | } |
102 | 95 |
|
103 | | -export function computeIntakeRequestInfos(req: IncomingRequest): IntakeRequestInfos { |
104 | | - let intakeUrl: URL |
105 | | - if (isPlaywrightRequest(req)) { |
106 | | - intakeUrl = new URL(req.url(), 'https://example.org') |
107 | | - } else if (typeof req.query.ddforward === 'string') { |
108 | | - intakeUrl = new URL(req.query.ddforward, 'https://example.org') |
109 | | - } else { |
110 | | - intakeUrl = new URL(req.url, 'https://example.org') |
| 96 | +function computeIntakeRequestInfos(req: express.Request): IntakeRequestInfos { |
| 97 | + const ddforward = req.query.ddforward as string | undefined |
| 98 | + if (!ddforward) { |
| 99 | + throw new Error('ddforward is missing') |
111 | 100 | } |
| 101 | + const { pathname, searchParams } = new URL(ddforward, 'https://example.org') |
112 | 102 |
|
113 | | - const { pathname, searchParams } = intakeUrl |
114 | | - const headers = isPlaywrightRequest(req) ? req.headers() : req.headers |
115 | | - const encoding = headers['content-encoding'] || searchParams.get('dd-evp-encoding') |
| 103 | + const encoding = req.headers['content-encoding'] || searchParams.get('dd-evp-encoding') |
116 | 104 | const transport = searchParams.get('_dd.api') |
117 | 105 | const batchTimeRaw = searchParams.get('batch_time') |
118 | 106 | const batchTime = batchTimeRaw ? Number(batchTimeRaw) : null |
119 | 107 |
|
120 | | - if (!isPlaywrightRequest(req) && req.query.bridge === 'true') { |
| 108 | + if (req.query.bridge === 'true') { |
121 | 109 | const eventType = req.query.event_type |
122 | 110 | return { |
123 | 111 | isBridge: true, |
@@ -152,21 +140,21 @@ export function computeIntakeRequestInfos(req: IncomingRequest): IntakeRequestIn |
152 | 140 | } |
153 | 141 | } |
154 | 142 |
|
155 | | -export function readIntakeRequest(req: IncomingRequest, infos: IntakeRequestInfos): Promise<IntakeRequest> { |
| 143 | +function readIntakeRequest(req: express.Request, infos: IntakeRequestInfos): Promise<IntakeRequest> { |
156 | 144 | if (infos.intakeType === 'replay') { |
157 | | - return readReplayIntakeRequest(req as express.Request, infos as IntakeRequestInfos & { intakeType: 'replay' }) |
| 145 | + return readReplayIntakeRequest(req, infos as IntakeRequestInfos & { intakeType: 'replay' }) |
158 | 146 | } |
159 | 147 | if (infos.intakeType === 'profile') { |
160 | | - return readProfileIntakeRequest(req as express.Request, infos as IntakeRequestInfos & { intakeType: 'profile' }) |
| 148 | + return readProfileIntakeRequest(req, infos as IntakeRequestInfos & { intakeType: 'profile' }) |
161 | 149 | } |
162 | 150 | return readEventIntakeRequest(req, infos as IntakeRequestInfos & { intakeType: 'rum' | 'logs' | 'debugger' }) |
163 | 151 | } |
164 | 152 |
|
165 | 153 | async function readEventIntakeRequest( |
166 | | - req: IncomingRequest, |
| 154 | + req: express.Request, |
167 | 155 | infos: IntakeRequestInfos & { intakeType: 'rum' | 'logs' | 'debugger' } |
168 | 156 | ): Promise<RumIntakeRequest | LogsIntakeRequest | DebuggerIntakeRequest> { |
169 | | - const rawBody = isPlaywrightRequest(req) ? req.postDataBuffer()! : await readStream(req) |
| 157 | + const rawBody = await readStream(req) |
170 | 158 | const encodedBody = infos.encoding === 'deflate' ? inflateSync(rawBody) : rawBody |
171 | 159 |
|
172 | 160 | return { |
|
0 commit comments