понедельник, 17 октября 2016 г.

Node.js Quick Docs Help

// Errors

try {
    var a = b + 1;
} catch (error) {
    console.log('Ошибка!');
}

try {
    setTimeout(function () {throw new Error('Эта ошибка не будет перехвачена');}, 1000);
} catch (error) {
    console.log('Ошибки внутри асинхронных функций не могут быть перехвачены с помощью try-catch.');
}

myObserver.on('error', function (error) {console.log('Ошибка!');});
myObserver.emit('error', new Error('Произошла ошибка'));

process.on('uncaughtException', function (error) {console.log('Ошибка!');});

// Stack trace

var error = new Error('message text');
console.log(error.message); // message text
console.log(error.stack);

var myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // подобно new Error().stack;

function MyError () {
    Error.captureStackTrace(this, MyError);
}
new MyError().stack;

console.log(Error.stackTraceLimit);

// Domain

var domain = require('domain');

var dom = domain.create();

dom.on('error', function (error) {
    console.log('error: ' + error.message);
});

function asyncFunction () {
    setTimeout(function () {throw new Error('Произошла ошибка!');}, 1000);
}

dom.add(asyncFunction);

dom.run(function () {
    setTimeout(function () {throw new Error('Произошла еще  одна ошибка!');}, 1000);
});

console.log(dom.members);

dom.remove(asyncFunction);

dom.enter();
dom.exit();

dom.dispose();

function asyncReadSomeFile (filePath, callback) {
    fs.readFile(filePath, 'utf8', dom.intrcept(function (data) {
        return callback(JSON.parse(data));
    }));
}

// Assert

var assert = require('assert');
assert.ok(1 > 2, 'Ошибка! 1 должно быть меньше 2.');
assert.deepStrictEqual({a: {b: 1}}, {a: {b: '1'}}, 'Ошибка! Объекты не равны.');

// Console

console.log('message1', 'message2', 'message3');
console.info('message %d', 4);
console.warn('Danger!', 'Danger!', 'Danger!');
console.error('Error', 5);
console.assert(1 > 2, 'Ошибка!');
console.dir({a: 1, b: 2}, {showHidden: true, depth: 3, colors: true});

console.time('100-elements');
for (var i = 0; i < 100; i++) {/* some code */}
console.endTime('100-elements');

console.trace('Show me my stack trace');

var logger = new console.Console(fs.createWriteStream('./stdout.log'), fs.createWriteStream('./stderr.log'));
logger.log('message');

// Global

global.somVar = 1;

Buffer

__dirname; console.log(__dirname); // /Users/work/
__filename; console.log(__filename); // /Users/work/example.js

console.log('message');

var lib = require('./path/to/lib.js');

exports.value = value;
module.exports = function () {};

var fileName = require.resolve('./path/to/lib.js');

console.log(require.cache);

require.extensions['.sjs'] =  require.extensions['.js'];

// Modules

// Пути для загрузи модулей
// '/' - абсолютный путь
// './' - текущая папка
// '../' - переход на папку выше

// Раширения модулей
// 'file-name'
// 'file-name.js'
// 'file-name.json'
// 'file-name.node'

var lib = require('./lib.js');
lib.square(4);

exports.circle = function () {};
exports.cube = function () {};
module.exports = function () {
    return {
       square: function () {}
    }:
};

(function (exports, require, __filename, __dirname) {
    // по факту весь код модуля обернут в эту функции и находится здесь
});

// Определение загружен ли модуль внутри другого модуля или его код выполняется отдельно

if (require.main === module) {
    console.log('standalone module');
} else {
    console.log('this module is loaded by require("./lib.js")');
}

var moduleEntryPoint = require.main.filename;

// Перечень значений, содержащихся в объект module

module = {...};
module.id = 'id';
module.filename = 'filename';
module.loaded = true; // if (module.loaded) {}
module.parent = {};
module.children = [...];
module.exports = exports = {...};

var lib = module.require('id');

// Timers

var timeout =  setTimeout(function (a, b) {}, 1000, a, b);
var interval =  setInterval(function (a, b) {}, 1000, a, b);
var immediate = setImmediate(function (a, b) {}, a, b);

clearTimeout(timeout);
clearInterval(interval);
clearImmediate(immediate);

timeout.ref();
timeout.unref();

process.nextTick(function () {});

// Util

var util = require('util');

util.inherits(constructorFunction, superConstructorFunction);

var result = util.format('string: %s, number: %d, json: %j', 'text', 123, {a: 1}); // 'string: text, number: 123, json: {"a": 1}'

util.inspect({a: 1, b: 2}, {showHidden: true, depth: null}); // '{a: 1, b: 2}'

var obj = {name: 'John'};
obj.inspect = function (depth) {return 'My name is ' + this.name;};
util.inspect(obj); // 'My name is John'

util.isString('text'); // true
util.isObject({a: 1}); // true
util.isRegExp(/abc/); // true

// OS

var os = require('os');

writeStream.write(data + os.EOL); // os.EOL - \n on POSIX, \r\n on Windows

console.log(os.userinfo({encoding: 'utf8'}));

console.log(os.arch); // arm, arm64, ia32, mips, mipsel, ppc
console.log(os.platform()); // aix, darwin, freebsd, linux, openbsd, sunos, win32
console.log(os.type()); // Linux, Darwin, Windows_NT

console.log(os.release()); // 6.1.7601

console.log(os.constants);

console.log(os.cpus());
console.log(os.cpus().length);

console.log(os.endianness()); // BE for big endian and LE for little endian

console.log(os.homedir());
console.log(os.tmpdir());
console.log(os.hostname());

console.log(os.loadavg()); // [0, 0, 0] - minutes load averages
console.log(os.uptime()); // 399711.205897

console.log(os.networkInterfaces());

console.log(os.totalmem()); // 1952432128
console.log(os.freemem()); // 196345856

// Process

if (process.mainModule) {}

process.nextTick(function () {});

console.log(process.argv); // ['C:\\work\\node.exe', 'server.js', '--start']
process.argv.forEach(function (arg) {console.log(arg);});
console.log(process.argv0);
console.log(process.execArgv); // ['--harmony']
console.log(process.execPath); // 'C:\work\node.exe

console.log(process.hrtime()); // [1159384, 69735025]

process.env.someValue = 1;
console.log(process.env);
delete process.env.someValue;

console.log(process.cwd()); // C:\work
process.chdir('/path/to/directory');

process.stdin.setEncoding();
process.stdin.on('readable', function () {
    var chunk = process.stdin.read();
    if (chunk !== null) {
        process.stdout.write('data: ' + chunk);
    }
});
process.stdin.on('end', function () {
    process.stdout.end('done');
});

console.log = function (message) {process.stdout.write('' + message + '\n');};

console.log(process.stdin);
console.log(process.stdout);
console.log(process.stderr);

process.stdin.pause();
process.stdin.resume();

process.abort();

process.on('beforeExit', function () {});
process.on('exit', function () {});
process.exit(1);
if (process.exitCode === 1) {}

process.on('message', function () {});
process.send(message, sendHandle, options, callback);

process.on('disconnect', function () {});
process.disconnect();

if (process.connected) {}

process.on('unheldedRejection', function () {});
process.on('heldedRejection', function () {});

process.on('warning', function (warning) {
    console.log(warning.name);
    console.log(warning.message);
    console.log(warning.stack);
});
process.emitWarning('Something happened', 'Custom warning');

process.on('SIGINT', function () {});
process.on('SIGHUP', function () {});
process.kill(process.pid, 'SIGHUP');

console.log(process.pid);

console.log(process.platform); // darwin, freebsd, linux, sunos, win32
console.log(process.arch); // ia32
console.log(process.release); // {name: 'node'}
console.log(process.version);
console.log(process.versions);

console.log(process.config);

console.log(process.cpuUsage(previousCpuUsageValue));
console.log(process.memoryUsage());
console.log(process.uptime());

console.log(process.title); // C:\WINDOWS\system32\cmd.exe - node.exe

process.unmask(0o022);

// Child Process

var child_process = require('child_process');

// Выполнить файл и получить результат выполнения сразу через значения stdout и stderr
child_process.execFile('node.exe', ['--version'], {encoding: 'utf8'}, function (error, stdout, stderr) {
    if (error) {
        console.log('exec file error: ' + error);
        return;
    }
    console.log('stdout: ' + stdout); // v6.2.2
    console.log('stderr: ' + stderr);
});

// Выполнить файл и получить поток его выполнения
var spawnProcessStream = child_process.spawn('dir', ['/L'], {shell: true, stdin: 'ignore'});
spawnProcessStream.stdout.on('data', function (data) {console.log('stdout: ' + data);});
spawnProcessStream.stderr.on('data', function (data) {console.log('stderr: ' + data);});
spawnProcessStream.on('close', function (code) {console.log('spawn process exited with code ' + code);});
spawnProcessStream.on('error', function (error) {console.log('Spawn process failed');});

// Выполнить команду в консоли командной строки и получить результат выполнения сразу через значения stdout и stderr
child_process.exec('file.bat', {encoding: 'utf8'}, function (error, stdout, stderr) {
    if (error) {
        console.log('exec file error: ' + error);
        return;
    }
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
});

// Выполнить код JavaScript-файла в отдельном процессе Node.js
child_process.fork('./work/server.js', ['127.0.0.1', '80'], {cwd: null, env: process.env});

// Child Process STDIN, STDOUT, STDERR

childProcess.stdin.on('data', function (data) {});
childProcess.stdio.on('data', function (data) {});
childProcess.stdout.on('data', function (data) {});
childProcess.stderr.on('data', function (data) {});

// Child Process Events

childProcess.on('disconnect', function () {});
childProcess.disconnect();

