Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: source-code

RSS

Using `readFile` as an example, how to implement it in Node.js

This article provides an in-depth analysis of the underlying implementation mechanisms of file reading operations in Node.js, with a focus on tracing the call chains of `fs.readFileSync` and `fs.readFile`. It points out that, whether synchronous or asynchronous, all operations ultimately converge on libuv's `uv_fs_read` function, with the execution mode distinguished by the presence or absence of a callback function. At the code level, it details how `FSReqCallback` and its base class `FSReqBase` encapsulate request state after entering the C++ layer from the JS layer, and how tasks are dispatched to the event loop through the `AsyncCall` and `Dispatch` mechanisms. For the asynchronous flow, it emphasizes how the `ReqWrap` template class adapts to the libuv interface, and how the `AfterInteger` callback function triggers the Promise's Resolve logic to return the result upon completion of the operation. Additionally, the article analyzes the details of permission checks and file descriptor management within the `open` function, revealing Node.js's meticulous considerations for platform compatibility and resource management at each level of abstraction. Finally, it summarizes legacy issues such as built-in module loading and asynchronous construction, aiming to help readers gain a deep understanding of Node.js's core architecture and design philosophy.

Views 185
2025 c++ daily javascript libuv nodejs source-code study v8

Debug Vue3 source code (deprecated)

This article primarily shares an awkward experience of accidentally using Vue 2 source code while debugging Vue 3 source code, and elaborates in detail on the correct debugging approach and configuration methods. The core strategy is to have the Vue 3 source code generate sourcemaps and link them to the project template via a local build, replacing remote package references. Specifically, in Vue's Rollup build configuration file, you need to add `sourcemap: true` in the `output` section to enable source mapping. Regarding the debugging environment configuration, the article provides a VS Code `launch.json` example covering three debugging scenarios: first, debugging the `npm run build` build script itself; second, debugging a remote project based on the official Vue template, where you need to configure `outFiles` to correctly map the source code; and third, debugging raw HTML files, which also requires specifying the `outFiles` path to point to the local Vue source code directory. By combining local builds with source mapping, developers can more effectively track and debug Vue 3 internal logic, avoiding the difficulties of directly debugging minified code, and providing a practical technical solution for deeply understanding the framework's source code.

Views 83
2024 source-code study vue vue3 web