Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

downloadUtils.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.downloadToTmp = void 0;
  4. const url_1 = require("url");
  5. const fs = require("fs-extra");
  6. const ProgressBar = require("progress");
  7. const tmp = require("tmp");
  8. const apiv2_1 = require("./apiv2");
  9. const error_1 = require("./error");
  10. async function downloadToTmp(remoteUrl) {
  11. const u = new url_1.URL(remoteUrl);
  12. const c = new apiv2_1.Client({ urlPrefix: u.origin, auth: false });
  13. const tmpfile = tmp.fileSync();
  14. const writeStream = fs.createWriteStream(tmpfile.name);
  15. const res = await c.request({
  16. method: "GET",
  17. path: u.pathname,
  18. queryParams: u.searchParams,
  19. responseType: "stream",
  20. resolveOnHTTPError: true,
  21. });
  22. if (res.status !== 200) {
  23. throw new error_1.FirebaseError(`download failed, status ${res.status}: ${await res.response.text()}`);
  24. }
  25. const total = parseInt(res.response.headers.get("content-length") || "0", 10);
  26. const totalMb = Math.ceil(total / 1000000);
  27. const bar = new ProgressBar(`Progress: :bar (:percent of ${totalMb}MB)`, { total, head: ">" });
  28. res.body.on("data", (chunk) => {
  29. bar.tick(chunk.length);
  30. });
  31. await new Promise((resolve) => {
  32. writeStream.on("finish", resolve);
  33. res.body.pipe(writeStream);
  34. });
  35. return tmpfile.name;
  36. }
  37. exports.downloadToTmp = downloadToTmp;