Skip to content

Commit

Permalink
fix(types): Add missing types (#95)
Browse files Browse the repository at this point in the history
* fix(types): 🏷️ Adding missing types

* Update packages/helia/src/storage.ts

Co-authored-by: Alex Potsides <[email protected]>

---------

Co-authored-by: Alex Potsides <[email protected]>
  • Loading branch information
whizzzkid and achingbrain authored Apr 17, 2023
1 parent 8e86c00 commit e858b8d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
10 changes: 5 additions & 5 deletions packages/helia/src/helia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class HeliaImpl implements Helia {

// @ts-expect-error incomplete libp2p implementation
const libp2p = init.libp2p ?? new Proxy<Libp2p>({}, {
get (_, prop) {
get (_, prop): true | (() => void) {
const noop = (): void => {}
const noops = ['start', 'stop']

Expand All @@ -54,21 +54,21 @@ export class HeliaImpl implements Helia {

throw new Error('Please configure Helia with a libp2p instance')
},
set () {
set (): never {
throw new Error('Please configure Helia with a libp2p instance')
}
})

if (init.libp2p != null) {
this.#bitswap = createBitswap(libp2p, blockstore, {
hashLoader: {
getHasher: async (codecOrName: string | number) => {
getHasher: async (codecOrName: string | number): Promise<MultihashHasher<number>> => {
const hasher = hashers.find(hasher => {
return hasher.code === codecOrName || hasher.name === codecOrName
})

if (hasher != null) {
return await Promise.resolve(hasher)
return hasher
}

throw new Error(`Could not load hasher for code/name "${codecOrName}"`)
Expand Down Expand Up @@ -108,7 +108,7 @@ export class HeliaImpl implements Helia {

log('gc start')

await drain(blockstore.deleteMany((async function * () {
await drain(blockstore.deleteMany((async function * (): AsyncGenerator<CID> {
for await (const { cid } of blockstore.getAll()) {
try {
if (await helia.pins.isPinned(cid, options)) {
Expand Down
10 changes: 5 additions & 5 deletions packages/helia/src/pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export class PinsImpl implements Pins {
const queue = new PQueue({
concurrency: DAG_WALK_QUEUE_CONCURRENCY
})
void queue.add(async () => {
await this.#walkDag(cid, queue, (pinnedBlock) => {
void queue.add(async (): Promise<void> => {
await this.#walkDag(cid, queue, (pinnedBlock): void => {
// do not update pinned block if this block is already pinned by this CID
if (pinnedBlock.pinnedBy.find(c => uint8ArrayEquals(c, cid.bytes)) != null) {
return
Expand All @@ -103,7 +103,7 @@ export class PinsImpl implements Pins {
// if a job in the queue errors, throw that error
const deferred = defer()

queue.on('error', (err) => {
queue.on('error', (err): void => {
queue.clear()
deferred.reject(err)
})
Expand Down Expand Up @@ -199,8 +199,8 @@ export class PinsImpl implements Pins {
const queue = new PQueue({
concurrency: DAG_WALK_QUEUE_CONCURRENCY
})
void queue.add(async () => {
await this.#walkDag(cid, queue, (pinnedBlock) => {
void queue.add(async (): Promise<void> => {
await this.#walkDag(cid, queue, (pinnedBlock): void => {
pinnedBlock.pinCount--
pinnedBlock.pinnedBy = pinnedBlock.pinnedBy.filter(c => uint8ArrayEquals(c, cid.bytes))
}, {
Expand Down
9 changes: 5 additions & 4 deletions packages/helia/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class BlockStorage implements Blocks {
const releaseLock = await this.lock.readLock()

try {
const missingBlocks = filter(blocks, async ({ cid }) => {
const missingBlocks = filter(blocks, async ({ cid }): Promise<boolean> => {
const has = await this.child.has(cid)

if (has) {
Expand All @@ -89,7 +89,7 @@ export class BlockStorage implements Blocks {
return !has
})

const notifyEach = forEach(missingBlocks, ({ cid, block }) => {
const notifyEach = forEach(missingBlocks, ({ cid, block }): void => {
options.onProgress?.(new CustomProgressEvent<CID>('blocks:put-many:bitswap:notify', cid))
this.bitswap?.notify(cid, block, options)
})
Expand Down Expand Up @@ -119,6 +119,7 @@ export class BlockStorage implements Blocks {
}

options.onProgress?.(new CustomProgressEvent<CID>('blocks:get:blockstore:get', cid))

return await this.child.get(cid, options)
} finally {
releaseLock()
Expand All @@ -133,7 +134,7 @@ export class BlockStorage implements Blocks {

try {
options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:get-many'))
yield * this.child.getMany(forEach(cids, async (cid) => {
yield * this.child.getMany(forEach(cids, async (cid): Promise<void> => {
if (this.bitswap?.isStarted() === true && !(await this.child.has(cid))) {
options.onProgress?.(new CustomProgressEvent<CID>('blocks:get-many:bitswap:get', cid))
const block = await this.bitswap.want(cid, options)
Expand Down Expand Up @@ -174,7 +175,7 @@ export class BlockStorage implements Blocks {
const storage = this

options.onProgress?.(new CustomProgressEvent('blocks:delete-many:blockstore:delete-many'))
yield * this.child.deleteMany((async function * () {
yield * this.child.deleteMany((async function * (): AsyncGenerator<CID> {
for await (const cid of cids) {
if (await storage.pins.isPinned(cid)) {
throw new Error('CID was pinned')
Expand Down

0 comments on commit e858b8d

Please sign in to comment.