Last active
March 9, 2023 19:52
-
-
Save ashnel3/e7f8cd8967ed3ab02e324cfe4c7c1001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================ | |
// 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