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 pathget.js
53 lines (44 loc) · 1.5 KB
/
get.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
import { configure } from '../lib/configure.js'
import { resolve } from '../lib/resolve.js'
import first from 'it-first'
import last from 'it-last'
import errCode from 'err-code'
import { createGet as createBlockGet } from '../block/get.js'
/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/dag').API<HTTPClientExtraOptions>} DAGAPI
*/
/**
* @param {import('ipfs-core-utils/multicodecs').Multicodecs} codecs
* @param {import('../types').Options} options
*/
export const createGet = (codecs, options) => {
const fn = configure((api, opts) => {
const getBlock = createBlockGet(opts)
/**
* @type {DAGAPI["get"]}
*/
const get = async (cid, options = {}) => {
if (options.path) {
const entry = options.localResolve
? await first(resolve(cid, options.path, codecs, getBlock, options))
: await last(resolve(cid, options.path, codecs, getBlock, options))
/** @type {import('ipfs-core-types/src/dag').GetResult | undefined} - first and last will return undefined when empty */
const result = (entry)
if (!result) {
throw errCode(new Error('Not found'), 'ERR_NOT_FOUND')
}
return result
}
const codec = await codecs.getCodec(cid.code)
const block = await getBlock(cid, options)
const node = codec.decode(block)
return {
value: node,
remainderPath: ''
}
}
return get
})
return fn(options)
}