-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathserialization.js
More file actions
38 lines (30 loc) · 849 Bytes
/
serialization.js
File metadata and controls
38 lines (30 loc) · 849 Bytes
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
'use strict';
/**
* The purpose of this module is to provide a serialization layer for passing arguments to a new Worker instance
* This allows us to completely separate the cluster worker from the cluster primary
*/
function circularReplacer() {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
}
}
function serialize(object) {
let data = encodeURIComponent(JSON.stringify(object, circularReplacer()));
let buff = new Buffer.from(data);
return buff.toString('base64');
}
function deserialize(string) {
let buff = new Buffer.from(string, 'base64');
return JSON.parse(decodeURIComponent(buff.toString('ascii')));
}
module.exports = {
serialize,
deserialize
};