About this project
Request
Marshlight Parcel needs a review of six related service features. The release owner wants the controls kept together so the next reviewer can reproduce the result.
Data
All companies, receipt identifiers and review findings are fictional. The base fixture and a fresh fixture contain separate dependency, network and policy records. No request reaches a real host.
Completion
Keep every earlier fix in the carried source file. Each completed feature must reject its saved misuse and retain both supported requests. The final source file is the deliverable. Its six feature routes must pass together on both fixtures.
Marshlight Parcel has asked you to review a small service before its next release. You'll work in one file across six stages. Each stage adds a control to a different feature, and Check keeps replaying every boundary you have already repaired. A later edit that removes an earlier control will fail.
Read the brief and the current feature's requirements before changing the code. The ordinary requests are part of the acceptance criteria. The fixture uses only seeded data and a fictional network; its stated limits remain in force throughout. Finish with a service that supports legitimate work and refuses each documented misuse.
function feature0(request, env) {
const url = String(request.body.url || '');
let parsed;
try { parsed = new URL(url); }
catch { return env.reply(400, { error: 'invalid address' }); }
if (parsed.origin !== "https://images-221.example.test") return env.reply(403, { error: 'policy refused' });
if (!env.seed.allowed.some(approved => new URL(approved).pathname === parsed.pathname) || parsed.search !== "" || parsed.hash !== "" || parsed.username !== "" || parsed.password !== "") return env.reply(403, { error: 'policy refused' });
const response = env.fetchUrl(parsed.href);
return env.reply(response.status, { ok: true, preview: response.body });
}
function feature1(request, env) {
const document = request.body;
return env.reply(200, { ok: true, title: String(document.title) });
}
function feature2(request, env) {
const component = env.db.find("packages", row => row.id === request.body.id);
if (!component) return env.reply(404, { error: "component missing" });
return env.reply(200, { ok: true, id: component.id, revision: component.revision });
}
function feature3(request, env) {
const artifact = env.db.find("artifacts", row => row.id === request.body.id);
if (!artifact) return env.reply(404, { error: "artifact missing" });
return env.reply(200, { ok: true, id: artifact.id, subject: artifact.subject });
}
function feature4(request, env) {
const value = request.body;
return env.reply(200, { ok: true, note: { text: String(value.text) } });
}
function feature5(request, env) {
const order = env.db.find("orders", row => row.id === request.body.id);
if (!order) return env.reply(404, { error: "order missing" });
const amount = request.body.amount;
return env.reply(200, { ok: true, id: order.id, refund: amount, remaining: order.balance - amount });
}
function handle(request, env) {
const input = request.body || {};
const feature = input.feature;
const body = Object.assign({}, input);
delete body.feature;
const localRequest = Object.assign({}, request, { body });
const localEnv = Object.assign({}, env, { seed: env.seed.routes[feature] });
if (feature === 'feature-0') return feature0(localRequest, localEnv);
if (feature === 'feature-1') return feature1(localRequest, localEnv);
if (feature === 'feature-2') return feature2(localRequest, localEnv);
if (feature === 'feature-3') return feature3(localRequest, localEnv);
if (feature === 'feature-4') return feature4(localRequest, localEnv);
if (feature === 'feature-5') return feature5(localRequest, localEnv);
return env.reply(404, { error: 'feature missing' });
}
Read the fixed handler, then apply the same idea to each exercise.