Skip to content

Commit

Permalink
display content-length
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Jun 16, 2024
1 parent de07d7c commit c9e6141
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const path = require('path')
const tar = require('tar')
const tmp = require('tmp')
const fs = require('fs')
const { pipeline } = require('stream')
const { promisify } = require('util')
const streamPipeline = promisify(pipeline)
const { execSync } = require('child_process')
const app = express()

const PORT = process.env.PORT || 3000
Expand Down Expand Up @@ -85,7 +83,7 @@ app.get('/install.sh', (req, res) => {
})
})

app.get('/v2/:os/:arch(*)', async (req, res) => {
app.get('/:os/:arch(*)', async (req, res) => {
const os = req.params.os.toLowerCase()
let arch = req.params.arch.toLowerCase()
let version = req.query.version
Expand All @@ -109,34 +107,44 @@ app.get('/v2/:os/:arch(*)', async (req, res) => {
try {
const response = await axios.get(registryUrl, { responseType: 'stream' })
const tmpDir = tmp.dirSync({ unsafeCleanup: true }).name

await streamPipeline(
response.data,
tar.x({
cwd: tmpDir,
strip: 1, // Strip the 'package' folder
filter: (path) => path.startsWith('package/dotenvx') // Only extract files within the 'package' folder
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()
})
)

const tarStream = tar.c(
{
gzip: true,
cwd: tmpDir
},
['.']
)

res.setHeader('Content-Type', 'application/gzip')
await streamPipeline(tarStream, res)

fs.rmSync(tmpDir, { recursive: true, force: true })
}).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('/:os/:arch', async (req, res) => {
app.get('/deprecated/:os/:arch', async (req, res) => {
const os = req.params.os.toLowerCase()
let arch = req.params.arch.toLowerCase()
let version = req.query.version
Expand Down

0 comments on commit c9e6141

Please sign in to comment.