Compare commits

..

5 Commits

Author SHA1 Message Date
Your Name
808e848561 Switch to CF KV instead of D1, more appropriate for the purpose 2024-07-24 01:47:20 -04:00
Your Name
491a1a3a4f Add migration script 2023-03-15 20:13:34 -04:00
Your Name
da3aad8713 Error response 2023-03-15 19:45:52 -04:00
Your Name
9e6397f038 Add DB dump support 2023-03-15 19:42:52 -04:00
Your Name
bdda2bcb8d Add basic D1 support 2023-03-15 19:27:25 -04:00
10 changed files with 9284 additions and 1 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
cryptopad.db
node_modules
.wrangler

1
d1setup.sql Normal file
View File

@@ -0,0 +1 @@
CREATE TABLE data (k BLOB, v BLOB, PRIMARY KEY (`k`));

23
functions/dump/[dump].js Normal file
View File

@@ -0,0 +1,23 @@
export async function onRequest(context) {
if (context.params.dump !== context.env.DUMP_KEY) {
return new Response("Wrong key");
}
const db = context.env.cryptopad;
console.log(db)
let accum = []
let cursor = undefined;
do {
const listResult = await db.list({cursor});
cursor = listResult.cursor;
accum = accum.concat(listResult.keys.map(x => x.name));
} while (cursor !== undefined);
const results = await Promise.all(accum.map(async x => ({key: x, value: await db.get(x)})));
return new Response(JSON.stringify(results), {
status: 200,
headers: {
'Content-Type': 'application/octet-stream'
}
});
}

View File

@@ -0,0 +1,22 @@
export async function onRequest(context) {
const db = context.env.cryptopad;
if (context.params.storage[0] == "delete") {
const deleteReq = context.params.storage[1];
await db.delete(deleteReq);
return new Response();
}
const key = context.params.storage[0];
console.log(context);
if (context.request.method == "POST") {
const formData = await context.request.formData();
const value = formData.get("value");
console.log("Inserting", key, value);
// this doesn't use numbered parameters because miniflare behaves differently because better-sqlite3 doesn't handled numbered params well
// CF really needs named support and better-sqlite3 really needs to fix this
await db.put(key, value);
return new Response();
}
const value = await db.get(key);
return new Response(JSON.stringify(value));
}

16
migrate.py Normal file
View File

@@ -0,0 +1,16 @@
import anydbm
import sqlite3
data = anydbm.open("cryptopad.db", 'c');
con = sqlite3.connect("my.db")
con.text_factory = str
con.execute("CREATE TABLE data (k BLOB, v BLOB, PRIMARY KEY (`k`))")
cur = con.cursor()
for k,v in data.iteritems():
print k
cur.execute("INSERT INTO data VALUES (?,?)", [k,v])
con.commit()

9191
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

14
package.json Normal file
View File

@@ -0,0 +1,14 @@
{
"name": "cryptopad",
"version": "0.0.0",
"devDependencies": {
"better-sqlite3": "^8.0.1",
"jest": "^29.5.0",
"wrangler": "3.66.0"
},
"private": true,
"scripts": {
"start": "wrangler pages dev static --d1=D1DB --persist",
"deploy": "wrangler pages publish static --project-name cryptopad"
}
}

View File

@@ -29,7 +29,7 @@ def mystatic(filepath):
@app.route('/')
def index():
return bottle.static_file("cryptopad.html", root='static')
return bottle.static_file("index.html", root='static')
if __name__ == "__main__":
bottle.debug(True)

14
wrangler.toml Normal file
View File

@@ -0,0 +1,14 @@
name = "cryptopad"
compatibility_date = "2023-03-15"
[vars]
DUMP_KEY = "dump"
[[ d1_databases ]]
binding = "D1DB"
database_name = "cryptopad"
database_id = "17080b9e-80c5-4253-8e7c-451ece72ed42"
[[kv_namespaces]]
binding = "cryptopad"
id = "f3899275342e429987dc34d37100b5b5"