This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathprotobuf.js
More file actions
120 lines (98 loc) · 3.56 KB
/
Copy pathprotobuf.js
File metadata and controls
120 lines (98 loc) · 3.56 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
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
var fs = require('fs');
var path = require('path');
var protobuf = require('pomelo-protobuf');
var Constants = require('../util/constants');
var crypto = require('crypto');
var logger = require('pomelo-logger').getLogger('pomelo', __filename);
module.exports = function(app, opts) {
return new Component(app, opts);
};
var Component = function(app, opts) {
this.app = app;
opts = opts || {};
this.watchers = {};
this.serverProtos = {};
this.clientProtos = {};
this.version = "";
var env = app.get(Constants.RESERVED.ENV);
var originServerPath = path.join(app.getBase(), Constants.FILEPATH.SERVER_PROTOS);
var presentServerPath = path.join(Constants.FILEPATH.CONFIG_DIR, env, path.basename(Constants.FILEPATH.SERVER_PROTOS));
var originClientPath = path.join(app.getBase(), Constants.FILEPATH.CLIENT_PROTOS);
var presentClientPath = path.join(Constants.FILEPATH.CONFIG_DIR, env, path.basename(Constants.FILEPATH.CLIENT_PROTOS));
this.serverProtosPath = opts.serverProtos || (fs.existsSync(originServerPath) ? Constants.FILEPATH.SERVER_PROTOS : presentServerPath);
this.clientProtosPath = opts.clientProtos || (fs.existsSync(originClientPath) ? Constants.FILEPATH.CLIENT_PROTOS : presentClientPath);
this.setProtos(Constants.RESERVED.SERVER, path.join(app.getBase(), this.serverProtosPath));
this.setProtos(Constants.RESERVED.CLIENT, path.join(app.getBase(), this.clientProtosPath));
protobuf.init({encoderProtos:this.serverProtos, decoderProtos:this.clientProtos});
};
var pro = Component.prototype;
pro.name = '__protobuf__';
pro.encode = function(key, msg) {
return protobuf.encode(key, msg);
};
pro.encode2Bytes = function(key, msg) {
return protobuf.encode2Bytes(key, msg);
};
pro.decode = function(key, msg) {
return protobuf.decode(key, msg);
};
pro.getProtos = function() {
return {
server : this.serverProtos,
client : this.clientProtos,
version : this.version
};
};
pro.getVersion = function() {
return this.version;
};
pro.setProtos = function(type, path) {
if(!fs.existsSync(path)) {
return;
}
if(type === Constants.RESERVED.SERVER) {
this.serverProtos = protobuf.parse(require(path));
}
if(type === Constants.RESERVED.CLIENT) {
this.clientProtos = protobuf.parse(require(path));
}
var protoStr = JSON.stringify(this.clientProtos) + JSON.stringify(this.serverProtos);
this.version = crypto.createHash('md5').update(protoStr).digest('base64');
//Watch file
var watcher = fs.watch(path, this.onUpdate.bind(this, type, path));
if (this.watchers[type]) {
this.watchers[type].close();
}
this.watchers[type] = watcher;
};
pro.onUpdate = function(type, path, event) {
if(event !== 'change') {
return;
}
var self = this;
fs.readFile(path, 'utf8' ,function(err, data) {
try {
var protos = protobuf.parse(JSON.parse(data));
if(type === Constants.RESERVED.SERVER) {
protobuf.setEncoderProtos(protos);
self.serverProtos = protos;
} else {
protobuf.setDecoderProtos(protos);
self.clientProtos = protos;
}
var protoStr = JSON.stringify(self.clientProtos) + JSON.stringify(self.serverProtos);
self.version = crypto.createHash('md5').update(protoStr).digest('base64');
logger.info('change proto file , type : %j, path : %j, version : %j', type, path, self.version);
} catch(e) {
logger.warn("change proto file error! path : %j", path);
logger.warn(e);
}
});
};
pro.stop = function(force, cb) {
for (var type in this.watchers) {
this.watchers[type].close();
}
this.watchers = {};
process.nextTick(cb);
};