stream#pipeline TypeScript Examples

The following examples show how to use stream#pipeline. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: calibrate.ts    From Assistive-Webdriver with MIT License 6 votes vote down vote up
/**
   * Saves the {@link CalibrationError.screenshot | screenshot} as a file.
   * @param fileName - Full path (or path relative to the current directory) where to store the screenshot.
   * The screenshot will be stored in the PNG format.
   */
  saveScreenshot(fileName: string): Promise<void> {
    return new Promise((resolve, reject) =>
      pipeline(this.screenshot.pack(), createWriteStream(fileName), err =>
        err ? reject(err) : resolve()
      )
    );
  }
Example #2
Source File: TestPipeline.ts    From backstage with Apache License 2.0 6 votes vote down vote up
/**
   * Execute the test pipeline so that you can make assertions about the result
   * or behavior of the given test subject.
   */
  async execute(): Promise<TestPipelineResult> {
    const documents: IndexableDocument[] = [];
    if (!this.collator) {
      throw new Error(
        'Cannot execute pipeline without a collator or documents',
      );
    }

    // If we are here and there is no indexer, we are testing a collator or a
    // decorator. Set up a naive writable that captures documents in memory.
    if (!this.indexer) {
      this.indexer = new Writable({ objectMode: true });
      this.indexer._write = (document: IndexableDocument, _, done) => {
        documents.push(document);
        done();
      };
    }

    return new Promise<TestPipelineResult>(done => {
      const pipes: (Readable | Transform | Writable)[] = [this.collator!];
      if (this.decorator) {
        pipes.push(this.decorator);
      }
      pipes.push(this.indexer!);

      pipeline(pipes, (error: NodeJS.ErrnoException | null) => {
        done({
          error,
          documents,
        });
      });
    });
  }
Example #3
Source File: install.ts    From blake3 with MIT License 6 votes vote down vote up
async function download(url: string): Promise<boolean> {
  return new Promise<boolean>(resolve => {
    const onError = (err: Error) => {
      console.error(`Could not download binding from ${url}: ${err.stack || err.message}`);
      resolve(false);
    };

    const req = get(url, res => {
      if (res.headers.location) {
        resolve(download(res.headers.location));
        return;
      }

      if (!res.statusCode || res.statusCode >= 300) {
        console.error(`Unexpected ${res.statusCode} from ${url}`);
        resolve(false);
        return;
      }

      pipeline(res, createWriteStream(bindingPath), err => (err ? onError(err) : resolve(true)));
    });

    req.on('error', onError);
  });
}
Example #4
Source File: dataDump.ts    From cloud-pricing-api with Apache License 2.0 6 votes vote down vote up
async function dumpFile(client: PoolClient, outfile: string): Promise<void> {
  const promisifiedPipeline = promisify(pipeline);

  const copyStream = client.query(
    copyTo(
      format(
        `
      COPY %I TO STDOUT WITH (
      FORMAT csv,
      HEADER true,
      DELIMITER ','
    )`,
        config.productTableName
      )
    )
  );

  const gzip = zlib.createGzip().on('error', (e) => {
    config.logger.info(e);
    process.exit(1);
  });

  const fileStream = fs.createWriteStream(`${outfile}`);

  return promisifiedPipeline(copyStream, gzip, fileStream);
}
Example #5
Source File: VoicePanel.ts    From discord-qt with GNU General Public License v3.0 6 votes vote down vote up
private initPlayback = () => {
    if (!this.connection || !this.channel || !vp) {
      return;
    }

    const { channel } = this;

    this.mixer?.close();
    this.playbackStream?.end();
    this.streams.clear();

    pipeline(
      (this.mixer = new Mixer(MIXER_OPTIONS)), // Initialize the member streams mixer
      ((this.playbackVolumeTransformer = new VolumeTransformer({
        type: 's16le',
      })) as unknown) as Transform, // Change the volume
      (this.playbackStream = vp.createPlaybackStream()).stream, // Output to a playback stream
      (err) => err && debug("Couldn't finish playback pipeline.", err)
    );

    for (const member of channel.members.filter((m) => m.user !== app.client.user).values()) {
      this.connectToMixer(member);
    }
  };
