This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathresolve.js
74 lines (61 loc) · 1.96 KB
/
resolve.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
'use strict'
const isIpfs = require('is-ipfs')
const { CID } = require('multiformats/cid')
const PeerID = require('peer-id')
const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
const { resolve: res } = require('../utils')
/**
* @param {Object} config
* @param {import('ipfs-repo').IPFSRepo} config.repo
* @param {import('ipfs-core-utils/src/multicodecs')} config.codecs
* @param {import('ipfs-core-utils/src/multibases')} config.bases
* @param {import('ipfs-core-types/src/name').API} config.name
*/
module.exports = ({ repo, codecs, bases, name }) => {
/**
* @type {import('ipfs-core-types/src/root').API["resolve"]}
*/
async function resolve (path, opts = {}) {
if (!isIpfs.path(path)) {
throw new Error('invalid argument ' + path)
}
if (isIpfs.ipnsPath(path)) {
for await (const resolvedPath of name.resolve(path, opts)) {
path = resolvedPath
}
}
const [, schema, hash, ...rest] = path.split('/') // ['', 'ipfs', 'hash', ...path]
const base = opts.cidBase ? await bases.getBase(opts.cidBase) : undefined
const bytes = parseBytes(hash)
// nothing to resolve return the input
if (rest.length === 0) {
const str = base ? base.encoder.encode(bytes) : hash
return `/${schema}/${str}`
}
const cid = CID.decode(bytes)
path = rest.join('/')
const results = res(cid, path, codecs, repo, opts)
let value = cid
let remainderPath = path
for await (const result of results) {
if (result.value instanceof CID) {
value = result.value
remainderPath = result.remainderPath
}
}
return `/ipfs/${value.toString(base && base.encoder)}${remainderPath ? '/' + remainderPath : ''}`
}
return withTimeoutOption(resolve)
}
/**
* Parse the input as a PeerID or a CID or throw an error
*
* @param {string} str
*/
function parseBytes (str) {
try {
return PeerID.parse(str).toBytes()
} catch {
return CID.parse(str).bytes
}
}