{"name":"@ipld/dag-pb","version":"4.1.7","keywords":["ipfs","ipld","multiformats"],"author":{"url":"http://r.va.gg/","name":"Rod","email":"rod@vagg.org"},"license":"Apache-2.0 OR MIT","_id":"@ipld/dag-pb@4.1.7","maintainers":[{"name":"daviddias","email":"mail@daviddias.me"},{"name":"mikeal","email":"mikeal.rogers@gmail.com"},{"name":"vmx","email":"volker.mische@gmail.com"},{"name":"alanshaw","email":"alan138@gmail.com"},{"name":"achingbrain","email":"alex@achingbrain.net"},{"name":"vascosantos","email":"santos.vasco10@gmail.com"},{"name":"rvagg","email":"r@va.gg"},{"name":"lidel","email":"lidel@lidel.org"},{"name":"npm-service-account-ipld","email":"npm-service-account+ipld@ipfs.tech"}],"homepage":"https://github.com/ipld/js-dag-pb#readme","bugs":{"url":"https://github.com/ipld/js-dag-pb/issues"},"dist":{"shasum":"8133f099cf0172d41238b23a30355db565d19413","tarball":"http://registry-npm.ywwl.com/@ipld/dag-pb/download/@ipld/dag-pb-4.1.7.tgz","fileCount":21,"integrity":"sha512-/i/13trFihjWfDyXlylRwhuYjtzYjvOFw0vlRjYGnZuv7d7MOgA2lV/vRuL5RfeUajM03aZfFLdq4S7cTbbTRg==","signatures":[{"sig":"MEQCIC4U2GXBLlqhFIwNsqr+06VXnA4+8nzPf74YiEUaSpHcAiAOwD3GpF0Y2y/wxV3liEBe1ybSoIA4y4pUCAjajOYwYQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@ipld%2fdag-pb@4.1.7","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":142380,"size":39068,"noattachment":false,"key":"/@ipld/dag-pb/-/@ipld/dag-pb-4.1.7.tgz"},"type":"module","types":"./dist/src/index.d.ts","engines":{"npm":">=7.0.0","node":">=16.0.0"},"exports":{".":{"types":"./dist/src/index.d.ts","import":"./src/index.js"}},"gitHead":"3b870f604cac5a512a2244015c795d18923ec98b","release":{"plugins":[["@semantic-release/commit-analyzer",{"preset":"conventionalcommits","releaseRules":[{"release":"major","breaking":true},{"revert":true,"release":"patch"},{"type":"feat","release":"minor"},{"type":"fix","release":"patch"},{"type":"docs","release":"patch"},{"type":"test","release":"patch"},{"type":"deps","release":"patch"},{"scope":"no-release","release":false}]}],["@semantic-release/release-notes-generator",{"preset":"conventionalcommits","presetConfig":{"types":[{"type":"feat","section":"Features"},{"type":"fix","section":"Bug Fixes"},{"type":"chore","section":"Trivial Changes"},{"type":"docs","section":"Documentation"},{"type":"deps","section":"Dependencies"},{"type":"test","section":"Tests"}]}}],"@semantic-release/changelog","@semantic-release/npm","@semantic-release/github",["@semantic-release/git",{"assets":["CHANGELOG.md","package.json"]}]],"branches":["master"]},"scripts":{"docs":"aegir docs","lint":"aegir lint","test":"aegir lint && aegir test","build":"aegir build","clean":"aegir clean","release":"aegir release","test:ts":"npm run test --prefix test/ts-use","dep-check":"aegir dep-check -i @ipld/dag-pb","test:node":"aegir test -t node --cov","test:chrome":"aegir test -t browser --cov","test:firefox":"aegir test -t browser -- --browser firefox","test:electron-main":"aegir test -t electron-main","test:chrome-webworker":"aegir test -t webworker","test:firefox-webworker":"aegir test -t webworker -- --browser firefox"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:ee2284f7-d33f-439b-bd08-2bbf551e7632"}},"repository":{"url":"git+https://github.com/ipld/js-dag-pb.git","type":"git"},"_npmVersion":"11.14.1","description":"JS implementation of DAG-PB","directories":{},"_nodeVersion":"24.14.1","dependencies":{"multiformats":"^14.0.0"},"eslintConfig":{"extends":"ipfs","parserOptions":{"sourceType":"module"},"ignorePatterns":["dist","test/ts-use"]},"typesVersions":{"*":{"*":["*","dist/*","dist/src/*","dist/src/*/index"],"src/*":["*","dist/*","dist/src/*","dist/src/*/index"]}},"_hasShrinkwrap":false,"devDependencies":{"aegir":"^48.0.1"},"_npmOperationalInternal":{"tmp":"tmp/dag-pb_4.1.7_1778472894801_0.17196982382384363","host":"s3://npm-registry-packages-npm-production"},"readme":"# @ipld/dag-pb <!-- omit in toc -->\n\n[![codecov](https://img.shields.io/codecov/c/github/ipld/js-dag-pb.svg?style=flat-square)](https://codecov.io/gh/ipld/js-dag-pb)\n[![CI](https://img.shields.io/github/actions/workflow/status/ipld/js-dag-pb/js-test-and-release.yml?branch=master\\&style=flat-square)](https://github.com/ipld/js-dag-pb/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)\n\n> JS implementation of DAG-PB\n\n## Table of contents <!-- omit in toc -->\n\n- [Install](#install)\n  - [Browser `<script>` tag](#browser-script-tag)\n- [Overview](#overview)\n- [Example](#example)\n- [Usage](#usage)\n  - [`prepare()`](#prepare)\n- [`createNode()` \\& `createLink()`](#createnode--createlink)\n- [API Docs](#api-docs)\n- [License](#license)\n- [Contribute](#contribute)\n\n## Install\n\n```console\n$ npm i @ipld/dag-pb\n```\n\n### Browser `<script>` tag\n\nLoading this module through a script tag will make it's exports available as `IpldDagPb` in the global namespace.\n\n```html\n<script src=\"https://unpkg.com/@ipld/dag-pb/dist/index.min.js\"></script>\n```\n\n## Overview\n\nAn implementation of the [DAG-PB spec](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-pb.md) for JavaScript designed for use with [multiformats](https://github.com/multiformats/js-multiformats) or via the higher-level `Block` abstraction in [@ipld/block](https://github.com/ipld/js-block).\n\n## Example\n\n```js\nimport { CID } from 'multiformats/cid'\nimport { sha256 } from 'multiformats/hashes/sha2'\nimport * as dagPB from '@ipld/dag-pb'\n\nasync function run () {\n  const bytes = dagPB.encode({\n    Data: new TextEncoder().encode('Some data as a string'),\n    Links: []\n  })\n\n  // also possible if you `import dagPB, { prepare } from '@ipld/dag-pb'`\n  // const bytes = dagPB.encode(prepare('Some data as a string'))\n  // const bytes = dagPB.encode(prepare(new TextEncoder().encode('Some data as a string')))\n\n  const hash = await sha256.digest(bytes)\n  const cid = CID.create(1, dagPB.code, hash)\n\n  console.log(cid, '=>', Buffer.from(bytes).toString('hex'))\n\n  const decoded = dagPB.decode(bytes)\n\n  console.log(decoded)\n  console.log(`decoded \"Data\": ${new TextDecoder().decode(decoded.Data)}`)\n}\n\nrun().catch((err) => {\n  console.error(err)\n  process.exit(1)\n})\n```\n\n## Usage\n\n`@ipld/dag-pb` is designed to be used within multiformats but can be used separately. `encode()`, `decode()`, `validate()` and `prepare()` functions are available if you pass in a `multiformats` object to the default export function. Each of these can operate independently as required.\n\n### `prepare()`\n\nThe DAG-PB encoding is very strict about the Data Model forms that are passed in. The objects *must* exactly resemble what they would if they were to undergo a round-trip of encode & decode. Therefore, extraneous or mistyped properties are not acceptable and will be rejected. See the [DAG-PB spec](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-pb.md) for full details of the acceptable schema and additional constraints.\n\nDue to this strictness, a `prepare()` function is made available which simplifies construction and allows for more flexible input forms. Prior to encoding objects, call `prepare()` to receive a new object that strictly conforms to the schema.\n\n```js\nimport { CID } from 'multiformats/cid'\nimport { prepare } from '@ipld/dag-pb'\n\nconsole.log(prepare({ Data: 'some data' }))\n// ->{ Data: Uint8Array(9) [115, 111, 109, 101, 32, 100,  97, 116, 97] }\nconsole.log(prepare({ Links: [CID.parse('bafkqabiaaebagba')] }))\n// -> { Links: [ { Hash: CID(bafkqabiaaebagba) } ] }\n```\n\nSome features of `prepare()`:\n\n- Extraneous properties are omitted\n- String values for `Data` are converted\n- Strings are converted to `{ Data: bytes }` (as are `Uint8Array`s)\n- Multiple ways of finding CIDs in the `Links` array are attempted, including interpreting the whole link element as a CID, reading a `Uint8Array` as a CID\n- Ensuring that properties are of the correct type (link `Name` is a `string` and `Tsize` is a `number`)\n- `Links` array is always present, even if empty\n- `Links` array is properly sorted\n\n## `createNode()` & `createLink()`\n\nThese utility exports are available to make transition from the older [ipld-dag-pb](https://github.com/ipld/js-ipld-dag-pb) library which used `DAGNode` and `DAGLink` objects with constructors. `createNode()` mirrors the `new DAGNode()` API while `createLink()` mirrors `new DAGLink()` API.\n\n- `createNode(data: Uint8Array, links: PBLink[]|void): PBNode`: create a correctly formed `PBNode` object from a `Uint8Array` and an optional array of correctly formed `PBLink` objects. The returned object will be suitable for passing to `encode()` and using `prepare()` on it should result in a noop.\n- `createLink(name: string, size: number, cid: CID): PBLink`: create a correctly formed `PBLink` object from a name, size and CID. The returned object will be suitable for attaching to a `PBNode`'s `Links` array, or in an array for the second argument to `createNode()`.\n\n```js\nimport { CID, bytes } from 'multiformats'\nimport * as Block from 'multiformats/block'\nimport { sha256 as hasher } from 'multiformats/hashes/sha2'\nimport * as codec from '@ipld/dag-pb'\n\nconst { createLink, createNode } = codec\n\nasync function run () {\n  const cid1 = CID.parse('QmWDtUQj38YLW8v3q4A6LwPn4vYKEbuKWpgSm6bjKW6Xfe')\n  const cid2 = CID.parse('bafyreifepiu23okq5zuyvyhsoiazv2icw2van3s7ko6d3ixl5jx2yj2yhu')\n\n  const links = [createLink('link1', 100, cid1), createLink('link2', 200, cid2)]\n  const value = createNode(Uint8Array.from([0, 1, 2, 3, 4]), links)\n  console.log(value)\n\n  const block = await Block.encode({ value, codec, hasher })\n  console.log(block.cid)\n  console.log(`Encoded: ${bytes.toHex(block.bytes).replace(/(.{80})/g, '$1\\n         ')}`)\n}\n\nrun().catch((err) => console.error(err))\n```\n\nResults in:\n\n```\n    {\n      Data: Uint8Array(5) [ 0, 1, 2, 3, 4 ],\n      Links: [\n        {\n          Hash: CID(QmWDtUQj38YLW8v3q4A6LwPn4vYKEbuKWpgSm6bjKW6Xfe),\n          Name: 'link1',\n          Tsize: 100\n        },\n        {\n          Hash: CID(bafyreifepiu23okq5zuyvyhsoiazv2icw2van3s7ko6d3ixl5jx2yj2yhu),\n          Name: 'link2',\n          Tsize: 200\n        }\n      ]\n    }\n    CID(bafybeihsp53wkzsaif76mjv564cawzqyjwianosamlvf6sht2m25ttyxiy)\n    Encoded: 122d0a2212207521fe19c374a97759226dc5c0c8e674e73950e81b211f7dd3b6b30883a08a511205\n             6c696e6b31186412300a2401711220a47a29adb950ee698ae0f272019ae902b6aa06ee5f53bc3da2\n             ebea6fac27583d12056c696e6b3218c8010a050001020304\n```\n\n## API Docs\n\n- <https://ipld.github.io/js-dag-pb>\n\n## License\n\nLicensed under either of\n\n- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)\n- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)\n\n## Contribute\n\nUnless 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.\n","publish_time":1778472894932,"_cnpm_publish_time":1778472894932,"dist-tags":{"latest":"4.2.0"}}