2018-04-28 03:17:21 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
const { SourceMapConsumer } = require('source-map')
|
2018-07-03 00:56:51 +02:00
|
|
|
const path = require('path')
|
2019-03-11 17:55:43 +01:00
|
|
|
const pify = require('pify')
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2019-03-11 17:55:43 +01:00
|
|
|
const fsAsync = pify(fs)
|
|
|
|
|
2018-04-28 03:17:21 +02:00
|
|
|
//
|
|
|
|
// Utility to help check if sourcemaps are working
|
|
|
|
//
|
|
|
|
// searches `dist/chrome/inpage.js` for "new Error" statements
|
|
|
|
// and prints their source lines using the sourcemaps.
|
|
|
|
// if not working it may error or print minified garbage
|
|
|
|
//
|
|
|
|
|
2020-08-04 17:09:02 +02:00
|
|
|
start().catch((error) => {
|
|
|
|
console.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2018-04-28 03:17:21 +02:00
|
|
|
|
2018-07-03 00:56:51 +02:00
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
async function start () {
|
2020-08-04 18:45:03 +02:00
|
|
|
const targetFiles = [
|
|
|
|
`background.js`,
|
|
|
|
// `bg-libs`, skipped because source maps are invalid due to browserify bug: https://github.com/browserify/browserify/issues/1971
|
|
|
|
// `contentscript.js`, skipped because the validator is erroneously sampling the inlined `inpage.js` script
|
|
|
|
`inpage.js`,
|
|
|
|
'phishing-detect.js',
|
|
|
|
`ui.js`,
|
|
|
|
// `ui-libs.js`, skipped because source maps are invalid due to browserify bug: https://github.com/browserify/browserify/issues/1971
|
|
|
|
]
|
2020-08-04 17:09:02 +02:00
|
|
|
let valid = true
|
|
|
|
|
2019-03-11 17:55:43 +01:00
|
|
|
for (const buildName of targetFiles) {
|
2020-08-04 17:09:02 +02:00
|
|
|
const fileIsValid = await validateSourcemapForFile({ buildName })
|
|
|
|
valid = valid && fileIsValid
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
process.exit(1)
|
2019-03-11 17:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
2020-08-12 22:50:29 +02:00
|
|
|
} catch (_) {
|
|
|
|
// empty
|
|
|
|
}
|
2019-03-11 17:55:43 +01:00
|
|
|
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')
|
2020-08-12 22:50:29 +02:00
|
|
|
} catch (_) {
|
|
|
|
// empty
|
|
|
|
}
|
2019-03-11 17:55:43 +01:00
|
|
|
// attempt to load in dev mode
|
|
|
|
try {
|
|
|
|
const filePath = path.join(__dirname, `/../dist/${platform}/`, `${buildName}.map`)
|
|
|
|
rawSourceMap = await fsAsync.readFile(filePath, 'utf8')
|
2020-08-12 22:50:29 +02:00
|
|
|
} catch (_) {
|
|
|
|
// empty
|
|
|
|
}
|
2019-03-11 17:55:43 +01:00
|
|
|
if (!rawSourceMap) {
|
|
|
|
throw new Error(`SourcemapValidator - failed to load sourcemaps for "${buildName}"`)
|
|
|
|
}
|
|
|
|
|
2018-04-28 03:17:21 +02:00
|
|
|
const consumer = await new SourceMapConsumer(rawSourceMap)
|
|
|
|
|
2019-03-11 17:55:43 +01:00
|
|
|
const hasContentsOfAllSources = consumer.hasContentsOfAllSources()
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!hasContentsOfAllSources) {
|
|
|
|
console.warn('SourcemapValidator - missing content of some sources...')
|
|
|
|
}
|
2019-03-11 17:55:43 +01:00
|
|
|
|
|
|
|
console.log(` sampling from ${consumer.sources.length} files`)
|
|
|
|
let sampleCount = 0
|
2020-08-04 17:09:02 +02:00
|
|
|
let valid = true
|
2019-03-11 17:55:43 +01:00
|
|
|
|
|
|
|
const buildLines = rawBuild.split('\n')
|
|
|
|
const targetString = 'new Error'
|
|
|
|
// const targetString = 'null'
|
2020-02-15 21:34:12 +01:00
|
|
|
const matchesPerLine = buildLines.map((line) => indicesOf(targetString, line))
|
2019-03-11 17:55:43 +01:00
|
|
|
matchesPerLine.forEach((matchIndices, lineIndex) => {
|
|
|
|
matchIndices.forEach((matchColumn) => {
|
2020-08-13 01:06:44 +02:00
|
|
|
sampleCount += 1
|
2019-03-11 17:55:43 +01:00
|
|
|
const position = { line: lineIndex + 1, column: matchColumn }
|
2018-04-28 03:17:21 +02:00
|
|
|
const result = consumer.originalPositionFor(position)
|
2019-03-11 17:55:43 +01:00
|
|
|
// warn if source content is missing
|
|
|
|
if (!result.source) {
|
2020-08-04 17:09:02 +02:00
|
|
|
valid = false
|
2019-03-11 17:55:43 +01:00
|
|
|
console.warn(`!! missing source for position: ${JSON.stringify(position)}`)
|
|
|
|
// 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
|
|
|
|
}
|
2018-04-28 03:17:21 +02:00
|
|
|
const sourceContent = consumer.sourceContentFor(result.source)
|
|
|
|
const sourceLines = sourceContent.split('\n')
|
2018-07-03 00:49:33 +02:00
|
|
|
const line = sourceLines[result.line - 1]
|
2019-03-11 17:55:43 +01:00
|
|
|
// this sometimes includes the whole line though we tried to match somewhere in the middle
|
|
|
|
const portion = line.slice(result.column)
|
|
|
|
const isMaybeValid = portion.includes(targetString)
|
|
|
|
if (!isMaybeValid) {
|
2020-08-04 17:09:02 +02:00
|
|
|
valid = false
|
2020-08-04 17:55:11 +02:00
|
|
|
console.error(`Sourcemap seems invalid:\n${getFencedCode(result.source, line)}`)
|
2019-03-11 17:55:43 +01:00
|
|
|
}
|
2018-04-28 03:17:21 +02:00
|
|
|
})
|
|
|
|
})
|
2019-03-11 17:55:43 +01:00
|
|
|
console.log(` checked ${sampleCount} samples`)
|
2020-08-04 17:09:02 +02:00
|
|
|
return valid
|
2018-04-28 03:17:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 17:55:11 +02:00
|
|
|
const CODE_FENCE_LENGTH = 80
|
|
|
|
const TITLE_PADDING_LENGTH = 1
|
|
|
|
|
|
|
|
function getFencedCode (filename, code) {
|
|
|
|
const title = `${' '.repeat(TITLE_PADDING_LENGTH)}${filename}${' '.repeat(TITLE_PADDING_LENGTH)}`
|
|
|
|
const openingFenceLength = Math.max(CODE_FENCE_LENGTH - (filename.length + (TITLE_PADDING_LENGTH * 2)), 0)
|
|
|
|
const startOpeningFenceLength = Math.floor(openingFenceLength / 2)
|
|
|
|
const endOpeningFenceLength = Math.ceil(openingFenceLength / 2)
|
|
|
|
const openingFence = `${'='.repeat(startOpeningFenceLength)}${title}${'='.repeat(endOpeningFenceLength)}`
|
|
|
|
const closingFence = '='.repeat(CODE_FENCE_LENGTH)
|
|
|
|
|
|
|
|
return `${openingFence}\n${code}\n${closingFence}\n`
|
|
|
|
}
|
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
function indicesOf (substring, string) {
|
2019-12-03 15:52:01 +01:00
|
|
|
const a = []
|
|
|
|
let i = -1
|
2019-11-20 01:03:20 +01:00
|
|
|
while ((i = string.indexOf(substring, i + 1)) >= 0) {
|
|
|
|
a.push(i)
|
|
|
|
}
|
2018-07-03 00:49:33 +02:00
|
|
|
return a
|
2018-04-28 03:17:21 +02:00
|
|
|
}
|