-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
152 lines (137 loc) · 4.27 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var levelup = require('levelup');
var leveldown = require('leveldown');
var encode = require('encoding-down');
var iconv = require('iconv-lite');
module.exports = class SteamCategories {
constructor(dbPath, steamid) {
this.db = null;
this.dbPath = dbPath;
this.steam3Id = steamid;
this.namespaceKeys = [];
this.collections = {};
this.collectionsFiltered = {};
this.keyPrefix = `_https://steamloopback.host\u0000\u0001U${this.steam3Id}-cloud-storage-namespace`;
}
async read() {
return new Promise((resolve, reject) => {
this.db = levelup(encode(leveldown(this.dbPath), { valueEncoding: 'hex' }),(err)=>{
if (err) return reject(err)
});
this.db.get(`${this.keyPrefix}s`, {valueEncoding: 'utf-8'}, (err, value) => {
if (err) return reject(err);
this.namespaceKeys = JSON.parse(value.slice(1)).map((x) => `${this.keyPrefix}-${x[0]}`);
const collections = {};
this.db.createReadStream({
keys: true,
values: true,
gte: `${this.keyPrefix}-`,
lte: `${this.keyPrefix}-~`
}).on('data', (data) => {
if (this.namespaceKeys.includes(data.key)) {
const id = data.key.replace(`${this.keyPrefix}-`, '');
if(['00','01'].indexOf(data.value.substr(0,2))<0){
reject('Illegal BOM')
}
try {
const unserialized = this.unserializeCollections(data.value);
collections[id] = unserialized;
} catch(err) {
reject(err)
}
}
}).on('end', () => {
this.collections = collections;
resolve(collections);
});
});
});
}
list() {
const names = [];
for (const [, collection] of Object.entries(this.collections)) {
for (const key of Object.entries(collection)) {
names.push(key[0].replace('user-collections.', ''));
}
}
return names;
}
remove(collectionId) {
for (const id in this.collections) {
if ((`user-collections.${collectionId}` in this.collections[id])) {
delete this.collections[id][`user-collections.${collectionId}`];
return true;
}
}
return false;
}
// Expose some leveldb methods
close() {
return this.db.close();
}
isClosed() {
return this.db.isClosed();
}
isOpen() {
return this.db.isOpen();
}
get(id) {
id = `user-collections.${id}`;
for (const [, x] of Object.entries(this.collections)) {
if (x[id]) {
return x[id];
}
}
return false;
}
add(id, values) {
const data = {
key: `user-collections.${id}`,
timestamp: Math.ceil(Date.now()/1000),
value: Object.assign({}, { id: `${id}` }, values),
conflictResolutionMethod: 'custom',
strMethodId: 'union-collections'
};
// add to last
this.collections[Object.keys(this.collections).slice(-1)[0]][`user-collections.${id}`] = data;
return data;
}
async save() {
return new Promise((resolve) => {
const promises = [];
for (const [key, collections] of Object.entries(this.collections)) {
const putPromise = new Promise((resolve, reject) => {
this.db.put(`${this.keyPrefix}-${key}`, this.serializeCollections(collections), (err) => {
if (err) reject(err);
resolve();
});
});
promises.push(putPromise);
}
Promise.all(promises).then(() => resolve());
});
}
serializeCollections(collections) {
const output = [];
Object.entries(collections).forEach((collection) => {
if (collection[1].value) {
collection[1].value = JSON.stringify(collection[1].value);
}
output.push(collection);
});
return `00${iconv.encode(JSON.stringify(output),'utf16le').toString('hex')}`;
}
unserializeCollections(input) {
let transformed = input.substr(0,2)==='01'?input.slice(2).match(/.{1,2}/g).join('00').concat('00'): input.slice(2);
let iBuf = Buffer.from(transformed,'hex');
let decoded = iconv.decode(iBuf,'utf16le');
const collections = JSON.parse(decoded);
const output = {};
collections.forEach((x) => {
if (x[1].value) {
x[1].value = JSON.parse(x[1].value);
}
output[x[0]] = x[1];
});
return output;
}
};