|
| 1 | +import type { |
| 2 | + Uint8ArrayBuffer, |
| 3 | + Encoder, |
| 4 | + EncoderResult, |
| 5 | + DeflateEncoderStreamId, |
| 6 | + RawError, |
| 7 | + Context, |
| 8 | +} from '@datadog/browser-core' |
| 9 | +import { addTelemetryDebug, createHttpRequest, jsonStringify, objectEntries } from '@datadog/browser-core' |
| 10 | +import type { RumConfiguration } from '../domain/configuration' |
| 11 | +import type { LifeCycle } from '../domain/lifeCycle' |
| 12 | +import { LifeCycleEventType } from '../domain/lifeCycle' |
| 13 | + |
| 14 | +/** |
| 15 | + * transport payload consist of an event and one or more attachments |
| 16 | + */ |
| 17 | +export interface TransportPayload { |
| 18 | + event: Context |
| 19 | + [key: string]: Context |
| 20 | +} |
| 21 | + |
| 22 | +export interface Transport<T extends TransportPayload> { |
| 23 | + send: (data: T) => Promise<void> |
| 24 | +} |
| 25 | + |
| 26 | +export function createFormDataTransport<T extends TransportPayload>( |
| 27 | + configuration: RumConfiguration, |
| 28 | + lifeCycle: LifeCycle, |
| 29 | + createEncoder: (streamId: DeflateEncoderStreamId) => Encoder, |
| 30 | + streamId: DeflateEncoderStreamId |
| 31 | +) { |
| 32 | + const reportError = (error: RawError) => { |
| 33 | + lifeCycle.notify(LifeCycleEventType.RAW_ERROR_COLLECTED, { error }) |
| 34 | + |
| 35 | + // monitor-until: forever, to keep an eye on the errors reported to customers |
| 36 | + addTelemetryDebug('Error reported to customer', { 'error.message': error.message }) |
| 37 | + } |
| 38 | + |
| 39 | + const httpRequest = createHttpRequest( |
| 40 | + [configuration.profilingEndpointBuilder], |
| 41 | + configuration.batchBytesLimit, |
| 42 | + reportError |
| 43 | + ) |
| 44 | + |
| 45 | + const encoder = createEncoder(streamId) |
| 46 | + |
| 47 | + return { |
| 48 | + async send({ event, ...attachments }: T) { |
| 49 | + const formData = new FormData() |
| 50 | + const serializedEvent = jsonStringify(event) |
| 51 | + |
| 52 | + if (!serializedEvent) { |
| 53 | + throw new Error('Failed to serialize event') |
| 54 | + } |
| 55 | + |
| 56 | + formData.append('event', new Blob([serializedEvent], { type: 'application/json' }), 'event.json') |
| 57 | + |
| 58 | + let bytesCount = serializedEvent.length |
| 59 | + |
| 60 | + for (const [key, value] of objectEntries(attachments as Record<string, Context>)) { |
| 61 | + const serializedValue = jsonStringify(value) |
| 62 | + |
| 63 | + if (!serializedValue) { |
| 64 | + throw new Error('Failed to serialize attachment') |
| 65 | + } |
| 66 | + |
| 67 | + const result = await encode(encoder, serializedValue) |
| 68 | + |
| 69 | + bytesCount += result.outputBytesCount |
| 70 | + formData.append(key, new Blob([result.output]), key) |
| 71 | + } |
| 72 | + |
| 73 | + httpRequest.send({ |
| 74 | + data: formData, |
| 75 | + bytesCount, |
| 76 | + }) |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function encode<T extends string | Uint8ArrayBuffer>(encoder: Encoder<T>, data: string): Promise<EncoderResult<T>> { |
| 82 | + return new Promise((resolve) => { |
| 83 | + encoder.write(data) |
| 84 | + |
| 85 | + encoder.finish((encoderResult) => { |
| 86 | + resolve(encoderResult) |
| 87 | + }) |
| 88 | + }) |
| 89 | +} |
0 commit comments