-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
60 lines (51 loc) · 1.71 KB
/
index.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
55
56
57
58
59
60
import TurtleDB from "turtledb";
import { getFile, putFile, encryptContent, decryptContent } from "blockstack";
const IPFS = require("ipfs");
const node = new IPFS();
export async function newFile(options) {
const mydb = new TurtleDB("graphite-docs");
let storedData;
//if encrypt = true, encrypt data
if(options.encrypt) {
//if optional public key is passed, encrypt content with that key
if(options.key) {
storedData = encryptContent(options.data, {publicKey: options.key})
} else {
//if no optional key passed, encrypt with the blockstack app-specific key
storedData = encryptContent(options.data)
}
} else {
//if encrypt = false, store just the data
storedData = options.data;
}
//Since we are focusing on offline-first, we always post to indexeddb using turtledb
mydb.create({ _id: options.filename, data: JSON.stringify(storedData) }).then(() => {
mydb.read(options.filename).then((doc) => console.log(doc));
})
//To prevent too many http request, the sync boolean controls if and when we sync to gaia/ipfs
if(options.sync) {
//if ipfs = true, post to ipfs
if(options.ipfs) {
return node.files.add({
path: options.filename,
content: Buffer.from(JSON.stringify(storedData))
}).then((res) => {
putFile(options.filename, JSON.stringify(storedData), {encrypt: false})
return res;
})
} else {
//if ipfs = false or is missing, only post to gaia
putFile(options.filename, JSON.stringify(storedData), {encrypt: false})
}
}
}
export async function loadFile(options) {
if(options.ipfs) {
} else {
}
}
export async function updateFile(options) {
if(options.ipfs) {
} else {
}
}