@midwayjs/mock 3.12.0-beta.2 → 3.12.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013 - Now midwayjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/app.js ADDED
@@ -0,0 +1,33 @@
1
+ const { createApp, close } = require('./dist');
2
+ const { join } = require('path');
3
+
4
+ (async () => {
5
+ process.env.MIDWAY_TS_MODE = 'false';
6
+ // 查找 process.argv 中的 --port 参数
7
+ const portIndex = process.argv.findIndex((item) => item === '--port');
8
+ if (portIndex !== -1) {
9
+ process.env.MIDWAY_HTTP_PORT = process.argv[portIndex + 1];
10
+ }
11
+
12
+ process.once('SIGINT', onSignal);
13
+ // kill(3) Ctrl-\
14
+ process.once('SIGQUIT', onSignal);
15
+ // kill(15) default
16
+ process.once('SIGTERM', onSignal);
17
+
18
+ const app = await createApp({
19
+ appDir: process.cwd(),
20
+ baseDir: join(process.cwd(), 'dist'),
21
+ });
22
+
23
+ process.send({
24
+ title: 'server-ready',
25
+ port: process.env.MIDWAY_HTTP_PORT,
26
+ });
27
+
28
+ function onSignal() {
29
+ close(app).then(() => {
30
+ process.exit(0);
31
+ });
32
+ }
33
+ })();
package/dist/creator.js CHANGED
@@ -20,13 +20,17 @@ function formatPath(baseDir, p) {
20
20
  return (0, path_1.resolve)(baseDir, p);
21
21
  }
22
22
  }
23
+ function getFileNameWithSuffix(fileName) {
24
+ return (0, core_1.isTypeScriptEnvironment)() ? `${fileName}.ts` : `${fileName}.js`;
25
+ }
23
26
  async function create(appDir, options = {}, customFramework) {
24
- debug(`[mock]: Create app, appDir="${appDir}"`);
25
- process.env.MIDWAY_TS_MODE = 'true';
27
+ var _a;
28
+ process.env.MIDWAY_TS_MODE = (_a = process.env.MIDWAY_TS_MODE) !== null && _a !== void 0 ? _a : 'true';
26
29
  if (typeof appDir === 'object') {
27
30
  options = appDir;
28
31
  appDir = options.appDir || '';
29
32
  }
33
+ debug(`[mock]: Create app, appDir="${appDir}"`);
30
34
  try {
31
35
  if (appDir) {
32
36
  // 处理测试的 fixtures
@@ -71,19 +75,19 @@ async function create(appDir, options = {}, customFramework) {
71
75
  safeLoad: true,
72
76
  enableCache: false,
73
77
  });
74
- options.fileLoadMode = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
78
+ options.moduleLoadType = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
75
79
  }
76
80
  if (options.baseDir) {
77
- await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, 'interface.ts'), {
81
+ await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
78
82
  safeLoad: true,
79
- loadMode: options.fileLoadMode,
83
+ loadMode: options.moduleLoadType,
80
84
  });
81
85
  }
82
86
  else if (appDir) {
83
87
  options.baseDir = `${appDir}/src`;
84
- await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, 'interface.ts'), {
88
+ await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
85
89
  safeLoad: true,
86
- loadMode: options.fileLoadMode,
90
+ loadMode: options.moduleLoadType,
87
91
  });
88
92
  }
89
93
  if (!options.imports && customFramework) {
@@ -139,9 +143,9 @@ async function create(appDir, options = {}, customFramework) {
139
143
  appDir,
140
144
  asyncContextManager: (0, async_hooks_context_manager_1.createContextManager)(),
141
145
  imports: [].concat(options.imports).concat(options.baseDir
142
- ? await (0, core_1.loadModule)((0, path_1.join)(options.baseDir, 'configuration.ts'), {
146
+ ? await (0, core_1.loadModule)((0, path_1.join)(options.baseDir, getFileNameWithSuffix('configuration')), {
143
147
  safeLoad: true,
144
- loadMode: options.fileLoadMode,
148
+ loadMode: options.moduleLoadType,
145
149
  })
146
150
  : []),
147
151
  });
@@ -155,7 +159,7 @@ async function create(appDir, options = {}, customFramework) {
155
159
  return mainFramework;
156
160
  }
157
161
  else {
158
- throw new Error('Can not get main framework, please check your configuration.ts.');
162
+ throw new Error(`Can not get main framework, please check your ${getFileNameWithSuffix('configuration')}.`);
159
163
  }
160
164
  }
161
165
  }
