Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(all): handle base64 inline source maps #283

Merged
merged 1 commit into from Feb 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/report.js
Expand Up @@ -185,8 +185,8 @@ class Report {
if (ext === '.js' || ext === '.ts' || ext === '.mjs') {
const stat = statSync(fullPath)
const sourceMap = getSourceMapFromFile(fullPath)
if (sourceMap !== undefined) {
this.sourceMapCache[`file://${fullPath}`] = { data: JSON.parse(readFileSync(sourceMap).toString()) }
if (sourceMap) {
this.sourceMapCache[`file://${fullPath}`] = { data: sourceMap }
}
emptyReports.push({
scriptId: 0,
Expand Down
95 changes: 85 additions & 10 deletions lib/source-map-from-file.js
@@ -1,24 +1,99 @@
const { isAbsolute, join, dirname } = require('path')
/*
* Copyright Node.js contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
// TODO(bcoe): this logic is ported from Node.js' internal source map
// helpers:
// https://github.com/nodejs/node/blob/master/lib/internal/source_map/source_map_cache.js
// we should to upstream and downstream fixes.

const { readFileSync } = require('fs')
const { fileURLToPath, pathToFileURL } = require('url')
const util = require('util')
const debuglog = util.debuglog('c8')

/**
* Extract the sourcemap url from a source file
* reference: https://sourcemaps.info/spec.html
* @param {String} file - compilation target file
* @returns {String} full path to source map file
* @private
*/
function getSourceMapFromFile (file) {
const fileBody = readFileSync(file).toString()
const sourceMapLineRE = /\/\/[#@] ?sourceMappingURL=([^\s'"]+)\s*$/mg
function getSourceMapFromFile (filename) {
const fileBody = readFileSync(filename).toString()
const sourceMapLineRE = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/
const results = fileBody.match(sourceMapLineRE)
if (results !== null) {
const sourceMap = results[results.length - 1].split('=')[1]
if (isAbsolute(sourceMap)) {
return sourceMap.trim()
} else {
const base = dirname(file)
return join(base, sourceMap).trim()
const sourceMappingURL = results.groups.sourceMappingURL
const sourceMap = dataFromUrl(pathToFileURL(filename), sourceMappingURL)
return sourceMap
} else {
return null
}
}

function dataFromUrl (sourceURL, sourceMappingURL) {
try {
const url = new URL(sourceMappingURL)
switch (url.protocol) {
case 'data:':
return sourceMapFromDataUrl(url.pathname)
default:
return null
}
} catch (err) {
debuglog(err)
// If no scheme is present, we assume we are dealing with a file path.
const mapURL = new URL(sourceMappingURL, sourceURL).href
return sourceMapFromFile(mapURL)
}
}

function sourceMapFromFile (mapURL) {
try {
const content = readFileSync(fileURLToPath(mapURL), 'utf8')
return JSON.parse(content)
} catch (err) {
debuglog(err)
return null
}
}

// data:[<mediatype>][;base64],<data> see:
// https://tools.ietf.org/html/rfc2397#section-2
function sourceMapFromDataUrl (url) {
const { 0: format, 1: data } = url.split(',')
const splitFormat = format.split(';')
const contentType = splitFormat[0]
const base64 = splitFormat[splitFormat.length - 1] === 'base64'
if (contentType === 'application/json') {
const decodedData = base64 ? Buffer.from(data, 'base64').toString('utf8') : data
try {
return JSON.parse(decodedData)
} catch (err) {
debuglog(err)
return null
}
} else {
debuglog(`unexpected content-type ${contentType}`)
return null
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/source-maps/inline.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/source-maps/padded.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions test/integration.js_10.snap
Expand Up @@ -139,24 +139,24 @@ hey
--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 75.76 | 58.23 | 66.67 | 75.76 |
All files | 72.64 | 58.23 | 61.11 | 72.64 |
bin | 78.85 | 60 | 66.67 | 78.85 |
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
lib | 80.75 | 51.85 | 83.33 | 80.75 |
lib | 75.95 | 51.85 | 71.43 | 75.95 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 96.13 | 45.45 | 100 | 96.13 | 109-110,118-119,132-133
report.js | 75.35 | 58.82 | 83.33 | 75.35 | ...208,238-239,266-267,273-275
source-map-from-file.js | 44 | 100 | 0 | 44 | 10-23
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 44.44 | 75 | 16.67 | 44.44 |
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
report.js | 93.1 | 71.43 | 50 | 93.1 | 9-10
test/fixtures | 83.33 | 85.71 | 66.67 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (75.76%) does not meet global threshold (101%)
,ERROR: Coverage for lines (72.64%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.23%) does not meet global threshold (82%)
ERROR: Coverage for statements (75.76%) does not meet global threshold (95%)
ERROR: Coverage for statements (72.64%) does not meet global threshold (95%)
"
`;

Expand All @@ -177,8 +177,8 @@ ERROR: Coverage for branches (45.45%) does not meet threshold (82%) for lib/pars
ERROR: Coverage for lines (75.35%) does not meet threshold (101%) for lib/report.js
ERROR: Coverage for branches (58.82%) does not meet threshold (82%) for lib/report.js
ERROR: Coverage for statements (75.35%) does not meet threshold (95%) for lib/report.js
ERROR: Coverage for lines (44%) does not meet threshold (101%) for lib/source-map-from-file.js
ERROR: Coverage for statements (44%) does not meet threshold (95%) for lib/source-map-from-file.js
ERROR: Coverage for lines (45%) does not meet threshold (101%) for lib/source-map-from-file.js
ERROR: Coverage for statements (45%) does not meet threshold (95%) for lib/source-map-from-file.js
ERROR: Coverage for lines (100%) does not meet threshold (101%) for test/fixtures/async.js
ERROR: Coverage for lines (75%) does not meet threshold (101%) for test/fixtures/normal.js
ERROR: Coverage for branches (66.67%) does not meet threshold (82%) for test/fixtures/normal.js
Expand All @@ -189,9 +189,9 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;

exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
",,ERROR: Coverage for lines (75.76%) does not meet global threshold (101%)
",,ERROR: Coverage for lines (72.64%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.23%) does not meet global threshold (82%)
ERROR: Coverage for statements (75.76%) does not meet global threshold (95%)
ERROR: Coverage for statements (72.64%) does not meet global threshold (95%)
"
`;

Expand Down Expand Up @@ -274,14 +274,14 @@ exports[`c8 report generates report from existing temporary files 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 75.76 | 58.23 | 66.67 | 75.76 |
All files | 72.64 | 58.23 | 61.11 | 72.64 |
bin | 78.85 | 60 | 66.67 | 78.85 |
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
lib | 80.75 | 51.85 | 83.33 | 80.75 |
lib | 75.95 | 51.85 | 71.43 | 75.95 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 96.13 | 45.45 | 100 | 96.13 | 109-110,118-119,132-133
report.js | 75.35 | 58.82 | 83.33 | 75.35 | ...208,238-239,266-267,273-275
source-map-from-file.js | 44 | 100 | 0 | 44 | 10-23
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 44.44 | 75 | 16.67 | 44.44 |
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
report.js | 93.1 | 71.43 | 50 | 93.1 | 9-10
Expand All @@ -296,24 +296,24 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 75.76 | 58.23 | 66.67 | 75.76 |
All files | 72.64 | 58.23 | 61.11 | 72.64 |
bin | 78.85 | 60 | 66.67 | 78.85 |
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
lib | 80.75 | 51.85 | 83.33 | 80.75 |
lib | 75.95 | 51.85 | 71.43 | 75.95 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 96.13 | 45.45 | 100 | 96.13 | 109-110,118-119,132-133
report.js | 75.35 | 58.82 | 83.33 | 75.35 | ...208,238-239,266-267,273-275
source-map-from-file.js | 44 | 100 | 0 | 44 | 10-23
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 44.44 | 75 | 16.67 | 44.44 |
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
report.js | 93.1 | 71.43 | 50 | 93.1 | 9-10
test/fixtures | 83.33 | 85.71 | 66.67 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (75.76%) does not meet global threshold (101%)
,ERROR: Coverage for lines (72.64%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.23%) does not meet global threshold (82%)
ERROR: Coverage for statements (75.76%) does not meet global threshold (95%)
ERROR: Coverage for statements (72.64%) does not meet global threshold (95%)
"
`;

Expand Down
11 changes: 8 additions & 3 deletions test/source-map-from-file.js
@@ -1,14 +1,19 @@
/* global describe, it */
const getSourceMapFromFile = require('../lib/source-map-from-file')
const assert = require('assert')
const path = require('path')
const { readFileSync } = require('fs')
describe('source-map-from-file', () => {
it('should parse source maps from compiled targets', () => {
const sourceMap = getSourceMapFromFile('./test/fixtures/all/ts-compiled/main.js')
assert.strictEqual(sourceMap, ['test', 'fixtures', 'all', 'ts-compiled', 'main.js.map'].join(path.sep))
const expected = JSON.parse(readFileSync(require.resolve('./fixtures/all/ts-compiled/main.js.map'), 'utf8'))
assert.deepStrictEqual(sourceMap, expected)
})
it('should handle extra whitespace characters', () => {
const sourceMap = getSourceMapFromFile('./test/fixtures/source-maps/padded.js')
assert.strictEqual(sourceMap, ['test', 'fixtures', 'source-maps', 'padded.js.map'].join(path.sep))
assert.deepStrictEqual(sourceMap, { version: 3 })
})
it('should support base64 encoded inline source maps', () => {
const sourceMap = getSourceMapFromFile('./test/fixtures/source-maps/inline.js')
assert.strictEqual(sourceMap.version, 3)
})
})