refactor: 템플릿에서 기본적으로 제공하고 있는 워크 플로우에 디자인과 라우팅 흡수
This commit is contained in:
@@ -13,9 +13,16 @@ import {
|
||||
attachStudioSessionCredentials,
|
||||
invalidateTechLogCsrfOnOutcome,
|
||||
} from "../../../src/features/tech-log/adapters/http/studio-session-credentials.ts";
|
||||
import type { StudioOperationExecutor } from "../../../src/features/tech-log/adapters/http/http-studio-gateway.ts";
|
||||
import { TECH_LOG_STUDIO_CONTRIBUTION } from "../../../src/features/tech-log/contracts/tech-log-studio-contract-contribution.ts";
|
||||
import { REFERENCE_FEATURE_TEMPLATE_CONTRIBUTION } from "../../../src/features/reference-feature/contracts/reference-feature-contract-contribution.ts";
|
||||
import type { InstalledHttpOperationExecutor } from "../../../src/adapters/http/index.ts";
|
||||
import { createTechLogHttpBinding } from "../../../src/features/tech-log/adapters/http/tech-log-http-binding.ts";
|
||||
import {
|
||||
TECH_LOG_STUDIO_CONTRIBUTION,
|
||||
TECH_LOG_STUDIO_SESSION_CONTRACT,
|
||||
} from "../../../src/features/tech-log/contracts/tech-log-studio-contract-contribution.ts";
|
||||
import {
|
||||
LIST_REFERENCE_RESOURCES_CONTRACT,
|
||||
REFERENCE_FEATURE_TEMPLATE_CONTRIBUTION,
|
||||
} from "../../../src/features/reference-feature/contracts/reference-feature-contract-contribution.ts";
|
||||
|
||||
/**
|
||||
* I1 (Task 7 fix round 1). C1 was a self-referential CSRF bootstrap cycle
|
||||
@@ -72,16 +79,12 @@ function scopeSnapshot() {
|
||||
* afterward.
|
||||
*/
|
||||
function composeStudioRuntime() {
|
||||
// Both contributions, exactly as `installed-contract-contributions.ts`
|
||||
// composes them: `contractOperations.execute` is one executor shared by
|
||||
// every installed feature, so a reference-feature operation's outcome
|
||||
// travels through the same code path a Studio operation's does.
|
||||
const composed = composeContractContributions([
|
||||
TECH_LOG_STUDIO_CONTRIBUTION,
|
||||
REFERENCE_FEATURE_TEMPLATE_CONTRIBUTION,
|
||||
]);
|
||||
|
||||
let contractOperations: StudioOperationExecutor = Object.freeze({
|
||||
let contractOperations: InstalledHttpOperationExecutor = Object.freeze({
|
||||
async execute() {
|
||||
throw new Error("contractOperations used before assignment");
|
||||
},
|
||||
@@ -90,7 +93,7 @@ function composeStudioRuntime() {
|
||||
const techLogCsrf = createCsrfTokenProvider({
|
||||
async execute(options) {
|
||||
const outcome = await contractOperations.execute(
|
||||
"getStudioSession",
|
||||
TECH_LOG_STUDIO_SESSION_CONTRACT,
|
||||
{},
|
||||
{
|
||||
routeId: "TECH_LOG_STUDIO",
|
||||
@@ -100,13 +103,19 @@ function composeStudioRuntime() {
|
||||
if (outcome.kind !== "SUCCESS") {
|
||||
throw new Error("studio session is unavailable");
|
||||
}
|
||||
const value = outcome.value as { csrfToken: string; csrfHeaderName: string };
|
||||
return { csrfToken: value.csrfToken, csrfHeaderName: value.csrfHeaderName };
|
||||
const value = outcome.value as {
|
||||
csrfToken: string;
|
||||
csrfHeaderName: string;
|
||||
};
|
||||
return {
|
||||
csrfToken: value.csrfToken,
|
||||
csrfHeaderName: value.csrfHeaderName,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const contractHttp = createContractHttpExecutor({
|
||||
baseUrl: `${BASE}/`,
|
||||
baseUrl: BASE + "/",
|
||||
maxRetryAttempts: 0,
|
||||
authProfiles: INSTALLED_REST_AUTH_PROFILES,
|
||||
async attachCredentials(operation, authContext) {
|
||||
@@ -115,9 +124,6 @@ function composeStudioRuntime() {
|
||||
authContext,
|
||||
techLogCsrf,
|
||||
);
|
||||
// `attachStudioSessionCredentials` returns null for a non-Studio
|
||||
// profile so the caller falls through to its own credential logic --
|
||||
// the bearer path in the composition root, this stand-in here.
|
||||
return (
|
||||
outcome ??
|
||||
Object.freeze({
|
||||
@@ -129,30 +135,37 @@ function composeStudioRuntime() {
|
||||
});
|
||||
|
||||
contractOperations = Object.freeze({
|
||||
async execute(operationId, input, executionContext) {
|
||||
const operation = composed.httpByOperationId.get(operationId);
|
||||
if (!operation) throw new Error(`no such operation: ${operationId}`);
|
||||
const outcome = await contractHttp.execute(operation, input, {
|
||||
routeId: executionContext.routeId,
|
||||
scope: scopeSnapshot(),
|
||||
...(executionContext.signal ? { signal: executionContext.signal } : {}),
|
||||
...(executionContext.intent ? { intent: executionContext.intent } : {}),
|
||||
});
|
||||
// Fix round 2, item 1. Same production call as
|
||||
// `bootstrap/runtime-adapters.ts`'s `contractOperations.execute` — not
|
||||
// a reimplementation of it. Final fix wave item 8 added the auth
|
||||
// profile: this executor serves every installed feature, so the call
|
||||
// has to be told which one produced the outcome.
|
||||
async execute(operation, input, executionContext) {
|
||||
const installed = composed.httpByOperationId.get(
|
||||
operation.contract.operationId,
|
||||
);
|
||||
if (!installed) {
|
||||
throw new Error("no such operation: " + operation.contract.operationId);
|
||||
}
|
||||
const outcome = await contractHttp.execute(
|
||||
installed as typeof operation,
|
||||
input,
|
||||
{
|
||||
routeId: executionContext.routeId,
|
||||
scope: scopeSnapshot(),
|
||||
...(executionContext.signal ? { signal: executionContext.signal } : {}),
|
||||
...(executionContext.intent ? { intent: executionContext.intent } : {}),
|
||||
},
|
||||
);
|
||||
invalidateTechLogCsrfOnOutcome(
|
||||
outcome.kind,
|
||||
operation.frontend.authProfileId,
|
||||
installed.frontend.authProfileId,
|
||||
techLogCsrf,
|
||||
);
|
||||
return outcome;
|
||||
},
|
||||
});
|
||||
|
||||
return { contractOperations, techLogCsrf };
|
||||
return {
|
||||
contractOperations,
|
||||
techLogHttp: createTechLogHttpBinding(contractOperations),
|
||||
techLogCsrf,
|
||||
};
|
||||
}
|
||||
|
||||
test(
|
||||
@@ -193,23 +206,22 @@ test(
|
||||
}),
|
||||
);
|
||||
|
||||
const { contractOperations, techLogCsrf } = composeStudioRuntime();
|
||||
const { techLogHttp, techLogCsrf } = composeStudioRuntime();
|
||||
|
||||
// JSON path: a plain read that uses `TECH_LOG_STUDIO_SESSION` and
|
||||
// therefore requires the CSRF header — this is what C1 made impossible
|
||||
// (unbounded recursion, zero dispatched requests).
|
||||
const dashboardOutcome = await contractOperations.execute(
|
||||
const dashboardOutcome = await techLogHttp.execute(
|
||||
"getStudioDashboard",
|
||||
{},
|
||||
{ routeId: "TECH_LOG_STUDIO" },
|
||||
);
|
||||
assert.equal(dashboardOutcome.kind, "SUCCESS");
|
||||
assert.equal(dashboardOutcome.ok, true);
|
||||
assert.equal(jsonRequestHeader, "csrf-token-1");
|
||||
|
||||
// Multipart path: bypasses `contractOperations` entirely but reads the
|
||||
// token from the same `techLogCsrf` instance.
|
||||
const assetGateway = createHttpStudioAssetGateway({
|
||||
operations: contractOperations,
|
||||
http: techLogHttp,
|
||||
csrf: techLogCsrf,
|
||||
upload: createAssetUploadTransport({ baseUrl: `${BASE}/`, timeoutMs: 10_000 }),
|
||||
});
|
||||
@@ -268,22 +280,16 @@ test(
|
||||
}),
|
||||
);
|
||||
|
||||
const { contractOperations } = composeStudioRuntime();
|
||||
const { techLogHttp } = composeStudioRuntime();
|
||||
|
||||
const first = await contractOperations.execute(
|
||||
"getStudioDashboard",
|
||||
{},
|
||||
{ routeId: "TECH_LOG_STUDIO" },
|
||||
);
|
||||
assert.equal(first.kind, "FORBIDDEN");
|
||||
const first = await techLogHttp.execute("getStudioDashboard", {});
|
||||
assert.equal(first.ok, false);
|
||||
if (first.ok) throw new Error("expected dashboard request to fail");
|
||||
assert.equal(first.error.kind, "FORBIDDEN");
|
||||
assert.equal(dashboardHeaders[0], "csrf-token-1");
|
||||
|
||||
const second = await contractOperations.execute(
|
||||
"getStudioDashboard",
|
||||
{},
|
||||
{ routeId: "TECH_LOG_STUDIO" },
|
||||
);
|
||||
assert.equal(second.kind, "SUCCESS");
|
||||
const second = await techLogHttp.execute("getStudioDashboard", {});
|
||||
assert.equal(second.ok, true);
|
||||
assert.equal(dashboardHeaders[1], "csrf-token-2");
|
||||
|
||||
// Two fresh session fetches: the cache was discarded after the 403, not
|
||||
@@ -331,31 +337,23 @@ test(
|
||||
http.get(`${BASE}/api/reference-resources`, () => new HttpResponse(null, { status: 403 })),
|
||||
);
|
||||
|
||||
const { contractOperations } = composeStudioRuntime();
|
||||
const { contractOperations, techLogHttp } = composeStudioRuntime();
|
||||
|
||||
const first = await contractOperations.execute(
|
||||
"getStudioDashboard",
|
||||
{},
|
||||
{ routeId: "TECH_LOG_STUDIO" },
|
||||
);
|
||||
assert.equal(first.kind, "SUCCESS");
|
||||
const first = await techLogHttp.execute("getStudioDashboard", {});
|
||||
assert.equal(first.ok, true);
|
||||
assert.equal(dashboardHeaders[0], "csrf-token-1");
|
||||
|
||||
// Someone else's 403, on a feature that has nothing to do with the Studio
|
||||
// session.
|
||||
const rejected = await contractOperations.execute(
|
||||
"LIST_REFERENCE_RESOURCES",
|
||||
LIST_REFERENCE_RESOURCES_CONTRACT,
|
||||
{ limit: 10 },
|
||||
{ routeId: "REFERENCE_FEATURE" },
|
||||
);
|
||||
assert.equal(rejected.kind, "FORBIDDEN");
|
||||
|
||||
const second = await contractOperations.execute(
|
||||
"getStudioDashboard",
|
||||
{},
|
||||
{ routeId: "TECH_LOG_STUDIO" },
|
||||
);
|
||||
assert.equal(second.kind, "SUCCESS");
|
||||
const second = await techLogHttp.execute("getStudioDashboard", {});
|
||||
assert.equal(second.ok, true);
|
||||
assert.equal(dashboardHeaders[1], "csrf-token-1");
|
||||
assert.equal(sessionCalls, 1);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user