Sunday, 17 May 2020

How to detect the encoding type from the stream with node-fetch?

With node-fetch, json() & text() default to decode with UTF-8. Also, we can use buffer() to decode by ourselves. How to detect the stream encoding type? So we can choose the type before decoding. ``` /** * Decode response as json * * @return Promise */ json() { return consumeBody.call(this).then(buffer => JSON.parse(buffer.toString())); },/** * Decode response as text * * @return Promise */ text() { return consumeBody.call(this).then(buffer => buffer.toString()); }, /** * Decode response as buffer (non-spec api) * * @return Promise */ buffer() { return consumeBody.call(this); } ```

Submitted May 18, 2020 at 06:56AM by licaomeng

No comments:

Post a Comment