Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
e17c737
tools: copyedit `build-tarball.yml`
aduh95 Sep 10, 2025
43e6e54
build: do not include custom ESLint rules testing in tarball
aduh95 Sep 8, 2025
f0c20cc
src: remove unnecessary `Environment::GetCurrent()` calls
iknoom Sep 11, 2025
0d23fd5
tools: skip test-internet workflow for draft PRs
targos Sep 11, 2025
b200cd8
lib,src: refactor assert to load error source from memory
legendecas Sep 2, 2025
d785929
lib: add source map support for assert messages
legendecas Sep 3, 2025
ff13d1d
lib,src: cache ModuleWrap.hasAsyncGraph
legendecas Sep 11, 2025
8a5325d
src: ensure `v8::Eternal` is empty before setting it
addaleax Sep 11, 2025
b711256
deps: update undici to 7.16.0
nodejs-github-bot Sep 11, 2025
e9cb986
doc: rephrase dynamic import() description
meteorqz6 Sep 12, 2025
e4f5716
doc: deprecate closing `fs.Dir` on garbage collection
LiviaMedeiros Sep 12, 2025
6458867
url: add type checking to urlToHttpOptions()
simon-id Sep 12, 2025
9a4bbdc
benchmark: calibrate config cluster/echo.js
meteorqz6 Sep 12, 2025
bb051c5
crypto: avoid calls to `promise.catch()`
Renegade334 Sep 9, 2025
8ed4587
crypto: use async functions for non-stub Promise-returning functions
Renegade334 Sep 9, 2025
647c332
crypto: use `return await` when returning Promises from async functions
Renegade334 Sep 9, 2025
f006a14
node-api: make napi_delete_reference use node_api_basic_env
JeetuSuthar Sep 12, 2025
03116a7
src: remove `std::array` overload of `FIXED_ONE_BYTE_STRING`
addaleax Aug 4, 2025
2bb1525
src: create strings in `FIXED_ONE_BYTE_STRING` as internalized
addaleax Aug 4, 2025
026d4e3
doc,crypto: update subtle.generateKey and subtle.importKey
panva Sep 13, 2025
0f46c1c
repl: fix cpu overhead pasting big strings to the REPL
BridgeAR Sep 14, 2025
6133a82
util: fix debuglog.enabled not being present with callback logger
BridgeAR Sep 14, 2025
3c62b38
util: inspect objects with throwing Symbol.toStringTag
BridgeAR Sep 14, 2025
9df91e5
doc: type improvement of file `http.md`
samuel871211 Sep 14, 2025
8b29bbc
url: replaced slice with at
Connormiha Sep 14, 2025
cbec4fd
benchmark: calibrate config dgram multi-buffer
brunocroh Sep 14, 2025
fed1dac
lib: update isDeepStrictEqual to support options
miguelmarcondesf Sep 14, 2025
9782ca2
zlib: implement fast path for crc32
gurgunday Sep 14, 2025
83ae610
http: optimize checkIsHttpToken for short strings
JinhyeokFang Sep 14, 2025
acada1f
inspector: ensure adequate memory allocation for `Binary::toBase64`
Renegade334 Sep 14, 2025
bd767c5
doc: add security escalation policy
UlisesGascon Sep 15, 2025
97c4e1b
typings: remove unused imports
meteorqz6 Sep 15, 2025
be48760
node-api: added SharedArrayBuffer api
mertcanaltin Sep 16, 2025
039ac19
crypto: expose signatureAlgorithm on X509Certificate
patrickrbc Sep 16, 2025
213188e
stream: use new AsyncResource instead of bind
mcollina Sep 16, 2025
abecdcb
src: replace FIXED_ONE_BYTE_STRING with Environment-cached strings
iknoom Sep 17, 2025
ac78491
src: reduce the nearest parent package JSON cache size
michaelsmithxyz Sep 17, 2025
993f05d
fs,win: do not add a second trailing slash in readdir
Flarna Sep 17, 2025
728472a
module: only put directly require-d ESM into require.cache
joyeecheung Sep 17, 2025
8f32746
test: guard write to proxy client if proxy connection is ended
joyeecheung Sep 17, 2025
d17f299
src: use DictionaryTemplate more in URLPattern
jasnell Sep 17, 2025
87bbaa2
tools: fix `tools/make-v8.sh` for clang
richardlau Sep 17, 2025
7aec53b
http: add shouldUpgradeCallback to let servers control HTTP upgrades
pimterry Sep 17, 2025
9a3e58a
sqlite: add tagged template
0hmX Sep 17, 2025
e94c573
meta: add .npmrc with ignore-scripts=true
joyeecheung Sep 17, 2025
8c85570
deps: upgrade openssl sources to openssl-3.5.3
nodejs-github-bot Sep 16, 2025
fa40d3a
deps: update archs files for openssl-3.5.3
nodejs-github-bot Sep 16, 2025
f5d3f91
tls: only do off-thread certificate loading on loading tls
joyeecheung Sep 11, 2025
6790093
tls: load bundled and extra certificates off-thread
joyeecheung Sep 11, 2025
2b2591d
esm: make hasAsyncGraph non-enumerable
joyeecheung Sep 18, 2025
b0c1e67
diagnostics_channel: fix race condition with diagnostics_channel and GC
Sep 19, 2025
6e586a1
vm: expose hasTopLevelAwait on SourceTextModule
legendecas Sep 19, 2025
c5d910a
http2: fix allowHttp1+Upgrade, broken by shouldUpgradeCallback
pimterry Sep 20, 2025
86150f3
src: rename private fields to follow naming convention
iknoom Sep 20, 2025
69b5607
src: simplify is_callable by making it a concept
tniessen Sep 20, 2025
396cc8e
lib: update inspect output format for subclasses
miguelmarcondesf Sep 21, 2025
ce8435b
test: testcase demonstrating issue 59541
ericrannaud Sep 7, 2025
44ce971
vm: "afterEvaluate", evaluate() return a promise from the outer context
ericrannaud Sep 10, 2025
9347ddd
vm: explain how to share promises between contexts w/ afterEvaluate
ericrannaud Sep 19, 2025
dea5dd7
dgram: restore buffer optimization in fixBufferList
ri7116 Sep 21, 2025
a28962a
doc: update V8 fast API guidance
Renegade334 Sep 21, 2025
0b284d8
build: add the missing macro definitions for OpenHarmony
hqzing Sep 22, 2025
e330f03
src: update crypto objects to use DictionaryTemplate
jasnell Sep 22, 2025
b970c0b
zlib: reduce code duplication
jhofstee Sep 22, 2025
6695067
http,https: handle IPv6 with proxies
joyeecheung Sep 22, 2025
05e560d
deps: update googletest to 50b8600
nodejs-github-bot Sep 23, 2025
1e723f9
doc: fix typo in section on microtask order
tniessen Sep 23, 2025
49747a5
worker: add heap profile API
theanarkh Sep 23, 2025
f14ed5a
src: simplify watchdog instantiations via `std::optional`
addaleax Sep 23, 2025
0b37b59
doc: use "WebAssembly" instead of "Web Assembly"
tniessen Sep 24, 2025
0fe5337
sqlite: cleanup ERM support and export Session class
jasnell May 18, 2025
3eeb7b4
sqlite: fix crash session extension callbacks with workers
louwers Sep 23, 2025
31c6e70
2025-09-25, Version 24.9.0 (Current)
nodejs-github-bot Sep 24, 2025
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
lib,src: refactor assert to load error source from memory
The source code is available from V8 API and assert can avoid reading
the source file from the filesystem and parse the file again.

