{"id":256738,"date":"2015-05-07T06:43:02","date_gmt":"2015-05-07T02:43:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=256738"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=256738","title":{"rendered":"\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u043d\u0430 Node.js \u2014 \u0427\u0430\u0441\u0442\u044c 2"},"content":{"rendered":"<p>     \t\u0412 <a href=\"http:\/\/habrahabr.ru\/post\/257365\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a> \u0431\u044b\u043b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043a \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c\u0443. \u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u043a \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e\u043c\u0443. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430, \u044f \u0447\u0443\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0432\u043e\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<h2>\u0427\u0430\u0441\u0442\u044c 2: \u041a\u0430\u043a \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>  \u0421\u0430\u043c\u044b\u0439 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u0444\u0430\u0439\u043b \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0435\u0451 \u043f\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f\u043c \u0441\u0442\u0440\u043e\u043a:<\/p>\n<pre><code class=\"javascript\">fs.readFile('large.txt', { encoding : 'utf8' },   (err, data) =&gt; {     if (err) throw err;     data.split('\\n').forEach(line =&gt; {       doSomethingWithLine(line);     });   }); <\/code><\/pre>\n<p>  \u041e\u043d \u0436\u0435, \u043f\u043e\u0436\u0430\u043b\u0443\u0439, \u0441\u0430\u043c\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u044b\u0439. \u041d\u043e \u043e\u043d \u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u043e\u0442 100% \u0434\u043e 200% \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0444\u0430\u0439\u043b\u0430. 200% \u2014 \u044d\u0442\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 \u0441\u0430\u043c\u044b\u0439 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0430 \u0443 \u0441\u0442\u0440\u043e\u043a\u0438 UTF-16 \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u043c\u0435\u0440 \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u0435\u0441\u043b\u0438 \u0444\u0430\u0439\u043b \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0438\u0437 \u043e\u0434\u043d\u043e\u0431\u0430\u0439\u0442\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 UTF-8.<\/p>\n<p>  \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Node.js \u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 Node.js \u043f\u0440\u043e\u0446\u0435\u0441\u0441 (\u0441\u043c. <a href=\"https:\/\/github.com\/joyent\/node\/wiki\/FAQ#what-is-the-memory-limit-on-a-node-process\">What is the memory limit on a node process?)<\/a>. \u0421\u0434\u0435\u043b\u0430\u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e \u2014 \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0445\u0432\u0430\u0442\u0430\u0435\u0442, \u0442\u043e \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0444\u0430\u0439\u043b \u0431\u043e\u043b\u044c\u0448\u0435 1Gb \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"javascript\">this.parent = new SlowBuffer(this.length);               ^ RangeError: length &gt; kMaxLength <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0436\u0435 \u0444\u0430\u0439\u043b \u043f\u043e\u043c\u0435\u043d\u044c\u0448\u0435, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0442\u0430\u043a\u043e\u0435:<\/p>\n<pre><code class=\"javascript\">FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory <\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0435\u0433\u043e \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0438 Node.js \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u0438\u043d\u0438\u043c\u0443\u043c 5 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432:<\/p>\n<ol>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0441\u0442\u0430\u0440\u044b\u0435\u00bb \u043f\u043e\u0442\u043e\u043a\u0438 \u2014 \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u00abdata\u00bb.<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u043d\u043e\u0432\u044b\u0435\u00bb \u043f\u043e\u0442\u043e\u043a\u0438 \u2014 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u00abreadable\u00bb \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 read().<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u0439 WritableStream \u0438 \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u00abpipe()\u00bb.<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0435 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u044b \u0438 \u043d\u0430\u0431\u043e\u0440 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 open(), read(), close().<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u2014 openSync(), readSync(), closeSync().<\/li>\n<\/ol>\n<p>  \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b 1-3 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u044b\u043c\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043f\u0435\u0440\u0438\u0440\u0443\u044e\u0442 \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0435\u0439 \u2014 \u043f\u043e\u0442\u043e\u043a\u043e\u043c. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043a\u0430\u043a \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 (data flow diagram) \u0438 \u043f\u0440\u0438 \u0434\u0438\u0437\u0430\u0439\u043d\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u043c\u0438 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043c\u0438 \u043a\u0430\u043a \u0441\u043b\u0438\u044f\u043d\u0438\u0435, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b 1 \u0438 2 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0447\u0442\u0435\u043d\u0438\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430. \u0412 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445 3 \u0438 4 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0431\u0443\u0444\u0435\u0440 \u0438 \u0437\u0430\u0442\u0435\u043c \u0438\u0445 \u043d\u0430\u0434\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0442\u0435\u043a\u0441\u0442. <\/p>\n<pre><code class=\"javascript\">\/\/ \u0412\u0430\u0440\u0438\u0430\u043d\u0442 #1 - &quot;\u0441\u0442\u0430\u0440\u044b\u0435&quot; \u043f\u043e\u0442\u043e\u043a\u0438 var stream = fs.createReadStream(file, { encoding : 'utf8' }); stream.on(&quot;data&quot;, (_, data) =&gt; processData(data)); stream.on(&quot;end&quot;, done);  \/\/ \u0412\u0430\u0440\u0438\u0430\u043d\u0442 #2 - &quot;\u043d\u043e\u0432\u044b\u0435&quot; \u043f\u043e\u0442\u043e\u043a\u0438 var stream = fs.createReadStream(file, { encoding : 'utf8' }); stream.on(&quot;readable&quot;, () =&gt; processData(stream.read())); stream.on(&quot;end&quot;, done);  \/\/ \u0412\u0430\u0440\u0438\u0430\u043d\u0442 #3 - pipe var stream = fs.createReadStream(file, { encoding : 'utf8' }); var writeStream = new Writable(); writeStream._write = (chunk, encoding, callback) =&gt; {     processData(chunk);     callback(); }; writeStream.on('end', done); stream.pipe(writeStream);  \/\/ \u0412\u0430\u0440\u0438\u0430\u043d\u0442 #4 - \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b fs fs.open(file, 'r', (err, fd) =&gt; {     var buffer = new Buffer(1000*1000);     (function next() {         fs.read(fd, buffer, 0, buffer.length, null,           (err, bytesRead, buffer) =&gt; {               if (bytesRead === 0) {                   fs.close(fd, done);               } else {                   processData(buffer);                   next();               }           });     }()); }); <\/code><\/pre>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b 1-2 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0435\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0430:<\/p>\n<pre><code class=\"javascript\">function processData(chunk) {   console.log('first')   setImmediate(() =&gt; {     console.log('second');     setImmediate(() =&gt; console.log('third'));   }); }  var stream = fs.createReadStream(file, { encoding : 'utf8' }); stream.on(&quot;readable&quot;, () =&gt; processData(stream.read()));  ... first third second third first second ... <\/code><\/pre>\n<p>  \u0421\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434\u044b pause()\/resume().<\/p>\n<pre><code class=\"javascript\">function processData(chunk, done) {   console.log('first')   setImmediate(() =&gt; {     console.log('second');     setImmediate(() =&gt; {       console.log('third');       done();     });   }); }  var stream = fs.createReadStream(file, { encoding : 'utf8' }); stream.on(&quot;readable&quot;, () =&gt; {   stream.pause();   processData(stream.read(), () =&gt; stream.resume()); });  ... first second third first second third ... <\/code><\/pre>\n<p>  \u0412 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445 3 \u0438 4 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f (\u0432\u0430\u0440\u0438\u0430\u043d\u0442 3) \u0438\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (\u0432\u0430\u0440\u0438\u0430\u043d\u0442 4).<\/p>\n<p>  \u0414\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 createTextReader() \u0438\u0437 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438. \u0418\u0437 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044b\u0439, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e\u0442\u043e\u043a \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0443 \u043d\u0435\u0433\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443 (request-callback).<\/p>\n<pre><code class=\"javascript\">function createTextReader(file, options, done) {   var length, encoding, separator;    if ('function' === typeof options) {     done = options;     options = { };   }    length = 4 * 1024;   encoding = options.encoding || 'utf8';   separator = (options.separator || '\\n');    fs.open(file, 'r', (err, fd) =&gt; {     var eof, tail, buffer, decoder, lines;      if (err) {       done(err);       return;     }      eof = false;     buffer = new Buffer(length);     tail = '';     lines = [];      decoder = new StringDecoder(encoding);      done(null, {       readLine : done =&gt; {         var line;         if (lines.length &gt; 0) {           line = lines.shift();           done(null, line);         } else if (eof) {           done(null, null);         } else {           (function read() {             fs.read(fd, buffer, 0, length, null,               function (err, bytesRead, buffer) {                 var index, position;                  if (bytesRead === 0) {                   eof = true;                   done(null, tail);                 } else {                   tail = tail + decoder.write(buffer.slice(0, bytesRead));                   index = -1;                   while (-1 !== (position = tail.indexOf(separator, index))) {                     lines.push(tail.substring(index, position));                     index = position + separator.length;                   }                   tail = tail.substring(index);                   if (lines.length === 0) {                     read();                   } else {                     line = lines.shift();                     done(null, line);                   }                 }               });           }());         }       },       close : done =&gt; {         fs.close(fd, () =&gt; {           if (done) {             done(err || null);           }         });       }     });   }); } <\/code><\/pre>\n<h2>\u041f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>  \u0412 \u0434\u0432\u0443\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u0438\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e \u043c\u043d\u0435 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/github.com\/AlexAtNet\/async-read-lines\">https:\/\/github.com\/AlexAtNet\/async-read-lines<\/a>. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043c\u043d\u043e\u0433\u043e\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430 \u0440\u0430\u043c\u043a\u0430\u043c\u0438, \u043d\u0435 \u043d\u0430 \u0432\u0441\u0435 \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0422\u0430\u043a \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0430\u0448\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0443 \u0438\u043b\u0438 \u043e\u043f\u0435\u0447\u0430\u0442\u043a\u0443 \u2014 \u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043f\u043e \u0442\u0435\u043c\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445. \u0415\u0441\u043b\u0438 \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0431\u0430\u0433\u0438 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 \u2014 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 <a href=\"https:\/\/github.com\/AlexAtNet\/async-read-lines\/issues\">github issues<\/a>. \u0421\u0432\u044f\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u043e \u043c\u043d\u043e\u0439 \u043b\u0438\u0447\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u0430\u0439\u0442 <a href=\"http:\/\/alexatnet.com\/\">alexatnet.com<\/a>.<\/p>\n<p>  \u0423\u0441\u043f\u0435\u0445\u043e\u0432 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438!<\/p>\n<hr\/>\n<p>  <em>\u041e\u0431 \u0430\u0432\u0442\u043e\u0440\u0435: \u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440 \u041d\u0435\u0442\u043a\u0430\u0447\u0435\u0432 \u2014 \u0441\u0442\u0430\u0440\u0448\u0438\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043d\u0430 \u0421# \u0438 F#. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u0430\u0439\u0442 <a href=\"http:\/\/alexatnet.com\">alexatnet.com<\/a>, \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442 \u0432\u0435\u0431\u0438\u043d\u0430\u0440\u044b (Code&#038;Coffe), \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0441 \u043a\u043e\u0434\u043e\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c (CodeReview4U).<\/em>     \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/257441\/\"> http:\/\/habrahabr.ru\/post\/257441\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u0412 <a href=\"http:\/\/habrahabr.ru\/post\/257365\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a> \u0431\u044b\u043b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043a \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c\u0443. \u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u043a \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e\u043c\u0443. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430, \u044f \u0447\u0443\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0432\u043e\u043f\u0440\u043e\u0441\u0430.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-256738","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/256738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=256738"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/256738\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=256738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=256738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=256738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}