refactor: convert Stream to Buffer using Array.push()

This commit is contained in:
curbengh 2019-10-06 06:32:35 +01:00
parent 2c5ad19932
commit 5de4a237b7
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
1 changed files with 12 additions and 20 deletions

View File

@ -115,19 +115,17 @@ function minifySvg () {
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => { return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Grab all assets using hexo router
const assetPath = route.get(path) const assetPath = route.get(path)
let assetTxt = '' const assetTxt = []
// Extract the content assetPath.on('data', (chunk) => (assetTxt.push(chunk)))
assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try { try {
const result = await new Svgo(options).optimize(assetTxt) const result = await new Svgo(options).optimize(assetTxt)
if (options.logger) verbose.call(this, assetTxt, result.data, path, 'svg') if (options.logger) verbose.call(this, assetTxt.toString(), result.data, path, 'svg')
resolve(route.set(path, result.data)) resolve(route.set(path, result.data))
} catch (err) { } catch (err) {
error(err) error.call(this, err)
reject(err) reject(err)
} }
} }
@ -151,17 +149,15 @@ function gzipFn () {
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => { return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Grab all assets using hexo router
const assetPath = route.get(path) const assetPath = route.get(path)
let assetTxt = '' const assetTxt = []
// Extract the content assetPath.on('data', (chunk) => (assetTxt.push(chunk)))
assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try { try {
// gzip compress using highest level const input = Buffer.from(assetTxt[0], 'utf-8')
const result = await gzip(assetTxt, { level: zlib.constants.Z_BEST_COMPRESSION }) const result = await gzip(input, { level: zlib.constants.Z_BEST_COMPRESSION })
if (options.logger) verbose.call(this, assetTxt, result.toString(), path, 'gzip') if (options.logger) verbose.call(this, input, result.toString(), path, 'gzip')
resolve(route.set(path + '.gz', result)) resolve(route.set(path + '.gz', result))
} catch (err) { } catch (err) {
error(err) error(err)
@ -188,17 +184,13 @@ function brotliFn () {
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => { return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Grab all assets using hexo router
const assetPath = route.get(path) const assetPath = route.get(path)
let assetTxt = '' const assetTxt = []
// Extract the content assetPath.on('data', (chunk) => (assetTxt.push(chunk)))
assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
// Input has to be buffer
const input = Buffer.from(assetTxt, 'utf-8')
try { try {
const input = Buffer.from(assetTxt[0], 'utf-8')
const result = await br(input) const result = await br(input)
if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli') if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli')
resolve(route.set(path + '.br', result)) resolve(route.set(path + '.br', result))