最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js pipe實現(xiàn)源碼解析

 更新時間:2017年08月12日 11:05:00   作者:xiedacon  
這篇文章主要介紹了Node.js pipe實現(xiàn)源碼解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

從前面兩篇文章,我們了解到。想要把 Readable 的數(shù)據(jù)寫到 Writable,就必須先手動的將數(shù)據(jù)讀入內(nèi)存,然后寫入 Writable。換句話說,每次傳遞數(shù)據(jù)時,都需要寫如下的模板代碼

readable.on('readable', (err) => {
 if(err) throw err

 writable.write(readable.read())
})

為了方便使用,Node.js 提供了 pipe() 方法,讓我們可以優(yōu)雅的傳遞數(shù)據(jù)

readable.pipe(writable)

現(xiàn)在,就讓我們來看看它是如何實現(xiàn)的吧

pipe

首先需要先調(diào)用 Readable 的 pipe() 方法

// lib/_stream_readable.js

Readable.prototype.pipe = function(dest, pipeOpts) {
 var src = this;
 var state = this._readableState;

 // 記錄 Writable
 switch (state.pipesCount) {
  case 0:
   state.pipes = dest;
   break;
  case 1:
   state.pipes = [state.pipes, dest];
   break;
  default:
   state.pipes.push(dest);
   break;
 }
 state.pipesCount += 1;

 // ...

  src.once('end', endFn);

 dest.on('unpipe', onunpipe);
 
 // ...

 dest.on('drain', ondrain);

 // ...

 src.on('data', ondata);

 // ...

 // 保證 error 事件觸發(fā)時,onerror 首先被執(zhí)行
 prependListener(dest, 'error', onerror);

 // ...

 dest.once('close', onclose);
 
 // ...

 dest.once('finish', onfinish);

 // ...

 // 觸發(fā) Writable 的 pipe 事件
 dest.emit('pipe', src);

 // 將 Readable 改為 flow 模式
 if (!state.flowing) {
  debug('pipe resume');
  src.resume();
 }

 return dest;
};

執(zhí)行 pipe() 函數(shù)時,首先將 Writable 記錄到 state.pipes 中,然后綁定相關(guān)事件,最后如果 Readable 不是 flow 模式,就調(diào)用 resume() 將 Readable 改為 flow 模式

傳遞數(shù)據(jù)

Readable 從數(shù)據(jù)源獲取到數(shù)據(jù)后,觸發(fā) data 事件,執(zhí)行 ondata()

ondata() 相關(guān)代碼:

// lib/_stream_readable.js

 // 防止在 dest.write(chunk) 內(nèi)調(diào)用 src.push(chunk) 造成 awaitDrain 重復增加,awaitDrain 不能清零,Readable 卡住的情況
 // 詳情見 https://github.com/nodejs/node/issues/7278
 var increasedAwaitDrain = false;
 function ondata(chunk) {
  debug('ondata');
  increasedAwaitDrain = false;
  var ret = dest.write(chunk);
  if (false === ret && !increasedAwaitDrain) {
   // 防止在 dest.write() 內(nèi)調(diào)用 src.unpipe(dest),導致 awaitDrain 不能清零,Readable 卡住的情況
   if (((state.pipesCount === 1 && state.pipes === dest) ||
      (state.pipesCount > 1 && state.pipes.indexOf(dest) !== -1)
     ) && 
     !cleanedUp) {
    debug('false write response, pause', src._readableState.awaitDrain);
    src._readableState.awaitDrain++;
    increasedAwaitDrain = true;
   }
   // 進入 pause 模式
   src.pause();
  }
 }

在 ondata(chunk) 函數(shù)內(nèi),通過 dest.write(chunk) 將數(shù)據(jù)寫入 Writable

此時,在 _write() 內(nèi)部可能會調(diào)用 src.push(chunk) 或使其 unpipe,這會導致 awaitDrain 多次增加,不能清零,Readable 卡住

