message: custom encoding + iso fallback
diff: @@ -6,6 +6,18 @@ import CharacterSetECI from '../common/CharacterSetECI';
  */
  export default class StringEncoding {
  + /**
  + * Allows the user to set a custom decoding function
  + * so more encoding formats the native ones can be supported.
  + */
  + public static customDecoder: (bytes: Uint8Array, encodingName: string) => string;
  +
  + /**
  + * Allows the user to set a custom encoding function
  + * so more encoding formats the native ones can be supported.
  + */
  + public static customEncoder: (s: string, encodingName: string) => Uint8Array;
  +
  /**
  * Decodes some Uint8Array to a string format.
  */
  @@ -13,8 +25,12 @@ export default class StringEncoding {
  const encodingName = this.encodingName(encoding);
  + if (this.customDecoder) {
  + return this.customDecoder(bytes, encodingName);
  + }
  +
  // Increases browser support.
  - if (typeof TextDecoder === 'undefined') {
  + if (typeof TextDecoder === 'undefined' || this.shouldDecodeOnFallback(encodingName)) {
  return this.decodeFallback(bytes, encodingName);
  }
  @@ -22,19 +38,24 @@ export default class StringEncoding {
  }
  /**
  - * Encodes some string into a Uint8Array.
  + * Checks if the decoding method should use the fallback for decoding
  + * once Node TextDecoder doesn't support all encoding formats.
  *
  - * @todo natively support other string formats than UTF-8.
  + * @param encodingName
  + */
  + private static shouldDecodeOnFallback(encodingName: string): boolean {
  + return !StringEncoding.isBrowser() && encodingName === 'ISO-8859-1';
  + }
  +
  + /**
  + * Encodes some string into a Uint8Array.
  */
  public static encode(s: string, encoding: string | CharacterSetECI): Uint8Array {
  - // Uses `text-encoding` package.
  - if (!StringEncoding.isBrowser()) {
  - // SEE: https://nodejs.org/api/buffer.html#buffer_class_buffer
  - // SEE: https://github.com/polygonplanet/encoding.js/
  - // SEE: https://stackoverflow.com/questions/17191945/conversion-between-utf-8-arraybuffer-and-string
  - const EncoderConstructor = TextEncoder as any;
  - return new EncoderConstructor(this.encodingName(encoding), { NONSTANDARD_allowLegacyEncoding: true }).encode(s);
  + const encodingName = this.encodingName(encoding);
  +
  + if (this.customEncoder) {
  + return this.customEncoder(s, encodingName);
  }
  // Increases browser support.
  @@ -63,7 +84,12 @@ export default class StringEncoding {
  * Returns character set from some encoding character set.
  */
  public static encodingCharacterSet(encoding: string | CharacterSetECI): CharacterSetECI {
  - return CharacterSetECI.getCharacterSetECIByName(this.encodingName(encoding));
  +
  + if (encoding instanceof CharacterSetECI) {
  + return encoding;
  + }
  +
  + return CharacterSetECI.getCharacterSetECIByName(encoding);
  }
  /**
  @@ -73,9 +99,7 @@ export default class StringEncoding {
  const characterSet = this.encodingCharacterSet(encoding);
  - if (characterSet.equals(CharacterSetECI.UTF8) ||
  - characterSet.equals(CharacterSetECI.ISO8859_1) ||
  - characterSet.equals(CharacterSetECI.ASCII)) {
  + if (StringEncoding.isDecodeFallbackSupported(characterSet)) {
  let s = '';
  @@ -100,6 +124,12 @@ export default class StringEncoding {
  throw new UnsupportedOperationException(`Encoding ${this.encodingName(encoding)} not supported by fallback.`);
  }
  + private static isDecodeFallbackSupported(characterSet: CharacterSetECI) {
  + return characterSet.equals(CharacterSetECI.UTF8) ||
  + characterSet.equals(CharacterSetECI.ISO8859_1) ||
  + characterSet.equals(CharacterSetECI.ASCII);
  + }
  +
  /**
  * Runs a fallback for the native encoding funcion.
  *
  