• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5const {
6  open,
7} = require('fs/promises');
8
9const {
10  Buffer,
11} = require('buffer');
12
13class Source {
14  async start(controller) {
15    this.file = await open(__filename);
16    this.controller = controller;
17  }
18
19  async pull(controller) {
20    const byobRequest = controller.byobRequest;
21    const view = byobRequest.view;
22
23    const {
24      bytesRead,
25    } = await this.file.read({
26      buffer: view,
27      offset: view.byteOffset,
28      length: view.byteLength
29    });
30
31    if (bytesRead === 0) {
32      await this.file.close();
33      this.controller.close();
34    }
35
36    byobRequest.respond(bytesRead);
37  }
38
39  get type() { return 'bytes'; }
40
41  get autoAllocateChunkSize() { return 1024; }
42}
43
44(async () => {
45  const source = new Source();
46  const stream = new ReadableStream(source);
47
48  const { emitWarning } = process;
49
50  process.emitWarning = common.mustNotCall();
51
52  try {
53    const reader = stream.getReader({ mode: 'byob' });
54
55    let result;
56    do {
57      result = await reader.read(Buffer.alloc(100));
58    } while (!result.done);
59  } finally {
60    process.emitWarning = emitWarning;
61  }
62})().then(common.mustCall());
63