Skip to content

Commit

Permalink
added zlib.constants.Z_BEST_SPEED to createGzip() method
Browse files Browse the repository at this point in the history
  • Loading branch information
kyostiebi committed Oct 6, 2023
1 parent da8a4a4 commit 8d0b7c9
Showing 1 changed file with 7 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createHash } from 'node:crypto'
import { createWriteStream } from 'node:fs'
import { mkdir, mkdtemp, rename, rmdir } from 'node:fs/promises'
import { join } from 'node:path'
import { Transform, finished } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import { createGzip } from 'node:zlib'

Expand Down Expand Up @@ -41,40 +40,17 @@ export class FileStorageEngine implements StorageEngine {
this.logger.debug(`User uploaded file: ${file.originalname}`)

const hash = createHash('md5')
// file.stream.on('data', (chunk) => {
// hash.update(chunk, 'utf8')
// return chunk
// })
const hashUpdater = new Transform({
transform: (chunk, encoding, callback) => {
hash.update(chunk, encoding)
callback(null, chunk)
},
file.stream.on('data', (chunk) => {
hash.update(chunk, 'utf8')
return chunk
})

// eslint-disable-next-line @typescript-eslint/no-var-requires
const zlib = require('node:zlib')
// Check md5 checksum of saved file
const fileWriteStream = createWriteStream(tempFullFileName)
const gz = createGzip()

// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-node-protocol
const zlib = require('zlib')
this.logger.debug('*** Pipeline starts here...')
await pipeline(
file.stream,
new Transform({
transform: (chunk, encoding, callback) => {
hash.update(chunk, encoding)
callback(null, chunk)
},
}),
// createGzip(), // If this is commented then pipeline is processed
zlib.createGzip({ level: zlib.constants.Z_BEST_SPEED }),
fileWriteStream,
)

this.logger.debug('*** Pipeline ended ...')

// await pipeline(file.stream, hashUpdater, gz, fileWriteStream)
const gz = createGzip({ level: zlib.constants.Z_BEST_SPEED }) // BEST_SPEED option must be used in order to process also FASTA files > 1 GB (in Node 18 onwards)
await pipeline(file.stream, gz, fileWriteStream)
this.logger.debug(`Compressed file: ${tempFullFileName}`)
const fileChecksum = hash.digest('hex')
this.logger.debug(`Uploaded file checksum: ${fileChecksum}`)
Expand Down

0 comments on commit 8d0b7c9

Please sign in to comment.