Skip to content

Commit 92fa4e0

Browse files
richardlauMylesBorins
authored andcommitted
tools: make doctool work if no internet available
Allow doctool to fallback to use local files if not building a release build. PR-URL: #30214Fixes: #29918 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Jiawen Geng <[email protected]>
1 parent 3727a65 commit 92fa4e0

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

‎tools/doc/versions.js‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
'use strict';
22

3+
const{ readFileSync }=require('fs');
4+
constpath=require('path');
5+
constsrcRoot=path.join(__dirname,'..','..');
6+
37
let_versions;
48

9+
constisRelease=()=>{
10+
constre=/#defineNODE_VERSION_IS_RELEASE0/;
11+
constfile=path.join(srcRoot,'src','node_version.h');
12+
return!re.test(readFileSync(file,{encoding: 'utf8'}));
13+
};
14+
515
constgetUrl=(url)=>{
616
returnnewPromise((resolve,reject)=>{
717
consthttps=require('https');
8-
constrequest=https.get(url,(response)=>{
18+
constrequest=https.get(url,{timeout: 5000},(response)=>{
919
if(response.statusCode!==200){
1020
reject(newError(
1121
`Failed to get ${url}, status code ${response.statusCode}`));
1222
}
1323
response.setEncoding('utf8');
1424
letbody='';
25+
response.on('aborted',()=>reject());
1526
response.on('data',(data)=>body+=data);
1627
response.on('end',()=>resolve(body));
1728
});
1829
request.on('error',(err)=>reject(err));
30+
request.on('timeout',()=>request.abort());
1931
});
2032
};
2133

@@ -27,10 +39,23 @@ module.exports ={
2739

2840
// The CHANGELOG.md on release branches may not reference newer semver
2941
// majors of Node.js so fetch and parse the version from the master branch.
30-
constgithubContentUrl='https://raw.githubusercontent.com/nodejs/node/';
31-
constchangelog=awaitgetUrl(`${githubContentUrl}/master/CHANGELOG.md`);
42+
consturl=
43+
'https://raw.githubusercontent.com/nodejs/node/master/CHANGELOG.md';
44+
letchangelog;
45+
try{
46+
changelog=awaitgetUrl(url);
47+
}catch(e){
48+
// Fail if this is a release build, otherwise fallback to local files.
49+
if(isRelease()){
50+
throwe;
51+
}else{
52+
constfile=path.join(srcRoot,'CHANGELOG.md');
53+
console.warn(`Unable to retrieve ${url}. Falling back to ${file}.`);
54+
changelog=readFileSync(file,{encoding: 'utf8'});
55+
}
56+
}
3257
constltsRE=/LongTermSupport/i;
33-
constversionRE=/\*\[Node\.js([0-9.]+)\][^-]+[-]\s*(.*)\n/g;
58+
constversionRE=/\*\[Node\.js([0-9.]+)\][^-]+[-]\s*(.*)\r?\n/g;
3459
_versions=[];
3560
letmatch;
3661
while((match=versionRE.exec(changelog))!=null){

0 commit comments

Comments
(0)