1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

development - enhancement for sourcemap validator tool (#6277)

This commit is contained in:
kumavis 2019-03-12 00:55:43 +08:00 committed by Dan Finlay
parent 20feaa4baf
commit 82959b6b0e

View File

@ -1,6 +1,9 @@
const fs = require('fs') const fs = require('fs')
const { SourceMapConsumer } = require('source-map') const { SourceMapConsumer } = require('source-map')
const path = require('path') const path = require('path')
const pify = require('pify')
const fsAsync = pify(fs)
// //
// Utility to help check if sourcemaps are working // Utility to help check if sourcemaps are working
// //
@ -9,39 +12,85 @@ const path = require('path')
// if not working it may error or print minified garbage // if not working it may error or print minified garbage
// //
start() start().catch(console.error)
async function start () { async function start () {
const rawBuild = fs.readFileSync(path.join(__dirname, '/../dist/chrome/', 'inpage.js') const targetFiles = [`inpage.js`, `contentscript.js`, `ui.js`, `background.js`]
, 'utf8') for (const buildName of targetFiles) {
const rawSourceMap = fs.readFileSync(path.join(__dirname, '/../dist/sourcemaps/', 'inpage.js.map'), 'utf8') await validateSourcemapForFile({ buildName })
}
}
async function validateSourcemapForFile ({ buildName }) {
console.log(`build "${buildName}"`)
const platform = `chrome`
// load build and sourcemaps
let rawBuild
try {
const filePath = path.join(__dirname, `/../dist/${platform}/`, `${buildName}`)
rawBuild = await fsAsync.readFile(filePath, 'utf8')
} catch (err) {}
if (!rawBuild) {
throw new Error(`SourcemapValidator - failed to load source file for "${buildName}"`)
}
// attempt to load in dist mode
let rawSourceMap
try {
const filePath = path.join(__dirname, `/../dist/sourcemaps/`, `${buildName}.map`)
rawSourceMap = await fsAsync.readFile(filePath, 'utf8')
} catch (err) {}
// attempt to load in dev mode
try {
const filePath = path.join(__dirname, `/../dist/${platform}/`, `${buildName}.map`)
rawSourceMap = await fsAsync.readFile(filePath, 'utf8')
} catch (err) {}
if (!rawSourceMap) {
throw new Error(`SourcemapValidator - failed to load sourcemaps for "${buildName}"`)
}
const consumer = await new SourceMapConsumer(rawSourceMap) const consumer = await new SourceMapConsumer(rawSourceMap)
console.log('hasContentsOfAllSources:', consumer.hasContentsOfAllSources(), '\n') const hasContentsOfAllSources = consumer.hasContentsOfAllSources()
console.log('sources:') if (!hasContentsOfAllSources) console.warn('SourcemapValidator - missing content of some sources...')
consumer.sources.map((sourcePath) => console.log(sourcePath))
console.log('\nexamining "new Error" statements:\n') console.log(` sampling from ${consumer.sources.length} files`)
const sourceLines = rawBuild.split('\n') let sampleCount = 0
sourceLines.map(line => indicesOf('new Error', line))
.forEach((errorIndices, lineIndex) => { const buildLines = rawBuild.split('\n')
// if (errorIndex === null) return console.log('line does not contain "new Error"') const targetString = 'new Error'
errorIndices.forEach((errorIndex) => { // const targetString = 'null'
const position = { line: lineIndex + 1, column: errorIndex } const matchesPerLine = buildLines.map(line => indicesOf(targetString, line))
matchesPerLine.forEach((matchIndices, lineIndex) => {
matchIndices.forEach((matchColumn) => {
sampleCount++
const position = { line: lineIndex + 1, column: matchColumn }
const result = consumer.originalPositionFor(position) const result = consumer.originalPositionFor(position)
if (!result.source) return console.warn(`!! missing source for position: ${position}`) // warn if source content is missing
// filter out deps distributed minified without sourcemaps if (!result.source) {
if (result.source === 'node_modules/browserify/node_modules/browser-pack/_prelude.js') return // minified mess console.warn(`!! missing source for position: ${JSON.stringify(position)}`)
if (result.source === 'node_modules/web3/dist/web3.min.js') return // minified mess // const buildLine = buildLines[position.line - 1]
console.warn(` origin in build:`)
console.warn(` ${buildLines[position.line - 2]}`)
console.warn(`-> ${buildLines[position.line - 1]}`)
console.warn(` ${buildLines[position.line - 0]}`)
return
}
const sourceContent = consumer.sourceContentFor(result.source) const sourceContent = consumer.sourceContentFor(result.source)
const sourceLines = sourceContent.split('\n') const sourceLines = sourceContent.split('\n')
const line = sourceLines[result.line - 1] const line = sourceLines[result.line - 1]
console.log(`\n========================== ${result.source} ====================================\n`) // this sometimes includes the whole line though we tried to match somewhere in the middle
console.log(line) const portion = line.slice(result.column)
console.log(`\n==============================================================================\n`) const isMaybeValid = portion.includes(targetString)
if (!isMaybeValid) {
console.error('Sourcemap seems invalid:')
console.log(`\n========================== ${result.source} ====================================\n`)
console.log(line)
console.log(`\n==============================================================================\n`)
}
}) })
}) })
console.log(` checked ${sampleCount} samples`)
} }
function indicesOf (substring, string) { function indicesOf (substring, string) {