Skip to content

Commit ef25845

Browse files
committed
chore: use Fetch instead of HTTP module
1 parent 844c955 commit ef25845

1 file changed

Lines changed: 6 additions & 37 deletions

File tree

updateLib.js

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
'use strict';
2-
const https = require('https');
32
const path = require('path');
43
const { readFileSync, writeFileSync } = require('fs');
54
const { getAllDockerfiles, getDockerfileNodeVersion } = require('./utils');
65

7-
const releaseUrl = 'https://nodejs.org/dist/index.json';
8-
96
const templates = Object.freeze({
107
alpine: 1,
118
debian: 2,
@@ -24,36 +21,6 @@ const templateRepoMap = Object.freeze({
2421
[templates.debianSlim]: 'debian',
2522
});
2623

27-
const fetchText = (url) => new Promise((resolve, reject) => {
28-
https.get(url, (res) => {
29-
const { statusCode } = res;
30-
31-
if (statusCode < 200 || statusCode >= 300) {
32-
// Consume response data to free up memory
33-
res.resume();
34-
reject(new Error(`Request Failed.\nStatus Code: ${statusCode}`));
35-
return;
36-
}
37-
38-
res.setEncoding('utf8');
39-
let rawData = '';
40-
res.on('data', (chunk) => {
41-
rawData += chunk;
42-
});
43-
44-
res.on('end', () => {
45-
resolve(rawData);
46-
});
47-
}).on('error', (e) => {
48-
reject(e);
49-
}).end();
50-
});
51-
52-
const fetchJson = async (url) => {
53-
const text = await fetchText(url);
54-
return JSON.parse(text);
55-
};
56-
5724
// nodeVersions is sorted
5825
const getLatestNodeVersion = (nodeVersions, majorVersion) => nodeVersions
5926
.find((version) => version.startsWith(`${majorVersion}.`));
@@ -88,8 +55,9 @@ const isDockerfileOutdated = ({ fileNodeVersion, latestVersion }) => fileNodeVer
8855
!== latestVersion;
8956

9057
const fetchLatestNodeVersions = async () => {
91-
const nodeDist = await fetchJson(releaseUrl);
92-
return nodeDist.map(({ version }) => version.substring(1));
58+
const nodeDist = await fetch('https://nodejs.org/dist/index.json');
59+
const content = await nodeDist.json();
60+
return content.map(({ version }) => version.substring(1));
9361
};
9462

9563
const findOutdated = async (updateAll) => {
@@ -140,10 +108,11 @@ const formatTemplate = (nodeKeys, muslChecksum, base, metadata) => {
140108
};
141109

142110
const fetchMuslChecksum = async (nodeVersion) => {
143-
const checksums = await fetchText(
111+
const checksums = await fetch(
144112
`https://unofficial-builds.nodejs.org/download/release/v${nodeVersion}/SHASUMS256.txt`,
145113
);
146-
return checksums.match(/(\S+)\s+\S+-linux-x64-musl.tar.xz/m)[1];
114+
const content = await checksums.text();
115+
return await content.match(/(\S+)\s+\S+-linux-x64-musl.tar.xz/m)[1];
147116
};
148117

149118
const updateDockerfile = async (nodeKeys, metadata) => {

0 commit comments

Comments
 (0)