Skip to content

Commit 41f9249

Browse files
committed
Use 'streams' to download sources from GitHub
1 parent c1e5c0a commit 41f9249

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

packed/index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ var crypto = __importStar(__nccwpck_require__(6113));
8181
var fs = __importStar(__nccwpck_require__(7147));
8282
var os = __importStar(__nccwpck_require__(2037));
8383
var path = __importStar(__nccwpck_require__(1017));
84+
var node_fs_1 = __nccwpck_require__(7561);
85+
var promises_1 = __nccwpck_require__(6402);
8486
var cache = __importStar(__nccwpck_require__(7799));
8587
var core = __importStar(__nccwpck_require__(2186));
8688
var rest_1 = __nccwpck_require__(1273);
@@ -128,7 +130,7 @@ function download_git_repo(args) {
128130
case 0:
129131
fs.mkdirSync(args.directory, { recursive: true });
130132
return [4 /*yield*/, core.group("Downloading and extracting ".concat(args.repo_owner, "/").concat(args.repo_name, " (").concat(args.git_hash, ") into ").concat(args.directory), function () { return __awaiter(_this, void 0, void 0, function () {
131-
var response, ARCHIVE_PATH, admzip;
133+
var response, assetStream, ARCHIVE_PATH, outputFile, admzip;
132134
return __generator(this, function (_a) {
133135
switch (_a.label) {
134136
case 0:
@@ -137,12 +139,19 @@ function download_git_repo(args) {
137139
owner: args.repo_owner,
138140
repo: args.repo_name,
139141
ref: args.git_hash,
142+
request: {
143+
parseSuccessResponseBody: false, // required to access response as stream
144+
},
140145
})];
141146
case 1:
142147
response = _a.sent();
143-
core.info("Writing zip archive to disk...");
148+
assetStream = response.data;
144149
ARCHIVE_PATH = path.join(args.directory, "archive.zip");
145-
fs.writeFileSync(ARCHIVE_PATH, Buffer.from(response.data));
150+
outputFile = (0, node_fs_1.createWriteStream)(ARCHIVE_PATH);
151+
core.info("Writing zip archive to disk...");
152+
return [4 /*yield*/, (0, promises_1.pipeline)(assetStream, outputFile)];
153+
case 2:
154+
_a.sent();
146155
core.info("Extracting zip archive...");
147156
admzip = new AdmZip(ARCHIVE_PATH);
148157
admzip.getEntries().forEach(function (entry) {
@@ -67087,6 +67096,14 @@ module.exports = require("node:events");
6708767096

6708867097
/***/ }),
6708967098

67099+
/***/ 7561:
67100+
/***/ ((module) => {
67101+
67102+
"use strict";
67103+
module.exports = require("node:fs");
67104+
67105+
/***/ }),
67106+
6709067107
/***/ 8849:
6709167108
/***/ ((module) => {
6709267109

@@ -67127,6 +67144,14 @@ module.exports = require("node:stream");
6712767144

6712867145
/***/ }),
6712967146

67147+
/***/ 6402:
67148+
/***/ ((module) => {
67149+
67150+
"use strict";
67151+
module.exports = require("node:stream/promises");
67152+
67153+
/***/ }),
67154+
6713067155
/***/ 7261:
6713167156
/***/ ((module) => {
6713267157

src/main.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as crypto from "crypto";
33
import * as fs from "fs";
44
import * as os from "os";
55
import * as path from "path";
6+
import { createWriteStream } from "node:fs";
7+
import { pipeline } from "node:stream/promises";
68

79
import * as cache from "@actions/cache";
810
import * as core from "@actions/core";
@@ -73,16 +75,25 @@ async function download_git_repo(args: {
7375
`Downloading and extracting ${args.repo_owner}/${args.repo_name} (${args.git_hash}) into ${args.directory}`,
7476
async () => {
7577
core.info("Downloading git zip archive...");
78+
/* Use streams to avoid HTTP 500 HttpError/RequestError: other side closed
79+
* https://github.com/octokit/rest.js/issues/12#issuecomment-1916023479
80+
* https://github.com/octokit/rest.js/issues/461#issuecomment-2293930969
81+
*/
7682
const response = await args.octokit.rest.repos.downloadZipballArchive({
7783
owner: args.repo_owner,
7884
repo: args.repo_name,
7985
ref: args.git_hash,
86+
request: {
87+
parseSuccessResponseBody: false, // required to access response as stream
88+
},
8089
});
81-
core.info("Writing zip archive to disk...");
90+
const assetStream = response.data as unknown as NodeJS.ReadableStream;
8291
const ARCHIVE_PATH = path.join(args.directory, "archive.zip");
83-
fs.writeFileSync(ARCHIVE_PATH, Buffer.from(response.data as ArrayBuffer));
84-
core.info("Extracting zip archive...");
92+
const outputFile = createWriteStream(ARCHIVE_PATH);
93+
core.info("Writing zip archive to disk...");
94+
await pipeline(assetStream, outputFile);
8595

96+
core.info("Extracting zip archive...");
8697
const admzip = new AdmZip(ARCHIVE_PATH);
8798
admzip.getEntries().forEach((entry) => {
8899
if (entry.isDirectory) {

0 commit comments

Comments
 (0)