Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illustrate multiblock encoder/decoder API #38

Open
wants to merge 5 commits into
base: context-binding
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ const cid = CID.create(1, dagcbor.code, hash)
```

However, if you're doing this much you should probably use multiformats
with the `Block` API.
with the `dag` API.

```js
// Import basics package with dep-free codecs, hashes, and base encodings
import { block } from 'multiformats/basics'
import { dag } from 'multiformats/basics'
import { sha256 } from 'multiformats/hashes/sha2'
import dagcbor from '@ipld/dag-cbor'
import cbor from '@ipld/dag-cbor'
import json from '@ipld/dag-json'

const encoder = dag.encoder({ multicodec: dag.or(json), hasher: sha256 })

const encoder = block.encoder(dagcbor, { hasher: sha256 })
const hello = encoder.encode({ hello: 'world' })
const hello = encoder.encodeBlock({ hello: 'world' })
const cid = await hello.cid()

const greeting = encoder.encode({ code: json.code, value: { greeting: hello } })
await greeting.cid()

const decoder = dag.decoder({ multicodec: dag.or(json), hasher: sha256 })
decoder.decode({ code: json.code, bytes: greeting.bytes })
```

# Plugins
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
}
},
"devDependencies": {
"@types/node": "14.11.1",
"c8": "^7.3.0",
"hundreds": "0.0.7",
"mocha": "^8.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/bases/base64-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import b64 from './base64.js'

const { base64, base64pad, base64url, base64urlpad, __browser } = b64({
encode: b => btoa([].reduce.call(b, (p, c) => p + String.fromCharCode(c), '')),
encode: b => btoa(/** @type {string} */([].reduce.call(b, (p, c) => p + String.fromCharCode(c), ''))),
decode: str => Uint8Array.from(atob(str), c => c.charCodeAt(0)),
__browser: true
})
Expand Down
1 change: 1 addition & 0 deletions src/bases/base64-import.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

// @ts-check

import { Buffer } from 'buffer'
import { coerce } from '../bytes.js'
import b64 from './base64.js'

Expand Down
4 changes: 2 additions & 2 deletions src/basics-browser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-check

import * as base64 from './bases/base64-browser.js'
import { CID, Block, hasher, digest, varint, bytes, hashes, codecs, bases as _bases } from './basics.js'
import { CID, dag, hasher, digest, varint, bytes, hashes, codecs, bases as _bases } from './basics.js'

const bases = { ..._bases, ...base64 }

export { CID, Block, hasher, digest, varint, bytes, hashes, codecs, bases }
export { CID, dag, hasher, digest, varint, bytes, hashes, codecs, bases }
4 changes: 2 additions & 2 deletions src/basics-import.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { CID, Block, hasher, digest, varint, bytes, hashes, codecs, bases as _bases } from './basics.js'
import { CID, dag, hasher, digest, varint, bytes, hashes, codecs, bases as _bases } from './basics.js'
import * as base64 from './bases/base64-import.js'

const bases = { ..._bases, ...base64 }
export { CID, Block, hasher, digest, varint, bytes, hashes, codecs, bases }
export { CID, dag, hasher, digest, varint, bytes, hashes, codecs, bases }
4 changes: 2 additions & 2 deletions src/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import * as sha2 from './hashes/sha2.js'
import raw from './codecs/raw.js'
import json from './codecs/json.js'

import { CID, Block, hasher, digest, varint, bytes } from './index.js'
import { CID, dag, hasher, digest, varint, bytes } from './index.js'

const bases = { ...base32, ...base58 }
const hashes = { ...sha2 }
const codecs = { raw, json }

export { CID, Block, hasher, digest, varint, bytes, hashes, bases, codecs }
export { CID, dag, hasher, digest, varint, bytes, hashes, bases, codecs }
308 changes: 0 additions & 308 deletions src/block.js

This file was deleted.

Loading