Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/core/src/helpers/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// Copyright 2019-Present Datadog, Inc.

import retry from 'async-retry';
import { Readable } from 'stream';
import type { RequestInit } from 'undici-types';
import type { Gzip } from 'zlib';
import { createGzip } from 'zlib';

import type { RequestOpts } from '../types';

Expand Down Expand Up @@ -61,7 +58,7 @@ export const getOriginHeaders = (opts: { bundler: string; plugin: string; versio
};

export type RequestData = {
data: Gzip | Readable;
data: ReadableStream;
headers: Record<string, string>;
};

Expand All @@ -78,8 +75,12 @@ export const createRequestData = async (options: {
// Serialize FormData through Request to get a streaming body
// and auto-generated headers (boundary) that we can forward while piping through gzip.
const req = new Request('fake://url', { method: 'POST', body: form });
const formStream = Readable.fromWeb(req.body!);
const data = zip ? formStream.pipe(createGzip()) : formStream;

// Use Web Streams pipeThrough instead of Node.js pipe() to keep the pipeline lazy.
// Node.js pipe() immediately puts the source into flowing mode (starts reading blobs
// via process.nextTick), which races with cleanup of file-backed blobs after the
// request completes. Web Streams only start reading when the output is consumed.
const data = zip ? req.body!.pipeThrough(new CompressionStream('gzip')) : req.body!;

const headers = {
'Content-Encoding': zip ? 'gzip' : 'multipart/form-data',
Expand Down
20 changes: 3 additions & 17 deletions packages/plugins/error-tracking/src/sourcemaps/sender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
getSourcemapsConfiguration,
addFixtureFiles,
} from '@dd/tests/_jest/helpers/mocks';
import { type Stream } from 'stream';
import { unzipSync } from 'zlib';

jest.mock('@dd/core/helpers/fs', () => {
const original = jest.requireActual('@dd/core/helpers/fs');
Expand All @@ -42,19 +40,6 @@ jest.mock('@dd/core/helpers/request', () => {

const doRequestMock = jest.mocked(doRequest);

function readFully(stream: Stream): Promise<Buffer> {
const chunks: any[] = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(chunk));

stream.on('end', () => {
resolve(Buffer.concat(chunks));
});

stream.on('error', reject);
});
}

const contextMock = getContextMock();
const uploadContextMock = {
apiKey: contextMock.auth.apiKey,
Expand Down Expand Up @@ -106,8 +91,9 @@ describe('Error Tracking Plugin Sourcemaps', () => {
const payload = getPayloadMock();

const { data, headers } = await getData(payload)();
const zippedData = await readFully(data);
const unzippedData = unzipSync(zippedData).toString('utf-8');
const unzippedData = await new Response(
data.pipeThrough(new DecompressionStream('gzip')),
).text();
const dataLines = unzippedData.split(/[\r\n]/g).filter(Boolean);
const boundary = headers['content-type']
.split('boundary=')
Expand Down
Loading