Skip to content

Commit

Permalink
Download git without using Got
Browse files Browse the repository at this point in the history
  • Loading branch information
niik committed Aug 18, 2022
1 parent e228ede commit 64c9f55
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 225 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@
"homepage": "https://github.com/desktop/dugite#readme",
"dependencies": {
"checksum": "^0.1.1",
"got": "^9.6.0",
"mkdirp": "^0.5.1",
"progress": "^2.0.3",
"rimraf": "^2.5.4",
"tar": "^6.1.11"
},
"devDependencies": {
"@types/checksum": "^0.1.30",
"@types/got": "^9.6.0",
"@types/jest": "^28.1.7",
"@types/mkdirp": "^0.5.2",
"@types/node": "^11.9.0",
Expand Down
32 changes: 23 additions & 9 deletions script/download-git.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const fs = require('fs')

const got = require('got')
const ProgressBar = require('progress')
const mkdirp = require('mkdirp')
const checksum = require('checksum')
const rimraf = require('rimraf')
const tar = require('tar')
const http = require('https')
const https = require('https')

const config = require('./config')()

Expand All @@ -28,8 +29,10 @@ const unpackFile = function(file) {
})
}

const downloadAndUnpack = () => {
console.log(`Downloading Git from: ${config.source}`)
const downloadAndUnpack = (url, redirect) => {
if (!redirect) {
console.log(`Downloading Git from: ${config.source}`)
}

const options = {
headers: {
Expand All @@ -39,11 +42,15 @@ const downloadAndUnpack = () => {
secureProtocol: 'TLSv1_2_method'
}

const client = got.stream(config.source, options)
const client = url.startsWith('https:') ? https : config.startsWith('http:') ? http : null

if (!client) {
throw new Error(`Invalid protocol for ${config.source}`)
}

client.pipe(fs.createWriteStream(config.tempFile))
const req = client.get(url, options)

client.on('error', function(error) {
req.on('error', function(error) {
if (error.code === 'ETIMEDOUT') {
console.log(
`A timeout has occurred while downloading '${config.source}' - check ` +
Expand All @@ -57,7 +64,12 @@ const downloadAndUnpack = () => {
process.exit(1)
})

client.on('response', function(res) {
req.on('response', function(res) {
if ([301, 302].includes(res.statusCode) && res.headers['location']) {
downloadAndUnpack(res.headers.location, true)
return
}

if (res.statusCode !== 200) {
console.log(`Non-200 response returned from ${config.source} - (${res.statusCode})`)
process.exit(1)
Expand All @@ -73,6 +85,8 @@ const downloadAndUnpack = () => {
total: len
})

res.pipe(fs.createWriteStream(config.tempFile))

res.on('data', function(chunk) {
bar.tick(chunk.length)
})
Expand Down Expand Up @@ -123,11 +137,11 @@ mkdirp(config.outputPath, function(error) {
unpackFile(tempFile)
} else {
rimraf.sync(tempFile)
downloadAndUnpack()
downloadAndUnpack(config.source)
}
})
return
}

downloadAndUnpack()
downloadAndUnpack(config.source)
})
Loading

0 comments on commit 64c9f55

Please sign in to comment.