Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,23 @@ jobs:
node scripts/framework-consumers.mjs fastify-startup-esm packed/*.tgz
node scripts/framework-consumers.mjs fastify-startup-cjs packed/*.tgz

nuxt-consumer:
name: Nuxt response and streaming integration
needs: pack
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24.x
- uses: actions/download-artifact@v8
with:
name: packed-tarball
path: packed
- name: Build and probe a real Nuxt app
run: node scripts/framework-consumers.mjs nuxt packed/*.tgz

next-consumers:
name: Next.js ${{ matrix.next }} (${{ matrix.bundler }}) production integration
needs: pack
Expand Down Expand Up @@ -474,6 +491,7 @@ jobs:
- next-consumers
- tanstack-consumer
- fastify-startup
- nuxt-consumer
runs-on: ubuntu-latest

steps:
Expand Down
6 changes: 3 additions & 3 deletions scripts/framework-consumers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export async function* stream() {
yield 'data: finished\\n\\n';
}`,
'server/routes/stream.get.ts': `import { Readable } from 'node:stream';
import { sendStream, setResponseHeader } from 'h3';
import { getQuery, sendStream, setResponseHeader } from 'h3';
import { stream } from '../fixture-stream';
export default defineEventHandler(event => {
setResponseHeader(event, 'content-type', 'text/event-stream');
setResponseHeader(event, 'content-type', getQuery(event).text ? 'text/plain' : 'text/event-stream');
setResponseHeader(event, 'x-fixture', 'fixture-stream-private');
return sendStream(event, Readable.from(stream()));
});`,
Expand All @@ -71,7 +71,7 @@ export default defineEventHandler(() => { finishStream(); return { ok: true }; }
rules: 'server/middleware/patchstack.rules.json',
build: ['node_modules/.bin/nuxt', 'build'],
start: ['.output/server/index.mjs'],
stream: [''],
stream: ['', '?text=1'],
},
'react-router': {
dependencies: { '@react-router/dev': '8.5.1', '@react-router/serve': '8.5.1', react: '19.3.1',
Expand Down
1 change: 1 addition & 0 deletions src/protect/install/template-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const PREVIOUS: Record<string, string | readonly string[]> = {
'nuxt-middleware.ts': [
'e377c48cc8c08d20c61c6119bb5f4c8991f0eed3ebd31310b8a0fc6eb5505e4e',
'c4d1cf018db3d8c25d09b4b042c24059db97af5d0ea1847ddb75ab8f6baa8eae',
'e2beb775a8bbbac1d5523018910d43b557c090f7f2c8e6bda782e9dedfd78b65',
],
'next-middleware.ts': '65c163cdb2ddda4fe53c1a3cd20aa6ab63ad7595a66985d9141d595374ecfab3',
'next-guard.ts': 'e37c4d67619df9d850a7d94b567bd76841b6ac8e774a5b442689482f5374c451',
Expand Down
6 changes: 5 additions & 1 deletion src/protect/protect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export interface Protection {
screenResponse(response: Response, request?: Request, ...hostArgs: unknown[]): Promise<Response>;
/** Attach screening before a native Node ServerResponse sends its headers.
* Set headersOnly when the host must pass the response body through without buffering. */
screenNodeResponse(response: unknown, request?: Request, options?: { headersOnly?: boolean }): void;
screenNodeResponse(response: unknown, request?: Request, options?: {
headersOnly?: boolean;
/** Screen headers and pass the stream through when the response starts with write(). */
streamOnWrite?: boolean;
}): void;
express(options?: { screenResponses?: boolean }): (req: unknown, res: unknown, next: () => void) => void;
/**
* Node / Connect middleware that reads the request body itself and exposes it as `req.body`.
Expand Down
9 changes: 5 additions & 4 deletions src/protect/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,9 @@ export async function createProtection(options = {}) {
// Wrap a Node ServerResponse so its (buffered, text) body is screened before it's sent.
// Opt-in (buffering can delay a streamed response); over 512 KiB it stops buffering and
// passes through unscanned.
const wrapNodeResponse = (res, reqCtx, headersOnly = false) => {
const wrapNodeResponse = (res, reqCtx, opts) => {
const headersOnly = opts?.headersOnly === true;
const streamOnWrite = opts?.streamOnWrite === true;
const origWrite = res.write.bind(res);
const origEnd = res.end.bind(res);
// The response's own header operations, for this wrapper's use. While a `writeHead` is held the
Expand Down Expand Up @@ -1549,7 +1551,7 @@ export async function createProtection(options = {}) {
const value = effectiveHead().headers['content-type'];
const contentType = Array.isArray(value) ? value[0] : value;
if (baseContentType(contentType) === 'text/event-stream') skipBody('live-stream');
else if (headersOnly) skipBody('streamed-body');
else if (headersOnly || streamOnWrite) skipBody('streamed-body');
}
if (withheld) {
if (typeof enc === 'function') enc();
Expand Down Expand Up @@ -1898,8 +1900,7 @@ export async function createProtection(options = {}) {
recordSkip('response', 'unsupported-node-response');
return;
}
wrapNodeResponse(response, request ? reqContextFromFetch(request, clientForResponse(request, [])) : undefined,
options?.headersOnly === true);
wrapNodeResponse(response, request ? reqContextFromFetch(request, clientForResponse(request, [])) : undefined, options);
},

// (request) => Response | null (null = allow, caller proceeds). Request phase only.
Expand Down
3 changes: 2 additions & 1 deletion src/protect/templates/nuxt-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default defineEventHandler(async (event) => {
if (contentType) setResponseHeader(event, "content-type", contentType);
return blocked.text(); // 403 — blocked before it reaches your route
}
protection.screenNodeResponse(event.node?.res, request);
// Keep incrementally written responses streaming while screening their headers.
protection.screenNodeResponse(event.node?.res, request, { streamOnWrite: true });
});
// #endregion patchstack-nuxt
Loading