@helia/utils 0.3.3-ec8bf66 → 0.3.3-efdefc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/index.min.js +1 -1
- package/dist/src/index.d.ts +24 -14
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +12 -19
- package/dist/src/index.js.map +1 -1
- package/dist/src/pins.d.ts +6 -3
- package/dist/src/pins.d.ts.map +1 -1
- package/dist/src/pins.js +20 -9
- package/dist/src/pins.js.map +1 -1
- package/dist/src/utils/get-codec.d.ts +4 -0
- package/dist/src/utils/get-codec.d.ts.map +1 -0
- package/dist/src/utils/get-codec.js +38 -0
- package/dist/src/utils/get-codec.js.map +1 -0
- package/dist/src/utils/get-hasher.d.ts +4 -0
- package/dist/src/utils/get-hasher.d.ts.map +1 -0
- package/dist/src/utils/get-hasher.js +32 -0
- package/dist/src/utils/get-hasher.js.map +1 -0
- package/dist/src/utils/is-promise.d.ts +2 -0
- package/dist/src/utils/is-promise.d.ts.map +1 -0
- package/dist/src/utils/is-promise.js +4 -0
- package/dist/src/utils/is-promise.js.map +1 -0
- package/dist/src/utils/networked-storage.d.ts +3 -2
- package/dist/src/utils/networked-storage.d.ts.map +1 -1
- package/dist/src/utils/networked-storage.js +16 -6
- package/dist/src/utils/networked-storage.js.map +1 -1
- package/package.json +4 -3
- package/src/index.ts +36 -26
- package/src/pins.ts +26 -12
- package/src/utils/get-codec.ts +47 -0
- package/src/utils/get-hasher.ts +40 -0
- package/src/utils/is-promise.ts +3 -0
- package/src/utils/networked-storage.ts +21 -8
- package/dist/src/utils/dag-walkers.d.ts +0 -28
- package/dist/src/utils/dag-walkers.d.ts.map +0 -1
- package/dist/src/utils/dag-walkers.js +0 -171
- package/dist/src/utils/dag-walkers.js.map +0 -1
- package/dist/src/utils/default-hashers.d.ts +0 -3
- package/dist/src/utils/default-hashers.d.ts.map +0 -1
- package/dist/src/utils/default-hashers.js +0 -15
- package/dist/src/utils/default-hashers.js.map +0 -1
- package/src/utils/dag-walkers.ts +0 -198
- package/src/utils/default-hashers.ts +0 -18
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { identity } from 'multiformats/hashes/identity';
|
|
2
|
-
import { sha256, sha512 } from 'multiformats/hashes/sha2';
|
|
3
|
-
export function defaultHashers(hashers = []) {
|
|
4
|
-
const output = {};
|
|
5
|
-
[
|
|
6
|
-
sha256,
|
|
7
|
-
sha512,
|
|
8
|
-
identity,
|
|
9
|
-
...hashers
|
|
10
|
-
].forEach(hasher => {
|
|
11
|
-
output[hasher.code] = hasher;
|
|
12
|
-
});
|
|
13
|
-
return output;
|
|
14
|
-
}
|
|
15
|
-
//# sourceMappingURL=default-hashers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"default-hashers.js","sourceRoot":"","sources":["../../../src/utils/default-hashers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAGzD,MAAM,UAAU,cAAc,CAAE,UAA6B,EAAE;IAC7D,MAAM,MAAM,GAAoC,EAAE,CAEjD;IAAA;QACC,MAAM;QACN,MAAM;QACN,QAAQ;QACR,GAAG,OAAO;KACX,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACjB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAA;IAC9B,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/src/utils/dag-walkers.ts
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
/* eslint max-depth: ["error", 7] */
|
|
2
|
-
|
|
3
|
-
import * as dagCbor from '@ipld/dag-cbor'
|
|
4
|
-
import * as dagJson from '@ipld/dag-json'
|
|
5
|
-
import * as dagPb from '@ipld/dag-pb'
|
|
6
|
-
import * as cborg from 'cborg'
|
|
7
|
-
import { Type, Token } from 'cborg'
|
|
8
|
-
import * as cborgJson from 'cborg/json'
|
|
9
|
-
import { CID } from 'multiformats'
|
|
10
|
-
import { base64 } from 'multiformats/bases/base64'
|
|
11
|
-
import * as json from 'multiformats/codecs/json'
|
|
12
|
-
import * as raw from 'multiformats/codecs/raw'
|
|
13
|
-
import type { DAGWalker } from '@helia/interface'
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Dag walker for dag-pb CIDs
|
|
17
|
-
*/
|
|
18
|
-
export const dagPbWalker: DAGWalker = {
|
|
19
|
-
codec: dagPb.code,
|
|
20
|
-
* walk (block) {
|
|
21
|
-
const node = dagPb.decode(block)
|
|
22
|
-
|
|
23
|
-
yield * node.Links.map(l => l.Hash)
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Dag walker for raw CIDs
|
|
29
|
-
*/
|
|
30
|
-
export const rawWalker: DAGWalker = {
|
|
31
|
-
codec: raw.code,
|
|
32
|
-
* walk () {
|
|
33
|
-
// no embedded CIDs in a raw block
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
|
|
38
|
-
const CID_TAG = 42
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
|
|
42
|
-
* all we are interested in is extracting the the CIDs from the block
|
|
43
|
-
* so we can just use cborg for that.
|
|
44
|
-
*/
|
|
45
|
-
export const dagCborWalker: DAGWalker = {
|
|
46
|
-
codec: dagCbor.code,
|
|
47
|
-
* walk (block) {
|
|
48
|
-
const cids: CID[] = []
|
|
49
|
-
const tags: cborg.TagDecoder[] = []
|
|
50
|
-
tags[CID_TAG] = (bytes) => {
|
|
51
|
-
if (bytes[0] !== 0) {
|
|
52
|
-
throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const cid = CID.decode(bytes.subarray(1)) // ignore leading 0x00
|
|
56
|
-
|
|
57
|
-
cids.push(cid)
|
|
58
|
-
|
|
59
|
-
return cid
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
cborg.decode(block, {
|
|
63
|
-
tags
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
yield * cids
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Borrowed from @ipld/dag-json
|
|
72
|
-
*/
|
|
73
|
-
class DagJsonTokenizer extends cborgJson.Tokenizer {
|
|
74
|
-
private readonly tokenBuffer: cborg.Token[]
|
|
75
|
-
|
|
76
|
-
constructor (data: Uint8Array, options?: cborg.DecodeOptions) {
|
|
77
|
-
super(data, options)
|
|
78
|
-
|
|
79
|
-
this.tokenBuffer = []
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
done (): boolean {
|
|
83
|
-
return this.tokenBuffer.length === 0 && super.done()
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
_next (): cborg.Token {
|
|
87
|
-
if (this.tokenBuffer.length > 0) {
|
|
88
|
-
// @ts-expect-error https://github.com/Microsoft/TypeScript/issues/30406
|
|
89
|
-
return this.tokenBuffer.pop()
|
|
90
|
-
}
|
|
91
|
-
return super.next()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Implements rules outlined in https://github.com/ipld/specs/pull/356
|
|
96
|
-
*/
|
|
97
|
-
next (): cborg.Token {
|
|
98
|
-
const token = this._next()
|
|
99
|
-
|
|
100
|
-
if (token.type === Type.map) {
|
|
101
|
-
const keyToken = this._next()
|
|
102
|
-
if (keyToken.type === Type.string && keyToken.value === '/') {
|
|
103
|
-
const valueToken = this._next()
|
|
104
|
-
if (valueToken.type === Type.string) { // *must* be a CID
|
|
105
|
-
const breakToken = this._next() // swallow the end-of-map token
|
|
106
|
-
if (breakToken.type !== Type.break) {
|
|
107
|
-
throw new Error('Invalid encoded CID form')
|
|
108
|
-
}
|
|
109
|
-
this.tokenBuffer.push(valueToken) // CID.parse will pick this up after our tag token
|
|
110
|
-
return new Token(Type.tag, 42, 0)
|
|
111
|
-
}
|
|
112
|
-
if (valueToken.type === Type.map) {
|
|
113
|
-
const innerKeyToken = this._next()
|
|
114
|
-
if (innerKeyToken.type === Type.string && innerKeyToken.value === 'bytes') {
|
|
115
|
-
const innerValueToken = this._next()
|
|
116
|
-
if (innerValueToken.type === Type.string) { // *must* be Bytes
|
|
117
|
-
for (let i = 0; i < 2; i++) {
|
|
118
|
-
const breakToken = this._next() // swallow two end-of-map tokens
|
|
119
|
-
if (breakToken.type !== Type.break) {
|
|
120
|
-
throw new Error('Invalid encoded Bytes form')
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
const bytes = base64.decode(`m${innerValueToken.value}`)
|
|
124
|
-
return new Token(Type.bytes, bytes, innerValueToken.value.length)
|
|
125
|
-
}
|
|
126
|
-
this.tokenBuffer.push(innerValueToken) // bail
|
|
127
|
-
}
|
|
128
|
-
this.tokenBuffer.push(innerKeyToken) // bail
|
|
129
|
-
}
|
|
130
|
-
this.tokenBuffer.push(valueToken) // bail
|
|
131
|
-
}
|
|
132
|
-
this.tokenBuffer.push(keyToken) // bail
|
|
133
|
-
}
|
|
134
|
-
return token
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Dag walker for dag-json CIDs. Does not actually use dag-json since
|
|
140
|
-
* all we are interested in is extracting the the CIDs from the block
|
|
141
|
-
* so we can just use cborg/json for that.
|
|
142
|
-
*/
|
|
143
|
-
export const dagJsonWalker: DAGWalker = {
|
|
144
|
-
codec: dagJson.code,
|
|
145
|
-
* walk (block) {
|
|
146
|
-
const cids: CID[] = []
|
|
147
|
-
const tags: cborg.TagDecoder[] = []
|
|
148
|
-
tags[CID_TAG] = (string) => {
|
|
149
|
-
const cid = CID.parse(string)
|
|
150
|
-
|
|
151
|
-
cids.push(cid)
|
|
152
|
-
|
|
153
|
-
return cid
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
cborgJson.decode(block, {
|
|
157
|
-
tags,
|
|
158
|
-
tokenizer: new DagJsonTokenizer(block, {
|
|
159
|
-
tags,
|
|
160
|
-
allowIndefinite: true,
|
|
161
|
-
allowUndefined: true,
|
|
162
|
-
allowNaN: true,
|
|
163
|
-
allowInfinity: true,
|
|
164
|
-
allowBigInt: true,
|
|
165
|
-
strict: false,
|
|
166
|
-
rejectDuplicateMapKeys: false
|
|
167
|
-
})
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
yield * cids
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Dag walker for json CIDs. JSON has no facility for linking to
|
|
176
|
-
* external blocks so the walker is a no-op.
|
|
177
|
-
*/
|
|
178
|
-
export const jsonWalker: DAGWalker = {
|
|
179
|
-
codec: json.code,
|
|
180
|
-
* walk () {}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export function defaultDagWalkers (walkers: DAGWalker[] = []): Record<number, DAGWalker> {
|
|
184
|
-
const output: Record<number, DAGWalker> = {}
|
|
185
|
-
|
|
186
|
-
;[
|
|
187
|
-
dagPbWalker,
|
|
188
|
-
rawWalker,
|
|
189
|
-
dagCborWalker,
|
|
190
|
-
dagJsonWalker,
|
|
191
|
-
jsonWalker,
|
|
192
|
-
...walkers
|
|
193
|
-
].forEach(dagWalker => {
|
|
194
|
-
output[dagWalker.codec] = dagWalker
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
return output
|
|
198
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { identity } from 'multiformats/hashes/identity'
|
|
2
|
-
import { sha256, sha512 } from 'multiformats/hashes/sha2'
|
|
3
|
-
import type { MultihashHasher } from 'multiformats/hashes/interface'
|
|
4
|
-
|
|
5
|
-
export function defaultHashers (hashers: MultihashHasher[] = []): Record<number, MultihashHasher> {
|
|
6
|
-
const output: Record<number, MultihashHasher> = {}
|
|
7
|
-
|
|
8
|
-
;[
|
|
9
|
-
sha256,
|
|
10
|
-
sha512,
|
|
11
|
-
identity,
|
|
12
|
-
...hashers
|
|
13
|
-
].forEach(hasher => {
|
|
14
|
-
output[hasher.code] = hasher
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
return output
|
|
18
|
-
}
|