📜  Node.js Stream writeable.end() 方法

📅  最后修改于: 2022-05-13 01:56:25.162000             🧑  作者: Mango

Node.js Stream writeable.end() 方法

writable.end() 方法是 Stream 模块的内置应用程序编程接口,因此不再有数据可以写入 Writable。参数编码是可选的,这将允许在关闭流之前立即写入一个最终的新数据块。此外,添加了可选的回调函数作为 Writable 流的“完成”事件的侦听器。

句法:

writable.end( chunk, encoding, callback)

参数:此方法接受三个参数,如上所述,如下所述:

  • chunk:这是一个可选的数据写入。 chunk 的值必须是字符串、缓冲区或 Uint8Array。对于对象模式,块值可以是 null 以外的任何值。
  • encoding:如果块是字符串值,则保存编码值。
  • callback:流的可选回调函数。

返回值:它返回调用此方法之前写入的数据,如果 end() 方法有一大块新数据,那么最后也会返回。

下面的例子说明了 Node.js 中writable.end() 方法的使用:

示例 1:

// Node.js program to demonstrate the     
// writable.end() method  
  
// INcluding stream module
const stream = require('stream');
  
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
  
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
  
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
  
// Writing data
writable.write('hi');
  
// Calling end method with its 
// all the parameters
writable.end("last data", "utf8", () => {
     console.log("Writable stream ended!");
});

输出:

hi
last data
Writable {
  _writableState:
   WritableState {
     objectMode: false,
     highWaterMark: 16384,
     finalCalled: false,
     needDrain: false,
     ending: true,
     ended: true,
     finished: false,
     destroyed: false,
     decodeStrings: true,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     corked: 0,
     sync: false,
     bufferProcessing: false,
     onwrite: [Function: bound onwrite],
     writecb: null,
     writelen: 0,
     bufferedRequest: null,
     lastBufferedRequest: null,
     pendingcb: 2,
     prefinished: true,
     errorEmitted: false,
     emitClose: true,
     autoDestroy: false,
     bufferedRequestCount: 0,
     corkedRequestsFree:
      { next: null,
        entry: null,
        finish: [Function: bound onCorkedFinish] } },
  writable: false,
  _write: [Function: write],
  domain: null,
  _events:
   [Object: null prototype] {
     finish: { [Function: bound onceWrapper] listener: [Function] } },
  _eventsCount: 1,
  _maxListeners: undefined }
Writable stream ended!

示例 2:

// Node.js program to demonstrate the     
// writable.end() method  
  
// INcluding stream module
const stream = require('stream');
  
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
  
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
  
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
  
// Writing data
writable.write('hi');
  
// Calling end method with its 
// all the parameters
writable.end("last data", "utf8", () => {
     console.log("Writable stream ended!");
});
writable.write('GfG');

输出:

hi
last data
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at writeAfterEnd (_stream_writable.js:248:12)
    at Writable.write (_stream_writable.js:296:5)
    at /home/runner/LuxuriousLegitimateObservation/index.js:30:10
    at Script.runInContext (vm.js:133:20)
    at Object. (/run_dir/interp.js:156:20)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)Writab
le stream ended!

此处显示错误,因为在 end() 方法之后调用 write() 方法,这是不可能的。

参考: https://nodejs.org/api/stream.html#stream_writable_end_chunk_encoding_callback