-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathraw.js
54 lines (45 loc) · 1.16 KB
/
raw.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
'use strict'
const errCode = require('err-code')
const extractDataFromBlock = require('../utils/extract-data-from-block')
const validateOffsetAndLength = require('../utils/validate-offset-and-length')
/**
* @typedef {import('../types').ExporterOptions} ExporterOptions
*/
/**
* @param {Uint8Array} node
*/
const rawContent = (node) => {
/**
* @param {ExporterOptions} options
*/
async function * contentGenerator (options = {}) {
const {
offset,
length
} = validateOffsetAndLength(node.length, options.offset, options.length)
yield extractDataFromBlock(node, 0, offset, offset + length)
}
return contentGenerator
}
/**
* @type {import('../types').Resolver}
*/
const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
if (toResolve.length) {
throw errCode(new Error(`No link named ${path} found in raw node ${cid}`), 'ERR_NOT_FOUND')
}
const block = await blockstore.get(cid, options)
return {
entry: {
type: 'raw',
name,
path,
cid,
content: rawContent(block),
depth,
size: block.length,
node: block
}
}
}
module.exports = resolve