soma-core 1.0.3 → 1.1.0
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/app/SomaPageClient.tsx +8 -45
- package/app/[...path]/page.tsx +21 -0
- package/app/api/[...path]/route.ts +86 -77
- package/app/components/SomaHostPage.tsx +133 -0
- package/app/components/SomaHydrationShell.tsx +1 -1
- package/app/layout.tsx +10 -6
- package/app/page.tsx +7 -18
- package/app/soma/[...path]/route.ts +9 -0
- package/app/{api/soma-manifest → soma/manifest}/route.ts +3 -3
- package/app/soma/page/route.ts +29 -0
- package/app/soma/page-object/route.ts +158 -0
- package/app/soma/route.ts +9 -0
- package/bin/soma-mock.mjs +38 -0
- package/core/base/SomaHttpClient.ts +5 -3
- package/core/compiler/full-manifest-builder.ts +84 -69
- package/core/compiler/host-config-sync.cjs +32 -6
- package/core/compiler/soma-manifest-webpack-plugin.js +41 -7
- package/core/engine/soma-headless-page.ts +86 -0
- package/core/host/app-config-composer.ts +90 -0
- package/core/host/host-config-paths.cjs +100 -0
- package/core/host/host-config-paths.ts +132 -0
- package/core/host/invoke-host-request-hook.ts +37 -0
- package/core/host/load-host-domain-class.ts +87 -0
- package/core/host/page-response-mode.cjs +96 -0
- package/core/host/page-response-request.ts +35 -0
- package/core/host/request-origin.ts +18 -0
- package/core/host/resolve-host-request-hook.ts +29 -0
- package/core/host/server-page-assets.ts +193 -0
- package/core/host/soma-domain-loaders.stub.ts +6 -0
- package/core/host/soma-domain-loaders.ts +69 -0
- package/core/host/soma-request-hook-host.stub.ts +4 -0
- package/core/host/source-packages.ts +46 -5
- package/core/index.ts +22 -0
- package/core/infra/http/SomaHttpService.ts +23 -14
- package/core/infra/http/soma-server-request-context.ts +126 -0
- package/core/mock/node/catch-all.mjs +35 -0
- package/core/mock/node/cli.d.mts +58 -0
- package/core/mock/node/cli.mjs +243 -0
- package/core/mock/node/context.mjs +167 -0
- package/core/mock/node/environment-policy.mjs +52 -0
- package/core/mock/node/plugin-loader.mjs +212 -0
- package/core/mock/node/preload.mjs +91 -0
- package/core/mock/node/process-lifecycle.mjs +236 -0
- package/core/mock/node/public.d.mts +110 -0
- package/core/mock/node/public.mjs +4 -0
- package/core/mock/node/registry.mjs +336 -0
- package/core/mock/node/schema-generator.mjs +1310 -0
- package/core/mock/testing/host-harness.d.mts +66 -0
- package/core/mock/testing/host-harness.mjs +276 -0
- package/core/mock/testing/host-runtime.mjs +48 -0
- package/core/runtime/SomaRuntimeServices.ts +5 -1
- package/core/runtime/serialize-page-state.ts +152 -0
- package/dist/cli/soma.js +374 -3
- package/dist/core/index.cjs +2861 -2388
- package/dist/tsconfig.types.tsbuildinfo +1 -1
- package/dist/types/base/SomaHttpClient.d.ts +2 -0
- package/dist/types/compiler/full-manifest-builder.d.ts +2 -0
- package/dist/types/engine/soma-headless-page.d.ts +10 -0
- package/dist/types/host/app-config-composer.d.ts +6 -0
- package/dist/types/host/host-config-paths.d.ts +23 -0
- package/dist/types/host/invoke-host-request-hook.d.ts +8 -0
- package/dist/types/host/load-host-domain-class.d.ts +12 -0
- package/dist/types/host/page-response-request.d.ts +9 -0
- package/dist/types/host/request-origin.d.ts +1 -0
- package/dist/types/host/resolve-host-request-hook.d.ts +16 -0
- package/dist/types/host/server-page-assets.d.ts +29 -0
- package/dist/types/host/soma-domain-loaders.d.ts +19 -0
- package/dist/types/host/soma-domain-loaders.stub.d.ts +4 -0
- package/dist/types/host/soma-request-hook-host.stub.d.ts +2 -0
- package/dist/types/host/source-packages.d.ts +4 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/infra/http/SomaHttpService.d.ts +2 -0
- package/dist/types/infra/http/soma-server-request-context.d.ts +5 -0
- package/dist/types/runtime/serialize-page-state.d.ts +10 -0
- package/docs//351/241/265/351/235/242JSON/346/250/241/345/274/217/346/216/245/345/205/245/346/214/207/345/215/227.md +141 -0
- package/global.d.ts +67 -26
- package/middleware.ts +33 -0
- package/next.config.js +109 -23
- package/package.json +39 -9
- package/tsconfig.json +1 -5
- package/app/api/page/route.ts +0 -119
- package/app/loading.tsx +0 -5
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const SERVER_SYMBOL = Symbol.for('soma.msw.node.server');
|
|
5
|
+
|
|
6
|
+
function isEnabled(value) {
|
|
7
|
+
return ['1', 'true', 'yes', 'on'].includes(String(value || '').trim().toLowerCase());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function resolveRealDirectory(value, label) {
|
|
11
|
+
if (typeof value !== 'string' || !value.trim()) throw new Error(`${label} is required`);
|
|
12
|
+
const resolved = path.resolve(value);
|
|
13
|
+
let stats;
|
|
14
|
+
try {
|
|
15
|
+
stats = fs.statSync(resolved);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
throw new Error(`${label} does not exist: ${resolved}`, { cause: error });
|
|
18
|
+
}
|
|
19
|
+
if (!stats.isDirectory()) throw new Error(`${label} must be a directory: ${resolved}`);
|
|
20
|
+
return fs.realpathSync(resolved);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function startMockServer(appRoot, runtimeHostDir) {
|
|
24
|
+
const [{ setupServer }, { createMockContext }, { createMockHandlers }] = await Promise.all([
|
|
25
|
+
import('msw/node'),
|
|
26
|
+
import('./context.mjs'),
|
|
27
|
+
import('./plugin-loader.mjs'),
|
|
28
|
+
]);
|
|
29
|
+
const context = createMockContext({ hostDir: appRoot, runtimeHostDir });
|
|
30
|
+
const { handlers, registry } = await createMockHandlers(context);
|
|
31
|
+
const server = setupServer(...handlers);
|
|
32
|
+
server.listen({ onUnhandledRequest: 'error' });
|
|
33
|
+
|
|
34
|
+
const runtime = {
|
|
35
|
+
status: 'ready',
|
|
36
|
+
server,
|
|
37
|
+
context,
|
|
38
|
+
handlers,
|
|
39
|
+
registry,
|
|
40
|
+
close() {
|
|
41
|
+
if (runtime.status === 'closed') return;
|
|
42
|
+
runtime.status = 'closed';
|
|
43
|
+
server.close();
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
process.once('exit', () => runtime.close());
|
|
47
|
+
if (!isEnabled(process.env.SOMA_MOCK_QUIET)) {
|
|
48
|
+
console.error(`[soma-msw] enabled pid=${process.pid} for ${appRoot}; ${handlers.length} handlers; registry ${registry.filePath}`);
|
|
49
|
+
}
|
|
50
|
+
return runtime;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (isEnabled(process.env.SOMA_MOCK_ENABLED)) {
|
|
54
|
+
// NODE_OPTIONS is inherited by npm/npx and the SOMA CLI. SOMA core only sets
|
|
55
|
+
// SOMA_HOST_DIR when it starts the real Next process. The workspace is either
|
|
56
|
+
// the regular <Host>/.soma/next directory or the explicit isolated workspace.
|
|
57
|
+
if (process.env.SOMA_HOST_DIR) {
|
|
58
|
+
const runtimeHostDir = resolveRealDirectory(process.env.SOMA_HOST_DIR, 'SOMA_HOST_DIR');
|
|
59
|
+
const appRoot = resolveRealDirectory(
|
|
60
|
+
process.env.SOMA_MOCK_APP_ROOT || runtimeHostDir,
|
|
61
|
+
'SOMA_MOCK_APP_ROOT',
|
|
62
|
+
);
|
|
63
|
+
const cwd = fs.realpathSync(process.cwd());
|
|
64
|
+
const nextWorkspace = resolveRealDirectory(
|
|
65
|
+
process.env.SOMA_NEXT_WORKSPACE_DIR || path.join(runtimeHostDir, '.soma', 'next'),
|
|
66
|
+
'SOMA Next workspace',
|
|
67
|
+
);
|
|
68
|
+
if (cwd !== nextWorkspace) {
|
|
69
|
+
// A non-Next child inherited both variables. It is intentionally outside
|
|
70
|
+
// the interceptor boundary.
|
|
71
|
+
} else {
|
|
72
|
+
if (!globalThis[SERVER_SYMBOL]) {
|
|
73
|
+
const starting = { status: 'starting', ready: null };
|
|
74
|
+
globalThis[SERVER_SYMBOL] = starting;
|
|
75
|
+
starting.ready = startMockServer(appRoot, runtimeHostDir)
|
|
76
|
+
.then((runtime) => {
|
|
77
|
+
globalThis[SERVER_SYMBOL] = runtime;
|
|
78
|
+
return runtime;
|
|
79
|
+
})
|
|
80
|
+
.catch((error) => {
|
|
81
|
+
starting.status = 'failed';
|
|
82
|
+
starting.error = error;
|
|
83
|
+
throw error;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
await globalThis[SERVER_SYMBOL].ready;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { SERVER_SYMBOL };
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { constants as osConstants } from 'node:os';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
|
|
5
|
+
export const PROCESS_TERM_GRACE_MS = 1_500;
|
|
6
|
+
export const PROCESS_KILL_WAIT_MS = 1_500;
|
|
7
|
+
|
|
8
|
+
export class SomaMockCliError extends Error {
|
|
9
|
+
constructor(code, message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'SomaMockCliError';
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function childHasExited(child) {
|
|
17
|
+
return Boolean(child) && (
|
|
18
|
+
(child.exitCode !== null && child.exitCode !== undefined)
|
|
19
|
+
|| (child.signalCode !== null && child.signalCode !== undefined)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const WINDOWS_DESCENDANT_QUERY = [
|
|
24
|
+
'$root = [uint32]$args[0]',
|
|
25
|
+
'$all = @(Get-CimInstance Win32_Process | Select-Object ProcessId, ParentProcessId)',
|
|
26
|
+
'$ids = @()',
|
|
27
|
+
'$parents = @($root)',
|
|
28
|
+
'do {',
|
|
29
|
+
' $next = @($all | Where-Object {',
|
|
30
|
+
' $parents -contains [uint32]$_.ParentProcessId -and $ids -notcontains [uint32]$_.ProcessId',
|
|
31
|
+
' } | ForEach-Object { [uint32]$_.ProcessId })',
|
|
32
|
+
' $ids += $next',
|
|
33
|
+
' $parents = $next',
|
|
34
|
+
'} while ($parents.Count -gt 0)',
|
|
35
|
+
'[Console]::Out.Write((@($ids | Sort-Object -Unique) -join ","))',
|
|
36
|
+
].join('\n');
|
|
37
|
+
|
|
38
|
+
function runWindowsCommand(command, args, options) {
|
|
39
|
+
return (options.windowsCommandRunner ?? spawnSync)(command, args, {
|
|
40
|
+
encoding: 'utf8',
|
|
41
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
42
|
+
windowsHide: true,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function defaultWindowsDescendantPids(pid, options) {
|
|
47
|
+
const result = runWindowsCommand('powershell.exe', [
|
|
48
|
+
'-NoLogo',
|
|
49
|
+
'-NoProfile',
|
|
50
|
+
'-NonInteractive',
|
|
51
|
+
'-Command',
|
|
52
|
+
WINDOWS_DESCENDANT_QUERY,
|
|
53
|
+
String(pid),
|
|
54
|
+
], options);
|
|
55
|
+
if (result.error || result.status !== 0) return null;
|
|
56
|
+
const source = String(result.stdout || '').trim();
|
|
57
|
+
if (!source) return [];
|
|
58
|
+
const processIds = source.split(',').map((value) => Number(value.trim()));
|
|
59
|
+
return processIds.every((value) => Number.isInteger(value) && value > 0)
|
|
60
|
+
? [...new Set(processIds)]
|
|
61
|
+
: null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function defaultWindowsTreeHasExited(child, options) {
|
|
65
|
+
if (!childHasExited(child)) return false;
|
|
66
|
+
if (!Number.isInteger(child?.pid) || child.pid <= 0) return true;
|
|
67
|
+
const descendants = defaultWindowsDescendantPids(child.pid, options);
|
|
68
|
+
// Inspection failure is fail-closed: lifecycle cleanup will attempt /T and
|
|
69
|
+
// eventually return HOST_TERMINATION_TIMEOUT instead of a false success.
|
|
70
|
+
return Array.isArray(descendants) && descendants.length === 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function defaultWindowsTreeKill(pid, signal, options) {
|
|
74
|
+
const argumentsList = ['/PID', String(pid), '/T'];
|
|
75
|
+
if (signal === 'SIGKILL') argumentsList.push('/F');
|
|
76
|
+
const rootResult = runWindowsCommand('taskkill', argumentsList, options);
|
|
77
|
+
if (!rootResult.error && rootResult.status === 0) return true;
|
|
78
|
+
|
|
79
|
+
// The wrapper may already be gone. Windows retains ParentProcessId on live
|
|
80
|
+
// descendants, so enumerate those records and apply taskkill /T to each root.
|
|
81
|
+
const descendants = defaultWindowsDescendantPids(pid, options);
|
|
82
|
+
if (!Array.isArray(descendants)) return false;
|
|
83
|
+
for (const descendantPid of descendants) {
|
|
84
|
+
const descendantArgs = ['/PID', String(descendantPid), '/T'];
|
|
85
|
+
if (signal === 'SIGKILL') descendantArgs.push('/F');
|
|
86
|
+
const result = runWindowsCommand('taskkill', descendantArgs, options);
|
|
87
|
+
if (result.error || result.status !== 0) return false;
|
|
88
|
+
}
|
|
89
|
+
const remaining = defaultWindowsDescendantPids(pid, options);
|
|
90
|
+
return Array.isArray(remaining) && remaining.length === 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function signalChildProcessTree(child, signal, options = {}) {
|
|
94
|
+
const platform = options.platform ?? process.platform;
|
|
95
|
+
let treeSignalled = false;
|
|
96
|
+
if (platform === 'win32' && Number.isInteger(child?.pid) && child.pid > 0) {
|
|
97
|
+
try {
|
|
98
|
+
treeSignalled = options.windowsTreeKill
|
|
99
|
+
? options.windowsTreeKill(child.pid, signal) !== false
|
|
100
|
+
: defaultWindowsTreeKill(child.pid, signal, options);
|
|
101
|
+
} catch {
|
|
102
|
+
// Direct-child fallback below still reports failure through exit confirmation.
|
|
103
|
+
}
|
|
104
|
+
} else if (options.detached && Number.isInteger(child?.pid) && child.pid > 0) {
|
|
105
|
+
try {
|
|
106
|
+
(options.killProcess ?? process.kill)(-child.pid, signal);
|
|
107
|
+
treeSignalled = true;
|
|
108
|
+
} catch {
|
|
109
|
+
// The process group may already have exited; the direct child fallback
|
|
110
|
+
// below still covers a wrapper without a process group.
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!treeSignalled) {
|
|
114
|
+
try {
|
|
115
|
+
child?.kill(signal);
|
|
116
|
+
} catch {
|
|
117
|
+
// Exit confirmation below decides whether this was an already-dead child.
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function processTreeHasExited(child, options = {}) {
|
|
123
|
+
const platform = options.platform ?? process.platform;
|
|
124
|
+
if (platform === 'win32') {
|
|
125
|
+
return typeof options.windowsTreeHasExited === 'function'
|
|
126
|
+
? options.windowsTreeHasExited(child)
|
|
127
|
+
: defaultWindowsTreeHasExited(child, options);
|
|
128
|
+
}
|
|
129
|
+
if (options.detached && platform !== 'win32' && Number.isInteger(child?.pid) && child.pid > 0) {
|
|
130
|
+
try {
|
|
131
|
+
(options.killProcess ?? process.kill)(-child.pid, 0);
|
|
132
|
+
return false;
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (error?.code === 'EPERM') return false;
|
|
135
|
+
return childHasExited(child);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return childHasExited(child);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function waitForProcessTreeExit(child, timeoutMs, options) {
|
|
142
|
+
if (processTreeHasExited(child, options)) return Promise.resolve(true);
|
|
143
|
+
return new Promise((resolve) => {
|
|
144
|
+
const cleanup = () => {
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
clearInterval(poll);
|
|
147
|
+
child.off('exit', check);
|
|
148
|
+
};
|
|
149
|
+
const check = () => {
|
|
150
|
+
if (!processTreeHasExited(child, options)) return;
|
|
151
|
+
cleanup();
|
|
152
|
+
resolve(true);
|
|
153
|
+
};
|
|
154
|
+
const poll = setInterval(check, 25);
|
|
155
|
+
const timer = setTimeout(() => {
|
|
156
|
+
cleanup();
|
|
157
|
+
resolve(processTreeHasExited(child, options));
|
|
158
|
+
}, timeoutMs);
|
|
159
|
+
child.on('exit', check);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function terminateMockChild(child, options = {}) {
|
|
164
|
+
if (!child || processTreeHasExited(child, options)) return;
|
|
165
|
+
signalChildProcessTree(child, 'SIGTERM', options);
|
|
166
|
+
if (await waitForProcessTreeExit(child, options.termGraceMs ?? PROCESS_TERM_GRACE_MS, options)) return;
|
|
167
|
+
signalChildProcessTree(child, 'SIGKILL', options);
|
|
168
|
+
if (!await waitForProcessTreeExit(child, options.killWaitMs ?? PROCESS_KILL_WAIT_MS, options)) {
|
|
169
|
+
throw new SomaMockCliError(
|
|
170
|
+
'HOST_TERMINATION_TIMEOUT',
|
|
171
|
+
`Unable to terminate SOMA Mock Host process tree (pid ${child.pid || 'unknown'})`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function waitForChild(child, signalSource, terminationOptions) {
|
|
177
|
+
return new Promise((resolve, reject) => {
|
|
178
|
+
const handlers = new Map();
|
|
179
|
+
let settled = false;
|
|
180
|
+
let terminationPromise = null;
|
|
181
|
+
let childErrorHandler = null;
|
|
182
|
+
let childExitHandler = null;
|
|
183
|
+
const cleanup = () => {
|
|
184
|
+
for (const [signal, handler] of handlers) signalSource.off(signal, handler);
|
|
185
|
+
handlers.clear();
|
|
186
|
+
if (childErrorHandler) child.off('error', childErrorHandler);
|
|
187
|
+
if (childExitHandler) child.off('exit', childExitHandler);
|
|
188
|
+
childErrorHandler = null;
|
|
189
|
+
childExitHandler = null;
|
|
190
|
+
};
|
|
191
|
+
const settle = (callback, value) => {
|
|
192
|
+
if (settled) return;
|
|
193
|
+
settled = true;
|
|
194
|
+
cleanup();
|
|
195
|
+
callback(value);
|
|
196
|
+
};
|
|
197
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
|
|
198
|
+
const handler = () => {
|
|
199
|
+
if (processTreeHasExited(child, terminationOptions)) return;
|
|
200
|
+
if (!terminationPromise) {
|
|
201
|
+
terminationPromise = terminateMockChild(child, terminationOptions);
|
|
202
|
+
terminationPromise.catch((error) => settle(reject, error));
|
|
203
|
+
} else {
|
|
204
|
+
// A second termination signal is an explicit request to skip grace.
|
|
205
|
+
signalChildProcessTree(child, 'SIGKILL', terminationOptions);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
handlers.set(signal, handler);
|
|
209
|
+
signalSource.on(signal, handler);
|
|
210
|
+
}
|
|
211
|
+
childErrorHandler = (error) => {
|
|
212
|
+
settle(reject, error);
|
|
213
|
+
};
|
|
214
|
+
childExitHandler = (code, signal) => {
|
|
215
|
+
const result = Number.isInteger(code)
|
|
216
|
+
? code
|
|
217
|
+
: signal && osConstants.signals[signal] ? 128 + osConstants.signals[signal] : 1;
|
|
218
|
+
const finish = async () => {
|
|
219
|
+
try {
|
|
220
|
+
if (!terminationPromise && !processTreeHasExited(child, terminationOptions)) {
|
|
221
|
+
// A wrapper may exit before its detached descendants. Treat that as
|
|
222
|
+
// lifecycle cleanup, not a successful process-tree exit.
|
|
223
|
+
terminationPromise = terminateMockChild(child, terminationOptions);
|
|
224
|
+
}
|
|
225
|
+
if (terminationPromise) await terminationPromise;
|
|
226
|
+
settle(resolve, result);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
settle(reject, error);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
void finish();
|
|
232
|
+
};
|
|
233
|
+
child.once('error', childErrorHandler);
|
|
234
|
+
child.once('exit', childExitHandler);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { HttpHandler } from 'msw';
|
|
2
|
+
|
|
3
|
+
export { HttpResponse, delay, http } from 'msw';
|
|
4
|
+
|
|
5
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
6
|
+
export type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue };
|
|
7
|
+
|
|
8
|
+
export interface MockJournalRecord extends Record<string, unknown> {
|
|
9
|
+
sequence: number;
|
|
10
|
+
pid: number;
|
|
11
|
+
timestamp: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface MockJournal {
|
|
15
|
+
readonly filePath: string | null;
|
|
16
|
+
readonly maxRecords: number;
|
|
17
|
+
record(entry: Record<string, unknown>): MockJournalRecord;
|
|
18
|
+
list(): MockJournalRecord[];
|
|
19
|
+
clear(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MockJournalOptions {
|
|
23
|
+
filePath?: string | null;
|
|
24
|
+
maxRecords?: number | string | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface MockContext {
|
|
28
|
+
hostDir: string;
|
|
29
|
+
runtimeHostDir: string;
|
|
30
|
+
seed: string;
|
|
31
|
+
state: Record<string, unknown>;
|
|
32
|
+
journal: MockJournal;
|
|
33
|
+
generateFromSchema<T = unknown>(schema: unknown, options?: SchemaGenerationOptions): T;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface MockContextOptions {
|
|
37
|
+
hostDir?: string;
|
|
38
|
+
runtimeHostDir?: string;
|
|
39
|
+
seed?: string;
|
|
40
|
+
state?: Record<string, unknown>;
|
|
41
|
+
journal?: MockJournal;
|
|
42
|
+
journalFile?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface SchemaGenerationOptions {
|
|
46
|
+
seed?: string;
|
|
47
|
+
path?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type MockHttpMethod = 'ALL' | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
51
|
+
|
|
52
|
+
export interface MockRoute {
|
|
53
|
+
id?: string;
|
|
54
|
+
enabled?: boolean;
|
|
55
|
+
method?: MockHttpMethod | Lowercase<MockHttpMethod>;
|
|
56
|
+
url: string;
|
|
57
|
+
status?: number;
|
|
58
|
+
headers?: Record<string, string | number | boolean>;
|
|
59
|
+
response?: JsonValue;
|
|
60
|
+
fixture?: string;
|
|
61
|
+
schema?: Record<string, unknown>;
|
|
62
|
+
merge?: Record<string, JsonValue>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface RegisteredMockRoute {
|
|
66
|
+
readonly id: string;
|
|
67
|
+
readonly method: MockHttpMethod;
|
|
68
|
+
readonly url: string;
|
|
69
|
+
readonly status: number;
|
|
70
|
+
readonly headers: Record<string, string>;
|
|
71
|
+
readonly mode: 'json' | 'schema';
|
|
72
|
+
readonly payload: unknown;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface RegisteredMockRoutes {
|
|
76
|
+
routes: RegisteredMockRoute[];
|
|
77
|
+
jsonHandlers: HttpHandler[];
|
|
78
|
+
schemaHandlers: HttpHandler[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** App-owned `<App>/mock/handlers.mjs` factory contract. */
|
|
82
|
+
export type SomaMockHandlersFactory = (
|
|
83
|
+
context: MockContext,
|
|
84
|
+
) => readonly HttpHandler[] | Promise<readonly HttpHandler[]>;
|
|
85
|
+
|
|
86
|
+
/** Domain provider definition returned to Core by an upper framework. */
|
|
87
|
+
export interface SomaMockProvider {
|
|
88
|
+
readonly name: string;
|
|
89
|
+
readonly handlers: readonly HttpHandler[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `createSomaMockProvider(context)` factory contract for trusted providers. */
|
|
93
|
+
export type SomaMockProviderFactory = (
|
|
94
|
+
context: MockContext,
|
|
95
|
+
) => SomaMockProvider | Promise<SomaMockProvider>;
|
|
96
|
+
|
|
97
|
+
export interface SomaMockProviderModule {
|
|
98
|
+
createSomaMockProvider: SomaMockProviderFactory;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function createMockJournal(options?: MockJournalOptions): MockJournal;
|
|
102
|
+
export function createMockContext(options?: MockContextOptions): MockContext;
|
|
103
|
+
export function redactJournalValue<T>(value: T): T;
|
|
104
|
+
export function defineMockRoute<T extends MockRoute>(route: T): Readonly<T>;
|
|
105
|
+
export function registerMockRoutes(
|
|
106
|
+
context: MockContext,
|
|
107
|
+
routes: readonly MockRoute[],
|
|
108
|
+
options?: { baseDir?: string },
|
|
109
|
+
): RegisteredMockRoutes;
|
|
110
|
+
export function generateFromSchema<T = unknown>(schema: unknown, options?: SchemaGenerationOptions): T;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { HttpResponse, delay, http } from 'msw';
|
|
2
|
+
export { createMockContext, createMockJournal, redactJournalValue } from './context.mjs';
|
|
3
|
+
export { defineMockRoute, registerMockRoutes } from './registry.mjs';
|
|
4
|
+
export { generateFromSchema } from './schema-generator.mjs';
|