@nktkas/hyperliquid 0.13.0
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.
- package/CONTRIBUTING.md +59 -0
- package/LICENSE +21 -0
- package/README.md +363 -0
- package/SECURITY.md +7 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +1 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +95 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +1 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +10 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts +23 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +43 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts +55 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +65 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts +2 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts +52 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +283 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts +120 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +211 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts +19 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.js +32 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts +2 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +26 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts +37 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.js +109 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts +37 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.js +237 -0
- package/esm/mod.d.ts +24 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +9 -0
- package/esm/package.json +3 -0
- package/esm/src/clients/event.d.ts +374 -0
- package/esm/src/clients/event.d.ts.map +1 -0
- package/esm/src/clients/event.js +490 -0
- package/esm/src/clients/public.d.ts +695 -0
- package/esm/src/clients/public.d.ts.map +1 -0
- package/esm/src/clients/public.js +704 -0
- package/esm/src/clients/wallet.d.ts +682 -0
- package/esm/src/clients/wallet.d.ts.map +1 -0
- package/esm/src/clients/wallet.js +984 -0
- package/esm/src/transports/base.d.ts +55 -0
- package/esm/src/transports/base.d.ts.map +1 -0
- package/esm/src/transports/base.js +14 -0
- package/esm/src/transports/http/http_transport.d.ts +78 -0
- package/esm/src/transports/http/http_transport.d.ts.map +1 -0
- package/esm/src/transports/http/http_transport.js +170 -0
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +66 -0
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -0
- package/esm/src/transports/websocket/hyperliquid_event_target.js +33 -0
- package/esm/src/transports/websocket/reconnecting_websocket.d.ts +160 -0
- package/esm/src/transports/websocket/reconnecting_websocket.d.ts.map +1 -0
- package/esm/src/transports/websocket/reconnecting_websocket.js +370 -0
- package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts +63 -0
- package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -0
- package/esm/src/transports/websocket/websocket_request_dispatcher.js +201 -0
- package/esm/src/transports/websocket/websocket_transport.d.ts +117 -0
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -0
- package/esm/src/transports/websocket/websocket_transport.js +233 -0
- package/esm/src/utils/key_sort.d.ts +21 -0
- package/esm/src/utils/key_sort.d.ts.map +1 -0
- package/esm/src/utils/key_sort.js +124 -0
- package/esm/src/utils/signing.d.ts +109 -0
- package/esm/src/utils/signing.d.ts.map +1 -0
- package/esm/src/utils/signing.js +164 -0
- package/package.json +34 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +2 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +17 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +95 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +1 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +14 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts +23 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +49 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts +55 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +88 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts +2 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +4 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts +52 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts +120 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +235 -0
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts +19 -0
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +35 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts +2 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +29 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts +37 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +113 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts +37 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +240 -0
- package/script/mod.d.ts +24 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +27 -0
- package/script/package.json +3 -0
- package/script/src/clients/event.d.ts +374 -0
- package/script/src/clients/event.d.ts.map +1 -0
- package/script/src/clients/event.js +494 -0
- package/script/src/clients/public.d.ts +695 -0
- package/script/src/clients/public.d.ts.map +1 -0
- package/script/src/clients/public.js +708 -0
- package/script/src/clients/wallet.d.ts +682 -0
- package/script/src/clients/wallet.d.ts.map +1 -0
- package/script/src/clients/wallet.js +989 -0
- package/script/src/transports/base.d.ts +55 -0
- package/script/src/transports/base.d.ts.map +1 -0
- package/script/src/transports/base.js +18 -0
- package/script/src/transports/http/http_transport.d.ts +78 -0
- package/script/src/transports/http/http_transport.d.ts.map +1 -0
- package/script/src/transports/http/http_transport.js +175 -0
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts +66 -0
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -0
- package/script/src/transports/websocket/hyperliquid_event_target.js +37 -0
- package/script/src/transports/websocket/reconnecting_websocket.d.ts +160 -0
- package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +1 -0
- package/script/src/transports/websocket/reconnecting_websocket.js +374 -0
- package/script/src/transports/websocket/websocket_request_dispatcher.d.ts +63 -0
- package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -0
- package/script/src/transports/websocket/websocket_request_dispatcher.js +206 -0
- package/script/src/transports/websocket/websocket_transport.d.ts +117 -0
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -0
- package/script/src/transports/websocket/websocket_transport.js +237 -0
- package/script/src/utils/key_sort.d.ts +21 -0
- package/script/src/utils/key_sort.d.ts.map +1 -0
- package/script/src/utils/key_sort.js +127 -0
- package/script/src/utils/signing.d.ts +109 -0
- package/script/src/utils/signing.d.ts.map +1 -0
- package/script/src/utils/signing.js +172 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
6
|
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
7
|
+
// node.js versions earlier than v19 don't declare it in global scope.
|
|
8
|
+
// For node.js, package.json#exports field mapping rewrites import
|
|
9
|
+
// from `crypto` to `cryptoNode`, which imports native module.
|
|
10
|
+
// Makes the utils un-importable in browsers without a bundler.
|
|
11
|
+
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
12
|
+
import { crypto } from './crypto.js';
|
|
13
|
+
import { abytes } from './_assert.js';
|
|
14
|
+
// export { isBytes } from './_assert.js';
|
|
15
|
+
// We can't reuse isBytes from _assert, because somehow this causes huge perf issues
|
|
16
|
+
export function isBytes(a) {
|
|
17
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
18
|
+
}
|
|
19
|
+
// Cast array to different type
|
|
20
|
+
export function u8(arr) {
|
|
21
|
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
22
|
+
}
|
|
23
|
+
export function u32(arr) {
|
|
24
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
25
|
+
}
|
|
26
|
+
// Cast array to view
|
|
27
|
+
export function createView(arr) {
|
|
28
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
29
|
+
}
|
|
30
|
+
/** The rotate right (circular right shift) operation for uint32 */
|
|
31
|
+
export function rotr(word, shift) {
|
|
32
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
33
|
+
}
|
|
34
|
+
/** The rotate left (circular left shift) operation for uint32 */
|
|
35
|
+
export function rotl(word, shift) {
|
|
36
|
+
return (word << shift) | ((word >>> (32 - shift)) >>> 0);
|
|
37
|
+
}
|
|
38
|
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
|
39
|
+
export const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
40
|
+
// The byte swap operation for uint32
|
|
41
|
+
export function byteSwap(word) {
|
|
42
|
+
return (((word << 24) & 0xff000000) |
|
|
43
|
+
((word << 8) & 0xff0000) |
|
|
44
|
+
((word >>> 8) & 0xff00) |
|
|
45
|
+
((word >>> 24) & 0xff));
|
|
46
|
+
}
|
|
47
|
+
/** Conditionally byte swap if on a big-endian platform */
|
|
48
|
+
export const byteSwapIfBE = isLE
|
|
49
|
+
? (n) => n
|
|
50
|
+
: (n) => byteSwap(n);
|
|
51
|
+
/** In place byte swap for Uint32Array */
|
|
52
|
+
export function byteSwap32(arr) {
|
|
53
|
+
for (let i = 0; i < arr.length; i++) {
|
|
54
|
+
arr[i] = byteSwap(arr[i]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
58
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
59
|
+
/**
|
|
60
|
+
* Convert byte array to hex string.
|
|
61
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
62
|
+
*/
|
|
63
|
+
export function bytesToHex(bytes) {
|
|
64
|
+
abytes(bytes);
|
|
65
|
+
// pre-caching improves the speed 6x
|
|
66
|
+
let hex = '';
|
|
67
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
68
|
+
hex += hexes[bytes[i]];
|
|
69
|
+
}
|
|
70
|
+
return hex;
|
|
71
|
+
}
|
|
72
|
+
// We use optimized technique to convert hex string to byte array
|
|
73
|
+
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
74
|
+
function asciiToBase16(ch) {
|
|
75
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
76
|
+
return ch - asciis._0; // '2' => 50-48
|
|
77
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
78
|
+
return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
|
79
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
80
|
+
return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Convert hex string to byte array.
|
|
85
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
86
|
+
*/
|
|
87
|
+
export function hexToBytes(hex) {
|
|
88
|
+
if (typeof hex !== 'string')
|
|
89
|
+
throw new Error('hex string expected, got ' + typeof hex);
|
|
90
|
+
const hl = hex.length;
|
|
91
|
+
const al = hl / 2;
|
|
92
|
+
if (hl % 2)
|
|
93
|
+
throw new Error('hex string expected, got unpadded hex of length ' + hl);
|
|
94
|
+
const array = new Uint8Array(al);
|
|
95
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
96
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
97
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
98
|
+
if (n1 === undefined || n2 === undefined) {
|
|
99
|
+
const char = hex[hi] + hex[hi + 1];
|
|
100
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
101
|
+
}
|
|
102
|
+
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
|
103
|
+
}
|
|
104
|
+
return array;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* There is no setImmediate in browser and setTimeout is slow.
|
|
108
|
+
* Call of async fn will return Promise, which will be fullfiled only on
|
|
109
|
+
* next scheduler queue processing step and this is exactly what we need.
|
|
110
|
+
*/
|
|
111
|
+
export const nextTick = async () => { };
|
|
112
|
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
|
113
|
+
export async function asyncLoop(iters, tick, cb) {
|
|
114
|
+
let ts = Date.now();
|
|
115
|
+
for (let i = 0; i < iters; i++) {
|
|
116
|
+
cb(i);
|
|
117
|
+
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
|
118
|
+
const diff = Date.now() - ts;
|
|
119
|
+
if (diff >= 0 && diff < tick)
|
|
120
|
+
continue;
|
|
121
|
+
await nextTick();
|
|
122
|
+
ts += diff;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Convert JS string to byte array.
|
|
127
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
128
|
+
*/
|
|
129
|
+
export function utf8ToBytes(str) {
|
|
130
|
+
if (typeof str !== 'string')
|
|
131
|
+
throw new Error('utf8ToBytes expected string, got ' + typeof str);
|
|
132
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
136
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
137
|
+
* Keep in mind for future mutable operations.
|
|
138
|
+
*/
|
|
139
|
+
export function toBytes(data) {
|
|
140
|
+
if (typeof data === 'string')
|
|
141
|
+
data = utf8ToBytes(data);
|
|
142
|
+
abytes(data);
|
|
143
|
+
return data;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Copies several Uint8Arrays into one.
|
|
147
|
+
*/
|
|
148
|
+
export function concatBytes(...arrays) {
|
|
149
|
+
let sum = 0;
|
|
150
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
151
|
+
const a = arrays[i];
|
|
152
|
+
abytes(a);
|
|
153
|
+
sum += a.length;
|
|
154
|
+
}
|
|
155
|
+
const res = new Uint8Array(sum);
|
|
156
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
157
|
+
const a = arrays[i];
|
|
158
|
+
res.set(a, pad);
|
|
159
|
+
pad += a.length;
|
|
160
|
+
}
|
|
161
|
+
return res;
|
|
162
|
+
}
|
|
163
|
+
/** For runtime check if class implements interface */
|
|
164
|
+
export class Hash {
|
|
165
|
+
// Safe version that clones internal state
|
|
166
|
+
clone() {
|
|
167
|
+
return this._cloneInto();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export function checkOpts(defaults, opts) {
|
|
171
|
+
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
|
172
|
+
throw new Error('Options should be object or undefined');
|
|
173
|
+
const merged = Object.assign(defaults, opts);
|
|
174
|
+
return merged;
|
|
175
|
+
}
|
|
176
|
+
/** Wraps hash function, creating an interface on top of it */
|
|
177
|
+
export function wrapConstructor(hashCons) {
|
|
178
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
179
|
+
const tmp = hashCons();
|
|
180
|
+
hashC.outputLen = tmp.outputLen;
|
|
181
|
+
hashC.blockLen = tmp.blockLen;
|
|
182
|
+
hashC.create = () => hashCons();
|
|
183
|
+
return hashC;
|
|
184
|
+
}
|
|
185
|
+
export function wrapConstructorWithOpts(hashCons) {
|
|
186
|
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
187
|
+
const tmp = hashCons({});
|
|
188
|
+
hashC.outputLen = tmp.outputLen;
|
|
189
|
+
hashC.blockLen = tmp.blockLen;
|
|
190
|
+
hashC.create = (opts) => hashCons(opts);
|
|
191
|
+
return hashC;
|
|
192
|
+
}
|
|
193
|
+
export function wrapXOFConstructorWithOpts(hashCons) {
|
|
194
|
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
195
|
+
const tmp = hashCons({});
|
|
196
|
+
hashC.outputLen = tmp.outputLen;
|
|
197
|
+
hashC.blockLen = tmp.blockLen;
|
|
198
|
+
hashC.create = (opts) => hashCons(opts);
|
|
199
|
+
return hashC;
|
|
200
|
+
}
|
|
201
|
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
|
202
|
+
export function randomBytes(bytesLength = 32) {
|
|
203
|
+
if (crypto && typeof crypto.getRandomValues === 'function') {
|
|
204
|
+
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
205
|
+
}
|
|
206
|
+
// Legacy Node.js compatibility
|
|
207
|
+
if (crypto && typeof crypto.randomBytes === 'function') {
|
|
208
|
+
return crypto.randomBytes(bytesLength);
|
|
209
|
+
}
|
|
210
|
+
throw new Error('crypto.getRandomValues must be defined');
|
|
211
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concatenate an array of byte slices into a single slice.
|
|
3
|
+
*
|
|
4
|
+
* @param buffers Array of byte slices to concatenate.
|
|
5
|
+
* @returns A new byte slice containing all the input slices concatenated.
|
|
6
|
+
*
|
|
7
|
+
* @example Basic usage
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { concat } from "@std/bytes/concat";
|
|
10
|
+
* import { assertEquals } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* const a = new Uint8Array([0, 1, 2]);
|
|
13
|
+
* const b = new Uint8Array([3, 4, 5]);
|
|
14
|
+
*
|
|
15
|
+
* assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function concat(buffers: Uint8Array[]): Uint8Array;
|
|
19
|
+
//# sourceMappingURL=concat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concat.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/bytes/1.0.4/concat.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,UAAU,CAaxD"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
/**
|
|
4
|
+
* Concatenate an array of byte slices into a single slice.
|
|
5
|
+
*
|
|
6
|
+
* @param buffers Array of byte slices to concatenate.
|
|
7
|
+
* @returns A new byte slice containing all the input slices concatenated.
|
|
8
|
+
*
|
|
9
|
+
* @example Basic usage
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { concat } from "@std/bytes/concat";
|
|
12
|
+
* import { assertEquals } from "@std/assert";
|
|
13
|
+
*
|
|
14
|
+
* const a = new Uint8Array([0, 1, 2]);
|
|
15
|
+
* const b = new Uint8Array([3, 4, 5]);
|
|
16
|
+
*
|
|
17
|
+
* assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function concat(buffers) {
|
|
21
|
+
let length = 0;
|
|
22
|
+
for (const buffer of buffers) {
|
|
23
|
+
length += buffer.length;
|
|
24
|
+
}
|
|
25
|
+
const output = new Uint8Array(length);
|
|
26
|
+
let index = 0;
|
|
27
|
+
for (const buffer of buffers) {
|
|
28
|
+
output.set(buffer, index);
|
|
29
|
+
index += buffer.length;
|
|
30
|
+
}
|
|
31
|
+
return output;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_validate_binary_like.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.ts"],"names":[],"mappings":"AAeA,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,OAAO,GAAG,UAAU,CAa9D"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
function getTypeName(value) {
|
|
4
|
+
const type = typeof value;
|
|
5
|
+
if (type !== "object") {
|
|
6
|
+
return type;
|
|
7
|
+
}
|
|
8
|
+
else if (value === null) {
|
|
9
|
+
return "null";
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return value?.constructor?.name ?? "object";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function validateBinaryLike(source) {
|
|
16
|
+
if (typeof source === "string") {
|
|
17
|
+
return encoder.encode(source);
|
|
18
|
+
}
|
|
19
|
+
else if (source instanceof Uint8Array) {
|
|
20
|
+
return source;
|
|
21
|
+
}
|
|
22
|
+
else if (source instanceof ArrayBuffer) {
|
|
23
|
+
return new Uint8Array(source);
|
|
24
|
+
}
|
|
25
|
+
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)}`);
|
|
26
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts data into a hex-encoded string.
|
|
3
|
+
*
|
|
4
|
+
* @param src The data to encode.
|
|
5
|
+
*
|
|
6
|
+
* @returns The hex-encoded string.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { encodeHex } from "@std/encoding/hex";
|
|
11
|
+
* import { assertEquals } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assertEquals(encodeHex("abc"), "616263");
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function encodeHex(src: string | Uint8Array | ArrayBuffer): string;
|
|
17
|
+
/**
|
|
18
|
+
* Decodes the given hex-encoded string. If the input is malformed, an error is
|
|
19
|
+
* thrown.
|
|
20
|
+
*
|
|
21
|
+
* @param src The hex-encoded string to decode.
|
|
22
|
+
*
|
|
23
|
+
* @returns The decoded data.
|
|
24
|
+
*
|
|
25
|
+
* @example Usage
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { decodeHex } from "@std/encoding/hex";
|
|
28
|
+
* import { assertEquals } from "@std/assert";
|
|
29
|
+
*
|
|
30
|
+
* assertEquals(
|
|
31
|
+
* decodeHex("616263"),
|
|
32
|
+
* new TextEncoder().encode("abc"),
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function decodeHex(src: string): Uint8Array;
|
|
37
|
+
//# sourceMappingURL=hex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Copyright 2009 The Go Authors. All rights reserved.
|
|
2
|
+
// https://github.com/golang/go/blob/master/LICENSE
|
|
3
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
4
|
+
// This module is browser compatible.
|
|
5
|
+
/**
|
|
6
|
+
* Port of the Go
|
|
7
|
+
* {@link https://github.com/golang/go/blob/go1.12.5/src/encoding/hex/hex.go | encoding/hex}
|
|
8
|
+
* library.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {
|
|
12
|
+
* decodeHex,
|
|
13
|
+
* encodeHex,
|
|
14
|
+
* } from "@std/encoding/hex";
|
|
15
|
+
* import { assertEquals } from "@std/assert";
|
|
16
|
+
*
|
|
17
|
+
* assertEquals(encodeHex("abc"), "616263");
|
|
18
|
+
*
|
|
19
|
+
* assertEquals(
|
|
20
|
+
* decodeHex("616263"),
|
|
21
|
+
* new TextEncoder().encode("abc"),
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @module
|
|
26
|
+
*/
|
|
27
|
+
import { validateBinaryLike } from "./_validate_binary_like.js";
|
|
28
|
+
const hexTable = new TextEncoder().encode("0123456789abcdef");
|
|
29
|
+
const textEncoder = new TextEncoder();
|
|
30
|
+
const textDecoder = new TextDecoder();
|
|
31
|
+
function errInvalidByte(byte) {
|
|
32
|
+
return new TypeError(`Invalid byte '${String.fromCharCode(byte)}'`);
|
|
33
|
+
}
|
|
34
|
+
function errLength(len) {
|
|
35
|
+
return new RangeError(`Cannot decode the hex string as the input length should be even: length is ${len}`);
|
|
36
|
+
}
|
|
37
|
+
/** Converts a hex character into its value. */
|
|
38
|
+
function fromHexChar(byte) {
|
|
39
|
+
// '0' <= byte && byte <= '9'
|
|
40
|
+
if (48 <= byte && byte <= 57)
|
|
41
|
+
return byte - 48;
|
|
42
|
+
// 'a' <= byte && byte <= 'f'
|
|
43
|
+
if (97 <= byte && byte <= 102)
|
|
44
|
+
return byte - 97 + 10;
|
|
45
|
+
// 'A' <= byte && byte <= 'F'
|
|
46
|
+
if (65 <= byte && byte <= 70)
|
|
47
|
+
return byte - 65 + 10;
|
|
48
|
+
throw errInvalidByte(byte);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Converts data into a hex-encoded string.
|
|
52
|
+
*
|
|
53
|
+
* @param src The data to encode.
|
|
54
|
+
*
|
|
55
|
+
* @returns The hex-encoded string.
|
|
56
|
+
*
|
|
57
|
+
* @example Usage
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { encodeHex } from "@std/encoding/hex";
|
|
60
|
+
* import { assertEquals } from "@std/assert";
|
|
61
|
+
*
|
|
62
|
+
* assertEquals(encodeHex("abc"), "616263");
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function encodeHex(src) {
|
|
66
|
+
const u8 = validateBinaryLike(src);
|
|
67
|
+
const dst = new Uint8Array(u8.length * 2);
|
|
68
|
+
for (let i = 0; i < u8.length; i++) {
|
|
69
|
+
const v = u8[i];
|
|
70
|
+
dst[i * 2] = hexTable[v >> 4];
|
|
71
|
+
dst[i * 2 + 1] = hexTable[v & 0x0f];
|
|
72
|
+
}
|
|
73
|
+
return textDecoder.decode(dst);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Decodes the given hex-encoded string. If the input is malformed, an error is
|
|
77
|
+
* thrown.
|
|
78
|
+
*
|
|
79
|
+
* @param src The hex-encoded string to decode.
|
|
80
|
+
*
|
|
81
|
+
* @returns The decoded data.
|
|
82
|
+
*
|
|
83
|
+
* @example Usage
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { decodeHex } from "@std/encoding/hex";
|
|
86
|
+
* import { assertEquals } from "@std/assert";
|
|
87
|
+
*
|
|
88
|
+
* assertEquals(
|
|
89
|
+
* decodeHex("616263"),
|
|
90
|
+
* new TextEncoder().encode("abc"),
|
|
91
|
+
* );
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export function decodeHex(src) {
|
|
95
|
+
const u8 = textEncoder.encode(src);
|
|
96
|
+
const dst = new Uint8Array(u8.length / 2);
|
|
97
|
+
for (let i = 0; i < dst.length; i++) {
|
|
98
|
+
const a = fromHexChar(u8[i * 2]);
|
|
99
|
+
const b = fromHexChar(u8[i * 2 + 1]);
|
|
100
|
+
dst[i] = (a << 4) | b;
|
|
101
|
+
}
|
|
102
|
+
if (u8.length % 2 === 1) {
|
|
103
|
+
// Check for invalid char before reporting bad length,
|
|
104
|
+
// since the invalid char (if present) is an earlier problem.
|
|
105
|
+
fromHexChar(u8[dst.length * 2]);
|
|
106
|
+
throw errLength(u8.length);
|
|
107
|
+
}
|
|
108
|
+
return dst;
|
|
109
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Value types that can be encoded to MessagePack.
|
|
3
|
+
*/
|
|
4
|
+
export type ValueType = number | bigint | string | boolean | null | Uint8Array | readonly ValueType[] | ValueMap;
|
|
5
|
+
/**
|
|
6
|
+
* Value map that can be encoded to MessagePack.
|
|
7
|
+
*/
|
|
8
|
+
export interface ValueMap {
|
|
9
|
+
/** Value map entry */
|
|
10
|
+
[index: string | number]: ValueType;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Encode a value to {@link https://msgpack.org/ | MessagePack} binary format.
|
|
14
|
+
*
|
|
15
|
+
* @example Usage
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { encode } from "@std/msgpack/encode";
|
|
18
|
+
* import { assertEquals } from "@std/assert";
|
|
19
|
+
*
|
|
20
|
+
* const obj = {
|
|
21
|
+
* str: "deno",
|
|
22
|
+
* arr: [1, 2, 3],
|
|
23
|
+
* map: {
|
|
24
|
+
* foo: "bar"
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const encoded = encode(obj);
|
|
29
|
+
*
|
|
30
|
+
* assertEquals(encoded.length, 31);
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param object Value to encode to MessagePack binary format.
|
|
34
|
+
* @returns Encoded MessagePack binary data.
|
|
35
|
+
*/
|
|
36
|
+
export declare function encode(object: ValueType): Uint8Array;
|
|
37
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|