@abtnode/router-provider 1.8.28 → 1.8.30

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
 
@@ -10,6 +10,7 @@ const getPort = require('get-port');
10
10
  const uniqBy = require('lodash/uniqBy');
11
11
  const isEmpty = require('lodash/isEmpty');
12
12
  const cloneDeep = require('lodash/cloneDeep');
13
+ const formatBackSlash = require('@abtnode/util/lib/format-back-slash');
13
14
  const {
14
15
  DOMAIN_FOR_DEFAULT_SITE,
15
16
  ROUTING_RULE_TYPES,
@@ -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(formatBackSlash(this.logDir)),
140
+ tmpDir: this.getRelativeConfigDir(formatBackSlash(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 ${formatBackSlash(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
@@ -9,6 +9,7 @@ const shelljs = require('shelljs');
9
9
  const findProcess = require('find-process');
10
10
  const moment = require('moment');
11
11
  const { MAX_UPLOAD_FILE_SIZE } = require('@abtnode/constant');
12
+ const formatBackSlash = require('@abtnode/util/lib/format-back-slash');
12
13
 
13
14
  const logger = require('@abtnode/logger')('router:nginx:util');
14
15
 
@@ -62,7 +63,7 @@ const addTestServer = ({ configPath, port, upstreamPort }) =>
62
63
  `
63
64
  );
64
65
 
65
- if (upstreamPort) {
66
+ if (upstreamPort && os.platform() !== 'win32') {
66
67
  conf.nginx._addVerbatimBlock(
67
68
  'stream',
68
69
  `
@@ -182,7 +183,6 @@ http {
182
183
  include includes/compression;
183
184
  }
184
185
  `;
185
-
186
186
  const getNginxStatus = async (configPath) => {
187
187
  const list = await findProcess('name', /nginx:/);
188
188
  const result = {
@@ -273,11 +273,16 @@ const getMissingModules = (configParams) => {
273
273
 
274
274
  const getUpstreamName = (port) => `server_${port}`;
275
275
 
276
+ const joinNginxPath = (...args) => {
277
+ const result = path.join(...args);
278
+ return formatBackSlash(result);
279
+ };
280
+
276
281
  module.exports = {
277
282
  addTestServer,
283
+ formatError,
278
284
  getMainTemplate,
279
285
  getNginxLoadModuleDirectives,
280
- formatError,
281
286
  getMissingModules,
282
287
  parseNginxConfigArgs,
283
288
  getNginxStatus,
@@ -285,4 +290,5 @@ module.exports = {
285
290
  getUserGroup,
286
291
  getWorkerConnectionCount,
287
292
  getUpstreamName,
293
+ joinNginxPath,
288
294
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.8.28",
3
+ "version": "1.8.30",
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.28",
36
- "@abtnode/logger": "1.8.28",
37
- "@abtnode/router-templates": "1.8.28",
38
- "@abtnode/util": "1.8.28",
35
+ "@abtnode/constant": "1.8.30",
36
+ "@abtnode/logger": "1.8.30",
37
+ "@abtnode/router-templates": "1.8.30",
38
+ "@abtnode/util": "1.8.30",
39
39
  "axios": "^0.27.2",
40
40
  "debug": "^4.3.4",
41
41
  "find-process": "^1.4.7",
@@ -62,5 +62,5 @@
62
62
  "fs-extra": "^10.1.0",
63
63
  "needle": "^3.1.0"
64
64
  },
65
- "gitHead": "af7aacf5507177600ae563ab597cb42d7223b113"
65
+ "gitHead": "cb294f4ee7382c6ef7692b6c2c33ad080fced13e"
66
66
  }