-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparse-url-string.ts
214 lines (181 loc) · 7.38 KB
/
parse-url-string.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { peerIdFromString } from '@libp2p/peer-id'
import { CID } from 'multiformats/cid'
import { TLRU } from './tlru.js'
import type { ParseResourceOptions } from './parse-resource.js'
import type { RequestFormatShorthand } from '../types.js'
import type { IPNS, ResolveResult } from '@helia/ipns'
import type { ComponentLogger } from '@libp2p/interface'
const ipnsCache = new TLRU<ResolveResult>(1000)
export interface ParseUrlStringInput {
urlString: string
ipns: IPNS
logger: ComponentLogger
}
export interface ParseUrlStringOptions extends ParseResourceOptions {
}
export interface ParsedUrlQuery extends Record<string, string | unknown> {
format?: RequestFormatShorthand
download?: boolean
filename?: string
}
export interface ParsedUrlStringResults {
protocol: string
path: string
cid: CID
query: ParsedUrlQuery
}
const URL_REGEX = /^(?<protocol>ip[fn]s):\/\/(?<cidOrPeerIdOrDnsLink>[^/?]+)\/?(?<path>[^?]*)\??(?<queryString>.*)$/
const PATH_REGEX = /^\/(?<protocol>ip[fn]s)\/(?<cidOrPeerIdOrDnsLink>[^/?]+)\/?(?<path>[^?]*)\??(?<queryString>.*)$/
const PATH_GATEWAY_REGEX = /^https?:\/\/(.*[^/])\/(?<protocol>ip[fn]s)\/(?<cidOrPeerIdOrDnsLink>[^/?]+)\/?(?<path>[^?]*)\??(?<queryString>.*)$/
const SUBDOMAIN_GATEWAY_REGEX = /^https?:\/\/(?<cidOrPeerIdOrDnsLink>[^/?]+)\.(?<protocol>ip[fn]s)\.([^/?]+)\/?(?<path>[^?]*)\??(?<queryString>.*)$/
function matchURLString (urlString: string): Record<string, string> {
for (const pattern of [URL_REGEX, PATH_REGEX, PATH_GATEWAY_REGEX, SUBDOMAIN_GATEWAY_REGEX]) {
const match = urlString.match(pattern)
if (match?.groups != null) {
return match.groups
}
}
throw new TypeError(`Invalid URL: ${urlString}, please use ipfs://, ipns://, or gateway URLs only`)
}
/**
* For dnslinks see https://specs.ipfs.tech/http-gateways/subdomain-gateway/#host-request-header
* DNSLink names include . which means they must be inlined into a single DNS label to provide unique origin and work with wildcard TLS certificates.
*/
// DNS label can have up to 63 characters, consisting of alphanumeric
// characters or hyphens -, but it must not start or end with a hyphen.
const dnsLabelRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/
/**
* Checks if label looks like inlined DNSLink.
* (https://specs.ipfs.tech/http-gateways/subdomain-gateway/#host-request-header)
*/
function isInlinedDnsLink (label: string): boolean {
return dnsLabelRegex.test(label) && label.includes('-') && !label.includes('.')
}
/**
* DNSLink label decoding
* * Every standalone - is replaced with .
* * Every remaining -- is replaced with -
*
* @example en-wikipedia--on--ipfs-org.ipns.example.net -> example.net/ipns/en.wikipedia-on-ipfs.org
*/
function dnsLinkLabelDecoder (linkLabel: string): string {
return linkLabel.replace(/--/g, '%').replace(/-/g, '.').replace(/%/g, '-')
}
/**
* A function that parses ipfs:// and ipns:// URLs, returning an object with easily recognizable properties.
*
* After determining the protocol successfully, we process the cidOrPeerIdOrDnsLink:
* * If it's ipfs, it parses the CID or throws an Aggregate error
* * If it's ipns, it attempts to resolve the PeerId and then the DNSLink. If both fail, an Aggregate error is thrown.
*/
export async function parseUrlString ({ urlString, ipns, logger }: ParseUrlStringInput, options?: ParseUrlStringOptions): Promise<ParsedUrlStringResults> {
const log = logger.forComponent('helia:verified-fetch:parse-url-string')
const { protocol, cidOrPeerIdOrDnsLink, path: urlPath, queryString } = matchURLString(urlString)
let cid: CID | undefined
let resolvedPath: string | undefined
const errors: Error[] = []
if (protocol === 'ipfs') {
try {
cid = CID.parse(cidOrPeerIdOrDnsLink)
} catch (err) {
log.error(err)
errors.push(new TypeError('Invalid CID for ipfs://<cid> URL'))
}
} else {
let resolveResult = ipnsCache.get(cidOrPeerIdOrDnsLink)
if (resolveResult != null) {
cid = resolveResult.cid
resolvedPath = resolveResult.path
log.trace('resolved %s to %c from cache', cidOrPeerIdOrDnsLink, cid)
} else {
// protocol is ipns
log.trace('attempting to resolve PeerId for %s', cidOrPeerIdOrDnsLink)
let peerId = null
try {
peerId = peerIdFromString(cidOrPeerIdOrDnsLink)
options?.signal?.throwIfAborted()
resolveResult = await ipns.resolve(peerId, options)
cid = resolveResult?.cid
resolvedPath = resolveResult?.path
log.trace('resolved %s to %c', cidOrPeerIdOrDnsLink, cid)
ipnsCache.set(cidOrPeerIdOrDnsLink, resolveResult, 60 * 1000 * 2)
} catch (err) {
if (peerId == null) {
log.error('could not parse PeerId string "%s"', cidOrPeerIdOrDnsLink, err)
errors.push(new TypeError(`Could not parse PeerId in ipns url "${cidOrPeerIdOrDnsLink}", ${(err as Error).message}`))
} else {
log.error('could not resolve PeerId %c', peerId, err)
errors.push(new TypeError(`Could not resolve PeerId "${cidOrPeerIdOrDnsLink}", ${(err as Error).message}`))
}
}
if (cid == null) {
let decodedDnsLinkLabel = cidOrPeerIdOrDnsLink
if (isInlinedDnsLink(cidOrPeerIdOrDnsLink)) {
decodedDnsLinkLabel = dnsLinkLabelDecoder(cidOrPeerIdOrDnsLink)
log.trace('decoded dnslink from "%s" to "%s"', cidOrPeerIdOrDnsLink, decodedDnsLinkLabel)
}
log.trace('Attempting to resolve DNSLink for %s', decodedDnsLinkLabel)
try {
options?.signal?.throwIfAborted()
resolveResult = await ipns.resolveDNSLink(decodedDnsLinkLabel, options)
cid = resolveResult?.cid
resolvedPath = resolveResult?.path
log.trace('resolved %s to %c', decodedDnsLinkLabel, cid)
ipnsCache.set(cidOrPeerIdOrDnsLink, resolveResult, 60 * 1000 * 2)
} catch (err: any) {
log.error('could not resolve DnsLink for "%s"', cidOrPeerIdOrDnsLink, err)
errors.push(err)
}
}
}
}
options?.signal?.throwIfAborted()
if (cid == null) {
if (errors.length === 1) {
throw errors[0]
}
throw new AggregateError(errors, `Invalid resource. Cannot determine CID from URL "${urlString}"`)
}
// parse query string
const query: Record<string, any> = {}
if (queryString != null && queryString.length > 0) {
const queryParts = queryString.split('&')
for (const part of queryParts) {
const [key, value] = part.split('=')
query[key] = decodeURIComponent(value)
}
if (query.download != null) {
query.download = query.download === 'true'
}
if (query.filename != null) {
query.filename = query.filename.toString()
}
}
return {
protocol,
cid,
path: joinPaths(resolvedPath, urlPath),
query
}
}
/**
* join the path from resolve result & given path.
* e.g. /ipns/<peerId>/ that is resolved to /ipfs/<cid>/<path1>, when requested as /ipns/<peerId>/<path2>, should be
* resolved to /ipfs/<cid>/<path1>/<path2>
*/
function joinPaths (resolvedPath: string | undefined, urlPath: string): string {
let path = ''
if (resolvedPath != null) {
path += resolvedPath
}
if (urlPath.length > 0) {
path = `${path.length > 0 ? `${path}/` : path}${urlPath}`
}
// replace duplicate forward slashes
path = path.replace(/\/(\/)+/g, '/')
// strip trailing forward slash if present
if (path.startsWith('/')) {
path = path.substring(1)
}
return path
}