Skip to content

Commit

Permalink
fix: local only unless remote is enabled(default) (#438)
Browse files Browse the repository at this point in the history
* tmp: testing dynamic import

* chore: temporarily disable delegated routing

* fix: getHashersForCodes handles errors gracefully

* fix: revert version, other minor changes

* chore: log update

* chore: address PR comments
  • Loading branch information
SgtPooki authored Sep 11, 2024
1 parent 8f37d37 commit 3b87aea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/lib/hash-importer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* global globalThis */
import * as sha3 from '@multiformats/sha3'
import { createBLAKE3, createBLAKE2b } from 'hash-wasm'
import { type Hasher, from } from 'multiformats/hashes/hasher'
import * as sha2 from 'multiformats/hashes/sha2'

Expand All @@ -15,7 +14,15 @@ export type SupportedHashers = typeof sha2.sha256 |
Hasher<'blake3', 30>

export async function getHashersForCodes (...codes: number[]): Promise<SupportedHashers[]> {
return Promise.all(codes.map(getHasherForCode))
const hashers: SupportedHashers[] = []
for (const code of codes) {
try {
hashers.push(await getHasherForCode(code))
} catch (error) {
console.error(`Failed to get hasher for code ${code}: ${error}`)
}
}
return hashers
}

/**
Expand Down Expand Up @@ -56,6 +63,7 @@ export async function getHasherForCode (code: number): Promise<SupportedHashers>
name: 'blake3',
code,
encode: async (data: Uint8Array): Promise<Uint8Array> => {
const { createBLAKE3 } = await import('hash-wasm')
const blake3Hasher = await createBLAKE3()
blake3Hasher.init()
blake3Hasher.update(data)
Expand All @@ -67,6 +75,7 @@ export async function getHasherForCode (code: number): Promise<SupportedHashers>
name: 'blake2b-256',
code,
encode: async (data: Uint8Array): Promise<Uint8Array> => {
const { createBLAKE2b } = await import('hash-wasm')
const blake2bHasher = await createBLAKE2b(256)
blake2bHasher.init()
blake2bHasher.update(data)
Expand All @@ -78,6 +87,7 @@ export async function getHasherForCode (code: number): Promise<SupportedHashers>
name: 'blake2b-512',
code,
encode: async (data: Uint8Array): Promise<Uint8Array> => {
const { createBLAKE2b } = await import('hash-wasm')
const blake2bHasher = await createBLAKE2b(512)
blake2bHasher.init()
blake2bHasher.update(data)
Expand Down
10 changes: 6 additions & 4 deletions src/lib/init-helia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ function areRemoteGatewaysEnabled (): boolean {

export default async function initHelia (kuboGatewayOptions: KuboGatewayOptions): Promise<Helia> {
const routers = [
// always use delegated routing
delegatedHTTPRouting('http://delegated-ipfs.dev'),
// Always add the Kubo gatewawy
// Always add the Kubo gateway
httpGatewayRouting({ gateways: [`${kuboGatewayOptions.protocol ?? 'http'}://${kuboGatewayOptions.host}:${kuboGatewayOptions.port}`] })
]

if (areRemoteGatewaysEnabled()) {
// eslint-disable-next-line no-console
console.log('remote gateways and delegated routing are enabled')
routers.push(delegatedHTTPRouting('http://delegated-ipfs.dev'))
routers.push(httpGatewayRouting())
}

Expand All @@ -46,7 +47,8 @@ export default async function initHelia (kuboGatewayOptions: KuboGatewayOptions)
addDagNodeToHelia(helia, 'json', { hello: 'world' }, 20), // bagaaifcavabu6fzheerrmtxbbwv7jjhc3kaldmm7lbnvfopyrthcvod4m6ygpj3unrcggkzhvcwv5wnhc5ufkgzlsji7agnmofovc2g4a3ui7ja
addDagNodeToHelia(helia, 'json', { hello: 'world' }, 30), // bagaaihraf4oq2kddg6o5ewlu6aol6xab75xkwbgzx2dlot7cdun7iirve23a
addDagNodeToHelia(helia, 'raw', (new TextEncoder()).encode('hello'), 30), // bafkr4ihkr4ld3m4gqkjf4reryxsy2s5tkbxprqkow6fin2iiyvreuzzab4
addDagNodeToHelia(helia, 'dag-pb', { Data: (new TextEncoder()).encode('hello'), Links: [] }, 0xb220) // bafykbzacec3ssfzln7bfcn54t5voa4onlcx63kkx3reucaiwc7eaffmla7gci
addDagNodeToHelia(helia, 'dag-pb', { Data: (new TextEncoder()).encode('hello'), Links: [] }, 0xb220), // bafykbzacec3ssfzln7bfcn54t5voa4onlcx63kkx3reucaiwc7eaffmla7gci
addDagNodeToHelia(helia, 'dag-pb', { Data: (new TextEncoder()).encode('hello'), Links: [] }, 0xb240) // bafymbzacia5oqpl3kqdjk6hgisdemv44omuqse33bf3a2gnurnzcmkstjhupcqymbuvsj2qlke4phr5iudjruwbjqsx34psaqsuezr4ivka5ul2y
])

return helia
Expand Down

0 comments on commit 3b87aea

Please sign in to comment.