@screeps/storage 5.1.0 → 5.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm.yml +41 -0
- package/LICENSE.txt +9 -9
- package/bin/start.js +3 -3
- package/db.original.json +1 -1
- package/lib/db.js +776 -776
- package/lib/index.js +57 -57
- package/lib/pubsub.js +36 -36
- package/lib/queue.js +108 -108
- package/package.json +19 -19
package/lib/index.js
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
var q = require('q'),
|
|
2
|
-
_ = require('lodash'),
|
|
3
|
-
net = require('net'),
|
|
4
|
-
EventEmitter = require('events').EventEmitter,
|
|
5
|
-
common = require('@screeps/common'),
|
|
6
|
-
{ RpcServer } = common.rpc;
|
|
7
|
-
config = Object.assign(common.configManager.config, {storage: new EventEmitter()}),
|
|
8
|
-
databaseMethods = require('./db'),
|
|
9
|
-
pubsub = require('./pubsub'),
|
|
10
|
-
queueMethods = require('./queue');
|
|
11
|
-
|
|
12
|
-
Object.assign(config.storage, {
|
|
13
|
-
socketListener(socket) {
|
|
14
|
-
var connectionDesc = `${socket.remoteAddress}:${socket.remotePort}`;
|
|
15
|
-
|
|
16
|
-
console.log(`[${connectionDesc}] Incoming connection`);
|
|
17
|
-
|
|
18
|
-
socket.on('error', error => console.log(`[${connectionDesc}] Connection error: ${error.message}`));
|
|
19
|
-
|
|
20
|
-
var pubsubConnection = pubsub.create();
|
|
21
|
-
|
|
22
|
-
new RpcServer(socket, _.extend({}, databaseMethods, queueMethods, pubsubConnection.methods));
|
|
23
|
-
|
|
24
|
-
socket.on('close', () => {
|
|
25
|
-
pubsubConnection.close();
|
|
26
|
-
console.log(`[${connectionDesc}] Connection closed`);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
module.exports.start = function() {
|
|
32
|
-
|
|
33
|
-
if (!process.env.STORAGE_PORT) {
|
|
34
|
-
throw new Error('STORAGE_PORT environment variable is not set!');
|
|
35
|
-
}
|
|
36
|
-
if (!process.env.DB_PATH) {
|
|
37
|
-
throw new Error('DB_PATH environment variable is not set!');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
common.configManager.load();
|
|
41
|
-
|
|
42
|
-
config.storage.loadDb().then(() => {
|
|
43
|
-
|
|
44
|
-
console.log(`Starting storage server`);
|
|
45
|
-
|
|
46
|
-
var server = net.createServer(config.storage.socketListener);
|
|
47
|
-
|
|
48
|
-
server.on('listening', () => {
|
|
49
|
-
console.log('Storage listening on', process.env.STORAGE_PORT);
|
|
50
|
-
if(process.send) {
|
|
51
|
-
process.send('storageLaunched');
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
server.listen(process.env.STORAGE_PORT, process.env.STORAGE_HOST || 'localhost');
|
|
56
|
-
})
|
|
57
|
-
.catch(error => console.error(error));
|
|
1
|
+
var q = require('q'),
|
|
2
|
+
_ = require('lodash'),
|
|
3
|
+
net = require('net'),
|
|
4
|
+
EventEmitter = require('events').EventEmitter,
|
|
5
|
+
common = require('@screeps/common'),
|
|
6
|
+
{ RpcServer } = common.rpc;
|
|
7
|
+
config = Object.assign(common.configManager.config, {storage: new EventEmitter()}),
|
|
8
|
+
databaseMethods = require('./db'),
|
|
9
|
+
pubsub = require('./pubsub'),
|
|
10
|
+
queueMethods = require('./queue');
|
|
11
|
+
|
|
12
|
+
Object.assign(config.storage, {
|
|
13
|
+
socketListener(socket) {
|
|
14
|
+
var connectionDesc = `${socket.remoteAddress}:${socket.remotePort}`;
|
|
15
|
+
|
|
16
|
+
console.log(`[${connectionDesc}] Incoming connection`);
|
|
17
|
+
|
|
18
|
+
socket.on('error', error => console.log(`[${connectionDesc}] Connection error: ${error.message}`));
|
|
19
|
+
|
|
20
|
+
var pubsubConnection = pubsub.create();
|
|
21
|
+
|
|
22
|
+
new RpcServer(socket, _.extend({}, databaseMethods, queueMethods, pubsubConnection.methods));
|
|
23
|
+
|
|
24
|
+
socket.on('close', () => {
|
|
25
|
+
pubsubConnection.close();
|
|
26
|
+
console.log(`[${connectionDesc}] Connection closed`);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
module.exports.start = function() {
|
|
32
|
+
|
|
33
|
+
if (!process.env.STORAGE_PORT) {
|
|
34
|
+
throw new Error('STORAGE_PORT environment variable is not set!');
|
|
35
|
+
}
|
|
36
|
+
if (!process.env.DB_PATH) {
|
|
37
|
+
throw new Error('DB_PATH environment variable is not set!');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
common.configManager.load();
|
|
41
|
+
|
|
42
|
+
config.storage.loadDb().then(() => {
|
|
43
|
+
|
|
44
|
+
console.log(`Starting storage server`);
|
|
45
|
+
|
|
46
|
+
var server = net.createServer(config.storage.socketListener);
|
|
47
|
+
|
|
48
|
+
server.on('listening', () => {
|
|
49
|
+
console.log('Storage listening on', process.env.STORAGE_PORT);
|
|
50
|
+
if(process.send) {
|
|
51
|
+
process.send('storageLaunched');
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
server.listen(process.env.STORAGE_PORT, process.env.STORAGE_HOST || 'localhost');
|
|
56
|
+
})
|
|
57
|
+
.catch(error => console.error(error));
|
|
58
58
|
};
|
package/lib/pubsub.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
var q = require('q'),
|
|
2
|
-
_ = require('lodash'),
|
|
3
|
-
EventEmitter = require('events').EventEmitter;
|
|
4
|
-
|
|
5
|
-
var subs = {}, id = 0, emitter = new EventEmitter();
|
|
6
|
-
|
|
7
|
-
emitter.setMaxListeners(0);
|
|
8
|
-
|
|
9
|
-
function publish(channel, data, cb) {
|
|
10
|
-
emitter.emit(channel, {channel, data});
|
|
11
|
-
emitter.emit('*', {channel, data});
|
|
12
|
-
cb && cb(null);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
exports.create = function createPubsub() {
|
|
16
|
-
var connId = id++;
|
|
17
|
-
var connSubs = subs[connId] = [];
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
methods: {
|
|
21
|
-
publish,
|
|
22
|
-
subscribe(channel, listener) {
|
|
23
|
-
connSubs.push([channel, listener]);
|
|
24
|
-
emitter.on(channel, listener);
|
|
25
|
-
return () => {
|
|
26
|
-
emitter.removeListener(channel, listener);
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
close() {
|
|
31
|
-
connSubs.forEach(i => emitter.removeListener(i[0], i[1]));
|
|
32
|
-
delete subs[connId];
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
1
|
+
var q = require('q'),
|
|
2
|
+
_ = require('lodash'),
|
|
3
|
+
EventEmitter = require('events').EventEmitter;
|
|
4
|
+
|
|
5
|
+
var subs = {}, id = 0, emitter = new EventEmitter();
|
|
6
|
+
|
|
7
|
+
emitter.setMaxListeners(0);
|
|
8
|
+
|
|
9
|
+
function publish(channel, data, cb) {
|
|
10
|
+
emitter.emit(channel, {channel, data});
|
|
11
|
+
emitter.emit('*', {channel, data});
|
|
12
|
+
cb && cb(null);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
exports.create = function createPubsub() {
|
|
16
|
+
var connId = id++;
|
|
17
|
+
var connSubs = subs[connId] = [];
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
methods: {
|
|
21
|
+
publish,
|
|
22
|
+
subscribe(channel, listener) {
|
|
23
|
+
connSubs.push([channel, listener]);
|
|
24
|
+
emitter.on(channel, listener);
|
|
25
|
+
return () => {
|
|
26
|
+
emitter.removeListener(channel, listener);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
close() {
|
|
31
|
+
connSubs.forEach(i => emitter.removeListener(i[0], i[1]));
|
|
32
|
+
delete subs[connId];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
37
|
exports.publish = publish;
|
package/lib/queue.js
CHANGED
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
var q = require('q'),
|
|
2
|
-
_ = require('lodash'),
|
|
3
|
-
pubsub = require('./pubsub'),
|
|
4
|
-
EventEmitter = require('events').EventEmitter;
|
|
5
|
-
|
|
6
|
-
var queues = {
|
|
7
|
-
usersLegacy: {
|
|
8
|
-
pending: [],
|
|
9
|
-
processing: [],
|
|
10
|
-
emitter: new EventEmitter()
|
|
11
|
-
},
|
|
12
|
-
usersIvm: {
|
|
13
|
-
pending: [],
|
|
14
|
-
processing: [],
|
|
15
|
-
emitter: new EventEmitter()
|
|
16
|
-
},
|
|
17
|
-
rooms: {
|
|
18
|
-
pending: [],
|
|
19
|
-
processing: [],
|
|
20
|
-
emitter: new EventEmitter()
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
module.exports = {
|
|
25
|
-
queueFetch(name, cb) {
|
|
26
|
-
try {
|
|
27
|
-
var check = function () {
|
|
28
|
-
if (!queues[name].pending.length) {
|
|
29
|
-
queues[name].emitter.once('add', check);
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
var item = queues[name].pending.pop();
|
|
33
|
-
queues[name].processing.push(item);
|
|
34
|
-
cb(null, item);
|
|
35
|
-
};
|
|
36
|
-
check();
|
|
37
|
-
}
|
|
38
|
-
catch (e) {
|
|
39
|
-
cb(e.message);
|
|
40
|
-
console.error(e);
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
queueMarkDone(name, id, cb) {
|
|
44
|
-
try {
|
|
45
|
-
_.pull(queues[name].processing, id);
|
|
46
|
-
queues[name].emitter.emit('done');
|
|
47
|
-
cb && cb(null, true);
|
|
48
|
-
}
|
|
49
|
-
catch (e) {
|
|
50
|
-
cb(e.message);
|
|
51
|
-
console.error(e);
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
queueAdd(name, id, cb) {
|
|
55
|
-
try {
|
|
56
|
-
queues[name].pending.push(id);
|
|
57
|
-
queues[name].emitter.emit('add');
|
|
58
|
-
cb && cb(null, true);
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
cb(e.message);
|
|
62
|
-
console.error(e);
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
queueAddMulti(name, array, cb) {
|
|
66
|
-
try {
|
|
67
|
-
queues[name].pending = queues[name].pending.concat(array);
|
|
68
|
-
queues[name].emitter.emit('add');
|
|
69
|
-
cb && cb(null, true);
|
|
70
|
-
}
|
|
71
|
-
catch (e) {
|
|
72
|
-
cb(e.message);
|
|
73
|
-
console.error(e);
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
queueWhenAllDone(name, cb) {
|
|
77
|
-
try {
|
|
78
|
-
var check = function () {
|
|
79
|
-
if (queues[name].pending.length || queues[name].processing.length) {
|
|
80
|
-
queues[name].emitter.once('done', check);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
pubsub.publish('queueDone:' + name, '1');
|
|
84
|
-
cb(null, true);
|
|
85
|
-
};
|
|
86
|
-
check();
|
|
87
|
-
}
|
|
88
|
-
catch (e) {
|
|
89
|
-
cb(e.message);
|
|
90
|
-
console.error(e);
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
queueReset(name, cb) {
|
|
94
|
-
try {
|
|
95
|
-
queues[name].pending = [];
|
|
96
|
-
queues[name].processing = [];
|
|
97
|
-
queues[name].emitter.emit('done');
|
|
98
|
-
cb && cb(null, true);
|
|
99
|
-
}
|
|
100
|
-
catch (e) {
|
|
101
|
-
cb(e.message);
|
|
102
|
-
console.error(e);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
setInterval(() => {
|
|
108
|
-
//console.log(queues.users.processing, queues.users.pending);
|
|
1
|
+
var q = require('q'),
|
|
2
|
+
_ = require('lodash'),
|
|
3
|
+
pubsub = require('./pubsub'),
|
|
4
|
+
EventEmitter = require('events').EventEmitter;
|
|
5
|
+
|
|
6
|
+
var queues = {
|
|
7
|
+
usersLegacy: {
|
|
8
|
+
pending: [],
|
|
9
|
+
processing: [],
|
|
10
|
+
emitter: new EventEmitter()
|
|
11
|
+
},
|
|
12
|
+
usersIvm: {
|
|
13
|
+
pending: [],
|
|
14
|
+
processing: [],
|
|
15
|
+
emitter: new EventEmitter()
|
|
16
|
+
},
|
|
17
|
+
rooms: {
|
|
18
|
+
pending: [],
|
|
19
|
+
processing: [],
|
|
20
|
+
emitter: new EventEmitter()
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports = {
|
|
25
|
+
queueFetch(name, cb) {
|
|
26
|
+
try {
|
|
27
|
+
var check = function () {
|
|
28
|
+
if (!queues[name].pending.length) {
|
|
29
|
+
queues[name].emitter.once('add', check);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
var item = queues[name].pending.pop();
|
|
33
|
+
queues[name].processing.push(item);
|
|
34
|
+
cb(null, item);
|
|
35
|
+
};
|
|
36
|
+
check();
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
cb(e.message);
|
|
40
|
+
console.error(e);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
queueMarkDone(name, id, cb) {
|
|
44
|
+
try {
|
|
45
|
+
_.pull(queues[name].processing, id);
|
|
46
|
+
queues[name].emitter.emit('done');
|
|
47
|
+
cb && cb(null, true);
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
cb(e.message);
|
|
51
|
+
console.error(e);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
queueAdd(name, id, cb) {
|
|
55
|
+
try {
|
|
56
|
+
queues[name].pending.push(id);
|
|
57
|
+
queues[name].emitter.emit('add');
|
|
58
|
+
cb && cb(null, true);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
cb(e.message);
|
|
62
|
+
console.error(e);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
queueAddMulti(name, array, cb) {
|
|
66
|
+
try {
|
|
67
|
+
queues[name].pending = queues[name].pending.concat(array);
|
|
68
|
+
queues[name].emitter.emit('add');
|
|
69
|
+
cb && cb(null, true);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
cb(e.message);
|
|
73
|
+
console.error(e);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
queueWhenAllDone(name, cb) {
|
|
77
|
+
try {
|
|
78
|
+
var check = function () {
|
|
79
|
+
if (queues[name].pending.length || queues[name].processing.length) {
|
|
80
|
+
queues[name].emitter.once('done', check);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
pubsub.publish('queueDone:' + name, '1');
|
|
84
|
+
cb(null, true);
|
|
85
|
+
};
|
|
86
|
+
check();
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
cb(e.message);
|
|
90
|
+
console.error(e);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
queueReset(name, cb) {
|
|
94
|
+
try {
|
|
95
|
+
queues[name].pending = [];
|
|
96
|
+
queues[name].processing = [];
|
|
97
|
+
queues[name].emitter.emit('done');
|
|
98
|
+
cb && cb(null, true);
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
cb(e.message);
|
|
102
|
+
console.error(e);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
setInterval(() => {
|
|
108
|
+
//console.log(queues.users.processing, queues.users.pending);
|
|
109
109
|
}, 500);
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@screeps/storage",
|
|
3
|
-
"version": "5.1.
|
|
4
|
-
"main": "lib/index.js",
|
|
5
|
-
"bin": {
|
|
6
|
-
"screeps-storage": "bin/start.js"
|
|
7
|
-
},
|
|
8
|
-
"license": "ISC",
|
|
9
|
-
"author": "Artem Chivchalov <contact@screeps.com>",
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "https://github.com/screeps/storage.git"
|
|
13
|
-
},
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"lodash": "^3.10.1",
|
|
16
|
-
"lokijs": "^1.5.6",
|
|
17
|
-
"q": "^1.4.1"
|
|
18
|
-
}
|
|
19
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@screeps/storage",
|
|
3
|
+
"version": "5.1.2",
|
|
4
|
+
"main": "lib/index.js",
|
|
5
|
+
"bin": {
|
|
6
|
+
"screeps-storage": "bin/start.js"
|
|
7
|
+
},
|
|
8
|
+
"license": "ISC",
|
|
9
|
+
"author": "Artem Chivchalov <contact@screeps.com>",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/screeps/storage.git"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"lodash": "^3.10.1",
|
|
16
|
+
"lokijs": "^1.5.6",
|
|
17
|
+
"q": "^1.4.1"
|
|
18
|
+
}
|
|
19
|
+
}
|