@abtnode/router-provider 1.8.27 → 1.8.29

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/lib/index.js CHANGED
@@ -9,12 +9,20 @@ const debug = require('debug')(`${require('../package.json').name}:provider:inde
9
9
  const Nginx = require('./nginx');
10
10
  const Default = require('./default');
11
11
 
12
- const providerMap = new Map([
13
- ['nginx', Nginx],
14
- ['default', Default],
15
- ]);
12
+ const providerMap = new Map([['default', Default]]);
16
13
 
17
- const getProviderNames = () => [...providerMap.keys()];
14
+ if (process.platform !== 'win32') {
15
+ providerMap.set('nginx', Nginx);
16
+ }
17
+
18
+ const getProviderNames = () =>
19
+ [...providerMap.keys()].sort((x) => {
20
+ if (x === 'nginx') {
21
+ return -1;
22
+ }
23
+
24
+ return 1;
25
+ });
18
26
 
19
27
  /**
20
28
  * Get provider by name
@@ -36,15 +44,16 @@ const listProviders = async (configDir) =>
36
44
  Promise.all(getProviderNames().map((x) => providerMap.get(x).describe({ configDir })));
37
45
 
38
46
  const findExistsProvider = () => {
39
- for (const [key, Provider] of providerMap.entries()) {
47
+ for (const name of getProviderNames()) {
40
48
  try {
49
+ const Provider = providerMap.get(name);
41
50
  const exists = Provider.exists();
42
51
  if (exists) {
43
- return key;
52
+ return name;
44
53
  }
45
54
  } catch (error) {
46
55
  debug(error);
47
- console.error(`provider ${key} exists check failed:`, error.message);
56
+ console.error(`provider ${name} exists check failed:`, error.message);
48
57
  }
49
58
  }
50
59
 
@@ -28,6 +28,7 @@ const BaseProvider = require('../base');
28
28
  const {
29
29
  addTestServer,
30
30
  formatError,
31
+ formatNginxPath,
31
32
  getNginxLoadModuleDirectives,
32
33
  parseNginxConfigArgs,
33
34
  getNginxStatus,
@@ -35,6 +36,7 @@ const {
35
36
  getMissingModules,
36
37
  getMainTemplate,
37
38
  getUpstreamName,
39
+ joinNginxPath,
38
40
  } = require('./util');
39
41
  const {
40
42
  decideHttpPort,
@@ -128,6 +130,19 @@ class NginxProvider extends BaseProvider {
128
130
  this.initialize();
129
131
  }
130
132
 
133
+ getRelativeConfigDir(dir) {
134
+ return path.relative(this.configDir, dir);
135
+ }
136
+
137
+ getConfTemplate() {
138
+ return getMainTemplate({
139
+ logDir: this.getRelativeConfigDir(formatNginxPath(this.logDir)),
140
+ tmpDir: this.getRelativeConfigDir(formatNginxPath(this.tmpDir)),
141
+ workerProcess: this.getWorkerProcess(),
142
+ nginxLoadModules: getNginxLoadModuleDirectives(REQUIRED_MODULES, this.readNginxConfigParams()).join(os.EOL),
143
+ });
144
+ }
145
+
131
146
  async update({
132
147
  routingTable = [],
133
148
  certificates = [],
@@ -144,12 +159,7 @@ class NginxProvider extends BaseProvider {
144
159
 
145
160
  // eslint-disable-next-line consistent-return
146
161
  return new Promise((resolve, reject) => {
147
- const confTemplate = getMainTemplate({
148
- logDir: this.logDir.replace(this.configDir, '').replace(/^\//, ''),
149
- tmpDir: this.tmpDir.replace(this.configDir, '').replace(/^\//, ''),
150
- workerProcess: this.getWorkerProcess(),
151
- nginxLoadModules: getNginxLoadModuleDirectives(REQUIRED_MODULES, this.readNginxConfigParams()).join(os.EOL),
152
- });
162
+ const confTemplate = this.getConfTemplate();
153
163
 
154
164
  NginxConfFile.createFromSource(confTemplate, (err, conf) => {
155
165
  if (err) {
@@ -180,7 +190,7 @@ class NginxProvider extends BaseProvider {
180
190
  this.addGlobalReqLimit(conf.nginx.http, requestLimit);
181
191
  }
182
192
 
183
- logger.info('routing sites:', sites);
193
+ logger.debug('routing sites:', sites);
184
194
 
185
195
  const allRules = sites.reduce((acc, site) => {
186
196
  acc.push(...(site.rules || []));
@@ -199,8 +209,8 @@ class NginxProvider extends BaseProvider {
199
209
  // HTTPS configurations
200
210
  // update all certs to disk
201
211
  certificates.forEach((item) => {
202
- const crtPath = `${path.join(this.certDir, item.domain)}.crt`;
203
- const keyPath = `${path.join(this.certDir, item.domain)}.key`;
212
+ const crtPath = `${path.join(this.certDir, item.domain.replace('*', '-'))}.crt`;
213
+ const keyPath = `${path.join(this.certDir, item.domain.replace('*', '-'))}.key`;
204
214
  fs.writeFileSync(crtPath, item.certificate);
205
215
  fs.writeFileSync(keyPath, item.privateKey);
206
216
  });
@@ -262,21 +272,14 @@ class NginxProvider extends BaseProvider {
262
272
 
263
273
  async stop() {
264
274
  logger.info('stop');
275
+
265
276
  return this._exec('stop');
266
277
  }
267
278
 
268
279
  // FIXME: 这个函数可以不暴露出去?
269
280
  initialize() {
270
281
  if (!fs.existsSync(this.configPath)) {
271
- fs.writeFileSync(
272
- this.configPath,
273
- getMainTemplate({
274
- logDir: this.logDir.replace(this.configDir, '').replace(/^\//, ''),
275
- tmpDir: this.tmpDir.replace(this.configDir, '').replace(/^\//, ''),
276
- nginxLoadModules: getNginxLoadModuleDirectives(REQUIRED_MODULES, this.readNginxConfigParams()).join(os.EOL),
277
- workerProcess: this.getWorkerProcess(),
278
- })
279
- );
282
+ fs.writeFileSync(this.configPath, this.getConfTemplate());
280
283
  }
281
284
  }
282
285
 
@@ -312,12 +315,13 @@ class NginxProvider extends BaseProvider {
312
315
  */
313
316
  _exec(param = '') {
314
317
  logger.info('exec', { param });
315
- let command = `${this.binPath} -c ${this.configPath} -p ${this.configDir}`; // eslint-disable-line no-param-reassign
318
+ let command = `${this.binPath} -c ${this.configPath} -p ${formatNginxPath(this.configDir)}`; // eslint-disable-line no-param-reassign
316
319
  if (param) {
317
320
  command = `${command} -s ${param}`;
318
321
  }
319
322
 
320
323
  logger.info('exec command:', { command });
324
+
321
325
  const result = shelljs.exec(command, { silent: true });
322
326
  if (result.code !== 0) {
323
327
  logger.error(`exec ${command} error`, { error: result.stderr });
@@ -614,7 +618,7 @@ class NginxProvider extends BaseProvider {
614
618
  throw new Error('daemonPort is required');
615
619
  }
616
620
 
617
- server._add('root', this.wwwDir.replace(this.configDir, '').replace(/^\//, ''));
621
+ server._add('root', this.getRelativeConfigDir(this.wwwDir));
618
622
  server._add('error_page', '404 =404 /_abtnode_404');
619
623
  server._add('error_page', '502 =502 /_abtnode_502');
620
624
  server._add('error_page', '500 502 503 504 =500 /_abtnode_5xx');
@@ -639,6 +643,7 @@ class NginxProvider extends BaseProvider {
639
643
 
640
644
  _addWwwFiles(nodeInfo) {
641
645
  const welcomePage = nodeInfo.enableWelcomePage ? getWelcomeTemplate(nodeInfo) : get404Template(nodeInfo);
646
+
642
647
  fs.writeFileSync(`${this.wwwDir}/index.html`, welcomePage); // disable index.html
643
648
  fs.writeFileSync(`${this.wwwDir}/404.html`, get404Template(nodeInfo));
644
649
  fs.writeFileSync(`${this.wwwDir}/502.html`, get502Template(nodeInfo));
@@ -665,7 +670,7 @@ class NginxProvider extends BaseProvider {
665
670
  }
666
671
 
667
672
  if (this.isTest || process.env.NODE_ENV === 'test') {
668
- fs.copySync(path.join(__dirname, 'includes/dhparam.pem'), targetFile, { overwrite: true });
673
+ fs.copySync(path.join(__dirname, 'includes', 'dhparam.pem'), targetFile, { overwrite: true });
669
674
  this._isDhparamGenerated = true;
670
675
  return;
671
676
  }
@@ -761,8 +766,8 @@ class NginxProvider extends BaseProvider {
761
766
  conf.nginx.http._add('server');
762
767
  const httpsServerUnit = this._getLastServer(conf);
763
768
 
764
- const crtPath = `${path.join(this.certDir, certificateFileName)}.crt`;
765
- const keyPath = `${path.join(this.certDir, certificateFileName)}.key`;
769
+ const crtPath = `${joinNginxPath(this.certDir, certificateFileName.replace('*', '-'))}.crt`;
770
+ const keyPath = `${joinNginxPath(this.certDir, certificateFileName.replace('*', '-'))}.key`;
766
771
 
767
772
  let listen = `${this.httpsPort} ssl ${this.isHttp2Supported ? 'http2' : ''}`.trim();
768
773
  if (serverName === '_') {
@@ -889,7 +894,7 @@ NginxProvider.check = async ({ configDir = '' } = {}) => {
889
894
  }
890
895
 
891
896
  if (nginxStatus.managed) {
892
- const pidFile = path.join(configDir, 'nginx/nginx.pid');
897
+ const pidFile = path.join(configDir, 'nginx', 'nginx.pid');
893
898
  if (fs.existsSync(pidFile)) {
894
899
  const diskPid = Number(fs.readFileSync(pidFile).toString().trim());
895
900
  // If we have the pid lock file, but not the same as running nginx pid, nginx should be killed
package/lib/nginx/util.js CHANGED
@@ -62,7 +62,7 @@ const addTestServer = ({ configPath, port, upstreamPort }) =>
62
62
  `
63
63
  );
