@loaders.gl/parquet 4.2.0-alpha.4 → 4.2.0-alpha.5

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