|
| 1 | +/** |
| 2 | + * Answering a form without a person. |
| 3 | + * |
| 4 | + * Two pieces, deliberately separate. `FormResponder.respond` receives only the |
| 5 | + * form's URL: whoever installs it has already closed over the answer, so nothing |
| 6 | + * downstream ever carries predetermined data — not a component prop, not a |
| 7 | + * live-form input, not the authoring contract. Production answers nothing. |
| 8 | + * |
| 9 | + * `submitForm` is the submission itself, and it speaks the same HTTP a browser |
| 10 | + * speaks to the same token-scoped URL. An injected responder goes through it, so |
| 11 | + * the token, the exact `Host`, the loopback `Origin`, the media type, the 1 MiB |
| 12 | + * ceiling, schema validation, the single reservation, the 409, and the wait for |
| 13 | + * the 204 all still apply. A responder able to skip any of those would make a |
| 14 | + * green test say nothing about whether a browser could submit. |
| 15 | + */ |
| 16 | + |
| 17 | +import { type Api, createApi, type Operations } from "@effectionx/context-api"; |
| 18 | +import { action } from "effection"; |
| 19 | +import type { Operation } from "effection"; |
| 20 | +import { request } from "node:http"; |
| 21 | + |
| 22 | +import type { Json } from "./json.ts"; |
| 23 | + |
| 24 | +export interface FormResponderApi { |
| 25 | + /** |
| 26 | + * Answer the form at `url`. |
| 27 | + * |
| 28 | + * The URL is the only input. An implementation that needs an answer holds it |
| 29 | + * already; asking for one here is what would push test data into the caller. |
| 30 | + */ |
| 31 | + respond(url: string): Operation<void>; |
| 32 | +} |
| 33 | + |
| 34 | +export const FormResponder: Api<FormResponderApi> = createApi<FormResponderApi>("FormResponder", { |
| 35 | + // A live form is answered by a person, so the default does nothing at all and |
| 36 | + // the submission stays pending until one arrives. |
| 37 | + // deno-lint-ignore require-yield |
| 38 | + *respond(): Operation<void> {}, |
| 39 | +}); |
| 40 | + |
| 41 | +export const respond: Operations<FormResponderApi>["respond"] = FormResponder.operations.respond; |
| 42 | + |
| 43 | +export interface FormResponse { |
| 44 | + status: number; |
| 45 | + body: string; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Submit to a form's URL exactly as its page would. |
| 50 | + * |
| 51 | + * `Origin` is set to the form's own origin because the server refuses a |
| 52 | + * submission without it. That is not a bypass — it is what a browser on that page |
| 53 | + * sends, and the server still checks it. `Host` is left to `node:http` to derive |
| 54 | + * from the URL, so it names where the request is actually going. |
| 55 | + */ |
| 56 | +export function submitForm(url: string, data: Json): Operation<FormResponse> { |
| 57 | + return postJson(new URL("submit", url), JSON.stringify(data)); |
| 58 | +} |
| 59 | + |
| 60 | +function postJson(url: URL, body: string): Operation<FormResponse> { |
| 61 | + return action<FormResponse>((resolve, reject) => { |
| 62 | + const payload = new TextEncoder().encode(body); |
| 63 | + |
| 64 | + const outgoing = request( |
| 65 | + { |
| 66 | + protocol: url.protocol, |
| 67 | + hostname: url.hostname, |
| 68 | + port: url.port, |
| 69 | + path: url.pathname, |
| 70 | + method: "POST", |
| 71 | + headers: { |
| 72 | + "Content-Type": "application/json", |
| 73 | + "Content-Length": String(payload.byteLength), |
| 74 | + Origin: url.origin, |
| 75 | + }, |
| 76 | + }, |
| 77 | + (incoming) => { |
| 78 | + let received = ""; |
| 79 | + incoming.setEncoding("utf8"); |
| 80 | + incoming.on("data", (chunk: string) => { |
| 81 | + received += chunk; |
| 82 | + }); |
| 83 | + incoming.on("end", () => { |
| 84 | + resolve({ status: incoming.statusCode ?? 0, body: received }); |
| 85 | + }); |
| 86 | + incoming.on("error", (error: Error) => reject(error)); |
| 87 | + }, |
| 88 | + ); |
| 89 | + |
| 90 | + outgoing.on("error", (error: Error) => reject(error)); |
| 91 | + outgoing.end(payload); |
| 92 | + |
| 93 | + // Runs however the action ends, so a halted responder leaves no socket. |
| 94 | + return () => { |
| 95 | + outgoing.destroy(); |
| 96 | + }; |
| 97 | + }); |
| 98 | +} |
0 commit comments