11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst { Readable, Writable } = require('stream');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst ABC = new Uint8Array([0x41, 0x42, 0x43]);
81cb0ef41Sopenharmony_ciconst DEF = new Uint8Array([0x44, 0x45, 0x46]);
91cb0ef41Sopenharmony_ciconst GHI = new Uint8Array([0x47, 0x48, 0x49]);
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  // Simple Writable test.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  let n = 0;
151cb0ef41Sopenharmony_ci  const writable = new Writable({
161cb0ef41Sopenharmony_ci    write: common.mustCall((chunk, encoding, cb) => {
171cb0ef41Sopenharmony_ci      assert(chunk instanceof Buffer);
181cb0ef41Sopenharmony_ci      if (n++ === 0) {
191cb0ef41Sopenharmony_ci        assert.strictEqual(String(chunk), 'ABC');
201cb0ef41Sopenharmony_ci      } else {
211cb0ef41Sopenharmony_ci        assert.strictEqual(String(chunk), 'DEF');
221cb0ef41Sopenharmony_ci      }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci      cb();
251cb0ef41Sopenharmony_ci    }, 2)
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  writable.write(ABC);
291cb0ef41Sopenharmony_ci  writable.end(DEF);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci{
331cb0ef41Sopenharmony_ci  // Writable test, pass in Uint8Array in object mode.
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const writable = new Writable({
361cb0ef41Sopenharmony_ci    objectMode: true,
371cb0ef41Sopenharmony_ci    write: common.mustCall((chunk, encoding, cb) => {
381cb0ef41Sopenharmony_ci      assert(!(chunk instanceof Buffer));
391cb0ef41Sopenharmony_ci      assert(chunk instanceof Uint8Array);
401cb0ef41Sopenharmony_ci      assert.strictEqual(chunk, ABC);
411cb0ef41Sopenharmony_ci      assert.strictEqual(encoding, 'utf8');
421cb0ef41Sopenharmony_ci      cb();
431cb0ef41Sopenharmony_ci    })
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  writable.end(ABC);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  // Writable test, multiple writes carried out via writev.
511cb0ef41Sopenharmony_ci  let callback;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  const writable = new Writable({
541cb0ef41Sopenharmony_ci    write: common.mustCall((chunk, encoding, cb) => {
551cb0ef41Sopenharmony_ci      assert(chunk instanceof Buffer);
561cb0ef41Sopenharmony_ci      assert.strictEqual(encoding, 'buffer');
571cb0ef41Sopenharmony_ci      assert.strictEqual(String(chunk), 'ABC');
581cb0ef41Sopenharmony_ci      callback = cb;
591cb0ef41Sopenharmony_ci    }),
601cb0ef41Sopenharmony_ci    writev: common.mustCall((chunks, cb) => {
611cb0ef41Sopenharmony_ci      assert.strictEqual(chunks.length, 2);
621cb0ef41Sopenharmony_ci      assert.strictEqual(chunks[0].encoding, 'buffer');
631cb0ef41Sopenharmony_ci      assert.strictEqual(chunks[1].encoding, 'buffer');
641cb0ef41Sopenharmony_ci      assert.strictEqual(chunks[0].chunk + chunks[1].chunk, 'DEFGHI');
651cb0ef41Sopenharmony_ci    })
661cb0ef41Sopenharmony_ci  });
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  writable.write(ABC);
691cb0ef41Sopenharmony_ci  writable.write(DEF);
701cb0ef41Sopenharmony_ci  writable.end(GHI);
711cb0ef41Sopenharmony_ci  callback();
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  // Simple Readable test.
761cb0ef41Sopenharmony_ci  const readable = new Readable({
771cb0ef41Sopenharmony_ci    read() {}
781cb0ef41Sopenharmony_ci  });
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  readable.push(DEF);
811cb0ef41Sopenharmony_ci  readable.unshift(ABC);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  const buf = readable.read();
841cb0ef41Sopenharmony_ci  assert(buf instanceof Buffer);
851cb0ef41Sopenharmony_ci  assert.deepStrictEqual([...buf], [...ABC, ...DEF]);
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci{
891cb0ef41Sopenharmony_ci  // Readable test, setEncoding.
901cb0ef41Sopenharmony_ci  const readable = new Readable({
911cb0ef41Sopenharmony_ci    read() {}
921cb0ef41Sopenharmony_ci  });
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  readable.setEncoding('utf8');
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  readable.push(DEF);
971cb0ef41Sopenharmony_ci  readable.unshift(ABC);
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  const out = readable.read();
1001cb0ef41Sopenharmony_ci  assert.strictEqual(out, 'ABCDEF');
1011cb0ef41Sopenharmony_ci}
102