diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b80ded0..499a3f3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,7 +14,7 @@ jobs: - uses: actions/checkout@v2 - run: yarn - run: yarn lint - # - uses: gozala/typescript-error-reporter-action@v1.0.4 + - uses: gozala/typescript-error-reporter-action@v1.0.4 - run: yarn build - run: yarn aegir dep-check - uses: ipfs/aegir/actions/bundle-size@master diff --git a/README.md b/README.md index 3e9f286..70d3142 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,6 @@ > npm install multicodec ``` -The type definitions for this package are available on http://definitelytyped.org/. To install just use: - - ```sh - $ npm install -D @types/multicodec - ``` - ## Usage ### Example diff --git a/example.js b/example.js deleted file mode 100644 index 7413d49..0000000 --- a/example.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const multicodec = require('multicodec') - -const prefixedProtobuf = multicodec.addPrefix('protobuf', new TextEncoder().encode('some protobuf code')) - -// eslint-disable-next-line no-console -console.log(prefixedProtobuf) -// => prefixedProtobuf 0x50... diff --git a/package.json b/package.json index 6c9b306..2686f9a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,15 @@ "description": "JavaScript implementation of the multicodec specification", "leadMaintainer": "Henrique Dias ", "main": "src/index.js", + "types": "dist/src/index.d.ts", + "typesVersions": { + "*": { + "src/*": [ + "dist/src/*", + "dist/src/*/index" + ] + } + }, "scripts": { "lint": "aegir lint", "test": "aegir test", diff --git a/src/base-table.js b/src/base-table.js new file mode 100644 index 0000000..275cac9 --- /dev/null +++ b/src/base-table.js @@ -0,0 +1,471 @@ +// DO NOT CHANGE THIS FILE. IT IS GENERATED BY tools/update-table.js +/* eslint quote-props: off */ +'use strict' + +/** + * @type {import('./generated-types').NameNumberMap} + */ +const baseTable = Object.freeze({ + 'identity': 0x00, + 'cidv1': 0x01, + 'cidv2': 0x02, + 'cidv3': 0x03, + 'ip4': 0x04, + 'tcp': 0x06, + 'sha1': 0x11, + 'sha2-256': 0x12, + 'sha2-512': 0x13, + 'sha3-512': 0x14, + 'sha3-384': 0x15, + 'sha3-256': 0x16, + 'sha3-224': 0x17, + 'shake-128': 0x18, + 'shake-256': 0x19, + 'keccak-224': 0x1a, + 'keccak-256': 0x1b, + 'keccak-384': 0x1c, + 'keccak-512': 0x1d, + 'blake3': 0x1e, + 'dccp': 0x21, + 'murmur3-128': 0x22, + 'murmur3-32': 0x23, + 'ip6': 0x29, + 'ip6zone': 0x2a, + 'path': 0x2f, + 'multicodec': 0x30, + 'multihash': 0x31, + 'multiaddr': 0x32, + 'multibase': 0x33, + 'dns': 0x35, + 'dns4': 0x36, + 'dns6': 0x37, + 'dnsaddr': 0x38, + 'protobuf': 0x50, + 'cbor': 0x51, + 'raw': 0x55, + 'dbl-sha2-256': 0x56, + 'rlp': 0x60, + 'bencode': 0x63, + 'dag-pb': 0x70, + 'dag-cbor': 0x71, + 'libp2p-key': 0x72, + 'git-raw': 0x78, + 'torrent-info': 0x7b, + 'torrent-file': 0x7c, + 'leofcoin-block': 0x81, + 'leofcoin-tx': 0x82, + 'leofcoin-pr': 0x83, + 'sctp': 0x84, + 'dag-jose': 0x85, + 'dag-cose': 0x86, + 'eth-block': 0x90, + 'eth-block-list': 0x91, + 'eth-tx-trie': 0x92, + 'eth-tx': 0x93, + 'eth-tx-receipt-trie': 0x94, + 'eth-tx-receipt': 0x95, + 'eth-state-trie': 0x96, + 'eth-account-snapshot': 0x97, + 'eth-storage-trie': 0x98, + 'bitcoin-block': 0xb0, + 'bitcoin-tx': 0xb1, + 'bitcoin-witness-commitment': 0xb2, + 'zcash-block': 0xc0, + 'zcash-tx': 0xc1, + 'docid': 0xce, + 'stellar-block': 0xd0, + 'stellar-tx': 0xd1, + 'md4': 0xd4, + 'md5': 0xd5, + 'bmt': 0xd6, + 'decred-block': 0xe0, + 'decred-tx': 0xe1, + 'ipld-ns': 0xe2, + 'ipfs-ns': 0xe3, + 'swarm-ns': 0xe4, + 'ipns-ns': 0xe5, + 'zeronet': 0xe6, + 'secp256k1-pub': 0xe7, + 'bls12_381-g1-pub': 0xea, + 'bls12_381-g2-pub': 0xeb, + 'x25519-pub': 0xec, + 'ed25519-pub': 0xed, + 'bls12_381-g1g2-pub': 0xee, + 'dash-block': 0xf0, + 'dash-tx': 0xf1, + 'swarm-manifest': 0xfa, + 'swarm-feed': 0xfb, + 'udp': 0x0111, + 'p2p-webrtc-star': 0x0113, + 'p2p-webrtc-direct': 0x0114, + 'p2p-stardust': 0x0115, + 'p2p-circuit': 0x0122, + 'dag-json': 0x0129, + 'udt': 0x012d, + 'utp': 0x012e, + 'unix': 0x0190, + 'p2p': 0x01a5, + 'ipfs': 0x01a5, + 'https': 0x01bb, + 'onion': 0x01bc, + 'onion3': 0x01bd, + 'garlic64': 0x01be, + 'garlic32': 0x01bf, + 'tls': 0x01c0, + 'quic': 0x01cc, + 'ws': 0x01dd, + 'wss': 0x01de, + 'p2p-websocket-star': 0x01df, + 'http': 0x01e0, + 'json': 0x0200, + 'messagepack': 0x0201, + 'libp2p-peer-record': 0x0301, + 'sha2-256-trunc254-padded': 0x1012, + 'ripemd-128': 0x1052, + 'ripemd-160': 0x1053, + 'ripemd-256': 0x1054, + 'ripemd-320': 0x1055, + 'x11': 0x1100, + 'p256-pub': 0x1200, + 'p384-pub': 0x1201, + 'p521-pub': 0x1202, + 'ed448-pub': 0x1203, + 'x448-pub': 0x1204, + 'ed25519-priv': 0x1300, + 'kangarootwelve': 0x1d01, + 'sm3-256': 0x534d, + 'blake2b-8': 0xb201, + 'blake2b-16': 0xb202, + 'blake2b-24': 0xb203, + 'blake2b-32': 0xb204, + 'blake2b-40': 0xb205, + 'blake2b-48': 0xb206, + 'blake2b-56': 0xb207, + 'blake2b-64': 0xb208, + 'blake2b-72': 0xb209, + 'blake2b-80': 0xb20a, + 'blake2b-88': 0xb20b, + 'blake2b-96': 0xb20c, + 'blake2b-104': 0xb20d, + 'blake2b-112': 0xb20e, + 'blake2b-120': 0xb20f, + 'blake2b-128': 0xb210, + 'blake2b-136': 0xb211, + 'blake2b-144': 0xb212, + 'blake2b-152': 0xb213, + 'blake2b-160': 0xb214, + 'blake2b-168': 0xb215, + 'blake2b-176': 0xb216, + 'blake2b-184': 0xb217, + 'blake2b-192': 0xb218, + 'blake2b-200': 0xb219, + 'blake2b-208': 0xb21a, + 'blake2b-216': 0xb21b, + 'blake2b-224': 0xb21c, + 'blake2b-232': 0xb21d, + 'blake2b-240': 0xb21e, + 'blake2b-248': 0xb21f, + 'blake2b-256': 0xb220, + 'blake2b-264': 0xb221, + 'blake2b-272': 0xb222, + 'blake2b-280': 0xb223, + 'blake2b-288': 0xb224, + 'blake2b-296': 0xb225, + 'blake2b-304': 0xb226, + 'blake2b-312': 0xb227, + 'blake2b-320': 0xb228, + 'blake2b-328': 0xb229, + 'blake2b-336': 0xb22a, + 'blake2b-344': 0xb22b, + 'blake2b-352': 0xb22c, + 'blake2b-360': 0xb22d, + 'blake2b-368': 0xb22e, + 'blake2b-376': 0xb22f, + 'blake2b-384': 0xb230, + 'blake2b-392': 0xb231, + 'blake2b-400': 0xb232, + 'blake2b-408': 0xb233, + 'blake2b-416': 0xb234, + 'blake2b-424': 0xb235, + 'blake2b-432': 0xb236, + 'blake2b-440': 0xb237, + 'blake2b-448': 0xb238, + 'blake2b-456': 0xb239, + 'blake2b-464': 0xb23a, + 'blake2b-472': 0xb23b, + 'blake2b-480': 0xb23c, + 'blake2b-488': 0xb23d, + 'blake2b-496': 0xb23e, + 'blake2b-504': 0xb23f, + 'blake2b-512': 0xb240, + 'blake2s-8': 0xb241, + 'blake2s-16': 0xb242, + 'blake2s-24': 0xb243, + 'blake2s-32': 0xb244, + 'blake2s-40': 0xb245, + 'blake2s-48': 0xb246, + 'blake2s-56': 0xb247, + 'blake2s-64': 0xb248, + 'blake2s-72': 0xb249, + 'blake2s-80': 0xb24a, + 'blake2s-88': 0xb24b, + 'blake2s-96': 0xb24c, + 'blake2s-104': 0xb24d, + 'blake2s-112': 0xb24e, + 'blake2s-120': 0xb24f, + 'blake2s-128': 0xb250, + 'blake2s-136': 0xb251, + 'blake2s-144': 0xb252, + 'blake2s-152': 0xb253, + 'blake2s-160': 0xb254, + 'blake2s-168': 0xb255, + 'blake2s-176': 0xb256, + 'blake2s-184': 0xb257, + 'blake2s-192': 0xb258, + 'blake2s-200': 0xb259, + 'blake2s-208': 0xb25a, + 'blake2s-216': 0xb25b, + 'blake2s-224': 0xb25c, + 'blake2s-232': 0xb25d, + 'blake2s-240': 0xb25e, + 'blake2s-248': 0xb25f, + 'blake2s-256': 0xb260, + 'skein256-8': 0xb301, + 'skein256-16': 0xb302, + 'skein256-24': 0xb303, + 'skein256-32': 0xb304, + 'skein256-40': 0xb305, + 'skein256-48': 0xb306, + 'skein256-56': 0xb307, + 'skein256-64': 0xb308, + 'skein256-72': 0xb309, + 'skein256-80': 0xb30a, + 'skein256-88': 0xb30b, + 'skein256-96': 0xb30c, + 'skein256-104': 0xb30d, + 'skein256-112': 0xb30e, + 'skein256-120': 0xb30f, + 'skein256-128': 0xb310, + 'skein256-136': 0xb311, + 'skein256-144': 0xb312, + 'skein256-152': 0xb313, + 'skein256-160': 0xb314, + 'skein256-168': 0xb315, + 'skein256-176': 0xb316, + 'skein256-184': 0xb317, + 'skein256-192': 0xb318, + 'skein256-200': 0xb319, + 'skein256-208': 0xb31a, + 'skein256-216': 0xb31b, + 'skein256-224': 0xb31c, + 'skein256-232': 0xb31d, + 'skein256-240': 0xb31e, + 'skein256-248': 0xb31f, + 'skein256-256': 0xb320, + 'skein512-8': 0xb321, + 'skein512-16': 0xb322, + 'skein512-24': 0xb323, + 'skein512-32': 0xb324, + 'skein512-40': 0xb325, + 'skein512-48': 0xb326, + 'skein512-56': 0xb327, + 'skein512-64': 0xb328, + 'skein512-72': 0xb329, + 'skein512-80': 0xb32a, + 'skein512-88': 0xb32b, + 'skein512-96': 0xb32c, + 'skein512-104': 0xb32d, + 'skein512-112': 0xb32e, + 'skein512-120': 0xb32f, + 'skein512-128': 0xb330, + 'skein512-136': 0xb331, + 'skein512-144': 0xb332, + 'skein512-152': 0xb333, + 'skein512-160': 0xb334, + 'skein512-168': 0xb335, + 'skein512-176': 0xb336, + 'skein512-184': 0xb337, + 'skein512-192': 0xb338, + 'skein512-200': 0xb339, + 'skein512-208': 0xb33a, + 'skein512-216': 0xb33b, + 'skein512-224': 0xb33c, + 'skein512-232': 0xb33d, + 'skein512-240': 0xb33e, + 'skein512-248': 0xb33f, + 'skein512-256': 0xb340, + 'skein512-264': 0xb341, + 'skein512-272': 0xb342, + 'skein512-280': 0xb343, + 'skein512-288': 0xb344, + 'skein512-296': 0xb345, + 'skein512-304': 0xb346, + 'skein512-312': 0xb347, + 'skein512-320': 0xb348, + 'skein512-328': 0xb349, + 'skein512-336': 0xb34a, + 'skein512-344': 0xb34b, + 'skein512-352': 0xb34c, + 'skein512-360': 0xb34d, + 'skein512-368': 0xb34e, + 'skein512-376': 0xb34f, + 'skein512-384': 0xb350, + 'skein512-392': 0xb351, + 'skein512-400': 0xb352, + 'skein512-408': 0xb353, + 'skein512-416': 0xb354, + 'skein512-424': 0xb355, + 'skein512-432': 0xb356, + 'skein512-440': 0xb357, + 'skein512-448': 0xb358, + 'skein512-456': 0xb359, + 'skein512-464': 0xb35a, + 'skein512-472': 0xb35b, + 'skein512-480': 0xb35c, + 'skein512-488': 0xb35d, + 'skein512-496': 0xb35e, + 'skein512-504': 0xb35f, + 'skein512-512': 0xb360, + 'skein1024-8': 0xb361, + 'skein1024-16': 0xb362, + 'skein1024-24': 0xb363, + 'skein1024-32': 0xb364, + 'skein1024-40': 0xb365, + 'skein1024-48': 0xb366, + 'skein1024-56': 0xb367, + 'skein1024-64': 0xb368, + 'skein1024-72': 0xb369, + 'skein1024-80': 0xb36a, + 'skein1024-88': 0xb36b, + 'skein1024-96': 0xb36c, + 'skein1024-104': 0xb36d, + 'skein1024-112': 0xb36e, + 'skein1024-120': 0xb36f, + 'skein1024-128': 0xb370, + 'skein1024-136': 0xb371, + 'skein1024-144': 0xb372, + 'skein1024-152': 0xb373, + 'skein1024-160': 0xb374, + 'skein1024-168': 0xb375, + 'skein1024-176': 0xb376, + 'skein1024-184': 0xb377, + 'skein1024-192': 0xb378, + 'skein1024-200': 0xb379, + 'skein1024-208': 0xb37a, + 'skein1024-216': 0xb37b, + 'skein1024-224': 0xb37c, + 'skein1024-232': 0xb37d, + 'skein1024-240': 0xb37e, + 'skein1024-248': 0xb37f, + 'skein1024-256': 0xb380, + 'skein1024-264': 0xb381, + 'skein1024-272': 0xb382, + 'skein1024-280': 0xb383, + 'skein1024-288': 0xb384, + 'skein1024-296': 0xb385, + 'skein1024-304': 0xb386, + 'skein1024-312': 0xb387, + 'skein1024-320': 0xb388, + 'skein1024-328': 0xb389, + 'skein1024-336': 0xb38a, + 'skein1024-344': 0xb38b, + 'skein1024-352': 0xb38c, + 'skein1024-360': 0xb38d, + 'skein1024-368': 0xb38e, + 'skein1024-376': 0xb38f, + 'skein1024-384': 0xb390, + 'skein1024-392': 0xb391, + 'skein1024-400': 0xb392, + 'skein1024-408': 0xb393, + 'skein1024-416': 0xb394, + 'skein1024-424': 0xb395, + 'skein1024-432': 0xb396, + 'skein1024-440': 0xb397, + 'skein1024-448': 0xb398, + 'skein1024-456': 0xb399, + 'skein1024-464': 0xb39a, + 'skein1024-472': 0xb39b, + 'skein1024-480': 0xb39c, + 'skein1024-488': 0xb39d, + 'skein1024-496': 0xb39e, + 'skein1024-504': 0xb39f, + 'skein1024-512': 0xb3a0, + 'skein1024-520': 0xb3a1, + 'skein1024-528': 0xb3a2, + 'skein1024-536': 0xb3a3, + 'skein1024-544': 0xb3a4, + 'skein1024-552': 0xb3a5, + 'skein1024-560': 0xb3a6, + 'skein1024-568': 0xb3a7, + 'skein1024-576': 0xb3a8, + 'skein1024-584': 0xb3a9, + 'skein1024-592': 0xb3aa, + 'skein1024-600': 0xb3ab, + 'skein1024-608': 0xb3ac, + 'skein1024-616': 0xb3ad, + 'skein1024-624': 0xb3ae, + 'skein1024-632': 0xb3af, + 'skein1024-640': 0xb3b0, + 'skein1024-648': 0xb3b1, + 'skein1024-656': 0xb3b2, + 'skein1024-664': 0xb3b3, + 'skein1024-672': 0xb3b4, + 'skein1024-680': 0xb3b5, + 'skein1024-688': 0xb3b6, + 'skein1024-696': 0xb3b7, + 'skein1024-704': 0xb3b8, + 'skein1024-712': 0xb3b9, + 'skein1024-720': 0xb3ba, + 'skein1024-728': 0xb3bb, + 'skein1024-736': 0xb3bc, + 'skein1024-744': 0xb3bd, + 'skein1024-752': 0xb3be, + 'skein1024-760': 0xb3bf, + 'skein1024-768': 0xb3c0, + 'skein1024-776': 0xb3c1, + 'skein1024-784': 0xb3c2, + 'skein1024-792': 0xb3c3, + 'skein1024-800': 0xb3c4, + 'skein1024-808': 0xb3c5, + 'skein1024-816': 0xb3c6, + 'skein1024-824': 0xb3c7, + 'skein1024-832': 0xb3c8, + 'skein1024-840': 0xb3c9, + 'skein1024-848': 0xb3ca, + 'skein1024-856': 0xb3cb, + 'skein1024-864': 0xb3cc, + 'skein1024-872': 0xb3cd, + 'skein1024-880': 0xb3ce, + 'skein1024-888': 0xb3cf, + 'skein1024-896': 0xb3d0, + 'skein1024-904': 0xb3d1, + 'skein1024-912': 0xb3d2, + 'skein1024-920': 0xb3d3, + 'skein1024-928': 0xb3d4, + 'skein1024-936': 0xb3d5, + 'skein1024-944': 0xb3d6, + 'skein1024-952': 0xb3d7, + 'skein1024-960': 0xb3d8, + 'skein1024-968': 0xb3d9, + 'skein1024-976': 0xb3da, + 'skein1024-984': 0xb3db, + 'skein1024-992': 0xb3dc, + 'skein1024-1000': 0xb3dd, + 'skein1024-1008': 0xb3de, + 'skein1024-1016': 0xb3df, + 'skein1024-1024': 0xb3e0, + 'poseidon-bls12_381-a2-fc1': 0xb401, + 'poseidon-bls12_381-a2-fc1-sc': 0xb402, + 'zeroxcert-imprint-256': 0xce11, + 'fil-commitment-unsealed': 0xf101, + 'fil-commitment-sealed': 0xf102, + 'holochain-adr-v0': 0x807124, + 'holochain-adr-v1': 0x817124, + 'holochain-key-v0': 0x947124, + 'holochain-key-v1': 0x957124, + 'holochain-sig-v0': 0xa27124, + 'holochain-sig-v1': 0xa37124, + 'skynet-ns': 0xb19910 +}) + +module.exports = { baseTable } diff --git a/src/base-table.json b/src/base-table.json deleted file mode 100644 index 61f978a..0000000 --- a/src/base-table.json +++ /dev/null @@ -1,462 +0,0 @@ -{ - "identity": 0, - "cidv1": 1, - "cidv2": 2, - "cidv3": 3, - "ip4": 4, - "tcp": 6, - "sha1": 17, - "sha2-256": 18, - "sha2-512": 19, - "sha3-512": 20, - "sha3-384": 21, - "sha3-256": 22, - "sha3-224": 23, - "shake-128": 24, - "shake-256": 25, - "keccak-224": 26, - "keccak-256": 27, - "keccak-384": 28, - "keccak-512": 29, - "blake3": 30, - "dccp": 33, - "murmur3-128": 34, - "murmur3-32": 35, - "ip6": 41, - "ip6zone": 42, - "path": 47, - "multicodec": 48, - "multihash": 49, - "multiaddr": 50, - "multibase": 51, - "dns": 53, - "dns4": 54, - "dns6": 55, - "dnsaddr": 56, - "protobuf": 80, - "cbor": 81, - "raw": 85, - "dbl-sha2-256": 86, - "rlp": 96, - "bencode": 99, - "dag-pb": 112, - "dag-cbor": 113, - "libp2p-key": 114, - "git-raw": 120, - "torrent-info": 123, - "torrent-file": 124, - "leofcoin-block": 129, - "leofcoin-tx": 130, - "leofcoin-pr": 131, - "sctp": 132, - "dag-jose": 133, - "dag-cose": 134, - "eth-block": 144, - "eth-block-list": 145, - "eth-tx-trie": 146, - "eth-tx": 147, - "eth-tx-receipt-trie": 148, - "eth-tx-receipt": 149, - "eth-state-trie": 150, - "eth-account-snapshot": 151, - "eth-storage-trie": 152, - "bitcoin-block": 176, - "bitcoin-tx": 177, - "bitcoin-witness-commitment": 178, - "zcash-block": 192, - "zcash-tx": 193, - "docid": 206, - "stellar-block": 208, - "stellar-tx": 209, - "md4": 212, - "md5": 213, - "bmt": 214, - "decred-block": 224, - "decred-tx": 225, - "ipld-ns": 226, - "ipfs-ns": 227, - "swarm-ns": 228, - "ipns-ns": 229, - "zeronet": 230, - "secp256k1-pub": 231, - "bls12_381-g1-pub": 234, - "bls12_381-g2-pub": 235, - "x25519-pub": 236, - "ed25519-pub": 237, - "bls12_381-g1g2-pub": 238, - "dash-block": 240, - "dash-tx": 241, - "swarm-manifest": 250, - "swarm-feed": 251, - "udp": 273, - "p2p-webrtc-star": 275, - "p2p-webrtc-direct": 276, - "p2p-stardust": 277, - "p2p-circuit": 290, - "dag-json": 297, - "udt": 301, - "utp": 302, - "unix": 400, - "p2p": 421, - "ipfs": 421, - "https": 443, - "onion": 444, - "onion3": 445, - "garlic64": 446, - "garlic32": 447, - "tls": 448, - "quic": 460, - "ws": 477, - "wss": 478, - "p2p-websocket-star": 479, - "http": 480, - "json": 512, - "messagepack": 513, - "libp2p-peer-record": 769, - "sha2-256-trunc254-padded": 4114, - "ripemd-128": 4178, - "ripemd-160": 4179, - "ripemd-256": 4180, - "ripemd-320": 4181, - "x11": 4352, - "p256-pub": 4608, - "p384-pub": 4609, - "p521-pub": 4610, - "ed448-pub": 4611, - "x448-pub": 4612, - "ed25519-priv": 4864, - "kangarootwelve": 7425, - "sm3-256": 21325, - "blake2b-8": 45569, - "blake2b-16": 45570, - "blake2b-24": 45571, - "blake2b-32": 45572, - "blake2b-40": 45573, - "blake2b-48": 45574, - "blake2b-56": 45575, - "blake2b-64": 45576, - "blake2b-72": 45577, - "blake2b-80": 45578, - "blake2b-88": 45579, - "blake2b-96": 45580, - "blake2b-104": 45581, - "blake2b-112": 45582, - "blake2b-120": 45583, - "blake2b-128": 45584, - "blake2b-136": 45585, - "blake2b-144": 45586, - "blake2b-152": 45587, - "blake2b-160": 45588, - "blake2b-168": 45589, - "blake2b-176": 45590, - "blake2b-184": 45591, - "blake2b-192": 45592, - "blake2b-200": 45593, - "blake2b-208": 45594, - "blake2b-216": 45595, - "blake2b-224": 45596, - "blake2b-232": 45597, - "blake2b-240": 45598, - "blake2b-248": 45599, - "blake2b-256": 45600, - "blake2b-264": 45601, - "blake2b-272": 45602, - "blake2b-280": 45603, - "blake2b-288": 45604, - "blake2b-296": 45605, - "blake2b-304": 45606, - "blake2b-312": 45607, - "blake2b-320": 45608, - "blake2b-328": 45609, - "blake2b-336": 45610, - "blake2b-344": 45611, - "blake2b-352": 45612, - "blake2b-360": 45613, - "blake2b-368": 45614, - "blake2b-376": 45615, - "blake2b-384": 45616, - "blake2b-392": 45617, - "blake2b-400": 45618, - "blake2b-408": 45619, - "blake2b-416": 45620, - "blake2b-424": 45621, - "blake2b-432": 45622, - "blake2b-440": 45623, - "blake2b-448": 45624, - "blake2b-456": 45625, - "blake2b-464": 45626, - "blake2b-472": 45627, - "blake2b-480": 45628, - "blake2b-488": 45629, - "blake2b-496": 45630, - "blake2b-504": 45631, - "blake2b-512": 45632, - "blake2s-8": 45633, - "blake2s-16": 45634, - "blake2s-24": 45635, - "blake2s-32": 45636, - "blake2s-40": 45637, - "blake2s-48": 45638, - "blake2s-56": 45639, - "blake2s-64": 45640, - "blake2s-72": 45641, - "blake2s-80": 45642, - "blake2s-88": 45643, - "blake2s-96": 45644, - "blake2s-104": 45645, - "blake2s-112": 45646, - "blake2s-120": 45647, - "blake2s-128": 45648, - "blake2s-136": 45649, - "blake2s-144": 45650, - "blake2s-152": 45651, - "blake2s-160": 45652, - "blake2s-168": 45653, - "blake2s-176": 45654, - "blake2s-184": 45655, - "blake2s-192": 45656, - "blake2s-200": 45657, - "blake2s-208": 45658, - "blake2s-216": 45659, - "blake2s-224": 45660, - "blake2s-232": 45661, - "blake2s-240": 45662, - "blake2s-248": 45663, - "blake2s-256": 45664, - "skein256-8": 45825, - "skein256-16": 45826, - "skein256-24": 45827, - "skein256-32": 45828, - "skein256-40": 45829, - "skein256-48": 45830, - "skein256-56": 45831, - "skein256-64": 45832, - "skein256-72": 45833, - "skein256-80": 45834, - "skein256-88": 45835, - "skein256-96": 45836, - "skein256-104": 45837, - "skein256-112": 45838, - "skein256-120": 45839, - "skein256-128": 45840, - "skein256-136": 45841, - "skein256-144": 45842, - "skein256-152": 45843, - "skein256-160": 45844, - "skein256-168": 45845, - "skein256-176": 45846, - "skein256-184": 45847, - "skein256-192": 45848, - "skein256-200": 45849, - "skein256-208": 45850, - "skein256-216": 45851, - "skein256-224": 45852, - "skein256-232": 45853, - "skein256-240": 45854, - "skein256-248": 45855, - "skein256-256": 45856, - "skein512-8": 45857, - "skein512-16": 45858, - "skein512-24": 45859, - "skein512-32": 45860, - "skein512-40": 45861, - "skein512-48": 45862, - "skein512-56": 45863, - "skein512-64": 45864, - "skein512-72": 45865, - "skein512-80": 45866, - "skein512-88": 45867, - "skein512-96": 45868, - "skein512-104": 45869, - "skein512-112": 45870, - "skein512-120": 45871, - "skein512-128": 45872, - "skein512-136": 45873, - "skein512-144": 45874, - "skein512-152": 45875, - "skein512-160": 45876, - "skein512-168": 45877, - "skein512-176": 45878, - "skein512-184": 45879, - "skein512-192": 45880, - "skein512-200": 45881, - "skein512-208": 45882, - "skein512-216": 45883, - "skein512-224": 45884, - "skein512-232": 45885, - "skein512-240": 45886, - "skein512-248": 45887, - "skein512-256": 45888, - "skein512-264": 45889, - "skein512-272": 45890, - "skein512-280": 45891, - "skein512-288": 45892, - "skein512-296": 45893, - "skein512-304": 45894, - "skein512-312": 45895, - "skein512-320": 45896, - "skein512-328": 45897, - "skein512-336": 45898, - "skein512-344": 45899, - "skein512-352": 45900, - "skein512-360": 45901, - "skein512-368": 45902, - "skein512-376": 45903, - "skein512-384": 45904, - "skein512-392": 45905, - "skein512-400": 45906, - "skein512-408": 45907, - "skein512-416": 45908, - "skein512-424": 45909, - "skein512-432": 45910, - "skein512-440": 45911, - "skein512-448": 45912, - "skein512-456": 45913, - "skein512-464": 45914, - "skein512-472": 45915, - "skein512-480": 45916, - "skein512-488": 45917, - "skein512-496": 45918, - "skein512-504": 45919, - "skein512-512": 45920, - "skein1024-8": 45921, - "skein1024-16": 45922, - "skein1024-24": 45923, - "skein1024-32": 45924, - "skein1024-40": 45925, - "skein1024-48": 45926, - "skein1024-56": 45927, - "skein1024-64": 45928, - "skein1024-72": 45929, - "skein1024-80": 45930, - "skein1024-88": 45931, - "skein1024-96": 45932, - "skein1024-104": 45933, - "skein1024-112": 45934, - "skein1024-120": 45935, - "skein1024-128": 45936, - "skein1024-136": 45937, - "skein1024-144": 45938, - "skein1024-152": 45939, - "skein1024-160": 45940, - "skein1024-168": 45941, - "skein1024-176": 45942, - "skein1024-184": 45943, - "skein1024-192": 45944, - "skein1024-200": 45945, - "skein1024-208": 45946, - "skein1024-216": 45947, - "skein1024-224": 45948, - "skein1024-232": 45949, - "skein1024-240": 45950, - "skein1024-248": 45951, - "skein1024-256": 45952, - "skein1024-264": 45953, - "skein1024-272": 45954, - "skein1024-280": 45955, - "skein1024-288": 45956, - "skein1024-296": 45957, - "skein1024-304": 45958, - "skein1024-312": 45959, - "skein1024-320": 45960, - "skein1024-328": 45961, - "skein1024-336": 45962, - "skein1024-344": 45963, - "skein1024-352": 45964, - "skein1024-360": 45965, - "skein1024-368": 45966, - "skein1024-376": 45967, - "skein1024-384": 45968, - "skein1024-392": 45969, - "skein1024-400": 45970, - "skein1024-408": 45971, - "skein1024-416": 45972, - "skein1024-424": 45973, - "skein1024-432": 45974, - "skein1024-440": 45975, - "skein1024-448": 45976, - "skein1024-456": 45977, - "skein1024-464": 45978, - "skein1024-472": 45979, - "skein1024-480": 45980, - "skein1024-488": 45981, - "skein1024-496": 45982, - "skein1024-504": 45983, - "skein1024-512": 45984, - "skein1024-520": 45985, - "skein1024-528": 45986, - "skein1024-536": 45987, - "skein1024-544": 45988, - "skein1024-552": 45989, - "skein1024-560": 45990, - "skein1024-568": 45991, - "skein1024-576": 45992, - "skein1024-584": 45993, - "skein1024-592": 45994, - "skein1024-600": 45995, - "skein1024-608": 45996, - "skein1024-616": 45997, - "skein1024-624": 45998, - "skein1024-632": 45999, - "skein1024-640": 46000, - "skein1024-648": 46001, - "skein1024-656": 46002, - "skein1024-664": 46003, - "skein1024-672": 46004, - "skein1024-680": 46005, - "skein1024-688": 46006, - "skein1024-696": 46007, - "skein1024-704": 46008, - "skein1024-712": 46009, - "skein1024-720": 46010, - "skein1024-728": 46011, - "skein1024-736": 46012, - "skein1024-744": 46013, - "skein1024-752": 46014, - "skein1024-760": 46015, - "skein1024-768": 46016, - "skein1024-776": 46017, - "skein1024-784": 46018, - "skein1024-792": 46019, - "skein1024-800": 46020, - "skein1024-808": 46021, - "skein1024-816": 46022, - "skein1024-824": 46023, - "skein1024-832": 46024, - "skein1024-840": 46025, - "skein1024-848": 46026, - "skein1024-856": 46027, - "skein1024-864": 46028, - "skein1024-872": 46029, - "skein1024-880": 46030, - "skein1024-888": 46031, - "skein1024-896": 46032, - "skein1024-904": 46033, - "skein1024-912": 46034, - "skein1024-920": 46035, - "skein1024-928": 46036, - "skein1024-936": 46037, - "skein1024-944": 46038, - "skein1024-952": 46039, - "skein1024-960": 46040, - "skein1024-968": 46041, - "skein1024-976": 46042, - "skein1024-984": 46043, - "skein1024-992": 46044, - "skein1024-1000": 46045, - "skein1024-1008": 46046, - "skein1024-1016": 46047, - "skein1024-1024": 46048, - "poseidon-bls12_381-a2-fc1": 46081, - "poseidon-bls12_381-a2-fc1-sc": 46082, - "zeroxcert-imprint-256": 52753, - "fil-commitment-unsealed": 61697, - "fil-commitment-sealed": 61698, - "holochain-adr-v0": 8417572, - "holochain-adr-v1": 8483108, - "holochain-key-v0": 9728292, - "holochain-key-v1": 9793828, - "holochain-sig-v0": 10645796, - "holochain-sig-v1": 10711332, - "skynet-ns": 11639056 -} \ No newline at end of file diff --git a/src/constants.js b/src/constants.js index a0234e6..530f9fe 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,12 +1,14 @@ 'use strict' -const table = require('./base-table.json') +/** @typedef {import('./generated-types').ConstantNumberMap} ConstantNumberMap */ -// map for codecConstant -> code -const constants = {} +const { baseTable } = require('./base-table') -for (const [name, code] of Object.entries(table)) { - constants[name.toUpperCase().replace(/-/g, '_')] = code +const constants = /** @type {ConstantNumberMap} */({}) + +for (const [name, code] of Object.entries(baseTable)) { + const constant = name.toUpperCase().replace(/-/g, '_') + constants[constant] = code } module.exports = Object.freeze(constants) diff --git a/src/generated-types.ts b/src/generated-types.ts new file mode 100644 index 0000000..46e2639 --- /dev/null +++ b/src/generated-types.ts @@ -0,0 +1,21 @@ +// DO NOT CHANGE THIS FILE MANUALLY. IT IS GENERATED BY tools/update-table.js + +/** + * Constant names for all available codecs + */ +export type CodecConstant = 'IDENTITY' | 'CIDV1' | 'CIDV2' | 'CIDV3' | 'IP4' | 'TCP' | 'SHA1' | 'SHA2_256' | 'SHA2_512' | 'SHA3_512' | 'SHA3_384' | 'SHA3_256' | 'SHA3_224' | 'SHAKE_128' | 'SHAKE_256' | 'KECCAK_224' | 'KECCAK_256' | 'KECCAK_384' | 'KECCAK_512' | 'BLAKE3' | 'DCCP' | 'MURMUR3_128' | 'MURMUR3_32' | 'IP6' | 'IP6ZONE' | 'PATH' | 'MULTICODEC' | 'MULTIHASH' | 'MULTIADDR' | 'MULTIBASE' | 'DNS' | 'DNS4' | 'DNS6' | 'DNSADDR' | 'PROTOBUF' | 'CBOR' | 'RAW' | 'DBL_SHA2_256' | 'RLP' | 'BENCODE' | 'DAG_PB' | 'DAG_CBOR' | 'LIBP2P_KEY' | 'GIT_RAW' | 'TORRENT_INFO' | 'TORRENT_FILE' | 'LEOFCOIN_BLOCK' | 'LEOFCOIN_TX' | 'LEOFCOIN_PR' | 'SCTP' | 'DAG_JOSE' | 'DAG_COSE' | 'ETH_BLOCK' | 'ETH_BLOCK_LIST' | 'ETH_TX_TRIE' | 'ETH_TX' | 'ETH_TX_RECEIPT_TRIE' | 'ETH_TX_RECEIPT' | 'ETH_STATE_TRIE' | 'ETH_ACCOUNT_SNAPSHOT' | 'ETH_STORAGE_TRIE' | 'BITCOIN_BLOCK' | 'BITCOIN_TX' | 'BITCOIN_WITNESS_COMMITMENT' | 'ZCASH_BLOCK' | 'ZCASH_TX' | 'DOCID' | 'STELLAR_BLOCK' | 'STELLAR_TX' | 'MD4' | 'MD5' | 'BMT' | 'DECRED_BLOCK' | 'DECRED_TX' | 'IPLD_NS' | 'IPFS_NS' | 'SWARM_NS' | 'IPNS_NS' | 'ZERONET' | 'SECP256K1_PUB' | 'BLS12_381_G1_PUB' | 'BLS12_381_G2_PUB' | 'X25519_PUB' | 'ED25519_PUB' | 'BLS12_381_G1G2_PUB' | 'DASH_BLOCK' | 'DASH_TX' | 'SWARM_MANIFEST' | 'SWARM_FEED' | 'UDP' | 'P2P_WEBRTC_STAR' | 'P2P_WEBRTC_DIRECT' | 'P2P_STARDUST' | 'P2P_CIRCUIT' | 'DAG_JSON' | 'UDT' | 'UTP' | 'UNIX' | 'P2P' | 'IPFS' | 'HTTPS' | 'ONION' | 'ONION3' | 'GARLIC64' | 'GARLIC32' | 'TLS' | 'QUIC' | 'WS' | 'WSS' | 'P2P_WEBSOCKET_STAR' | 'HTTP' | 'JSON' | 'MESSAGEPACK' | 'LIBP2P_PEER_RECORD' | 'SHA2_256_TRUNC254_PADDED' | 'RIPEMD_128' | 'RIPEMD_160' | 'RIPEMD_256' | 'RIPEMD_320' | 'X11' | 'P256_PUB' | 'P384_PUB' | 'P521_PUB' | 'ED448_PUB' | 'X448_PUB' | 'ED25519_PRIV' | 'KANGAROOTWELVE' | 'SM3_256' | 'BLAKE2B_8' | 'BLAKE2B_16' | 'BLAKE2B_24' | 'BLAKE2B_32' | 'BLAKE2B_40' | 'BLAKE2B_48' | 'BLAKE2B_56' | 'BLAKE2B_64' | 'BLAKE2B_72' | 'BLAKE2B_80' | 'BLAKE2B_88' | 'BLAKE2B_96' | 'BLAKE2B_104' | 'BLAKE2B_112' | 'BLAKE2B_120' | 'BLAKE2B_128' | 'BLAKE2B_136' | 'BLAKE2B_144' | 'BLAKE2B_152' | 'BLAKE2B_160' | 'BLAKE2B_168' | 'BLAKE2B_176' | 'BLAKE2B_184' | 'BLAKE2B_192' | 'BLAKE2B_200' | 'BLAKE2B_208' | 'BLAKE2B_216' | 'BLAKE2B_224' | 'BLAKE2B_232' | 'BLAKE2B_240' | 'BLAKE2B_248' | 'BLAKE2B_256' | 'BLAKE2B_264' | 'BLAKE2B_272' | 'BLAKE2B_280' | 'BLAKE2B_288' | 'BLAKE2B_296' | 'BLAKE2B_304' | 'BLAKE2B_312' | 'BLAKE2B_320' | 'BLAKE2B_328' | 'BLAKE2B_336' | 'BLAKE2B_344' | 'BLAKE2B_352' | 'BLAKE2B_360' | 'BLAKE2B_368' | 'BLAKE2B_376' | 'BLAKE2B_384' | 'BLAKE2B_392' | 'BLAKE2B_400' | 'BLAKE2B_408' | 'BLAKE2B_416' | 'BLAKE2B_424' | 'BLAKE2B_432' | 'BLAKE2B_440' | 'BLAKE2B_448' | 'BLAKE2B_456' | 'BLAKE2B_464' | 'BLAKE2B_472' | 'BLAKE2B_480' | 'BLAKE2B_488' | 'BLAKE2B_496' | 'BLAKE2B_504' | 'BLAKE2B_512' | 'BLAKE2S_8' | 'BLAKE2S_16' | 'BLAKE2S_24' | 'BLAKE2S_32' | 'BLAKE2S_40' | 'BLAKE2S_48' | 'BLAKE2S_56' | 'BLAKE2S_64' | 'BLAKE2S_72' | 'BLAKE2S_80' | 'BLAKE2S_88' | 'BLAKE2S_96' | 'BLAKE2S_104' | 'BLAKE2S_112' | 'BLAKE2S_120' | 'BLAKE2S_128' | 'BLAKE2S_136' | 'BLAKE2S_144' | 'BLAKE2S_152' | 'BLAKE2S_160' | 'BLAKE2S_168' | 'BLAKE2S_176' | 'BLAKE2S_184' | 'BLAKE2S_192' | 'BLAKE2S_200' | 'BLAKE2S_208' | 'BLAKE2S_216' | 'BLAKE2S_224' | 'BLAKE2S_232' | 'BLAKE2S_240' | 'BLAKE2S_248' | 'BLAKE2S_256' | 'SKEIN256_8' | 'SKEIN256_16' | 'SKEIN256_24' | 'SKEIN256_32' | 'SKEIN256_40' | 'SKEIN256_48' | 'SKEIN256_56' | 'SKEIN256_64' | 'SKEIN256_72' | 'SKEIN256_80' | 'SKEIN256_88' | 'SKEIN256_96' | 'SKEIN256_104' | 'SKEIN256_112' | 'SKEIN256_120' | 'SKEIN256_128' | 'SKEIN256_136' | 'SKEIN256_144' | 'SKEIN256_152' | 'SKEIN256_160' | 'SKEIN256_168' | 'SKEIN256_176' | 'SKEIN256_184' | 'SKEIN256_192' | 'SKEIN256_200' | 'SKEIN256_208' | 'SKEIN256_216' | 'SKEIN256_224' | 'SKEIN256_232' | 'SKEIN256_240' | 'SKEIN256_248' | 'SKEIN256_256' | 'SKEIN512_8' | 'SKEIN512_16' | 'SKEIN512_24' | 'SKEIN512_32' | 'SKEIN512_40' | 'SKEIN512_48' | 'SKEIN512_56' | 'SKEIN512_64' | 'SKEIN512_72' | 'SKEIN512_80' | 'SKEIN512_88' | 'SKEIN512_96' | 'SKEIN512_104' | 'SKEIN512_112' | 'SKEIN512_120' | 'SKEIN512_128' | 'SKEIN512_136' | 'SKEIN512_144' | 'SKEIN512_152' | 'SKEIN512_160' | 'SKEIN512_168' | 'SKEIN512_176' | 'SKEIN512_184' | 'SKEIN512_192' | 'SKEIN512_200' | 'SKEIN512_208' | 'SKEIN512_216' | 'SKEIN512_224' | 'SKEIN512_232' | 'SKEIN512_240' | 'SKEIN512_248' | 'SKEIN512_256' | 'SKEIN512_264' | 'SKEIN512_272' | 'SKEIN512_280' | 'SKEIN512_288' | 'SKEIN512_296' | 'SKEIN512_304' | 'SKEIN512_312' | 'SKEIN512_320' | 'SKEIN512_328' | 'SKEIN512_336' | 'SKEIN512_344' | 'SKEIN512_352' | 'SKEIN512_360' | 'SKEIN512_368' | 'SKEIN512_376' | 'SKEIN512_384' | 'SKEIN512_392' | 'SKEIN512_400' | 'SKEIN512_408' | 'SKEIN512_416' | 'SKEIN512_424' | 'SKEIN512_432' | 'SKEIN512_440' | 'SKEIN512_448' | 'SKEIN512_456' | 'SKEIN512_464' | 'SKEIN512_472' | 'SKEIN512_480' | 'SKEIN512_488' | 'SKEIN512_496' | 'SKEIN512_504' | 'SKEIN512_512' | 'SKEIN1024_8' | 'SKEIN1024_16' | 'SKEIN1024_24' | 'SKEIN1024_32' | 'SKEIN1024_40' | 'SKEIN1024_48' | 'SKEIN1024_56' | 'SKEIN1024_64' | 'SKEIN1024_72' | 'SKEIN1024_80' | 'SKEIN1024_88' | 'SKEIN1024_96' | 'SKEIN1024_104' | 'SKEIN1024_112' | 'SKEIN1024_120' | 'SKEIN1024_128' | 'SKEIN1024_136' | 'SKEIN1024_144' | 'SKEIN1024_152' | 'SKEIN1024_160' | 'SKEIN1024_168' | 'SKEIN1024_176' | 'SKEIN1024_184' | 'SKEIN1024_192' | 'SKEIN1024_200' | 'SKEIN1024_208' | 'SKEIN1024_216' | 'SKEIN1024_224' | 'SKEIN1024_232' | 'SKEIN1024_240' | 'SKEIN1024_248' | 'SKEIN1024_256' | 'SKEIN1024_264' | 'SKEIN1024_272' | 'SKEIN1024_280' | 'SKEIN1024_288' | 'SKEIN1024_296' | 'SKEIN1024_304' | 'SKEIN1024_312' | 'SKEIN1024_320' | 'SKEIN1024_328' | 'SKEIN1024_336' | 'SKEIN1024_344' | 'SKEIN1024_352' | 'SKEIN1024_360' | 'SKEIN1024_368' | 'SKEIN1024_376' | 'SKEIN1024_384' | 'SKEIN1024_392' | 'SKEIN1024_400' | 'SKEIN1024_408' | 'SKEIN1024_416' | 'SKEIN1024_424' | 'SKEIN1024_432' | 'SKEIN1024_440' | 'SKEIN1024_448' | 'SKEIN1024_456' | 'SKEIN1024_464' | 'SKEIN1024_472' | 'SKEIN1024_480' | 'SKEIN1024_488' | 'SKEIN1024_496' | 'SKEIN1024_504' | 'SKEIN1024_512' | 'SKEIN1024_520' | 'SKEIN1024_528' | 'SKEIN1024_536' | 'SKEIN1024_544' | 'SKEIN1024_552' | 'SKEIN1024_560' | 'SKEIN1024_568' | 'SKEIN1024_576' | 'SKEIN1024_584' | 'SKEIN1024_592' | 'SKEIN1024_600' | 'SKEIN1024_608' | 'SKEIN1024_616' | 'SKEIN1024_624' | 'SKEIN1024_632' | 'SKEIN1024_640' | 'SKEIN1024_648' | 'SKEIN1024_656' | 'SKEIN1024_664' | 'SKEIN1024_672' | 'SKEIN1024_680' | 'SKEIN1024_688' | 'SKEIN1024_696' | 'SKEIN1024_704' | 'SKEIN1024_712' | 'SKEIN1024_720' | 'SKEIN1024_728' | 'SKEIN1024_736' | 'SKEIN1024_744' | 'SKEIN1024_752' | 'SKEIN1024_760' | 'SKEIN1024_768' | 'SKEIN1024_776' | 'SKEIN1024_784' | 'SKEIN1024_792' | 'SKEIN1024_800' | 'SKEIN1024_808' | 'SKEIN1024_816' | 'SKEIN1024_824' | 'SKEIN1024_832' | 'SKEIN1024_840' | 'SKEIN1024_848' | 'SKEIN1024_856' | 'SKEIN1024_864' | 'SKEIN1024_872' | 'SKEIN1024_880' | 'SKEIN1024_888' | 'SKEIN1024_896' | 'SKEIN1024_904' | 'SKEIN1024_912' | 'SKEIN1024_920' | 'SKEIN1024_928' | 'SKEIN1024_936' | 'SKEIN1024_944' | 'SKEIN1024_952' | 'SKEIN1024_960' | 'SKEIN1024_968' | 'SKEIN1024_976' | 'SKEIN1024_984' | 'SKEIN1024_992' | 'SKEIN1024_1000' | 'SKEIN1024_1008' | 'SKEIN1024_1016' | 'SKEIN1024_1024' | 'POSEIDON_BLS12_381_A2_FC1' | 'POSEIDON_BLS12_381_A2_FC1_SC' | 'ZEROXCERT_IMPRINT_256' | 'FIL_COMMITMENT_UNSEALED' | 'FIL_COMMITMENT_SEALED' | 'HOLOCHAIN_ADR_V0' | 'HOLOCHAIN_ADR_V1' | 'HOLOCHAIN_KEY_V0' | 'HOLOCHAIN_KEY_V1' | 'HOLOCHAIN_SIG_V0' | 'HOLOCHAIN_SIG_V1' | 'SKYNET_NS'; + +/** + * Names for all available codecs + */ +export type CodecName = 'identity' | 'cidv1' | 'cidv2' | 'cidv3' | 'ip4' | 'tcp' | 'sha1' | 'sha2-256' | 'sha2-512' | 'sha3-512' | 'sha3-384' | 'sha3-256' | 'sha3-224' | 'shake-128' | 'shake-256' | 'keccak-224' | 'keccak-256' | 'keccak-384' | 'keccak-512' | 'blake3' | 'dccp' | 'murmur3-128' | 'murmur3-32' | 'ip6' | 'ip6zone' | 'path' | 'multicodec' | 'multihash' | 'multiaddr' | 'multibase' | 'dns' | 'dns4' | 'dns6' | 'dnsaddr' | 'protobuf' | 'cbor' | 'raw' | 'dbl-sha2-256' | 'rlp' | 'bencode' | 'dag-pb' | 'dag-cbor' | 'libp2p-key' | 'git-raw' | 'torrent-info' | 'torrent-file' | 'leofcoin-block' | 'leofcoin-tx' | 'leofcoin-pr' | 'sctp' | 'dag-jose' | 'dag-cose' | 'eth-block' | 'eth-block-list' | 'eth-tx-trie' | 'eth-tx' | 'eth-tx-receipt-trie' | 'eth-tx-receipt' | 'eth-state-trie' | 'eth-account-snapshot' | 'eth-storage-trie' | 'bitcoin-block' | 'bitcoin-tx' | 'bitcoin-witness-commitment' | 'zcash-block' | 'zcash-tx' | 'docid' | 'stellar-block' | 'stellar-tx' | 'md4' | 'md5' | 'bmt' | 'decred-block' | 'decred-tx' | 'ipld-ns' | 'ipfs-ns' | 'swarm-ns' | 'ipns-ns' | 'zeronet' | 'secp256k1-pub' | 'bls12_381-g1-pub' | 'bls12_381-g2-pub' | 'x25519-pub' | 'ed25519-pub' | 'bls12_381-g1g2-pub' | 'dash-block' | 'dash-tx' | 'swarm-manifest' | 'swarm-feed' | 'udp' | 'p2p-webrtc-star' | 'p2p-webrtc-direct' | 'p2p-stardust' | 'p2p-circuit' | 'dag-json' | 'udt' | 'utp' | 'unix' | 'p2p' | 'ipfs' | 'https' | 'onion' | 'onion3' | 'garlic64' | 'garlic32' | 'tls' | 'quic' | 'ws' | 'wss' | 'p2p-websocket-star' | 'http' | 'json' | 'messagepack' | 'libp2p-peer-record' | 'sha2-256-trunc254-padded' | 'ripemd-128' | 'ripemd-160' | 'ripemd-256' | 'ripemd-320' | 'x11' | 'p256-pub' | 'p384-pub' | 'p521-pub' | 'ed448-pub' | 'x448-pub' | 'ed25519-priv' | 'kangarootwelve' | 'sm3-256' | 'blake2b-8' | 'blake2b-16' | 'blake2b-24' | 'blake2b-32' | 'blake2b-40' | 'blake2b-48' | 'blake2b-56' | 'blake2b-64' | 'blake2b-72' | 'blake2b-80' | 'blake2b-88' | 'blake2b-96' | 'blake2b-104' | 'blake2b-112' | 'blake2b-120' | 'blake2b-128' | 'blake2b-136' | 'blake2b-144' | 'blake2b-152' | 'blake2b-160' | 'blake2b-168' | 'blake2b-176' | 'blake2b-184' | 'blake2b-192' | 'blake2b-200' | 'blake2b-208' | 'blake2b-216' | 'blake2b-224' | 'blake2b-232' | 'blake2b-240' | 'blake2b-248' | 'blake2b-256' | 'blake2b-264' | 'blake2b-272' | 'blake2b-280' | 'blake2b-288' | 'blake2b-296' | 'blake2b-304' | 'blake2b-312' | 'blake2b-320' | 'blake2b-328' | 'blake2b-336' | 'blake2b-344' | 'blake2b-352' | 'blake2b-360' | 'blake2b-368' | 'blake2b-376' | 'blake2b-384' | 'blake2b-392' | 'blake2b-400' | 'blake2b-408' | 'blake2b-416' | 'blake2b-424' | 'blake2b-432' | 'blake2b-440' | 'blake2b-448' | 'blake2b-456' | 'blake2b-464' | 'blake2b-472' | 'blake2b-480' | 'blake2b-488' | 'blake2b-496' | 'blake2b-504' | 'blake2b-512' | 'blake2s-8' | 'blake2s-16' | 'blake2s-24' | 'blake2s-32' | 'blake2s-40' | 'blake2s-48' | 'blake2s-56' | 'blake2s-64' | 'blake2s-72' | 'blake2s-80' | 'blake2s-88' | 'blake2s-96' | 'blake2s-104' | 'blake2s-112' | 'blake2s-120' | 'blake2s-128' | 'blake2s-136' | 'blake2s-144' | 'blake2s-152' | 'blake2s-160' | 'blake2s-168' | 'blake2s-176' | 'blake2s-184' | 'blake2s-192' | 'blake2s-200' | 'blake2s-208' | 'blake2s-216' | 'blake2s-224' | 'blake2s-232' | 'blake2s-240' | 'blake2s-248' | 'blake2s-256' | 'skein256-8' | 'skein256-16' | 'skein256-24' | 'skein256-32' | 'skein256-40' | 'skein256-48' | 'skein256-56' | 'skein256-64' | 'skein256-72' | 'skein256-80' | 'skein256-88' | 'skein256-96' | 'skein256-104' | 'skein256-112' | 'skein256-120' | 'skein256-128' | 'skein256-136' | 'skein256-144' | 'skein256-152' | 'skein256-160' | 'skein256-168' | 'skein256-176' | 'skein256-184' | 'skein256-192' | 'skein256-200' | 'skein256-208' | 'skein256-216' | 'skein256-224' | 'skein256-232' | 'skein256-240' | 'skein256-248' | 'skein256-256' | 'skein512-8' | 'skein512-16' | 'skein512-24' | 'skein512-32' | 'skein512-40' | 'skein512-48' | 'skein512-56' | 'skein512-64' | 'skein512-72' | 'skein512-80' | 'skein512-88' | 'skein512-96' | 'skein512-104' | 'skein512-112' | 'skein512-120' | 'skein512-128' | 'skein512-136' | 'skein512-144' | 'skein512-152' | 'skein512-160' | 'skein512-168' | 'skein512-176' | 'skein512-184' | 'skein512-192' | 'skein512-200' | 'skein512-208' | 'skein512-216' | 'skein512-224' | 'skein512-232' | 'skein512-240' | 'skein512-248' | 'skein512-256' | 'skein512-264' | 'skein512-272' | 'skein512-280' | 'skein512-288' | 'skein512-296' | 'skein512-304' | 'skein512-312' | 'skein512-320' | 'skein512-328' | 'skein512-336' | 'skein512-344' | 'skein512-352' | 'skein512-360' | 'skein512-368' | 'skein512-376' | 'skein512-384' | 'skein512-392' | 'skein512-400' | 'skein512-408' | 'skein512-416' | 'skein512-424' | 'skein512-432' | 'skein512-440' | 'skein512-448' | 'skein512-456' | 'skein512-464' | 'skein512-472' | 'skein512-480' | 'skein512-488' | 'skein512-496' | 'skein512-504' | 'skein512-512' | 'skein1024-8' | 'skein1024-16' | 'skein1024-24' | 'skein1024-32' | 'skein1024-40' | 'skein1024-48' | 'skein1024-56' | 'skein1024-64' | 'skein1024-72' | 'skein1024-80' | 'skein1024-88' | 'skein1024-96' | 'skein1024-104' | 'skein1024-112' | 'skein1024-120' | 'skein1024-128' | 'skein1024-136' | 'skein1024-144' | 'skein1024-152' | 'skein1024-160' | 'skein1024-168' | 'skein1024-176' | 'skein1024-184' | 'skein1024-192' | 'skein1024-200' | 'skein1024-208' | 'skein1024-216' | 'skein1024-224' | 'skein1024-232' | 'skein1024-240' | 'skein1024-248' | 'skein1024-256' | 'skein1024-264' | 'skein1024-272' | 'skein1024-280' | 'skein1024-288' | 'skein1024-296' | 'skein1024-304' | 'skein1024-312' | 'skein1024-320' | 'skein1024-328' | 'skein1024-336' | 'skein1024-344' | 'skein1024-352' | 'skein1024-360' | 'skein1024-368' | 'skein1024-376' | 'skein1024-384' | 'skein1024-392' | 'skein1024-400' | 'skein1024-408' | 'skein1024-416' | 'skein1024-424' | 'skein1024-432' | 'skein1024-440' | 'skein1024-448' | 'skein1024-456' | 'skein1024-464' | 'skein1024-472' | 'skein1024-480' | 'skein1024-488' | 'skein1024-496' | 'skein1024-504' | 'skein1024-512' | 'skein1024-520' | 'skein1024-528' | 'skein1024-536' | 'skein1024-544' | 'skein1024-552' | 'skein1024-560' | 'skein1024-568' | 'skein1024-576' | 'skein1024-584' | 'skein1024-592' | 'skein1024-600' | 'skein1024-608' | 'skein1024-616' | 'skein1024-624' | 'skein1024-632' | 'skein1024-640' | 'skein1024-648' | 'skein1024-656' | 'skein1024-664' | 'skein1024-672' | 'skein1024-680' | 'skein1024-688' | 'skein1024-696' | 'skein1024-704' | 'skein1024-712' | 'skein1024-720' | 'skein1024-728' | 'skein1024-736' | 'skein1024-744' | 'skein1024-752' | 'skein1024-760' | 'skein1024-768' | 'skein1024-776' | 'skein1024-784' | 'skein1024-792' | 'skein1024-800' | 'skein1024-808' | 'skein1024-816' | 'skein1024-824' | 'skein1024-832' | 'skein1024-840' | 'skein1024-848' | 'skein1024-856' | 'skein1024-864' | 'skein1024-872' | 'skein1024-880' | 'skein1024-888' | 'skein1024-896' | 'skein1024-904' | 'skein1024-912' | 'skein1024-920' | 'skein1024-928' | 'skein1024-936' | 'skein1024-944' | 'skein1024-952' | 'skein1024-960' | 'skein1024-968' | 'skein1024-976' | 'skein1024-984' | 'skein1024-992' | 'skein1024-1000' | 'skein1024-1008' | 'skein1024-1016' | 'skein1024-1024' | 'poseidon-bls12_381-a2-fc1' | 'poseidon-bls12_381-a2-fc1-sc' | 'zeroxcert-imprint-256' | 'fil-commitment-unsealed' | 'fil-commitment-sealed' | 'holochain-adr-v0' | 'holochain-adr-v1' | 'holochain-key-v0' | 'holochain-key-v1' | 'holochain-sig-v0' | 'holochain-sig-v1' | 'skynet-ns'; + +/** + * Number for all available codecs + */ +export type CodecNumber = 0x00 | 0x01 | 0x02 | 0x03 | 0x04 | 0x06 | 0x11 | 0x12 | 0x13 | 0x14 | 0x15 | 0x16 | 0x17 | 0x18 | 0x19 | 0x1a | 0x1b | 0x1c | 0x1d | 0x1e | 0x21 | 0x22 | 0x23 | 0x29 | 0x2a | 0x2f | 0x30 | 0x31 | 0x32 | 0x33 | 0x35 | 0x36 | 0x37 | 0x38 | 0x50 | 0x51 | 0x55 | 0x56 | 0x60 | 0x63 | 0x70 | 0x71 | 0x72 | 0x78 | 0x7b | 0x7c | 0x81 | 0x82 | 0x83 | 0x84 | 0x85 | 0x86 | 0x90 | 0x91 | 0x92 | 0x93 | 0x94 | 0x95 | 0x96 | 0x97 | 0x98 | 0xb0 | 0xb1 | 0xb2 | 0xc0 | 0xc1 | 0xce | 0xd0 | 0xd1 | 0xd4 | 0xd5 | 0xd6 | 0xe0 | 0xe1 | 0xe2 | 0xe3 | 0xe4 | 0xe5 | 0xe6 | 0xe7 | 0xea | 0xeb | 0xec | 0xed | 0xee | 0xf0 | 0xf1 | 0xfa | 0xfb | 0x0111 | 0x0113 | 0x0114 | 0x0115 | 0x0122 | 0x0129 | 0x012d | 0x012e | 0x0190 | 0x01a5 | 0x01a5 | 0x01bb | 0x01bc | 0x01bd | 0x01be | 0x01bf | 0x01c0 | 0x01cc | 0x01dd | 0x01de | 0x01df | 0x01e0 | 0x0200 | 0x0201 | 0x0301 | 0x1012 | 0x1052 | 0x1053 | 0x1054 | 0x1055 | 0x1100 | 0x1200 | 0x1201 | 0x1202 | 0x1203 | 0x1204 | 0x1300 | 0x1d01 | 0x534d | 0xb201 | 0xb202 | 0xb203 | 0xb204 | 0xb205 | 0xb206 | 0xb207 | 0xb208 | 0xb209 | 0xb20a | 0xb20b | 0xb20c | 0xb20d | 0xb20e | 0xb20f | 0xb210 | 0xb211 | 0xb212 | 0xb213 | 0xb214 | 0xb215 | 0xb216 | 0xb217 | 0xb218 | 0xb219 | 0xb21a | 0xb21b | 0xb21c | 0xb21d | 0xb21e | 0xb21f | 0xb220 | 0xb221 | 0xb222 | 0xb223 | 0xb224 | 0xb225 | 0xb226 | 0xb227 | 0xb228 | 0xb229 | 0xb22a | 0xb22b | 0xb22c | 0xb22d | 0xb22e | 0xb22f | 0xb230 | 0xb231 | 0xb232 | 0xb233 | 0xb234 | 0xb235 | 0xb236 | 0xb237 | 0xb238 | 0xb239 | 0xb23a | 0xb23b | 0xb23c | 0xb23d | 0xb23e | 0xb23f | 0xb240 | 0xb241 | 0xb242 | 0xb243 | 0xb244 | 0xb245 | 0xb246 | 0xb247 | 0xb248 | 0xb249 | 0xb24a | 0xb24b | 0xb24c | 0xb24d | 0xb24e | 0xb24f | 0xb250 | 0xb251 | 0xb252 | 0xb253 | 0xb254 | 0xb255 | 0xb256 | 0xb257 | 0xb258 | 0xb259 | 0xb25a | 0xb25b | 0xb25c | 0xb25d | 0xb25e | 0xb25f | 0xb260 | 0xb301 | 0xb302 | 0xb303 | 0xb304 | 0xb305 | 0xb306 | 0xb307 | 0xb308 | 0xb309 | 0xb30a | 0xb30b | 0xb30c | 0xb30d | 0xb30e | 0xb30f | 0xb310 | 0xb311 | 0xb312 | 0xb313 | 0xb314 | 0xb315 | 0xb316 | 0xb317 | 0xb318 | 0xb319 | 0xb31a | 0xb31b | 0xb31c | 0xb31d | 0xb31e | 0xb31f | 0xb320 | 0xb321 | 0xb322 | 0xb323 | 0xb324 | 0xb325 | 0xb326 | 0xb327 | 0xb328 | 0xb329 | 0xb32a | 0xb32b | 0xb32c | 0xb32d | 0xb32e | 0xb32f | 0xb330 | 0xb331 | 0xb332 | 0xb333 | 0xb334 | 0xb335 | 0xb336 | 0xb337 | 0xb338 | 0xb339 | 0xb33a | 0xb33b | 0xb33c | 0xb33d | 0xb33e | 0xb33f | 0xb340 | 0xb341 | 0xb342 | 0xb343 | 0xb344 | 0xb345 | 0xb346 | 0xb347 | 0xb348 | 0xb349 | 0xb34a | 0xb34b | 0xb34c | 0xb34d | 0xb34e | 0xb34f | 0xb350 | 0xb351 | 0xb352 | 0xb353 | 0xb354 | 0xb355 | 0xb356 | 0xb357 | 0xb358 | 0xb359 | 0xb35a | 0xb35b | 0xb35c | 0xb35d | 0xb35e | 0xb35f | 0xb360 | 0xb361 | 0xb362 | 0xb363 | 0xb364 | 0xb365 | 0xb366 | 0xb367 | 0xb368 | 0xb369 | 0xb36a | 0xb36b | 0xb36c | 0xb36d | 0xb36e | 0xb36f | 0xb370 | 0xb371 | 0xb372 | 0xb373 | 0xb374 | 0xb375 | 0xb376 | 0xb377 | 0xb378 | 0xb379 | 0xb37a | 0xb37b | 0xb37c | 0xb37d | 0xb37e | 0xb37f | 0xb380 | 0xb381 | 0xb382 | 0xb383 | 0xb384 | 0xb385 | 0xb386 | 0xb387 | 0xb388 | 0xb389 | 0xb38a | 0xb38b | 0xb38c | 0xb38d | 0xb38e | 0xb38f | 0xb390 | 0xb391 | 0xb392 | 0xb393 | 0xb394 | 0xb395 | 0xb396 | 0xb397 | 0xb398 | 0xb399 | 0xb39a | 0xb39b | 0xb39c | 0xb39d | 0xb39e | 0xb39f | 0xb3a0 | 0xb3a1 | 0xb3a2 | 0xb3a3 | 0xb3a4 | 0xb3a5 | 0xb3a6 | 0xb3a7 | 0xb3a8 | 0xb3a9 | 0xb3aa | 0xb3ab | 0xb3ac | 0xb3ad | 0xb3ae | 0xb3af | 0xb3b0 | 0xb3b1 | 0xb3b2 | 0xb3b3 | 0xb3b4 | 0xb3b5 | 0xb3b6 | 0xb3b7 | 0xb3b8 | 0xb3b9 | 0xb3ba | 0xb3bb | 0xb3bc | 0xb3bd | 0xb3be | 0xb3bf | 0xb3c0 | 0xb3c1 | 0xb3c2 | 0xb3c3 | 0xb3c4 | 0xb3c5 | 0xb3c6 | 0xb3c7 | 0xb3c8 | 0xb3c9 | 0xb3ca | 0xb3cb | 0xb3cc | 0xb3cd | 0xb3ce | 0xb3cf | 0xb3d0 | 0xb3d1 | 0xb3d2 | 0xb3d3 | 0xb3d4 | 0xb3d5 | 0xb3d6 | 0xb3d7 | 0xb3d8 | 0xb3d9 | 0xb3da | 0xb3db | 0xb3dc | 0xb3dd | 0xb3de | 0xb3df | 0xb3e0 | 0xb401 | 0xb402 | 0xce11 | 0xf101 | 0xf102 | 0x807124 | 0x817124 | 0x947124 | 0x957124 | 0xa27124 | 0xa37124 | 0xb19910; + +export type ConstantNumberMap = Record +export type NameUint8ArrayMap = Record +export type NumberNameMap = Record +export type NameNumberMap = Record diff --git a/src/index.js b/src/index.js index f8f43b4..602354c 100644 --- a/src/index.js +++ b/src/index.js @@ -11,25 +11,25 @@ */ 'use strict' +/** @typedef {import('./generated-types').CodecName} CodecName */ +/** @typedef {import('./generated-types').CodecNumber} CodecNumber */ + const varint = require('varint') const intTable = require('./int-table') const codecNameToCodeVarint = require('./varint-table') const util = require('./util') const uint8ArrayConcat = require('uint8arrays/concat') -exports = module.exports - /** * Prefix a buffer with a multicodec-packed. * - * @param {string|number} multicodecStrOrCode + * @param {CodecName|Uint8Array} multicodecStrOrCode * @param {Uint8Array} data * @returns {Uint8Array} */ -exports.addPrefix = (multicodecStrOrCode, data) => { +function addPrefix (multicodecStrOrCode, data) { let prefix - // @ts-ignore: TS2358 if (multicodecStrOrCode instanceof Uint8Array) { prefix = util.varintUint8ArrayEncode(multicodecStrOrCode) } else { @@ -48,7 +48,7 @@ exports.addPrefix = (multicodecStrOrCode, data) => { * @param {Uint8Array} data * @returns {Uint8Array} */ -exports.rmPrefix = (data) => { +function rmPrefix (data) { varint.decode(data) return data.slice(varint.decode.bytes) } @@ -57,9 +57,9 @@ exports.rmPrefix = (data) => { * Get the codec of the prefixed data. * * @param {Uint8Array} prefixedData - * @returns {string} + * @returns {CodecName} */ -exports.getCodec = (prefixedData) => { +function getCodec (prefixedData) { const code = varint.decode(prefixedData) const codecName = intTable.get(code) if (codecName === undefined) { @@ -71,20 +71,20 @@ exports.getCodec = (prefixedData) => { /** * Get the name of the codec. * - * @param {number} codec - * @returns {string} + * @param {CodecNumber} codec + * @returns {CodecName|undefined} */ -exports.getName = (codec) => { +function getName (codec) { return intTable.get(codec) } /** * Get the code of the codec * - * @param {string} name - * @returns {number} + * @param {CodecName} name + * @returns {CodecNumber} */ -exports.getNumber = (name) => { +function getNumber (name) { const code = codecNameToCodeVarint[name] if (code === undefined) { throw new Error('Codec `' + name + '` not found') @@ -96,19 +96,19 @@ exports.getNumber = (name) => { * Get the code of the prefixed data. * * @param {Uint8Array} prefixedData - * @returns {number} + * @returns {CodecNumber} */ -exports.getCode = (prefixedData) => { +function getCode (prefixedData) { return varint.decode(prefixedData) } /** * Get the code as varint of a codec name. * - * @param {string} codecName + * @param {CodecName} codecName * @returns {Uint8Array} */ -exports.getCodeVarint = (codecName) => { +function getCodeVarint (codecName) { const code = codecNameToCodeVarint[codecName] if (code === undefined) { throw new Error('Codec `' + codecName + '` not found') @@ -119,16 +119,28 @@ exports.getCodeVarint = (codecName) => { /** * Get the varint of a code. * - * @param {number} code + * @param {CodecNumber} code * @returns {Array.} */ -exports.getVarint = (code) => { +function getVarint (code) { return varint.encode(code) } // Make the constants top-level constants const constants = require('./constants') -Object.assign(exports, constants) // Human friendly names for printing, e.g. in error messages -exports.print = require('./print') +const print = require('./print') + +module.exports = { + addPrefix, + rmPrefix, + getCodec, + getName, + getNumber, + getCode, + getCodeVarint, + getVarint, + print, + ...constants +} diff --git a/src/int-table.js b/src/int-table.js index 98b168a..f28b04b 100644 --- a/src/int-table.js +++ b/src/int-table.js @@ -1,12 +1,18 @@ 'use strict' -const baseTable = require('./base-table.json') -// map for hexString -> codecName +/** @typedef {import('./generated-types').CodecName} CodecName */ +/** @typedef {import('./generated-types').CodecNumber} CodecNumber */ + +const { baseTable } = require('./base-table') + +/** + * @type {Map} + */ const nameTable = new Map() for (const encodingName in baseTable) { const code = baseTable[encodingName] - nameTable.set(code, encodingName) + nameTable.set(code, /** @type {CodecName} */(encodingName)) } module.exports = Object.freeze(nameTable) diff --git a/src/print.js b/src/print.js index 0523f3c..fc4ec4d 100644 --- a/src/print.js +++ b/src/print.js @@ -1,12 +1,16 @@ 'use strict' -const table = require('./base-table.json') +/** @typedef {import('./generated-types').CodecName} CodecName */ +/** @typedef {import('./generated-types').NumberNameMap} NumberNameMap */ -// map for code -> print friendly name -const tableByCode = {} +const { baseTable } = require('./base-table') -for (const [name, code] of Object.entries(table)) { - if (tableByCode[code] === undefined) tableByCode[code] = name +const tableByCode = /** @type {NumberNameMap} */({}) + +for (const [name, code] of Object.entries(baseTable)) { + if (tableByCode[code] === undefined) { + tableByCode[code] = /** @type {CodecName} **/(name) + } } -module.exports = Object.freeze(tableByCode) +module.exports = /** @type {NumberNameMap} */(Object.freeze(tableByCode)) diff --git a/src/varint-table.js b/src/varint-table.js index 30b647e..01e611e 100644 --- a/src/varint-table.js +++ b/src/varint-table.js @@ -1,10 +1,11 @@ 'use strict' -const baseTable = require('./base-table.json') +/** @typedef {import('./generated-types').NameUint8ArrayMap} NameUint8ArrayMap */ + +const { baseTable } = require('./base-table') const varintEncode = require('./util').varintEncode -// map for codecName -> codeVarintUint8Array -const varintTable = {} +const varintTable = /** @type {NameUint8ArrayMap} */ ({}) for (const encodingName in baseTable) { const code = baseTable[encodingName] diff --git a/test/multicodec.spec.js b/test/multicodec.spec.js index 857d2d4..91923bf 100644 --- a/test/multicodec.spec.js +++ b/test/multicodec.spec.js @@ -1,10 +1,13 @@ /* eslint-env mocha */ 'use strict' +/** @typedef {import("../src/generated-types").CodecName} CodecName */ +/** @typedef {import("../src/generated-types").CodecNumber} CodecNumber */ + const { expect } = require('aegir/utils/chai') const multicodec = require('../src') const uint8ArrayFromString = require('uint8arrays/from-string') -const baseTable = require('../src/base-table.json') +const { baseTable } = require('../src/base-table') describe('multicodec', () => { it('add prefix through multicodec (string)', () => { @@ -59,7 +62,7 @@ describe('multicodec', () => { it('returns all codec numbers from names', () => { for (const name in baseTable) { - expect(multicodec.getNumber(name)).to.eql(baseTable[name]) + expect(multicodec.getNumber(/** @type {CodecName} */(name))).to.eql(baseTable[name]) } }) @@ -88,6 +91,7 @@ describe('multicodec', () => { it('throws error on unknown codec name when getting the code', () => { expect(() => { + // @ts-expect-error multicodec.getCodeVarint('this-codec-doesnt-exist') }).to.throw( 'Codec `this-codec-doesnt-exist` not found' diff --git a/tools/update-table.js b/tools/update-table.js index a4ecaeb..ad2161c 100755 --- a/tools/update-table.js +++ b/tools/update-table.js @@ -1,30 +1,71 @@ 'use strict' -const bent = require('bent') +const fs = require('fs') const path = require('path') - -const get = bent('string') +const http = require('ipfs-utils/src/http') const url = 'https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv' -const fs = require('fs') - -const parse = async function * () { - const str = await get(url) - const lines = str.split('\n') - lines.shift() - for (const line of lines) { - if (!line.length) continue - const [name, tag, code] = line.split(',') - yield { name: name.trim(), tag: tag.trim(), code: parseInt(code.trim(), 16) } - } -} const run = async () => { - const table = {} + const rsp = await http.get(url) + const lines = (await rsp.text()).split('\n') + const names = [] + const codes = [] + const processed = lines + .slice(1, lines.length - 1) + .map(l => { + const [name, tag, code] = l.split(',') + return [name.trim(), tag.trim(), code.trim()] + }) + .reduce((acc, l, index, arr) => { + names.push(`'${l[0]}'`) + codes.push(`${l[2].replace('\'', '')}`) + acc += ` '${l[0]}': ${l[2].replace('\'', '')}` + + if (index !== arr.length - 1) { + acc += ',\n' + } + return acc + }, '') + + const typesTemplate = `// DO NOT CHANGE THIS FILE MANUALLY. IT IS GENERATED BY tools/update-table.js + +/** + * Constant names for all available codecs + */ +export type CodecConstant = ${names.map(n => `${n.toUpperCase().replace(/-/g, '_')}`).join(' | ')}; - for await (const { name, code } of parse()) { - table[name] = code - } +/** + * Names for all available codecs + */ +export type CodecName = ${names.join(' | ')}; - fs.writeFileSync(path.join(__dirname, '../src/base-table.json'), JSON.stringify(table, null, 2)) +/** + * Number for all available codecs + */ +export type CodecNumber = ${codes.join(' | ')}; + +export type ConstantNumberMap = Record +export type NameUint8ArrayMap = Record +export type NumberNameMap = Record +export type NameNumberMap = Record +` + + const tableTemplate = `// DO NOT CHANGE THIS FILE. IT IS GENERATED BY tools/update-table.js +/* eslint quote-props: off */ +'use strict' + +/** + * @type {import('./generated-types').NameNumberMap} + */ +const baseTable = Object.freeze({ +${processed} +}) + +module.exports = { baseTable } +` + + fs.writeFileSync(path.join(__dirname, '../src/generated-types.ts'), typesTemplate) + fs.writeFileSync(path.join(__dirname, '../src/base-table.js'), tableTemplate) } + run() diff --git a/tsconfig.json b/tsconfig.json index a364cb9..7371337 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,10 @@ { - "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", - "compilerOptions": { - "outDir": "dist" - }, - "files": [ - "src/base-table.json" - ], - "include": [ - "test", // remove this line if you don't want to type-check tests - "src" - ] + "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "test", + "src" + ] }