Skip to content

Instantly share code, notes, and snippets.

@ashnel3
Last active March 9, 2023 19:52
Show Gist options
  • Save ashnel3/e7f8cd8967ed3ab02e324cfe4c7c1001 to your computer and use it in GitHub Desktop.
Save ashnel3/e7f8cd8967ed3ab02e324cfe4c7c1001 to your computer and use it in GitHub Desktop.
// ================================
// Fetch stream to file
// ================================
import { Readable } from 'stream'
import { createWriteStream } from 'fs'
import { finished } from 'stream/promises'
const downloadFile = async (path, url) => {
const res = await fetch(url)
const body = Readable.fromWeb(res.body)
const stream = createWriteStream(path)
await finished(body.pipe(stream))
}
// ================================
// Stream to hash
// ================================
import { createReadStream } from 'fs'
import { createHash } from 'crypto'
import { pipeline } from 'stream/promises'
const shasum = async (path) => {
const stream = createReadStream(path)
const hash = createHash('sha1')
await pipeline(stream, hash)
return hash.end().digest('hex')
}
// ================================
// Stream to tar extract
// ================================
import { finished } from 'stream/promises'
import tar from 'tar'
const tarExtract = async (path, out, opts = {}) => {
const stream = createReadStream(path)
await finished(
stream.pipe(tar.x({ C: out, ...opts }))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment