Skip to content

Commit

Permalink
Improve code quality by increasing code coverage: lib/source-map-from…
Browse files Browse the repository at this point in the history
…-file.js (bcoe#453)

refactor:  lib/source-map-from-file.js to improve code coverage (bcoe#453)
refactor: exposed source-map-from-file.js function to write test cases (bcoe#453)
test: Added two test case cases to cover error handling for improper formating (bcoe#453)
  • Loading branch information
mcknasty committed Jul 29, 2023
1 parent a13584d commit 6adda9f
Show file tree
Hide file tree
Showing 6 changed files with 993 additions and 639 deletions.
2 changes: 1 addition & 1 deletion lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ try {
const { readdirSync, readFileSync, statSync } = require('fs')
const { isAbsolute, resolve, extname } = require('path')
const { pathToFileURL, fileURLToPath } = require('url')
const getSourceMapFromFile = require('./source-map-from-file')
const { getSourceMapFromFile } = require('./source-map-from-file')
// TODO: switch back to @c88/v8-coverage once patch is landed.
const v8toIstanbul = require('v8-to-istanbul')
const isCjsEsmBridgeCov = require('./is-cjs-esm-bridge')
Expand Down
27 changes: 26 additions & 1 deletion lib/source-map-from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ function getSourceMapFromFile (filename) {
}

function dataFromUrl (sourceURL, sourceMappingURL) {
const nodeMajorVersion = Number(process.version.slice(1).split('.')[0])
/* c8 ignore next 1 */
if (nodeMajorVersion === 12) {
/* c8 ignore next 14 */
}
try {
const url = new URL(sourceMappingURL)
switch (url.protocol) {
Expand All @@ -67,6 +72,11 @@ function dataFromUrl (sourceURL, sourceMappingURL) {
}

function sourceMapFromFile (mapURL) {
const nodeMajorVersion = Number(process.version.slice(1).split('.')[0])
/* c8 ignore next 1 */
if (nodeMajorVersion === 12) {
/* c8 ignore next 8 */
}
try {
const content = readFileSync(fileURLToPath(mapURL), 'utf8')
return JSON.parse(content)
Expand All @@ -79,12 +89,22 @@ function sourceMapFromFile (mapURL) {
// data:[<mediatype>][;base64],<data> see:
// https://tools.ietf.org/html/rfc2397#section-2
function sourceMapFromDataUrl (url) {
const nodeMajorVersion = Number(process.version.slice(1).split('.')[0])
const { 0: format, 1: data } = url.split(',')
const splitFormat = format.split(';')
const contentType = splitFormat[0]
// always evaluates to true
const base64 = splitFormat[splitFormat.length - 1] === 'base64'
if (contentType === 'application/json') {
// the data variable is never a json string in the test cases
// the upstream branch in node.js still has this logic
// https://github.com/nodejs/node/blob/v19.x/lib/internal/source_map/source_map_cache.js#L241
/* c8 ignore next 1 */
const decodedData = base64 ? Buffer.from(data, 'base64').toString('utf8') : data
/* c8 ignore next 1 */
if (nodeMajorVersion === 12) {
/* c8 ignore next 11 */
}
try {
return JSON.parse(decodedData)
} catch (err) {
Expand All @@ -97,4 +117,9 @@ function sourceMapFromDataUrl (url) {
}
}

module.exports = getSourceMapFromFile
module.exports = {
getSourceMapFromFile,
dataFromUrl,
sourceMapFromFile,
sourceMapFromDataUrl
}

0 comments on commit 6adda9f

Please sign in to comment.