-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparse-resource.ts
40 lines (34 loc) · 1.41 KB
/
parse-resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { CID } from 'multiformats/cid'
import { parseUrlString } from './parse-url-string.js'
import type { ParsedUrlStringResults } from './parse-url-string.js'
import type { Resource } from '../index.js'
import type { IPNS, IPNSRoutingEvents, ResolveDNSLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns'
import type { AbortOptions, ComponentLogger } from '@libp2p/interface'
import type { ProgressOptions } from 'progress-events'
export interface ParseResourceComponents {
ipns: IPNS
logger: ComponentLogger
}
export interface ParseResourceOptions extends ProgressOptions<ResolveProgressEvents | IPNSRoutingEvents | ResolveDNSLinkProgressEvents>, AbortOptions {
}
/**
* Handles the different use cases for the `resource` argument.
* The resource can represent an IPFS path, IPNS path, or CID.
* If the resource represents an IPNS path, we need to resolve it to a CID.
*/
export async function parseResource (resource: Resource, { ipns, logger }: ParseResourceComponents, options?: ParseResourceOptions): Promise<ParsedUrlStringResults> {
if (typeof resource === 'string') {
return parseUrlString({ urlString: resource, ipns, logger }, options)
}
const cid = CID.asCID(resource)
if (cid != null) {
// an actual CID
return {
cid,
protocol: 'ipfs',
path: '',
query: {}
}
}
throw new TypeError(`Invalid resource. Cannot determine CID from resource: ${resource}`)
}