From 5e8b2a8190c01774494d6676dc177e9458e00e72 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Sat, 2 Nov 2019 03:48:47 -0400 Subject: [PATCH] tools: make doctool work if no internet available Allow doctool to fallback to use local files if not building a release build. PR-URL: https://github.com/nodejs/node/pull/30214 Fixes: https://github.com/nodejs/node/issues/29918 Reviewed-By: Rich Trott Reviewed-By: Gus Caplan Reviewed-By: Chengzhong Wu Reviewed-By: Joyee Cheung Reviewed-By: Ruben Bridgewater Reviewed-By: Jiawen Geng --- .../test-doctool-versions.js | 0 tools/doc/versions.js | 33 ++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) rename test/{internet => doctool}/test-doctool-versions.js (100%) diff --git a/test/internet/test-doctool-versions.js b/test/doctool/test-doctool-versions.js similarity index 100% rename from test/internet/test-doctool-versions.js rename to test/doctool/test-doctool-versions.js diff --git a/tools/doc/versions.js b/tools/doc/versions.js index 854329bd9a8a02..7a4e2c3ff76b1a 100644 --- a/tools/doc/versions.js +++ b/tools/doc/versions.js @@ -1,21 +1,33 @@ 'use strict'; +const { readFileSync } = require('fs'); +const path = require('path'); +const srcRoot = path.join(__dirname, '..', '..'); + let _versions; +const isRelease = () => { + const re = /#define NODE_VERSION_IS_RELEASE 0/; + const file = path.join(srcRoot, 'src', 'node_version.h'); + return !re.test(readFileSync(file, { encoding: 'utf8' })); +}; + const getUrl = (url) => { return new Promise((resolve, reject) => { const https = require('https'); - const request = https.get(url, (response) => { + const request = https.get(url, { timeout: 5000 }, (response) => { if (response.statusCode !== 200) { reject(new Error( `Failed to get ${url}, status code ${response.statusCode}`)); } response.setEncoding('utf8'); let body = ''; + response.on('aborted', () => reject()); response.on('data', (data) => body += data); response.on('end', () => resolve(body)); }); request.on('error', (err) => reject(err)); + request.on('timeout', () => request.abort()); }); }; @@ -27,10 +39,23 @@ module.exports = { // The CHANGELOG.md on release branches may not reference newer semver // majors of Node.js so fetch and parse the version from the master branch. - const githubContentUrl = 'https://raw.githubusercontent.com/nodejs/node/'; - const changelog = await getUrl(`${githubContentUrl}/master/CHANGELOG.md`); + const url = + 'https://raw.githubusercontent.com/nodejs/node/master/CHANGELOG.md'; + let changelog; + try { + changelog = await getUrl(url); + } catch (e) { + // Fail if this is a release build, otherwise fallback to local files. + if (isRelease()) { + throw e; + } else { + const file = path.join(srcRoot, 'CHANGELOG.md'); + console.warn(`Unable to retrieve ${url}. Falling back to ${file}.`); + changelog = readFileSync(file, { encoding: 'utf8' }); + } + } const ltsRE = /Long Term Support/i; - const versionRE = /\* \[Node\.js ([0-9.]+)\][^-—]+[-—]\s*(.*)\n/g; + const versionRE = /\* \[Node\.js ([0-9.]+)\][^-—]+[-—]\s*(.*)\r?\n/g; _versions = []; let match; while ((match = versionRE.exec(changelog)) != null) {