@loaders.gl/parquet 4.0.0-alpha.9 → 4.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (463) hide show
  1. package/dist/dist.min.js +21 -32
  2. package/dist/dist.min.js.map +3 -3
  3. package/dist/es5/buffer-polyfill/buffer-polyfill.browser.js +12 -0
  4. package/dist/es5/buffer-polyfill/buffer-polyfill.browser.js.map +1 -0
  5. package/dist/es5/buffer-polyfill/buffer-polyfill.node.js +16 -0
  6. package/dist/es5/buffer-polyfill/buffer-polyfill.node.js.map +1 -0
  7. package/dist/es5/buffer-polyfill/buffer.js +1665 -0
  8. package/dist/es5/buffer-polyfill/buffer.js.map +1 -0
  9. package/dist/es5/buffer-polyfill/index.js +27 -0
  10. package/dist/es5/buffer-polyfill/index.js.map +1 -0
  11. package/dist/es5/buffer-polyfill/install-buffer-polyfill.js +10 -0
  12. package/dist/es5/buffer-polyfill/install-buffer-polyfill.js.map +1 -0
  13. package/dist/es5/index.js +25 -28
  14. package/dist/es5/index.js.map +1 -1
  15. package/dist/es5/lib/geo/decode-geo-column.js +53 -0
  16. package/dist/es5/lib/geo/decode-geo-column.js.map +1 -0
  17. package/dist/es5/lib/geo/decode-geo-metadata.js +52 -16
  18. package/dist/es5/lib/geo/decode-geo-metadata.js.map +1 -1
  19. package/dist/es5/lib/geo/{geoparquet-schema.js → geoparquet-metadata-schema.js} +4 -4
  20. package/dist/es5/lib/geo/geoparquet-metadata-schema.js.map +1 -0
  21. package/dist/es5/lib/geo/geoparquet-metadata-schema.json +60 -0
  22. package/dist/es5/lib/parsers/get-parquet-schema.js +41 -0
  23. package/dist/es5/lib/parsers/get-parquet-schema.js.map +1 -0
  24. package/dist/es5/lib/parsers/parse-parquet-to-columns.js +71 -72
  25. package/dist/es5/lib/parsers/parse-parquet-to-columns.js.map +1 -1
  26. package/dist/es5/lib/parsers/parse-parquet-to-rows.js +95 -62
  27. package/dist/es5/lib/parsers/parse-parquet-to-rows.js.map +1 -1
  28. package/dist/es5/parquet-loader.js +14 -14
  29. package/dist/es5/parquet-loader.js.map +1 -1
  30. package/dist/es5/parquet-wasm-loader.js +8 -11
  31. package/dist/es5/parquet-wasm-loader.js.map +1 -1
  32. package/dist/es5/parquet-wasm-writer.js +6 -7
  33. package/dist/es5/parquet-wasm-writer.js.map +1 -1
  34. package/dist/es5/parquet-writer.js +2 -3
  35. package/dist/es5/parquet-writer.js.map +1 -1
  36. package/dist/es5/parquetjs/codecs/plain.js.map +1 -1
  37. package/dist/es5/parquetjs/codecs/rle.js.map +1 -1
  38. package/dist/es5/parquetjs/encoder/parquet-encoder.js.map +1 -1
  39. package/dist/es5/parquetjs/parquet-thrift/index.js +75 -0
  40. package/dist/es5/parquetjs/parquet-thrift/index.js.map +1 -1
  41. package/dist/es5/parquetjs/parser/decoders.js +53 -36
  42. package/dist/es5/parquetjs/parser/decoders.js.map +1 -1
  43. package/dist/es5/parquetjs/parser/parquet-reader.js +15 -13
  44. package/dist/es5/parquetjs/parser/parquet-reader.js.map +1 -1
  45. package/dist/es5/parquetjs/schema/declare.js.map +1 -1
  46. package/dist/es5/parquetjs/utils/read-utils.js +6 -7
  47. package/dist/es5/parquetjs/utils/read-utils.js.map +1 -1
  48. package/dist/esm/buffer-polyfill/buffer-polyfill.browser.js +6 -0
  49. package/dist/esm/buffer-polyfill/buffer-polyfill.browser.js.map +1 -0
  50. package/dist/esm/buffer-polyfill/buffer-polyfill.node.js +10 -0
  51. package/dist/esm/buffer-polyfill/buffer-polyfill.node.js.map +1 -0
  52. package/dist/esm/buffer-polyfill/buffer.js +1489 -0
  53. package/dist/esm/buffer-polyfill/buffer.js.map +1 -0
  54. package/dist/esm/buffer-polyfill/index.js +4 -0
  55. package/dist/esm/buffer-polyfill/index.js.map +1 -0
  56. package/dist/esm/buffer-polyfill/install-buffer-polyfill.js +3 -0
  57. package/dist/esm/buffer-polyfill/install-buffer-polyfill.js.map +1 -0
  58. package/dist/esm/index.js +4 -10
  59. package/dist/esm/index.js.map +1 -1
  60. package/dist/esm/lib/geo/decode-geo-column.js +47 -0
  61. package/dist/esm/lib/geo/decode-geo-column.js.map +1 -0
  62. package/dist/esm/lib/geo/decode-geo-metadata.js +32 -5
  63. package/dist/esm/lib/geo/decode-geo-metadata.js.map +1 -1
  64. package/dist/esm/lib/geo/{geoparquet-schema.js → geoparquet-metadata-schema.js} +2 -2
  65. package/dist/esm/lib/geo/geoparquet-metadata-schema.js.map +1 -0
  66. package/dist/esm/lib/geo/geoparquet-metadata-schema.json +60 -0
  67. package/dist/esm/lib/parsers/get-parquet-schema.js +11 -0
  68. package/dist/esm/lib/parsers/get-parquet-schema.js.map +1 -0
  69. package/dist/esm/lib/parsers/parse-parquet-to-columns.js +10 -11
  70. package/dist/esm/lib/parsers/parse-parquet-to-columns.js.map +1 -1
  71. package/dist/esm/lib/parsers/parse-parquet-to-rows.js +32 -7
  72. package/dist/esm/lib/parsers/parse-parquet-to-rows.js.map +1 -1
  73. package/dist/esm/parquet-loader.js +12 -12
  74. package/dist/esm/parquet-loader.js.map +1 -1
  75. package/dist/esm/parquet-wasm-loader.js +7 -9
  76. package/dist/esm/parquet-wasm-loader.js.map +1 -1
  77. package/dist/esm/parquet-wasm-writer.js +6 -7
  78. package/dist/esm/parquet-wasm-writer.js.map +1 -1
  79. package/dist/esm/parquet-writer.js +2 -3
  80. package/dist/esm/parquet-writer.js.map +1 -1
  81. package/dist/esm/parquetjs/codecs/plain.js.map +1 -1
  82. package/dist/esm/parquetjs/codecs/rle.js.map +1 -1
  83. package/dist/esm/parquetjs/encoder/parquet-encoder.js.map +1 -1
  84. package/dist/esm/parquetjs/parquet-thrift/index.js +2 -0
  85. package/dist/esm/parquetjs/parquet-thrift/index.js.map +1 -1
  86. package/dist/esm/parquetjs/parser/decoders.js +48 -32
  87. package/dist/esm/parquetjs/parser/decoders.js.map +1 -1
  88. package/dist/esm/parquetjs/parser/parquet-reader.js +14 -12
  89. package/dist/esm/parquetjs/parser/parquet-reader.js.map +1 -1
  90. package/dist/esm/parquetjs/schema/declare.js.map +1 -1
  91. package/dist/esm/parquetjs/utils/read-utils.js +1 -1
  92. package/dist/esm/parquetjs/utils/read-utils.js.map +1 -1
  93. package/dist/parquet-worker.js +23 -34
  94. package/dist/parquet-worker.js.map +3 -3
  95. package/dist/src/buffer-polyfill/buffer-polyfill.browser.d.ts +4 -0
  96. package/dist/src/buffer-polyfill/buffer-polyfill.browser.d.ts.map +1 -0
  97. package/dist/src/buffer-polyfill/buffer-polyfill.node.d.ts +4 -0
  98. package/dist/src/buffer-polyfill/buffer-polyfill.node.d.ts.map +1 -0
  99. package/dist/src/buffer-polyfill/buffer.d.ts +222 -0
  100. package/dist/src/buffer-polyfill/buffer.d.ts.map +1 -0
  101. package/dist/src/buffer-polyfill/index.d.ts +4 -0
  102. package/dist/src/buffer-polyfill/index.d.ts.map +1 -0
  103. package/dist/src/buffer-polyfill/install-buffer-polyfill.d.ts +30 -0
  104. package/dist/src/buffer-polyfill/install-buffer-polyfill.d.ts.map +1 -0
  105. package/dist/src/bundle.d.ts.map +1 -0
  106. package/dist/src/constants.d.ts.map +1 -0
  107. package/dist/{index.d.ts → src/index.d.ts} +6 -10
  108. package/dist/src/index.d.ts.map +1 -0
  109. package/dist/src/lib/arrow/convert-columns-to-row-group.d.ts.map +1 -0
  110. package/dist/src/lib/arrow/convert-row-group-to-columns.d.ts.map +1 -0
  111. package/dist/src/lib/arrow/convert-schema-from-parquet.d.ts.map +1 -0
  112. package/dist/src/lib/arrow/convert-schema-to-parquet.d.ts.map +1 -0
  113. package/dist/src/lib/geo/decode-geo-column.d.ts +4 -0
  114. package/dist/src/lib/geo/decode-geo-column.d.ts.map +1 -0
  115. package/dist/src/lib/geo/decode-geo-metadata.d.ts +44 -0
  116. package/dist/src/lib/geo/decode-geo-metadata.d.ts.map +1 -0
  117. package/dist/{lib/geo/geoparquet-schema.d.ts → src/lib/geo/geoparquet-metadata-schema.d.ts} +2 -3
  118. package/dist/src/lib/geo/geoparquet-metadata-schema.d.ts.map +1 -0
  119. package/dist/src/lib/parsers/get-parquet-schema.d.ts +4 -0
  120. package/dist/src/lib/parsers/get-parquet-schema.d.ts.map +1 -0
  121. package/dist/src/lib/parsers/parse-parquet-to-columns.d.ts +6 -0
  122. package/dist/src/lib/parsers/parse-parquet-to-columns.d.ts.map +1 -0
  123. package/dist/src/lib/parsers/parse-parquet-to-rows.d.ts +6 -0
  124. package/dist/src/lib/parsers/parse-parquet-to-rows.d.ts.map +1 -0
  125. package/dist/src/lib/wasm/encode-parquet-wasm.d.ts.map +1 -0
  126. package/dist/src/lib/wasm/load-wasm/index.d.ts.map +1 -0
  127. package/dist/src/lib/wasm/load-wasm/load-wasm-browser.d.ts.map +1 -0
  128. package/dist/src/lib/wasm/load-wasm/load-wasm-node.d.ts.map +1 -0
  129. package/dist/src/lib/wasm/parse-parquet-wasm.d.ts.map +1 -0
  130. package/dist/src/parquet-loader.d.ts +22 -0
  131. package/dist/src/parquet-loader.d.ts.map +1 -0
  132. package/dist/src/parquet-wasm-loader.d.ts +12 -0
  133. package/dist/src/parquet-wasm-loader.d.ts.map +1 -0
  134. package/dist/src/parquet-wasm-writer.d.ts +6 -0
  135. package/dist/src/parquet-wasm-writer.d.ts.map +1 -0
  136. package/dist/src/parquet-writer.d.ts.map +1 -0
  137. package/dist/src/parquetjs/codecs/declare.d.ts.map +1 -0
  138. package/dist/src/parquetjs/codecs/dictionary.d.ts.map +1 -0
  139. package/dist/src/parquetjs/codecs/index.d.ts.map +1 -0
  140. package/dist/src/parquetjs/codecs/plain.d.ts.map +1 -0
  141. package/dist/src/parquetjs/codecs/rle.d.ts.map +1 -0
  142. package/dist/src/parquetjs/compression.d.ts.map +1 -0
  143. package/dist/src/parquetjs/encoder/parquet-encoder.d.ts.map +1 -0
  144. package/dist/src/parquetjs/parquet-thrift/BoundaryOrder.d.ts.map +1 -0
  145. package/dist/src/parquetjs/parquet-thrift/BsonType.d.ts.map +1 -0
  146. package/dist/src/parquetjs/parquet-thrift/ColumnChunk.d.ts.map +1 -0
  147. package/dist/src/parquetjs/parquet-thrift/ColumnIndex.d.ts.map +1 -0
  148. package/dist/src/parquetjs/parquet-thrift/ColumnMetaData.d.ts.map +1 -0
  149. package/dist/src/parquetjs/parquet-thrift/ColumnOrder.d.ts.map +1 -0
  150. package/dist/src/parquetjs/parquet-thrift/CompressionCodec.d.ts.map +1 -0
  151. package/dist/src/parquetjs/parquet-thrift/ConvertedType.d.ts.map +1 -0
  152. package/dist/src/parquetjs/parquet-thrift/DataPageHeader.d.ts.map +1 -0
  153. package/dist/src/parquetjs/parquet-thrift/DataPageHeaderV2.d.ts.map +1 -0
  154. package/dist/src/parquetjs/parquet-thrift/DateType.d.ts.map +1 -0
  155. package/dist/src/parquetjs/parquet-thrift/DecimalType.d.ts.map +1 -0
  156. package/dist/src/parquetjs/parquet-thrift/DictionaryPageHeader.d.ts.map +1 -0
  157. package/dist/src/parquetjs/parquet-thrift/Encoding.d.ts.map +1 -0
  158. package/dist/src/parquetjs/parquet-thrift/EnumType.d.ts.map +1 -0
  159. package/dist/src/parquetjs/parquet-thrift/FieldRepetitionType.d.ts.map +1 -0
  160. package/dist/src/parquetjs/parquet-thrift/FileMetaData.d.ts.map +1 -0
  161. package/dist/src/parquetjs/parquet-thrift/IndexPageHeader.d.ts.map +1 -0
  162. package/dist/src/parquetjs/parquet-thrift/IntType.d.ts.map +1 -0
  163. package/dist/src/parquetjs/parquet-thrift/JsonType.d.ts.map +1 -0
  164. package/dist/src/parquetjs/parquet-thrift/KeyValue.d.ts.map +1 -0
  165. package/dist/src/parquetjs/parquet-thrift/ListType.d.ts.map +1 -0
  166. package/dist/src/parquetjs/parquet-thrift/LogicalType.d.ts.map +1 -0
  167. package/dist/src/parquetjs/parquet-thrift/MapType.d.ts.map +1 -0
  168. package/dist/src/parquetjs/parquet-thrift/MicroSeconds.d.ts.map +1 -0
  169. package/dist/src/parquetjs/parquet-thrift/MilliSeconds.d.ts.map +1 -0
  170. package/dist/src/parquetjs/parquet-thrift/NullType.d.ts.map +1 -0
  171. package/dist/src/parquetjs/parquet-thrift/OffsetIndex.d.ts.map +1 -0
  172. package/dist/src/parquetjs/parquet-thrift/PageEncodingStats.d.ts.map +1 -0
  173. package/dist/src/parquetjs/parquet-thrift/PageHeader.d.ts.map +1 -0
  174. package/dist/src/parquetjs/parquet-thrift/PageLocation.d.ts.map +1 -0
  175. package/dist/src/parquetjs/parquet-thrift/PageType.d.ts.map +1 -0
  176. package/dist/src/parquetjs/parquet-thrift/RowGroup.d.ts.map +1 -0
  177. package/dist/src/parquetjs/parquet-thrift/SchemaElement.d.ts.map +1 -0
  178. package/dist/src/parquetjs/parquet-thrift/SortingColumn.d.ts.map +1 -0
  179. package/dist/src/parquetjs/parquet-thrift/Statistics.d.ts.map +1 -0
  180. package/dist/src/parquetjs/parquet-thrift/StringType.d.ts.map +1 -0
  181. package/dist/src/parquetjs/parquet-thrift/TimeType.d.ts.map +1 -0
  182. package/dist/src/parquetjs/parquet-thrift/TimeUnit.d.ts.map +1 -0
  183. package/dist/src/parquetjs/parquet-thrift/TimestampType.d.ts.map +1 -0
  184. package/dist/src/parquetjs/parquet-thrift/Type.d.ts.map +1 -0
  185. package/dist/src/parquetjs/parquet-thrift/TypeDefinedOrder.d.ts.map +1 -0
  186. package/dist/src/parquetjs/parquet-thrift/UUIDType.d.ts.map +1 -0
  187. package/dist/{parquetjs → src/parquetjs}/parquet-thrift/index.d.ts +2 -0
  188. package/dist/src/parquetjs/parquet-thrift/index.d.ts.map +1 -0
  189. package/dist/{parquetjs → src/parquetjs}/parser/decoders.d.ts +4 -4
  190. package/dist/src/parquetjs/parser/decoders.d.ts.map +1 -0
  191. package/dist/{parquetjs → src/parquetjs}/parser/parquet-reader.d.ts +5 -3
  192. package/dist/src/parquetjs/parser/parquet-reader.d.ts.map +1 -0
  193. package/dist/{parquetjs → src/parquetjs}/schema/declare.d.ts +3 -1
  194. package/dist/src/parquetjs/schema/declare.d.ts.map +1 -0
  195. package/dist/src/parquetjs/schema/schema.d.ts.map +1 -0
  196. package/dist/src/parquetjs/schema/shred.d.ts.map +1 -0
  197. package/dist/src/parquetjs/schema/types.d.ts.map +1 -0
  198. package/dist/src/parquetjs/utils/file-utils.d.ts.map +1 -0
  199. package/dist/src/parquetjs/utils/read-utils.d.ts.map +1 -0
  200. package/dist/src/workers/parquet-worker.d.ts.map +1 -0
  201. package/dist/tsconfig.tsbuildinfo +1 -0
  202. package/package.json +21 -9
  203. package/src/buffer-polyfill/buffer-polyfill.browser.ts +11 -0
  204. package/src/buffer-polyfill/buffer-polyfill.node.ts +15 -0
  205. package/src/buffer-polyfill/buffer.ts +2207 -0
  206. package/src/buffer-polyfill/index.ts +8 -0
  207. package/src/buffer-polyfill/install-buffer-polyfill.ts +3 -0
  208. package/src/index.ts +31 -17
  209. package/src/lib/geo/decode-geo-column.ts +54 -0
  210. package/src/lib/geo/decode-geo-metadata.ts +81 -15
  211. package/src/lib/geo/geoparquet-metadata-schema.json +60 -0
  212. package/src/lib/geo/{geoparquet-schema.ts → geoparquet-metadata-schema.ts} +1 -1
  213. package/src/lib/parsers/get-parquet-schema.ts +14 -0
  214. package/src/lib/parsers/parse-parquet-to-columns.ts +15 -13
  215. package/src/lib/parsers/parse-parquet-to-rows.ts +39 -11
  216. package/src/parquet-loader.ts +29 -16
  217. package/src/parquet-wasm-loader.ts +13 -13
  218. package/src/parquet-wasm-writer.ts +10 -8
  219. package/src/parquet-writer.ts +1 -3
  220. package/src/parquetjs/codecs/plain.ts +1 -0
  221. package/src/parquetjs/codecs/rle.ts +2 -0
  222. package/src/parquetjs/encoder/parquet-encoder.ts +1 -0
  223. package/src/parquetjs/parquet-thrift/index.ts +4 -0
  224. package/src/parquetjs/parser/decoders.ts +63 -41
  225. package/src/parquetjs/parser/parquet-reader.ts +19 -14
  226. package/src/parquetjs/schema/declare.ts +3 -1
  227. package/src/parquetjs/utils/read-utils.ts +2 -2
  228. package/dist/bundle.d.ts.map +0 -1
  229. package/dist/bundle.js +0 -5
  230. package/dist/constants.d.ts.map +0 -1
  231. package/dist/constants.js +0 -18
  232. package/dist/es5/lib/geo/geoparquet-schema.js.map +0 -1
  233. package/dist/esm/lib/geo/geoparquet-schema.js.map +0 -1
  234. package/dist/index.d.ts.map +0 -1
  235. package/dist/index.js +0 -58
  236. package/dist/lib/arrow/convert-columns-to-row-group.d.ts.map +0 -1
  237. package/dist/lib/arrow/convert-columns-to-row-group.js +0 -1
  238. package/dist/lib/arrow/convert-row-group-to-columns.d.ts.map +0 -1
  239. package/dist/lib/arrow/convert-row-group-to-columns.js +0 -12
  240. package/dist/lib/arrow/convert-schema-from-parquet.d.ts.map +0 -1
  241. package/dist/lib/arrow/convert-schema-from-parquet.js +0 -86
  242. package/dist/lib/arrow/convert-schema-to-parquet.d.ts.map +0 -1
  243. package/dist/lib/arrow/convert-schema-to-parquet.js +0 -71
  244. package/dist/lib/geo/decode-geo-metadata.d.ts +0 -31
  245. package/dist/lib/geo/decode-geo-metadata.d.ts.map +0 -1
  246. package/dist/lib/geo/decode-geo-metadata.js +0 -77
  247. package/dist/lib/geo/geoparquet-schema.d.ts.map +0 -1
  248. package/dist/lib/geo/geoparquet-schema.js +0 -69
  249. package/dist/lib/parsers/parse-parquet-to-columns.d.ts +0 -5
  250. package/dist/lib/parsers/parse-parquet-to-columns.d.ts.map +0 -1
  251. package/dist/lib/parsers/parse-parquet-to-columns.js +0 -46
  252. package/dist/lib/parsers/parse-parquet-to-rows.d.ts +0 -5
  253. package/dist/lib/parsers/parse-parquet-to-rows.d.ts.map +0 -1
  254. package/dist/lib/parsers/parse-parquet-to-rows.js +0 -37
  255. package/dist/lib/wasm/encode-parquet-wasm.d.ts.map +0 -1
  256. package/dist/lib/wasm/encode-parquet-wasm.js +0 -30
  257. package/dist/lib/wasm/load-wasm/index.d.ts.map +0 -1
  258. package/dist/lib/wasm/load-wasm/index.js +0 -5
  259. package/dist/lib/wasm/load-wasm/load-wasm-browser.d.ts.map +0 -1
  260. package/dist/lib/wasm/load-wasm/load-wasm-browser.js +0 -38
  261. package/dist/lib/wasm/load-wasm/load-wasm-node.d.ts.map +0 -1
  262. package/dist/lib/wasm/load-wasm/load-wasm-node.js +0 -31
  263. package/dist/lib/wasm/parse-parquet-wasm.d.ts.map +0 -1
  264. package/dist/lib/wasm/parse-parquet-wasm.js +0 -27
  265. package/dist/parquet-loader.d.ts +0 -14
  266. package/dist/parquet-loader.d.ts.map +0 -1
  267. package/dist/parquet-loader.js +0 -41
  268. package/dist/parquet-wasm-loader.d.ts +0 -23
  269. package/dist/parquet-wasm-loader.d.ts.map +0 -1
  270. package/dist/parquet-wasm-loader.js +0 -27
  271. package/dist/parquet-wasm-writer.d.ts +0 -3
  272. package/dist/parquet-wasm-writer.d.ts.map +0 -1
  273. package/dist/parquet-wasm-writer.js +0 -23
  274. package/dist/parquet-writer.d.ts.map +0 -1
  275. package/dist/parquet-writer.js +0 -22
  276. package/dist/parquetjs/codecs/declare.d.ts.map +0 -1
  277. package/dist/parquetjs/codecs/declare.js +0 -2
  278. package/dist/parquetjs/codecs/dictionary.d.ts.map +0 -1
  279. package/dist/parquetjs/codecs/dictionary.js +0 -14
  280. package/dist/parquetjs/codecs/index.d.ts.map +0 -1
  281. package/dist/parquetjs/codecs/index.js +0 -55
  282. package/dist/parquetjs/codecs/plain.d.ts.map +0 -1
  283. package/dist/parquetjs/codecs/plain.js +0 -211
  284. package/dist/parquetjs/codecs/rle.d.ts.map +0 -1
  285. package/dist/parquetjs/codecs/rle.js +0 -145
  286. package/dist/parquetjs/compression.d.ts.map +0 -1
  287. package/dist/parquetjs/compression.js +0 -183
  288. package/dist/parquetjs/encoder/parquet-encoder.d.ts.map +0 -1
  289. package/dist/parquetjs/encoder/parquet-encoder.js +0 -484
  290. package/dist/parquetjs/parquet-thrift/BoundaryOrder.d.ts.map +0 -1
  291. package/dist/parquetjs/parquet-thrift/BoundaryOrder.js +0 -15
  292. package/dist/parquetjs/parquet-thrift/BsonType.d.ts.map +0 -1
  293. package/dist/parquetjs/parquet-thrift/BsonType.js +0 -62
  294. package/dist/parquetjs/parquet-thrift/ColumnChunk.d.ts.map +0 -1
  295. package/dist/parquetjs/parquet-thrift/ColumnChunk.js +0 -211
  296. package/dist/parquetjs/parquet-thrift/ColumnIndex.d.ts.map +0 -1
  297. package/dist/parquetjs/parquet-thrift/ColumnIndex.js +0 -217
  298. package/dist/parquetjs/parquet-thrift/ColumnMetaData.d.ts.map +0 -1
  299. package/dist/parquetjs/parquet-thrift/ColumnMetaData.js +0 -402
  300. package/dist/parquetjs/parquet-thrift/ColumnOrder.d.ts.map +0 -1
  301. package/dist/parquetjs/parquet-thrift/ColumnOrder.js +0 -108
  302. package/dist/parquetjs/parquet-thrift/CompressionCodec.d.ts.map +0 -1
  303. package/dist/parquetjs/parquet-thrift/CompressionCodec.js +0 -20
  304. package/dist/parquetjs/parquet-thrift/ConvertedType.d.ts.map +0 -1
  305. package/dist/parquetjs/parquet-thrift/ConvertedType.js +0 -34
  306. package/dist/parquetjs/parquet-thrift/DataPageHeader.d.ts.map +0 -1
  307. package/dist/parquetjs/parquet-thrift/DataPageHeader.js +0 -170
  308. package/dist/parquetjs/parquet-thrift/DataPageHeaderV2.d.ts.map +0 -1
  309. package/dist/parquetjs/parquet-thrift/DataPageHeaderV2.js +0 -230
  310. package/dist/parquetjs/parquet-thrift/DateType.d.ts.map +0 -1
  311. package/dist/parquetjs/parquet-thrift/DateType.js +0 -62
  312. package/dist/parquetjs/parquet-thrift/DecimalType.d.ts.map +0 -1
  313. package/dist/parquetjs/parquet-thrift/DecimalType.js +0 -109
  314. package/dist/parquetjs/parquet-thrift/DictionaryPageHeader.d.ts.map +0 -1
  315. package/dist/parquetjs/parquet-thrift/DictionaryPageHeader.js +0 -126
  316. package/dist/parquetjs/parquet-thrift/Encoding.d.ts.map +0 -1
  317. package/dist/parquetjs/parquet-thrift/Encoding.js +0 -20
  318. package/dist/parquetjs/parquet-thrift/EnumType.d.ts.map +0 -1
  319. package/dist/parquetjs/parquet-thrift/EnumType.js +0 -62
  320. package/dist/parquetjs/parquet-thrift/FieldRepetitionType.d.ts.map +0 -1
  321. package/dist/parquetjs/parquet-thrift/FieldRepetitionType.js +0 -15
  322. package/dist/parquetjs/parquet-thrift/FileMetaData.d.ts.map +0 -1
  323. package/dist/parquetjs/parquet-thrift/FileMetaData.js +0 -260
  324. package/dist/parquetjs/parquet-thrift/IndexPageHeader.d.ts.map +0 -1
  325. package/dist/parquetjs/parquet-thrift/IndexPageHeader.js +0 -62
  326. package/dist/parquetjs/parquet-thrift/IntType.d.ts.map +0 -1
  327. package/dist/parquetjs/parquet-thrift/IntType.js +0 -109
  328. package/dist/parquetjs/parquet-thrift/JsonType.d.ts.map +0 -1
  329. package/dist/parquetjs/parquet-thrift/JsonType.js +0 -62
  330. package/dist/parquetjs/parquet-thrift/KeyValue.d.ts.map +0 -1
  331. package/dist/parquetjs/parquet-thrift/KeyValue.js +0 -106
  332. package/dist/parquetjs/parquet-thrift/ListType.d.ts.map +0 -1
  333. package/dist/parquetjs/parquet-thrift/ListType.js +0 -62
  334. package/dist/parquetjs/parquet-thrift/LogicalType.d.ts.map +0 -1
  335. package/dist/parquetjs/parquet-thrift/LogicalType.js +0 -384
  336. package/dist/parquetjs/parquet-thrift/MapType.d.ts.map +0 -1
  337. package/dist/parquetjs/parquet-thrift/MapType.js +0 -62
  338. package/dist/parquetjs/parquet-thrift/MicroSeconds.d.ts.map +0 -1
  339. package/dist/parquetjs/parquet-thrift/MicroSeconds.js +0 -62
  340. package/dist/parquetjs/parquet-thrift/MilliSeconds.d.ts.map +0 -1
  341. package/dist/parquetjs/parquet-thrift/MilliSeconds.js +0 -62
  342. package/dist/parquetjs/parquet-thrift/NullType.d.ts.map +0 -1
  343. package/dist/parquetjs/parquet-thrift/NullType.js +0 -62
  344. package/dist/parquetjs/parquet-thrift/OffsetIndex.d.ts.map +0 -1
  345. package/dist/parquetjs/parquet-thrift/OffsetIndex.js +0 -101
  346. package/dist/parquetjs/parquet-thrift/PageEncodingStats.d.ts.map +0 -1
  347. package/dist/parquetjs/parquet-thrift/PageEncodingStats.js +0 -131
  348. package/dist/parquetjs/parquet-thrift/PageHeader.d.ts.map +0 -1
  349. package/dist/parquetjs/parquet-thrift/PageHeader.js +0 -220
  350. package/dist/parquetjs/parquet-thrift/PageLocation.d.ts.map +0 -1
  351. package/dist/parquetjs/parquet-thrift/PageLocation.js +0 -145
  352. package/dist/parquetjs/parquet-thrift/PageType.d.ts.map +0 -1
  353. package/dist/parquetjs/parquet-thrift/PageType.js +0 -16
  354. package/dist/parquetjs/parquet-thrift/RowGroup.d.ts.map +0 -1
  355. package/dist/parquetjs/parquet-thrift/RowGroup.js +0 -186
  356. package/dist/parquetjs/parquet-thrift/SchemaElement.d.ts.map +0 -1
  357. package/dist/parquetjs/parquet-thrift/SchemaElement.js +0 -243
  358. package/dist/parquetjs/parquet-thrift/SortingColumn.d.ts.map +0 -1
  359. package/dist/parquetjs/parquet-thrift/SortingColumn.js +0 -131
  360. package/dist/parquetjs/parquet-thrift/Statistics.d.ts.map +0 -1
  361. package/dist/parquetjs/parquet-thrift/Statistics.js +0 -180
  362. package/dist/parquetjs/parquet-thrift/StringType.d.ts.map +0 -1
  363. package/dist/parquetjs/parquet-thrift/StringType.js +0 -62
  364. package/dist/parquetjs/parquet-thrift/TimeType.d.ts.map +0 -1
  365. package/dist/parquetjs/parquet-thrift/TimeType.js +0 -110
  366. package/dist/parquetjs/parquet-thrift/TimeUnit.d.ts.map +0 -1
  367. package/dist/parquetjs/parquet-thrift/TimeUnit.js +0 -131
  368. package/dist/parquetjs/parquet-thrift/TimestampType.d.ts.map +0 -1
  369. package/dist/parquetjs/parquet-thrift/TimestampType.js +0 -110
  370. package/dist/parquetjs/parquet-thrift/Type.d.ts.map +0 -1
  371. package/dist/parquetjs/parquet-thrift/Type.js +0 -20
  372. package/dist/parquetjs/parquet-thrift/TypeDefinedOrder.d.ts.map +0 -1
  373. package/dist/parquetjs/parquet-thrift/TypeDefinedOrder.js +0 -62
  374. package/dist/parquetjs/parquet-thrift/UUIDType.d.ts.map +0 -1
  375. package/dist/parquetjs/parquet-thrift/UUIDType.js +0 -62
  376. package/dist/parquetjs/parquet-thrift/index.d.ts.map +0 -1
  377. package/dist/parquetjs/parquet-thrift/index.js +0 -65
  378. package/dist/parquetjs/parser/decoders.d.ts.map +0 -1
  379. package/dist/parquetjs/parser/decoders.js +0 -318
  380. package/dist/parquetjs/parser/parquet-reader.d.ts.map +0 -1
  381. package/dist/parquetjs/parser/parquet-reader.js +0 -200
  382. package/dist/parquetjs/schema/declare.d.ts.map +0 -1
  383. package/dist/parquetjs/schema/declare.js +0 -12
  384. package/dist/parquetjs/schema/schema.d.ts.map +0 -1
  385. package/dist/parquetjs/schema/schema.js +0 -162
  386. package/dist/parquetjs/schema/shred.d.ts.map +0 -1
  387. package/dist/parquetjs/schema/shred.js +0 -355
  388. package/dist/parquetjs/schema/types.d.ts.map +0 -1
  389. package/dist/parquetjs/schema/types.js +0 -416
  390. package/dist/parquetjs/utils/file-utils.d.ts.map +0 -1
  391. package/dist/parquetjs/utils/file-utils.js +0 -43
  392. package/dist/parquetjs/utils/read-utils.d.ts.map +0 -1
  393. package/dist/parquetjs/utils/read-utils.js +0 -109
  394. package/dist/workers/parquet-worker.d.ts.map +0 -1
  395. package/dist/workers/parquet-worker.js +0 -5
  396. /package/dist/{bundle.d.ts → src/bundle.d.ts} +0 -0
  397. /package/dist/{constants.d.ts → src/constants.d.ts} +0 -0
  398. /package/dist/{lib → src/lib}/arrow/convert-columns-to-row-group.d.ts +0 -0
  399. /package/dist/{lib → src/lib}/arrow/convert-row-group-to-columns.d.ts +0 -0
  400. /package/dist/{lib → src/lib}/arrow/convert-schema-from-parquet.d.ts +0 -0
  401. /package/dist/{lib → src/lib}/arrow/convert-schema-to-parquet.d.ts +0 -0
  402. /package/dist/{lib → src/lib}/wasm/encode-parquet-wasm.d.ts +0 -0
  403. /package/dist/{lib → src/lib}/wasm/load-wasm/index.d.ts +0 -0
  404. /package/dist/{lib → src/lib}/wasm/load-wasm/load-wasm-browser.d.ts +0 -0
  405. /package/dist/{lib → src/lib}/wasm/load-wasm/load-wasm-node.d.ts +0 -0
  406. /package/dist/{lib → src/lib}/wasm/parse-parquet-wasm.d.ts +0 -0
  407. /package/dist/{parquet-writer.d.ts → src/parquet-writer.d.ts} +0 -0
  408. /package/dist/{parquetjs → src/parquetjs}/codecs/declare.d.ts +0 -0
  409. /package/dist/{parquetjs → src/parquetjs}/codecs/dictionary.d.ts +0 -0
  410. /package/dist/{parquetjs → src/parquetjs}/codecs/index.d.ts +0 -0
  411. /package/dist/{parquetjs → src/parquetjs}/codecs/plain.d.ts +0 -0
  412. /package/dist/{parquetjs → src/parquetjs}/codecs/rle.d.ts +0 -0
  413. /package/dist/{parquetjs → src/parquetjs}/compression.d.ts +0 -0
  414. /package/dist/{parquetjs → src/parquetjs}/encoder/parquet-encoder.d.ts +0 -0
  415. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/BoundaryOrder.d.ts +0 -0
  416. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/BsonType.d.ts +0 -0
  417. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/ColumnChunk.d.ts +0 -0
  418. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/ColumnIndex.d.ts +0 -0
  419. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/ColumnMetaData.d.ts +0 -0
  420. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/ColumnOrder.d.ts +0 -0
  421. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/CompressionCodec.d.ts +0 -0
  422. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/ConvertedType.d.ts +0 -0
  423. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/DataPageHeader.d.ts +0 -0
  424. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/DataPageHeaderV2.d.ts +0 -0
  425. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/DateType.d.ts +0 -0
  426. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/DecimalType.d.ts +0 -0
  427. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/DictionaryPageHeader.d.ts +0 -0
  428. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/Encoding.d.ts +0 -0
  429. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/EnumType.d.ts +0 -0
  430. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/FieldRepetitionType.d.ts +0 -0
  431. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/FileMetaData.d.ts +0 -0
  432. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/IndexPageHeader.d.ts +0 -0
  433. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/IntType.d.ts +0 -0
  434. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/JsonType.d.ts +0 -0
  435. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/KeyValue.d.ts +0 -0
  436. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/ListType.d.ts +0 -0
  437. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/LogicalType.d.ts +0 -0
  438. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/MapType.d.ts +0 -0
  439. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/MicroSeconds.d.ts +0 -0
  440. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/MilliSeconds.d.ts +0 -0
  441. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/NullType.d.ts +0 -0
  442. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/OffsetIndex.d.ts +0 -0
  443. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/PageEncodingStats.d.ts +0 -0
  444. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/PageHeader.d.ts +0 -0
  445. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/PageLocation.d.ts +0 -0
  446. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/PageType.d.ts +0 -0
  447. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/RowGroup.d.ts +0 -0
  448. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/SchemaElement.d.ts +0 -0
  449. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/SortingColumn.d.ts +0 -0
  450. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/Statistics.d.ts +0 -0
  451. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/StringType.d.ts +0 -0
  452. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/TimeType.d.ts +0 -0
  453. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/TimeUnit.d.ts +0 -0
  454. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/TimestampType.d.ts +0 -0
  455. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/Type.d.ts +0 -0
  456. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/TypeDefinedOrder.d.ts +0 -0
  457. /package/dist/{parquetjs → src/parquetjs}/parquet-thrift/UUIDType.d.ts +0 -0
  458. /package/dist/{parquetjs → src/parquetjs}/schema/schema.d.ts +0 -0
  459. /package/dist/{parquetjs → src/parquetjs}/schema/shred.d.ts +0 -0
  460. /package/dist/{parquetjs → src/parquetjs}/schema/types.d.ts +0 -0
  461. /package/dist/{parquetjs → src/parquetjs}/utils/file-utils.d.ts +0 -0
  462. /package/dist/{parquetjs → src/parquetjs}/utils/read-utils.d.ts +0 -0
  463. /package/dist/{workers → src/workers}/parquet-worker.d.ts +0 -0
