انتقل إلى المحتوى

node:stream

⁦Readable⁩ و⁦Writable⁩ و⁦Duplex⁩ و⁦Transform⁩ ومساعدات التركيب.

في هذه الصفحة

يصدّر ⁦Elide⁩ ‏⁦Readable⁩ و⁦Writable⁩ و⁦Duplex⁩ و⁦Transform⁩ و⁦PassThrough⁩. ويغطي المثال الموثَّق أدناه ⁦Readable.from⁩، وخطافات ⁦Transform⁩ و ⁦Writable⁩ المخصّصة، وإكمال خط الأنابيب القائم على ⁦Promise⁩.

مثال على خط أنابيب

استخدم ⁦node:stream/promises⁩ عند انتظار ⁦pipeline⁩:

import { Readable, Transform, Writable } from "node:stream";
import { pipeline } from "node:stream/promises";

const output = [];
const source = Readable.from(["a", "b"]);
const upper = new Transform({
  transform(chunk, _encoding, callback) {
    callback(null, String(chunk).toUpperCase());
  },
});
const sink = new Writable({
  write(chunk, _encoding, callback) {
    output.push(String(chunk));
    callback();
  },
});

await pipeline(source, upper, sink);
console.log(output.join("")); // AB

يستخدم ⁦pipeline⁩ و⁦finished⁩ من ⁦node:stream⁩ تواقيع دوال الاستدعاء. أما صيغهما التي تعيد ⁦Promise⁩ فتصدّرها ⁦node:stream/promises⁩.

واجهة إضافية

  • ⁦Readable.from⁩ و⁦Readable.wrap⁩ والتكرار غير المتزامن
  • مستهلكات ⁦Promise⁩ مثل ⁦toArray⁩ و⁦forEach⁩ و⁦some⁩ و⁦every⁩ و⁦find⁩ و ⁦reduce⁩
  • محوّلات التدفقات مثل ⁦map⁩ و⁦filter⁩ و⁦flatMap⁩ و⁦drop⁩ و⁦take⁩
  • ⁦compose⁩ و⁦addAbortSignal⁩ ومسندات الحالة ومساعدات علامة الحد الأعلى
  • ⁦node:stream/iter⁩، وهي امتداد من ⁦Elide⁩ يتضمن مساعدات لتحويل المكرِّرات

راجع توثيق ⁦stream⁩ في ⁦Node.js⁩ لتواقيع الفئات والخطافات القياسية.

Reference

Status: Partial · 215 of 238 suite tests pass (90.3%) · since 1.1.0

Exports

ExportKindStatusNotes
ReadableclassFullreadable, readableEnded, readableFlowing, readableHighWaterMark, readableLength, readableObjectMode, readableEncoding, destroyed, read(), setEncoding(), pause(), resume(), isPaused(), unpipe(), unshift(), wrap(), push(), destroy(), pipe(), toArray(), forEach(), some(), every(), find(), reduce(), map(), filter(), flatMap(), drop(), take()
WritableclassFullwritable, writableEnded, writableFinished, writableHighWaterMark, writableLength, writableObjectMode, writableCorked, destroyed, write(), end(), cork(), uncork(), destroy(), setDefaultEncoding()
DuplexclassFullwritable, writableEnded, writableFinished, writableHighWaterMark, writableLength, writableObjectMode, writableCorked, write(), end(), cork(), uncork(), setDefaultEncoding()
TransformclassFull
PassThroughclassFull
ReadablepropertyFullThe Readable stream class
WritablepropertyFullThe Writable stream class
DuplexpropertyFullThe Duplex stream class
TransformpropertyFullThe Transform stream class
PassThroughpropertyFullThe PassThrough stream class
pipelinepropertyFullPromise-based pipeline() utility for composing stream pipelines with error propagation
finishedpropertyFullPromise-based finished() utility that resolves/rejects when a stream completes or errors
composepropertyFullComposes streams into a single Duplex; pipes them and returns a Duplex that writes to the head and reads…

Not implemented

stream/web.ReadableStreamTee.

Tests: node-api node:streams: 215 pass, 22 fail, 1 error, 9 skipped. Failing: test-stream-auto-destroy.js, test-stream-compose.js, test-stream-consumers.js, test-stream-destroy.js, test-stream-duplex-from.js, test-stream-duplex.js, test-stream-iter-disabled.js, test-stream-iter-readable-interop-disabled.js, and 15 more.

هل كانت هذه الصفحة مفيدة؟