encoding-option.js
This file
Path
lib/arguments/encoding-option.js
61 lines · 2.3 KiB · JavaScript
Last changed by
Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–19why, one step back1// Validate `encoding` option2export const validateEncoding = ({encoding}) => {3 if (ENCODINGS.has(encoding)) {4 return;5 }67 const correctEncoding = getCorrectEncoding(encoding);8 if (correctEncoding !== undefined) {9 throw new TypeError(`Invalid option \`encoding: ${serializeEncoding(encoding)}\`.10Please rename it to ${serializeEncoding(correctEncoding)}.`);11 }1213 const correctEncodings = [...ENCODINGS].map(correctEncoding => serializeEncoding(correctEncoding)).join(', ');14 throw new TypeError(`Invalid option \`encoding: ${serializeEncoding(encoding)}\`.15Please rename it to one of: ${correctEncodings}.`);16};1718const TEXT_ENCODINGS = new Set(['utf8', 'utf16le']);19export const BINARY_ENCODINGS = new Set(['buffer', 'hex', 'base64', 'base64url', 'latin1', 'ascii']);Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 20why, one step back20const ENCODINGS = TEXT_ENCODINGS.union(BINARY_ENCODINGS);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 21why, one step back21Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 22–31why, one step back22/*23Encoding to use when converting a transform's string to bytes.24Text encodings describe how text is represented as bytes, so those must be used, otherwise the string would be decoded with a different encoding than the one it was encoded with, which corrupts it.25Binary encodings instead describe how bytes are serialized as text, so the string's UTF-8 bytes are used. The same applies with the `binary` transform option, since the transform then deals with raw bytes.26*/27export const getStringEncoding = (binary, encoding) => binary || !TEXT_ENCODINGS.has(encoding) ? 'utf8' : encoding;2829// Text encodings only apply to the subprocess' output. Its input is always text encoded with UTF-8, so input transforms decode and encode strings with UTF-8 too, whichever text encoding the `encoding` option uses.30export const getInputEncoding = encoding => TEXT_ENCODINGS.has(encoding) ? 'utf8' : encoding;31100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 32–51why, one step back32const getCorrectEncoding = encoding => {33 if (encoding === null) {34 return 'buffer';35 }3637 if (typeof encoding !== 'string') {38 return;39 }4041 const lowerEncoding = encoding.toLowerCase();42 if (lowerEncoding in ENCODING_ALIASES) {43 return ENCODING_ALIASES[lowerEncoding];44 }4546 if (ENCODINGS.has(lowerEncoding)) {47 return lowerEncoding;48 }49};5051const ENCODING_ALIASES = {Harden some thingsSindre Sorhusbefore adoption4a0a121
line 52why, one step back52 __proto__: null,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 53–61why, one step back53 // eslint-disable-next-line unicorn/text-encoding-identifier-case54 'utf-8': 'utf8',55 'utf-16le': 'utf16le',56 'ucs-2': 'utf16le',57 ucs2: 'utf16le',58 binary: 'latin1',59};6061const serializeEncoding = encoding => typeof encoding === 'string' ? `"${encoding}"` : String(encoding);