11cb0ef41Sopenharmony_ci// Flags: --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst {
81cb0ef41Sopenharmony_ci  TextEncoderStream,
91cb0ef41Sopenharmony_ci  TextDecoderStream,
101cb0ef41Sopenharmony_ci} = require('stream/web');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst kEuroBytes = Buffer.from([0xe2, 0x82, 0xac]);
131cb0ef41Sopenharmony_ciconst kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci[1, false, [], {}, 'hello'].forEach((i) => {
161cb0ef41Sopenharmony_ci  assert.throws(() => new TextDecoderStream(i), {
171cb0ef41Sopenharmony_ci    code: 'ERR_ENCODING_NOT_SUPPORTED',
181cb0ef41Sopenharmony_ci  });
191cb0ef41Sopenharmony_ci});
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci[1, false, 'hello'].forEach((i) => {
221cb0ef41Sopenharmony_ci  assert.throws(() => new TextDecoderStream(undefined, i), {
231cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci});
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci{
281cb0ef41Sopenharmony_ci  const tds = new TextDecoderStream();
291cb0ef41Sopenharmony_ci  const writer = tds.writable.getWriter();
301cb0ef41Sopenharmony_ci  const reader = tds.readable.getReader();
311cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
321cb0ef41Sopenharmony_ci    assert(!done);
331cb0ef41Sopenharmony_ci    assert.strictEqual(kEuro, value);
341cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ done }) => {
351cb0ef41Sopenharmony_ci      assert(done);
361cb0ef41Sopenharmony_ci    }));
371cb0ef41Sopenharmony_ci  }));
381cb0ef41Sopenharmony_ci  Promise.all([
391cb0ef41Sopenharmony_ci    writer.write(kEuroBytes.slice(0, 1)),
401cb0ef41Sopenharmony_ci    writer.write(kEuroBytes.slice(1, 2)),
411cb0ef41Sopenharmony_ci    writer.write(kEuroBytes.slice(2, 3)),
421cb0ef41Sopenharmony_ci    writer.close(),
431cb0ef41Sopenharmony_ci  ]).then(common.mustCall());
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  assert.strictEqual(tds.encoding, 'utf-8');
461cb0ef41Sopenharmony_ci  assert.strictEqual(tds.fatal, false);
471cb0ef41Sopenharmony_ci  assert.strictEqual(tds.ignoreBOM, false);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  assert.throws(
501cb0ef41Sopenharmony_ci    () => Reflect.get(TextDecoderStream.prototype, 'encoding', {}), {
511cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
521cb0ef41Sopenharmony_ci    });
531cb0ef41Sopenharmony_ci  assert.throws(
541cb0ef41Sopenharmony_ci    () => Reflect.get(TextDecoderStream.prototype, 'fatal', {}), {
551cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
561cb0ef41Sopenharmony_ci    });
571cb0ef41Sopenharmony_ci  assert.throws(
581cb0ef41Sopenharmony_ci    () => Reflect.get(TextDecoderStream.prototype, 'ignoreBOM', {}), {
591cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
601cb0ef41Sopenharmony_ci    });
611cb0ef41Sopenharmony_ci  assert.throws(
621cb0ef41Sopenharmony_ci    () => Reflect.get(TextDecoderStream.prototype, 'readable', {}), {
631cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
641cb0ef41Sopenharmony_ci    });
651cb0ef41Sopenharmony_ci  assert.throws(
661cb0ef41Sopenharmony_ci    () => Reflect.get(TextDecoderStream.prototype, 'writable', {}), {
671cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
681cb0ef41Sopenharmony_ci    });
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci{
721cb0ef41Sopenharmony_ci  const tds = new TextEncoderStream();
731cb0ef41Sopenharmony_ci  const writer = tds.writable.getWriter();
741cb0ef41Sopenharmony_ci  const reader = tds.readable.getReader();
751cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
761cb0ef41Sopenharmony_ci    assert(!done);
771cb0ef41Sopenharmony_ci    const buf = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
781cb0ef41Sopenharmony_ci    assert.deepStrictEqual(kEuroBytes, buf);
791cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ done }) => {
801cb0ef41Sopenharmony_ci      assert(done);
811cb0ef41Sopenharmony_ci    }));
821cb0ef41Sopenharmony_ci  }));
831cb0ef41Sopenharmony_ci  Promise.all([
841cb0ef41Sopenharmony_ci    writer.write(kEuro),
851cb0ef41Sopenharmony_ci    writer.close(),
861cb0ef41Sopenharmony_ci  ]).then(common.mustCall());
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  assert.strictEqual(tds.encoding, 'utf-8');
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  assert.throws(
911cb0ef41Sopenharmony_ci    () => Reflect.get(TextEncoderStream.prototype, 'encoding', {}), {
921cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
931cb0ef41Sopenharmony_ci    });
941cb0ef41Sopenharmony_ci  assert.throws(
951cb0ef41Sopenharmony_ci    () => Reflect.get(TextEncoderStream.prototype, 'readable', {}), {
961cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
971cb0ef41Sopenharmony_ci    });
981cb0ef41Sopenharmony_ci  assert.throws(
991cb0ef41Sopenharmony_ci    () => Reflect.get(TextEncoderStream.prototype, 'writable', {}), {
1001cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
1011cb0ef41Sopenharmony_ci    });
1021cb0ef41Sopenharmony_ci}
103