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

feat: BlockEncoder#encode and BlockDecoder#decode can be async #282

Open
wants to merge 3 commits into
base: master
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
13 changes: 6 additions & 7 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async function encode ({ value, codec, hasher }) {
if (typeof value === 'undefined') throw new Error('Missing required argument "value"')
if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher')

const bytes = codec.encode(value)
const bytes = await codec.encode(value)
const hash = await hasher.digest(bytes)
/** @type {CID<T, Code, Alg, 1>} */
const cid = CID.create(
Expand All @@ -204,7 +204,7 @@ async function decode ({ bytes, codec, hasher }) {
if (!bytes) throw new Error('Missing required argument "bytes"')
if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher')

const value = codec.decode(bytes)
const value = await codec.decode(bytes)
const hash = await hasher.digest(bytes)
/** @type {CID<T, Code, Alg, 1>} */
const cid = CID.create(1, codec.code, hash)
Expand All @@ -223,12 +223,12 @@ async function decode ({ bytes, codec, hasher }) {
* @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>}
* @returns {Promise<API.BlockView<T, Code, Alg, V>>}
*/
function createUnsafe ({ bytes, cid, value: maybeValue, codec }) {
async function createUnsafe ({ bytes, cid, value: maybeValue, codec }) {
const value = maybeValue !== undefined
? maybeValue
: (codec && codec.decode(bytes))
: (codec && await codec.decode(bytes))

if (value === undefined) throw new Error('Missing required argument, must either provide "value" or "codec"')

Expand All @@ -255,12 +255,11 @@ function createUnsafe ({ bytes, cid, value: maybeValue, codec }) {
async function create ({ bytes, cid, hasher, codec }) {
if (!bytes) throw new Error('Missing required argument "bytes"')
if (!hasher) throw new Error('Missing required argument "hasher"')
const value = codec.decode(bytes)
const value = await codec.decode(bytes)
const hash = await hasher.digest(bytes)
if (!binary.equals(cid.multihash.bytes, hash.bytes)) {
throw new Error('CID hash does not match bytes')
}

return createUnsafe({
bytes,
cid,
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import type { ByteView } from '../block/interface.js'
export interface BlockEncoder<Code extends number, T> {
name: string
code: Code
encode(data: T): ByteView<T>
encode(data: T): ByteView<T> | Promise<ByteView<T>>
}

/**
* IPLD decoder part of the codec.
*/
export interface BlockDecoder<Code extends number, T> {
code: Code
decode(bytes: ByteView<T>): T
decode(bytes: ByteView<T>): T | Promise<T>
}

/**
Expand Down
16 changes: 8 additions & 8 deletions test/test-block.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('block', () => {

it('createUnsafe', async () => {
const block = await main.encode({ value: fixture, codec, hasher })
const block2 = main.createUnsafe({ bytes: block.bytes, cid: block.cid, codec })
const block2 = await main.createUnsafe({ bytes: block.bytes, cid: block.cid, codec })
assert.deepStrictEqual(block.cid.equals(block2.cid), true)
})

describe('reader', () => {
describe('reader', async () => {
const value = {
link,
nope: 'skip',
Expand All @@ -37,7 +37,7 @@ describe('block', () => {
bytes: Uint8Array.from('1234')
}
// @ts-expect-error - 'boolean' is not assignable to type 'CID'
const block = main.createUnsafe({ value, codec, hasher, cid: true, bytes: true })
const block = await main.createUnsafe({ value, codec, hasher, cid: true, bytes: true })

it('links', () => {
const expected = ['link', 'arr/0']
Expand All @@ -63,8 +63,8 @@ describe('block', () => {
assert.deepStrictEqual(ret, { value: 'skip' })
})

it('null links/tree', () => {
const block = main.createUnsafe({
it('null links/tree', async () => {
const block = await main.createUnsafe({
value: null,
codec,
hasher,
Expand Down Expand Up @@ -95,9 +95,9 @@ describe('block', () => {
assert.equal(links[0][1].toString(), link.toString())
})

it('kitchen sink', () => {
it('kitchen sink', async () => {
const sink = { one: { two: { arr: [true, false, null], three: 3, buff, link } } }
const block = main.createUnsafe({
const block = await main.createUnsafe({
value: sink,
codec,
// @ts-expect-error - 'boolean' is not assignable to type 'ByteView<unknown>'
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('block', () => {

it('createUnsafe', async () => {
// @ts-expect-error
assert.throws(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"')
assert.isRejected(main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"')
})

it('create', async () => {
Expand Down