-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhelper.js
213 lines (145 loc) · 4.31 KB
/
helper.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const env = require("./env");
const defaults = require("./config/default");
const { isMaster, worker } = require("cluster");
const { readFile, writeFile, exists, existsSync } = require("fs");
const filename = "store/" + env + (isMaster ? "" : "@" + worker.id) + ".json";
let config_env;
const config = (function(){
if(existsSync("../../package.json")){
const config_package = require("../../package.json");
if(config_package.flexsearch){
if((config_package.flexsearch === "development") || (config_package.flexsearch === "production")){
config_env = require("./config/" + config_package.flexsearch);
}
else{
if(config_package.development || config_package.production){
if(config_package[env] && (config_package[env].indexOf(".json") !== -1) && existsSync("../../" + config_package[env])){
config_env = require("../../" + config_package[env]);
}
}
else{
config_env = config_package.flexsearch;
}
}
}
}
if(!config_env && existsSync("../../flexsearch.json")){
config_env = require("../../flexsearch.json");
}
if(!config_env){
config_env = require("./config/" + env);
}
[
"debug",
"port",
"port_ssl",
"force_ssl",
"https",
"compress",
"autosave",
"worker"
].forEach(function(flag){
const env_var = global.process.env[flag.toUpperCase()];
defaults[flag] = (
typeof env_var === "undefined" ?
config_env.server[flag]
:
env_var
);
});
/*
[
"host",
"port",
"pass"
].forEach(function(flag){
const env_var = global.process.env[flag.toUpperCase()];
defaults.redis[flag] = (
typeof env_var === "undefined" ?
config_env.redis[flag]
:
env_var
);
});
*/
[
"async",
"cache",
"threshold",
"depth",
"limit",
"encode",
"tokenize",
"filter",
"stemmer"
//"worker"
].forEach(function(flag){
const env_var = global.process.env[flag.toUpperCase()];
defaults[flag] = (
typeof env_var === "undefined" ?
config_env.client[flag]
:
env_var
);
});
return defaults;
})();
let timer = null;
let flexsearch;
let index_map;
module.exports = {
config: config,
init: function(_flexsearch, _index_map){
flexsearch = _flexsearch;
index_map = _index_map;
},
read_from_file: function(){
exists(filename, function(exists){
if(exists) readFile(filename, function(err, data){
if(err){
throw err;
}
if(data && data.length){
try{
flexsearch.import(data);
const index = flexsearch.index;
const keys = Object.keys(index);
for(let i = 0, len = keys.length; i < len; i++){
const key = keys[i];
index_map[key] = index[key];
}
if(config.debug){
console.info("Data was loaded successfully.");
}
}
catch(err){
throw err;
}
}
});
});
},
write_schedule: config.autosave || (config.autosave === 0) ? function(){
if(timer){
clearTimeout(timer);
}
timer = setTimeout(write_to_file, config.autosave);
} : write_to_file
};
function write_to_file(){
try{
const json = flexsearch.export();
json[2] = index_map;
writeFile(filename, json, function(err){
if(err){
throw err;
}
if(config.debug){
console.info("Data was saved successfully.");
}
});
}
catch(err){
throw err;
}
}