childProcess.on('close', function (code, signal) {
    console.log('child process closed with code: ' + code + ' and signal ' + signal);
});

childProcess.on('exit', function (code, signal) {
    console.log('child process exit with code: ' + code + ' and signal ' + signal);
});

childProcess.on('error', function (error) {});

childProcess.kill('SIGHUP');

childProcess.on('message', function (message, sendHandler) {
    console.log(message);
    sendHandler(); // <Handle> netSocket or netServer object or undefined
});

childProcess.send({hello: 'workd'}, require('net').createServer(), {keepOpen: true}, function (error) {
    if (error) {throw error;}
    console.log('Message sent');
});

if (process.connected) {
    process.send('message');
}

console.log(childProcess.pid);

// Cluster

var cluster = require('cluster');

var createServer = require('./server.js');

var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    var worker;
    for (var i = 0; i < numCPUs; i++) {
        worker = cluster.fork();
        worker.send('hi');
        // Навешиваем события на воркеры
        var numReqs = 0;
        Object.keys(cluster.workers).forEach(function (id) {
            cluster.workers[id].on('message', function (message) {
                if (message.cmd && message.cmd === 'notifyRequest') {
                    numReqs++;
                }
            });
        });
        cluster.on('exit', function (worker, code, signal) {
            console.log('worker ' + worker.process.pid + ' died');
            console.log('starting new worker');
            cluster.fork();
        });
        var timeout;
        Object.keys(cluster.workers).forEach(function (id) {
            var worker = cluster.workers[id];
            worker.on('listening', function (address) {
                worker.send('shutdown');
                worker.disconnect();
                timeout = setTimeout(function () {
                    worker.kill();
                }, 2000);
            });
            worker.on('disconnect', function () {
                clearTimeout(timeout);
            });
        });
    }
} else if (cluster.isWorker) {
    createServer(function (request, response) {
        response.writeHead(200);
        response.end('hello world');
        // notify master about the request
        process.send({cmd: 'notifyRequest'});
    });
    process.on('message', function (message) {
        if (message === 'shutdown') {
            // initiate graceful close of any connection to server
        }
        if (message === 'hi') {
            process.send('bye');
        }
    });
    cluster.on('exit', function (worker, code, signal) {
        if (worker.exitedAfterDisconnect === true) { // Set by calling .kill() or .disconnect(). Until then, it is undefined.
            console.log('It was voluntary - no need to worry');
        }
    });
}

// Cluster events

cluster.on('fork', function (worker) {});

cluster.on('setup', function () {});

cluster.on('online', function (worker) {console.log('worker responded after it was forked');});

cluster.on('listening', function (worker, address) {console.log('worker connected to ' + address.address + ':' + address.port);});

cluster.on('mesage'), function (worker, message, handle) {
    if (arguments.length === 2) {
        handle = message;
        message = worker;
        worker = undefined;
    }
});

cluster.on('disconnect', function (worker) {console.log('worker ' + worker.id + ' disconnected');});

cluster.on('exit', function (worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died ' + (signal || code) + '. Restaring...');
    cluster.fork();
});

// Cluster status and methods

if (cluster.isMaster) {
    // cluster code
    console.log('I am master');
    cluster.fork();
    cluster.fork();
} else if (cluster.isWorker) {
    // worker code
    console.log('I am worker ' + cluster.worker.id);
}

var worker = cluster.fork({env});

cluster.disconnect(function () {});

cluster.shedulingPolicy;

cluster.settings;

cluster.setupMaster({exec: 'worker.js', args: ['--use', 'http']});
cluster.fork(); // http worker

cluster.setupMaster({exec: 'worker.js', args: ['--use', 'https', silent: true]});
cluster.fork(); // https worker

// Worker events

var worker = cluster.fork();

worker.on('online', function () {console.log('worker is online');});

worker.on('listening', function (address) {console.log('worker is listening');});

worker.on('message', function (message) {
    if (message.cmd && message.cmd === 'notifyRequest') {
        requestNumber++;
    }
});
var requestNumber = 0;
process.send({cmd: 'notifyRequest'});

worker.on('disconnect', function () {console.log('Worker has disconnected');});
worker.disconnect();

worker.on('exit', function (code, signal) {
    if (signal) {
        console.log('worker was killed by signal: ' + signal);
    } else if (code !== 0) {
        console.log('worker exited with cocde: ' + code);
    } else {
        console.log('worker success');
    }
});

worker.on('error', function (error) {console.log('Error in worker process');});

// Worker status and methods

worker.kill('SIGTERM');
worker.send(message, sendHandle, callback);

if (worker.isDead()) {}
if (worker.isDisconnected()) {}
if (worker.exitedAfterDisconnect) {}

console.log(worker.id);
console.log(worker.process.pid);

// Пройтись по всем созданным worker

function forEachWorker (callback) {
    for (var id in cluster.workers) {
        callback(cluster.workers[id]);
    }
}

forEachWorker(function (worker) {
    worker.send('message to all workers');
});

// V8

var v8 = require('v8');

console.log(v8.getHeapStatistics());
console.log(v8.getHeapSpaceStatistics());
v8.setFlagsFromString('--trace_gc');

// VM

var vm = require('vm');

var script = new vm.Script('var a = 1; console.log(a);');
script.runInThisContext(); // 1

var sandbox = {globalVar: 1};
var script = new vm.Script('globalVar *= 2;');
script.runInNewContext(sandbox);
console.log(sandbox); // {globalVar: 2}

eval('var b = 3;');
console.log('b = ' + b); // b = 3

// REPL

var repl = require('repl');

repl.start('>>> ').context.m = 'Привет!'; // >>> 'Привет!'

// Debugger

debugger;

// Пример.

var x = 5;
setTimeout(function () {
    debugger;
    console.log('world');
}, 1000);
console.log('Hello');

// Events

var events = require('events');

class Observer extends events.EventEmitter {}

var myObserver= new Observer();

myObserver.addListener('click', function firstClick (a, b) {console.log('First event: ' + a + ', ' + b);});
myObserver.prependListener('click', function zeroClick () {console.log('Zero event');});
myObserver.on('click', secondClick); function secondClick () {console.log('Second event');}
myObserver.once('keyup', function () {console.log('First keyup event'); console.log(this);});
myObserver.prependOnceListener('keyup', function () {console.log('Zero keyup event');});

myObserver.emit('click', 1, 2); // | Zero event | First event: 1, 2 | Second event |
myObserver.emit('keyup'); // | Zero keyup event | First keyup event |

myObserver.removeListener('click', secondClick);
myObserver.removeAllListeners('keyup');

myObserver.on('error', function (error) {console.log('Произошла ошибка: ' + error);});
myObserver.emit('error', new Error('Ошибка!')); // Произошла ошибка!
process.on('uncaughtException', function (error) {console.log('Где-то произошла ошибка!');});

console.log(myObserver.eventNames()); // ['click', 'error']

console.log(myObserver.listeners('click')); // [[Function: zeroClick], [Function: firstClick]]

console.log(myObserver.listenerCount('click')); // 2

console.log(events.EventEmitter.defaultMaxListeners); // 10

myObserver.setMaxListeners(20);
console.log(myObserver.getMaxListeners()); // 20

// Stream

// Readable - читает информацию из источника
// Writable - записывает информацию
// Duplex - читает информацию из источника и записывает её далее
// Transform - преобразует информацию из источника

// Readable streams:
// http.Client.Response
// fs.createReadStream
// process.stdin
// child.process.stdout

var fs = require('fs');

var readFileStream = fs.createReadStream('input.txt', {encoding: 'utf8', hightWaterMark: 2});
var data = '';
readFileStream.on('readable', function () {
    data += readFileStream.read();
});
readFileStream.on('error', function (error) {
    console.log('Ошибка');
});

// 2 режима чтения потока:
// Flowing Mode - поток информации течет непрерывно и автоматически обрабатывается как можно скорее
// Paused Mode - информация получается с через вызов метода read()

// Flowing Mode
readStream.on('data', function (data) {});
readStream.pipe(writeStream);
readStream.resume();

// Paused Mode
readStream.removeAllListeners('data');
readStream.unpipe(writeStream);
readStream.pause();

// Events
readStream.on('readable', function () {result += readStream.read();}); // как только будет доступна следующая часть данных
readStream.on('data', function (data) {result += data;}); // как только будет автоматически прочитана часть данных
readStream.on('end', function () {console.log('чтение данных завершено');}); // как только больше не будет новых частей данных
readStream.on('close', function () {}); // как только поток для чтения будет закрыт
readStream.on('error', function (error) {}); // как только произойдет ошибка в процессе чтения данных

// Данные читаются в формате string или buffer.

// Пример.

var stream = require('stream');

class TemperatureReadableStream extends stream.Readable {
    constructor (options) {
        super(options);
        this.items = 0;
        this.maxItems = 10;
    }
    _read () {
        if (this.items++ < this.maxItems) {
            this.push({
                  date: new Date(2015, 9, this.items + 1)
                , temperature: Math.floor(Math.random() * 1000 - 273) + 'C'
            });
        } else {
            this.push(null);
        }
    }
}

var readTemperatureStream = new TemperatureReadableStream({objectMode: true});
var temperatureObject;
readTemperatureStream.on('readable', function () {
    temperatureObject = readTemperatureStream.read();
    while (temperatureObject !== null) {
        console.log(JSON.stringify(temperatureObject));
        temperatureObject = readTemperatureStream.read();
    }
});

// Дополнительный пример.

var readableStream = getReadableStreamSomehow();

readableStream.on('readable', function () {console.log('Теперь данные можно прочитать'); readableStream.read();});
readableStream.on('data', function (chunk) {console.log(chunk.length);});
readableStream.on('end', function () {console.log('Все данные успешно прочитаны.'); writableStream.end('Goodbye')});
readableStream.on('close', function () {console.log('Поток чтения данных закрыт.');});
readableStream.on('error', function (error) {console.log('Ошибка!');});

