11cb0ef41Sopenharmony_ci// Flags: --expose-internals --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  internalBinding,
101cb0ef41Sopenharmony_ci} = require('internal/test/binding');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst {
131cb0ef41Sopenharmony_ci  newWritableStreamFromStreamBase,
141cb0ef41Sopenharmony_ci  newReadableStreamFromStreamBase,
151cb0ef41Sopenharmony_ci} = require('internal/webstreams/adapters');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst {
181cb0ef41Sopenharmony_ci  JSStream
191cb0ef41Sopenharmony_ci} = internalBinding('js_stream');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  const stream = new JSStream();
231cb0ef41Sopenharmony_ci  stream.onwrite = common.mustCall((req, buf) => {
241cb0ef41Sopenharmony_ci    assert.deepStrictEqual(buf[0], Buffer.from('hello'));
251cb0ef41Sopenharmony_ci    req.oncomplete();
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const writable = newWritableStreamFromStreamBase(stream);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  writer.write(Buffer.from('hello')).then(common.mustCall());
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci  const buf = Buffer.from('hello');
371cb0ef41Sopenharmony_ci  const check = new Uint8Array(buf);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  const stream = new JSStream();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const readable = newReadableStreamFromStreamBase(stream);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  const reader = readable.getReader();
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ done, value }) => {
461cb0ef41Sopenharmony_ci    assert(!done);
471cb0ef41Sopenharmony_ci    assert.deepStrictEqual(new Uint8Array(value), check);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ done, value }) => {
501cb0ef41Sopenharmony_ci      assert(done);
511cb0ef41Sopenharmony_ci      assert.strictEqual(value, undefined);
521cb0ef41Sopenharmony_ci    }));
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  }));
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  stream.readBuffer(buf);
571cb0ef41Sopenharmony_ci  stream.emitEOF();
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci{
611cb0ef41Sopenharmony_ci  const stream = new JSStream();
621cb0ef41Sopenharmony_ci  stream.onshutdown = common.mustCall((req) => {
631cb0ef41Sopenharmony_ci    req.oncomplete();
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci  const readable = newReadableStreamFromStreamBase(stream);
661cb0ef41Sopenharmony_ci  readable.cancel().then(common.mustCall());
671cb0ef41Sopenharmony_ci}
68