encoding-factory.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // This is free and unencumbered software released into the public domain.
  2. // See LICENSE.md for more information.
  3. import { Big5Decoder, Big5Encoder } from '../coders/big5';
  4. import { EUCJPDecoder, EUCJPEncoder } from '../coders/euc-jp';
  5. import { EUCKRDecoder, EUCKREncoder } from '../coders/euc-kr';
  6. import { GB18030Decoder, GB18030Encoder } from '../coders/gb18030';
  7. import { ISO2022JPDecoder, ISO2022JPEncoder } from '../coders/iso-2022-jp';
  8. import { ShiftJISDecoder, ShiftJISEncoder } from '../coders/shift-jis';
  9. import { SingleByteDecoder, SingleByteEncoder } from '../coders/single-byte';
  10. import { UTF16Decoder, UTF16Encoder } from '../coders/utf-16';
  11. import { UTF8Decoder, UTF8Encoder } from '../coders/utf-8';
  12. import { XUserDefinedDecoder, XUserDefinedEncoder } from '../coders/x-user-defined';
  13. import { encodings } from './encodings';
  14. import { index } from './indexes';
  15. import { getEncodingIndexes } from './encoding-indexes-provider';
  16. //
  17. // Utilities
  18. //
  19. // import './encoding/utilities';
  20. //
  21. // Implementation of Encoding specification
  22. // https://encoding.spec.whatwg.org/
  23. //
  24. //
  25. // 4. Terminology
  26. //
  27. // import './encoding/terminology';
  28. //
  29. // 5. Encodings
  30. //
  31. // import "./encoding/encodings";
  32. //
  33. // 6. Indexes
  34. //
  35. // import './encoding/indexes';
  36. var encodingIndexes = getEncodingIndexes();
  37. // Registry of of encoder/decoder factories, by encoding name.
  38. /** @type {Object.<string, function({fatal:boolean}): Encoder>} */
  39. // const encoders: Encoders = {};
  40. /** @type {Object.<string, function({fatal:boolean}): Decoder>} */
  41. // const decoders: Decoders = {};
  42. //
  43. // 10. Legacy single-byte encodings
  44. //
  45. // 10.1 single-byte decoder
  46. // 10.2 single-byte encoder
  47. export var encoders = {
  48. // 9.1 utf-8
  49. // 9.1.1 utf-8 decoder
  50. // 9.1.2 utf-8 encoder
  51. /** @param {{fatal: boolean}} options */
  52. 'UTF-8': function (options) { return new UTF8Encoder(options); },
  53. //
  54. // 11. Legacy multi-byte Chinese (simplified) encodings
  55. //
  56. // 11.1 gbk
  57. // 11.1.1 gbk decoder
  58. // gbk's decoder is gb18030's decoder.
  59. // 11.1.2 gbk encoder
  60. // gbk's encoder is gb18030's encoder with its gbk flag set.
  61. /** @param {{fatal: boolean}} options */
  62. 'GBK': function (options) { return new GB18030Encoder(options, true); },
  63. // 11.2 gb18030
  64. // 11.2.1 gb18030 decoder
  65. // 11.2.2 gb18030 encoder
  66. /** @param {{fatal: boolean}} options */
  67. 'gb18030': function (options) { return new GB18030Encoder(options); },
  68. //
  69. // 12. Legacy multi-byte Chinese (traditional) encodings
  70. //
  71. // 12.1 Big5
  72. // 12.1.1 Big5 decoder
  73. // 12.1.2 Big5 encoder
  74. /** @param {{fatal: boolean}} options */
  75. 'Big5': function (options) { return new Big5Encoder(options); },
  76. //
  77. // 13. Legacy multi-byte Japanese encodings
  78. //
  79. // 13.1 euc-jp
  80. // 13.1.1 euc-jp decoder
  81. // 13.1.2 euc-jp encoder
  82. /** @param {{fatal: boolean}} options */
  83. 'EUC-JP': function (options) { return new EUCJPEncoder(options); },
  84. // 13.2 iso-2022-jp
  85. // 13.2.1 iso-2022-jp decoder
  86. // 13.2.2 iso-2022-jp encoder
  87. /** @param {{fatal: boolean}} options */
  88. 'ISO-2022-JP': function (options) { return new ISO2022JPEncoder(options); },
  89. // 13.3 Shift_JIS
  90. // 13.3.1 Shift_JIS decoder
  91. // 13.3.2 Shift_JIS encoder
  92. /** @param {{fatal: boolean}} options */
  93. 'Shift_JIS': function (options) { return new ShiftJISEncoder(options); },
  94. //
  95. // 14. Legacy multi-byte Korean encodings
  96. //
  97. // 14.1 euc-kr
  98. // 14.1.1 euc-kr decoder
  99. // 14.1.2 euc-kr encoder
  100. /** @param {{fatal: boolean}} options */
  101. 'EUC-KR': function (options) { return new EUCKREncoder(options); },
  102. //
  103. // 15. Legacy miscellaneous encodings
  104. //
  105. // 15.1 replacement
  106. // Not needed - API throws RangeError
  107. // 15.2 Common infrastructure for utf-16be and utf-16le
  108. // 15.2.1 shared utf-16 decoder
  109. // 15.2.2 shared utf-16 encoder
  110. // 15.3 utf-16be
  111. // 15.3.1 utf-16be decoder
  112. /** @param {{fatal: boolean}} options */
  113. 'UTF-16BE': function (options) { return new UTF16Encoder(true, options); },
  114. // 15.3.2 utf-16be encoder
  115. // 15.4 utf-16le
  116. // 15.4.1 utf-16le decoder
  117. /** @param {{fatal: boolean}} options */
  118. 'UTF-16LE': function (options) { return new UTF16Encoder(false, options); },
  119. // 15.4.2 utf-16le encoder
  120. // 15.5 x-user-defined
  121. // 15.5.1 x-user-defined decoder
  122. // 15.5.2 x-user-defined encoder
  123. /** @param {{fatal: boolean}} options */
  124. 'x-user-defined': function (options) { return new XUserDefinedEncoder(options); },
  125. };
  126. export var decoders = {
  127. /** @param {{fatal: boolean}} options */
  128. 'UTF-8': function (options) { return new UTF8Decoder(options); },
  129. /** @param {{fatal: boolean}} options */
  130. 'GBK': function (options) { return new GB18030Decoder(options); },
  131. /** @param {{fatal: boolean}} options */
  132. 'gb18030': function (options) { return new GB18030Decoder(options); },
  133. /** @param {{fatal: boolean}} options */
  134. 'Big5': function (options) { return new Big5Decoder(options); },
  135. /** @param {{fatal: boolean}} options */
  136. 'EUC-JP': function (options) { return new EUCJPDecoder(options); },
  137. /** @param {{fatal: boolean}} options */
  138. 'ISO-2022-JP': function (options) { return new ISO2022JPDecoder(options); },
  139. /** @param {{fatal: boolean}} options */
  140. 'Shift_JIS': function (options) { return new ShiftJISDecoder(options); },
  141. /** @param {{fatal: boolean}} options */
  142. 'EUC-KR': function (options) { return new EUCKRDecoder(options); },
  143. /** @param {{fatal: boolean}} options */
  144. 'UTF-16BE': function (options) { return new UTF16Decoder(true, options); },
  145. /** @param {{fatal: boolean}} options */
  146. 'UTF-16LE': function (options) { return new UTF16Decoder(false, options); },
  147. /** @param {{fatal: boolean}} options */
  148. 'x-user-defined': function (options) { return new XUserDefinedDecoder(options); },
  149. };
  150. if (encodingIndexes) {
  151. encodings.forEach(function (category) {
  152. if (category.heading !== 'Legacy single-byte encodings')
  153. return;
  154. category.encodings.forEach(function (encoding) {
  155. var name = encoding.name;
  156. var idx = index(name.toLowerCase());
  157. /** @param {{fatal: boolean}} options */
  158. decoders[name] = function (options) {
  159. return new SingleByteDecoder(idx, options);
  160. };
  161. /** @param {{fatal: boolean}} options */
  162. encoders[name] = function (options) {
  163. return new SingleByteEncoder(idx, options);
  164. };
  165. });
  166. });
  167. }
  168. //# sourceMappingURL=encoding-factory.js.map