@waku/rln 0.1.8-e224c05.0 → 0.1.8-e800af3.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/bundle/_virtual/utils.js +2 -2
- package/bundle/_virtual/utils2.js +2 -2
- package/bundle/index.js +6 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/_sha2.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/hmac.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/pbkdf2.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/scrypt.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha256.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha512.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/utils.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
- package/bundle/node_modules/@chainsafe/is-ip/lib/is-ip.js +12 -0
- package/bundle/node_modules/@chainsafe/is-ip/lib/parse.js +26 -0
- package/bundle/node_modules/@chainsafe/is-ip/lib/parser.js +202 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/constants.js +43 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/errors.js +17 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/registry.js +245 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/utils.js +191 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/validation.js +30 -0
- package/bundle/node_modules/@noble/hashes/esm/hmac.js +88 -0
- package/bundle/node_modules/@noble/hashes/esm/sha3.js +1 -1
- package/bundle/node_modules/@noble/hashes/esm/utils.js +8 -1
- package/bundle/node_modules/@waku/zerokit-rln-wasm/rln_wasm.js +517 -255
- package/bundle/node_modules/it-length-prefixed/dist/src/decode.js +6 -0
- package/bundle/node_modules/multiformats/dist/src/bases/base10.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base16.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base2.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base256emoji.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base32.js +11 -9
- package/bundle/node_modules/multiformats/dist/src/bases/base36.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base58.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base64.js +6 -4
- package/bundle/node_modules/multiformats/dist/src/bases/base8.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/identity.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/basics.js +15 -0
- package/bundle/node_modules/multiformats/dist/src/bytes.js +15 -1
- package/bundle/node_modules/multiformats/dist/src/cid.js +371 -0
- package/bundle/node_modules/multiformats/dist/src/hashes/digest.js +62 -0
- package/bundle/node_modules/multiformats/dist/src/varint.js +15 -0
- package/bundle/node_modules/multiformats/dist/src/vendor/varint.js +78 -0
- package/bundle/node_modules/protons-runtime/dist/src/codec.js +20 -0
- package/bundle/node_modules/protons-runtime/dist/src/codecs/enum.js +24 -0
- package/bundle/node_modules/protons-runtime/dist/src/codecs/message.js +7 -0
- package/bundle/node_modules/protons-runtime/dist/src/decode.js +8 -0
- package/bundle/node_modules/protons-runtime/dist/src/encode.js +11 -0
- package/bundle/node_modules/protons-runtime/dist/src/index.js +30 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/float.js +54 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/longbits.js +175 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/pool.js +28 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/reader.js +367 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/utf8.js +99 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/writer.js +438 -0
- package/bundle/node_modules/uint8-varint/dist/src/index.js +124 -0
- package/bundle/node_modules/uint8arrays/dist/src/alloc.js +17 -0
- package/bundle/node_modules/uint8arrays/dist/src/concat.js +20 -0
- package/bundle/node_modules/uint8arrays/dist/src/from-string.js +19 -0
- package/bundle/node_modules/uint8arrays/dist/src/to-string.js +19 -0
- package/bundle/node_modules/uint8arrays/dist/src/util/as-uint8array.js +9 -0
- package/bundle/node_modules/uint8arrays/dist/src/util/bases.js +49 -0
- package/bundle/packages/core/dist/lib/connection_manager/connection_limiter.js +18 -0
- package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +24 -0
- package/bundle/packages/core/dist/lib/connection_manager/dialer.js +14 -0
- package/bundle/packages/core/dist/lib/connection_manager/discovery_dialer.js +14 -0
- package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +15 -0
- package/bundle/packages/core/dist/lib/connection_manager/shard_reader.js +14 -0
- package/bundle/packages/core/dist/lib/filter/filter.js +28 -0
- package/bundle/packages/core/dist/lib/light_push/light_push.js +28 -0
- package/bundle/packages/core/dist/lib/message/version_0.js +172 -0
- package/bundle/packages/core/dist/lib/metadata/metadata.js +28 -0
- package/bundle/packages/core/dist/lib/store/store.js +24 -0
- package/bundle/packages/interfaces/dist/connection_manager.js +9 -0
- package/bundle/packages/interfaces/dist/health_status.js +17 -0
- package/bundle/packages/interfaces/dist/protocols.js +92 -0
- package/bundle/packages/interfaces/dist/waku.js +7 -0
- package/bundle/packages/proto/dist/generated/filter.js +447 -0
- package/bundle/packages/proto/dist/generated/filter_v2.js +426 -0
- package/bundle/packages/proto/dist/generated/light_push.js +550 -0
- package/bundle/packages/proto/dist/generated/message.js +215 -0
- package/bundle/packages/proto/dist/generated/metadata.js +132 -0
- package/bundle/packages/proto/dist/generated/peer_exchange.js +211 -0
- package/bundle/packages/proto/dist/generated/sds_message.js +172 -0
- package/bundle/packages/proto/dist/generated/store_v3.js +492 -0
- package/bundle/packages/proto/dist/generated/topic_only_message.js +63 -0
- package/bundle/packages/rln/dist/codec.js +92 -0
- package/bundle/packages/rln/dist/contract/constants.js +14 -7
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +2 -1
- package/bundle/packages/rln/dist/contract/rln_contract.js +109 -0
- package/bundle/packages/rln/dist/credentials_manager.js +45 -4
- package/bundle/packages/rln/dist/identity.js +2 -1
- package/bundle/packages/rln/dist/keystore/keystore.js +30 -9
- package/bundle/packages/rln/dist/message.js +59 -0
- package/bundle/packages/rln/dist/proof.js +54 -0
- package/bundle/packages/rln/dist/resources/verification_key.js +112 -0
- package/bundle/packages/rln/dist/resources/witness_calculator.js +1 -1
- package/bundle/packages/rln/dist/rln.js +36 -4
- package/bundle/packages/rln/dist/root_tracker.js +76 -0
- package/bundle/packages/rln/dist/utils/bytes.js +70 -31
- package/bundle/packages/rln/dist/utils/epoch.js +23 -1
- package/bundle/packages/rln/dist/utils/hash.js +10 -0
- package/bundle/packages/rln/dist/zerokit.js +99 -2
- package/bundle/packages/utils/dist/bytes/index.js +31 -0
- package/bundle/resources/rln.wasm +0 -0
- package/bundle/resources/rln_final.zkey +0 -0
- package/bundle/resources/verification_key.d.ts +13 -0
- package/bundle/resources/verification_key.js +112 -0
- package/bundle/resources/witness_calculator.d.ts +7 -21
- package/bundle/resources/witness_calculator.js +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/codec.d.ts +40 -0
- package/dist/codec.js +79 -0
- package/dist/codec.js.map +1 -0
- package/dist/codec.test-utils.d.ts +37 -0
- package/dist/codec.test-utils.js +61 -0
- package/dist/codec.test-utils.js.map +1 -0
- package/dist/contract/constants.d.ts +10 -3
- package/dist/contract/constants.js +13 -6
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.js +1 -0
- package/dist/contract/index.js.map +1 -1
- package/dist/contract/rln_base_contract.js +2 -1
- package/dist/contract/rln_base_contract.js.map +1 -1
- package/dist/contract/rln_contract.d.ts +17 -0
- package/dist/contract/rln_contract.js +107 -0
- package/dist/contract/rln_contract.js.map +1 -0
- package/dist/contract/test_setup.d.ts +26 -0
- package/dist/contract/test_setup.js +56 -0
- package/dist/contract/test_setup.js.map +1 -0
- package/dist/contract/test_utils.d.ts +39 -0
- package/dist/contract/test_utils.js +118 -0
- package/dist/contract/test_utils.js.map +1 -0
- package/dist/credentials_manager.d.ts +14 -2
- package/dist/credentials_manager.js +45 -4
- package/dist/credentials_manager.js.map +1 -1
- package/dist/identity.js +2 -1
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.js +30 -9
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/message.d.ts +19 -0
- package/dist/message.js +51 -0
- package/dist/message.js.map +1 -0
- package/dist/proof.d.ts +21 -0
- package/dist/proof.js +50 -0
- package/dist/proof.js.map +1 -0
- package/dist/resources/rln.wasm +0 -0
- package/dist/resources/rln_final.zkey +0 -0
- package/dist/resources/verification_key.d.ts +13 -0
- package/dist/resources/verification_key.js +112 -0
- package/dist/resources/witness_calculator.d.ts +7 -21
- package/dist/resources/witness_calculator.js +1 -1
- package/dist/rln.d.ts +9 -0
- package/dist/rln.js +32 -4
- package/dist/rln.js.map +1 -1
- package/dist/root_tracker.d.ts +10 -0
- package/dist/root_tracker.js +75 -0
- package/dist/root_tracker.js.map +1 -0
- package/dist/utils/bytes.d.ts +31 -9
- package/dist/utils/bytes.js +70 -31
- package/dist/utils/bytes.js.map +1 -1
- package/dist/zerokit.d.ts +11 -0
- package/dist/zerokit.js +97 -1
- package/dist/zerokit.js.map +1 -1
- package/package.json +1 -1
- package/src/codec.test-utils.ts +88 -0
- package/src/codec.ts +138 -0
- package/src/contract/constants.ts +16 -6
- package/src/contract/index.ts +1 -0
- package/src/contract/rln_base_contract.ts +2 -1
- package/src/contract/rln_contract.ts +147 -0
- package/src/contract/test_setup.ts +86 -0
- package/src/contract/test_utils.ts +179 -0
- package/src/credentials_manager.ts +72 -8
- package/src/identity.ts +2 -1
- package/src/index.ts +11 -1
- package/src/keystore/keystore.ts +32 -9
- package/src/message.ts +73 -0
- package/src/proof.ts +69 -0
- package/src/resources/verification_key.d.ts +13 -0
- package/src/resources/witness_calculator.d.ts +7 -21
- package/src/rln.ts +65 -5
- package/src/root_tracker.ts +92 -0
- package/src/utils/bytes.ts +73 -36
- package/src/zerokit.ts +217 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { equals as equals$1, coerce } from '../bytes.js';
|
|
2
|
+
import { encodeTo, decode as decode$1, encodingLength } from '../varint.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a multihash digest.
|
|
6
|
+
*/
|
|
7
|
+
function create(code, digest) {
|
|
8
|
+
const size = digest.byteLength;
|
|
9
|
+
const sizeOffset = encodingLength(code);
|
|
10
|
+
const digestOffset = sizeOffset + encodingLength(size);
|
|
11
|
+
const bytes = new Uint8Array(digestOffset + size);
|
|
12
|
+
encodeTo(code, bytes, 0);
|
|
13
|
+
encodeTo(size, bytes, sizeOffset);
|
|
14
|
+
bytes.set(digest, digestOffset);
|
|
15
|
+
return new Digest(code, size, digest, bytes);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Turns bytes representation of multihash digest into an instance.
|
|
19
|
+
*/
|
|
20
|
+
function decode(multihash) {
|
|
21
|
+
const bytes = coerce(multihash);
|
|
22
|
+
const [code, sizeOffset] = decode$1(bytes);
|
|
23
|
+
const [size, digestOffset] = decode$1(bytes.subarray(sizeOffset));
|
|
24
|
+
const digest = bytes.subarray(sizeOffset + digestOffset);
|
|
25
|
+
if (digest.byteLength !== size) {
|
|
26
|
+
throw new Error('Incorrect length');
|
|
27
|
+
}
|
|
28
|
+
return new Digest(code, size, digest, bytes);
|
|
29
|
+
}
|
|
30
|
+
function equals(a, b) {
|
|
31
|
+
if (a === b) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const data = b;
|
|
36
|
+
return (a.code === data.code &&
|
|
37
|
+
a.size === data.size &&
|
|
38
|
+
data.bytes instanceof Uint8Array &&
|
|
39
|
+
equals$1(a.bytes, data.bytes));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Represents a multihash digest which carries information about the
|
|
44
|
+
* hashing algorithm and an actual hash digest.
|
|
45
|
+
*/
|
|
46
|
+
class Digest {
|
|
47
|
+
code;
|
|
48
|
+
size;
|
|
49
|
+
digest;
|
|
50
|
+
bytes;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a multihash digest.
|
|
53
|
+
*/
|
|
54
|
+
constructor(code, size, digest, bytes) {
|
|
55
|
+
this.code = code;
|
|
56
|
+
this.size = size;
|
|
57
|
+
this.digest = digest;
|
|
58
|
+
this.bytes = bytes;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { Digest, create, decode, equals };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import _brrp_varint from './vendor/varint.js';
|
|
2
|
+
|
|
3
|
+
function decode(data, offset = 0) {
|
|
4
|
+
const code = _brrp_varint.decode(data, offset);
|
|
5
|
+
return [code, _brrp_varint.decode.bytes];
|
|
6
|
+
}
|
|
7
|
+
function encodeTo(int, target, offset = 0) {
|
|
8
|
+
_brrp_varint.encode(int, target, offset);
|
|
9
|
+
return target;
|
|
10
|
+
}
|
|
11
|
+
function encodingLength(int) {
|
|
12
|
+
return _brrp_varint.encodingLength(int);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { decode, encodeTo, encodingLength };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
var encode_1 = encode;
|
|
3
|
+
var MSB = 0x80, MSBALL = -128, INT = Math.pow(2, 31);
|
|
4
|
+
/**
|
|
5
|
+
* @param {number} num
|
|
6
|
+
* @param {number[]} out
|
|
7
|
+
* @param {number} offset
|
|
8
|
+
*/
|
|
9
|
+
function encode(num, out, offset) {
|
|
10
|
+
out = out || [];
|
|
11
|
+
offset = offset || 0;
|
|
12
|
+
var oldOffset = offset;
|
|
13
|
+
while (num >= INT) {
|
|
14
|
+
out[offset++] = (num & 0xFF) | MSB;
|
|
15
|
+
num /= 128;
|
|
16
|
+
}
|
|
17
|
+
while (num & MSBALL) {
|
|
18
|
+
out[offset++] = (num & 0xFF) | MSB;
|
|
19
|
+
num >>>= 7;
|
|
20
|
+
}
|
|
21
|
+
out[offset] = num | 0;
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
encode.bytes = offset - oldOffset + 1;
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
var decode = read;
|
|
27
|
+
var MSB$1 = 0x80, REST$1 = 0x7F;
|
|
28
|
+
/**
|
|
29
|
+
* @param {string | any[]} buf
|
|
30
|
+
* @param {number} offset
|
|
31
|
+
*/
|
|
32
|
+
function read(buf, offset) {
|
|
33
|
+
var res = 0, offset = offset || 0, shift = 0, counter = offset, b, l = buf.length;
|
|
34
|
+
do {
|
|
35
|
+
if (counter >= l) {
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
read.bytes = 0;
|
|
38
|
+
throw new RangeError('Could not decode varint');
|
|
39
|
+
}
|
|
40
|
+
b = buf[counter++];
|
|
41
|
+
res += shift < 28
|
|
42
|
+
? (b & REST$1) << shift
|
|
43
|
+
: (b & REST$1) * Math.pow(2, shift);
|
|
44
|
+
shift += 7;
|
|
45
|
+
} while (b >= MSB$1);
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
read.bytes = counter - offset;
|
|
48
|
+
return res;
|
|
49
|
+
}
|
|
50
|
+
var N1 = Math.pow(2, 7);
|
|
51
|
+
var N2 = Math.pow(2, 14);
|
|
52
|
+
var N3 = Math.pow(2, 21);
|
|
53
|
+
var N4 = Math.pow(2, 28);
|
|
54
|
+
var N5 = Math.pow(2, 35);
|
|
55
|
+
var N6 = Math.pow(2, 42);
|
|
56
|
+
var N7 = Math.pow(2, 49);
|
|
57
|
+
var N8 = Math.pow(2, 56);
|
|
58
|
+
var N9 = Math.pow(2, 63);
|
|
59
|
+
var length = function (/** @type {number} */ value) {
|
|
60
|
+
return (value < N1 ? 1
|
|
61
|
+
: value < N2 ? 2
|
|
62
|
+
: value < N3 ? 3
|
|
63
|
+
: value < N4 ? 4
|
|
64
|
+
: value < N5 ? 5
|
|
65
|
+
: value < N6 ? 6
|
|
66
|
+
: value < N7 ? 7
|
|
67
|
+
: value < N8 ? 8
|
|
68
|
+
: value < N9 ? 9
|
|
69
|
+
: 10);
|
|
70
|
+
};
|
|
71
|
+
var varint = {
|
|
72
|
+
encode: encode_1,
|
|
73
|
+
decode: decode,
|
|
74
|
+
encodingLength: length
|
|
75
|
+
};
|
|
76
|
+
var _brrp_varint = varint;
|
|
77
|
+
|
|
78
|
+
export { _brrp_varint as default };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// https://developers.google.com/protocol-buffers/docs/encoding#structure
|
|
2
|
+
var CODEC_TYPES;
|
|
3
|
+
(function (CODEC_TYPES) {
|
|
4
|
+
CODEC_TYPES[CODEC_TYPES["VARINT"] = 0] = "VARINT";
|
|
5
|
+
CODEC_TYPES[CODEC_TYPES["BIT64"] = 1] = "BIT64";
|
|
6
|
+
CODEC_TYPES[CODEC_TYPES["LENGTH_DELIMITED"] = 2] = "LENGTH_DELIMITED";
|
|
7
|
+
CODEC_TYPES[CODEC_TYPES["START_GROUP"] = 3] = "START_GROUP";
|
|
8
|
+
CODEC_TYPES[CODEC_TYPES["END_GROUP"] = 4] = "END_GROUP";
|
|
9
|
+
CODEC_TYPES[CODEC_TYPES["BIT32"] = 5] = "BIT32";
|
|
10
|
+
})(CODEC_TYPES || (CODEC_TYPES = {}));
|
|
11
|
+
function createCodec(name, type, encode, decode) {
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
type,
|
|
15
|
+
encode,
|
|
16
|
+
decode
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { CODEC_TYPES, createCodec };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createCodec, CODEC_TYPES } from '../codec.js';
|
|
2
|
+
|
|
3
|
+
function enumeration(v) {
|
|
4
|
+
function findValue(val) {
|
|
5
|
+
// Use the reverse mapping to look up the enum key for the stored value
|
|
6
|
+
// https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
|
|
7
|
+
if (v[val.toString()] == null) {
|
|
8
|
+
throw new Error('Invalid enum value');
|
|
9
|
+
}
|
|
10
|
+
return v[val];
|
|
11
|
+
}
|
|
12
|
+
const encode = function enumEncode(val, writer) {
|
|
13
|
+
const enumValue = findValue(val);
|
|
14
|
+
writer.int32(enumValue);
|
|
15
|
+
};
|
|
16
|
+
const decode = function enumDecode(reader) {
|
|
17
|
+
const val = reader.int32();
|
|
18
|
+
return findValue(val);
|
|
19
|
+
};
|
|
20
|
+
// @ts-expect-error yeah yeah
|
|
21
|
+
return createCodec('enum', CODEC_TYPES.VARINT, encode, decode);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { enumeration };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import './utils/float.js';
|
|
2
|
+
import './utils/longbits.js';
|
|
3
|
+
export { createWriter as writer } from './utils/writer.js';
|
|
4
|
+
import './codec.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*
|
|
9
|
+
* This module contains serialization/deserialization code used when encoding/decoding protobufs.
|
|
10
|
+
*
|
|
11
|
+
* It should be declared as a dependency of your project:
|
|
12
|
+
*
|
|
13
|
+
* ```console
|
|
14
|
+
* npm i protons-runtime
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Thrown when a repeated field has too many elements
|
|
19
|
+
*/
|
|
20
|
+
class MaxLengthError extends Error {
|
|
21
|
+
/**
|
|
22
|
+
* This will be removed in a future release
|
|
23
|
+
*
|
|
24
|
+
* @deprecated use the `.name` property instead
|
|
25
|
+
*/
|
|
26
|
+
code = 'ERR_MAX_LENGTH';
|
|
27
|
+
name = 'MaxLengthError';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { MaxLengthError };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const f32 = new Float32Array([-0]);
|
|
2
|
+
const f8b = new Uint8Array(f32.buffer);
|
|
3
|
+
/**
|
|
4
|
+
* Writes a 32 bit float to a buffer using little endian byte order
|
|
5
|
+
*/
|
|
6
|
+
function writeFloatLE(val, buf, pos) {
|
|
7
|
+
f32[0] = val;
|
|
8
|
+
buf[pos] = f8b[0];
|
|
9
|
+
buf[pos + 1] = f8b[1];
|
|
10
|
+
buf[pos + 2] = f8b[2];
|
|
11
|
+
buf[pos + 3] = f8b[3];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reads a 32 bit float from a buffer using little endian byte order
|
|
15
|
+
*/
|
|
16
|
+
function readFloatLE(buf, pos) {
|
|
17
|
+
f8b[0] = buf[pos];
|
|
18
|
+
f8b[1] = buf[pos + 1];
|
|
19
|
+
f8b[2] = buf[pos + 2];
|
|
20
|
+
f8b[3] = buf[pos + 3];
|
|
21
|
+
return f32[0];
|
|
22
|
+
}
|
|
23
|
+
const f64 = new Float64Array([-0]);
|
|
24
|
+
const d8b = new Uint8Array(f64.buffer);
|
|
25
|
+
/**
|
|
26
|
+
* Writes a 64 bit double to a buffer using little endian byte order
|
|
27
|
+
*/
|
|
28
|
+
function writeDoubleLE(val, buf, pos) {
|
|
29
|
+
f64[0] = val;
|
|
30
|
+
buf[pos] = d8b[0];
|
|
31
|
+
buf[pos + 1] = d8b[1];
|
|
32
|
+
buf[pos + 2] = d8b[2];
|
|
33
|
+
buf[pos + 3] = d8b[3];
|
|
34
|
+
buf[pos + 4] = d8b[4];
|
|
35
|
+
buf[pos + 5] = d8b[5];
|
|
36
|
+
buf[pos + 6] = d8b[6];
|
|
37
|
+
buf[pos + 7] = d8b[7];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Reads a 64 bit double from a buffer using little endian byte order
|
|
41
|
+
*/
|
|
42
|
+
function readDoubleLE(buf, pos) {
|
|
43
|
+
d8b[0] = buf[pos];
|
|
44
|
+
d8b[1] = buf[pos + 1];
|
|
45
|
+
d8b[2] = buf[pos + 2];
|
|
46
|
+
d8b[3] = buf[pos + 3];
|
|
47
|
+
d8b[4] = buf[pos + 4];
|
|
48
|
+
d8b[5] = buf[pos + 5];
|
|
49
|
+
d8b[6] = buf[pos + 6];
|
|
50
|
+
d8b[7] = buf[pos + 7];
|
|
51
|
+
return f64[0];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { readDoubleLE, readFloatLE, writeDoubleLE, writeFloatLE };
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// the largest BigInt we can safely downcast to a Number
|
|
2
|
+
const MAX_SAFE_NUMBER_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
|
|
3
|
+
const MIN_SAFE_NUMBER_INTEGER = BigInt(Number.MIN_SAFE_INTEGER);
|
|
4
|
+
/**
|
|
5
|
+
* Constructs new long bits.
|
|
6
|
+
*
|
|
7
|
+
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
|
|
8
|
+
* @memberof util
|
|
9
|
+
* @function Object() { [native code] }
|
|
10
|
+
* @param {number} lo - Low 32 bits, unsigned
|
|
11
|
+
* @param {number} hi - High 32 bits, unsigned
|
|
12
|
+
*/
|
|
13
|
+
class LongBits {
|
|
14
|
+
lo;
|
|
15
|
+
hi;
|
|
16
|
+
constructor(lo, hi) {
|
|
17
|
+
// note that the casts below are theoretically unnecessary as of today, but older statically
|
|
18
|
+
// generated converter code might still call the ctor with signed 32bits. kept for compat.
|
|
19
|
+
/**
|
|
20
|
+
* Low bits
|
|
21
|
+
*/
|
|
22
|
+
this.lo = lo | 0;
|
|
23
|
+
/**
|
|
24
|
+
* High bits
|
|
25
|
+
*/
|
|
26
|
+
this.hi = hi | 0;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Converts this long bits to a possibly unsafe JavaScript number
|
|
30
|
+
*/
|
|
31
|
+
toNumber(unsigned = false) {
|
|
32
|
+
if (!unsigned && (this.hi >>> 31) > 0) {
|
|
33
|
+
const lo = ~this.lo + 1 >>> 0;
|
|
34
|
+
let hi = ~this.hi >>> 0;
|
|
35
|
+
if (lo === 0) {
|
|
36
|
+
hi = hi + 1 >>> 0;
|
|
37
|
+
}
|
|
38
|
+
return -(lo + hi * 4294967296);
|
|
39
|
+
}
|
|
40
|
+
return this.lo + this.hi * 4294967296;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Converts this long bits to a bigint
|
|
44
|
+
*/
|
|
45
|
+
toBigInt(unsigned = false) {
|
|
46
|
+
if (unsigned) {
|
|
47
|
+
return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
|
|
48
|
+
}
|
|
49
|
+
if ((this.hi >>> 31) !== 0) {
|
|
50
|
+
const lo = ~this.lo + 1 >>> 0;
|
|
51
|
+
let hi = ~this.hi >>> 0;
|
|
52
|
+
if (lo === 0) {
|
|
53
|
+
hi = hi + 1 >>> 0;
|
|
54
|
+
}
|
|
55
|
+
return -(BigInt(lo) + (BigInt(hi) << 32n));
|
|
56
|
+
}
|
|
57
|
+
return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts this long bits to a string
|
|
61
|
+
*/
|
|
62
|
+
toString(unsigned = false) {
|
|
63
|
+
return this.toBigInt(unsigned).toString();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Zig-zag encodes this long bits
|
|
67
|
+
*/
|
|
68
|
+
zzEncode() {
|
|
69
|
+
const mask = this.hi >> 31;
|
|
70
|
+
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
|
|
71
|
+
this.lo = (this.lo << 1 ^ mask) >>> 0;
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Zig-zag decodes this long bits
|
|
76
|
+
*/
|
|
77
|
+
zzDecode() {
|
|
78
|
+
const mask = -(this.lo & 1);
|
|
79
|
+
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
|
|
80
|
+
this.hi = (this.hi >>> 1 ^ mask) >>> 0;
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Calculates the length of this longbits when encoded as a varint.
|
|
85
|
+
*/
|
|
86
|
+
length() {
|
|
87
|
+
const part0 = this.lo;
|
|
88
|
+
const part1 = (this.lo >>> 28 | this.hi << 4) >>> 0;
|
|
89
|
+
const part2 = this.hi >>> 24;
|
|
90
|
+
return part2 === 0
|
|
91
|
+
? part1 === 0
|
|
92
|
+
? part0 < 16384
|
|
93
|
+
? part0 < 128 ? 1 : 2
|
|
94
|
+
: part0 < 2097152 ? 3 : 4
|
|
95
|
+
: part1 < 16384
|
|
96
|
+
? part1 < 128 ? 5 : 6
|
|
97
|
+
: part1 < 2097152 ? 7 : 8
|
|
98
|
+
: part2 < 128 ? 9 : 10;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Constructs new long bits from the specified number
|
|
102
|
+
*/
|
|
103
|
+
static fromBigInt(value) {
|
|
104
|
+
if (value === 0n) {
|
|
105
|
+
return zero;
|
|
106
|
+
}
|
|
107
|
+
if (value < MAX_SAFE_NUMBER_INTEGER && value > MIN_SAFE_NUMBER_INTEGER) {
|
|
108
|
+
return this.fromNumber(Number(value));
|
|
109
|
+
}
|
|
110
|
+
const negative = value < 0n;
|
|
111
|
+
if (negative) {
|
|
112
|
+
value = -value;
|
|
113
|
+
}
|
|
114
|
+
let hi = value >> 32n;
|
|
115
|
+
let lo = value - (hi << 32n);
|
|
116
|
+
if (negative) {
|
|
117
|
+
hi = ~hi | 0n;
|
|
118
|
+
lo = ~lo | 0n;
|
|
119
|
+
if (++lo > TWO_32) {
|
|
120
|
+
lo = 0n;
|
|
121
|
+
if (++hi > TWO_32) {
|
|
122
|
+
hi = 0n;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return new LongBits(Number(lo), Number(hi));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Constructs new long bits from the specified number
|
|
130
|
+
*/
|
|
131
|
+
static fromNumber(value) {
|
|
132
|
+
if (value === 0) {
|
|
133
|
+
return zero;
|
|
134
|
+
}
|
|
135
|
+
const sign = value < 0;
|
|
136
|
+
if (sign) {
|
|
137
|
+
value = -value;
|
|
138
|
+
}
|
|
139
|
+
let lo = value >>> 0;
|
|
140
|
+
let hi = (value - lo) / 4294967296 >>> 0;
|
|
141
|
+
if (sign) {
|
|
142
|
+
hi = ~hi >>> 0;
|
|
143
|
+
lo = ~lo >>> 0;
|
|
144
|
+
if (++lo > 4294967295) {
|
|
145
|
+
lo = 0;
|
|
146
|
+
if (++hi > 4294967295) {
|
|
147
|
+
hi = 0;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return new LongBits(lo, hi);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Constructs new long bits from a number, long or string
|
|
155
|
+
*/
|
|
156
|
+
static from(value) {
|
|
157
|
+
if (typeof value === 'number') {
|
|
158
|
+
return LongBits.fromNumber(value);
|
|
159
|
+
}
|
|
160
|
+
if (typeof value === 'bigint') {
|
|
161
|
+
return LongBits.fromBigInt(value);
|
|
162
|
+
}
|
|
163
|
+
if (typeof value === 'string') {
|
|
164
|
+
return LongBits.fromBigInt(BigInt(value));
|
|
165
|
+
}
|
|
166
|
+
return value.low != null || value.high != null ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const zero = new LongBits(0, 0);
|
|
170
|
+
zero.toBigInt = function () { return 0n; };
|
|
171
|
+
zero.zzEncode = zero.zzDecode = function () { return this; };
|
|
172
|
+
zero.length = function () { return 1; };
|
|
173
|
+
const TWO_32 = 4294967296n;
|
|
174
|
+
|
|
175
|
+
export { LongBits };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { allocUnsafe } from '../../../../uint8arrays/dist/src/alloc.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A general purpose buffer pool
|
|
5
|
+
*/
|
|
6
|
+
function pool(size) {
|
|
7
|
+
const SIZE = 8192;
|
|
8
|
+
const MAX = SIZE >>> 1;
|
|
9
|
+
let slab;
|
|
10
|
+
let offset = SIZE;
|
|
11
|
+
return function poolAlloc(size) {
|
|
12
|
+
if (size < 1 || size > MAX) {
|
|
13
|
+
return allocUnsafe(size);
|
|
14
|
+
}
|
|
15
|
+
if (offset + size > SIZE) {
|
|
16
|
+
slab = allocUnsafe(SIZE);
|
|
17
|
+
offset = 0;
|
|
18
|
+
}
|
|
19
|
+
const buf = slab.subarray(offset, offset += size);
|
|
20
|
+
if ((offset & 7) !== 0) {
|
|
21
|
+
// align to 32 bit
|
|
22
|
+
offset = (offset | 7) + 1;
|
|
23
|
+
}
|
|
24
|
+
return buf;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { pool as default };
|