Node.js ç¨ã®éåæå¦çãç°¡åã«ãã¦ãããã©ã¤ãã©ãª async.js
Node.js ã¨ããã°éåæå¦çã§ãï¼ããã¦éåæå¦çã¨è¨ãã°ã³ã¼ã«ããã¯ï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼ããã«ãããªã³ã¼ãã«ãªã£ããããã¡ã§ãããï¼
fs.writeFile(path, "hello", function (error) { fs.open(path, "a", 0666, function (error, file) { fs.write(file, "\nworld", null, "utf-8", function () { fs.close(file, function (error) { fs.readFile(path, "utf-8", function (error, data) { var lines = data.split("\n"); sys.puts(lines[1]); }); }); }); }); });
奥ã«åãã£ã¦çªãé²ãã®ãæ°æã¡ããï¼ãããªé¢¨ã«æã£ã¦ããææãåã«ãããã¾ããï¼ï¼ï¼
ã§ãï¼ãã®ãã¡ãããªåè«ãè¨ããããªãã¾ãããï¼
The last few lines of our #nodejs application - });});});});});});});});});}); our #clojure app looks like this - )))))))))))))))))))) #joke
ãããªå°ããªãµã³ãã«ã§ããã ã¨ï¼ç¾å®çãªã¢ããªã§ã¯ã©ããªããã¨ããï¼ã¨å¿é
ã§å¤ãç ããªãã®ã§èª¿ã¹ã¦ã¿ããï¼ãããããªã©ã¤ãã©ãªãå½ç¶ã®ããã«ãã£ããããããã§ããï¼
JSDeferred
ããã¯å
ã
ãã©ã¦ã¶ã®ä¸ã§åãæ®éã® JavaScript åãã«ä½ãããããã§ããï¼Node.js ã§ã使ãã¾ãï¼
Deferred ã¯å
é±æ¸ãããã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ Vows ã«åºã¦ãã Promise ã¨ä¼¼ã¦ãã¨ãããï¼ãã®å¿ç¨ã¨ããæãã§ãï¼
Promise/Future ã¯å°æ¥ç¢ºå®ããå¤ãåå¾ã§ããããã«ããããã®ãªãã¸ã§ã¯ãã§ããï¼Deferred ã¯ããã«ï¼å¤ã確å®ããæã«å®è¡ããå¦çãç»é²ãã¦ãããã¨ãã§ãã¾ãï¼å¦çã®å®è¡ããã®æã¾ã§å
éããããã Deferred ãªãã§ããï¼
ãã§ï¼JSDeferred ã使ãã¨å
ã®ãµã³ãã«ã¯æ¬¡ã®ããã«æ¸ããã¨ãã§ãã¾ãï¼
var fs = require('fs'), sys = require('sys'); var Deferred = require('../lib/jsdeferred/jsdeferred').Deferred; var path = "fileio.txt"; Deferred.define(); next(function() { var deferred = new Deferred(); fs.writeFile(path, "hello", function(error) { deferred.call(); }); return deferred; }).next(function() { var deferred = new Deferred(); fs.open(path, "a", 0666, function(error, file) { deferred.call(file); }); return deferred; }).next(function(file) { var deferred = new Deferred(); fs.write(file, "\nworld", null, "utf-8", function() { deferred.call(file); }); return deferred; }).next(function(file) { var deferred = new Deferred(); fs.close(file, function(error) { deferred.call(); }); return deferred; }).next(function() { fs.readFile(path, "utf-8", function(error, data) { var lines = data.split("\n"); sys.puts(lines[1]); }); });
ãã¼ã¿ã
ï¼ï¼ï¼ 奥ã«åãã£ã¦çªãé²ãã³ã¼ããæ®éã«ç¸¦ã«è½ã¡ã¦ããã³ã¼ãã«ã¯ãªãã¾ããï¼ãããï¼å°ãæ®å¿µãªæããããªãã§ããªãï¼ï¼ï¼
ãã㯠JSDeferred ãæªãããã§ã¯ãªãï¼ãã«ãã£ã³ã°ãããã¯ã§ãã Deferred
ãç´æ¥ä½¿ã£ã¦ããããããªã£ã¡ãã£ã¦ããã§ããï¼ãã®æ±ºã¾ãåã£ãå¦çãã©ããããé¢æ°ãç¨æããã°ï¼ã¹ãããªããã³ã¼ãã«ãªãã¯ãã§ãï¼ãããï¼ç¾æç¹ã§ã¯ãããªããããããã®ã¯ãªãããï¼ï¼ï¼
ãªããªãä½ãã°ãããããªãï¼ã¨çå¦æ§ã«è¨ãããããªæ°ããã¾ããï¼JSDeferred ã®ä½è
ããã Node.js ã使ã£ã¦ãããããªã®ã§ï¼å¾
ã£ã¦ãã°ãããåºã¦ããããããªãã®ããªãã¨æå¾
ãã¦ããã¾ãï¼
promised-io
ãã¡ã㯠promised-based ãªãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ patr ã¨åãä½è
ã«ãã promised-based 㪠IO ãã¬ã¼ã ã¯ã¼ã¯ï¼
Node.js ã® fs
ã¢ã¸ã¥ã¼ã«ã http.Client
ã®ã©ããã¼ãç¨æãã¦ããã¦ããããã§ãï¼
ãããï¼ï¼ï¼ ããã¥ã¡ã³ããï¼ï¼ï¼
ä¸å¿ãã¡ãã«ãæ
å ±ãããã®ã§ããï¼
ãªããªãè¾ããã®ãããã¾ãï¼
ãã®ä¸ã«åºã¦ããä¾ï¼
var myFile = fs.open("my-file.txt", "a"); myFile.write("some data").then(myFile.close);
ããã ãè¦ãã¨ï¼çµæ§ããæãã«è¦ãã¾ãï¼
ä¸è¦åæçãªã³ã¼ãã«è¦ãã¾ããï¼å®éã«ã¯ãã¡ã¤ã«ã® open()
ãå®äºãã¦ãã write()
ãï¼ãããçµãã£ã¦ãã close()
ãè¡ããã¾ãï¼ã«ãã³ããï¼
ã®ã§ããï¼ããã¡ãã£ã¨ä½ããããã¨ããã¨è¡ãè©°ã¾ã£ã¦ãã¾ãã¾ããï¼
ä¾ãã°æåã®ä¾ã promised-io ã§ãããã¨ããã¨ï¼
var sys = require("sys"); var fs = require("../lib/promised-io/fs"); var path = "fileio.txt"; fs.writeFile(path, "hello").then(function() { var myFile = fs.open(path, "a", 0666); myFile.write("\nworld").then(myFile.close); })
ãã®æç¹ã§ãã£ããããªãæ°ãããä¸ã«ï¼ãã®å ã®æ¸ãæ¹ãåããã¾ããï¼
promised-io ã¯ããã¤ãã®ã¯ã©ã¹ãæä¾ããã®ã§ããï¼ããã§åºã¦ããã®ã¯ãã®ãã¡ã® 3 ã¤ï¼Promise
ï¼Deferred
ï¼ãã㦠File
ã§ãï¼ãããã¯ï¼
Promise <|-- Deferred <|-- File
ã¨ããç¶æ¿é¢ä¿ã«ããã¾ã (ãã¶ãï¼ã§ãèªåã® JavaScript åã§ã¯æªããã®ã )ï¼
ã§ï¼åé¡ãªã®ã¯ File
ã® write()
ãè¿ãã®ã File
ã§ã¯ãªã Deferred
ã£ã½ããã¨ï¼ãªã®ã§ï¼
myFile.write(...).read(...).close().then(...);
ãªãã¦ã§ãããã«è¦ããªãã®ã§ããï¼ãã§ã¼ã³ãã§ããªãï¼
ããã«ï¼Deferred
ã® then()
ãè¿ãã®ã¯ Deferred
ã§ã¯ãªã Promise
ã§ï¼ããã¤ã® then()
ã¯
Promise.prototype.then = function(resolvedCallback, errorCallback, progressCallback){ throw new TypeError("The Promise base class is abstract, this function must be implemented by the Promise implementation"); };
ã£ã¦ãªã£ã¦ãããã¦ï¼ä½¿ãç©ã«ãªããªãã£ããï¼
è¦ããã«ï¼JSDeferred ã§ã§ãã¦ãããããªãã§ã¼ã³ãã§ããªãæ°ãããããã§ãï¼
èªåã®åéãã¨ããå¯è½æ§ãé«ãã®ã§ãã (ãªãã JavaScript ã§ã¯æä¸ç´æ¦å£«)ï¼ããã¥ã¡ã³ããã¢ã¬ãªã®ã§ï¼ãã以ä¸é¢ããã®ã¯é£ããã¨å¤æãã¦æ¬¡ã¸è¡ãã¾ãï¼
async.js
ãããªãã㧠(ã©ããªããã§ï¼)ï¼ããããä»æ¥ã®ä¸»é¡ï¼async.js ã§ãï¼åç½®ãé·ããï¼
async.js ã¯å
ã®äºã¤ã¨ã¯éåç°ãªã£ãå°è±¡ã®ã©ã¤ãã©ãªã§ï¼å
ã
ã¯é¢æ°åè¨èªã«è¦ãããç¡éãªã¹ãã map
ãªã©ã®é¢æ°ãæä¾ãããã®ã¿ããã§ãï¼
ããã¤ã使ãã¨ä¾ãã°
async.range(1, null, 2)
.print()
.end();
ããã§ã³ã³ã½ã¼ã«ã« 1, 3, 5... ã¨å¥æ°ã延ã
ã¨åºåããã¾ãï¼
æåã® range(start, stop, step)
ã¯ã¸ã§ãã¬ã¼ã¿ã¨å¼ã°ãï¼ãããå¾ç¶ã®é¢æ°ã«ãã¼ã¿ã渡ãã¾ãï¼ãã®ä¾ã§ã¯ print()
ã§ãï¼å¾ç¶ã®é¢æ°ã¯èªåã®å¦çãçµãã£ãã次ã®é¢æ°ã«å¦çã渡ãã¾ãï¼ã¤ã¾ããã¤ãã©ã¤ã³ãæ§æãã¦ããããã§ãï¼ããã延ã
ã¨ç¶ã㦠(ãã®ä¾ã§ã¯ print()
ããããã¾ããã)ï¼æå¾ã« end()
ã«ãã©ãçãã¨ï¼ã¸ã§ãã¬ã¼ã¿ã«æ»ã£ã¦æ¬¡ã®å¤ãçæããã¦ï¼ï¼ï¼ ãç¹°ãè¿ãã¾ãï¼
ãã£ã¨ï¼ãã¡ã®ç°å¢ã 㨠4000 ãè¶ããã¨ããã§ä¾å¤ãï¼ã¹ã¿ãã¯ãªã¼ãã¼ããã¼ã¿ãããªï¼
Node.js ã£ã¦ã¨ã©ã¼ããã£ãæã®ã¡ãã»ã¼ã¸ãä¸è¦ªåã§ç¶æ³ãã¾ãã§åãããªããã¨ãå¤ãæ°ããã¾ãï¼ã§ãã¾ãï¼ããã¯ãã£ã¨ã¹ã¿ãã¯ãªã¼ãã¼ããã¼ã§ãããï¼
ã§ã大ä¸å¤«ã§ãï¼
async.js ã«ã¯ delay()
ã¨ããé¢æ°ããã£ã¦ï¼ãããæãã¨å¾ç¶ã®å¦çã setTimeout()
ã«æ¸¡ãã¦ããã¾ãï¼ããã§ä¸åº¦ Node.js ã«å¶å¾¡ãæ»ãã¦ãããã°ã¹ã¿ãã¯ãªã¼ãã¼ããã¼ã«ã¯ãªããªãã¯ãï¼
async.range(1, null, 2)
.print()
.delay(0)
.end();
ããï¼å¤§ä¸å¤«ã£ã½ãï¼
ããã«
// å¶æ° async.range(0, null, 2) .print() .delay(0) .end(); // å¥æ° async.range(1, null, 2) .print() .delay(0) .end();
ãªãã¦ããã¨ï¼0, 1, 2, 3, 4, 5... ã¨ããå ·åã«ä¸¡æ¹ã®æ°åã交äºã«åºåããã¾ãï¼åæçãªãã¤ãã©ã¤ã³ã«è¦ããã³ã¼ããå®ã¯éåæã«å®è¡ããã¦ããï¼ï¼ï¼ ãªããã«ãã³ããï¼ï¼
ã§ï¼ä»ã® delay()
ã®æåãçµæ§éè¦ã§ï¼ããã£ã¦ã³ã¼ã«ããã¯ãããã¾ã§å¾ç¶ã®å¦çãå
éããã¦ãããããã§ãï¼è¦ããã« Deferred ãªãã§ããï¼ã¤ã¾ã delay()
ã¯ï¼JSDefferd ã® next()
ã promised-io ã® then()
ã¿ãããªãã¨ããã£ã¦ããã¦ããã¨ãããã¨ï¼
ã£ã¦ãã¨ã¯ï¼ãã®ãã¤ãã©ã¤ã³ã« I/O ãå«ãããã¨ã ã£ã¦ã§ããã¯ãï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼async.js 㯠Node.js æ¨æºã® fs
ã¢ã¸ã¥ã¼ã«ã©ã¤ã¯ãªãã©ã°ã¤ã³ãç¨æãã¦ããã¦ã¾ãï¼
ãã ï¼ã¡ãã£ã¨ã ãæ®å¿µãªãã¨ã«ï¼open()
ã®å¼æ°ã®ä¸¦ã³ã Node.js ã®ã¨ã¯éã£ã¦ãããï¼readFile()/writeFile()
ã¯ããã®ã« read()/write()
ã¯ãªãã£ããï¼
ããã¦åãæ®å¿µãªãã¨ã«ï¼ãããã¯ã¼ã¯é¢ä¿ã®ãã©ã°ã¤ã³ã¯ãªãã£ããï¼ãã¼ã¿ã
ï¼ï¼ï¼
ã¨ããããæ°ãåãç´ãã¦ï¼async.js ã® fs-node.js
ã¨ãããã©ã°ã¤ã³ãã¡ãã£ã¨ä¿®æ£ãã¦ã¿ã¾ããï¼
@@ -53,7 +53,7 @@ async.plugin({ }) }, - open: function(flags, mode) { + open: function(mode, flags) { return this.$fileOp(function(file, next) { fs.open(file.path, mode || file.mode || "r", flags || file.flags || 0666, next) }, "fd") @@ -124,17 +124,12 @@ async.plugin({ if (!file.path) return next("not a file sequence!") - if (encoding) - fs.readFile(file.path, encoding, readCallback) - else - fs.readFile(file.path, readCallback) - - function readCallback(err, data) { + fs.readFile(file.path, encoding, function readCallback(err, data) { if (err) return next(err) file.data = data next() - } + }) }) }, @@ -142,6 +137,20 @@ async.plugin({ return this.$fileOp(function(file, next) { fs.writeFile(file.path, data || file.data, next) }) + }, + + write: function(buffer, offset, length, position) { + return this.$fileOp(function(file, next) { + if (!Buffer.isBuffer(buffer)) { + buffer = new Buffer(buffer, length); + length = buffer.length; + position = offset; + offset = 0; + } + fs.write(file.fd, buffer, offset, length, position, function(err, result){ + next(null, file); + }); + }); } }, { files: function(files, root) { @@ -154,6 +163,10 @@ async.plugin({ } })) }, + + file: function(file, root) { + return this.files([file], root); + }, glob: function(pattern) { function fileSort(file1, file2) {
ããã使ãã¨ï¼æåã®ä¾ã¯ãããªãã¾ãï¼ã¸ã£ã¸ã£ã¼ã³ï¼
var sys = require('sys'), fs = require('fs'); var async = require('../lib/async'); var path = 'fileio.txt'; async.file(path) .writeFile('hello') .open('a') .write('\nworld') .close() .readFile('utf-8') .get('data') .each(function(data) { var lines = data.split("\n"); sys.puts(lines[1]); }) .end();
ãããããï¼ï¼ï¼
ããã§ãï¼ã¾ãã«ï¼ãããæãã§ãããã®ãã®ãã®ã§ããï¼ï¼
async.jsâ³ï¼ï¼ï¼ï¼
ãã®åä½ãç°¡åã«èª¬æããã¨ï¼
æåã® file()
é¢æ°ã¯ã¸ã§ãã¬ã¼ã¿ã§ï¼ãã¡ã¤ã«ã®ãã¹ãä½ã£ã¦å¾ç¶ã«æ¸¡ãã¾ãï¼
次㮠writeFile()
ã¯ãã®ãã¡ã¤ã«ã«æååãæ¸ãè¾¼ã¿ã¾ãï¼
ãã®æ¬¡ã® open()
ã¯ãã¡ã¤ã«ããªã¼ãã³ãï¼ãã¡ã¤ã«è¨è¿°åãå¾ç¶ã¸ã®ãã¼ã¿ã«è¿½å ãã¾ãï¼
write()
ã¯ãã®ãã¡ã¤ã«è¨è¿°åã使ã£ã¦æååãæ¸ãè¾¼ã¿ã¾ãï¼
close()
ã¯ãã¡ã¤ã«ãã¯ãã¼ãºãã¦ãã¡ã¤ã«è¨è¿°åãåé¤ãã¾ãï¼
readFile()
ã¯ã¸ã§ãã¬ã¼ã¿ãè¨å®ãããã¹ã使ã£ã¦ãã¡ã¤ã«ãèªã¿è¾¼ã¿ï¼çµæããã¤ãã©ã¤ã³ãæµãããªãã¸ã§ã¯ãã® data
ããããã£ã«è¨å®ãã¾ãï¼
get()
ã¯ãã® data
ããããã£ãåãåºãã¦å¾ç¶ã«æ¸¡ãã¾ãï¼
each()
ã¯ãã (data
) ãã³ã¼ã«ããã¯ã§åãåã£ã¦å¦çãã¾ãï¼
ã¨ããæãï¼
ãã¤ã³ãã¯ï¼open()
ã¨ã write()
ã¨ããååã®é¢æ°ã¯ï¼å®éã«ã¯ open()
ã write()
ãè¡ãé¢æ°ãå¼ã³åºãé¢æ°ãæºåãã¦ãã (ãã¤ãã©ã¤ã³ãæ§ç¯ãã) ã ãã¨ãããã¨ï¼ããã¦ï¼å®éã®é¢æ°ã¯å段ã®å¦çãçµãã£ãå¾ã«éåæã«å¼ã³åºãããã¨ãããã¨ï¼
éä¸ã« print()
ãå
¥ããã¨ï¼ãã¤ãã©ã¤ã³ãæµãããã¼ã¿ã表示ãã¦ããã¦ä¾¿å©ã§ãï¼
対å¿ããã½ã¼ã¹ã¨æ··ããã¨ãããªæãã«ãªãã¾ãï¼
//async.file(path) { path: 'fileio.txt', name: 'fileio.txt' } // .writeFile('hello') { path: 'fileio.txt', name: 'fileio.txt' } // .open('a') { path: 'fileio.txt', name: 'fileio.txt', fd: 5 } // .write('\nworld') { path: 'fileio.txt', name: 'fileio.txt', fd: 5 } // .close() { path: 'fileio.txt' , name: 'fileio.txt' , fd: undefined } // .readFile('utf-8') { path: 'fileio.txt' , name: 'fileio.txt' , fd: undefined , data: 'hello\nworld' } // .get('data') hello world
ã¸ã§ãã¬ã¼ã¿ã® file()
ãä½ã£ããªãã¸ã§ã¯ãã« open()
ã fd
ã追å ãã¦ï¼close
ããããæ¶ãã¦ï¼readFile
ã data
ã追å ãã¦ï¼get()
ããã® data
ã ããå¾ç¶ã«æ¸¡ãã¦ããæ§åãåããã¾ãï¼
ã¡ãªã¿ã«ï¼ã¸ã§ãã¬ã¼ã¿ã«è¤æ°ã®ãã¡ã¤ã«ã®ãã¹ãåãåã async.js æ¬æ¥ã® files()
ã使ãã¨ï¼
async.files(['file1.txt', 'file2.txt', 'file3.txt']) .writeFile('hello') .open('a') .write('\nworld') .close() .readFile('utf-8') .get('data') .each(function(data) { var lines = data.split("\n"); sys.puts(lines[1]); }) .end();
ãªãã¦é¢¨ã«è¤æ°ã®ãã¡ã¤ã«ã«å¯¾ããç¹°ãè¿ããç°¡åï¼
ããã«ï¼glob()
ã¨ã walkfiles()
ãªãã¦ä¾¿å©ãããªã¸ã§ãã¬ã¼ã¿ãç¨æããã¦ããã®ã§ï¼ãã¡ã¤ã«ã«å¯¾ããæä½ã«é¢ãã¦ã¯ãããã便å©ããã§ãï¼
ãããã¯ã¼ã¯é¢ä¿ã®ãã©ã°ã¤ã³ããªãã¨ãï¼ReadableStream
ã«ã対å¿ãã¦ãªãããã¨ãï¼ã¨ã©ã¼ãã³ããªã³ã°ã¯ã©ãããã¨ãï¼èª²é¡ãå¤ãã㪠async.js ã§ããï¼ããã§ã Node.js ã§ã®ããã°ã©ãã³ã°ã¹ã¿ã¤ã«ã«ã¤ãã¦èãããã¦ãããããææã¨ããæãããã¾ãï¼
http.Client
ã®ã©ããã¼ã§ãä½ããªããããå°ãéãã§ã¿ããã¨æã£ã¦ã¾ãï¼