readableStream.pause();
if (readableStream.isPaused()) {
    console.log('поток чтения роставлен на паузу');
}
readableStream.resume();

readableStream.setEncoding('utf8');

readableStream.read();
readableStream.end();

readerStream.pipe(writableStream);
readerStream.unpipe(writableStream);

// Writable streams:
// http.Client.Rerquest
// fs.createWriteStream
// process.stdout
// child_process.stdin

var fs = require('fs');

var writeFileStream = fs.createWriteStream('output.txt');
for (var i = 0; i < 10; i++) {
    writeFileStream.write('chunk ' + i);
}
writeFileStream.end('Done');

// Events
writeStream.on('drain', function () {}); // как только метод write() вернул значение false, событие drain сообщает о том, что поток готов принять новые данных для записи
writeStream.on('finish', function () {}); // как только поток закончит записывать данные
writeStream.on('close', function () {}); // как только поток для записи данных будет закрыт
writeStream.on('error', function () {}); // как только произойдет ошибка в процессе записи данных
writeStream.on('pipe', function () {}); // как только поток чтения будет подключен к потоку записи данных
writeStream.on('unpipe', function () {}); // как только поток чтения будет отключен от потока записи данных

// Пример.

var stream = require('stream');

class WriteStream extends stream.Writable {
    _write (chunk, encoding, callback) {
        console.log('write: ' + chunk.toString());
        callback();
    }
}

var writeStream = new WriteStream();
for (var i = 0; i < 10; i++) {
    writeStream.write('chink ' + 1);
}
writeStream.end();

// Buffering
writeStream.cork(); // поместить все данные в буфер памяти для дозирования потока
writeStream.uncork(); // очистить данные из буфера памяти

// Пример.

var writeStream = fs.createWriteStream('output.txt');

writeStream.write('Start');

writeStream.cork();

for (var i = 0; i < 10; i++) {
    writeStream.write('chunk ' + i);
}
setTimeout(function () {

    writeStream.uncork();

    writeStream.end('Done');

}, 2000);

// Дополнительный пример.

var writableStream = getWritableStreamSomehow();

writableStream.on('drain', function writeData () {
    writableStream.write('some data', 'utf8');
});
writableStream.on('pipe', function (source) {console.log('Что-то подключилось к потоку записи данных.');});
writableStream.on('unpipe', function (source) {console.log('Что-то отключилось от потока записи данных.');});
writableStream.on('finish', function () {console.log('Все данные успешно записаны.');});
writableStream.on('close', function () {console.log('Поток записи данных закрыт.');}});
writableStream.on('error', function (error) {console.log('Ошибка!');});

writableStream.setDefaultEncoding('utf8');
writableStream.write('some data', 'utf8', function () {console.log('Writing...');});
writableStream.write('some more data', 'utf8', function () {console.log('Writing...');});
writableStream.end('done writing data', 'utf8', function () {console.log('Done');});

readerStream.pipe(writableStream);
readerStream.unpipe(writableStream);

writableStream.cork(); // поместить все данные в буфер памяти для дозирования потока
writableStream.uncork(); // очистить данные из буфера памяти

// Piping

var fs = require('fs');

var readStrem = fs.createReadStream('input.txt');
var writeStream = fs.createWriteStream('output.txt');

writeStream.on('pipe', function () {
    console.log('read stream piped to write stream');
});

readStream.pipe(writeStream);

// Duplex streams:
// tcp sockets
// zlib streams
// crypto streams

var stream = require('stream');

class DuplexStream extends stream.Duplex {
    _read (size) {...}
    _write (chunk, encoding, callback) {...}
}

// Transform streams
// Трансформирует поток чтения по заданным правилам и передает его в поток записи

var fs = require('fs');
var readStream = fs.createReadStream('input.txt');
var writeStream = fs.createWriteStream('output.txt');

var stream = require('stream');

class ToUpperCaseStream extends stream.Transform {
    _transform (chunk, encoding, callback) {
        this.push(chunk.toString().toUpperCase());
        callback();
    }
}

var toUpperCaseStream = new ToUpperCaseStream();

readStream.pipe(toUpperCaseStream).pipe(writeStream);

// _flush(callback) - вызывается как только все данные будут потреблены прямо перед событием end

// Path

var path = require('path');

           if (path.posix) {var root = path.parse('/home/user/dir/file.txt').root; // '/'
} else if (path.win32) {var root = path.parse('C:\\path\\dir\\file.txt').root; // 'C:\'
}

path.dirname('/foo/bar/baz/asdf/quux'); // return '/foo/bar/baz/asdf'

path.basename('/foo/bar/baz/asdf/quux.html'); // return 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', 'html'); // return 'quux'

path.extname('index.coffee.md'); // return '.md'
path.extname('index.'); // return '.'
path.extname('index'); // return ''

path.delimiter; // ; for Windows and : for POSIX
process.env.PATH.split(path.delimiter); // ['C\\Windows\\system32', 'C\\Windows', 'C:\\Program Files\\node\\']

path.sep; // \ on Windows and / on POSIX
'foo/bar/baz'.split(path.sep); // return ['foo', 'bar', 'baz']

path.parse('/home/user/dir/file.txt'); // return {root: '/', dir: '/home/user/dir', base: 'file.txt', name: 'file', ext: '.txt'}

path.format({root: '/', base: 'file.txt'}); // return '/file.txt'
path.format({root: '/', name: 'file', ext: '.txt'}); // return '/file.txt'
path.format({base: 'file.txt'}); // return 'file.txt'
path.format({dir: '/home/user/dir', base: 'file.txt'}); // return '/home/user/dir/file.txt'
path.format({root: 'C:\\', dir: 'C:\\path\\dir', base: 'file.txt', name: 'file', ext: '.txt'}); // return 'C:\\path\\dir\\file.txt'

path.normalize('/foo//bar//baz//asdf/quux'); // return '/foo/bar/baz/asdf/quux'
path.normalize('C:\\temp\\\\foo\\bar'); // return 'C:\\temp\\foo\\bar''

path.join('/foo', 'bar', 'baz/asdf', 'quux'); // return '/foo/bar/baz/asdf/quux'

path.resolve('/foo/bar', './baz'); // return '/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file'); // return '/tmp/file'
path.resolve('www', 'static_files/png/', '../gif/image.gif'); // Current Working Directory is '/home/user/node' -> return '/home/user/node/www/static_files/gif/image.png'

path.relative(from, to);
path.relative('data/orandea/text/aaa', '/data/ornadea/impl/bbb'); // return '../../impl/bbb'

path.isAbsolute('C:/foo/'); // true
path.isAbsolute('/foo/bar'); // true
path.isAbsolute('quux/'); // false
path.isAbsolute('.'); // false

// File System

var fs = require('fs');

// Константы

fs.constants.F_OK - файл виден текущему процессу
fs.constants.R_OK - файл может быть прочитан
fs.constants.W_OK - файл может быть записан
fs.constants.X_OK - файл может быть выполнен

// Открытие файла для чтения

fs.open('/dir/file.txt', 'r', function (error, fileDescriptor) { // fileDescriptor - переменная, хранящая в себе ссылку на открытый файл - число
    if (error) {
        if (error.code === 'ENOENT') {
            console.log('Файл не существует');
            return;
        } else {
            throw error;
        }
    }
    readMyData(fileDescriptor);
});

// Открытие файла для записи

fs.open('/dir/file.txt', 'wx', function (error, fileDescriptor) { // fileDescriptor - переменная, хранящая в себе ссылку на открытый файл - число
    if (error) {
        if (error.code === 'EEXIST') {
            console.log('Файл уже существует');
            return;
        } else {
            throw error;
        }
    }
    writeMyData(fileDescriptor);
});

// Закрыть открытый файл

fs.close(fileDescriptor); // fileDescriptor - переменная, хранящая в себе ссылку на открытый файл - число

// Прочитать данные

fs.read(fileDescriptor, buffer, offset, length, position, function (error, bytesRead, buffer) {});

// Прочитать данные из файла

fs.readFile('path/to/file.txt', {encoding: 'utf8', flags: 'r'}, function (error, data) {
    if (error) {throw error;}
    console.log(data);
});

// Записать данные

fs.write(fileDescriptor, buffer, offset, length, position, function (error, written, buffer) {});
fs.write(fileDescriptor, 'data', postion, 'utf8', function (error, writte, string) {});

// Записать данные в файл или создать новй файл

fs.writeFile('path/to/file.txt', 'some text', {encoding: 'utf8', mode: 0o666, flag: 'w'}, function (error) {
    if (error) {throw error;}
    console.log('Данные в файл записаны');
});

// Запись данных в конец файла

fs.appendFile('/dir/file.txt', 'data to append', 'utf8', function (error) {
    if (error) {throw error;}
    console.log('Данные успешно записаны в конец файла');
});

// Обрезать файл

fs.truncate('path/to/file.txt', 10, function (error) {});

fs.ftruncate(fileDescriptor, 10, function (error) {
    if (error) {throw error;}
    console.log('Файл обрезан до 10 байт');
});

// Удаление файла

fs.unlink('/dir/file.txt', function (error) {
    if (error) {
        console.log('Файл не найден');
        return;
    }
    console.log('Файл успешно удален');
});

fs.unlinkSync('/dir/file.txt');
console.log('Файл успешно удален');

// Переименование или перемещение файла в другую директорию

fs.rename('/dir/file-1.txt', '/dir/file-2.txt', function (error) {
    if (error) {
        console.log('Невозможно переименовать файл');
        return;
    }
    console.log('Файл успешно переименован');
});

// Получение информации о файле