當不能再向 Writable 寫入數(shù)據(jù)時,Readable 會進入 pause 模式,直到所有的 drain 事件觸發(fā)

觸發(fā) drain 事件,執(zhí)行 ondrain()

// lib/_stream_readable.js

 var ondrain = pipeOnDrain(src);

 function pipeOnDrain(src) {
  return function() {
   var state = src._readableState;
   debug('pipeOnDrain', state.awaitDrain);
   if (state.awaitDrain)
    state.awaitDrain--;
   // awaitDrain === 0,且有 data 監(jiān)聽器
   if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) {
    state.flowing = true;
    flow(src);
   }
  };
 }

每個 drain 事件觸發(fā)時,都會減少 awaitDrain,直到 awaitDrain 為 0。此時,調(diào)用 flow(src),使 Readable 進入 flow 模式

到這里,整個數(shù)據(jù)傳遞循環(huán)已經(jīng)建立,數(shù)據(jù)會順著循環(huán)源源不斷的流入 Writable,直到所有數(shù)據(jù)寫入完成

unpipe

不管寫入過程中是否出現(xiàn)錯誤,最后都會執(zhí)行 unpipe()

// lib/_stream_readable.js

// ...

 function unpipe() {
  debug('unpipe');
  src.unpipe(dest);
 }

// ...

Readable.prototype.unpipe = function(dest) {
 var state = this._readableState;
 var unpipeInfo = { hasUnpiped: false };

 // 啥也沒有
 if (state.pipesCount === 0)
  return this;

 // 只有一個
 if (state.pipesCount === 1) {
  if (dest && dest !== state.pipes)
   return this;
  // 沒有指定就 unpipe 所有
  if (!dest)
   dest = state.pipes;

  state.pipes = null;
  state.pipesCount = 0;
  state.flowing = false;
  if (dest)
   dest.emit('unpipe', this, unpipeInfo);
  return this;
 }

 // 沒有指定就 unpipe 所有
 if (!dest) {
  var dests = state.pipes;
  var len = state.pipesCount;
  state.pipes = null;
  state.pipesCount = 0;
  state.flowing = false;

  for (var i = 0; i < len; i++)
   dests[i].emit('unpipe', this, unpipeInfo);
  return this;
 }

 // 找到指定 Writable,并 unpipe
 var index = state.pipes.indexOf(dest);
 if (index === -1)
  return this;

 state.pipes.splice(index, 1);
 state.pipesCount -= 1;
 if (state.pipesCount === 1)
  state.pipes = state.pipes[0];

 dest.emit('unpipe', this, unpipeInfo);

 return this;
};

Readable.prototype.unpipe() 函數(shù)會根據(jù) state.pipes 屬性和 dest 參數(shù),選擇執(zhí)行策略。最后會觸發(fā) dest 的 unpipe 事件

unpipe 事件觸發(fā)后,調(diào)用 onunpipe(),清理相關(guān)數(shù)據(jù)

// lib/_stream_readable.js

 function onunpipe(readable, unpipeInfo) {
  debug('onunpipe');
  if (readable === src) {
   if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
    unpipeInfo.hasUnpiped = true;
    // 清理相關(guān)數(shù)據(jù)
    cleanup();
   }
  }
 }

End

在整個 pipe 的過程中,Readable 是主動方 ( 負責整個 pipe 過程:包括數(shù)據(jù)傳遞、unpipe 與異常處理 ),Writable 是被動方 ( 只需要觸發(fā) drain 事件 )

總結(jié)一下 pipe 的過程:

  • 首先執(zhí)行 readbable.pipe(writable),將 readable 與 writable 對接上
  • 當 readable 中有數(shù)據(jù)時,readable.emit('data'),將數(shù)據(jù)寫入 writable
  • 如果 writable.write(chunk) 返回 false,則進入 pause 模式,等待 drain 事件觸發(fā)
  • drain 事件全部觸發(fā)后,再次進入 flow 模式,寫入數(shù)據(jù)
  • 不管數(shù)據(jù)寫入完成或發(fā)生中斷,最后都會調(diào)用 unpipe()
  • unpipe() 調(diào)用 Readable.prototype.unpipe(),觸發(fā) dest 的 unpipe 事件,清理相關(guān)數(shù)據(jù)

