Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs')
- const { promisify } = require('util')
- const { pipeline } = require('stream')
- const got = require('got')
- ;(async () => {
- const url = 'https://www.onlygfx.com/wp-content/uploads/2016/04/tree-2.png'
- const readStream = got.stream(url)
- const writeStream = fs.createWriteStream('./image.png')
- readStream.once('data', checkType)
- pipeline(
- readStream,
- writeStream,
- err => {
- console.error(err)
- readStream.off('data', checkType)
- }
- )
- function checkType(chunk) {
- const slice = chunk.slice(1, 4).toString('ascii')
- console.log('slice', slice)
- if (slice !== 'PNG') {
- const err = new Error('не PNG')
- return this.destroy(err)
- }
- }
- })()
Advertisement
Add Comment
Please, Sign In to add comment