@@ -206,7 +210,8 @@ async function close(app, options) {
206
210
  }
207
211
  exports.close = close;
208
212
  async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
209
- var _a, _b, _c, _d;
213
+ var _a, _b, _c, _d, _e;
214
+ process.env.MIDWAY_TS_MODE = (_a = process.env.MIDWAY_TS_MODE) !== null && _a !== void 0 ? _a : 'true';
210
215
  if (typeof baseDir === 'object') {
211
216
  options = baseDir;
212
217
  baseDir = options.appDir || '';
@@ -225,7 +230,7 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
225
230
  // load yaml
226
231
  try {
227
232
  const doc = yaml.load((0, fs_1.readFileSync)((0, path_1.join)(baseDir, 'f.yml'), 'utf8'));
228
- starterName = (_a = doc === null || doc === void 0 ? void 0 : doc['provider']) === null || _a === void 0 ? void 0 : _a['starter'];
233
+ starterName = (_b = doc === null || doc === void 0 ? void 0 : doc['provider']) === null || _b === void 0 ? void 0 : _b['starter'];
229
234
  if (starterName) {
230
235
  const m = await (0, core_1.loadModule)(starterName);
231
236
  if (m && m['BootstrapStarter']) {
@@ -241,7 +246,6 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
241
246
  if (options.starter) {
242
247
  options.appDir = baseDir;
243
248
  debug(`[mock]: Create app, appDir="${options.appDir}"`);
244
- process.env.MIDWAY_TS_MODE = 'true';
245
249
  if (options.appDir) {
246
250
  // 处理测试的 fixtures
247
251
  if (!(0, path_1.isAbsolute)(options.appDir)) {
@@ -256,19 +260,19 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
256
260
  safeLoad: true,
257
261
  enableCache: false,
258
262
  });
259
- options.fileLoadMode = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
263
+ options.moduleLoadType = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
260
264
  options = options || {};
261
265
  if (options.baseDir) {
262
- await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, 'interface.ts'), {
266
+ await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
263
267
  safeLoad: true,
264
- loadMode: options.fileLoadMode,
268
+ loadMode: options.moduleLoadType,
265
269
  });
266
270
  }
267
271
  else if (options.appDir) {
268
272
  options.baseDir = `${options.appDir}/src`;
269
- await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, 'interface.ts'), {
273
+ await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
270
274
  safeLoad: true,
271
- loadMode: options.fileLoadMode,
275
+ loadMode: options.moduleLoadType,
272
276
  });
273
277
  }
274
278
  // new mode
@@ -280,8 +284,8 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
280
284
  const framework = frameworkService.getMainFramework();
281
285
  const appManager = appCtx.get(core_1.MidwayApplicationManager);
282
286
  const app = appManager.getApplication(core_1.MidwayFrameworkType.FAAS);
283
- const faasConfig = (_b = configService.getConfiguration('faas')) !== null && _b !== void 0 ? _b : {};
284
- const customPort = (_d = (_c = process.env.MIDWAY_HTTP_PORT) !== null && _c !== void 0 ? _c : faasConfig['port']) !== null && _d !== void 0 ? _d : options['port'];
287
+ const faasConfig = (_c = configService.getConfiguration('faas')) !== null && _c !== void 0 ? _c : {};
288
+ const customPort = (_e = (_d = process.env.MIDWAY_HTTP_PORT) !== null && _d !== void 0 ? _d : faasConfig['port']) !== null && _e !== void 0 ? _e : options['port'];
285
289
  if (options.starter.callback2) {
286
290
  app.callback2 = options.starter.callback2.bind(options.starter);
287
291
  }
@@ -437,7 +441,7 @@ class BootstrapAppStarter {
437
441
  async close(options = {}) {
438
442
  // eslint-disable-next-line node/no-extraneous-require
439
443
  const BootstrapModule = await (0, core_1.loadModule)('@midwayjs/bootstrap', {
440
- loadMode: this.options.fileLoadMode,
444
+ loadMode: this.options.moduleLoadType,
441
445
  safeLoad: true,
442
446
  });
443
447
  if (BootstrapModule === null || BootstrapModule === void 0 ? void 0 : BootstrapModule.Bootstrap) {
@@ -473,7 +477,7 @@ async function createLightApp(baseDir = '', options = {}) {
473
477
  safeLoad: true,
474
478
  enableCache: false,
475
479
  });
476
- options.fileLoadMode = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
480
+ options.moduleLoadType = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
477
481
  }
478
482
  return createApp(baseDir, {
479
483
  ...options,
package/function.js ADDED
@@ -0,0 +1,33 @@
1
+ const { createFunctionApp, close } = require('./dist');
2
+ const { join } = require('path');
3
+
4
+ (async () => {
5
+ process.env.MIDWAY_TS_MODE = 'false';
6
+ // 查找 process.argv 中的 --port 参数
7
+ const portIndex = process.argv.findIndex(item => item === '--port');
8
+ if (portIndex !== -1) {
9
+ process.env.MIDWAY_HTTP_PORT = process.argv[portIndex + 1];
10
+ }
11
+
12
+ process.once('SIGINT', onSignal);
13
+ // kill(3) Ctrl-\
14
+ process.once('SIGQUIT', onSignal);
15
+ // kill(15) default
16
+ process.once('SIGTERM', onSignal);
17
+
18
+ const app = await createFunctionApp({
19
+ appDir: process.cwd(),
20
+ baseDir: join(process.cwd(), 'dist'),
21
+ });
22
+
23
+ process.send({
24
+ title: 'server-ready',
25
+ port: process.env.MIDWAY_HTTP_PORT,
26
+ });
27
+
28
+ function onSignal() {
29
+ close(app).then(() => {
30
+ process.exit(0);
31
+ });
32
+ }
33
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/mock",
3
- "version": "3.12.0-beta.2",
3
+ "version": "3.12.1",
4
4
  "description": "create your test app from midway framework",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -20,24 +20,26 @@
20
20
  "files": [
21
21
  "dist/**/*.js",
22
22
  "dist/**/*.d.ts",
23
- "ssl"
23
+ "ssl",
24
+ "app.js",
25
+ "function.js"
24
26
  ],
25
27
  "engines": {
26
28
  "node": ">=12"
27
29
  },
28
30
  "license": "MIT",
29
31
  "devDependencies": {
30
- "@midwayjs/core": "^3.11.15",
32
+ "@midwayjs/core": "^3.12.1",
31
33
  "@midwayjs/logger": "^2.15.0",
32
34
  "@types/amqplib": "0.10.1",
33
35
  "amqplib": "0.10.3",
34
36
  "kafkajs": "2.2.4",
35
- "socket.io": "4.7.1",
36
- "socket.io-client": "4.7.1",
37
+ "socket.io": "4.7.2",
38
+ "socket.io-client": "4.7.2",
37
39
  "ws": "8.13.0"
38
40
  },
39
41
  "dependencies": {
40
- "@midwayjs/async-hooks-context-manager": "^3.11.15",
42
+ "@midwayjs/async-hooks-context-manager": "^3.12.1",
41
43
  "@types/superagent": "4.1.14",
42
44
  "@types/supertest": "2.0.12",
43
45
  "js-yaml": "4.1.0",
@@ -49,5 +51,5 @@
49
51
  "type": "git",
50
52
  "url": "https://github.com/midwayjs/midway.git"
51
53
  },
52
- "gitHead": "a603d2348d6141f8f723901498f03a162a037708"
54
+ "gitHead": "48413b87b12354cb8207de6aa58e0a4ca22ea000"
53
55
  }