Skip to content

Commit

Permalink
chore: replace 'request' dependency by 'node-fetch' (philips-labs#903)
Browse files Browse the repository at this point in the history
Also await the success of both promises before reporting that it was successfully uploaded to S3.
  • Loading branch information
aadrijnberg committed Aug 13, 2021
1 parent ae9c277 commit a74042c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@octokit/rest": "^18.5.2",
"@types/jest": "^26.0.22",
"@types/node": "^16.4.13",
"@types/node-fetch": "^2.5.12",
"@types/request": "^2.48.4",
"@typescript-eslint/eslint-plugin": "^4.29.1",
"@typescript-eslint/parser": "^4.29.1",
Expand All @@ -32,7 +33,7 @@
"typescript": "^4.3.4"
},
"dependencies": {
"request": "^2.88.2",
"node-fetch": "^2.6.1",
"yn": "^4.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Octokit } from '@octokit/rest';
import { PassThrough } from 'stream';
import request from 'request';
import fetch from 'node-fetch';
import { S3 } from 'aws-sdk';
import AWS from 'aws-sdk';
import yn from 'yn';
Expand Down Expand Up @@ -65,29 +65,32 @@ async function getLinuxReleaseAsset(

async function uploadToS3(s3: S3, cacheObject: CacheObject, actionRunnerReleaseAsset: ReleaseAsset): Promise<void> {
const writeStream = new PassThrough();
s3.upload({
Bucket: cacheObject.bucket,
Key: cacheObject.key,
Tagging: versionKey + '=' + actionRunnerReleaseAsset.name,
Body: writeStream,
}).promise();
const writePromise = s3
.upload({
Bucket: cacheObject.bucket,
Key: cacheObject.key,
Tagging: versionKey + '=' + actionRunnerReleaseAsset.name,
Body: writeStream,
})
.promise();

await new Promise<void>((resolve, reject) => {
console.debug('Start downloading %s and uploading to S3.', actionRunnerReleaseAsset.name);
request
.get(actionRunnerReleaseAsset.downloadUrl)
.pipe(writeStream)
.on('finish', () => {
console.info(`The new distribution is uploaded to S3.`);
resolve();
})
.on('error', (error) => {
reject(error);
});
}).catch((error) => {
console.error(`Exception: ${error}`);
throw error;
console.debug('Start downloading %s and uploading to S3.', actionRunnerReleaseAsset.name);
const readPromise = new Promise<void>((resolve, reject) => {
fetch(actionRunnerReleaseAsset.downloadUrl)
.then((res) =>
res.body
.pipe(writeStream)
.on('finish', () => resolve())
.on('error', (error) => reject(error)),
)
.catch((error) => reject(error));
});
await Promise.all([readPromise, writePromise])
.then(() => console.info(`The new distribution is uploaded to S3.`))
.catch((error) => {
console.error(`Uploading of the new distribution to S3 failed: ${error}`);
throw error;
});
}

export const handle = async (): Promise<void> => {
Expand Down
Loading

0 comments on commit a74042c

Please sign in to comment.