xxxxxxxxxx
streams is process(read and write) data piece(chunks) without completing the
whole read or write operation, and therefore without keeping all the data in
memory.
There are four types of streams => readable streams, writable streams, duplex
and transform streams.
xxxxxxxxxx
const { Readable } = require("stream")
const readable = Readable.from(["input string"])
readable.on("data", (chunk) => {
console.log(chunk) // will be called once with `"input string"`
})
xxxxxxxxxx
import { WriteStream, createWriteStream } from 'fs'
import path from 'path'
import { Stream } from 'stream'
import consola from 'consola'
import { ConfigsEnvironment } from '~/configs/config.env'
import { IUser } from '~/interfaces/interface.users'
export const fileStream = (chunk: Buffer, user: IUser): void => {
const now: Date = new Date()
const year: number = now.getFullYear()
const month: string = ('0' + (now.getMonth() + 1)).slice(-2)
const day: string = ('0' + now.getDate()).slice(-2)
const hour: string = ('0' + now.getHours()).slice(-2)
const minute: string = ('0' + now.getMinutes()).slice(-2)
const second: string = ('0' + now.getSeconds()).slice(-2)
const milliseconds: number = now.getMilliseconds()
const fileName: string = `img_web_${user.userId}_${year}${month}${day}_1707${hour}${minute}${second}${milliseconds}.jpg`
const fileWriteStream: WriteStream = createWriteStream(path.resolve(ConfigsEnvironment.STORAGE_DIR, fileName))
const fileStream: Stream = new Stream()
fileStream.pipe<NodeJS.WritableStream> = (dest: NodeJS.WritableStream) => {
dest.write(chunk)
return dest
}
fileStream
.pipe(fileWriteStream)
.on('pipe', () => {
consola.info('File process to recreate')
})
.on('error', (err: Error) => {
consola.error('File error to recreate')
Promise.reject(err)
})
.on('finish', () => {
console.info('File finish to recreate')
})
}
xxxxxxxxxx
const fs = require('fs');
const r = fs.createReadStream('file.txt');
const z = zlib.createGzip();
const w = fs.createWriteStream('file.txt.gz');
r.pipe(z).pipe(w);
xxxxxxxxxx
import { Stream } from 'stream'
import { WriteStream, createWriteStream } from 'fs'
import path from 'path'
import { ConfigsEnvironment } from '~/configs/config.env'
import { IUser } from '~/interfaces/interface.users'
export const fileStream = (chunk: Buffer, user: IUser): Promise<string> => {
return new Promise((resolve: (value: string | PromiseLike<string>) => void, reject: (reason?: any) => void) => {
const now: Date = new Date()
const year: number = now.getFullYear()
const month: string = ('0' + (now.getMonth() + 1)).slice(-2)
const day: string = ('0' + now.getDate()).slice(-2)
const hour: string = ('0' + now.getHours()).slice(-2)
const minute: string = ('0' + now.getMinutes()).slice(-2)
const second: string = ('0' + now.getSeconds()).slice(-2)
const milliseconds: number = now.getMilliseconds()
const fileName: string = `img_web_${user.userId}_${year}${month}${day}_1707${hour}${minute}${second}${milliseconds}.jpg`
const writeStream: WriteStream = createWriteStream(path.resolve(ConfigsEnvironment.STORAGE_DIR, fileName))
const stream: Stream = new Stream()
stream.pipe<NodeJS.WritableStream> = (destination: NodeJS.WritableStream) => {
destination.write(chunk)
return destination
}
stream.pipe(writeStream).on('error', reject).end()
writeStream.on('error', reject).end(() => resolve(fileName))
})
}