File size: 2,883 Bytes
fea495a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from 'fs/promises';
import path from 'path';
import url from 'url';
import dataUriToBuffer from 'next/dist/compiled/data-uri-to-buffer';
function getSourceMapUrl(fileContents) {
    const regex = /\/\/[#@] ?sourceMappingURL=([^\s'"]+)\s*$/gm;
    let match = null;
    for(;;){
        let next = regex.exec(fileContents);
        if (next == null) {
            break;
        }
        match = next;
    }
    if (!(match && match[1])) {
        return null;
    }
    return match[1].toString();
}
export async function getSourceMapFromFile(filename) {
    filename = filename.startsWith('file://') ? url.fileURLToPath(filename) : filename;
    let fileContents;
    try {
        fileContents = await fs.readFile(filename, 'utf-8');
    } catch (error) {
        throw Object.defineProperty(new Error(`Failed to read file contents of ${filename}.`, {
            cause: error
        }), "__NEXT_ERROR_CODE", {
            value: "E466",
            enumerable: false,
            configurable: true
        });
    }
    const sourceUrl = getSourceMapUrl(fileContents);
    if (!sourceUrl) {
        return undefined;
    }
    if (sourceUrl.startsWith('data:')) {
        let buffer;
        try {
            buffer = dataUriToBuffer(sourceUrl);
        } catch (error) {
            throw Object.defineProperty(new Error(`Failed to parse source map URL for ${filename}.`, {
                cause: error
            }), "__NEXT_ERROR_CODE", {
                value: "E199",
                enumerable: false,
                configurable: true
            });
        }
        if (buffer.type !== 'application/json') {
            throw Object.defineProperty(new Error(`Unknown source map type for ${filename}: ${buffer.typeFull}.`), "__NEXT_ERROR_CODE", {
                value: "E113",
                enumerable: false,
                configurable: true
            });
        }
        try {
            return JSON.parse(buffer.toString());
        } catch (error) {
            throw Object.defineProperty(new Error(`Failed to parse source map for ${filename}.`, {
                cause: error
            }), "__NEXT_ERROR_CODE", {
                value: "E318",
                enumerable: false,
                configurable: true
            });
        }
    }
    const sourceMapFilename = path.resolve(path.dirname(filename), decodeURIComponent(sourceUrl));
    try {
        const sourceMapContents = await fs.readFile(sourceMapFilename, 'utf-8');
        return JSON.parse(sourceMapContents.toString());
    } catch (error) {
        throw Object.defineProperty(new Error(`Failed to parse source map ${sourceMapFilename}.`, {
            cause: error
        }), "__NEXT_ERROR_CODE", {
            value: "E220",
            enumerable: false,
            configurable: true
        });
    }
}

//# sourceMappingURL=get-source-map-from-file.js.map