-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathprotobuf.ts
More file actions
157 lines (132 loc) · 4.66 KB
/
protobuf.ts
File metadata and controls
157 lines (132 loc) · 4.66 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
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
153
154
155
156
157
import * as fs from 'fs';
import * as path from 'path';
import { Protobuf} from 'pinus-protobuf';
import * as Constants from '../util/constants';
import * as crypto from 'crypto';
import { getLogger } from 'pinus-logger'; import { Application } from '../application';
import { IComponent } from '../interfaces/IComponent';
let logger = getLogger('pinus', __filename);
export interface ProtobufComponentOptions
{
serverProtos ?: string;
clientProtos ?: string;
}
export class ProtobufComponent implements IComponent
{
app: Application;
watchers : {[key:string]:fs.FSWatcher} = {};
serverProtos : {
[key: string]: any;
} = {};
clientProtos : {
[key: string]: any;
}= {};
version = "";
serverProtosPath: string;
clientProtosPath: string;
protobuf: Protobuf;
name = '__protobuf__';
constructor(app : Application, opts ?: ProtobufComponentOptions)
{
this.app = app;
opts = opts || {};
let env = app.get(Constants.RESERVED.ENV);
let originServerPath = path.join(app.getBase(), Constants.FILEPATH.SERVER_PROTOS);
let presentServerPath = path.join(Constants.FILEPATH.CONFIG_DIR, env, path.basename(Constants.FILEPATH.SERVER_PROTOS));
let originClientPath = path.join(app.getBase(), Constants.FILEPATH.CLIENT_PROTOS);
let 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));
this.protobuf = new Protobuf({ encoderProtos: this.serverProtos, decoderProtos: this.clientProtos });
};
encode(key : string, msg : any)
{
return this.protobuf.encode(key, msg);
};
encode2Bytes(key : string, msg : any)
{
return this.protobuf.encode2Bytes(key, msg);
};
decode(key : string, msg : any)
{
return this.protobuf.decode(key, msg);
};
getProtos()
{
return {
server: this.serverProtos,
client: this.clientProtos,
version: this.version
};
};
getVersion()
{
return this.version;
};
setProtos(type : string, path : string)
{
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));
}
let protoStr = JSON.stringify(this.clientProtos) + JSON.stringify(this.serverProtos);
this.version = crypto.createHash('md5').update(protoStr).digest('base64');
//Watch file
let watcher = fs.watch(path, this.onUpdate.bind(this, type, path));
if (this.watchers[type])
{
this.watchers[type].close();
}
this.watchers[type] = watcher;
};
onUpdate(type : string, path : string, event : string)
{
if (event !== 'change')
{
return;
}
let self = this;
fs.readFile(path, 'utf8', (err, data)=>
{
try
{
let protos = Protobuf.parse(JSON.parse(data));
if (type === Constants.RESERVED.SERVER)
{
this.protobuf.setEncoderProtos(protos);
self.serverProtos = protos;
} else
{
this.protobuf.setDecoderProtos(protos);
self.clientProtos = protos;
}
let 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);
}
});
};
stop(force: boolean, cb : ()=>void)
{
for (let type in this.watchers)
{
this.watchers[type].close();
}
this.watchers = {};
process.nextTick(cb);
};
}