8000 Group require · HowProgrammingWorks/Modularity@3a50182 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a50182

Browse files
committed
Group require
1 parent 8b36bdf commit 3a50182

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

NodeJS/0-internal/application.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const { fs, path, vm, os, http, util, crypto, timers } = require('./node.js');
4+
5+
// Use modules as usual
6+
7+
fs.readFile('./application.js', (error, data) => {
8+
if (error) {
9+
console.log({ error });
10+
} else {
11+
console.log({ data });
12+
}
13+
});
14+
15+
// Show what we have
16+
17+
const node = { fs, path, vm, os, http, util, crypto, timers };
18+
console.log({ node });

NodeJS/0-internal/node.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const node = {};
4+
const tools = ['util', 'path', 'buffer', 'os', 'v8', 'vm'];
5+
const multi = ['child_process', 'worker_threads'];
6+
const streams = ['stream', 'fs', 'crypto', 'zlib', 'readline'];
7+
const async = ['perf_hooks', 'async_hooks', 'timers', 'events'];
8+
const network = ['dns', 'net', 'tls', 'http', 'https', 'http2', 'dgram'];
9+
const internals = [...tools, ...multi, ...streams, ...async, ...network];
10+
11+
for (const name of internals) node[name] = require(`node:${name}`);
12+
node.process = process;
13+
node.fsp = node.fs.promises;
14+
node.timers.promises = node['timers/promises'];
15+
16+
module.exports = Object.freeze(node);

NodeJS/0-internal/traditional.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const fs = require('node:fs');
4+
const path = require('node:path');
5+
const vm = require('node:vm');
6+
const os = require('node:os');
7+
const http = require('node:http');
8+
const util = require('node:util');
9+
const crypto = require('node:crypto');
10+
const timers = require('node:timers');
11+
12+
// Use modules as usual
13+
14+
fs.readFile('./application.js', (error, data) => {
15+
if (error) {
16+
console.log({ error });
17+
} else {
18+
console.log({ data });
19+
}
20+
});
21+
22+
// Show what we have
23+
24+
const node = { fs, path, vm, os, http, util, crypto, timers };
25+
console.log({ node });

0 commit comments

Comments
 (0)
0