From 339abf78498099979448cc63d1a7fc3d3de45389 Mon Sep 17 00:00:00 2001
From: Spencer <email@spalger.com>
Date: Wed, 23 Oct 2019 16:38:42 -0700
Subject: [PATCH] [7.x] download node from our proxy-cache (#49081) (#49125)

* download node from our proxy-cache

* download node with axios to support redirects

* fix test assertion now that we're using axios
---
 .../build/tasks/nodejs/__tests__/download.js    |  2 +-
 src/dev/build/tasks/nodejs/download.js          | 17 ++++++++++-------
 .../build/tasks/nodejs/node_download_info.js    |  2 +-
 src/dev/build/tasks/nodejs/node_shasums.test.ts |  4 +++-
 src/dev/build/tasks/nodejs/node_shasums.ts      |  2 +-
 src/dev/ci_setup/setup_env.sh                   |  4 ++--
 6 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/src/dev/build/tasks/nodejs/__tests__/download.js b/src/dev/build/tasks/nodejs/__tests__/download.js
index 551773a6f6325..c76ff15b89289 100644
--- a/src/dev/build/tasks/nodejs/__tests__/download.js
+++ b/src/dev/build/tasks/nodejs/__tests__/download.js
@@ -196,7 +196,7 @@ describe('src/dev/build/tasks/nodejs/download', () => {
       } catch (error) {
         expect(error)
           .to.have.property('message')
-          .contain('Unexpected status code 500');
+          .contain('Request failed with status code 500');
         expect(reqCount).to.be(6);
       }
     });
diff --git a/src/dev/build/tasks/nodejs/download.js b/src/dev/build/tasks/nodejs/download.js
index 48313c0911c24..0a030aced0d42 100644
--- a/src/dev/build/tasks/nodejs/download.js
+++ b/src/dev/build/tasks/nodejs/download.js
@@ -22,7 +22,7 @@ import { dirname } from 'path';
 
 import chalk from 'chalk';
 import { createHash } from 'crypto';
-import wreck from '@hapi/wreck';
+import Axios from 'axios';
 
 import { mkdirp } from '../../lib';
 
@@ -51,21 +51,24 @@ export async function download(options) {
   try {
     log.debug(`Attempting download of ${url}`, chalk.dim(sha256));
 
-    const response = await wreck.request('GET', url);
+    const response = await Axios.request({
+      url: url,
+      responseType: 'stream'
+    });
 
-    if (response.statusCode !== 200) {
-      throw new Error(`Unexpected status code ${response.statusCode} when downloading ${url}`);
+    if (response.status !== 200) {
+      throw new Error(`Unexpected status code ${response.status} when downloading ${url}`);
     }
 
     const hash = createHash('sha256');
     await new Promise((resolve, reject) => {
-      response.on('data', chunk => {
+      response.data.on('data', chunk => {
         hash.update(chunk);
         writeSync(fileHandle, chunk);
       });
 
-      response.on('error', reject);
-      response.on('end', resolve);
+      response.data.on('error', reject);
+      response.data.on('end', resolve);
     });
 
     const downloadedSha256 = hash.digest('hex');
diff --git a/src/dev/build/tasks/nodejs/node_download_info.js b/src/dev/build/tasks/nodejs/node_download_info.js
index 7c4fd5fde7bed..33ffd042d85a3 100644
--- a/src/dev/build/tasks/nodejs/node_download_info.js
+++ b/src/dev/build/tasks/nodejs/node_download_info.js
@@ -27,7 +27,7 @@ export function getNodeDownloadInfo(config, platform) {
     ? 'win-x64/node.exe'
     : `node-v${version}-${arch}.tar.gz`;
 
-  const url = `https://nodejs.org/dist/v${version}/${downloadName}`;
+  const url = `https://us-central1-elastic-kibana-184716.cloudfunctions.net/kibana-ci-proxy-cache/dist/v${version}/${downloadName}`;
   const downloadPath = config.resolveFromRepo('.node_binaries', version, basename(downloadName));
   const extractDir = config.resolveFromRepo('.node_binaries', version, arch);
 
diff --git a/src/dev/build/tasks/nodejs/node_shasums.test.ts b/src/dev/build/tasks/nodejs/node_shasums.test.ts
index ee91d2a370fb1..08ac823c7ebf0 100644
--- a/src/dev/build/tasks/nodejs/node_shasums.test.ts
+++ b/src/dev/build/tasks/nodejs/node_shasums.test.ts
@@ -60,7 +60,9 @@ c4edece2c0aa68e816c4e067f397eb12e9d0c81bb37b3d349dbaf47cf246b0b7  win-x86/node.l
 
 jest.mock('axios', () => ({
   async get(url: string) {
-    expect(url).toBe('https://nodejs.org/dist/v8.9.4/SHASUMS256.txt');
+    expect(url).toBe(
+      'https://us-central1-elastic-kibana-184716.cloudfunctions.net/kibana-ci-proxy-cache/dist/v8.9.4/SHASUMS256.txt'
+    );
     return {
       status: 200,
       data: mockResponse,
diff --git a/src/dev/build/tasks/nodejs/node_shasums.ts b/src/dev/build/tasks/nodejs/node_shasums.ts
index 1b8d01a9b1d94..e0926aa3e49e4 100644
--- a/src/dev/build/tasks/nodejs/node_shasums.ts
+++ b/src/dev/build/tasks/nodejs/node_shasums.ts
@@ -20,7 +20,7 @@
 import axios from 'axios';
 
 export async function getNodeShasums(nodeVersion: string) {
-  const url = `https://nodejs.org/dist/v${nodeVersion}/SHASUMS256.txt`;
+  const url = `https://us-central1-elastic-kibana-184716.cloudfunctions.net/kibana-ci-proxy-cache/dist/v${nodeVersion}/SHASUMS256.txt`;
 
   const { status, data } = await axios.get(url);
 
diff --git a/src/dev/ci_setup/setup_env.sh b/src/dev/ci_setup/setup_env.sh
index b9fe8fe77d122..51c21dc45cdd1 100644
--- a/src/dev/ci_setup/setup_env.sh
+++ b/src/dev/ci_setup/setup_env.sh
@@ -53,10 +53,10 @@ nodeDir="$cacheDir/node/$nodeVersion"
 
 if [[ "$OS" == "win" ]]; then
   nodeBin="$HOME/node"
-  nodeUrl="https://nodejs.org/dist/v$nodeVersion/node-v$nodeVersion-win-x64.zip"
+  nodeUrl="https://us-central1-elastic-kibana-184716.cloudfunctions.net/kibana-ci-proxy-cache/dist/v$nodeVersion/node-v$nodeVersion-win-x64.zip"
 else
   nodeBin="$nodeDir/bin"
-  nodeUrl="https://nodejs.org/dist/v$nodeVersion/node-v$nodeVersion-linux-x64.tar.gz"
+  nodeUrl="https://us-central1-elastic-kibana-184716.cloudfunctions.net/kibana-ci-proxy-cache/dist/v$nodeVersion/node-v$nodeVersion-linux-x64.tar.gz"
 fi
 
 if [[ "$installNode" == "true" ]]; then