mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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>
This commit is contained in:
1 parent
3ca6d750b9
commit
3624eaf691
36 files changed
+4891
-3913
No files matched your search
+600
-534
File diff suppressed because it is too large.
Load diff
+201
-165
@@ -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);
|
||||
// throws "AssertionError: false == true"
|
||||
assert(0);
|
||||
// throws "AssertionError: 0 == true"
|
||||
assert(false, 'it\'s false');
|
||||
// throws "AssertionError: it's false"
|
||||
assert(true); // OK
|
||||
assert(1); // OK
|
||||
assert(false);
|
||||
// throws "AssertionError: false == true"
|
||||
assert(0);
|
||||
// throws "AssertionError: 0 == true"
|
||||
assert(false, 'it\'s false');
|
||||
// throws "AssertionError: it's false"
|
||||
|
||||
assert.ok(true); // OK
|
||||
assert.ok(1); // OK
|
||||
assert.ok(false);
|
||||
// throws "AssertionError: false == true"
|
||||
assert.ok(0);
|
||||
// throws "AssertionError: 0 == true"
|
||||
assert.ok(false, 'it\'s false');
|
||||
// throws "AssertionError: it's false"
|
||||
assert.ok(true); // OK
|
||||
assert.ok(1); // OK
|
||||
assert.ok(false);
|
||||
// throws "AssertionError: false == true"
|
||||
assert.ok(0);
|
||||
// throws "AssertionError: 0 == true"
|
||||
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 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
};
|
||||
const obj2 = {
|
||||
a : {
|
||||
b : 2
|
||||
}
|
||||
};
|
||||
const obj3 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
}
|
||||
const obj4 = Object.create(obj1);
|
||||
const obj1 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
};
|
||||
const obj2 = {
|
||||
a : {
|
||||
b : 2
|
||||
}
|
||||
};
|
||||
const obj3 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
}
|
||||
const obj4 = Object.create(obj1);
|
||||
|
||||
assert.deepEqual(obj1, obj1);
|
||||
// OK, object is equal to itself
|
||||
assert.deepEqual(obj1, obj1);
|
||||
// OK, object is equal to itself
|
||||
|
||||
assert.deepEqual(obj1, obj2);
|
||||
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
|
||||
// values of b are different
|
||||
assert.deepEqual(obj1, obj2);
|
||||
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
|
||||
// values of b are different
|
||||
|
||||
assert.deepEqual(obj1, obj3);
|
||||
// OK, objects are equal
|
||||
assert.deepEqual(obj1, obj3);
|
||||
// OK, objects are equal
|
||||
|
||||
assert.deepEqual(obj1, obj4);
|
||||
// AssertionError: { a: { b: 1 } } deepEqual {}
|
||||
// Prototypes are ignored
|
||||
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'});
|
||||
// OK, because 1 == '1'
|
||||
assert.deepEqual({a:1}, {a:'1'});
|
||||
// OK, because 1 == '1'
|
||||
|
||||
assert.deepStrictEqual({a:1}, {a:'1'});
|
||||
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
|
||||
// because 1 !== '1' using strict equality
|
||||
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(
|
||||
function() {
|
||||
throw new TypeError('Wrong value');
|
||||
},
|
||||
SyntaxError
|
||||
);
|
||||
```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(
|
||||
function() {
|
||||
throw new TypeError('Wrong value');
|
||||
},
|
||||
TypeError
|
||||
);
|
||||
```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(
|
||||
function() {
|
||||
throw new TypeError('Wrong value');
|
||||
},
|
||||
TypeError,
|
||||
'Whoops'
|
||||
);
|
||||
// Throws: AssertionError: Got unwanted exception (TypeError). Whoops
|
||||
```js
|
||||
assert.doesNotThrow(
|
||||
function() {
|
||||
throw new TypeError('Wrong value');
|
||||
},
|
||||
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);
|
||||
// OK, 1 == 1
|
||||
assert.equal(1, '1');
|
||||
// OK, 1 == '1'
|
||||
assert.equal(1, 1);
|
||||
// OK, 1 == 1
|
||||
assert.equal(1, '1');
|
||||
// OK, 1 == '1'
|
||||
|
||||
assert.equal(1, 2);
|
||||
// AssertionError: 1 == 2
|
||||
assert.equal({a: {b: 1}}, {a: {b: 1}});
|
||||
//AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
|
||||
assert.equal(1, 2);
|
||||
// AssertionError: 1 == 2
|
||||
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, '>');
|
||||
// AssertionError: 1 > 2
|
||||
assert.fail(1, 2, undefined, '>');
|
||||
// AssertionError: 1 > 2
|
||||
|
||||
assert.fail(1, 2, 'whoops', '>');
|
||||
// AssertionError: 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 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
};
|
||||
const obj2 = {
|
||||
a : {
|
||||
b : 2
|
||||
}
|
||||
};
|
||||
const obj3 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
}
|
||||
const obj4 = Object.create(obj1);
|
||||
const obj1 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
};
|
||||
const obj2 = {
|
||||
a : {
|
||||
b : 2
|
||||
}
|
||||
};
|
||||
const obj3 = {
|
||||
a : {
|
||||
b : 1
|
||||
}
|
||||
}
|
||||
const obj4 = Object.create(obj1);
|
||||
|
||||
assert.deepEqual(obj1, obj1);
|
||||
AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
|
||||
assert.deepEqual(obj1, obj1);
|
||||
AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
|
||||
|
||||
assert.deepEqual(obj1, obj2);
|
||||
// OK, obj1 and obj2 are not deeply equal
|
||||
assert.deepEqual(obj1, obj2);
|
||||
// OK, obj1 and obj2 are not deeply equal
|
||||
|
||||
assert.deepEqual(obj1, obj3);
|
||||
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
|
||||
assert.deepEqual(obj1, obj3);
|
||||
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
|
||||
|
||||
assert.deepEqual(obj1, obj4);
|
||||
// OK, obj1 and obj2 are not deeply equal
|
||||
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'});
|
||||
// AssertionError: { a: 1 } notDeepEqual { a: '1' }
|
||||
assert.notDeepEqual({a:1}, {a:'1'});
|
||||
// AssertionError: { a: 1 } notDeepEqual { a: '1' }
|
||||
|
||||
assert.notDeepStrictEqual({a:1}, {a:'1'});
|
||||
// OK
|
||||
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);
|
||||
// OK
|
||||
assert.notEqual(1, 2);
|
||||
// OK
|
||||
|
||||
assert.notEqual(1, 1);
|
||||
// AssertionError: 1 != 1
|
||||
assert.notEqual(1, 1);
|
||||
// AssertionError: 1 != 1
|
||||
|
||||
assert.notEqual(1, '1');
|
||||
// AssertionError: 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);
|
||||
// OK
|
||||
assert.notStrictEqual(1, 2);
|
||||
// OK
|
||||
|
||||
assert.notStrictEqual(1, 1);
|
||||
// AssertionError: 1 != 1
|
||||
assert.notStrictEqual(1, 1);
|
||||
// AssertionError: 1 != 1
|
||||
|
||||
assert.notStrictEqual(1, '1');
|
||||
// OK
|
||||
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);
|
||||
// AssertionError: 1 === 2
|
||||
assert.strictEqual(1, 2);
|
||||
// AssertionError: 1 === 2
|
||||
|
||||
assert.strictEqual(1, 1);
|
||||
// OK
|
||||
assert.strictEqual(1, 1);
|
||||
// OK
|
||||
|
||||
assert.strictEqual(1, '1');
|
||||
// AssertionError: 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,35 +357,41 @@ constructor, [`RegExp`][], or validation function.
|
||||
|
||||
Validate instanceof using constructor:
|
||||
|
||||
assert.throws(
|
||||
function() {
|
||||
throw new Error('Wrong value');
|
||||
},
|
||||
Error
|
||||
);
|
||||
```js
|
||||
assert.throws(
|
||||
function() {
|
||||
throw new Error('Wrong value');
|
||||
},
|
||||
Error
|
||||
);
|
||||
```
|
||||
|
||||
Validate error message using [`RegExp`][]:
|
||||
|
||||
assert.throws(
|
||||
function() {
|
||||
throw new Error('Wrong value');
|
||||
},
|
||||
/value/
|
||||
);
|
||||
```js
|
||||
assert.throws(
|
||||
function() {
|
||||
throw new Error('Wrong value');
|
||||
},
|
||||
/value/
|
||||
);
|
||||
```
|
||||
|
||||
Custom error validation:
|
||||
|
||||
assert.throws(
|
||||
function() {
|
||||
throw new Error('Wrong value');
|
||||
},
|
||||
function(err) {
|
||||
if ( (err instanceof Error) && /value/.test(err) ) {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
'unexpected error'
|
||||
);
|
||||
```js
|
||||
assert.throws(
|
||||
function() {
|
||||
throw new Error('Wrong value');
|
||||
},
|
||||
function(err) {
|
||||
if ( (err instanceof Error) && /value/.test(err) ) {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
'unexpected error'
|
||||
);
|
||||
```
|
||||
|
||||
[Locked]: documentation.html#documentation_stability_index
|
||||
[`assert.deepEqual`]: #assert_assert_deepequal_actual_expected_message
|
||||
|
||||
+497
-395
File diff suppressed because it is too large.
Load diff
+231
-191
@@ -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) => {
|
||||
console.log(`stdout: ${data}`);
|
||||
});
|
||||
ls.stdout.on('data', (data) => {
|
||||
console.log(`stdout: ${data}`);
|
||||
});
|
||||
|
||||
ls.stderr.on('data', (data) => {
|
||||
console.log(`stderr: ${data}`);
|
||||
});
|
||||
ls.stderr.on('data', (data) => {
|
||||
console.log(`stderr: ${data}`);
|
||||
});
|
||||
|
||||
ls.on('close', (code) => {
|
||||
console.log(`child process exited with code ${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) => {
|
||||
console.log(data);
|
||||
});
|
||||
bat.stdout.on('data', (data) => {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
bat.stderr.on('data', (data) => {
|
||||
console.log(data);
|
||||
});
|
||||
bat.stderr.on('data', (data) => {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
bat.on('exit', (code) => {
|
||||
console.log(`Child exited with code ${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) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
console.log(stdout);
|
||||
});
|
||||
// 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',
|
||||
(error, stdout, stderr) => {
|
||||
console.log(`stdout: ${stdout}`);
|
||||
console.log(`stderr: ${stderr}`);
|
||||
if (error !== null) {
|
||||
console.log(`exec error: ${error}`);
|
||||
}
|
||||
});
|
||||
```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:
|
||||
|
||||
{
|
||||
encoding: 'utf8',
|
||||
timeout: 0,
|
||||
maxBuffer: 200*1024,
|
||||
killSignal: 'SIGTERM',
|
||||
cwd: null,
|
||||
env: null
|
||||
}
|
||||
```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) => {
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
console.log(stdout);
|
||||
});
|
||||
```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:
|
||||
|
||||
{
|
||||
cwd: undefined,
|
||||
env: process.env
|
||||
}
|
||||
```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) => {
|
||||
console.log(`stdout: ${data}`);
|
||||
});
|
||||
ls.stdout.on('data', (data) => {
|
||||
console.log(`stdout: ${data}`);
|
||||
});
|
||||
|
||||
ls.stderr.on('data', (data) => {
|
||||
console.log(`stderr: ${data}`);
|
||||
});
|
||||
ls.stderr.on('data', (data) => {
|
||||
console.log(`stderr: ${data}`);
|
||||
});
|
||||
|
||||
ls.on('close', (code) => {
|
||||
console.log(`child process exited with code ${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) => {
|
||||
grep.stdin.write(data);
|
||||
});
|
||||
ps.stdout.on('data', (data) => {
|
||||
grep.stdin.write(data);
|
||||
});
|
||||
|
||||
ps.stderr.on('data', (data) => {
|
||||
console.log(`ps stderr: ${data}`);
|
||||
});
|
||||
ps.stderr.on('data', (data) => {
|
||||
console.log(`ps stderr: ${data}`);
|
||||
});
|
||||
|
||||
ps.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
console.log(`ps process exited with code ${code}`);
|
||||
}
|
||||
grep.stdin.end();
|
||||
});
|
||||
ps.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
console.log(`ps process exited with code ${code}`);
|
||||
}
|
||||
grep.stdin.end();
|
||||
});
|
||||
|
||||
grep.stdout.on('data', (data) => {
|
||||
console.log(`${data}`);
|
||||
});
|
||||
grep.stdout.on('data', (data) => {
|
||||
console.log(`${data}`);
|
||||
});
|
||||
|
||||
grep.stderr.on('data', (data) => {
|
||||
console.log(`grep stderr: ${data}`);
|
||||
});
|
||||
grep.stderr.on('data', (data) => {
|
||||
console.log(`grep stderr: ${data}`);
|
||||
});
|
||||
|
||||
grep.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
console.log(`grep process exited with code ${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) => {
|
||||
console.log('Failed to start child process.');
|
||||
});
|
||||
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', [], {
|
||||
detached: true,
|
||||
stdio: [ 'ignore', out, err ]
|
||||
});
|
||||
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) => {
|
||||
console.log(
|
||||
`child process terminated due to receipt of signal ${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) => {
|
||||
console.log('PARENT got 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) => {
|
||||
console.log('CHILD got 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) => {
|
||||
socket.end('handled by parent');
|
||||
});
|
||||
server.listen(1337, () => {
|
||||
child.send('server', server);
|
||||
});
|
||||
// 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, () => {
|
||||
child.send('server', server);
|
||||
});
|
||||
```
|
||||
|
||||
The child would then receive the server object as:
|
||||
|
||||
process.on('message', (m, server) => {
|
||||
if (m === 'server') {
|
||||
server.on('connection', (socket) => {
|
||||
socket.end('handled by child');
|
||||
});
|
||||
}
|
||||
```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,31 +849,35 @@ 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') {
|
||||
special.send('socket', socket);
|
||||
return;
|
||||
}
|
||||
// This is normal priority
|
||||
normal.send('socket', socket);
|
||||
});
|
||||
server.listen(1337);
|
||||
// If this is special priority
|
||||
if (socket.remoteAddress === '74.125.127.100') {
|
||||
special.send('socket', socket);
|
||||
return;
|
||||
}
|
||||
// This is normal priority
|
||||
normal.send('socket', socket);
|
||||
});
|
||||
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) => {
|
||||
if (m === 'socket') {
|
||||
socket.end(`Request handled with ${process.argv[2]} priority`);
|
||||
}
|
||||
});
|
||||
```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', {
|
||||
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
|
||||
]
|
||||
});
|
||||
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
|
||||
|
||||
|
||||
+215
-177
@@ -9,35 +9,39 @@ 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) {
|
||||
// Fork workers.
|
||||
for (var i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
if (cluster.isMaster) {
|
||||
// Fork workers.
|
||||
for (var i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
|
||||
cluster.on('exit', (worker, code, signal) => {
|
||||
console.log(`worker ${worker.process.pid} died`);
|
||||
});
|
||||
} 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);
|
||||
}
|
||||
cluster.on('exit', (worker, code, signal) => {
|
||||
console.log(`worker ${worker.process.pid} died`);
|
||||
});
|
||||
} 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', () => {
|
||||
// Worker has disconnected
|
||||
});
|
||||
```js
|
||||
cluster.fork().on('disconnect', () => {
|
||||
// Worker has disconnected
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'error'
|
||||
|
||||
@@ -131,16 +137,18 @@ 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) => {
|
||||
if( signal ) {
|
||||
console.log(`worker was killed by signal: ${signal}`);
|
||||
} else if( code !== 0 ) {
|
||||
console.log(`worker exited with error code: ${code}`);
|
||||
} else {
|
||||
console.log('worker success!');
|
||||
}
|
||||
});
|
||||
```js
|
||||
const worker = cluster.fork();
|
||||
worker.on('exit', (code, signal) => {
|
||||
if( signal ) {
|
||||
console.log(`worker was killed by signal: ${signal}`);
|
||||
} else if( code !== 0 ) {
|
||||
console.log(`worker exited with error code: ${code}`);
|
||||
} 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) => {
|
||||
// Worker is listening
|
||||
});
|
||||
```js
|
||||
cluster.fork().on('listening', (address) => {
|
||||
// Worker is listening
|
||||
});
|
||||
```
|
||||
|
||||
It is not emitted in the worker.
|
||||
|
||||
@@ -167,53 +177,57 @@ 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;
|
||||
setInterval(() => {
|
||||
console.log('numReqs =', numReqs);
|
||||
}, 1000);
|
||||
// Keep track of http requests
|
||||
var numReqs = 0;
|
||||
setInterval(() => {
|
||||
console.log('numReqs =', numReqs);
|
||||
}, 1000);
|
||||
|
||||
// Count requests
|
||||
function messageHandler(msg) {
|
||||
if (msg.cmd && msg.cmd == 'notifyRequest') {
|
||||
numReqs += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Start workers and listen for messages containing notifyRequest
|
||||
const numCPUs = require('os').cpus().length;
|
||||
for (var i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
|
||||
Object.keys(cluster.workers).forEach((id) => {
|
||||
cluster.workers[id].on('message', messageHandler);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
// Worker processes have a http server.
|
||||
http.Server((req, res) => {
|
||||
res.writeHead(200);
|
||||
res.end('hello world\n');
|
||||
|
||||
// notify master about the request
|
||||
process.send({ cmd: 'notifyRequest' });
|
||||
}).listen(8000);
|
||||
// Count requests
|
||||
function messageHandler(msg) {
|
||||
if (msg.cmd && msg.cmd == 'notifyRequest') {
|
||||
numReqs += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Start workers and listen for messages containing notifyRequest
|
||||
const numCPUs = require('os').cpus().length;
|
||||
for (var i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
|
||||
Object.keys(cluster.workers).forEach((id) => {
|
||||
cluster.workers[id].on('message', messageHandler);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
// Worker processes have a http server.
|
||||
http.Server((req, res) => {
|
||||
res.writeHead(200);
|
||||
res.end('hello world\n');
|
||||
|
||||
// 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', () => {
|
||||
// Worker is online
|
||||
});
|
||||
```js
|
||||
cluster.fork().on('online', () => {
|
||||
// Worker is online
|
||||
});
|
||||
```
|
||||
|
||||
It is not emitted in the worker.
|
||||
|
||||
@@ -245,36 +259,38 @@ 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) {
|
||||
var worker = cluster.fork();
|
||||
var timeout;
|
||||
```js
|
||||
if (cluster.isMaster) {
|
||||
var worker = cluster.fork();
|
||||
var timeout;
|
||||
|
||||
worker.on('listening', (address) => {
|
||||
worker.send('shutdown');
|
||||
worker.disconnect();
|
||||
timeout = setTimeout(() => {
|
||||
worker.kill();
|
||||
}, 2000);
|
||||
});
|
||||
worker.on('listening', (address) => {
|
||||
worker.send('shutdown');
|
||||
worker.disconnect();
|
||||
timeout = setTimeout(() => {
|
||||
worker.kill();
|
||||
}, 2000);
|
||||
});
|
||||
|
||||
worker.on('disconnect', () => {
|
||||
clearTimeout(timeout);
|
||||
});
|
||||
worker.on('disconnect', () => {
|
||||
clearTimeout(timeout);
|
||||
});
|
||||
|
||||
} else if (cluster.isWorker) {
|
||||
const net = require('net');
|
||||
var server = net.createServer((socket) => {
|
||||
// connections never end
|
||||
});
|
||||
} else if (cluster.isWorker) {
|
||||
const net = require('net');
|
||||
var server = net.createServer((socket) => {
|
||||
// connections never end
|
||||
});
|
||||
|
||||
server.listen(8000);
|
||||
server.listen(8000);
|
||||
|
||||
process.on('message', (msg) => {
|
||||
if(msg === 'shutdown') {
|
||||
// initiate graceful close of any connections to server
|
||||
}
|
||||
});
|
||||
process.on('message', (msg) => {
|
||||
if(msg === 'shutdown') {
|
||||
// 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) {
|
||||
var worker = cluster.fork();
|
||||
worker.send('hi there');
|
||||
```js
|
||||
if (cluster.isMaster) {
|
||||
var worker = cluster.fork();
|
||||
worker.send('hi there');
|
||||
|
||||
} else if (cluster.isWorker) {
|
||||
process.on('message', (msg) => {
|
||||
process.send(msg);
|
||||
});
|
||||
}
|
||||
} 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) => {
|
||||
if (worker.suicide === true) {
|
||||
console.log('Oh, it was just suicide\' – no need to worry').
|
||||
}
|
||||
});
|
||||
```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) => {
|
||||
console.log(`The worker #${worker.id} has disconnected`);
|
||||
});
|
||||
```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) => {
|
||||
console.log('worker %d died (%s). restarting...',
|
||||
worker.process.pid, signal || code);
|
||||
cluster.fork();
|
||||
});
|
||||
```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() {
|
||||
console.error('Something must be wrong with the connection ...');
|
||||
}
|
||||
```js
|
||||
var timeouts = [];
|
||||
function errorMsg() {
|
||||
console.error('Something must be wrong with the connection ...');
|
||||
}
|
||||
|
||||
cluster.on('fork', (worker) => {
|
||||
timeouts[worker.id] = setTimeout(errorMsg, 2000);
|
||||
});
|
||||
cluster.on('listening', (worker, address) => {
|
||||
clearTimeout(timeouts[worker.id]);
|
||||
});
|
||||
cluster.on('exit', (worker, code, signal) => {
|
||||
clearTimeout(timeouts[worker.id]);
|
||||
errorMsg();
|
||||
});
|
||||
cluster.on('fork', (worker) => {
|
||||
timeouts[worker.id] = setTimeout(errorMsg, 2000);
|
||||
});
|
||||
cluster.on('listening', (worker, address) => {
|
||||
clearTimeout(timeouts[worker.id]);
|
||||
});
|
||||
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) => {
|
||||
console.log(
|
||||
`A worker is now connected to ${address.address}:${address.port}`);
|
||||
});
|
||||
```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) => {
|
||||
console.log('Yay, the worker responded after it was forked');
|
||||
});
|
||||
```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({
|
||||
exec: 'worker.js',
|
||||
args: ['--use', 'https'],
|
||||
silent: true
|
||||
});
|
||||
cluster.fork(); // https worker
|
||||
cluster.setupMaster({
|
||||
args: ['--use', 'http']
|
||||
});
|
||||
cluster.fork(); // http worker
|
||||
```js
|
||||
const cluster = require('cluster');
|
||||
cluster.setupMaster({
|
||||
exec: 'worker.js',
|
||||
args: ['--use', 'https'],
|
||||
silent: true
|
||||
});
|
||||
cluster.fork(); // https worker
|
||||
cluster.setupMaster({
|
||||
args: ['--use', 'http']
|
||||
});
|
||||
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) {
|
||||
console.log('I am master');
|
||||
cluster.fork();
|
||||
cluster.fork();
|
||||
} else if (cluster.isWorker) {
|
||||
console.log(`I am worker #${cluster.worker.id}`);
|
||||
}
|
||||
if (cluster.isMaster) {
|
||||
console.log('I am master');
|
||||
cluster.fork();
|
||||
cluster.fork();
|
||||
} 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) {
|
||||
for (var id in cluster.workers) {
|
||||
callback(cluster.workers[id]);
|
||||
}
|
||||
}
|
||||
eachWorker((worker) => {
|
||||
worker.send('big announcement to all workers');
|
||||
});
|
||||
```js
|
||||
// Go through all workers
|
||||
function eachWorker(callback) {
|
||||
for (var id in cluster.workers) {
|
||||
callback(cluster.workers[id]);
|
||||
}
|
||||
}
|
||||
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) => {
|
||||
var worker = cluster.workers[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
|
||||
|
||||
+88
-66
@@ -15,33 +15,37 @@ The module exports two specific components:
|
||||
|
||||
Example using the global `console`:
|
||||
|
||||
console.log('hello world');
|
||||
// Prints: hello world, to stdout
|
||||
console.log('hello %s', 'world');
|
||||
// Prints: hello world, to stdout
|
||||
console.error(new Error('Whoops, something bad happened'));
|
||||
// Prints: [Error: Whoops, something bad happened], to stderr
|
||||
```js
|
||||
console.log('hello world');
|
||||
// Prints: hello world, to stdout
|
||||
console.log('hello %s', 'world');
|
||||
// Prints: hello world, to stdout
|
||||
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!`);
|
||||
// Prints: Danger Will Robinson! Danger!, to stderr
|
||||
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');
|
||||
// Prints: hello world, to out
|
||||
myConsole.log('hello %s', 'world');
|
||||
// Prints: hello world, to out
|
||||
myConsole.error(new Error('Whoops, something bad happened'));
|
||||
// Prints: [Error: Whoops, something bad happened], to err
|
||||
myConsole.log('hello world');
|
||||
// Prints: hello world, to out
|
||||
myConsole.log('hello %s', 'world');
|
||||
// Prints: hello world, to out
|
||||
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!`);
|
||||
// Prints: Danger Will Robinson! Danger!, to err
|
||||
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');
|
||||
// OK
|
||||
console.assert(false, 'Whoops %s', 'didn\'t work');
|
||||
// AssertionError: Whoops didn't work
|
||||
```js
|
||||
console.assert(true, 'does nothing');
|
||||
// OK
|
||||
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);
|
||||
// Prints: error #5, to stderr
|
||||
console.error('error', code);
|
||||
// Prints: error 5, to stderr
|
||||
```js
|
||||
const code = 5;
|
||||
console.error('error #%d', code);
|
||||
// Prints: error #5, to stderr
|
||||
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);
|
||||
// Prints: count: 5, to stdout
|
||||
console.log('count: ', count);
|
||||
// Prints: count: 5, to stdout
|
||||
```js
|
||||
var count = 5;
|
||||
console.log('count: %d', count);
|
||||
// Prints: count: 5, to stdout
|
||||
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
|
||||
@@ -173,31 +191,35 @@ timer's unique `label` as the parameter.
|
||||
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++) {
|
||||
;
|
||||
}
|
||||
console.timeEnd('100-elements');
|
||||
// prints 100-elements: 262ms
|
||||
```js
|
||||
console.time('100-elements');
|
||||
for (var i = 0; i < 100; i++) {
|
||||
;
|
||||
}
|
||||
console.timeEnd('100-elements');
|
||||
// prints 100-elements: 262ms
|
||||
```
|
||||
|
||||
### 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');
|
||||
// Prints: (stack trace will vary based on where trace is called)
|
||||
// Trace: Show me
|
||||
// at repl:2:9
|
||||
// at REPLServer.defaultEval (repl.js:248:27)
|
||||
// at bound (domain.js:287:14)
|
||||
// at REPLServer.runBound [as eval] (domain.js:300:12)
|
||||
// at REPLServer.<anonymous> (repl.js:412:12)
|
||||
// at emitOne (events.js:82:20)
|
||||
// at REPLServer.emit (events.js:169:7)
|
||||
// at REPLServer.Interface._onLine (readline.js:210:10)
|
||||
// at REPLServer.Interface._line (readline.js:549:8)
|
||||
// at REPLServer.Interface._ttyWrite (readline.js:826:14)
|
||||
```js
|
||||
console.trace('Show me');
|
||||
// Prints: (stack trace will vary based on where trace is called)
|
||||
// Trace: Show me
|
||||
// at repl:2:9
|
||||
// at REPLServer.defaultEval (repl.js:248:27)
|
||||
// at bound (domain.js:287:14)
|
||||
// at REPLServer.runBound [as eval] (domain.js:300:12)
|
||||
// at REPLServer.<anonymous> (repl.js:412:12)
|
||||
// at emitOne (events.js:82:20)
|
||||
// at REPLServer.emit (events.js:169:7)
|
||||
// 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][, ...])
|
||||
|
||||
|
||||
+308
-246
@@ -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)
|
||||
.update('I love cupcakes')
|
||||
.digest('hex');
|
||||
console.log(hash);
|
||||
// Prints:
|
||||
// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
|
||||
const secret = 'abcdefg';
|
||||
const hash = crypto.createHmac('sha256', secret)
|
||||
.update('I love cupcakes')
|
||||
.digest('hex');
|
||||
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'));
|
||||
// Prints the challenge as a UTF8 string
|
||||
```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);
|
||||
// Prints the public key as <Buffer ...>
|
||||
```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)));
|
||||
// Prints true or false
|
||||
```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', () => {
|
||||
var data = cipher.read();
|
||||
if (data)
|
||||
console.log(data.toString('hex'));
|
||||
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
|
||||
});
|
||||
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'));
|
||||
// Prints: b919f20fc5ac2f9c1d2cce94cb1d9c2d
|
||||
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', () => {
|
||||
var data = decipher.read();
|
||||
if (data)
|
||||
console.log(data.toString());
|
||||
// Prints: clear text data
|
||||
});
|
||||
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'));
|
||||
// Prints: clear text data
|
||||
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);
|
||||
// OK
|
||||
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);
|
||||
// OK
|
||||
assert(alice_secret, bob_secret);
|
||||
// OK
|
||||
```
|
||||
|
||||
### ECDH.computeSecret(other_public_key[, input_encoding][, output_encoding])
|
||||
|
||||
@@ -471,31 +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(
|
||||
crypto.createHash('sha256').update('alice', 'utf8').digest()
|
||||
);
|
||||
// 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 */
|
||||
console.log(alice_secret == bob_secret);
|
||||
|
||||
### ECDH.setPublicKey(public_key[, encoding])
|
||||
|
||||
Sets the EC Diffie-Hellman public key. Key encoding can be `'binary'`,
|
||||
`'hex'` or `'base64'`. If no encoding is provided, then a buffer is
|
||||
expected.
|
||||
// alice_secret and bob_secret should be the same shared secret value
|
||||
console.log(alice_secret === bob_secret);
|
||||
```
|
||||
|
||||
## Class: Hash
|
||||
|
||||
@@ -512,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', () => {
|
||||
var data = hash.read();
|
||||
if (data)
|
||||
console.log(data.toString('hex'));
|
||||
// Prints:
|
||||
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
|
||||
});
|
||||
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'));
|
||||
// Prints:
|
||||
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
|
||||
hash.update('some data to hash');
|
||||
console.log(hash.digest('hex'));
|
||||
// Prints:
|
||||
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
|
||||
```
|
||||
|
||||
### hash.digest([encoding])
|
||||
|
||||
@@ -580,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', () => {
|
||||
var data = hmac.read();
|
||||
if (data)
|
||||
console.log(data.toString('hex'));
|
||||
// Prints:
|
||||
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
|
||||
});
|
||||
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'));
|
||||
// Prints:
|
||||
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
|
||||
hmac.update('some data to hash');
|
||||
console.log(hmac.digest('hex'));
|
||||
// Prints:
|
||||
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
|
||||
```
|
||||
|
||||
### hmac.digest([encoding])
|
||||
|
||||
@@ -642,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'));
|
||||
// Prints the calculated signature
|
||||
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'));
|
||||
// Prints the calculated signature
|
||||
const private_key = getPrivateKeySomehow();
|
||||
console.log(sign.sign(private_key, 'hex'));
|
||||
// Prints the calculated signature
|
||||
```
|
||||
|
||||
### sign.sign(private_key[, output_format])
|
||||
|
||||
@@ -702,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));
|
||||
// Prints true or false
|
||||
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));
|
||||
// Prints true or false
|
||||
const public_key = getPublicKeySomehow();
|
||||
const signature = getSignatureToVerify();
|
||||
console.log(verify.verify(public_key, signature));
|
||||
// Prints true or false
|
||||
```
|
||||
|
||||
### verifier.update(data)
|
||||
|
||||
@@ -896,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', () => {
|
||||
var data = input.read();
|
||||
if (data)
|
||||
hash.update(data);
|
||||
else {
|
||||
console.log(`${hash.digest('hex')} ${filename}`);
|
||||
}
|
||||
});
|
||||
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)
|
||||
|
||||
@@ -925,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', () => {
|
||||
var data = input.read();
|
||||
if (data)
|
||||
hmac.update(data);
|
||||
else {
|
||||
console.log(`${hmac.digest('hex')} ${filename}`);
|
||||
}
|
||||
});
|
||||
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)
|
||||
|
||||
@@ -959,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()
|
||||
|
||||
@@ -968,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)
|
||||
|
||||
@@ -986,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()
|
||||
|
||||
@@ -1005,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)
|
||||
|
||||
@@ -1030,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) => {
|
||||
if (err) throw err;
|
||||
console.log(key.toString('hex')); // 'c5e478d...1469e50'
|
||||
});
|
||||
```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()`][].
|
||||
@@ -1060,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()`][].
|
||||
@@ -1155,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) => {
|
||||
if (err) throw err;
|
||||
console.log(
|
||||
`${buf.length}` bytes of random data: ${buf.toString('hex')});
|
||||
});
|
||||
```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(
|
||||
`${buf.length}` bytes of random data: ${buf.toString('hex')});
|
||||
```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
|
||||
|
||||
+81
-74
@@ -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
|
||||
1 x = 5;
|
||||
2 setTimeout(function () {
|
||||
3 debugger;
|
||||
debug>
|
||||
```
|
||||
% 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>
|
||||
```
|
||||
|
||||
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 () {
|
||||
debugger;
|
||||
console.log('world');
|
||||
}, 1000);
|
||||
console.log('hello');
|
||||
```js
|
||||
// myscript.js
|
||||
x = 5;
|
||||
setTimeout(function () {
|
||||
debugger;
|
||||
console.log('world');
|
||||
}, 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
|
||||
1 x = 5;
|
||||
2 setTimeout(function () {
|
||||
3 debugger;
|
||||
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
|
||||
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
|
||||
3 debugger;
|
||||
4 console.log('world');
|
||||
5 }, 1000);
|
||||
6 console.log('hello');
|
||||
7
|
||||
debug> quit
|
||||
%
|
||||
|
||||
```
|
||||
% 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
|
||||
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
|
||||
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
|
||||
3 debugger;
|
||||
4 console.log('world');
|
||||
5 }, 1000);
|
||||
6 console.log('hello');
|
||||
7
|
||||
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
|
||||
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.
|
||||
1 var mod = require('./mod.js');
|
||||
2 mod.hello();
|
||||
3 mod.hello();
|
||||
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>
|
||||
```
|
||||
% ./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.
|
||||
1 var mod = require('./mod.js');
|
||||
2 mod.hello();
|
||||
3 mod.hello();
|
||||
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>
|
||||
```
|
||||
|
||||
### Info
|
||||
|
||||
|
||||
+66
-52
@@ -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) => {
|
||||
console.log(`server error:\n${err.stack}`);
|
||||
server.close();
|
||||
});
|
||||
server.on('error', (err) => {
|
||||
console.log(`server error:\n${err.stack}`);
|
||||
server.close();
|
||||
});
|
||||
|
||||
server.on('message', (msg, rinfo) => {
|
||||
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
||||
});
|
||||
server.on('message', (msg, rinfo) => {
|
||||
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
||||
});
|
||||
|
||||
server.on('listening', () => {
|
||||
var address = server.address();
|
||||
console.log(`server listening ${address.address}:${address.port}`);
|
||||
});
|
||||
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) => {
|
||||
console.log('Received %d bytes from %s:%d\n',
|
||||
msg.length, rinfo.address, rinfo.port);
|
||||
});
|
||||
```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) => {
|
||||
console.log(`server error:\n${err.stack}`);
|
||||
server.close();
|
||||
});
|
||||
server.on('error', (err) => {
|
||||
console.log(`server error:\n${err.stack}`);
|
||||
server.close();
|
||||
});
|
||||
|
||||
server.on('message', (msg, rinfo) => {
|
||||
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
||||
});
|
||||
server.on('message', (msg, rinfo) => {
|
||||
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
||||
});
|
||||
|
||||
server.on('listening', () => {
|
||||
var address = server.address();
|
||||
console.log(`server listening ${address.address}:${address.port}`);
|
||||
});
|
||||
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({
|
||||
address: 'localhost',
|
||||
port: 8000,
|
||||
exclusive: true
|
||||
});
|
||||
```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) => {
|
||||
client.close();
|
||||
});
|
||||
```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, () => {
|
||||
s.addMembership('224.0.0.114');
|
||||
});
|
||||
```js
|
||||
const s = dgram.createSocket('udp4');
|
||||
s.bind(1234, () => {
|
||||
s.addMembership('224.0.0.114');
|
||||
});
|
||||
```
|
||||
|
||||
## `dgram` module functions
|
||||
|
||||
|
||||
+53
-41
@@ -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) => {
|
||||
console.log('addresses:', addresses);
|
||||
});
|
||||
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,22 +31,24 @@ 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) => {
|
||||
if (err) throw err;
|
||||
dns.resolve4('nodejs.org', (err, addresses) => {
|
||||
if (err) throw err;
|
||||
|
||||
console.log(`addresses: ${JSON.stringify(addresses)}`);
|
||||
console.log(`addresses: ${JSON.stringify(addresses)}`);
|
||||
|
||||
addresses.forEach((a) => {
|
||||
dns.reverse(a, (err, hostnames) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
|
||||
});
|
||||
});
|
||||
addresses.forEach((a) => {
|
||||
dns.reverse(a, (err, hostnames) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
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
|
||||
}
|
||||
```
|
||||
{
|
||||
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) => {
|
||||
console.log(hostname, service);
|
||||
// Prints: localhost ssh
|
||||
});
|
||||
```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,15 +212,17 @@ be an object with the following properties:
|
||||
* `expire`
|
||||
* `minttl`
|
||||
|
||||
{
|
||||
nsname: 'ns.example.com',
|
||||
hostmaster: 'root.example.com',
|
||||
serial: 2013101809,
|
||||
refresh: 10000,
|
||||
retry: 2400,
|
||||
expire: 604800,
|
||||
minttl: 3600
|
||||
}
|
||||
```
|
||||
{
|
||||
nsname: 'ns.example.com',
|
||||
hostmaster: 'root.example.com',
|
||||
serial: 2013101809,
|
||||
refresh: 10000,
|
||||
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'
|
||||
}
|
||||
```
|
||||
{
|
||||
priority: 10,
|
||||
weight: 5,
|
||||
port: 21223,
|
||||
name: 'service.example.com'
|
||||
}
|
||||
```
|
||||
|
||||
## dns.resolveTxt(hostname, callback)
|
||||
|
||||
|
||||
+38
-34
@@ -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) {
|
||||
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);
|
||||
}));
|
||||
}
|
||||
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) => {
|
||||
// an error occurred somewhere.
|
||||
// if we throw it now, it will crash the program
|
||||
// with the normal line number and stack message.
|
||||
});
|
||||
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,27 +376,29 @@ with a single error handler in a single place.
|
||||
|
||||
#### Example
|
||||
|
||||
const d = domain.create();
|
||||
```js
|
||||
const d = domain.create();
|
||||
|
||||
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
|
||||
// and thus intercepted by the domain.
|
||||
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
|
||||
// and thus intercepted by the domain.
|
||||
|
||||
// if this throws, it will also be passed to the domain
|
||||
// so the error-handling logic can be moved to the 'error'
|
||||
// event on the domain instead of being repeated throughout
|
||||
// the program.
|
||||
return cb(null, JSON.parse(data));
|
||||
}));
|
||||
}
|
||||
// if this throws, it will also be passed to the domain
|
||||
// so the error-handling logic can be moved to the 'error'
|
||||
// event on the domain instead of being repeated throughout
|
||||
// the program.
|
||||
return cb(null, JSON.parse(data));
|
||||
}));
|
||||
}
|
||||
|
||||
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.
|
||||
});
|
||||
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()
|
||||
|
||||
|
||||
+123
-95
@@ -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 {
|
||||
const m = 1;
|
||||
const n = m + z;
|
||||
} catch (err) {
|
||||
// Handle the error here.
|
||||
}
|
||||
```js
|
||||
// Throws with a ReferenceError because z is undefined
|
||||
try {
|
||||
const m = 1;
|
||||
const n = m + z;
|
||||
} 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(() => {
|
||||
// This will crash the process because no 'error' event
|
||||
// handler has been added.
|
||||
ee.emit('error', new Error('This will crash'));
|
||||
});
|
||||
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) {
|
||||
if (err) {
|
||||
console.error('There was an error', err);
|
||||
return;
|
||||
}
|
||||
console.log(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 {
|
||||
fs.readFile('/some/file/that/does-not-exist', (err, data) => {
|
||||
// mistaken assumption: throwing here...
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
} catch(err) {
|
||||
// This will not catch the throw!
|
||||
console.log(err);
|
||||
try {
|
||||
fs.readFile('/some/file/that/does-not-exist', (err, data) => {
|
||||
// mistaken assumption: throwing here...
|
||||
if (err) {
|
||||
throw 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() {
|
||||
Error.captureStackTrace(this, 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);
|
||||
// Prints: The 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!
|
||||
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)
|
||||
```
|
||||
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,30 +292,32 @@ 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() {
|
||||
// cheetahify *synchronously* calls speedy.
|
||||
cheetahify(function speedy() {
|
||||
throw new Error('oh no!');
|
||||
});
|
||||
}
|
||||
function makeFaster() {
|
||||
// cheetahify *synchronously* calls speedy.
|
||||
cheetahify(function speedy() {
|
||||
throw new Error('oh no!');
|
||||
});
|
||||
}
|
||||
|
||||
makeFaster(); // will throw:
|
||||
// /home/gbusey/file.js:6
|
||||
// throw new Error('oh no!');
|
||||
// ^
|
||||
// Error: oh no!
|
||||
// at speedy (/home/gbusey/file.js:6:11)
|
||||
// at makeFaster (/home/gbusey/file.js:5:3)
|
||||
// at Object.<anonymous> (/home/gbusey/file.js:10:1)
|
||||
// at Module._compile (module.js:456:26)
|
||||
// at Object.Module._extensions..js (module.js:474:10)
|
||||
// at Module.load (module.js:356:32)
|
||||
// at Function.Module._load (module.js:312:12)
|
||||
// at Function.Module.runMain (module.js:497:10)
|
||||
// at startup (node.js:119:16)
|
||||
// at node.js:906:3
|
||||
makeFaster(); // will throw:
|
||||
// /home/gbusey/file.js:6
|
||||
// throw new Error('oh no!');
|
||||
// ^
|
||||
// Error: oh no!
|
||||
// at speedy (/home/gbusey/file.js:6:11)
|
||||
// at makeFaster (/home/gbusey/file.js:5:3)
|
||||
// at Object.<anonymous> (/home/gbusey/file.js:10:1)
|
||||
// at Module._compile (module.js:456:26)
|
||||
// at Object.Module._extensions..js (module.js:474:10)
|
||||
// at Module.load (module.js:356:32)
|
||||
// at Function.Module._load (module.js:312:12)
|
||||
// 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);
|
||||
// throws RangeError, port should be > 0 && < 65536
|
||||
```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;
|
||||
// throws ReferenceError, doesNotExist is not a variable in this program.
|
||||
```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 {
|
||||
doesNotExist;
|
||||
} catch(err) {
|
||||
assert(err.arguments[0], 'doesNotExist');
|
||||
}
|
||||
```js
|
||||
const assert = require('assert');
|
||||
try {
|
||||
doesNotExist;
|
||||
} 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 {
|
||||
require('vm').runInThisContext('binary ! isNotOk');
|
||||
} catch(err) {
|
||||
// err will be a SyntaxError
|
||||
}
|
||||
```js
|
||||
try {
|
||||
require('vm').runInThisContext('binary ! isNotOk');
|
||||
} 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() { });
|
||||
// throws TypeError, since it expected a string
|
||||
```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.
|
||||
|
||||
+154
-118
@@ -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() {
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
util.inherits(MyEmitter, EventEmitter);
|
||||
function MyEmitter() {
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
util.inherits(MyEmitter, EventEmitter);
|
||||
|
||||
const myEmitter = new MyEmitter();
|
||||
myEmitter.on('event', function() {
|
||||
console.log('an event occurred!');
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
const myEmitter = new MyEmitter();
|
||||
myEmitter.on('event', function() {
|
||||
console.log('an event occurred!');
|
||||
});
|
||||
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() {
|
||||
console.log('an event occurred!');
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
const myEmitter = new MyEmitter();
|
||||
myEmitter.on('event', function() {
|
||||
console.log('an event occurred!');
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
```
|
||||
|
||||
## Passing arguments and `this` to listeners
|
||||
|
||||
@@ -64,27 +68,31 @@ 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) {
|
||||
console.log(a, b, this);
|
||||
// Prints:
|
||||
// a b MyEmitter {
|
||||
// domain: null,
|
||||
// _events: { event: [Function] },
|
||||
// _eventsCount: 1,
|
||||
// _maxListeners: undefined }
|
||||
});
|
||||
myEmitter.emit('event', 'a', 'b');
|
||||
```js
|
||||
const myEmitter = new MyEmitter();
|
||||
myEmitter.on('event', function(a, b) {
|
||||
console.log(a, b, this);
|
||||
// Prints:
|
||||
// a b MyEmitter {
|
||||
// domain: null,
|
||||
// _events: { event: [Function] },
|
||||
// _eventsCount: 1,
|
||||
// _maxListeners: undefined }
|
||||
});
|
||||
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) => {
|
||||
console.log(a, b, this);
|
||||
// Prints: a b {}
|
||||
});
|
||||
myEmitter.emit('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');
|
||||
```
|
||||
|
||||
## 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) => {
|
||||
setImmediate(() => {
|
||||
console.log('this happens asynchronously');
|
||||
});
|
||||
});
|
||||
myEmitter.emit('event', 'a', 'b');
|
||||
```js
|
||||
const myEmitter = new MyEmitter();
|
||||
myEmitter.on('event', (a, b) => {
|
||||
setImmediate(() => {
|
||||
console.log('this happens asynchronously');
|
||||
});
|
||||
});
|
||||
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', () => {
|
||||
console.log(++m);
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
// Prints: 1
|
||||
myEmitter.emit('event');
|
||||
// Prints: 2
|
||||
```js
|
||||
const myEmitter = new MyEmitter();
|
||||
var m = 0;
|
||||
myEmitter.on('event', () => {
|
||||
console.log(++m);
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
// Prints: 1
|
||||
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', () => {
|
||||
console.log(++m);
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
// Prints: 1
|
||||
myEmitter.emit('event');
|
||||
// Ignored
|
||||
```js
|
||||
const myEmitter = new MyEmitter();
|
||||
var m = 0;
|
||||
myEmitter.once('event', () => {
|
||||
console.log(++m);
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
// Prints: 1
|
||||
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!'));
|
||||
// Throws and crashes Node.js
|
||||
```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) => {
|
||||
console.log('whoops! there was an error');
|
||||
});
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.log('whoops! there was an error');
|
||||
});
|
||||
|
||||
myEmitter.emit('error', new Error('whoops!'));
|
||||
// Prints: whoops! there was an error
|
||||
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) => {
|
||||
console.log('whoops! there was an error');
|
||||
});
|
||||
myEmitter.emit('error', new Error('whoops!'));
|
||||
// Prints: whoops! there was an error
|
||||
```js
|
||||
const myEmitter = new MyEmitter();
|
||||
myEmitter.on('error', (err) => {
|
||||
console.log('whoops! there was an error');
|
||||
});
|
||||
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) => {
|
||||
if (event === 'event') {
|
||||
// Insert a new listener in front
|
||||
myEmitter.on('event', () => {
|
||||
console.log('B');
|
||||
});
|
||||
}
|
||||
});
|
||||
```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('A');
|
||||
console.log('B');
|
||||
});
|
||||
myEmitter.emit('event');
|
||||
// Prints:
|
||||
// B
|
||||
// A
|
||||
}
|
||||
});
|
||||
myEmitter.on('event', () => {
|
||||
console.log('A');
|
||||
});
|
||||
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'));
|
||||
// Prints: 2
|
||||
```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', () => {
|
||||
// do stuff
|
||||
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
|
||||
});
|
||||
```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) => {
|
||||
console.log('someone connected!');
|
||||
});
|
||||
console.log(util.inspect(server.listeners('connection')));
|
||||
// Prints: [ [Function] ]
|
||||
```js
|
||||
server.on('connection', (stream) => {
|
||||
console.log('someone connected!');
|
||||
});
|
||||
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) => {
|
||||
console.log('someone connected!');
|
||||
});
|
||||
```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) => {
|
||||
console.log('Ah, we have our first user!');
|
||||
});
|
||||
```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) {
|
||||
console.log('someone connected!');
|
||||
};
|
||||
server.on('connection', callback);
|
||||
// ...
|
||||
server.removeListener('connection', callback);
|
||||
```js
|
||||
var callback = function(stream) {
|
||||
console.log('someone connected!');
|
||||
};
|
||||
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
|
||||
|
||||
+150
-103
@@ -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) => {
|
||||
if (err) throw err;
|
||||
console.log('successfully deleted /tmp/hello');
|
||||
});
|
||||
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) => {
|
||||
if (err) throw err;
|
||||
console.log('renamed complete');
|
||||
});
|
||||
fs.stat('/tmp/world', (err, stats) => {
|
||||
if (err) throw err;
|
||||
console.log(`stats: ${JSON.stringify(stats)}`);
|
||||
});
|
||||
```js
|
||||
fs.rename('/tmp/hello', '/tmp/world', (err) => {
|
||||
if (err) throw err;
|
||||
console.log('renamed complete');
|
||||
});
|
||||
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) => {
|
||||
if (err) throw err;
|
||||
fs.stat('/tmp/world', (err, stats) => {
|
||||
if (err) throw err;
|
||||
console.log(`stats: ${JSON.stringify(stats)}`);
|
||||
});
|
||||
});
|
||||
```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() {
|
||||
require('fs').readFile('/');
|
||||
}
|
||||
bad();
|
||||
```
|
||||
$ cat script.js
|
||||
function bad() {
|
||||
require('fs').readFile('/');
|
||||
}
|
||||
bad();
|
||||
|
||||
$ env NODE_DEBUG=fs node script.js
|
||||
fs.js:66
|
||||
throw err;
|
||||
^
|
||||
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.>
|
||||
$ env NODE_DEBUG=fs node script.js
|
||||
fs.js:66
|
||||
throw err;
|
||||
^
|
||||
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,20 +146,24 @@ synchronous counterparts are of this type.
|
||||
For a regular file [`util.inspect(stats)`][] would return a string very
|
||||
similar to this:
|
||||
|
||||
{ dev: 2114,
|
||||
ino: 48064969,
|
||||
mode: 33188,
|
||||
nlink: 1,
|
||||
uid: 85,
|
||||
gid: 100,
|
||||
rdev: 0,
|
||||
size: 527,
|
||||
blksize: 4096,
|
||||
blocks: 8,
|
||||
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 }
|
||||
```js
|
||||
{
|
||||
dev: 2114,
|
||||
ino: 48064969,
|
||||
mode: 33188,
|
||||
nlink: 1,
|
||||
uid: 85,
|
||||
gid: 100,
|
||||
rdev: 0,
|
||||
size: 527,
|
||||
blksize: 4096,
|
||||
blocks: 8,
|
||||
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
|
||||
}
|
||||
```
|
||||
|
||||
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) {
|
||||
console.log(err ? 'no access!' : 'can read/write');
|
||||
});
|
||||
```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) => {
|
||||
if (err) throw err;
|
||||
console.log('The "data to append" was appended to file!');
|
||||
});
|
||||
```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',
|
||||
encoding: null,
|
||||
fd: null,
|
||||
mode: 0o666,
|
||||
autoClose: true
|
||||
}
|
||||
```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,10 +364,14 @@ Returns a new [`WriteStream`][] object. (See [Writable Stream][]).
|
||||
|
||||
`options` is an object or string with the following defaults:
|
||||
|
||||
{ flags: 'w',
|
||||
defaultEncoding: 'utf8',
|
||||
fd: null,
|
||||
mode: 0o666 }
|
||||
```js
|
||||
{
|
||||
flags: 'w',
|
||||
defaultEncoding: 'utf8',
|
||||
fd: null,
|
||||
mode: 0o666
|
||||
}
|
||||
```
|
||||
|
||||
`options` may also include a `start` option to allow writing data at
|
||||
some position past the beginning of the file. Modifying a file rather
|
||||
@@ -363,9 +392,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) => {
|
||||
console.log(exists ? 'it\'s there' : 'no passwd!');
|
||||
});
|
||||
```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
|
||||
@@ -587,10 +618,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) => {
|
||||
if (err) throw err;
|
||||
console.log(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.
|
||||
@@ -599,7 +632,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.
|
||||
|
||||
@@ -630,11 +665,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) => {
|
||||
if (err) throw err;
|
||||
console.log(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)
|
||||
|
||||
@@ -685,7 +722,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".
|
||||
|
||||
@@ -798,14 +837,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) => {
|
||||
console.log(`event is: ${event}`);
|
||||
if (filename) {
|
||||
console.log(`filename provided: ${filename}`);
|
||||
} else {
|
||||
console.log('filename not provided');
|
||||
}
|
||||
});
|
||||
```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)
|
||||
|
||||
@@ -822,10 +863,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) => {
|
||||
console.log(`the current mtime is: ${curr.mtime}`);
|
||||
console.log(`the previous mtime was: ${prev.mtime}`);
|
||||
});
|
||||
```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`.
|
||||
|
||||
@@ -908,14 +951,18 @@ to `'utf8'`.
|
||||
|
||||
Example:
|
||||
|
||||
fs.writeFile('message.txt', 'Hello Node.js', (err) => {
|
||||
if (err) throw err;
|
||||
console.log('It\'s saved!');
|
||||
});
|
||||
```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.
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
+237
-189
@@ -12,11 +12,13 @@ user is able to stream data.
|
||||
|
||||
HTTP message headers are represented by an object like this:
|
||||
|
||||
{ 'content-length': '123',
|
||||
'content-type': 'text/plain',
|
||||
'connection': 'keep-alive',
|
||||
'host': 'mysite.com',
|
||||
'accept': '*/*' }
|
||||
```
|
||||
{ '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', '123',
|
||||
'content-type', 'text/plain',
|
||||
'CONNECTION', 'keep-alive',
|
||||
'Host', 'mysite.com',
|
||||
'accepT', '*/*' ]
|
||||
```
|
||||
[ '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) => {
|
||||
// Do stuff
|
||||
}).on('socket', (socket) => {
|
||||
socket.emit('agentRemove');
|
||||
});
|
||||
```js
|
||||
http.get(options, (res) => {
|
||||
// Do stuff
|
||||
}).on('socket', (socket) => {
|
||||
socket.emit('agentRemove');
|
||||
});
|
||||
```
|
||||
|
||||
Alternatively, you could just opt out of pooling entirely using
|
||||
`agent:false`:
|
||||
|
||||
http.get({
|
||||
hostname: 'localhost',
|
||||
port: 80,
|
||||
path: '/',
|
||||
agent: false // create a new agent just for this one request
|
||||
}, (res) => {
|
||||
// Do stuff with response
|
||||
})
|
||||
```js
|
||||
http.get({
|
||||
hostname: 'localhost',
|
||||
port: 80,
|
||||
path: '/',
|
||||
agent: false // create a new agent just for this one request
|
||||
}, (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;
|
||||
@@ -202,58 +210,60 @@ 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) => {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.end('okay');
|
||||
// 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) => {
|
||||
// connect to an origin server
|
||||
var srvUrl = url.parse(`http://${req.url}`);
|
||||
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
|
||||
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
|
||||
'Proxy-agent: Node.js-Proxy\r\n' +
|
||||
'\r\n');
|
||||
srvSocket.write(head);
|
||||
srvSocket.pipe(cltSocket);
|
||||
cltSocket.pipe(srvSocket);
|
||||
});
|
||||
});
|
||||
|
||||
// now that proxy is running
|
||||
proxy.listen(1337, '127.0.0.1', () => {
|
||||
|
||||
// make a request to a tunneling proxy
|
||||
var options = {
|
||||
port: 1337,
|
||||
hostname: '127.0.0.1',
|
||||
method: 'CONNECT',
|
||||
path: 'www.google.com:80'
|
||||
};
|
||||
|
||||
var req = http.request(options);
|
||||
req.end();
|
||||
|
||||
req.on('connect', (res, socket, head) => {
|
||||
console.log('got connected!');
|
||||
|
||||
// make a request over an HTTP tunnel
|
||||
socket.write('GET / HTTP/1.1\r\n' +
|
||||
'Host: www.google.com:80\r\n' +
|
||||
'Connection: close\r\n' +
|
||||
'\r\n');
|
||||
socket.on('data', (chunk) => {
|
||||
console.log(chunk.toString());
|
||||
});
|
||||
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, () => {
|
||||
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
|
||||
'Proxy-agent: Node.js-Proxy\r\n' +
|
||||
'\r\n');
|
||||
srvSocket.write(head);
|
||||
srvSocket.pipe(cltSocket);
|
||||
cltSocket.pipe(srvSocket);
|
||||
});
|
||||
});
|
||||
|
||||
// now that proxy is running
|
||||
proxy.listen(1337, '127.0.0.1', () => {
|
||||
|
||||
// make a request to a tunneling proxy
|
||||
var options = {
|
||||
port: 1337,
|
||||
hostname: '127.0.0.1',
|
||||
method: 'CONNECT',
|
||||
path: 'www.google.com:80'
|
||||
};
|
||||
|
||||
var req = http.request(options);
|
||||
req.end();
|
||||
|
||||
req.on('connect', (res, socket, head) => {
|
||||
console.log('got connected!');
|
||||
|
||||
// make a request over an HTTP tunnel
|
||||
socket.write('GET / HTTP/1.1\r\n' +
|
||||
'Host: www.google.com:80\r\n' +
|
||||
'Connection: close\r\n' +
|
||||
'\r\n');
|
||||
socket.on('data', (chunk) => {
|
||||
console.log(chunk.toString());
|
||||
});
|
||||
socket.on('end', () => {
|
||||
proxy.close();
|
||||
});
|
||||
});
|
||||
socket.on('end', () => {
|
||||
proxy.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'continue'
|
||||
|
||||
@@ -292,44 +302,46 @@ 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) => {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.end('okay');
|
||||
});
|
||||
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');
|
||||
// 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) => {
|
||||
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
|
||||
});
|
||||
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 = {
|
||||
port: 1337,
|
||||
hostname: '127.0.0.1',
|
||||
headers: {
|
||||
'Connection': 'Upgrade',
|
||||
'Upgrade': 'websocket'
|
||||
}
|
||||
};
|
||||
// make a request
|
||||
var options = {
|
||||
port: 1337,
|
||||
hostname: '127.0.0.1',
|
||||
headers: {
|
||||
'Connection': 'Upgrade',
|
||||
'Upgrade': 'websocket'
|
||||
}
|
||||
};
|
||||
|
||||
var req = http.request(options);
|
||||
req.end();
|
||||
var req = http.request(options);
|
||||
req.end();
|
||||
|
||||
req.on('upgrade', (res, socket, upgradeHead) => {
|
||||
console.log('got upgraded!');
|
||||
socket.end();
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
req.on('upgrade', (res, socket, upgradeHead) => {
|
||||
console.log('got upgraded!');
|
||||
socket.end();
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### request.abort()
|
||||
|
||||
@@ -608,11 +620,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',
|
||||
'Trailer': 'Content-MD5' });
|
||||
response.write(fileData);
|
||||
response.addTrailers({'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667'});
|
||||
response.end();
|
||||
```js
|
||||
response.writeHead(200, { 'Content-Type': 'text/plain',
|
||||
'Trailer': 'Content-MD5' });
|
||||
response.write(fileData);
|
||||
response.addTrailers({'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667'});
|
||||
response.end();
|
||||
```
|
||||
|
||||
Attempting to set a header field name or value that contains invalid characters
|
||||
will result in a [`TypeError`][] being thrown.
|
||||
@@ -642,7 +656,9 @@ implicitly flushed.
|
||||
|
||||
Example:
|
||||
|
||||
var contentType = response.getHeader('content-type');
|
||||
```js
|
||||
var contentType = response.getHeader('content-type');
|
||||
```
|
||||
|
||||
### response.headersSent
|
||||
|
||||
@@ -654,7 +670,9 @@ Removes a header that's queued for implicit sending.
|
||||
|
||||
Example:
|
||||
|
||||
response.removeHeader('Content-Encoding');
|
||||
```js
|
||||
response.removeHeader('Content-Encoding');
|
||||
```
|
||||
|
||||
### response.sendDate
|
||||
|
||||
@@ -672,11 +690,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 or value that contains invalid characters
|
||||
will result in a [`TypeError`][] being thrown.
|
||||
@@ -706,7 +728,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.
|
||||
@@ -720,7 +744,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.
|
||||
@@ -765,10 +791,12 @@ argument.
|
||||
|
||||
Example:
|
||||
|
||||
var body = 'hello world';
|
||||
response.writeHead(200, {
|
||||
'Content-Length': body.length,
|
||||
'Content-Type': 'text/plain' });
|
||||
```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.
|
||||
@@ -810,12 +838,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:
|
||||
@@ -853,17 +883,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
|
||||
|
||||
@@ -909,32 +941,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',
|
||||
search: '?name=ryan',
|
||||
query: 'name=ryan',
|
||||
pathname: '/status' }
|
||||
```
|
||||
node> require('url').parse('/status?name=ryan')
|
||||
{
|
||||
href: '/status?name=ryan',
|
||||
search: '?name=ryan',
|
||||
query: 'name=ryan',
|
||||
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',
|
||||
search: '?name=ryan',
|
||||
query: { name: 'ryan' },
|
||||
pathname: '/status' }
|
||||
```
|
||||
node> require('url').parse('/status?name=ryan', true)
|
||||
{
|
||||
href: '/status?name=ryan',
|
||||
search: '?name=ryan',
|
||||
query: {name: 'ryan'},
|
||||
pathname: '/status'
|
||||
}
|
||||
```
|
||||
|
||||
## http.METHODS
|
||||
|
||||
@@ -972,13 +1016,15 @@ is that it sets the method to GET and calls `req.end()` automatically.
|
||||
|
||||
Example:
|
||||
|
||||
http.get('http://www.google.com/index.html', (res) => {
|
||||
console.log(`Got response: ${res.statusCode}`);
|
||||
// consume response body
|
||||
res.resume();
|
||||
}).on('error', (e) => {
|
||||
console.log(`Got error: ${e.message}`);
|
||||
});
|
||||
```js
|
||||
http.get('http://www.google.com/index.html', (res) => {
|
||||
console.log(`Got response: ${res.statusCode}`);
|
||||
// consume response body
|
||||
res.resume();
|
||||
}).on('error', (e) => {
|
||||
console.log(`Got error: ${e.message}`);
|
||||
});
|
||||
```
|
||||
|
||||
## http.globalAgent
|
||||
|
||||
@@ -1030,40 +1076,42 @@ upload a file with a POST request, then write to the `ClientRequest` object.
|
||||
|
||||
Example:
|
||||
|
||||
var postData = querystring.stringify({
|
||||
'msg' : 'Hello World!'
|
||||
});
|
||||
```js
|
||||
var postData = querystring.stringify({
|
||||
'msg' : 'Hello World!'
|
||||
});
|
||||
|
||||
var options = {
|
||||
hostname: 'www.google.com',
|
||||
port: 80,
|
||||
path: '/upload',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Length': postData.length
|
||||
}
|
||||
};
|
||||
var options = {
|
||||
hostname: 'www.google.com',
|
||||
port: 80,
|
||||
path: '/upload',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Length': postData.length
|
||||
}
|
||||
};
|
||||
|
||||
var req = http.request(options, (res) => {
|
||||
console.log(`STATUS: ${res.statusCode}`);
|
||||
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', (chunk) => {
|
||||
console.log(`BODY: ${chunk}`);
|
||||
});
|
||||
res.on('end', () => {
|
||||
console.log('No more data in response.')
|
||||
})
|
||||
});
|
||||
var req = http.request(options, (res) => {
|
||||
console.log(`STATUS: ${res.statusCode}`);
|
||||
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', (chunk) => {
|
||||
console.log(`BODY: ${chunk}`);
|
||||
});
|
||||
res.on('end', () => {
|
||||
console.log('No more data in response.')
|
||||
})
|
||||
});
|
||||
|
||||
req.on('error', (e) => {
|
||||
console.log(`problem with request: ${e.message}`);
|
||||
});
|
||||
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 -
|
||||
|
||||
+84
-72
@@ -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 = {
|
||||
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
|
||||
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
|
||||
};
|
||||
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) => {
|
||||
res.writeHead(200);
|
||||
res.end('hello world\n');
|
||||
}).listen(8000);
|
||||
https.createServer(options, (req, res) => {
|
||||
res.writeHead(200);
|
||||
res.end('hello world\n');
|
||||
}).listen(8000);
|
||||
```
|
||||
|
||||
Or
|
||||
|
||||
const https = require('https');
|
||||
const fs = require('fs');
|
||||
```js
|
||||
const https = require('https');
|
||||
const fs = require('fs');
|
||||
|
||||
const options = {
|
||||
pfx: fs.readFileSync('server.pfx')
|
||||
};
|
||||
const options = {
|
||||
pfx: fs.readFileSync('server.pfx')
|
||||
};
|
||||
|
||||
https.createServer(options, (req, res) => {
|
||||
res.writeHead(200);
|
||||
res.end('hello world\n');
|
||||
}).listen(8000);
|
||||
https.createServer(options, (req, res) => {
|
||||
res.writeHead(200);
|
||||
res.end('hello world\n');
|
||||
}).listen(8000);
|
||||
```
|
||||
|
||||
### server.close([callback])
|
||||
|
||||
@@ -78,19 +82,21 @@ automatically parsed with [`url.parse()`][].
|
||||
|
||||
Example:
|
||||
|
||||
const https = require('https');
|
||||
```js
|
||||
const https = require('https');
|
||||
|
||||
https.get('https://encrypted.google.com/', (res) => {
|
||||
console.log('statusCode: ', res.statusCode);
|
||||
console.log('headers: ', res.headers);
|
||||
https.get('https://encrypted.google.com/', (res) => {
|
||||
console.log('statusCode: ', res.statusCode);
|
||||
console.log('headers: ', res.headers);
|
||||
|
||||
res.on('data', (d) => {
|
||||
process.stdout.write(d);
|
||||
});
|
||||
res.on('data', (d) => {
|
||||
process.stdout.write(d);
|
||||
});
|
||||
|
||||
}).on('error', (e) => {
|
||||
console.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 = {
|
||||
hostname: 'encrypted.google.com',
|
||||
port: 443,
|
||||
path: '/',
|
||||
method: 'GET'
|
||||
};
|
||||
var options = {
|
||||
hostname: 'encrypted.google.com',
|
||||
port: 443,
|
||||
path: '/',
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
var req = https.request(options, (res) => {
|
||||
console.log('statusCode: ', res.statusCode);
|
||||
console.log('headers: ', res.headers);
|
||||
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();
|
||||
res.on('data', (d) => {
|
||||
process.stdout.write(d);
|
||||
});
|
||||
});
|
||||
req.end();
|
||||
|
||||
req.on('error', (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
req.on('error', (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
```
|
||||
|
||||
The options argument has the following options
|
||||
|
||||
@@ -183,37 +191,41 @@ In order to specify these options, use a custom [`Agent`][].
|
||||
|
||||
Example:
|
||||
|
||||
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);
|
||||
```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);
|
||||
|
||||
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 = {
|
||||
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'),
|
||||
agent: false
|
||||
};
|
||||
```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'),
|
||||
agent: false
|
||||
};
|
||||
|
||||
var req = https.request(options, (res) => {
|
||||
...
|
||||
}
|
||||
var req = https.request(options, (res) => {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
[`Agent`]: #https_class_https_agent
|
||||
[`Buffer`]: buffer.html#buffer_buffer
|
||||
|
||||
+142
-110
@@ -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) {
|
||||
return PI * r * r;
|
||||
};
|
||||
exports.area = function (r) {
|
||||
return PI * r * r;
|
||||
};
|
||||
|
||||
exports.circumference = function (r) {
|
||||
return 2 * PI * 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) {
|
||||
return {
|
||||
area: function() {
|
||||
return width * 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,
|
||||
a. return the core module
|
||||
b. STOP
|
||||
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"
|
||||
```
|
||||
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 '../'
|
||||
a. LOAD_AS_FILE(Y + X)
|
||||
b. LOAD_AS_DIRECTORY(Y + X)
|
||||
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,
|
||||
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
|
||||
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
|
||||
|
||||
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)
|
||||
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,
|
||||
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
|
||||
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
|
||||
```
|
||||
|
||||
## 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",
|
||||
"main" : "./lib/some-library.js" }
|
||||
```
|
||||
{ "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(() => {
|
||||
module.exports.emit('ready');
|
||||
}, 1000);
|
||||
// Do some work, and after some time emit
|
||||
// the 'ready' event from the module itself.
|
||||
setTimeout(() => {
|
||||
module.exports.emit('ready');
|
||||
}, 1000);
|
||||
```
|
||||
|
||||
Then in another file we could do
|
||||
|
||||
const a = require('./a');
|
||||
a.on('ready', () => {
|
||||
console.log('module a is 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(() => {
|
||||
module.exports = { a: 'hello' };
|
||||
}, 0);
|
||||
```js
|
||||
setTimeout(() => {
|
||||
module.exports = { a: 'hello' };
|
||||
}, 0);
|
||||
```
|
||||
|
||||
y.js:
|
||||
|
||||
const x = require('./x');
|
||||
console.log(x.a);
|
||||
```js
|
||||
const x = require('./x');
|
||||
console.log(x.a);
|
||||
```
|
||||
|
||||
#### exports alias
|
||||
|
||||
@@ -465,16 +495,18 @@ is no longer bound to the previous value.
|
||||
To illustrate the behavior, imagine this hypothetical implementation of
|
||||
`require()`:
|
||||
|
||||
function require(...) {
|
||||
// ...
|
||||
function (module, exports) {
|
||||
// Your module code here
|
||||
exports = some_func; // re-assigns exports, exports is no longer
|
||||
// a shortcut, and nothing is exported.
|
||||
module.exports = some_func; // makes your module export 0
|
||||
} (module, module.exports);
|
||||
return module;
|
||||
}
|
||||
```js
|
||||
function require(...) {
|
||||
// ...
|
||||
function (module, exports) {
|
||||
// Your module code here
|
||||
exports = some_func; // re-assigns exports, exports is no longer
|
||||
// a shortcut, and nothing is exported.
|
||||
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`.
|
||||
|
||||
+100
-73
@@ -45,15 +45,17 @@ Returns an object with three properties, e.g.
|
||||
|
||||
Example:
|
||||
|
||||
var server = net.createServer((socket) => {
|
||||
socket.end('goodbye\n');
|
||||
});
|
||||
```js
|
||||
var server = net.createServer((socket) => {
|
||||
socket.end('goodbye\n');
|
||||
});
|
||||
|
||||
// grab a random port.
|
||||
server.listen(() => {
|
||||
address = server.address();
|
||||
console.log('opened server on %j', address);
|
||||
});
|
||||
// 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({
|
||||
host: 'localhost',
|
||||
port: 80,
|
||||
exclusive: true
|
||||
});
|
||||
```js
|
||||
server.listen({
|
||||
host: 'localhost',
|
||||
port: 80,
|
||||
exclusive: true
|
||||
});
|
||||
```
|
||||
|
||||
### server.listen(path[, backlog][, callback])
|
||||
|
||||
@@ -184,15 +188,17 @@ 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) => {
|
||||
if (e.code == 'EADDRINUSE') {
|
||||
console.log('Address in use, retrying...');
|
||||
setTimeout(() => {
|
||||
server.close();
|
||||
server.listen(PORT, HOST);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
```js
|
||||
server.on('error', (e) => {
|
||||
if (e.code == 'EADDRINUSE') {
|
||||
console.log('Address in use, retrying...');
|
||||
setTimeout(() => {
|
||||
server.close();
|
||||
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,
|
||||
allowHalfOpen: false,
|
||||
readable: false,
|
||||
writable: false
|
||||
}
|
||||
```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
|
||||
console.log('connected to server!');
|
||||
client.write('world!\r\n');
|
||||
});
|
||||
client.on('data', (data) => {
|
||||
console.log(data.toString());
|
||||
client.end();
|
||||
});
|
||||
client.on('end', () => {
|
||||
console.log('disconnected from server');
|
||||
});
|
||||
```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) => {
|
||||
console.log(data.toString());
|
||||
client.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},
|
||||
() => { //'connect' listener
|
||||
console.log('connected to server!');
|
||||
client.write('world!\r\n');
|
||||
});
|
||||
client.on('data', (data) => {
|
||||
console.log(data.toString());
|
||||
client.end();
|
||||
});
|
||||
client.on('end', () => {
|
||||
console.log('disconnected from server');
|
||||
});
|
||||
```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) => {
|
||||
console.log(data.toString());
|
||||
client.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:
|
||||
|
||||
{
|
||||
allowHalfOpen: false,
|
||||
pauseOnConnect: false
|
||||
}
|
||||
```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
|
||||
console.log('client connected');
|
||||
c.on('end', () => {
|
||||
console.log('client disconnected');
|
||||
});
|
||||
c.write('hello\r\n');
|
||||
c.pipe(c);
|
||||
});
|
||||
server.listen(8124, () => { //'listening' listener
|
||||
console.log('server bound');
|
||||
});
|
||||
```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
|
||||
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)
|
||||
|
||||
|
||||
+90
-86
@@ -24,70 +24,72 @@ 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',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 252020,
|
||||
nice: 0,
|
||||
sys: 30340,
|
||||
idle: 1070356870,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 306960,
|
||||
nice: 0,
|
||||
sys: 26980,
|
||||
idle: 1071569080,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 248450,
|
||||
nice: 0,
|
||||
sys: 21750,
|
||||
idle: 1070919370,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 256880,
|
||||
nice: 0,
|
||||
sys: 19430,
|
||||
idle: 1070905480,
|
||||
irq: 20 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 511580,
|
||||
nice: 20,
|
||||
sys: 40900,
|
||||
idle: 1070842510,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 291660,
|
||||
nice: 0,
|
||||
sys: 34360,
|
||||
idle: 1070888000,
|
||||
irq: 10 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 308260,
|
||||
nice: 0,
|
||||
sys: 55410,
|
||||
idle: 1071129970,
|
||||
irq: 880 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 266450,
|
||||
nice: 1480,
|
||||
sys: 34920,
|
||||
idle: 1072572010,
|
||||
irq: 30 } } ]
|
||||
```js
|
||||
[ { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 252020,
|
||||
nice: 0,
|
||||
sys: 30340,
|
||||
idle: 1070356870,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 306960,
|
||||
nice: 0,
|
||||
sys: 26980,
|
||||
idle: 1071569080,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 248450,
|
||||
nice: 0,
|
||||
sys: 21750,
|
||||
idle: 1070919370,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 256880,
|
||||
nice: 0,
|
||||
sys: 19430,
|
||||
idle: 1070905480,
|
||||
irq: 20 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 511580,
|
||||
nice: 20,
|
||||
sys: 40900,
|
||||
idle: 1070842510,
|
||||
irq: 0 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 291660,
|
||||
nice: 0,
|
||||
sys: 34360,
|
||||
idle: 1070888000,
|
||||
irq: 10 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 308260,
|
||||
nice: 0,
|
||||
sys: 55410,
|
||||
idle: 1071129970,
|
||||
irq: 880 } },
|
||||
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
|
||||
speed: 2926,
|
||||
times:
|
||||
{ user: 266450,
|
||||
nice: 1480,
|
||||
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,28 +127,30 @@ Windows.
|
||||
|
||||
Get a list of network interfaces:
|
||||
|
||||
{ lo:
|
||||
[ { address: '127.0.0.1',
|
||||
netmask: '255.0.0.0',
|
||||
family: 'IPv4',
|
||||
mac: '00:00:00:00:00:00',
|
||||
internal: true },
|
||||
{ address: '::1',
|
||||
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
|
||||
family: 'IPv6',
|
||||
mac: '00:00:00:00:00:00',
|
||||
internal: true } ],
|
||||
eth0:
|
||||
[ { address: '192.168.1.108',
|
||||
netmask: '255.255.255.0',
|
||||
family: 'IPv4',
|
||||
mac: '01:02:03:0a:0b:0c',
|
||||
internal: false },
|
||||
{ address: 'fe80::a00:27ff:fe4e:66a1',
|
||||
netmask: 'ffff:ffff:ffff:ffff::',
|
||||
family: 'IPv6',
|
||||
mac: '01:02:03:0a:0b:0c',
|
||||
internal: false } ] }
|
||||
```js
|
||||
{ lo:
|
||||
[ { address: '127.0.0.1',
|
||||
netmask: '255.0.0.0',
|
||||
family: 'IPv4',
|
||||
mac: '00:00:00:00:00:00',
|
||||
internal: true },
|
||||
{ address: '::1',
|
||||
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
|
||||
family: 'IPv6',
|
||||
mac: '00:00:00:00:00:00',
|
||||
internal: true } ],
|
||||
eth0:
|
||||
[ { address: '192.168.1.108',
|
||||
netmask: '255.255.255.0',
|
||||
family: 'IPv4',
|
||||
mac: '01:02:03:0a:0b:0c',
|
||||
internal: false },
|
||||
{ address: 'fe80::a00:27ff:fe4e:66a1',
|
||||
netmask: 'ffff:ffff:ffff:ffff::',
|
||||
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.
|
||||
|
||||
+144
-107
@@ -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,40 +69,43 @@ 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({
|
||||
root : "/",
|
||||
dir : "/home/user/dir",
|
||||
base : "file.txt",
|
||||
ext : ".txt",
|
||||
name : "file"
|
||||
})
|
||||
// returns
|
||||
'/home/user/dir/file.txt'
|
||||
|
||||
```js
|
||||
path.format({
|
||||
root : "/",
|
||||
dir : "/home/user/dir",
|
||||
base : "file.txt",
|
||||
ext : ".txt",
|
||||
name : "file"
|
||||
})
|
||||
// returns
|
||||
'/home/user/dir/file.txt'
|
||||
```
|
||||
## path.isAbsolute(path)
|
||||
|
||||
Determines whether `path` is an absolute path. An absolute path will always
|
||||
@@ -102,17 +113,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
|
||||
@@ -127,13 +142,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
|
||||
@@ -150,9 +167,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.
|
||||
@@ -163,27 +182,31 @@ Returns an object from a path string.
|
||||
|
||||
An example on \*nix:
|
||||
|
||||
path.parse('/home/user/dir/file.txt')
|
||||
// returns
|
||||
{
|
||||
root : "/",
|
||||
dir : "/home/user/dir",
|
||||
base : "file.txt",
|
||||
ext : ".txt",
|
||||
name : "file"
|
||||
}
|
||||
```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
|
||||
{
|
||||
root : "C:\\",
|
||||
dir : "C:\\path\\dir",
|
||||
base : "index.html",
|
||||
ext : ".html",
|
||||
name : "index"
|
||||
}
|
||||
```js
|
||||
path.parse('C:\\path\\dir\\index.html')
|
||||
// returns
|
||||
{
|
||||
root : "C:\\",
|
||||
dir : "C:\\path\\dir",
|
||||
base : "index.html",
|
||||
ext : ".html",
|
||||
name : "index"
|
||||
}
|
||||
```
|
||||
|
||||
## path.posix
|
||||
|
||||
@@ -198,17 +221,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
|
||||
@@ -226,32 +253,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.
|
||||
@@ -262,15 +295,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
|
||||
|
||||
|
||||
+360
-257
@@ -29,13 +29,15 @@ implicitly by the event loop draining.
|
||||
|
||||
Example of listening for `'exit'`:
|
||||
|
||||
process.on('exit', (code) => {
|
||||
// do *NOT* do this
|
||||
setTimeout(() => {
|
||||
console.log('This will not run');
|
||||
}, 0);
|
||||
console.log('About to exit with code:', 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) => {
|
||||
unhandledRejections.set(p, reason);
|
||||
});
|
||||
process.on('rejectionHandled', (p) => {
|
||||
unhandledRejections.delete(p);
|
||||
});
|
||||
```js
|
||||
const unhandledRejections = new Map();
|
||||
process.on('unhandledRejection', (reason, p) => {
|
||||
unhandledRejections.set(p, reason);
|
||||
});
|
||||
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) => {
|
||||
console.log(`Caught exception: ${err}`);
|
||||
});
|
||||
```js
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.log(`Caught exception: ${err}`);
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
console.log('This will still run.');
|
||||
}, 500);
|
||||
setTimeout(() => {
|
||||
console.log('This will still run.');
|
||||
}, 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.
|
||||
@@ -142,28 +148,34 @@ instance).
|
||||
|
||||
Here is an example that logs every unhandled rejection to the console
|
||||
|
||||
process.on('unhandledRejection', (reason, p) => {
|
||||
console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason);
|
||||
// application specific logging, throwing an error, or other logic here
|
||||
});
|
||||
```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) => {
|
||||
return reportToUser(JSON.parse(res)); // note the typo
|
||||
}); // no `.catch` or `.then`
|
||||
```js
|
||||
somePromise.then((res) => {
|
||||
return reportToUser(JSON.parse(res)); // note the typo
|
||||
}); // no `.catch` or `.then`
|
||||
```
|
||||
|
||||
Here is an example of a coding pattern that will also trigger
|
||||
`'unhandledRejection'`:
|
||||
|
||||
function SomeResource() {
|
||||
// Initially set the loaded status to a rejected promise
|
||||
this.loaded = Promise.reject(new Error('Resource not yet loaded!'));
|
||||
}
|
||||
```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
|
||||
@@ -226,12 +238,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', () => {
|
||||
console.log('Got SIGINT. Press Control-D to exit.');
|
||||
});
|
||||
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.
|
||||
@@ -279,7 +293,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
|
||||
|
||||
@@ -287,32 +303,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) => {
|
||||
console.log(`${index}: ${val}`);
|
||||
});
|
||||
```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 {
|
||||
process.chdir('/tmp');
|
||||
console.log(`New directory: ${process.cwd()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`chdir: ${err}`);
|
||||
}
|
||||
```js
|
||||
console.log(`Starting directory: ${process.cwd()}`);
|
||||
try {
|
||||
process.chdir('/tmp');
|
||||
console.log(`New directory: ${process.cwd()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`chdir: ${err}`);
|
||||
}
|
||||
```
|
||||
|
||||
## process.config
|
||||
|
||||
@@ -322,26 +344,32 @@ the `config.gypi` file that was produced when running the `./configure` script.
|
||||
|
||||
An example of the possible output looks like:
|
||||
|
||||
{ target_defaults:
|
||||
{ cflags: [],
|
||||
default_configuration: 'Release',
|
||||
defines: [],
|
||||
include_dirs: [],
|
||||
libraries: [] },
|
||||
variables:
|
||||
{ host_arch: 'x64',
|
||||
node_install_npm: 'true',
|
||||
node_prefix: '',
|
||||
node_shared_cares: 'false',
|
||||
node_shared_http_parser: 'false',
|
||||
node_shared_libuv: 'false',
|
||||
node_shared_zlib: 'false',
|
||||
node_use_dtrace: 'false',
|
||||
node_use_openssl: 'true',
|
||||
node_shared_openssl: 'false',
|
||||
strict_aliasing: 'true',
|
||||
target_arch: 'x64',
|
||||
v8_use_snapshot: 'true' } }
|
||||
```
|
||||
{
|
||||
target_defaults:
|
||||
{ cflags: [],
|
||||
default_configuration: 'Release',
|
||||
defines: [],
|
||||
include_dirs: [],
|
||||
libraries: [] },
|
||||
variables:
|
||||
{
|
||||
host_arch: 'x64',
|
||||
node_install_npm: 'true',
|
||||
node_prefix: '',
|
||||
node_shared_cares: 'false',
|
||||
node_shared_http_parser: 'false',
|
||||
node_shared_libuv: 'false',
|
||||
node_shared_zlib: 'false',
|
||||
node_use_dtrace: 'false',
|
||||
node_use_openssl: 'true',
|
||||
node_shared_openssl: 'false',
|
||||
strict_aliasing: 'true',
|
||||
target_arch: 'x64',
|
||||
v8_use_snapshot: 'true'
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## process.connected
|
||||
|
||||
@@ -353,7 +381,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()
|
||||
|
||||
@@ -371,26 +401,32 @@ An object containing the user environment. See environ(7).
|
||||
|
||||
An example of this object looks like:
|
||||
|
||||
{ TERM: 'xterm-256color',
|
||||
SHELL: '/usr/local/bin/bash',
|
||||
USER: 'maciej',
|
||||
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
|
||||
PWD: '/Users/maciej',
|
||||
EDITOR: 'vim',
|
||||
SHLVL: '1',
|
||||
HOME: '/Users/maciej',
|
||||
LOGNAME: 'maciej',
|
||||
_: '/usr/local/bin/node' }
|
||||
```js
|
||||
{ TERM: 'xterm-256color',
|
||||
SHELL: '/usr/local/bin/bash',
|
||||
USER: 'maciej',
|
||||
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
|
||||
PWD: '/Users/maciej',
|
||||
EDITOR: 'vim',
|
||||
SHLVL: '1',
|
||||
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);
|
||||
```
|
||||
|
||||
Assigning a property on `process.env` will implicitly convert the value
|
||||
to a string.
|
||||
@@ -428,15 +464,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
|
||||
|
||||
@@ -444,7 +486,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])
|
||||
@@ -454,7 +498,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.
|
||||
|
||||
@@ -477,9 +523,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) {
|
||||
console.log(`Current gid: ${process.getegid()}`);
|
||||
}
|
||||
```js
|
||||
if (process.getegid) {
|
||||
console.log(`Current gid: ${process.getegid()}`);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## process.geteuid()
|
||||
@@ -490,9 +538,11 @@ Android)
|
||||
Gets the effective user identity of the process. (See geteuid(2).)
|
||||
This is the numerical userid, not the username.
|
||||
|
||||
if (process.geteuid) {
|
||||
console.log(`Current uid: ${process.geteuid()}`);
|
||||
}
|
||||
```js
|
||||
if (process.geteuid) {
|
||||
console.log(`Current uid: ${process.geteuid()}`);
|
||||
}
|
||||
```
|
||||
|
||||
## process.getgid()
|
||||
|
||||
@@ -502,9 +552,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) {
|
||||
console.log(`Current gid: ${process.getgid()}`);
|
||||
}
|
||||
```js
|
||||
if (process.getgid) {
|
||||
console.log(`Current gid: ${process.getgid()}`);
|
||||
}
|
||||
```
|
||||
|
||||
## process.getgroups()
|
||||
|
||||
@@ -522,9 +574,11 @@ Android)
|
||||
Gets the user identity of the process. (See getuid(2).)
|
||||
This is the numerical userid, not the username.
|
||||
|
||||
if (process.getuid) {
|
||||
console.log(`Current uid: ${process.getuid()}`);
|
||||
}
|
||||
```js
|
||||
if (process.getuid) {
|
||||
console.log(`Current uid: ${process.getuid()}`);
|
||||
}
|
||||
```
|
||||
|
||||
## process.hrtime()
|
||||
|
||||
@@ -536,16 +590,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(() => {
|
||||
var diff = process.hrtime(time);
|
||||
// [ 1, 552 ]
|
||||
setTimeout(() => {
|
||||
var diff = process.hrtime(time);
|
||||
// [ 1, 552 ]
|
||||
|
||||
console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
|
||||
// benchmark took 1000000527 nanoseconds
|
||||
}, 1000);
|
||||
console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
|
||||
// benchmark took 1000000527 nanoseconds
|
||||
}, 1000);
|
||||
```
|
||||
|
||||
|
||||
## process.initgroups(user, extra_group)
|
||||
@@ -561,11 +617,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])
|
||||
|
||||
@@ -584,16 +642,18 @@ something other than kill the target process.
|
||||
|
||||
Example of sending a signal to yourself:
|
||||
|
||||
process.on('SIGHUP', () => {
|
||||
console.log('Got SIGHUP signal.');
|
||||
});
|
||||
```js
|
||||
process.on('SIGHUP', () => {
|
||||
console.log('Got SIGHUP signal.');
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
console.log('Exiting.');
|
||||
process.exit(0);
|
||||
}, 100);
|
||||
setTimeout(() => {
|
||||
console.log('Exiting.');
|
||||
process.exit(0);
|
||||
}, 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][].
|
||||
@@ -612,15 +672,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,
|
||||
heapTotal: 1826816,
|
||||
heapUsed: 650472 }
|
||||
```js
|
||||
{ rss: 4935680,
|
||||
heapTotal: 1826816,
|
||||
heapUsed: 650472 }
|
||||
```
|
||||
|
||||
`heapTotal` and `heapUsed` refer to V8's memory usage.
|
||||
|
||||
@@ -636,65 +700,76 @@ 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(() => {
|
||||
console.log('nextTick callback');
|
||||
});
|
||||
console.log('scheduled');
|
||||
// Output:
|
||||
// start
|
||||
// scheduled
|
||||
// nextTick callback
|
||||
```js
|
||||
console.log('start');
|
||||
process.nextTick(() => {
|
||||
console.log('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) {
|
||||
this.setupOptions(options);
|
||||
```js
|
||||
function MyThing(options) {
|
||||
this.setupOptions(options);
|
||||
|
||||
process.nextTick(() => {
|
||||
this.startDoingStuff();
|
||||
});
|
||||
}
|
||||
process.nextTick(() => {
|
||||
this.startDoingStuff();
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
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) {
|
||||
if (arg) {
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
```js
|
||||
// WARNING! DO NOT USE! BAD UNSAFE HAZARD!
|
||||
function maybeSync(arg, cb) {
|
||||
if (arg) {
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
|
||||
fs.stat('file', cb);
|
||||
}
|
||||
fs.stat('file', cb);
|
||||
}
|
||||
```
|
||||
|
||||
This API is hazardous. If you do this:
|
||||
|
||||
maybeSync(true, function() {
|
||||
foo();
|
||||
});
|
||||
bar();
|
||||
```js
|
||||
maybeSync(true, function() {
|
||||
foo();
|
||||
});
|
||||
bar();
|
||||
```
|
||||
|
||||
then it's not clear whether `foo()` or `bar()` will be called first.
|
||||
|
||||
This approach is much better:
|
||||
|
||||
function definitelyAsync(arg, cb) {
|
||||
if (arg) {
|
||||
process.nextTick(cb);
|
||||
return;
|
||||
}
|
||||
```js
|
||||
function definitelyAsync(arg, cb) {
|
||||
if (arg) {
|
||||
process.nextTick(cb);
|
||||
return;
|
||||
}
|
||||
|
||||
fs.stat('file', cb);
|
||||
}
|
||||
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,
|
||||
@@ -705,14 +780,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
|
||||
|
||||
@@ -736,10 +815,12 @@ for the source tarball and headers-only tarball.
|
||||
|
||||
e.g.
|
||||
|
||||
{ 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' }
|
||||
```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
|
||||
@@ -767,16 +848,18 @@ 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) {
|
||||
console.log(`Current gid: ${process.getegid()}`);
|
||||
try {
|
||||
process.setegid(501);
|
||||
console.log(`New gid: ${process.getegid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set gid: ${err}`);
|
||||
}
|
||||
}
|
||||
```js
|
||||
if (process.getegid && process.setegid) {
|
||||
console.log(`Current gid: ${process.getegid()}`);
|
||||
try {
|
||||
process.setegid(501);
|
||||
console.log(`New gid: ${process.getegid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set gid: ${err}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## process.seteuid(id)
|
||||
|
||||
@@ -787,16 +870,18 @@ 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) {
|
||||
console.log(`Current uid: ${process.geteuid()}`);
|
||||
try {
|
||||
process.seteuid(501);
|
||||
console.log(`New uid: ${process.geteuid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set uid: ${err}`);
|
||||
}
|
||||
}
|
||||
```js
|
||||
if (process.geteuid && process.seteuid) {
|
||||
console.log(`Current uid: ${process.geteuid()}`);
|
||||
try {
|
||||
process.seteuid(501);
|
||||
console.log(`New uid: ${process.geteuid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set uid: ${err}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## process.setgid(id)
|
||||
|
||||
@@ -807,16 +892,18 @@ 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) {
|
||||
console.log(`Current gid: ${process.getgid()}`);
|
||||
try {
|
||||
process.setgid(501);
|
||||
console.log(`New gid: ${process.getgid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set gid: ${err}`);
|
||||
}
|
||||
}
|
||||
```js
|
||||
if (process.getgid && process.setgid) {
|
||||
console.log(`Current gid: ${process.getgid()}`);
|
||||
try {
|
||||
process.setgid(501);
|
||||
console.log(`New gid: ${process.getgid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set gid: ${err}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## process.setgroups(groups)
|
||||
|
||||
@@ -837,16 +924,18 @@ 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) {
|
||||
console.log(`Current uid: ${process.getuid()}`);
|
||||
try {
|
||||
process.setuid(501);
|
||||
console.log(`New uid: ${process.getuid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set uid: ${err}`);
|
||||
}
|
||||
}
|
||||
```js
|
||||
if (process.getuid && process.setuid) {
|
||||
console.log(`Current uid: ${process.getuid()}`);
|
||||
try {
|
||||
process.setuid(501);
|
||||
console.log(`New uid: ${process.getuid()}`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`Failed to set uid: ${err}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## process.stderr
|
||||
|
||||
@@ -864,18 +953,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', () => {
|
||||
var chunk = process.stdin.read();
|
||||
if (chunk !== null) {
|
||||
process.stdout.write(`data: ${chunk}`);
|
||||
}
|
||||
});
|
||||
process.stdin.on('readable', () => {
|
||||
var chunk = process.stdin.read();
|
||||
if (chunk !== null) {
|
||||
process.stdout.write(`data: ${chunk}`);
|
||||
}
|
||||
});
|
||||
|
||||
process.stdin.on('end', () => {
|
||||
process.stdout.write('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.
|
||||
@@ -894,9 +985,11 @@ A `Writable Stream` to `stdout` (on fd `1`).
|
||||
|
||||
For example, a `console.log` equivalent could look like this:
|
||||
|
||||
console.log = function(msg) {
|
||||
process.stdout.write(`${msg}\n`);
|
||||
};
|
||||
```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'`
|
||||
@@ -907,15 +1000,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.
|
||||
|
||||
@@ -939,11 +1034,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(
|
||||
`Changed umask from ${oldmask.toString(8)} to ${newmask.toString(8)}`
|
||||
);
|
||||
```js
|
||||
const newmask = 0o022;
|
||||
const oldmask = process.umask(newmask);
|
||||
console.log(
|
||||
`Changed umask from ${oldmask.toString(8)} to ${newmask.toString(8)}`
|
||||
);
|
||||
```
|
||||
|
||||
|
||||
## process.uptime()
|
||||
@@ -954,25 +1051,31 @@ 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',
|
||||
node: '1.1.1',
|
||||
v8: '4.1.0.14',
|
||||
uv: '1.3.0',
|
||||
zlib: '1.2.8',
|
||||
ares: '1.10.0-DEV',
|
||||
modules: '43',
|
||||
icu: '55.1',
|
||||
openssl: '1.0.1k' }
|
||||
```js
|
||||
{ http_parser: '2.3.0',
|
||||
node: '1.1.1',
|
||||
v8: '4.1.0.14',
|
||||
uv: '1.3.0',
|
||||
zlib: '1.2.8',
|
||||
ares: '1.10.0-DEV',
|
||||
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
|
||||
|
||||
+29
-17
@@ -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
|
||||
|
||||
|
||||
@@ -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: '' }
|
||||
```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' }
|
||||
// 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' }
|
||||
```
|
||||
|
||||
## 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,
|
||||
{ encodeURIComponent: gbkEncodeURIComponent })
|
||||
// returns
|
||||
'w=%D6%D0%CE%C4&foo=bar'
|
||||
// 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'
|
||||
```
|
||||
|
||||
## querystring.unescape
|
||||
|
||||
|
||||
+107
-79
@@ -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({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
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.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();
|
||||
});
|
||||
rl.close();
|
||||
});
|
||||
```
|
||||
|
||||
## Class: Interface
|
||||
|
||||
@@ -66,9 +68,11 @@ nothing is displayed.
|
||||
|
||||
Example usage:
|
||||
|
||||
interface.question('What is your favorite food?', (answer) => {
|
||||
console.log(`Oh, so your favorite food is ${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
|
||||
|
||||
@@ -118,9 +124,11 @@ hook to listen for user input.
|
||||
|
||||
Example of listening for `'line'`:
|
||||
|
||||
rl.on('line', (cmd) => {
|
||||
console.log(`You just typed: ${cmd}`);
|
||||
});
|
||||
```js
|
||||
rl.on('line', (cmd) => {
|
||||
console.log(`You just typed: ${cmd}`);
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'pause'
|
||||
|
||||
@@ -133,9 +141,11 @@ Also emitted whenever the `input` stream is not paused and receives the
|
||||
|
||||
Example of listening for `'pause'`:
|
||||
|
||||
rl.on('pause', () => {
|
||||
console.log('Readline paused.');
|
||||
});
|
||||
```js
|
||||
rl.on('pause', () => {
|
||||
console.log('Readline paused.');
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'resume'
|
||||
|
||||
@@ -145,9 +155,11 @@ Emitted whenever the `input` stream is resumed.
|
||||
|
||||
Example of listening for `'resume'`:
|
||||
|
||||
rl.on('resume', () => {
|
||||
console.log('Readline resumed.');
|
||||
});
|
||||
```js
|
||||
rl.on('resume', () => {
|
||||
console.log('Readline resumed.');
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'SIGCONT'
|
||||
|
||||
@@ -162,10 +174,12 @@ background.
|
||||
|
||||
Example of listening for `SIGCONT`:
|
||||
|
||||
rl.on('SIGCONT', () => {
|
||||
// `prompt` will automatically resume the stream
|
||||
rl.prompt();
|
||||
});
|
||||
```js
|
||||
rl.on('SIGCONT', () => {
|
||||
// `prompt` will automatically resume the stream
|
||||
rl.prompt();
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'SIGINT'
|
||||
|
||||
@@ -177,11 +191,13 @@ stream receives a `SIGINT`, `pause` will be triggered.
|
||||
|
||||
Example of listening for `SIGINT`:
|
||||
|
||||
rl.on('SIGINT', () => {
|
||||
rl.question('Are you sure you want to exit?', (answer) => {
|
||||
if (answer.match(/^y(es)?$/i)) rl.pause();
|
||||
});
|
||||
});
|
||||
```js
|
||||
rl.on('SIGINT', () => {
|
||||
rl.question('Are you sure you want to exit?', (answer) => {
|
||||
if (answer.match(/^y(es)?$/i)) rl.pause();
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'SIGTSTP'
|
||||
|
||||
@@ -201,53 +217,59 @@ before the program was sent to the background.
|
||||
|
||||
Example of listening for `SIGTSTP`:
|
||||
|
||||
rl.on('SIGTSTP', () => {
|
||||
// This will override SIGTSTP and prevent the program from going to the
|
||||
// background.
|
||||
console.log('Caught 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) => {
|
||||
switch(line.trim()) {
|
||||
case 'hello':
|
||||
console.log('world!');
|
||||
break;
|
||||
default:
|
||||
console.log('Say what? I might have heard `' + line.trim() + '`');
|
||||
break;
|
||||
}
|
||||
rl.prompt();
|
||||
}).on('close', () => {
|
||||
console.log('Have a great day!');
|
||||
process.exit(0);
|
||||
});
|
||||
rl.on('line', (line) => {
|
||||
switch(line.trim()) {
|
||||
case 'hello':
|
||||
console.log('world!');
|
||||
break;
|
||||
default:
|
||||
console.log('Say what? I might have heard `' + line.trim() + '`');
|
||||
break;
|
||||
}
|
||||
rl.prompt();
|
||||
}).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({
|
||||
input: fs.createReadStream('sample.txt')
|
||||
});
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream('sample.txt')
|
||||
});
|
||||
|
||||
rl.on('line', function (line) {
|
||||
console.log('Line from file:', line);
|
||||
});
|
||||
rl.on('line', function (line) {
|
||||
console.log('Line from file:', line);
|
||||
});
|
||||
```
|
||||
|
||||
## readline.clearLine(stream, dir)
|
||||
|
||||
@@ -292,27 +314,33 @@ Which ends up looking something like:
|
||||
|
||||
Example:
|
||||
|
||||
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]
|
||||
}
|
||||
```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) {
|
||||
callback(null, [['123'], linePartial]);
|
||||
}
|
||||
```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({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
```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.
|
||||
|
||||
+108
-84
@@ -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', () => {
|
||||
console.log('Got "exit" event from repl!');
|
||||
process.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) => {
|
||||
console.log('repl has a new context');
|
||||
someExtension.extend(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', {
|
||||
help: 'Say hello',
|
||||
action: function(name) {
|
||||
this.write(`Hello, ${name}!\n');
|
||||
this.displayPrompt();
|
||||
}
|
||||
});
|
||||
var replServer = repl.start();
|
||||
replServer.defineCommand('sayhello', {
|
||||
help: 'Say hello',
|
||||
action: function(name) {
|
||||
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,37 +299,39 @@ 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({
|
||||
prompt: 'Node.js via stdin> ',
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
repl.start({
|
||||
prompt: 'Node.js via stdin> ',
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via Unix socket> ',
|
||||
input: socket,
|
||||
output: socket
|
||||
}).on('exit', () => {
|
||||
socket.end();
|
||||
})
|
||||
}).listen('/tmp/node-repl-sock');
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via Unix socket> ',
|
||||
input: socket,
|
||||
output: socket
|
||||
}).on('exit', () => {
|
||||
socket.end();
|
||||
})
|
||||
}).listen('/tmp/node-repl-sock');
|
||||
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via TCP socket> ',
|
||||
input: socket,
|
||||
output: socket
|
||||
}).on('exit', () => {
|
||||
socket.end();
|
||||
});
|
||||
}).listen(5001);
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via TCP socket> ',
|
||||
input: socket,
|
||||
output: socket
|
||||
}).on('exit', () => {
|
||||
socket.end();
|
||||
});
|
||||
}).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
|
||||
|
||||
+43
-38
@@ -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
|
||||
|
||||
@@ -883,7 +884,7 @@ more data from the resource and pushing that data onto the queue.
|
||||
Note: once the `_read()` method is called, it will not be called again until
|
||||
the `push` method is called.
|
||||
|
||||
The `size` argument is advisory. Implementations where a "read" is a
|
||||
The `size` argument is advisory. Implementations where a "read" is a
|
||||
single call that returns data can use this to know how much data to
|
||||
fetch. Implementations where that is not relevant, such as TCP or
|
||||
TLS, may ignore this argument, and simply provide data whenever it
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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) => {
|
||||
response.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
response.end('Hello World\n');
|
||||
}).listen(8124);
|
||||
http.createServer( (request, response) => {
|
||||
response.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
response.end('Hello World\n');
|
||||
}).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.
|
||||
|
||||
|
||||
+191
-159
@@ -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 \
|
||||
-certfile ca-cert.pem -out agent5.pfx
|
||||
```
|
||||
openssl pkcs12 -export -in agent5-cert.pem -inkey agent5-key.pem \
|
||||
-certfile ca-cert.pem -out agent5.pfx
|
||||
```
|
||||
|
||||
- `in`: certificate
|
||||
- `inkey`: private key
|
||||
@@ -71,33 +81,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.
|
||||
@@ -248,14 +262,16 @@ established after addition of event listener.
|
||||
|
||||
Here's an example for using TLS session resumption:
|
||||
|
||||
var tlsSessionStore = {};
|
||||
server.on('newSession', (id, data, cb) => {
|
||||
tlsSessionStore[id.toString('hex')] = data;
|
||||
cb();
|
||||
});
|
||||
server.on('resumeSession', (id, cb) => {
|
||||
cb(null, tlsSessionStore[id.toString('hex')] || null);
|
||||
});
|
||||
```js
|
||||
var tlsSessionStore = {};
|
||||
server.on('newSession', (id, data, cb) => {
|
||||
tlsSessionStore[id.toString('hex')] = data;
|
||||
cb();
|
||||
});
|
||||
server.on('resumeSession', (id, cb) => {
|
||||
cb(null, tlsSessionStore[id.toString('hex')] || null);
|
||||
});
|
||||
```
|
||||
|
||||
### Event: 'secureConnection'
|
||||
|
||||
@@ -434,27 +450,29 @@ if `false` - only the top certificate without `issuer` property.
|
||||
|
||||
Example:
|
||||
|
||||
{ subject:
|
||||
{ C: 'UK',
|
||||
ST: 'Acknack Ltd',
|
||||
L: 'Rhys Jones',
|
||||
O: 'node.js',
|
||||
OU: 'Test TLS Certificate',
|
||||
CN: 'localhost' },
|
||||
issuerInfo:
|
||||
{ C: 'UK',
|
||||
ST: 'Acknack Ltd',
|
||||
L: 'Rhys Jones',
|
||||
O: 'node.js',
|
||||
OU: 'Test TLS Certificate',
|
||||
CN: 'localhost' },
|
||||
issuer:
|
||||
{ ... another certificate ... },
|
||||
raw: < RAW DER buffer >,
|
||||
valid_from: 'Nov 11 09:52:22 2009 GMT',
|
||||
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' }
|
||||
```
|
||||
{ subject:
|
||||
{ C: 'UK',
|
||||
ST: 'Acknack Ltd',
|
||||
L: 'Rhys Jones',
|
||||
O: 'node.js',
|
||||
OU: 'Test TLS Certificate',
|
||||
CN: 'localhost' },
|
||||
issuerInfo:
|
||||
{ C: 'UK',
|
||||
ST: 'Acknack Ltd',
|
||||
L: 'Rhys Jones',
|
||||
O: 'node.js',
|
||||
OU: 'Test TLS Certificate',
|
||||
CN: 'localhost' },
|
||||
issuer:
|
||||
{ ... another certificate ... },
|
||||
raw: < RAW DER buffer >,
|
||||
valid_from: 'Nov 11 09:52:22 2009 GMT',
|
||||
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.
|
||||
@@ -581,54 +599,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 = {
|
||||
// These are necessary only if using the client certificate authentication
|
||||
key: fs.readFileSync('client-key.pem'),
|
||||
cert: fs.readFileSync('client-cert.pem'),
|
||||
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') ]
|
||||
};
|
||||
// This is necessary only if the server uses the self-signed certificate
|
||||
ca: [ fs.readFileSync('server-cert.pem') ]
|
||||
};
|
||||
|
||||
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) => {
|
||||
console.log(data);
|
||||
});
|
||||
socket.on('end', () => {
|
||||
server.close();
|
||||
});
|
||||
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) => {
|
||||
console.log(data);
|
||||
});
|
||||
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 = {
|
||||
pfx: fs.readFileSync('client.pfx')
|
||||
};
|
||||
const options = {
|
||||
pfx: fs.readFileSync('client.pfx')
|
||||
};
|
||||
|
||||
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) => {
|
||||
console.log(data);
|
||||
});
|
||||
socket.on('end', () => {
|
||||
server.close();
|
||||
});
|
||||
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) => {
|
||||
console.log(data);
|
||||
});
|
||||
socket.on('end', () => {
|
||||
server.close();
|
||||
});
|
||||
```
|
||||
|
||||
## tls.createSecureContext(details)
|
||||
|
||||
@@ -717,27 +739,29 @@ 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:
|
||||
|
||||
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
|
||||
```js
|
||||
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
|
||||
```
|
||||
|
||||
The default cipher suite prefers GCM ciphers for [Chrome's 'modern
|
||||
cryptography' setting] and also prefers ECDHE and DHE ciphers for Perfect
|
||||
@@ -816,58 +840,64 @@ 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 = {
|
||||
key: fs.readFileSync('server-key.pem'),
|
||||
cert: fs.readFileSync('server-cert.pem'),
|
||||
const options = {
|
||||
key: fs.readFileSync('server-key.pem'),
|
||||
cert: fs.readFileSync('server-cert.pem'),
|
||||
|
||||
// This is necessary only if using the client certificate authentication.
|
||||
requestCert: true,
|
||||
// This is necessary only if using the client certificate authentication.
|
||||
requestCert: true,
|
||||
|
||||
// This is necessary only if the client uses the self-signed certificate.
|
||||
ca: [ fs.readFileSync('client-cert.pem') ]
|
||||
};
|
||||
// This is necessary only if the client uses the self-signed certificate.
|
||||
ca: [ fs.readFileSync('client-cert.pem') ]
|
||||
};
|
||||
|
||||
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, () => {
|
||||
console.log('server bound');
|
||||
});
|
||||
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, () => {
|
||||
console.log('server bound');
|
||||
});
|
||||
```
|
||||
|
||||
Or
|
||||
|
||||
const tls = require('tls');
|
||||
const fs = require('fs');
|
||||
```js
|
||||
const tls = require('tls');
|
||||
const fs = require('fs');
|
||||
|
||||
const options = {
|
||||
pfx: fs.readFileSync('server.pfx'),
|
||||
const options = {
|
||||
pfx: fs.readFileSync('server.pfx'),
|
||||
|
||||
// This is necessary only if using the client certificate authentication.
|
||||
requestCert: true,
|
||||
// This is necessary only if using the client certificate authentication.
|
||||
requestCert: true,
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
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, () => {
|
||||
console.log('server bound');
|
||||
});
|
||||
```
|
||||
|
||||
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, () => {
|
||||
console.log('server bound');
|
||||
});
|
||||
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()
|
||||
|
||||
@@ -875,8 +905,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
|
||||
[Chrome's 'modern cryptography' setting]: https://www.chromium.org/Home/chromium-security/education/tls#TOC-Deprecation-of-TLS-Features-Algorithms-in-Chrome
|
||||
|
||||
+12
-8
@@ -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', () => {
|
||||
console.log('screen size has changed!');
|
||||
console.log(`${process.stdout.columns}x${process.stdout.rows}`);
|
||||
});
|
||||
```js
|
||||
process.stdout.on('resize', () => {
|
||||
console.log('screen size has changed!');
|
||||
console.log(`${process.stdout.columns}x${process.stdout.rows}`);
|
||||
});
|
||||
```
|
||||
|
||||
### ws.columns
|
||||
|
||||
|
||||
@@ -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'
|
||||
```
|
||||
+216
-168
@@ -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() {
|
||||
for (var i = 0, len = arguments.length; i < len; ++i) {
|
||||
process.stdout.write(arguments[i] + '\n');
|
||||
}
|
||||
}, 'util.puts: Use console.log instead');
|
||||
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');
|
||||
```
|
||||
|
||||
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() {
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
function MyStream() {
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
|
||||
util.inherits(MyStream, EventEmitter);
|
||||
util.inherits(MyStream, EventEmitter);
|
||||
|
||||
MyStream.prototype.write = function(data) {
|
||||
this.emit('data', 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) => {
|
||||
console.log(`Received data: "${data}"`);
|
||||
})
|
||||
stream.write('It works!'); // Received data: "It works!"
|
||||
stream.on('data', (data) => {
|
||||
console.log(`Received data: "${data}"`);
|
||||
})
|
||||
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) {
|
||||
return `{${this.name}}`;
|
||||
};
|
||||
var obj = { name: 'nate' };
|
||||
obj.inspect = function(depth) {
|
||||
return `{${this.name}}`;
|
||||
};
|
||||
|
||||
util.inspect(obj);
|
||||
// "{nate}"
|
||||
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) {
|
||||
return { bar: 'baz' };
|
||||
};
|
||||
```js
|
||||
var obj = { foo: 'this will not show up in the inspect() output' };
|
||||
obj.inspect = function(depth) {
|
||||
return { bar: 'baz' };
|
||||
};
|
||||
|
||||
util.inspect(obj);
|
||||
// "{ bar: 'baz' }"
|
||||
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([])
|
||||
// true
|
||||
util.isArray(new Array)
|
||||
// true
|
||||
util.isArray({})
|
||||
// false
|
||||
util.isArray([])
|
||||
// true
|
||||
util.isArray(new Array)
|
||||
// true
|
||||
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)
|
||||
// false
|
||||
util.isBoolean(0)
|
||||
// false
|
||||
util.isBoolean(false)
|
||||
// true
|
||||
util.isBoolean(1)
|
||||
// false
|
||||
util.isBoolean(0)
|
||||
// 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 })
|
||||
// false
|
||||
util.isBuffer([])
|
||||
// false
|
||||
util.isBuffer(new Buffer('hello world'))
|
||||
// true
|
||||
util.isBuffer({ length: 0 })
|
||||
// false
|
||||
util.isBuffer([])
|
||||
// false
|
||||
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())
|
||||
// true
|
||||
util.isDate(Date())
|
||||
// false (without 'new' returns a String)
|
||||
util.isDate({})
|
||||
// false
|
||||
util.isDate(new Date())
|
||||
// true
|
||||
util.isDate(Date())
|
||||
// false (without 'new' returns a String)
|
||||
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())
|
||||
// true
|
||||
util.isError(new TypeError())
|
||||
// true
|
||||
util.isError({ name: 'Error', message: 'an error occurred' })
|
||||
// false
|
||||
util.isError(new Error())
|
||||
// true
|
||||
util.isError(new TypeError())
|
||||
// true
|
||||
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({})
|
||||
// false
|
||||
util.isFunction(Foo)
|
||||
// true
|
||||
util.isFunction(Bar)
|
||||
// true
|
||||
util.isFunction({})
|
||||
// false
|
||||
util.isFunction(Foo)
|
||||
// true
|
||||
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)
|
||||
// false
|
||||
util.isNull(undefined)
|
||||
// false
|
||||
util.isNull(null)
|
||||
// true
|
||||
util.isNull(0)
|
||||
// false
|
||||
util.isNull(undefined)
|
||||
// false
|
||||
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)
|
||||
// false
|
||||
util.isNullOrUndefined(undefined)
|
||||
// true
|
||||
util.isNullOrUndefined(null)
|
||||
// true
|
||||
util.isNullOrUndefined(0)
|
||||
// false
|
||||
util.isNullOrUndefined(undefined)
|
||||
// true
|
||||
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)
|
||||
// false
|
||||
util.isNumber(Infinity)
|
||||
// true
|
||||
util.isNumber(0)
|
||||
// true
|
||||
util.isNumber(NaN)
|
||||
// true
|
||||
util.isNumber(false)
|
||||
// false
|
||||
util.isNumber(Infinity)
|
||||
// true
|
||||
util.isNumber(0)
|
||||
// true
|
||||
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)
|
||||
// false
|
||||
util.isObject(null)
|
||||
// false
|
||||
util.isObject({})
|
||||
// true
|
||||
util.isObject(function(){})
|
||||
// false
|
||||
util.isObject(5)
|
||||
// false
|
||||
util.isObject(null)
|
||||
// false
|
||||
util.isObject({})
|
||||
// true
|
||||
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)
|
||||
// true
|
||||
util.isPrimitive('foo')
|
||||
// true
|
||||
util.isPrimitive(false)
|
||||
// true
|
||||
util.isPrimitive(null)
|
||||
// true
|
||||
util.isPrimitive(undefined)
|
||||
// true
|
||||
util.isPrimitive({})
|
||||
// false
|
||||
util.isPrimitive(function() {})
|
||||
// false
|
||||
util.isPrimitive(/^$/)
|
||||
// false
|
||||
util.isPrimitive(new Date())
|
||||
// false
|
||||
util.isPrimitive(5)
|
||||
// true
|
||||
util.isPrimitive('foo')
|
||||
// true
|
||||
util.isPrimitive(false)
|
||||
// true
|
||||
util.isPrimitive(null)
|
||||
// true
|
||||
util.isPrimitive(undefined)
|
||||
// true
|
||||
util.isPrimitive({})
|
||||
// false
|
||||
util.isPrimitive(function() {})
|
||||
// false
|
||||
util.isPrimitive(/^$/)
|
||||
// false
|
||||
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/)
|
||||
// true
|
||||
util.isRegExp(new RegExp('another regexp'))
|
||||
// true
|
||||
util.isRegExp({})
|
||||
// false
|
||||
util.isRegExp(/some regexp/)
|
||||
// true
|
||||
util.isRegExp(new RegExp('another regexp'))
|
||||
// true
|
||||
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('')
|
||||
// true
|
||||
util.isString('foo')
|
||||
// true
|
||||
util.isString(String('foo'))
|
||||
// true
|
||||
util.isString(5)
|
||||
// false
|
||||
util.isString('')
|
||||
// true
|
||||
util.isString('foo')
|
||||
// true
|
||||
util.isString(String('foo'))
|
||||
// true
|
||||
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)
|
||||
// false
|
||||
util.isSymbol('foo')
|
||||
// false
|
||||
util.isSymbol(Symbol('foo'))
|
||||
// true
|
||||
util.isSymbol(5)
|
||||
// false
|
||||
util.isSymbol('foo')
|
||||
// false
|
||||
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)
|
||||
// false
|
||||
util.isUndefined(foo)
|
||||
// true
|
||||
util.isUndefined(null)
|
||||
// false
|
||||
var foo;
|
||||
util.isUndefined(5)
|
||||
// false
|
||||
util.isUndefined(foo)
|
||||
// true
|
||||
util.isUndefined(null)
|
||||
// false
|
||||
```
|
||||
|
||||
## util.log(string)
|
||||
|
||||
|
||||
+2
-2
@@ -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,
|
||||
@@ -33,7 +33,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');
|
||||
|
||||
+77
-61
@@ -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.
|
||||
@@ -50,24 +52,26 @@ 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 = {
|
||||
animal: 'cat',
|
||||
count: 2
|
||||
};
|
||||
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) {
|
||||
script.runInContext(context);
|
||||
}
|
||||
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
|
||||
@@ -88,20 +92,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) => {
|
||||
script.runInNewContext(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
|
||||
@@ -117,19 +123,21 @@ access to the 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) {
|
||||
script.runInThisContext();
|
||||
}
|
||||
for (var i = 0; i < 1000; ++i) {
|
||||
script.runInThisContext();
|
||||
}
|
||||
|
||||
console.log(globalVar);
|
||||
console.log(globalVar);
|
||||
|
||||
// 1000
|
||||
// 1000
|
||||
```
|
||||
|
||||
The options for running a script are:
|
||||
|
||||
@@ -179,18 +187,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) {
|
||||
vm.runInContext('globalVar *= 2;', sandbox);
|
||||
}
|
||||
console.log(util.inspect(sandbox));
|
||||
for (var i = 0; i < 10; ++i) {
|
||||
vm.runInContext('globalVar *= 2;', 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
|
||||
@@ -201,8 +211,10 @@ a 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.
|
||||
@@ -220,18 +232,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 = {
|
||||
animal: 'cat',
|
||||
count: 2
|
||||
};
|
||||
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
|
||||
@@ -245,19 +259,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
|
||||
|
||||
+80
-68
@@ -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) {
|
||||
if (!err) {
|
||||
console.log(buffer.toString('base64'));
|
||||
}
|
||||
});
|
||||
```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) {
|
||||
if (!err) {
|
||||
console.log(buffer.toString());
|
||||
}
|
||||
});
|
||||
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,57 +51,59 @@ 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',
|
||||
path: '/',
|
||||
port: 80,
|
||||
headers: { 'accept-encoding': 'gzip,deflate' } });
|
||||
request.on('response', (response) => {
|
||||
var output = fs.createWriteStream('izs.me_index.html');
|
||||
```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) => {
|
||||
var output = fs.createWriteStream('izs.me_index.html');
|
||||
|
||||
switch (response.headers['content-encoding']) {
|
||||
// or, just use zlib.createUnzip() to handle both cases
|
||||
case 'gzip':
|
||||
response.pipe(zlib.createGunzip()).pipe(output);
|
||||
break;
|
||||
case 'deflate':
|
||||
response.pipe(zlib.createInflate()).pipe(output);
|
||||
break;
|
||||
default:
|
||||
response.pipe(output);
|
||||
break;
|
||||
}
|
||||
});
|
||||
switch (response.headers['content-encoding']) {
|
||||
// or, just use zlib.createUnzip() to handle both cases
|
||||
case 'gzip':
|
||||
response.pipe(zlib.createGunzip()).pipe(output);
|
||||
break;
|
||||
case 'deflate':
|
||||
response.pipe(zlib.createInflate()).pipe(output);
|
||||
break;
|
||||
default:
|
||||
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) => {
|
||||
var raw = fs.createReadStream('index.html');
|
||||
var acceptEncoding = request.headers['accept-encoding'];
|
||||
if (!acceptEncoding) {
|
||||
acceptEncoding = '';
|
||||
}
|
||||
// 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) {
|
||||
acceptEncoding = '';
|
||||
}
|
||||
|
||||
// Note: this is not a conformant accept-encoding parser.
|
||||
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
||||
if (acceptEncoding.match(/\bdeflate\b/)) {
|
||||
response.writeHead(200, { 'content-encoding': 'deflate' });
|
||||
raw.pipe(zlib.createDeflate()).pipe(response);
|
||||
} else if (acceptEncoding.match(/\bgzip\b/)) {
|
||||
response.writeHead(200, { 'content-encoding': 'gzip' });
|
||||
raw.pipe(zlib.createGzip()).pipe(response);
|
||||
} else {
|
||||
response.writeHead(200, {});
|
||||
raw.pipe(response);
|
||||
}
|
||||
}).listen(1337);
|
||||
// Note: this is not a conformant accept-encoding parser.
|
||||
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
||||
if (acceptEncoding.match(/\bdeflate\b/)) {
|
||||
response.writeHead(200, { 'content-encoding': 'deflate' });
|
||||
raw.pipe(zlib.createDeflate()).pipe(response);
|
||||
} else if (acceptEncoding.match(/\bgzip\b/)) {
|
||||
response.writeHead(200, { 'content-encoding': 'gzip' });
|
||||
raw.pipe(zlib.createGzip()).pipe(response);
|
||||
} else {
|
||||
response.writeHead(200, {});
|
||||
raw.pipe(response);
|
||||
}
|
||||
}).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.
|
||||
|
||||
Reference in new issue
Block a user