fs.stat('/dir/file.txt', function (error, stats) {
    if (error) {
        console.log('Файл не найден');
        return;
    }
               if (stats.isFile()) { // some code
    } else if (stats.isDirectory()) { // some code
    } else if (stats.isBlockDevice()) { // some code
    } else if (stats.isCharacterDevice()) { // some code
    } else if (stats.isSymbolicLink()) { // some code
    } else if (stats.isFIFO()) { // some code
    } else if (stats.isSocket()) { // some code
    }
    console.log('Информация о файле: ' + JSON.stringify(stats));
});

// Опеределния возможности доступа к файлу

fs.access('path/to/file.txt', fs.constants.R_OK | fs.constants.W_OK, function (error) {
    if (error) {
        console.log('К файлу не доступа');
    } else {
        console.log('Файл доступен для чтения и записи');
    }
});

// Отслеживание изменения файла

fs.watch('/dir/file.txt', {persistent: true, recursive: true, encoding: 'buffer'}, function (eventType, filename) {
    console.log('event type: ' + eventType);
    if (filename) {
        console.log(filename); // Prints: <Buffer ...>
    } else {
        console.log('filename not provided');
    }
});

fs.watchFile('/dir/file.txt', function (curr, prev) {
    console.log('current mtime: ' + curr.mtime);
    console.log('previous mtime: ' + prev.mtime);
});

var watcher = fs.watch();
watcher.on('change', function () {});
watcher.on('error', function (error) {});
watcher.close();

// Отменить отслеживание измнения файла

fs.unwatch('filename', listener);

// Изменение прав доступа к файлу

fs.chmod('path/to/file.txt', 777, function (error) {});

// Изменение владельца файла

fs.chown('path/to/file.txt', uid, gid, function (error) {});

// Прочитать ссылку на файл

fs.readlink('path/to/file.txt', {encoding: 'utf8'}, function (error, linkString) {});

// Создать ссылку на файл

fs.link('path/to/file.txt', 'path/to/dstfile.txt', function (error) {});

fs.symlink('./foo', './new-port');

// Получить реальный путь до файла

fs.realpath('path/to/file.txt', {encoding: 'utf8'}, function (error, resolvedPath) {});

// Создание директории

fs.mkdir('path/to/directory', 777, function (error) {});

// Удаление директории

fs.rmdir('path/to/directory', function (error) {});

// Создание директории для временных файлов

fs.mkdtemp('/tmp/foo', function (error, folder) {
    if (error) {throw error;}
    console.log(folder);
});

// Создание потока чтения

var readStream = fs.readStream();

readStream.on('open', function () {});
readStream.on('close', function () {});

console.log(readStream.bytesRead);
console.log(readStream.path);

// Создание потока записи

var writeStream = fs.writeStream();

writeStream.on('open', function () {});
writeStream.on('close', function () {});

console.log(writeStream.bytesWritten);
console.log(writeStream.path);

// Создание потока чтения из файла

var readFileStream = fs.createReadStream('/dir/file.txt', {flags: 'r', encoding: 'utf8', fs: null, mode: 0o666, autoClose: true, start: 90, end: 99});
var data = '';
readFileStream.on('readable', function () {
    data += readFileStream.read();
});
readFileStream.on('error', function (error) {
    console.log('Ошибка');
});

// Создание потока записи в файл

var writeFileStream = fs.createWriteStream('/dir/file.txt', {flags: 'w', defaultEncoding: 'utf8', fs: null, mode: 0o666, autoClose: true, start: 15});
for (var i = 0; i < 10; i++) {
    writeFileStream.write('chunk ' + i);
}
writeFileStream.end('Done');

// Readline - чтение по строке за раз из потока чтения

var readline = require('readline');

readline.clearLine(stream, dir);
readline.clearScreenDown(stream);

readline.cursorTo(stream, x, y);
readline.moveCursor(strean, dx, dy);

readline.emitKeypressEvents(stream, interface);
readline.emitKeypressEvents(procecss.stdin);
if (process.stdin.isTTY) {
    process.stdinsetRawMode(true);
}

var readLine = readline.createInterface({
      input: process.stdin
    , output: process.stdout
});

readLine.on('line', function (line) {
    console.log('Received: ' + line);
});

readLine.question('What do you think?', function (answer) {
    // write answer in Database
    console.log('Thank you for your feedback: ' + answer);
    readLine.close();
});

// Events

readLine.on('line', function (input) {console.log('Received: ' + input);});
readLine.on('pause', function () {console.log('Paused');});
readLine.on('resume', function () {console.log('Resumed');});
readLine.on('close', function () {});

readLine.on('SIGCONT', function () {
    readLine.prompt(); // "prompt" will automatically resume the stream
});

readLine.on('SIGINT', function () {
    readLine.question('Are you sure you want to exit?', function (answer) {
        if (answer.match(/^y(es)$/i)) {
            readLine.pause();
        }
    });
});

readLine.on('SIGTSTP', function () {
    console.log('Caught SIGTSTP');
});

// Methods

readLine.pause();
readLine.resume();
readLine.close();

readLine.question('What is your name?', function (answer) {console.log('Your name is ' + answer);});
readLine.prompt(preserveCursor);
readLine.setPrompt(prompt);

readLine.write(data, key);
readLine.write('Delete this!');
readLine.write(null, {ctrl: true, name: 'u'}); // Similar Ctrl+u to delete the line written previously

// Completer function

function completer (line) {
    var completions = '.help .error .exit .quit .q'.split(' ');
    var hits = completions.filter(function (c) {
        return c.indexOf(line) === 0;
    });
    return [hits.length ? hits : completions, line]; // show all completions if none found
}

funciton completer (linePartial, callback) {
    callback(null, [['123'], linePartial]);
}

// Пример Tiny CLI

var readline = require('readline');

var readLine = readline.createInterface({
      input: process.stdin
    , output: process.stdout
    , prompt: '>>> '
});

readLine.on('line', function (line) {
    switch (line.trim()) {
        case 'hello': console.log('world'); break;
        default: console.log('Say something.');
    }
    line.prompt();
});

readLine.on('close', function () {
    console.log('Goodbye');
    process.exit(0);
});

readLine.prompt();

// Пример чтения из потока файла линия за линией

var readline = require('readline');
var fs = require('fs');

var readLine = readline.createInterface({
    input: fs.createReadStream('./file.txt')
});

readLine.on('line', function (line) {
    console.log('Line from file: ' + line);
});

// TTY

var tty = require('tty');

if (process.stdout.isTTY) {}

// tty.ReadStream

if (readStream.isRaw) {}
readStream.setRawMode(mode);

// tty.WriteStream

process.stdout.on('resize', function () {
    console.log('screen size has changed');
    console.log(process.stdout.columns + 'x' + process.stdout.rows);
});

var columns = writeStream.columns;
var rows = writeStream.rows;

if (tty.isatty(fileDescriptor)) {}

// Buffer

console.log(buffer.INSPECT_MAX_BYTES); // 50
console.log(buffer.kMaxLength); // ~ 1GB

var buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); // deprecated
var buf = new Buffer(new Buffer('buffer')); // deprecated
var buf = new Buffer(new Uint16Array(2), 0, 2); // deprecated
var buf = new Buffer(10); // deprecated
var buf = new Buffer('text', 'utf8'); // deprecated

var buf = new SlowBuffer(size); // deprecated

var buf = Buffer.alloc(10, 'a' , 'utf8');
var buf = Buffer.allocUnsafe(10)
var buf = Buffer.allocUnsafeSlow(10);

var str = '0123456789';
for (var index = 0; index < str.length; index++) {
    buf[index] = str.charAt(index);
}

var buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
var buf = Buffer.from(new Buffer('buffer'));
var buf = Buffer.from(new Uint16Array(2), 0, 2);
var buf = Buffer.from('text', 'utf8');

var isBuffer = Buffer.isBuffer(new Buffer('buffer'));

var isBufferEncodingUTF8 = Buffer.isEncoding('utf8');

var bytesLength = Buffer.bytesLength('text', 'utf8');

var buf = Buffer.alloc(5);
console.log(buf.length);

var result = buf.toString('base64', start, end);
var result = buf.toJSON();

var isEqual = Buffer.compare(Buffer.form(['1234']), Buffer.form(['0123']));
var isEqual = buf1.compare(buf2, targetStart, targetEnd, sourceStart, sourceEnd);
var isEqual = buf1.equals(buf2);

buf.write('text', offest, length, 'utf8');
buf.writeInt8(2, offset, false);
buf.writeUInt8(0x3, offset, false);
buf.writeFloatBE(0xcafebabe, offset, false);
buf.writeDoubleBE(0xdeadbeefcafebabe, offset, false);

var buf = Buffer.concat([buf1, buf2, buf3], buf1.length + buf2.length + buf3.length);

buf1.copy(buf2, targetStart, sourceStart, sourceEnd);

var buf2 = buf1.slice(0, 3);

var index = buf.indexOf('abc', byteOffset, 'utf8');
var lastIndex = buf.lastIndexOf('abc', byteOffest, 'utf8');

var includes = buf.includes('abc', byteOffset, 'utf8');

var poolSize = Buffer.poolSize; // 8192

var buf = Buffer.from('buffer');
for (var pair of buf.entries()) {
    console.log(pair); // [0, 98]
}

var buf = Buffer.from('buffer');
for (var key of buf.keys()) {
    console.log(key); // 0
}

var buf = Buffer.from('buffer');
for (var value of buf.values()) {
    console.log(value); // 98
}

var buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
console.log(buf); // <Buffer 01 02 03 04 05 06 07 08>
buf.swap16();
console.log(buf); // <Buffer 02 01 04 03 06 05 08 07>
buf.swap16();
buf.swap32();
buf.swap64();