64
64
 
65
- if (upstreamPort) {
65
+ if (upstreamPort && os.platform() !== 'win32') {
66
66
  conf.nginx._addVerbatimBlock(
67
67
  'stream',
68
68
  `
@@ -182,7 +182,6 @@ http {
182
182
  include includes/compression;
183
183
  }
184
184
  `;
185
-
186
185
  const getNginxStatus = async (configPath) => {
187
186
  const list = await findProcess('name', /nginx:/);
188
187
  const result = {
@@ -273,11 +272,19 @@ const getMissingModules = (configParams) => {
273
272
 
274
273
  const getUpstreamName = (port) => `server_${port}`;
275
274
 
275
+ const joinNginxPath = (...args) => {
276
+ const result = path.join(...args);
277
+ return result.replaceAll(path.sep, '/');
278
+ };
279
+
280
+ const formatNginxPath = (filepath) => filepath.replaceAll(path.sep, '/');
281
+
276
282
  module.exports = {
277
283
  addTestServer,
284
+ formatError,
285
+ formatNginxPath,
278
286
  getMainTemplate,
279
287
  getNginxLoadModuleDirectives,
280
- formatError,
281
288
  getMissingModules,
282
289
  parseNginxConfigArgs,
283
290
  getNginxStatus,
@@ -285,4 +292,5 @@ module.exports = {
285
292
  getUserGroup,
286
293
  getWorkerConnectionCount,
287
294
  getUpstreamName,
295
+ joinNginxPath,
288
296
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.8.27",
3
+ "version": "1.8.29",
4
4
  "description": "Routing engine implementations for abt node",
5
5
  "author": "polunzh <polunzh@gmail.com>",
6
6
  "homepage": "https://github.com/ArcBlock/blocklet-server#readme",
@@ -32,10 +32,10 @@
32
32
  "url": "https://github.com/ArcBlock/blocklet-server/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@abtnode/constant": "1.8.27",
36
- "@abtnode/logger": "1.8.27",
37
- "@abtnode/router-templates": "1.8.27",
38
- "@abtnode/util": "1.8.27",
35
+ "@abtnode/constant": "1.8.29",
36
+ "@abtnode/logger": "1.8.29",
37
+ "@abtnode/router-templates": "1.8.29",
38
+ "@abtnode/util": "1.8.29",
39
39
  "axios": "^0.27.2",
40
40
  "debug": "^4.3.4",
41
41
  "find-process": "^1.4.7",
@@ -45,7 +45,7 @@
45
45
  "http-proxy": "^1.18.1",
46
46
  "is-valid-domain": "^0.1.6",
47
47
  "lodash": "^4.17.21",
48
- "lru-cache": "^7.13.2",
48
+ "lru-cache": "^7.14.0",
49
49
  "moment": "^2.29.4",
50
50
  "nginx-conf": "^1.7.0",
51
51
  "object-hash": "^3.0.0",
@@ -62,5 +62,5 @@
62
62
  "fs-extra": "^10.1.0",
63
63
  "needle": "^3.1.0"
64
64
  },
65
- "gitHead": "84b2cb3ee703479f17e88c91650ecf3015cbf6af"
65
+ "gitHead": "ee52d838f0cf97e551d53a6509cb0da7e5f31978"
66
66
  }