Skip to content

Commit 92fa4e0

Browse files
richardlauMylesBorins
authored andcommittedNov 17, 2019
tools: make doctool work if no internet available
Allow doctool to fallback to use local files if not building a release build. PR-URL: #30214 Fixes: #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
 
File renamed without changes.

‎tools/doc/versions.js

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
'use strict';
22

3+
const { readFileSync } = require('fs');
4+
const path = require('path');
5+
const srcRoot = path.join(__dirname, '..', '..');
6+
37
let _versions;
48

9+
const isRelease = () => {
10+
const re = /#define NODE_VERSION_IS_RELEASE 0/;
11+
const file = path.join(srcRoot, 'src', 'node_version.h');
12+
return !re.test(readFileSync(file, { encoding: 'utf8' }));
13+
};
14+
515
const getUrl = (url) => {
616
return new Promise((resolve, reject) => {
717
const https = require('https');
8-
const request = https.get(url, (response) => {
18+
const request = https.get(url, { timeout: 5000 }, (response) => {
919
if (response.statusCode !== 200) {
1020
reject(new Error(
1121
`Failed to get ${url}, status code ${response.statusCode}`));
1222
}
1323
response.setEncoding('utf8');
1424
let body = '';
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-
const githubContentUrl = 'https://raw.githubusercontent.com/nodejs/node/';
31-
const changelog = await getUrl(`${githubContentUrl}/master/CHANGELOG.md`);
42+
const url =
43+
'https://raw.githubusercontent.com/nodejs/node/master/CHANGELOG.md';
44+
let changelog;
45+
try {
46+
changelog = await getUrl(url);
47+
} catch (e) {
48+
// Fail if this is a release build, otherwise fallback to local files.
49+
if (isRelease()) {
50+
throw e;
51+
} else {
52+
const file = 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
const ltsRE = /Long Term Support/i;
33-
const versionRE = /\* \[Node\.js ([0-9.]+)\][^-]+[-]\s*(.*)\n/g;
58+
const versionRE = /\* \[Node\.js ([0-9.]+)\][^-]+[-]\s*(.*)\r?\n/g;
3459
_versions = [];
3560
let match;
3661
while ((match = versionRE.exec(changelog)) != null) {

0 commit comments

Comments
 (0)
Please sign in to comment.