mirror of
https://github.com/DeterminateSystems/nix-installer-action.git
synced 2025-01-10 22:32:06 +01:00
main.ts: use fetch-retry, add retry/backoff to fetching installer
This commit is contained in:
parent
e0fea1fb58
commit
58853de798
3 changed files with 3364 additions and 7087 deletions
10418
dist/index.js
vendored
10418
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
31
src/main.ts
31
src/main.ts
|
@ -5,12 +5,15 @@ import { spawn } from "node:child_process";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import { pipeline } from "node:stream";
|
import stream from "node:stream";
|
||||||
import fetch from "node-fetch";
|
import stream_web from "node:stream/web";
|
||||||
import { promisify } from "node:util";
|
import { finished } from "node:stream/promises";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import stringArgv from "string-argv";
|
import stringArgv from "string-argv";
|
||||||
|
|
||||||
|
import fetchRetry from "fetch-retry";
|
||||||
|
const retry_fetch = fetchRetry(global.fetch);
|
||||||
|
|
||||||
class NixInstallerAction {
|
class NixInstallerAction {
|
||||||
platform: string;
|
platform: string;
|
||||||
nix_package_url: string | null;
|
nix_package_url: string | null;
|
||||||
|
@ -408,7 +411,17 @@ class NixInstallerAction {
|
||||||
private async fetch_binary(): Promise<string> {
|
private async fetch_binary(): Promise<string> {
|
||||||
if (!this.local_root) {
|
if (!this.local_root) {
|
||||||
actions_core.info(`Fetching binary from ${this.nix_installer_url}`);
|
actions_core.info(`Fetching binary from ${this.nix_installer_url}`);
|
||||||
const response = await fetch(this.nix_installer_url);
|
const response = await retry_fetch(this.nix_installer_url, {
|
||||||
|
retries: 5,
|
||||||
|
retryDelay(
|
||||||
|
attempt: number,
|
||||||
|
_error: Error | null,
|
||||||
|
_response: Response | null,
|
||||||
|
) {
|
||||||
|
return Math.pow(2, attempt) * 1000; // 1000, 2000, 4000
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Got a status of ${response.status} from \`${this.nix_installer_url}\`, expected a 200`,
|
`Got a status of ${response.status} from \`${this.nix_installer_url}\`, expected a 200`,
|
||||||
|
@ -423,8 +436,14 @@ class NixInstallerAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.body !== null) {
|
if (response.body !== null) {
|
||||||
const streamPipeline = promisify(pipeline);
|
// shameless: https://stackoverflow.com/a/51302466
|
||||||
await streamPipeline(response.body, fs.createWriteStream(tempfile));
|
const fileStream = fs.createWriteStream(tempfile);
|
||||||
|
const responseBodyCast =
|
||||||
|
response.body as stream_web.ReadableStream<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||||
|
await finished(
|
||||||
|
stream.Readable.fromWeb(responseBodyCast).pipe(fileStream),
|
||||||
|
);
|
||||||
|
|
||||||
actions_core.info(`Downloaded \`nix-installer\` to \`${tempfile}\``);
|
actions_core.info(`Downloaded \`nix-installer\` to \`${tempfile}\``);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("No response body recieved");
|
throw new Error("No response body recieved");
|
||||||
|
|
Loading…
Reference in a new issue