|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// See https://sourcemaps.info/spec.html for SourceMap V3 specification. |
| 4 | +const { Buffer } = require('buffer'); |
| 5 | +const debug = require('internal/util/debuglog').debuglog('source_map'); |
| 6 | +const { dirname, resolve } = require('path'); |
| 7 | +const fs = require('fs'); |
| 8 | +const { |
| 9 | + normalizeReferrerURL, |
| 10 | +} = require('internal/modules/cjs/helpers'); |
| 11 | +const { JSON, Object } = primordials; |
| 12 | +// For cjs, since Module._cache is exposed to users, we use a WeakMap |
| 13 | +// keyed on module, facilitating garbage collection. |
| 14 | +const cjsSourceMapCache = new WeakMap(); |
| 15 | +// The esm cache is not exposed to users, so we can use a Map keyed |
| 16 | +// on filenames. |
| 17 | +const esmSourceMapCache = new Map(); |
| 18 | +const { fileURLToPath, URL } = require('url'); |
| 19 | + |
| 20 | +function maybeCacheSourceMap(filename, content, cjsModuleInstance) { |
| 21 | + if (!process.env.NODE_V8_COVERAGE) return; |
| 22 | + |
| 23 | + let basePath; |
| 24 | + try { |
| 25 | + filename = normalizeReferrerURL(filename); |
| 26 | + basePath = dirname(fileURLToPath(filename)); |
| 27 | + } catch (err) { |
| 28 | + // This is most likely an [eval]-wrapper, which is currently not |
| 29 | + // supported. |
| 30 | + debug(err.stack); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const match = content.match(/\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/); |
| 35 | + if (match) { |
| 36 | + if (cjsModuleInstance) { |
| 37 | + cjsSourceMapCache.set(cjsModuleInstance, { |
| 38 | + url: match.groups.sourceMappingURL, |
| 39 | + data: dataFromUrl(basePath, match.groups.sourceMappingURL) |
| 40 | + }); |
| 41 | + } else { |
| 42 | + // If there is no cjsModuleInstance assume we are in a |
| 43 | + // "modules/esm" context. |
| 44 | + esmSourceMapCache.set(filename, { |
| 45 | + url: match.groups.sourceMappingURL, |
| 46 | + data: dataFromUrl(basePath, match.groups.sourceMappingURL) |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function dataFromUrl(basePath, sourceMappingURL) { |
| 53 | + try { |
| 54 | + const url = new URL(sourceMappingURL); |
| 55 | + switch (url.protocol) { |
| 56 | + case 'data:': |
| 57 | + return sourceMapFromDataUrl(basePath, url.pathname); |
| 58 | + default: |
| 59 | + debug(`unknown protocol ${url.protocol}`); |
| 60 | + return null; |
| 61 | + } |
| 62 | + } catch (err) { |
| 63 | + debug(err.stack); |
| 64 | + // If no scheme is present, we assume we are dealing with a file path. |
| 65 | + const sourceMapFile = resolve(basePath, sourceMappingURL); |
| 66 | + return sourceMapFromFile(sourceMapFile); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function sourceMapFromFile(sourceMapFile) { |
| 71 | + try { |
| 72 | + const content = fs.readFileSync(sourceMapFile, 'utf8'); |
| 73 | + const data = JSON.parse(content); |
| 74 | + return sourcesToAbsolute(dirname(sourceMapFile), data); |
| 75 | + } catch (err) { |
| 76 | + debug(err.stack); |
| 77 | + return null; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// data:[<mediatype>][;base64],<data> see: |
| 82 | +// https://tools.ietf.org/html/rfc2397#section-2 |
| 83 | +function sourceMapFromDataUrl(basePath, url) { |
| 84 | + const [format, data] = url.split(','); |
| 85 | + const splitFormat = format.split(';'); |
| 86 | + const contentType = splitFormat[0]; |
| 87 | + const base64 = splitFormat[splitFormat.length - 1] === 'base64'; |
| 88 | + if (contentType === 'application/json') { |
| 89 | + const decodedData = base64 ? |
| 90 | + Buffer.from(data, 'base64').toString('utf8') : data; |
| 91 | + try { |
| 92 | + const parsedData = JSON.parse(decodedData); |
| 93 | + return sourcesToAbsolute(basePath, parsedData); |
| 94 | + } catch (err) { |
| 95 | + debug(err.stack); |
| 96 | + return null; |
| 97 | + } |
| 98 | + } else { |
| 99 | + debug(`unknown content-type ${contentType}`); |
| 100 | + return null; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// If the sources are not absolute URLs after prepending of the "sourceRoot", |
| 105 | +// the sources are resolved relative to the SourceMap (like resolving script |
| 106 | +// src in a html document). |
| 107 | +function sourcesToAbsolute(base, data) { |
| 108 | + data.sources = data.sources.map((source) => { |
| 109 | + source = (data.sourceRoot || '') + source; |
| 110 | + if (!/^[\\/]/.test(source[0])) { |
| 111 | + source = resolve(base, source); |
| 112 | + } |
| 113 | + if (!source.startsWith('file://')) source = `file://${source}`; |
| 114 | + return source; |
| 115 | + }); |
| 116 | + // The sources array is now resolved to absolute URLs, sourceRoot should |
| 117 | + // be updated to noop. |
| 118 | + data.sourceRoot = ''; |
| 119 | + return data; |
| 120 | +} |
| 121 | + |
| 122 | +function sourceMapCacheToObject() { |
| 123 | + const obj = Object.create(null); |
| 124 | + |
| 125 | + for (const [k, v] of esmSourceMapCache) { |
| 126 | + obj[k] = v; |
| 127 | + } |
| 128 | + appendCJSCache(obj); |
| 129 | + |
| 130 | + if (Object.keys(obj).length === 0) { |
| 131 | + return undefined; |
| 132 | + } else { |
| 133 | + return obj; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Since WeakMap can't be iterated over, we use Module._cache's |
| 138 | +// keys to facilitate Source Map serialization. |
| 139 | +function appendCJSCache(obj) { |
| 140 | + const { Module } = require('internal/modules/cjs/loader'); |
| 141 | + Object.keys(Module._cache).forEach((key) => { |
| 142 | + const value = cjsSourceMapCache.get(Module._cache[key]); |
| 143 | + if (value) { |
| 144 | + obj[`file://${key}`] = value; |
| 145 | + } |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +module.exports = { |
| 150 | + sourceMapCacheToObject, |
| 151 | + maybeCacheSourceMap |
| 152 | +}; |
0 commit comments