PR-URL: #59751
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
  • Loading branch information
legendecas authored and targos committed Sep 12, 2025
commit b200cd8470a4084c53c91a1b03b6aed29861f31f
214 changes: 9 additions & 205 deletions lib/internal/assert/utils.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
'use strict';

const {
ArrayPrototypeShift,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
RegExpPrototypeSymbolReplace,
SafeMap,
StringPrototypeCharCodeAt,
StringPrototypeIncludes,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
} = primordials;

const { Buffer } = require('buffer');
const {
isErrorStackTraceLimitWritable,
overrideStackTrace,
} = require('internal/errors');
const AssertionError = require('internal/assert/assertion_error');
const { openSync, closeSync, readSync } = require('fs');
const { EOL } = require('internal/constants');
const { BuiltinModule } = require('internal/bootstrap/realm');
const { isError } = require('internal/util');

const errorCache = new SafeMap();
const { fileURLToPath } = require('internal/url');

let parseExpressionAt;
let findNodeAround;
let tokenizer;
let decoder;
const {
getErrorSourceExpression,
} = require('internal/errors/error_source');

// Escape control characters but not \n and \t to keep the line breaks and
// indentation intact.
Expand All @@ -50,111 +33,7 @@ const meta = [

const escapeFn = (str) => meta[StringPrototypeCharCodeAt(str, 0)];

function findColumn(fd, column, code) {
if (code.length > column + 100) {
try {
return parseCode(code, column);
} catch {
// End recursion in case no code could be parsed. The expression should
// have been found after 2500 characters, so stop trying.
if (code.length - column > 2500) {
// eslint-disable-next-line no-throw-literal
throw null;
}
}
}
// Read up to 2500 bytes more than necessary in columns. That way we address
// multi byte characters and read enough data to parse the code.
const bytesToRead = column - code.length + 2500;
const buffer = Buffer.allocUnsafe(bytesToRead);
const bytesRead = readSync(fd, buffer, 0, bytesToRead);
code += decoder.write(buffer.slice(0, bytesRead));
// EOF: fast path.
if (bytesRead < bytesToRead) {
return parseCode(code, column);
}
// Read potentially missing code.
return findColumn(fd, column, code);
}

function getCode(fd, line, column) {
let bytesRead = 0;
if (line === 0) {
// Special handle line number one. This is more efficient and simplifies the
// rest of the algorithm. Read more than the regular column number in bytes
// to prevent multiple reads in case multi byte characters are used.
return findColumn(fd, column, '');
}
let lines = 0;
// Prevent blocking the event loop by limiting the maximum amount of
// data that may be read.
let maxReads = 32; // bytesPerRead * maxReads = 512 KiB
const bytesPerRead = 16384;
// Use a single buffer up front that is reused until the call site is found.
let buffer = Buffer.allocUnsafe(bytesPerRead);
while (maxReads-- !== 0) {
// Only allocate a new buffer in case the needed line is found. All data
// before that can be discarded.
buffer = lines < line ? buffer : Buffer.allocUnsafe(bytesPerRead);
bytesRead = readSync(fd, buffer, 0, bytesPerRead);
// Read the buffer until the required code line is found.
for (let i = 0; i < bytesRead; i++) {
if (buffer[i] === 10 && ++lines === line) {
// If the end of file is reached, directly parse the code and return.
if (bytesRead < bytesPerRead) {
return parseCode(buffer.toString('utf8', i + 1, bytesRead), column);
}
// Check if the read code is sufficient or read more until the whole
// expression is read. Make sure multi byte characters are preserved
// properly by using the decoder.
const code = decoder.write(buffer.slice(i + 1, bytesRead));
return findColumn(fd, column, code);
}
}
}
}

function parseCode(code, offset) {
// Lazy load acorn.
if (parseExpressionAt === undefined) {
const Parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
({ findNodeAround } = require('internal/deps/acorn/acorn-walk/dist/walk'));

parseExpressionAt = FunctionPrototypeBind(Parser.parseExpressionAt, Parser);
tokenizer = FunctionPrototypeBind(Parser.tokenizer, Parser);
}
let node;
let start;
// Parse the read code until the correct expression is found.
for (const token of tokenizer(code, { ecmaVersion: 'latest' })) {
start = token.start;
if (start > offset) {
// No matching expression found. This could happen if the assert
// expression is bigger than the provided buffer.
break;
}
try {
node = parseExpressionAt(code, start, { ecmaVersion: 'latest' });
// Find the CallExpression in the tree.
node = findNodeAround(node, offset, 'CallExpression');
if (node?.node.end >= offset) {
return [
node.node.start,
StringPrototypeReplace(StringPrototypeSlice(code,
node.node.start, node.node.end),
escapeSequencesRegExp, escapeFn),
];
}
// eslint-disable-next-line no-unused-vars
} catch (err) {
continue;
}
}
// eslint-disable-next-line no-throw-literal
throw null;
}

function getErrMessage(message, fn) {
function getErrMessage(fn) {
const tmpLimit = Error.stackTraceLimit;
const errorStackTraceLimitIsWritable = isErrorStackTraceLimitWritable();
// Make sure the limit is set to 1. Otherwise it could fail (<= 0) or it
Expand All @@ -166,85 +45,10 @@ function getErrMessage(message, fn) {
ErrorCaptureStackTrace(err, fn);
if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = tmpLimit;

overrideStackTrace.set(err, (_, stack) => stack);
const call = err.stack[0];

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

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

// Skip Node.js modules!
if (StringPrototypeStartsWith(filename, 'node:') &&
BuiltinModule.exists(StringPrototypeSlice(filename, 5))) {
errorCache.set(identifier, undefined);
return;
}
} else {
return message;
}

if (errorCache.has(identifier)) {
return errorCache.get(identifier);
}

let fd;
try {
// Set the stack trace limit to zero. This makes sure unexpected token
// errors are handled faster.
if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = 0;

if (decoder === undefined) {
const { StringDecoder } = require('string_decoder');
decoder = new StringDecoder('utf8');
}

// ESM file prop is a file proto. Convert that to path.
// This ensure opensync will not throw ENOENT for ESM files.
const fileProtoPrefix = 'file://';
if (StringPrototypeStartsWith(filename, fileProtoPrefix)) {
filename = fileURLToPath(filename);
}

fd = openSync(filename, 'r', 0o666);
// Reset column and message.
({ 0: column, 1: message } = getCode(fd, line, column));
// Flush unfinished multi byte characters.
decoder.end();

// Always normalize indentation, otherwise the message could look weird.
if (StringPrototypeIncludes(message, '\n')) {
if (EOL === '\r\n') {
message = RegExpPrototypeSymbolReplace(/\r\n/g, message, '\n');
}
const frames = StringPrototypeSplit(message, '\n');
message = ArrayPrototypeShift(frames);
for (let i = 0; i < frames.length; i++) {
const frame = frames[i];
let pos = 0;
while (pos < column && (frame[pos] === ' ' || frame[pos] === '\t')) {
pos++;
}
message += `\n ${StringPrototypeSlice(frame, pos)}`;
}
}
message = `The expression evaluated to a falsy value:\n\n ${message}\n`;
// Make sure to always set the cache! No matter if the message is
// undefined or not
errorCache.set(identifier, message);

return message;
} catch {
// Invalidate cache to prevent trying to read this part again.
errorCache.set(identifier, undefined);
} finally {
// Reset limit.
if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = tmpLimit;
if (fd !== undefined)
closeSync(fd);
let source = getErrorSourceExpression(err);
if (source) {
source = StringPrototypeReplace(source, escapeSequencesRegExp, escapeFn);
return `The expression evaluated to a falsy value:\n\n ${source}\n`;
}
}

Expand All @@ -257,7 +61,7 @@ function innerOk(fn, argLen, value, message) {
message = 'No value argument passed to `assert.ok()`';
} else if (message == null) {
generatedMessage = true;
message = getErrMessage(message, fn);
message = getErrMessage(fn);
} else if (isError(message)) {
throw message;
}
Expand Down
133 changes: 133 additions & 0 deletions lib/internal/errors/error_source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use strict';

const {
FunctionPrototypeBind,
StringPrototypeSlice,
} = primordials;

const {
getErrorSourcePositions,
} = internalBinding('errors');

/**
* Get the source location of an error.
*
* The `error.stack` must not have been accessed. The resolution is based on the structured
* error stack data.
* @param {Error|object} error An error object, or an object being invoked with ErrorCaptureStackTrace
* @returns {{sourceLine: string, startColumn: number}|undefined}
*/
function getErrorSourceLocation(error) {
const pos = getErrorSourcePositions(error);
const {
sourceLine,
startColumn,
} = pos;

return { sourceLine, startColumn };
}

const memberAccessTokens = [ '.', '?.', '[', ']' ];
const memberNameTokens = [ 'name', 'string', 'num' ];
let tokenizer;
/**
* Get the first expression in a code string at the startColumn.
* @param {string} code source code line
* @param {number} startColumn which column the error is constructed
* @returns {string}
*/
function getFirstExpression(code, startColumn) {
// Lazy load acorn.
if (tokenizer === undefined) {
const Parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
tokenizer = FunctionPrototypeBind(Parser.tokenizer, Parser);
}

let lastToken;
let firstMemberAccessNameToken;
let terminatingCol;
let parenLvl = 0;
// Tokenize the line to locate the expression at the startColumn.
// The source line may be an incomplete JavaScript source, so do not parse the source line.
for (const token of tokenizer(code, { ecmaVersion: 'latest' })) {
// Peek before the startColumn.
if (token.start < startColumn) {
// There is a semicolon. This is a statement before the startColumn, so reset the memo.
if (token.type.label === ';') {
firstMemberAccessNameToken = null;
continue;
}
// Try to memo the member access expressions before the startColumn, so that the
// returned source code contains more info:
// assert.ok(value)
// ^ startColumn
// The member expression can also be like
// assert['ok'](value) or assert?.ok(value)
// ^ startColumn ^ startColumn
if (memberAccessTokens.includes(token.type.label) && lastToken?.type.label === 'name') {
// First member access name token must be a 'name'.
firstMemberAccessNameToken ??= lastToken;
} else if (!memberAccessTokens.includes(token.type.label) &&
!memberNameTokens.includes(token.type.label)) {
// Reset the memo if it is not a simple member access.
// For example: assert[(() => 'ok')()](value)
// ^ startColumn
firstMemberAccessNameToken = null;
}
lastToken = token;
continue;
}
// Now after the startColumn, this must be an expression.
if (token.type.label === '(') {
parenLvl++;
continue;
}
if (token.type.label === ')') {
parenLvl--;
if (parenLvl === 0) {
// A matched closing parenthesis found after the startColumn,
// terminate here. Include the token.
// (assert.ok(false), assert.ok(true))
// ^ startColumn
terminatingCol = token.start + 1;
break;
}
continue;
}
if (token.type.label === ';') {
// A semicolon found after the startColumn, terminate here.
// assert.ok(false); assert.ok(true));
// ^ startColumn
terminatingCol = token;
break;
}
// If no semicolon found after the startColumn. The string after the
// startColumn must be the expression.
// assert.ok(false)
// ^ startColumn
}
const start = firstMemberAccessNameToken?.start ?? startColumn;
return StringPrototypeSlice(code, start, terminatingCol);
}

/**
* Get the source expression of an error.
*
* The `error.stack` must not have been accessed, or the source location may be incorrect. The
* resolution is based on the structured error stack data.
* @param {Error|object} error An error object, or an object being invoked with ErrorCaptureStackTrace
* @returns {string|undefined}
*/
function getErrorSourceExpression(error) {
const loc = getErrorSourceLocation(error);
if (loc === undefined) {
return;
}
const { sourceLine, startColumn } = loc;
return getFirstExpression(sourceLine, startColumn);
}

module.exports = {
getErrorSourceLocation,
getErrorSourceExpression,
};
Loading