Skip to content

Commit

Permalink
fix: extend blockstore interface (#55)
Browse files Browse the repository at this point in the history
To ensure type alignment, extend the blockstore interface
  • Loading branch information
achingbrain authored Mar 15, 2023
1 parent 202b966 commit 42308c0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@
"@libp2p/interface-libp2p": "^1.1.0",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interfaces": "^3.3.1",
"interface-blockstore": "^5.0.0",
"interface-datastore": "^8.0.0",
"interface-store": "^4.0.0",
"ipfs-bitswap": "^17.0.0",
"multiformats": "^11.0.1",
"progress-events": "^1.0.0"
Expand Down
21 changes: 11 additions & 10 deletions packages/interface/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { AbortOptions } from '@libp2p/interfaces'
import type { ProgressEvent, ProgressOptions } from 'progress-events'
import type { CID } from 'multiformats/cid'
import type { BitswapNotifyProgressEvents, BitswapWantProgressEvents } from 'ipfs-bitswap'
import type { AwaitIterable } from './index.js'
import type { AwaitIterable, Await } from './index.js'
import type { Blockstore } from 'interface-blockstore'

export interface Pair {
cid: CID
Expand Down Expand Up @@ -42,7 +43,7 @@ export type DeleteBlockProgressEvents =
export type DeleteManyBlocksProgressEvents =
ProgressEvent<'blocks:delete-many:blockstore:delete-many'>

export interface Blocks {
export interface Blocks extends Blockstore {
/**
* Check for the existence of a value for the passed key
*
Expand All @@ -57,7 +58,7 @@ export interface Blocks {
* }
*```
*/
has: (key: CID, options?: AbortOptions) => Promise<boolean>
has: (key: CID, options?: AbortOptions) => Await<boolean>

/**
* Store the passed block under the passed CID
Expand All @@ -68,7 +69,7 @@ export interface Blocks {
* await store.put(CID('bafyfoo'), new Uint8Array([0, 1, 2, 3]))
* ```
*/
put: (key: CID, val: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>) => Promise<void>
put: (key: CID, val: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>) => Await<void>

/**
* Store the given key/value pairs
Expand All @@ -85,7 +86,7 @@ export interface Blocks {
putMany: (
source: AwaitIterable<Pair>,
options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>
) => AsyncIterable<Pair>
) => AwaitIterable<Pair>

/**
* Retrieve the value stored under the given key
Expand All @@ -97,7 +98,7 @@ export interface Blocks {
* // => got content: datastore
* ```
*/
get: (key: CID, options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>) => Promise<Uint8Array>
get: (key: CID, options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>) => Await<Uint8Array>

/**
* Retrieve values for the passed keys
Expand All @@ -113,7 +114,7 @@ export interface Blocks {
getMany: (
source: AwaitIterable<CID>,
options?: AbortOptions & ProgressOptions<GetManyBlocksProgressEvents>
) => AsyncIterable<Uint8Array>
) => AwaitIterable<Uint8Array>

/**
* Retrieve all blocks in the blockstore
Expand All @@ -128,7 +129,7 @@ export interface Blocks {
*/
getAll: (
options?: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents>
) => AsyncIterable<Pair>
) => AwaitIterable<Pair>

/**
* Remove the record for the passed key
Expand All @@ -140,7 +141,7 @@ export interface Blocks {
* console.log('deleted awesome content :(')
* ```
*/
delete: (key: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>) => Promise<void>
delete: (key: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>) => Await<void>

/**
* Remove values for the passed keys
Expand All @@ -158,5 +159,5 @@ export interface Blocks {
deleteMany: (
source: AwaitIterable<CID>,
options?: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents>
) => AsyncIterable<CID>
) => AwaitIterable<CID>
}
2 changes: 1 addition & 1 deletion packages/interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { ProgressEvent, ProgressOptions } from 'progress-events'
import type { CID } from 'multiformats/cid'
import type { Blocks } from './blocks.js'

export type AwaitIterable<T> = Iterable<T> | AsyncIterable<T>
export type { Await, AwaitIterable } from 'interface-store'

/**
* The API presented by a Helia node.
Expand Down

0 comments on commit 42308c0

Please sign in to comment.