-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadapter.ts
45 lines (34 loc) · 1.28 KB
/
adapter.ts
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
import type Address from '../../ad4m/Address'
import type Expression from '../../ad4m/Expression'
import type { ExpressionAdapter, PublicSharing } from '../../ad4m/Language'
import type LanguageContext from '../../ad4m/LanguageContext'
import type { IPFSNode } from '../../ad4m/LanguageContext'
import { IpfsPutAdapter } from './putAdapter'
const _appendBuffer = (buffer1, buffer2) => {
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
const uint8ArrayConcat = (chunks) => {
return chunks.reduce(_appendBuffer)
}
export default class Adapter implements ExpressionAdapter {
#IPFS: IPFSNode
putAdapter: PublicSharing
constructor(context: LanguageContext) {
this.#IPFS = context.IPFS
this.putAdapter = new IpfsPutAdapter(context)
}
async get(address: Address): Promise<void | Expression> {
const cid = address.toString()
const chunks = []
// @ts-ignore
for await (const chunk of this.#IPFS.cat(cid)) {
chunks.push(chunk)
}
const fileString = uint8ArrayConcat(chunks).toString();
const fileJson = JSON.parse(fileString)
return fileJson
}
}