import { err, warn, l, line } from "$lib/logging";
import { types } from "$lib/payments";
import { archive, arc2, db, g, ga, s } from "$lib/db";
import { wait } from "$lib/utils";
let queued = {};
let migrating = {};
async function migrate(id) {
try {
delete queued[id];
if (id) id = id.replace(/\s/g, "").toLowerCase();
let user = await g(`user:${id}`);
if (typeof user === "string") user = await g(`user:${user}`);
if (user) return user;
user = await ga(`user:${id}`);
if (typeof user === "string") {
if (await g(`user:${user}`)) {
warn("user already migrated", id);
s(`user:${id}`, user);
}
id = user;
user = await ga(`user:${id}`);
}
if (user) {
if (migrating[id]) return;
migrating[id] = true;
let { username } = user;
username = username.replace(/\s/g, "").toLowerCase();
l("migrating", username);
try {
let multi = await db
.multi()
.set(`user:${username}`, id)
.set(`user:${user.pubkey}`, id)
.set(`user:${id}`, JSON.stringify(user));
await multi.exec();
await archive.del(`user:${username}`);
await archive.del(`user:${user.pubkey}`);
await archive.del(`user:${id}`);
await s(`${id}:contacts`, (await ga(`${id}:contacts`)) || []);
await archive.del(`${id}:contacts`);
await s(
`credit:bitcoin:${id}`,
(await ga(`credit:bitcoin:${id}`)) || 0,
);
await archive.del(`credit:bitcoin:${id}`);
await s(
`credit:lightning:${id}`,
(await ga(`credit:lightning:${id}`)) || 0,
);
await archive.del(`credit:lightning:${id}`);
await s(`${id}:lastlen`, (await ga(`${id}:lastlen`)) || 0);
await archive.del(`${id}:lastlen`);
await s(`${id}:cindex`, (await ga(`${id}:cindex`)) || 0);
await archive.del(`${id}:cindex`);
await s(`balance:${id}`, (await ga(`balance:${id}`)) || 0);
await archive.del(`balance:${id}`);
await s(`pending:${id}`, (await ga(`pending:${id}`)) || 0);
await archive.del(`pending:${id}`);
let pid;
while ((pid = await archive.rPop(`${id}:payments`))) {
let p = await ga(`payment:${pid}`);
let pos = await db.lPos(`${id}:payments`, pid);
if (!p || pos) continue;
if (p.type === types.internal && p.ref) {
let recipient = await g(`user:${p.ref}`);
if (!recipient) queued[p.ref] = true;
}
await db.lPush(`${id}:payments`, pid);
await s(`payment:${pid}`, p);
await archive.del(`payment:${pid}`);
}
await archive.del(`${id}:payments`);
let iid;
while ((iid = await archive.rPop(`${id}:invoices`))) {
let inv = await ga(`invoice:${iid}`);
let pos = await db.lPos(`${id}:invoices`, iid);
if (!inv || pos) continue;
await db.lPush(`${id}:invoices`, iid);
await s(`invoice:${iid}`, inv);
await archive.del(`invoice:${iid}`);
}
await archive.del(`${id}:invoices`);
l("done migrating", username);
} catch (e) {
warn("failed to migrate", username, e.message, line());
}
delete migrating[id];
return user;
}
} catch (e) {
delete migrating[id];
throw e;
}
}
let keepMigrating = async () => {
try {
if (Object.keys(queued).length) {
let next = Object.keys(queued)[0];
await migrate(next);
}
} catch (e) {
err("problem migrating", e.message);
}
setTimeout(keepMigrating, 1000);
};
keepMigrating();
export default migrate;
lib/socket.js?h=HEAD&id=a0fc80a1bd391acccee31cd9c3a3cc0fad3d00c6'>treecommitdiffstats
|
blob: e85e8f151866823e1ab2dbaa047e93a4eeb42406 (
plain) (
blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
import { get } from 'svelte/store';
import { rate, user, token, invoices, newPayment } from '$lib/store';
import { success } from '$lib/utils';
let interval, socket;
export const auth = () => {
let t = get(token);
if (t) send('login', t);
};
export const send = (type, data) => {
socket?.readyState === 1 && socket.send(JSON.stringify({ type, data }));
};
export const messages = (data) => ({
rate() {
rate.set(data);
},
payment() {
if (data.invoice) invoices.set({ ...get(invoices), [data.invoice.uuid]: data.invoice });
newPayment.set(true);
if (data.amount > 0) {
success(`Received ${data.amount} sats!`);
} else {
success(`Sent ${-data.amount} sats!`);
}
},
connected: auth,
login() {
user.set(data);
}
});
const initialReconnectDelay = 1000;
const maxReconnectDelay = 16000;
let currentReconnectDelay = initialReconnectDelay;
export function connect() {
clearInterval(interval);
if (socket) return auth();
socket = new WebSocket('ws://localhost:3119/ws');
socket.addEventListener('open', onWebsocketOpen);
socket.addEventListener('close', onWebsocketClose);
socket.addEventListener('message', onWebsocketMessage);
interval = setInterval(() => send('heartbeat'), 5000);
}
export function close() {
if (socket) socket.close();
}
function onWebsocketMessage(msg) {
let { type, data } = JSON.parse(msg.data);
messages(data)[type] && messages(data)[type]();
}
function onWebsocketOpen() {
currentReconnectDelay = initialReconnectDelay;
}
function onWebsocketClose() {
socket = null;
setTimeout(() => {
reconnectToWebsocket();
}, currentReconnectDelay + Math.floor(Math.random() * 3000));
}
function reconnectToWebsocket() {
if (currentReconnectDelay < maxReconnectDelay) {
currentReconnectDelay *= 2;
}
connect();
}
|