@nktkas/hyperliquid 0.13.0 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +42 -17
  3. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  4. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
  6. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  7. package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
  9. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  10. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
  12. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  13. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
  14. package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  15. package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
  17. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  18. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
  20. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  21. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
  23. package/esm/mod.d.ts +15 -14
  24. package/esm/mod.d.ts.map +1 -1
  25. package/esm/src/clients/event.d.ts +64 -21
  26. package/esm/src/clients/event.d.ts.map +1 -1
  27. package/esm/src/clients/event.js +76 -18
  28. package/esm/src/clients/public.d.ts +244 -52
  29. package/esm/src/clients/public.d.ts.map +1 -1
  30. package/esm/src/clients/public.js +243 -47
  31. package/esm/src/clients/wallet.d.ts +122 -53
  32. package/esm/src/clients/wallet.d.ts.map +1 -1
  33. package/esm/src/clients/wallet.js +201 -65
  34. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  35. package/esm/src/transports/http/http_transport.js +4 -1
  36. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  37. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  38. package/esm/src/transports/websocket/hyperliquid_event_target.js +37 -0
  39. package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -1
  40. package/esm/src/transports/websocket/websocket_request_dispatcher.js +4 -4
  41. package/esm/src/transports/websocket/websocket_transport.d.ts +7 -10
  42. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  43. package/esm/src/transports/websocket/websocket_transport.js +41 -35
  44. package/esm/src/types/common.d.ts +3 -0
  45. package/esm/src/types/common.d.ts.map +1 -0
  46. package/esm/src/types/common.js +1 -0
  47. package/esm/src/types/exchange/common.d.ts +36 -0
  48. package/esm/src/types/exchange/common.d.ts.map +1 -0
  49. package/esm/src/types/exchange/common.js +1 -0
  50. package/esm/src/types/exchange/requests.d.ts +502 -0
  51. package/esm/src/types/exchange/requests.d.ts.map +1 -0
  52. package/esm/src/types/exchange/requests.js +1 -0
  53. package/esm/src/types/exchange/responses.d.ts +141 -0
  54. package/esm/src/types/exchange/responses.d.ts.map +1 -0
  55. package/esm/src/types/exchange/responses.js +1 -0
  56. package/esm/src/types/explorer/common.d.ts +37 -0
  57. package/esm/src/types/explorer/common.d.ts.map +1 -0
  58. package/esm/src/types/explorer/common.js +1 -0
  59. package/esm/src/types/explorer/requests.d.ts +35 -0
  60. package/esm/src/types/explorer/requests.d.ts.map +1 -0
  61. package/esm/src/types/explorer/requests.js +1 -0
  62. package/esm/src/types/explorer/responses.d.ts +23 -0
  63. package/esm/src/types/explorer/responses.d.ts.map +1 -0
  64. package/esm/src/types/explorer/responses.js +1 -0
  65. package/esm/src/types/info/accounts.d.ts +489 -0
  66. package/esm/src/types/info/accounts.d.ts.map +1 -0
  67. package/esm/src/types/info/accounts.js +1 -0
  68. package/esm/src/types/info/assets.d.ts +275 -0
  69. package/esm/src/types/info/assets.d.ts.map +1 -0
  70. package/esm/src/types/info/assets.js +1 -0
  71. package/esm/src/types/info/delegations.d.ts +117 -0
  72. package/esm/src/types/info/delegations.d.ts.map +1 -0
  73. package/esm/src/types/info/delegations.js +1 -0
  74. package/esm/src/types/info/orders.d.ts +209 -0
  75. package/esm/src/types/info/orders.d.ts.map +1 -0
  76. package/esm/src/types/info/orders.js +1 -0
  77. package/esm/src/types/info/requests.d.ts +445 -0
  78. package/esm/src/types/info/requests.d.ts.map +1 -0
  79. package/esm/src/types/info/requests.js +1 -0
  80. package/esm/src/types/info/vaults.d.ts +90 -0
  81. package/esm/src/types/info/vaults.d.ts.map +1 -0
  82. package/esm/src/types/info/vaults.js +1 -0
  83. package/esm/src/types/subscriptions/common.d.ts +208 -0
  84. package/esm/src/types/subscriptions/common.d.ts.map +1 -0
  85. package/esm/src/types/subscriptions/common.js +1 -0
  86. package/esm/src/types/subscriptions/requests.d.ts +134 -0
  87. package/esm/src/types/subscriptions/requests.d.ts.map +1 -0
  88. package/esm/src/types/subscriptions/requests.js +1 -0
  89. package/esm/src/utils/key_sort.d.ts +3 -3
  90. package/esm/src/utils/key_sort.d.ts.map +1 -1
  91. package/esm/src/utils/signing.d.ts +1 -1
  92. package/esm/src/utils/signing.js +2 -2
  93. package/package.json +2 -2
  94. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
  95. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
  96. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
  97. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
  98. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
  99. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
  100. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
  101. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  102. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  103. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
  104. package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  105. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  106. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
  107. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  108. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  109. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
  110. package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  111. package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
  112. package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  113. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  114. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
  115. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  116. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  117. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
  118. package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  119. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  120. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
  121. package/script/mod.d.ts +15 -14
  122. package/script/mod.d.ts.map +1 -1
  123. package/script/mod.js +23 -13
  124. package/script/src/clients/event.d.ts +64 -21
  125. package/script/src/clients/event.d.ts.map +1 -1
  126. package/script/src/clients/event.js +551 -483
  127. package/script/src/clients/public.d.ts +244 -52
  128. package/script/src/clients/public.d.ts.map +1 -1
  129. package/script/src/clients/public.js +914 -708
  130. package/script/src/clients/wallet.d.ts +122 -53
  131. package/script/src/clients/wallet.d.ts.map +1 -1
  132. package/script/src/clients/wallet.js +1121 -975
  133. package/script/src/transports/base.js +25 -15
  134. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  135. package/script/src/transports/http/http_transport.js +174 -161
  136. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  137. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  138. package/script/src/transports/websocket/hyperliquid_event_target.js +80 -33
  139. package/script/src/transports/websocket/reconnecting_websocket.js +364 -354
  140. package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -1
  141. package/script/src/transports/websocket/websocket_request_dispatcher.js +196 -186
  142. package/script/src/transports/websocket/websocket_transport.d.ts +7 -10
  143. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  144. package/script/src/transports/websocket/websocket_transport.js +236 -220
  145. package/script/src/types/common.d.ts +3 -0
  146. package/script/src/types/common.d.ts.map +1 -0
  147. package/script/src/types/common.js +12 -0
  148. package/script/src/types/exchange/common.d.ts +36 -0
  149. package/script/src/types/exchange/common.d.ts.map +1 -0
  150. package/script/src/types/exchange/common.js +12 -0
  151. package/script/src/types/exchange/requests.d.ts +502 -0
  152. package/script/src/types/exchange/requests.d.ts.map +1 -0
  153. package/script/src/types/exchange/requests.js +12 -0
  154. package/script/src/types/exchange/responses.d.ts +141 -0
  155. package/script/src/types/exchange/responses.d.ts.map +1 -0
  156. package/script/src/types/exchange/responses.js +12 -0
  157. package/script/src/types/explorer/common.d.ts +37 -0
  158. package/script/src/types/explorer/common.d.ts.map +1 -0
  159. package/script/src/types/explorer/common.js +12 -0
  160. package/script/src/types/explorer/requests.d.ts +35 -0
  161. package/script/src/types/explorer/requests.d.ts.map +1 -0
  162. package/script/src/types/explorer/requests.js +12 -0
  163. package/script/src/types/explorer/responses.d.ts +23 -0
  164. package/script/src/types/explorer/responses.d.ts.map +1 -0
  165. package/script/src/types/explorer/responses.js +12 -0
  166. package/script/src/types/info/accounts.d.ts +489 -0
  167. package/script/src/types/info/accounts.d.ts.map +1 -0
  168. package/script/src/types/info/accounts.js +12 -0
  169. package/script/src/types/info/assets.d.ts +275 -0
  170. package/script/src/types/info/assets.d.ts.map +1 -0
  171. package/script/src/types/info/assets.js +12 -0
  172. package/script/src/types/info/delegations.d.ts +117 -0
  173. package/script/src/types/info/delegations.d.ts.map +1 -0
  174. package/script/src/types/info/delegations.js +12 -0
  175. package/script/src/types/info/orders.d.ts +209 -0
  176. package/script/src/types/info/orders.d.ts.map +1 -0
  177. package/script/src/types/info/orders.js +12 -0
  178. package/script/src/types/info/requests.d.ts +445 -0
  179. package/script/src/types/info/requests.d.ts.map +1 -0
  180. package/script/src/types/info/requests.js +12 -0
  181. package/script/src/types/info/vaults.d.ts +90 -0
  182. package/script/src/types/info/vaults.d.ts.map +1 -0
  183. package/script/src/types/info/vaults.js +12 -0
  184. package/script/src/types/subscriptions/common.d.ts +208 -0
  185. package/script/src/types/subscriptions/common.d.ts.map +1 -0
  186. package/script/src/types/subscriptions/common.js +12 -0
  187. package/script/src/types/subscriptions/requests.d.ts +134 -0
  188. package/script/src/types/subscriptions/requests.d.ts.map +1 -0
  189. package/script/src/types/subscriptions/requests.js +12 -0
  190. package/script/src/utils/key_sort.d.ts +3 -3
  191. package/script/src/utils/key_sort.d.ts.map +1 -1
  192. package/script/src/utils/key_sort.js +133 -123
  193. package/script/src/utils/signing.d.ts +1 -1
  194. package/script/src/utils/signing.js +172 -162
  195. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  196. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  197. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  198. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  199. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
  200. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
  201. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  202. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
  203. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  204. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
  205. /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
  206. /package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
