Skip to content

Commit

Permalink
Add missing local file error message (#200)
Browse files Browse the repository at this point in the history
* Add missing local file error message

* Update message
  • Loading branch information
cosnomi authored and yhatt committed Jan 27, 2020
1 parent d539831 commit c3c7f23
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,29 @@ export class Converter {
try {
const browser = await Converter.runBrowser()
const page = await browser.newPage()
const tracker = this.trackFailedLocalFileAccess(page)
const {
missingFileSet: missingTracker,
failedFileSet: failedTracker,
} = this.trackFailedLocalFileAccess(page)

try {
return await process(page, uri)
} finally {
if (tracker.size > 0) {
if (missingTracker.size > 0) {
warn(
`${missingTracker.size > 1 ? 'Some of t' : 'T'}he local file${
missingTracker.size > 1 ? 's are' : ' is'
} missing and will be ignored. Make sure the file path${
missingTracker.size > 1 ? 's are' : ' is'
} correct.`
)
}
if (failedTracker.size > 0) {
warn(
`Marp CLI has detected accessing to local file${
tracker.size > 1 ? 's' : ''
failedTracker.size > 1 ? 's' : ''
}. ${
tracker.size > 1 ? 'They are' : 'That is'
failedTracker.size > 1 ? 'They are' : 'That is'
} blocked by security reason. Instead we recommend using assets uploaded to online. (Or you can use ${chalk.yellow(
'--allow-local-files'
)} option if you are understood of security risk)`
Expand All @@ -387,17 +399,26 @@ export class Converter {
}
}

private trackFailedLocalFileAccess(page: puppeteer.Page): Set<string> {
private trackFailedLocalFileAccess(
page: puppeteer.Page
): { missingFileSet: Set<string>; failedFileSet: Set<string> } {
const missingFileSet = new Set<string>()
const failedFileSet = new Set<string>()

page.on('requestfailed', (req: puppeteer.Request) => {
try {
const url = new URL(req.url())
if (url.protocol === 'file:') failedFileSet.add(url.href)
if (url.protocol === 'file:') {
if (req.failure()?.errorText === 'net::ERR_FILE_NOT_FOUND') {
missingFileSet.add(url.href)
} else {
failedFileSet.add(url.href)
}
}
} catch (e) {}
})

return failedFileSet
return { missingFileSet, failedFileSet }
}

static async closeBrowser() {
Expand Down

0 comments on commit c3c7f23

Please sign in to comment.