var result = buf.readInt8(offset, false);
var result = buf.readUInt8(offset, false);
var result = buf.readFloatBE(offset, false);
var result = buf.readDoubleBE(offset, false);

// StringDecoder

var StringDecoder = require('string_decoder').StringDecoder;

var decoder = new StringDecoder('utf8');
decoder.write(buffer);
decoder.end(buffer);

var cent = Buffer.from([0xC2, 0xA2]);
console.log(decoder.write(cent));

var euro = Buffer.from([0xE2, 0x82, 0xAC]);
console.log(decoder.write(euro));

decoder.write(Buffer.from([0xE2]));
decoder.write(Buffer.from([0x82]));
console.log(decoder.end(Buffer.from([0xAC])));

// Punycode - deprecated

var punycode = require('punycode');

console.log(punycode.version); // 1.2.0

punycode.decode('maana-pta'); // 'mahana'
punycode.decode('--dqo34k'); // ':)' - смайлик

punycode.encode('mahana.com'); // 'xn--maana-pta.com'
punycode.encode(':).com'); // 'xn----dqo34k.com'

punycode.toASCII('mahana.com'); // 'xn--maana-pta.com'
punycode.toASCII(':).com'); // 'xn----dqo34k.com'

punycode.toUnicode('xn----maana-pta.com'); // 'mahana.com'
punycode.toUnicode('xn--dqo34k.com'); // ':).com'

punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306] - сурогатная пара

punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06' - сурогатная пара

// URL

var url = require('url');

Escapes characters: < > " ` \r \n \t {} | \ ^ '

var urlObject = url.parse('http://username:password@host.com/8080/p/a/t/h?query=string#hash', parseQueryString, slashesDenoteHost);

urlObject.href; // 'http://username:password@host.com/8080/p/a/t/h?query=string#hash'
urlObject.protocol; // 'http'
if (urlObject.slashes) {} // true если требуется, чтобы за двоеточием (:) в протоколе следовало 2 прямых слэша (//)
urlObject.auth; // 'username:password'
urlObject.host; // 'host.com:8080'
urlObject.hostname; // 'host.com'
urlObject.port; // '8080'
urlObject.path; // '/p/a/t/h?query=string'
urlObject.pathname; // '/p/a/t/h'
urlObject.search; // '?query=string'
urlObject.query; // 'query=string' или {query: 'string'}
urlObject.hash; // '#hash'

url.format(urlObject); // 'http://username:password@host.com/8080/p/a/t/h?query=string#hash'

url.resolve(from, to);
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'

// Query String

var querystring = require('querystring');

querystring.parse('w=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&foo=bar', null, null, {decodeURIComponent: gbkDecodeURIComponent});
querystring.stringify({w: 'привет', foo: 'bar'}, null, null, {encodeURIComponent: gbkEncodeURIComponent});

querystring.escape('w=привет&foo=bar');
querystring.unescape('w=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&foo=bar');

// DNS

var dns = require('dns');

dns.lookup('google.com', {family: 4, hints: dns.ADDRCONFIG | dns.V4MAPPED, all: false}, function (error, addresses, family) {
    console.log('addresses: ' + addresses);
});

dns.lookupService('127.0.0.1', 22, function (error, hostname, service) {
    console.log(hostname, service); // localhost ssh
});

dns.resolve('google.com', 'TXT', function (error, addresses) {});

dns.resolve4('google.com', function (error, addresses) {
    if (error) {throw error;}
    console.log('addresses: ' + JSON.stringify(addresses));
    addresses.forEach(function (a) {
        dns.reverse(a, function (error, hostnames) {
            if (error) {throw error;}
            console.log('reverse for ' + a ':' + JSON.stringify(hostnames));
        });
    });
});

dns.reverse(ip, function (error, hostnames) {});

// HTTP

http.METHODS
http.STATUS_CODES

http.globalAgent

http.get('http://www.google.com/index.html', function (response) {
    console.log('Got response: ' + response.statusCode);
    response.resume();
}).on('error', function (error) {
    console.log('Got error: ' + error.message);
});

http.createClient(80, '127.0.0.1'); // deprecated

var request = http.request({
      hostname: 'www.google.com'
    , port: 80
    , path: '/'
    , method: 'POST'
    , headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        , 'Content-Length': Buffer.byteLength(querystring.stringify({msg: 'Hello World!'}))
      }
}, function (response) {
    console.log('STATUS: ' + response.statusCode);
    console.log('HEADERS: ' + JSON.stringify(response.headers));
    response.setEncoding('utf8');
    response.on('data', function (chunk) {console.log('BODY: ' + chunk);});
    response.on('end', function () {console.log('Node more data in response');});
});
request.on('error', function (error) {console.log('Problem with request: ' + error.message);});
request.write(querystring.stringify({msg: 'Hello World!'}));
request.end();

var server = http.createServer(function (request, response) {
    response.writeHead(200, 'OK');
    response.write('Hello');
    response.end('World');
});
server.listen(80);

// http.get

http.get({
      hostname: 'localhost'
    , port: 80
    , path: '/'
    , agent: false // create a new agent just for this one request
}, function (response) {
    // Do stuff with response
}).on('socket', function (socket) {
    socket.emit('agentRemove');
});

// http.Agent

var http = require('http');

var keepAliveAgent = new http.Agent({keepAlive: true});

http.request({
      hostname: 'localhost'
    , port: 80
    , path: '/'
    , agent: keepAliveAgent
}, function (request, response) {
    // Do stuff with response
});

agent.createConnection(options, function (error, stream) {});
agent.destroy();
agent.getName(options); // return host:port:localAddress
agent.freeSockets;
agent.maxFreeSockets = 256;
agent.maxSockets;
agent.requests;
agent.sockets;

// http.ClientRequest

request.on('response', function (response) {});
request.on('socket', function (socket) {});
request.on('continue', function () {});
request.on('abort', function () {});
request.on('aborted', function () {});
request.on('checkExpectation', function (request, response) {});
request.on('upgrade', function (response, socket, head) {});

// Create HTTP tunneling proxy

var http = require('http');
var net = require('net');
var url = require('url');

var proxy = http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('ok');
});

proxy.on('connect', function (response, clientSocket, head) {
    // connect to origin server
    var serverUrl = url.parse('http://' + request.url);
    var serverSocket = net.connect(serverUrl.port, serverUrl.hostname, function () {
        clientSocket.write('HTTP/1.1 200 Connection Established\r\nProxy-agent: Node.js-Proxy\r\n\r\n');
        serverSocket.write(head);
        serverSocket.pipe(clientSocket);
        clientSocket.pipe(serverSocket);
    });
});

// now that proxy is running
proxy.listen(1337, '127.0.0.1', function () {
    // make request to tunneling proxy
    var request = http.request({
          hostname: '127.0.0.1'
        , port: 1337
        , path: 'www.google.com:80'
        , method: 'CONNECT'
    });
    request.end();
    request.on('connect', function (response, socket, head) {
        console.log('got connected!');
        // make request over HTTP tunnel
        socket.write('GET / HTTP/1.1\r\nHost: www.google.com:80\r\nConnection: close\r\n\r\n');
        socket.on('data', function (chunk) {console.log(chunk.toString());});
        socket.on('end', function () {proxy.close();});
    });
});

// http.createServer

var http = require('http');

var server = http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('Hello');
    response.end('World');
});

server.on('upgrade', function (request, socket, head) {
    socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\n\r\n');
    socket.pipe(socket); // echo back
});

// now that server is running
server.listen(1337, '127.0.0.1', function () {
    // make request
    var request = http.request({
          hostname: '127.0.0.1'
        , port: 1337
        , headers: {
              'Connection': 'Upgarde'
            , 'Upgrade': 'websocket'
          }
    });
    request.end();
    request.on('upgrade', function (response, socket, upgradeHead) {
        console.log('got upgraded!');
        socket.end();
        process.exit(0);
    });
});

request.flushHeaders();
request.write('some text', 'utf8', function () {});
request.end('some text', 'utf8', function () {});
request.abort();
request.setNoDelay(noDelay);
request.setSocketKeepAlive(enable, initialDelay);
request.setTimeout(1500, function () {});

// http.Server

// Events

server.on('connect', function (request, socket, head) {});
server.on('connection', function (socket) {});
server.on('request', function (request, response) {});
server.on('upgrade', function (request, socket, head) {});
server.on('checkContinue', function (request, response) {});
server.on('close', function () {});
server.on('clientError', function (exception, socket) {});

