當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript progress-stream.default函數代碼示例

本文整理匯總了TypeScript中progress-stream.default函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript default函數的具體用法?TypeScript default怎麽用?TypeScript default使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了default函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: async

  const extractEntry = async (
    entry: yauzl.Entry,
    err: Error,
    src: NodeJS.ReadableStream
  ) => {
    if (err) {
      throw err;
    }

    const entryPath = join(destination, entry.fileName);
    logger.info(`Extracting ${entryPath}`);

    await sf.mkdirp(dirname(entryPath));
    const dst = createWriteStream(entryPath);
    const progressStream = progress({
      length: entry.uncompressedSize,
      time: 100,
    });
    let progressFactor = entry.compressedSize / zipfile.fileSize;
    progressStream.on("progress", info => {
      opts.onProgress({
        progress: progressOffset + (info.percentage / 100) * progressFactor,
      });
    });
    /****************************************************************************
     * 💩 Crash course in Node Streams 💩
     *
     * Survival tips:
     *
     * 1. pipe() returns the destination stream.
     *    This is so you can chain pipe() calls, lulling you into a false sense of security.
     *
     * 2. If you don't subscribe to the "error" event, ***that's an uncaught exception***,
     *    which takes down the whole app.
     *
     *    It is thrown from what I can only assume is a scheduler, because the error does
     *    not have any stack trace anyway. It doesn't throw in the current function, or reject
     *    the current promise.
     *
     * 3. Whenever a destination stream encounters an error, it will unpipe itself
     *    from its source.
     *
     * 4. Errors do not propagate.
     *
     *    So, if you have A->B->C and C craps out, A->B will still be live and happily
     *    hanging forever once it's filled whatever buffer space it has.
     *
     * 5. Nobody agrees on what the final event is - might be "finish", "close", "end", or "error",
     *    gotta read the docs (and hope the docs are right!)
     *
     * 6. Stream "end"/"finish"/etc. *does* propagate, because in src.pipe(dst, opts?),
     *    opts = {end: true} is the default.
     *
     * 7. If you have A->B->C and A finishes, it doesn't mean C is done. In our case,
     *    A decompresses a zlib stream, B measures progress, and C writes to a file, so
     *    we have to wait for C to be done.
     *
     ***************************************************************************/
    src.pipe(progressStream).pipe(dst);
    await new Promise((_resolve, _reject) => {
      let timeout = setTimeout(() => {
        logger.warn(
          `Extracting a single entry took more than 10 seconds, something's wrong`
        );
      }, 10 * 1000 /* 10 seconds */);
      let resolve = () => {
        clearTimeout(timeout);
        _resolve();
      };
      let reject = (err: Error) => {
        clearTimeout(timeout);
        _reject(err);
      };

      src.on("error", err => {
        logger.warn(`Caught yauzl error: ${err.stack}`);
        reject(err);
      });
      progressStream.on("error", err => {
        logger.warn(`Caught progress stream error: ${err.stack}`);
        reject(err);
      });
      dst.on("error", err => {
        logger.warn(`Caught output stream error: ${err.stack}`);
        reject(err);
      });
      dst.on("finish", () => {
        resolve();
      });
    });

    progressOffset += progressFactor;
    await sf.chmod(entryPath, 0o755);

    const fileBuffer = await sf.readFile(entryPath, { encoding: null });
    const signedHash = crc32.buf(fileBuffer);
    // this converts an int32 to an uint32 (which is what yauzl reports)
    const hash = new Uint32Array([signedHash])[0];
    if (hash !== entry.crc32) {
      await sf.unlink(entryPath);
//.........這裏部分代碼省略.........
開發者ID:itchio,項目名稱:itch,代碼行數:101,代碼來源:unzip.ts

示例2: progress

 sink: () => {
   progressStream = progress({ length: totalSize, time: 500 });
   progressStream.on("progress", info => {
     onProgress({
       progress: info.percentage / 100,
       eta: info.eta,
       bps: info.speed,
       doneBytes: (info.percentage / 100) * totalSize,
       totalBytes: totalSize,
     });
     logger.info(
       `${info.percentage.toFixed(1)}% done, eta ${info.eta.toFixed(
         1
       )}s @ ${fileSize(info.speed)}/s`
     );
   });
   progressStream.pipe(fileSink);
   return progressStream;
 },
開發者ID:itchio,項目名稱:itch,代碼行數:19,代碼來源:download.ts

示例3: download

        let request = https.get(options, res => {
            // handle redirection
            if (res.statusCode === 302) {
                return download(res.headers.location).then(is => resolve(is)).catch(err => reject(err));
            }
            else if (res.statusCode !== 200) {
                return reject(Error(`Download failed with code ${res.statusCode}.`));
            }
			let len = parseInt(res.headers['content-length'], 10);
			let progressStream = progress({
				length: len,
				time: 500
			});
			downloadProgressItem.text = 'Completing Java installation';
			downloadProgressItem.show();
			progressStream.on('progress', function (progressData) {
				downloadProgressItem.text = 'Completing Java installation ' + Number.parseInt(progressData.percentage) + '%';
			});
            return resolve(res.pipe(progressStream));
        });
開發者ID:silverbulleters,項目名稱:vsc-spellchecker,代碼行數:20,代碼來源:downloadManager.ts

示例4: Error

  const extractEntry = async (entry: yauzl.Entry, err: Error, src: Stream) => {
    if (err) {
      throw err;
    }

    const entryPath = join(destination, entry.fileName);

    await sf.mkdirp(dirname(entryPath));
    const dst = createWriteStream(entryPath);
    const progressStream = progress({
      length: entry.uncompressedSize,
      time: 100,
    });
    let progressFactor = entry.compressedSize / zipfile.fileSize;
    progressStream.on("progress", info => {
      opts.onProgress({
        progress: progressOffset + info.percentage / 100 * progressFactor,
      });
    });
    progressStream.pipe(dst);
    src.pipe(progressStream);
    await sf.promised(dst);
    progressOffset += progressFactor;
    await sf.chmod(entryPath, 0o755);

    const fileBuffer = await sf.readFile(entryPath, { encoding: null });
    const signedHash = crc32.buf(fileBuffer);
    // this converts an int32 to an uint32 (which is what yauzl reports)
    const hash = new Uint32Array([signedHash])[0];
    if (hash !== entry.crc32) {
      await sf.unlink(entryPath);
      throw new Error(
        `CRC32 mismatch for ${entry.fileName}: expected ${
          entry.crc32
        } got ${hash}`
      );
    }
  };
開發者ID:HorrerGames,項目名稱:itch,代碼行數:38,代碼來源:unzip.ts

示例5: require

    return new Promise<any>((resolve, reject) => {

        var p = null;

        if (isUrlLocal(url)) {
            const URL = require('url').Url;
            var localUrl = url;
            if (!localUrl.startsWith("file:/")) {

                localUrl = path.join(path.dirname(previousVersionsLocalPath), new URL().parse(localUrl).path);
            }

            let localPath = new URL().parse(localUrl).path;
            var stat = fs.statSync(localPath);
            var str = fsProgress({
                length: stat.size,
                time: 100 /* ms */
            });

            str.on('progress', function (progress: any) {
                var p: any = {};
                p["percentage"] = progress.percentage / 100;
                progressCallback(p);

                if (progress.percentage >= 100) {
                    progressCallback(1);
                    hash(filePath).then(function (newMd5sum: string) {
                        if (newMd5sum == md5sum) {
                            resolve(filePath);
                        } else {
                            reject("Bad MD5 expected: '" + md5sum + "' got: '" + newMd5sum + "'");
                        }
                    }, function (error: string) {
                        reject(error);
                    });
                }
                /*
                {
                    percentage: 9.05,
                    transferred: 949624,
                    length: 10485760,
                    remaining: 9536136,
                    eta: 42,
                    runtime: 3,
                    delta: 295396,
                    speed: 949624
                }
                */
            });

            p = fs.createReadStream(localPath)
                .pipe(str);


        } else {

            p = progress(request(url))
                .on("progress", function (state: any) {
                    progressCallback(state);
                })
                .on("error", function (error: Error) {
                    reject(error);
                })
                .on("end", function () {
                    progressCallback(1);
                    hash(filePath).then(function (newMd5sum: string) {
                        if (newMd5sum == md5sum) {
                            resolve(filePath);
                        } else {
                            reject("Bad MD5 expected: '" + md5sum + "' got: '" + newMd5sum + "'");
                        }
                    }, function (error: string) {
                        reject(error);
                    });
                });
        }
        p.pipe(fs.createWriteStream(filePath));
    });
開發者ID:into-cps,項目名稱:intocps-ui,代碼行數:78,代碼來源:Downloader.ts


注:本文中的progress-stream.default函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。