Node.js Core Module Analysis
Built-in Module List:
assert: Assertion modulebuffer: Binary data handlingchild_process: Subprocess managementcluster: Process clusteringcrypto: Cryptographic functionsdns: Domain name resolutionevents: Event emitterfs: File system operationshttp: HTTP server and clienthttps: HTTPS server and clientnet: Network socketsos: Operating system informationpath: Path operationsprocess: Current process informationstream: Stream operationstimers: Timersurl: URL parsingutil: General utility functionsvm: Virtual machine modulezlib: Compression and decompression
Module Basics and Analysis
assert: Assertion Module
The assert module provides assertion functions for validating conditions during development, primarily used for testing and debugging.
The assert module is implemented in JavaScript, with its source located in lib/assert.js. It defines a series of functions for validating expressions, such as strictEqual and deepStrictEqual. These functions internally check if the provided values meet expectations, throwing an AssertionError if they do not.
- Throws
AssertionErroron assertion failure. - Supports various comparison operations like
deepEqualandstrictEqual.
const assert = require('assert');
assert.strictEqual(1 + 1, 2);
assert.deepEqual([1, 2], [1, 2]);buffer: Binary Data Handling
The Buffer class is used to handle binary data, serving as the foundation for processing streaming data and file system operations in Node.js.
Most Buffer functionality is implemented in C++, but buffer.js provides methods for creating and manipulating Buffer instances at the JavaScript level.
Bufferis a fixed-size array for storing arbitrary-length binary data.- Offers methods for data type conversion, such as
toString()andwrite().
const buf = Buffer.from('Hello World');
console.log(buf.toString());child_process: Subprocess Management
The child_process module enables Node.js processes to create and control subprocesses. Most functionality is implemented in C++, with child_process.js providing the JavaScript interface.
- Uses
spawn,exec, orforkmethods to create subprocesses. - Supports redirection of standard input/output streams.
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh']);
ls.stdout.on('data', (data) => {
console.log(data.toString());
});cluster: Process Clustering
The cluster module is used to create multi-process servers to leverage multi-core CPUs. It includes both C++ and JavaScript implementations, with cluster.js providing APIs for creating and managing process clusters.
- The master process manages multiple worker processes.
- Supports inter-process communication (IPC).
if (require.main === module) {
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello World\n');
}).listen(8000);
}
}crypto: Cryptographic Functions
The crypto module provides encryption and hashing functions. Most functionality is implemented in C++, offering interfaces for various cryptographic and hashing algorithms.
- Supports algorithms like AES, RSA, and SHA.
- Can create encrypted streams or directly encrypt data.
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('Hello World');
console.log(hash.digest('hex'));fs: File System Operations
The fs module provides APIs for file system operations. While underlying file operations are implemented in C++, fs.js offers the JavaScript interface for all file system operations.
- Provides synchronous and asynchronous methods like
readFileandwriteFile. - Uses callbacks or Promises for asynchronous operations.
const fs = require('fs').promises;
fs.readFile('./example.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));http/https: HTTP/HTTPS Server and Client
The http and https modules are used to create HTTP and HTTPS servers or clients. Most functionality is implemented in C++, with http.js and https.js providing JavaScript interfaces for creating servers and clients.
createServercreates a server.requestinitiates a request.
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
});
server.listen(8000);net: Network Sockets
The net module provides functionality for TCP/IP network programming. Underlying network operations are implemented in C++, with net.js offering JavaScript interfaces for creating TCP servers and clients.
createServercreates a TCP server.connectestablishes a client connection.
const net = require('net');
const server = net.createServer(socket => {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(1337);os: Operating System Information
The os module provides utility information about the operating system. os.js offers a JavaScript interface for querying OS information, with most data retrieval implemented in C++.
- Provides methods like
platform,arch, andcpus.
const os = require('os');
console.log(os.platform());
console.log(os.cpus().length);path: Path Operations
The path module provides tools for handling file paths. path.js is entirely implemented in JavaScript, offering various path operation functions.
- Provides methods like
join,resolve, andnormalize.
const path = require('path');
console.log(path.join('/usr', 'local', 'bin'));process: Current Process Information
The process module provides information and control over the current Node.js process. Most functionality is implemented in C++, with process.js offering a JavaScript interface.
- Provides properties and methods like
argv,cwd, andexit.
console.log(process.argv);
process.exit(0);stream: Stream Operations
The stream module enables stream-based data processing. stream.js provides high-level abstractions for stream processing, including Readable, Writable, Duplex, and Transform streams.
- Supports
Readable,Writable,Duplex, andTransformstream types. - Streams can be connected via
pipe.
const fs = require('fs');
const stream = require('stream');
const rs = fs.createReadStream('./example.txt');
const ws = fs.createWriteStream('./output.txt');
rs.pipe(ws);timers: Timers
The timers module provides timer functionality. timers.js implements timer functions like setTimeout and setInterval.
- Provides methods like
setTimeoutandsetInterval.
setTimeout(() => {
console.log('Hello World');
}, 1000);url: URL Parsing
The url module is used for parsing and formatting URLs. url.js provides URL parsing and formatting functionality, entirely implemented in JavaScript.
- Provides methods like
parseandformat.
const url = require('url');
const myUrl = url.parse('http://www.example.com/path?query=string');
console.log(myUrl);util: General Utility Functions
The util module provides various utility functions. util.js offers functions like inspect and promisify.
- Provides methods like
inspectandpromisify.
const util = require('util');
const fs = require('fs');
const readFileAsync = util.promisify(fs.readFile);
readFileAsync('./example.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));vm: Virtual Machine Module
The vm module allows execution of JavaScript code in isolated contexts. vm.js provides functionality for running JavaScript in isolated contexts.
- Provides methods like
runInNewContextandrunInThisContext.
const vm = require('vm');
const script = new vm.Script('var x = 1; x + 1');
const context = {};
script.runInNewContext(context);
console.log(context.x);zlib: Compression and Decompression
The zlib module provides data compression and decompression functionality. zlib.js implements compression and decompression, with underlying algorithms in C++.
- Supports algorithms like gzip and deflate.
const zlib = require('zlib');
const fs = require('fs');
const gzip = zlib.createGzip();
const rs = fs.createReadStream('./example.txt');
const ws = fs.createWriteStream('./example.txt.gz');
rs.pipe(gzip).pipe(ws);



