-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverWorker.js
More file actions
76 lines (62 loc) · 2.06 KB
/
serverWorker.js
File metadata and controls
76 lines (62 loc) · 2.06 KB
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
/* Session Server-Worker.
*
* @author Meliantchenkov Pavel
* @version 1.0
*/
const _ = require("./helper.js");
function _handleMessage(_CONF, session, data)
{
let id = data[0];
let type = data[1] || "";
let hash = data[2] || "";
let sess = data[3] || "";
let res = [id];
if(type === "new" && hash.length > 0)
{
res.push("new");
let expTime = Number.isInteger(data[3]) && data[3] > 0 ? data[3] : _CONF.expTime;
let keyLength = Number.isInteger(data[4]) && data[4] > 0 ? data[4] : _CONF.defaultKeyLength;
let base; if(Number.isInteger(data[5])) base = data[5];
if(keyLength > _CONF.largestKeyLength) keyLength = _CONF.largestKeyLength;
res.push({
key: _.getRandomString(keyLength, base),
hash: hash,
exp: Date.now() + expTime,
_exp: expTime,
_var: {}
});
}
else if((type === "check" || type === "close" || type === "set" || type === "get"))
{
res.push(type);
res.push(hash.length > 0 && sess.length > 0 && session.hash === hash && session.exp > Date.now());
}
else if(type === "ping") res.push("PING");
else
{
res.push("err");
res.push("incomprehensible request");
res.push(data);
}
return res;
}
typeof module !== "undefined" && (module.exports = {
handleMessage: _handleMessage
});
try // use nodejs >= v12 or '--experimental-worker' option in version >= v10.5
{
const WorkerThreads = require("worker_threads");
const parentPort = WorkerThreads.parentPort;
const workerData = WorkerThreads.workerData;
parentPort && parentPort.on('message', function(msg)
{
let id = msg.id;
let type = msg.type;
let args = msg.args;
let res;
if(type === "rndstr") res = _.getRandomString.apply(null, args);
else if(type === "handle") res = _handleMessage.apply(null, args);
parentPort.postMessage({i:workerData, id:id, res:res});
});
}
catch(e){ /* empty */ }