Skip to content

Commit bae6728

Browse files
committed
doc: additional example edits
Additional example edits for consistency
1 parent f950904 commit bae6728

16 files changed

+113
-111
lines changed

Diff for: doc/api/child_process.markdown

+12-13
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ a `'message'` event on the child.
166166
For example:
167167

168168
const cp = require('child_process');
169-
170-
var n = cp.fork(`${__dirname}/sub.js`);
169+
const n = cp.fork(`${__dirname}/sub.js`);
171170

172171
n.on('message', (m) => {
173172
console.log('PARENT got message:', m);
@@ -215,7 +214,7 @@ Here is an example of sending a server:
215214
const child = require('child_process').fork('child.js');
216215

217216
// Open up the server object and send the handle.
218-
var server = require('net').createServer();
217+
const server = require('net').createServer();
219218
server.on('connection', (socket) => {
220219
socket.end('handled by parent');
221220
});
@@ -251,7 +250,7 @@ process.
251250
const special = require('child_process').fork('child.js', ['special']);
252251

253252
// Open up the server and send sockets to child
254-
var server = require('net').createServer();
253+
const server = require('net').createServer();
255254
server.on('connection', (socket) => {
256255

257256
// if this is a VIP
@@ -320,7 +319,7 @@ the parent's `child.stdio[1]` is a stream, all other values in the array are
320319
const fs = require('fs');
321320
const child_process = require('child_process');
322321

323-
child = child_process.spawn('ls', {
322+
const child = child_process.spawn('ls', {
324323
stdio: [
325324
0, // use parents stdin for child
326325
'pipe', // pipe child's stdout to parent
@@ -380,7 +379,7 @@ callback or returning an EventEmitter).
380379
Runs a command in a shell and buffers the output.
381380

382381
const exec = require('child_process').exec;
383-
var child = exec('cat *.js bad_file | wc -l',
382+
const child = exec('cat *.js bad_file | wc -l',
384383
(error, stdout, stderr) => {
385384
console.log(`stdout: ${stdout}`);
386385
console.log(`stderr: ${stderr}`);
@@ -508,7 +507,7 @@ process, the default is `process.env`.
508507
Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the exit code:
509508

510509
const spawn = require('child_process').spawn;
511-
var ls = spawn('ls', ['-lh', '/usr']);
510+
const ls = spawn('ls', ['-lh', '/usr']);
512511

513512
ls.stdout.on('data', (data) => {
514513
console.log(`stdout: ${data}`);
@@ -526,8 +525,8 @@ Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the exit cod
526525
Example: A very elaborate way to run 'ps ax | grep ssh'
527526

528527
const spawn = require('child_process').spawn;
529-
var ps = spawn('ps', ['ax']);
530-
var grep = spawn('grep', ['ssh']);
528+
const ps = spawn('ps', ['ax']);
529+
const grep = spawn('grep', ['ssh']);
531530

532531
ps.stdout.on('data', (data) => {
533532
grep.stdin.write(data);
@@ -562,7 +561,7 @@ Example: A very elaborate way to run 'ps ax | grep ssh'
562561
Example of checking for failed exec:
563562

564563
const spawn = require('child_process').spawn;
565-
var child = spawn('bad_command');
564+
const child = spawn('bad_command');
566565

567566
child.on('error', (err) => {
568567
console.log('Failed to start child process.');
@@ -588,10 +587,10 @@ file:
588587

589588
const fs = require('fs');
590589
const spawn = require('child_process').spawn;
591-
var out = fs.openSync('./out.log', 'a');
592-
var err = fs.openSync('./out.log', 'a');
590+
const out = fs.openSync('./out.log', 'a');
591+
const err = fs.openSync('./out.log', 'a');
593592

594-
var child = spawn('prg', [], {
593+
const child = spawn('prg', [], {
595594
detached: true,
596595
stdio: [ 'ignore', out, err ]
597596
});

Diff for: doc/api/cluster.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ In a worker you can also use `process.on('error')`.
131131

132132
Similar to the `cluster.on('exit')` event, but specific to this worker.
133133

134-
var worker = cluster.fork();
134+
const worker = cluster.fork();
135135
worker.on('exit', (code, signal) => {
136136
if( signal ) {
137137
console.log(`worker was killed by signal: ${signal}`);
@@ -612,7 +612,7 @@ A reference to the current worker object. Not available in the master process.
612612
cluster.fork();
613613
cluster.fork();
614614
} else if (cluster.isWorker) {
615-
console.log('I am worker #' + cluster.worker.id);
615+
console.log(`I am worker #${cluster.worker.id}`);
616616
}
617617

618618
## cluster.workers

Diff for: doc/api/console.markdown

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ directly without `require`.
1616

1717
Use `require('console').Console` or `console.Console` to access this class.
1818

19-
var Console = require('console').Console;
20-
var Console = console.Console;
19+
const Console = require('console').Console;
20+
const Console = console.Console;
2121

2222
You can use the `Console` class to create a simple logger like `console` but
2323
with different output streams.
@@ -29,10 +29,10 @@ Create a new `Console` by passing one or two writable stream instances.
2929
is used for warning or error output. If `stderr` isn't passed, the warning
3030
and error output will be sent to the `stdout`.
3131

32-
var output = fs.createWriteStream('./stdout.log');
33-
var errorOutput = fs.createWriteStream('./stderr.log');
32+
const output = fs.createWriteStream('./stdout.log');
33+
const errorOutput = fs.createWriteStream('./stderr.log');
3434
// custom simple logger
35-
var logger = new Console(output, errorOutput);
35+
const logger = new Console(output, errorOutput);
3636
// use it like console
3737
var count = 5;
3838
logger.log('count: %d', count);

Diff for: doc/api/crypto.markdown

+14-14
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ associated with the private key being set.
281281
Example (obtaining a shared secret):
282282

283283
const crypto = require('crypto');
284-
var alice = crypto.createECDH('secp256k1');
285-
var bob = crypto.createECDH('secp256k1');
284+
const alice = crypto.createECDH('secp256k1');
285+
const bob = crypto.createECDH('secp256k1');
286286

287287
// Note: This is a shortcut way to specify one of Alice's previous private
288288
// keys. It would be unwise to use such a predictable private key in a real
@@ -294,8 +294,8 @@ Example (obtaining a shared secret):
294294
// Bob uses a newly generated cryptographically strong pseudorandom key pair
295295
bob.generateKeys();
296296

297-
var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
298-
var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
297+
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
298+
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
299299

300300
// alice_secret and bob_secret should be the same shared secret value
301301
console.log(alice_secret === bob_secret);
@@ -535,13 +535,13 @@ algorithms.
535535

536536
Example: this program that takes the sha256 sum of a file
537537

538-
var filename = process.argv[2];
538+
const filename = process.argv[2];
539539
const crypto = require('crypto');
540540
const fs = require('fs');
541541

542-
var shasum = crypto.createHash('sha256');
542+
const shasum = crypto.createHash('sha256');
543543

544-
var s = fs.ReadStream(filename);
544+
const s = fs.ReadStream(filename);
545545
s.on('data', (d) => {
546546
shasum.update(d);
547547
});
@@ -581,7 +581,7 @@ Returns an array with the names of the supported ciphers.
581581

582582
Example:
583583

584-
var ciphers = crypto.getCiphers();
584+
const ciphers = crypto.getCiphers();
585585
console.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...]
586586

587587
## crypto.getCurves()
@@ -590,7 +590,7 @@ Returns an array with the names of the supported elliptic curves.
590590

591591
Example:
592592

593-
var curves = crypto.getCurves();
593+
const curves = crypto.getCurves();
594594
console.log(curves); // ['secp256k1', 'secp384r1', ...]
595595

596596
## crypto.getDiffieHellman(group_name)
@@ -609,14 +609,14 @@ and communication time.
609609
Example (obtaining a shared secret):
610610

611611
const crypto = require('crypto');
612-
var alice = crypto.getDiffieHellman('modp14');
613-
var bob = crypto.getDiffieHellman('modp14');
612+
const alice = crypto.getDiffieHellman('modp14');
613+
const bob = crypto.getDiffieHellman('modp14');
614614

615615
alice.generateKeys();
616616
bob.generateKeys();
617617

618-
var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
619-
var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
618+
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
619+
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
620620

621621
/* alice_secret and bob_secret should be the same */
622622
console.log(alice_secret == bob_secret);
@@ -627,7 +627,7 @@ Returns an array with the names of the supported hash algorithms.
627627

628628
Example:
629629

630-
var hashes = crypto.getHashes();
630+
const hashes = crypto.getHashes();
631631
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
632632

633633
## crypto.pbkdf2(password, salt, iterations, keylen[, digest], callback)

Diff for: doc/api/debugger.markdown

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ For example, suppose `myscript.js` looked like this:
2727
x = 5;
2828
setTimeout(function () {
2929
debugger;
30-
console.log("world");
30+
console.log('world');
3131
}, 1000);
32-
console.log("hello");
32+
console.log('hello');
3333

3434
Then once the debugger is run, it will break on line 4.
3535

@@ -46,15 +46,15 @@ Then once the debugger is run, it will break on line 4.
4646
1 x = 5;
4747
2 setTimeout(function () {
4848
3 debugger;
49-
4 console.log("world");
49+
4 console.log('world');
5050
5 }, 1000);
5151
debug> next
5252
break in /home/indutny/Code/git/indutny/myscript.js:4
5353
2 setTimeout(function () {
5454
3 debugger;
55-
4 console.log("world");
55+
4 console.log('world');
5656
5 }, 1000);
57-
6 console.log("hello");
57+
6 console.log('hello');
5858
debug> repl
5959
Press Ctrl + C to leave debug repl
6060
> x
@@ -65,9 +65,9 @@ Then once the debugger is run, it will break on line 4.
6565
< world
6666
break in /home/indutny/Code/git/indutny/myscript.js:5
6767
3 debugger;
68-
4 console.log("world");
68+
4 console.log('world');
6969
5 }, 1000);
70-
6 console.log("hello");
70+
6 console.log('hello');
7171
7
7272
debug> quit
7373
%

Diff for: doc/api/dgram.markdown

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Datagram sockets are available through `require('dgram')`.
99
Important note: the behavior of [`dgram.Socket#bind()`][] has changed in v0.10
1010
and is always asynchronous now. If you have code that looks like this:
1111

12-
var s = dgram.createSocket('udp4');
12+
const s = dgram.createSocket('udp4');
1313
s.bind(1234);
1414
s.addMembership('224.0.0.114');
1515

1616
You have to change it to this:
1717

18-
var s = dgram.createSocket('udp4');
18+
const s = dgram.createSocket('udp4');
1919
s.bind(1234, () => {
2020
s.addMembership('224.0.0.114');
2121
});
@@ -94,7 +94,7 @@ Example of a UDP server listening on port 41234:
9494

9595
const dgram = require('dgram');
9696

97-
var server = dgram.createSocket('udp4');
97+
const server = dgram.createSocket('udp4');
9898

9999
server.on('error', (err) => {
100100
console.log(`server error:\n${err.stack}`);
@@ -188,8 +188,8 @@ be calculated with respect to [byte length][] and not the character position.
188188
Example of sending a UDP packet to a random port on `localhost`;
189189

190190
const dgram = require('dgram');
191-
var message = new Buffer('Some bytes');
192-
var client = dgram.createSocket('udp4');
191+
const message = new Buffer('Some bytes');
192+
const client = dgram.createSocket('udp4');
193193
client.send(message, 0, message.length, 41234, 'localhost', (err) => {
194194
client.close();
195195
});

Diff for: doc/api/domain.markdown

+10-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ For example, this is not a good idea:
4848
// XXX WARNING! BAD IDEA!
4949

5050
var d = require('domain').create();
51-
d.on('error', function(er) {
51+
d.on('error', (er) => {
5252
// The error won't crash the process, but what it does is worse!
5353
// Though we've prevented abrupt process restarting, we are leaking
5454
// resources like crazy if this ever happens.
@@ -103,7 +103,7 @@ if (cluster.isMaster) {
103103
// See the cluster documentation for more details about using
104104
// worker processes to serve requests. How it works, caveats, etc.
105105

106-
var server = require('http').createServer((req, res) => {
106+
const server = require('http').createServer((req, res) => {
107107
var d = domain.create();
108108
d.on('error', (er) => {
109109
console.error('error', er.stack);
@@ -229,7 +229,9 @@ For example:
229229

230230
```
231231
// create a top-level domain for the server
232-
var serverDomain = domain.create();
232+
const domain = require('domain');
233+
const http = require('http');
234+
const serverDomain = domain.create();
233235
234236
serverDomain.run(() => {
235237
// server is created in the scope of serverDomain
@@ -281,7 +283,9 @@ This is the most basic way to use a domain.
281283
Example:
282284

283285
```
284-
var d = domain.create();
286+
const domain = require('domain');
287+
const fs = require('fs');
288+
const d = domain.create();
285289
d.on('error', (er) => {
286290
console.error('Caught error!', er);
287291
});
@@ -341,7 +345,7 @@ thrown will be routed to the domain's `'error'` event.
341345

342346
#### Example
343347

344-
var d = domain.create();
348+
const d = domain.create();
345349

346350
function readSomeFile(filename, cb) {
347351
fs.readFile(filename, 'utf8', d.bind(function(er, data) {
@@ -370,7 +374,7 @@ with a single error handler in a single place.
370374

371375
#### Example
372376

373-
var d = domain.create();
377+
const d = domain.create();
374378

375379
function readSomeFile(filename, cb) {
376380
fs.readFile(filename, 'utf8', d.intercept(function(data) {

Diff for: doc/api/errors.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ employed appropriately or [`process.on('uncaughtException')`][] has a handler.
4949
```javascript
5050
const net = require('net');
5151

52-
var connection = net.connect('localhost');
52+
const connection = net.connect('localhost');
5353

5454
// adding an 'error' event handler to a stream:
5555
connection.on('error', (err) => {
@@ -69,7 +69,7 @@ errors when no error handlers are attached. An example:
6969
```javascript
7070
const EventEmitter = require('events');
7171

72-
var ee = new EventEmitter();
72+
const ee = new EventEmitter();
7373

7474
setImmediate(() => {
7575
// this will crash the process because no 'error' event

0 commit comments

Comments
 (0)