var http = require('http');
var server = http.createServer(function (request, response) {response.end();});
server.on('clientError', function (error, socket) {socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');});
server.listen(8000);

// Methods

server.listen(handle, function () {});
server.listen('127.0.0.1', function () {});
server.listen(80, '127.0.0.1', backlog, function () {});
if (server.listening) {}
server.close(function () {});

server.maxHeadersCount = 1000;
server.setTimeout(1500, function () {});
server.timeout = 120000;

// http.ServerResponse

// Events

respose.on('finish', function () {});
respose.on('close', function () {});

// Methods

response.writeHead(200, 'OK', {'Content-Length': Buffer.byteLength('hello'), 'Content-Type': 'text/plain', 'Trailer': 'Content-MD5'});
response.write('some text', 'utf8', function () {});
response.writeContinue(); // send HTTP/1.1 100 Continue message to the client
response.end('some text', 'utf8', function () {});

respose.addTrailers({'Content-MD5': '7895b5573b623g62l62qw'});

response.statusCode = 404;
response.statusMessage = 'Not Found';

response.setTimeout(1500, function () {});

response.setHeader('Content-Type', 'text/html');
response.setHeader('Set-Cookie', ['type=ninjs', 'language=javasript']);

var contentType = respose.getHeader('content-type');

respose.removeHeader('Content-Encoding');

if (respose.headersSend()) {}

console.log(response.sendDate);

if (response.finished) {}

var server = http.createServer(function (request, response) {
    response.setHeader('Content-Type', 'text/html');
    response.setHeader('X-Foo', 'bar');
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('Hello');
    response.end('World'); // return content-type = text/plain
});

// http.IncomingMessage

// Events

message.on('aborted', function () {});
message.on('close', function () {});

// Methods

message.setTimeout(1500, function () {});
message.destroy();
message.statusCode = 404;
message.statusMessage = 'Not Found';
console.log(message.rawHeaders); // ['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(message.headers); // {'user-agent': 'curl/7.22.0', host: '127.0.0.1:8000', accept: '*/*'}
console.log(message.httpVersion);
console.log(message.url); // '/status?name=ryan'
console.log(message.method);
console.log(message.rawTrailers);
console.log(message.trailers);
console.log(message.sockets);

// HTTPS

// https.globalAgent

// https.get

var https = require('https');

https.get('https://encrypted.google.com/', function (response) {
    console.log('Status code: ' + response.statusCode);
    console.log('Header: ' + response.headers);
    response.on('data', function (data) {process.stdout.write(data);});
}).on('error', function (error) {
    console.error(error);
});

// https.request

var https = require('https');
var fs = require('fs');

var request = https.request({
      hostname: 'encrypted.google.com'
    , port: 443
    , path: '/'
    , method: 'GET'
    , key: fs.readFileSync('test/keys/agent2-key.pem')
    , cert: fs.readFileSync('test/keys/agent2-cert.pem')
    , agent: new https.Agent(options) // или false
}, function (response) {
    console.log('Status code' + response.statusCode);
    console.log('Headers: ' + response.headers);
    response.on('data', function (data) {process.stdout.write(data);});
});
request.end();
request.on('error', function (error) {console.error(error);});

// https.createServer

var https = require('https');
var fs = require('fs');

var httpsServer = https.createServer({
      key: fs.readFileSync('test/keys/agent2-key.pem')
    , cert: fs.readFileSync('test/keys/agent2-cert.pem')
// , pfx: fs.readFileSync('server.pfx')
}, function (request, response) {
    response.writeHead(200);
    response.write('Hello');
    response.end('World');
});
httpsServer.listen(8000);

// Method

httpsServer.listen(handle, function () {});
httpsServer.listen(path, function () {});
httpsServer.listen(80, '127.0.0.1', backlog function () {});

httpsServer.close();

httpsServer.setTimeout(1500, function () {});
httpsServer.timeout;

// Net

var net = require('net');

var server = net.Server();

// Events

server.on('listening', function () {});
server.on('connection', function () {});
server.on('close', function () {});
server.on('error', function () {});

// Method

var address = server.address();

var server = net.createServer(function (socket) {
    socket.end('bye');
}).on('error', function (error) {throw error;});

// grab random port
server.listen(function () {
    console.log('opened server on ' + server.address()); // don't call server.address() until the 'listening' event has been emitted
});

server.listen(handle, backlog, callback);
server.listen(options, callback);
server.listen(path, backlog, callback);
server.listen(port, hostname, backlog, callback);
server.listen({host: 'localhost', port: 80, exclusive: true});
net.createServer().listen(path.join('\\\\?\\pipe', process.cwd(), 'myctl'));

if (server.listening) {}

server.close();

console.log(server.connections); // deprecated
server.getConnections(function (error, count) {
    console.log('Connections number: ' = count);
});

console.log(server.maxConnections);

server.on('error', function (error) {
    if (error.code === 'EADDRINUSE') {
        console.log('Address in use, retrying...');
        setTimeout(function () {
            server.close();
            server.listen(PORT, HOST);
        }, 1000);
    }
});

server.ref();
server.unref();

// net.Socket

var socket = new http.Socket({
      fs: null
    , allowHalfOpen: false
    , redable: false
    , writable: false
});

// Events

socket.on('connect', function () {});
socket.on('lookup', function () {});
socket.on('drain', function () {});
socket.on('data', function () {});
socket.on('end', function () {});
socket.on('close', function () {});
socket.on('timeout', function () {});
socket.on('error', function () {});

// Methods

var address = socket.address(); // return {port: 1234, family: 'IPv4', address: '127.0.0.1'}

socket.bufferSize;
socket.bytesRead;
socket.bytesWritten;

socket.connect(options, connectListener);
socket.connect(path, connectListener);
socket.connect(port, host, connectListener);

if (socket.connectiong) {}

socket.destroy(exception);
if (socket.destroyed) {}

socket.write(data, 'utf8', function () {});
socket.end(data, 'utf8');

socket.remoteAddress(); // '74.125.127.100'
socket.remoteFamily(); // 'IPv6'
socket.remotePosrt(); // 80

socket.localAddress; // '192.168.1.1'
socket.localPort; // 80

socket.pause();
socket.resume();

socket.setEncoding('utf8');
socket.setKeepAlive(ebable, initialDelay);
socket.setNoDelay(noDelay);

socket.setTimeout(1500, function () {});

socket.ref();
socket.unref();

// connect

net.connect(options, connectListener);
net.connect(path, connectListener);
net.connect(port, host, connectListener);

var net = require('net');

var client = net.connect({port: 8124}, function () {
    // 'connect' listener
    console.log('connected to server');
    client.write('world');
});
client.on('data', function (data) {
    console.log(data.toString());
    client.end();
});
client.on('end', function () {
    console.log('desconnected from server');
});

// createConnection

net.createConnection(options, connectListener);
net.createConnection(path, connectListener);
net.createConnection(port, host, connectListener);

var net = require('net');

var client = net.createConnection({port: 8124}, function () {
    // 'connect' listener
    console.log('connected to server');
    client.write('world');
});
client.on('data', function (data) {
    console.log(data.toString());
    client.end();
});
client.on('end', function () {
    console.log('desconnected from server');
});

// createServer

net.createServer({
      allowHalfOpen: false
    , pauseOnConnect: false
}, connectionListener);

var net = require('net');

var server = net.createServer(function (connection) {
    // 'connection' listener
    console.log('client connected');
    connection.on('end', function () {
        console.log('client diconnected');
    });
    connection.write('hello');
    connection.pipe(connection);
});

server.on('error', function (error) {throw error;});
server.listen(8124, function () {console.log('server bound');});

>>> telnet localhost 8124

// net.isIPv4

if (net.isIP('input')) {}
if (net.isIPv4('input')) {}
if (net.isIPv6('input')) {}

// UDP / Datagaram Sockets

var dgram = require('dgram');

dgram.createSocket(options, function () {});
dgram.createSocket(type, function () {});

var server = dgram.createServer('udp4');

server.on('listening', function () {
    var address = server.address();
    console.log('server listening ' + address.address + ':' + address.port);
});

server.on('message', function (message, rinfo) {
    console.log('server got: ' + message + ' from ' + rinfo.address + ':' + rinfo.port);
});

server.on('error', function (error) {
    console.log('server error: ' + error.stack);
    server.close();
});

server.bind(41234); // server listening: 0.0.0.0:41234

server.bind({
      address: 'localhost'
    , port: 8000
    , exclusive: true
});

dgram.Socket

// Events

dgram.on('listening', function () {});
dgram.on('message', function (message, rinfo) {console.log('Received ' + msg.length + ' bytes from: ' + rinfo.address + ':' + rinfo.port);});
dgram.on('close', function () {});
dgram.on('error', function (error) {});

// Methods

socket.addMembership(mulitcastAdddress, multicastInterface);
socket.dropMembership(mulitcastAdddress, multicastInterface);

socket.address();

socket.bind(port, address, function () {});
socket.bind(otions, function () {});

socket.send(Buffer.from('some bytes'), offest, length, 41234, 'localhost', function (error) {socket.close();});

socket.setBroadcast(flag);
socket.setMulticastLoopback(flag);
socket.setMulticastTTL(ttl);
sockeet.setTTL(ttl);

socket.close(function () {});

socket.ref();
spcket.unref();

// Change to asynchronous socket.bind() behavior

var socket = dgram.createSocket('udp4');

socket.bind(1234);
socket.addMembership('224.0.0.114');

var socket = dgram.createSocket('udp4');

socket.bind(1234, function () {
    socket.addMembership('224.0.0.114');
});


// Transport Layer Security - TLS and Secure Socket Layer - SSL

var tls = require('tls');

// tls.Server

// Events

tlsServer.on('newSession', function (sessionId, sessionData, callback) {});
tlsServer.on('resumeSession', function (sessionId, callback) {});
tlsServer.on('secureConnection', function (tlsSocket) {});
tlsServer.on('OCSPRequest', function (certificate, issuer, callback) {});
tlsServer.on('tlsClientError', function (exception, tlsSocket) {});

var tlsSessionStore = {};
server.on('newSession', function (id, data, callback) {
    tlsSessionStore[id.toString('hex')] = data;
    callback();
});
server.on('resumeSession', function (id, callback)  {
    callback(null, tlsSessionStore[id.toString('hex')] || null);
});

// Methods

server.listen(port, hostname, callback);
var connectionsNumber = server.connections;
server.addContext(hostname, context);
server.address();
server.setTicketKeys(keys);
server.getTicketKeys();
server.close(callback);

// tls.TLSSocket

new tls.TLSSocket(socket, options);

// Events

tlsSocket.on('OCSPResponse', function (response) {});
tlsSocket.on('secureConnect', function () {});

// Methods

tlsSocket.address(); // {port: 12346, family: 'IPv4', address: '127.0.0.1'}
tlsSocket.getCipher(); // {name: 'AES256-SHA', version: 'TLSv1/SSLv3'}
tlsSocket.getEphemeralKeyInfo(); // {type: 'ECDH', name: 'prime256v1', size: 256}
tlsSocket.getPeerCertificate(detailed);
tlsSocket.getProtocol(); // SSLv3, TLSv1, TLSv1.1, TLSv1.2, unknown
tlsSocket.getSession();
tlsSocket.getTLSTicket();
tlsSocket.renegotiate(options, function () {});
tlsSocket.setMaxSendFragment(16384);

tlsSocket.localAddress; // local IP address
tlsSocket.localPort; // local port

tlsSocket.remoteAddress; // '74.125.127.100'
tlsSocket.remoteFamily; // 'IPv4' or 'IPv6'
tlsSocket.remotePort; // 443

if (tlsSocket.authorized) {}
if (tlsSocket.authorizationError) {}
if (tlsSocket.encrypted) {}

// connect

tls.connect(options, callback);
tls.connect(port, host, options, function () {});

var tls = require('tls');
var fs = require('fs');

var options = {
    // Necessary only if using the client certificate authentication
    key: fs.readFileSync('client-key.pem')
  , cert: fs.readFileSync('client-cert.pem')

    // Necessary only if the server uses the self-signed certificate
  , ca: [fs.readFileSync('server-cert.pem')]
};

var socket = tls.connect(8000, options, function () {
    console.log('client connected', socket.authorized ? 'authorized' : 'unauthorized');
    process.stdin.pipe(socket);
    process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', function(data) {console.log(data);});
socket.on('end', function () {server.close();});

var tls = require('tls');
var fs = require('fs');

var options = {
    pfx: fs.readFileSync('client.pfx')
};

var socket = tls.connect(8000, options, function () {
    console.log('client connected', socket.authorized ? 'authorized' : 'unauthorized');
    process.stdin.pipe(socket);
    process.stdin.resume();
});
socket.setEncoding('utf8');
socket.on('data', function (data) {console.log(data);});
socket.on('end', function () {server.close();});

// createServer

tls.createSecureContext(options);
tls.createServer(options, secureConnectionListener);

var tls = require('tls');
var fs = require('fs');

var 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 the client uses the self-signed certificate.
    , ca: [ fs.readFileSync('client-cert.pem') ]
};

var server = tls.createServer(options, function (socket) {
    console.log('server connected', socket.authorized ? 'authorized' : 'unauthorized');
    socket.write('welcome!\n');
    socket.setEncoding('utf8');
    socket.pipe(socket);
});
server.listen(8000, function () {console.log('server bound');});

var tls = require('tls');
var fs = require('fs');

var options = {
      pfx: fs.readFileSync('server.pfx')

      // This is necessary only if using the client certificate authentication.
    , requestCert: true
};

var server = tls.createServer(options, function (socket) {
    console.log('server connected', socket.authorized ? 'authorized' : 'unauthorized');
    socket.write('welcome!\n');
    socket.setEncoding('utf8');
    socket.pipe(socket);
});
server.listen(8000, function () {console.log('server bound');});

openssl s_client -connect 127.0.0.1:8000

// getCiphers

tls.getCiphers();
console.log(tls.getCiphers()); // ['AES128-SHA', 'AES256-SHA', ...]

// Deprecated APIs

// CryptoStream
cryptoStream.bytesWritten

// SecurePair

// Events
securePair.on('secure', function () {});

// Methods

tls.createSecurePair(context, isServer, requestCert, rejectUnauthorized, options);

pair = tls.createSecurePair(...);
pair.encrypted.pipe(socket);
socket.pipe(pair.encrypted);

secure_socket = tls.TLSSocket(socket, options);

// Crypto

// Determining if crypto support is unavailable

var crypto;

try {
    crypto = require('crypto');
} catch (error) {
    console.log('crypto support is disabled!');
}

if (crypto) {
    var secret = 'abcdefg';
    var hash = crypto.createHmac('sha256', secret)
                               .update('I love cupcakes')
                               .digest('hex');
    console.log(hash); // Prints: c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
}

// Certificate

var crypto = require('crypto');
var cert1 = new crypto.Certificate();
var cert2 = crypto.Certificate();

var cert = require('crypto').Certificate();
var spkac = getSpkacSomehow();
var challenge = cert.exportChallenge(spkac);
console.log(challenge.toString('utf8')); // Prints the challenge as a UTF8 string

var cert = require('crypto').Certificate();
var spkac = getSpkacSomehow();
var publicKey = cert.exportPublicKey(spkac);
console.log(publicKey); // Prints the public key as <Buffer ...>

var cert = require('crypto').Certificate();
var spkac = getSpkacSomehow();
console.log(cert.verifySpkac(Buffer.from(spkac))); // Prints true or false

// Cipher

var crypto = require('crypto');
var cipher = crypto.createCipher('aes192', 'a password');

var encrypted = '';
cipher.on('readable', function () {
    var data = cipher.read();
    if (data) {
        encrypted += data.toString('hex');
    }
});
cipher.on('end', function () {
    console.log(encrypted); // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
});

cipher.write('some clear text data');
cipher.end();


var crypto = require('crypto');
var fs = require('fs');
var cipher = crypto.createCipher('aes192', 'a password');

var input = fs.createReadStream('test.js');
var output = fs.createWriteStream('test.enc');

input.pipe(cipher).pipe(output);


var crypto = require('crypto');
var cipher = crypto.createCipher('aes192', 'a password');

var encrypted = cipher.update('some clear text data', 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted); // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504

cipher.final('base64');
cipher.setAAD(buffer);
cipher.getAuthTag();
cipher.setAutoPadding(false);
cipher.update('some text', 'ascii', 'utf8');

// Decipher

var crypto = require('crypto');
var decipher = crypto.createDecipher('aes192', 'a password');

var decrypted = '';
decipher.on('readable', function () {
    var data = decipher.read();
    if (data) {decrypted += data.toString('utf8');}
});
decipher.on('end', function () {
    console.log(decrypted); // Prints: some clear text data
});

var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
decipher.write(encrypted, 'hex');
decipher.end();


var crypto = require('crypto');
var fs = require('fs');
var decipher = crypto.createDecipher('aes192', 'a password');

var input = fs.createReadStream('test.enc');
var output = fs.createWriteStream('test.js');

input.pipe(decipher).pipe(output);


var crypto = require('crypto');
var decipher = crypto.createDecipher('aes192', 'a password');

var encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(decrypted); // Prints: some clear text data

decipher.final('base64');
decipher.setAAD(buffer);
decipher.setAuthTag(buffer);
decipher.setAutoPadding(false);
decipher.update('some text', 'base64', 'utf8');

// DiffieHellman

var crypto = require('crypto');
var assert = require('assert');

// Generate Alice's keys...
var alice = crypto.createDiffieHellman(2048);
var alice_key = alice.generateKeys();

// Generate Bob's keys...
var bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
var bob_key = bob.generateKeys();

// Exchange and generate the secret...
var alice_secret = alice.computeSecret(bob_key);
var bob_secret = bob.computeSecret(alice_key);

assert.equal(alice_secret.toString('hex'), bob_secret.toString('hex')); // OK

diffieHellman.computeSecret(other_public_key, 'hex', 'base64');
diffieHellman.generateKeys('base64');
diffieHellman.getGenerator('base64');
diffieHellman.getPrime('base64');
diffieHellman.getPrivateKey('base64');
diffieHellman.getPublicKey('base64');
diffieHellman.setPrivateKey('some key', 'base64');
diffieHellman.setPublicKey('some key', 'base64');
diffieHellman.verifyError = DH_CHECK_P_NOT_SAFE_PRIME';

// ECDH

var crypto = require('crypto');
var assert = require('assert');

// Generate Alice's keys...
var alice = crypto.createECDH('secp521r1');
var alice_key = alice.generateKeys();

// Generate Bob's keys...
var bob = crypto.createECDH('secp521r1');
var bob_key = bob.generateKeys();

// Exchange and generate the secret...
var alice_secret = alice.computeSecret(bob_key);
var bob_secret = bob.computeSecret(alice_key);

assert(alice_secret, bob_secret); // OK

ecdh.computeSecret(other_public_key, 'hex', 'base64');
ecdh.generateKeys('base64', 'compressed');
ecdh.getPrivateKey('base64');
ecdh.getPublicKey('base64', 'compressed');
ecdh.setPrivateKey('some key', 'base64');

ecdh.setPublicKey('some key', 'base64'); // deprecated

var crypto = require('crypto');
var alice = crypto.createECDH('secp256k1');
var 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()
);

// Bob uses a newly generated cryptographically strong
// pseudorandom key pair bob.generateKeys();

var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');

// alice_secret and bob_secret should be the same shared secret value
console.log(alice_secret === bob_secret);

// Hash

var crypto = require('crypto');
var hash = crypto.createHash('sha256');

hash.on('readable', function () {
    var data = hash.read();
    if (data) {
        console.log(data.toString('hex')); // Prints:  6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
    }
});

hash.write('some data to hash');
hash.end();

var crypto = require('crypto');
var fs = require('fs');
var hash = crypto.createHash('sha256');

var input = fs.createReadStream('test.js');
input.pipe(hash).pipe(process.stdout);


var crypto = require('crypto');
var hash = crypto.createHash('sha256');

hash.update('some data to hash');
console.log(hash.digest('hex')); // Prints: 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50

hash.digest('base64');
hash.update('some text', 'utf8');

// Hmac

var crypto = require('crypto');
var hmac = crypto.createHmac('sha256', 'a secret');

hmac.on('readable', function () {
    var data = hmac.read();
    if (data) {
        console.log(data.toString('hex')); // Prints: 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
    }
});

hmac.write('some data to hash');
hmac.end();


var crypto = require('crypto');
var fs = require('fs');
var hmac = crypto.createHmac('sha256', 'a secret');

var input = fs.createReadStream('test.js');
input.pipe(hmac).pipe(process.stdout);


var crypto = require('crypto');
var hmac = crypto.createHmac('sha256', 'a secret');

hmac.update('some data to hash');
console.log(hmac.digest('hex')); // Prints: 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e

hmac.digest('base64');
hmac.update('some text', 'utf8');

// Sign

var crypto = require('crypto');
var sign = crypto.createSign('RSA-SHA256');

sign.write('some data to sign');
sign.end();

var private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex')); // Prints the calculated signature


var crypto = require('crypto');
var sign = crypto.createSign('RSA-SHA256');

sign.update('some data to sign');

var private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex')); // Prints the calculated signature


var crypto = require('crypto');
var sign = crypto.createSign('sha256');

sign.update('some data to sign');

var private_key = '-----BEGIN EC PRIVATE KEY-----\n' +
        'MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n' +
        'AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n' +
        'pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n' +
        '-----END EC PRIVATE KEY-----\n';

console.log(sign.sign(private_key).toString('hex'));


sign.sign('some key', 'base64');
sign.update('some text', 'utf8');

// Verify

var crypto = require('crypto');
var verify = crypto.createVerify('RSA-SHA256');

verify.write('some data to sign');
verify.end();

var public_key = getPublicKeySomehow();
var signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature)); // Prints true or false