Example #6
Source File: VoicePanel.ts    From discord-qt with GNU General Public License v3.0 6 votes vote down vote up
private initRecord = () => {
    if (!this.connection || !vp) {
      return;
    }

    this.connection.dispatcher.end();

    const recorder = pipeline(
      (this.recordStream = vp.createRecordStream()).stream, // Open a recording stream
      ((this.recordVolumeTransformer = new VolumeTransformer({
        type: 's16le',
      })) as unknown) as Transform,
      // Change the volume
      (this.recordNoiseReductor = new NoiseReductor(this.onSpeaking.bind(this))), // Audio gate
      (err) => err && debug("Couldn't finish recording pipeline.", err)
    );

    this.connection.play(recorder, { bitrate: 256, type: 'converted', highWaterMark: 0 });
  };
Example #7
Source File: cms.service.ts    From Cromwell with MIT License 5 votes vote down vote up
pump = util.promisify(pipeline)
Example #8
Source File: IndexBuilder.ts    From backstage with Apache License 2.0 5 votes vote down vote up
/**
   * Compiles collators and decorators into tasks, which are added to a
   * scheduler returned to the caller.
   */
  async build(): Promise<{ scheduler: Scheduler }> {
    const scheduler = new Scheduler({
      logger: this.logger,
    });

    Object.keys(this.collators).forEach(type => {
      scheduler.addToSchedule({
        id: `search_index_${type.replace('-', '_').toLocaleLowerCase('en-US')}`,
        scheduledRunner: this.collators[type].schedule,
        task: async () => {
          // Instantiate the collator.
          const collator = await this.collators[type].factory.getCollator();
          this.logger.info(
            `Collating documents for ${type} via ${this.collators[type].factory.constructor.name}`,
          );

          // Instantiate all relevant decorators.
          const decorators: Transform[] = await Promise.all(
            (this.decorators['*'] || [])
              .concat(this.decorators[type] || [])
              .map(async factory => {
                const decorator = await factory.getDecorator();
                this.logger.info(
                  `Attached decorator via ${factory.constructor.name} to ${type} index pipeline.`,
                );
                return decorator;
              }),
          );

          // Instantiate the indexer.
          const indexer = await this.searchEngine.getIndexer(type);

          // Compose collator/decorators/indexer into a pipeline
          return new Promise<void>((resolve, reject) => {
            pipeline(
              [collator, ...decorators, indexer],
              (error: NodeJS.ErrnoException | null) => {
                if (error) {
                  this.logger.error(
                    `Collating documents for ${type} failed: ${error}`,
                  );
                  reject(error);
                } else {
                  // Signal index pipeline completion!
                  this.logger.info(`Collating documents for ${type} succeeded`);
                  resolve();
                }
              },
            );
          });
        },
      });
    });

    return {
      scheduler,
    };
  }
