@eggjs/mock 6.0.0-beta.3
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 +21 -0
- package/README.md +547 -0
- package/README.zh_CN.md +512 -0
- package/dist/commonjs/app/extend/agent.d.ts +33 -0
- package/dist/commonjs/app/extend/agent.js +49 -0
- package/dist/commonjs/app/extend/application.d.ts +175 -0
- package/dist/commonjs/app/extend/application.js +448 -0
- package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +3 -0
- package/dist/commonjs/app/middleware/cluster_app_mock.js +100 -0
- package/dist/commonjs/app.d.ts +6 -0
- package/dist/commonjs/app.js +20 -0
- package/dist/commonjs/bootstrap.d.ts +4 -0
- package/dist/commonjs/bootstrap.js +58 -0
- package/dist/commonjs/index.d.ts +76 -0
- package/dist/commonjs/index.js +99 -0
- package/dist/commonjs/index.test-d.d.ts +1 -0
- package/dist/commonjs/index.test-d.js +43 -0
- package/dist/commonjs/lib/agent_handler.d.ts +3 -0
- package/dist/commonjs/lib/agent_handler.js +28 -0
- package/dist/commonjs/lib/app.d.ts +28 -0
- package/dist/commonjs/lib/app.js +303 -0
- package/dist/commonjs/lib/app_handler.d.ts +5 -0
- package/dist/commonjs/lib/app_handler.js +67 -0
- package/dist/commonjs/lib/cluster.d.ts +114 -0
- package/dist/commonjs/lib/cluster.js +337 -0
- package/dist/commonjs/lib/context.d.ts +1 -0
- package/dist/commonjs/lib/context.js +16 -0
- package/dist/commonjs/lib/format_options.d.ts +5 -0
- package/dist/commonjs/lib/format_options.js +100 -0
- package/dist/commonjs/lib/inject_context.d.ts +6 -0
- package/dist/commonjs/lib/inject_context.js +132 -0
- package/dist/commonjs/lib/mock_agent.d.ts +5 -0
- package/dist/commonjs/lib/mock_agent.js +49 -0
- package/dist/commonjs/lib/mock_custom_loader.d.ts +1 -0
- package/dist/commonjs/lib/mock_custom_loader.js +37 -0
- package/dist/commonjs/lib/mock_http_server.d.ts +2 -0
- package/dist/commonjs/lib/mock_http_server.js +24 -0
- package/dist/commonjs/lib/mock_httpclient.d.ts +35 -0
- package/dist/commonjs/lib/mock_httpclient.js +147 -0
- package/dist/commonjs/lib/parallel/agent.d.ts +20 -0
- package/dist/commonjs/lib/parallel/agent.js +125 -0
- package/dist/commonjs/lib/parallel/app.d.ts +20 -0
- package/dist/commonjs/lib/parallel/app.js +115 -0
- package/dist/commonjs/lib/parallel/util.d.ts +3 -0
- package/dist/commonjs/lib/parallel/util.js +77 -0
- package/dist/commonjs/lib/prerequire.d.ts +1 -0
- package/dist/commonjs/lib/prerequire.js +26 -0
- package/dist/commonjs/lib/request_call_function.d.ts +1 -0
- package/dist/commonjs/lib/request_call_function.js +52 -0
- package/dist/commonjs/lib/restore.d.ts +1 -0
- package/dist/commonjs/lib/restore.js +16 -0
- package/dist/commonjs/lib/start-cluster.d.ts +2 -0
- package/dist/commonjs/lib/start-cluster.js +23 -0
- package/dist/commonjs/lib/supertest.d.ts +11 -0
- package/dist/commonjs/lib/supertest.js +48 -0
- package/dist/commonjs/lib/tmp/empty.d.ts +1 -0
- package/dist/commonjs/lib/tmp/empty.js +3 -0
- package/dist/commonjs/lib/types.d.ts +60 -0
- package/dist/commonjs/lib/types.js +3 -0
- package/dist/commonjs/lib/utils.d.ts +9 -0
- package/dist/commonjs/lib/utils.js +80 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/register.d.ts +8 -0
- package/dist/commonjs/register.js +80 -0
- package/dist/esm/app/extend/agent.d.ts +33 -0
- package/dist/esm/app/extend/agent.js +46 -0
- package/dist/esm/app/extend/application.d.ts +175 -0
- package/dist/esm/app/extend/application.js +442 -0
- package/dist/esm/app/middleware/cluster_app_mock.d.ts +3 -0
- package/dist/esm/app/middleware/cluster_app_mock.js +98 -0
- package/dist/esm/app.d.ts +6 -0
- package/dist/esm/app.js +17 -0
- package/dist/esm/bootstrap.d.ts +4 -0
- package/dist/esm/bootstrap.js +16 -0
- package/dist/esm/index.d.ts +76 -0
- package/dist/esm/index.js +90 -0
- package/dist/esm/index.test-d.d.ts +1 -0
- package/dist/esm/index.test-d.js +42 -0
- package/dist/esm/lib/agent_handler.d.ts +3 -0
- package/dist/esm/lib/agent_handler.js +24 -0
- package/dist/esm/lib/app.d.ts +28 -0
- package/dist/esm/lib/app.js +295 -0
- package/dist/esm/lib/app_handler.d.ts +5 -0
- package/dist/esm/lib/app_handler.js +61 -0
- package/dist/esm/lib/cluster.d.ts +114 -0
- package/dist/esm/lib/cluster.js +328 -0
- package/dist/esm/lib/context.d.ts +1 -0
- package/dist/esm/lib/context.js +13 -0
- package/dist/esm/lib/format_options.d.ts +5 -0
- package/dist/esm/lib/format_options.js +94 -0
- package/dist/esm/lib/inject_context.d.ts +6 -0
- package/dist/esm/lib/inject_context.js +126 -0
- package/dist/esm/lib/mock_agent.d.ts +5 -0
- package/dist/esm/lib/mock_agent.js +45 -0
- package/dist/esm/lib/mock_custom_loader.d.ts +1 -0
- package/dist/esm/lib/mock_custom_loader.js +34 -0
- package/dist/esm/lib/mock_http_server.d.ts +2 -0
- package/dist/esm/lib/mock_http_server.js +18 -0
- package/dist/esm/lib/mock_httpclient.d.ts +35 -0
- package/dist/esm/lib/mock_httpclient.js +144 -0
- package/dist/esm/lib/parallel/agent.d.ts +20 -0
- package/dist/esm/lib/parallel/agent.js +117 -0
- package/dist/esm/lib/parallel/app.d.ts +20 -0
- package/dist/esm/lib/parallel/app.js +110 -0
- package/dist/esm/lib/parallel/util.d.ts +3 -0
- package/dist/esm/lib/parallel/util.js +73 -0
- package/dist/esm/lib/prerequire.d.ts +1 -0
- package/dist/esm/lib/prerequire.js +25 -0
- package/dist/esm/lib/request_call_function.d.ts +1 -0
- package/dist/esm/lib/request_call_function.js +47 -0
- package/dist/esm/lib/restore.d.ts +1 -0
- package/dist/esm/lib/restore.js +13 -0
- package/dist/esm/lib/start-cluster.d.ts +2 -0
- package/dist/esm/lib/start-cluster.js +18 -0
- package/dist/esm/lib/supertest.d.ts +11 -0
- package/dist/esm/lib/supertest.js +40 -0
- package/dist/esm/lib/tmp/empty.d.ts +1 -0
- package/dist/esm/lib/tmp/empty.js +2 -0
- package/dist/esm/lib/types.d.ts +60 -0
- package/dist/esm/lib/types.js +2 -0
- package/dist/esm/lib/utils.d.ts +9 -0
- package/dist/esm/lib/utils.js +69 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/register.d.ts +8 -0
- package/dist/esm/register.js +75 -0
- package/dist/package.json +4 -0
- package/package.json +131 -0
- package/src/app/extend/agent.ts +56 -0
- package/src/app/extend/application.ts +512 -0
- package/src/app/middleware/cluster_app_mock.ts +101 -0
- package/src/app.ts +18 -0
- package/src/bootstrap.ts +25 -0
- package/src/index.d.ts +193 -0
- package/src/index.test-d.ts +47 -0
- package/src/index.ts +110 -0
- package/src/lib/agent_handler.ts +28 -0
- package/src/lib/app.ts +313 -0
- package/src/lib/app_handler.ts +69 -0
- package/src/lib/cluster.ts +363 -0
- package/src/lib/context.ts +14 -0
- package/src/lib/format_options.ts +103 -0
- package/src/lib/inject_context.ts +134 -0
- package/src/lib/mock_agent.ts +57 -0
- package/src/lib/mock_custom_loader.ts +36 -0
- package/src/lib/mock_http_server.ts +19 -0
- package/src/lib/mock_httpclient.ts +181 -0
- package/src/lib/parallel/agent.ts +128 -0
- package/src/lib/parallel/app.ts +123 -0
- package/src/lib/parallel/util.ts +66 -0
- package/src/lib/prerequire.ts +25 -0
- package/src/lib/request_call_function.ts +49 -0
- package/src/lib/restore.ts +14 -0
- package/src/lib/start-cluster.ts +23 -0
- package/src/lib/supertest.ts +45 -0
- package/src/lib/tmp/.gitkeep +0 -0
- package/src/lib/tmp/empty.ts +0 -0
- package/src/lib/types.ts +72 -0
- package/src/lib/utils.ts +82 -0
- package/src/register.ts +80 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { debuglog } from 'node:util';
|
|
2
|
+
import {
|
|
3
|
+
MockAgent, setGlobalDispatcher, getGlobalDispatcher, Dispatcher,
|
|
4
|
+
HttpClient,
|
|
5
|
+
} from 'urllib';
|
|
6
|
+
|
|
7
|
+
const debug = debuglog('@eggjs/mock/lib/mock_agent');
|
|
8
|
+
|
|
9
|
+
declare namespace globalThis {
|
|
10
|
+
let __mockAgent: MockAgent | null;
|
|
11
|
+
let __globalDispatcher: Dispatcher;
|
|
12
|
+
let __httpClientDispatchers: Map<HttpClient, Dispatcher>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
globalThis.__mockAgent = null;
|
|
16
|
+
globalThis.__httpClientDispatchers = new Map<HttpClient, Dispatcher>();
|
|
17
|
+
|
|
18
|
+
export function getMockAgent(app?: { httpClient?: HttpClient }) {
|
|
19
|
+
debug('getMockAgent');
|
|
20
|
+
if (!globalThis.__globalDispatcher) {
|
|
21
|
+
globalThis.__globalDispatcher = getGlobalDispatcher();
|
|
22
|
+
debug('create global dispatcher');
|
|
23
|
+
}
|
|
24
|
+
if (app?.httpClient && !globalThis.__httpClientDispatchers.has(app.httpClient)) {
|
|
25
|
+
globalThis.__httpClientDispatchers.set(app.httpClient, app.httpClient.getDispatcher());
|
|
26
|
+
debug('add new httpClient, size: %d', globalThis.__httpClientDispatchers.size);
|
|
27
|
+
}
|
|
28
|
+
if (!globalThis.__mockAgent) {
|
|
29
|
+
globalThis.__mockAgent = new MockAgent();
|
|
30
|
+
setGlobalDispatcher(globalThis.__mockAgent);
|
|
31
|
+
if (typeof app?.httpClient?.setDispatcher === 'function') {
|
|
32
|
+
app.httpClient.setDispatcher(globalThis.__mockAgent);
|
|
33
|
+
}
|
|
34
|
+
debug('create new mockAgent');
|
|
35
|
+
}
|
|
36
|
+
return globalThis.__mockAgent;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function restoreMockAgent() {
|
|
40
|
+
debug('restoreMockAgent start');
|
|
41
|
+
if (globalThis.__globalDispatcher) {
|
|
42
|
+
setGlobalDispatcher(globalThis.__globalDispatcher);
|
|
43
|
+
debug('restore global dispatcher');
|
|
44
|
+
}
|
|
45
|
+
debug('restore httpClient, size: %d', globalThis.__httpClientDispatchers.size);
|
|
46
|
+
for (const [ httpClient, dispatcher ] of globalThis.__httpClientDispatchers) {
|
|
47
|
+
httpClient.setDispatcher(dispatcher);
|
|
48
|
+
}
|
|
49
|
+
globalThis.__httpClientDispatchers.clear();
|
|
50
|
+
if (globalThis.__mockAgent) {
|
|
51
|
+
const agent = globalThis.__mockAgent;
|
|
52
|
+
globalThis.__mockAgent = null;
|
|
53
|
+
await agent.close();
|
|
54
|
+
debug('close mockAgent');
|
|
55
|
+
}
|
|
56
|
+
debug('restoreMockAgent end');
|
|
57
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { debuglog } from 'node:util';
|
|
2
|
+
|
|
3
|
+
const debug = debuglog('@eggjs/mock/lib/mock_custom_loader');
|
|
4
|
+
|
|
5
|
+
export function setCustomLoader(app: any) {
|
|
6
|
+
const customLoader = app.config.customLoader;
|
|
7
|
+
if (!customLoader) return;
|
|
8
|
+
|
|
9
|
+
for (const field of Object.keys(customLoader)) {
|
|
10
|
+
const loaderConfig = Object.assign({}, customLoader[field]);
|
|
11
|
+
loaderConfig.field = field;
|
|
12
|
+
addMethod(loaderConfig);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function addMethod(loaderConfig: any) {
|
|
16
|
+
const field = loaderConfig.field as string;
|
|
17
|
+
const appMethodName = 'mock' + field.replace(/^[a-z]/i, s => s.toUpperCase());
|
|
18
|
+
if (app[appMethodName]) {
|
|
19
|
+
app.coreLogger.warn('Can\'t override app.%s', appMethodName);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
debug('[addMethod] %s => %j', appMethodName, loaderConfig);
|
|
23
|
+
app[appMethodName] = function(service: any, methodName: string, fn: any) {
|
|
24
|
+
if (typeof service === 'string') {
|
|
25
|
+
const arr = service.split('.');
|
|
26
|
+
service = loaderConfig.inject === 'ctx' ? this[field + 'Classes'] : this[field];
|
|
27
|
+
for (const key of arr) {
|
|
28
|
+
service = service[key];
|
|
29
|
+
}
|
|
30
|
+
service = service.prototype || service;
|
|
31
|
+
}
|
|
32
|
+
this._mockFn(service, methodName, fn);
|
|
33
|
+
return this;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import http, { Server } from 'node:http';
|
|
2
|
+
|
|
3
|
+
const SERVER = Symbol('http_server');
|
|
4
|
+
|
|
5
|
+
export function createServer(app: any): Server {
|
|
6
|
+
let server = app[SERVER] || app.callback();
|
|
7
|
+
if (typeof server === 'function') {
|
|
8
|
+
server = http.createServer(server);
|
|
9
|
+
// cache server, avoid create many times
|
|
10
|
+
app[SERVER] = server;
|
|
11
|
+
if (!app.server) {
|
|
12
|
+
app.server = server;
|
|
13
|
+
}
|
|
14
|
+
// emit server event just like egg-cluster does
|
|
15
|
+
// https://github.com/eggjs/egg-cluster/blob/master/lib/app_worker.js#L52
|
|
16
|
+
app.emit('server', server);
|
|
17
|
+
}
|
|
18
|
+
return server;
|
|
19
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { mm } from 'mm';
|
|
2
|
+
import { extend } from 'extend2';
|
|
3
|
+
import type { Dispatcher, Headers, BodyInit } from 'urllib';
|
|
4
|
+
import { getMockAgent } from './mock_agent.js';
|
|
5
|
+
|
|
6
|
+
export interface MockResultOptions {
|
|
7
|
+
data: string | Buffer | Record<string, any>;
|
|
8
|
+
/**
|
|
9
|
+
* http status
|
|
10
|
+
*/
|
|
11
|
+
status?: number;
|
|
12
|
+
/**
|
|
13
|
+
* response header
|
|
14
|
+
*/
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
/**
|
|
17
|
+
* delay the associated reply by a set amount in ms
|
|
18
|
+
*/
|
|
19
|
+
delay?: number;
|
|
20
|
+
/**
|
|
21
|
+
* any matching request will always reply with the defined response indefinitely
|
|
22
|
+
*/
|
|
23
|
+
persist?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* any matching request will reply with the defined response a fixed amount of times
|
|
26
|
+
*/
|
|
27
|
+
repeats?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface MockResponseCallbackOptions {
|
|
31
|
+
path: string;
|
|
32
|
+
method: string;
|
|
33
|
+
headers?: Headers | Record<string, string>;
|
|
34
|
+
origin?: string;
|
|
35
|
+
body?: BodyInit | Dispatcher.DispatchOptions['body'] | null;
|
|
36
|
+
maxRedirections?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type MockResultFunction =
|
|
40
|
+
(url: string, options: MockResponseCallbackOptions) => MockResultOptions | string;
|
|
41
|
+
|
|
42
|
+
function normalizeResult(result: string | MockResultOptions) {
|
|
43
|
+
if (typeof result === 'string') {
|
|
44
|
+
result = { data: result };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!result.status) {
|
|
48
|
+
result.status = 200;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
result.data = result.data || '';
|
|
52
|
+
if (Buffer.isBuffer(result.data)) {
|
|
53
|
+
// do nothing
|
|
54
|
+
} else if (typeof result.data === 'object') {
|
|
55
|
+
// json
|
|
56
|
+
result.data = Buffer.from(JSON.stringify(result.data));
|
|
57
|
+
} else if (typeof result.data === 'string') {
|
|
58
|
+
// string
|
|
59
|
+
result.data = Buffer.from(result.data);
|
|
60
|
+
} else {
|
|
61
|
+
throw new Error('`mockResult.data` must be buffer, string or json');
|
|
62
|
+
}
|
|
63
|
+
result.headers = result.headers ?? {};
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const MOCK_CONFIGS = Symbol('MOCK_CONFIGS');
|
|
68
|
+
const MOCK_CONFIG_INDEX = Symbol('MOCK_CONFIG_INDEX');
|
|
69
|
+
|
|
70
|
+
export function createMockHttpClient(app: any) {
|
|
71
|
+
/**
|
|
72
|
+
* mock httpclient
|
|
73
|
+
* @function mockHttpclient
|
|
74
|
+
* @param {String} mockUrl - url
|
|
75
|
+
* @param {String|Array} mockMethod - http method, default is '*'
|
|
76
|
+
* @param {Object|Function} mockResult - you data
|
|
77
|
+
* - data - buffer / string / json
|
|
78
|
+
* - status - http status
|
|
79
|
+
* - headers - response header
|
|
80
|
+
* - delay - delay the associated reply by a set amount in ms.
|
|
81
|
+
* - persist - any matching request will always reply with the defined response indefinitely, default is true
|
|
82
|
+
* - repeats - number, any matching request will reply with the defined response a fixed amount of times
|
|
83
|
+
*/
|
|
84
|
+
return function mockHttpClient(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string) {
|
|
85
|
+
let mockMethods = mockMethod as string[];
|
|
86
|
+
if (!mockResult) {
|
|
87
|
+
// app.mockHttpclient(mockUrl, mockResult)
|
|
88
|
+
mockResult = mockMethod as MockResultOptions;
|
|
89
|
+
mockMethods = [ '*' ];
|
|
90
|
+
}
|
|
91
|
+
if (!Array.isArray(mockMethods)) {
|
|
92
|
+
mockMethods = [ mockMethods ];
|
|
93
|
+
}
|
|
94
|
+
mockMethods = mockMethods.map(method => (method || 'GET').toUpperCase());
|
|
95
|
+
|
|
96
|
+
// use MockAgent on undici
|
|
97
|
+
let mockConfigs = app[MOCK_CONFIGS];
|
|
98
|
+
if (!mockConfigs) {
|
|
99
|
+
mockConfigs = [];
|
|
100
|
+
mm(app, MOCK_CONFIGS, mockConfigs);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let mockConfigIndex = -1;
|
|
104
|
+
let origin = mockUrl;
|
|
105
|
+
let originMethod: ((value: string) => boolean) | undefined;
|
|
106
|
+
const pathname = mockUrl;
|
|
107
|
+
let pathMethod: (path: string) => boolean;
|
|
108
|
+
if (typeof mockUrl === 'string') {
|
|
109
|
+
const urlObject = new URL(mockUrl);
|
|
110
|
+
origin = urlObject.origin;
|
|
111
|
+
const originalPathname = urlObject.pathname;
|
|
112
|
+
pathMethod = path => {
|
|
113
|
+
if (path === originalPathname) return true;
|
|
114
|
+
// should match /foo?a=1 including query
|
|
115
|
+
if (path.includes('?')) return path.startsWith(originalPathname);
|
|
116
|
+
return false;
|
|
117
|
+
};
|
|
118
|
+
} else if (mockUrl instanceof RegExp) {
|
|
119
|
+
let requestOrigin = '';
|
|
120
|
+
originMethod = value => {
|
|
121
|
+
requestOrigin = value;
|
|
122
|
+
return true;
|
|
123
|
+
};
|
|
124
|
+
pathMethod = path => {
|
|
125
|
+
for (const config of mockConfigs) {
|
|
126
|
+
if (config.mockUrl.test(`${requestOrigin}${path}`)) {
|
|
127
|
+
mm(app, MOCK_CONFIG_INDEX, config.mockConfigIndex);
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
};
|
|
133
|
+
mockConfigIndex = mockConfigs.length;
|
|
134
|
+
mockConfigs.push({ mockUrl, mockResult, mockConfigIndex });
|
|
135
|
+
}
|
|
136
|
+
const mockPool = originMethod
|
|
137
|
+
? getMockAgent(app).get(originMethod)
|
|
138
|
+
: getMockAgent(app).get(originMethod ?? origin as string);
|
|
139
|
+
// persist default is true
|
|
140
|
+
let persist = true;
|
|
141
|
+
if (typeof mockResult === 'object' && typeof mockResult.persist === 'boolean') {
|
|
142
|
+
persist = mockResult.persist;
|
|
143
|
+
}
|
|
144
|
+
mockMethods.forEach(function(method) {
|
|
145
|
+
const mockScope = mockPool.intercept({
|
|
146
|
+
path: pathMethod ?? pathname,
|
|
147
|
+
method: method === '*' ? () => true : method,
|
|
148
|
+
}).reply(options => {
|
|
149
|
+
// not support mockResult as an async function
|
|
150
|
+
const requestUrl = `${options.origin}${options.path}`;
|
|
151
|
+
let mockRequestResult;
|
|
152
|
+
if (mockConfigIndex >= 0) {
|
|
153
|
+
mockResult = mockConfigs[app[MOCK_CONFIG_INDEX]].mockResult;
|
|
154
|
+
mockRequestResult = typeof mockResult === 'function' ? mockResult(requestUrl, options) : mockResult;
|
|
155
|
+
} else {
|
|
156
|
+
mockRequestResult = typeof mockResult === 'function' ? mockResult(requestUrl, options) : mockResult;
|
|
157
|
+
}
|
|
158
|
+
const result = extend(true, {}, normalizeResult(mockRequestResult!));
|
|
159
|
+
return {
|
|
160
|
+
statusCode: result.status,
|
|
161
|
+
data: result.data,
|
|
162
|
+
responseOptions: {
|
|
163
|
+
headers: result.headers,
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
if (typeof mockResult === 'object') {
|
|
168
|
+
if (mockResult.delay && mockResult.delay > 0) {
|
|
169
|
+
mockScope.delay(mockResult.delay);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (persist) {
|
|
173
|
+
mockScope.persist();
|
|
174
|
+
} else if (typeof mockResult === 'object' && mockResult.repeats && mockResult.repeats > 0) {
|
|
175
|
+
mockScope.times(mockResult.repeats);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export type MockHttpClientMethod = ReturnType<typeof createMockHttpClient>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { debuglog } from 'node:util';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { Base } from 'sdk-base';
|
|
4
|
+
import { detectPort } from 'detect-port';
|
|
5
|
+
import { importModule } from '@eggjs/utils';
|
|
6
|
+
import type { EggCore } from '@eggjs/core';
|
|
7
|
+
import { context } from '../context.js';
|
|
8
|
+
import { formatOptions } from '../format_options.js';
|
|
9
|
+
import { MockOptions, MockApplicationOptions } from '../types.js';
|
|
10
|
+
import { sleep, rimraf } from '../utils.js';
|
|
11
|
+
import { setCustomLoader } from '../mock_custom_loader.js';
|
|
12
|
+
import { APP_INIT } from './util.js';
|
|
13
|
+
|
|
14
|
+
const debug = debuglog('@eggjs/mock/lib/parallel/agent');
|
|
15
|
+
|
|
16
|
+
export class MockAgent extends Base {
|
|
17
|
+
declare options: MockApplicationOptions;
|
|
18
|
+
baseDir: string;
|
|
19
|
+
[APP_INIT] = false;
|
|
20
|
+
#initOnListeners = new Set<any[]>();
|
|
21
|
+
#initOnceListeners = new Set<any[]>();
|
|
22
|
+
_instance: EggCore;
|
|
23
|
+
|
|
24
|
+
constructor(options: MockApplicationOptions) {
|
|
25
|
+
super({ initMethod: '_init' });
|
|
26
|
+
this.options = options;
|
|
27
|
+
this.baseDir = this.options.baseDir;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async _init() {
|
|
31
|
+
if (this.options.beforeInit) {
|
|
32
|
+
await this.options.beforeInit(this);
|
|
33
|
+
delete this.options.beforeInit;
|
|
34
|
+
}
|
|
35
|
+
if (this.options.clean !== false) {
|
|
36
|
+
const logDir = path.join(this.options.baseDir, 'logs');
|
|
37
|
+
try {
|
|
38
|
+
await rimraf(logDir);
|
|
39
|
+
} catch (err: any) {
|
|
40
|
+
console.error(`remove log dir ${logDir} failed: ${err.stack}`);
|
|
41
|
+
}
|
|
42
|
+
const runDir = path.join(this.options.baseDir, 'run');
|
|
43
|
+
try {
|
|
44
|
+
await rimraf(runDir);
|
|
45
|
+
} catch (err: any) {
|
|
46
|
+
console.error(`remove run dir ${runDir} failed: ${err.stack}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.options.clusterPort = await detectPort();
|
|
51
|
+
process.env.CLUSTER_PORT = String(this.options.clusterPort);
|
|
52
|
+
debug('get clusterPort %s', this.options.clusterPort);
|
|
53
|
+
const { Agent }: { Agent: typeof EggCore } = await importModule(this.options.framework);
|
|
54
|
+
|
|
55
|
+
const agent = this._instance = new Agent({ ...this.options });
|
|
56
|
+
|
|
57
|
+
// egg-mock plugin need to override egg context
|
|
58
|
+
Object.assign(agent.context, context);
|
|
59
|
+
setCustomLoader(agent);
|
|
60
|
+
|
|
61
|
+
debug('agent instantiate');
|
|
62
|
+
this[APP_INIT] = true;
|
|
63
|
+
debug('this[APP_INIT] = true');
|
|
64
|
+
this.#bindEvents();
|
|
65
|
+
await agent.ready();
|
|
66
|
+
|
|
67
|
+
const msg = {
|
|
68
|
+
action: 'egg-ready',
|
|
69
|
+
data: this.options,
|
|
70
|
+
};
|
|
71
|
+
(agent as any).messenger.onMessage(msg);
|
|
72
|
+
debug('agent ready');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#bindEvents() {
|
|
76
|
+
debug('bind cache events to agent');
|
|
77
|
+
for (const args of this.#initOnListeners) {
|
|
78
|
+
debug('on(%s), use cache and pass to agent', args);
|
|
79
|
+
this._instance.on(args[0], args[1]);
|
|
80
|
+
this.removeListener(args[0], args[1]);
|
|
81
|
+
}
|
|
82
|
+
for (const args of this.#initOnceListeners) {
|
|
83
|
+
debug('once(%s), use cache and pass to agent', args);
|
|
84
|
+
this._instance.once(args[0], args[1]);
|
|
85
|
+
this.removeListener(args[0], args[1]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
on(...args: any[]) {
|
|
90
|
+
if (this[APP_INIT]) {
|
|
91
|
+
debug('on(%s), pass to agent', args);
|
|
92
|
+
this._instance.on(args[0], args[1]);
|
|
93
|
+
} else {
|
|
94
|
+
debug('on(%s), cache it because agent has not init', args);
|
|
95
|
+
this.#initOnListeners.add(args);
|
|
96
|
+
super.on(args[0], args[1]);
|
|
97
|
+
}
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
once(...args: any[]) {
|
|
102
|
+
if (this[APP_INIT]) {
|
|
103
|
+
debug('once(%s), pass to agent', args);
|
|
104
|
+
this._instance.once(args[0], args[1]);
|
|
105
|
+
} else {
|
|
106
|
+
debug('once(%s), cache it because agent has not init', args);
|
|
107
|
+
this.#initOnceListeners.add(args);
|
|
108
|
+
super.on(args[0], args[1]);
|
|
109
|
+
}
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* close agent
|
|
115
|
+
*/
|
|
116
|
+
async _close() {
|
|
117
|
+
if (this._instance) {
|
|
118
|
+
await this._instance.close();
|
|
119
|
+
} else {
|
|
120
|
+
// when agent init throws an exception, must wait for agent quit gracefully
|
|
121
|
+
await sleep(200);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function createAgent(options: MockOptions) {
|
|
127
|
+
return new MockAgent(formatOptions(options));
|
|
128
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { debuglog } from 'node:util';
|
|
2
|
+
import { Base } from 'sdk-base';
|
|
3
|
+
import { importModule } from '@eggjs/utils';
|
|
4
|
+
import type { EggCore } from '@eggjs/core';
|
|
5
|
+
import { context } from '../context.js';
|
|
6
|
+
import { formatOptions } from '../format_options.js';
|
|
7
|
+
import { MockOptions, MockApplicationOptions } from '../types.js';
|
|
8
|
+
import { sleep } from '../utils.js';
|
|
9
|
+
import { setCustomLoader } from '../mock_custom_loader.js';
|
|
10
|
+
import { createServer } from '../mock_http_server.js';
|
|
11
|
+
import { proxyApp, APP_INIT } from './util.js';
|
|
12
|
+
|
|
13
|
+
const debug = debuglog('@eggjs/mock/lib/parallel/app');
|
|
14
|
+
|
|
15
|
+
export class MockParallelApplication extends Base {
|
|
16
|
+
declare options: MockApplicationOptions;
|
|
17
|
+
baseDir: string;
|
|
18
|
+
[APP_INIT] = false;
|
|
19
|
+
#initOnListeners = new Set<any[]>();
|
|
20
|
+
#initOnceListeners = new Set<any[]>();
|
|
21
|
+
_instance: EggCore;
|
|
22
|
+
|
|
23
|
+
constructor(options: MockApplicationOptions) {
|
|
24
|
+
super({ initMethod: '_init' });
|
|
25
|
+
this.options = options;
|
|
26
|
+
this.baseDir = options.baseDir;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async _init() {
|
|
30
|
+
if (this.options.beforeInit) {
|
|
31
|
+
await this.options.beforeInit(this);
|
|
32
|
+
delete this.options.beforeInit;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!this.options.clusterPort) {
|
|
36
|
+
this.options.clusterPort = parseInt(process.env.CLUSTER_PORT!);
|
|
37
|
+
}
|
|
38
|
+
if (!this.options.clusterPort) {
|
|
39
|
+
throw new Error('cannot get env.CLUSTER_PORT, parallel run fail');
|
|
40
|
+
}
|
|
41
|
+
debug('get clusterPort %s', this.options.clusterPort);
|
|
42
|
+
const { Application }: { Application: typeof EggCore } = await importModule(this.options.framework);
|
|
43
|
+
|
|
44
|
+
const app = this._instance = new Application({ ...this.options });
|
|
45
|
+
|
|
46
|
+
// egg-mock plugin need to override egg context
|
|
47
|
+
Object.assign(app.context, context);
|
|
48
|
+
setCustomLoader(app);
|
|
49
|
+
|
|
50
|
+
debug('app instantiate');
|
|
51
|
+
this[APP_INIT] = true;
|
|
52
|
+
debug('this[APP_INIT] = true');
|
|
53
|
+
this.#bindEvents();
|
|
54
|
+
debug('http server instantiate');
|
|
55
|
+
createServer(app);
|
|
56
|
+
await app.ready();
|
|
57
|
+
|
|
58
|
+
const msg = {
|
|
59
|
+
action: 'egg-ready',
|
|
60
|
+
data: this.options,
|
|
61
|
+
};
|
|
62
|
+
(app as any).messenger.onMessage(msg);
|
|
63
|
+
debug('app ready');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#bindEvents() {
|
|
67
|
+
for (const args of this.#initOnListeners) {
|
|
68
|
+
debug('on(%s), use cache and pass to app', args);
|
|
69
|
+
this._instance.on(args[0], args[1]);
|
|
70
|
+
this.removeListener(args[0], args[1]);
|
|
71
|
+
}
|
|
72
|
+
for (const args of this.#initOnceListeners) {
|
|
73
|
+
debug('once(%s), use cache and pass to app', args);
|
|
74
|
+
this._instance.once(args[0], args[1]);
|
|
75
|
+
this.removeListener(args[0], args[1]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
on(...args: any[]) {
|
|
80
|
+
if (this[APP_INIT]) {
|
|
81
|
+
debug('on(%s), pass to app', args);
|
|
82
|
+
this._instance.on(args[0], args[1]);
|
|
83
|
+
} else {
|
|
84
|
+
debug('on(%s), cache it because app has not init', args);
|
|
85
|
+
if (this.#initOnListeners) {
|
|
86
|
+
this.#initOnListeners.add(args);
|
|
87
|
+
}
|
|
88
|
+
super.on(args[0], args[1]);
|
|
89
|
+
}
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
once(...args: any[]) {
|
|
94
|
+
if (this[APP_INIT]) {
|
|
95
|
+
debug('once(%s), pass to app', args);
|
|
96
|
+
this._instance.once(args[0], args[1]);
|
|
97
|
+
} else {
|
|
98
|
+
debug('once(%s), cache it because app has not init', args);
|
|
99
|
+
if (this.#initOnceListeners) {
|
|
100
|
+
this.#initOnceListeners.add(args);
|
|
101
|
+
}
|
|
102
|
+
super.on(args[0], args[1]);
|
|
103
|
+
}
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* close app
|
|
109
|
+
*/
|
|
110
|
+
async _close() {
|
|
111
|
+
if (this._instance) {
|
|
112
|
+
await this._instance.close();
|
|
113
|
+
} else {
|
|
114
|
+
// when app init throws an exception, must wait for app quit gracefully
|
|
115
|
+
await sleep(200);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function createApp(initOptions: MockOptions) {
|
|
121
|
+
const app = new MockParallelApplication(formatOptions(initOptions));
|
|
122
|
+
return proxyApp(app);
|
|
123
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { debuglog } from 'node:util';
|
|
2
|
+
import { getProperty } from '../utils.js';
|
|
3
|
+
|
|
4
|
+
const debug = debuglog('@eggjs/mock/lib/parallel/util');
|
|
5
|
+
|
|
6
|
+
export const MOCK_APP_METHOD = [
|
|
7
|
+
'ready',
|
|
8
|
+
'isClosed',
|
|
9
|
+
'closed',
|
|
10
|
+
'close',
|
|
11
|
+
'on',
|
|
12
|
+
'once',
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export const APP_INIT = Symbol('appInit');
|
|
16
|
+
|
|
17
|
+
export function proxyApp(app: any) {
|
|
18
|
+
const proxyApp = new Proxy(app, {
|
|
19
|
+
get(target, prop: string) {
|
|
20
|
+
// don't delegate properties on MockAgent
|
|
21
|
+
if (MOCK_APP_METHOD.includes(prop)) {
|
|
22
|
+
return getProperty(target, prop);
|
|
23
|
+
}
|
|
24
|
+
if (!target[APP_INIT]) throw new Error(`can't get ${prop} before ready`);
|
|
25
|
+
// it's asynchronous when agent and app are loading,
|
|
26
|
+
// so should get the properties after loader ready
|
|
27
|
+
debug('proxy handler.get %s', prop);
|
|
28
|
+
return target._instance[prop];
|
|
29
|
+
},
|
|
30
|
+
set(target, prop: string, value) {
|
|
31
|
+
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
32
|
+
if (!target[APP_INIT]) throw new Error(`can't set ${prop} before ready`);
|
|
33
|
+
debug('proxy handler.set %s', prop);
|
|
34
|
+
target._instance[prop] = value;
|
|
35
|
+
return true;
|
|
36
|
+
},
|
|
37
|
+
defineProperty(target, prop: string, descriptor) {
|
|
38
|
+
// can't define properties on MockAgent
|
|
39
|
+
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
40
|
+
if (!target[APP_INIT]) throw new Error(`can't defineProperty ${prop} before ready`);
|
|
41
|
+
debug('proxy handler.defineProperty %s', prop);
|
|
42
|
+
Object.defineProperty(target._instance, prop, descriptor);
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
deleteProperty(target, prop: string) {
|
|
46
|
+
// can't delete properties on MockAgent
|
|
47
|
+
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
48
|
+
if (!target[APP_INIT]) throw new Error(`can't delete ${prop} before ready`);
|
|
49
|
+
debug('proxy handler.deleteProperty %s', prop);
|
|
50
|
+
delete target._instance[prop];
|
|
51
|
+
return true;
|
|
52
|
+
},
|
|
53
|
+
getOwnPropertyDescriptor(target, prop: string) {
|
|
54
|
+
if (MOCK_APP_METHOD.includes(prop)) return Object.getOwnPropertyDescriptor(target, prop);
|
|
55
|
+
if (!target[APP_INIT]) throw new Error(`can't getOwnPropertyDescriptor ${prop} before ready`);
|
|
56
|
+
debug('proxy handler.getOwnPropertyDescriptor %s', prop);
|
|
57
|
+
return Object.getOwnPropertyDescriptor(target._instance, prop);
|
|
58
|
+
},
|
|
59
|
+
getPrototypeOf(target) {
|
|
60
|
+
if (!target[APP_INIT]) throw new Error('can\'t getPrototypeOf before ready');
|
|
61
|
+
debug('proxy handler.getPrototypeOf %s');
|
|
62
|
+
return Object.getPrototypeOf(target._instance);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
return proxyApp;
|
|
66
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// const debug = require('util').debuglog('egg-mock/prerequire');
|
|
2
|
+
// const path = require('path');
|
|
3
|
+
// const { existsSync } = require('fs');
|
|
4
|
+
// const globby = require('globby');
|
|
5
|
+
|
|
6
|
+
// const cwd = process.cwd();
|
|
7
|
+
// const dirs = [];
|
|
8
|
+
// if (existsSync(path.join(cwd, 'app'))) {
|
|
9
|
+
// dirs.push('app/**/*.js');
|
|
10
|
+
// }
|
|
11
|
+
// // avoid Error: ENOENT: no such file or directory, scandir
|
|
12
|
+
// if (existsSync(path.join(cwd, 'config'))) {
|
|
13
|
+
// dirs.push('config/**/*.js');
|
|
14
|
+
// }
|
|
15
|
+
// const files = globby.sync(dirs, { cwd });
|
|
16
|
+
|
|
17
|
+
// for (const file of files) {
|
|
18
|
+
// const filepath = path.join(cwd, file);
|
|
19
|
+
// try {
|
|
20
|
+
// debug('%s prerequire %s', process.pid, filepath);
|
|
21
|
+
// require(filepath);
|
|
22
|
+
// } catch (err) {
|
|
23
|
+
// debug('prerequire error %s', err.message);
|
|
24
|
+
// }
|
|
25
|
+
// }
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import httpClient from 'urllib';
|
|
2
|
+
|
|
3
|
+
const { port, method, args, property, needResult } = JSON.parse(process.argv[2]);
|
|
4
|
+
const url = `http://127.0.0.1:${port}/__egg_mock_call_function`;
|
|
5
|
+
|
|
6
|
+
httpClient.request(url, {
|
|
7
|
+
method: 'POST',
|
|
8
|
+
data: {
|
|
9
|
+
method,
|
|
10
|
+
args,
|
|
11
|
+
property,
|
|
12
|
+
needResult,
|
|
13
|
+
},
|
|
14
|
+
contentType: 'json',
|
|
15
|
+
dataType: 'json',
|
|
16
|
+
}).then(({ data }) => {
|
|
17
|
+
if (!data.success) {
|
|
18
|
+
// console.log('POST %s error, method: %s, args: %j, result data: %j',
|
|
19
|
+
// url, method, args, data);
|
|
20
|
+
if (data.error) {
|
|
21
|
+
console.error(data.error);
|
|
22
|
+
} else if (data.message) {
|
|
23
|
+
const err = new Error(data.message);
|
|
24
|
+
err.stack = data.stack;
|
|
25
|
+
console.error(err);
|
|
26
|
+
}
|
|
27
|
+
process.exit(2);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (data.result) {
|
|
31
|
+
console.log('%j', data.result);
|
|
32
|
+
}
|
|
33
|
+
process.exit(0);
|
|
34
|
+
}).catch(err => {
|
|
35
|
+
// ignore ECONNREFUSED error on mockRestore
|
|
36
|
+
if (method === 'mockRestore' && err.message.includes('ECONNREFUSED')) {
|
|
37
|
+
process.exit(0);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
console.error('POST %s error, method: %s, args: %j', url, method, args);
|
|
41
|
+
console.error(err.stack);
|
|
42
|
+
|
|
43
|
+
// ignore all error on mockRestore
|
|
44
|
+
if (method === 'mockRestore') {
|
|
45
|
+
process.exit(0);
|
|
46
|
+
} else {
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
});
|