summaryrefslogtreecommitdiff
path: root/test/parallel/test-whatwg-webstreams-adapters-streambase.js
blob: 43d28f0499dc7d9306c8202d0fddb15e8977d8c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Flags: --expose-internals --no-warnings
'use strict';

const common = require('../common');

const assert = require('assert');

const {
  internalBinding,
} = require('internal/test/binding');

const {
  newWritableStreamFromStreamBase,
  newReadableStreamFromStreamBase,
} = require('internal/webstreams/adapters');

const {
  JSStream
} = internalBinding('js_stream');

{
  const stream = new JSStream();
  stream.onwrite = common.mustCall((req, buf) => {
    assert.deepStrictEqual(buf[0], Buffer.from('hello'));
    req.oncomplete();
  });

  const writable = newWritableStreamFromStreamBase(stream);

  const writer = writable.getWriter();

  writer.write(Buffer.from('hello')).then(common.mustCall());
}

{
  const buf = Buffer.from('hello');
  const check = new Uint8Array(buf);

  const stream = new JSStream();

  const readable = newReadableStreamFromStreamBase(stream);

  const reader = readable.getReader();

  reader.read().then(common.mustCall(({ done, value }) => {
    assert(!done);
    assert.deepStrictEqual(new Uint8Array(value), check);

    reader.read().then(common.mustCall(({ done, value }) => {
      assert(done);
      assert.strictEqual(value, undefined);
    }));

  }));

  stream.readBuffer(buf);
  stream.emitEOF();
}

{
  const stream = new JSStream();
  stream.onshutdown = common.mustCall((req) => {
    req.oncomplete();
  });
  const readable = newReadableStreamFromStreamBase(stream);
  readable.cancel().then(common.mustCall());
}