Example #9
Source File: dataLoad.ts    From cloud-pricing-api with Apache License 2.0 5 votes vote down vote up
async function loadFile(client: PoolClient, filename: string): Promise<void> {
  const promisifiedPipeline = promisify(pipeline);

  const gunzip = zlib.createGunzip().on('error', (e) => {
    config.logger.error(
      `There was an error decompressing the file: ${e.message}`
    );
    config.logger.error(
      `The latest data files can be downloaded with "npm run-scripts data:download"`
    );
    process.exit(1);
  });

  const pgCopy = client.query(
    copyFrom(`
    COPY "ProductLoad" FROM STDIN WITH (
      FORMAT csv, 
      HEADER true, 
      DELIMITER ',', 
      FORCE_NOT_NULL ("productFamily")
    )`)
  );

  const { size } = fs.statSync(filename);
  const progressBar = new ProgressBar(
    '-> loading [:bar] :percent (:etas remaining)',
    {
      width: 40,
      complete: '=',
      incomplete: ' ',
      renderThrottle: 500,
      total: size,
    }
  );

  const readStream = fs.createReadStream(filename);
  readStream.on('data', (buffer) => progressBar.tick(buffer.length));

  return promisifiedPipeline(readStream, gunzip, pgCopy);
}
Example #10
Source File: Stream.ts    From vscode-file-downloader with MIT License 5 votes vote down vote up
pipelineAsync = promisify(pipeline)
Example #11
Source File: calibration.ts    From Assistive-Webdriver with MIT License 4 votes vote down vote up
export async function webdriverCalibrate(
  vm: VM,
  sessionUrl: string,
  failedCalibrationFileName?: string,
  log: LogFunction = () => {}
): Promise<ScreenPosition> {
  log = createSubLogFunction(log, { category: "calibration" });
  try {
    const currentUrl = await request(
      `${sessionUrl}/url`,
      { method: "GET" },
      log
    );
    if (currentUrl.value === "about:blank") {
      // on Firefox, we must navigate to a URL different than about:blank before anything can be displayed
      // (such as the calibration rectangle)
      await request(
        `${sessionUrl}/url`,
        {
          method: "POST",
          body: {
            // navigate to an empty <html><body></body></html> document
            url: "data:text/html,%3Chtml%3E%%3Cbody%3E%3C%2Fbody%3E3C%2Fhtml%3E"
          }
        },
        log
      );
    }
    const sizeInfo = await request(
      `${sessionUrl}/execute/sync`,
      {
        body: {
          script: `var div = document.createElement("div"); div.setAttribute("id", "calibrationDIV");
div.style.cssText = "display:block;position:absolute;left:0px;top:0px;right:0px;bottom:0px;cursor:none;z-index:999999;";
document.body.appendChild(div);
return {
  width: div.offsetWidth,
  height: div.offsetHeight,
  screenX: window.screenX,
  screenY: window.screenY
};`,
          args: []
        }
      },
      log
    );
    const viewportImage = calibrationQRCodesGenerate(
      sizeInfo.value.width,
      sizeInfo.value.height
    );
    await request(
      `${sessionUrl}/execute/sync`,
      {
        body: {
          script: `var calibrationDIV = document.getElementById("calibrationDIV"); calibrationDIV.style.backgroundImage = ${JSON.stringify(
            `url("${await pngToDataURI(viewportImage)}")`
          )};`,
          args: []
        }
      },
      log
    );
    log({
      message: "displayed",
      result: sizeInfo.value
    });
    await wait(1000);
    const image = await vm.takePNGScreenshot();
    let calibrationResult;
    try {
      calibrationResult = calibrationQRCodesScan(image);
    } catch (error) {
      if (failedCalibrationFileName) {
        await new Promise<void>((resolve, reject) =>
          pipeline(
            image.pack(),
            createWriteStream(failedCalibrationFileName),
            err => (err ? reject(err) : resolve())
          )
        );
        throw new Error(
          `${error}\nScreenshot recorded as ${failedCalibrationFileName}`
        );
      } else {
        throw new Error(`${error}\nScreenshot was not saved.`);
      }
    }
    log({
      message: "success",
      result: calibrationResult
    });
    // click on the detected QR code:
    await vm.sendMouseMoveEvent({
      x: Math.floor(
        calibrationResult.qrCode.x + calibrationResult.qrCode.width / 2
      ),
      y: Math.floor(
        calibrationResult.qrCode.y + calibrationResult.qrCode.height / 2
      ),
      screenWidth: image.width,
      screenHeight: image.height
    });
    await wait(100);
    await vm.sendMouseDownEvent(MouseButton.LEFT);
    await wait(50);
    await vm.sendMouseUpEvent(MouseButton.LEFT);
    await wait(100);
    // moves again the mouse out of the way
    await vm.sendMouseMoveEvent({
      x: sizeInfo.value.screenX,
      y: sizeInfo.value.screenY,
      screenWidth: image.width,
      screenHeight: image.height
    });
    await wait(100);
    await request(
      `${sessionUrl}/execute/sync`,
      {
        body: {
          script: `var calibrationDIV = document.getElementById("calibrationDIV"); calibrationDIV.parentNode.removeChild(calibrationDIV);`,
          args: []
        }
      },
      log
    );
    return {
      x: calibrationResult.viewport.x - sizeInfo.value.screenX,
      y: calibrationResult.viewport.y - sizeInfo.value.screenY,
      screenWidth: image.width,
      screenHeight: image.height
    };
  } catch (error) {
    throw new PublicError(
      500,
      "calibration failed",
      `Calibration for native events failed: ${error}`
    );
  }
}