forked from LIT-Protocol/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx-handler-server.mjs
117 lines (99 loc) · 3.02 KB
/
tx-handler-server.mjs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// This is not used anymore locally but still can be used if you want
// to run the tx-handler-server locally instead of using the one in the cloud
// 'https://lit-general-worker.herokuapp.com/';
import express from 'express';
import bodyParser from 'body-parser';
import request from 'supertest';
const txHandler = express();
txHandler.use(bodyParser.json());
// Create a queue to store incoming requests
const queue = [];
// Process the next item in the queue
async function processNext() {
if (queue.length > 0) {
const { req, res } = queue.shift();
// Process the request
try {
const result = await processRequest(req.body);
res.json({ success: true, result });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
}
}
// Function to process the request (simulate a long-running operation)
async function processRequest(data) {
return new Promise(async (resolve) => {
// wait for 1 second
await new Promise((resolve) => setTimeout(resolve, 1000));
resolve(`Processed: ${data.data}`);
});
}
// API endpoint
txHandler.post('/process', (req, res) => {
console.log('-----');
console.log('Start: ' + req.body.data);
// Add the request to the queue
queue.push({ req, res });
});
// API endpoint to wait until the queue is empty
txHandler.get('/wait-until-empty', async (req, res) => {
console.log('waiting...');
// Wait for the queue to become empty
await new Promise((resolve) => {
console.log('queue.length: ' + queue.length);
const interval = setInterval(() => {
if (queue.length <= 1) {
clearInterval(interval);
resolve();
}
}, 2000);
});
console.log('queue is empty');
// Send a response when the queue is empty
res.json({ success: true, message: 'The queue is empty.' });
});
// Resolve endpoint
txHandler.post('/resolve', (_req, res) => {
console.log('resolve: ' + _req.body.data);
processNext();
res.json({ success: true, message: 'Processing the next item in the queue' });
});
// create a 'status' endpoint to return when the server is up
txHandler.get('/status', (_req, res) => {
res.json({ success: true, message: 'The server is up and running.' });
});
async function waitTx(server, data = 'tx') {
return await request(server).post('/process').send({ data });
}
async function waitUntilEmpty(server) {
return await request(server).get('/wait-until-empty').send();
}
async function resolveTx(server) {
return await request(server).post('/resolve').send();
}
function useTxHandler(server) {
server = express();
server.use(bodyParser.json());
server.use(txHandler);
return server;
}
async function processTx(server, description, callback) {
waitTx(server, description);
await waitUntilEmpty(server);
const res = await callback();
await resolveTx(server);
return res;
}
export {
txHandler,
waitTx,
useTxHandler,
resolveTx,
waitUntilEmpty,
processTx,
};
// run the server at port 3031
txHandler.listen(3031, () =>
console.log('Tx handling server listening on port 3031')
);