Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: assert using file urls
It seems getErrMessage() function uses filename from a CallSite to
open a file and extract source code location of the statement. The
CallSite now returns file URL, so it must be converted back to path.
  • Loading branch information
Dragiyski committed Jan 19, 2021
commit 547391a00d6e206623ca452101b93a298b90aabd
9 changes: 8 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const { isPromise, isRegExp } = require('internal/util/types');
const { EOL } = require('internal/constants');
const { NativeModule } = require('internal/bootstrap/loaders');
const { isError } = require('internal/util');
const { fileURLToPath } = require('internal/url');

const errorCache = new SafeMap();
const CallTracker = require('internal/assert/calltracker');
Expand Down Expand Up @@ -291,12 +292,18 @@ function getErrMessage(message, fn) {
overrideStackTrace.set(err, (_, stack) => stack);
const call = err.stack[0];

const filename = call.getFileName();
let filename = call.getFileName();
const line = call.getLineNumber() - 1;
let column = call.getColumnNumber() - 1;
let identifier;
let code;

if (filename && StringPrototypeStartsWith(filename, 'file:')) {
try {
filename = fileURLToPath(filename);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the check for file:, under what conditions do we expect this to fail?

If we leave this try/catch, we might want to put a debug in the catch.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileURLToPath and normalizeReferrerURL call URL constructor with a single argument. There was a string (I do not remember exactly, but I can try to remove the try/catch and rebuild to find it) like [eval something] that makes URL throws an ERR_INVALID_URL error. Since that is in the unit tests, it might be used in the future. Moreover, users of vm can pass any string for filename or have source map containing any string. The combination of both is possible to make otherwise invalid URL to appear as valid module and in general invalid URLs supplied by the user should not crush for the proper reason: MODULE_NOT_FOUND for example.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth getting a unit test around the edge case if possible 👌

} catch {}
}

if (filename) {
identifier = `${filename}${line}${column}`;

Expand Down