Browse Source

doc: fenced all code blocks, typo fixes

This changes the code blocks from 4-space indentation to ``` fences for
better syntax highlighting and future linting support. Minor On-the-fly
changes for typos and highlight breaking markdown have been made.

JSON-Style objects have been changed so their closing bracket is
on the same line as the opening one.

Known issues:
* Not every JSON / object notation has been improved. Should
  make another run for this.
* Some example functions break hightlighting due to various
  combinations of brackets. However changing them means leaving
  the code style.

Fixes: https://github.com/nodejs/node/issues/4726
PR-URL: https://github.com/nodejs/node/pull/4733
Reviewed-By: Roman Reiss <me@silverwind.io>
process-exit-stdio-flushing
Robert Jefe Lindstaedt 9 years ago
committed by Roman Reiss
parent
commit
e436272897
  1. 722
      doc/api/addons.markdown
  2. 198
      doc/api/assert.markdown
  3. 664
      doc/api/buffer.markdown
  4. 280
      doc/api/child_process.markdown
  5. 178
      doc/api/cluster.markdown
  6. 100
      doc/api/console.markdown
  7. 436
      doc/api/crypto.markdown
  8. 87
      doc/api/debugger.markdown
  9. 84
      doc/api/dgram.markdown
  10. 42
      doc/api/dns.markdown
  11. 32
      doc/api/domain.markdown
  12. 120
      doc/api/errors.markdown
  13. 188
      doc/api/events.markdown
  14. 141
      doc/api/fs.markdown
  15. 16
      doc/api/globals.markdown
  16. 214
      doc/api/http.markdown
  17. 80
      doc/api/https.markdown
  18. 190
      doc/api/modules.markdown
  19. 101
      doc/api/net.markdown
  20. 8
      doc/api/os.markdown
  21. 232
      doc/api/path.markdown
  22. 368
      doc/api/process.markdown
  23. 46
      doc/api/punycode.markdown
  24. 44
      doc/api/querystring.markdown
  25. 112
      doc/api/readline.markdown
  26. 138
      doc/api/repl.markdown
  27. 79
      doc/api/stream.markdown
  28. 14
      doc/api/string_decoder.markdown
  29. 16
      doc/api/synopsis.markdown
  30. 182
      doc/api/tls.markdown
  31. 16
      doc/api/tty.markdown
  32. 12
      doc/api/url.markdown
  33. 256
      doc/api/util.markdown
  34. 4
      doc/api/v8.markdown
  35. 122
      doc/api/vm.markdown
  36. 72
      doc/api/zlib.markdown

722
doc/api/addons.markdown

File diff suppressed because it is too large

198
doc/api/assert.markdown

@ -21,25 +21,27 @@ If `value` is not truthy, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is `undefined`, a default error message is assigned.
const assert = require('assert');
```js
const assert = require('assert');
assert(true); // OK
assert(1); // OK
assert(false);
assert(true); // OK
assert(1); // OK
assert(false);
// throws "AssertionError: false == true"
assert(0);
assert(0);
// throws "AssertionError: 0 == true"
assert(false, 'it\'s false');
assert(false, 'it\'s false');
// throws "AssertionError: it's false"
assert.ok(true); // OK
assert.ok(1); // OK
assert.ok(false);
assert.ok(true); // OK
assert.ok(1); // OK
assert.ok(false);
// throws "AssertionError: false == true"
assert.ok(0);
assert.ok(0);
// throws "AssertionError: 0 == true"
assert.ok(false, 'it\'s false');
assert.ok(false, 'it\'s false');
// throws "AssertionError: it's false"
```
## assert.deepEqual(actual, expected[, message])
@ -52,44 +54,48 @@ non-enumerable properties. This can lead to some potentially surprising
results. For example, the following example does not throw an `AssertionError`
because the properties on the [`Error`][] object are non-enumerable:
// WARNING: This does not throw an AssertionError!
assert.deepEqual(Error('a'), Error('b'));
```js
// WARNING: This does not throw an AssertionError!
assert.deepEqual(Error('a'), Error('b'));
```
"Deep" equality means that the enumerable "own" properties of child objects
are evaluated also:
const assert = require('assert');
```js
const assert = require('assert');
const obj1 = {
const obj1 = {
a : {
b : 1
}
};
const obj2 = {
};
const obj2 = {
a : {
b : 2
}
};
const obj3 = {
};
const obj3 = {
a : {
b : 1
}
}
const obj4 = Object.create(obj1);
}
const obj4 = Object.create(obj1);
assert.deepEqual(obj1, obj1);
assert.deepEqual(obj1, obj1);
// OK, object is equal to itself
assert.deepEqual(obj1, obj2);
assert.deepEqual(obj1, obj2);
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
// values of b are different
assert.deepEqual(obj1, obj3);
assert.deepEqual(obj1, obj3);
// OK, objects are equal
assert.deepEqual(obj1, obj4);
assert.deepEqual(obj1, obj4);
// AssertionError: { a: { b: 1 } } deepEqual {}
// Prototypes are ignored
```
If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
@ -100,14 +106,16 @@ parameter is undefined, a default error message is assigned.
Generally identical to `assert.deepEqual` with the exception that primitive
values are compared using the strict equality operator ( `===` ).
const assert = require('assert');
```js
const assert = require('assert');
assert.deepEqual({a:1}, {a:'1'});
assert.deepEqual({a:1}, {a:'1'});
// OK, because 1 == '1'
assert.deepStrictEqual({a:1}, {a:'1'});
assert.deepStrictEqual({a:1}, {a:'1'});
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
// because 1 !== '1' using strict equality
```
If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
@ -129,52 +137,60 @@ to the caller.
The following, for instance, will throw the [`TypeError`][] because there is no
matching error type in the assertion:
assert.doesNotThrow(
```js
assert.doesNotThrow(
function() {
throw new TypeError('Wrong value');
},
SyntaxError
);
);
```
However, the following will result in an `AssertionError` with the message
'Got unwanted exception (TypeError)..':
assert.doesNotThrow(
```js
assert.doesNotThrow(
function() {
throw new TypeError('Wrong value');
},
TypeError
);
);
```
If an `AssertionError` is thrown and a value is provided for the `message`
parameter, the value of `message` will be appended to the `AssertionError`
message:
assert.doesNotThrow(
```js
assert.doesNotThrow(
function() {
throw new TypeError('Wrong value');
},
TypeError,
'Whoops'
);
// Throws: AssertionError: Got unwanted exception (TypeError). Whoops
);
// Throws: AssertionError: Got unwanted exception (TypeError). Whoops
```
## assert.equal(actual, expected[, message])
Tests shallow, coercive equality between the `actual` and `expected` parameters
using the equal comparison operator ( `==` ).
const assert = require('assert');
```js
const assert = require('assert');
assert.equal(1, 1);
assert.equal(1, 1);
// OK, 1 == 1
assert.equal(1, '1');
assert.equal(1, '1');
// OK, 1 == '1'
assert.equal(1, 2);
assert.equal(1, 2);
// AssertionError: 1 == 2
assert.equal({a: {b: 1}}, {a: {b: 1}});
assert.equal({a: {b: 1}}, {a: {b: 1}});
//AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
```
If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
@ -186,60 +202,66 @@ Throws an `AssertionError`. If `message` is falsy, the error message is set as
the values of `actual` and `expected` separated by the provided `operator`.
Otherwise, the error message is the value of `message`.
const assert = require('assert');
```js
const assert = require('assert');
assert.fail(1, 2, undefined, '>');
assert.fail(1, 2, undefined, '>');
// AssertionError: 1 > 2
assert.fail(1, 2, 'whoops', '>');
assert.fail(1, 2, 'whoops', '>');
// AssertionError: whoops
```
## assert.ifError(value)
Throws `value` if `value` is truthy. This is useful when testing the `error`
argument in callbacks.
const assert = require('assert');
```js
const assert = require('assert');
assert.ifError(0); // OK
assert.ifError(1); // Throws 1
assert.ifError('error') // Throws 'error'
assert.ifError(new Error()); // Throws Error
assert.ifError(0); // OK
assert.ifError(1); // Throws 1
assert.ifError('error') // Throws 'error'
assert.ifError(new Error()); // Throws Error
```
## assert.notDeepEqual(actual, expected[, message])
Tests for any deep inequality. Opposite of [`assert.deepEqual`][].
const assert = require('assert');
```js
const assert = require('assert');
const obj1 = {
const obj1 = {
a : {
b : 1
}
};
const obj2 = {
};
const obj2 = {
a : {
b : 2
}
};
const obj3 = {
};
const obj3 = {
a : {
b : 1
}
}
const obj4 = Object.create(obj1);
}
const obj4 = Object.create(obj1);
assert.deepEqual(obj1, obj1);
assert.deepEqual(obj1, obj1);
AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
assert.deepEqual(obj1, obj2);
assert.deepEqual(obj1, obj2);
// OK, obj1 and obj2 are not deeply equal
assert.deepEqual(obj1, obj3);
assert.deepEqual(obj1, obj3);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
assert.deepEqual(obj1, obj4);
assert.deepEqual(obj1, obj4);
// OK, obj1 and obj2 are not deeply equal
```
If the values are deeply equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
@ -249,13 +271,15 @@ parameter is undefined, a default error message is assigned.
Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual`][].
const assert = require('assert');
```js
const assert = require('assert');
assert.notDeepEqual({a:1}, {a:'1'});
assert.notDeepEqual({a:1}, {a:'1'});
// AssertionError: { a: 1 } notDeepEqual { a: '1' }
assert.notDeepStrictEqual({a:1}, {a:'1'});
assert.notDeepStrictEqual({a:1}, {a:'1'});
// OK
```
If the values are deeply and strictly equal, an `AssertionError` is thrown
with a `message` property set equal to the value of the `message` parameter. If
@ -266,16 +290,18 @@ the `message` parameter is undefined, a default error message is assigned.
Tests shallow, coercive inequality with the not equal comparison operator
( `!=` ).
const assert = require('assert');
```js
const assert = require('assert');
assert.notEqual(1, 2);
assert.notEqual(1, 2);
// OK
assert.notEqual(1, 1);
assert.notEqual(1, 1);
// AssertionError: 1 != 1
assert.notEqual(1, '1');
assert.notEqual(1, '1');
// AssertionError: 1 != '1'
```
If the values are equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
@ -286,16 +312,18 @@ parameter is undefined, a default error message is assigned.
Tests strict inequality as determined by the strict not equal operator
( `!==` ).
const assert = require('assert');
```js
const assert = require('assert');
assert.notStrictEqual(1, 2);
assert.notStrictEqual(1, 2);
// OK
assert.notStrictEqual(1, 1);
assert.notStrictEqual(1, 1);
// AssertionError: 1 != 1
assert.notStrictEqual(1, '1');
assert.notStrictEqual(1, '1');
// OK
```
If the values are strictly equal, an `AssertionError` is thrown with a
`message` property set equal to the value of the `message` parameter. If the
@ -305,16 +333,18 @@ If the values are strictly equal, an `AssertionError` is thrown with a
Tests strict equality as determined by the strict equality operator ( `===` ).
const assert = require('assert');
```js
const assert = require('assert');
assert.strictEqual(1, 2);
assert.strictEqual(1, 2);
// AssertionError: 1 === 2
assert.strictEqual(1, 1);
assert.strictEqual(1, 1);
// OK
assert.strictEqual(1, '1');
assert.strictEqual(1, '1');
// AssertionError: 1 === '1'
```
If the values are not strictly equal, an `AssertionError` is thrown with a
`message` property set equal to the value of the `message` parameter. If the
@ -327,25 +357,30 @@ constructor, [`RegExp`][], or validation function.
Validate instanceof using constructor:
assert.throws(
```js
assert.throws(
function() {
throw new Error('Wrong value');
},
Error
);
);
```
Validate error message using [`RegExp`][]:
assert.throws(
```js
assert.throws(
function() {
throw new Error('Wrong value');
},
/value/
);
);
```
Custom error validation:
assert.throws(
```js
assert.throws(
function() {
throw new Error('Wrong value');
},
@ -355,7 +390,8 @@ Custom error validation:
}
},
'unexpected error'
);
);
```
[Locked]: documentation.html#documentation_stability_index
[`assert.deepEqual`]: #assert_assert_deepequal_actual_expected_message

664
doc/api/buffer.markdown

File diff suppressed because it is too large

280
doc/api/child_process.markdown

@ -6,20 +6,22 @@ The `child_process` module provides the ability to spawn child processes in
a manner that is similar, but not identical, to [`popen(3)`][]. This capability
is primarily provided by the `child_process.spawn()` function:
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
```js
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
});
ls.stderr.on('data', (data) => {
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
});
ls.on('close', (code) => {
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
});
```
By default, pipes for `stdin`, `stdout` and `stderr` are established between
the parent Node.js process and the spawned child. It is possible to stream data
@ -81,31 +83,33 @@ When running on Windows, `.bat` and `.cmd` files can only be invoked using
either `child_process.exec()` or by spawning `cmd.exe` and passing the `.bat`
or `.cmd` file as an argument (which is what `child_process.exec()` does).
// On Windows Only ...
const spawn = require('child_process').spawn;
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
```js
// On Windows Only ...
const spawn = require('child_process').spawn;
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
bat.stdout.on('data', (data) => {
bat.stdout.on('data', (data) => {
console.log(data);
});
});
bat.stderr.on('data', (data) => {
bat.stderr.on('data', (data) => {
console.log(data);
});
});
bat.on('exit', (code) => {
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
});
// OR...
const exec = require('child_process').exec;
exec('my.bat', (err, stdout, stderr) => {
// OR...
const exec = require('child_process').exec;
exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
});
```
### child_process.exec(command[, options][, callback])
@ -133,15 +137,17 @@ or `.cmd` file as an argument (which is what `child_process.exec()` does).
Spawns a shell then executes the `command` within that shell, buffering any
generated output.
const exec = require('child_process').exec;
const child = exec('cat *.js bad_file | wc -l',
```js
const exec = require('child_process').exec;
const child = exec('cat *.js bad_file | wc -l',
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
});
```
If a `callback` function is provided, it is called with the arguments
`(error, stdout, stderr)`. On success, `error` will be `null`. On error,
@ -153,14 +159,16 @@ to be an error.
The `options` argument may be passed as the second argument to customize how
the process is spawned. The default options are:
{
```js
{
encoding: 'utf8',
timeout: 0,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: null,
env: null
}
}
```
If `timeout` is greater than `0`, the parent will send the the signal
identified by the `killSignal` property (the default is `'SIGTERM'`) if the
@ -201,13 +209,15 @@ is spawned directly as a new process making it slightly more efficient than
The same options as `child_process.exec()` are supported. Since a shell is not
spawned, behaviors such as I/O redirection and file globbing are not supported.
const execFile = require('child_process').execFile;
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
```js
const execFile = require('child_process').execFile;
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
});
```
### child_process.fork(modulePath[, args][, options])
@ -275,10 +285,12 @@ to an empty array.
A third argument may be used to specify additional options, with these defaults:
{
```js
{
cwd: undefined,
env: process.env
}
}
```
Use `cwd` to specify the working directory from which the process is spawned.
If not given, the default is to inherit the current working directory.
@ -289,66 +301,72 @@ process, the default is `process.env`.
Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the
exit code:
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
```js
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
});
ls.stderr.on('data', (data) => {
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
});
ls.on('close', (code) => {
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
});
```
Example: A very elaborate way to run 'ps ax | grep ssh'
const spawn = require('child_process').spawn;
const ps = spawn('ps', ['ax']);
const grep = spawn('grep', ['ssh']);
```js
const spawn = require('child_process').spawn;
const ps = spawn('ps', ['ax']);
const grep = spawn('grep', ['ssh']);
ps.stdout.on('data', (data) => {
ps.stdout.on('data', (data) => {
grep.stdin.write(data);
});
});
ps.stderr.on('data', (data) => {
ps.stderr.on('data', (data) => {
console.log(`ps stderr: ${data}`);
});
});
ps.on('close', (code) => {
ps.on('close', (code) => {
if (code !== 0) {
console.log(`ps process exited with code ${code}`);
}
grep.stdin.end();
});
});
grep.stdout.on('data', (data) => {
grep.stdout.on('data', (data) => {
console.log(`${data}`);
});
});
grep.stderr.on('data', (data) => {
grep.stderr.on('data', (data) => {
console.log(`grep stderr: ${data}`);
});
});
grep.on('close', (code) => {
grep.on('close', (code) => {
if (code !== 0) {
console.log(`grep process exited with code ${code}`);
}
});
});
```
Example of checking for failed exec:
const spawn = require('child_process').spawn;
const child = spawn('bad_command');
```js
const spawn = require('child_process').spawn;
const child = spawn('bad_command');
child.on('error', (err) => {
child.on('error', (err) => {
console.log('Failed to start child process.');
});
});
```
#### options.detached
@ -371,17 +389,19 @@ there is an established IPC channel between the child and parent.
Example of detaching a long-running process and redirecting its output to a
file:
const fs = require('fs');
const spawn = require('child_process').spawn;
const out = fs.openSync('./out.log', 'a');
const err = fs.openSync('./out.log', 'a');
```js
const fs = require('fs');
const spawn = require('child_process').spawn;
const out = fs.openSync('./out.log', 'a');
const err = fs.openSync('./out.log', 'a');
const child = spawn('prg', [], {
const child = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ]
});
});
child.unref();
child.unref();
```
When using the `detached` option to start a long-running process, the process
will not stay running in the background after the parent exits unless it is
@ -441,17 +461,19 @@ pipes between the parent and child. The value is one of the following:
Example:
const spawn = require('child_process').spawn;
```js
const spawn = require('child_process').spawn;
// Child will use parent's stdios
spawn('prg', [], { stdio: 'inherit' });
// Child will use parent's stdios
spawn('prg', [], { stdio: 'inherit' });
// Spawn child sharing only stderr
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
// Spawn child sharing only stderr
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
// Open an extra fd=4, to interact with programs presenting a
// startd-style interface.
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
// Open an extra fd=4, to interact with programs presenting a
// startd-style interface.
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
```
*It is worth noting that when an IPC channel is established between the
parent and child processes, and the child is a Node.js process, the child
@ -679,16 +701,18 @@ The `child.kill()` methods sends a signal to the child process. If no argument
is given, the process will be sent the `'SIGTERM'` signal. See `signal(7)` for
a list of available signals.
const spawn = require('child_process').spawn;
const grep = spawn('grep', ['ssh']);
```js
const spawn = require('child_process').spawn;
const grep = spawn('grep', ['ssh']);
grep.on('close', (code, signal) => {
grep.on('close', (code, signal) => {
console.log(
`child process terminated due to receipt of signal ${signal}`);
});
});
// Send SIGHUP to process
grep.kill('SIGHUP');
// Send SIGHUP to process
grep.kill('SIGHUP');
```
The `ChildProcess` object may emit an `'error'` event if the signal cannot be
delivered. Sending a signal to a child process that has already exited is not
@ -709,11 +733,13 @@ Returns the process identifier (PID) of the child process.
Example:
const spawn = require('child_process').spawn;
const grep = spawn('grep', ['ssh']);
```js
const spawn = require('child_process').spawn;
const grep = spawn('grep', ['ssh']);
console.log(`Spawned child pid: ${grep.pid}`);
grep.stdin.end();
console.log(`Spawned child pid: ${grep.pid}`);
grep.stdin.end();
```
### child.send(message[, sendHandle][, callback])
@ -729,22 +755,26 @@ instance, these messages can be received via the `process.on('message')` event.
For example, in the parent script:
const cp = require('child_process');
const n = cp.fork(`${__dirname}/sub.js`);
```js
const cp = require('child_process');
const n = cp.fork(`${__dirname}/sub.js`);
n.on('message', (m) => {
n.on('message', (m) => {
console.log('PARENT got message:', m);
});
});
n.send({ hello: 'world' });
n.send({ hello: 'world' });
```
And then the child script, `'sub.js'` might look like this:
process.on('message', (m) => {
```js
process.on('message', (m) => {
console.log('CHILD got message:', m);
});
});
process.send({ foo: 'bar' });
process.send({ foo: 'bar' });
```
Child Node.js processes will have a `process.send()` method of their own that
allows the child to send messages back to the parent.
@ -780,26 +810,30 @@ used to implement flow control.
The `sendHandle` argument can be used, for instance, to pass the handle of
a TSCP server object to the child process as illustrated in the example below:
const child = require('child_process').fork('child.js');
```js
const child = require('child_process').fork('child.js');
// Open up the server object and send the handle.
const server = require('net').createServer();
server.on('connection', (socket) => {
// Open up the server object and send the handle.
const server = require('net').createServer();
server.on('connection', (socket) => {
socket.end('handled by parent');
});
server.listen(1337, () => {
});
server.listen(1337, () => {
child.send('server', server);
});
});
```
The child would then receive the server object as:
process.on('message', (m, server) => {
```js
process.on('message', (m, server) => {
if (m === 'server') {
server.on('connection', (socket) => {
socket.end('handled by child');
});
}
});
});
```
Once the server is now shared between the parent and child, some connections
can be handled by the parent and some by the child.
@ -815,12 +849,13 @@ Similarly, the `sendHandler` argument can be used to pass the handle of a
socket to the child process. The example below spawns two children that each
handle connections with "normal" or "special" priority:
const normal = require('child_process').fork('child.js', ['normal']);
const special = require('child_process').fork('child.js', ['special']);
```js
const normal = require('child_process').fork('child.js', ['normal']);
const special = require('child_process').fork('child.js', ['special']);
// Open up the server and send sockets to child
const server = require('net').createServer();
server.on('connection', (socket) => {
// Open up the server and send sockets to child
const server = require('net').createServer();
server.on('connection', (socket) => {
// If this is special priority
if (socket.remoteAddress === '74.125.127.100') {
@ -829,17 +864,20 @@ handle connections with "normal" or "special" priority:
}
// This is normal priority
normal.send('socket', socket);
});
server.listen(1337);
});
server.listen(1337);
```
The `child.js` would receive the socket handle as the second argument passed
to the event callback function:
process.on('message', (m, socket) => {
```js
process.on('message', (m, socket) => {
if (m === 'socket') {
socket.end(`Request handled with ${process.argv[2]} priority`);
}
});
});
```
Once a socket has been passed to a child, the parent is no longer capable of
tracking when the socket is destroyed. To indicate this, the `.connections`
@ -887,26 +925,28 @@ In the following example, only the child's fd `1` (stdout) is configured as a
pipe, so only the parent's `child.stdio[1]` is a stream, all other values in
the array are `null`.
const assert = require('assert');
const fs = require('fs');
const child_process = require('child_process');
```js
const assert = require('assert');
const fs = require('fs');
const child_process = require('child_process');
const child = child_process.spawn('ls', {
const child = child_process.spawn('ls', {
stdio: [
0, // Use parents stdin for child
'pipe', // Pipe child's stdout to parent
fs.openSync('err.out', 'w') // Direct child's stderr to a file
]
});
});
assert.equal(child.stdio[0], null);
assert.equal(child.stdio[0], child.stdin);
assert.equal(child.stdio[0], null);
assert.equal(child.stdio[0], child.stdin);
assert(child.stdout);
assert.equal(child.stdio[1], child.stdout);
assert(child.stdout);
assert.equal(child.stdio[1], child.stdout);
assert.equal(child.stdio[2], null);
assert.equal(child.stdio[2], child.stderr);
assert.equal(child.stdio[2], null);
assert.equal(child.stdio[2], child.stderr);
```
### child.stdout

178
doc/api/cluster.markdown

@ -9,11 +9,12 @@ processes to handle the load.
The cluster module allows you to easily create child processes that
all share server ports.
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
```js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
@ -22,22 +23,25 @@ all share server ports.
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
}
}
```
Running Node.js will now share port 8000 between the workers:
% NODE_DEBUG=cluster node server.js
23521,Master Worker 23524 online
23521,Master Worker 23526 online
23521,Master Worker 23523 online
23521,Master Worker 23528 online
```
% NODE_DEBUG=cluster node server.js
23521,Master Worker 23524 online
23521,Master Worker 23526 online
23521,Master Worker 23523 online
23521,Master Worker 23528 online
```
Please note that, on Windows, it is not yet possible to set up a named pipe
server in a worker.
@ -113,9 +117,11 @@ it can be obtained using `cluster.worker`.
Similar to the `cluster.on('disconnect')` event, but specific to this worker.
cluster.fork().on('disconnect', () => {
```js
cluster.fork().on('disconnect', () => {
// Worker has disconnected
});
});
```
### Event: 'error'
@ -131,8 +137,9 @@ In a worker you can also use `process.on('error')`.
Similar to the `cluster.on('exit')` event, but specific to this worker.
const worker = cluster.fork();
worker.on('exit', (code, signal) => {
```js
const worker = cluster.fork();
worker.on('exit', (code, signal) => {
if( signal ) {
console.log(`worker was killed by signal: ${signal}`);
} else if( code !== 0 ) {
@ -140,7 +147,8 @@ Similar to the `cluster.on('exit')` event, but specific to this worker.
} else {
console.log('worker success!');
}
});
});
```
### Event: 'listening'
@ -148,9 +156,11 @@ Similar to the `cluster.on('exit')` event, but specific to this worker.
Similar to the `cluster.on('listening')` event, but specific to this worker.
cluster.fork().on('listening', (address) => {
```js
cluster.fork().on('listening', (address) => {
// Worker is listening
});
});
```
It is not emitted in the worker.
@ -167,10 +177,11 @@ In a worker you can also use `process.on('message')`.
As an example, here is a cluster that keeps count of the number of requests
in the master process using the message system:
const cluster = require('cluster');
const http = require('http');
```js
const cluster = require('cluster');
const http = require('http');
if (cluster.isMaster) {
if (cluster.isMaster) {
// Keep track of http requests
var numReqs = 0;
@ -195,7 +206,7 @@ in the master process using the message system:
cluster.workers[id].on('message', messageHandler);
});
} else {
} else {
// Worker processes have a http server.
http.Server((req, res) => {
@ -205,15 +216,18 @@ in the master process using the message system:
// notify master about the request
process.send({ cmd: 'notifyRequest' });
}).listen(8000);
}
}
```
### Event: 'online'
Similar to the `cluster.on('online')` event, but specific to this worker.
cluster.fork().on('online', () => {
```js
cluster.fork().on('online', () => {
// Worker is online
});
});
```
It is not emitted in the worker.
@ -245,7 +259,8 @@ may be useful to send a message, so application specific actions may be taken to
close them. It also may be useful to implement a timeout, killing a worker if
the `'disconnect'` event has not been emitted after some time.
if (cluster.isMaster) {
```js
if (cluster.isMaster) {
var worker = cluster.fork();
var timeout;
@ -261,7 +276,7 @@ the `'disconnect'` event has not been emitted after some time.
clearTimeout(timeout);
});
} else if (cluster.isWorker) {
} else if (cluster.isWorker) {
const net = require('net');
var server = net.createServer((socket) => {
// connections never end
@ -274,7 +289,8 @@ the `'disconnect'` event has not been emitted after some time.
// initiate graceful close of any connections to server
}
});
}
}
```
### worker.id
@ -344,15 +360,17 @@ In a worker this sends a message to the master. It is identical to
This example will echo back all messages from the master:
if (cluster.isMaster) {
```js
if (cluster.isMaster) {
var worker = cluster.fork();
worker.send('hi there');
} else if (cluster.isWorker) {
} else if (cluster.isWorker) {
process.on('message', (msg) => {
process.send(msg);
});
}
}
```
### worker.suicide
@ -363,14 +381,16 @@ Set by calling `.kill()` or `.disconnect()`, until then it is `undefined`.
The boolean `worker.suicide` lets you distinguish between voluntary and accidental
exit, the master may choose not to respawn a worker based on this value.
cluster.on('exit', (worker, code, signal) => {
```js
cluster.on('exit', (worker, code, signal) => {
if (worker.suicide === true) {
console.log('Oh, it was just suicide\' – no need to worry').
}
});
});
// kill worker
worker.kill();
// kill worker
worker.kill();
```
## Event: 'disconnect'
@ -384,9 +404,11 @@ There may be a delay between the `'disconnect'` and `'exit'` events. These even
can be used to detect if the process is stuck in a cleanup or if there are
long-living connections.
cluster.on('disconnect', (worker) => {
```js
cluster.on('disconnect', (worker) => {
console.log(`The worker #${worker.id} has disconnected`);
});
});
```
## Event: 'exit'
@ -399,11 +421,13 @@ When any of the workers die the cluster module will emit the `'exit'` event.
This can be used to restart the worker by calling `.fork()` again.
cluster.on('exit', (worker, code, signal) => {
```js
cluster.on('exit', (worker, code, signal) => {
console.log('worker %d died (%s). restarting...',
worker.process.pid, signal || code);
cluster.fork();
});
});
```
See [child_process event: 'exit'][].
@ -414,21 +438,23 @@ See [child_process event: 'exit'][].
When a new worker is forked the cluster module will emit a `'fork'` event.
This can be used to log worker activity, and create your own timeout.
var timeouts = [];
function errorMsg() {
```js
var timeouts = [];
function errorMsg() {
console.error('Something must be wrong with the connection ...');
}
}
cluster.on('fork', (worker) => {
cluster.on('fork', (worker) => {
timeouts[worker.id] = setTimeout(errorMsg, 2000);
});
cluster.on('listening', (worker, address) => {
});
cluster.on('listening', (worker, address) => {
clearTimeout(timeouts[worker.id]);
});
cluster.on('exit', (worker, code, signal) => {
});
cluster.on('exit', (worker, code, signal) => {
clearTimeout(timeouts[worker.id]);
errorMsg();
});
});
```
## Event: 'listening'
@ -443,10 +469,12 @@ object and the `address` object contains the following connection properties:
`address`, `port` and `addressType`. This is very useful if the worker is listening
on more than one address.
cluster.on('listening', (worker, address) => {
```js
cluster.on('listening', (worker, address) => {
console.log(
`A worker is now connected to ${address.address}:${address.port}`);
});
});
```
The `addressType` is one of:
@ -473,9 +501,11 @@ When the master receives an online message it will emit this event.
The difference between `'fork'` and `'online'` is that fork is emitted when the
master forks a worker, and 'online' is emitted when the worker is running.
cluster.on('online', (worker) => {
```js
cluster.on('online', (worker) => {
console.log('Yay, the worker responded after it was forked');
});
});
```
## Event: 'setup'
@ -585,17 +615,19 @@ Note that:
Example:
const cluster = require('cluster');
cluster.setupMaster({
```js
const cluster = require('cluster');
cluster.setupMaster({
exec: 'worker.js',
args: ['--use', 'https'],
silent: true
});
cluster.fork(); // https worker
cluster.setupMaster({
});
cluster.fork(); // https worker
cluster.setupMaster({
args: ['--use', 'http']
});
cluster.fork(); // http worker
});
cluster.fork(); // http worker
```
This can only be called from the master process.
@ -605,15 +637,17 @@ This can only be called from the master process.
A reference to the current worker object. Not available in the master process.
const cluster = require('cluster');
```js
const cluster = require('cluster');
if (cluster.isMaster) {
if (cluster.isMaster) {
console.log('I am master');
cluster.fork();
cluster.fork();
} else if (cluster.isWorker) {
} else if (cluster.isWorker) {
console.log(`I am worker #${cluster.worker.id}`);
}
}
```
## cluster.workers
@ -628,22 +662,26 @@ exited. The order between these two events cannot be determined in advance.
However, it is guaranteed that the removal from the cluster.workers list happens
before last `'disconnect'` or `'exit'` event is emitted.
// Go through all workers
function eachWorker(callback) {
```js
// Go through all workers
function eachWorker(callback) {
for (var id in cluster.workers) {
callback(cluster.workers[id]);
}
}
eachWorker((worker) => {
}
eachWorker((worker) => {
worker.send('big announcement to all workers');
});
});
```
Should you wish to reference a worker over a communication channel, using
the worker's unique id is the easiest way to find the worker.
socket.on('data', (id) => {
```js
socket.on('data', (id) => {
var worker = cluster.workers[id];
});
});
```
[`child_process.fork()`]: child_process.html#child_process_child_process_fork_modulepath_args_options
[`ChildProcess.send()`]: child_process.html#child_process_child_send_message_sendhandle_callback

100
doc/api/console.markdown

@ -15,33 +15,37 @@ The module exports two specific components:
Example using the global `console`:
console.log('hello world');
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
console.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to stderr
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
While the API for the `Console` class is designed fundamentally around the
Web browser `console` object, the `Console` is Node.js is *not* intended to
@ -55,7 +59,9 @@ when the destination is a pipe (to avoid blocking for long periods of time).
In the following example, stdout is non-blocking while stderr is blocking:
$ node script.js 2> error.log | tee info.log
```
$ node script.js 2> error.log | tee info.log
```
Typically, the distinction between blocking/non-blocking is not important
unless an application is logging significant amounts of data. High volume
@ -69,8 +75,10 @@ The `Console` class can be used to create a simple logger with configurable
output streams and can be accessed using either `require('console').Console`
or `console.Console`:
const Console = require('console').Console;
const Console = console.Console;
```js
const Console = require('console').Console;
const Console = console.Console;
```
### new Console(stdout[, stderr])
@ -79,19 +87,23 @@ Creates a new `Console` by passing one or two writable stream instances.
is used for warning or error output. If `stderr` isn't passed, the warning
and error output will be sent to the `stdout`.
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5
```js
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5
```
The global `console` is a special `Console` whose output is sent to
`process.stdout` and `process.stderr`. It is equivalent to calling:
new Console(process.stdout, process.stderr);
```js
new Console(process.stdout, process.stderr);
```
### console.assert(value[, message][, ...])
@ -99,10 +111,12 @@ A simple assertion test that verifies whether `value` is truthy. If it is not,
an `AssertionError` is throw. If provided, the error `message` is formatted
using [`util.format()`][] and used as the error message.
console.assert(true, 'does nothing');
```js
console.assert(true, 'does nothing');
// OK
console.assert(false, 'Whoops %s', 'didn\'t work');
console.assert(false, 'Whoops %s', 'didn\'t work');
// AssertionError: Whoops didn't work
```
### console.dir(obj[, options])
@ -129,11 +143,13 @@ used as the primary message and all additional used as substitution
values similar to `printf()` (the arguments are all passed to
[`util.format()`][]).
const code = 5;
console.error('error #%d', code);
```js
const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
console.error('error', code);
// Prints: error 5, to stderr
```
If formatting elements (e.g. `%d`) are not found in the first string then
[`util.inspect()`][] is called on each argument and the resulting string
@ -150,11 +166,13 @@ used as the primary message and all additional used as substitution
values similar to `printf()` (the arguments are all passed to
[`util.format()`][]).
var count = 5;
console.log('count: %d', count);
```js
var count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count: ', count);
console.log('count: ', count);
// Prints: count: 5, to stdout
```
If formatting elements (e.g. `%d`) are not found in the first string then
[`util.inspect()`][] is called on each argument and the resulting string
@ -172,19 +190,22 @@ milliseconds to stdout. Timer durations are accurate to the sub-millisecond.
Stops a timer that was previously started by calling [`console.time()`][] and
prints the result to stdout:
console.time('100-elements');
for (var i = 0; i < 100; i++) {
```js
console.time('100-elements');
for (var i = 0; i < 100; i++) {
;
}
console.timeEnd('100-elements');
// prints 100-elements: 225.438ms
}
console.timeEnd('100-elements');
// prints 100-elements: 225.438ms
```
### console.trace(message[, ...])
Prints to stderr the string `'Trace :'`, followed by the [`util.format()`][]
formatted message and stack trace to the current position in the code.
console.trace('Show me');
```js
console.trace('Show me');
// Prints: (stack trace will vary based on where trace is called)
// Trace: Show me
// at repl:2:9
@ -197,6 +218,7 @@ formatted message and stack trace to the current position in the code.
// at REPLServer.Interface._onLine (readline.js:210:10)
// at REPLServer.Interface._line (readline.js:549:8)
// at REPLServer.Interface._ttyWrite (readline.js:826:14)
```
### console.warn([data][, ...])

436
doc/api/crypto.markdown

@ -7,15 +7,17 @@ wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign and verify functions.
Use `require('crypto')` to access this module.
const crypto = require('crypto');
```js
const crypto = require('crypto');
const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('hex');
console.log(hash);
console.log(hash);
// Prints:
// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
```
## Class: Certificate
@ -31,10 +33,12 @@ data. The most common usage is handling output generated by the HTML5
Instances of the `Certificate` class can be created using the `new` keyword
or by calling `crypto.Certificate()` as a function:
const crypto = require('crypto');
```js
const crypto = require('crypto');
const cert1 = new crypto.Certificate();
const cert2 = crypto.Certificate();
const cert1 = new crypto.Certificate();
const cert2 = crypto.Certificate();
```
### certificate.exportChallenge(spkac)
@ -43,11 +47,13 @@ The `spkac` data structure includes a public key and a challenge. The
form of a Node.js [`Buffer`][]. The `spkac` argument can be either a string
or a [`Buffer`][].
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
const challenge = cert.exportChallenge(spkac);
console.log(challenge.toString('utf8'));
```js
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
const challenge = cert.exportChallenge(spkac);
console.log(challenge.toString('utf8'));
// Prints the challenge as a UTF8 string
```
### Certificate.exportPublicKey(spkac)
@ -56,21 +62,25 @@ The `spkac` data structure includes a public key and a challenge. The
form of a Node.js [`Buffer`][]. The `spkac` argument can be either a string
or a [`Buffer`][].
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
const publicKey = cert.exportPublicKey(spkac);
console.log(publicKey);
```js
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
const publicKey = cert.exportPublicKey(spkac);
console.log(publicKey);
// Prints the public key as <Buffer ...>
```
### Certificate.verifySpkac(spkac)
Returns `true` if the given `spkac` data structure is valid, `false` otherwise.
The `spkac` argument must be a Node.js [`Buffer`][].
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
console.log(cert.verifySpkac(new Buffer(spkac)));
```js
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
console.log(cert.verifySpkac(new Buffer(spkac)));
// Prints true or false
```
## Class: Cipher
@ -88,38 +98,44 @@ using the `new` keyword.
Example: Using `Cipher` objects as streams:
const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');
```js
const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');
cipher.on('readable', () => {
cipher.on('readable', () => {
var data = cipher.read();
if (data)
console.log(data.toString('hex'));
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
});
});
cipher.write('clear text data');
cipher.end();
cipher.write('clear text data');
cipher.end();
```
Example: Using `Cipher` and piped streams:
const crypto = require('crypto');
const fs = require('fs');
const cipher = crypto.createCipher('aes192', 'a password');
```js
const crypto = require('crypto');
const fs = require('fs');
const cipher = crypto.createCipher('aes192', 'a password');
const input = fs.createReadStream('test.js');
const output = fs.createWriteStream('test.enc');
const input = fs.createReadStream('test.js');
const output = fs.createWriteStream('test.enc');
input.pipe(cipher).pipe(output);
input.pipe(cipher).pipe(output);
```
Example: Using the `cipher.update()` and `cipher.final()` methods:
const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');
```js
const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');
cipher.update('clear text data');
console.log(cipher.final('hex'));
cipher.update('clear text data');
console.log(cipher.final('hex'));
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
```
### cipher.final([output_encoding])
@ -192,38 +208,44 @@ directly using the `new` keyword.
Example: Using `Decipher` objects as streams:
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');
```js
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');
decipher.on('readable', () => {
decipher.on('readable', () => {
var data = decipher.read();
if (data)
console.log(data.toString());
// Prints: clear text data
});
});
decipher.write('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
decipher.end();
decipher.write('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
decipher.end();
```
Example: Using `Decipher` and piped streams:
const crypto = require('crypto');
const fs = require('fs');
const decipher = crypto.createDecipher('aes192', 'a password');
```
const crypto = require('crypto');
const fs = require('fs');
const decipher = crypto.createDecipher('aes192', 'a password');
const input = fs.createReadStream('test.enc');
const output = fs.createWriteStream('test.js');
const input = fs.createReadStream('test.enc');
const output = fs.createWriteStream('test.js');
input.pipe(decipher).pipe(output);
input.pipe(decipher).pipe(output);
```
Example: Using the `decipher.update()` and `decipher.final()` methods:
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');
```js
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');
decipher.update('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
console.log(decipher.final('utf8'));
decipher.update('b919f20fc5ac2f9c1d2cce94cb1d9c2d', 'hex');
console.log(decipher.final('utf8'));
// Prints: clear text data
```
### decipher.final([output_encoding])
@ -286,23 +308,25 @@ exchanges.
Instances of the `DiffieHellman` class can be created using the
`crypto.createDiffieHellman()` function.
const crypto = require('crypto');
const assert = require('assert');
```js
const crypto = require('crypto');
const assert = require('assert');
// Generate Alice's keys...
const alice = crypto.createDiffieHellman(11);
const alice_key = alice.generateKeys();
// Generate Alice's keys...
const alice = crypto.createDiffieHellman(11);
const alice_key = alice.generateKeys();
// Generate Bob's keys...
const bob = crypto.createDiffieHellman(11);
const bob_key = bob.generateKeys();
// Generate Bob's keys...
const bob = crypto.createDiffieHellman(11);
const bob_key = bob.generateKeys();
// Exchange and generate the secret...
const alice_secret = alice.computeSecret(bob_key);
const bob_secret = bob.computeSecret(alice_key);
// Exchange and generate the secret...
const alice_secret = alice.computeSecret(bob_key);
const bob_secret = bob.computeSecret(alice_key);
assert(alice_secret, bob_secret);
assert(alice_secret, bob_secret);
// OK
```
### diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding])
@ -383,23 +407,25 @@ key exchanges.
Instances of the `ECDH` class can be created using the
`crypto.createECDH()` function.
const crypto = require('crypto');
const assert = require('assert');
```js
const crypto = require('crypto');
const assert = require('assert');
// Generate Alice's keys...
const alice = crypto.createECDH('secp521r1');
const alice_key = alice.generateKeys();
// Generate Alice's keys...
const alice = crypto.createECDH('secp521r1');
const alice_key = alice.generateKeys();
// Generate Bob's keys...
const bob = crypto.createECDH('secp521r1');
const bob_key = bob.generateKeys();
// Generate Bob's keys...
const bob = crypto.createECDH('secp521r1');
const bob_key = bob.generateKeys();
// Exchange and generate the secret...
const alice_secret = alice.computeSecret(bob_key);
const bob_secret = bob.computeSecret(alice_key);
// Exchange and generate the secret...
const alice_secret = alice.computeSecret(bob_key);
const bob_secret = bob.computeSecret(alice_key);
assert(alice_secret, bob_secret);
assert(alice_secret, bob_secret);
// OK
```
### ECDH.computeSecret(other_public_key[, input_encoding][, output_encoding])
@ -471,25 +497,27 @@ public point/key associated with the private key being set.
Example (obtaining a shared secret):
const crypto = require('crypto');
const alice = crypto.createECDH('secp256k1');
const bob = crypto.createECDH('secp256k1');
```js
const crypto = require('crypto');
const alice = crypto.createECDH('secp256k1');
const bob = crypto.createECDH('secp256k1');
// Note: This is a shortcut way to specify one of Alice's previous private
// keys. It would be unwise to use such a predictable private key in a real
// application.
alice.setPrivateKey(
// Note: This is a shortcut way to specify one of Alice's previous private
// keys. It would be unwise to use such a predictable private key in a real
// application.
alice.setPrivateKey(
crypto.createHash('sha256').update('alice', 'utf8').digest()
);
);
// Bob uses a newly generated cryptographically strong
// pseudorandom key pair bob.generateKeys();
// Bob uses a newly generated cryptographically strong
// pseudorandom key pair bob.generateKeys();
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
// alice_secret and bob_secret should be the same shared secret value
console.log(alice_secret === bob_secret);
// alice_secret and bob_secret should be the same shared secret value
console.log(alice_secret === bob_secret);
```
## Class: Hash
@ -506,38 +534,44 @@ objects are not to be created directly using the `new` keyword.
Example: Using `Hash` objects as streams:
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
```js
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.on('readable', () => {
hash.on('readable', () => {
var data = hash.read();
if (data)
console.log(data.toString('hex'));
// Prints:
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
});
});
hash.write('some data to hash');
hash.end();
hash.write('some data to hash');
hash.end();
```
Example: Using `Hash` and piped streams:
const crypto = require('crypto');
const fs = require('fs');
const hash = crypto.createHash('sha256');
```js
const crypto = require('crypto');
const fs = require('fs');
const hash = crypto.createHash('sha256');
const input = fs.createReadStream('test.js');
input.pipe(hash).pipe(process.stdout);
const input = fs.createReadStream('test.js');
input.pipe(hash).pipe(process.stdout);
```
Example: Using the `hash.update()` and `hash.digest()` methods:
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
```js
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('some data to hash');
console.log(hash.digest('hex'));
hash.update('some data to hash');
console.log(hash.digest('hex'));
// Prints:
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
```
### hash.digest([encoding])
@ -574,38 +608,44 @@ objects are not to be created directly using the `new` keyword.
Example: Using `Hmac` objects as streams:
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'a secret');
```js
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'a secret');
hmac.on('readable', () => {
hmac.on('readable', () => {
var data = hmac.read();
if (data)
console.log(data.toString('hex'));
// Prints:
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
});
});
hmac.write('some data to hash');
hmac.end();
hmac.write('some data to hash');
hmac.end();
```
Example: Using `Hmac` and piped streams:
const crypto = require('crypto');
const fs = require('fs');
const hmac = crypto.createHmac('sha256', 'a secret');
```js
const crypto = require('crypto');
const fs = require('fs');
const hmac = crypto.createHmac('sha256', 'a secret');
const input = fs.createReadStream('test.js');
input.pipe(hmac).pipe(process.stdout);
const input = fs.createReadStream('test.js');
input.pipe(hmac).pipe(process.stdout);
```
Example: Using the `hmac.update()` and `hmac.digest()` methods:
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'a secret');
```js
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'a secret');
hmac.update('some data to hash');
console.log(hmac.digest('hex'));
hmac.update('some data to hash');
console.log(hmac.digest('hex'));
// Prints:
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
```
### hmac.digest([encoding])
@ -636,26 +676,30 @@ objects are not to be created directly using the `new` keyword.
Example: Using `Sign` objects as streams:
const crypto = require('crypto');
const sign = crypto.createSign('rsa-sha256');
```js
const crypto = require('crypto');
const sign = crypto.createSign('rsa-sha256');
sign.write('some data to sign');
sign.end();
sign.write('some data to sign');
sign.end();
const private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex'));
const private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex'));
// Prints the calculated signature
```
Example: Using the `sign.update()` and `sign.sign()` methods:
const crypto = require('crypto');
const sign = crypto.createSign('rsa-sha256');
```js
const crypto = require('crypto');
const sign = crypto.createSign('rsa-sha256');
sign.update('some data to sign');
sign.update('some data to sign');
const private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex'));
const private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex'));
// Prints the calculated signature
```
### sign.sign(private_key[, output_format])
@ -696,28 +740,32 @@ of two ways:
Example: Using `Verify` objects as streams:
const crypto = require('crypto');
const verify = crypto.createVerify('rsa-sha256');
```js
const crypto = require('crypto');
const verify = crypto.createVerify('rsa-sha256');
verify.write('some data to sign');
verify.end();
verify.write('some data to sign');
verify.end();
const public_key = getPublicKeySomehow();
const signature = getSignatureToVerify();
console.log(sign.verify(public_key, signature));
const public_key = getPublicKeySomehow();
const signature = getSignatureToVerify();
console.log(sign.verify(public_key, signature));
// Prints true or false
```
Example: Using the `verify.update()` and `verify.verify()` methods:
const crypto = require('crypto');
const verify = crypto.createVerify('rsa-sha256');
```js
const crypto = require('crypto');
const verify = crypto.createVerify('rsa-sha256');
verify.update('some data to sign');
verify.update('some data to sign');
const public_key = getPublicKeySomehow();
const signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature));
const public_key = getPublicKeySomehow();
const signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature));
// Prints true or false
```
### verifier.update(data)
@ -890,21 +938,23 @@ display the available digest algorithms.
Example: generating the sha256 sum of a file
const filename = process.argv[2];
const crypto = require('crypto');
const fs = require('fs');
```js
const filename = process.argv[2];
const crypto = require('crypto');
const fs = require('fs');
const hash = crypto.createHash('sha256');
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(filename);
input.on('readable', () => {
const input = fs.createReadStream(filename);
input.on('readable', () => {
var data = input.read();
if (data)
hash.update(data);
else {
console.log(`${hash.digest('hex')} ${filename}`);
}
});
});
```
### crypto.createHmac(algorithm, key)
@ -919,21 +969,23 @@ The `key` is the HMAC key used to generate the cryptographic HMAC hash.
Example: generating the sha256 HMAC of a file
const filename = process.argv[2];
const crypto = require('crypto');
const fs = require('fs');
```js
const filename = process.argv[2];
const crypto = require('crypto');
const fs = require('fs');
const hmac = crypto.createHmac('sha256', 'a secret');
const hmac = crypto.createHmac('sha256', 'a secret');
const input = fs.createReadStream(filename);
input.on('readable', () => {
const input = fs.createReadStream(filename);
input.on('readable', () => {
var data = input.read();
if (data)
hmac.update(data);
else {
console.log(`${hmac.digest('hex')} ${filename}`);
}
});
});
```
### crypto.createSign(algorithm)
@ -953,8 +1005,10 @@ Returns an array with the names of the supported cipher algorithms.
Example:
const ciphers = crypto.getCiphers();
console.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...]
```js
const ciphers = crypto.getCiphers();
console.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...]
```
### crypto.getCurves()
@ -962,8 +1016,10 @@ Returns an array with the names of the supported elliptic curves.
Example:
const curves = crypto.getCurves();
console.log(curves); // ['secp256k1', 'secp384r1', ...]
```js
const curves = crypto.getCurves();
console.log(curves); // ['secp256k1', 'secp384r1', ...]
```
### crypto.getDiffieHellman(group_name)
@ -980,18 +1036,20 @@ and communication time.
Example (obtaining a shared secret):
const crypto = require('crypto');
const alice = crypto.getDiffieHellman('modp14');
const bob = crypto.getDiffieHellman('modp14');
```js
const crypto = require('crypto');
const alice = crypto.getDiffieHellman('modp14');
const bob = crypto.getDiffieHellman('modp14');
alice.generateKeys();
bob.generateKeys();
alice.generateKeys();
bob.generateKeys();
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
/* alice_secret and bob_secret should be the same */
console.log(alice_secret == bob_secret);
/* alice_secret and bob_secret should be the same */
console.log(alice_secret == bob_secret);
```
### crypto.getHashes()
@ -999,8 +1057,10 @@ Returns an array with the names of the supported hash algorithms.
Example:
const hashes = crypto.getHashes();
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
```js
const hashes = crypto.getHashes();
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
```
### crypto.pbkdf2(password, salt, iterations, keylen[, digest], callback)
@ -1024,11 +1084,13 @@ salts are random and their lengths are greater than 16 bytes. See
Example:
const crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => {
```js
const crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => {
if (err) throw err;
console.log(key.toString('hex')); // 'c5e478d...1469e50'
});
});
```
An array of supported digest functions can be retrieved using
[`crypto.getHashes()`][].
@ -1054,9 +1116,11 @@ salts are random and their lengths are greater than 16 bytes. See
Example:
const crypto = require('crypto');
const key = crypto.pbkdf2sync('secret', 'salt', 100000, 512, 'sha512');
console.log(key.toString('hex')); // 'c5e478d...1469e50'
```js
const crypto = require('crypto');
const key = crypto.pbkdf2sync('secret', 'salt', 100000, 512, 'sha512');
console.log(key.toString('hex')); // 'c5e478d...1469e50'
```
An array of supported digest functions can be retrieved using
[`crypto.getHashes()`][].
@ -1149,22 +1213,26 @@ and the `callback` function is invoked with two arguments: `err` and `buf`.
If an error occurs, `err` will be an Error object; otherwise it is null. The
`buf` argument is a [`Buffer`][] containing the generated bytes.
// Asynchronous
const crypto = require('crypto');
crypto.randomBytes(256, (err, buf) => {
```js
// Asynchronous
const crypto = require('crypto');
crypto.randomBytes(256, (err, buf) => {
if (err) throw err;
console.log(
`${buf.length}` bytes of random data: ${buf.toString('hex')});
});
});
```
If the `callback` function is not provided, the random bytes are generated
synchronously and returned as a [`Buffer`][]. An error will be thrown if
there is a problem generating the bytes.
// Synchronous
const buf = crypto.randomBytes(256);
console.log(
```js
// Synchronous
const buf = crypto.randomBytes(256);
console.log(
`${buf.length}` bytes of random data: ${buf.toString('hex')});
```
The `crypto.randomBytes()` method will block until there is sufficient entropy.
This should normally never take longer than a few milliseconds. The only time

87
doc/api/debugger.markdown

@ -9,14 +9,16 @@ via a simple [TCP-based protocol][] and built-in debugging client. To use it,
start Node.js with the `debug` argument followed by the path to the script to
debug; a prompt will be displayed indicating successful launch of the debugger:
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
```
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug>
debug>
```
Node.js's debugger client does not yet support the full range of commands, but
simple step and inspection are possible.
@ -26,55 +28,58 @@ enable a breakpoint at that position in the code.
For example, suppose `myscript.js` is written as:
// myscript.js
x = 5;
setTimeout(function () {
```js
// myscript.js
x = 5;
setTimeout(function () {
debugger;
console.log('world');
}, 1000);
console.log('hello');
}, 1000);
console.log('hello');
```
Once the debugger is run, a breakpoint will occur at line 4:
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
```
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug> cont
< hello
break in /home/indutny/Code/git/indutny/myscript.js:3
debug> cont
< hello
break in /home/indutny/Code/git/indutny/myscript.js:3
1 x = 5;
2 setTimeout(function () {
3 debugger;
4 console.log('world');
5 }, 1000);
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
2 setTimeout(function () {
3 debugger;
4 console.log('world');
5 }, 1000);
6 console.log('hello');
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< world
break in /home/indutny/Code/git/indutny/myscript.js:5
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< world
break in /home/indutny/Code/git/indutny/myscript.js:5
3 debugger;
4 console.log('world');
5 }, 1000);
6 console.log('hello');
7
debug> quit
%
debug> quit
%
```
The `repl` command allows code to be evaluated remotely. The `next` command
steps over to the next line. Type `help` to see what other commands are
@ -115,26 +120,28 @@ on line 1
It is also possible to set a breakpoint in a file (module) that
isn't loaded yet:
% ./node debug test/fixtures/break-in-module/main.js
< debugger listening on port 5858
connecting to port 5858... ok
break in test/fixtures/break-in-module/main.js:1
```
% ./node debug test/fixtures/break-in-module/main.js
< debugger listening on port 5858
connecting to port 5858... ok
break in test/fixtures/break-in-module/main.js:1
1 var mod = require('./mod.js');
2 mod.hello();
3 mod.hello();
debug> setBreakpoint('mod.js', 23)
Warning: script 'mod.js' was not loaded yet.
debug> setBreakpoint('mod.js', 23)
Warning: script 'mod.js' was not loaded yet.
1 var mod = require('./mod.js');
2 mod.hello();
3 mod.hello();
debug> c
break in test/fixtures/break-in-module/mod.js:23
debug> c
break in test/fixtures/break-in-module/mod.js:23
21
22 exports.hello = function() {
23 return 'hello from module';
24 };
25
debug>
debug>
```
### Info

84
doc/api/dgram.markdown

@ -6,25 +6,27 @@
The `dgram` module provides an implementation of UDP Datagram sockets.
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
```js
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
});
server.on('message', (msg, rinfo) => {
server.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
});
server.on('listening', () => {
server.on('listening', () => {
var address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
});
server.bind(41234);
// server listening 0.0.0.0:41234
server.bind(41234);
// server listening 0.0.0.0:41234
```
## Class: dgram.Socket
@ -61,10 +63,12 @@ The event handler function is passed two arguments: `msg` and `rinfo`. The
`msg` argument is a [`Buffer`][] and `rinfo` is an object with the sender's
address information provided by the `address`, `family` and `port` properties:
socket.on('message', (msg, rinfo) => {
```js
socket.on('message', (msg, rinfo) => {
console.log('Received %d bytes from %s:%d\n',
msg.length, rinfo.address, rinfo.port);
});
});
```
### socket.addMembership(multicastAddress[, multicastInterface])
@ -108,25 +112,27 @@ attempting to bind with a closed socket), an [`Error`][] may be thrown.
Example of a UDP server listening on port 41234:
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
```js
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
});
server.on('message', (msg, rinfo) => {
server.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
});
server.on('listening', () => {
server.on('listening', () => {
var address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
});
server.bind(41234);
// server listening 0.0.0.0:41234
server.bind(41234);
// server listening 0.0.0.0:41234
```
### socket.bind(options[, callback])
@ -153,11 +159,13 @@ port sharing results in an error.
An example socket listening on an exclusive port is shown below.
socket.bind({
```js
socket.bind({
address: 'localhost',
port: 8000,
exclusive: true
});
});
```
### socket.close([callback])
@ -218,12 +226,14 @@ the error is emitted as an `'error'` event on the `socket` object.
Example of sending a UDP packet to a random port on `localhost`;
const dgram = require('dgram');
const message = new Buffer('Some bytes');
const client = dgram.createSocket('udp4');
client.send(message, 0, message.length, 41234, 'localhost', (err) => {
```js
const dgram = require('dgram');
const message = new Buffer('Some bytes');
const client = dgram.createSocket('udp4');
client.send(message, 0, message.length, 41234, 'localhost', (err) => {
client.close();
});
});
```
**A Note about UDP datagram size**
@ -324,17 +334,21 @@ As of Node.js v0.10, [`dgram.Socket#bind()`][] changed to an asynchronous
execution model. Legacy code that assumes synchronous behavior, as in the
following example:
const s = dgram.createSocket('udp4');
s.bind(1234);
s.addMembership('224.0.0.114');
```js
const s = dgram.createSocket('udp4');
s.bind(1234);
s.addMembership('224.0.0.114');
```
Must be changed to pass a callback function to the [`dgram.Socket#bind()`][]
function:
const s = dgram.createSocket('udp4');
s.bind(1234, () => {
```js
const s = dgram.createSocket('udp4');
s.bind(1234, () => {
s.addMembership('224.0.0.114');
});
});
```
## `dgram` module functions

42
doc/api/dns.markdown

@ -12,11 +12,13 @@ the same operating system behave should use [`dns.lookup()`][].__
For example, looking up `nodejs.org`.
const dns = require('dns');
```js
const dns = require('dns');
dns.lookup('nodejs.org', (err, addresses, family) => {
dns.lookup('nodejs.org', (err, addresses, family) => {
console.log('addresses:', addresses);
});
});
```
2) Functions that connect to an actual DNS server to perform name resolution,
and that _always_ use the network to perform DNS queries. This category
@ -29,9 +31,10 @@ for name resolution, and instead want to _always_ perform DNS queries.
Below is an example that resolves `'nodejs.org'` then reverse resolves the IP
addresses that are returned.
const dns = require('dns');
```js
const dns = require('dns');
dns.resolve4('nodejs.org', (err, addresses) => {
dns.resolve4('nodejs.org', (err, addresses) => {
if (err) throw err;
console.log(`addresses: ${JSON.stringify(addresses)}`);
@ -44,7 +47,8 @@ addresses that are returned.
console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
});
});
});
});
```
There are subtle consequences in choosing one over the other, please consult
the [Implementation considerations section][] for more information.
@ -76,11 +80,13 @@ Alternatively, `options` can be an object containing these properties:
All properties are optional. An example usage of options is shown below.
{
```
{
family: 4,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
all: false
}
}
```
The `callback` function has arguments `(err, address, family)`. `address` is a
string representation of an IPv4 or IPv6 address. `family` is either the
@ -125,11 +131,13 @@ The callback has arguments `(err, hostname, service)`. The `hostname` and
On error, `err` is an [`Error`][] object, where `err.code` is the error code.
const dns = require('dns');
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
```js
const dns = require('dns');
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
console.log(hostname, service);
// Prints: localhost ssh
});
});
```
## dns.resolve(hostname[, rrtype], callback)
@ -204,7 +212,8 @@ be an object with the following properties:
* `expire`
* `minttl`
{
```
{
nsname: 'ns.example.com',
hostmaster: 'root.example.com',
serial: 2013101809,
@ -212,7 +221,8 @@ be an object with the following properties:
retry: 2400,
expire: 604800,
minttl: 3600
}
}
```
## dns.resolveSrv(hostname, callback)
@ -225,12 +235,14 @@ be an array of objects with the following properties:
* `port`
* `name`
{
```
{
priority: 10,
weight: 5,
port: 21223,
name: 'service.example.com'
}
}
```
## dns.resolveTxt(hostname, callback)

32
doc/api/domain.markdown

@ -44,7 +44,7 @@ the failure, and react accordingly.
For example, this is not a good idea:
```javascript
```js
// XXX WARNING! BAD IDEA!
var d = require('domain').create();
@ -66,7 +66,7 @@ By using the context of a domain, and the resilience of separating our
program into multiple worker processes, we can react more
appropriately, and handle errors with much greater safety.
```javascript
```js
// Much better!
const cluster = require('cluster');
@ -227,7 +227,7 @@ That is possible via explicit binding.
For example:
```
```js
// create a top-level domain for the server
const domain = require('domain');
const http = require('http');
@ -282,7 +282,7 @@ This is the most basic way to use a domain.
Example:
```
```js
const domain = require('domain');
const fs = require('fs');
const d = domain.create();
@ -345,20 +345,22 @@ thrown will be routed to the domain's `'error'` event.
#### Example
const d = domain.create();
```js
const d = domain.create();
function readSomeFile(filename, cb) {
function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.bind(function(er, data) {
// if this throws, it will also be passed to the domain
return cb(er, data ? JSON.parse(data) : null);
}));
}
}
d.on('error', (er) => {
d.on('error', (er) => {
// an error occurred somewhere.
// if we throw it now, it will crash the program
// with the normal line number and stack message.
});
});
```
### domain.intercept(callback)
@ -374,9 +376,10 @@ with a single error handler in a single place.
#### Example
const d = domain.create();
```js
const d = domain.create();
function readSomeFile(filename, cb) {
function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.intercept(function(data) {
// note, the first argument is never passed to the
// callback since it is assumed to be the 'Error' argument
@ -388,13 +391,14 @@ with a single error handler in a single place.
// the program.
return cb(null, JSON.parse(data));
}));
}
}
d.on('error', (er) => {
d.on('error', (er) => {
// an error occurred somewhere.
// if we throw it now, it will crash the program
// with the normal line number and stack message.
});
});
```
### domain.enter()

120
doc/api/errors.markdown

@ -39,13 +39,15 @@ and throw an error using the standard JavaScript `throw` mechanism. These
are handled using the [`try / catch` construct][] provided by the JavaScript
language.
// Throws with a ReferenceError because z is undefined
try {
```js
// Throws with a ReferenceError because z is undefined
try {
const m = 1;
const n = m + z;
} catch (err) {
} catch (err) {
// Handle the error here.
}
}
```
Any use of the JavaScript `throw` mechanism will raise an exception that
*must* be handled using `try / catch` or the Node.js process will exit
@ -62,7 +64,7 @@ Errors that occur within _Asynchronous APIs_ may be reported in multiple ways:
argument is not `null` and is an instance of `Error`, then an error occurred
that should be handled.
```
```js
const fs = require('fs');
fs.readFile('a file that does not exist', (err, data) => {
if (err) {
@ -75,7 +77,7 @@ Errors that occur within _Asynchronous APIs_ may be reported in multiple ways:
- When an asynchronous method is called on an object that is an `EventEmitter`,
errors can be routed to that object's `'error'` event.
```
```js
const net = require('net');
const connection = net.connect('localhost');
@ -107,14 +109,16 @@ unhandled exception and crash unless either: The [`domain`][] module is used
appropriately or a handler has been registered for the
[`process.on('uncaughtException')`][] event.
const EventEmitter = require('events');
const ee = new EventEmitter();
```js
const EventEmitter = require('events');
const ee = new EventEmitter();
setImmediate(() => {
setImmediate(() => {
// This will crash the process because no 'error' event
// handler has been added.
ee.emit('error', new Error('This will crash'));
});
});
```
Errors generated in this way *cannot* be intercepted using `try / catch` as
they are thrown *after* the calling code has already exited.
@ -133,37 +137,41 @@ either completes or an error is raised, the callback function is called with
the Error object (if any) passed as the first argument. If no error was raised,
the first argument will be passed as `null`.
const fs = require('fs');
```js
const fs = require('fs');
function nodeStyleCallback(err, data) {
function nodeStyleCallback(err, data) {
if (err) {
console.error('There was an error', err);
return;
}
console.log(data);
}
}
fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);
fs.readFile('/some/file/that/does-exist', nodeStyleCallback)
fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);
fs.readFile('/some/file/that/does-exist', nodeStyleCallback)
```
The JavaScript `try / catch` mechanism **cannot** be used to intercept errors
generated by asynchronous APIs. A common mistake for beginners is to try to
use `throw` inside a Node.js style callback:
// THIS WILL NOT WORK:
const fs = require('fs');
```js
// THIS WILL NOT WORK:
const fs = require('fs');
try {
try {
fs.readFile('/some/file/that/does-not-exist', (err, data) => {
// mistaken assumption: throwing here...
if (err) {
throw err;
}
});
} catch(err) {
} catch(err) {
// This will not catch the throw!
console.log(err);
}
}
```
This will not work because the callback function passed to `fs.readFile()` is
called asynchronously. By the time the callback has been called, the
@ -201,9 +209,11 @@ Creates a `.stack` property on `targetObject`, which when accessed returns
a string representing the location in the code at which
`Error.captureStackTrace()` was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack // similar to `new Error().stack`
```js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack // similar to `new Error().stack`
```
The first line of the trace, instead of being prefixed with `ErrorType:
message`, will be the result of calling `targetObject.toString()`.
@ -215,14 +225,16 @@ generated stack trace.
The `constructorOpt` argument is useful for hiding implementation
details of error generation from an end user. For instance:
function MyError() {
```js
function MyError() {
Error.captureStackTrace(this, MyError);
}
}
// Without passing MyError to captureStackTrace, the MyError
// frame would should up in the .stack property. by passing
// the constructor, we omit that frame and all frames above it.
new MyError().stack
// Without passing MyError to captureStackTrace, the MyError
// frame would should up in the .stack property. by passing
// the constructor, we omit that frame and all frames above it.
new MyError().stack
```
### Error.stackTraceLimit
@ -243,9 +255,11 @@ The `message` passed to the constructor will also appear in the first line of
the stack trace of the `Error`, however changing this property after the
`Error` object is created *may not* change the first line of the stack trace.
const err = new Error('The message');
console.log(err.message);
```js
const err = new Error('The message');
console.log(err.message);
// Prints: The message
```
#### error.stack
@ -254,11 +268,13 @@ instantiated.
For example:
Error: Things keep happening!
```
Error: Things keep happening!
at /home/gbusey/file.js:525:2
at Frobnicator.refrobulate (/home/gbusey/business-logic.js:424:21)
at Actor.<anonymous> (/home/gbusey/actors.js:400:8)
at increaseSynergy (/home/gbusey/actors.js:701:6)
```
The first line is formatted as `<error class name>: <error message>`, and
is followed by a series of stack frames (each line beginning with "at ").
@ -276,16 +292,17 @@ function called `cheetahify`, which itself calls a JavaScript function, the
frame representing the `cheetahify` call will **not** be present in the stack
traces:
const cheetahify = require('./native-binding.node');
```js
const cheetahify = require('./native-binding.node');
function makeFaster() {
function makeFaster() {
// cheetahify *synchronously* calls speedy.
cheetahify(function speedy() {
throw new Error('oh no!');
});
}
}
makeFaster(); // will throw:
makeFaster(); // will throw:
// /home/gbusey/file.js:6
// throw new Error('oh no!');
// ^
@ -300,6 +317,7 @@ traces:
// at Function.Module.runMain (module.js:497:10)
// at startup (node.js:119:16)
// at node.js:906:3
```
The location information will be one of:
@ -327,8 +345,10 @@ range, or outside the set of options for a given function parameter.
For example:
require('net').connect(-1);
```js
require('net').connect(-1);
// throws RangeError, port should be > 0 && < 65536
```
Node.js will generate and throw `RangeError` instances *immediately* as a form
of argument validation.
@ -342,19 +362,23 @@ an otherwise broken program.
While client code may generate and propagate these errors, in practice, only V8
will do so.
doesNotExist;
```js
doesNotExist;
// throws ReferenceError, doesNotExist is not a variable in this program.
```
`ReferenceError` instances will have an `error.arguments` property whose value
is an array containing a single element: a string representing the variable
that was not defined.
const assert = require('assert');
try {
```js
const assert = require('assert');
try {
doesNotExist;
} catch(err) {
} catch(err) {
assert(err.arguments[0], 'doesNotExist');
}
}
```
Unless an application is dynamically generating and running code,
`ReferenceError` instances should always be considered a bug in the code
@ -368,11 +392,13 @@ evaluation. Code evaluation may happen as a result of `eval`, `Function`,
`require`, or [vm][]. These errors are almost always indicative of a broken
program.
try {
```js
try {
require('vm').runInThisContext('binary ! isNotOk');
} catch(err) {
} catch(err) {
// err will be a SyntaxError
}
}
```
`SyntaxError` instances are unrecoverable in the context that created them –
they may only be caught by other contexts.
@ -383,8 +409,10 @@ A subclass of `Error` that indicates that a provided argument is not an
allowable type. For example, passing a function to a parameter which expects a
string would be considered a TypeError.
require('url').parse(function() { });
```js
require('url').parse(function() { });
// throws TypeError, since it expected a string
```
Node.js will generate and throw `TypeError` instances *immediately* as a form
of argument validation.

188
doc/api/events.markdown

@ -27,34 +27,38 @@ The following example shows a simple `EventEmitter` instance with a single
listener. The `eventEmitter.on()` method is used to register listeners, while
the `eventEmitter.emit()` method is used to trigger the event.
const EventEmitter = require('events');
const util = require('util');
```js
const EventEmitter = require('events');
const util = require('util');
function MyEmitter() {
function MyEmitter() {
EventEmitter.call(this);
}
util.inherits(MyEmitter, EventEmitter);
}
util.inherits(MyEmitter, EventEmitter);
const myEmitter = new MyEmitter();
myEmitter.on('event', function() {
const myEmitter = new MyEmitter();
myEmitter.on('event', function() {
console.log('an event occurred!');
});
myEmitter.emit('event');
});
myEmitter.emit('event');
```
Any object can become an `EventEmitter` through inheritance. The example above
uses the traditional Node.js style prototypical inheritance using
the `util.inherits()` method. It is, however, possible to use ES6 classes as
well:
const EventEmitter = require('events');
```js
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', function() {
const myEmitter = new MyEmitter();
myEmitter.on('event', function() {
console.log('an event occurred!');
});
myEmitter.emit('event');
});
myEmitter.emit('event');
```
## Passing arguments and `this` to listeners
@ -64,8 +68,9 @@ ordinary listener function is called by the `EventEmitter`, the standard `this`
keyword is intentionally set to reference the `EventEmitter` to which the
listener is attached.
const myEmitter = new MyEmitter();
myEmitter.on('event', function(a, b) {
```js
const myEmitter = new MyEmitter();
myEmitter.on('event', function(a, b) {
console.log(a, b, this);
// Prints:
// a b MyEmitter {
@ -73,18 +78,21 @@ listener is attached.
// _events: { event: [Function] },
// _eventsCount: 1,
// _maxListeners: undefined }
});
myEmitter.emit('event', 'a', 'b');
});
myEmitter.emit('event', 'a', 'b');
```
It is possible to use ES6 Arrow Functions as listeners, however, when doing so,
the `this` keyword will no longer reference the `EventEmitter` instance:
const myEmitter = new MyEmitter();
myEmitter.on('event', (a, b) => {
```js
const myEmitter = new MyEmitter();
myEmitter.on('event', (a, b) => {
console.log(a, b, this);
// Prints: a b {}
});
myEmitter.emit('event', 'a', 'b');
});
myEmitter.emit('event', 'a', 'b');
```
## Asynchronous vs. Synchronous
@ -94,41 +102,47 @@ events and to avoid race conditions or logic errors. When appropriate,
listener functions can switch to an asynchronous mode of operation using
the `setImmediate()` or `process.nextTick()` methods:
const myEmitter = new MyEmitter();
myEmitter.on('event', (a, b) => {
```js
const myEmitter = new MyEmitter();
myEmitter.on('event', (a, b) => {
setImmediate(() => {
console.log('this happens asynchronously');
});
});
myEmitter.emit('event', 'a', 'b');
});
myEmitter.emit('event', 'a', 'b');
```
## Handling events only once
When a listener is registered using the `eventEmitter.on()` method, that
listener will be invoked _every time_ the named event is emitted.
const myEmitter = new MyEmitter();
var m = 0;
myEmitter.on('event', () => {
```js
const myEmitter = new MyEmitter();
var m = 0;
myEmitter.on('event', () => {
console.log(++m);
});
myEmitter.emit('event');
});
myEmitter.emit('event');
// Prints: 1
myEmitter.emit('event');
myEmitter.emit('event');
// Prints: 2
```
Using the `eventEmitter.once()` method, it is possible to register a listener
that is immediately unregistered after it is called.
const myEmitter = new MyEmitter();
var m = 0;
myEmitter.once('event', () => {
```js
const myEmitter = new MyEmitter();
var m = 0;
myEmitter.once('event', () => {
console.log(++m);
});
myEmitter.emit('event');
});
myEmitter.emit('event');
// Prints: 1
myEmitter.emit('event');
myEmitter.emit('event');
// Ignored
```
## Error events
@ -140,39 +154,47 @@ If an `EventEmitter` does _not_ have at least one listener registered for the
`'error'` event, and an `'error'` event is emitted, the error is thrown, a
stack trace is printed, and the Node.js process exits.
const myEmitter = new MyEmitter();
myEmitter.emit('error', new Error('whoops!'));
```js
const myEmitter = new MyEmitter();
myEmitter.emit('error', new Error('whoops!'));
// Throws and crashes Node.js
```
To guard against crashing the Node.js process, developers can either register
a listener for the `process.on('uncaughtException')` event or use the
[`domain`][] module (_Note, however, that the `domain` module has been
deprecated_).
const myEmitter = new MyEmitter();
```js
const myEmitter = new MyEmitter();
process.on('uncaughtException', (err) => {
process.on('uncaughtException', (err) => {
console.log('whoops! there was an error');
});
});
myEmitter.emit('error', new Error('whoops!'));
myEmitter.emit('error', new Error('whoops!'));
// Prints: whoops! there was an error
```
As a best practice, developers should always register listeners for the
`'error'` event:
const myEmitter = new MyEmitter();
myEmitter.on('error', (err) => {
```js
const myEmitter = new MyEmitter();
myEmitter.on('error', (err) => {
console.log('whoops! there was an error');
});
myEmitter.emit('error', new Error('whoops!'));
});
myEmitter.emit('error', new Error('whoops!'));
// Prints: whoops! there was an error
```
## Class: EventEmitter
The `EventEmitter` class is defined and exposed by the `events` module:
const EventEmitter = require('events');
```js
const EventEmitter = require('events');
```
All EventEmitters emit the event `'newListener'` when new listeners are
added and `'removeListener'` when a listener is removed.
@ -193,23 +215,25 @@ but important side effect: any *additional* listeners registered to the same
`name` *within* the `'newListener'` callback will be inserted *before* the
listener that is in the process of being added.
const myEmitter = new MyEmitter();
// Only do this once so we don't loop forever
myEmitter.once('newListener', (event, listener) => {
```js
const myEmitter = new MyEmitter();
// Only do this once so we don't loop forever
myEmitter.once('newListener', (event, listener) => {
if (event === 'event') {
// Insert a new listener in front
myEmitter.on('event', () => {
console.log('B');
});
}
});
myEmitter.on('event', () => {
});
myEmitter.on('event', () => {
console.log('A');
});
myEmitter.emit('event');
});
myEmitter.emit('event');
// Prints:
// B
// A
```
### Event: 'removeListener'
@ -225,11 +249,13 @@ The `'removeListener'` event is emitted *after* a listener is removed.
A class method that returns the number of listeners for the given `event`
registered on the given `emitter`.
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(EventEmitter.listenerCount(myEmitter, 'event'));
```js
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(EventEmitter.listenerCount(myEmitter, 'event'));
// Prints: 2
```
### EventEmitter.defaultMaxListeners
@ -250,11 +276,13 @@ that a `possible EventEmitter memory leak` has been detected. For any single
`EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()`
methods can be used to temporarily avoid this warning:
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', () => {
```js
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', () => {
// do stuff
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
});
});
```
### emitter.addListener(event, listener)
@ -283,11 +311,13 @@ Returns the number of listeners listening to the `event` type.
Returns a copy of the array of listeners for the specified `event`.
server.on('connection', (stream) => {
```js
server.on('connection', (stream) => {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
});
console.log(util.inspect(server.listeners('connection')));
// Prints: [ [Function] ]
```
### emitter.on(event, listener)
@ -297,9 +327,11 @@ been added. Multiple calls passing the same combination of `event` and
`listener` will result in the `listener` being added, and called, multiple
times.
server.on('connection', (stream) => {
```js
server.on('connection', (stream) => {
console.log('someone connected!');
});
});
```
Returns a reference to the `EventEmitter` so calls can be chained.
@ -308,9 +340,11 @@ Returns a reference to the `EventEmitter` so calls can be chained.
Adds a **one time** `listener` function for the `event`. This listener is
invoked only the next time `event` is triggered, after which it is removed.
server.once('connection', (stream) => {
```js
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});
});
```
Returns a reference to the `EventEmitter` so calls can be chained.
@ -329,12 +363,14 @@ Returns a reference to the `EventEmitter` so calls can be chained.
Removes the specified `listener` from the listener array for the specified
`event`.
var callback = function(stream) {
```js
var callback = function(stream) {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
```
`removeListener` will remove, at most, one instance of a listener from the
listener array. If any single listener has been added multiple times to the

141
doc/api/fs.markdown

@ -18,42 +18,50 @@ You can use try/catch to handle exceptions or allow them to bubble up.
Here is an example of the asynchronous version:
const fs = require('fs');
```js
const fs = require('fs');
fs.unlink('/tmp/hello', (err) => {
fs.unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
});
```
Here is the synchronous version:
const fs = require('fs');
```js
const fs = require('fs');
fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
```
With the asynchronous methods there is no guaranteed ordering. So the
following is prone to error:
fs.rename('/tmp/hello', '/tmp/world', (err) => {
```js
fs.rename('/tmp/hello', '/tmp/world', (err) => {
if (err) throw err;
console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
});
fs.stat('/tmp/world', (err, stats) => {
if (err) throw err;
console.log(`stats: ${JSON.stringify(stats)}`);
});
});
```
It could be that `fs.stat` is executed before `fs.rename`.
The correct way to do this is to chain the callbacks.
fs.rename('/tmp/hello', '/tmp/world', (err) => {
```js
fs.rename('/tmp/hello', '/tmp/world', (err) => {
if (err) throw err;
fs.stat('/tmp/world', (err, stats) => {
if (err) throw err;
console.log(`stats: ${JSON.stringify(stats)}`);
});
});
});
```
In busy processes, the programmer is _strongly encouraged_ to use the
asynchronous versions of these calls. The synchronous versions will block
@ -66,23 +74,25 @@ Most fs functions let you omit the callback argument. If you do, a default
callback is used that rethrows errors. To get a trace to the original call
site, set the `NODE_DEBUG` environment variable:
$ cat script.js
function bad() {
```
$ cat script.js
function bad() {
require('fs').readFile('/');
}
bad();
}
bad();
$ env NODE_DEBUG=fs node script.js
fs.js:66
$ env NODE_DEBUG=fs node script.js
fs.js:66
throw err;
^
Error: EISDIR, read
Error: EISDIR, read
at rethrow (fs.js:61:21)
at maybeCallback (fs.js:79:42)
at Object.fs.readFile (fs.js:153:18)
at bad (/path/to/script.js:2:17)
at Object.<anonymous> (/path/to/script.js:5:1)
<etc.>
```
## Class: fs.FSWatcher
@ -136,7 +146,9 @@ synchronous counterparts are of this type.
For a regular file [`util.inspect(stats)`][] would return a string very
similar to this:
{ dev: 2114,
```js
{
dev: 2114,
ino: 48064969,
mode: 33188,
nlink: 1,
@ -149,7 +161,9 @@ similar to this:
atime: Mon, 10 Oct 2011 23:24:11 GMT,
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
birthtime: Mon, 10 Oct 2011 23:24:11 GMT
}
```
Please note that `atime`, `mtime`, `birthtime`, and `ctime` are
instances of [`Date`][MDN-Date] object and to compare the values of
@ -224,9 +238,11 @@ a possible error argument. If any of the accessibility checks fail, the error
argument will be populated. The following example checks if the file
`/etc/passwd` can be read and written by the current process.
fs.access('/etc/passwd', fs.R_OK | fs.W_OK, function (err) {
```js
fs.access('/etc/passwd', fs.R_OK | fs.W_OK, function (err) {
console.log(err ? 'no access!' : 'can read/write');
});
});
```
## fs.accessSync(path[, mode])
@ -248,14 +264,18 @@ Asynchronously append data to a file, creating the file if it does not yet exist
Example:
fs.appendFile('message.txt', 'data to append', (err) => {
```js
fs.appendFile('message.txt', 'data to append', (err) => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
});
```
If `options` is a string, then it specifies the encoding. Example:
fs.appendFile('message.txt', 'data to append', 'utf8', callback);
```js
fs.appendFile('message.txt', 'data to append', 'utf8', callback);
```
Any specified file descriptor has to have been opened for appending.
@ -302,12 +322,15 @@ default value of 64 kb for the same parameter.
`options` is an object or string with the following defaults:
{ flags: 'r',
```js
{
flags: 'r',
encoding: null,
fd: null,
mode: 0o666,
autoClose: true
}
}
```
`options` can include `start` and `end` values to read a range of bytes from
the file instead of the entire file. Both `start` and `end` are inclusive and
@ -329,7 +352,9 @@ file was created.
An example to read the last 10 bytes of a file which is 100 bytes long:
fs.createReadStream('sample.txt', {start: 90, end: 99});
```js
fs.createReadStream('sample.txt', {start: 90, end: 99});
```
If `options` is a string, then it specifies the encoding.
@ -339,11 +364,15 @@ Returns a new [`WriteStream`][] object. (See [Writable Stream][]).
`options` is an object or string with the following defaults:
{ flags: 'w',
```js
{
flags: 'w',
defaultEncoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true }
autoClose: true
}
```
`options` may also include a `start` option to allow writing data at
some position past the beginning of the file. Modifying a file rather
@ -370,9 +399,11 @@ If `options` is a string, then it specifies the encoding.
Test whether or not the given path exists by checking with the file system.
Then call the `callback` argument with either true or false. Example:
fs.exists('/etc/passwd', (exists) => {
```js
fs.exists('/etc/passwd', (exists) => {
console.log(exists ? 'it\'s there' : 'no passwd!');
});
});
```
`fs.exists()` should not be used to check if a file exists before calling
`fs.open()`. Doing so introduces a race condition since other processes may
@ -594,10 +625,12 @@ Synchronous readdir(3). Returns an array of filenames excluding `'.'` and
Asynchronously reads the entire contents of a file. Example:
fs.readFile('/etc/passwd', (err, data) => {
```js
fs.readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});
});
```
The callback is passed two arguments `(err, data)`, where `data` is the
contents of the file.
@ -606,7 +639,9 @@ If no encoding is specified, then the raw buffer is returned.
If `options` is a string, then it specifies the encoding. Example:
fs.readFile('/etc/passwd', 'utf8', callback);
```js
fs.readFile('/etc/passwd', 'utf8', callback);
```
Any specified file descriptor has to support reading.
@ -637,11 +672,13 @@ resolution or avoid additional `fs.stat` calls for known real paths.
Example:
var cache = {'/etc':'/private/etc'};
fs.realpath('/etc/passwd', cache, (err, resolvedPath) => {
```js
var cache = {'/etc':'/private/etc'};
fs.realpath('/etc/passwd', cache, (err, resolvedPath) => {
if (err) throw err;
console.log(resolvedPath);
});
});
```
## fs.readSync(fd, buffer, offset, length, position)
@ -692,7 +729,9 @@ Note that Windows junction points require the destination path to be absolute.
Here is an example below:
fs.symlink('./foo', './new-port');
```js
fs.symlink('./foo', './new-port');
```
It would create a symlic link named with "new-port" that points to "foo".
@ -805,14 +844,16 @@ Windows. Even on supported platforms, `filename` is not always guaranteed to
be provided. Therefore, don't assume that `filename` argument is always
provided in the callback, and have some fallback logic if it is null.
fs.watch('somedir', (event, filename) => {
```js
fs.watch('somedir', (event, filename) => {
console.log(`event is: ${event}`);
if (filename) {
console.log(`filename provided: ${filename}`);
} else {
console.log('filename not provided');
}
});
});
```
## fs.watchFile(filename[, options], listener)
@ -829,10 +870,12 @@ target should be polled in milliseconds. The default is
The `listener` gets two arguments the current stat object and the previous
stat object:
fs.watchFile('message.text', (curr, prev) => {
```js
fs.watchFile('message.text', (curr, prev) => {
console.log(`the current mtime is: ${curr.mtime}`);
console.log(`the previous mtime was: ${prev.mtime}`);
});
});
```
These stat objects are instances of `fs.Stat`.
@ -915,14 +958,18 @@ to `'utf8'`.
Example:
fs.writeFile('message.txt', 'Hello Node.js', (err) => {
```js
fs.writeFile('message.txt', 'Hello Node.js', (err) => {
if (err) throw err;
console.log('It\'s saved!');
});
});
```
If `options` is a string, then it specifies the encoding. Example:
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
```js
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
```
Any specified file descriptor has to support writing.

16
doc/api/globals.markdown

@ -23,8 +23,10 @@ The name of the directory that the currently executing script resides in.
Example: running `node example.js` from `/Users/mjr`
console.log(__dirname);
// /Users/mjr
```js
console.log(__dirname);
// /Users/mjr
```
`__dirname` isn't actually a global but rather local to each module.
@ -41,8 +43,10 @@ to that module file.
Example: running `node example.js` from `/Users/mjr`
console.log(__filename);
// /Users/mjr/example.js
```js
console.log(__filename);
// /Users/mjr/example.js
```
`__filename` isn't actually a global but rather local to each module.
@ -139,7 +143,9 @@ Instruct `require` on how to handle certain file extensions.
Process files with the extension `.sjs` as `.js`:
require.extensions['.sjs'] = require.extensions['.js'];
```js
require.extensions['.sjs'] = require.extensions['.js'];
```
**Deprecated** In the past, this list has been used to load
non-JavaScript modules into Node.js by compiling them on-demand.

214
doc/api/http.markdown

@ -12,11 +12,13 @@ user is able to stream data.
HTTP message headers are represented by an object like this:
{ 'content-length': '123',
```
{ 'content-length': '123',
'content-type': 'text/plain',
'connection': 'keep-alive',
'host': 'mysite.com',
'accept': '*/*' }
```
Keys are lowercased. Values are not modified.
@ -32,12 +34,14 @@ property, which is an array of `[key, value, key2, value2, ...]`. For
example, the previous message header object might have a `rawHeaders`
list like the following:
[ 'ConTent-Length', '123456',
```
[ 'ConTent-Length', '123456',
'content-LENGTH', '123',
'content-type', 'text/plain',
'CONNECTION', 'keep-alive',
'Host', 'mysite.com',
'accepT', '*/*' ]
```
## Class: http.Agent
@ -64,23 +68,27 @@ a `'close'` event or a special `'agentRemove'` event. This means that if
you intend to keep one HTTP request open for a long time and don't
want it to stay in the pool you can do something along the lines of:
http.get(options, (res) => {
```js
http.get(options, (res) => {
// Do stuff
}).on('socket', (socket) => {
}).on('socket', (socket) => {
socket.emit('agentRemove');
});
});
```
Alternatively, you could just opt out of pooling entirely using
`agent:false`:
http.get({
```js
http.get({
hostname: 'localhost',
port: 80,
path: '/',
agent: false // create a new agent just for this one request
}, (res) => {
}, (res) => {
// Do stuff with response
})
})
```
### new Agent([options])
@ -102,7 +110,7 @@ of these values set to their respective defaults.
To configure any of them, you must create your own [`http.Agent`][] object.
```javascript
```js
const http = require('http');
var keepAliveAgent = new http.Agent({ keepAlive: true });
options.agent = keepAliveAgent;
@ -213,16 +221,17 @@ their connections closed.
A client server pair that show you how to listen for the `'connect'` event.
const http = require('http');
const net = require('net');
const url = require('url');
```js
const http = require('http');
const net = require('net');
const url = require('url');
// Create an HTTP tunneling proxy
var proxy = http.createServer( (req, res) => {
// Create an HTTP tunneling proxy
var proxy = http.createServer( (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
});
proxy.on('connect', (req, cltSocket, head) => {
});
proxy.on('connect', (req, cltSocket, head) => {
// connect to an origin server
var srvUrl = url.parse(`http://${req.url}`);
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
@ -233,10 +242,10 @@ A client server pair that show you how to listen for the `'connect'` event.
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
});
});
});
// now that proxy is running
proxy.listen(1337, '127.0.0.1', () => {
// now that proxy is running
proxy.listen(1337, '127.0.0.1', () => {
// make a request to a tunneling proxy
var options = {
@ -264,7 +273,8 @@ A client server pair that show you how to listen for the `'connect'` event.
proxy.close();
});
});
});
});
```
### Event: 'continue'
@ -303,24 +313,25 @@ their connections closed.
A client server pair that show you how to listen for the `'upgrade'` event.
const http = require('http');
```js
const http = require('http');
// Create an HTTP server
var srv = http.createServer( (req, res) => {
// Create an HTTP server
var srv = http.createServer( (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
});
srv.on('upgrade', (req, socket, head) => {
});
srv.on('upgrade', (req, socket, head) => {
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
'\r\n');
socket.pipe(socket); // echo back
});
});
// now that server is running
srv.listen(1337, '127.0.0.1', () => {
// now that server is running
srv.listen(1337, '127.0.0.1', () => {
// make a request
var options = {
@ -340,7 +351,8 @@ A client server pair that show you how to listen for the `'upgrade'` event.
socket.end();
process.exit(0);
});
});
});
```
### request.abort()
@ -624,11 +636,13 @@ be silently discarded.
Note that HTTP requires the `Trailer` header to be sent if you intend to
emit trailers, with a list of the header fields in its value. E.g.,
response.writeHead(200, { 'Content-Type': 'text/plain',
```js
response.writeHead(200, { 'Content-Type': 'text/plain',
'Trailer': 'Content-MD5' });
response.write(fileData);
response.addTrailers({'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667'});
response.end();
response.write(fileData);
response.addTrailers({'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667'});
response.end();
```
Attempting to set a trailer field name that contains invalid characters will
result in a [`TypeError`][] being thrown.
@ -658,7 +672,9 @@ implicitly flushed.
Example:
var contentType = response.getHeader('content-type');
```js
var contentType = response.getHeader('content-type');
```
### response.headersSent
@ -670,7 +686,9 @@ Removes a header that's queued for implicit sending.
Example:
response.removeHeader('Content-Encoding');
```js
response.removeHeader('Content-Encoding');
```
### response.sendDate
@ -688,11 +706,15 @@ here if you need to send multiple headers with the same name.
Example:
response.setHeader('Content-Type', 'text/html');
```js
response.setHeader('Content-Type', 'text/html');
```
or
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
```js
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
```
Attempting to set a header field name that contains invalid characters will
result in a [`TypeError`][] being thrown.
@ -722,7 +744,9 @@ the headers get flushed.
Example:
response.statusCode = 404;
```js
response.statusCode = 404;
```
After response header was sent to the client, this property indicates the
status code which was sent out.
@ -736,7 +760,9 @@ code will be used.
Example:
response.statusMessage = 'Not found';
```js
response.statusMessage = 'Not found';
```
After response header was sent to the client, this property indicates the
status message which was sent out.
@ -781,10 +807,12 @@ argument.
Example:
var body = 'hello world';
response.writeHead(200, {
```js
var body = 'hello world';
response.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/plain' });
```
This method must only be called once on a message and it must
be called before [`response.end()`][] is called.
@ -823,12 +851,14 @@ The request/response headers object.
Key-value pairs of header names and values. Header names are lower-cased.
Example:
// Prints something like:
//
// { 'user-agent': 'curl/7.22.0',
// host: '127.0.0.1:8000',
// accept: '*/*' }
console.log(request.headers);
```js
// Prints something like:
//
// { 'user-agent': 'curl/7.22.0',
// host: '127.0.0.1:8000',
// accept: '*/*' }
console.log(request.headers);
```
Duplicates in raw headers are handled in the following ways, depending on the
header name:
@ -866,17 +896,19 @@ odd-numbered offsets are the associated values.
Header names are not lowercased, and duplicates are not merged.
// Prints something like:
//
// [ 'user-agent',
// 'this is invalid because there can be only one',
// 'User-Agent',
// 'curl/7.22.0',
// 'Host',
// '127.0.0.1:8000',
// 'ACCEPT',
// '*/*' ]
console.log(request.rawHeaders);
```js
// Prints something like:
//
// [ 'user-agent',
// 'this is invalid because there can be only one',
// 'User-Agent',
// 'curl/7.22.0',
// 'Host',
// '127.0.0.1:8000',
// 'ACCEPT',
// '*/*' ]
console.log(request.rawHeaders);
```
### message.rawTrailers
@ -922,32 +954,44 @@ The request/response trailers object. Only populated at the `'end'` event.
Request URL string. This contains only the URL that is
present in the actual HTTP request. If the request is:
GET /status?name=ryan HTTP/1.1\r\n
Accept: text/plain\r\n
\r\n
```
GET /status?name=ryan HTTP/1.1\r\n
Accept: text/plain\r\n
\r\n
```
Then `request.url` will be:
'/status?name=ryan'
```
'/status?name=ryan'
```
If you would like to parse the URL into its parts, you can use
`require('url').parse(request.url)`. Example:
node> require('url').parse('/status?name=ryan')
{ href: '/status?name=ryan',
```
node> require('url').parse('/status?name=ryan')
{
href: '/status?name=ryan',
search: '?name=ryan',
query: 'name=ryan',
pathname: '/status' }
pathname: '/status'
}
```
If you would like to extract the params from the query string,
you can use the `require('querystring').parse` function, or pass
`true` as the second argument to `require('url').parse`. Example:
node> require('url').parse('/status?name=ryan', true)
{ href: '/status?name=ryan',
```
node> require('url').parse('/status?name=ryan', true)
{
href: '/status?name=ryan',
search: '?name=ryan',
query: { name: 'ryan' },
pathname: '/status' }
query: {name: 'ryan'},
pathname: '/status'
}
```
## http.METHODS
@ -985,13 +1029,15 @@ is that it sets the method to GET and calls `req.end()` automatically.
Example:
http.get('http://www.google.com/index.html', (res) => {
```js
http.get('http://www.google.com/index.html', (res) => {
console.log(`Got response: ${res.statusCode}`);
// consume response body
res.resume();
}).on('error', (e) => {
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
});
```
## http.globalAgent
@ -1043,11 +1089,12 @@ upload a file with a POST request, then write to the `ClientRequest` object.
Example:
var postData = querystring.stringify({
```js
var postData = querystring.stringify({
'msg' : 'Hello World!'
});
});
var options = {
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
@ -1056,9 +1103,9 @@ Example:
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
};
var req = http.request(options, (res) => {
var req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
@ -1068,15 +1115,16 @@ Example:
res.on('end', () => {
console.log('No more data in response.')
})
});
});
req.on('error', (e) => {
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
});
// write data to request body
req.write(postData);
req.end();
// write data to request body
req.write(postData);
req.end();
```
Note that in the example `req.end()` was called. With `http.request()` one
must always call `req.end()` to signify that you're done with the request -

80
doc/api/https.markdown

@ -31,33 +31,37 @@ automatically added to the `'request'` event.
Example:
// curl -k https://localhost:8000/
const https = require('https');
const fs = require('fs');
```js
// curl -k https://localhost:8000/
const https = require('https');
const fs = require('fs');
const options = {
const options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
};
https.createServer(options, (req, res) => {
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
}).listen(8000);
```
Or
const https = require('https');
const fs = require('fs');
```js
const https = require('https');
const fs = require('fs');
const options = {
const options = {
pfx: fs.readFileSync('server.pfx')
};
};
https.createServer(options, (req, res) => {
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
}).listen(8000);
```
### server.close([callback])
@ -78,9 +82,10 @@ automatically parsed with [`url.parse()`][].
Example:
const https = require('https');
```js
const https = require('https');
https.get('https://encrypted.google.com/', (res) => {
https.get('https://encrypted.google.com/', (res) => {
console.log('statusCode: ', res.statusCode);
console.log('headers: ', res.headers);
@ -88,9 +93,10 @@ Example:
process.stdout.write(d);
});
}).on('error', (e) => {
}).on('error', (e) => {
console.error(e);
});
});
```
## https.globalAgent
@ -107,28 +113,30 @@ All options from [`http.request()`][] are valid.
Example:
const https = require('https');
```js
const https = require('https');
var options = {
var options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET'
};
};
var req = https.request(options, (res) => {
var req = https.request(options, (res) => {
console.log('statusCode: ', res.statusCode);
console.log('headers: ', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.end();
});
req.end();
req.on('error', (e) => {
req.on('error', (e) => {
console.error(e);
});
});
```
The options argument has the following options
@ -182,25 +190,28 @@ In order to specify these options, use a custom [`Agent`][].
Example:
var options = {
```js
var options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
options.agent = new https.Agent(options);
};
options.agent = new https.Agent(options);
var req = https.request(options, (res) => {
var req = https.request(options, (res) => {
...
}
}
```
Alternatively, opt out of connection pooling by not using an `Agent`.
Example:
var options = {
```js
var options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
@ -208,11 +219,12 @@ Example:
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),
agent: false
};
};
var req = https.request(options, (res) => {
var req = https.request(options, (res) => {
...
}
}
```
[`Agent`]: #https_class_https_agent
[`globalAgent`]: #https_https_globalagent

190
doc/api/modules.markdown

@ -10,20 +10,24 @@ in one-to-one correspondence. As an example, `foo.js` loads the module
The contents of `foo.js`:
const circle = require('./circle.js');
console.log( `The area of a circle of radius 4 is ${circle.area(4)}`);
```js
const circle = require('./circle.js');
console.log( `The area of a circle of radius 4 is ${circle.area(4)}`);
```
The contents of `circle.js`:
const PI = Math.PI;
```js
const PI = Math.PI;
exports.area = function (r) {
exports.area = function (r) {
return PI * r * r;
};
};
exports.circumference = function (r) {
exports.circumference = function (r) {
return 2 * PI * r;
};
};
```
The module `circle.js` has exported the functions `area()` and
`circumference()`. To add functions and objects to the root of your module,
@ -39,20 +43,24 @@ instead of `exports`.
Below, `bar.js` makes use of the `square` module, which exports a constructor:
const square = require('./square.js');
var mySquare = square(2);
console.log(`The area of my square is ${mySquare.area()}`);
```js
const square = require('./square.js');
var mySquare = square(2);
console.log(`The area of my square is ${mySquare.area()}`);
```
The `square` module is defined in `square.js`:
// assigning to exports will not modify module, must use module.exports
module.exports = function(width) {
```js
// assigning to exports will not modify module, must use module.exports
module.exports = function(width) {
return {
area: function() {
return width * width;
}
};
}
}
```
The module system is implemented in the `require("module")` module.
@ -64,7 +72,9 @@ When a file is run directly from Node.js, `require.main` is set to its
`module`. That means that you can determine whether a file has been run
directly by testing
require.main === module
```js
require.main === module
```
For a file `foo.js`, this will be `true` if run via `node foo.js`, but
`false` if run by `require('./foo')`.
@ -137,47 +147,49 @@ the `require.resolve()` function.
Putting together all of the above, here is the high-level algorithm
in pseudocode of what require.resolve does:
require(X) from module at path Y
1. If X is a core module,
```
require(X) from module at path Y
1. If X is a core module,
a. return the core module
b. STOP
2. If X begins with './' or '/' or '../'
2. If X begins with './' or '/' or '../'
a. LOAD_AS_FILE(Y + X)
b. LOAD_AS_DIRECTORY(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW "not found"
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW "not found"
LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text. STOP
2. If X.js is a file, load X.js as JavaScript text. STOP
3. If X.json is a file, parse X.json to a JavaScript Object. STOP
4. If X.node is a file, load X.node as binary addon. STOP
LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text. STOP
2. If X.js is a file, load X.js as JavaScript text. STOP
3. If X.json is a file, parse X.json to a JavaScript Object. STOP
4. If X.node is a file, load X.node as binary addon. STOP
LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
a. Parse X/package.json, and look for "main" field.
b. let M = X + (json main field)
c. LOAD_AS_FILE(M)
2. If X/index.js is a file, load X/index.js as JavaScript text. STOP
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon. STOP
2. If X/index.js is a file, load X/index.js as JavaScript text. STOP
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon. STOP
LOAD_NODE_MODULES(X, START)
1. let DIRS=NODE_MODULES_PATHS(START)
2. for each DIR in DIRS:
LOAD_NODE_MODULES(X, START)
1. let DIRS=NODE_MODULES_PATHS(START)
2. for each DIR in DIRS:
a. LOAD_AS_FILE(DIR/X)
b. LOAD_AS_DIRECTORY(DIR/X)
NODE_MODULES_PATHS(START)
1. let PARTS = path split(START)
2. let I = count of PARTS - 1
3. let DIRS = []
4. while I >= 0,
NODE_MODULES_PATHS(START)
1. let PARTS = path split(START)
2. let I = count of PARTS - 1
3. let DIRS = []
4. while I >= 0,
a. if PARTS[I] = "node_modules" CONTINUE
c. DIR = path join(PARTS[0 .. I] + "node_modules")
b. DIRS = DIRS + DIR
c. let I = I - 1
5. return DIRS
5. return DIRS
```
## Caching
@ -230,28 +242,34 @@ Consider this situation:
`a.js`:
console.log('a starting');
exports.done = false;
const b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');
```
console.log('a starting');
exports.done = false;
const b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');
```
`b.js`:
console.log('b starting');
exports.done = false;
const a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');
```
console.log('b starting');
exports.done = false;
const a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');
```
`main.js`:
console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done=%j, b.done=%j', a.done, b.done);
```
console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done=%j, b.done=%j', a.done, b.done);
```
When `main.js` loads `a.js`, then `a.js` in turn loads `b.js`. At that
point, `b.js` tries to load `a.js`. In order to prevent an infinite
@ -262,15 +280,17 @@ provided to the `a.js` module.
By the time `main.js` has loaded both modules, they're both finished.
The output of this program would thus be:
$ node main.js
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done=true, b.done=true
```
$ node main.js
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done=true, b.done=true
```
If you have cyclic module dependencies in your program, make sure to
plan accordingly.
@ -314,8 +334,10 @@ The first is to create a `package.json` file in the root of the folder,
which specifies a `main` module. An example package.json file might
look like this:
{ "name" : "some-library",
```
{ "name" : "some-library",
"main" : "./lib/some-library.js" }
```
If this was in a folder at `./some-library`, then
`require('./some-library')` would attempt to load
@ -424,22 +446,26 @@ which is probably not what you want to do.
For example suppose we were making a module called `a.js`
const EventEmitter = require('events');
```js
const EventEmitter = require('events');
module.exports = new EventEmitter();
module.exports = new EventEmitter();
// Do some work, and after some time emit
// the 'ready' event from the module itself.
setTimeout(() => {
// Do some work, and after some time emit
// the 'ready' event from the module itself.
setTimeout(() => {
module.exports.emit('ready');
}, 1000);
}, 1000);
```
Then in another file we could do
const a = require('./a');
a.on('ready', () => {
```js
const a = require('./a');
a.on('ready', () => {
console.log('module a is ready');
});
});
```
Note that assignment to `module.exports` must be done immediately. It cannot be
@ -447,14 +473,18 @@ done in any callbacks. This does not work:
x.js:
setTimeout(() => {
```js
setTimeout(() => {
module.exports = { a: 'hello' };
}, 0);
}, 0);
```
y.js:
const x = require('./x');
console.log(x.a);
```js
const x = require('./x');
console.log(x.a);
```
#### exports alias
@ -465,7 +495,8 @@ is no longer bound to the previous value.
To illustrate the behavior, imagine this hypothetical implementation of
`require()`:
function require(...) {
```js
function require(...) {
// ...
function (module, exports) {
// Your module code here
@ -474,7 +505,8 @@ To illustrate the behavior, imagine this hypothetical implementation of
module.exports = some_func; // makes your module export 0
} (module, module.exports);
return module;
}
}
```
As a guideline, if the relationship between `exports` and `module.exports`
seems like magic to you, ignore `exports` and only use `module.exports`.

101
doc/api/net.markdown

@ -45,15 +45,17 @@ Returns an object with three properties, e.g.
Example:
var server = net.createServer((socket) => {
```js
var server = net.createServer((socket) => {
socket.end('goodbye\n');
});
});
// grab a random port.
server.listen(() => {
// grab a random port.
server.listen(() => {
address = server.address();
console.log('opened server on %j', address);
});
});
```
Don't call `server.address()` until the `'listening'` event has been emitted.
@ -127,11 +129,13 @@ underlying handle, allowing connection handling duties to be shared. When
results in an error. An example which listens on an exclusive port is
shown below.
server.listen({
```js
server.listen({
host: 'localhost',
port: 80,
exclusive: true
});
});
```
### server.listen(path[, backlog][, callback])
@ -184,7 +188,8 @@ One issue some users run into is getting `EADDRINUSE` errors. This means that
another server is already running on the requested port. One way of handling this
would be to wait a second and then try again. This can be done with
server.on('error', (e) => {
```js
server.on('error', (e) => {
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
setTimeout(() => {
@ -192,7 +197,8 @@ would be to wait a second and then try again. This can be done with
server.listen(PORT, HOST);
}, 1000);
}
});
});
```
(Note: All sockets in Node.js set `SO_REUSEADDR` already)
@ -233,11 +239,14 @@ Construct a new socket object.
`options` is an object with the following defaults:
{ fd: null,
```js
{
fd: null,
allowHalfOpen: false,
readable: false,
writable: false
}
}
```
`fd` allows you to specify the existing file descriptor of socket.
Set `readable` and/or `writable` to `true` to allow reads and/or writes on this
@ -512,23 +521,27 @@ The `connectListener` parameter will be added as a listener for the
Here is an example of a client of the previously described echo server:
const net = require('net');
const client = net.connect({port: 8124}, () => { //'connect' listener
```js
const net = require('net');
const client = net.connect({port: 8124}, () => { //'connect' listener
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
});
client.on('end', () => {
console.log('disconnected from server');
});
});
```
To connect on the socket `/tmp/echo.sock` the second line would just be
changed to
const client = net.connect({path: '/tmp/echo.sock'});
```js
const client = net.connect({path: '/tmp/echo.sock'});
```
## net.connect(path[, connectListener])
@ -561,24 +574,28 @@ The `connectListener` parameter will be added as a listener for the
Here is an example of a client of the previously described echo server:
const net = require('net');
const client = net.connect({port: 8124},
```js
const net = require('net');
const client = net.connect({port: 8124},
() => { //'connect' listener
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
});
client.on('end', () => {
console.log('disconnected from server');
});
});
```
To connect on the socket `/tmp/echo.sock` the second line would just be
changed to
const client = net.connect({path: '/tmp/echo.sock'});
```js
const client = net.connect({path: '/tmp/echo.sock'});
```
## net.createConnection(path[, connectListener])
@ -605,10 +622,12 @@ automatically set as a listener for the [`'connection'`][] event.
`options` is an object with the following defaults:
{
```js
{
allowHalfOpen: false,
pauseOnConnect: false
}
}
```
If `allowHalfOpen` is `true`, then the socket won't automatically send a FIN
packet when the other end of the socket sends a FIN packet. The socket becomes
@ -623,31 +642,39 @@ original process. To begin reading data from a paused socket, call [`resume()`][
Here is an example of an echo server which listens for connections
on port 8124:
const net = require('net');
const server = net.createServer((c) => { //'connection' listener
```js
const net = require('net');
const server = net.createServer((c) => { //'connection' listener
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(8124, () => { //'listening' listener
});
server.listen(8124, () => { //'listening' listener
console.log('server bound');
});
});
```
Test this by using `telnet`:
telnet localhost 8124
```
telnet localhost 8124
```
To listen on the socket `/tmp/echo.sock` the third line from the last would
just be changed to
server.listen('/tmp/echo.sock', () => { //'listening' listener
```js
server.listen('/tmp/echo.sock', () => { /* 'listening' listener*/ })
```
Use `nc` to connect to a UNIX domain socket server:
nc -U /tmp/echo.sock
```js
nc -U /tmp/echo.sock
```
## net.isIP(input)

8
doc/api/os.markdown

@ -24,7 +24,8 @@ milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).
Example inspection of os.cpus:
[ { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
```js
[ { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 252020,
@ -88,6 +89,7 @@ Example inspection of os.cpus:
sys: 34920,
idle: 1072572010,
irq: 30 } } ]
```
Note that since `nice` values are UNIX centric in Windows the `nice` values of
all processors are always 0.
@ -125,7 +127,8 @@ Windows.
Get a list of network interfaces:
{ lo:
```js
{ lo:
[ { address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
@ -147,6 +150,7 @@ Get a list of network interfaces:
family: 'IPv6',
mac: '01:02:03:0a:0b:0c',
internal: false } ] }
```
Note that due to the underlying implementation this will only return network
interfaces that have been assigned an address.

232
doc/api/path.markdown

@ -14,13 +14,15 @@ Return the last portion of a path. Similar to the Unix `basename` command.
Example:
path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'
```js
path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// returns
'quux'
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// returns
'quux'
```
## path.delimiter
@ -28,21 +30,25 @@ The platform-specific path delimiter, `;` or `':'`.
An example on *nix:
console.log(process.env.PATH)
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
```js
console.log(process.env.PATH)
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter)
// returns
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
process.env.PATH.split(path.delimiter)
// returns
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
```
An example on Windows:
console.log(process.env.PATH)
// 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
```js
console.log(process.env.PATH)
// 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
process.env.PATH.split(path.delimiter)
// returns
['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
process.env.PATH.split(path.delimiter)
// returns
['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
```
## path.dirname(p)
@ -50,9 +56,11 @@ Return the directory name of a path. Similar to the Unix `dirname` command.
Example:
path.dirname('/foo/bar/baz/asdf/quux')
// returns
'/foo/bar/baz/asdf'
```js
path.dirname('/foo/bar/baz/asdf/quux')
// returns
'/foo/bar/baz/asdf'
```
## path.extname(p)
@ -61,49 +69,53 @@ in the last portion of the path. If there is no '.' in the last portion
of the path or the first character of it is '.', then it returns
an empty string. Examples:
path.extname('index.html')
// returns
'.html'
```js
path.extname('index.html')
// returns
'.html'
path.extname('index.coffee.md')
// returns
'.md'
path.extname('index.coffee.md')
// returns
'.md'
path.extname('index.')
// returns
'.'
path.extname('index.')
// returns
'.'
path.extname('index')
// returns
''
path.extname('index')
// returns
''
path.extname('.index')
// returns
''
path.extname('.index')
// returns
''
```
## path.format(pathObject)
Returns a path string from an object, the opposite of [`path.parse`][].
path.format({
```js
path.format({
root : "/",
dir : "/home/user/dir",
base : "file.txt",
ext : ".txt",
name : "file"
})
// returns
'/home/user/dir/file.txt'
})
// returns
'/home/user/dir/file.txt'
// `root` will be used if `dir` is not specified and `name` + `ext` will be used
// if `base` is not specified
path.format({
// `root` will be used if `dir` is not specified and `name` + `ext` will be used
// if `base` is not specified
path.format({
root : "/",
ext : ".txt",
name : "file"
})
// returns
'/file.txt'
})
// returns
'/file.txt'
```
## path.isAbsolute(path)
@ -112,17 +124,21 @@ resolve to the same location, regardless of the working directory.
Posix examples:
path.isAbsolute('/foo/bar') // true
path.isAbsolute('/baz/..') // true
path.isAbsolute('qux/') // false
path.isAbsolute('.') // false
```js
path.isAbsolute('/foo/bar') // true
path.isAbsolute('/baz/..') // true
path.isAbsolute('qux/') // false
path.isAbsolute('.') // false
```
Windows examples:
path.isAbsolute('//server') // true
path.isAbsolute('C:/foo/..') // true
path.isAbsolute('bar\\baz') // false
path.isAbsolute('.') // false
```js
path.isAbsolute('//server') // true
path.isAbsolute('C:/foo/..') // true
path.isAbsolute('bar\\baz') // false
path.isAbsolute('.') // false
```
*Note:* If the path string passed as parameter is a zero-length string, unlike
other path module functions, it will be used as-is and `false` will be
@ -137,13 +153,15 @@ silently ignored. In v0.10 and up, an exception is thrown.
Example:
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns
'/foo/bar/baz/asdf'
```js
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns
'/foo/bar/baz/asdf'
path.join('foo', {}, 'bar')
// throws exception
TypeError: Arguments to path.join must be strings
path.join('foo', {}, 'bar')
// throws exception
TypeError: Arguments to path.join must be strings
```
*Note:* If the arguments to `join` have zero-length strings, unlike other path
module functions, they will be ignored. If the joined path string is a
@ -160,9 +178,11 @@ On Windows backslashes are used.
Example:
path.normalize('/foo/bar//baz/asdf/quux/..')
// returns
'/foo/bar/baz/asdf'
```js
path.normalize('/foo/bar//baz/asdf/quux/..')
// returns
'/foo/bar/baz/asdf'
```
*Note:* If the path string passed as argument is a zero-length string then `'.'`
will be returned, which represents the current working directory.
@ -173,27 +193,31 @@ Returns an object from a path string.
An example on *nix:
path.parse('/home/user/dir/file.txt')
// returns
{
```js
path.parse('/home/user/dir/file.txt')
// returns
{
root : "/",
dir : "/home/user/dir",
base : "file.txt",
ext : ".txt",
name : "file"
}
}
```
An example on Windows:
path.parse('C:\\path\\dir\\index.html')
// returns
{
```js
path.parse('C:\\path\\dir\\index.html')
// returns
{
root : "C:\\",
dir : "C:\\path\\dir",
base : "index.html",
ext : ".html",
name : "index"
}
}
```
## path.posix
@ -208,17 +232,21 @@ At times we have two absolute paths, and we need to derive the relative
path from one to the other. This is actually the reverse transform of
`path.resolve`, which means we see that:
path.resolve(from, path.relative(from, to)) == path.resolve(to)
```js
path.resolve(from, path.relative(from, to)) == path.resolve(to)
```
Examples:
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
// returns
'..\\..\\impl\\bbb'
```js
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
// returns
'..\\..\\impl\\bbb'
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
// returns
'../../impl/bbb'
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
// returns
'../../impl/bbb'
```
*Note:* If the arguments to `relative` have zero-length strings then the current
working directory will be used instead of the zero-length strings. If
@ -236,32 +264,38 @@ gets resolved to the root directory. Non-string `from` arguments are ignored.
Another way to think of it is as a sequence of `cd` commands in a shell.
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
```js
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
```
Is similar to:
cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd
```
cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd
```
The difference is that the different paths don't need to exist and may also be
files.
Examples:
path.resolve('/foo/bar', './baz')
// returns
'/foo/bar/baz'
```js
path.resolve('/foo/bar', './baz')
// returns
'/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/')
// returns
'/tmp/file'
path.resolve('/foo/bar', '/tmp/file/')
// returns
'/tmp/file'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if currently in /home/myself/node, it returns
'/home/myself/node/wwwroot/static_files/gif/image.gif'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if currently in /home/myself/node, it returns
'/home/myself/node/wwwroot/static_files/gif/image.gif'
```
*Note:* If the arguments to `resolve` have zero-length strings then the current
working directory will be used instead of them.
@ -272,15 +306,19 @@ The platform-specific file separator. `'\\'` or `'/'`.
An example on *nix:
'foo/bar/baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
```js
'foo/bar/baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
```
An example on Windows:
'foo\\bar\\baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
```js
'foo\\bar\\baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']
```
## path.win32

368
doc/api/process.markdown

@ -29,13 +29,15 @@ implicitly by the event loop draining.
Example of listening for `'exit'`:
process.on('exit', (code) => {
```js
process.on('exit', (code) => {
// do *NOT* do this
setTimeout(() => {
console.log('This will not run');
}, 0);
console.log('About to exit with code:', code);
});
});
```
## Event: 'message'
@ -71,13 +73,15 @@ event tells you when the list of unhandled rejections shrinks.
For example using the rejection detection hooks in order to keep a map of all
the rejected promise reasons at a given time:
const unhandledRejections = new Map();
process.on('unhandledRejection', (reason, p) => {
```js
const unhandledRejections = new Map();
process.on('unhandledRejection', (reason, p) => {
unhandledRejections.set(p, reason);
});
process.on('rejectionHandled', (p) => {
});
process.on('rejectionHandled', (p) => {
unhandledRejections.delete(p);
});
});
```
This map will grow and shrink over time, reflecting rejections that start
unhandled and then become handled. You could record the errors in some error
@ -93,17 +97,19 @@ a stack trace and exit) will not occur.
Example of listening for `'uncaughtException'`:
process.on('uncaughtException', (err) => {
```js
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
});
setTimeout(() => {
setTimeout(() => {
console.log('This will still run.');
}, 500);
}, 500);
// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');
// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');
```
Note that `'uncaughtException'` is a very crude mechanism for exception
handling.
@ -138,28 +144,34 @@ instance).
Here is an example that logs every unhandled rejection to the console
process.on('unhandledRejection', (reason, p) => {
```js
process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason);
// application specific logging, throwing an error, or other logic here
});
});
```
For example, here is a rejection that will trigger the `'unhandledRejection'`
event:
somePromise.then((res) => {
```js
somePromise.then((res) => {
return reportToUser(JSON.parse(res)); // note the typo
}); // no `.catch` or `.then`
}); // no `.catch` or `.then`
```
Here is an example of a coding pattern that will also trigger
`'unhandledRejection'`:
function SomeResource() {
```js
function SomeResource() {
// Initially set the loaded status to a rejected promise
this.loaded = Promise.reject(new Error('Resource not yet loaded!'));
}
}
var resource = new SomeResource();
// no .catch or .then on resource.loaded for at least a turn
var resource = new SomeResource();
// no .catch or .then on resource.loaded for at least a turn
```
In cases like this, you may not want to track the rejection as a developer
error like you would for other `'unhandledRejection'` events. To address
@ -222,12 +234,14 @@ standard POSIX signal names such as `SIGINT`, `SIGHUP`, etc.
Example of listening for `SIGINT`:
// Start reading from stdin so we don't exit.
process.stdin.resume();
```js
// Start reading from stdin so we don't exit.
process.stdin.resume();
process.on('SIGINT', () => {
process.on('SIGINT', () => {
console.log('Got SIGINT. Press Control-D to exit.');
});
});
```
An easy way to send the `SIGINT` signal is with `Control-C` in most terminal
programs.
@ -275,7 +289,9 @@ generate a core file.
What processor architecture you're running on: `'arm'`, `'ia32'`, or `'x64'`.
console.log('This processor architecture is ' + process.arch);
```js
console.log('This processor architecture is ' + process.arch);
```
## process.argv
@ -283,32 +299,38 @@ An array containing the command line arguments. The first element will be
'node', the second element will be the name of the JavaScript file. The
next elements will be any additional command line arguments.
// print process.argv
process.argv.forEach((val, index, array) => {
```js
// print process.argv
process.argv.forEach((val, index, array) => {
console.log(`${index}: ${val}`);
});
});
```
This will generate:
$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four
```
$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four
```
## process.chdir(directory)
Changes the current working directory of the process or throws an exception if that fails.
console.log(`Starting directory: ${process.cwd()}`);
try {
```js
console.log(`Starting directory: ${process.cwd()}`);
try {
process.chdir('/tmp');
console.log(`New directory: ${process.cwd()}`);
}
catch (err) {
}
catch (err) {
console.log(`chdir: ${err}`);
}
}
```
## process.config
@ -318,14 +340,17 @@ the `config.gypi` file that was produced when running the `./configure` script.
An example of the possible output looks like:
{ target_defaults:
```
{
target_defaults:
{ cflags: [],
default_configuration: 'Release',
defines: [],
include_dirs: [],
libraries: [] },
variables:
{ host_arch: 'x64',
{
host_arch: 'x64',
node_install_npm: 'true',
node_prefix: '',
node_shared_cares: 'false',
@ -337,7 +362,10 @@ An example of the possible output looks like:
node_shared_openssl: 'false',
strict_aliasing: 'true',
target_arch: 'x64',
v8_use_snapshot: 'true' } }
v8_use_snapshot: 'true'
}
}
```
## process.connected
@ -349,7 +377,9 @@ If `process.connected` is false, it is no longer possible to send messages.
Returns the current working directory of the process.
console.log(`Current directory: ${process.cwd()}`);
```js
console.log(`Current directory: ${process.cwd()}`);
```
## process.disconnect()
@ -367,7 +397,8 @@ An object containing the user environment. See environ(7).
An example of this object looks like:
{ TERM: 'xterm-256color',
```js
{ TERM: 'xterm-256color',
SHELL: '/usr/local/bin/bash',
USER: 'maciej',
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
@ -377,16 +408,21 @@ An example of this object looks like:
HOME: '/Users/maciej',
LOGNAME: 'maciej',
_: '/usr/local/bin/node' }
```
You can write to this object, but changes won't be reflected outside of your
process. That means that the following won't work:
$ node -e 'process.env.foo = "bar"' && echo $foo
```
$ node -e 'process.env.foo = "bar"' && echo $foo
```
But this will:
process.env.foo = 'bar';
console.log(process.env.foo);
```js
process.env.foo = 'bar';
console.log(process.env.foo);
```
## process.execArgv
@ -399,15 +435,21 @@ environment as the parent.
Example:
$ node --harmony script.js --version
```
$ node --harmony script.js --version
```
results in process.execArgv:
['--harmony']
```js
['--harmony']
```
and process.argv:
['/usr/local/bin/node', 'script.js', '--version']
```js
['/usr/local/bin/node', 'script.js', '--version']
```
## process.execPath
@ -415,7 +457,9 @@ This is the absolute pathname of the executable that started the process.
Example:
/usr/local/bin/node
```
/usr/local/bin/node
```
## process.exit([code])
@ -425,7 +469,9 @@ Ends the process with the specified `code`. If omitted, exit uses the
To exit with a 'failure' code:
process.exit(1);
```js
process.exit(1);
```
The shell that executed Node.js should see the exit code as 1.
@ -448,9 +494,11 @@ Android)
Gets the effective group identity of the process. (See getegid(2).)
This is the numerical group id, not the group name.
if (process.getegid) {
```js
if (process.getegid) {
console.log(`Current gid: ${process.getegid()}`);
}
}
```
## process.geteuid()
@ -461,9 +509,11 @@ Android)
Gets the effective user identity of the process. (See geteuid(2).)
This is the numerical userid, not the username.
if (process.geteuid) {
```js
if (process.geteuid) {
console.log(`Current uid: ${process.geteuid()}`);
}
}
```
## process.getgid()
@ -473,9 +523,11 @@ Android)
Gets the group identity of the process. (See getgid(2).)
This is the numerical group id, not the group name.
if (process.getgid) {
```js
if (process.getgid) {
console.log(`Current gid: ${process.getgid()}`);
}
}
```
## process.getgroups()
@ -493,9 +545,11 @@ Android)
Gets the user identity of the process. (See getuid(2).)
This is the numerical userid, not the username.
if (process.getuid) {
```js
if (process.getuid) {
console.log(`Current uid: ${process.getuid()}`);
}
}
```
## process.hrtime()
@ -507,16 +561,18 @@ primary use is for measuring performance between intervals.
You may pass in the result of a previous call to `process.hrtime()` to get
a diff reading, useful for benchmarks and measuring intervals:
var time = process.hrtime();
// [ 1800216, 25 ]
```js
var time = process.hrtime();
// [ 1800216, 25 ]
setTimeout(() => {
setTimeout(() => {
var diff = process.hrtime(time);
// [ 1, 552 ]
console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
// benchmark took 1000000527 nanoseconds
}, 1000);
}, 1000);
```
## process.initgroups(user, extra_group)
@ -532,11 +588,13 @@ to be root or have the `CAP_SETGID` capability.
Some care needs to be taken when dropping privileges. Example:
console.log(process.getgroups()); // [ 0 ]
process.initgroups('bnoordhuis', 1000); // switch user
console.log(process.getgroups()); // [ 27, 30, 46, 1000, 0 ]
process.setgid(1000); // drop root gid
console.log(process.getgroups()); // [ 27, 30, 46, 1000 ]
```js
console.log(process.getgroups()); // [ 0 ]
process.initgroups('bnoordhuis', 1000); // switch user
console.log(process.getgroups()); // [ 27, 30, 46, 1000, 0 ]
process.setgid(1000); // drop root gid
console.log(process.getgroups()); // [ 27, 30, 46, 1000 ]
```
## process.kill(pid[, signal])
@ -555,16 +613,18 @@ something other than kill the target process.
Example of sending a signal to yourself:
process.on('SIGHUP', () => {
```js
process.on('SIGHUP', () => {
console.log('Got SIGHUP signal.');
});
});
setTimeout(() => {
setTimeout(() => {
console.log('Exiting.');
process.exit(0);
}, 100);
}, 100);
process.kill(process.pid, 'SIGHUP');
process.kill(process.pid, 'SIGHUP');
```
Note: When SIGUSR1 is received by Node.js it starts the debugger, see
[Signal Events][].
@ -583,15 +643,19 @@ As with `require.main`, it will be `undefined` if there was no entry script.
Returns an object describing the memory usage of the Node.js process
measured in bytes.
const util = require('util');
```js
const util = require('util');
console.log(util.inspect(process.memoryUsage()));
console.log(util.inspect(process.memoryUsage()));
```
This will generate:
{ rss: 4935680,
```js
{ rss: 4935680,
heapTotal: 1826816,
heapUsed: 650472 }
```
`heapTotal` and `heapUsed` refer to V8's memory usage.
@ -607,65 +671,75 @@ This is *not* a simple alias to [`setTimeout(fn, 0)`][], it's much more
efficient. It runs before any additional I/O events (including
timers) fire in subsequent ticks of the event loop.
console.log('start');
process.nextTick(() => {
```js
console.log('start');
process.nextTick(() => {
console.log('nextTick callback');
});
console.log('scheduled');
// Output:
// start
// scheduled
// nextTick callback
});
console.log('scheduled');
// Output:
// start
// scheduled
// nextTick callback
```
This is important in developing APIs where you want to give the user the
chance to assign event handlers after an object has been constructed,
but before any I/O has occurred.
function MyThing(options) {
```js
function MyThing(options) {
this.setupOptions(options);
process.nextTick(() => {
this.startDoingStuff();
}.bind(this));
}
}
var thing = new MyThing();
thing.getReadyForStuff();
var thing = new MyThing();
thing.getReadyForStuff();
// thing.startDoingStuff() gets called now, not before.
// thing.startDoingStuff() gets called now, not before.
```
It is very important for APIs to be either 100% synchronous or 100%
asynchronous. Consider this example:
// WARNING! DO NOT USE! BAD UNSAFE HAZARD!
function maybeSync(arg, cb) {
```js
// WARNING! DO NOT USE! BAD UNSAFE HAZARD!
function maybeSync(arg, cb) {
if (arg) {
cb();
return;
}
fs.stat('file', cb);
}
}
```
This API is hazardous. If you do this:
maybeSync(true, function() {
```js
maybeSync(true, function() {
foo();
});
bar();
});
bar();
```
then it's not clear whether `foo()` or `bar()` will be called first.
This approach is much better:
function definitelyAsync(arg, cb) {
```js
function definitelyAsync(arg, cb) {
if (arg) {
process.nextTick(cb);
return;
}
fs.stat('file', cb);
}
}
```
Note: the nextTick queue is completely drained on each pass of the
event loop **before** additional I/O is processed. As a result,
@ -676,14 +750,18 @@ happening, just like a `while(true);` loop.
The PID of the process.
console.log(`This process is pid ${process.pid}`);
```js
console.log(`This process is pid ${process.pid}`);
```
## process.platform
What platform you're running on:
`'darwin'`, `'freebsd'`, `'linux'`, `'sunos'` or `'win32'`
console.log(`This platform is ${process.platform}`);
```js
console.log(`This platform is ${process.platform}`);
```
## process.release
@ -707,10 +785,12 @@ for the source tarball and headers-only tarball.
e.g.
{ name: 'node',
```js
{ name: 'node',
sourceUrl: 'https://nodejs.org/download/release/v4.0.0/node-v4.0.0.tar.gz',
headersUrl: 'https://nodejs.org/download/release/v4.0.0/node-v4.0.0-headers.tar.gz',
libUrl: 'https://nodejs.org/download/release/v4.0.0/win-x64/node.lib' }
```
In custom builds from non-release versions of the source tree, only the
`name` property may be present. The additional properties should not be
@ -736,7 +816,8 @@ Sets the effective group identity of the process. (See setegid(2).)
This accepts either a numerical ID or a groupname string. If a groupname
is specified, this method blocks while resolving it to a numerical ID.
if (process.getegid && process.setegid) {
```js
if (process.getegid && process.setegid) {
console.log(`Current gid: ${process.getegid()}`);
try {
process.setegid(501);
@ -745,7 +826,8 @@ is specified, this method blocks while resolving it to a numerical ID.
catch (err) {
console.log(`Failed to set gid: ${err}`);
}
}
}
```
## process.seteuid(id)
@ -756,7 +838,8 @@ Sets the effective user identity of the process. (See seteuid(2).)
This accepts either a numerical ID or a username string. If a username
is specified, this method blocks while resolving it to a numerical ID.
if (process.geteuid && process.seteuid) {
```js
if (process.geteuid && process.seteuid) {
console.log(`Current uid: ${process.geteuid()}`);
try {
process.seteuid(501);
@ -765,7 +848,8 @@ is specified, this method blocks while resolving it to a numerical ID.
catch (err) {
console.log(`Failed to set uid: ${err}`);
}
}
}
```
## process.setgid(id)
@ -776,7 +860,8 @@ Sets the group identity of the process. (See setgid(2).) This accepts either
a numerical ID or a groupname string. If a groupname is specified, this method
blocks while resolving it to a numerical ID.
if (process.getgid && process.setgid) {
```js
if (process.getgid && process.setgid) {
console.log(`Current gid: ${process.getgid()}`);
try {
process.setgid(501);
@ -785,7 +870,8 @@ blocks while resolving it to a numerical ID.
catch (err) {
console.log(`Failed to set gid: ${err}`);
}
}
}
```
## process.setgroups(groups)
@ -806,7 +892,8 @@ Sets the user identity of the process. (See setuid(2).) This accepts either
a numerical ID or a username string. If a username is specified, this method
blocks while resolving it to a numerical ID.
if (process.getuid && process.setuid) {
```js
if (process.getuid && process.setuid) {
console.log(`Current uid: ${process.getuid()}`);
try {
process.setuid(501);
@ -815,7 +902,8 @@ blocks while resolving it to a numerical ID.
catch (err) {
console.log(`Failed to set uid: ${err}`);
}
}
}
```
## process.stderr
@ -833,18 +921,20 @@ A `Readable Stream` for stdin (on fd `0`).
Example of opening standard input and listening for both events:
process.stdin.setEncoding('utf8');
```js
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
process.stdin.on('readable', () => {
var chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write(`data: ${chunk}`);
}
});
});
process.stdin.on('end', () => {
process.stdin.on('end', () => {
process.stdout.write('end');
});
});
```
As a Stream, `process.stdin` can also be used in "old" mode that is compatible
with scripts written for node.js prior to v0.10.
@ -863,9 +953,11 @@ A `Writable Stream` to `stdout` (on fd `1`).
For example, a `console.log` equivalent could look like this:
console.log = function(msg) {
```js
console.log = function(msg) {
process.stdout.write(`${msg}\n`);
};
};
```
`process.stderr` and `process.stdout` are unlike other streams in Node.js in
that they cannot be closed (`end()` will throw), they never emit the `'finish'`
@ -876,15 +968,17 @@ should be a very rare occurrence indeed.)
To check if Node.js is being run in a TTY context, read the `isTTY` property
on `process.stderr`, `process.stdout`, or `process.stdin`:
$ node -p "Boolean(process.stdin.isTTY)"
true
$ echo "foo" | node -p "Boolean(process.stdin.isTTY)"
false
```
$ node -p "Boolean(process.stdin.isTTY)"
true
$ echo "foo" | node -p "Boolean(process.stdin.isTTY)"
false
$ node -p "Boolean(process.stdout.isTTY)"
true
$ node -p "Boolean(process.stdout.isTTY)" | cat
false
$ node -p "Boolean(process.stdout.isTTY)"
true
$ node -p "Boolean(process.stdout.isTTY)" | cat
false
```
See [the tty docs][] for more information.
@ -908,11 +1002,13 @@ Sets or reads the process's file mode creation mask. Child processes inherit
the mask from the parent process. Returns the old mask if `mask` argument is
given, otherwise returns the current mask.
const newmask = 0o022;
const oldmask = process.umask(newmask);
console.log(
```js
const newmask = 0o022;
const oldmask = process.umask(newmask);
console.log(
`Changed umask from ${oldmask.toString(8)} to ${newmask.toString(8)}`
);
);
```
## process.uptime()
@ -923,17 +1019,22 @@ Number of seconds Node.js has been running.
A compiled-in property that exposes `NODE_VERSION`.
console.log(`Version: ${process.version}`);
```js
console.log(`Version: ${process.version}`);
```
## process.versions
A property exposing version strings of Node.js and its dependencies.
console.log(process.versions);
```js
console.log(process.versions);
```
Will print something like:
{ http_parser: '2.3.0',
```js
{ http_parser: '2.3.0',
node: '1.1.1',
v8: '4.1.0.14',
uv: '1.3.0',
@ -942,6 +1043,7 @@ Will print something like:
modules: '43',
icu: '55.1',
openssl: '1.0.1k' }
```
[`'message'`]: child_process.html#child_process_event_message
[`ChildProcess.disconnect()`]: child_process.html#child_process_child_disconnect

46
doc/api/punycode.markdown

@ -10,17 +10,21 @@ access it. (To use it with other Node.js versions, use npm to install the
Converts a Punycode string of ASCII-only symbols to a string of Unicode symbols.
// decode domain name parts
punycode.decode('maana-pta'); // 'mañana'
punycode.decode('--dqo34k'); // '☃-⌘'
```js
// decode domain name parts
punycode.decode('maana-pta'); // 'mañana'
punycode.decode('--dqo34k'); // '☃-⌘'
```
## punycode.encode(string)
Converts a string of Unicode symbols to a Punycode string of ASCII-only symbols.
// encode domain name parts
punycode.encode('mañana'); // 'maana-pta'
punycode.encode('☃-⌘'); // '--dqo34k'
```js
// encode domain name parts
punycode.encode('mañana'); // 'maana-pta'
punycode.encode('☃-⌘'); // '--dqo34k'
```
## punycode.toASCII(domain)
@ -28,9 +32,11 @@ Converts a Unicode string representing a domain name to Punycode. Only the
non-ASCII parts of the domain name will be converted, i.e. it doesn't matter if
you call it with a domain that's already in ASCII.
// encode domain names
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
```js
// encode domain names
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
```
## punycode.toUnicode(domain)
@ -38,9 +44,11 @@ Converts a Punycode string representing a domain name to Unicode. Only the
Punycoded parts of the domain name will be converted, i.e. it doesn't matter if
you call it on a string that has already been converted to Unicode.
// decode domain names
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
```js
// decode domain names
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
```
## punycode.ucs2
@ -51,16 +59,20 @@ symbol in the string. While [JavaScript uses UCS-2 internally][], this function
will convert a pair of surrogate halves (each of which UCS-2 exposes as
separate characters) into a single code point, matching UTF-16.
punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
// surrogate pair for U+1D306 tetragram for centre:
punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
```js
punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
// surrogate pair for U+1D306 tetragram for centre:
punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
```
### punycode.ucs2.encode(codePoints)
Creates a string based on an array of numeric code point values.
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
```js
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
```
## punycode.version

44
doc/api/querystring.markdown

@ -26,16 +26,18 @@ it can be used to decode a `non-utf8` encoding string if necessary.
Example:
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }
// Suppose gbkDecodeURIComponent function already exists,
// it can decode `gbk` encoding string
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
```js
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }
// Suppose gbkDecodeURIComponent function already exists,
// it can decode `gbk` encoding string
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
{ decodeURIComponent: gbkDecodeURIComponent })
// returns
{ w: '中文', foo: 'bar' }
// returns
{ w: '中文', foo: 'bar' }
```
## querystring.stringify(obj[, sep][, eq][, options])
@ -48,20 +50,22 @@ it can be used to encode string with `non-utf8` encoding if necessary.
Example:
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
// returns
'foo=bar&baz=qux&baz=quux&corge='
```js
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
// returns
'foo=bar&baz=qux&baz=quux&corge='
querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
// returns
'foo:bar;baz:qux'
querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
// returns
'foo:bar;baz:qux'
// Suppose gbkEncodeURIComponent function already exists,
// it can encode string with `gbk` encoding
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
// Suppose gbkEncodeURIComponent function already exists,
// it can encode string with `gbk` encoding
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
{ encodeURIComponent: gbkEncodeURIComponent })
// returns
'w=%D6%D0%CE%C4&foo=bar'
// returns
'w=%D6%D0%CE%C4&foo=bar'
```
## querystring.unescape

112
doc/api/readline.markdown

@ -9,19 +9,21 @@ Note that once you've invoked this module, your Node.js program will not
terminate until you've closed the interface. Here's how to allow your
program to gracefully exit:
const readline = require('readline');
```js
const readline = require('readline');
const rl = readline.createInterface({
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
});
rl.question('What do you think of Node.js? ', (answer) => {
rl.question('What do you think of Node.js? ', (answer) => {
// TODO: Log the answer in a database
console.log('Thank you for your valuable feedback:', answer);
rl.close();
});
});
```
## Class: Interface
@ -66,9 +68,11 @@ nothing is displayed.
Example usage:
interface.question('What is your favorite food?', (answer) => {
```js
interface.question('What is your favorite food?', (answer) => {
console.log(`Oh, so your favorite food is ${answer}`);
});
});
```
### rl.resume()
@ -89,9 +93,11 @@ This will also resume the `input` stream if it has been paused.
Example:
rl.write('Delete me!');
// Simulate ctrl+u to delete the line written previously
rl.write(null, {ctrl: true, name: 'u'});
```js
rl.write('Delete me!');
// Simulate ctrl+u to delete the line written previously
rl.write(null, {ctrl: true, name: 'u'});
```
## Events
@ -117,9 +123,11 @@ user hits enter, or return. This is a good hook to listen for user input.
Example of listening for `'line'`:
rl.on('line', (cmd) => {
```js
rl.on('line', (cmd) => {
console.log(`You just typed: ${cmd}`);
});
});
```
### Event: 'pause'
@ -132,9 +140,11 @@ Also emitted whenever the `input` stream is not paused and receives the
Example of listening for `'pause'`:
rl.on('pause', () => {
```js
rl.on('pause', () => {
console.log('Readline paused.');
});
});
```
### Event: 'resume'
@ -144,9 +154,11 @@ Emitted whenever the `input` stream is resumed.
Example of listening for `'resume'`:
rl.on('resume', () => {
```js
rl.on('resume', () => {
console.log('Readline resumed.');
});
});
```
### Event: 'SIGCONT'
@ -161,10 +173,12 @@ background.
Example of listening for `SIGCONT`:
rl.on('SIGCONT', () => {
```js
rl.on('SIGCONT', () => {
// `prompt` will automatically resume the stream
rl.prompt();
});
});
```
### Event: 'SIGINT'
@ -176,11 +190,13 @@ stream receives a `SIGINT`, `pause` will be triggered.
Example of listening for `SIGINT`:
rl.on('SIGINT', () => {
```js
rl.on('SIGINT', () => {
rl.question('Are you sure you want to exit?', (answer) => {
if (answer.match(/^y(es)?$/i)) rl.pause();
});
});
});
```
### Event: 'SIGTSTP'
@ -200,24 +216,27 @@ before the program was sent to the background.
Example of listening for `SIGTSTP`:
rl.on('SIGTSTP', () => {
```js
rl.on('SIGTSTP', () => {
// This will override SIGTSTP and prevent the program from going to the
// background.
console.log('Caught SIGTSTP.');
});
});
```
## Example: Tiny CLI
Here's an example of how to use all these together to craft a tiny command
line interface:
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
```js
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('OHAI> ');
rl.prompt();
rl.setPrompt('OHAI> ');
rl.prompt();
rl.on('line', (line) => {
rl.on('line', (line) => {
switch(line.trim()) {
case 'hello':
console.log('world!');
@ -227,26 +246,29 @@ line interface:
break;
}
rl.prompt();
}).on('close', () => {
}).on('close', () => {
console.log('Have a great day!');
process.exit(0);
});
});
```
## Example: Read File Stream Line-by-Line
A common case for `readline`'s `input` option is to pass a filesystem readable
stream to it. This is how one could craft line-by-line parsing of a file:
const readline = require('readline');
const fs = require('fs');
```js
const readline = require('readline');
const fs = require('fs');
const rl = readline.createInterface({
const rl = readline.createInterface({
input: fs.createReadStream('sample.txt')
});
});
rl.on('line', function (line) {
rl.on('line', function (line) {
console.log('Line from file:', line);
});
});
```
## readline.clearLine(stream, dir)
@ -291,27 +313,33 @@ Which ends up looking something like:
Example:
function completer(line) {
```js
function completer(line) {
var completions = '.help .error .exit .quit .q'.split(' ')
var hits = completions.filter((c) => { return c.indexOf(line) == 0 })
// show all completions if none found
return [hits.length ? hits : completions, line]
}
}
```
Also `completer` can be run in async mode if it accepts two arguments:
function completer(linePartial, callback) {
```js
function completer(linePartial, callback) {
callback(null, [['123'], linePartial]);
}
}
```
`createInterface` is commonly used with [`process.stdin`][] and
[`process.stdout`][] in order to accept user input:
const readline = require('readline');
const rl = readline.createInterface({
```js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
});
```
Once you have a readline instance, you most commonly listen for the
`'line'` event.

138
doc/api/repl.markdown

@ -10,16 +10,18 @@ just trying things out.
By executing `node` without any arguments from the command-line you will be
dropped into the REPL. It has simplistic emacs line-editing.
mjr:~$ node
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
> a.forEach(function (v) {
... console.log(v);
... });
1
2
3
```
mjr:~$ node
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
> a.forEach(function (v){
... console.log(v);
... });
1
2
3
```
For advanced line-editors, start Node.js with the environmental variable
`NODE_NO_READLINE=1`. This will start the main and debugger REPL in canonical
@ -27,7 +29,9 @@ terminal settings which will allow you to use with `rlwrap`.
For example, you could add this to your bashrc file:
alias node="env NODE_NO_READLINE=1 rlwrap node"
```
alias node="env NODE_NO_READLINE=1 rlwrap node"
```
## Environment Variable Options
@ -73,28 +77,34 @@ accessing `fs` will `require()` the `fs` module as `global.fs`.
The special variable `_` (underscore) contains the result of the last expression.
> [ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
4
```
> [ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
4
```
The REPL provides access to any variables in the global scope. You can expose
a variable to the REPL explicitly by assigning it to the `context` object
associated with each `REPLServer`. For example:
// repl_test.js
const repl = require('repl');
var msg = 'message';
```js
// repl_test.js
const repl = require('repl');
var msg = 'message';
repl.start('> ').context.m = msg;
repl.start('> ').context.m = msg;
```
Things in the `context` object appear as local within the REPL:
mjr:~$ node repl_test.js
> m
'message'
```
mjr:~$ node repl_test.js
> m
'message'
```
There are a few special REPL commands:
@ -126,17 +136,21 @@ The REPL module internally uses
For example, if you have defined an `inspect()` function on an object, like this:
> var obj = { foo: 'this will not show up in the inspect() output' };
undefined
> obj.inspect = function() {
... return { bar: 'baz' };
... };
[Function]
```
> var obj = {foo: 'this will not show up in the inspect() output'};
undefined
> obj.inspect = function() {
... return {bar: 'baz'};
... };
[Function]
```
and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
> obj
{ bar: 'baz' }
```
> obj
{bar: 'baz'}
```
## Class: REPLServer
@ -152,10 +166,12 @@ to signal `'end'` on the `input` stream.
Example of listening for `exit`:
replServer.on('exit', () => {
```js
replServer.on('exit', () => {
console.log('Got "exit" event from repl!');
process.exit();
});
});
```
### Event: 'reset'
@ -168,15 +184,17 @@ be emitted.
Example of listening for `reset`:
// Extend the initial repl context.
var replServer = repl.start({ options ... });
someExtension.extend(r.context);
```js
// Extend the initial repl context.
var replServer = repl.start({ options ... });
someExtension.extend(r.context);
// When a new context is created extend it as well.
replServer.on('reset', (context) => {
// When a new context is created extend it as well.
replServer.on('reset', (context) => {
console.log('repl has a new context');
someExtension.extend(context);
});
});
```
### replServer.defineCommand(keyword, cmd)
@ -195,22 +213,26 @@ If a function is provided instead of an object for `cmd`, it is treated as the
Example of defining a command:
// repl_test.js
const repl = require('repl');
```js
// repl_test.js
const repl = require('repl');
var replServer = repl.start();
replServer.defineCommand('sayhello', {
var replServer = repl.start();
replServer.defineCommand('sayhello', {
help: 'Say hello',
action: function(name) {
this.write(`Hello, ${name}!\n');
this.write(`Hello, ${name}!\n`);
this.displayPrompt();
}
});
});
```
Example of invoking that command from the REPL:
> .sayhello Node.js User
Hello, Node.js User!
```
> .sayhello Node.js User
Hello, Node.js User!
```
### replServer.displayPrompt([preserveCursor])
@ -277,17 +299,18 @@ will share the same global object but will have unique I/O.
Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
const net = require('net');
const repl = require('repl');
var connections = 0;
```js
const net = require('net');
const repl = require('repl');
var connections = 0;
repl.start({
repl.start({
prompt: 'Node.js via stdin> ',
input: process.stdin,
output: process.stdout
});
});
net.createServer((socket) => {
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via Unix socket> ',
@ -296,9 +319,9 @@ Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
}).on('exit', () => {
socket.end();
})
}).listen('/tmp/node-repl-sock');
}).listen('/tmp/node-repl-sock');
net.createServer((socket) => {
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via TCP socket> ',
@ -307,7 +330,8 @@ Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
}).on('exit', () => {
socket.end();
});
}).listen(5001);
}).listen(5001);
```
Running this program from the command line will start a REPL on stdin. Other
REPL clients may connect through the Unix socket or TCP socket. `telnet` is useful

79
doc/api/stream.markdown

@ -47,7 +47,7 @@ interfaces in your own program, please also refer to
Almost all Node.js programs, no matter how simple, use Streams in some
way. Here is an example of using Streams in an Node.js program:
```javascript
```js
const http = require('http');
var server = http.createServer( (req, res) => {
@ -172,7 +172,7 @@ then be passed as soon as it is available.
If you just want to get all the data out of the stream as fast as
possible, this is the best way to do so.
```javascript
```js
var readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
console.log('got %d bytes of data', chunk.length);
@ -187,7 +187,7 @@ Note that the `'end'` event **will not fire** unless the data is
completely consumed. This can be done by switching into flowing mode,
or by calling `read()` repeatedly until you get to the end.
```javascript
```js
var readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
console.log('got %d bytes of data', chunk.length);
@ -231,7 +231,7 @@ In the former case, `.read()` will return that data. In the latter case,
`.read()` will return null. For instance, in the following example, `foo.txt`
is an empty file:
```javascript
```js
const fs = require('fs');
var rr = fs.createReadStream('foo.txt');
rr.on('readable', () => {
@ -245,7 +245,7 @@ rr.on('end', () => {
The output of running this script is:
```
bash-3.2$ node test.js
$ node test.js
readable: null
end
```
@ -258,7 +258,7 @@ This method returns whether or not the `readable` has been **explicitly**
paused by client code (using `readable.pause()` without a corresponding
`readable.resume()`).
```javascript
```js
var readable = new stream.Readable
readable.isPaused() // === false
@ -276,7 +276,7 @@ This method will cause a stream in flowing mode to stop emitting
`'data'` events, switching out of flowing mode. Any data that becomes
available will remain in the internal buffer.
```javascript
```js
var readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
console.log('got %d bytes of data', chunk.length);
@ -301,7 +301,7 @@ the destination is not overwhelmed by a fast readable stream.
Multiple destinations can be piped to safely.
```javascript
```js
var readable = getReadableStreamSomehow();
var writable = fs.createWriteStream('file.txt');
// All the data from readable goes into 'file.txt'
@ -311,7 +311,7 @@ readable.pipe(writable);
This function returns the destination stream, so you can set up pipe
chains like so:
```javascript
```js
var r = fs.createReadStream('file.txt');
var z = zlib.createGzip();
var w = fs.createWriteStream('file.txt.gz');
@ -320,7 +320,7 @@ r.pipe(z).pipe(w);
For example, emulating the Unix `cat` command:
```javascript
```js
process.stdin.pipe(process.stdout);
```
@ -331,7 +331,7 @@ false }` as `options` to keep the destination stream open.
This keeps `writer` open so that "Goodbye" can be written at the
end.
```javascript
```js
reader.pipe(writer, { end: false });
reader.on('end', () => {
writer.end('Goodbye\n');
@ -362,7 +362,7 @@ This method should only be called in paused mode. In flowing mode,
this method is called automatically until the internal buffer is
drained.
```javascript
```js
var readable = getReadableStreamSomehow();
readable.on('readable', () => {
var chunk;
@ -390,7 +390,7 @@ want to consume the data from a stream, but you *do* want to get to
its `'end'` event, you can call [`readable.resume()`][] to open the flow of
data.
```javascript
```js
var readable = getReadableStreamSomehow();
readable.resume();
readable.on('end', () => {
@ -415,7 +415,7 @@ potentially mangled if you simply pulled the Buffers directly and
called `buf.toString(encoding)` on them. If you want to read the data
as strings, always use this method.
```javascript
```js
var readable = getReadableStreamSomehow();
readable.setEncoding('utf8');
readable.on('data', (chunk) => {
@ -435,7 +435,7 @@ If the destination is not specified, then all pipes are removed.
If the destination is specified, but no pipe is set up for it, then
this is a no-op.
```javascript
```js
var readable = getReadableStreamSomehow();
var writable = fs.createWriteStream('file.txt');
// All the data from readable goes into 'file.txt',
@ -465,7 +465,7 @@ If you find that you must often call `stream.unshift(chunk)` in your
programs, consider implementing a [Transform][] stream instead. (See [API
for Stream Implementors][].)
```javascript
```js
// Pull off a header delimited by \n\n
// use unshift() if we get too much
// Call the callback with (error, header, stream)
@ -499,6 +499,7 @@ function parseHeader(stream, callback) {
}
}
```
Note that, unlike `stream.push(chunk)`, `stream.unshift(chunk)` will not
end the reading process by resetting the internal reading state of the
stream. This can cause unexpected results if `unshift` is called during a
@ -525,7 +526,7 @@ as a convenience for interacting with old Node.js programs and libraries.
For example:
```javascript
```js
const OldReader = require('./old-api-module.js').OldReader;
const Readable = require('stream').Readable;
const oreader = new OldReader;
@ -571,7 +572,7 @@ If a [`writable.write(chunk)`][] call returns false, then the `'drain'`
event will indicate when it is appropriate to begin writing more data
to the stream.
```javascript
```js
// Write the data to the supplied writable stream one million times.
// Be attentive to back-pressure.
function writeOneMillionTimes(writer, data, encoding, callback) {
@ -628,7 +629,7 @@ writer.on('finish', () => {
This is emitted whenever the `pipe()` method is called on a readable
stream, adding this writable to its set of destinations.
```javascript
```js
var writer = getWritableStreamSomehow();
var reader = getReadableStreamSomehow();
writer.on('pipe', (src) => {
@ -645,7 +646,7 @@ reader.pipe(writer);
This is emitted whenever the [`unpipe()`][] method is called on a
readable stream, removing this writable from its set of destinations.
```javascript
```js
var writer = getWritableStreamSomehow();
var reader = getReadableStreamSomehow();
writer.on('unpipe', (src) => {
@ -673,7 +674,7 @@ supplied, the callback is attached as a listener on the `'finish'` event.
Calling [`write()`][] after calling [`end()`][] will raise an error.
```javascript
```js
// write 'hello, ' and then end with 'world!'
var file = fs.createWriteStream('example.txt');
file.write('hello, ');
@ -862,7 +863,7 @@ In classes that extend the Readable class, make sure to call the
Readable constructor so that the buffering settings can be properly
initialized.
#### readable.\_read(size)
#### readable._read(size)
* `size` {Number} Number of bytes to read asynchronously
@ -913,7 +914,7 @@ you may be wrapping a lower-level source which has some sort of
pause/resume mechanism, and a data callback. In those cases, you
could wrap the low-level source object by doing something like this:
```javascript
```js
// source is an object with readStop() and readStart() methods,
// and an `ondata` member that gets called when it has data, and
// an `onend` member that gets called when the data is over.
@ -953,7 +954,7 @@ SourceWrapper.prototype._read = function(size) {
This is a basic example of a Readable stream. It emits the numerals
from 1 to 1,000,000 in ascending order, and then ends.
```javascript
```js
const Readable = require('stream').Readable;
const util = require('util');
util.inherits(Counter, Readable);
@ -986,7 +987,7 @@ string.
However, this would be better implemented as a [Transform][] stream. See
[SimpleProtocol v2][] for a better implementation.
```javascript
```js
// A parser for a simple data protocol.
// The "header" is a JSON object, followed by 2 \n characters, and
// then a message body.
@ -1126,7 +1127,7 @@ and Readable classes respectively. The `'finish'` event is fired after
`end` is fired after all data has been output which is after the callback
in `_flush` has been called.
#### transform.\_flush(callback)
#### transform._flush(callback)
* `callback` {Function} Call this function (optionally with an error
argument) when you are done flushing any remaining data.
@ -1153,7 +1154,7 @@ the class that defines it, and should not be called directly by user
programs. However, you **are** expected to override this method in
your own extension classes.
#### transform.\_transform(chunk, encoding, callback)
#### transform._transform(chunk, encoding, callback)
* `chunk` {Buffer | String} The chunk to be transformed. Will **always**
be a buffer unless the `decodeStrings` option was set to `false`.
@ -1185,7 +1186,7 @@ particular input chunk. If you supply a second argument to the callback
it will be passed to the push method. In other words the following are
equivalent:
```javascript
```js
transform.prototype._transform = function (data, encoding, callback) {
this.push(data);
callback();
@ -1308,7 +1309,7 @@ In classes that extend the Writable class, make sure to call the
constructor so that the buffering settings can be properly
initialized.
#### writable.\_write(chunk, encoding, callback)
#### writable._write(chunk, encoding, callback)
* `chunk` {Buffer | String} The chunk to be written. Will **always**
be a buffer unless the `decodeStrings` option was set to `false`.
@ -1341,7 +1342,7 @@ the class that defines it, and should not be called directly by user
programs. However, you **are** expected to override this method in
your own extension classes.
#### writable.\_writev(chunks, callback)
#### writable._writev(chunks, callback)
* `chunks` {Array} The chunks to be written. Each chunk has following
format: `{ chunk: ..., encoding: ... }`.
@ -1368,7 +1369,8 @@ This can be done by passing the appropriate methods as constructor options:
Examples:
### Duplex
```javascript
```js
var duplex = new stream.Duplex({
read: function(n) {
// sets this._read under the hood
@ -1405,7 +1407,8 @@ var duplex = new stream.Duplex({
```
### Readable
```javascript
```js
var readable = new stream.Readable({
read: function(n) {
// sets this._read under the hood
@ -1418,7 +1421,8 @@ var readable = new stream.Readable({
```
### Transform
```javascript
```js
var transform = new stream.Transform({
transform: function(chunk, encoding, next) {
// sets this._transform under the hood
@ -1441,7 +1445,8 @@ var transform = new stream.Transform({
```
### Writable
```javascript
```js
var writable = new stream.Writable({
write: function(chunk, encoding, next) {
// sets this._write under the hood
@ -1521,7 +1526,7 @@ introduces an edge case in the following conditions:
For example, consider the following code:
```javascript
```js
// WARNING! BROKEN!
net.createServer((socket) => {
@ -1541,7 +1546,7 @@ the socket will remain paused forever.
The workaround in this situation is to call the `resume()` method to
start the flow of data:
```javascript
```js
// Workaround
net.createServer((socket) => {
@ -1593,7 +1598,7 @@ writable side with `readableObjectMode` and `writableObjectMode`
respectively. These options can be used to implement parsers and
serializers with Transform streams.
```javascript
```js
const util = require('util');
const StringDecoder = require('string_decoder').StringDecoder;
const Transform = require('stream').Transform;

14
doc/api/string_decoder.markdown

@ -6,14 +6,16 @@ To use this module, do `require('string_decoder')`. StringDecoder decodes a
buffer to a string. It is a simple interface to `buffer.toString()` but provides
additional support for utf8.
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');
```js
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');
const cent = new Buffer([0xC2, 0xA2]);
console.log(decoder.write(cent));
const cent = new Buffer([0xC2, 0xA2]);
console.log(decoder.write(cent));
const euro = new Buffer([0xE2, 0x82, 0xAC]);
console.log(decoder.write(euro));
const euro = new Buffer([0xE2, 0x82, 0xAC]);
console.log(decoder.write(euro));
```
## Class: StringDecoder

16
doc/api/synopsis.markdown

@ -5,20 +5,24 @@
An example of a [web server][] written with Node.js which responds with
`'Hello World'`:
const http = require('http');
```js
const http = require('http');
http.createServer( (request, response) => {
http.createServer( (request, response) => {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
console.log('Server running at http://127.0.0.1:8124/');
```
To run the server, put the code into a file called `example.js` and execute
it with the node program
> node example.js
Server running at http://127.0.0.1:8124/
```
> node example.js
Server running at http://127.0.0.1:8124/
```
All of the examples in the documentation can be run similarly.

182
doc/api/tls.markdown

@ -10,30 +10,40 @@ Secure Socket Layer: encrypted stream communication.
TLS/SSL is a public/private key infrastructure. Each client and each
server must have a private key. A private key is created like this:
openssl genrsa -out ryans-key.pem 2048
```
openssl genrsa -out ryans-key.pem 2048
```
All servers and some clients need to have a certificate. Certificates are public
keys signed by a Certificate Authority or self-signed. The first step to
getting a certificate is to create a "Certificate Signing Request" (CSR)
file. This is done with:
openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem
```
openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem
```
To create a self-signed certificate with the CSR, do this:
openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem
```
openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem
```
Alternatively you can send the CSR to a Certificate Authority for signing.
For Perfect Forward Secrecy, it is required to generate Diffie-Hellman
parameters:
openssl dhparam -outform PEM -out dhparam.pem 2048
```
openssl dhparam -outform PEM -out dhparam.pem 2048
```
To create .pfx or .p12, do this:
openssl pkcs12 -export -in agent5-cert.pem -inkey agent5-key.pem \
```
openssl pkcs12 -export -in agent5-cert.pem -inkey agent5-key.pem \
-certfile ca-cert.pem -out agent5.pfx
```
- `in`: certificate
- `inkey`: private key
@ -81,33 +91,37 @@ times.
Node.js is built with a default suite of enabled and disabled TLS ciphers.
Currently, the default cipher suite is:
ECDHE-RSA-AES128-GCM-SHA256:
ECDHE-ECDSA-AES128-GCM-SHA256:
ECDHE-RSA-AES256-GCM-SHA384:
ECDHE-ECDSA-AES256-GCM-SHA384:
DHE-RSA-AES128-GCM-SHA256:
ECDHE-RSA-AES128-SHA256:
DHE-RSA-AES128-SHA256:
ECDHE-RSA-AES256-SHA384:
DHE-RSA-AES256-SHA384:
ECDHE-RSA-AES256-SHA256:
DHE-RSA-AES256-SHA256:
HIGH:
!aNULL:
!eNULL:
!EXPORT:
!DES:
!RC4:
!MD5:
!PSK:
!SRP:
!CAMELLIA
```
ECDHE-RSA-AES128-GCM-SHA256:
ECDHE-ECDSA-AES128-GCM-SHA256:
ECDHE-RSA-AES256-GCM-SHA384:
ECDHE-ECDSA-AES256-GCM-SHA384:
DHE-RSA-AES128-GCM-SHA256:
ECDHE-RSA-AES128-SHA256:
DHE-RSA-AES128-SHA256:
ECDHE-RSA-AES256-SHA384:
DHE-RSA-AES256-SHA384:
ECDHE-RSA-AES256-SHA256:
DHE-RSA-AES256-SHA256:
HIGH:
!aNULL:
!eNULL:
!EXPORT:
!DES:
!RC4:
!MD5:
!PSK:
!SRP:
!CAMELLIA
```
This default can be overriden entirely using the `--tls-cipher-list` command
line switch. For instance, the following makes
`ECDHE-RSA-AES128-GCM-SHA256:!RC4` the default TLS cipher suite:
node --tls-cipher-list="ECDHE-RSA-AES128-GCM-SHA256:!RC4"
```
node --tls-cipher-list="ECDHE-RSA-AES128-GCM-SHA256:!RC4"
```
Note that the default cipher suite included within Node.js has been carefully
selected to reflect current security best practices and risk mitigation.
@ -242,14 +256,16 @@ established after addition of event listener.
Here's an example for using TLS session resumption:
var tlsSessionStore = {};
server.on('newSession', (id, data, cb) => {
```js
var tlsSessionStore = {};
server.on('newSession', (id, data, cb) => {
tlsSessionStore[id.toString('hex')] = data;
cb();
});
server.on('resumeSession', (id, cb) => {
});
server.on('resumeSession', (id, cb) => {
cb(null, tlsSessionStore[id.toString('hex')] || null);
});
});
```
### Event: 'secureConnection'
@ -450,7 +466,8 @@ if `false` - only the top certificate without `issuer` property.
Example:
{ subject:
```
{ subject:
{ C: 'UK',
ST: 'Acknack Ltd',
L: 'Rhys Jones',
@ -471,6 +488,7 @@ Example:
valid_to: 'Nov 6 09:52:22 2029 GMT',
fingerprint: '2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:5A:71:38:52:EC:8A:DF',
serialNumber: 'B9B0D332A1AA5635' }
```
If the peer does not provide a certificate, it returns `null` or an empty
object.
@ -615,54 +633,58 @@ The `callback` parameter will be added as a listener for the
Here is an example of a client of echo server as described previously:
const tls = require('tls');
const fs = require('fs');
```js
const tls = require('tls');
const fs = require('fs');
const options = {
const options = {
// These are necessary only if using the client certificate authentication
key: fs.readFileSync('client-key.pem'),
cert: fs.readFileSync('client-cert.pem'),
// This is necessary only if the server uses the self-signed certificate
ca: [ fs.readFileSync('server-cert.pem') ]
};
};
var socket = tls.connect(8000, options, () => {
var socket = tls.connect(8000, options, () => {
console.log('client connected',
socket.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(socket);
process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
console.log(data);
});
socket.on('end', () => {
});
socket.on('end', () => {
server.close();
});
});
```
Or
const tls = require('tls');
const fs = require('fs');
```js
const tls = require('tls');
const fs = require('fs');
const options = {
const options = {
pfx: fs.readFileSync('client.pfx')
};
};
var socket = tls.connect(8000, options, () => {
var socket = tls.connect(8000, options, () => {
console.log('client connected',
socket.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(socket);
process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
});
socket.setEncoding('utf8');
socket.on('data', (data) => {
console.log(data);
});
socket.on('end', () => {
});
socket.on('end', () => {
server.close();
});
});
```
## tls.createSecureContext(details)
@ -751,6 +773,7 @@ automatically set as a listener for the [`'secureConnection'`][] event. The
- `ciphers`: A string describing the ciphers to use or exclude, separated by
`:`. The default cipher suite is:
```js
ECDHE-RSA-AES128-GCM-SHA256:
ECDHE-ECDSA-AES128-GCM-SHA256:
ECDHE-RSA-AES256-GCM-SHA384:
@ -772,6 +795,7 @@ automatically set as a listener for the [`'secureConnection'`][] event. The
!PSK:
!SRP:
!CAMELLIA
```
The default cipher suite prefers GCM ciphers for [Chrome's 'modern
cryptography' setting] and also prefers ECDHE and DHE ciphers for Perfect
@ -856,10 +880,11 @@ automatically set as a listener for the [`'secureConnection'`][] event. The
Here is a simple example echo server:
const tls = require('tls');
const fs = require('fs');
```js
const tls = require('tls');
const fs = require('fs');
const options = {
const options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem'),
@ -868,46 +893,51 @@ Here is a simple example echo server:
// This is necessary only if the client uses the self-signed certificate.
ca: [ fs.readFileSync('client-cert.pem') ]
};
};
var server = tls.createServer(options, (socket) => {
var server = tls.createServer(options, (socket) => {
console.log('server connected',
socket.authorized ? 'authorized' : 'unauthorized');
socket.write('welcome!\n');
socket.setEncoding('utf8');
socket.pipe(socket);
});
server.listen(8000, () => {
});
server.listen(8000, () => {
console.log('server bound');
});
});
```
Or
const tls = require('tls');
const fs = require('fs');
```js
const tls = require('tls');
const fs = require('fs');
const options = {
const options = {
pfx: fs.readFileSync('server.pfx'),
// This is necessary only if using the client certificate authentication.
requestCert: true,
};
};
var server = tls.createServer(options, (socket) => {
var server = tls.createServer(options, (socket) => {
console.log('server connected',
socket.authorized ? 'authorized' : 'unauthorized');
socket.write('welcome!\n');
socket.setEncoding('utf8');
socket.pipe(socket);
});
server.listen(8000, () => {
});
server.listen(8000, () => {
console.log('server bound');
});
You can test this server by connecting to it with `openssl s_client`:
});
```
You can test this server by connecting to it with `openssl s_client`:
openssl s_client -connect 127.0.0.1:8000
```
openssl s_client -connect 127.0.0.1:8000
```
## tls.getCiphers()
@ -915,8 +945,10 @@ Returns an array with the names of the supported SSL ciphers.
Example:
var ciphers = tls.getCiphers();
console.log(ciphers); // ['AES128-SHA', 'AES256-SHA', ...]
```js
var ciphers = tls.getCiphers();
console.log(ciphers); // ['AES128-SHA', 'AES256-SHA', ...]
```
[OpenSSL cipher list format documentation]: https://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT

16
doc/api/tty.markdown

@ -10,10 +10,12 @@ will be a `tty.ReadStream` instance and `process.stdout` will be
a `tty.WriteStream` instance. The preferred way to check if Node.js is being run
in a TTY context is to check `process.stdout.isTTY`:
$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false
```
$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false
```
## Class: ReadStream
@ -45,10 +47,12 @@ ever created (and only when `isatty(1)` is true).
Emitted by `refreshSize()` when either of the `columns` or `rows` properties
has changed.
process.stdout.on('resize', () => {
```js
process.stdout.on('resize', () => {
console.log('screen size has changed!');
console.log(`${process.stdout.columns}x${process.stdout.rows}`);
});
});
```
### ws.columns

12
doc/api/url.markdown

@ -71,7 +71,9 @@ string will not be in the parsed object. Examples are shown for the URL
Spaces (`' '`) and the following characters will be automatically escaped in the
properties of URL objects:
< > " ` \r \n \t { } | \ ^ '
```
< > " ` \r \n \t { } | \ ^ '
```
---
@ -122,6 +124,8 @@ Pass `true` as the third argument to treat `//foo/bar` as
Take a base URL, and a href URL, and resolve them as a browser would for
an anchor tag. Examples:
url.resolve('/one/two/three', 'four') // '/one/two/four'
url.resolve('http://example.com/', '/one') // 'http://example.com/one'
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
```js
url.resolve('/one/two/three', 'four') // '/one/two/four'
url.resolve('http://example.com/', '/one') // 'http://example.com/one'
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
```

256
doc/api/util.markdown

@ -31,7 +31,7 @@ returned function is a no-op.
For example:
```javascript
```js
var debuglog = util.debuglog('foo');
var bar = 123;
@ -41,7 +41,9 @@ debuglog('hello from foo [%d]', bar);
If this program is run with `NODE_DEBUG=foo` in the environment, then
it will output something like:
FOO 3245: hello from foo [123]
```
FOO 3245: hello from foo [123]
```
where `3245` is the process id. If it is not run with that
environment variable set, then it will not print anything.
@ -53,13 +55,15 @@ comma. For example, `NODE_DEBUG=fs,net,tls`.
Marks that a method should not be used any more.
const util = require('util');
```js
const util = require('util');
exports.puts = util.deprecate(function() {
exports.puts = util.deprecate(function() {
for (var i = 0, len = arguments.length; i < len; ++i) {
process.stdout.write(arguments[i] + '\n');
}
}, 'util.puts: Use console.log instead');
}, 'util.puts: Use console.log instead');
```
It returns a modified function which warns once by default.
@ -100,19 +104,25 @@ contains circular references.
If the placeholder does not have a corresponding argument, the placeholder is
not replaced.
util.format('%s:%s', 'foo'); // 'foo:%s'
```js
util.format('%s:%s', 'foo'); // 'foo:%s'
```
If there are more arguments than placeholders, the extra arguments are
coerced to strings (for objects and symbols, `util.inspect()` is used)
and then concatenated, delimited by a space.
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
```js
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
```
If the first argument is not a format string then `util.format()` returns
a string that is the concatenation of all its arguments separated by spaces.
Each argument is converted to a string with `util.inspect()`.
util.format(1, 2, 3); // '1 2 3'
```js
util.format(1, 2, 3); // '1 2 3'
```
## util.inherits(constructor, superConstructor)
@ -123,28 +133,30 @@ prototype of `constructor` will be set to a new object created from
As an additional convenience, `superConstructor` will be accessible
through the `constructor.super_` property.
const util = require('util');
const EventEmitter = require('events');
```js
const util = require('util');
const EventEmitter = require('events');
function MyStream() {
function MyStream() {
EventEmitter.call(this);
}
}
util.inherits(MyStream, EventEmitter);
util.inherits(MyStream, EventEmitter);
MyStream.prototype.write = function(data) {
MyStream.prototype.write = function(data) {
this.emit('data', data);
}
}
var stream = new MyStream();
var stream = new MyStream();
console.log(stream instanceof EventEmitter); // true
console.log(MyStream.super_ === EventEmitter); // true
console.log(stream instanceof EventEmitter); // true
console.log(MyStream.super_ === EventEmitter); // true
stream.on('data', (data) => {
stream.on('data', (data) => {
console.log(`Received data: "${data}"`);
})
stream.write('It works!'); // Received data: "It works!"
})
stream.write('It works!'); // Received data: "It works!"
```
## util.inspect(object[, options])
@ -169,9 +181,11 @@ formatted string:
Example of inspecting all properties of the `util` object:
const util = require('util');
```js
const util = require('util');
console.log(util.inspect(util, { showHidden: true, depth: null }));
console.log(util.inspect(util, { showHidden: true, depth: null }));
```
Values may supply their own custom `inspect(depth, opts)` functions, when
called they receive the current depth in the recursive inspection, as well as
@ -208,27 +222,31 @@ There are also `bold`, `italic`, `underline` and `inverse` codes.
Objects also may define their own `inspect(depth)` function which `util.inspect()`
will invoke and use the result of when inspecting the object:
const util = require('util');
```js
const util = require('util');
var obj = { name: 'nate' };
obj.inspect = function(depth) {
var obj = { name: 'nate' };
obj.inspect = function(depth) {
return `{${this.name}}`;
};
};
util.inspect(obj);
util.inspect(obj);
// "{nate}"
```
You may also return another Object entirely, and the returned String will be
formatted according to the returned Object. This is similar to how
`JSON.stringify()` works:
var obj = { foo: 'this will not show up in the inspect() output' };
obj.inspect = function(depth) {
```js
var obj = { foo: 'this will not show up in the inspect() output' };
obj.inspect = function(depth) {
return { bar: 'baz' };
};
};
util.inspect(obj);
util.inspect(obj);
// "{ bar: 'baz' }"
```
## util.isArray(object)
@ -238,14 +256,16 @@ Internal alias for [`Array.isArray`][].
Returns `true` if the given "object" is an `Array`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isArray([])
util.isArray([])
// true
util.isArray(new Array)
util.isArray(new Array)
// true
util.isArray({})
util.isArray({})
// false
```
## util.isBoolean(object)
@ -253,14 +273,16 @@ Returns `true` if the given "object" is an `Array`. `false` otherwise.
Returns `true` if the given "object" is a `Boolean`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isBoolean(1)
util.isBoolean(1)
// false
util.isBoolean(0)
util.isBoolean(0)
// false
util.isBoolean(false)
util.isBoolean(false)
// true
```
## util.isBuffer(object)
@ -270,14 +292,16 @@ Use `Buffer.isBuffer()` instead.
Returns `true` if the given "object" is a `Buffer`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isBuffer({ length: 0 })
util.isBuffer({ length: 0 })
// false
util.isBuffer([])
util.isBuffer([])
// false
util.isBuffer(new Buffer('hello world'))
util.isBuffer(new Buffer('hello world'))
// true
```
## util.isDate(object)
@ -285,14 +309,16 @@ Returns `true` if the given "object" is a `Buffer`. `false` otherwise.
Returns `true` if the given "object" is a `Date`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isDate(new Date())
util.isDate(new Date())
// true
util.isDate(Date())
util.isDate(Date())
// false (without 'new' returns a String)
util.isDate({})
util.isDate({})
// false
```
## util.isError(object)
@ -300,14 +326,16 @@ Returns `true` if the given "object" is a `Date`. `false` otherwise.
Returns `true` if the given "object" is an [`Error`][]. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isError(new Error())
util.isError(new Error())
// true
util.isError(new TypeError())
util.isError(new TypeError())
// true
util.isError({ name: 'Error', message: 'an error occurred' })
util.isError({ name: 'Error', message: 'an error occurred' })
// false
```
## util.isFunction(object)
@ -315,17 +343,19 @@ Returns `true` if the given "object" is an [`Error`][]. `false` otherwise.
Returns `true` if the given "object" is a `Function`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
function Foo() {}
var Bar = function() {};
function Foo() {}
var Bar = function() {};
util.isFunction({})
util.isFunction({})
// false
util.isFunction(Foo)
util.isFunction(Foo)
// true
util.isFunction(Bar)
util.isFunction(Bar)
// true
```
## util.isNull(object)
@ -333,14 +363,16 @@ Returns `true` if the given "object" is a `Function`. `false` otherwise.
Returns `true` if the given "object" is strictly `null`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isNull(0)
util.isNull(0)
// false
util.isNull(undefined)
util.isNull(undefined)
// false
util.isNull(null)
util.isNull(null)
// true
```
## util.isNullOrUndefined(object)
@ -348,14 +380,16 @@ Returns `true` if the given "object" is strictly `null`. `false` otherwise.
Returns `true` if the given "object" is `null` or `undefined`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isNullOrUndefined(0)
util.isNullOrUndefined(0)
// false
util.isNullOrUndefined(undefined)
util.isNullOrUndefined(undefined)
// true
util.isNullOrUndefined(null)
util.isNullOrUndefined(null)
// true
```
## util.isNumber(object)
@ -363,16 +397,18 @@ Returns `true` if the given "object" is `null` or `undefined`. `false` otherwise
Returns `true` if the given "object" is a `Number`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isNumber(false)
util.isNumber(false)
// false
util.isNumber(Infinity)
util.isNumber(Infinity)
// true
util.isNumber(0)
util.isNumber(0)
// true
util.isNumber(NaN)
util.isNumber(NaN)
// true
```
## util.isObject(object)
@ -381,16 +417,18 @@ Returns `true` if the given "object" is a `Number`. `false` otherwise.
Returns `true` if the given "object" is strictly an `Object` __and__ not a
`Function`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isObject(5)
util.isObject(5)
// false
util.isObject(null)
util.isObject(null)
// false
util.isObject({})
util.isObject({})
// true
util.isObject(function(){})
util.isObject(function(){})
// false
```
## util.isPrimitive(object)
@ -398,26 +436,28 @@ Returns `true` if the given "object" is strictly an `Object` __and__ not a
Returns `true` if the given "object" is a primitive type. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isPrimitive(5)
util.isPrimitive(5)
// true
util.isPrimitive('foo')
util.isPrimitive('foo')
// true
util.isPrimitive(false)
util.isPrimitive(false)
// true
util.isPrimitive(null)
util.isPrimitive(null)
// true
util.isPrimitive(undefined)
util.isPrimitive(undefined)
// true
util.isPrimitive({})
util.isPrimitive({})
// false
util.isPrimitive(function() {})
util.isPrimitive(function() {})
// false
util.isPrimitive(/^$/)
util.isPrimitive(/^$/)
// false
util.isPrimitive(new Date())
util.isPrimitive(new Date())
// false
```
## util.isRegExp(object)
@ -425,14 +465,16 @@ Returns `true` if the given "object" is a primitive type. `false` otherwise.
Returns `true` if the given "object" is a `RegExp`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isRegExp(/some regexp/)
util.isRegExp(/some regexp/)
// true
util.isRegExp(new RegExp('another regexp'))
util.isRegExp(new RegExp('another regexp'))
// true
util.isRegExp({})
util.isRegExp({})
// false
```
## util.isString(object)
@ -440,16 +482,18 @@ Returns `true` if the given "object" is a `RegExp`. `false` otherwise.
Returns `true` if the given "object" is a `String`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isString('')
util.isString('')
// true
util.isString('foo')
util.isString('foo')
// true
util.isString(String('foo'))
util.isString(String('foo'))
// true
util.isString(5)
util.isString(5)
// false
```
## util.isSymbol(object)
@ -457,14 +501,16 @@ Returns `true` if the given "object" is a `String`. `false` otherwise.
Returns `true` if the given "object" is a `Symbol`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
util.isSymbol(5)
util.isSymbol(5)
// false
util.isSymbol('foo')
util.isSymbol('foo')
// false
util.isSymbol(Symbol('foo'))
util.isSymbol(Symbol('foo'))
// true
```
## util.isUndefined(object)
@ -472,15 +518,17 @@ Returns `true` if the given "object" is a `Symbol`. `false` otherwise.
Returns `true` if the given "object" is `undefined`. `false` otherwise.
const util = require('util');
```js
const util = require('util');
var foo;
util.isUndefined(5)
var foo;
util.isUndefined(5)
// false
util.isUndefined(foo)
util.isUndefined(foo)
// true
util.isUndefined(null)
util.isUndefined(null)
// false
```
## util.log(string)

4
doc/api/v8.markdown

@ -10,7 +10,7 @@ therefore not covered under the stability index.
Returns an object with the following properties
```
```js
{
total_heap_size: 7326976,
total_heap_size_executable: 4194304,
@ -82,7 +82,7 @@ and their effects is available [here][].
Usage:
```
```js
// Print GC events to stdout for one minute.
const v8 = require('v8');
v8.setFlagsFromString('--trace_gc');

122
doc/api/vm.markdown

@ -6,7 +6,9 @@
You can access this module with:
const vm = require('vm');
```js
const vm = require('vm');
```
JavaScript code can be compiled and run immediately or compiled, saved, and run
later.
@ -48,24 +50,26 @@ and returns the result. Running code does not have access to local scope.
Example: compile code that increments a global variable and sets one, then
execute the code multiple times. These globals are contained in the sandbox.
const util = require('util');
const vm = require('vm');
```js
const util = require('util');
const vm = require('vm');
var sandbox = {
var sandbox = {
animal: 'cat',
count: 2
};
};
var context = new vm.createContext(sandbox);
var script = new vm.Script('count += 1; name = "kitty"');
var context = new vm.createContext(sandbox);
var script = new vm.Script('count += 1; name = "kitty"');
for (var i = 0; i < 10; ++i) {
for (var i = 0; i < 10; ++i) {
script.runInContext(context);
}
}
console.log(util.inspect(sandbox));
console.log(util.inspect(sandbox));
// { animal: 'cat', count: 12, name: 'kitty' }
// { animal: 'cat', count: 12, name: 'kitty' }
```
Note that running untrusted code is a tricky business requiring great care.
`script.runInContext` is quite useful, but safely running untrusted code
@ -85,20 +89,22 @@ Example: compile code that sets a global variable, then execute the code
multiple times in different contexts. These globals are set on and contained in
the sandboxes.
const util = require('util');
const vm = require('vm');
```js
const util = require('util');
const vm = require('vm');
const sandboxes = [{}, {}, {}];
const sandboxes = [{}, {}, {}];
const script = new vm.Script('globalVar = "set"');
const script = new vm.Script('globalVar = "set"');
sandboxes.forEach((sandbox) => {
sandboxes.forEach((sandbox) => {
script.runInNewContext(sandbox);
});
});
console.log(util.inspect(sandboxes));
console.log(util.inspect(sandboxes));
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
```
Note that running untrusted code is a tricky business requiring great care.
`script.runInNewContext` is quite useful, but safely running untrusted code
@ -114,19 +120,21 @@ current `global` object.
Example of using `script.runInThisContext` to compile code once and run it
multiple times:
const vm = require('vm');
```js
const vm = require('vm');
global.globalVar = 0;
global.globalVar = 0;
const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
for (var i = 0; i < 1000; ++i) {
for (var i = 0; i < 1000; ++i) {
script.runInThisContext();
}
}
console.log(globalVar);
console.log(globalVar);
// 1000
// 1000
```
The options for running a script are:
@ -176,18 +184,20 @@ returns the result. Running code does not have access to local scope. The
Example: compile and execute different scripts in a single existing context.
const util = require('util');
const vm = require('vm');
```js
const util = require('util');
const vm = require('vm');
const sandbox = { globalVar: 1 };
vm.createContext(sandbox);
const sandbox = { globalVar: 1 };
vm.createContext(sandbox);
for (var i = 0; i < 10; ++i) {
for (var i = 0; i < 10; ++i) {
vm.runInContext('globalVar *= 2;', sandbox);
}
console.log(util.inspect(sandbox));
}
console.log(util.inspect(sandbox));
// { globalVar: 1024 }
// { globalVar: 1024 }
```
Note that running untrusted code is a tricky business requiring great care.
`vm.runInContext` is quite useful, but safely running untrusted code requires a
@ -198,8 +208,10 @@ separate process.
`vm.runInDebugContext` compiles and executes `code` inside the V8 debug context.
The primary use case is to get access to the V8 debug object:
const Debug = vm.runInDebugContext('Debug');
Debug.scripts().forEach(function(script) { console.log(script.name); });
```js
const Debug = vm.runInDebugContext('Debug');
Debug.scripts().forEach(function(script) { console.log(script.name); });
```
Note that the debug context and object are intrinsically tied to V8's debugger
implementation and may change (or even get removed) without prior warning.
@ -217,18 +229,20 @@ the sandbox as the global object and returns the result.
Example: compile and execute code that increments a global variable and sets a
new one. These globals are contained in the sandbox.
const util = require('util');
const vm = require('vm');
```js
const util = require('util');
const vm = require('vm');
const sandbox = {
const sandbox = {
animal: 'cat',
count: 2
};
};
vm.runInNewContext('count += 1; name = "kitty"', sandbox);
console.log(util.inspect(sandbox));
vm.runInNewContext('count += 1; name = "kitty"', sandbox);
console.log(util.inspect(sandbox));
// { animal: 'cat', count: 3, name: 'kitty' }
// { animal: 'cat', count: 3, name: 'kitty' }
```
Note that running untrusted code is a tricky business requiring great care.
`vm.runInNewContext` is quite useful, but safely running untrusted code requires
@ -242,19 +256,21 @@ code does not have access to local scope, but does have access to the current
Example of using `vm.runInThisContext` and `eval` to run the same code:
const vm = require('vm');
var localVar = 'initial value';
```js
const vm = require('vm');
var localVar = 'initial value';
const vmResult = vm.runInThisContext('localVar = "vm";');
console.log('vmResult: ', vmResult);
console.log('localVar: ', localVar);
const vmResult = vm.runInThisContext('localVar = "vm";');
console.log('vmResult: ', vmResult);
console.log('localVar: ', localVar);
const evalResult = eval('localVar = "eval";');
console.log('evalResult: ', evalResult);
console.log('localVar: ', localVar);
const evalResult = eval('localVar = "eval";');
console.log('evalResult: ', evalResult);
console.log('localVar: ', localVar);
// vmResult: 'vm', localVar: 'initial value'
// evalResult: 'eval', localVar: 'eval'
// vmResult: 'vm', localVar: 'initial value'
// evalResult: 'eval', localVar: 'eval'
```
`vm.runInThisContext` does not have access to the local scope, so `localVar` is
unchanged. `eval` does have access to the local scope, so `localVar` is changed.

72
doc/api/zlib.markdown

@ -15,29 +15,33 @@ is a readable/writable Stream.
Compressing or decompressing a file can be done by piping an
fs.ReadStream into a zlib stream, then into an fs.WriteStream.
const gzip = zlib.createGzip();
const fs = require('fs');
const inp = fs.createReadStream('input.txt');
const out = fs.createWriteStream('input.txt.gz');
```js
const gzip = zlib.createGzip();
const fs = require('fs');
const inp = fs.createReadStream('input.txt');
const out = fs.createWriteStream('input.txt.gz');
inp.pipe(gzip).pipe(out);
inp.pipe(gzip).pipe(out);
```
Compressing or decompressing data in one step can be done by using
the convenience methods.
const input = '.................................';
zlib.deflate(input, function(err, buffer) {
```js
const input = '.................................';
zlib.deflate(input, function(err, buffer) {
if (!err) {
console.log(buffer.toString('base64'));
}
});
});
const buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64');
zlib.unzip(buffer, function(err, buffer) {
const buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64');
zlib.unzip(buffer, function(err, buffer) {
if (!err) {
console.log(buffer.toString());
}
});
});
```
To use this module in an HTTP client or server, use the [accept-encoding][]
on requests, and the [content-encoding][] header on responses.
@ -47,15 +51,16 @@ the basic concept.** Zlib encoding can be expensive, and the results
ought to be cached. See [Memory Usage Tuning][] for more information
on the speed/memory/compression tradeoffs involved in zlib usage.
// client request example
const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
const request = http.get({ host: 'izs.me',
```js
// client request example
const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
const request = http.get({ host: 'izs.me',
path: '/',
port: 80,
headers: { 'accept-encoding': 'gzip,deflate' } });
request.on('response', (response) => {
request.on('response', (response) => {
var output = fs.createWriteStream('izs.me_index.html');
switch (response.headers['content-encoding']) {
@ -70,15 +75,15 @@ on the speed/memory/compression tradeoffs involved in zlib usage.
response.pipe(output);
break;
}
});
// server example
// Running a gzip operation on every request is quite expensive.
// It would be much more efficient to cache the compressed buffer.
const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
});
// server example
// Running a gzip operation on every request is quite expensive.
// It would be much more efficient to cache the compressed buffer.
const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
var raw = fs.createReadStream('index.html');
var acceptEncoding = request.headers['accept-encoding'];
if (!acceptEncoding) {
@ -97,7 +102,8 @@ on the speed/memory/compression tradeoffs involved in zlib usage.
response.writeHead(200, {});
raw.pipe(response);
}
}).listen(1337);
}).listen(1337);
```
## Memory Usage Tuning
@ -107,7 +113,9 @@ From `zlib/zconf.h`, modified to node.js's usage:
The memory requirements for deflate are (in bytes):
(1 << (windowBits+2)) + (1 << (memLevel+9))
```
(1 << (windowBits+2)) + (1 << (memLevel+9))
```
that is: 128K for windowBits=15 + 128K for memLevel = 8
(default values) plus a few kilobytes for small objects.
@ -115,13 +123,17 @@ that is: 128K for windowBits=15 + 128K for memLevel = 8
For example, if you want to reduce
the default memory requirements from 256K to 128K, set the options to:
{ windowBits: 14, memLevel: 7 }
```
{ windowBits: 14, memLevel: 7 }
```
Of course this will generally degrade compression (there's no free lunch).
The memory requirements for inflate are (in bytes)
1 << windowBits
```
1 << windowBits
```
that is, 32K for windowBits=15 (default value) plus a few kilobytes
for small objects.

Loading…
Cancel
Save