@hfunlabs/hyperliquid 0.30.0-hfunlabs.1 → 0.30.2-hfunlabs.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.
- package/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +58 -0
- package/esm/_dnt.shims.js.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts +195 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js +457 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts +90 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js +207 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts +159 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js +555 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts +325 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js +1222 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts +76 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js +283 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts +103 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.js +241 -0
- package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.js.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts +49 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js +147 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts +55 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js +67 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts +36 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js +90 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts +199 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js +397 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts +58 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js +254 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts +124 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js +243 -0
- package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts +44 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js +144 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts +38 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js +96 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts +23 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js +98 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts +300 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js +535 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts +98 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js +207 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts +78 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js +298 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts +6 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js +6 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts +66 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js +180 -0
- package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts +877 -0
- package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts.map +1 -0
- package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js +2001 -0
- package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js.map +1 -0
- package/esm/deps/jsr.io/@scure/base/2.0.0/index.d.ts +294 -0
- package/esm/deps/jsr.io/@scure/base/2.0.0/index.d.ts.map +1 -0
- package/esm/deps/jsr.io/@scure/base/2.0.0/index.js +704 -0
- package/esm/deps/jsr.io/@scure/base/2.0.0/index.js.map +1 -0
- package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts +93 -0
- package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js +137 -0
- package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js.map +1 -0
- package/esm/src/api/exchange/_methods/_base/_nonce.d.ts +3 -2
- package/esm/src/api/exchange/_methods/_base/_nonce.d.ts.map +1 -1
- package/esm/src/api/exchange/_methods/_base/_nonce.js +3 -2
- package/esm/src/api/exchange/_methods/_base/_nonce.js.map +1 -1
- package/esm/src/api/exchange/_methods/_base/_semaphore.d.ts +7 -6
- package/esm/src/api/exchange/_methods/_base/_semaphore.d.ts.map +1 -1
- package/esm/src/api/exchange/_methods/_base/_semaphore.js +34 -47
- package/esm/src/api/exchange/_methods/_base/_semaphore.js.map +1 -1
- package/esm/src/api/exchange/_methods/_base/execute.d.ts +14 -3
- package/esm/src/api/exchange/_methods/_base/execute.d.ts.map +1 -1
- package/esm/src/api/exchange/_methods/_base/execute.js +44 -10
- package/esm/src/api/exchange/_methods/_base/execute.js.map +1 -1
- package/esm/src/api/exchange/_methods/batchModify.d.ts +6 -6
- package/esm/src/api/exchange/_methods/createVault.d.ts +5 -5
- package/esm/src/api/exchange/_methods/modify.d.ts +5 -5
- package/esm/src/api/exchange/_methods/order.d.ts +13 -13
- package/esm/src/api/exchange/_methods/order.d.ts.map +1 -1
- package/esm/src/api/exchange/_methods/order.js +4 -2
- package/esm/src/api/exchange/_methods/order.js.map +1 -1
- package/esm/src/api/exchange/client.d.ts +1 -1
- package/esm/src/api/exchange/client.d.ts.map +1 -1
- package/esm/src/api/exchange/client.js.map +1 -1
- package/esm/src/api/exchange/mod.d.ts +1 -1
- package/esm/src/api/exchange/mod.d.ts.map +1 -1
- package/esm/src/api/exchange/mod.js.map +1 -1
- package/esm/src/api/info/_methods/_base/commonSchemas.d.ts +8 -8
- package/esm/src/api/info/_methods/frontendOpenOrders.d.ts +3 -3
- package/esm/src/api/info/_methods/historicalOrders.d.ts +4 -4
- package/esm/src/api/info/_methods/l3Orders.d.ts +11 -11
- package/esm/src/api/info/_methods/l4Orders.d.ts +6 -6
- package/esm/src/api/info/_methods/openOrders.d.ts +3 -3
- package/esm/src/api/info/_methods/orderStatus.d.ts +5 -5
- package/esm/src/api/info/_methods/recentTrades.d.ts +2 -2
- package/esm/src/api/info/_methods/twapHistory.d.ts +4 -4
- package/esm/src/api/info/_methods/userFills.d.ts +3 -3
- package/esm/src/api/info/_methods/userFillsByTime.d.ts +4 -4
- package/esm/src/api/info/_methods/userFunding.d.ts +3 -3
- package/esm/src/api/info/_methods/userFunding.js +2 -2
- package/esm/src/api/info/_methods/userFunding.js.map +1 -1
- package/esm/src/api/info/_methods/userTwapSliceFills.d.ts +4 -4
- package/esm/src/api/info/_methods/userTwapSliceFillsByTime.d.ts +5 -5
- package/esm/src/api/info/_methods/webData2.d.ts +9 -9
- package/esm/src/api/subscription/_methods/assetCtxs.d.ts +2 -0
- package/esm/src/api/subscription/_methods/assetCtxs.d.ts.map +1 -1
- package/esm/src/api/subscription/_methods/assetCtxs.js.map +1 -1
- package/esm/src/api/subscription/_methods/clearinghouseState.d.ts +2 -0
- package/esm/src/api/subscription/_methods/clearinghouseState.d.ts.map +1 -1
- package/esm/src/api/subscription/_methods/clearinghouseState.js +2 -0
- package/esm/src/api/subscription/_methods/clearinghouseState.js.map +1 -1
- package/esm/src/api/subscription/_methods/explorerBlock.d.ts +2 -0
- package/esm/src/api/subscription/_methods/explorerBlock.d.ts.map +1 -1
- package/esm/src/api/subscription/_methods/explorerBlock.js +2 -0
- package/esm/src/api/subscription/_methods/explorerBlock.js.map +1 -1
- package/esm/src/api/subscription/_methods/explorerTxs.d.ts +2 -0
- package/esm/src/api/subscription/_methods/explorerTxs.d.ts.map +1 -1
- package/esm/src/api/subscription/_methods/explorerTxs.js +2 -0
- package/esm/src/api/subscription/_methods/explorerTxs.js.map +1 -1
- package/esm/src/api/subscription/_methods/openOrders.d.ts +5 -5
- package/esm/src/api/subscription/_methods/orderUpdates.d.ts +4 -4
- package/esm/src/api/subscription/_methods/trades.d.ts +3 -3
- package/esm/src/api/subscription/_methods/twapStates.d.ts +9 -4
- package/esm/src/api/subscription/_methods/twapStates.d.ts.map +1 -1
- package/esm/src/api/subscription/_methods/twapStates.js +6 -1
- package/esm/src/api/subscription/_methods/twapStates.js.map +1 -1
- package/esm/src/api/subscription/_methods/userEvents.d.ts +17 -17
- package/esm/src/api/subscription/_methods/userFills.d.ts +5 -5
- package/esm/src/api/subscription/_methods/userHistoricalOrders.d.ts +6 -6
- package/esm/src/api/subscription/_methods/userTwapHistory.d.ts +6 -6
- package/esm/src/api/subscription/_methods/userTwapSliceFills.d.ts +6 -6
- package/esm/src/api/subscription/_methods/webData2.d.ts +11 -11
- package/esm/src/signing/_privateKeySigner.js +1 -1
- package/esm/src/signing/_privateKeySigner.js.map +1 -1
- package/esm/src/signing/mod.js +1 -1
- package/esm/src/signing/mod.js.map +1 -1
- package/package.json +2 -3
- package/script/_dnt.shims.d.ts +2 -0
- package/script/_dnt.shims.d.ts.map +1 -0
- package/script/_dnt.shims.js +61 -0
- package/script/_dnt.shims.js.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts +195 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js +468 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts +90 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js +215 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts +159 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js +578 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts +325 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js +1232 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts +76 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js +286 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts +103 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.js +273 -0
- package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.js.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts +49 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js +153 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts +55 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js +90 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts +36 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js +95 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts +199 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js +439 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts +58 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js +259 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts +124 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js +304 -0
- package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts +44 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js +182 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts +38 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js +99 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts +23 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js +134 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts +300 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js +576 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts +98 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js +211 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts +78 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js +308 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts +6 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js +21 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts +66 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js +195 -0
- package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts +877 -0
- package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts.map +1 -0
- package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js +2032 -0
- package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js.map +1 -0
- package/script/deps/jsr.io/@scure/base/2.0.0/index.d.ts +294 -0
- package/script/deps/jsr.io/@scure/base/2.0.0/index.d.ts.map +1 -0
- package/script/deps/jsr.io/@scure/base/2.0.0/index.js +710 -0
- package/script/deps/jsr.io/@scure/base/2.0.0/index.js.map +1 -0
- package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts +93 -0
- package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js +141 -0
- package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js.map +1 -0
- package/script/src/api/exchange/_methods/_base/_nonce.d.ts +3 -2
- package/script/src/api/exchange/_methods/_base/_nonce.d.ts.map +1 -1
- package/script/src/api/exchange/_methods/_base/_nonce.js +4 -3
- package/script/src/api/exchange/_methods/_base/_nonce.js.map +1 -1
- package/script/src/api/exchange/_methods/_base/_semaphore.d.ts +7 -6
- package/script/src/api/exchange/_methods/_base/_semaphore.d.ts.map +1 -1
- package/script/src/api/exchange/_methods/_base/_semaphore.js +35 -50
- package/script/src/api/exchange/_methods/_base/_semaphore.js.map +1 -1
- package/script/src/api/exchange/_methods/_base/execute.d.ts +14 -3
- package/script/src/api/exchange/_methods/_base/execute.d.ts.map +1 -1
- package/script/src/api/exchange/_methods/_base/execute.js +43 -9
- package/script/src/api/exchange/_methods/_base/execute.js.map +1 -1
- package/script/src/api/exchange/_methods/batchModify.d.ts +6 -6
- package/script/src/api/exchange/_methods/createVault.d.ts +5 -5
- package/script/src/api/exchange/_methods/modify.d.ts +5 -5
- package/script/src/api/exchange/_methods/order.d.ts +13 -13
- package/script/src/api/exchange/_methods/order.d.ts.map +1 -1
- package/script/src/api/exchange/_methods/order.js +4 -2
- package/script/src/api/exchange/_methods/order.js.map +1 -1
- package/script/src/api/exchange/client.d.ts +1 -1
- package/script/src/api/exchange/client.d.ts.map +1 -1
- package/script/src/api/exchange/client.js.map +1 -1
- package/script/src/api/exchange/mod.d.ts +1 -1
- package/script/src/api/exchange/mod.d.ts.map +1 -1
- package/script/src/api/exchange/mod.js.map +1 -1
- package/script/src/api/info/_methods/_base/commonSchemas.d.ts +8 -8
- package/script/src/api/info/_methods/frontendOpenOrders.d.ts +3 -3
- package/script/src/api/info/_methods/historicalOrders.d.ts +4 -4
- package/script/src/api/info/_methods/l3Orders.d.ts +11 -11
- package/script/src/api/info/_methods/l4Orders.d.ts +6 -6
- package/script/src/api/info/_methods/openOrders.d.ts +3 -3
- package/script/src/api/info/_methods/orderStatus.d.ts +5 -5
- package/script/src/api/info/_methods/recentTrades.d.ts +2 -2
- package/script/src/api/info/_methods/twapHistory.d.ts +4 -4
- package/script/src/api/info/_methods/userFills.d.ts +3 -3
- package/script/src/api/info/_methods/userFillsByTime.d.ts +4 -4
- package/script/src/api/info/_methods/userFunding.d.ts +3 -3
- package/script/src/api/info/_methods/userFunding.js +2 -2
- package/script/src/api/info/_methods/userFunding.js.map +1 -1
- package/script/src/api/info/_methods/userTwapSliceFills.d.ts +4 -4
- package/script/src/api/info/_methods/userTwapSliceFillsByTime.d.ts +5 -5
- package/script/src/api/info/_methods/webData2.d.ts +9 -9
- package/script/src/api/subscription/_methods/assetCtxs.d.ts +2 -0
- package/script/src/api/subscription/_methods/assetCtxs.d.ts.map +1 -1
- package/script/src/api/subscription/_methods/assetCtxs.js.map +1 -1
- package/script/src/api/subscription/_methods/clearinghouseState.d.ts +2 -0
- package/script/src/api/subscription/_methods/clearinghouseState.d.ts.map +1 -1
- package/script/src/api/subscription/_methods/clearinghouseState.js +2 -0
- package/script/src/api/subscription/_methods/clearinghouseState.js.map +1 -1
- package/script/src/api/subscription/_methods/explorerBlock.d.ts +2 -0
- package/script/src/api/subscription/_methods/explorerBlock.d.ts.map +1 -1
- package/script/src/api/subscription/_methods/explorerBlock.js +2 -0
- package/script/src/api/subscription/_methods/explorerBlock.js.map +1 -1
- package/script/src/api/subscription/_methods/explorerTxs.d.ts +2 -0
- package/script/src/api/subscription/_methods/explorerTxs.d.ts.map +1 -1
- package/script/src/api/subscription/_methods/explorerTxs.js +2 -0
- package/script/src/api/subscription/_methods/explorerTxs.js.map +1 -1
- package/script/src/api/subscription/_methods/openOrders.d.ts +5 -5
- package/script/src/api/subscription/_methods/orderUpdates.d.ts +4 -4
- package/script/src/api/subscription/_methods/trades.d.ts +3 -3
- package/script/src/api/subscription/_methods/twapStates.d.ts +9 -4
- package/script/src/api/subscription/_methods/twapStates.d.ts.map +1 -1
- package/script/src/api/subscription/_methods/twapStates.js +6 -1
- package/script/src/api/subscription/_methods/twapStates.js.map +1 -1
- package/script/src/api/subscription/_methods/userEvents.d.ts +17 -17
- package/script/src/api/subscription/_methods/userFills.d.ts +5 -5
- package/script/src/api/subscription/_methods/userHistoricalOrders.d.ts +6 -6
- package/script/src/api/subscription/_methods/userTwapHistory.d.ts +6 -6
- package/script/src/api/subscription/_methods/userTwapSliceFills.d.ts +6 -6
- package/script/src/api/subscription/_methods/webData2.d.ts +11 -11
- package/script/src/signing/_privateKeySigner.js +3 -3
- package/script/src/signing/_privateKeySigner.js.map +1 -1
- package/script/src/signing/mod.js +2 -2
- package/script/src/signing/mod.js.map +1 -1
- package/src/_dnt.shims.ts +60 -0
- package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.ts +633 -0
- package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.ts +292 -0
- package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.ts +621 -0
- package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.ts +1562 -0
- package/src/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.ts +327 -0
- package/src/deps/jsr.io/@noble/curves/2.0.1/src/utils.ts +306 -0
- package/src/deps/jsr.io/@noble/hashes/2.0.1/src/_md.ts +156 -0
- package/src/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.ts +91 -0
- package/src/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.ts +94 -0
- package/src/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.ts +469 -0
- package/src/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.ts +295 -0
- package/src/deps/jsr.io/@noble/hashes/2.0.1/src/utils.ts +340 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.ts +212 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.ts +100 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.ts +104 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.ts +626 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.ts +274 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.ts +391 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.ts +12 -0
- package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.ts +233 -0
- package/src/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.ts +2196 -0
- package/src/deps/jsr.io/@scure/base/2.0.0/index.ts +857 -0
- package/src/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.ts +146 -0
- package/src/src/api/exchange/_methods/_base/_nonce.ts +3 -2
- package/src/src/api/exchange/_methods/_base/_semaphore.ts +34 -59
- package/src/src/api/exchange/_methods/_base/execute.ts +60 -12
- package/src/src/api/exchange/_methods/order.ts +5 -3
- package/src/src/api/exchange/client.ts +5 -1
- package/src/src/api/exchange/mod.ts +5 -1
- package/src/src/api/info/_methods/userFunding.ts +2 -2
- package/src/src/api/subscription/_methods/assetCtxs.ts +2 -0
- package/src/src/api/subscription/_methods/clearinghouseState.ts +2 -0
- package/src/src/api/subscription/_methods/explorerBlock.ts +2 -0
- package/src/src/api/subscription/_methods/explorerTxs.ts +2 -0
- package/src/src/api/subscription/_methods/twapStates.ts +9 -1
- package/src/src/signing/_privateKeySigner.ts +1 -1
- package/src/src/signing/mod.ts +1 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts +0 -21
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.js +0 -292
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.js.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts +0 -44
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.js +0 -46
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.js.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts +0 -21
- package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.js +0 -295
- package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.js.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts +0 -44
- package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.js +0 -62
- package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.js.map +0 -1
- package/src/deps/jsr.io/@std/msgpack/1.0.3/decode.ts +0 -391
- package/src/deps/jsr.io/@std/msgpack/1.0.3/mod.ts +0 -47
|
@@ -0,0 +1,2001 @@
|
|
|
1
|
+
import { hex as baseHex, utf8 } from '../../../../@scure/base/2.0.0/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Define complex binary structures using composable primitives.
|
|
4
|
+
* Main ideas:
|
|
5
|
+
* - Encode / decode can be chained, same as in `scure-base`
|
|
6
|
+
* - A complex structure can be created from an array and struct of primitive types
|
|
7
|
+
* - Strings / bytes are arrays with specific optimizations: we can just read bytes directly
|
|
8
|
+
* without creating plain array first and reading each byte separately.
|
|
9
|
+
* - Types are inferred from definition
|
|
10
|
+
* @module
|
|
11
|
+
* @example
|
|
12
|
+
* import * as P from 'micro-packed';
|
|
13
|
+
* const s = P.struct({
|
|
14
|
+
* field1: P.U32BE, // 32-bit unsigned big-endian integer
|
|
15
|
+
* field2: P.string(P.U8), // String with U8 length prefix
|
|
16
|
+
* field3: P.bytes(32), // 32 bytes
|
|
17
|
+
* field4: P.array(P.U16BE, P.struct({ // Array of structs with U16BE length
|
|
18
|
+
* subField1: P.U64BE, // 64-bit unsigned big-endian integer
|
|
19
|
+
* subField2: P.string(10) // 10-byte string
|
|
20
|
+
* }))
|
|
21
|
+
* });
|
|
22
|
+
*/
|
|
23
|
+
// TODO: remove dependency on scure-base & inline?
|
|
24
|
+
/*
|
|
25
|
+
Exports can be groupped like this:
|
|
26
|
+
|
|
27
|
+
- Primitive types: P.bytes, P.string, P.hex, P.constant, P.pointer
|
|
28
|
+
- Complex types: P.array, P.struct, P.tuple, P.map, P.tag, P.mappedTag
|
|
29
|
+
- Padding, prefix, magic: P.padLeft, P.padRight, P.prefix, P.magic, P.magicBytes
|
|
30
|
+
- Flags: P.flag, P.flagged, P.optional
|
|
31
|
+
- Wrappers: P.apply, P.wrap, P.lazy
|
|
32
|
+
- Bit fiddling: P.bits, P.bitset
|
|
33
|
+
- utils: P.validate, coders.decimal
|
|
34
|
+
- Debugger
|
|
35
|
+
*/
|
|
36
|
+
/** Shortcut to zero-length (empty) byte array */
|
|
37
|
+
export const EMPTY = /* @__PURE__ */ Uint8Array.of();
|
|
38
|
+
/** Shortcut to one-element (element is 0) byte array */
|
|
39
|
+
export const NULL = /* @__PURE__ */ Uint8Array.of(0);
|
|
40
|
+
/** Checks if two Uint8Arrays are equal. Not constant-time. */
|
|
41
|
+
function equalBytes(a, b) {
|
|
42
|
+
if (a.length !== b.length)
|
|
43
|
+
return false;
|
|
44
|
+
for (let i = 0; i < a.length; i++)
|
|
45
|
+
if (a[i] !== b[i])
|
|
46
|
+
return false;
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
/** Checks if the given value is a Uint8Array. */
|
|
50
|
+
function isBytes(a) {
|
|
51
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Concatenates multiple Uint8Arrays.
|
|
55
|
+
* Engines limit functions to 65K+ arguments.
|
|
56
|
+
* @param arrays Array of Uint8Array elements
|
|
57
|
+
* @returns Concatenated Uint8Array
|
|
58
|
+
*/
|
|
59
|
+
function concatBytes(...arrays) {
|
|
60
|
+
let sum = 0;
|
|
61
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
62
|
+
const a = arrays[i];
|
|
63
|
+
if (!isBytes(a))
|
|
64
|
+
throw new Error('Uint8Array expected');
|
|
65
|
+
sum += a.length;
|
|
66
|
+
}
|
|
67
|
+
const res = new Uint8Array(sum);
|
|
68
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
69
|
+
const a = arrays[i];
|
|
70
|
+
res.set(a, pad);
|
|
71
|
+
pad += a.length;
|
|
72
|
+
}
|
|
73
|
+
return res;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Creates DataView from Uint8Array
|
|
77
|
+
* @param arr - bytes
|
|
78
|
+
* @returns DataView
|
|
79
|
+
*/
|
|
80
|
+
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
81
|
+
/**
|
|
82
|
+
* Checks if the provided value is a plain object, not created from any class or special constructor.
|
|
83
|
+
* Array, Uint8Array and others are not plain objects.
|
|
84
|
+
* @param obj - The value to be checked.
|
|
85
|
+
*/
|
|
86
|
+
function isPlainObject(obj) {
|
|
87
|
+
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
88
|
+
}
|
|
89
|
+
function isNum(num) {
|
|
90
|
+
return Number.isSafeInteger(num);
|
|
91
|
+
}
|
|
92
|
+
export const utils = {
|
|
93
|
+
equalBytes,
|
|
94
|
+
isBytes,
|
|
95
|
+
isCoder,
|
|
96
|
+
checkBounds,
|
|
97
|
+
concatBytes,
|
|
98
|
+
createView,
|
|
99
|
+
isPlainObject,
|
|
100
|
+
};
|
|
101
|
+
// NOTE: we can't have terminator separate function, since it won't know about boundaries
|
|
102
|
+
// E.g. array of U16LE ([1,2,3]) would be [1, 0, 2, 0, 3, 0]
|
|
103
|
+
// But terminator will find array at index '1', which happens to be inside of an element itself
|
|
104
|
+
/**
|
|
105
|
+
* Can be:
|
|
106
|
+
* - Dynamic (CoderType)
|
|
107
|
+
* - Fixed (number)
|
|
108
|
+
* - Terminated (usually zero): Uint8Array with terminator
|
|
109
|
+
* - Field path to field with length (string)
|
|
110
|
+
* - Infinity (null) - decodes until end of buffer
|
|
111
|
+
* Used in:
|
|
112
|
+
* - bytes (string, prefix is implementation of bytes)
|
|
113
|
+
* - array
|
|
114
|
+
*/
|
|
115
|
+
const lengthCoder = (len) => {
|
|
116
|
+
if (len !== null && typeof len !== 'string' && !isCoder(len) && !isBytes(len) && !isNum(len)) {
|
|
117
|
+
throw new Error(`lengthCoder: expected null | number | Uint8Array | CoderType, got ${len} (${typeof len})`);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
encodeStream(w, value) {
|
|
121
|
+
if (len === null)
|
|
122
|
+
return;
|
|
123
|
+
if (isCoder(len))
|
|
124
|
+
return len.encodeStream(w, value);
|
|
125
|
+
let byteLen;
|
|
126
|
+
if (typeof len === 'number')
|
|
127
|
+
byteLen = len;
|
|
128
|
+
else if (typeof len === 'string')
|
|
129
|
+
byteLen = Path.resolve(w.stack, len);
|
|
130
|
+
if (typeof byteLen === 'bigint')
|
|
131
|
+
byteLen = Number(byteLen);
|
|
132
|
+
if (byteLen === undefined || byteLen !== value)
|
|
133
|
+
throw w.err(`Wrong length: ${byteLen} len=${len} exp=${value} (${typeof value})`);
|
|
134
|
+
},
|
|
135
|
+
decodeStream(r) {
|
|
136
|
+
let byteLen;
|
|
137
|
+
if (isCoder(len))
|
|
138
|
+
byteLen = Number(len.decodeStream(r));
|
|
139
|
+
else if (typeof len === 'number')
|
|
140
|
+
byteLen = len;
|
|
141
|
+
else if (typeof len === 'string')
|
|
142
|
+
byteLen = Path.resolve(r.stack, len);
|
|
143
|
+
if (typeof byteLen === 'bigint')
|
|
144
|
+
byteLen = Number(byteLen);
|
|
145
|
+
if (typeof byteLen !== 'number')
|
|
146
|
+
throw r.err(`Wrong length: ${byteLen}`);
|
|
147
|
+
return byteLen;
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Small bitset structure to store position of ranges that have been read.
|
|
153
|
+
* Can be more efficient when internal trees are utilized at the cost of complexity.
|
|
154
|
+
* Needs `O(N/8)` memory for parsing.
|
|
155
|
+
* Purpose: if there are pointers in parsed structure,
|
|
156
|
+
* they can cause read of two distinct ranges:
|
|
157
|
+
* [0-32, 64-128], which means 'pos' is not enough to handle them
|
|
158
|
+
*/
|
|
159
|
+
const Bitset = {
|
|
160
|
+
BITS: 32,
|
|
161
|
+
FULL_MASK: -1 >>> 0, // 1<<32 will overflow
|
|
162
|
+
len: (len) => Math.ceil(len / 32),
|
|
163
|
+
create: (len) => new Uint32Array(Bitset.len(len)),
|
|
164
|
+
clean: (bs) => bs.fill(0),
|
|
165
|
+
debug: (bs) => Array.from(bs).map((i) => (i >>> 0).toString(2).padStart(32, '0')),
|
|
166
|
+
checkLen: (bs, len) => {
|
|
167
|
+
if (Bitset.len(len) === bs.length)
|
|
168
|
+
return;
|
|
169
|
+
throw new Error(`wrong length=${bs.length}. Expected: ${Bitset.len(len)}`);
|
|
170
|
+
},
|
|
171
|
+
chunkLen: (bsLen, pos, len) => {
|
|
172
|
+
if (pos < 0)
|
|
173
|
+
throw new Error(`wrong pos=${pos}`);
|
|
174
|
+
if (pos + len > bsLen)
|
|
175
|
+
throw new Error(`wrong range=${pos}/${len} of ${bsLen}`);
|
|
176
|
+
},
|
|
177
|
+
set: (bs, chunk, value, allowRewrite = true) => {
|
|
178
|
+
if (!allowRewrite && (bs[chunk] & value) !== 0)
|
|
179
|
+
return false;
|
|
180
|
+
bs[chunk] |= value;
|
|
181
|
+
return true;
|
|
182
|
+
},
|
|
183
|
+
pos: (pos, i) => ({
|
|
184
|
+
chunk: Math.floor((pos + i) / 32),
|
|
185
|
+
mask: 1 << (32 - ((pos + i) % 32) - 1),
|
|
186
|
+
}),
|
|
187
|
+
indices: (bs, len, invert = false) => {
|
|
188
|
+
Bitset.checkLen(bs, len);
|
|
189
|
+
const { FULL_MASK, BITS } = Bitset;
|
|
190
|
+
const left = BITS - (len % BITS);
|
|
191
|
+
const lastMask = left ? (FULL_MASK >>> left) << left : FULL_MASK;
|
|
192
|
+
const res = [];
|
|
193
|
+
for (let i = 0; i < bs.length; i++) {
|
|
194
|
+
let c = bs[i];
|
|
195
|
+
if (invert)
|
|
196
|
+
c = ~c; // allows to gen unset elements
|
|
197
|
+
// apply mask to last element, so we won't iterate non-existent items
|
|
198
|
+
if (i === bs.length - 1)
|
|
199
|
+
c &= lastMask;
|
|
200
|
+
if (c === 0)
|
|
201
|
+
continue; // fast-path
|
|
202
|
+
for (let j = 0; j < BITS; j++) {
|
|
203
|
+
const m = 1 << (BITS - j - 1);
|
|
204
|
+
if (c & m)
|
|
205
|
+
res.push(i * BITS + j);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return res;
|
|
209
|
+
},
|
|
210
|
+
range: (arr) => {
|
|
211
|
+
const res = [];
|
|
212
|
+
let cur;
|
|
213
|
+
for (const i of arr) {
|
|
214
|
+
if (cur === undefined || i !== cur.pos + cur.length)
|
|
215
|
+
res.push((cur = { pos: i, length: 1 }));
|
|
216
|
+
else
|
|
217
|
+
cur.length += 1;
|
|
218
|
+
}
|
|
219
|
+
return res;
|
|
220
|
+
},
|
|
221
|
+
rangeDebug: (bs, len, invert = false) => `[${Bitset.range(Bitset.indices(bs, len, invert))
|
|
222
|
+
.map((i) => `(${i.pos}/${i.length})`)
|
|
223
|
+
.join(', ')}]`,
|
|
224
|
+
setRange: (bs, bsLen, pos, len, allowRewrite = true) => {
|
|
225
|
+
Bitset.chunkLen(bsLen, pos, len);
|
|
226
|
+
const { FULL_MASK, BITS } = Bitset;
|
|
227
|
+
// Try to set range with maximum efficiency:
|
|
228
|
+
// - first chunk is always '0000[1111]' (only right ones)
|
|
229
|
+
// - middle chunks are set to '[1111 1111]' (all ones)
|
|
230
|
+
// - last chunk is always '[1111]0000' (only left ones)
|
|
231
|
+
// - max operations: (N/32) + 2 (first and last)
|
|
232
|
+
const first = pos % BITS ? Math.floor(pos / BITS) : undefined;
|
|
233
|
+
const lastPos = pos + len;
|
|
234
|
+
const last = lastPos % BITS ? Math.floor(lastPos / BITS) : undefined;
|
|
235
|
+
// special case, whole range inside single chunk
|
|
236
|
+
if (first !== undefined && first === last)
|
|
237
|
+
return Bitset.set(bs, first, (FULL_MASK >>> (BITS - len)) << (BITS - len - pos), allowRewrite);
|
|
238
|
+
if (first !== undefined) {
|
|
239
|
+
if (!Bitset.set(bs, first, FULL_MASK >>> pos % BITS, allowRewrite))
|
|
240
|
+
return false; // first chunk
|
|
241
|
+
}
|
|
242
|
+
// middle chunks
|
|
243
|
+
const start = first !== undefined ? first + 1 : pos / BITS;
|
|
244
|
+
const end = last !== undefined ? last : lastPos / BITS;
|
|
245
|
+
for (let i = start; i < end; i++)
|
|
246
|
+
if (!Bitset.set(bs, i, FULL_MASK, allowRewrite))
|
|
247
|
+
return false;
|
|
248
|
+
if (last !== undefined && first !== last)
|
|
249
|
+
if (!Bitset.set(bs, last, FULL_MASK << (BITS - (lastPos % BITS)), allowRewrite))
|
|
250
|
+
return false; // last chunk
|
|
251
|
+
return true;
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
const Path = {
|
|
255
|
+
/**
|
|
256
|
+
* Internal method for handling stack of paths (debug, errors, dynamic fields via path)
|
|
257
|
+
* This is looks ugly (callback), but allows us to force stack cleaning by construction (.pop always after function).
|
|
258
|
+
* Also, this makes impossible:
|
|
259
|
+
* - pushing field when stack is empty
|
|
260
|
+
* - pushing field inside of field (real bug)
|
|
261
|
+
* NOTE: we don't want to do '.pop' on error!
|
|
262
|
+
*/
|
|
263
|
+
pushObj: (stack, obj, objFn) => {
|
|
264
|
+
const last = { obj };
|
|
265
|
+
stack.push(last);
|
|
266
|
+
objFn((field, fieldFn) => {
|
|
267
|
+
last.field = field;
|
|
268
|
+
fieldFn();
|
|
269
|
+
last.field = undefined;
|
|
270
|
+
});
|
|
271
|
+
stack.pop();
|
|
272
|
+
},
|
|
273
|
+
path: (stack) => {
|
|
274
|
+
const res = [];
|
|
275
|
+
for (const i of stack)
|
|
276
|
+
if (i.field !== undefined)
|
|
277
|
+
res.push(i.field);
|
|
278
|
+
return res.join('/');
|
|
279
|
+
},
|
|
280
|
+
err: (name, stack, msg) => {
|
|
281
|
+
const err = new Error(`${name}(${Path.path(stack)}): ${typeof msg === 'string' ? msg : msg.message}`);
|
|
282
|
+
if (msg instanceof Error && msg.stack)
|
|
283
|
+
err.stack = msg.stack;
|
|
284
|
+
return err;
|
|
285
|
+
},
|
|
286
|
+
resolve: (stack, path) => {
|
|
287
|
+
const parts = path.split('/');
|
|
288
|
+
const objPath = stack.map((i) => i.obj);
|
|
289
|
+
let i = 0;
|
|
290
|
+
for (; i < parts.length; i++) {
|
|
291
|
+
if (parts[i] === '..')
|
|
292
|
+
objPath.pop();
|
|
293
|
+
else
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
let cur = objPath.pop();
|
|
297
|
+
for (; i < parts.length; i++) {
|
|
298
|
+
if (!cur || cur[parts[i]] === undefined)
|
|
299
|
+
return undefined;
|
|
300
|
+
cur = cur[parts[i]];
|
|
301
|
+
}
|
|
302
|
+
return cur;
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* Internal structure. Reader class for reading from a byte array.
|
|
307
|
+
* `stack` is internal: for debugger and logging
|
|
308
|
+
* @class Reader
|
|
309
|
+
*/
|
|
310
|
+
class _Reader {
|
|
311
|
+
pos = 0;
|
|
312
|
+
data;
|
|
313
|
+
opts;
|
|
314
|
+
stack;
|
|
315
|
+
parent;
|
|
316
|
+
parentOffset;
|
|
317
|
+
bitBuf = 0;
|
|
318
|
+
bitPos = 0;
|
|
319
|
+
bs; // bitset
|
|
320
|
+
view;
|
|
321
|
+
constructor(data, opts = {}, stack = [], parent = undefined, parentOffset = 0) {
|
|
322
|
+
this.data = data;
|
|
323
|
+
this.opts = opts;
|
|
324
|
+
this.stack = stack;
|
|
325
|
+
this.parent = parent;
|
|
326
|
+
this.parentOffset = parentOffset;
|
|
327
|
+
this.view = createView(data);
|
|
328
|
+
}
|
|
329
|
+
/** Internal method for pointers. */
|
|
330
|
+
_enablePointers() {
|
|
331
|
+
if (this.parent)
|
|
332
|
+
return this.parent._enablePointers();
|
|
333
|
+
if (this.bs)
|
|
334
|
+
return;
|
|
335
|
+
this.bs = Bitset.create(this.data.length);
|
|
336
|
+
Bitset.setRange(this.bs, this.data.length, 0, this.pos, this.opts.allowMultipleReads);
|
|
337
|
+
}
|
|
338
|
+
markBytesBS(pos, len) {
|
|
339
|
+
if (this.parent)
|
|
340
|
+
return this.parent.markBytesBS(this.parentOffset + pos, len);
|
|
341
|
+
if (!len)
|
|
342
|
+
return true;
|
|
343
|
+
if (!this.bs)
|
|
344
|
+
return true;
|
|
345
|
+
return Bitset.setRange(this.bs, this.data.length, pos, len, false);
|
|
346
|
+
}
|
|
347
|
+
markBytes(len) {
|
|
348
|
+
const pos = this.pos;
|
|
349
|
+
this.pos += len;
|
|
350
|
+
const res = this.markBytesBS(pos, len);
|
|
351
|
+
if (!this.opts.allowMultipleReads && !res)
|
|
352
|
+
throw this.err(`multiple read pos=${this.pos} len=${len}`);
|
|
353
|
+
return res;
|
|
354
|
+
}
|
|
355
|
+
pushObj(obj, objFn) {
|
|
356
|
+
return Path.pushObj(this.stack, obj, objFn);
|
|
357
|
+
}
|
|
358
|
+
readView(n, fn) {
|
|
359
|
+
if (!Number.isFinite(n))
|
|
360
|
+
throw this.err(`readView: wrong length=${n}`);
|
|
361
|
+
if (this.pos + n > this.data.length)
|
|
362
|
+
throw this.err('readView: Unexpected end of buffer');
|
|
363
|
+
const res = fn(this.view, this.pos);
|
|
364
|
+
this.markBytes(n);
|
|
365
|
+
return res;
|
|
366
|
+
}
|
|
367
|
+
// read bytes by absolute offset
|
|
368
|
+
absBytes(n) {
|
|
369
|
+
if (n > this.data.length)
|
|
370
|
+
throw new Error('Unexpected end of buffer');
|
|
371
|
+
return this.data.subarray(n);
|
|
372
|
+
}
|
|
373
|
+
finish() {
|
|
374
|
+
if (this.opts.allowUnreadBytes)
|
|
375
|
+
return;
|
|
376
|
+
if (this.bitPos) {
|
|
377
|
+
throw this.err(`${this.bitPos} bits left after unpack: ${baseHex.encode(this.data.slice(this.pos))}`);
|
|
378
|
+
}
|
|
379
|
+
if (this.bs && !this.parent) {
|
|
380
|
+
const notRead = Bitset.indices(this.bs, this.data.length, true);
|
|
381
|
+
if (notRead.length) {
|
|
382
|
+
const formatted = Bitset.range(notRead)
|
|
383
|
+
.map(({ pos, length }) => `(${pos}/${length})[${baseHex.encode(this.data.subarray(pos, pos + length))}]`)
|
|
384
|
+
.join(', ');
|
|
385
|
+
throw this.err(`unread byte ranges: ${formatted} (total=${this.data.length})`);
|
|
386
|
+
}
|
|
387
|
+
else
|
|
388
|
+
return; // all bytes read, everything is ok
|
|
389
|
+
}
|
|
390
|
+
// Default: no pointers enabled
|
|
391
|
+
if (!this.isEnd()) {
|
|
392
|
+
throw this.err(`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${baseHex.encode(this.data.slice(this.pos))}`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
// User methods
|
|
396
|
+
err(msg) {
|
|
397
|
+
return Path.err('Reader', this.stack, msg);
|
|
398
|
+
}
|
|
399
|
+
offsetReader(n) {
|
|
400
|
+
if (n > this.data.length)
|
|
401
|
+
throw this.err('offsetReader: Unexpected end of buffer');
|
|
402
|
+
return new _Reader(this.absBytes(n), this.opts, this.stack, this, n);
|
|
403
|
+
}
|
|
404
|
+
bytes(n, peek = false) {
|
|
405
|
+
if (this.bitPos)
|
|
406
|
+
throw this.err('readBytes: bitPos not empty');
|
|
407
|
+
if (!Number.isFinite(n))
|
|
408
|
+
throw this.err(`readBytes: wrong length=${n}`);
|
|
409
|
+
if (this.pos + n > this.data.length)
|
|
410
|
+
throw this.err('readBytes: Unexpected end of buffer');
|
|
411
|
+
const slice = this.data.subarray(this.pos, this.pos + n);
|
|
412
|
+
if (!peek)
|
|
413
|
+
this.markBytes(n);
|
|
414
|
+
return slice;
|
|
415
|
+
}
|
|
416
|
+
byte(peek = false) {
|
|
417
|
+
if (this.bitPos)
|
|
418
|
+
throw this.err('readByte: bitPos not empty');
|
|
419
|
+
if (this.pos + 1 > this.data.length)
|
|
420
|
+
throw this.err('readBytes: Unexpected end of buffer');
|
|
421
|
+
const data = this.data[this.pos];
|
|
422
|
+
if (!peek)
|
|
423
|
+
this.markBytes(1);
|
|
424
|
+
return data;
|
|
425
|
+
}
|
|
426
|
+
get leftBytes() {
|
|
427
|
+
return this.data.length - this.pos;
|
|
428
|
+
}
|
|
429
|
+
get totalBytes() {
|
|
430
|
+
return this.data.length;
|
|
431
|
+
}
|
|
432
|
+
isEnd() {
|
|
433
|
+
return this.pos >= this.data.length && !this.bitPos;
|
|
434
|
+
}
|
|
435
|
+
// bits are read in BE mode (left to right): (0b1000_0000).readBits(1) == 1
|
|
436
|
+
bits(bits) {
|
|
437
|
+
if (bits > 32)
|
|
438
|
+
throw this.err('BitReader: cannot read more than 32 bits in single call');
|
|
439
|
+
let out = 0;
|
|
440
|
+
while (bits) {
|
|
441
|
+
if (!this.bitPos) {
|
|
442
|
+
this.bitBuf = this.byte();
|
|
443
|
+
this.bitPos = 8;
|
|
444
|
+
}
|
|
445
|
+
const take = Math.min(bits, this.bitPos);
|
|
446
|
+
this.bitPos -= take;
|
|
447
|
+
out = (out << take) | ((this.bitBuf >> this.bitPos) & (2 ** take - 1));
|
|
448
|
+
this.bitBuf &= 2 ** this.bitPos - 1;
|
|
449
|
+
bits -= take;
|
|
450
|
+
}
|
|
451
|
+
// Fix signed integers
|
|
452
|
+
return out >>> 0;
|
|
453
|
+
}
|
|
454
|
+
find(needle, pos = this.pos) {
|
|
455
|
+
if (!isBytes(needle))
|
|
456
|
+
throw this.err(`find: needle is not bytes! ${needle}`);
|
|
457
|
+
if (this.bitPos)
|
|
458
|
+
throw this.err('findByte: bitPos not empty');
|
|
459
|
+
if (!needle.length)
|
|
460
|
+
throw this.err(`find: needle is empty`);
|
|
461
|
+
// indexOf should be faster than full equalBytes check
|
|
462
|
+
for (let idx = pos; (idx = this.data.indexOf(needle[0], idx)) !== -1; idx++) {
|
|
463
|
+
if (idx === -1)
|
|
464
|
+
return;
|
|
465
|
+
const leftBytes = this.data.length - idx;
|
|
466
|
+
if (leftBytes < needle.length)
|
|
467
|
+
return;
|
|
468
|
+
if (equalBytes(needle, this.data.subarray(idx, idx + needle.length)))
|
|
469
|
+
return idx;
|
|
470
|
+
}
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Internal structure. Writer class for writing to a byte array.
|
|
476
|
+
* The `stack` argument of constructor is internal, for debugging and logs.
|
|
477
|
+
* @class Writer
|
|
478
|
+
*/
|
|
479
|
+
class _Writer {
|
|
480
|
+
pos = 0;
|
|
481
|
+
stack;
|
|
482
|
+
// We could have a single buffer here and re-alloc it with
|
|
483
|
+
// x1.5-2 size each time it full, but it will be slower:
|
|
484
|
+
// basic/encode bench: 395ns -> 560ns
|
|
485
|
+
buffers = [];
|
|
486
|
+
ptrs = [];
|
|
487
|
+
bitBuf = 0;
|
|
488
|
+
bitPos = 0;
|
|
489
|
+
viewBuf = new Uint8Array(8);
|
|
490
|
+
view;
|
|
491
|
+
finished = false;
|
|
492
|
+
constructor(stack = []) {
|
|
493
|
+
this.stack = stack;
|
|
494
|
+
this.view = createView(this.viewBuf);
|
|
495
|
+
}
|
|
496
|
+
pushObj(obj, objFn) {
|
|
497
|
+
return Path.pushObj(this.stack, obj, objFn);
|
|
498
|
+
}
|
|
499
|
+
writeView(len, fn) {
|
|
500
|
+
if (this.finished)
|
|
501
|
+
throw this.err('buffer: finished');
|
|
502
|
+
if (!isNum(len) || len > 8)
|
|
503
|
+
throw new Error(`wrong writeView length=${len}`);
|
|
504
|
+
fn(this.view);
|
|
505
|
+
this.bytes(this.viewBuf.slice(0, len));
|
|
506
|
+
this.viewBuf.fill(0);
|
|
507
|
+
}
|
|
508
|
+
// User methods
|
|
509
|
+
err(msg) {
|
|
510
|
+
if (this.finished)
|
|
511
|
+
throw this.err('buffer: finished');
|
|
512
|
+
return Path.err('Reader', this.stack, msg);
|
|
513
|
+
}
|
|
514
|
+
bytes(b) {
|
|
515
|
+
if (this.finished)
|
|
516
|
+
throw this.err('buffer: finished');
|
|
517
|
+
if (this.bitPos)
|
|
518
|
+
throw this.err('writeBytes: ends with non-empty bit buffer');
|
|
519
|
+
this.buffers.push(b);
|
|
520
|
+
this.pos += b.length;
|
|
521
|
+
}
|
|
522
|
+
byte(b) {
|
|
523
|
+
if (this.finished)
|
|
524
|
+
throw this.err('buffer: finished');
|
|
525
|
+
if (this.bitPos)
|
|
526
|
+
throw this.err('writeByte: ends with non-empty bit buffer');
|
|
527
|
+
this.buffers.push(new Uint8Array([b]));
|
|
528
|
+
this.pos++;
|
|
529
|
+
}
|
|
530
|
+
finish(clean = true) {
|
|
531
|
+
if (this.finished)
|
|
532
|
+
throw this.err('buffer: finished');
|
|
533
|
+
if (this.bitPos)
|
|
534
|
+
throw this.err('buffer: ends with non-empty bit buffer');
|
|
535
|
+
// Can't use concatBytes, because it limits amount of arguments (65K).
|
|
536
|
+
const buffers = this.buffers.concat(this.ptrs.map((i) => i.buffer));
|
|
537
|
+
const sum = buffers.map((b) => b.length).reduce((a, b) => a + b, 0);
|
|
538
|
+
const buf = new Uint8Array(sum);
|
|
539
|
+
for (let i = 0, pad = 0; i < buffers.length; i++) {
|
|
540
|
+
const a = buffers[i];
|
|
541
|
+
buf.set(a, pad);
|
|
542
|
+
pad += a.length;
|
|
543
|
+
}
|
|
544
|
+
for (let pos = this.pos, i = 0; i < this.ptrs.length; i++) {
|
|
545
|
+
const ptr = this.ptrs[i];
|
|
546
|
+
buf.set(ptr.ptr.encode(pos), ptr.pos);
|
|
547
|
+
pos += ptr.buffer.length;
|
|
548
|
+
}
|
|
549
|
+
// Cleanup
|
|
550
|
+
if (clean) {
|
|
551
|
+
// We cannot cleanup buffers here, since it can be static user provided buffer.
|
|
552
|
+
// Only '.byte' and '.bits' create buffer which we can safely clean.
|
|
553
|
+
// for (const b of this.buffers) b.fill(0);
|
|
554
|
+
this.buffers = [];
|
|
555
|
+
for (const p of this.ptrs)
|
|
556
|
+
p.buffer.fill(0);
|
|
557
|
+
this.ptrs = [];
|
|
558
|
+
this.finished = true;
|
|
559
|
+
this.bitBuf = 0;
|
|
560
|
+
}
|
|
561
|
+
return buf;
|
|
562
|
+
}
|
|
563
|
+
bits(value, bits) {
|
|
564
|
+
if (bits > 32)
|
|
565
|
+
throw this.err('writeBits: cannot write more than 32 bits in single call');
|
|
566
|
+
if (value >= 2 ** bits)
|
|
567
|
+
throw this.err(`writeBits: value (${value}) >= 2**bits (${bits})`);
|
|
568
|
+
while (bits) {
|
|
569
|
+
const take = Math.min(bits, 8 - this.bitPos);
|
|
570
|
+
this.bitBuf = (this.bitBuf << take) | (value >> (bits - take));
|
|
571
|
+
this.bitPos += take;
|
|
572
|
+
bits -= take;
|
|
573
|
+
value &= 2 ** bits - 1;
|
|
574
|
+
if (this.bitPos === 8) {
|
|
575
|
+
this.bitPos = 0;
|
|
576
|
+
this.buffers.push(new Uint8Array([this.bitBuf]));
|
|
577
|
+
this.pos++;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
// Immutable LE<->BE
|
|
583
|
+
const swapEndianness = (b) => Uint8Array.from(b).reverse();
|
|
584
|
+
/** Internal function for checking bit bounds of bigint in signed/unsinged form */
|
|
585
|
+
function checkBounds(value, bits, signed) {
|
|
586
|
+
if (signed) {
|
|
587
|
+
// [-(2**(32-1)), 2**(32-1)-1]
|
|
588
|
+
const signBit = 2n ** (bits - 1n);
|
|
589
|
+
if (value < -signBit || value >= signBit)
|
|
590
|
+
throw new Error(`value out of signed bounds. Expected ${-signBit} <= ${value} < ${signBit}`);
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
// [0, 2**32-1]
|
|
594
|
+
if (0n > value || value >= 2n ** bits)
|
|
595
|
+
throw new Error(`value out of unsigned bounds. Expected 0 <= ${value} < ${2n ** bits}`);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function _wrap(inner) {
|
|
599
|
+
return {
|
|
600
|
+
// NOTE: we cannot export validate here, since it is likely mistake.
|
|
601
|
+
encodeStream: inner.encodeStream,
|
|
602
|
+
decodeStream: inner.decodeStream,
|
|
603
|
+
size: inner.size,
|
|
604
|
+
encode: (value) => {
|
|
605
|
+
const w = new _Writer();
|
|
606
|
+
inner.encodeStream(w, value);
|
|
607
|
+
return w.finish();
|
|
608
|
+
},
|
|
609
|
+
decode: (data, opts = {}) => {
|
|
610
|
+
const r = new _Reader(data, opts);
|
|
611
|
+
const res = inner.decodeStream(r);
|
|
612
|
+
r.finish();
|
|
613
|
+
return res;
|
|
614
|
+
},
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Validates a value before encoding and after decoding using a provided function.
|
|
619
|
+
* @param inner - The inner CoderType.
|
|
620
|
+
* @param fn - The validation function.
|
|
621
|
+
* @returns CoderType which check value with validation function.
|
|
622
|
+
* @example
|
|
623
|
+
* const val = (n: number) => {
|
|
624
|
+
* if (n > 10) throw new Error(`${n} > 10`);
|
|
625
|
+
* return n;
|
|
626
|
+
* };
|
|
627
|
+
*
|
|
628
|
+
* const RangedInt = P.validate(P.U32LE, val); // Will check if value is <= 10 during encoding and decoding
|
|
629
|
+
*/
|
|
630
|
+
export function validate(inner, fn) {
|
|
631
|
+
if (!isCoder(inner))
|
|
632
|
+
throw new Error(`validate: invalid inner value ${inner}`);
|
|
633
|
+
if (typeof fn !== 'function')
|
|
634
|
+
throw new Error('validate: fn should be function');
|
|
635
|
+
return _wrap({
|
|
636
|
+
size: inner.size,
|
|
637
|
+
encodeStream: (w, value) => {
|
|
638
|
+
let res;
|
|
639
|
+
try {
|
|
640
|
+
res = fn(value);
|
|
641
|
+
}
|
|
642
|
+
catch (e) {
|
|
643
|
+
throw w.err(e);
|
|
644
|
+
}
|
|
645
|
+
inner.encodeStream(w, res);
|
|
646
|
+
},
|
|
647
|
+
decodeStream: (r) => {
|
|
648
|
+
const res = inner.decodeStream(r);
|
|
649
|
+
try {
|
|
650
|
+
return fn(res);
|
|
651
|
+
}
|
|
652
|
+
catch (e) {
|
|
653
|
+
throw r.err(e);
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Wraps a stream encoder into a generic encoder and optionally validation function
|
|
660
|
+
* @param {inner} inner BytesCoderStream & { validate?: Validate<T> }.
|
|
661
|
+
* @returns The wrapped CoderType.
|
|
662
|
+
* @example
|
|
663
|
+
* const U8 = P.wrap({
|
|
664
|
+
* encodeStream: (w: Writer, value: number) => w.byte(value),
|
|
665
|
+
* decodeStream: (r: Reader): number => r.byte()
|
|
666
|
+
* });
|
|
667
|
+
* const checkedU8 = P.wrap({
|
|
668
|
+
* encodeStream: (w: Writer, value: number) => w.byte(value),
|
|
669
|
+
* decodeStream: (r: Reader): number => r.byte()
|
|
670
|
+
* validate: (n: number) => {
|
|
671
|
+
* if (n > 10) throw new Error(`${n} > 10`);
|
|
672
|
+
* return n;
|
|
673
|
+
* }
|
|
674
|
+
* });
|
|
675
|
+
*/
|
|
676
|
+
export const wrap = (inner) => {
|
|
677
|
+
const res = _wrap(inner);
|
|
678
|
+
return inner.validate ? validate(res, inner.validate) : res;
|
|
679
|
+
};
|
|
680
|
+
const isBaseCoder = (elm) => isPlainObject(elm) && typeof elm.decode === 'function' && typeof elm.encode === 'function';
|
|
681
|
+
/**
|
|
682
|
+
* Checks if the given value is a CoderType.
|
|
683
|
+
* @param elm - The value to check.
|
|
684
|
+
* @returns True if the value is a CoderType, false otherwise.
|
|
685
|
+
*/
|
|
686
|
+
export function isCoder(elm) {
|
|
687
|
+
return (isPlainObject(elm) &&
|
|
688
|
+
isBaseCoder(elm) &&
|
|
689
|
+
typeof elm.encodeStream === 'function' &&
|
|
690
|
+
typeof elm.decodeStream === 'function' &&
|
|
691
|
+
(elm.size === undefined || isNum(elm.size)));
|
|
692
|
+
}
|
|
693
|
+
// Coders (like in @scure/base) for common operations
|
|
694
|
+
/**
|
|
695
|
+
* Base coder for working with dictionaries (records, objects, key-value map)
|
|
696
|
+
* Dictionary is dynamic type like: `[key: string, value: any][]`
|
|
697
|
+
* @returns base coder that encodes/decodes between arrays of key-value tuples and dictionaries.
|
|
698
|
+
* @example
|
|
699
|
+
* const dict: P.CoderType<Record<string, number>> = P.apply(
|
|
700
|
+
* P.array(P.U16BE, P.tuple([P.cstring, P.U32LE] as const)),
|
|
701
|
+
* P.coders.dict()
|
|
702
|
+
* );
|
|
703
|
+
*/
|
|
704
|
+
function dict() {
|
|
705
|
+
return {
|
|
706
|
+
encode: (from) => {
|
|
707
|
+
if (!Array.isArray(from))
|
|
708
|
+
throw new Error('array expected');
|
|
709
|
+
const to = {};
|
|
710
|
+
for (const item of from) {
|
|
711
|
+
if (!Array.isArray(item) || item.length !== 2)
|
|
712
|
+
throw new Error(`array of two elements expected`);
|
|
713
|
+
const name = item[0];
|
|
714
|
+
const value = item[1];
|
|
715
|
+
if (to[name] !== undefined)
|
|
716
|
+
throw new Error(`key(${name}) appears twice in struct`);
|
|
717
|
+
to[name] = value;
|
|
718
|
+
}
|
|
719
|
+
return to;
|
|
720
|
+
},
|
|
721
|
+
decode: (to) => {
|
|
722
|
+
if (!isPlainObject(to))
|
|
723
|
+
throw new Error(`expected plain object, got ${to}`);
|
|
724
|
+
return Object.entries(to);
|
|
725
|
+
},
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Safely converts bigint to number.
|
|
730
|
+
* Sometimes pointers / tags use u64 or other big numbers which cannot be represented by number,
|
|
731
|
+
* but we still can use them since real value will be smaller than u32
|
|
732
|
+
*/
|
|
733
|
+
const numberBigint = {
|
|
734
|
+
encode: (from) => {
|
|
735
|
+
if (typeof from !== 'bigint')
|
|
736
|
+
throw new Error(`expected bigint, got ${typeof from}`);
|
|
737
|
+
if (from > BigInt(Number.MAX_SAFE_INTEGER))
|
|
738
|
+
throw new Error(`element bigger than MAX_SAFE_INTEGER=${from}`);
|
|
739
|
+
return Number(from);
|
|
740
|
+
},
|
|
741
|
+
decode: (to) => {
|
|
742
|
+
if (!isNum(to))
|
|
743
|
+
throw new Error('element is not a safe integer');
|
|
744
|
+
return BigInt(to);
|
|
745
|
+
},
|
|
746
|
+
};
|
|
747
|
+
/**
|
|
748
|
+
* Base coder for working with TypeScript enums.
|
|
749
|
+
* @param e - TypeScript enum.
|
|
750
|
+
* @returns base coder that encodes/decodes between numbers and enum keys.
|
|
751
|
+
* @example
|
|
752
|
+
* enum Color { Red, Green, Blue }
|
|
753
|
+
* const colorCoder = P.coders.tsEnum(Color);
|
|
754
|
+
* colorCoder.encode(Color.Red); // 'Red'
|
|
755
|
+
* colorCoder.decode('Green'); // 1
|
|
756
|
+
*/
|
|
757
|
+
function tsEnum(e) {
|
|
758
|
+
if (!isPlainObject(e))
|
|
759
|
+
throw new Error('plain object expected');
|
|
760
|
+
return {
|
|
761
|
+
encode: (from) => {
|
|
762
|
+
if (!isNum(from) || !(from in e))
|
|
763
|
+
throw new Error(`wrong value ${from}`);
|
|
764
|
+
return e[from];
|
|
765
|
+
},
|
|
766
|
+
decode: (to) => {
|
|
767
|
+
if (typeof to !== 'string')
|
|
768
|
+
throw new Error(`wrong value ${typeof to}`);
|
|
769
|
+
return e[to];
|
|
770
|
+
},
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Base coder for working with decimal numbers.
|
|
775
|
+
* @param precision - Number of decimal places.
|
|
776
|
+
* @param round - Round fraction part if bigger than precision (throws error by default)
|
|
777
|
+
* @returns base coder that encodes/decodes between bigints and decimal strings.
|
|
778
|
+
* @example
|
|
779
|
+
* const decimal8 = P.coders.decimal(8);
|
|
780
|
+
* decimal8.encode(630880845n); // '6.30880845'
|
|
781
|
+
* decimal8.decode('6.30880845'); // 630880845n
|
|
782
|
+
*/
|
|
783
|
+
function decimal(precision, round = false) {
|
|
784
|
+
if (!isNum(precision))
|
|
785
|
+
throw new Error(`decimal/precision: wrong value ${precision}`);
|
|
786
|
+
if (typeof round !== 'boolean')
|
|
787
|
+
throw new Error(`decimal/round: expected boolean, got ${typeof round}`);
|
|
788
|
+
const decimalMask = 10n ** BigInt(precision);
|
|
789
|
+
return {
|
|
790
|
+
encode: (from) => {
|
|
791
|
+
if (typeof from !== 'bigint')
|
|
792
|
+
throw new Error(`expected bigint, got ${typeof from}`);
|
|
793
|
+
let s = (from < 0n ? -from : from).toString(10);
|
|
794
|
+
let sep = s.length - precision;
|
|
795
|
+
if (sep < 0) {
|
|
796
|
+
s = s.padStart(s.length - sep, '0');
|
|
797
|
+
sep = 0;
|
|
798
|
+
}
|
|
799
|
+
let i = s.length - 1;
|
|
800
|
+
for (; i >= sep && s[i] === '0'; i--)
|
|
801
|
+
;
|
|
802
|
+
let int = s.slice(0, sep);
|
|
803
|
+
let frac = s.slice(sep, i + 1);
|
|
804
|
+
if (!int)
|
|
805
|
+
int = '0';
|
|
806
|
+
if (from < 0n)
|
|
807
|
+
int = '-' + int;
|
|
808
|
+
if (!frac)
|
|
809
|
+
return int;
|
|
810
|
+
return `${int}.${frac}`;
|
|
811
|
+
},
|
|
812
|
+
decode: (to) => {
|
|
813
|
+
if (typeof to !== 'string')
|
|
814
|
+
throw new Error(`expected string, got ${typeof to}`);
|
|
815
|
+
if (to === '-0')
|
|
816
|
+
throw new Error(`negative zero is not allowed`);
|
|
817
|
+
let neg = false;
|
|
818
|
+
if (to.startsWith('-')) {
|
|
819
|
+
neg = true;
|
|
820
|
+
to = to.slice(1);
|
|
821
|
+
}
|
|
822
|
+
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(to))
|
|
823
|
+
throw new Error(`wrong string value=${to}`);
|
|
824
|
+
let sep = to.indexOf('.');
|
|
825
|
+
sep = sep === -1 ? to.length : sep;
|
|
826
|
+
// split by separator and strip trailing zeros from fraction. always returns [string, string] (.split doesn't).
|
|
827
|
+
const intS = to.slice(0, sep);
|
|
828
|
+
const fracS = to.slice(sep + 1).replace(/0+$/, '');
|
|
829
|
+
const int = BigInt(intS) * decimalMask;
|
|
830
|
+
if (!round && fracS.length > precision) {
|
|
831
|
+
throw new Error(`fractional part cannot be represented with this precision (num=${to}, prec=${precision})`);
|
|
832
|
+
}
|
|
833
|
+
const fracLen = Math.min(fracS.length, precision);
|
|
834
|
+
const frac = BigInt(fracS.slice(0, fracLen)) * 10n ** BigInt(precision - fracLen);
|
|
835
|
+
const value = int + frac;
|
|
836
|
+
return neg ? -value : value;
|
|
837
|
+
},
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Combines multiple coders into a single coder, allowing conditional encoding/decoding based on input.
|
|
842
|
+
* Acts as a parser combinator, splitting complex conditional coders into smaller parts.
|
|
843
|
+
*
|
|
844
|
+
* `encode = [Ae, Be]; decode = [Ad, Bd]`
|
|
845
|
+
* ->
|
|
846
|
+
* `match([{encode: Ae, decode: Ad}, {encode: Be; decode: Bd}])`
|
|
847
|
+
*
|
|
848
|
+
* @param lst - Array of coders to match.
|
|
849
|
+
* @returns Combined coder for conditional encoding/decoding.
|
|
850
|
+
*/
|
|
851
|
+
function match(lst) {
|
|
852
|
+
if (!Array.isArray(lst))
|
|
853
|
+
throw new Error(`expected array, got ${typeof lst}`);
|
|
854
|
+
for (const i of lst)
|
|
855
|
+
if (!isBaseCoder(i))
|
|
856
|
+
throw new Error(`wrong base coder ${i}`);
|
|
857
|
+
return {
|
|
858
|
+
encode: (from) => {
|
|
859
|
+
for (const c of lst) {
|
|
860
|
+
const elm = c.encode(from);
|
|
861
|
+
if (elm !== undefined)
|
|
862
|
+
return elm;
|
|
863
|
+
}
|
|
864
|
+
throw new Error(`match/encode: cannot find match in ${from}`);
|
|
865
|
+
},
|
|
866
|
+
decode: (to) => {
|
|
867
|
+
for (const c of lst) {
|
|
868
|
+
const elm = c.decode(to);
|
|
869
|
+
if (elm !== undefined)
|
|
870
|
+
return elm;
|
|
871
|
+
}
|
|
872
|
+
throw new Error(`match/decode: cannot find match in ${to}`);
|
|
873
|
+
},
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
/** Reverses direction of coder */
|
|
877
|
+
const reverse = (coder) => {
|
|
878
|
+
if (!isBaseCoder(coder))
|
|
879
|
+
throw new Error('BaseCoder expected');
|
|
880
|
+
return { encode: coder.decode, decode: coder.encode };
|
|
881
|
+
};
|
|
882
|
+
export const coders = { dict, numberBigint, tsEnum, decimal, match, reverse };
|
|
883
|
+
/**
|
|
884
|
+
* CoderType for parsing individual bits.
|
|
885
|
+
* NOTE: Structure should parse whole amount of bytes before it can start parsing byte-level elements.
|
|
886
|
+
* @param len - Number of bits to parse.
|
|
887
|
+
* @returns CoderType representing the parsed bits.
|
|
888
|
+
* @example
|
|
889
|
+
* const s = P.struct({ magic: P.bits(1), version: P.bits(1), tag: P.bits(4), len: P.bits(2) });
|
|
890
|
+
*/
|
|
891
|
+
export const bits = (len) => {
|
|
892
|
+
if (!isNum(len))
|
|
893
|
+
throw new Error(`bits: wrong length ${len} (${typeof len})`);
|
|
894
|
+
return wrap({
|
|
895
|
+
encodeStream: (w, value) => w.bits(value, len),
|
|
896
|
+
decodeStream: (r) => r.bits(len),
|
|
897
|
+
validate: (value) => {
|
|
898
|
+
if (!isNum(value))
|
|
899
|
+
throw new Error(`bits: wrong value ${value}`);
|
|
900
|
+
return value;
|
|
901
|
+
},
|
|
902
|
+
});
|
|
903
|
+
};
|
|
904
|
+
/**
|
|
905
|
+
* CoderType for working with bigint values.
|
|
906
|
+
* Unsized bigint values should be wrapped in a container (e.g., bytes or string).
|
|
907
|
+
*
|
|
908
|
+
* `0n = Uint8Array.of()`
|
|
909
|
+
*
|
|
910
|
+
* `1n = new Uint8Array([1n])`
|
|
911
|
+
*
|
|
912
|
+
* Please open issue, if you need different behavior for zero.
|
|
913
|
+
*
|
|
914
|
+
* @param size - Size of the bigint in bytes.
|
|
915
|
+
* @param le - Whether to use little-endian byte order.
|
|
916
|
+
* @param signed - Whether the bigint is signed.
|
|
917
|
+
* @param sized - Whether the bigint should have a fixed size.
|
|
918
|
+
* @returns CoderType representing the bigint value.
|
|
919
|
+
* @example
|
|
920
|
+
* const U512BE = P.bigint(64, false, true, true); // Define a CoderType for a 512-bit unsigned big-endian integer
|
|
921
|
+
*/
|
|
922
|
+
export const bigint = (size, le = false, signed = false, sized = true) => {
|
|
923
|
+
if (!isNum(size))
|
|
924
|
+
throw new Error(`bigint/size: wrong value ${size}`);
|
|
925
|
+
if (typeof le !== 'boolean')
|
|
926
|
+
throw new Error(`bigint/le: expected boolean, got ${typeof le}`);
|
|
927
|
+
if (typeof signed !== 'boolean')
|
|
928
|
+
throw new Error(`bigint/signed: expected boolean, got ${typeof signed}`);
|
|
929
|
+
if (typeof sized !== 'boolean')
|
|
930
|
+
throw new Error(`bigint/sized: expected boolean, got ${typeof sized}`);
|
|
931
|
+
const bLen = BigInt(size);
|
|
932
|
+
const signBit = 2n ** (8n * bLen - 1n);
|
|
933
|
+
return wrap({
|
|
934
|
+
size: sized ? size : undefined,
|
|
935
|
+
encodeStream: (w, value) => {
|
|
936
|
+
if (signed && value < 0)
|
|
937
|
+
value = value | signBit;
|
|
938
|
+
const b = [];
|
|
939
|
+
for (let i = 0; i < size; i++) {
|
|
940
|
+
b.push(Number(value & 255n));
|
|
941
|
+
value >>= 8n;
|
|
942
|
+
}
|
|
943
|
+
let res = new Uint8Array(b).reverse();
|
|
944
|
+
if (!sized) {
|
|
945
|
+
let pos = 0;
|
|
946
|
+
for (pos = 0; pos < res.length; pos++)
|
|
947
|
+
if (res[pos] !== 0)
|
|
948
|
+
break;
|
|
949
|
+
res = res.subarray(pos); // remove leading zeros
|
|
950
|
+
}
|
|
951
|
+
w.bytes(le ? res.reverse() : res);
|
|
952
|
+
},
|
|
953
|
+
decodeStream: (r) => {
|
|
954
|
+
// TODO: for le we can read until first zero?
|
|
955
|
+
const value = r.bytes(sized ? size : Math.min(size, r.leftBytes));
|
|
956
|
+
const b = le ? value : swapEndianness(value);
|
|
957
|
+
let res = 0n;
|
|
958
|
+
for (let i = 0; i < b.length; i++)
|
|
959
|
+
res |= BigInt(b[i]) << (8n * BigInt(i));
|
|
960
|
+
if (signed && res & signBit)
|
|
961
|
+
res = (res ^ signBit) - signBit;
|
|
962
|
+
return res;
|
|
963
|
+
},
|
|
964
|
+
validate: (value) => {
|
|
965
|
+
if (typeof value !== 'bigint')
|
|
966
|
+
throw new Error(`bigint: invalid value: ${value}`);
|
|
967
|
+
checkBounds(value, 8n * bLen, !!signed);
|
|
968
|
+
return value;
|
|
969
|
+
},
|
|
970
|
+
});
|
|
971
|
+
};
|
|
972
|
+
/** Unsigned 256-bit little-endian integer CoderType. */
|
|
973
|
+
export const U256LE = /* @__PURE__ */ bigint(32, true);
|
|
974
|
+
/** Unsigned 256-bit big-endian integer CoderType. */
|
|
975
|
+
export const U256BE = /* @__PURE__ */ bigint(32, false);
|
|
976
|
+
/** Signed 256-bit little-endian integer CoderType. */
|
|
977
|
+
export const I256LE = /* @__PURE__ */ bigint(32, true, true);
|
|
978
|
+
/** Signed 256-bit big-endian integer CoderType. */
|
|
979
|
+
export const I256BE = /* @__PURE__ */ bigint(32, false, true);
|
|
980
|
+
/** Unsigned 128-bit little-endian integer CoderType. */
|
|
981
|
+
export const U128LE = /* @__PURE__ */ bigint(16, true);
|
|
982
|
+
/** Unsigned 128-bit big-endian integer CoderType. */
|
|
983
|
+
export const U128BE = /* @__PURE__ */ bigint(16, false);
|
|
984
|
+
/** Signed 128-bit little-endian integer CoderType. */
|
|
985
|
+
export const I128LE = /* @__PURE__ */ bigint(16, true, true);
|
|
986
|
+
/** Signed 128-bit big-endian integer CoderType. */
|
|
987
|
+
export const I128BE = /* @__PURE__ */ bigint(16, false, true);
|
|
988
|
+
/** Unsigned 64-bit little-endian integer CoderType. */
|
|
989
|
+
export const U64LE = /* @__PURE__ */ bigint(8, true);
|
|
990
|
+
/** Unsigned 64-bit big-endian integer CoderType. */
|
|
991
|
+
export const U64BE = /* @__PURE__ */ bigint(8, false);
|
|
992
|
+
/** Signed 64-bit little-endian integer CoderType. */
|
|
993
|
+
export const I64LE = /* @__PURE__ */ bigint(8, true, true);
|
|
994
|
+
/** Signed 64-bit big-endian integer CoderType. */
|
|
995
|
+
export const I64BE = /* @__PURE__ */ bigint(8, false, true);
|
|
996
|
+
/**
|
|
997
|
+
* CoderType for working with numbber values (up to 6 bytes/48 bits).
|
|
998
|
+
* Unsized int values should be wrapped in a container (e.g., bytes or string).
|
|
999
|
+
*
|
|
1000
|
+
* `0 = Uint8Array.of()`
|
|
1001
|
+
*
|
|
1002
|
+
* `1 = new Uint8Array([1n])`
|
|
1003
|
+
*
|
|
1004
|
+
* Please open issue, if you need different behavior for zero.
|
|
1005
|
+
*
|
|
1006
|
+
* @param size - Size of the number in bytes.
|
|
1007
|
+
* @param le - Whether to use little-endian byte order.
|
|
1008
|
+
* @param signed - Whether the number is signed.
|
|
1009
|
+
* @param sized - Whether the number should have a fixed size.
|
|
1010
|
+
* @returns CoderType representing the number value.
|
|
1011
|
+
* @example
|
|
1012
|
+
* const uint64BE = P.bigint(8, false, true); // Define a CoderType for a 64-bit unsigned big-endian integer
|
|
1013
|
+
*/
|
|
1014
|
+
export const int = (size, le = false, signed = false, sized = true) => {
|
|
1015
|
+
if (!isNum(size))
|
|
1016
|
+
throw new Error(`int/size: wrong value ${size}`);
|
|
1017
|
+
if (typeof le !== 'boolean')
|
|
1018
|
+
throw new Error(`int/le: expected boolean, got ${typeof le}`);
|
|
1019
|
+
if (typeof signed !== 'boolean')
|
|
1020
|
+
throw new Error(`int/signed: expected boolean, got ${typeof signed}`);
|
|
1021
|
+
if (typeof sized !== 'boolean')
|
|
1022
|
+
throw new Error(`int/sized: expected boolean, got ${typeof sized}`);
|
|
1023
|
+
if (size > 6)
|
|
1024
|
+
throw new Error('int supports size up to 6 bytes (48 bits): use bigints instead');
|
|
1025
|
+
return apply(bigint(size, le, signed, sized), coders.numberBigint);
|
|
1026
|
+
};
|
|
1027
|
+
const view = (len, opts) => wrap({
|
|
1028
|
+
size: len,
|
|
1029
|
+
encodeStream: (w, value) => w.writeView(len, (view) => opts.write(view, value)),
|
|
1030
|
+
decodeStream: (r) => r.readView(len, opts.read),
|
|
1031
|
+
validate: (value) => {
|
|
1032
|
+
if (typeof value !== 'number')
|
|
1033
|
+
throw new Error(`viewCoder: expected number, got ${typeof value}`);
|
|
1034
|
+
if (opts.validate)
|
|
1035
|
+
opts.validate(value);
|
|
1036
|
+
return value;
|
|
1037
|
+
},
|
|
1038
|
+
});
|
|
1039
|
+
const intView = (len, signed, opts) => {
|
|
1040
|
+
const bits = len * 8;
|
|
1041
|
+
const signBit = 2 ** (bits - 1);
|
|
1042
|
+
// Inlined checkBounds for integer
|
|
1043
|
+
const validateSigned = (value) => {
|
|
1044
|
+
if (!isNum(value))
|
|
1045
|
+
throw new Error(`sintView: value is not safe integer: ${value}`);
|
|
1046
|
+
if (value < -signBit || value >= signBit) {
|
|
1047
|
+
throw new Error(`sintView: value out of bounds. Expected ${-signBit} <= ${value} < ${signBit}`);
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
const maxVal = 2 ** bits;
|
|
1051
|
+
const validateUnsigned = (value) => {
|
|
1052
|
+
if (!isNum(value))
|
|
1053
|
+
throw new Error(`uintView: value is not safe integer: ${value}`);
|
|
1054
|
+
if (0 > value || value >= maxVal) {
|
|
1055
|
+
throw new Error(`uintView: value out of bounds. Expected 0 <= ${value} < ${maxVal}`);
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
return view(len, {
|
|
1059
|
+
write: opts.write,
|
|
1060
|
+
read: opts.read,
|
|
1061
|
+
validate: signed ? validateSigned : validateUnsigned,
|
|
1062
|
+
});
|
|
1063
|
+
};
|
|
1064
|
+
/** Unsigned 32-bit little-endian integer CoderType. */
|
|
1065
|
+
export const U32LE = /* @__PURE__ */ intView(4, false, {
|
|
1066
|
+
read: (view, pos) => view.getUint32(pos, true),
|
|
1067
|
+
write: (view, value) => view.setUint32(0, value, true),
|
|
1068
|
+
});
|
|
1069
|
+
/** Unsigned 32-bit big-endian integer CoderType. */
|
|
1070
|
+
export const U32BE = /* @__PURE__ */ intView(4, false, {
|
|
1071
|
+
read: (view, pos) => view.getUint32(pos, false),
|
|
1072
|
+
write: (view, value) => view.setUint32(0, value, false),
|
|
1073
|
+
});
|
|
1074
|
+
/** Signed 32-bit little-endian integer CoderType. */
|
|
1075
|
+
export const I32LE = /* @__PURE__ */ intView(4, true, {
|
|
1076
|
+
read: (view, pos) => view.getInt32(pos, true),
|
|
1077
|
+
write: (view, value) => view.setInt32(0, value, true),
|
|
1078
|
+
});
|
|
1079
|
+
/** Signed 32-bit big-endian integer CoderType. */
|
|
1080
|
+
export const I32BE = /* @__PURE__ */ intView(4, true, {
|
|
1081
|
+
read: (view, pos) => view.getInt32(pos, false),
|
|
1082
|
+
write: (view, value) => view.setInt32(0, value, false),
|
|
1083
|
+
});
|
|
1084
|
+
/** Unsigned 16-bit little-endian integer CoderType. */
|
|
1085
|
+
export const U16LE = /* @__PURE__ */ intView(2, false, {
|
|
1086
|
+
read: (view, pos) => view.getUint16(pos, true),
|
|
1087
|
+
write: (view, value) => view.setUint16(0, value, true),
|
|
1088
|
+
});
|
|
1089
|
+
/** Unsigned 16-bit big-endian integer CoderType. */
|
|
1090
|
+
export const U16BE = /* @__PURE__ */ intView(2, false, {
|
|
1091
|
+
read: (view, pos) => view.getUint16(pos, false),
|
|
1092
|
+
write: (view, value) => view.setUint16(0, value, false),
|
|
1093
|
+
});
|
|
1094
|
+
/** Signed 16-bit little-endian integer CoderType. */
|
|
1095
|
+
export const I16LE = /* @__PURE__ */ intView(2, true, {
|
|
1096
|
+
read: (view, pos) => view.getInt16(pos, true),
|
|
1097
|
+
write: (view, value) => view.setInt16(0, value, true),
|
|
1098
|
+
});
|
|
1099
|
+
/** Signed 16-bit big-endian integer CoderType. */
|
|
1100
|
+
export const I16BE = /* @__PURE__ */ intView(2, true, {
|
|
1101
|
+
read: (view, pos) => view.getInt16(pos, false),
|
|
1102
|
+
write: (view, value) => view.setInt16(0, value, false),
|
|
1103
|
+
});
|
|
1104
|
+
/** Unsigned 8-bit integer CoderType. */
|
|
1105
|
+
export const U8 = /* @__PURE__ */ intView(1, false, {
|
|
1106
|
+
read: (view, pos) => view.getUint8(pos),
|
|
1107
|
+
write: (view, value) => view.setUint8(0, value),
|
|
1108
|
+
});
|
|
1109
|
+
/** Signed 8-bit integer CoderType. */
|
|
1110
|
+
export const I8 = /* @__PURE__ */ intView(1, true, {
|
|
1111
|
+
read: (view, pos) => view.getInt8(pos),
|
|
1112
|
+
write: (view, value) => view.setInt8(0, value),
|
|
1113
|
+
});
|
|
1114
|
+
// Floats
|
|
1115
|
+
const f32 = (le) => view(4, {
|
|
1116
|
+
read: (view, pos) => view.getFloat32(pos, le),
|
|
1117
|
+
write: (view, value) => view.setFloat32(0, value, le),
|
|
1118
|
+
validate: (value) => {
|
|
1119
|
+
if (Math.fround(value) !== value && !Number.isNaN(value))
|
|
1120
|
+
throw new Error(`f32: wrong value=${value}`);
|
|
1121
|
+
},
|
|
1122
|
+
});
|
|
1123
|
+
const f64 = (le) => view(8, {
|
|
1124
|
+
read: (view, pos) => view.getFloat64(pos, le),
|
|
1125
|
+
write: (view, value) => view.setFloat64(0, value, le),
|
|
1126
|
+
});
|
|
1127
|
+
/** 32-bit big-endian floating point CoderType ("binary32", IEEE 754-2008). */
|
|
1128
|
+
export const F32BE = /* @__PURE__ */ f32(false);
|
|
1129
|
+
/** 32-bit little-endian floating point CoderType ("binary32", IEEE 754-2008). */
|
|
1130
|
+
export const F32LE = /* @__PURE__ */ f32(true);
|
|
1131
|
+
/** A 64-bit big-endian floating point type ("binary64", IEEE 754-2008). Any JS number can be encoded. */
|
|
1132
|
+
export const F64BE = /* @__PURE__ */ f64(false);
|
|
1133
|
+
/** A 64-bit little-endian floating point type ("binary64", IEEE 754-2008). Any JS number can be encoded. */
|
|
1134
|
+
export const F64LE = /* @__PURE__ */ f64(true);
|
|
1135
|
+
/** Boolean CoderType. */
|
|
1136
|
+
export const bool = /* @__PURE__ */ wrap({
|
|
1137
|
+
size: 1,
|
|
1138
|
+
encodeStream: (w, value) => w.byte(value ? 1 : 0),
|
|
1139
|
+
decodeStream: (r) => {
|
|
1140
|
+
const value = r.byte();
|
|
1141
|
+
if (value !== 0 && value !== 1)
|
|
1142
|
+
throw r.err(`bool: invalid value ${value}`);
|
|
1143
|
+
return value === 1;
|
|
1144
|
+
},
|
|
1145
|
+
validate: (value) => {
|
|
1146
|
+
if (typeof value !== 'boolean')
|
|
1147
|
+
throw new Error(`bool: invalid value ${value}`);
|
|
1148
|
+
return value;
|
|
1149
|
+
},
|
|
1150
|
+
});
|
|
1151
|
+
/**
|
|
1152
|
+
* Bytes CoderType with a specified length and endianness.
|
|
1153
|
+
* The bytes can have:
|
|
1154
|
+
* - Dynamic size (prefixed with a length CoderType like U16BE)
|
|
1155
|
+
* - Fixed size (specified by a number)
|
|
1156
|
+
* - Unknown size (null, will parse until end of buffer)
|
|
1157
|
+
* - Zero-terminated (terminator can be any Uint8Array)
|
|
1158
|
+
* @param len - CoderType, number, Uint8Array (terminator) or null
|
|
1159
|
+
* @param le - Whether to use little-endian byte order.
|
|
1160
|
+
* @returns CoderType representing the bytes.
|
|
1161
|
+
* @example
|
|
1162
|
+
* // Dynamic size bytes (prefixed with P.U16BE number of bytes length)
|
|
1163
|
+
* const dynamicBytes = P.bytes(P.U16BE, false);
|
|
1164
|
+
* const fixedBytes = P.bytes(32, false); // Fixed size bytes
|
|
1165
|
+
* const unknownBytes = P.bytes(null, false); // Unknown size bytes, will parse until end of buffer
|
|
1166
|
+
* const zeroTerminatedBytes = P.bytes(Uint8Array.of(0), false); // Zero-terminated bytes
|
|
1167
|
+
*/
|
|
1168
|
+
const createBytes = (len, le = false) => {
|
|
1169
|
+
if (typeof le !== 'boolean')
|
|
1170
|
+
throw new Error(`bytes/le: expected boolean, got ${typeof le}`);
|
|
1171
|
+
const _length = lengthCoder(len);
|
|
1172
|
+
const _isb = isBytes(len);
|
|
1173
|
+
return wrap({
|
|
1174
|
+
size: typeof len === 'number' ? len : undefined,
|
|
1175
|
+
encodeStream: (w, value) => {
|
|
1176
|
+
if (!_isb)
|
|
1177
|
+
_length.encodeStream(w, value.length);
|
|
1178
|
+
w.bytes(le ? swapEndianness(value) : value);
|
|
1179
|
+
if (_isb)
|
|
1180
|
+
w.bytes(len);
|
|
1181
|
+
},
|
|
1182
|
+
decodeStream: (r) => {
|
|
1183
|
+
let bytes;
|
|
1184
|
+
if (_isb) {
|
|
1185
|
+
const tPos = r.find(len);
|
|
1186
|
+
if (!tPos)
|
|
1187
|
+
throw r.err(`bytes: cannot find terminator`);
|
|
1188
|
+
bytes = r.bytes(tPos - r.pos);
|
|
1189
|
+
r.bytes(len.length);
|
|
1190
|
+
}
|
|
1191
|
+
else {
|
|
1192
|
+
bytes = r.bytes(len === null ? r.leftBytes : _length.decodeStream(r));
|
|
1193
|
+
}
|
|
1194
|
+
return le ? swapEndianness(bytes) : bytes;
|
|
1195
|
+
},
|
|
1196
|
+
validate: (value) => {
|
|
1197
|
+
if (!isBytes(value))
|
|
1198
|
+
throw new Error(`bytes: invalid value ${value}`);
|
|
1199
|
+
return value;
|
|
1200
|
+
},
|
|
1201
|
+
});
|
|
1202
|
+
};
|
|
1203
|
+
export { createBytes as bytes, createHex as hex };
|
|
1204
|
+
/**
|
|
1205
|
+
* Prefix-encoded value using a length prefix and an inner CoderType.
|
|
1206
|
+
* The prefix can have:
|
|
1207
|
+
* - Dynamic size (prefixed with a length CoderType like U16BE)
|
|
1208
|
+
* - Fixed size (specified by a number)
|
|
1209
|
+
* - Unknown size (null, will parse until end of buffer)
|
|
1210
|
+
* - Zero-terminated (terminator can be any Uint8Array)
|
|
1211
|
+
* @param len - Length CoderType (dynamic size), number (fixed size), Uint8Array (for terminator), or null (will parse until end of buffer)
|
|
1212
|
+
* @param inner - CoderType for the actual value to be prefix-encoded.
|
|
1213
|
+
* @returns CoderType representing the prefix-encoded value.
|
|
1214
|
+
* @example
|
|
1215
|
+
* const dynamicPrefix = P.prefix(P.U16BE, P.bytes(null)); // Dynamic size prefix (prefixed with P.U16BE number of bytes length)
|
|
1216
|
+
* const fixedPrefix = P.prefix(10, P.bytes(null)); // Fixed size prefix (always 10 bytes)
|
|
1217
|
+
*/
|
|
1218
|
+
export function prefix(len, inner) {
|
|
1219
|
+
if (!isCoder(inner))
|
|
1220
|
+
throw new Error(`prefix: invalid inner value ${inner}`);
|
|
1221
|
+
return apply(createBytes(len), reverse(inner));
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* String CoderType with a specified length and endianness.
|
|
1225
|
+
* The string can be:
|
|
1226
|
+
* - Dynamic size (prefixed with a length CoderType like U16BE)
|
|
1227
|
+
* - Fixed size (specified by a number)
|
|
1228
|
+
* - Unknown size (null, will parse until end of buffer)
|
|
1229
|
+
* - Zero-terminated (terminator can be any Uint8Array)
|
|
1230
|
+
* @param len - Length CoderType (dynamic size), number (fixed size), Uint8Array (for terminator), or null (will parse until end of buffer)
|
|
1231
|
+
* @param le - Whether to use little-endian byte order.
|
|
1232
|
+
* @returns CoderType representing the string.
|
|
1233
|
+
* @example
|
|
1234
|
+
* const dynamicString = P.string(P.U16BE, false); // Dynamic size string (prefixed with P.U16BE number of string length)
|
|
1235
|
+
* const fixedString = P.string(10, false); // Fixed size string
|
|
1236
|
+
* const unknownString = P.string(null, false); // Unknown size string, will parse until end of buffer
|
|
1237
|
+
* const nullTerminatedString = P.cstring; // NUL-terminated string
|
|
1238
|
+
* const _cstring = P.string(Uint8Array.of(0)); // Same thing
|
|
1239
|
+
*/
|
|
1240
|
+
export const string = (len, le = false) => validate(apply(createBytes(len, le), utf8), (value) => {
|
|
1241
|
+
// TextEncoder/TextDecoder will fail on non-string, but we create more readable errors earlier
|
|
1242
|
+
if (typeof value !== 'string')
|
|
1243
|
+
throw new Error(`expected string, got ${typeof value}`);
|
|
1244
|
+
return value;
|
|
1245
|
+
});
|
|
1246
|
+
/** NUL-terminated string CoderType. */
|
|
1247
|
+
export const cstring = /* @__PURE__ */ string(NULL);
|
|
1248
|
+
/**
|
|
1249
|
+
* Hexadecimal string CoderType with a specified length, endianness, and optional 0x prefix.
|
|
1250
|
+
* @param len - Length CoderType (dynamic size), number (fixed size), Uint8Array (for terminator), or null (will parse until end of buffer)
|
|
1251
|
+
* @param le - Whether to use little-endian byte order.
|
|
1252
|
+
* @param withZero - Whether to include the 0x prefix.
|
|
1253
|
+
* @returns CoderType representing the hexadecimal string.
|
|
1254
|
+
* @example
|
|
1255
|
+
* const dynamicHex = P.hex(P.U16BE, {isLE: false, with0x: true}); // Hex string with 0x prefix and U16BE length
|
|
1256
|
+
* const fixedHex = P.hex(32, {isLE: false, with0x: false}); // Fixed-length 32-byte hex string without 0x prefix
|
|
1257
|
+
*/
|
|
1258
|
+
const createHex = (len, options = { isLE: false, with0x: false }) => {
|
|
1259
|
+
let inner = apply(createBytes(len, options.isLE), baseHex);
|
|
1260
|
+
const prefix = options.with0x;
|
|
1261
|
+
if (typeof prefix !== 'boolean')
|
|
1262
|
+
throw new Error(`hex/with0x: expected boolean, got ${typeof prefix}`);
|
|
1263
|
+
if (prefix) {
|
|
1264
|
+
inner = apply(inner, {
|
|
1265
|
+
encode: (value) => `0x${value}`,
|
|
1266
|
+
decode: (value) => {
|
|
1267
|
+
if (!value.startsWith('0x'))
|
|
1268
|
+
throw new Error('hex(with0x=true).encode input should start with 0x');
|
|
1269
|
+
return value.slice(2);
|
|
1270
|
+
},
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1273
|
+
return inner;
|
|
1274
|
+
};
|
|
1275
|
+
/**
|
|
1276
|
+
* Applies a base coder to a CoderType.
|
|
1277
|
+
* @param inner - The inner CoderType.
|
|
1278
|
+
* @param b - The base coder to apply.
|
|
1279
|
+
* @returns CoderType representing the transformed value.
|
|
1280
|
+
* @example
|
|
1281
|
+
* import { hex } from '@scure/base';
|
|
1282
|
+
* const hex = P.apply(P.bytes(32), hex); // will decode bytes into a hex string
|
|
1283
|
+
*/
|
|
1284
|
+
export function apply(inner, base) {
|
|
1285
|
+
if (!isCoder(inner))
|
|
1286
|
+
throw new Error(`apply: invalid inner value ${inner}`);
|
|
1287
|
+
if (!isBaseCoder(base))
|
|
1288
|
+
throw new Error(`apply: invalid base value ${inner}`);
|
|
1289
|
+
return wrap({
|
|
1290
|
+
size: inner.size,
|
|
1291
|
+
encodeStream: (w, value) => {
|
|
1292
|
+
let innerValue;
|
|
1293
|
+
try {
|
|
1294
|
+
innerValue = base.decode(value);
|
|
1295
|
+
}
|
|
1296
|
+
catch (e) {
|
|
1297
|
+
throw w.err('' + e);
|
|
1298
|
+
}
|
|
1299
|
+
return inner.encodeStream(w, innerValue);
|
|
1300
|
+
},
|
|
1301
|
+
decodeStream: (r) => {
|
|
1302
|
+
const innerValue = inner.decodeStream(r);
|
|
1303
|
+
try {
|
|
1304
|
+
return base.encode(innerValue);
|
|
1305
|
+
}
|
|
1306
|
+
catch (e) {
|
|
1307
|
+
throw r.err('' + e);
|
|
1308
|
+
}
|
|
1309
|
+
},
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
/**
|
|
1313
|
+
* Lazy CoderType that is evaluated at runtime.
|
|
1314
|
+
* @param fn - A function that returns the CoderType.
|
|
1315
|
+
* @returns CoderType representing the lazy value.
|
|
1316
|
+
* @example
|
|
1317
|
+
* type Tree = { name: string; children: Tree[] };
|
|
1318
|
+
* const tree = P.struct({
|
|
1319
|
+
* name: P.cstring,
|
|
1320
|
+
* children: P.array(
|
|
1321
|
+
* P.U16BE,
|
|
1322
|
+
* P.lazy((): P.CoderType<Tree> => tree)
|
|
1323
|
+
* ),
|
|
1324
|
+
* });
|
|
1325
|
+
*/
|
|
1326
|
+
export function lazy(fn) {
|
|
1327
|
+
if (typeof fn !== 'function')
|
|
1328
|
+
throw new Error(`lazy: expected function, got ${typeof fn}`);
|
|
1329
|
+
return wrap({
|
|
1330
|
+
encodeStream: (w, value) => fn().encodeStream(w, value),
|
|
1331
|
+
decodeStream: (r) => fn().decodeStream(r),
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* Flag CoderType that encodes/decodes a boolean value based on the presence of a marker.
|
|
1336
|
+
* @param flagValue - Marker value.
|
|
1337
|
+
* @param xor - Whether to invert the flag behavior.
|
|
1338
|
+
* @returns CoderType representing the flag value.
|
|
1339
|
+
* @example
|
|
1340
|
+
* const flag = P.flag(new Uint8Array([0x01, 0x02])); // Encodes true as u8a([0x01, 0x02]), false as u8a([])
|
|
1341
|
+
* const flagXor = P.flag(new Uint8Array([0x01, 0x02]), true); // Encodes true as u8a([]), false as u8a([0x01, 0x02])
|
|
1342
|
+
* // Conditional encoding with flagged
|
|
1343
|
+
* const s = P.struct({ f: P.flag(new Uint8Array([0x0, 0x1])), f2: P.flagged('f', P.U32BE) });
|
|
1344
|
+
*/
|
|
1345
|
+
export const flag = (flagValue, xor = false) => {
|
|
1346
|
+
if (!isBytes(flagValue))
|
|
1347
|
+
throw new Error(`flag/flagValue: expected Uint8Array, got ${typeof flagValue}`);
|
|
1348
|
+
if (typeof xor !== 'boolean')
|
|
1349
|
+
throw new Error(`flag/xor: expected boolean, got ${typeof xor}`);
|
|
1350
|
+
return wrap({
|
|
1351
|
+
size: flagValue.length,
|
|
1352
|
+
encodeStream: (w, value) => {
|
|
1353
|
+
if (!!value !== xor)
|
|
1354
|
+
w.bytes(flagValue);
|
|
1355
|
+
},
|
|
1356
|
+
decodeStream: (r) => {
|
|
1357
|
+
let hasFlag = r.leftBytes >= flagValue.length;
|
|
1358
|
+
if (hasFlag) {
|
|
1359
|
+
hasFlag = equalBytes(r.bytes(flagValue.length, true), flagValue);
|
|
1360
|
+
// Found flag, advance cursor position
|
|
1361
|
+
if (hasFlag)
|
|
1362
|
+
r.bytes(flagValue.length);
|
|
1363
|
+
}
|
|
1364
|
+
return hasFlag !== xor; // hasFlag ^ xor
|
|
1365
|
+
},
|
|
1366
|
+
validate: (value) => {
|
|
1367
|
+
if (value !== undefined && typeof value !== 'boolean')
|
|
1368
|
+
throw new Error(`flag: expected boolean value or undefined, got ${typeof value}`);
|
|
1369
|
+
return value;
|
|
1370
|
+
},
|
|
1371
|
+
});
|
|
1372
|
+
};
|
|
1373
|
+
/**
|
|
1374
|
+
* Conditional CoderType that encodes/decodes a value only if a flag is present.
|
|
1375
|
+
* @param path - Path to the flag value or a CoderType for the flag.
|
|
1376
|
+
* @param inner - Inner CoderType for the value.
|
|
1377
|
+
* @param def - Optional default value to use if the flag is not present.
|
|
1378
|
+
* @returns CoderType representing the conditional value.
|
|
1379
|
+
* @example
|
|
1380
|
+
* const s = P.struct({
|
|
1381
|
+
* f: P.flag(new Uint8Array([0x0, 0x1])),
|
|
1382
|
+
* f2: P.flagged('f', P.U32BE)
|
|
1383
|
+
* });
|
|
1384
|
+
*
|
|
1385
|
+
* @example
|
|
1386
|
+
* const s2 = P.struct({
|
|
1387
|
+
* f: P.flag(new Uint8Array([0x0, 0x1])),
|
|
1388
|
+
* f2: P.flagged('f', P.U32BE, 123)
|
|
1389
|
+
* });
|
|
1390
|
+
*/
|
|
1391
|
+
export function flagged(path, inner, def) {
|
|
1392
|
+
if (!isCoder(inner))
|
|
1393
|
+
throw new Error(`flagged: invalid inner value ${inner}`);
|
|
1394
|
+
if (typeof path !== 'string' && !isCoder(inner))
|
|
1395
|
+
throw new Error(`flagged: wrong path=${path}`);
|
|
1396
|
+
return wrap({
|
|
1397
|
+
encodeStream: (w, value) => {
|
|
1398
|
+
if (typeof path === 'string') {
|
|
1399
|
+
if (Path.resolve(w.stack, path))
|
|
1400
|
+
inner.encodeStream(w, value);
|
|
1401
|
+
else if (def)
|
|
1402
|
+
inner.encodeStream(w, def);
|
|
1403
|
+
}
|
|
1404
|
+
else {
|
|
1405
|
+
path.encodeStream(w, !!value);
|
|
1406
|
+
if (!!value)
|
|
1407
|
+
inner.encodeStream(w, value);
|
|
1408
|
+
else if (def)
|
|
1409
|
+
inner.encodeStream(w, def);
|
|
1410
|
+
}
|
|
1411
|
+
},
|
|
1412
|
+
decodeStream: (r) => {
|
|
1413
|
+
let hasFlag = false;
|
|
1414
|
+
if (typeof path === 'string')
|
|
1415
|
+
hasFlag = !!Path.resolve(r.stack, path);
|
|
1416
|
+
else
|
|
1417
|
+
hasFlag = path.decodeStream(r);
|
|
1418
|
+
// If there is a flag -- decode and return value
|
|
1419
|
+
if (hasFlag)
|
|
1420
|
+
return inner.decodeStream(r);
|
|
1421
|
+
else if (def)
|
|
1422
|
+
inner.decodeStream(r);
|
|
1423
|
+
return;
|
|
1424
|
+
},
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
/**
|
|
1428
|
+
* Optional CoderType that encodes/decodes a value based on a flag.
|
|
1429
|
+
* @param flag - CoderType for the flag value.
|
|
1430
|
+
* @param inner - Inner CoderType for the value.
|
|
1431
|
+
* @param def - Optional default value to use if the flag is not present.
|
|
1432
|
+
* @returns CoderType representing the optional value.
|
|
1433
|
+
* @example
|
|
1434
|
+
* // Will decode into P.U32BE only if flag present
|
|
1435
|
+
* const optional = P.optional(P.flag(new Uint8Array([0x0, 0x1])), P.U32BE);
|
|
1436
|
+
*
|
|
1437
|
+
* @example
|
|
1438
|
+
* // If no flag present, will decode into default value
|
|
1439
|
+
* const optionalWithDefault = P.optional(P.flag(new Uint8Array([0x0, 0x1])), P.U32BE, 123);
|
|
1440
|
+
*/
|
|
1441
|
+
export function optional(flag, inner, def) {
|
|
1442
|
+
if (!isCoder(flag) || !isCoder(inner))
|
|
1443
|
+
throw new Error(`optional: invalid flag or inner value flag=${flag} inner=${inner}`);
|
|
1444
|
+
return wrap({
|
|
1445
|
+
size: def !== undefined && flag.size && inner.size ? flag.size + inner.size : undefined,
|
|
1446
|
+
encodeStream: (w, value) => {
|
|
1447
|
+
flag.encodeStream(w, !!value);
|
|
1448
|
+
if (value)
|
|
1449
|
+
inner.encodeStream(w, value);
|
|
1450
|
+
else if (def !== undefined)
|
|
1451
|
+
inner.encodeStream(w, def);
|
|
1452
|
+
},
|
|
1453
|
+
decodeStream: (r) => {
|
|
1454
|
+
if (flag.decodeStream(r))
|
|
1455
|
+
return inner.decodeStream(r);
|
|
1456
|
+
else if (def !== undefined)
|
|
1457
|
+
inner.decodeStream(r);
|
|
1458
|
+
return;
|
|
1459
|
+
},
|
|
1460
|
+
});
|
|
1461
|
+
}
|
|
1462
|
+
/**
|
|
1463
|
+
* Magic value CoderType that encodes/decodes a constant value.
|
|
1464
|
+
* This can be used to check for a specific magic value or sequence of bytes at the beginning of a data structure.
|
|
1465
|
+
* @param inner - Inner CoderType for the value.
|
|
1466
|
+
* @param constant - Constant value.
|
|
1467
|
+
* @param check - Whether to check the decoded value against the constant.
|
|
1468
|
+
* @returns CoderType representing the magic value.
|
|
1469
|
+
* @example
|
|
1470
|
+
* // Always encodes constant as bytes using inner CoderType, throws if encoded value is not present
|
|
1471
|
+
* const magicU8 = P.magic(P.U8, 0x42);
|
|
1472
|
+
*/
|
|
1473
|
+
export function magic(inner, constant, check = true) {
|
|
1474
|
+
if (!isCoder(inner))
|
|
1475
|
+
throw new Error(`magic: invalid inner value ${inner}`);
|
|
1476
|
+
if (typeof check !== 'boolean')
|
|
1477
|
+
throw new Error(`magic: expected boolean, got ${typeof check}`);
|
|
1478
|
+
return wrap({
|
|
1479
|
+
size: inner.size,
|
|
1480
|
+
encodeStream: (w, _value) => inner.encodeStream(w, constant),
|
|
1481
|
+
decodeStream: (r) => {
|
|
1482
|
+
const value = inner.decodeStream(r);
|
|
1483
|
+
if ((check && typeof value !== 'object' && value !== constant) ||
|
|
1484
|
+
(isBytes(constant) && !equalBytes(constant, value))) {
|
|
1485
|
+
throw r.err(`magic: invalid value: ${value} !== ${constant}`);
|
|
1486
|
+
}
|
|
1487
|
+
return;
|
|
1488
|
+
},
|
|
1489
|
+
validate: (value) => {
|
|
1490
|
+
if (value !== undefined)
|
|
1491
|
+
throw new Error(`magic: wrong value=${typeof value}`);
|
|
1492
|
+
return value;
|
|
1493
|
+
},
|
|
1494
|
+
});
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Magic bytes CoderType that encodes/decodes a constant byte array or string.
|
|
1498
|
+
* @param constant - Constant byte array or string.
|
|
1499
|
+
* @returns CoderType representing the magic bytes.
|
|
1500
|
+
* @example
|
|
1501
|
+
* // Always encodes undefined into byte representation of string 'MAGIC'
|
|
1502
|
+
* const magicBytes = P.magicBytes('MAGIC');
|
|
1503
|
+
*/
|
|
1504
|
+
export const magicBytes = (constant) => {
|
|
1505
|
+
const c = typeof constant === 'string' ? utf8.decode(constant) : constant;
|
|
1506
|
+
return magic(createBytes(c.length), c);
|
|
1507
|
+
};
|
|
1508
|
+
/**
|
|
1509
|
+
* Creates a CoderType for a constant value. The function enforces this value during encoding,
|
|
1510
|
+
* ensuring it matches the provided constant. During decoding, it always returns the constant value.
|
|
1511
|
+
* The actual value is not written to or read from any byte stream; it's used only for validation.
|
|
1512
|
+
*
|
|
1513
|
+
* @param c - Constant value.
|
|
1514
|
+
* @returns CoderType representing the constant value.
|
|
1515
|
+
* @example
|
|
1516
|
+
* // Always return 123 on decode, throws on encoding anything other than 123
|
|
1517
|
+
* const constantU8 = P.constant(123);
|
|
1518
|
+
*/
|
|
1519
|
+
export function constant(c) {
|
|
1520
|
+
return wrap({
|
|
1521
|
+
encodeStream: (_w, value) => {
|
|
1522
|
+
if (value !== c)
|
|
1523
|
+
throw new Error(`constant: invalid value ${value} (exp: ${c})`);
|
|
1524
|
+
},
|
|
1525
|
+
decodeStream: (_r) => c,
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
function sizeof(fields) {
|
|
1529
|
+
let size = 0;
|
|
1530
|
+
for (const f of fields) {
|
|
1531
|
+
if (f.size === undefined)
|
|
1532
|
+
return;
|
|
1533
|
+
if (!isNum(f.size))
|
|
1534
|
+
throw new Error(`sizeof: wrong element size=${size}`);
|
|
1535
|
+
size += f.size;
|
|
1536
|
+
}
|
|
1537
|
+
return size;
|
|
1538
|
+
}
|
|
1539
|
+
/**
|
|
1540
|
+
* Structure of composable primitives (C/Rust struct)
|
|
1541
|
+
* @param fields - Object mapping field names to CoderTypes.
|
|
1542
|
+
* @returns CoderType representing the structure.
|
|
1543
|
+
* @example
|
|
1544
|
+
* // Define a structure with a 32-bit big-endian unsigned integer, a string, and a nested structure
|
|
1545
|
+
* const myStruct = P.struct({
|
|
1546
|
+
* id: P.U32BE,
|
|
1547
|
+
* name: P.string(P.U8),
|
|
1548
|
+
* nested: P.struct({
|
|
1549
|
+
* flag: P.bool,
|
|
1550
|
+
* value: P.I16LE
|
|
1551
|
+
* })
|
|
1552
|
+
* });
|
|
1553
|
+
*/
|
|
1554
|
+
export function struct(fields) {
|
|
1555
|
+
if (!isPlainObject(fields))
|
|
1556
|
+
throw new Error(`struct: expected plain object, got ${fields}`);
|
|
1557
|
+
for (const name in fields) {
|
|
1558
|
+
if (!isCoder(fields[name]))
|
|
1559
|
+
throw new Error(`struct: field ${name} is not CoderType`);
|
|
1560
|
+
}
|
|
1561
|
+
return wrap({
|
|
1562
|
+
size: sizeof(Object.values(fields)),
|
|
1563
|
+
encodeStream: (w, value) => {
|
|
1564
|
+
w.pushObj(value, (fieldFn) => {
|
|
1565
|
+
for (const name in fields)
|
|
1566
|
+
fieldFn(name, () => fields[name].encodeStream(w, value[name]));
|
|
1567
|
+
});
|
|
1568
|
+
},
|
|
1569
|
+
decodeStream: (r) => {
|
|
1570
|
+
const res = {};
|
|
1571
|
+
r.pushObj(res, (fieldFn) => {
|
|
1572
|
+
for (const name in fields)
|
|
1573
|
+
fieldFn(name, () => (res[name] = fields[name].decodeStream(r)));
|
|
1574
|
+
});
|
|
1575
|
+
return res;
|
|
1576
|
+
},
|
|
1577
|
+
validate: (value) => {
|
|
1578
|
+
if (typeof value !== 'object' || value === null)
|
|
1579
|
+
throw new Error(`struct: invalid value ${value}`);
|
|
1580
|
+
return value;
|
|
1581
|
+
},
|
|
1582
|
+
});
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* Tuple (unnamed structure) of CoderTypes. Same as struct but with unnamed fields.
|
|
1586
|
+
* @param fields - Array of CoderTypes.
|
|
1587
|
+
* @returns CoderType representing the tuple.
|
|
1588
|
+
* @example
|
|
1589
|
+
* const myTuple = P.tuple([P.U8, P.U16LE, P.string(P.U8)]);
|
|
1590
|
+
*/
|
|
1591
|
+
export function tuple(fields) {
|
|
1592
|
+
if (!Array.isArray(fields))
|
|
1593
|
+
throw new Error(`Packed.Tuple: got ${typeof fields} instead of array`);
|
|
1594
|
+
for (let i = 0; i < fields.length; i++) {
|
|
1595
|
+
if (!isCoder(fields[i]))
|
|
1596
|
+
throw new Error(`tuple: field ${i} is not CoderType`);
|
|
1597
|
+
}
|
|
1598
|
+
return wrap({
|
|
1599
|
+
size: sizeof(fields),
|
|
1600
|
+
encodeStream: (w, value) => {
|
|
1601
|
+
// TODO: fix types
|
|
1602
|
+
if (!Array.isArray(value))
|
|
1603
|
+
throw w.err(`tuple: invalid value ${value}`);
|
|
1604
|
+
w.pushObj(value, (fieldFn) => {
|
|
1605
|
+
for (let i = 0; i < fields.length; i++)
|
|
1606
|
+
fieldFn(`${i}`, () => fields[i].encodeStream(w, value[i]));
|
|
1607
|
+
});
|
|
1608
|
+
},
|
|
1609
|
+
decodeStream: (r) => {
|
|
1610
|
+
const res = [];
|
|
1611
|
+
r.pushObj(res, (fieldFn) => {
|
|
1612
|
+
for (let i = 0; i < fields.length; i++)
|
|
1613
|
+
fieldFn(`${i}`, () => res.push(fields[i].decodeStream(r)));
|
|
1614
|
+
});
|
|
1615
|
+
return res;
|
|
1616
|
+
},
|
|
1617
|
+
validate: (value) => {
|
|
1618
|
+
if (!Array.isArray(value))
|
|
1619
|
+
throw new Error(`tuple: invalid value ${value}`);
|
|
1620
|
+
if (value.length !== fields.length)
|
|
1621
|
+
throw new Error(`tuple: wrong length=${value.length}, expected ${fields.length}`);
|
|
1622
|
+
return value;
|
|
1623
|
+
},
|
|
1624
|
+
});
|
|
1625
|
+
}
|
|
1626
|
+
/**
|
|
1627
|
+
* Array of items (inner type) with a specified length.
|
|
1628
|
+
* @param len - Length CoderType (dynamic size), number (fixed size), Uint8Array (for terminator), or null (will parse until end of buffer)
|
|
1629
|
+
* @param inner - CoderType for encoding/decoding each array item.
|
|
1630
|
+
* @returns CoderType representing the array.
|
|
1631
|
+
* @example
|
|
1632
|
+
* const a1 = P.array(P.U16BE, child); // Dynamic size array (prefixed with P.U16BE number of array length)
|
|
1633
|
+
* const a2 = P.array(4, child); // Fixed size array
|
|
1634
|
+
* const a3 = P.array(null, child); // Unknown size array, will parse until end of buffer
|
|
1635
|
+
* const a4 = P.array(Uint8Array.of(0), child); // zero-terminated array (NOTE: terminator can be any buffer)
|
|
1636
|
+
*/
|
|
1637
|
+
export function array(len, inner) {
|
|
1638
|
+
if (!isCoder(inner))
|
|
1639
|
+
throw new Error(`array: invalid inner value ${inner}`);
|
|
1640
|
+
// By construction length is inside array (otherwise there will be various incorrect stack states)
|
|
1641
|
+
// But forcing users always write '..' seems like bad idea. Also, breaking change.
|
|
1642
|
+
const _length = lengthCoder(typeof len === 'string' ? `../${len}` : len);
|
|
1643
|
+
return wrap({
|
|
1644
|
+
size: typeof len === 'number' && inner.size ? len * inner.size : undefined,
|
|
1645
|
+
encodeStream: (w, value) => {
|
|
1646
|
+
const _w = w;
|
|
1647
|
+
_w.pushObj(value, (fieldFn) => {
|
|
1648
|
+
if (!isBytes(len))
|
|
1649
|
+
_length.encodeStream(w, value.length);
|
|
1650
|
+
for (let i = 0; i < value.length; i++) {
|
|
1651
|
+
fieldFn(`${i}`, () => {
|
|
1652
|
+
const elm = value[i];
|
|
1653
|
+
const startPos = w.pos;
|
|
1654
|
+
inner.encodeStream(w, elm);
|
|
1655
|
+
if (isBytes(len)) {
|
|
1656
|
+
// Terminator is bigger than elm size, so skip
|
|
1657
|
+
if (len.length > _w.pos - startPos)
|
|
1658
|
+
return;
|
|
1659
|
+
const data = _w.finish(false).subarray(startPos, _w.pos);
|
|
1660
|
+
// There is still possible case when multiple elements create terminator,
|
|
1661
|
+
// but it is hard to catch here, will be very slow
|
|
1662
|
+
if (equalBytes(data.subarray(0, len.length), len))
|
|
1663
|
+
throw _w.err(`array: inner element encoding same as separator. elm=${elm} data=${data}`);
|
|
1664
|
+
}
|
|
1665
|
+
});
|
|
1666
|
+
}
|
|
1667
|
+
});
|
|
1668
|
+
if (isBytes(len))
|
|
1669
|
+
w.bytes(len);
|
|
1670
|
+
},
|
|
1671
|
+
decodeStream: (r) => {
|
|
1672
|
+
const res = [];
|
|
1673
|
+
r.pushObj(res, (fieldFn) => {
|
|
1674
|
+
if (len === null) {
|
|
1675
|
+
for (let i = 0; !r.isEnd(); i++) {
|
|
1676
|
+
fieldFn(`${i}`, () => res.push(inner.decodeStream(r)));
|
|
1677
|
+
if (inner.size && r.leftBytes < inner.size)
|
|
1678
|
+
break;
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
else if (isBytes(len)) {
|
|
1682
|
+
for (let i = 0;; i++) {
|
|
1683
|
+
if (equalBytes(r.bytes(len.length, true), len)) {
|
|
1684
|
+
// Advance cursor position if terminator found
|
|
1685
|
+
r.bytes(len.length);
|
|
1686
|
+
break;
|
|
1687
|
+
}
|
|
1688
|
+
fieldFn(`${i}`, () => res.push(inner.decodeStream(r)));
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
else {
|
|
1692
|
+
let length;
|
|
1693
|
+
fieldFn('arrayLen', () => (length = _length.decodeStream(r)));
|
|
1694
|
+
for (let i = 0; i < length; i++)
|
|
1695
|
+
fieldFn(`${i}`, () => res.push(inner.decodeStream(r)));
|
|
1696
|
+
}
|
|
1697
|
+
});
|
|
1698
|
+
return res;
|
|
1699
|
+
},
|
|
1700
|
+
validate: (value) => {
|
|
1701
|
+
if (!Array.isArray(value))
|
|
1702
|
+
throw new Error(`array: invalid value ${value}`);
|
|
1703
|
+
return value;
|
|
1704
|
+
},
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Mapping between encoded values and string representations.
|
|
1709
|
+
* @param inner - CoderType for encoded values.
|
|
1710
|
+
* @param variants - Object mapping string representations to encoded values.
|
|
1711
|
+
* @returns CoderType representing the mapping.
|
|
1712
|
+
* @example
|
|
1713
|
+
* // Map between numbers and strings
|
|
1714
|
+
* const numberMap = P.map(P.U8, {
|
|
1715
|
+
* 'one': 1,
|
|
1716
|
+
* 'two': 2,
|
|
1717
|
+
* 'three': 3
|
|
1718
|
+
* });
|
|
1719
|
+
*
|
|
1720
|
+
* // Map between byte arrays and strings
|
|
1721
|
+
* const byteMap = P.map(P.bytes(2, false), {
|
|
1722
|
+
* 'ab': Uint8Array.from([0x61, 0x62]),
|
|
1723
|
+
* 'cd': Uint8Array.from([0x63, 0x64])
|
|
1724
|
+
* });
|
|
1725
|
+
*/
|
|
1726
|
+
export function map(inner, variants) {
|
|
1727
|
+
if (!isCoder(inner))
|
|
1728
|
+
throw new Error(`map: invalid inner value ${inner}`);
|
|
1729
|
+
if (!isPlainObject(variants))
|
|
1730
|
+
throw new Error(`map: variants should be plain object`);
|
|
1731
|
+
const variantNames = new Map();
|
|
1732
|
+
for (const k in variants)
|
|
1733
|
+
variantNames.set(variants[k], k);
|
|
1734
|
+
return wrap({
|
|
1735
|
+
size: inner.size,
|
|
1736
|
+
encodeStream: (w, value) => inner.encodeStream(w, variants[value]),
|
|
1737
|
+
decodeStream: (r) => {
|
|
1738
|
+
const variant = inner.decodeStream(r);
|
|
1739
|
+
const name = variantNames.get(variant);
|
|
1740
|
+
if (name === undefined)
|
|
1741
|
+
throw r.err(`Enum: unknown value: ${variant} ${Array.from(variantNames.keys())}`);
|
|
1742
|
+
return name;
|
|
1743
|
+
},
|
|
1744
|
+
validate: (value) => {
|
|
1745
|
+
if (typeof value !== 'string')
|
|
1746
|
+
throw new Error(`map: invalid value ${value}`);
|
|
1747
|
+
if (!(value in variants))
|
|
1748
|
+
throw new Error(`Map: unknown variant: ${value}`);
|
|
1749
|
+
return value;
|
|
1750
|
+
},
|
|
1751
|
+
});
|
|
1752
|
+
}
|
|
1753
|
+
/**
|
|
1754
|
+
* Tagged union of CoderTypes, where the tag value determines which CoderType to use.
|
|
1755
|
+
* The decoded value will have the structure `{ TAG: number, data: ... }`.
|
|
1756
|
+
* @param tag - CoderType for the tag value.
|
|
1757
|
+
* @param variants - Object mapping tag values to CoderTypes.
|
|
1758
|
+
* @returns CoderType representing the tagged union.
|
|
1759
|
+
* @example
|
|
1760
|
+
* // Tagged union of array, string, and number
|
|
1761
|
+
* // Depending on the value of the first byte, it will be decoded as an array, string, or number.
|
|
1762
|
+
* const taggedUnion = P.tag(P.U8, {
|
|
1763
|
+
* 0x01: P.array(P.U16LE, P.U8),
|
|
1764
|
+
* 0x02: P.string(P.U8),
|
|
1765
|
+
* 0x03: P.U32BE
|
|
1766
|
+
* });
|
|
1767
|
+
*
|
|
1768
|
+
* const encoded = taggedUnion.encode({ TAG: 0x01, data: 'hello' }); // Encodes the string 'hello' with tag 0x01
|
|
1769
|
+
* const decoded = taggedUnion.decode(encoded); // Decodes the encoded value back to { TAG: 0x01, data: 'hello' }
|
|
1770
|
+
*/
|
|
1771
|
+
export function tag(tag, variants) {
|
|
1772
|
+
if (!isCoder(tag))
|
|
1773
|
+
throw new Error(`tag: invalid tag value ${tag}`);
|
|
1774
|
+
if (!isPlainObject(variants))
|
|
1775
|
+
throw new Error(`tag: variants should be plain object`);
|
|
1776
|
+
for (const name in variants) {
|
|
1777
|
+
if (!isCoder(variants[name]))
|
|
1778
|
+
throw new Error(`tag: variant ${name} is not CoderType`);
|
|
1779
|
+
}
|
|
1780
|
+
return wrap({
|
|
1781
|
+
size: tag.size,
|
|
1782
|
+
encodeStream: (w, value) => {
|
|
1783
|
+
const { TAG, data } = value;
|
|
1784
|
+
const dataType = variants[TAG];
|
|
1785
|
+
tag.encodeStream(w, TAG);
|
|
1786
|
+
dataType.encodeStream(w, data);
|
|
1787
|
+
},
|
|
1788
|
+
decodeStream: (r) => {
|
|
1789
|
+
const TAG = tag.decodeStream(r);
|
|
1790
|
+
const dataType = variants[TAG];
|
|
1791
|
+
if (!dataType)
|
|
1792
|
+
throw r.err(`Tag: invalid tag ${TAG}`);
|
|
1793
|
+
return { TAG, data: dataType.decodeStream(r) };
|
|
1794
|
+
},
|
|
1795
|
+
validate: (value) => {
|
|
1796
|
+
const { TAG } = value;
|
|
1797
|
+
const dataType = variants[TAG];
|
|
1798
|
+
if (!dataType)
|
|
1799
|
+
throw new Error(`Tag: invalid tag ${TAG.toString()}`);
|
|
1800
|
+
return value;
|
|
1801
|
+
},
|
|
1802
|
+
});
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Mapping between encoded values, string representations, and CoderTypes using a tag CoderType.
|
|
1806
|
+
* @param tagCoder - CoderType for the tag value.
|
|
1807
|
+
* @param variants - Object mapping string representations to [tag value, CoderType] pairs.
|
|
1808
|
+
* * @returns CoderType representing the mapping.
|
|
1809
|
+
* @example
|
|
1810
|
+
* const cborValue: P.CoderType<CborValue> = P.mappedTag(P.bits(3), {
|
|
1811
|
+
* uint: [0, cborUint], // An unsigned integer in the range 0..264-1 inclusive.
|
|
1812
|
+
* negint: [1, cborNegint], // A negative integer in the range -264..-1 inclusive
|
|
1813
|
+
* bytes: [2, P.lazy(() => cborLength(P.bytes, cborValue))], // A byte string.
|
|
1814
|
+
* string: [3, P.lazy(() => cborLength(P.string, cborValue))], // A text string (utf8)
|
|
1815
|
+
* array: [4, cborArrLength(P.lazy(() => cborValue))], // An array of data items
|
|
1816
|
+
* map: [5, P.lazy(() => cborArrLength(P.tuple([cborValue, cborValue])))], // A map of pairs of data items
|
|
1817
|
+
* tag: [6, P.tuple([cborUint, P.lazy(() => cborValue)] as const)], // A tagged data item ("tag") whose tag number
|
|
1818
|
+
* simple: [7, cborSimple], // Floating-point numbers and simple values, as well as the "break" stop code
|
|
1819
|
+
* });
|
|
1820
|
+
*/
|
|
1821
|
+
export function mappedTag(tagCoder, variants) {
|
|
1822
|
+
if (!isCoder(tagCoder))
|
|
1823
|
+
throw new Error(`mappedTag: invalid tag value ${tag}`);
|
|
1824
|
+
if (!isPlainObject(variants))
|
|
1825
|
+
throw new Error(`mappedTag: variants should be plain object`);
|
|
1826
|
+
const mapValue = {};
|
|
1827
|
+
const tagValue = {};
|
|
1828
|
+
for (const key in variants) {
|
|
1829
|
+
const v = variants[key];
|
|
1830
|
+
mapValue[key] = v[0];
|
|
1831
|
+
tagValue[key] = v[1];
|
|
1832
|
+
}
|
|
1833
|
+
return tag(map(tagCoder, mapValue), tagValue);
|
|
1834
|
+
}
|
|
1835
|
+
/**
|
|
1836
|
+
* Bitset of boolean values with optional padding.
|
|
1837
|
+
* @param names - An array of string names for the bitset values.
|
|
1838
|
+
* @param pad - Whether to pad the bitset to a multiple of 8 bits.
|
|
1839
|
+
* @returns CoderType representing the bitset.
|
|
1840
|
+
* @template Names
|
|
1841
|
+
* @example
|
|
1842
|
+
* const myBitset = P.bitset(['flag1', 'flag2', 'flag3', 'flag4'], true);
|
|
1843
|
+
*/
|
|
1844
|
+
export function bitset(names, pad = false) {
|
|
1845
|
+
if (typeof pad !== 'boolean')
|
|
1846
|
+
throw new Error(`bitset/pad: expected boolean, got ${typeof pad}`);
|
|
1847
|
+
if (!Array.isArray(names))
|
|
1848
|
+
throw new Error('bitset/names: expected array');
|
|
1849
|
+
for (const name of names) {
|
|
1850
|
+
if (typeof name !== 'string')
|
|
1851
|
+
throw new Error('bitset/names: expected array of strings');
|
|
1852
|
+
}
|
|
1853
|
+
return wrap({
|
|
1854
|
+
encodeStream: (w, value) => {
|
|
1855
|
+
for (let i = 0; i < names.length; i++)
|
|
1856
|
+
w.bits(+value[names[i]], 1);
|
|
1857
|
+
if (pad && names.length % 8)
|
|
1858
|
+
w.bits(0, 8 - (names.length % 8));
|
|
1859
|
+
},
|
|
1860
|
+
decodeStream: (r) => {
|
|
1861
|
+
const out = {};
|
|
1862
|
+
for (let i = 0; i < names.length; i++)
|
|
1863
|
+
out[names[i]] = !!r.bits(1);
|
|
1864
|
+
if (pad && names.length % 8)
|
|
1865
|
+
r.bits(8 - (names.length % 8));
|
|
1866
|
+
return out;
|
|
1867
|
+
},
|
|
1868
|
+
validate: (value) => {
|
|
1869
|
+
if (!isPlainObject(value))
|
|
1870
|
+
throw new Error(`bitset: invalid value ${value}`);
|
|
1871
|
+
for (const v of Object.values(value)) {
|
|
1872
|
+
if (typeof v !== 'boolean')
|
|
1873
|
+
throw new Error('expected boolean');
|
|
1874
|
+
}
|
|
1875
|
+
return value;
|
|
1876
|
+
},
|
|
1877
|
+
});
|
|
1878
|
+
}
|
|
1879
|
+
/** Padding function which always returns zero */
|
|
1880
|
+
export const ZeroPad = (_) => 0;
|
|
1881
|
+
function padLength(blockSize, len) {
|
|
1882
|
+
if (len % blockSize === 0)
|
|
1883
|
+
return 0;
|
|
1884
|
+
return blockSize - (len % blockSize);
|
|
1885
|
+
}
|
|
1886
|
+
/**
|
|
1887
|
+
* Pads a CoderType with a specified block size and padding function on the left side.
|
|
1888
|
+
* @param blockSize - Block size for padding (positive safe integer).
|
|
1889
|
+
* @param inner - Inner CoderType to pad.
|
|
1890
|
+
* @param padFn - Padding function to use. If not provided, zero padding is used.
|
|
1891
|
+
* @returns CoderType representing the padded value.
|
|
1892
|
+
* @example
|
|
1893
|
+
* // Pad a U32BE with a block size of 4 and zero padding
|
|
1894
|
+
* const paddedU32BE = P.padLeft(4, P.U32BE);
|
|
1895
|
+
*
|
|
1896
|
+
* // Pad a string with a block size of 16 and custom padding
|
|
1897
|
+
* const paddedString = P.padLeft(16, P.string(P.U8), (i) => i + 1);
|
|
1898
|
+
*/
|
|
1899
|
+
export function padLeft(blockSize, inner, padFn) {
|
|
1900
|
+
if (!isNum(blockSize) || blockSize <= 0)
|
|
1901
|
+
throw new Error(`padLeft: wrong blockSize=${blockSize}`);
|
|
1902
|
+
if (!isCoder(inner))
|
|
1903
|
+
throw new Error(`padLeft: invalid inner value ${inner}`);
|
|
1904
|
+
if (padFn !== undefined && typeof padFn !== 'function')
|
|
1905
|
+
throw new Error(`padLeft: wrong padFn=${typeof padFn}`);
|
|
1906
|
+
const _padFn = padFn || ZeroPad;
|
|
1907
|
+
if (!inner.size)
|
|
1908
|
+
throw new Error('padLeft cannot have dynamic size');
|
|
1909
|
+
return wrap({
|
|
1910
|
+
size: inner.size + padLength(blockSize, inner.size),
|
|
1911
|
+
encodeStream: (w, value) => {
|
|
1912
|
+
const padBytes = padLength(blockSize, inner.size);
|
|
1913
|
+
for (let i = 0; i < padBytes; i++)
|
|
1914
|
+
w.byte(_padFn(i));
|
|
1915
|
+
inner.encodeStream(w, value);
|
|
1916
|
+
},
|
|
1917
|
+
decodeStream: (r) => {
|
|
1918
|
+
r.bytes(padLength(blockSize, inner.size));
|
|
1919
|
+
return inner.decodeStream(r);
|
|
1920
|
+
},
|
|
1921
|
+
});
|
|
1922
|
+
}
|
|
1923
|
+
/**
|
|
1924
|
+
* Pads a CoderType with a specified block size and padding function on the right side.
|
|
1925
|
+
* @param blockSize - Block size for padding (positive safe integer).
|
|
1926
|
+
* @param inner - Inner CoderType to pad.
|
|
1927
|
+
* @param padFn - Padding function to use. If not provided, zero padding is used.
|
|
1928
|
+
* @returns CoderType representing the padded value.
|
|
1929
|
+
* @example
|
|
1930
|
+
* // Pad a U16BE with a block size of 2 and zero padding
|
|
1931
|
+
* const paddedU16BE = P.padRight(2, P.U16BE);
|
|
1932
|
+
*
|
|
1933
|
+
* // Pad a bytes with a block size of 8 and custom padding
|
|
1934
|
+
* const paddedBytes = P.padRight(8, P.bytes(null), (i) => i + 1);
|
|
1935
|
+
*/
|
|
1936
|
+
export function padRight(blockSize, inner, padFn) {
|
|
1937
|
+
if (!isCoder(inner))
|
|
1938
|
+
throw new Error(`padRight: invalid inner value ${inner}`);
|
|
1939
|
+
if (!isNum(blockSize) || blockSize <= 0)
|
|
1940
|
+
throw new Error(`padLeft: wrong blockSize=${blockSize}`);
|
|
1941
|
+
if (padFn !== undefined && typeof padFn !== 'function')
|
|
1942
|
+
throw new Error(`padRight: wrong padFn=${typeof padFn}`);
|
|
1943
|
+
const _padFn = padFn || ZeroPad;
|
|
1944
|
+
return wrap({
|
|
1945
|
+
size: inner.size ? inner.size + padLength(blockSize, inner.size) : undefined,
|
|
1946
|
+
encodeStream: (w, value) => {
|
|
1947
|
+
const _w = w;
|
|
1948
|
+
const pos = _w.pos;
|
|
1949
|
+
inner.encodeStream(w, value);
|
|
1950
|
+
const padBytes = padLength(blockSize, _w.pos - pos);
|
|
1951
|
+
for (let i = 0; i < padBytes; i++)
|
|
1952
|
+
w.byte(_padFn(i));
|
|
1953
|
+
},
|
|
1954
|
+
decodeStream: (r) => {
|
|
1955
|
+
const start = r.pos;
|
|
1956
|
+
const res = inner.decodeStream(r);
|
|
1957
|
+
r.bytes(padLength(blockSize, r.pos - start));
|
|
1958
|
+
return res;
|
|
1959
|
+
},
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
1;
|
|
1963
|
+
/**
|
|
1964
|
+
* Pointer to a value using a pointer CoderType and an inner CoderType.
|
|
1965
|
+
* Pointers are scoped, and the next pointer in the dereference chain is offset by the previous one.
|
|
1966
|
+
* By default (if no 'allowMultipleReads' in ReaderOpts is set) is safe, since
|
|
1967
|
+
* same region of memory cannot be read multiple times.
|
|
1968
|
+
* @param ptr - CoderType for the pointer value.
|
|
1969
|
+
* @param inner - CoderType for encoding/decoding the pointed value.
|
|
1970
|
+
* @param sized - Whether the pointer should have a fixed size.
|
|
1971
|
+
* @returns CoderType representing the pointer to the value.
|
|
1972
|
+
* @example
|
|
1973
|
+
* const pointerToU8 = P.pointer(P.U16BE, P.U8); // Pointer to a single U8 value
|
|
1974
|
+
*/
|
|
1975
|
+
export function pointer(ptr, inner, sized = false) {
|
|
1976
|
+
if (!isCoder(ptr))
|
|
1977
|
+
throw new Error(`pointer: invalid ptr value ${ptr}`);
|
|
1978
|
+
if (!isCoder(inner))
|
|
1979
|
+
throw new Error(`pointer: invalid inner value ${inner}`);
|
|
1980
|
+
if (typeof sized !== 'boolean')
|
|
1981
|
+
throw new Error(`pointer/sized: expected boolean, got ${typeof sized}`);
|
|
1982
|
+
if (!ptr.size)
|
|
1983
|
+
throw new Error('unsized pointer');
|
|
1984
|
+
return wrap({
|
|
1985
|
+
size: sized ? ptr.size : undefined,
|
|
1986
|
+
encodeStream: (w, value) => {
|
|
1987
|
+
const _w = w;
|
|
1988
|
+
const start = _w.pos;
|
|
1989
|
+
ptr.encodeStream(w, 0);
|
|
1990
|
+
_w.ptrs.push({ pos: start, ptr, buffer: inner.encode(value) });
|
|
1991
|
+
},
|
|
1992
|
+
decodeStream: (r) => {
|
|
1993
|
+
const ptrVal = ptr.decodeStream(r);
|
|
1994
|
+
r._enablePointers();
|
|
1995
|
+
return inner.decodeStream(r.offsetReader(ptrVal));
|
|
1996
|
+
},
|
|
1997
|
+
});
|
|
1998
|
+
}
|
|
1999
|
+
// Internal methods for test purposes only
|
|
2000
|
+
export const _TEST = { _bitset: Bitset, _Reader, _Writer, Path };
|
|
2001
|
+
//# sourceMappingURL=index.js.map
|