-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsender.ts
More file actions
281 lines (245 loc) · 9.55 KB
/
Copy pathsender.ts
File metadata and controls
281 lines (245 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.
import { getDDEnvValue } from '@dd/core/helpers/env';
import { getFile } from '@dd/core/helpers/fs';
import {
createRequestData,
doRequest,
getOriginHeaders,
NB_RETRIES,
type RequestData,
} from '@dd/core/helpers/request';
import { formatDuration, prettyObject } from '@dd/core/helpers/strings';
import type { Logger, Metric, RepositoryData } from '@dd/core/types';
import chalk from 'chalk';
import PQueue from 'p-queue';
import path from 'path';
import type { SourcemapsOptionsWithDefaults, Sourcemap } from '../types';
import type { Metadata, MultipartFileValue, Payload } from './payload';
import { getPayload } from './payload';
import {
addSourcemapUploadMetrics,
createSourcemapUploadMetrics,
recordSourcemapUploadFailure,
recordSourcemapUploadRetry,
} from './upload-metrics';
const green = chalk.green.bold;
const yellow = chalk.yellow.bold;
const red = chalk.red.bold;
type FileMetadata = {
sourcemap: string;
file: string;
};
export const SOURCEMAPS_API_SUBDOMAIN = 'sourcemap-intake';
export const SOURCEMAPS_API_PATH = 'api/v2/srcmap';
export const getIntakeUrl = (site: string) => {
const envIntake = getDDEnvValue('SOURCEMAP_INTAKE_URL');
return envIntake || `https://${SOURCEMAPS_API_SUBDOMAIN}.${site}/${SOURCEMAPS_API_PATH}`;
};
// Use a function to get new streams for each retry.
export const getData =
(payload: Payload, defaultHeaders: Record<string, string> = {}) =>
async (): Promise<RequestData> => {
return createRequestData({
getForm: async () => {
const form = new FormData();
for (const [key, content] of payload.content) {
const value =
content.type === 'file'
? // eslint-disable-next-line no-await-in-loop
await getFile(content.path, content.options)
: new Blob([content.value], { type: content.options.contentType });
form.append(key, value, content.options.filename);
}
return form;
},
defaultHeaders,
zip: true,
});
};
export type UploadContext = {
addMetric: (metric: Metric) => void;
apiKey?: string;
bundlerName: string;
sendMetrics?: boolean;
site: string;
version: string;
outDir: string;
};
export type DebugIdsContext = {
// Keyed by chunk relative path (forward-slashed), filled in by the RUM plugin.
debugIds: Map<string, string>;
};
// Bundlers report chunk paths with forward slashes regardless of OS.
const toPosixPath = (filePath: string) => filePath.split(path.sep).join('/');
export const upload = async (
payloads: Payload[],
options: SourcemapsOptionsWithDefaults,
context: UploadContext,
log: Logger,
) => {
const errors: { metadata?: FileMetadata; error: Error }[] = [];
const warnings: string[] = [];
if (!context.apiKey) {
errors.push({ error: new Error('No authentication token provided') });
return { errors, warnings };
}
if (payloads.length === 0) {
warnings.push('No sourcemaps to upload');
return { errors, warnings };
}
const queueTimer = log.time('Queue uploads');
// @ts-expect-error PQueue's default isn't typed.
const Queue = PQueue.default ? PQueue.default : PQueue;
const queue = new Queue({ concurrency: options.maxConcurrency });
const intakeUrl = getIntakeUrl(context.site);
const defaultHeaders = getOriginHeaders({
bundler: context.bundlerName,
plugin: 'sourcemaps',
version: context.version,
});
const uploadMetrics = createSourcemapUploadMetrics(options);
// Show a pretty summary of the configuration.
const configurationString = prettyObject({
...options,
intakeUrl,
outDir: context.outDir,
defaultHeaders: `\n${JSON.stringify(defaultHeaders, null, 2)}`,
});
const summary = `\nUploading ${green(payloads.length.toString())} sourcemaps with configuration:\n${configurationString}`;
log.debug(summary);
const addPromises = [];
for (const payload of payloads) {
const metadata = {
sourcemap: (payload.content.get('source_map') as MultipartFileValue)?.path.replace(
context.outDir,
'.',
),
file: (payload.content.get('minified_file') as MultipartFileValue)?.path.replace(
context.outDir,
'.',
),
};
addPromises.push(
queue.add(async () => {
try {
await doRequest({
auth: { apiKey: context.apiKey },
url: intakeUrl,
method: 'POST',
getData: getData(payload, defaultHeaders),
// On retry we store the error as a warning.
onRetry: (error: Error, attempt: number) => {
recordSourcemapUploadRetry(uploadMetrics, error, attempt);
const warningMessage = `Failed to upload ${yellow(metadata.sourcemap)} | ${yellow(metadata.file)}:\n ${error.message}\nRetrying ${attempt}/${NB_RETRIES}`;
// This will be logged at the end of the process.
warnings.push(warningMessage);
log.debug(warningMessage);
},
});
} catch (e: any) {
recordSourcemapUploadFailure(uploadMetrics, e);
errors.push({ metadata, error: e });
// Depending on the configuration we throw or not.
if (options.bailOnError === true) {
throw e;
}
}
}),
);
}
queueTimer.end();
log.debug(`Queued ${green(payloads.length.toString())} uploads.`);
try {
await Promise.all(addPromises);
await queue.onIdle();
} finally {
addSourcemapUploadMetrics(uploadMetrics, context);
}
return { warnings, errors };
};
export type SourcemapsSenderContext = UploadContext &
DebugIdsContext & {
git?: RepositoryData;
};
export const sendSourcemaps = async (
sourcemaps: Sourcemap[],
options: SourcemapsOptionsWithDefaults,
context: SourcemapsSenderContext,
log: Logger,
) => {
const start = Date.now();
const prefix = options.minifiedPathPrefix;
const metadata: Metadata = {
git_repository_url: context.git?.remote,
git_commit_sha: context.git?.hash,
plugin_version: context.version,
project_path: context.outDir,
service: options.service,
type: 'js_sourcemap',
version: options.releaseVersion,
};
const payloadsTimer = log.time('Compute payloads');
const payloads = await Promise.all(
sourcemaps.map((sourcemap) => {
const debugId = context.debugIds.get(toPosixPath(sourcemap.relativePath));
return getPayload(sourcemap, metadata, prefix, context.git, debugId);
}),
);
payloadsTimer.end();
const errors = payloads.map((payload) => payload.errors).flat();
const warnings = payloads.map((payload) => payload.warnings).flat();
if (warnings.length > 0) {
log.warn(`Warnings while preparing payloads:\n - ${warnings.join('\n - ')}`);
}
if (errors.length > 0) {
const errorMsg = `Failed to prepare payloads, aborting upload :\n - ${errors.join('\n - ')}`;
log.error(errorMsg);
// Depending on the configuration we throw or not.
if (options.bailOnError === true) {
throw new Error(errorMsg);
}
return;
}
const uploadTimer = log.time('Upload sourcemaps');
const { errors: uploadErrors, warnings: uploadWarnings } = await upload(
payloads,
options,
{
apiKey: context.apiKey,
bundlerName: context.bundlerName,
version: context.version,
outDir: context.outDir,
site: context.site,
sendMetrics: context.sendMetrics,
addMetric: context.addMetric,
},
log,
);
uploadTimer.end();
log.debug(
`Done uploading ${green(`${sourcemaps.length - uploadErrors.length}/${sourcemaps.length}`)} sourcemaps in ${green(formatDuration(Date.now() - start))}.`,
);
if (uploadErrors.length > 0) {
const listOfErrors = ` - ${uploadErrors
.map(({ metadata: fileMetadata, error }) => {
const errorToPrint = error.cause || error.stack || error.message;
if (fileMetadata) {
return `${red(fileMetadata.file)} | ${red(fileMetadata.sourcemap)} :\n${errorToPrint}`;
}
return errorToPrint;
})
.join('\n - ')}`;
const errorMsg = `Failed to upload some sourcemaps:\n${listOfErrors}`;
log.error(errorMsg);
// Depending on the configuration we throw or not.
// This should not be reached as we'd have thrown earlier.
if (options.bailOnError === true) {
throw new Error(errorMsg);
}
}
if (uploadWarnings.length > 0) {
log.warn(`Warnings while uploading sourcemaps:\n - ${uploadWarnings.join('\n - ')}`);
}
};