Lesson 29-Node.js Core Module Principles and Source Code Analysis

Node.js Core Module Analysis

Built-in Module List:

  • assert: Assertion module
  • buffer: Binary data handling
  • child_process: Subprocess management
  • cluster: Process clustering
  • crypto: Cryptographic functions
  • dns: Domain name resolution
  • events: Event emitter
  • fs: File system operations
  • http: HTTP server and client
  • https: HTTPS server and client
  • net: Network sockets
  • os: Operating system information
  • path: Path operations
  • process: Current process information
  • stream: Stream operations
  • timers: Timers
  • url: URL parsing
  • util: General utility functions
  • vm: Virtual machine module
  • zlib: 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 AssertionError on assertion failure.
  • Supports various comparison operations like deepEqual and strictEqual.
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.

  • Buffer is a fixed-size array for storing arbitrary-length binary data.
  • Offers methods for data type conversion, such as toString() and write().
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, or fork methods 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 readFile and writeFile.
  • 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.

  • createServer creates a server.
  • request initiates 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.

  • createServer creates a TCP server.
  • connect establishes 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, and cpus.
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, and normalize.
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, and exit.
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, and Transform stream 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 setTimeout and setInterval.
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 parse and format.
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 inspect and promisify.
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 runInNewContext and runInThisContext.
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);

Membership Required

You must be a member to access this content.

View Membership Levels

Already a member? Log in here

Share your love