Repository navigation
Expand file tree
/
Copy pathvalidation.ts
More file actions
58 lines (51 loc) · 2.03 KB
/
Copy pathvalidation.ts
File metadata and controls
58 lines (51 loc) · 2.03 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
import type {Sourcemap} from './interfaces'
import type {Writable} from 'stream'
import {checkFile} from '@datadog/datadog-ci-base/helpers/validation'
import {renderMinifiedPathPrefixMisusage} from './renderer'
import {extractRepeatedPath} from './utils'
export class InvalidPayload extends Error {
public reason: string
constructor(reason: string, message?: string) {
super(message)
this.reason = reason
}
}
export const validatePayload = (sourcemap: Sourcemap, stdout: Writable, debugIdRequired = false) => {
// Check existence of sourcemap file
const sourcemapCheck = checkFile(sourcemap.sourcemapPath)
if (!sourcemapCheck.exists) {
// This case should not happen as all collected sourcemaps should point to correct files
throw new InvalidPayload('missing_sourcemap', `Skipping missing sourcemap (${sourcemap.sourcemapPath})`)
}
if (sourcemapCheck.empty) {
throw new InvalidPayload('empty_sourcemap', `Skipping empty sourcemap (${sourcemap.sourcemapPath})`)
}
// Check existence of minified file
const minifiedFileCheck = checkFile(sourcemap.minifiedFilePath)
if (!minifiedFileCheck.exists) {
throw new InvalidPayload(
'missing_js',
`Missing corresponding JS file for sourcemap (${sourcemap.minifiedFilePath})`
)
}
if (minifiedFileCheck.empty) {
throw new InvalidPayload(
'empty_js',
`Skipping sourcemap (${sourcemap.sourcemapPath}) due to ${sourcemap.minifiedFilePath} being empty`
)
}
// Check for a debug ID when uploading in debug-ID mode.
if (debugIdRequired && sourcemap.debugId === undefined) {
throw new InvalidPayload(
'missing_debug_id',
`Skipping sourcemap ${sourcemap.sourcemapPath} because no debug ID was found in ${sourcemap.minifiedFilePath}`
)
}
// Check for --minified-path-prefix flag misuages.
if (sourcemap.minifiedPathPrefix) {
const repeated = extractRepeatedPath(sourcemap.minifiedPathPrefix, sourcemap.relativePath)
if (repeated) {
stdout.write(renderMinifiedPathPrefixMisusage(sourcemap, repeated))
}
}
}