Skip to content

Commit

Permalink
bring back v2
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Jun 16, 2024
1 parent e6b0713 commit 0957cb9
Showing 1 changed file with 63 additions and 60 deletions.
123 changes: 63 additions & 60 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,66 +96,69 @@ app.get('/install.sh', (req, res) => {
})
})

// app.get('/v2/:os/:arch(*)', async (req, res) => {
// const os = req.params.os.toLowerCase()
// let arch = req.params.arch.toLowerCase()
// let version = req.query.version
//
// // remove any extension from the arch parameter
// arch = arch.replace(/\.[^/.]+$/, '')
//
// // check if version is provided
// if (version) {
// if (version.startsWith('v')) {
// version = version.replace(/^v/, '')
// }
// } else {
// version = VERSION
// }
//
// const repo = `dotenvx-${os}-${arch}`
// const filename = `${repo}-${version}.tgz`
// const registryUrl = `https://registry.npmjs.org/@dotenvx/${repo}/-/${filename}`
//
// try {
// const response = await axios.get(registryUrl, { responseType: 'stream' })
// const tmpDir = tmp.dirSync({ unsafeCleanup: true }).name
// const tmpTarPath = path.join(tmpDir, filename)
//
// // extract the downloaded tarball to the temporary directory
// response.data.pipe(tar.x({
// cwd: tmpDir,
// strip: 1, // Strip the 'package' folder
// filter: path => path.startsWith('package/dotenvx') // Only extract files within the 'package' folder
// })).on('finish', () => {
// // permissions
// const dotenvxBinaryPath = path.join(tmpDir, 'dotenvx')
// fs.chmodSync(dotenvxBinaryPath, 0o755)
//
// // new tarball
// execSync(`tar -czf ${tmpTarPath} -C ${tmpDir} .`)
//
// // size of tarball
// const stat = fs.statSync(tmpTarPath)
// const tarballSize = stat.size
//
// // set the response headers
// res.setHeader('Content-Type', 'application/gzip')
// res.setHeader('Content-Length', tarballSize)
//
// // stream the tarball file to the response
// const readStream = fs.createReadStream(tmpTarPath)
// readStream.pipe(res).on('finish', () => {
// // Cleanup the temporary directory
// tmp.setGracefulCleanup()
// })
// }).on('error', error => {
// res.status(500).send('Error occurred while extracting the file: ' + error.message)
// })
// } catch (error) {
// res.status(500).send('Error occurred while fetching the file: ' + error.message)
// }
// })
app.get('/v2/:os/:arch(*)', async (req, res) => {
const os = req.params.os.toLowerCase()
let arch = req.params.arch.toLowerCase()
let version = req.query.version

// remove any extension from the arch parameter
arch = arch.replace(/\.[^/.]+$/, '')

// check if version is provided
if (version) {
if (version.startsWith('v')) {
version = version.replace(/^v/, '')
}
} else {
version = VERSION
}

const repo = `dotenvx-${os}-${arch}`
const filename = `${repo}-${version}.tgz`
const registryUrl = `https://registry.npmjs.org/@dotenvx/${repo}/-/${filename}`

try {
const response = await axios.get(registryUrl, { responseType: 'stream' })
const tmpDir = tmp.dirSync({ unsafeCleanup: true }).name
const tmpTarPath = path.join(tmpDir, filename)

// extract the downloaded tarball to the temporary directory
await new Promise((resolve, reject) => {
response.data.pipe(tar.x({
cwd: tmpDir,
strip: 1, // Strip the 'package' folder
filter: path => path.startsWith('package/dotenvx') // Only extract files within the 'package' folder
}))
.on('finish', resolve)
.on('error', reject)
})

// path to extracted binary
const dotenvxBinaryPath = path.join(tmpDir, 'dotenvx')

// permissions
fs.chmodSync(dotenvxBinaryPath, 0o755)

// tar it up
execSync(`tar -czf ${tmpTarPath} -C ${tmpDir} .`)

// get size for content-length
const stat = fs.statSync(tmpTarPath)

// set headers
res.setHeader('Content-Type', 'application/gzip')
res.setHeader('Content-Length', stat.size)

// stream the tarball file to the response
const readStream = fs.createReadStream(tmpTarPath)
readStream.pipe(res).on('finish', () => {
// heroku takes care of cleanup
// tmp.setGracefulCleanup()
})
} catch (error) {
res.status(500).send('Error occurred while fetching the file: ' + error.message)
}
})

app.get('/:os/:arch', async (req, res) => {
const os = req.params.os.toLowerCase()
Expand Down

0 comments on commit 0957cb9

Please sign in to comment.