var crypto = require('crypto');
var verify = crypto.createVerify('RSA-SHA256');

verify.update('some data to sign');

var public_key = getPublicKeySomehow();
var signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature)); // Prints true or false


verifier.update('some text', 'utf8');
verifier.verify({object}, 'hex', signature_format);


// Crypto module methods and properties

let constants = crypto.constants;
crypto.constants.ENGINE_METHOD_RSA
crypto.constants.ENGINE_METHOD_DSA
crypto.constants.ENGINE_METHOD_DH
crypto.constants.ENGINE_METHOD_RAND
crypto.constants.ENGINE_METHOD_ECDH
crypto.constants.ENGINE_METHOD_ECDSA
crypto.constants.ENGINE_METHOD_CIPHERS
crypto.constants.ENGINE_METHOD_DIGESTS
crypto.constants.ENGINE_METHOD_STORE
crypto.constants.ENGINE_METHOD_PKEY_METHS
crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS
crypto.constants.ENGINE_METHOD_ALL
crypto.constants.ENGINE_METHOD_NONE

let encoding = crypto.DEFAULT_ENCODING;

if (crypto.fips) {}

crypto.createCipher(algorithm, password);
crypto.createCipheriv(algorithm, key, iv);
crypto.createCredentials(details); // deprecated
crypto.createDecipher(algorithm, password);
crypto.createDecipheriv(algorithm, key, iv);
crypto.createDiffieHellman(prime, prime_encoding, generator, generator_encoding);
crypto.createDiffieHellman(prime_length, generator);
crypto.createECDH(curve_name);


