Skip to content

Commit

Permalink
Update Env.js
Browse files Browse the repository at this point in the history
解决Node.js环境下非UTF-8编码问题
如:Node.js环境下设置this.encoding = 'GBK'即可
  • Loading branch information
dukecd33369 authored Jul 26, 2021
1 parent 4a828a7 commit 34e5f4e
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,12 @@ function Env(name, opts) {
})
.then(
(resp) => {
if (this.encoding !== 'utf-8') {
const { statusCode: status, statusCode, headers, rawBody } = resp
callback(null, { status, statusCode, headers, rawBody }, iconv.decode(rawBody,this.encoding))
} else {
const { statusCode: status, statusCode, headers, body } = resp
callback(null, { status, statusCode, headers, body }, body)
}
const { statusCode: status, statusCode, headers, rawBody } = resp
callback(null, { status, statusCode, headers, rawBody }, iconv.decode(rawBody,this.encoding))
},
(err) => {
if (this.encoding !== 'utf-8') {
const { message: error, response: resp } = err
callback(error, resp, resp && iconv.decode(resp.rawBody,this.encoding))
} else {
const { message: error, response: resp } = err
callback(error, resp, resp && resp.body)
}
const { message: error, response: resp } = err
callback(error, resp, resp && iconv.decode(resp.rawBody,this.encoding))
}
)
}
Expand Down Expand Up @@ -369,22 +359,12 @@ function Env(name, opts) {
const { url, ..._opts } = opts
this.got[method](url, _opts).then(
(resp) => {
if (this.encoding !== 'utf-8') {
const { statusCode: status, statusCode, headers, rawBody } = resp
callback(null, { status, statusCode, headers, rawBody }, iconv.decode(rawBody,this.encoding))
} else {
const { statusCode: status, statusCode, headers, body } = resp
callback(null, { status, statusCode, headers, body }, body)
}
const { statusCode: status, statusCode, headers, rawBody } = resp
callback(null, { status, statusCode, headers, rawBody }, iconv.decode(rawBody,this.encoding))
},
(err) => {
if (this.encoding !== 'utf-8') {
const { message: error, response: resp } = err
callback(error, resp, resp && iconv.decode(resp.rawBody,this.encoding))
} else {
const { message: error, response: resp } = err
callback(error, resp, resp && resp.body)
}
const { message: error, response: resp } = err
callback(error, resp, resp && iconv.decode(resp.rawBody,this.encoding))
}
)
}
Expand Down

0 comments on commit 34e5f4e

Please sign in to comment.