-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathipfs-path.js
308 lines (283 loc) · 12.1 KB
/
ipfs-path.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict'
/* eslint-env browser */
const IsIpfs = require('is-ipfs')
const isFQDN = require('is-fqdn')
function normalizedIpfsPath (urlOrPath) {
let result = urlOrPath
// Convert CID-in-subdomain URL to /ipns/<fqdn>/ path
if (IsIpfs.subdomain(urlOrPath)) {
result = subdomainToIpfsPath(urlOrPath)
}
// Drop everything before the IPFS path
result = result.replace(/^.*(\/ip(f|n)s\/.+)$/, '$1')
// Remove Unescape special characters
// https://github.com/ipfs/ipfs-companion/issues/303
result = decodeURIComponent(result)
// Return a valid IPFS path or null otherwise
return IsIpfs.path(result) ? result : null
}
exports.normalizedIpfsPath = normalizedIpfsPath
function subdomainToIpfsPath (url) {
if (typeof url === 'string') {
url = new URL(url)
}
const match = url.toString().match(IsIpfs.subdomainPattern)
if (!match) throw new Error('no match for IsIpfs.subdomainPattern')
// TODO: support CID split with commas
const cid = match[1]
// TODO: support .ip(f|n)s. being at deeper levels
const protocol = match[2]
return `/${protocol}/${cid}${url.pathname}${url.search}${url.hash}`
}
function pathAtHttpGateway (path, gatewayUrl) {
// return URL without duplicated slashes
return trimDoubleSlashes(new URL(`${gatewayUrl}${path}`).toString())
}
exports.pathAtHttpGateway = pathAtHttpGateway
function redirectSubdomainGateway (url, subdomainGateway) {
if (typeof url === 'string') {
url = new URL(url)
}
const match = url.toString().match(IsIpfs.subdomainPattern)
if (!match) throw new Error('no match for IsIpfs.subdomainPattern')
const cid = match[1]
const protocol = match[2]
return trimDoubleSlashes(`${subdomainGateway.protocol}//${cid}.${protocol}.${subdomainGateway.hostname}${url.pathname}${url.search}${url.hash}`)
}
exports.redirectSubdomainGateway = redirectSubdomainGateway
function trimDoubleSlashes (urlString) {
return urlString.replace(/([^:]\/)\/+/g, '$1')
}
exports.trimDoubleSlashes = trimDoubleSlashes
function trimHashAndSearch (urlString) {
// https://github.com/ipfs-shipyard/ipfs-companion/issues/567
return urlString.split('#')[0].split('?')[0]
}
exports.trimHashAndSearch = trimHashAndSearch
function createIpfsPathValidator (getState, getIpfs, dnslinkResolver) {
const ipfsPathValidator = {
// Test if URL is a Public IPFS resource
// (pass validIpfsOrIpnsUrl(url) and not at the local gateway or API)
publicIpfsOrIpnsResource (url) {
// exclude custom gateway and api, otherwise we have infinite loops
if (!url.startsWith(getState().gwURLString) && !url.startsWith(getState().apiURLString)) {
return validIpfsOrIpnsUrl(url, dnslinkResolver)
}
return false
},
// Test if URL is a valid IPFS or IPNS
// (IPFS needs to be a CID, IPNS can be PeerId or have dnslink entry)
validIpfsOrIpnsUrl (url) {
return validIpfsOrIpnsUrl(url, dnslinkResolver)
},
// Same as validIpfsOrIpnsUrl (url) but for paths
// (we have separate methods to avoid 'new URL' where possible)
validIpfsOrIpnsPath (path) {
return validIpfsOrIpnsPath(path, dnslinkResolver)
},
// Test if URL is a subdomain gateway resource
// TODO: add test if URL is a public subdomain resource
ipfsOrIpnsSubdomain (url) {
return IsIpfs.subdomain(url)
},
// Test if actions such as 'copy URL', 'pin/unpin' should be enabled for the URL
isIpfsPageActionsContext (url) {
return Boolean(url && !url.startsWith(getState().apiURLString) && (
IsIpfs.url(url) ||
IsIpfs.subdomain(url) ||
dnslinkResolver.cachedDnslink(new URL(url).hostname)
))
},
// Test if actions such as 'per site redirect toggle' should be enabled for the URL
isRedirectPageActionsContext (url) {
const state = getState()
return state.ipfsNodeType !== 'embedded' && // hide with embedded node
(IsIpfs.ipnsUrl(url) || // show on /ipns/<fqdn>
(url.startsWith('http') && // hide on non-HTTP pages
!url.startsWith(state.gwURLString) && // hide on /ipfs/*
!url.startsWith(state.apiURLString))) // hide on api port
},
// Resolve URL or path to HTTP URL:
// - IPFS paths are attached to HTTP Gateway root
// - URL of DNSLinked websites are returned as-is
// The purpose of this resolver is to always return a meaningful, publicly
// accessible URL that can be accessed without the need of IPFS client.
resolveToPublicUrl (urlOrPath, optionalGatewayUrl) {
const input = urlOrPath
// CID-in-subdomain is good as-is
if (IsIpfs.subdomain(input)) return input
// IPFS Paths should be attached to the public gateway
const ipfsPath = normalizedIpfsPath(input)
const gateway = optionalGatewayUrl || getState().pubGwURLString
if (ipfsPath) return pathAtHttpGateway(ipfsPath, gateway)
// Return original URL (eg. DNSLink domains) or null if not an URL
return input.startsWith('http') ? input : null
},
// Resolve URL or path to subdomain gateway
// - non-subdomain path is returned as-is
// The purpose of this resolver is to return a valid IPFS
// subdomain URL
resolveToPublicSubdomainUrl (url, optionalGatewayUrl) {
// if non-subdomain return as-is
if (!IsIpfs.subdomain(url)) return url
const gateway = optionalGatewayUrl || getState().pubSubdomainGwURL
return redirectSubdomainGateway(url, gateway)
},
// Resolve URL or path to IPFS Path:
// - The path can be /ipfs/ or /ipns/
// - Keeps pathname + ?search + #hash from original URL
// - Returns null if no valid path can be produced
// The purpose of this resolver is to return a valid IPFS path
// that can be accessed with IPFS client.
resolveToIpfsPath (urlOrPath) {
const input = urlOrPath
// Try to normalize to IPFS path (gateway path or CID-in-subdomain)
const ipfsPath = normalizedIpfsPath(input)
if (ipfsPath) return ipfsPath
// Check URL for DNSLink
if (!input.startsWith('http')) return null
const { hostname } = new URL(input)
const dnslink = dnslinkResolver.cachedDnslink(hostname)
if (dnslink) {
// Return full IPNS path (keeps pathname + ?search + #hash)
return dnslinkResolver.convertToIpnsPath(input)
}
// No IPFS path by this point
return null
},
// Resolve URL or path to Immutable IPFS Path:
// - Same as resolveToIpfsPath, but the path is always immutable /ipfs/
// - Keeps pathname + ?search + #hash from original URL
// - Returns null if no valid path can be produced
// The purpose of this resolver is to return immutable /ipfs/ address
// even if /ipns/ is present in its input.
async resolveToImmutableIpfsPath (urlOrPath) {
const path = ipfsPathValidator.resolveToIpfsPath(urlOrPath)
// Fail fast if no IPFS Path
if (!path) return null
// Resolve /ipns/ → /ipfs/
if (IsIpfs.ipnsPath(path)) {
const labels = path.split('/')
// We resolve /ipns/<fqdn> as value in DNSLink cache may be out of date
const ipnsRoot = `/ipns/${labels[2]}`
// js-ipfs v0.34 does not support DNSLinks in ipfs.name.resolve: https://github.com/ipfs/js-ipfs/issues/1918
// TODO: remove ipfsNameResolveWithDnslinkFallback when js-ipfs implements DNSLink support in ipfs.name.resolve
const ipfsNameResolveWithDnslinkFallback = async (resolve) => {
try {
return await resolve()
} catch (err) {
const fqdn = ipnsRoot.replace(/^.*\/ipns\/([^/]+).*/, '$1')
if (err.message === 'Non-base58 character' && isFQDN(fqdn)) {
// js-ipfs without dnslink support, fallback to the value read from DNSLink
const dnslink = dnslinkResolver.readAndCacheDnslink(fqdn)
if (dnslink) {
// swap problematic /ipns/{fqdn} with /ipfs/{cid} and retry lookup
const safePath = trimDoubleSlashes(ipnsRoot.replace(/^.*(\/ipns\/[^/]+)/, dnslink))
if (ipnsRoot !== safePath) {
return ipfsPathValidator.resolveToImmutableIpfsPath(safePath)
}
}
}
throw err
}
}
const result = await ipfsNameResolveWithDnslinkFallback(async () =>
// dhtt/dhtrc optimize for lookup time
getIpfs().name.resolve(ipnsRoot, { recursive: true, dhtt: '5s', dhtrc: 1 })
)
// Old API returned object, latest one returns string ¯\_(ツ)_/¯
const ipfsRoot = result.Path ? result.Path : result
// Return original path with swapped root (keeps pathname + ?search + #hash)
return path.replace(ipnsRoot, ipfsRoot)
}
// Return /ipfs/ path
return path
},
// Resolve URL or path to a raw CID:
// - Result is the direct CID
// - Ignores ?search and #hash from original URL
// - Returns null if no CID can be produced
// The purpose of this resolver is to return direct CID without anything else.
async resolveToCid (urlOrPath) {
const path = ipfsPathValidator.resolveToIpfsPath(urlOrPath)
// Fail fast if no IPFS Path
if (!path) return null
// Drop unused parts
const rawPath = trimHashAndSearch(path)
// js-ipfs v0.34 does not support DNSLinks in ipfs.resolve: https://github.com/ipfs/js-ipfs/issues/1918
// TODO: remove ipfsResolveWithDnslinkFallback when js-ipfs implements DNSLink support in ipfs.resolve
const ipfsResolveWithDnslinkFallback = async (resolve) => {
try {
return await resolve()
} catch (err) {
const fqdn = rawPath.replace(/^.*\/ipns\/([^/]+).*/, '$1')
if (err.message === 'resolve non-IPFS names is not implemented' && isFQDN(fqdn)) {
// js-ipfs without dnslink support, fallback to the value read from DNSLink
const dnslink = dnslinkResolver.readAndCacheDnslink(fqdn)
if (dnslink) {
// swap problematic /ipns/{fqdn} with /ipfs/{cid} and retry lookup
const safePath = trimDoubleSlashes(rawPath.replace(/^.*(\/ipns\/[^/]+)/, dnslink))
if (rawPath !== safePath) {
const result = await ipfsPathValidator.resolveToCid(safePath)
// return in format of ipfs.resolve()
return IsIpfs.cid(result) ? `/ipfs/${result}` : result
}
}
}
throw err
}
}
const result = await ipfsResolveWithDnslinkFallback(async () =>
// dhtt/dhtrc optimize for lookup time
getIpfs().resolve(rawPath, { recursive: true, dhtt: '5s', dhtrc: 1 })
)
const directCid = IsIpfs.ipfsPath(result) ? result.split('/')[2] : result
return directCid
}
}
return ipfsPathValidator
}
exports.createIpfsPathValidator = createIpfsPathValidator
function validIpfsOrIpnsUrl (url, dnsLink) {
// `/ipfs/` is easy to validate, we just check if CID is correct
if (IsIpfs.ipfsUrl(url)) {
return true
}
// `/ipns/` requires multiple stages/branches (can be FQDN with dnslink or CID)
if (validIpnsPath(new URL(url).pathname, dnsLink)) {
return true
}
// everything else is not IPFS-related
return false
}
function validIpfsOrIpnsPath (path, dnsLink) {
// `/ipfs/` is easy to validate, we just check if CID is correct
if (IsIpfs.ipfsPath(path)) {
return true
}
// `/ipns/` requires multiple stages/branches (can be FQDN with dnslink or CID)
if (validIpnsPath(path, dnsLink)) {
return true
}
// everything else is not IPFS-related
return false
}
function validIpnsPath (path, dnsLink) {
if (IsIpfs.ipnsPath(path)) {
// we may have false-positives here, so we do additional checks below
const ipnsRoot = path.match(/^\/ipns\/([^/]+)/)[1]
// console.log('==> IPNS root', ipnsRoot)
// first check if root is a regular CID
if (IsIpfs.cid(ipnsRoot)) {
// console.log('==> IPNS is a valid CID', ipnsRoot)
return true
}
// then see if there is an DNSLink entry for 'ipnsRoot' hostname
// TODO: use dnslink cache only
if (dnsLink.readAndCacheDnslink(ipnsRoot)) {
// console.log('==> IPNS for FQDN with valid dnslink: ', ipnsRoot)
return true
}
}
return false
}