|
1 | | -import { StreamConfig, StreamInput, StreamOutput } from "@scramjet/types"; |
| 1 | +import { ParsedMessage, StreamConfig, StreamInput, StreamOutput } from "@scramjet/types"; |
2 | 2 | import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from "http"; |
3 | 3 | import { Writable, Readable, Duplex } from "stream"; |
4 | 4 | import { DuplexStream } from "../lib/duplex-stream"; |
@@ -106,16 +106,27 @@ export function createStreamHandlers(router: SequentialCeroRouter) { |
106 | 106 | const downstream = ( |
107 | 107 | path: string | RegExp, |
108 | 108 | stream: StreamOutput, |
109 | | - { json = false, text = false, end: _end = false, encoding = "utf-8", checkContentType = true, checkEndHeader = true, method = "post" }: StreamConfig = {} |
| 109 | + { json = false, text = false, end: _end = false, encoding = "utf-8", checkContentType = true, checkEndHeader = true, method = "post", postponeContinue = false }: StreamConfig = {} |
110 | 110 | ): void => { |
111 | | - router[method](path, async (req, res, next) => { |
| 111 | + // eslint-disable-next-line complexity |
| 112 | + router[method](path, async (req: ParsedMessage, res, next) => { |
112 | 113 | try { |
113 | 114 | if (checkContentType) { |
114 | 115 | checkAccepts(req.headers["content-type"], text, json); |
115 | 116 | } |
116 | 117 |
|
117 | 118 | if (req.headers.expect === "100-continue") { |
118 | | - res.writeContinue(); |
| 119 | + if (!postponeContinue) { |
| 120 | + res.writeContinue(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (postponeContinue && req.headers.expect !== "100-continue") { |
| 125 | + res.writeHead(400, "Bad Request", { "Content-type": "application/json" }); |
| 126 | + res.write(JSON.stringify({ error: "Missing 'expect' header" })); |
| 127 | + res.end(); |
| 128 | + |
| 129 | + return; |
119 | 130 | } |
120 | 131 |
|
121 | 132 | // Explicit pause causes next `on('data')` not to resume stream automatically. |
@@ -177,11 +188,14 @@ export function createStreamHandlers(router: SequentialCeroRouter) { |
177 | 188 | }; |
178 | 189 | const duplex = ( |
179 | 190 | path: string | RegExp, |
180 | | - callback: (stream: Duplex, headers: IncomingHttpHeaders) => void |
| 191 | + callback: (stream: Duplex, headers: IncomingHttpHeaders) => void, |
| 192 | + { postponeContinue = false }: StreamConfig = {} |
181 | 193 | ): void => { |
182 | 194 | router.post(path, (req, res, next) => { |
183 | 195 | if (req.headers.expect === "100-continue") { |
184 | | - res.writeContinue(); |
| 196 | + if (!postponeContinue) { |
| 197 | + res.writeContinue(); |
| 198 | + } |
185 | 199 | } |
186 | 200 |
|
187 | 201 | try { |
|
0 commit comments