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