crypto.createHash(algorithm);

onst filename = process.argv[2];
var crypto = require('crypto');
var fs = require('fs');

var hash = crypto.createHash('sha256');

var input = fs.createReadStream(filename);
input.on('readable', function () {
    var data = input.read();
    if (data) {
        hash.update(data);
    } else {
        console.log(`${hash.digest('hex')} ${filename}`);
    }
});


crypto.createHmac(algorithm, key);

var filename = process.argv[2];
var crypto = require('crypto');
var fs = require('fs');

var hmac = crypto.createHmac('sha256', 'a secret');

var input = fs.createReadStream(filename);
input.on('readable', function () {
  var data = input.read();
  if (data)
    hmac.update(data);
  else {
    console.log(`${hmac.digest('hex')} ${filename}`);
  }
});


crypto.createSign(algorithm);
crypto.createVerify(algorithm);


crypto.getCiphers();

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


crypto.getCurves();

var curves = crypto.getCurves();
console.log(curves); // ['secp256k1', 'secp384r1', ...]


crypto.getDiffieHellman(group_name);

var crypto = require('crypto');
var alice = crypto.getDiffieHellman('modp14');
var bob = crypto.getDiffieHellman('modp14');

alice.generateKeys();
bob.generateKeys();

var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');

/* alice_secret and bob_secret should be the same */
console.log(alice_secret == bob_secret);


crypto.getHashes();

var hashes = crypto.getHashes();
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]


crypto.pbkdf2(password, salt, iterations, keylen, digest, callback);

var crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, key) => {
  if (err) throw err;
  console.log(key.toString('hex'));  // 'c5e478d...1469e50'
});


crypto.pbkdf2Sync(password, salt, iterations, keylen, digest);

var crypto = require('crypto');
var key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
console.log(key.toString('hex'));  // 'c5e478d...1469e50'


crypto.privateDecrypt(private_key, buffer);
crypto.timingSafeEqual(a, b);
crypto.privateEncrypt(private_key, buffer);
crypto.publicDecrypt(public_key, buffer);
crypto.publicEncrypt(public_key, buffer);


crypto.randomBytes(size, callback);

// Asynchronous
var crypto = require('crypto');
crypto.randomBytes(256, (err, buf) => {
  if (err) throw err;
  console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`);
});

// Synchronous
var buf = crypto.randomBytes(256);
console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`);

crypto.setEngine(engine, flags);

// Zlib

var zlib = require('zlib');

var gzip = zlib.createGzip();
var fs = require('fs');
var inp = fs.createReadStream('input.txt');
var out = fs.createWriteStream('input.txt.gz');

inp.pipe(gzip).pipe(out);

var input = '.................................';
zlib.deflate(input, (err, buffer) => {
  if (!err) {
    console.log(buffer.toString('base64'));
  } else {
    // handle error
  }
});

var buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');
zlib.unzip(buffer, (err, buffer) => {
  if (!err) {
    console.log(buffer.toString());
  } else {
    // handle error
  }
});

// Compressing HTTP requests and responses

// client request example
var zlib = require('zlib');
var http = require('http');
var fs = require('fs');
var request = http.get({
      host: 'example.com'
    , port: 80
    , path: '/'
    , headers: {'Accept-Encoding': 'gzip,deflate'}
});
request.on('response', function (response) {
  var output = fs.createWriteStream('example.com_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;
  }
});

// server example
// Running a gzip operation on every request is quite expensive.
// It would be much more efficient to cache the compressed buffer.
var zlib = require('zlib');
var http = require('http');
var fs = require('fs');
http.createServer(function (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);


// This is a truncated version of the buffer from the above examples
var buffer = Buffer.from('eJzT0yMA', 'base64');

zlib.unzip(buffer, {finishFlush: zlib.Z_SYNC_FLUSH}, function (err, buffer) {
  if (!err) {
    console.log(buffer.toString());
  } else {
    // handle error
  }
});

// Memory Usage Tuning

(1 << (windowBits+2)) +  (1 << (memLevel+9))

{windowBits: 14, memLevel: 7}

1 << windowBits

// Flushing

var zlib = require('zlib');
var http = require('http');

http.createServer((request, response) => {
  // For the sake of simplicity, the Accept-Encoding checks are omitted.
  response.writeHead(200, { 'content-encoding': 'gzip' });
  var output = zlib.createGzip();
  output.pipe(response);
  setInterval(function () {
    output.write(`The current time is ${Date()}\n`, function () {
      // The data has been passed to zlib, but the compression algorithm may
      // have decided to buffer the data for more efficient compression.
      // Calling .flush() will make the data available as soon as the client
      // is ready to receive it.
      output.flush();
    });
  }, 1000);
}).listen(1337);

Constants

zlib.Z_NO_FLUSH
zlib.Z_PARTIAL_FLUSH
zlib.Z_SYNC_FLUSH
zlib.Z_FULL_FLUSH
zlib.Z_FINISH
zlib.Z_BLOCK
zlib.Z_TREES

zlib.Z_OK
zlib.Z_STREAM_END
zlib.Z_NEED_DICT
zlib.Z_ERRNO
zlib.Z_STREAM_ERROR
zlib.Z_DATA_ERROR
zlib.Z_MEM_ERROR
zlib.Z_BUF_ERROR
zlib.Z_VERSION_ERROR


// Class Options:
- flush (default: zlib.Z_NO_FLUSH)
- finishFlush (default: zlib.Z_FINISH)
- chunkSize (default: 16*1024)
- windowBits
- level (compression only)
- memLevel (compression only)
- strategy (compression only)
- dictionary (deflate/inflate only, empty dictionary by default)

Class: zlib.Deflate
Class: zlib.DeflateRaw
Class: zlib.Gunzip
Class: zlib.Gzip
Class: zlib.Inflate
Class: zlib.InflateRaw
Class: zlib.Unzip
Class: zlib.Zlib

zlib.flush(kind, function () {});
zlib.params(level, strategy, function () {});
zlib.reset();
zlib.constants
zlib.createDeflate(options);
zlib.createDeflateRaw(options);
zlib.createGunzip(options);
zlib.createGzip(options);
zlib.createInflate(options);
zlib.createInflateRaw(options);
zlib.createUnzip(options);

// Convenience Methods

zlib.deflate(buf, options, function () {});
zlib.deflateSync(buf, options);
zlib.deflateRaw(buf, options, function () {});
zlib.deflateRawSync(buf, options);
zlib.gunzip(buf, options, function () {});
zlib.gunzipSync(buf, options);
zlib.gzip(buf, options, function () {});
zlib.gzipSync(buf, options);
zlib.inflate(buf, options, function () {});
zlib.inflateSync(buf, options);
zlib.inflateRaw(buf, options, function () {});
zlib.inflateRawSync(buf, options);
zlib.unzip(buf, options, function () {});
zlib.unzipSync(buf, options);

Комментариев нет:

Отправить комментарий