Socket
Socket
Sign inDemoInstall

multiformats

Package Overview
Dependencies
Maintainers
3
Versions
153
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multiformats - npm Package Compare versions

Comparing version 9.8.1 to 10.0.0-pre.1

LICENSE

358

package.json
{
"name": "multiformats",
"version": "9.8.1",
"version": "10.0.0-pre.1",
"description": "Interface for multihash, multicodec, multibase and CID",
"main": "./cjs/src/index.js",
"types": "./types/src/index.d.ts",
"scripts": {
"build": "npm run build:js && npm run build:types",
"build:js": "ipjs build --tests --main && npm run build:copy",
"build:copy": "cp -a tsconfig.json src vendor test dist/ && rm -rf dist/test/ts-use",
"build:types": "npm run build:copy && cd dist && tsc --build",
"build:vendor": "npm run build:vendor:varint && npm run build:vendor:base-x",
"build:vendor:varint": "npm_config_yes=true npx brrp -x varint > vendor/varint.js",
"build:vendor:base-x": "npm_config_yes=true npx brrp -x @multiformats/base-x > vendor/base-x.js",
"lint": "standard",
"test:cjs": "npm run build:js && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
"test:esm": "npm run build:js && mocha dist/esm/node-test/test-*.js && npm run test:esm:browser",
"test:node": "c8 --check-coverage --branches 100 --functions 100 --lines 100 mocha test/test-*.js",
"test:cjs:browser": "polendina --page --worker --serviceworker --cleanup dist/cjs/browser-test/test-*.js",
"test:esm:browser": "polendina --page --worker --serviceworker --cleanup dist/esm/browser-test/test-*.js",
"test:ts": "npm run build:types && npm run test --prefix test/ts-use",
"test": "npm run lint && npm run test:node && npm run test:esm && npm run test:ts",
"test:ci": "npm run lint && npm run test:node && npm run test:esm && npm run test:cjs && npm run test:ts",
"coverage": "c8 --reporter=html mocha test/test-*.js && npm_config_yes=true npx st -d coverage -p 8080"
"author": "Mikeal Rogers <mikeal.rogers@gmail.com> (https://www.mikealrogers.com/)",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/multiformats/js-multiformats#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/multiformats/js-multiformats.git"
},
"c8": {
"exclude": [
"test/**",
"vendor/**"
]
"bugs": {
"url": "https://github.com/multiformats/js-multiformats/issues"
},

@@ -37,157 +20,137 @@ "keywords": [

],
"author": "Mikeal Rogers <mikeal.rogers@gmail.com> (https://www.mikealrogers.com/)",
"license": "(Apache-2.0 AND MIT)",
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
],
"src/*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"browser": "./esm/src/index.js",
"require": "./cjs/src/index.js",
"import": "./esm/src/index.js"
"types": "./dist/src/index.d.ts",
"import": "./src/index.js"
},
"./cid": {
"browser": "./esm/src/cid.js",
"require": "./cjs/src/cid.js",
"import": "./esm/src/cid.js"
"./bases/base10": {
"types": "./src/bases/base10.d.ts",
"import": "./src/bases/base10.js"
},
"./basics": {
"browser": "./esm/src/basics.js",
"require": "./cjs/src/basics.js",
"import": "./esm/src/basics.js"
"./bases/base16": {
"types": "./src/bases/base16.d.ts",
"import": "./src/bases/base16.js"
},
"./block": {
"browser": "./esm/src/block.js",
"require": "./cjs/src/block.js",
"import": "./esm/src/block.js"
"./bases/base2": {
"types": "./src/bases/base2.d.ts",
"import": "./src/bases/base2.js"
},
"./traversal": {
"browser": "./esm/src/traversal.js",
"require": "./cjs/src/traversal.js",
"import": "./esm/src/traversal.js"
"./bases/base256emoji": {
"types": "./src/bases/base256emoji.d.ts",
"import": "./src/bases/base256emoji.js"
},
"./bases/identity": {
"browser": "./esm/src/bases/identity.js",
"require": "./cjs/src/bases/identity.js",
"import": "./esm/src/bases/identity.js"
"./bases/base32": {
"types": "./src/bases/base32.d.ts",
"import": "./src/bases/base32.js"
},
"./bases/base2": {
"browser": "./esm/src/bases/base2.js",
"require": "./cjs/src/bases/base2.js",
"import": "./esm/src/bases/base2.js"
"./bases/base36": {
"types": "./src/bases/base36.d.ts",
"import": "./src/bases/base36.js"
},
"./bases/base58": {
"types": "./src/bases/base58.d.ts",
"import": "./src/bases/base58.js"
},
"./bases/base64": {
"types": "./src/bases/base64.d.ts",
"import": "./src/bases/base64.js"
},
"./bases/base8": {
"browser": "./esm/src/bases/base8.js",
"require": "./cjs/src/bases/base8.js",
"import": "./esm/src/bases/base8.js"
"types": "./src/bases/base8.d.ts",
"import": "./src/bases/base8.js"
},
"./bases/base10": {
"browser": "./esm/src/bases/base10.js",
"require": "./cjs/src/bases/base10.js",
"import": "./esm/src/bases/base10.js"
"./bases/identity": {
"types": "./src/bases/identity.d.ts",
"import": "./src/bases/identity.js"
},
"./bases/base16": {
"browser": "./esm/src/bases/base16.js",
"require": "./cjs/src/bases/base16.js",
"import": "./esm/src/bases/base16.js"
"./basics": {
"types": "./src/basics.d.ts",
"import": "./src/basics.js"
},
"./bases/base32": {
"browser": "./esm/src/bases/base32.js",
"require": "./cjs/src/bases/base32.js",
"import": "./esm/src/bases/base32.js"
"./block": {
"types": "./src/block.d.ts",
"import": "./src/block.js"
},
"./bases/base36": {
"browser": "./esm/src/bases/base36.js",
"require": "./cjs/src/bases/base36.js",
"import": "./esm/src/bases/base36.js"
"./bytes": {
"types": "./src/bytes.d.ts",
"import": "./src/bytes.js"
},
"./bases/base58": {
"browser": "./esm/src/bases/base58.js",
"require": "./cjs/src/bases/base58.js",
"import": "./esm/src/bases/base58.js"
"./cid": {
"types": "./src/cid.d.ts",
"import": "./src/cid.js"
},
"./bases/base64": {
"browser": "./esm/src/bases/base64.js",
"require": "./cjs/src/bases/base64.js",
"import": "./esm/src/bases/base64.js"
"./codecs/json": {
"types": "./src/codecs/json.d.ts",
"import": "./src/codecs/json.js"
},
"./bases/base256emoji": {
"browser": "./esm/src/bases/base256emoji.js",
"require": "./cjs/src/bases/base256emoji.js",
"import": "./esm/src/bases/base256emoji.js"
"./codecs/raw": {
"types": "./src/codecs/raw.d.ts",
"import": "./src/codecs/raw.js"
},
"./hashes/digest": {
"types": "./src/hashes/digest.d.ts",
"import": "./src/hashes/digest.js"
},
"./hashes/hasher": {
"browser": "./esm/src/hashes/hasher.js",
"require": "./cjs/src/hashes/hasher.js",
"import": "./esm/src/hashes/hasher.js"
"types": "./src/hashes/hasher.d.ts",
"import": "./src/hashes/hasher.js"
},
"./hashes/digest": {
"browser": "./esm/src/hashes/digest.js",
"require": "./cjs/src/hashes/digest.js",
"import": "./esm/src/hashes/digest.js"
"./hashes/identity": {
"types": "./src/hashes/identity.d.ts",
"import": "./src/hashes/identity.js"
},
"./hashes/sha2": {
"browser": "./esm/src/hashes/sha2-browser.js",
"require": "./cjs/src/hashes/sha2.js",
"import": "./esm/src/hashes/sha2.js"
"types": "./src/hashes/sha2.d.ts",
"browser": "./src/hashes/sha2-browser.js",
"import": "./src/hashes/sha2.js"
},
"./hashes/identity": {
"browser": "./esm/src/hashes/identity.js",
"require": "./cjs/src/hashes/identity.js",
"import": "./esm/src/hashes/identity.js"
"./interface": {
"types": "./src/interface.d.ts",
"import": "./src/interface.js"
},
"./codecs/json": {
"browser": "./esm/src/codecs/json.js",
"require": "./cjs/src/codecs/json.js",
"import": "./esm/src/codecs/json.js"
"./link": {
"types": "./src/link.d.ts",
"import": "./src/link.js"
},
"./codecs/raw": {
"browser": "./esm/src/codecs/raw.js",
"require": "./cjs/src/codecs/raw.js",
"import": "./esm/src/codecs/raw.js"
"./traversal": {
"types": "./src/traversal.d.ts",
"import": "./src/traversal.js"
}
},
"devDependencies": {
"@ipld/dag-pb": "^2.1.14",
"@stablelib/sha256": "^1.0.1",
"@stablelib/sha512": "^1.0.1",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"buffer": "^6.0.3",
"c8": "^7.10.0",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"cids": "^1.1.9",
"ipjs": "^5.2.0",
"mocha": "^10.0.0",
"polendina": "^3.0.0",
"standard": "^17.0.0",
"typescript": "^4.5.4"
"browser": {
"./hashes/sha2": "./src/hashes/sha2-browser.js",
"./src/hashes/sha2.js": "./src/hashes/sha2-browser.js"
},
"standard": {
"ignore": [
"dist",
"vendor"
]
},
"directories": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/multiformats/js-multiformats.git"
},
"bugs": {
"url": "https://github.com/multiformats/js-multiformats/issues"
},
"homepage": "https://github.com/multiformats/js-multiformats#readme",
"typesVersions": {
"*": {
"*": [
"types/src/*"
],
"types/*": [
"types/*"
]
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}

@@ -197,3 +160,3 @@ },

"branches": [
"master"
"esm-migration"
],

@@ -223,11 +186,11 @@ "plugins": [

{
"type": "chore",
"type": "docs",
"release": "patch"
},
{
"type": "docs",
"type": "test",
"release": "patch"
},
{
"type": "test",
"type": "deps",
"release": "patch"

@@ -262,5 +225,9 @@ },

"type": "docs",
"section": "Trivial Changes"
"section": "Documentation"
},
{
"type": "deps",
"section": "Dependencies"
},
{
"type": "test",

@@ -274,8 +241,3 @@ "section": "Tests"

"@semantic-release/changelog",
[
"@semantic-release/npm",
{
"pkgRoot": "dist"
}
],
"@semantic-release/npm",
"@semantic-release/github",

@@ -285,27 +247,45 @@ "@semantic-release/git"

},
"browser": {
".": "./cjs/src/index.js",
"./cid": "./cjs/src/cid.js",
"./basics": "./cjs/src/basics.js",
"./block": "./cjs/src/block.js",
"./traversal": "./cjs/src/traversal.js",
"./bases/identity": "./cjs/src/bases/identity.js",
"./bases/base2": "./cjs/src/bases/base2.js",
"./bases/base8": "./cjs/src/bases/base8.js",
"./bases/base10": "./cjs/src/bases/base10.js",
"./bases/base16": "./cjs/src/bases/base16.js",
"./bases/base32": "./cjs/src/bases/base32.js",
"./bases/base36": "./cjs/src/bases/base36.js",
"./bases/base58": "./cjs/src/bases/base58.js",
"./bases/base64": "./cjs/src/bases/base64.js",
"./bases/base256emoji": "./cjs/src/bases/base256emoji.js",
"./hashes/hasher": "./cjs/src/hashes/hasher.js",
"./hashes/digest": "./cjs/src/hashes/digest.js",
"./hashes/sha2": "./cjs/src/hashes/sha2-browser.js",
"./esm/src/hashes/sha2.js": "./esm/src/hashes/sha2-browser.js",
"./cjs/src/hashes/sha2.js": "./cjs/src/hashes/sha2-browser.js",
"./hashes/identity": "./cjs/src/hashes/identity.js",
"./codecs/json": "./cjs/src/codecs/json.js",
"./codecs/raw": "./cjs/src/codecs/raw.js"
}
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"build": "aegir build",
"release": "aegir release",
"test": "npm run lint && npm run test:node && npm run test:chrome && npm run test:ts",
"test:ts": "npm run test --prefix test/ts-use",
"test:node": "aegir test -t node --cov",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker --no-cov",
"test:firefox": "aegir test -t browser -- --browser firefox --no-cov",
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox --no-cov",
"test:electron-main": "aegir test -t electron-main"
},
"devDependencies": {
"@stablelib/sha256": "^1.0.1",
"@stablelib/sha512": "^1.0.1",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "^18.0.0",
"aegir": "^37.5.1",
"buffer": "^6.0.3",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"cids": "^1.1.9"
},
"aegir": {
"build": {
"bundle": false
},
"test": {
"cov": true,
"target": [
"node",
"browser"
]
}
},
"directories": {
"test": "test"
},
"main": "./src/index.js"
}

@@ -1,17 +0,34 @@

# multiformats
# multiformats <!-- omit in toc -->
* [Interfaces](#interfaces)
* [Creating Blocks](#creating-blocks)
* [Multibase Encoders / Decoders / Codecs](#multibase-encoders--decoders--codecs)
* [Multicodec Encoders / Decoders / Codecs](#multicodec-encoders--decoders--codecs)
* [Multihash Hashers](#multihash-hashers)
* [Traversal](#traversal)
* [Legacy interface](#legacy-interface)
* [Implementations](#implementations)
* [Multibase codecs](#multibase-codecs)
* [Multihash hashers](#multihash-hashers-1)
* [IPLD codecs (multicodec)](#ipld-codecs-multicodec)
* [License](#license)
* [Contribution](#contribution)
[![multiformats.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://multiformats.io)
[![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiformats.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiformats)
[![CI](https://img.shields.io/github/workflow/status/multiformats/js-multiformats/test%20&%20maybe%20release/esm-migration?style=flat-square)](https://github.com/multiformats/js-multiformats/actions/workflows/js-test-and-release.yml)
> Interface for multihash, multicodec, multibase and CID
## Table of contents <!-- omit in toc -->
- [Install](#install)
- [Interfaces](#interfaces)
- [Creating Blocks](#creating-blocks)
- [Multibase Encoders / Decoders / Codecs](#multibase-encoders--decoders--codecs)
- [Multicodec Encoders / Decoders / Codecs](#multicodec-encoders--decoders--codecs)
- [Multihash Hashers](#multihash-hashers)
- [Traversal](#traversal)
- [Legacy interface](#legacy-interface)
- [Implementations](#implementations)
- [Multibase codecs](#multibase-codecs)
- [Multihash hashers](#multihash-hashers-1)
- [IPLD codecs (multicodec)](#ipld-codecs-multicodec)
- [License](#license)
- [Contribution](#contribution)
## Install
```console
$ npm i multiformats
```
## Interfaces
This library defines common interfaces and low level building blocks for various interrelated multiformat technologies (multicodec, multihash, multibase, and CID). They can be used to implement custom base encoders / decoders / codecs, codec encoders /decoders and multihash hashers that comply to the interface that layers above assume.

@@ -21,4 +38,2 @@

## Interfaces
```js

@@ -81,3 +96,2 @@ import { CID } from 'multiformats/cid'

```js

@@ -194,8 +208,8 @@ cid.toString(base64)

| bases | import | repo |
--- | --- | --- |
`base16` | `multiformats/bases/base16` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |
`base32`, `base32pad`, `base32hex`, `base32hexpad`, `base32z` | `multiformats/bases/base32` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |
`base64`, `base64pad`, `base64url`, `base64urlpad` | `multiformats/bases/base64` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |
`base58btc`, `base58flick4` | `multiformats/bases/base58` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |
| bases | import | repo |
| ------------------------------------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------- |
| `base16` | `multiformats/bases/base16` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |
| `base32`, `base32pad`, `base32hex`, `base32hexpad`, `base32z` | `multiformats/bases/base32` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |
| `base64`, `base64pad`, `base64url`, `base64urlpad` | `multiformats/bases/base64` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |
| `base58btc`, `base58flick4` | `multiformats/bases/base58` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |

@@ -206,20 +220,20 @@ Other (less useful) bases implemented in [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) include: `base2`, `base8`, `base10`, `base36` and `base256emoji`.

| hashes | import | repo |
| --- | --- | --- |
| `sha2-256`, `sha2-512` | `multiformats/hashes/sha2` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes) |
| `sha3-224`, `sha3-256`, `sha3-384`,`sha3-512`, `shake-128`, `shake-256`, `keccak-224`, `keccak-256`, `keccak-384`, `keccak-512` | `@multiformats/sha3` | [multiformats/js-sha3](https://github.com/multiformats/js-sha3) |
| `identity` | `multiformats/hashes/identity` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes/identity.js) |
| `murmur3-128`, `murmur3-32` | `@multiformats/murmur3` | [multiformats/js-murmur3](https://github.com/multiformats/js-murmur3) |
| `blake2b-*`, `blake2s-*` | `@multiformats/blake2` | [multiformats/js-blake2](https://github.com/multiformats/js-blake2) |
| hashes | import | repo |
| ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------ |
| `sha2-256`, `sha2-512` | `multiformats/hashes/sha2` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes) |
| `sha3-224`, `sha3-256`, `sha3-384`,`sha3-512`, `shake-128`, `shake-256`, `keccak-224`, `keccak-256`, `keccak-384`, `keccak-512` | `@multiformats/sha3` | [multiformats/js-sha3](https://github.com/multiformats/js-sha3) |
| `identity` | `multiformats/hashes/identity` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes/identity.js) |
| `murmur3-128`, `murmur3-32` | `@multiformats/murmur3` | [multiformats/js-murmur3](https://github.com/multiformats/js-murmur3) |
| `blake2b-*`, `blake2s-*` | `@multiformats/blake2` | [multiformats/js-blake2](https://github.com/multiformats/js-blake2) |
### IPLD codecs (multicodec)
| codec | import | repo |
| --- | --- | --- |
| `raw` | `multiformats/codecs/raw` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/codecs) |
| `json` | `multiformats/codecs/json` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/codecs) |
| `dag-cbor` | `@ipld/dag-cbor` | [ipld/js-dag-cbor](https://github.com/ipld/js-dag-cbor) |
| `dag-json` | `@ipld/dag-json` | [ipld/js-dag-json](https://github.com/ipld/js-dag-json) |
| `dag-pb` | `@ipld/dag-pb` | [ipld/js-dag-pb](https://github.com/ipld/js-dag-pb) |
| `dag-jose` | `dag-jose`| [ceramicnetwork/js-dag-jose](https://github.com/ceramicnetwork/js-dag-jose) |
| codec | import | repo |
| ---------- | -------------------------- | ------------------------------------------------------------------------------------------------------ |
| `raw` | `multiformats/codecs/raw` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/codecs) |
| `json` | `multiformats/codecs/json` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/codecs) |
| `dag-cbor` | `@ipld/dag-cbor` | [ipld/js-dag-cbor](https://github.com/ipld/js-dag-cbor) |
| `dag-json` | `@ipld/dag-json` | [ipld/js-dag-json](https://github.com/ipld/js-dag-json) |
| `dag-pb` | `@ipld/dag-pb` | [ipld/js-dag-pb](https://github.com/ipld/js-dag-pb) |
| `dag-jose` | `dag-jose` | [ceramicnetwork/js-dag-jose](https://github.com/ceramicnetwork/js-dag-jose) |

@@ -230,7 +244,7 @@ ## License

* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
### Contribution
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
import basex from '../../vendor/base-x.js'
import { coerce } from '../bytes.js'
// Linter can't see that API is used in types.
// eslint-disable-next-line
import * as API from './interface.js'
/**
* @typedef {import('./interface').BaseEncoder} BaseEncoder
* @typedef {import('./interface').BaseDecoder} BaseDecoder
* @typedef {import('./interface').BaseCodec} BaseCodec
*/
/**
* @template {string} T
* @typedef {import('./interface').Multibase<T>} Multibase
*/
/**
* @template {string} T
* @typedef {import('./interface').MultibaseEncoder<T>} MultibaseEncoder
*/
/**
* Class represents both BaseEncoder and MultibaseEncoder meaning it
* can be used to encode to multibase or base encode without multibase
* prefix.
*
* @class
* @template {string} Base
* @template {string} Prefix
* @implements {MultibaseEncoder<Prefix>}
* @implements {BaseEncoder}
* @implements {API.MultibaseEncoder<Prefix>}
* @implements {API.BaseEncoder}
*/

@@ -43,3 +32,3 @@ class Encoder {

* @param {Uint8Array} bytes
* @returns {Multibase<Prefix>}
* @returns {API.Multibase<Prefix>}
*/

@@ -57,23 +46,14 @@ encode (bytes) {

* @template {string} Prefix
* @typedef {import('./interface').MultibaseDecoder<Prefix>} MultibaseDecoder
*/
/**
* @template {string} Prefix
* @typedef {import('./interface').UnibaseDecoder<Prefix>} UnibaseDecoder
*/
/**
* @template {string} Prefix
*/
/**
* Class represents both BaseDecoder and MultibaseDecoder so it could be used
* to decode multibases (with matching prefix) or just base decode strings
* with corresponding base encoding.
*
* @class
* @template {string} Base
* @template {string} Prefix
* @implements {MultibaseDecoder<Prefix>}
* @implements {UnibaseDecoder<Prefix>}
* @implements {BaseDecoder}
* @implements {API.MultibaseDecoder<Prefix>}
* @implements {API.UnibaseDecoder<Prefix>}
* @implements {API.BaseDecoder}
*/

@@ -114,3 +94,3 @@ class Decoder {

* @template {string} OtherPrefix
* @param {UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @returns {ComposedDecoder<Prefix|OtherPrefix>}

@@ -125,3 +105,3 @@ */

* @template {string} Prefix
* @typedef {import('./interface').CombobaseDecoder<Prefix>} CombobaseDecoder
* @typedef {Record<Prefix, API.UnibaseDecoder<Prefix>>} Decoders
*/

@@ -131,13 +111,8 @@

* @template {string} Prefix
* @typedef {Record<Prefix, UnibaseDecoder<Prefix>>} Decoders
* @implements {API.MultibaseDecoder<Prefix>}
* @implements {API.CombobaseDecoder<Prefix>}
*/
/**
* @template {string} Prefix
* @implements {MultibaseDecoder<Prefix>}
* @implements {CombobaseDecoder<Prefix>}
*/
class ComposedDecoder {
/**
* @param {Record<Prefix, UnibaseDecoder<Prefix>>} decoders
* @param {Decoders<Prefix>} decoders
*/

@@ -150,3 +125,3 @@ constructor (decoders) {

* @template {string} OtherPrefix
* @param {UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @returns {ComposedDecoder<Prefix|OtherPrefix>}

@@ -176,26 +151,21 @@ */

* @template {string} R
* @param {UnibaseDecoder<L>|CombobaseDecoder<L>} left
* @param {UnibaseDecoder<R>|CombobaseDecoder<R>} right
* @param {API.UnibaseDecoder<L>|API.CombobaseDecoder<L>} left
* @param {API.UnibaseDecoder<R>|API.CombobaseDecoder<R>} right
* @returns {ComposedDecoder<L|R>}
*/
export const or = (left, right) => new ComposedDecoder(/** @type {Decoders<L|R>} */({
...(left.decoders || { [/** @type UnibaseDecoder<L> */(left).prefix]: left }),
...(right.decoders || { [/** @type UnibaseDecoder<R> */(right).prefix]: right })
...(left.decoders || { [/** @type API.UnibaseDecoder<L> */(left).prefix]: left }),
...(right.decoders || { [/** @type API.UnibaseDecoder<R> */(right).prefix]: right })
}))
/**
* @template T
* @typedef {import('./interface').MultibaseCodec<T>} MultibaseCodec
*/
/**
* @class
* @template {string} Base
* @template {string} Prefix
* @implements {MultibaseCodec<Prefix>}
* @implements {MultibaseEncoder<Prefix>}
* @implements {MultibaseDecoder<Prefix>}
* @implements {BaseCodec}
* @implements {BaseEncoder}
* @implements {BaseDecoder}
* @implements {API.MultibaseCodec<Prefix>}
* @implements {API.MultibaseEncoder<Prefix>}
* @implements {API.MultibaseDecoder<Prefix>}
* @implements {API.BaseCodec}
* @implements {API.BaseEncoder}
* @implements {API.BaseDecoder}
*/

@@ -236,3 +206,3 @@ export class Codec {

* @template {string} Prefix
* @param {Object} options
* @param {object} options
* @param {Base} options.name

@@ -250,3 +220,3 @@ * @param {Prefix} options.prefix

* @template {string} Prefix
* @param {Object} options
* @param {object} options
* @param {Base} options.name

@@ -369,3 +339,3 @@ * @param {Prefix} options.prefix

* @template {string} Prefix
* @param {Object} options
* @param {object} options
* @param {Base} options.name

@@ -372,0 +342,0 @@ * @param {Prefix} options.prefix

@@ -11,5 +11,6 @@ // Base encoders / decoders just base encode / decode between binary and

* `encode` no multibase prefix is added.
*
* @param bytes
*/
baseEncode(bytes: Uint8Array): string
baseEncode: (bytes: Uint8Array) => string
}

@@ -24,5 +25,6 @@

* decode
*
* @param text
*/
baseDecode(text: string): Uint8Array
baseDecode: (text: string) => Uint8Array
}

@@ -63,3 +65,3 @@

*/
encode(bytes: Uint8Array): Multibase<Prefix>
encode: (bytes: Uint8Array) => Multibase<Prefix>
}

@@ -77,5 +79,6 @@

* If prefix does not match
*
* @param multibase
*/
decode(multibase: Multibase<Prefix>): Uint8Array
decode: (multibase: Multibase<Prefix>) => Uint8Array
}

@@ -93,3 +96,2 @@

export interface UnibaseDecoder<Prefix extends string> extends MultibaseDecoder<Prefix> {

@@ -96,0 +98,0 @@ // Reserve this property so it can be used to derive type.

import { bytes as binary, CID } from './index.js'
// Linter can see that API is used in types.
// eslint-disable-next-line
import * as API from './interface.js'
const readonly = ({ enumerable = true, configurable = false } = {}) => ({
enumerable,
configurable,
writable: false
})
function readonly ({ enumerable = true, configurable = false } = {}) {
return { enumerable, configurable, writable: false }
}
/**
* @param {[string|number, string]} path
* @param {any} value
* @returns {Iterable<[string, CID]>}
*/
function * linksWithin (path, value) {
if (value != null && typeof value === 'object') {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
const cid = CID.asCID(element)
if (cid) {
yield [elementPath.join('/'), cid]
} else if (typeof element === 'object') {
yield * links(element, elementPath)
}
}
} else {
const cid = CID.asCID(value)
if (cid) {
yield [path.join('/'), cid]
} else {
yield * links(value, path)
}
}
}
}
/**
* @template T

@@ -15,27 +44,28 @@ * @param {T} source

*/
const links = function * (source, base) {
if (source == null) return
if (source instanceof Uint8Array) return
function * links (source, base) {
if (source == null || source instanceof Uint8Array) {
return
}
for (const [key, value] of Object.entries(source)) {
const path = [...base, key]
if (value != null && typeof value === 'object') {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
const cid = CID.asCID(element)
if (cid) {
yield [elementPath.join('/'), cid]
} else if (typeof element === 'object') {
yield * links(element, elementPath)
}
}
} else {
const cid = CID.asCID(value)
if (cid) {
yield [path.join('/'), cid]
} else {
yield * links(value, path)
}
const path = /** @type {[string|number, string]} */ ([...base, key])
yield * linksWithin(path, value)
}
}
/**
* @param {[string|number, string]} path
* @param {any} value
* @returns {Iterable<string>}
*/
function * treeWithin (path, value) {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
yield elementPath.join('/')
if (typeof element === 'object' && !CID.asCID(element)) {
yield * tree(element, elementPath)
}
}
} else {
yield * tree(value, path)
}

@@ -50,19 +80,11 @@ }

*/
const tree = function * (source, base) {
if (source == null) return
function * tree (source, base) {
if (source == null || typeof source !== 'object') {
return
}
for (const [key, value] of Object.entries(source)) {
const path = [...base, key]
const path = /** @type {[string|number, string]} */ ([...base, key])
yield path.join('/')
if (value != null && !(value instanceof Uint8Array) && typeof value === 'object' && !CID.asCID(value)) {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
yield elementPath.join('/')
if (typeof element === 'object' && !CID.asCID(element)) {
yield * tree(element, elementPath)
}
}
} else {
yield * tree(value, path)
}
yield * treeWithin(path, value)
}

@@ -73,8 +95,10 @@ }

/**
*
* @template T
* @param {T} source
* @param {string[]} path
* @returns {API.BlockCursorView<unknown>}
*/
const get = (source, path) => {
let node = /** @type {Record<string, any>} */ (source)
function get (source, path) {
let node = /** @type {Record<string, any>} */(source)
for (const [index, key] of path.entries()) {

@@ -94,13 +118,17 @@ node = node[key]

/**
* @template T
* @template {unknown} T - Logical type of the data encoded in the block
* @template {number} C - multicodec code corresponding to codec used to encode the block
* @template {number} A - multicodec code corresponding to the hashing algorithm used in CID creation.
* @template {API.Version} V - CID version
* @implements {API.BlockView<T, C, A, V>}
*/
class Block {
/**
* @param {Object} options
* @param {CID} options.cid
* @param {ByteView<T>} options.bytes
* @param {object} options
* @param {CID<T, C, A, V>} options.cid
* @param {API.ByteView<T>} options.bytes
* @param {T} options.value
*/
constructor ({ cid, bytes, value }) {
if (!cid || !bytes || typeof value === 'undefined') throw new Error('Missing required argument')
if (!cid || !bytes || typeof value === 'undefined') { throw new Error('Missing required argument') }

@@ -130,4 +158,6 @@ this.cid = cid

/**
* @param {string} [path]
*/
*
* @param {string} [path]
* @returns {API.BlockCursorView<unknown>}
*/
get (path = '/') {

@@ -139,12 +169,12 @@ return get(this.value, path.split('/').filter(Boolean))

/**
* @template T
* @template {number} Code
* @template {number} Algorithm
* @param {Object} options
* @template {unknown} T - Logical type of the data encoded in the block
* @template {number} Code - multicodec code corresponding to codec used to encode the block
* @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
* @param {object} options
* @param {T} options.value
* @param {BlockEncoder<Code, T>} options.codec
* @param {Hasher<Algorithm>} options.hasher
* @returns {Promise<Block<T>>}
* @param {API.BlockEncoder<Code, T>} options.codec
* @param {API.MultihashHasher<Alg>} options.hasher
* @returns {Promise<API.BlockView<T, Code, Alg>>}
*/
const encode = async ({ value, codec, hasher }) => {
async function encode ({ value, codec, hasher }) {
if (typeof value === 'undefined') throw new Error('Missing required argument "value"')

@@ -155,3 +185,8 @@ if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher')

const hash = await hasher.digest(bytes)
const cid = CID.create(1, codec.code, hash)
/** @type {CID<T, Code, Alg, 1>} */
const cid = CID.create(
1,
codec.code,
hash
)

@@ -162,12 +197,12 @@ return new Block({ value, bytes, cid })

/**
* @template T
* @template {number} Code
* @template {number} Algorithm
* @param {Object} options
* @param {ByteView<T>} options.bytes
* @param {BlockDecoder<Code, T>} options.codec
* @param {Hasher<Algorithm>} options.hasher
* @returns {Promise<Block<T>>}
* @template {unknown} T - Logical type of the data encoded in the block
* @template {number} Code - multicodec code corresponding to codec used to encode the block
* @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
* @param {object} options
* @param {API.ByteView<T>} options.bytes
* @param {API.BlockDecoder<Code, T>} options.codec
* @param {API.MultihashHasher<Alg>} options.hasher
* @returns {Promise<API.BlockView<T, Code, Alg>>}
*/
const decode = async ({ bytes, codec, hasher }) => {
async function decode ({ bytes, codec, hasher }) {
if (!bytes) throw new Error('Missing required argument "bytes"')

@@ -178,2 +213,3 @@ if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher')

const hash = await hasher.digest(bytes)
/** @type {CID<T, Code, Alg, 1>} */
const cid = CID.create(1, codec.code, hash)

@@ -185,3 +221,3 @@

/**
* @typedef {Object} RequiredCreateOptions
* @typedef {object} RequiredCreateOptions
* @property {CID} options.cid

@@ -191,8 +227,10 @@ */

/**
* @template T
* @template {number} Code
* @param {{ cid: CID, value:T, codec?: BlockDecoder<Code, T>, bytes: ByteView<T> }|{cid:CID, bytes:ByteView<T>, value?:void, codec:BlockDecoder<Code, T>}} options
* @returns {Block<T>}
* @template {unknown} T - Logical type of the data encoded in the block
* @template {number} Code - multicodec code corresponding to codec used to encode the block
* @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
* @template {API.Version} V - CID version
* @param {{ cid: API.Link<T, Code, Alg, V>, value:T, codec?: API.BlockDecoder<Code, T>, bytes: API.ByteView<T> }|{cid:API.Link<T, Code, Alg, V>, bytes:API.ByteView<T>, value?:void, codec:API.BlockDecoder<Code, T>}} options
* @returns {API.BlockView<T, Code, Alg, V>}
*/
const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
function createUnsafe ({ bytes, cid, value: maybeValue, codec }) {
const value = maybeValue !== undefined

@@ -204,17 +242,23 @@ ? maybeValue

return new Block({ cid, bytes, value })
return new Block({
// eslint-disable-next-line object-shorthand
cid: /** @type {CID<T, Code, Alg, V>} */ (cid),
bytes,
value
})
}
/**
* @template T
* @template {number} Code
* @template {number} Algorithm
* @param {Object} options
* @param {CID} options.cid
* @param {ByteView<T>} options.bytes
* @param {BlockDecoder<Code, T>} options.codec
* @param {Hasher<Algorithm>} options.hasher
* @returns {Promise<Block<T>>}
* @template {unknown} T - Logical type of the data encoded in the block
* @template {number} Code - multicodec code corresponding to codec used to encode the block
* @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
* @template {API.Version} V - CID version
* @param {object} options
* @param {API.Link<T, Code, Alg, V>} options.cid
* @param {API.ByteView<T>} options.bytes
* @param {API.BlockDecoder<Code, T>} options.codec
* @param {API.MultihashHasher<Alg>} options.hasher
* @returns {Promise<API.BlockView<T, Code, Alg, V>>}
*/
const create = async ({ bytes, cid, hasher, codec }) => {
async function create ({ bytes, cid, hasher, codec }) {
if (!bytes) throw new Error('Missing required argument "bytes"')

@@ -228,27 +272,10 @@ if (!hasher) throw new Error('Missing required argument "hasher"')

return createUnsafe({ bytes, cid, value, codec })
return createUnsafe({
bytes,
cid,
value,
codec
})
}
export { encode, decode, create, createUnsafe, Block }
/**
* @template T
* @typedef {import('./codecs/interface').ByteView<T>} ByteView
*/
/**
* @template {number} Code
* @template T
* @typedef {import('./codecs/interface').BlockEncoder<Code, T>} BlockEncoder
*/
/**
* @template {number} Code
* @template T
* @typedef {import('./codecs/interface').BlockDecoder<Code, T>} BlockDecoder
*/
/**
* @template Algorithm
* @typedef {import('./hashes/interface').MultihashHasher} Hasher
*/

@@ -6,16 +6,57 @@ import * as varint from './varint.js'

import { coerce } from './bytes.js'
// Linter can see that API is used in types.
// eslint-disable-next-line
import * as API from "./link/interface.js"
// This way TS will also expose all the types from module
export * from './link/interface.js'
/**
* @typedef {import('./hashes/interface').MultihashDigest} MultihashDigest
* @typedef {0 | 1} CIDVersion
* @template {API.Link<unknown, number, number, API.Version>} T
* @template {string} Prefix
* @param {T} link
* @param {API.MultibaseEncoder<Prefix>} [base]
* @returns {API.ToString<T, Prefix>}
*/
export const format = (link, base) => {
const { bytes, version } = link
switch (version) {
case 0:
return toStringV0(
bytes,
baseCache(link),
/** @type {API.MultibaseEncoder<"z">} */ (base) || base58btc.encoder
)
default:
return toStringV1(
bytes,
baseCache(link),
/** @type {API.MultibaseEncoder<Prefix>} */ (base || base32.encoder)
)
}
}
/** @type {WeakMap<API.UnknownLink, Map<string, string>>} */
const cache = new WeakMap()
/**
* @template Prefix
* @typedef {import('./bases/interface').MultibaseEncoder<Prefix>} MultibaseEncoder
* @param {API.UnknownLink} cid
* @returns {Map<string, string>}
*/
const baseCache = cid => {
const baseCache = cache.get(cid)
if (baseCache == null) {
const baseCache = new Map()
cache.set(cid, baseCache)
return baseCache
}
return baseCache
}
/**
* @template Prefix
* @typedef {import('./bases/interface').MultibaseDecoder<Prefix>} MultibaseDecoder
* @template {unknown} [Data=unknown]
* @template {number} [Format=number]
* @template {number} [Alg=number]
* @template {API.Version} [Version=API.Version]
* @implements {API.Link<Data, Format, Alg, Version>}
*/

@@ -25,5 +66,5 @@

/**
* @param {CIDVersion} version
* @param {number} code - multicodec code, see https://github.com/multiformats/multicodec/blob/master/table.csv
* @param {MultihashDigest} multihash
* @param {Version} version - Version of the CID
* @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
* @param {API.MultihashDigest<Alg>} multihash - (Multi)hash of the of the content.
* @param {Uint8Array} bytes

@@ -33,19 +74,20 @@ *

constructor (version, code, multihash, bytes) {
/** @readonly */
this.code = code
/** @readonly */
this.version = version
/** @readonly */
this.multihash = multihash
/** @readonly */
this.bytes = bytes
// ArrayBufferView
/** @readonly */
this.byteOffset = bytes.byteOffset
/** @readonly */
this.byteLength = bytes.byteLength
// Circular reference
/** @private */
/** @readonly */
this.asCID = this
/**
* @type {Map<string, string>}
* @private
*/
this._baseCache = new Map()

@@ -62,3 +104,2 @@ // Configure private properties

_baseCache: hidden,
asCID: hidden

@@ -69,3 +110,3 @@ })

/**
* @returns {CID}
* @returns {CID<Data, API.DAG_PB, API.SHA_256, 0>}
*/

@@ -75,5 +116,5 @@ toV0 () {

case 0: {
return this
return /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */ (this)
}
default: {
case 1: {
const { code, multihash } = this

@@ -90,4 +131,13 @@

return CID.createV0(multihash)
return /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */ (
CID.createV0(
/** @type {API.MultihashDigest<API.SHA_256>} */ (multihash)
)
)
}
default: {
throw Error(
`Can not convert CID version ${this.version} to version 0. This is a bug please report`
)
}
}

@@ -97,3 +147,3 @@ }

/**
* @returns {CID}
* @returns {CID<Data, Format, Alg, 1>}
*/

@@ -105,10 +155,13 @@ toV1 () {

const multihash = Digest.create(code, digest)
return CID.createV1(this.code, multihash)
return /** @type {CID<Data, Format, Alg, 1>} */ (
CID.createV1(this.code, multihash)
)
}
case 1: {
return this
return /** @type {CID<Data, Format, Alg, 1>} */ (this)
}
/* c8 ignore next 3 */
default: {
throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)
throw Error(
`Can not convert CID version ${this.version} to version 1. This is a bug please report`
)
}

@@ -119,23 +172,37 @@ }

/**
* @param {any} other
* @param {unknown} other
* @returns {other is CID<Data, Format, Alg, Version>}
*/
equals (other) {
return other &&
this.code === other.code &&
this.version === other.version &&
Digest.equals(this.multihash, other.multihash)
return CID.equals(this, other)
}
/**
* @param {MultibaseEncoder<any>} [base]
* @template {unknown} Data
* @template {number} Format
* @template {number} Alg
* @template {API.Version} Version
* @param {API.Link<Data, Format, Alg, Version>} self
* @param {unknown} other
* @returns {other is CID}
*/
static equals (self, other) {
const unknown =
/** @type {{code?:unknown, version?:unknown, multihash?:unknown}} */ (
other
)
return (
unknown &&
self.code === unknown.code &&
self.version === unknown.version &&
Digest.equals(self.multihash, unknown.multihash)
)
}
/**
* @param {API.MultibaseEncoder<string>} [base]
* @returns {string}
*/
toString (base) {
const { bytes, version, _baseCache } = this
switch (version) {
case 0:
return toStringV0(bytes, _baseCache, base || base58btc.encoder)
default:
return toStringV1(bytes, _baseCache, base || base32.encoder)
}
return format(this, base)
}

@@ -151,2 +218,6 @@

link () {
return this
}
get [Symbol.toStringTag] () {

@@ -159,3 +230,3 @@ return 'CID'

[Symbol.for('nodejs.util.inspect.custom')] () {
return 'CID(' + this.toString() + ')'
return `CID(${this.toString()})`
}

@@ -171,3 +242,3 @@

deprecate(/^0\.0/, IS_CID_DEPRECATION)
return !!(value && (value[cidSymbol] || value.asCID === value))
return Boolean(value && (value[cidSymbol] || value.asCID === value))
}

@@ -180,7 +251,11 @@

get codec () {
throw new Error('"codec" property is deprecated, use integer "code" property instead')
throw new Error(
'"codec" property is deprecated, use integer "code" property instead'
)
}
get buffer () {
throw new Error('Deprecated .buffer property, use .bytes to get Uint8Array instead')
throw new Error(
'Deprecated .buffer property, use .bytes to get Uint8Array instead'
)
}

@@ -205,27 +280,41 @@

* co-exist and interop as long as binary interface is compatible.
* @param {any} value
* @returns {CID|null}
*
* @template {unknown} Data
* @template {number} Format
* @template {number} Alg
* @template {API.Version} Version
* @template {unknown} U
* @param {API.Link<Data, Format, Alg, Version>|U} input
* @returns {CID<Data, Format, Alg, Version>|null}
*/
static asCID (value) {
static asCID (input) {
const value = /** @type {any} */ (input)
if (value instanceof CID) {
// If value is instance of CID then we're all set.
// If value is instance of CID then we're all set.
return value
} else if (value != null && value.asCID === value) {
// If value isn't instance of this CID class but `this.asCID === this` is
// true it is CID instance coming from a different implementation (diff
// version or duplicate). In that case we rebase it to this `CID`
// implementation so caller is guaranteed to get instance with expected
// API.
// If value isn't instance of this CID class but `this.asCID === this` is
// true it is CID instance coming from a different implementation (diff
// version or duplicate). In that case we rebase it to this `CID`
// implementation so caller is guaranteed to get instance with expected
// API.
const { version, code, multihash, bytes } = value
return new CID(version, code, multihash, bytes || encodeCID(version, code, multihash.bytes))
return new CID(
version,
code,
/** @type {API.MultihashDigest<Alg>} */ (multihash),
bytes || encodeCID(version, code, multihash.bytes)
)
} else if (value != null && value[cidSymbol] === true) {
// If value is a CID from older implementation that used to be tagged via
// symbol we still rebase it to this `CID` implementation by
// delegating that to a constructor.
// If value is a CID from older implementation that used to be tagged via
// symbol we still rebase it to the this `CID` implementation by
// delegating that to a constructor.
const { version, multihash, code } = value
const digest = Digest.decode(multihash)
const digest =
/** @type {API.MultihashDigest<Alg>} */
(Digest.decode(multihash))
return CID.create(version, code, digest)
} else {
// Otherwise value is not a CID (or an incompatible version of it) in
// which case we return `null`.
// Otherwise value is not a CID (or an incompatible version of it) in
// which case we return `null`.
return null

@@ -236,8 +325,12 @@ }

/**
*
* @param {CIDVersion} version - Version of the CID
* @param {number} code - Code of the codec content is encoded in.
* @param {MultihashDigest} digest - (Multi)hash of the of the content.
* @returns {CID}
*/
*
* @template {unknown} Data
* @template {number} Format
* @template {number} Alg
* @template {API.Version} Version
* @param {Version} version - Version of the CID
* @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
* @param {API.MultihashDigest<Alg>} digest - (Multi)hash of the of the content.
* @returns {CID<Data, Format, Alg, Version>}
*/
static create (version, code, digest) {

@@ -251,3 +344,5 @@ if (typeof code !== 'number') {

if (code !== DAG_PB_CODE) {
throw new Error(`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`)
throw new Error(
`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`
)
} else {

@@ -269,3 +364,6 @@ return new CID(version, code, digest, digest.bytes)

* Simplified version of `create` for CIDv0.
* @param {MultihashDigest} digest - Multihash.
*
* @template {unknown} [T=unknown]
* @param {API.MultihashDigest<typeof SHA_256_CODE>} digest - Multihash.
* @returns {CID<T, typeof DAG_PB_CODE, typeof SHA_256_CODE, 0>}
*/

@@ -277,8 +375,11 @@ static createV0 (digest) {

/**
* Simplified version of `create` for CIDv1.
* @template {number} Code
* @param {Code} code - Content encoding format code.
* @param {MultihashDigest} digest - Miltihash of the content.
* @returns {CID}
*/
* Simplified version of `create` for CIDv1.
*
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @param {Code} code - Content encoding format code.
* @param {API.MultihashDigest<Alg>} digest - Miltihash of the content.
* @returns {CID<Data, Code, Alg, 1>}
*/
static createV1 (code, digest) {

@@ -295,4 +396,8 @@ return CID.create(1, code, digest)

*
* @param {Uint8Array} bytes
* @returns {CID}
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @template {API.Version} Ver
* @param {API.ByteView<API.Link<Data, Code, Alg, Ver>>} bytes
* @returns {CID<Data, Code, Alg, Ver>}
*/

@@ -316,4 +421,8 @@ static decode (bytes) {

*
* @param {Uint8Array} bytes
* @returns {[CID, Uint8Array]}
* @template {unknown} T
* @template {number} C
* @template {number} A
* @template {API.Version} V
* @param {API.ByteView<API.Link<T, C, A, V>>} bytes
* @returns {[CID<T, C, A, V>, Uint8Array]}
*/

@@ -323,10 +432,22 @@ static decodeFirst (bytes) {

const prefixSize = specs.size - specs.multihashSize
const multihashBytes = coerce(bytes.subarray(prefixSize, prefixSize + specs.multihashSize))
const multihashBytes = coerce(
bytes.subarray(prefixSize, prefixSize + specs.multihashSize)
)
if (multihashBytes.byteLength !== specs.multihashSize) {
throw new Error('Incorrect length')
}
const digestBytes = multihashBytes.subarray(specs.multihashSize - specs.digestSize)
const digest = new Digest.Digest(specs.multihashCode, specs.digestSize, digestBytes, multihashBytes)
const cid = specs.version === 0 ? CID.createV0(digest) : CID.createV1(specs.codec, digest)
return [cid, bytes.subarray(specs.size)]
const digestBytes = multihashBytes.subarray(
specs.multihashSize - specs.digestSize
)
const digest = new Digest.Digest(
specs.multihashCode,
specs.digestSize,
digestBytes,
multihashBytes
)
const cid =
specs.version === 0
? CID.createV0(/** @type {API.MultihashDigest<API.SHA_256>} */ (digest))
: CID.createV1(specs.codec, digest)
return [/** @type {CID<T, C, A, V>} */(cid), bytes.subarray(specs.size)]
}

@@ -343,4 +464,8 @@

*
* @param {Uint8Array} initialBytes
* @returns {{ version:CIDVersion, codec:number, multihashCode:number, digestSize:number, multihashSize:number, size:number }}
* @template {unknown} T
* @template {number} C
* @template {number} A
* @template {API.Version} V
* @param {API.ByteView<API.Link<T, C, A, V>>} initialBytes
* @returns {{ version:V, codec:C, multihashCode:A, digestSize:number, multihashSize:number, size:number }}
*/

@@ -355,9 +480,10 @@ static inspectBytes (initialBytes) {

let version = next()
let codec = DAG_PB_CODE
if (version === 18) { // CIDv0
version = 0
let version = /** @type {V} */ (next())
let codec = /** @type {C} */ (DAG_PB_CODE)
if (/** @type {number} */(version) === 18) {
// CIDv0
version = /** @type {V} */ (0)
offset = 0
} else if (version === 1) {
codec = next()
} else {
codec = /** @type {C} */ (next())
}

@@ -370,3 +496,3 @@

const prefixSize = offset
const multihashCode = next() // multihash code
const multihashCode = /** @type {A} */ (next()) // multihash code
const digestSize = next() // multihash length

@@ -386,4 +512,9 @@ const size = offset + digestSize

* @template {string} Prefix
* @param {string} source
* @param {MultibaseDecoder<Prefix>} [base]
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @template {API.Version} Ver
* @param {API.ToString<API.Link<Data, Code, Alg, Ver>, Prefix>} source
* @param {API.MultibaseDecoder<Prefix>} [base]
* @returns {CID<Data, Code, Alg, Ver>}
*/

@@ -394,5 +525,5 @@ static parse (source, base) {

const cid = CID.decode(bytes)
// Cache string representation to avoid computing it on `this.toString()`
// @ts-ignore - Can't access private
cid._baseCache.set(prefix, source)
baseCache(cid).set(prefix, source)

@@ -405,5 +536,9 @@ return cid

* @template {string} Prefix
* @param {string} source
* @param {MultibaseDecoder<Prefix>} [base]
* @returns {[string, Uint8Array]}
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @template {API.Version} Ver
* @param {API.ToString<API.Link<Data, Code, Alg, Ver>, Prefix>} source
* @param {API.MultibaseDecoder<Prefix>} [base]
* @returns {[Prefix, API.ByteView<API.Link<Data, Code, Alg, Ver>>]}
*/

@@ -415,17 +550,22 @@ const parseCIDtoBytes = (source, base) => {

const decoder = base || base58btc
return [base58btc.prefix, decoder.decode(`${base58btc.prefix}${source}`)]
return [
/** @type {Prefix} */ (base58btc.prefix),
decoder.decode(`${base58btc.prefix}${source}`)
]
}
case base58btc.prefix: {
const decoder = base || base58btc
return [base58btc.prefix, decoder.decode(source)]
return [/** @type {Prefix} */(base58btc.prefix), decoder.decode(source)]
}
case base32.prefix: {
const decoder = base || base32
return [base32.prefix, decoder.decode(source)]
return [/** @type {Prefix} */(base32.prefix), decoder.decode(source)]
}
default: {
if (base == null) {
throw Error('To parse non base32 or base58btc encoded CID multibase decoder must be provided')
throw Error(
'To parse non base32 or base58btc encoded CID multibase decoder must be provided'
)
}
return [source[0], base.decode(source)]
return [/** @type {Prefix} */(source[0]), base.decode(source)]
}

@@ -439,3 +579,3 @@ }

* @param {Map<string, string>} cache
* @param {MultibaseEncoder<'z'>} base
* @param {API.MultibaseEncoder<'z'>} base
*/

@@ -462,3 +602,3 @@ const toStringV0 = (bytes, cache, base) => {

* @param {Map<string, string>} cache
* @param {MultibaseEncoder<Prefix>} base
* @param {API.MultibaseEncoder<Prefix>} base
*/

@@ -481,3 +621,3 @@ const toStringV1 = (bytes, cache, base) => {

/**
* @param {CIDVersion} version
* @param {API.Version} version
* @param {number} code

@@ -511,5 +651,6 @@ * @param {Uint8Array} multihash

const deprecate = (range, message) => {
/* eslint-disable no-console */
if (range.test(version)) {
console.warn(message)
/* c8 ignore next 3 */
/* c8 ignore next 3 */
} else {

@@ -520,4 +661,3 @@ throw new Error(message)

const IS_CID_DEPRECATION =
`CID.isCID(v) is deprecated and will be removed in the next major release.
const IS_CID_DEPRECATION = `CID.isCID(v) is deprecated and will be removed in the next major release.
Following code pattern:

@@ -524,0 +664,0 @@

@@ -0,1 +1,3 @@

import type { ByteView } from '../block/interface'
/**

@@ -7,3 +9,3 @@ * IPLD encoder part of the codec.

code: Code
encode(data: T): ByteView<T>
encode: (data: T) => ByteView<T>
}

@@ -16,3 +18,3 @@

code: Code
decode(bytes: ByteView<T>): T
decode: (bytes: ByteView<T>) => T
}

@@ -25,7 +27,2 @@

// This just a hack to retain type information about the data that
// is encoded `T` Because it's a union `data` field is never going
// to be usable anyway.
export type ByteView<T> =
| Uint8Array
| Uint8Array & { data: T }
export type { ByteView }

@@ -6,2 +6,3 @@ import { coerce, equals as equalBytes } from '../bytes.js'

* Creates a multihash digest.
*
* @template {number} Code

@@ -26,2 +27,3 @@ * @param {Code} code

* Turns bytes representation of multihash digest into an instance.
*
* @param {Uint8Array} multihash

@@ -45,4 +47,4 @@ * @returns {MultihashDigest}

* @param {MultihashDigest} a
* @param {MultihashDigest} b
* @returns {boolean}
* @param {unknown} b
* @returns {b is MultihashDigest}
*/

@@ -53,3 +55,10 @@ export const equals = (a, b) => {

} else {
return a.code === b.code && a.size === b.size && equalBytes(a.bytes, b.bytes)
const data = /** @type {{code?:unknown, size?:unknown, bytes?:unknown}} */(b)
return (
a.code === data.code &&
a.size === data.size &&
data.bytes instanceof Uint8Array &&
equalBytes(a.bytes, data.bytes)
)
}

@@ -65,2 +74,3 @@ }

* hashing alogrithm and an actual hash digest.
*
* @template {number} Code

@@ -74,2 +84,3 @@ * @template {number} Size

* Creates a multihash digest.
*
* @param {Code} code

@@ -76,0 +87,0 @@ * @param {Size} size

@@ -6,3 +6,3 @@ import * as Digest from './digest.js'

* @template {number} Code
* @param {Object} options
* @param {object} options
* @param {Name} options.name

@@ -9,0 +9,0 @@ * @param {Code} options.code

@@ -47,3 +47,3 @@ // # Multihash

*/
digest(input: Uint8Array): Promise<MultihashDigest> | MultihashDigest
digest: (input: Uint8Array) => Promise<MultihashDigest<Code>> | MultihashDigest<Code>

@@ -72,3 +72,3 @@ /**

export interface SyncMultihashHasher<Code extends number = number> extends MultihashHasher<Code> {
digest(input: Uint8Array): MultihashDigest
digest: (input: Uint8Array) => MultihashDigest<Code>
}

@@ -6,3 +6,5 @@ import { CID } from './cid.js'

import * as digest from './hashes/digest.js'
// This way TS will also expose all the types from module
export * from './interface.js'
export { CID, hasher, digest, varint, bytes }
import { base58btc } from './bases/base58.js'
/**
* @typedef {import('./cid.js').CID} CID
* @template [C=number] - multicodec code corresponding to codec used to encode the block
* @template [A=number] - multicodec code corresponding to the hashing algorithm used in CID creation.
* @template [V=0|1] - CID version
* @typedef {import('./cid').CID<unknown, C, A, V>} CID
*/
/**
* @template T
* @typedef {import('./block.js').Block<T>} Block
* @template [T=unknown] - Logical type of the data encoded in the block
* @template [C=number] - multicodec code corresponding to codec used to encode the block
* @template [A=number] - multicodec code corresponding to the hashing algorithm used in CID creation.
* @template [V=0|1] - CID version
* @typedef {import('./block/interface').BlockView<T, C, A, V>} BlockView
*/
/**
* @template T
* @param {Object} options
* @param {object} options
* @param {CID} options.cid
* @param {(cid: CID) => Promise<Block<T>|null>} options.load
* @param {(cid: CID) => Promise<BlockView|null>} options.load
* @param {Set<string>} [options.seen]

@@ -18,0 +23,0 @@ */

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc