Skip to content

Commit

Permalink
chore: npm run dist nodejs archive caching (#5351)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefreak committed Nov 6, 2023
1 parent 3100bae commit 5fdf85e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .circleci/continue-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ commands:
- run:
name: NPM install
command: npm ci
# restore node cache
- restore_cache:
key: node-cache-v1
- run:
name: Run dist script
command: |
Expand All @@ -282,6 +285,11 @@ commands:
source "$HOME/.cargo/env"
npm run dist -- --version "<<parameters.version>>" <<parameters.targets>>
# We cache the node archives because the NodeJS download mirror is unreliable
- save_cache:
key: node-cache-v1
paths:
- garden-sea/tmp/node
- persist_to_workspace:
root: ./
paths:
Expand Down
53 changes: 32 additions & 21 deletions cli/src/build-pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ async function buildBinaries(args: string[]) {
await mkdirp(sourceTmpDir)

console.log(chalk.cyan("Creating temp node binary directory at " + nodeTmpDir))
await remove(nodeTmpDir)
await mkdirp(nodeTmpDir)

console.log(chalk.cyan("Creating static directory at " + tmpStaticDir))
Expand Down Expand Up @@ -315,37 +314,49 @@ async function buildBinaries(args: string[]) {
Object.entries(selected).map(async ([targetName, { spec }]) => {
const extractionDir = resolve(nodeTmpDir, targetName)

console.log(chalk.cyan(`Downloading node ${spec.node} for ${targetName} from ${spec.url}`))
const response = await fetch(spec.url)

if (!response.body) {
throw new Error(`No response body for ${spec.url}`)
}

const body = Readable.fromWeb(response.body)

// We know it's just those two file types, so we can hardcode this
// If we switch to other types, this needs adapting.
// Why aren't we just using `path.extname`?
// Because it doesn't do double endings like `.tar.gz`.
// Having generic code for that is still more than twice as much as this comment block and the ternary below.
const fileEnding = spec.url.endsWith("tar.gz") ? ".tar.gz" : ".zip"

const targetFileName = resolve(nodeTmpDir, `${targetName}${fileEnding}`)
const nodeArchiveFilename = resolve(nodeTmpDir, `${targetName}${fileEnding}`)

let nodeArchiveChecksum: string | undefined
if (await fsExtra.pathExists(nodeArchiveFilename)) {
const readStream = createReadStream(nodeArchiveFilename)
const hash = readStream.pipe(createHash("sha256"))
await finished(readStream)
nodeArchiveChecksum = hash.digest("hex")
}

if (nodeArchiveChecksum === spec.checksum) {
console.log(chalk.green(` ✓ Using cached node ${spec.node} for ${targetName} at ${nodeArchiveFilename}`))
} else {
console.log(chalk.cyan(`Downloading node ${spec.node} for ${targetName} from ${spec.url}`))
const response = await fetch(spec.url)

if (!response.body) {
throw new Error(`No response body for ${spec.url}`)
}

const body = Readable.fromWeb(response.body)

const hash = body.pipe(createHash("sha256"))
const hash = body.pipe(createHash("sha256"))

const writeStream = createWriteStream(targetFileName)
await finished(body.pipe(writeStream))
const writeStream = createWriteStream(nodeArchiveFilename)
await finished(body.pipe(writeStream))

console.log(chalk.green(` ✓ Downloaded node ${spec.node} for ${targetName}`))
console.log(chalk.green(` ✓ Downloaded node ${spec.node} for ${targetName}`))

const sha256 = hash.digest("hex")
const sha256 = hash.digest("hex")

if (sha256 !== spec.checksum) {
throw new Error(`Checksum mismatch for ${spec.url}! Expected ${spec.checksum} but got ${sha256}`)
if (sha256 !== spec.checksum) {
throw new Error(`Checksum mismatch for ${spec.url}! Expected ${spec.checksum} but got ${sha256}`)
}
console.log(chalk.green(` ✓ Verified checksum for ${targetName}`))
}
console.log(chalk.green(` ✓ Verified checksum for ${targetName}`))

console.log(chalk.cyan(`Extracting node ${spec.node} for ${targetName}`))
await mkdirp(extractionDir)
Expand All @@ -362,9 +373,9 @@ async function buildBinaries(args: string[]) {
return path.endsWith(`/bin/${nodeFileName}`)
},
})
await finished(createReadStream(targetFileName).pipe(extractStream))
await finished(createReadStream(nodeArchiveFilename).pipe(extractStream))
} else {
const zip = createReadStream(targetFileName).pipe(unzipper.Parse({ forceStream: true }))
const zip = createReadStream(nodeArchiveFilename).pipe(unzipper.Parse({ forceStream: true }))
for await (const i of zip) {
const entry = i as Entry
const fileName = entry.path
Expand Down

0 comments on commit 5fdf85e

Please sign in to comment.