@@ -0,0 +1,2207 @@
1
+ // loaders.gl, MIT license
2
+ // This file is forked from https://github.com/feross/buffer under MIT license
3
+ // The code has primarily been converted to TypeScript.
4
+
5
+ /* !
6
+ * The buffer module from node.js, for the browser.
7
+ * @author Feross Aboukhadijeh <https://feross.org>
8
+ * @license MIT
9
+ * https://github.com/feross/buffer/blob/master/AUTHORS.md
10
+ */
11
+
12
+ /**
13
+ * The decision to include this polyfill in loaders.gl may seem controversial.
14
+ *
15
+ * It is based on the following reasoning:
16
+ * - The Buffer API is used in the parquetjs library and likely other Node.js
17
+ * libraries we will use in the future.
18
+ * - While the goal is to convert code from Buffer to ArrayBuffer, the Thrift
19
+ * code in Parquet may be autogenerated.
20
+ * - Bundlers often require a polyfill to be included, this extra step is not
21
+ * a great experience for loaders.gl users.
22
+ * - The forked buffer polyfill module had old and generated some type errors.
23
+ */
24
+
25
+ /* eslint-disable */ // no-proto, max-statements, max-depth, complexity, no-continue, prefer-spread, no-constant-condition, consistent-return */
26
+
27
+ // @ts-nocheck - this references don't work well in prototype assignment
28
+
29
+ import base64 from 'base64-js';
30
+ import ieee754 from 'ieee754';
31
+
32
+ export const kMaxLength = 0x7fffffff;
33
+ export const INSPECT_MAX_BYTES = 50;
34
+
35
+ type BufferEncoding = string;
36
+
37
+ // const customInspectSymbol =
38
+ // typeof Symbol === 'function' && typeof Symbol['for'] === 'function' // eslint-disable-line dot-notation
39
+ // ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
40
+ // : null;
41
+
42
+ /**
43
+ * The Buffer constructor returns instances of `Uint8Array` that have their
44
+ * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
45
+ * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
46
+ * and the `Uint8Array` methods. Square bracket notation works as expected -- it
47
+ * returns a single octet.
48
+ *
49
+ * The `Uint8Array` prototype remains unmodified.
50
+ */
51
+ export class Buffer extends Uint8Array {
52
+ static poolSize = 8192; // not used by this implementation
53
+
54
+ // length: number; inherited
55
+
56
+ get parent() {
57
+ if (!Buffer.isBuffer(this)) return undefined;
58
+ return this.buffer;
59
+ }
60
+
61
+ get offset() {
62
+ if (!Buffer.isBuffer(this)) return undefined;
63
+ return this.byteOffset;
64
+ }
65
+
66
+ /** This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
67
+ * to detect a Buffer instance. It's not possible to use `instanceof Buffer`
68
+ * reliably in a browserify context because there could be multiple different
69
+ * copies of the 'buffer' package in use. This method works even for Buffer
70
+ * instances that were created from another copy of the `buffer` package.
71
+ * @see: https://github.com/feross/buffer/issues/154
72
+ */
73
+ _isBuffer = true;
74
+
75
+ /**
76
+ * Allocates a new buffer containing the given {str}.
77
+ *
78
+ * @param str String to store in buffer.
79
+ * @param encoding encoding to use, optional. Default is 'utf8'
80
+ */
81
+ constructor(str: string, encoding?: string);
82
+ /**
83
+ * Allocates a new buffer of {size} octets.
84
+ *
85
+ * @param size count of octets to allocate.
86
+ */
87
+ constructor(size: number);
88
+ /**
89
+ * Allocates a new buffer containing the given {array} of octets.
90
+ *
91
+ * @param array The octets to store.
92
+ */
93
+ constructor(array: Uint8Array);
94
+ /**
95
+ * Produces a Buffer backed by the same allocated memory as
96
+ * the given {ArrayBuffer}.
97
+ *
98
+ *
99
+ * @param arrayBuffer The ArrayBuffer with which to share memory.
100
+ */
101
+ constructor(arrayBuffer: ArrayBuffer);
102
+ /**
103
+ * Allocates a new buffer containing the given {array} of octets.
104
+ *
105
+ * @param array The octets to store.
106
+ */
107
+ constructor(array: any[]);
108
+ /**
109
+ * Copies the passed {buffer} data onto a new {Buffer} instance.
110
+ *
111
+ * @param buffer The buffer to copy.
112
+ */
113
+ constructor(buffer: Buffer);
114
+
115
+ constructor(arg, encodingOrOffset?, length?: number) {
116
+ if (typeof arg !== 'number') {
117
+ return Buffer.from(arg, encodingOrOffset, length);
118
+ }
119
+
120
+ // Basic case, just a length
121
+ const size = arg;
122
+ if (size > kMaxLength) {
123
+ throw new RangeError(`The value "${size}" is invalid for option "size"`);
124
+ }
125
+ if (typeof encodingOrOffset === 'string') {
126
+ throw new TypeError('The "string" argument must be of type string. Received type number');
127
+ }
128
+ super(size < 0 ? 0 : checked(size) | 0);
129
+ return;
130
+ }
131
+
132
+ /**
133
+ * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
134
+ * if value is a number.
135
+ * Buffer.from(str[, encoding])
136
+ * Buffer.from(array)
137
+ * Buffer.from(buffer)
138
+ * Buffer.from(arrayBuffer[, byteOffset[, length]])
139
+ **/
140
+
141
+ /**
142
+ * Allocates a new Buffer using an {array} of octets.
143
+ *
144
+ * @param array
145
+ */
146
+ static from(array: any[]): Buffer;
147
+ /**
148
+ * When passed a reference to the .buffer property of a TypedArray instance,
149
+ * the newly created Buffer will share the same allocated memory as the TypedArray.
150
+ * The optional {byteOffset} and {length} arguments specify a memory range
151
+ * within the {arrayBuffer} that will be shared by the Buffer.
152
+ *
153
+ * @param arrayBuffer The .buffer property of a TypedArray or a new ArrayBuffer()
154
+ * @param byteOffset
155
+ * @param length
156
+ */
157
+ static from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer;
158
+ /**
159
+ * Copies the passed {buffer} data onto a new Buffer instance.
160
+ *
161
+ * @param buffer
162
+ */
163
+ static from(buffer: Buffer | Uint8Array): Buffer;
164
+ /**
165
+ * Creates a new Buffer containing the given JavaScript string {str}.
166
+ * If provided, the {encoding} parameter identifies the character encoding.
167
+ * If not provided, {encoding} defaults to 'utf8'.
168
+ *
169
+ * @param str
170
+ */
171
+ static from(str: string, encoding?: string): Buffer;
172
+
173
+ static from(value, encodingOrOffset, length) {
174
+ if (typeof value === 'string') {
175
+ return fromString(value, encodingOrOffset);
176
+ }
177
+
178
+ if (ArrayBuffer.isView(value)) {
179
+ return fromArrayView(value);
180
+ }
181
+
182
+ if (value == null) {
183
+ throw new TypeError(
184
+ `${
185
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
186
+ 'or Array-like Object. Received type '
187
+ }${typeof value}`
188
+ );
189
+ }
190
+
191
+ if (isInstance(value, ArrayBuffer) || (value && isInstance(value.buffer, ArrayBuffer))) {
192
+ return fromArrayBuffer(value, encodingOrOffset, length);
193
+ }
194
+
195
+ if (
196
+ typeof SharedArrayBuffer !== 'undefined' &&
197
+ (isInstance(value, SharedArrayBuffer) ||
198
+ (value && isInstance(value.buffer, SharedArrayBuffer)))
199
+ ) {
200
+ return fromArrayBuffer(value, encodingOrOffset, length);
201
+ }
202
+
203
+ if (typeof value === 'number') {
204
+ throw new TypeError('The "value" argument must not be of type number. Received type number');
205
+ }
206
+
207
+ const valueOf = value.valueOf && value.valueOf();
208
+ if (valueOf != null && valueOf !== value) {
209
+ return Buffer.from(valueOf, encodingOrOffset, length);
210
+ }
211
+
212
+ const b = fromObject(value);
213
+ if (b) return b;
214
+
215
+ if (
216
+ typeof Symbol !== 'undefined' &&
217
+ Symbol.toPrimitive != null &&
218
+ typeof value[Symbol.toPrimitive] === 'function'
219
+ ) {
220
+ return Buffer.from(value[Symbol.toPrimitive]('string'), encodingOrOffset, length);
221
+ }
222
+
223
+ throw new TypeError(
224
+ `${
225
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
226
+ 'or Array-like Object. Received type '
227
+ }${typeof value}`
228
+ );
229
+ }
230
+
231
+ /**
232
+ * Returns true if {obj} is a Buffer
233
+ *
234
+ * @param obj object to test.
235
+ */
236
+ static isBuffer(b: any): obj is Buffer {
237
+ return b != null && b._isBuffer === true && b !== Buffer.prototype; // so Buffer.isBuffer(Buffer.prototype) will be false
238
+ }
239
+
240
+ /**
241
+ * The same as buf1.compare(buf2).
242
+ */
243
+ static compare(a: Uint8Array | Buffer, b: Uint8Array | Buffer): number {
244
+ if (!Buffer.isBuffer(a) && isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength);
245
+ if (!Buffer.isBuffer(b) && isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength);
246
+ if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
247
+ throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array');
248
+ }
249
+
250
+ if (a === b) return 0;
251
+
252
+ let x = a.length;
253
+ let y = b.length;
254
+
255
+ for (let i = 0, len = Math.min(x, y); i < len; ++i) {
256
+ if (a[i] !== b[i]) {
257
+ x = a[i];
258
+ y = b[i];
259
+ break;
260
+ }
261
+ }
262
+
263
+ if (x < y) return -1;
264
+ if (y < x) return 1;
265
+ return 0;
266
+ }
267
+
268
+ /**
269
+ * Returns true if {encoding} is a valid encoding argument.
270
+ * Valid string encodings in Node 0.12: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
271
+ *
272
+ * @param encoding string to test.
273
+ */
274
+ static isEncoding(encoding: string): boolean {
275
+ switch (String(encoding).toLowerCase()) {
276
+ case 'hex':
277
+ case 'utf8':
278
+ case 'utf-8':
279
+ case 'ascii':
280
+ case 'latin1':
281
+ case 'binary':
282
+ case 'base64':
283
+ case 'ucs2':
284
+ case 'ucs-2':
285
+ case 'utf16le':
286
+ case 'utf-16le':
287
+ return true;
288
+ default:
289
+ return false;
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Returns a buffer which is the result of concatenating all the buffers in the list together.
295
+ *
296
+ * If the list has no items, or if the totalLength is 0, then it returns a zero-length buffer.
297
+ * If the list has exactly one item, then the first item of the list is returned.
298
+ * If the list has more than one item, then a new Buffer is created.
299
+ *
300
+ * @param list An array of Buffer objects to concatenate
301
+ * @param totalLength Total length of the buffers when concatenated.
302
+ * If totalLength is not provided, it is read from the buffers in the list. However, this adds an additional loop to the function, so it is faster to provide the length explicitly.
303
+ */
304
+ static concat(list: (Uint8Array | Buffer)[], length?: number): Buffer {
305
+ if (!Array.isArray(list)) {
306
+ throw new TypeError('"list" argument must be an Array of Buffers');
307
+ }
308
+
309
+ if (list.length === 0) {
310
+ return Buffer.alloc(0);
311
+ }
312
+
313
+ let i;
314
+ if (length === undefined) {
315
+ length = 0;
316
+ for (i = 0; i < list.length; ++i) {
317
+ length += list[i].length;
318
+ }
319
+ }
320
+
321
+ const buffer = Buffer.allocUnsafe(length);
322
+ let pos = 0;
323
+ for (i = 0; i < list.length; ++i) {
324
+ let buf = list[i];
325
+ if (isInstance(buf, Uint8Array)) {
326
+ if (pos + buf.length > buffer.length) {
327
+ if (!Buffer.isBuffer(buf)) {
328
+ buf = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
329
+ }
330
+ buf.copy(buffer, pos);
331
+ } else {
332
+ Uint8Array.prototype.set.call(buffer, buf, pos);
333
+ }
334
+ } else if (!Buffer.isBuffer(buf)) {
335
+ throw new TypeError('"list" argument must be an Array of Buffers');
336
+ } else {
337
+ buf.copy(buffer, pos);
338
+ }
339
+ pos += buf.length;
340
+ }
341
+ return buffer;
342
+ }
343
+
344
+ /**
345
+ * Allocates a new buffer of {size} octets.
346
+ *
347
+ * @param size count of octets to allocate.
348
+ * @param fill if specified, buffer will be initialized by calling buf.fill(fill).
349
+ * If parameter is omitted, buffer will be filled with zeros.
350
+ * @param encoding encoding used for call to buf.fill while initializing
351
+ */
352
+ static alloc(size: number, fill?: string | Buffer | number, encoding?: string): Buffer {
353
+ return alloc(size, fill, encoding);
354
+ }
355
+
356
+ /**
357
+ * Allocates a new buffer of {size} octets, leaving memory not initialized, so the contents
358
+ * of the newly created Buffer are unknown and may contain sensitive data.
359
+ *
360
+ * @param size count of octets to allocate
361
+ */
362
+ static allocUnsafe(size: number): Buffer {
363
+ assertSize(size);
364
+ return new Buffer(size);
365
+ }
366
+
367
+ /**
368
+ * Allocates a new non-pooled buffer of {size} octets, leaving memory not initialized, so the contents
369
+ * of the newly created Buffer are unknown and may contain sensitive data.
370
+ *
371
+ * @param size count of octets to allocate
372
+ */
373
+ static allocUnsafeSlow(size: number): Buffer {
374
+ return allocUnsafe(size);
375
+ }
376
+
377
+ includes(val: string | number | Buffer, byteOffset?: number, encoding?: string): boolean {
378
+ return this.indexOf(val, byteOffset, encoding) !== -1;
379
+ }
380
+
381
+ indexOf(val: string | number | Buffer, byteOffset?: number, encoding?: string): number {
382
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, true);
383
+ }
384
+
385
+ lastIndexOf(val: string | number | Buffer, byteOffset?: number, encoding?: string): number {
386
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, false);
387
+ }
388
+
389
+ readInt8(offset: number, noAssert?: boolean): number {
390
+ offset = offset >>> 0;
391
+ if (!noAssert) checkOffset(offset, 1, this.length);
392
+ if (!(this[offset] & 0x80)) return this[offset];
393
+ return (0xff - this[offset] + 1) * -1;
394
+ }
395
+
396
+ readInt16LE(offset: number, noAssert?: boolean): number {
397
+ offset = offset >>> 0;
398
+ if (!noAssert) checkOffset(offset, 2, this.length);
399
+ const val = this[offset] | (this[offset + 1] << 8);
400
+ return val & 0x8000 ? val | 0xffff0000 : val;
401
+ }
402
+
403
+ readInt16BE(offset: number, noAssert?: boolean): number {
404
+ offset = offset >>> 0;
405
+ if (!noAssert) checkOffset(offset, 2, this.length);
406
+ const val = this[offset + 1] | (this[offset] << 8);
407
+ return val & 0x8000 ? val | 0xffff0000 : val;
408
+ }
409
+
410
+ readInt32LE(offset: number, noAssert?: boolean): number {
411
+ offset = offset >>> 0;
412
+ if (!noAssert) checkOffset(offset, 4, this.length);
413
+
414
+ return (
415
+ this[offset] | (this[offset + 1] << 8) | (this[offset + 2] << 16) | (this[offset + 3] << 24)
416
+ );
417
+ }
418
+
419
+ readInt32BE(offset: number, noAssert?: boolean): number {
420
+ offset = offset >>> 0;
421
+ if (!noAssert) checkOffset(offset, 4, this.length);
422
+
423
+ return (
424
+ (this[offset] << 24) | (this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3]
425
+ );
426
+ }
427
+
428
+ readIntBE(offset: number, byteLength: number, noAssert?: boolean): number {
429
+ offset = offset >>> 0;
430
+ byteLength = byteLength >>> 0;
431
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
432
+
433
+ let i = byteLength;
434
+ let mul = 1;
435
+ let val = this[offset + --i];
436
+ while (i > 0 && (mul *= 0x100)) {
437
+ val += this[offset + --i] * mul;
438
+ }
439
+ mul *= 0x80;
440
+
441
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
442
+
443
+ return val;
444
+ }
445
+
446
+ readIntLE(offset: number, byteLength: number, noAssert?: boolean): number {
447
+ offset = offset >>> 0;
448
+ byteLength = byteLength >>> 0;
449
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
450
+
451
+ let val = this[offset];
452
+ let mul = 1;
453
+ let i = 0;
454
+ while (++i < byteLength && (mul *= 0x100)) {
455
+ val += this[offset + i] * mul;
456
+ }
457
+ mul *= 0x80;
458
+
459
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
460
+
461
+ return val;
462
+ }
463
+
464
+ readBigInt64LE(offset: number): BigInt {
465
+ offset = offset >>> 0;
466
+ validateNumber(offset, 'offset');
467
+ const first = this[offset];
468
+ const last = this[offset + 7];
469
+ if (first === undefined || last === undefined) {
470
+ boundsError(offset, this.length - 8);
471
+ }
472
+
473
+ const val =
474
+ this[offset + 4] + this[offset + 5] * 2 ** 8 + this[offset + 6] * 2 ** 16 + (last << 24); // Overflow
475
+
476
+ return (
477
+ (BigInt(val) << BigInt(32)) +
478
+ BigInt(first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24)
479
+ );
480
+ }
481
+
482
+ readBigInt64BE(offset: number): BigInt {
483
+ offset = offset >>> 0;
484
+ validateNumber(offset, 'offset');
485
+ const first = this[offset];
486
+ const last = this[offset + 7];
487
+ if (first === undefined || last === undefined) {
488
+ boundsError(offset, this.length - 8);
489
+ }
490
+
491
+ const val =
492
+ (first << 24) + // Overflow
493
+ this[++offset] * 2 ** 16 +
494
+ this[++offset] * 2 ** 8 +
495
+ this[++offset];
496
+
497
+ return (
498
+ (BigInt(val) << BigInt(32)) +
499
+ BigInt(this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last)
500
+ );
501
+ }
502
+
503
+ readUInt8(offset: number, noAssert?: boolean): number {
504
+ offset = offset >>> 0;
505
+ if (!noAssert) checkOffset(offset, 1, this.length);
506
+ return this[offset];
507
+ }
508
+
509
+ readUInt16LE(offset: number, noAssert?: boolean): number {
510
+ offset = offset >>> 0;
511
+ if (!noAssert) checkOffset(offset, 2, this.length);
512
+ return this[offset] | (this[offset + 1] << 8);
513
+ }
514
+
515
+ readUInt16BE(offset: number, noAssert?: boolean): number {
516
+ offset = offset >>> 0;
517
+ if (!noAssert) checkOffset(offset, 2, this.length);
518
+ return (this[offset] << 8) | this[offset + 1];
519
+ }
520
+
521
+ readUInt32LE(offset: number, noAssert?: boolean): number {
522
+ offset = offset >>> 0;
523
+ if (!noAssert) checkOffset(offset, 4, this.length);
524
+
525
+ return (
526
+ (this[offset] | (this[offset + 1] << 8) | (this[offset + 2] << 16)) +
527
+ this[offset + 3] * 0x1000000
528
+ );
529
+ }
530
+
531
+ readUInt32BE(offset: number, noAssert?: boolean): number {
532
+ offset = offset >>> 0;
533
+ if (!noAssert) checkOffset(offset, 4, this.length);
534
+
535
+ return (
536
+ this[offset] * 0x1000000 +
537
+ ((this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3])
538
+ );
539
+ }
540
+
541
+ readUIntLE(offset, byteLength, noAssert) {
542
+ offset = offset >>> 0;
543
+ byteLength = byteLength >>> 0;
544
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
545
+
546
+ let val = this[offset];
547
+ let mul = 1;
548
+ let i = 0;
549
+ while (++i < byteLength && (mul *= 0x100)) {
550
+ val += this[offset + i] * mul;
551
+ }
552
+
553
+ return val;
554
+ }
555
+
556
+ readUIntBE(offset, byteLength, noAssert) {
557
+ offset = offset >>> 0;
558
+ byteLength = byteLength >>> 0;
559
+ if (!noAssert) {
560
+ checkOffset(offset, byteLength, this.length);
561
+ }
562
+
563
+ let val = this[offset + --byteLength];
564
+ let mul = 1;
565
+ while (byteLength > 0 && (mul *= 0x100)) {
566
+ val += this[offset + --byteLength] * mul;
567
+ }
568
+
569
+ return val;
570
+ }
571
+
572
+ readBigUInt64LE(offset: number): BigInt {
573
+ offset = offset >>> 0;
574
+ validateNumber(offset, 'offset');
575
+ const first = this[offset];
576
+ const last = this[offset + 7];
577
+ if (first === undefined || last === undefined) {
578
+ boundsError(offset, this.length - 8);
579
+ }
580
+
581
+ const lo =
582
+ first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24;
583
+
584
+ const hi = this[++offset] + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + last * 2 ** 24;
585
+
586
+ return BigInt(lo) + (BigInt(hi) << BigInt(32));
587
+ }
588
+
589
+ readBigUInt64BE(offset: number): BigInt {
590
+ offset = offset >>> 0;
591
+ validateNumber(offset, 'offset');
592
+ const first = this[offset];
593
+ const last = this[offset + 7];
594
+ if (first === undefined || last === undefined) {
595
+ boundsError(offset, this.length - 8);
596
+ }
597
+
598
+ const hi =
599
+ first * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + this[++offset];
600
+
601
+ const lo = this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last;
602
+
603
+ return (BigInt(hi) << BigInt(32)) + BigInt(lo);
604
+ }
605
+
606
+ readFloatLE(offset: number, noAssert?: boolean): number {
607
+ offset = offset >>> 0;
608
+ if (!noAssert) checkOffset(offset, 4, this.length);
609
+ return ieee754.read(this, offset, true, 23, 4);
610
+ }
611
+
612
+ readFloatBE(offset: number, noAssert?: boolean): number {
613
+ offset = offset >>> 0;
614
+ if (!noAssert) checkOffset(offset, 4, this.length);
615
+ return ieee754.read(this, offset, false, 23, 4);
616
+ }
617
+
618
+ readDoubleLE(offset: number, noAssert?: boolean): number {
619
+ offset = offset >>> 0;
620
+ if (!noAssert) checkOffset(offset, 8, this.length);
621
+ return ieee754.read(this, offset, true, 52, 8);
622
+ }
623
+
624
+ readDoubleBE(offset: number, noAssert?: boolean): number {
625
+ offset = offset >>> 0;
626
+ if (!noAssert) checkOffset(offset, 8, this.length);
627
+ return ieee754.read(this, offset, false, 52, 8);
628
+ }
629
+
630
+ writeUInt8(value: number, offset: number, noAssert?: boolean): number {
631
+ value = Number(value);
632
+ offset = offset >>> 0;
633
+ if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
634
+ this[offset] = value & 0xff;
635
+ return offset + 1;
636
+ }
637
+
638
+ writeUInt16LE(value: number, offset: number, noAssert?: boolean): number {
639
+ value = Number(value);
640
+ offset = offset >>> 0;
641
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
642
+ this[offset] = value & 0xff;
643
+ this[offset + 1] = value >>> 8;
644
+ return offset + 2;
645
+ }
646
+
647
+ writeUInt16BE(value: number, offset: number, noAssert?: boolean): number {
648
+ value = Number(value);
649
+ offset = offset >>> 0;
650
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
651
+ this[offset] = value >>> 8;
652
+ this[offset + 1] = value & 0xff;
653
+ return offset + 2;
654
+ }
655
+
656
+ writeUInt32LE(value: number, offset: number, noAssert?: boolean): number {
657
+ value = Number(value);
658
+ offset = offset >>> 0;
659
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
660
+ this[offset + 3] = value >>> 24;
661
+ this[offset + 2] = value >>> 16;
662
+ this[offset + 1] = value >>> 8;
663
+ this[offset] = value & 0xff;
664
+ return offset + 4;
665
+ }
666
+
667
+ writeUInt32BE(value: number, offset: number, noAssert?: boolean): number {
668
+ value = Number(value);
669
+ offset = offset >>> 0;
670
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
671
+ this[offset] = value >>> 24;
672
+ this[offset + 1] = value >>> 16;
673
+ this[offset + 2] = value >>> 8;
674
+ this[offset + 3] = value & 0xff;
675
+ return offset + 4;
676
+ }
677
+
678
+ writeUIntLE(value, offset, byteLength, noAssert) {
679
+ value = Number(value);
680
+ offset = offset >>> 0;
681
+ byteLength = byteLength >>> 0;
682
+ if (!noAssert) {
683
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
684
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
685
+ }
686
+
687
+ let mul = 1;
688
+ let i = 0;
689
+ this[offset] = value & 0xff;
690
+ while (++i < byteLength && (mul *= 0x100)) {
691
+ this[offset + i] = (value / mul) & 0xff;
692
+ }
693
+
694
+ return offset + byteLength;
695
+ }
696
+
697
+ writeUIntBE(value, offset, byteLength, noAssert) {
698
+ value = Number(value);
699
+ offset = offset >>> 0;
700
+ byteLength = byteLength >>> 0;
701
+ if (!noAssert) {
702
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
703
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
704
+ }
705
+
706
+ let i = byteLength - 1;
707
+ let mul = 1;
708
+ this[offset + i] = value & 0xff;
709
+ while (--i >= 0 && (mul *= 0x100)) {
710
+ this[offset + i] = (value / mul) & 0xff;
711
+ }
712
+
713
+ return offset + byteLength;
714
+ }
715
+
716
+ writeBigUInt64LE(value: number, offset: number = 0): BigInt {
717
+ return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'));
718
+ }
719
+
720
+ writeBigUInt64BE(value: number, offset: number = 0): BigInt {
721
+ return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'));
722
+ }
723
+
724
+ writeIntLE(value, offset, byteLength, noAssert) {
725
+ value = Number(value);
726
+ offset = offset >>> 0;
727
+ if (!noAssert) {
728
+ const limit = Math.pow(2, 8 * byteLength - 1);
729
+
730
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
731
+ }
732
+
733
+ let i = 0;
734
+ let mul = 1;
735
+ let sub = 0;
736
+ this[offset] = value & 0xff;
737
+ while (++i < byteLength && (mul *= 0x100)) {
738
+ if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
739
+ sub = 1;
740
+ }
741
+ this[offset + i] = (((value / mul) >> 0) - sub) & 0xff;
742
+ }
743
+
744
+ return offset + byteLength;
745
+ }
746
+
747
+ writeIntBE(value, offset, byteLength, noAssert) {
748
+ value = Number(value);
749
+ offset = offset >>> 0;
750
+ if (!noAssert) {
751
+ const limit = Math.pow(2, 8 * byteLength - 1);
752
+
753
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
754
+ }
755
+
756
+ let i = byteLength - 1;
757
+ let mul = 1;
758
+ let sub = 0;
759
+ this[offset + i] = value & 0xff;
760
+ while (--i >= 0 && (mul *= 0x100)) {
761
+ if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
762
+ sub = 1;
763
+ }
764
+ this[offset + i] = (((value / mul) >> 0) - sub) & 0xff;
765
+ }
766
+
767
+ return offset + byteLength;
768
+ }
769
+
770
+ writeInt8(value: number, offset: number, noAssert?: boolean): number {
771
+ value = Number(value);
772
+ offset = offset >>> 0;
773
+ if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80);
774
+ if (value < 0) value = 0xff + value + 1;
775
+ this[offset] = value & 0xff;
776
+ return offset + 1;
777
+ }
778
+
779
+ writeInt16LE(value: number, offset: number, noAssert?: boolean): number {
780
+ value = Number(value);
781
+ offset = offset >>> 0;
782
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
783
+ this[offset] = value & 0xff;
784
+ this[offset + 1] = value >>> 8;
785
+ return offset + 2;
786
+ }
787
+
788
+ writeInt16BE(value: number, offset: number, noAssert?: boolean): number {
789
+ value = Number(value);
790
+ offset = offset >>> 0;
791
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
792
+ this[offset] = value >>> 8;
793
+ this[offset + 1] = value & 0xff;
794
+ return offset + 2;
795
+ }
796
+
797
+ writeInt32LE(value: number, offset: number, noAssert?: boolean): number {
798
+ value = Number(value);
799
+ offset = offset >>> 0;
800
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
801
+ this[offset] = value & 0xff;
802
+ this[offset + 1] = value >>> 8;
803
+ this[offset + 2] = value >>> 16;
804
+ this[offset + 3] = value >>> 24;
805
+ return offset + 4;
806
+ }
807
+
808
+ writeInt32BE(value: number, offset: number, noAssert?: boolean): number {
809
+ value = Number(value);
810
+ offset = offset >>> 0;
811
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
812
+ if (value < 0) value = 0xffffffff + value + 1;
813
+ this[offset] = value >>> 24;
814
+ this[offset + 1] = value >>> 16;
815
+ this[offset + 2] = value >>> 8;
816
+ this[offset + 3] = value & 0xff;
817
+ return offset + 4;
818
+ }
819
+
820
+ writeBigInt64LE(value: number, offset: number = 0): BigInt {
821
+ return wrtBigUInt64LE(
822
+ this,
823
+ value,
824
+ offset,
825
+ -BigInt('0x8000000000000000'),
826
+ BigInt('0x7fffffffffffffff')
827
+ );
828
+ }
829
+
830
+ writeBigInt64BE(value: number, offset: number = 0): BigInt {
831
+ return wrtBigUInt64BE(
832
+ this,
833
+ value,
834
+ offset,
835
+ -BigInt('0x8000000000000000'),
836
+ BigInt('0x7fffffffffffffff')
837
+ );
838
+ }
839
+
840
+ writeFloatLE(value: number, offset: number, noAssert?: boolean): number {
841
+ return writeFloat(this, value, offset, true, noAssert);
842
+ }
843
+
844
+ writeFloatBE(value: number, offset: number, noAssert?: boolean): number {
845
+ return writeFloat(this, value, offset, false, noAssert);
846
+ }
847
+
848
+ writeDoubleLE(value: number, offset: number, noAssert?: boolean): number {
849
+ return writeDouble(this, value, offset, true, noAssert);
850
+ }
851
+
852
+ writeDoubleBE(value: number, offset: number, noAssert?: boolean): number {
853
+ return writeDouble(this, value, offset, false, noAssert);
854
+ }
855
+
856
+ write(string: string, encoding?: BufferEncoding): number;
857
+ write(string: string, offset: number, encoding?: BufferEncoding): number;
858
+ write(string: string, offset: number, length: number, encoding?: BufferEncoding): number;
859
+ write(string: string, offset, length, encoding): number {
860
+ // Buffer#write(string)
861
+ if (offset === undefined) {
862
+ encoding = 'utf8';
863
+ length = this.length;
864
+ offset = 0;
865
+ // Buffer#write(string, encoding)
866
+ } else if (length === undefined && typeof offset === 'string') {
867
+ encoding = offset;
868
+ length = this.length;
869
+ offset = 0;
870
+ // Buffer#write(string, offset[, length][, encoding])
871
+ } else if (isFinite(offset)) {
872
+ offset = offset >>> 0;
873
+ if (isFinite(length)) {
874
+ length = length >>> 0;
875
+ if (encoding === undefined) encoding = 'utf8';
876
+ } else {
877
+ encoding = length;
878
+ length = undefined;
879
+ }
880
+ } else {
881
+ throw new Error('Buffer.write(string, encoding, offset[, length]) is no longer supported');
882
+ }
883
+
884
+ const remaining = this.length - offset;
885
+ if (length === undefined || length > remaining) length = remaining;
886
+
887
+ if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
888
+ throw new RangeError('Attempt to write outside buffer bounds');
889
+ }
890
+
891
+ if (!encoding) encoding = 'utf8';
892
+
893
+ let loweredCase = false;
894
+ for (;;) {
895
+ switch (encoding) {
896
+ case 'hex':
897
+ return hexWrite(this, string, offset, length);
898
+
899
+ case 'utf8':
900
+ case 'utf-8':
901
+ return utf8Write(this, string, offset, length);
902
+
903
+ case 'ascii':
904
+ case 'latin1':
905
+ case 'binary':
906
+ return asciiWrite(this, string, offset, length);
907
+
908
+ case 'base64':
909
+ // Warning: maxLength not taken into account in base64Write
910
+ return base64Write(this, string, offset, length);
911
+
912
+ case 'ucs2':
913
+ case 'ucs-2':
914
+ case 'utf16le':
915
+ case 'utf-16le':
916
+ return ucs2Write(this, string, offset, length);
917
+
918
+ default:
919
+ if (loweredCase) throw new TypeError(`Unknown encoding: ${encoding}`);
920
+ encoding = `${encoding}`.toLowerCase();
921
+ loweredCase = true;
922
+ }
923
+ }
924
+ }
925
+
926
+ // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
927
+ copy(target: Buffer, targetStart?: number, start?: number, end?: number): number {
928
+ if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer');
929
+ if (!start) start = 0;
930
+ if (!end && end !== 0) end = this.length;
931
+ if (targetStart >= target.length) targetStart = target.length;
932
+ if (!targetStart) targetStart = 0;
933
+ if (end > 0 && end < start) end = start;
934
+
935
+ // Copy 0 bytes; we're done
936
+ if (end === start) return 0;
937
+ if (target.length === 0 || this.length === 0) return 0;
938
+
939
+ // Fatal error conditions
940
+ if (targetStart < 0) {
941
+ throw new RangeError('targetStart out of bounds');
942
+ }
943
+ if (start < 0 || start >= this.length) throw new RangeError('Index out of range');
944
+ if (end < 0) throw new RangeError('sourceEnd out of bounds');
945
+
946
+ // Are we oob?
947
+ if (end > this.length) end = this.length;
948
+ if (target.length - targetStart < end - start) {
949
+ end = target.length - targetStart + start;
950
+ }
951
+
952
+ const len = end - start;
953
+
954
+ if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
955
+ // Use built-in when available, missing from IE11
956
+ this.copyWithin(targetStart, start, end);
957
+ } else {
958
+ Uint8Array.prototype.set.call(target, this.subarray(start, end), targetStart);
959
+ }
960
+
961
+ return len;
962
+ }
963
+
964
+ // Usage:
965
+ // buffer.fill(number[, offset[, end]])
966
+ // buffer.fill(buffer[, offset[, end]])
967
+ // buffer.fill(string[, offset[, end]][, encoding])
968
+ fill(val: any, start?: number, end?: number, encoding?): this {
969
+ // Handle string cases:
970
+ if (typeof val === 'string') {
971
+ if (typeof start === 'string') {
972
+ encoding = start;
973
+ start = 0;
974
+ end = this.length;
975
+ } else if (typeof end === 'string') {
976
+ encoding = end;
977
+ end = this.length;
978
+ }
979
+ if (encoding !== undefined && typeof encoding !== 'string') {
980
+ throw new TypeError('encoding must be a string');
981
+ }
982
+ if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
983
+ throw new TypeError(`Unknown encoding: ${encoding}`);
984
+ }
985
+ if (val.length === 1) {
986
+ const code = val.charCodeAt(0);
987
+ if ((encoding === 'utf8' && code < 128) || encoding === 'latin1') {
988
+ // Fast path: If `val` fits into a single byte, use that numeric value.
989
+ val = code;
990
+ }
991
+ }
992
+ } else if (typeof val === 'number') {
993
+ val = val & 255;
994
+ } else if (typeof val === 'boolean') {
995
+ val = Number(val);
996
+ }
997
+
998
+ // Invalid ranges are not set to a default, so can range check early.
999
+ if (start < 0 || this.length < start || this.length < end) {
1000
+ throw new RangeError('Out of range index');
1001
+ }
1002
+
1003
+ if (end <= start) {
1004
+ return this;
1005
+ }
1006
+
1007
+ start = start >>> 0;
1008
+ end = end === undefined ? this.length : end >>> 0;
1009
+
1010
+ if (!val) val = 0;
1011
+
1012
+ let i;
1013
+ if (typeof val === 'number') {
1014
+ for (i = start; i < end; ++i) {
1015
+ this[i] = val;
1016
+ }
1017
+ } else {
1018
+ const bytes = Buffer.isBuffer(val) ? val : Buffer.from(val, encoding);
1019
+ const len = bytes.length;
1020
+ if (len === 0) {
1021
+ throw new TypeError(`The value "${val}" is invalid for argument "value"`);
1022
+ }
1023
+ for (i = 0; i < end - start; ++i) {
1024
+ this[i + start] = bytes[i % len];
1025
+ }
1026
+ }
1027
+
1028
+ return this;
1029
+ }
1030
+
1031
+ swap16() {
1032
+ const len = this.length;
1033
+ if (len % 2 !== 0) {
1034
+ throw new RangeError('Buffer size must be a multiple of 16-bits');
1035
+ }
1036
+ for (let i = 0; i < len; i += 2) {
1037
+ swap(this, i, i + 1);
1038
+ }
1039
+ return this;
1040
+ }
1041
+
1042
+ swap32() {
1043
+ const len = this.length;
1044
+ if (len % 4 !== 0) {
1045
+ throw new RangeError('Buffer size must be a multiple of 32-bits');
1046
+ }
1047
+ for (let i = 0; i < len; i += 4) {
1048
+ swap(this, i, i + 3);
1049
+ swap(this, i + 1, i + 2);
1050
+ }
1051
+ return this;
1052
+ }
1053
+
1054
+ swap64() {
1055
+ const len = this.length;
1056
+ if (len % 8 !== 0) {
1057
+ throw new RangeError('Buffer size must be a multiple of 64-bits');
1058
+ }
1059
+ for (let i = 0; i < len; i += 8) {
1060
+ swap(this, i, i + 7);
1061
+ swap(this, i + 1, i + 6);
1062
+ swap(this, i + 2, i + 5);
1063
+ swap(this, i + 3, i + 4);
1064
+ }
1065
+ return this;
1066
+ }
1067
+
1068
+ toString(encoding?: string, start?: number, end?: number): string {
1069
+ const length = this.length;
1070
+ if (length === 0) return '';
1071
+ if (arguments.length === 0) return utf8Slice(this, 0, length);
1072
+ return this._slowToString(...arguments);
1073
+ }
1074
+
1075
+ // toLocaleString(b) {
1076
+ // if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer');
1077
+ // if (this === b) return true;
1078
+ // return Buffer.compare(this, b) === 0;
1079
+ // }
1080
+
1081
+ inspect() {
1082
+ let str = '';
1083
+ const max = INSPECT_MAX_BYTES;
1084
+ str = this.toString('hex', 0, max)
1085
+ .replace(/(.{2})/g, '$1 ')
1086
+ .trim();
1087
+ if (this.length > max) str += ' ... ';
1088
+ return `<Buffer ${str}>`;
1089
+ }
1090
+ // if (customInspectSymbol) {
1091
+ // Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
1092
+ // }
1093
+ // }
1094
+
1095
+ equals(b: Buffer): boolean {
1096
+ if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer');
1097
+ if (this === b) return true;
1098
+ return Buffer.compare(this, b) === 0;
1099
+ }
1100
+
1101
+ compare(
1102
+ target: Buffer | Uint8Array,
1103
+ start?: number,
1104
+ end?: number,
1105
+ thisStart?: number,
1106
+ thisEnd?: number
1107
+ ): number {
1108
+ if (!Buffer.isBuffer(target) && isInstance(target, Uint8Array)) {
1109
+ target = Buffer.from(target, target.offset, target.byteLength);
1110
+ }
1111
+ if (!Buffer.isBuffer(target)) {
1112
+ throw new TypeError(
1113
+ `${
1114
+ 'The "target" argument must be one of type Buffer or Uint8Array. ' + 'Received type '
1115
+ }${typeof target}`
1116
+ );
1117
+ }
1118
+
1119
+ if (start === undefined) {
1120
+ start = 0;
1121
+ }
1122
+ if (end === undefined) {
1123
+ end = target ? target.length : 0;
1124
+ }
1125
+ if (thisStart === undefined) {
1126
+ thisStart = 0;
1127
+ }
1128
+ if (thisEnd === undefined) {
1129
+ thisEnd = this.length;
1130
+ }
1131
+
1132
+ if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
1133
+ throw new RangeError('out of range index');
1134
+ }
1135
+
1136
+ if (thisStart >= thisEnd && start >= end) {
1137
+ return 0;
1138
+ }
1139
+ if (thisStart >= thisEnd) {
1140
+ return -1;
1141
+ }
1142
+ if (start >= end) {
1143
+ return 1;
1144
+ }
1145
+
1146
+ start >>>= 0;
1147
+ end >>>= 0;
1148
+ thisStart >>>= 0;
1149
+ thisEnd >>>= 0;
1150
+
1151
+ if (this === target) return 0;
1152
+
1153
+ let x = thisEnd - thisStart;
1154
+ let y = end - start;
1155
+ const len = Math.min(x, y);
1156
+
1157
+ const thisCopy = this.slice(thisStart, thisEnd);
1158
+ const targetCopy = target.slice(start, end);
1159
+
1160
+ for (let i = 0; i < len; ++i) {
1161
+ if (thisCopy[i] !== targetCopy[i]) {
1162
+ x = thisCopy[i];
1163
+ y = targetCopy[i];
1164
+ break;
1165
+ }
1166
+ }
1167
+
1168
+ if (x < y) return -1;
1169
+ if (y < x) return 1;
1170
+ return 0;
1171
+ }
1172
+
1173
+ toJSON(): {type: 'Buffer'; data: any[]} {
1174
+ return {
1175
+ type: 'Buffer',
1176
+ data: Array.prototype.slice.call(this._arr || this, 0)
1177
+ };
1178
+ }
1179
+
1180
+ slice(start?: number, end?: number): this {
1181
+ const len = this.length;
1182
+ start = ~~start;
1183
+ end = end === undefined ? len : ~~end;
1184
+
1185
+ if (start < 0) {
1186
+ start += len;
1187
+ if (start < 0) start = 0;
1188
+ } else if (start > len) {
1189
+ start = len;
1190
+ }
1191
+
1192
+ if (end < 0) {
1193
+ end += len;
1194
+ if (end < 0) end = 0;
1195
+ } else if (end > len) {
1196
+ end = len;
1197
+ }
1198
+
1199
+ if (end < start) end = start;
1200
+
1201
+ const newBuf = this.subarray(start, end);
1202
+
1203
+ // Return an augmented `Uint8Array` instance
1204
+ Object.setPrototypeOf(newBuf, Buffer.prototype);
1205
+
1206
+ return newBuf as unknown as Buffer;
1207
+ }
1208
+
1209
+ // Typo support?
1210
+
1211
+ // readUint8(offset: number, noAssert?: boolean): number {
1212
+ // return this.readUInt8(...arguments);
1213
+ // }
1214
+
1215
+ // readUint16LE(offset: number, noAssert?: boolean): number {
1216
+ // return this.readUInt16LE(...arguments);
1217
+ // }
1218
+
1219
+ // readUint16BE(offset: number, noAssert?: boolean): number {
1220
+ // return this.readUInt16BE(...arguments);
1221
+ // }
1222
+
1223
+ // readUint32LE(offset: number, noAssert?: boolean): number {
1224
+ // return this.readUInt32LE(...arguments);
1225
+ // }
1226
+
1227
+ // readUint32BE(offset: number, noAssert?: boolean): number {
1228
+ // return this.readUInt32BE(...arguments);
1229
+ // }
1230
+
1231
+ // writeUint8() {
1232
+ // return this.writeUInt8(...arguments);
1233
+ // }
1234
+
1235
+ // writeUint16LE
1236
+ // writeUint16LE
1237
+ // writeUint32LE = Buffer.prototype.
1238
+ // Buffer.prototype.writeUint32BE
1239
+
1240
+ protected _slowToString(encoding, start, end) {
1241
+ let loweredCase = false;
1242
+
1243
+ // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
1244
+ // property of a typed array.
1245
+
1246
+ // This behaves neither like String nor Uint8Array in that we set start/end
1247
+ // to their upper/lower bounds if the value passed is out of range.
1248
+ // undefined is handled specially as per ECMA-262 6th Edition,
1249
+ // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
1250
+ if (start === undefined || start < 0) {
1251
+ start = 0;
1252
+ }
1253
+ // Return early if start > this.length. Done here to prevent potential uint32
1254
+ // coercion fail below.
1255
+ if (start > this.length) {
1256
+ return '';
1257
+ }
1258
+
1259
+ if (end === undefined || end > this.length) {
1260
+ end = this.length;
1261
+ }
1262
+
1263
+ if (end <= 0) {
1264
+ return '';
1265
+ }
1266
+
1267
+ // Force coercion to uint32. This will also coerce falsey/NaN values to 0.
1268
+ end >>>= 0;
1269
+ start >>>= 0;
1270
+
1271
+ if (end <= start) {
1272
+ return '';
1273
+ }
1274
+
1275
+ if (!encoding) encoding = 'utf8';
1276
+
1277
+ while (true) {
1278
+ switch (encoding) {
1279
+ case 'hex':
1280
+ return hexSlice(this, start, end);
1281
+
1282
+ case 'utf8':
1283
+ case 'utf-8':
1284
+ return utf8Slice(this, start, end);
1285
+
1286
+ case 'ascii':
1287
+ return asciiSlice(this, start, end);
1288
+
1289
+ case 'latin1':
1290
+ case 'binary':
1291
+ return latin1Slice(this, start, end);
1292
+
1293
+ case 'base64':
1294
+ return base64Slice(this, start, end);
1295
+
1296
+ case 'ucs2':
1297
+ case 'ucs-2':
1298
+ case 'utf16le':
1299
+ case 'utf-16le':
1300
+ return utf16leSlice(this, start, end);
1301
+
1302
+ default:
1303
+ if (loweredCase) throw new TypeError(`Unknown encoding: ${encoding}`);
1304
+ encoding = `${encoding}`.toLowerCase();
1305
+ loweredCase = true;
1306
+ }
1307
+ }
1308
+ }
1309
+ }
1310
+
1311
+ function checkInt(buf: Buffer, value, offset, ext, max, min) {
1312
+ if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance');
1313
+ if (value > max || value < min) throw new RangeError('"value" argument is out of bounds');
1314
+ if (offset + ext > buf.length) throw new RangeError('Index out of range');
1315
+ }
1316
+
1317
+ function wrtBigUInt64LE(buf: Buffer, value, offset, min, max) {
1318
+ checkIntBI(value, min, max, buf, offset, 7);
1319
+
1320
+ let lo = Number(value & BigInt(0xffffffff));
1321
+ buf[offset++] = lo;
1322
+ lo = lo >> 8;
1323
+ buf[offset++] = lo;
1324
+ lo = lo >> 8;
1325
+ buf[offset++] = lo;
1326
+ lo = lo >> 8;
1327
+ buf[offset++] = lo;
1328
+ let hi = Number((value >> BigInt(32)) & BigInt(0xffffffff));
1329
+ buf[offset++] = hi;
1330
+ hi = hi >> 8;
1331
+ buf[offset++] = hi;
1332
+ hi = hi >> 8;
1333
+ buf[offset++] = hi;
1334
+ hi = hi >> 8;
1335
+ buf[offset++] = hi;
1336
+ return offset;
1337
+ }
1338
+
1339
+ function wrtBigUInt64BE(buf: Buffer, value, offset, min, max) {
1340
+ checkIntBI(value, min, max, buf, offset, 7);
1341
+
1342
+ let lo = Number(value & BigInt(0xffffffff));
1343
+ buf[offset + 7] = lo;
1344
+ lo = lo >> 8;
1345
+ buf[offset + 6] = lo;
1346
+ lo = lo >> 8;
1347
+ buf[offset + 5] = lo;
1348
+ lo = lo >> 8;
1349
+ buf[offset + 4] = lo;
1350
+ let hi = Number((value >> BigInt(32)) & BigInt(0xffffffff));
1351
+ buf[offset + 3] = hi;
1352
+ hi = hi >> 8;
1353
+ buf[offset + 2] = hi;
1354
+ hi = hi >> 8;
1355
+ buf[offset + 1] = hi;
1356
+ hi = hi >> 8;
1357
+ buf[offset] = hi;
1358
+ return offset + 8;
1359
+ }
1360
+
1361
+ // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
1362
+ // https://github.com/feross/buffer/pull/148
1363
+ // Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
1364
+ // Object.setPrototypeOf(Buffer, Uint8Array);
1365
+
1366
+ function assertSize(size: number): void {
1367
+ if (typeof size !== 'number') {
1368
+ throw new TypeError('"size" argument must be of type number');
1369
+ } else if (size < 0) {
1370
+ throw new RangeError(`The value "${size}" is invalid for option "size"`);
1371
+ }
1372
+ }
1373
+
1374
+ function alloc(size, fill, encoding): Buffer {
1375
+ assertSize(size);
1376
+ if (size <= 0) {
1377
+ return new Buffer(size);
1378
+ }
1379
+ if (fill !== undefined) {
1380
+ // Only pay attention to encoding if it's a string. This
1381
+ // prevents accidentally sending in a number that would
1382
+ // be interpreted as a start offset.
1383
+ return typeof encoding === 'string'
1384
+ ? new Buffer(size).fill(fill, encoding)
1385
+ : new Buffer(size).fill(fill);
1386
+ }
1387
+ return new Buffer(size);
1388
+ }
1389
+
1390
+ function fromString(string: string, encoding: BufferEncoding): Buffer {
1391
+ if (typeof encoding !== 'string' || encoding === '') {
1392
+ encoding = 'utf8';
1393
+ }
1394
+
1395
+ if (!Buffer.isEncoding(encoding)) {
1396
+ throw new TypeError(`Unknown encoding: ${encoding}`);
1397
+ }
1398
+
1399
+ const length = byteLength(string, encoding) | 0;
1400
+ let buf = new Buffer(length);
1401
+
1402
+ const actual = buf.write(string, encoding);
1403
+
1404
+ if (actual !== length) {
1405
+ // Writing a hex string, for example, that contains invalid characters will
1406
+ // cause everything after the first invalid character to be ignored. (e.g.
1407
+ // 'abxxcd' will be treated as 'ab')
1408
+ buf = buf.slice(0, actual);
1409
+ }
1410
+
1411
+ return buf;
1412
+ }
1413
+
1414
+ function fromArrayLike(array: ArrayLike<any>): Buffer {
1415
+ const length = array.length < 0 ? 0 : checked(array.length) | 0;
1416
+ const buf = new Buffer(length);
1417
+ for (let i = 0; i < length; i += 1) {
1418
+ buf[i] = array[i] & 255;
1419
+ }
1420
+ return buf;
1421
+ }
1422
+
1423
+ function fromArrayView(arrayView): Buffer {
1424
+ if (isInstance(arrayView, Uint8Array)) {
1425
+ const copy = new Uint8Array(arrayView);
1426
+ return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength);
1427
+ }
1428
+ return fromArrayLike(arrayView);
1429
+ }
1430
+
1431
+ function fromArrayBuffer(array: ArrayBuffer, byteOffset: number, length: number): Buffer {
1432
+ if (byteOffset < 0 || array.byteLength < byteOffset) {
1433
+ throw new RangeError('"offset" is outside of buffer bounds');
1434
+ }
1435
+
1436
+ if (array.byteLength < byteOffset + (length || 0)) {
1437
+ throw new RangeError('"length" is outside of buffer bounds');
1438
+ }
1439
+
1440
+ let buf;
1441
+ if (byteOffset === undefined && length === undefined) {
1442
+ buf = new Uint8Array(array);
1443
+ } else if (length === undefined) {
1444
+ buf = new Uint8Array(array, byteOffset);
1445
+ } else {
1446
+ buf = new Uint8Array(array, byteOffset, length);
1447
+ }
1448
+
1449
+ // Return an augmented `Uint8Array` instance
1450
+ Object.setPrototypeOf(buf, Buffer.prototype);
1451
+
1452
+ return buf;
1453
+ }
1454
+
1455
+ function fromObject(obj) {
1456
+ if (Buffer.isBuffer(obj)) {
1457
+ const len = checked(obj.length) | 0;
1458
+ const buf = new Buffer(len);
1459
+
1460
+ if (buf.length === 0) {
1461
+ return buf;
1462
+ }
1463
+
1464
+ obj.copy(buf, 0, 0, len);
1465
+ return buf;
1466
+ }
1467
+
1468
+ if (obj.length !== undefined) {
1469
+ if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
1470
+ return new Buffer(0);
1471
+ }
1472
+ return fromArrayLike(obj);
1473
+ }
1474
+
1475
+ if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
1476
+ return fromArrayLike(obj.data);
1477
+ }
1478
+ }
1479
+
1480
+ function checked(length) {
1481
+ // Note: cannot use `length < kMaxLength` here because that fails when
1482
+ // length is NaN (which is otherwise coerced to zero.)
1483
+ if (length >= kMaxLength) {
1484
+ throw new RangeError(
1485
+ `${'Attempt to allocate Buffer larger than maximum ' + 'size: 0x'}${kMaxLength.toString(
1486
+ 16
1487
+ )} bytes`
1488
+ );
1489
+ }
1490
+ return length | 0;
1491
+ }
1492
+
1493
+ function byteLength(string, encoding) {
1494
+ if (Buffer.isBuffer(string)) {
1495
+ return string.length;
1496
+ }
1497
+ if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
1498
+ return string.byteLength;
1499
+ }
1500
+ if (typeof string !== 'string') {
1501
+ throw new TypeError(
1502
+ `${
1503
+ 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
1504
+ 'Received type '
1505
+ }${typeof string}`
1506
+ );
1507
+ }
1508
+
1509
+ const len = string.length;
1510
+ const mustMatch = arguments.length > 2 && arguments[2] === true;
1511
+ if (!mustMatch && len === 0) return 0;
1512
+
1513
+ // Use a for loop to avoid recursion
1514
+ let loweredCase = false;
1515
+ for (;;) {
1516
+ switch (encoding) {
1517
+ case 'ascii':
1518
+ case 'latin1':
1519
+ case 'binary':
1520
+ return len;
1521
+ case 'utf8':
1522
+ case 'utf-8':
1523
+ return utf8ToBytes(string).length;
1524
+ case 'ucs2':
1525
+ case 'ucs-2':
1526
+ case 'utf16le':
1527
+ case 'utf-16le':
1528
+ return len * 2;
1529
+ case 'hex':
1530
+ return len >>> 1;
1531
+ case 'base64':
1532
+ return base64ToBytes(string).length;
1533
+ default:
1534
+ if (loweredCase) {
1535
+ return mustMatch ? -1 : utf8ToBytes(string).length; // assume utf8
1536
+ }
1537
+ encoding = `${encoding}`.toLowerCase();
1538
+ loweredCase = true;
1539
+ }
1540
+ }
1541
+ }
1542
+
1543
+ // Buffer.byteLength = byteLength;
1544
+
1545
+ function swap(b, n, m) {
1546
+ const i = b[n];
1547
+ b[n] = b[m];
1548
+ b[m] = i;
1549
+ }
1550
+
1551
+ // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
1552
+ // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
1553
+ //
1554
+ // Arguments:
1555
+ // - buffer - a Buffer to search
1556
+ // - val - a string, Buffer, or number
1557
+ // - byteOffset - an index into `buffer`; will be clamped to an int32
1558
+ // - encoding - an optional encoding, relevant is val is a string
1559
+ // - dir - true for indexOf, false for lastIndexOf
1560
+ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
1561
+ // Empty buffer means no match
1562
+ if (buffer.length === 0) return -1;
1563
+
1564
+ // Normalize byteOffset
1565
+ if (typeof byteOffset === 'string') {
1566
+ encoding = byteOffset;
1567
+ byteOffset = 0;
1568
+ } else if (byteOffset > 0x7fffffff) {
1569
+ byteOffset = 0x7fffffff;
1570
+ } else if (byteOffset < -0x80000000) {
1571
+ byteOffset = -0x80000000;
1572
+ }
1573
+ byteOffset = Number(byteOffset); // Coerce to Number.
1574
+ if (numberIsNaN(byteOffset)) {
1575
+ // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
1576
+ byteOffset = dir ? 0 : buffer.length - 1;
1577
+ }
1578
+
1579
+ // Normalize byteOffset: negative offsets start from the end of the buffer
1580
+ if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
1581
+ if (byteOffset >= buffer.length) {
1582
+ if (dir) return -1;
1583
+ byteOffset = buffer.length - 1;
1584
+ } else if (byteOffset < 0) {
1585
+ if (dir) byteOffset = 0;
1586
+ else return -1;
1587
+ }
1588
+
1589
+ // Normalize val
1590
+ if (typeof val === 'string') {
1591
+ val = Buffer.from(val, encoding);
1592
+ }
1593
+
1594
+ // Finally, search either indexOf (if dir is true) or lastIndexOf
1595
+ if (Buffer.isBuffer(val)) {
1596
+ // Special case: looking for empty string/buffer always fails
1597
+ if (val.length === 0) {
1598
+ return -1;
1599
+ }
1600
+ return arrayIndexOf(buffer, val, byteOffset, encoding, dir);
1601
+ } else if (typeof val === 'number') {
1602
+ val = val & 0xff; // Search for a byte value [0-255]
1603
+ if (typeof Uint8Array.prototype.indexOf === 'function') {
1604
+ if (dir) {
1605
+ return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset);
1606
+ }
1607
+ return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset);
1608
+ }
1609
+ return arrayIndexOf(buffer, [val], byteOffset, encoding, dir);
1610
+ }
1611
+
1612
+ throw new TypeError('val must be string, number or Buffer');
1613
+ }
1614
+
1615
+ function arrayIndexOf(arr, val, byteOffset, encoding, dir) {
1616
+ let indexSize = 1;
1617
+ let arrLength = arr.length;
1618
+ let valLength = val.length;
1619
+
1620
+ if (encoding !== undefined) {
1621
+ encoding = String(encoding).toLowerCase();
1622
+ if (
1623
+ encoding === 'ucs2' ||
1624
+ encoding === 'ucs-2' ||
1625
+ encoding === 'utf16le' ||
1626
+ encoding === 'utf-16le'
1627
+ ) {
1628
+ if (arr.length < 2 || val.length < 2) {
1629
+ return -1;
1630
+ }
1631
+ indexSize = 2;
1632
+ arrLength /= 2;
1633
+ valLength /= 2;
1634
+ byteOffset /= 2;
1635
+ }
1636
+ }
1637
+
1638
+ function read(buf, i) {
1639
+ if (indexSize === 1) {
1640
+ return buf[i];
1641
+ }
1642
+ return buf.readUInt16BE(i * indexSize);
1643
+ }
1644
+
1645
+ let i;
1646
+ if (dir) {
1647
+ let foundIndex = -1;
1648
+ for (i = byteOffset; i < arrLength; i++) {
1649
+ if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
1650
+ if (foundIndex === -1) foundIndex = i;
1651
+ if (i - foundIndex + 1 === valLength) return foundIndex * indexSize;
1652
+ } else {
1653
+ if (foundIndex !== -1) i -= i - foundIndex;
1654
+ foundIndex = -1;
1655
+ }
1656
+ }
1657
+ } else {
1658
+ if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
1659
+ for (i = byteOffset; i >= 0; i--) {
1660
+ let found = true;
1661
+ for (let j = 0; j < valLength; j++) {
1662
+ if (read(arr, i + j) !== read(val, j)) {
1663
+ found = false;
1664
+ break;
1665
+ }
1666
+ }
1667
+ if (found) return i;
1668
+ }
1669
+ }
1670
+
1671
+ return -1;
1672
+ }
1673
+
1674
+ function hexWrite(buf: Buffer, string, offset, length): number {
1675
+ offset = Number(offset) || 0;
1676
+ const remaining = buf.length - offset;
1677
+ if (!length) {
1678
+ length = remaining;
1679
+ } else {
1680
+ length = Number(length);
1681
+ if (length > remaining) {
1682
+ length = remaining;
1683
+ }
1684
+ }
1685
+
1686
+ const strLen = string.length;
1687
+
1688
+ if (length > strLen / 2) {
1689
+ length = strLen / 2;
1690
+ }
1691
+ let i;
1692
+ for (i = 0; i < length; ++i) {
1693
+ const parsed = parseInt(string.substr(i * 2, 2), 16);
1694
+ if (numberIsNaN(parsed)) return i;
1695
+ buf[offset + i] = parsed;
1696
+ }
1697
+ return i;
1698
+ }
1699
+
1700
+ function utf8Write(buf: Buffer, string, offset, length) {
1701
+ return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length);
1702
+ }
1703
+
1704
+ function asciiWrite(buf: Buffer, string, offset, length) {
1705
+ return blitBuffer(asciiToBytes(string), buf, offset, length);
1706
+ }
1707
+
1708
+ function base64Write(buf: Buffer, string, offset, length) {
1709
+ return blitBuffer(base64ToBytes(string), buf, offset, length);
1710
+ }
1711
+
1712
+ function ucs2Write(buf: Buffer, string, offset, length) {
1713
+ return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length);
1714
+ }
1715
+
1716
+ function base64Slice(buf: Buffer, start, end) {
1717
+ if (start === 0 && end === buf.length) {
1718
+ return base64.fromByteArray(buf);
1719
+ }
1720
+ return base64.fromByteArray(buf.slice(start, end));
1721
+ }
1722
+
1723
+ function utf8Slice(buf: Buffer, start, end) {
1724
+ end = Math.min(buf.length, end);
1725
+ const res: number[] = [];
1726
+
1727
+ let i = start;
1728
+ while (i < end) {
1729
+ const firstByte = buf[i];
1730
+ let codePoint: number | null = null;
1731
+ let bytesPerSequence = firstByte > 0xef ? 4 : firstByte > 0xdf ? 3 : firstByte > 0xbf ? 2 : 1;
1732
+
1733
+ if (i + bytesPerSequence <= end) {
1734
+ let fourthByte;
1735
+ let secondByte;
1736
+ let tempCodePoint;
1737
+ let thirdByte;
1738
+
1739
+ switch (bytesPerSequence) {
1740
+ case 1:
1741
+ if (firstByte < 0x80) {
1742
+ codePoint = firstByte;
1743
+ }
1744
+ break;
1745
+ case 2:
1746
+ secondByte = buf[i + 1];
1747
+ if ((secondByte & 0xc0) === 0x80) {
1748
+ tempCodePoint = ((firstByte & 0x1f) << 0x6) | (secondByte & 0x3f);
1749
+ if (tempCodePoint > 0x7f) {
1750
+ codePoint = tempCodePoint;
1751
+ }
1752
+ }
1753
+ break;
1754
+ case 3:
1755
+ secondByte = buf[i + 1];
1756
+ thirdByte = buf[i + 2];
1757
+ if ((secondByte & 0xc0) === 0x80 && (thirdByte & 0xc0) === 0x80) {
1758
+ tempCodePoint =
1759
+ ((firstByte & 0xf) << 0xc) | ((secondByte & 0x3f) << 0x6) | (thirdByte & 0x3f);
1760
+ if (tempCodePoint > 0x7ff && (tempCodePoint < 0xd800 || tempCodePoint > 0xdfff)) {
1761
+ codePoint = tempCodePoint;
1762
+ }
1763
+ }
1764
+ break;
1765
+ case 4:
1766
+ secondByte = buf[i + 1];
1767
+ thirdByte = buf[i + 2];
1768
+ fourthByte = buf[i + 3];
1769
+ if (
1770
+ (secondByte & 0xc0) === 0x80 &&
1771
+ (thirdByte & 0xc0) === 0x80 &&
1772
+ (fourthByte & 0xc0) === 0x80
1773
+ ) {
1774
+ tempCodePoint =
1775
+ ((firstByte & 0xf) << 0x12) |
1776
+ ((secondByte & 0x3f) << 0xc) |
1777
+ ((thirdByte & 0x3f) << 0x6) |
1778
+ (fourthByte & 0x3f);
1779
+ if (tempCodePoint > 0xffff && tempCodePoint < 0x110000) {
1780
+ codePoint = tempCodePoint;
1781
+ }
1782
+ }
1783
+ }
1784
+ }
1785
+
1786
+ if (codePoint === null) {
1787
+ // we did not generate a valid codePoint so insert a
1788
+ // replacement char (U+FFFD) and advance only 1 byte
1789
+ codePoint = 0xfffd;
1790
+ bytesPerSequence = 1;
1791
+ } else if (codePoint > 0xffff) {
1792
+ // encode to utf16 (surrogate pair dance)
1793
+ codePoint -= 0x10000;
1794
+ res.push(((codePoint >>> 10) & 0x3ff) | 0xd800);
1795
+ codePoint = 0xdc00 | (codePoint & 0x3ff);
1796
+ }
1797
+
1798
+ res.push(codePoint);
1799
+ i += bytesPerSequence;
1800
+ }
1801
+
1802
+ return decodeCodePointsArray(res);
1803
+ }
1804
+
1805
+ // Based on http://stackoverflow.com/a/22747272/680742, the browser with
1806
+ // the lowest limit is Chrome, with 0x10000 args.
1807
+ // We go 1 magnitude less, for safety
1808
+ const MAX_ARGUMENTS_LENGTH = 0x1000;
1809
+
1810
+ function decodeCodePointsArray(codePoints: number[]): string {
1811
+ const len = codePoints.length;
1812
+ if (len <= MAX_ARGUMENTS_LENGTH) {
1813
+ return String.fromCharCode.apply(String, codePoints); // avoid extra slice()
1814
+ }
1815
+
1816
+ // Decode in chunks to avoid "call stack size exceeded".
1817
+ let res = '';
1818
+ let i = 0;
1819
+ while (i < len) {
1820
+ res += String.fromCharCode.apply(String, codePoints.slice(i, (i += MAX_ARGUMENTS_LENGTH)));
1821
+ }
1822
+ return res;
1823
+ }
1824
+
1825
+ function asciiSlice(buf: Buffer, start, end): string {
1826
+ let ret = '';
1827
+ end = Math.min(buf.length, end);
1828
+
1829
+ for (let i = start; i < end; ++i) {
1830
+ ret += String.fromCharCode(buf[i] & 0x7f);
1831
+ }
1832
+ return ret;
1833
+ }
1834
+
1835
+ function latin1Slice(buf: Buffer, start, end): string {
1836
+ let ret = '';
1837
+ end = Math.min(buf.length, end);
1838
+
1839
+ for (let i = start; i < end; ++i) {
1840
+ ret += String.fromCharCode(buf[i]);
1841
+ }
1842
+ return ret;
1843
+ }
1844
+
1845
+ function hexSlice(buf: Buffer, start, end): string {
1846
+ const len = buf.length;
1847
+
1848
+ if (!start || start < 0) start = 0;
1849
+ if (!end || end < 0 || end > len) end = len;
1850
+
1851
+ let out = '';
1852
+ for (let i = start; i < end; ++i) {
1853
+ out += hexSliceLookupTable[buf[i]];
1854
+ }
1855
+ return out;
1856
+ }
1857
+
1858
+ function utf16leSlice(buf: Buffer, start, end): string {
1859
+ const bytes = buf.slice(start, end);
1860
+ let res = '';
1861
+ // If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
1862
+ for (let i = 0; i < bytes.length - 1; i += 2) {
1863
+ res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
1864
+ }
1865
+ return res;
1866
+ }
1867
+
1868
+ /*
1869
+ * Need to make sure that buffer isn't trying to write out of bounds.
1870
+ */
1871
+ function checkOffset(offset, ext, length): void {
1872
+ if (offset % 1 !== 0 || offset < 0) throw new RangeError('offset is not uint');
1873
+ if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length');
1874
+ }
1875
+
1876
+ function checkIEEE754(buf: Buffer, value, offset, ext, max, min): void {
1877
+ if (offset + ext > buf.length) throw new RangeError('Index out of range');
1878
+ if (offset < 0) throw new RangeError('Index out of range');
1879
+ }
1880
+
1881
+ function writeFloat(buf: Buffer, value, offset, littleEndian, noAssert): number {
1882
+ value = Number(value);
1883
+ offset = offset >>> 0;
1884
+ if (!noAssert) {
1885
+ checkIEEE754(buf, value, offset, 4, 3.4028234663852886e38, -3.4028234663852886e38);
1886
+ }
1887
+ ieee754.write(buf, value, offset, littleEndian, 23, 4);
1888
+ return offset + 4;
1889
+ }
1890
+
1891
+ function writeDouble(buf: Buffer, value, offset, littleEndian, noAssert): number {
1892
+ value = Number(value);
1893
+ offset = offset >>> 0;
1894
+ if (!noAssert) {
1895
+ checkIEEE754(buf, value, offset, 8, 1.7976931348623157e308, -1.7976931348623157e308);
1896
+ }
1897
+ ieee754.write(buf, value, offset, littleEndian, 52, 8);
1898
+ return offset + 8;
1899
+ }
1900
+
1901
+ // CUSTOM ERRORS
1902
+ // =============
1903
+
1904
+ // Simplified versions from Node, changed for Buffer-only usage
1905
+ const errors: Record<string, any> = {};
1906
+ function E(sym, getMessage, Base) {
1907
+ errors[sym] = class NodeError extends Base {
1908
+ constructor() {
1909
+ super();
1910
+
1911
+ Object.defineProperty(this, 'message', {
1912
+ value: getMessage.apply(this, arguments),
1913
+ writable: true,
1914
+ configurable: true
1915
+ });
1916
+
1917
+ // Add the error code to the name to include it in the stack trace.
1918
+ this.name = `${this.name} [${sym}]`;
1919
+ // Access the stack to generate the error message including the error code
1920
+ // from the name.
1921
+ this.stack; // eslint-disable-line no-unused-expressions
1922
+ // Reset the name to the actual name.
1923
+ delete this.name;
1924
+ }
1925
+
1926
+ get code() {
1927
+ return sym;
1928
+ }
1929
+
1930
+ set code(value) {
1931
+ Object.defineProperty(this, 'code', {
1932
+ configurable: true,
1933
+ enumerable: true,
1934
+ value,
1935
+ writable: true
1936
+ });
1937
+ }
1938
+
1939
+ toString() {
1940
+ return `${this.name} [${sym}]: ${this.message}`;
1941
+ }
1942
+ };
1943
+ }
1944
+
1945
+ E(
1946
+ 'ERR_BUFFER_OUT_OF_BOUNDS',
1947
+ function (name) {
1948
+ if (name) {
1949
+ return `${name} is outside of buffer bounds`;
1950
+ }
1951
+
1952
+ return 'Attempt to access memory outside buffer bounds';
1953
+ },
1954
+ RangeError
1955
+ );
1956
+ E(
1957
+ 'ERR_INVALID_ARG_TYPE',
1958
+ function (name, actual) {
1959
+ return `The "${name}" argument must be of type number. Received type ${typeof actual}`;
1960
+ },
1961
+ TypeError
1962
+ );
1963
+ E(
1964
+ 'ERR_OUT_OF_RANGE',
1965
+ function (str, range, input) {
1966
+ let msg = `The value of "${str}" is out of range.`;
1967
+ let received = input;
1968
+ if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
1969
+ received = addNumericalSeparator(String(input));
1970
+ } else if (typeof input === 'bigint') {
1971
+ received = String(input);
1972
+ if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) {
1973
+ received = addNumericalSeparator(received);
1974
+ }
1975
+ received += 'n';
1976
+ }
1977
+ msg += ` It must be ${range}. Received ${received}`;
1978
+ return msg;
1979
+ },
1980
+ RangeError
1981
+ );
1982
+
1983
+ function addNumericalSeparator(val) {
1984
+ let res = '';
1985
+ let i = val.length;
1986
+ const start = val[0] === '-' ? 1 : 0;
1987
+ for (; i >= start + 4; i -= 3) {
1988
+ res = `_${val.slice(i - 3, i)}${res}`;
1989
+ }
1990
+ return `${val.slice(0, i)}${res}`;
1991
+ }
1992
+
1993
+ // CHECK FUNCTIONS
1994
+ // ===============
1995
+
1996
+ function checkBounds(buf, offset, byteLength) {
1997
+ validateNumber(offset, 'offset');
1998
+ if (buf[offset] === undefined || buf[offset + byteLength] === undefined) {
1999
+ boundsError(offset, buf.length - (byteLength + 1));
2000
+ }
2001
+ }
2002
+
2003
+ function checkIntBI(value, min, max, buf, offset, byteLength) {
2004
+ if (value > max || value < min) {
2005
+ const n = typeof min === 'bigint' ? 'n' : '';
2006
+ let range;
2007
+ if (byteLength > 3) {
2008
+ if (min === 0 || min === BigInt(0)) {
2009
+ range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`;
2010
+ } else {
2011
+ range =
2012
+ `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ` +
2013
+ `${(byteLength + 1) * 8 - 1}${n}`;
2014
+ }
2015
+ } else {
2016
+ range = `>= ${min}${n} and <= ${max}${n}`;
2017
+ }
2018
+ throw new errors.ERR_OUT_OF_RANGE('value', range, value);
2019
+ }
2020
+ checkBounds(buf, offset, byteLength);
2021
+ }
2022
+
2023
+ function validateNumber(value, name) {
2024
+ if (typeof value !== 'number') {
2025
+ throw new errors.ERR_INVALID_ARG_TYPE(name, 'number', value);
2026
+ }
2027
+ }
2028
+
2029
+ function boundsError(value, length, type?) {
2030
+ if (Math.floor(value) !== value) {
2031
+ validateNumber(value, type);
2032
+ throw new errors.ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value);
2033
+ }
2034
+
2035
+ if (length < 0) {
2036
+ throw new errors.ERR_BUFFER_OUT_OF_BOUNDS();
2037
+ }
2038
+
2039
+ throw new errors.ERR_OUT_OF_RANGE(type || 'offset', `>= ${type ? 1 : 0} and <= ${length}`, value);
2040
+ }
2041
+
2042
+ // HELPER FUNCTIONS
2043
+ // ================
2044
+
2045
+ const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
2046
+
2047
+ function base64clean(str: string): string {
2048
+ // Node takes equal signs as end of the Base64 encoding
2049
+ str = str.split('=')[0];
2050
+ // Node strips out invalid characters like \n and \t from the string, base64-js does not
2051
+ str = str.trim().replace(INVALID_BASE64_RE, '');
2052
+ // Node converts strings with length < 2 to ''
2053
+ if (str.length < 2) return '';
2054
+ // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
2055
+ while (str.length % 4 !== 0) {
2056
+ str = `${str}=`;
2057
+ }
2058
+ return str;
2059
+ }
2060
+
2061
+ function utf8ToBytes(string: string, units?: number): number[] {
2062
+ units = units || Infinity;
2063
+ let codePoint;
2064
+ const length = string.length;
2065
+ let leadSurrogate = null;
2066
+ const bytes: number[] = [];
2067
+
2068
+ for (let i = 0; i < length; ++i) {
2069
+ codePoint = string.charCodeAt(i);
2070
+
2071
+ // is surrogate component
2072
+ if (codePoint > 0xd7ff && codePoint < 0xe000) {
2073
+ // last char was a lead
2074
+ if (!leadSurrogate) {
2075
+ // no lead yet
2076
+ if (codePoint > 0xdbff) {
2077
+ // unexpected trail
2078
+ if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd);
2079
+ continue;
2080
+ } else if (i + 1 === length) {
2081
+ // unpaired lead
2082
+ if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd);
2083
+ continue;
2084
+ }
2085
+
2086
+ // valid lead
2087
+ leadSurrogate = codePoint;
2088
+
2089
+ continue;
2090
+ }
2091
+
2092
+ // 2 leads in a row
2093
+ if (codePoint < 0xdc00) {
2094
+ if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd);
2095
+ leadSurrogate = codePoint;
2096
+ continue;
2097
+ }
2098
+
2099
+ // valid surrogate pair
2100
+ codePoint = (((leadSurrogate - 0xd800) << 10) | (codePoint - 0xdc00)) + 0x10000;
2101
+ } else if (leadSurrogate) {
2102
+ // valid bmp char, but last char was a lead
2103
+ if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd);
2104
+ }
2105
+
2106
+ leadSurrogate = null;
2107
+
2108
+ // encode utf8
2109
+ if (codePoint < 0x80) {
2110
+ if ((units -= 1) < 0) break;
2111
+ bytes.push(codePoint);
2112
+ } else if (codePoint < 0x800) {
2113
+ if ((units -= 2) < 0) break;
2114
+ bytes.push((codePoint >> 0x6) | 0xc0, (codePoint & 0x3f) | 0x80);
2115
+ } else if (codePoint < 0x10000) {
2116
+ if ((units -= 3) < 0) break;
2117
+ bytes.push(
2118
+ (codePoint >> 0xc) | 0xe0,
2119
+ ((codePoint >> 0x6) & 0x3f) | 0x80,
2120
+ (codePoint & 0x3f) | 0x80
2121
+ );
2122
+ } else if (codePoint < 0x110000) {
2123
+ if ((units -= 4) < 0) break;
2124
+ bytes.push(
2125
+ (codePoint >> 0x12) | 0xf0,
2126
+ ((codePoint >> 0xc) & 0x3f) | 0x80,
2127
+ ((codePoint >> 0x6) & 0x3f) | 0x80,
2128
+ (codePoint & 0x3f) | 0x80
2129
+ );
2130
+ } else {
2131
+ throw new Error('Invalid code point');
2132
+ }
2133
+ }
2134
+
2135
+ return bytes;
2136
+ }
2137
+
2138
+ function asciiToBytes(str: string): number[] {
2139
+ const byteArray: number[] = [];
2140
+ for (let i = 0; i < str.length; ++i) {
2141
+ // Node's code seems to be doing this and not & 0x7F..
2142
+ byteArray.push(str.charCodeAt(i) & 0xff);
2143
+ }
2144
+ return byteArray;
2145
+ }
2146
+
2147
+ function utf16leToBytes(str: string, units: number): number[] {
2148
+ let c;
2149
+ let hi;
2150
+ let lo;
2151
+ const byteArray: number[] = [];
2152
+ for (let i = 0; i < str.length; ++i) {
2153
+ if ((units -= 2) < 0) break;
2154
+
2155
+ c = str.charCodeAt(i);
2156
+ hi = c >> 8;
2157
+ lo = c % 256;
2158
+ byteArray.push(lo);
2159
+ byteArray.push(hi);
2160
+ }
2161
+
2162
+ return byteArray;
2163
+ }
2164
+
2165
+ function base64ToBytes(str: string): Uint8Array {
2166
+ return base64.toByteArray(base64clean(str));
2167
+ }
2168
+
2169
+ function blitBuffer(src, dst, offset, length): number {
2170
+ let i;
2171
+ for (i = 0; i < length; ++i) {
2172
+ if (i + offset >= dst.length || i >= src.length) break;
2173
+ dst[i + offset] = src[i];
2174
+ }
2175
+ return i;
2176
+ }
2177
+
2178
+ // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
2179
+ // the `instanceof` check but they should be treated as of that type.
2180
+ // See: https://github.com/feross/buffer/issues/166
2181
+ function isInstance(obj, type): boolean {
2182
+ return (
2183
+ obj instanceof type ||
2184
+ (obj != null &&
2185
+ obj.constructor != null &&
2186
+ obj.constructor.name != null &&
2187
+ obj.constructor.name === type.name)
2188
+ );
2189
+ }
2190
+ function numberIsNaN(obj): boolean {
2191
+ // For IE11 support
2192
+ return obj !== obj; // eslint-disable-line no-self-compare
2193
+ }
2194
+
2195
+ // Create lookup table for `toString('hex')`
2196
+ // See: https://github.com/feross/buffer/issues/219
2197
+ const hexSliceLookupTable = (function () {
2198
+ const alphabet = '0123456789abcdef';
2199
+ const table = new Array(256);
2200
+ for (let i = 0; i < 16; ++i) {
2201
+ const i16 = i * 16;
2202
+ for (let j = 0; j < 16; ++j) {
2203
+ table[i16 + j] = alphabet[i] + alphabet[j];
2204
+ }
2205
+ }
2206
+ return table;
2207
+ })();