11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst {
61cb0ef41Sopenharmony_ci  open,
71cb0ef41Sopenharmony_ci} = require('fs/promises');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst {
101cb0ef41Sopenharmony_ci  Buffer,
111cb0ef41Sopenharmony_ci} = require('buffer');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciclass Source {
141cb0ef41Sopenharmony_ci  async start(controller) {
151cb0ef41Sopenharmony_ci    this.file = await open(__filename);
161cb0ef41Sopenharmony_ci    this.controller = controller;
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  async pull(controller) {
201cb0ef41Sopenharmony_ci    const byobRequest = controller.byobRequest;
211cb0ef41Sopenharmony_ci    const view = byobRequest.view;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    const {
241cb0ef41Sopenharmony_ci      bytesRead,
251cb0ef41Sopenharmony_ci    } = await this.file.read({
261cb0ef41Sopenharmony_ci      buffer: view,
271cb0ef41Sopenharmony_ci      offset: view.byteOffset,
281cb0ef41Sopenharmony_ci      length: view.byteLength
291cb0ef41Sopenharmony_ci    });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    if (bytesRead === 0) {
321cb0ef41Sopenharmony_ci      await this.file.close();
331cb0ef41Sopenharmony_ci      this.controller.close();
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    byobRequest.respond(bytesRead);
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  get type() { return 'bytes'; }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  get autoAllocateChunkSize() { return 1024; }
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci(async () => {
451cb0ef41Sopenharmony_ci  const source = new Source();
461cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const { emitWarning } = process;
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  process.emitWarning = common.mustNotCall();
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  try {
531cb0ef41Sopenharmony_ci    const reader = stream.getReader({ mode: 'byob' });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    let result;
561cb0ef41Sopenharmony_ci    do {
571cb0ef41Sopenharmony_ci      result = await reader.read(Buffer.alloc(100));
581cb0ef41Sopenharmony_ci    } while (!result.done);
591cb0ef41Sopenharmony_ci  } finally {
601cb0ef41Sopenharmony_ci    process.emitWarning = emitWarning;
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci})().then(common.mustCall());
63