@midwayjs/mock 3.3.4 → 3.4.0-beta.2
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/dist/client/http.js +4 -1
- package/dist/creator.js +119 -16
- package/package.json +9 -7
package/dist/client/http.js
CHANGED
|
@@ -3,7 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createHttpRequest = void 0;
|
|
4
4
|
const request = require("supertest");
|
|
5
5
|
function createHttpRequest(app) {
|
|
6
|
-
if (app.
|
|
6
|
+
if (app.callback2) {
|
|
7
|
+
return request(app.callback2());
|
|
8
|
+
}
|
|
9
|
+
else if (app.callback) {
|
|
7
10
|
return request(app.callback());
|
|
8
11
|
}
|
|
9
12
|
else {
|
package/dist/creator.js
CHANGED
|
@@ -9,6 +9,9 @@ const logger_1 = require("@midwayjs/logger");
|
|
|
9
9
|
const utils_1 = require("./utils");
|
|
10
10
|
const util_1 = require("util");
|
|
11
11
|
const fs_1 = require("fs");
|
|
12
|
+
const http = require("http");
|
|
13
|
+
const yaml = require("js-yaml");
|
|
14
|
+
const getRawBody = require("raw-body");
|
|
12
15
|
const debug = (0, util_1.debuglog)('midway:debug');
|
|
13
16
|
process.setMaxListeners(0);
|
|
14
17
|
async function create(appDir = process.cwd(), options, customFramework) {
|
|
@@ -112,25 +115,125 @@ async function close(app, options) {
|
|
|
112
115
|
}
|
|
113
116
|
exports.close = close;
|
|
114
117
|
async function createFunctionApp(baseDir = process.cwd(), options, customFrameworkModule) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
118
|
+
var _a, _b, _c;
|
|
119
|
+
let starterName;
|
|
120
|
+
if (!options.starter) {
|
|
121
|
+
// load yaml
|
|
122
|
+
try {
|
|
123
|
+
const doc = yaml.load((0, fs_1.readFileSync)((0, path_1.join)(baseDir, 'f.yml'), 'utf8'));
|
|
124
|
+
starterName = (_a = doc === null || doc === void 0 ? void 0 : doc['provider']) === null || _a === void 0 ? void 0 : _a['starter'];
|
|
125
|
+
if (starterName) {
|
|
126
|
+
const m = (0, core_1.safeRequire)(starterName);
|
|
127
|
+
if (m && m['BootstrapStarter']) {
|
|
128
|
+
options.starter = new m['BootstrapStarter']();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
124
131
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
132
|
+
catch (e) {
|
|
133
|
+
// ignore
|
|
134
|
+
console.error('[mock]: get f.yml information fail, err = ' + e.stack);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (options.starter) {
|
|
138
|
+
options.exportAllHandler = true;
|
|
139
|
+
options.appDir = baseDir;
|
|
140
|
+
debug(`[mock]: Create app, appDir="${options.appDir}"`);
|
|
141
|
+
process.env.MIDWAY_TS_MODE = 'true';
|
|
142
|
+
// 处理测试的 fixtures
|
|
143
|
+
if (!(0, path_1.isAbsolute)(options.appDir)) {
|
|
144
|
+
options.appDir = (0, path_1.join)(process.cwd(), 'test', 'fixtures', options.appDir);
|
|
145
|
+
}
|
|
146
|
+
if (!(0, fs_1.existsSync)(options.appDir)) {
|
|
147
|
+
throw new core_1.MidwayCommonError(`Path "${options.appDir}" not exists, please check it.`);
|
|
148
|
+
}
|
|
149
|
+
(0, logger_1.clearAllLoggers)();
|
|
150
|
+
options = options || {};
|
|
151
|
+
if (options.baseDir) {
|
|
152
|
+
(0, core_1.safeRequire)((0, path_1.join)(`${options.baseDir}`, 'interface'));
|
|
153
|
+
}
|
|
154
|
+
else if (options.appDir) {
|
|
155
|
+
options.baseDir = `${options.appDir}/src`;
|
|
156
|
+
(0, core_1.safeRequire)((0, path_1.join)(`${options.baseDir}`, 'interface'));
|
|
157
|
+
}
|
|
158
|
+
// new mode
|
|
159
|
+
const exports = options.starter.start(options);
|
|
160
|
+
await exports[options.initializeMethodName || 'initializer']();
|
|
161
|
+
const appCtx = options.starter.getApplicationContext();
|
|
162
|
+
const configService = appCtx.get(core_1.MidwayConfigService);
|
|
163
|
+
const frameworkService = appCtx.get(core_1.MidwayFrameworkService);
|
|
164
|
+
const framework = frameworkService.getMainFramework();
|
|
165
|
+
const appManager = appCtx.get(core_1.MidwayApplicationManager);
|
|
166
|
+
const app = appManager.getApplication(core_1.MidwayFrameworkType.FAAS);
|
|
167
|
+
const faasConfig = (_b = configService.getConfiguration('faas')) !== null && _b !== void 0 ? _b : {};
|
|
168
|
+
const customPort = (_c = process.env.MIDWAY_HTTP_PORT) !== null && _c !== void 0 ? _c : faasConfig['port'];
|
|
169
|
+
app.callback2 = () => {
|
|
170
|
+
// mock a real http server response for local dev
|
|
171
|
+
return async (req, res) => {
|
|
172
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
173
|
+
req.query = Object.fromEntries(url.searchParams);
|
|
174
|
+
req.path = url.pathname;
|
|
175
|
+
// 如果需要解析body并且body是个stream,函数网关不会接受比 10m 更大的文件了
|
|
176
|
+
if (['post', 'put', 'delete'].indexOf(req.method.toLowerCase()) !== -1 &&
|
|
177
|
+
!req.body &&
|
|
178
|
+
typeof req.on === 'function') {
|
|
179
|
+
req.body = await getRawBody(req, {
|
|
180
|
+
limit: '10mb',
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
const ctx = await framework.wrapHttpRequest(req);
|
|
184
|
+
// create event and invoke
|
|
185
|
+
const func = framework.getTriggerFunction(url.pathname);
|
|
186
|
+
const result = await func(ctx, {
|
|
187
|
+
isHttpFunction: true,
|
|
188
|
+
originEvent: req,
|
|
189
|
+
originContext: {},
|
|
190
|
+
});
|
|
191
|
+
const { statusCode, headers, body } = result;
|
|
192
|
+
if (res.headersSent) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
for (const key in headers) {
|
|
196
|
+
res.setHeader(key, headers[key]);
|
|
197
|
+
}
|
|
198
|
+
if (res.statusCode !== statusCode) {
|
|
199
|
+
res.statusCode = statusCode;
|
|
200
|
+
}
|
|
201
|
+
// http trigger only support `Buffer` or a `string` or a `stream.Readable`
|
|
202
|
+
res.end(body);
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
if (customPort) {
|
|
206
|
+
await new Promise(resolve => {
|
|
207
|
+
const server = http.createServer(app.callback2());
|
|
208
|
+
server.listen(customPort);
|
|
209
|
+
process.env.MIDWAY_HTTP_PORT = String(customPort);
|
|
210
|
+
app.server = server;
|
|
211
|
+
resolve();
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return app;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
const customFramework = customFrameworkModule !== null && customFrameworkModule !== void 0 ? customFrameworkModule : (0, utils_1.findFirstExistModule)([
|
|
218
|
+
process.env.MIDWAY_SERVERLESS_APP_NAME,
|
|
219
|
+
'@ali/serverless-app',
|
|
220
|
+
'@midwayjs/serverless-app',
|
|
221
|
+
]);
|
|
222
|
+
const serverlessModule = (0, utils_1.transformFrameworkToConfiguration)(customFramework);
|
|
223
|
+
if (serverlessModule) {
|
|
224
|
+
if (options && options.imports) {
|
|
225
|
+
options.imports.unshift(serverlessModule);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
options = options || {};
|
|
229
|
+
options.imports = [serverlessModule];
|
|
230
|
+
}
|
|
128
231
|
}
|
|
232
|
+
const framework = await createApp(baseDir, options);
|
|
233
|
+
const appCtx = framework.getApplicationContext();
|
|
234
|
+
const appManager = appCtx.get(core_1.MidwayApplicationManager);
|
|
235
|
+
return appManager.getApplication(core_1.MidwayFrameworkType.SERVERLESS_APP);
|
|
129
236
|
}
|
|
130
|
-
const framework = await createApp(baseDir, options);
|
|
131
|
-
const appCtx = framework.getApplicationContext();
|
|
132
|
-
const appManager = appCtx.get(core_1.MidwayApplicationManager);
|
|
133
|
-
return appManager.getApplication(core_1.MidwayFrameworkType.SERVERLESS_APP);
|
|
134
237
|
}
|
|
135
238
|
exports.createFunctionApp = createFunctionApp;
|
|
136
239
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/mock",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0-beta.2",
|
|
4
4
|
"description": "create your test app from midway framework",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -26,24 +26,26 @@
|
|
|
26
26
|
},
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@midwayjs/core": "^3.
|
|
30
|
-
"@midwayjs/decorator": "^3.
|
|
29
|
+
"@midwayjs/core": "^3.4.0-beta.2",
|
|
30
|
+
"@midwayjs/decorator": "^3.4.0-beta.2",
|
|
31
31
|
"@midwayjs/logger": "^2.15.0",
|
|
32
32
|
"@types/amqplib": "0.8.2",
|
|
33
|
-
"amqplib": "0.
|
|
33
|
+
"amqplib": "0.10.0",
|
|
34
34
|
"socket.io": "4.4.1",
|
|
35
35
|
"socket.io-client": "4.4.1",
|
|
36
36
|
"ws": "8.5.0"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@types/supertest": "2.0.
|
|
39
|
+
"@types/supertest": "2.0.12",
|
|
40
40
|
"fs-extra": "10.0.1",
|
|
41
|
-
"
|
|
41
|
+
"js-yaml": "4.1.0",
|
|
42
|
+
"raw-body": "2.5.1",
|
|
43
|
+
"supertest": "6.2.3"
|
|
42
44
|
},
|
|
43
45
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
44
46
|
"repository": {
|
|
45
47
|
"type": "git",
|
|
46
48
|
"url": "http://github.com/midwayjs/midway.git"
|
|
47
49
|
},
|
|
48
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "a61721d3946b30fd4cac183265edcd99b31ac72b"
|
|
49
51
|
}
|