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

fix: local only unless remote is enabled(default) #438

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
Comment on lines +17 to +25
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes an issue where trying to import a non-existent hasher would throw and break webui

}

/**
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
8 changes: 6 additions & 2 deletions src/lib/init-helia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ function areRemoteGatewaysEnabled (): boolean {
export default async function initHelia (kuboGatewayOptions: KuboGatewayOptions): Promise<Helia> {
const routers = [
// always use delegated routing
delegatedHTTPRouting('http://delegated-ipfs.dev'),
// delegatedHTTPRouting('http://delegated-ipfs.dev'),
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
// Always add the Kubo gatewawy
httpGatewayRouting({ gateways: [`${kuboGatewayOptions.protocol ?? 'http'}://${kuboGatewayOptions.host}:${kuboGatewayOptions.port}`] })
]

if (areRemoteGatewaysEnabled()) {
// eslint-disable-next-line no-console
console.log('remote gateways are enabled')
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
routers.push(delegatedHTTPRouting('http://delegated-ipfs.dev'))
routers.push(httpGatewayRouting())
}

Expand All @@ -46,7 +49,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
Loading