參考:

https://github.com/nodejs/node/blob/master/lib/_stream_readable.js

https://github.com/nodejs/node/blob/master/lib/_stream_writable.js

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • windows使用nvm對node進行版本管理切換的完整步驟

    windows使用nvm對node進行版本管理切換的完整步驟

    這篇文章主要介紹了windows使用nvm對node進行版本管理切換的完整步驟,在使用之前各位務(wù)必卸載掉自己安裝過的nvm或者node版本包括環(huán)境變量之類的,要保證自己的電腦完全沒有node環(huán)境,需要的朋友可以參考下
    2024-03-03
  • NodeJS實現(xiàn)阿里大魚短信通知發(fā)送

    NodeJS實現(xiàn)阿里大魚短信通知發(fā)送

    本文給大家介紹的是nodejs實現(xiàn)使用阿里大魚短信API發(fā)送消息的方法和代碼,有需要的小伙伴可以參考下。
    2016-01-01
  • package.json文件配置詳解

    package.json文件配置詳解

    這篇文章主要介紹了package.json文件配置詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-06-06
  • 詳解從買域名到使用pm2部署node.js項目全過程

    詳解從買域名到使用pm2部署node.js項目全過程

    本篇文章主要介紹了詳解從買域名到使用pm2部署node.js項目全過程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Zabbix添加Node.js監(jiān)控的方法

    Zabbix添加Node.js監(jiān)控的方法

    這篇文章主要介紹了Zabbix添加Node.js監(jiān)控的方法,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • Node.js中修改全局變量的方式及注意事項

    Node.js中修改全局變量的方式及注意事項

    本文詳細介紹了在Node.js中如何修改全局變量,包括全局變量的定義方式、正確的修改姿勢、潛在的隱患及其解決方案,以及推薦的替代方案,總結(jié)建議了在實際應(yīng)用中應(yīng)盡量避免使用全局變量,轉(zhuǎn)而采用模塊化、可維護性更高的方法,感興趣的朋友一起看看吧
    2025-02-02
  • 淺談KOA2 Restful方式路由初探

    淺談KOA2 Restful方式路由初探

    這篇文章主要介紹了淺談KOA2 Restful方式路由初探,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Node.js API詳解之 querystring用法實例分析

    Node.js API詳解之 querystring用法實例分析

    這篇文章主要介紹了Node.js API詳解之 querystring用法,結(jié)合實例形式分析了Node.js API中querystring的基本功能、用法及相關(guān)操作注意事項,需要的朋友可以參考下
    2020-04-04
  • package.json與package-lock.json創(chuàng)建及使用詳解

    package.json與package-lock.json創(chuàng)建及使用詳解

    這篇文章主要為大家介紹了package.json與package-lock.json創(chuàng)建及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Node.js 深度調(diào)試方法解析

    Node.js 深度調(diào)試方法解析

    這篇文章主要介紹了Node.js 深度調(diào)試方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07

最新評論

宁阳县| 江达县| 台北市| 图片| 南康市| 赫章县| 舒兰市| 平湖市| 黄石市| 府谷县| 阿拉尔市| 翁源县| 泸西县| 庆阳市| 思南县| 汶川县| 普格县| 桑植县| 南通市| 抚松县| 北京市| 梁河县| 龙山县| 贵溪市| 彩票| 毕节市| 诸城市| 宁波市| 耿马| 昭苏县| 昌宁县| 濮阳县| 高阳县| 汕尾市| 准格尔旗| 阜城县| 惠来县| 滦平县| 亳州市| 巴里| 潞西市|