encoding.js
This file
Path
lib/encoding.js
19 lines · 961 bytes · JavaScript
Last changed by
Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf
History of this fileTake it
Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf
lines 1–19why, one step back1import {Buffer} from 'node:buffer';23export const isSimpleEncoding = encoding => isBufferEncoding(encoding) || DEFAULT_ENCODING.has(encoding.toLowerCase());45// eslint-disable-next-line unicorn/text-encoding-identifier-case6const DEFAULT_ENCODING = new Set(['utf8', 'utf-8']);78export const isBufferEncoding = encoding => encoding === null || encoding.toLowerCase() === 'buffer';910export const isBinaryEncoding = encoding => isBufferEncoding(encoding) || BINARY_ENCODINGS.has(encoding.toLowerCase());1112const BINARY_ENCODINGS = new Set(['hex', 'base64', 'base64url', 'latin1', 'binary', 'ascii']);1314export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);1516export const isUint8Array = value => Object.prototype.toString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);1718const textDecoder = new TextDecoder();19export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);