@@ -1,35 +0,0 @@
1
- "use strict";
2
- // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
3
- // This module is browser compatible.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.concat = concat;
6
- /**
7
- * Concatenate an array of byte slices into a single slice.
8
- *
9
- * @param buffers Array of byte slices to concatenate.
10
- * @returns A new byte slice containing all the input slices concatenated.
11
- *
12
- * @example Basic usage
13
- * ```ts
14
- * import { concat } from "@std/bytes/concat";
15
- * import { assertEquals } from "@std/assert";
16
- *
17
- * const a = new Uint8Array([0, 1, 2]);
18
- * const b = new Uint8Array([3, 4, 5]);
19
- *
20
- * assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
21
- * ```
22
- */
23
- function concat(buffers) {
24
- let length = 0;
25
- for (const buffer of buffers) {
26
- length += buffer.length;
27
- }
28
- const output = new Uint8Array(length);
29
- let index = 0;
30
- for (const buffer of buffers) {
31
- output.set(buffer, index);
32
- index += buffer.length;
33
- }
34
- return output;
35
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.validateBinaryLike = validateBinaryLike;
5
- const encoder = new TextEncoder();
6
- function getTypeName(value) {
7
- const type = typeof value;
8
- if (type !== "object") {
9
- return type;
10
- }
11
- else if (value === null) {
12
- return "null";
13
- }
14
- else {
15
- return value?.constructor?.name ?? "object";
16
- }
17
- }
18
- function validateBinaryLike(source) {
19
- if (typeof source === "string") {
20
- return encoder.encode(source);
21
- }
22
- else if (source instanceof Uint8Array) {
23
- return source;
24
- }
25
- else if (source instanceof ArrayBuffer) {
26
- return new Uint8Array(source);
27
- }
28
- throw new TypeError(`Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type ${getTypeName(source)}`);
29
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/encoding/1.0.6/hex.ts"],"names":[],"mappings":"AAwDA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,CAUxE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAiBjD"}
@@ -1,113 +0,0 @@
1
- "use strict";
2
- // Copyright 2009 The Go Authors. All rights reserved.
3
- // https://github.com/golang/go/blob/master/LICENSE
4
- // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
5
- // This module is browser compatible.
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.encodeHex = encodeHex;
8
- exports.decodeHex = decodeHex;
9
- /**
10
- * Port of the Go
11
- * {@link https://github.com/golang/go/blob/go1.12.5/src/encoding/hex/hex.go | encoding/hex}
12
- * library.
13
- *
14
- * ```ts
15
- * import {
16
- * decodeHex,
17
- * encodeHex,
18
- * } from "@std/encoding/hex";
19
- * import { assertEquals } from "@std/assert";
20
- *
21
- * assertEquals(encodeHex("abc"), "616263");
22
- *
23
- * assertEquals(
24
- * decodeHex("616263"),
25
- * new TextEncoder().encode("abc"),
26
- * );
27
- * ```
28
- *
29
- * @module
30
- */
31
- const _validate_binary_like_js_1 = require("./_validate_binary_like.js");
32
- const hexTable = new TextEncoder().encode("0123456789abcdef");
33
- const textEncoder = new TextEncoder();
34
- const textDecoder = new TextDecoder();
35
- function errInvalidByte(byte) {
36
- return new TypeError(`Invalid byte '${String.fromCharCode(byte)}'`);
37
- }
38
- function errLength(len) {
39
- return new RangeError(`Cannot decode the hex string as the input length should be even: length is ${len}`);
40
- }
41
- /** Converts a hex character into its value. */
42
- function fromHexChar(byte) {
43
- // '0' <= byte && byte <= '9'
44
- if (48 <= byte && byte <= 57)
45
- return byte - 48;
46
- // 'a' <= byte && byte <= 'f'
47
- if (97 <= byte && byte <= 102)
48
- return byte - 97 + 10;
49
- // 'A' <= byte && byte <= 'F'
50
- if (65 <= byte && byte <= 70)
51
- return byte - 65 + 10;
52
- throw errInvalidByte(byte);
53
- }
54
- /**
55
- * Converts data into a hex-encoded string.
56
- *
57
- * @param src The data to encode.
58
- *
59
- * @returns The hex-encoded string.
60
- *
61
- * @example Usage
62
- * ```ts
63
- * import { encodeHex } from "@std/encoding/hex";
64
- * import { assertEquals } from "@std/assert";
65
- *
66
- * assertEquals(encodeHex("abc"), "616263");
67
- * ```
68
- */
69
- function encodeHex(src) {
70
- const u8 = (0, _validate_binary_like_js_1.validateBinaryLike)(src);
71
- const dst = new Uint8Array(u8.length * 2);
72
- for (let i = 0; i < u8.length; i++) {
73
- const v = u8[i];
74
- dst[i * 2] = hexTable[v >> 4];
75
- dst[i * 2 + 1] = hexTable[v & 0x0f];
76
- }
77
- return textDecoder.decode(dst);
78
- }
79
- /**
80
- * Decodes the given hex-encoded string. If the input is malformed, an error is
81
- * thrown.
82
- *
83
- * @param src The hex-encoded string to decode.
84
- *
85
- * @returns The decoded data.
86
- *
87
- * @example Usage
88
- * ```ts
89
- * import { decodeHex } from "@std/encoding/hex";
90
- * import { assertEquals } from "@std/assert";
91
- *
92
- * assertEquals(
93
- * decodeHex("616263"),
94
- * new TextEncoder().encode("abc"),
95
- * );
96
- * ```
97
- */
98
- function decodeHex(src) {
99
- const u8 = textEncoder.encode(src);
100
- const dst = new Uint8Array(u8.length / 2);
101
- for (let i = 0; i < dst.length; i++) {
102
- const a = fromHexChar(u8[i * 2]);
103
- const b = fromHexChar(u8[i * 2 + 1]);
104
- dst[i] = (a << 4) | b;
105
- }
106
- if (u8.length % 2 === 1) {
107
- // Check for invalid char before reporting bad length,
108
- // since the invalid char (if present) is an earlier problem.
109
- fromHexChar(u8[dst.length * 2]);
110
- throw errLength(u8.length);
111
- }
112
- return dst;
113
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/msgpack/1.0.2/encode.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,UAAU,GACV,SAAS,SAAS,EAAE,GACpB,QAAQ,CAAC;AAEb;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,sBAAsB;IACtB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACrC;AAeD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,UAAU,CAIpD"}
@@ -1,240 +0,0 @@
1
- "use strict";
2
- // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
3
- // This module is browser compatible.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.encode = encode;
6
- const concat_js_1 = require("../../bytes/1.0.4/concat.js");
7
- const FOUR_BITS = 16;
8
- const FIVE_BITS = 32;
9
- const SEVEN_BITS = 128;
10
- const EIGHT_BITS = 256;
11
- const FIFTEEN_BITS = 32768;
12
- const SIXTEEN_BITS = 65536;
13
- const THIRTY_ONE_BITS = 2147483648;
14
- const THIRTY_TWO_BITS = 4294967296;
15
- const SIXTY_THREE_BITS = 9223372036854775808n;
16
- const SIXTY_FOUR_BITS = 18446744073709551616n;
17
- const encoder = new TextEncoder();
18
- /**
19
- * Encode a value to {@link https://msgpack.org/ | MessagePack} binary format.
20
- *
21
- * @example Usage
22
- * ```ts
23
- * import { encode } from "@std/msgpack/encode";
24
- * import { assertEquals } from "@std/assert";
25
- *
26
- * const obj = {
27
- * str: "deno",
28
- * arr: [1, 2, 3],
29
- * map: {
30
- * foo: "bar"
31
- * }
32
- * }
33
- *
34
- * const encoded = encode(obj);
35
- *
36
- * assertEquals(encoded.length, 31);
37
- * ```
38
- *
39
- * @param object Value to encode to MessagePack binary format.
40
- * @returns Encoded MessagePack binary data.
41
- */
42
- function encode(object) {
43
- const byteParts = [];
44
- encodeSlice(object, byteParts);
45
- return (0, concat_js_1.concat)(byteParts);
46
- }
47
- function encodeFloat64(num) {
48
- const dataView = new DataView(new ArrayBuffer(9));
49
- dataView.setFloat64(1, num);
50
- dataView.setUint8(0, 0xcb);
51
- return new Uint8Array(dataView.buffer);
52
- }
53
- function encodeNumber(num) {
54
- if (!Number.isInteger(num)) { // float 64
55
- return encodeFloat64(num);
56
- }
57
- if (num < 0) {
58
- if (num >= -FIVE_BITS) { // negative fixint
59
- return new Uint8Array([num]);
60
- }
61
- if (num >= -SEVEN_BITS) { // int 8
62
- return new Uint8Array([0xd0, num]);
63
- }
64
- if (num >= -FIFTEEN_BITS) { // int 16
65
- const dataView = new DataView(new ArrayBuffer(3));
66
- dataView.setInt16(1, num);
67
- dataView.setUint8(0, 0xd1);
68
- return new Uint8Array(dataView.buffer);
69
- }
70
- if (num >= -THIRTY_ONE_BITS) { // int 32
71
- const dataView = new DataView(new ArrayBuffer(5));
72
- dataView.setInt32(1, num);
73
- dataView.setUint8(0, 0xd2);
74
- return new Uint8Array(dataView.buffer);
75
- }
76
- // float 64
77
- return encodeFloat64(num);
78
- }
79
- // if the number fits within a positive fixint, use it
80
- if (num <= 0x7f) {
81
- return new Uint8Array([num]);
82
- }
83
- if (num < EIGHT_BITS) { // uint8
84
- return new Uint8Array([0xcc, num]);
85
- }
86
- if (num < SIXTEEN_BITS) { // uint16
87
- const dataView = new DataView(new ArrayBuffer(3));
88
- dataView.setUint16(1, num);
89
- dataView.setUint8(0, 0xcd);
90
- return new Uint8Array(dataView.buffer);
91
- }
92
- if (num < THIRTY_TWO_BITS) { // uint32
93
- const dataView = new DataView(new ArrayBuffer(5));
94
- dataView.setUint32(1, num);
95
- dataView.setUint8(0, 0xce);
96
- return new Uint8Array(dataView.buffer);
97
- }
98
- // float 64
99
- return encodeFloat64(num);
100
- }
101
- function encodeSlice(object, byteParts) {
102
- if (object === null) {
103
- byteParts.push(new Uint8Array([0xc0]));
104
- return;
105
- }
106
- if (object === false) {
107
- byteParts.push(new Uint8Array([0xc2]));
108
- return;
109
- }
110
- if (object === true) {
111
- byteParts.push(new Uint8Array([0xc3]));
112
- return;
113
- }
114
- if (typeof object === "number") {
115
- byteParts.push(encodeNumber(object));
116
- return;
117
- }
118
- if (typeof object === "bigint") {
119
- if (object < 0) {
120
- if (object < -SIXTY_THREE_BITS) {
121
- throw new Error("Cannot safely encode bigint larger than 64 bits");
122
- }
123
- const dataView = new DataView(new ArrayBuffer(9));
124
- dataView.setBigInt64(1, object);
125
- dataView.setUint8(0, 0xd3);
126
- byteParts.push(new Uint8Array(dataView.buffer));
127
- return;
128
- }
129
- if (object >= SIXTY_FOUR_BITS) {
130
- throw new Error("Cannot safely encode bigint larger than 64 bits");
131
- }
132
- const dataView = new DataView(new ArrayBuffer(9));
133
- dataView.setBigUint64(1, object);
134
- dataView.setUint8(0, 0xcf);
135
- byteParts.push(new Uint8Array(dataView.buffer));
136
- return;
137
- }
138
- if (typeof object === "string") {
139
- const encoded = encoder.encode(object);
140
- const len = encoded.length;
141
- if (len < FIVE_BITS) { // fixstr
142
- byteParts.push(new Uint8Array([0xa0 | len]));
143
- }
144
- else if (len < EIGHT_BITS) { // str 8
145
- byteParts.push(new Uint8Array([0xd9, len]));
146
- }
147
- else if (len < SIXTEEN_BITS) { // str 16
148
- const dataView = new DataView(new ArrayBuffer(3));
149
- dataView.setUint16(1, len);
150
- dataView.setUint8(0, 0xda);
151
- byteParts.push(new Uint8Array(dataView.buffer));
152
- }
153
- else if (len < THIRTY_TWO_BITS) { // str 32
154
- const dataView = new DataView(new ArrayBuffer(5));
155
- dataView.setUint32(1, len);
156
- dataView.setUint8(0, 0xdb);
157
- byteParts.push(new Uint8Array(dataView.buffer));
158
- }
159
- else {
160
- throw new Error("Cannot safely encode string with size larger than 32 bits");
161
- }
162
- byteParts.push(encoded);
163
- return;
164
- }
165
- if (object instanceof Uint8Array) {
166
- if (object.length < EIGHT_BITS) { // bin 8
167
- byteParts.push(new Uint8Array([0xc4, object.length]));
168
- }
169
- else if (object.length < SIXTEEN_BITS) { // bin 16
170
- const dataView = new DataView(new ArrayBuffer(3));
171
- dataView.setUint16(1, object.length);
172
- dataView.setUint8(0, 0xc5);
173
- byteParts.push(new Uint8Array(dataView.buffer));
174
- }
175
- else if (object.length < THIRTY_TWO_BITS) { // bin 32
176
- const dataView = new DataView(new ArrayBuffer(5));
177
- dataView.setUint32(1, object.length);
178
- dataView.setUint8(0, 0xc6);
179
- byteParts.push(new Uint8Array(dataView.buffer));
180
- }
181
- else {
182
- throw new Error("Cannot safely encode Uint8Array with size larger than 32 bits");
183
- }
184
- byteParts.push(object);
185
- return;
186
- }
187
- if (Array.isArray(object)) {
188
- if (object.length < FOUR_BITS) { // fixarray
189
- byteParts.push(new Uint8Array([0x90 | object.length]));
190
- }
191
- else if (object.length < SIXTEEN_BITS) { // array 16
192
- const dataView = new DataView(new ArrayBuffer(3));
193
- dataView.setUint16(1, object.length);
194
- dataView.setUint8(0, 0xdc);
195
- byteParts.push(new Uint8Array(dataView.buffer));
196
- }
197
- else if (object.length < THIRTY_TWO_BITS) { // array 32
198
- const dataView = new DataView(new ArrayBuffer(5));
199
- dataView.setUint32(1, object.length);
200
- dataView.setUint8(0, 0xdd);
201
- byteParts.push(new Uint8Array(dataView.buffer));
202
- }
203
- else {
204
- throw new Error("Cannot safely encode array with size larger than 32 bits");
205
- }
206
- for (const obj of object) {
207
- encodeSlice(obj, byteParts);
208
- }
209
- return;
210
- }
211
- // If object is a plain object
212
- const prototype = Object.getPrototypeOf(object);
213
- if (prototype === null || prototype === Object.prototype) {
214
- const numKeys = Object.keys(object).length;
215
- if (numKeys < FOUR_BITS) { // fixarray
216
- byteParts.push(new Uint8Array([0x80 | numKeys]));
217
- }
218
- else if (numKeys < SIXTEEN_BITS) { // map 16
219
- const dataView = new DataView(new ArrayBuffer(3));
220
- dataView.setUint16(1, numKeys);
221
- dataView.setUint8(0, 0xde);
222
- byteParts.push(new Uint8Array(dataView.buffer));
223
- }
224
- else if (numKeys < THIRTY_TWO_BITS) { // map 32
225
- const dataView = new DataView(new ArrayBuffer(5));
226
- dataView.setUint32(1, numKeys);
227
- dataView.setUint8(0, 0xdf);
228
- byteParts.push(new Uint8Array(dataView.buffer));
229
- }
230
- else {
231
- throw new Error("Cannot safely encode map with size larger than 32 bits");
232
- }
233
- for (const [key, value] of Object.entries(object)) {
234
- encodeSlice(key, byteParts);
235
- encodeSlice(value, byteParts);
236
- }
237
- return;
238
- }
239
- throw new Error("Cannot safely encode value into messagepack");
240
- }