aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-12-29 09:08:27 +0000
committerAdam Soltys <[email protected]>2023-12-29 09:08:35 +0000
commitf584d7a56762d19ac6c1f5eda87cfb26816d5d57 (patch)
treedca37600e33dd3319cbc5d7e70ab1c70bd14830d
parentcb38551cc4ec392911d18dda9b5a9fe97087ad61 (diff)
downloadcoinos-server-f584d7a56762d19ac6c1f5eda87cfb26816d5d57.tar.gz
coinos-server-f584d7a56762d19ac6c1f5eda87cfb26816d5d57.zip
migrate in more places
-rw-r--r--index.js4
-rw-r--r--lib/auth.js2
-rw-r--r--lib/migrate.js24
-rw-r--r--lib/register.js12
-rw-r--r--lib/sockets.js14
-rw-r--r--lib/utils.js2
-rw-r--r--routes/lnurl.js1
7 files changed, 43 insertions, 16 deletions
diff --git a/index.js b/index.js
index 43cdde73..6caf3432 100644
--- a/index.js
+++ b/index.js
@@ -107,5 +107,5 @@ let port = process.env.PORT || 3119;
app.listen({ host, port });
-// process.on("unhandledRejection", (e) => err("Unhandled error", e.message));
-// process.on("uncaughtException", (e) => err(e.message));
+process.on("unhandledRejection", (e) => err("Unhandled error", e.message));
+process.on("uncaughtException", (e) => err(e.message));
diff --git a/lib/auth.js b/lib/auth.js
index b17a1f2d..cb9a4f0d 100644
--- a/lib/auth.js
+++ b/lib/auth.js
@@ -40,7 +40,7 @@ export const jwtStrategy = new jwt.Strategy(
let { id } = payload;
user = await g(`user:${id}`);
if (typeof user === "string") user = await g(`user:${user}`);
- if (!user) migrate(id);
+ if (!user) user = migrate(id);
}
next(null, user);
diff --git a/lib/migrate.js b/lib/migrate.js
index b2c094e3..a523fb27 100644
--- a/lib/migrate.js
+++ b/lib/migrate.js
@@ -1,3 +1,4 @@
+import { err } from "$lib/logging";
import { types } from "$lib/payments";
import { archive, db, g } from "$lib/db";
@@ -19,11 +20,11 @@ async function migrate(id) {
.set(`${id}:contacts`, (await archive.get(`${id}:contacts`)) || "[]")
.set(
`credit:bitcoin:${id}`,
- (await archive.get(`credit:bitcoin:${id}`)) || 0,
+ (await archive.get(`credit:bitcoin:${id}`)) || 0
)
.set(
`credit:lightning:${id}`,
- (await archive.get(`credit:lightning:${id}`)) || 0,
+ (await archive.get(`credit:lightning:${id}`)) || 0
)
.set(`${id}:lastlen`, (await archive.get(`${id}:lastlen`)) || 0)
.set(`${id}:cindex`, (await archive.get(`${id}:cindex`)) || 0)
@@ -42,7 +43,7 @@ async function migrate(id) {
multi = await multi
.lPush(`${id}:payments`, pid)
.set(`payment:${pid}`, JSON.stringify(p));
- }),
+ })
);
let invoices = await archive.lRange(`${id}:invoices`, 0, -1);
@@ -52,16 +53,29 @@ async function migrate(id) {
multi = await multi
.lPush(`${id}:invoices`, iid)
.set(`invoice:${iid}`, inv);
- }),
+ })
);
await multi.exec();
delete q[id];
- await migrate(Object.keys(q)[0]);
return user;
}
}
+let keepMigrating = async () => {
+ try {
+ if (Object.keys(q).length) {
+ await migrate(Object.keys(q)[0]);
+ }
+ } catch (e) {
+ err("problem migrating", e.message);
+ }
+
+ setTimeout(keepMigrating, 100);
+};
+
+keepMigrating();
+
export default migrate;
diff --git a/lib/register.js b/lib/register.js
index 487413a8..a1489d89 100644
--- a/lib/register.js
+++ b/lib/register.js
@@ -4,7 +4,7 @@ import { authenticator } from "otplib";
import bcrypt from "bcrypt";
import { l, warn } from "$lib/logging";
import { v4 } from "uuid";
-import { s, g } from "$lib/db";
+import { s, g, archive } from "$lib/db";
import { got } from "got";
export default async (user, ip, requireChallenge) => {
@@ -17,7 +17,9 @@ export default async (user, ip, requireChallenge) => {
let id = v4();
user.id = id;
- let exists = await g(`user:${username}`);
+ let exists =
+ (await g(`user:${username}`)) || (await archive.get(`user:${username}`));
+
if (exists) throw new Error(`Username ${username} taken`);
if (password) {
@@ -28,9 +30,11 @@ export default async (user, ip, requireChallenge) => {
if (config.ipregistry) {
try {
let {
- location: { country: { code } },
+ location: {
+ country: { code },
+ },
} = await got(
- `https://api.ipregistry.co/${ip}?key=${config.ipregistry}&fields=location.country.code`,
+ `https://api.ipregistry.co/${ip}?key=${config.ipregistry}&fields=location.country.code`
).json();
user.currency = countries[code];
diff --git a/lib/sockets.js b/lib/sockets.js
index c1a704c9..44b3bf2a 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -1,4 +1,5 @@
import config from "$config";
+import migrate from "$lib/migrate";
import store from "$lib/store";
import jwt from "jsonwebtoken";
import { v4 } from "uuid";
@@ -52,7 +53,8 @@ let track = async (ws, token) => {
if (!uid) throw new Error("Invalid JWT token");
let user = await g(`user:${uid}`);
- if (!user) throw new Error("User not found");
+ if (!user) user = migrate(id);
+ if (!user) throw new Error(`User not found ${uid}`);
if (!store.sockets[uid]) store.sockets[uid] = {};
@@ -92,8 +94,12 @@ export async function socketServer(app) {
switch (type) {
case "heartbeat":
ws.beats = 0;
- if (data) await track(ws, data);
- ws.send(JSON.stringify({ type: "id", data: ws.id }));
+ try {
+ if (data) await track(ws, data);
+ ws.send(JSON.stringify({ type: "id", data: ws.id }));
+ } catch (e) {
+ // err("Failed to send heartbeat", e.message);
+ }
break;
case "login":
@@ -107,7 +113,7 @@ export async function socketServer(app) {
ws.send(JSON.stringify({ type: "logout" }));
setTimeout(
() => ws && ws.close(code, `closing due to error ${e.message}`),
- 1000,
+ 1000
);
}
break;
diff --git a/lib/utils.js b/lib/utils.js
index 0c382a0c..b7212639 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,3 +1,4 @@
+import migrate from "$lib/migrate";
import store from "$lib/store";
import { g } from "$lib/db";
import config from "$config";
@@ -60,5 +61,6 @@ export const fields = [
export const getUser = async (username) => {
username = username.replace(/\s/g, "").toLowerCase();
let uid = await g(`user:${username}`);
+ if (!uid) await migrate(username);
return g(`user:${uid}`);
};
diff --git a/routes/lnurl.js b/routes/lnurl.js
index a1e7dde8..79bac995 100644
--- a/routes/lnurl.js
+++ b/routes/lnurl.js
@@ -1,3 +1,4 @@
+import migrate from "$lib/migrate";
import { g, s } from "$lib/db";
import { l, warn } from "$lib/logging";
import { bail, getUser, fail } from "$lib/utils";