@helia/ipns 0.0.0
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/LICENSE +4 -0
- package/README.md +59 -0
- package/dist/index.min.js +25 -0
- package/dist/src/index.d.ts +124 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +192 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/routing/dht.d.ts +18 -0
- package/dist/src/routing/dht.d.ts.map +1 -0
- package/dist/src/routing/dht.js +65 -0
- package/dist/src/routing/dht.js.map +1 -0
- package/dist/src/routing/index.d.ts +17 -0
- package/dist/src/routing/index.d.ts.map +1 -0
- package/dist/src/routing/index.js +3 -0
- package/dist/src/routing/index.js.map +1 -0
- package/dist/src/routing/local-store.d.ts +15 -0
- package/dist/src/routing/local-store.d.ts.map +1 -0
- package/dist/src/routing/local-store.js +48 -0
- package/dist/src/routing/local-store.js.map +1 -0
- package/dist/src/routing/pubsub.d.ts +20 -0
- package/dist/src/routing/pubsub.d.ts.map +1 -0
- package/dist/src/routing/pubsub.js +150 -0
- package/dist/src/routing/pubsub.js.map +1 -0
- package/dist/src/utils/resolve-dns-link.browser.d.ts +6 -0
- package/dist/src/utils/resolve-dns-link.browser.d.ts.map +1 -0
- package/dist/src/utils/resolve-dns-link.browser.js +46 -0
- package/dist/src/utils/resolve-dns-link.browser.js.map +1 -0
- package/dist/src/utils/resolve-dns-link.d.ts +3 -0
- package/dist/src/utils/resolve-dns-link.d.ts.map +1 -0
- package/dist/src/utils/resolve-dns-link.js +54 -0
- package/dist/src/utils/resolve-dns-link.js.map +1 -0
- package/dist/src/utils/tlru.d.ts +15 -0
- package/dist/src/utils/tlru.d.ts.map +1 -0
- package/dist/src/utils/tlru.js +39 -0
- package/dist/src/utils/tlru.js.map +1 -0
- package/package.json +191 -0
- package/src/index.ts +296 -0
- package/src/routing/dht.ts +85 -0
- package/src/routing/index.ts +26 -0
- package/src/routing/local-store.ts +63 -0
- package/src/routing/pubsub.ts +195 -0
- package/src/utils/resolve-dns-link.browser.ts +61 -0
- package/src/utils/resolve-dns-link.ts +65 -0
- package/src/utils/tlru.ts +52 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* IPNS operations using a Helia node
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { gossipsub } from '@chainsafe/libp2p'
|
|
10
|
+
* import { kadDHT } from '@libp2p/kad-dht'
|
|
11
|
+
* import { createLibp2p } from 'libp2p'
|
|
12
|
+
* import { createHelia } from 'helia'
|
|
13
|
+
* import { ipns, ipnsValidator, ipnsSelector } from '@helia/ipns'
|
|
14
|
+
* import { dht, pubsub } from '@helia/ipns/routing'
|
|
15
|
+
* import { unixfs } from '@helia/unixfs
|
|
16
|
+
*
|
|
17
|
+
* const libp2p = await createLibp2p({
|
|
18
|
+
* dht: kadDHT({
|
|
19
|
+
* validators: {
|
|
20
|
+
* ipns: ipnsValidator
|
|
21
|
+
* },
|
|
22
|
+
* selectors: {
|
|
23
|
+
* ipns: ipnsSelector
|
|
24
|
+
* }
|
|
25
|
+
* }),
|
|
26
|
+
* pubsub: gossipsub()
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* const helia = await createHelia({
|
|
30
|
+
* libp2p,
|
|
31
|
+
* //.. other options
|
|
32
|
+
* })
|
|
33
|
+
* const name = ipns(helia, [
|
|
34
|
+
* dht(helia)
|
|
35
|
+
* pubsub(helia)
|
|
36
|
+
* ])
|
|
37
|
+
*
|
|
38
|
+
* // create a public key to publish as an IPNS name
|
|
39
|
+
* const keyInfo = await helia.libp2p.keychain.createKey('my-key')
|
|
40
|
+
* const peerId = await helia.libp2p.keychain.exportPeerId(keyInfo.name)
|
|
41
|
+
*
|
|
42
|
+
* // store some data to publish
|
|
43
|
+
* const fs = unixfs(helia)
|
|
44
|
+
* const cid = await fs.add(Uint8Array.from([0, 1, 2, 3, 4]))
|
|
45
|
+
*
|
|
46
|
+
* // publish the name
|
|
47
|
+
* await name.publish(peerId, cid)
|
|
48
|
+
*
|
|
49
|
+
* // resolve the name
|
|
50
|
+
* const cid = name.resolve(peerId)
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
*
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // resolve a CID from a TXT record in a DNS zone file, eg:
|
|
57
|
+
* // > dig ipfs.io TXT
|
|
58
|
+
* // ;; ANSWER SECTION:
|
|
59
|
+
* // ipfs.io. 435 IN TXT "dnslink=/ipfs/Qmfoo"
|
|
60
|
+
*
|
|
61
|
+
* const cid = name.resolveDns('ipfs.io')
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
65
|
+
import { PeerId } from '@libp2p/interface-peer-id';
|
|
66
|
+
import type { IPNSEntry } from 'ipns';
|
|
67
|
+
import type { IPNSRouting, IPNSRoutingEvents } from './routing/index.js';
|
|
68
|
+
import { ipnsValidator } from 'ipns/validator';
|
|
69
|
+
import { CID } from 'multiformats/cid';
|
|
70
|
+
import type { ProgressEvent, ProgressOptions } from 'progress-events';
|
|
71
|
+
import type { Datastore } from 'interface-datastore';
|
|
72
|
+
export type PublishProgressEvents = ProgressEvent<'ipns:publish:start'> | ProgressEvent<'ipns:publish:success', IPNSEntry> | ProgressEvent<'ipns:publish:error', Error>;
|
|
73
|
+
export type ResolveProgressEvents = ProgressEvent<'ipns:resolve:start', unknown> | ProgressEvent<'ipns:resolve:success', IPNSEntry> | ProgressEvent<'ipns:resolve:error', Error>;
|
|
74
|
+
export type RepublishProgressEvents = ProgressEvent<'ipns:republish:start', unknown> | ProgressEvent<'ipns:republish:success', IPNSEntry> | ProgressEvent<'ipns:republish:error', {
|
|
75
|
+
record: IPNSEntry;
|
|
76
|
+
err: Error;
|
|
77
|
+
}>;
|
|
78
|
+
export interface PublishOptions extends AbortOptions, ProgressOptions<PublishProgressEvents | IPNSRoutingEvents> {
|
|
79
|
+
/**
|
|
80
|
+
* Time duration of the record in ms
|
|
81
|
+
*/
|
|
82
|
+
lifetime?: number;
|
|
83
|
+
}
|
|
84
|
+
export interface ResolveOptions extends AbortOptions, ProgressOptions<ResolveProgressEvents | IPNSRoutingEvents> {
|
|
85
|
+
/**
|
|
86
|
+
* do not use cached entries
|
|
87
|
+
*/
|
|
88
|
+
nocache?: boolean;
|
|
89
|
+
}
|
|
90
|
+
export interface RepublishOptions extends AbortOptions, ProgressOptions<RepublishProgressEvents | IPNSRoutingEvents> {
|
|
91
|
+
/**
|
|
92
|
+
* The republish interval in ms (default: 24hrs)
|
|
93
|
+
*/
|
|
94
|
+
interval?: number;
|
|
95
|
+
}
|
|
96
|
+
export interface IPNS {
|
|
97
|
+
/**
|
|
98
|
+
* Creates an IPNS record signed by the passed PeerId that will resolve to the passed value
|
|
99
|
+
*
|
|
100
|
+
* If the valid is a PeerId, a recursive IPNS record will be created.
|
|
101
|
+
*/
|
|
102
|
+
publish: (key: PeerId, value: CID | PeerId, options?: PublishOptions) => Promise<IPNSEntry>;
|
|
103
|
+
/**
|
|
104
|
+
* Accepts a public key formatted as a libp2p PeerID and resolves the IPNS record
|
|
105
|
+
* corresponding to that public key until a value is found
|
|
106
|
+
*/
|
|
107
|
+
resolve: (key: PeerId, options?: ResolveOptions) => Promise<CID>;
|
|
108
|
+
/**
|
|
109
|
+
* Resolve a CID from a dns-link style IPNS record
|
|
110
|
+
*/
|
|
111
|
+
resolveDns: (domain: string, options?: ResolveOptions) => Promise<CID>;
|
|
112
|
+
/**
|
|
113
|
+
* Periodically republish all IPNS records found in the datastore
|
|
114
|
+
*/
|
|
115
|
+
republish: (options?: RepublishOptions) => void;
|
|
116
|
+
}
|
|
117
|
+
export type { IPNSRouting } from './routing/index.js';
|
|
118
|
+
export interface IPNSComponents {
|
|
119
|
+
datastore: Datastore;
|
|
120
|
+
}
|
|
121
|
+
export declare function ipns(components: IPNSComponents, routers?: IPNSRouting[]): IPNS;
|
|
122
|
+
export { ipnsValidator };
|
|
123
|
+
export { ipnsSelector } from 'ipns/selector';
|
|
124
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,EAAY,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAE5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAA;AACrC,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAItC,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAIrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAWpD,MAAM,MAAM,qBAAqB,GAC/B,aAAa,CAAC,oBAAoB,CAAC,GACnC,aAAa,CAAC,sBAAsB,EAAE,SAAS,CAAC,GAChD,aAAa,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAA;AAE5C,MAAM,MAAM,qBAAqB,GAC/B,aAAa,CAAC,oBAAoB,EAAE,OAAO,CAAC,GAC5C,aAAa,CAAC,sBAAsB,EAAE,SAAS,CAAC,GAChD,aAAa,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAA;AAE5C,MAAM,MAAM,uBAAuB,GACjC,aAAa,CAAC,sBAAsB,EAAE,OAAO,CAAC,GAC9C,aAAa,CAAC,wBAAwB,EA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E,WAAW,EAAO,GAAG,IAAI,CAEnF;AAED,OAAO,EAAE,aAAa,EAAE,CAAA;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA"}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* IPNS operations using a Helia node
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { gossipsub } from '@chainsafe/libp2p'
|
|
10
|
+
* import { kadDHT } from '@libp2p/kad-dht'
|
|
11
|
+
* import { createLibp2p } from 'libp2p'
|
|
12
|
+
* import { createHelia } from 'helia'
|
|
13
|
+
* import { ipns, ipnsValidator, ipnsSelector } from '@helia/ipns'
|
|
14
|
+
* import { dht, pubsub } from '@helia/ipns/routing'
|
|
15
|
+
* import { unixfs } from '@helia/unixfs
|
|
16
|
+
*
|
|
17
|
+
* const libp2p = await createLibp2p({
|
|
18
|
+
* dht: kadDHT({
|
|
19
|
+
* validators: {
|
|
20
|
+
* ipns: ipnsValidator
|
|
21
|
+
* },
|
|
22
|
+
* selectors: {
|
|
23
|
+
* ipns: ipnsSelector
|
|
24
|
+
* }
|
|
25
|
+
* }),
|
|
26
|
+
* pubsub: gossipsub()
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* const helia = await createHelia({
|
|
30
|
+
* libp2p,
|
|
31
|
+
* //.. other options
|
|
32
|
+
* })
|
|
33
|
+
* const name = ipns(helia, [
|
|
34
|
+
* dht(helia)
|
|
35
|
+
* pubsub(helia)
|
|
36
|
+
* ])
|
|
37
|
+
*
|
|
38
|
+
* // create a public key to publish as an IPNS name
|
|
39
|
+
* const keyInfo = await helia.libp2p.keychain.createKey('my-key')
|
|
40
|
+
* const peerId = await helia.libp2p.keychain.exportPeerId(keyInfo.name)
|
|
41
|
+
*
|
|
42
|
+
* // store some data to publish
|
|
43
|
+
* const fs = unixfs(helia)
|
|
44
|
+
* const cid = await fs.add(Uint8Array.from([0, 1, 2, 3, 4]))
|
|
45
|
+
*
|
|
46
|
+
* // publish the name
|
|
47
|
+
* await name.publish(peerId, cid)
|
|
48
|
+
*
|
|
49
|
+
* // resolve the name
|
|
50
|
+
* const cid = name.resolve(peerId)
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
*
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // resolve a CID from a TXT record in a DNS zone file, eg:
|
|
57
|
+
* // > dig ipfs.io TXT
|
|
58
|
+
* // ;; ANSWER SECTION:
|
|
59
|
+
* // ipfs.io. 435 IN TXT "dnslink=/ipfs/Qmfoo"
|
|
60
|
+
*
|
|
61
|
+
* const cid = name.resolveDns('ipfs.io')
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
65
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
66
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
67
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
68
|
+
};
|
|
69
|
+
var _DefaultIPNS_instances, _DefaultIPNS_resolve, _DefaultIPNS_findIpnsRecord;
|
|
70
|
+
import { isPeerId } from '@libp2p/interface-peer-id';
|
|
71
|
+
import { create, marshal, peerIdToRoutingKey, unmarshal } from 'ipns';
|
|
72
|
+
import { ipnsValidator } from 'ipns/validator';
|
|
73
|
+
import { CID } from 'multiformats/cid';
|
|
74
|
+
import { resolveDnslink } from './utils/resolve-dns-link.js';
|
|
75
|
+
import { logger } from '@libp2p/logger';
|
|
76
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
77
|
+
import { CustomProgressEvent } from 'progress-events';
|
|
78
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
79
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
80
|
+
import { localStore } from './routing/local-store.js';
|
|
81
|
+
const log = logger('helia:ipns');
|
|
82
|
+
const MINUTE = 60 * 1000;
|
|
83
|
+
const HOUR = 60 * MINUTE;
|
|
84
|
+
const DEFAULT_LIFETIME_MS = 24 * HOUR;
|
|
85
|
+
const DEFAULT_REPUBLISH_INTERVAL_MS = 23 * HOUR;
|
|
86
|
+
class DefaultIPNS {
|
|
87
|
+
constructor(components, routers = []) {
|
|
88
|
+
_DefaultIPNS_instances.add(this);
|
|
89
|
+
this.routers = routers;
|
|
90
|
+
this.localStore = localStore(components.datastore);
|
|
91
|
+
}
|
|
92
|
+
async publish(key, value, options = {}) {
|
|
93
|
+
try {
|
|
94
|
+
let sequenceNumber = 1n;
|
|
95
|
+
const routingKey = peerIdToRoutingKey(key);
|
|
96
|
+
if (await this.localStore.has(routingKey, options)) {
|
|
97
|
+
// if we have published under this key before, increment the sequence number
|
|
98
|
+
const buf = await this.localStore.get(routingKey, options);
|
|
99
|
+
const existingRecord = unmarshal(buf);
|
|
100
|
+
sequenceNumber = existingRecord.sequence + 1n;
|
|
101
|
+
}
|
|
102
|
+
let str;
|
|
103
|
+
if (isPeerId(value)) {
|
|
104
|
+
str = `/ipns/${value.toString()}`;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
str = `/ipfs/${value.toString()}`;
|
|
108
|
+
}
|
|
109
|
+
const bytes = uint8ArrayFromString(str);
|
|
110
|
+
// create record
|
|
111
|
+
const record = await create(key, bytes, sequenceNumber, options.lifetime ?? DEFAULT_LIFETIME_MS);
|
|
112
|
+
const marshaledRecord = marshal(record);
|
|
113
|
+
await this.localStore.put(routingKey, marshaledRecord, options);
|
|
114
|
+
// publish record to routing
|
|
115
|
+
await Promise.all(this.routers.map(async (r) => { await r.put(routingKey, marshaledRecord, options); }));
|
|
116
|
+
return record;
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
options.onProgress?.(new CustomProgressEvent('ipns:publish:error', err));
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async resolve(key, options = {}) {
|
|
124
|
+
const routingKey = peerIdToRoutingKey(key);
|
|
125
|
+
const record = await __classPrivateFieldGet(this, _DefaultIPNS_instances, "m", _DefaultIPNS_findIpnsRecord).call(this, routingKey, options);
|
|
126
|
+
const str = uint8ArrayToString(record.value);
|
|
127
|
+
return await __classPrivateFieldGet(this, _DefaultIPNS_instances, "m", _DefaultIPNS_resolve).call(this, str);
|
|
128
|
+
}
|
|
129
|
+
async resolveDns(domain, options = {}) {
|
|
130
|
+
const dnslink = await resolveDnslink(domain, options);
|
|
131
|
+
return await __classPrivateFieldGet(this, _DefaultIPNS_instances, "m", _DefaultIPNS_resolve).call(this, dnslink);
|
|
132
|
+
}
|
|
133
|
+
republish(options = {}) {
|
|
134
|
+
if (this.timeout != null) {
|
|
135
|
+
throw new Error('Republish is already running');
|
|
136
|
+
}
|
|
137
|
+
options.signal?.addEventListener('abort', () => {
|
|
138
|
+
clearTimeout(this.timeout);
|
|
139
|
+
});
|
|
140
|
+
async function republish() {
|
|
141
|
+
const startTime = Date.now();
|
|
142
|
+
options.onProgress?.(new CustomProgressEvent('ipns:republish:start'));
|
|
143
|
+
const finishType = Date.now();
|
|
144
|
+
const timeTaken = finishType - startTime;
|
|
145
|
+
let nextInterval = DEFAULT_REPUBLISH_INTERVAL_MS - timeTaken;
|
|
146
|
+
if (nextInterval < 0) {
|
|
147
|
+
nextInterval = options.interval ?? DEFAULT_REPUBLISH_INTERVAL_MS;
|
|
148
|
+
}
|
|
149
|
+
setTimeout(() => {
|
|
150
|
+
republish().catch(err => {
|
|
151
|
+
log.error('error republishing', err);
|
|
152
|
+
});
|
|
153
|
+
}, nextInterval);
|
|
154
|
+
}
|
|
155
|
+
this.timeout = setTimeout(() => {
|
|
156
|
+
republish().catch(err => {
|
|
157
|
+
log.error('error republishing', err);
|
|
158
|
+
});
|
|
159
|
+
}, options.interval ?? DEFAULT_REPUBLISH_INTERVAL_MS);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
_DefaultIPNS_instances = new WeakSet(), _DefaultIPNS_resolve = async function _DefaultIPNS_resolve(ipfsPath) {
|
|
163
|
+
const parts = ipfsPath.split('/');
|
|
164
|
+
if (parts.length === 3) {
|
|
165
|
+
const scheme = parts[1];
|
|
166
|
+
if (scheme === 'ipns') {
|
|
167
|
+
return await this.resolve(peerIdFromString(parts[2]));
|
|
168
|
+
}
|
|
169
|
+
else if (scheme === 'ipfs') {
|
|
170
|
+
return CID.parse(parts[2]);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
log.error('invalid ipfs path %s', ipfsPath);
|
|
174
|
+
throw new Error('Invalid value');
|
|
175
|
+
}, _DefaultIPNS_findIpnsRecord = async function _DefaultIPNS_findIpnsRecord(routingKey, options) {
|
|
176
|
+
const routers = [
|
|
177
|
+
this.localStore,
|
|
178
|
+
...this.routers
|
|
179
|
+
];
|
|
180
|
+
const unmarshaledRecord = await Promise.any(routers.map(async (router) => {
|
|
181
|
+
const unmarshaledRecord = await router.get(routingKey, options);
|
|
182
|
+
await ipnsValidator(routingKey, unmarshaledRecord);
|
|
183
|
+
return unmarshaledRecord;
|
|
184
|
+
}));
|
|
185
|
+
return unmarshal(unmarshaledRecord);
|
|
186
|
+
};
|
|
187
|
+
export function ipns(components, routers = []) {
|
|
188
|
+
return new DefaultIPNS(components, routers);
|
|
189
|
+
}
|
|
190
|
+
export { ipnsValidator };
|
|
191
|
+
export { ipnsSelector } from 'ipns/selector';
|
|
192
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;;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,UAAsB,EAAE,OAAqB;IAClE,MAAM,OAAO,GAAG;QACd,IAAI,CAAC,UAAU;QACf,GAAG,IAAI,CAAC,OAAO;KAChB,CAAA;IAED,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,GAAG,CACzC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;QAC3B,MAAM,iBAAiB,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;QAC/D,MAAM,aAAa,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAA;QAElD,OAAO,iBAAiB,CAAA;IAC1B,CAAC,CAAC,CACH,CAAA;IAED,OAAO,SAAS,CAAC,iBAAiB,CAAC,CAAA;AACrC,CAAC;AAGH,MAAM,UAAU,IAAI,CAAE,UAA0B,EAAE,UAAyB,EAAE;IAC3E,OAAO,IAAI,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;AAC7C,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,CAAA;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IPNSRouting } from '../index.js';
|
|
2
|
+
import type { DHT, QueryEvent } from '@libp2p/interface-dht';
|
|
3
|
+
import type { GetOptions, PutOptions } from './index.js';
|
|
4
|
+
import { ProgressEvent } from 'progress-events';
|
|
5
|
+
export interface DHTRoutingComponents {
|
|
6
|
+
libp2p: {
|
|
7
|
+
dht: DHT;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export type DHTProgressEvents = ProgressEvent<'ipns:routing:dht:query', QueryEvent> | ProgressEvent<'ipns:routing:dht:error', Error>;
|
|
11
|
+
export declare class DHTRouting implements IPNSRouting {
|
|
12
|
+
private readonly dht;
|
|
13
|
+
constructor(components: DHTRoutingComponents);
|
|
14
|
+
put(routingKey: Uint8Array, marshaledRecord: Uint8Array, options?: PutOptions): Promise<void>;
|
|
15
|
+
get(routingKey: Uint8Array, options?: GetOptions): Promise<Uint8Array>;
|
|
16
|
+
}
|
|
17
|
+
export declare function dht(components: DHTRoutingComponents): IPNSRouting;
|
|
18
|
+
//# sourceMappingURL=dht.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dht.d.ts","sourceRoot":"","sources":["../../../src/routing/dht.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAC9C,OAAO,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACxD,OAAO,EAAuB,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAIpE,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE;QACN,GAAG,EAAE,GAAG,CAAA;KACT,CAAA;CACF;AAED,MAAM,MAAM,iBAAiB,GAC3B,aAAa,CAAC,wBAAwB,EAAE,UAAU,CAAC,GACnD,aAAa,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAA;AAEhD,qBAAa,UAAW,YAAW,WAAW;IAC5C,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAK;gBAEZ,UAAU,EAAE,oBAAoB;IAIvC,GAAG,CAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,EAAE,OAAO,GAAE,UAAe,GAAG,OAAO,CAAC,IAAI,CAAC;IAsBlG,GAAG,CAAE,UAAU,EAAE,UAAU,EAAE,OAAO,GAAE,UAAe,GAAG,OAAO,CAAC,UAAU,CAAC;CAiBlF;AAkBD,wBAAgB,GAAG,CAAE,UAAU,EAAE,oBAAoB,GAAG,WAAW,CAElE"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger';
|
|
2
|
+
import { CustomProgressEvent } from 'progress-events';
|
|
3
|
+
const log = logger('helia:ipns:routing:dht');
|
|
4
|
+
export class DHTRouting {
|
|
5
|
+
constructor(components) {
|
|
6
|
+
this.dht = components.libp2p.dht;
|
|
7
|
+
}
|
|
8
|
+
async put(routingKey, marshaledRecord, options = {}) {
|
|
9
|
+
let putValue = false;
|
|
10
|
+
try {
|
|
11
|
+
for await (const event of this.dht.put(routingKey, marshaledRecord, options)) {
|
|
12
|
+
logEvent('DHT put event', event);
|
|
13
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:dht:query', event));
|
|
14
|
+
if (event.name === 'PEER_RESPONSE' && event.messageName === 'PUT_VALUE') {
|
|
15
|
+
putValue = true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:dht:error', err));
|
|
21
|
+
}
|
|
22
|
+
if (!putValue) {
|
|
23
|
+
throw new Error('Could not put value to DHT');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async get(routingKey, options = {}) {
|
|
27
|
+
try {
|
|
28
|
+
for await (const event of this.dht.get(routingKey, options)) {
|
|
29
|
+
logEvent('DHT get event', event);
|
|
30
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:dht:query', event));
|
|
31
|
+
if (event.name === 'VALUE') {
|
|
32
|
+
return event.value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:dht:error', err));
|
|
38
|
+
}
|
|
39
|
+
throw new Error('Not found');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function logEvent(prefix, event) {
|
|
43
|
+
if (event.name === 'SENDING_QUERY') {
|
|
44
|
+
log(prefix, event.name, event.messageName, '->', event.to.toString());
|
|
45
|
+
}
|
|
46
|
+
else if (event.name === 'PEER_RESPONSE') {
|
|
47
|
+
log(prefix, event.name, event.messageName, '<-', event.from.toString());
|
|
48
|
+
}
|
|
49
|
+
else if (event.name === 'FINAL_PEER') {
|
|
50
|
+
log(prefix, event.name, event.peer.id.toString());
|
|
51
|
+
}
|
|
52
|
+
else if (event.name === 'QUERY_ERROR') {
|
|
53
|
+
log(prefix, event.name, event.error.message);
|
|
54
|
+
}
|
|
55
|
+
else if (event.name === 'PROVIDER') {
|
|
56
|
+
log(prefix, event.name, event.providers.map(p => p.id.toString()).join(', '));
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
log(prefix, event.name);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function dht(components) {
|
|
63
|
+
return new DHTRouting(components);
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=dht.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dht.js","sourceRoot":"","sources":["../../../src/routing/dht.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAIvC,OAAO,EAAE,mBAAmB,EAAiB,MAAM,iBAAiB,CAAA;AAEpE,MAAM,GAAG,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAA;AAY5C,MAAM,OAAO,UAAU;IAGrB,YAAa,UAAgC;QAC3C,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,GAAG,CAAA;IAClC,CAAC;IAED,KAAK,CAAC,GAAG,CAAE,UAAsB,EAAE,eAA2B,EAAE,UAAsB,EAAE;QACtF,IAAI,QAAQ,GAAG,KAAK,CAAA;QAEpB,IAAI;YACF,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE;gBAC5E,QAAQ,CAAC,eAAe,EAAE,KAAK,CAAC,CAAA;gBAEhC,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAa,wBAAwB,EAAE,KAAK,CAAC,CAAC,CAAA;gBAE1F,IAAI,KAAK,CAA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eAAe,EAAE;QACzC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,WAAW,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAA;KACxE;SAAM,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE;QACtC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAA;KAClD;SAAM,IAAI,KAAK,CAAC,IAAI,KAAK,aAAa,EAAE;QACvC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;KAC7C;SAAM,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,EAAE;QACpC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;KAC9E;SAAM;QACL,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,CAAA;KACxB;AACH,CAAC;AAED,MAAM,UAAU,GAAG,CAAE,UAAgC;IACnD,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,CAAA;AACnC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ProgressOptions } from 'progress-events';
|
|
2
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
3
|
+
import type { DHTProgressEvents } from './dht.js';
|
|
4
|
+
import type { DatastoreProgressEvents } from './local-store.js';
|
|
5
|
+
import type { PubSubProgressEvents } from './pubsub.js';
|
|
6
|
+
export interface PutOptions extends AbortOptions, ProgressOptions {
|
|
7
|
+
}
|
|
8
|
+
export interface GetOptions extends AbortOptions, ProgressOptions {
|
|
9
|
+
}
|
|
10
|
+
export interface IPNSRouting {
|
|
11
|
+
put: (routingKey: Uint8Array, marshaledRecord: Uint8Array, options?: PutOptions) => Promise<void>;
|
|
12
|
+
get: (routingKey: Uint8Array, options?: GetOptions) => Promise<Uint8Array>;
|
|
13
|
+
}
|
|
14
|
+
export type IPNSRoutingEvents = DatastoreProgressEvents | DHTProgressEvents | PubSubProgressEvents;
|
|
15
|
+
export { dht } from './dht.js';
|
|
16
|
+
export { pubsub } from './pubsub.js';
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/routing/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AACjD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAC/D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAEvD,MAAM,WAAW,UAAW,SAAQ,YAAY,EAAE,eAAe;CAEhE;AAED,MAAM,WAAW,UAAW,SAAQ,YAAY,EAAE,eAAe;CAEhE;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACjG,GAAG,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,OAAO,CAAC,UAAU,CAAC,CAAA;CAC3E;AAED,MAAM,MAAM,iBAAiB,GAC3B,uBAAuB,GACvB,iBAAiB,GACjB,oBAAoB,CAAA;AAEtB,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/routing/index.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ProgressEvent } from 'progress-events';
|
|
2
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
3
|
+
import { Datastore } from 'interface-datastore';
|
|
4
|
+
import type { IPNSRouting } from '../routing';
|
|
5
|
+
export type DatastoreProgressEvents = ProgressEvent<'ipns:routing:datastore:put'> | ProgressEvent<'ipns:routing:datastore:get'> | ProgressEvent<'ipns:routing:datastore:error', Error>;
|
|
6
|
+
export interface LocalStore extends IPNSRouting {
|
|
7
|
+
has: (routingKey: Uint8Array, options?: AbortOptions) => Promise<boolean>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns an IPNSRouting implementation that reads/writes IPNS records to the
|
|
11
|
+
* datastore as DHT records. This lets us publish IPNS records offline then
|
|
12
|
+
* serve them to the network later in response to DHT queries.
|
|
13
|
+
*/
|
|
14
|
+
export declare function localStore(datastore: Datastore): LocalStore;
|
|
15
|
+
//# sourceMappingURL=local-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-store.d.ts","sourceRoot":"","sources":["../../../src/routing/local-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEtD,OAAO,EAAE,SAAS,EAAO,MAAM,qBAAqB,CAAA;AAEpD,OAAO,KAAK,EAAc,WAAW,EAAc,MAAM,YAAY,CAAA;AAMrE,MAAM,MAAM,uBAAuB,GACjC,aAAa,CAAC,4BAA4B,CAAC,GAC3C,aAAa,CAAC,4BAA4B,CAAC,GAC3C,aAAa,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAA;AAEtD,MAAM,WAAW,UAAW,SAAQ,WAAW;IAC7C,GAAG,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CAC1E;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAE,SAAS,EAAE,SAAS,GAAG,UAAU,CAqC5D"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { CustomProgressEvent } from 'progress-events';
|
|
2
|
+
import { Libp2pRecord } from '@libp2p/record';
|
|
3
|
+
import { Key } from 'interface-datastore';
|
|
4
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
5
|
+
function dhtRoutingKey(key) {
|
|
6
|
+
return new Key('/dht/record/' + uint8ArrayToString(key, 'base32'), false);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Returns an IPNSRouting implementation that reads/writes IPNS records to the
|
|
10
|
+
* datastore as DHT records. This lets us publish IPNS records offline then
|
|
11
|
+
* serve them to the network later in response to DHT queries.
|
|
12
|
+
*/
|
|
13
|
+
export function localStore(datastore) {
|
|
14
|
+
return {
|
|
15
|
+
async put(routingKey, marshaledRecord, options = {}) {
|
|
16
|
+
try {
|
|
17
|
+
const key = dhtRoutingKey(routingKey);
|
|
18
|
+
// Marshal to libp2p record as the DHT does
|
|
19
|
+
const record = new Libp2pRecord(routingKey, marshaledRecord, new Date());
|
|
20
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:datastore:put'));
|
|
21
|
+
await datastore.put(key, record.serialize(), options);
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:datastore:error', err));
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
async get(routingKey, options = {}) {
|
|
29
|
+
try {
|
|
30
|
+
const key = dhtRoutingKey(routingKey);
|
|
31
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:datastore:get'));
|
|
32
|
+
const buf = await datastore.get(key, options);
|
|
33
|
+
// Unmarshal libp2p record as the DHT does
|
|
34
|
+
const record = Libp2pRecord.deserialize(buf);
|
|
35
|
+
return record.value;
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
options.onProgress?.(new CustomProgressEvent('ipns:routing:datastore:error', err));
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
async has(routingKey, options = {}) {
|
|
43
|
+
const key = dhtRoutingKey(routingKey);
|
|
44
|
+
return await datastore.has(key, options);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=local-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-store.js","sourceRoot":"","sources":["../../../src/routing/local-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAiB,MAAM,iBAAiB,CAAA;AAEpE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAa,GAAG,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAGtE,SAAS,aAAa,CAAE,GAAe;IACrC,OAAO,IAAI,GAAG,CAAC,cAAc,GAAG,kBAAkB,CAAC,GAAG,EAAE,QAAQ,CAAC,EAAE,KAAK,CAAC,CAAA;AAC3E,CAAC;AAWD;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAE,SAAoB;IAC9C,OAAO;QACL,KAAK,CAAC,GAAG,CAAE,UAAsB,EAAE,eAA2B,EAAE,UAAsB,EAAE;YACtF,IAAI;gBACF,MAAM,GAAG,GAAG,aAAa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AAG,EAAE,OAAO,CAAC,CAAA;QAC1C,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
2
|
+
import type { PublishResult, PubSub } from '@libp2p/interface-pubsub';
|
|
3
|
+
import type { Datastore } from 'interface-datastore';
|
|
4
|
+
import type { IPNSRouting } from './index.js';
|
|
5
|
+
import { ProgressEvent } from 'progress-events';
|
|
6
|
+
export interface PubsubRoutingComponents {
|
|
7
|
+
datastore: Datastore;
|
|
8
|
+
libp2p: {
|
|
9
|
+
peerId: PeerId;
|
|
10
|
+
pubsub: PubSub;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export type PubSubProgressEvents = ProgressEvent<'ipns:pubsub:publish', {
|
|
14
|
+
topic: string;
|
|
15
|
+
result: PublishResult;
|
|
16
|
+
}> | ProgressEvent<'ipns:pubsub:subscribe', {
|
|
17
|
+
topic: string;
|
|
18
|
+
}> | ProgressEvent<'ipns:pubsub:error', Error>;
|
|
19
|
+
export declare function pubsub(components: PubsubRoutingComponents): IPNSRouting;
|
|
20
|
+
//# sourceMappingURL=pubsub.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pubsub.d.ts","sourceRoot":"","sources":["../../../src/routing/pubsub.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAW,aAAa,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAC9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAc,WAAW,EAAc,MAAM,YAAY,CAAA;AAMrE,OAAO,EAAuB,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAIpE,MAAM,WAAW,uBAAuB;IACtC,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE;QACN,MAAM,EAAE,MAAM,CAAA;QACd,MAAM,EAAE,MAAM,CAAA;KACf,CAAA;CACF;AAED,MAAM,MAAM,oBAAoB,GAC9B,aAAa,CAAC,qBAAqB,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,aAAa,CAAA;CAAE,CAAC,GAC9E,aAAa,CAAC,uBAAuB,EAAE;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,GACzD,aAAa,CAAC,mBAAmB,EAAE,KAAK,CAAC,CAAA;AAoK3C,wBAAgB,MAAM,CAAE,UAAU,EAAE,uBAAuB,GAAG,WAAW,CAExE"}
|