Unverified Commit 3f4012ee authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add module-fragment empty-part regression tests

Cover the released pipe-readable and response-cache-utils inputs from #98128, and assert split_module part ids remain inside the emitted module list. The placeholder handling from #95516 makes these pass; reverting to skipping empty chunks reproduces the len 8/index 9 panic.
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 6469906e
use std::{collections::BTreeMap, fmt::Write, hash::Hash, path::PathBuf, sync::Arc}; use std::{collections::BTreeMap, fmt::Write, hash::Hash, path::PathBuf, sync::Arc};
use anyhow::Error; use anyhow::Error;
use rustc_hash::FxHashMap;
use serde::Deserialize; use serde::Deserialize;
use swc_core::{ use swc_core::{
atoms::{Wtf8Atom, atom}, atoms::{Wtf8Atom, atom},
...@@ -18,7 +19,8 @@ use turbo_tasks::FxIndexSet; ...@@ -18,7 +19,8 @@ use turbo_tasks::FxIndexSet;
use super::{ use super::{
Analyzer, Key, Analyzer, Key,
graph::{ graph::{
DepGraph, Dependency, InternedGraph, ItemId, ItemIdGroupKind, Mode, SplitModuleResult, DepGraph, Dependency, InternedGraph, ItemId, ItemIdGroupKind, Mode, PartId,
SplitModuleResult,
}, },
merge::Merger, merge::Merger,
}; };
...@@ -188,9 +190,12 @@ fn run(input: PathBuf) { ...@@ -188,9 +190,12 @@ fn run(input: PathBuf) {
let SplitModuleResult { let SplitModuleResult {
modules, modules,
entrypoints, entrypoints,
part_deps,
.. ..
} = g.split_module(&[], analyzer.items); } = g.split_module(&[], analyzer.items);
assert_part_ids_in_bounds(&modules, &entrypoints, &part_deps);
writeln!( writeln!(
s, s,
"# Entrypoints\n\n```\n{:#?}\n```\n\n", "# Entrypoints\n\n```\n{:#?}\n```\n\n",
...@@ -262,6 +267,37 @@ fn run(input: PathBuf) { ...@@ -262,6 +267,37 @@ fn run(input: PathBuf) {
.unwrap(); .unwrap();
} }
/// Every part id produced by `split_module` must be a valid position in `modules`.
/// Empty groups must not be dropped from `modules`; dropping them makes the indices point at
/// the wrong fragment or past the end of the vector.
fn assert_part_ids_in_bounds(
modules: &[Module],
entrypoints: &FxHashMap<Key, u32>,
part_deps: &FxHashMap<u32, Vec<PartId>>,
) {
let len = modules.len();
for (key, &ix) in entrypoints {
assert!(
(ix as usize) < len,
"entrypoint {key:?} points to part {ix}, but there are only {len} parts"
);
}
for (&part, deps) in part_deps {
assert!(
(part as usize) < len,
"part_deps contains part {part}, but there are only {len} parts"
);
for dep in deps {
if let PartId::Internal(ix, _) = dep {
assert!(
(*ix as usize) < len,
"part {part} depends on part {ix}, but there are only {len} parts"
);
}
}
}
}
struct SingleModuleLoader<'a> { struct SingleModuleLoader<'a> {
entry_module_uri: &'a Wtf8Atom, entry_module_uri: &'a Wtf8Atom,
modules: &'a [Module], modules: &'a [Module],
......
{
"exports": [
["isAbortError"],
["pipeToNodeResponse"],
["pipeNodeReadableToNodeResponse"]
]
}
import { ResponseAbortedName, createAbortController } from './web/spec-extension/adapters/next-request';
import { DetachedPromise } from '../lib/detached-promise';
import { getTracer } from './lib/trace/tracer';
import { NextNodeServerSpan } from './lib/trace/constants';
import { getClientComponentLoaderMetrics } from './client-component-renderer-logger';
export function isAbortError(e) {
return (e == null ? void 0 : e.name) === 'AbortError' || (e == null ? void 0 : e.name) === ResponseAbortedName;
}
const HAS_CLIENT_COMPONENT_METRICS_ENABLED = 'performance' in globalThis && process.env.NEXT_OTEL_PERFORMANCE_PREFIX;
function createWriterFromResponse(res, waitUntilForEnd) {
let started = false;
// Create a promise that will resolve once the response has drained. See
// https://nodejs.org/api/stream.html#stream_event_drain
let drained = new DetachedPromise();
function onDrain() {
drained.resolve();
}
res.on('drain', onDrain);
// If the finish event fires, it means we shouldn't block and wait for the
// drain event.
res.once('close', ()=>{
res.off('drain', onDrain);
drained.resolve();
});
// Create a promise that will resolve once the response has finished. See
// https://nodejs.org/api/http.html#event-finish_1
const finished = new DetachedPromise();
res.once('finish', ()=>{
finished.resolve();
});
// Create a writable stream that will write to the response.
return new WritableStream({
write: async (chunk)=>{
// You'd think we'd want to use `start` instead of placing this in `write`
// but this ensures that we don't actually flush the headers until we've
// started writing chunks.
if (!started) {
started = true;
if (HAS_CLIENT_COMPONENT_METRICS_ENABLED) {
const metrics = getClientComponentLoaderMetrics();
if (metrics) {
performance.measure(`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`, {
start: metrics.clientComponentLoadStart,
end: metrics.clientComponentLoadStart + metrics.clientComponentLoadTimes
});
}
}
res.flushHeaders();
getTracer().trace(NextNodeServerSpan.startResponse, {
spanName: 'start response'
}, ()=>undefined);
}
try {
const ok = res.write(chunk);
// Added by the `compression` middleware, this is a function that will
// flush the partially-compressed response to the client.
if ('flush' in res && typeof res.flush === 'function') {
res.flush();
}
// If the write returns false, it means there's some backpressure, so
// wait until it's streamed before continuing.
if (!ok) {
await drained.promise;
// Reset the drained promise so that we can wait for the next drain event.
drained = new DetachedPromise();
}
} catch (err) {
res.end();
throw Object.defineProperty(new Error('failed to write chunk to response', {
cause: err
}), "__NEXT_ERROR_CODE", {
value: "E321",
enumerable: false,
configurable: true
});
}
},
abort: (err)=>{
if (res.writableFinished) return;
res.destroy(err);
},
close: async ()=>{
// if a waitUntil promise was passed, wait for it to resolve before
// ending the response.
if (waitUntilForEnd) {
await waitUntilForEnd;
}
if (res.writableFinished) return;
res.end();
return finished.promise;
}
});
}
export async function pipeToNodeResponse(readable, res, waitUntilForEnd) {
try {
// If the response has already errored, then just return now.
const { errored, destroyed } = res;
if (errored || destroyed) return;
// Create a new AbortController so that we can abort the readable if the
// client disconnects.
const controller = createAbortController(res);
const writer = createWriterFromResponse(res, waitUntilForEnd);
await readable.pipeTo(writer, {
signal: controller.signal
});
} catch (err) {
// If this isn't related to an abort error, re-throw it.
if (isAbortError(err)) return;
throw Object.defineProperty(new Error('failed to pipe response', {
cause: err
}), "__NEXT_ERROR_CODE", {
value: "E180",
enumerable: false,
configurable: true
});
}
}
export async function pipeNodeReadableToNodeResponse(readable, res, waitUntilForEnd) {
try {
const { errored, destroyed } = res;
if (errored || destroyed) return;
let started = false;
const finished = new DetachedPromise();
res.once('close', ()=>{
readable.destroy();
finished.resolve();
});
readable.on('data', (chunk)=>{
if (!started) {
started = true;
if ('performance' in globalThis && process.env.NEXT_OTEL_PERFORMANCE_PREFIX) {
const metrics = getClientComponentLoaderMetrics();
if (metrics) {
performance.measure(`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`, {
start: metrics.clientComponentLoadStart,
end: metrics.clientComponentLoadStart + metrics.clientComponentLoadTimes
});
}
}
res.flushHeaders();
getTracer().trace(NextNodeServerSpan.startResponse, {
spanName: 'start response'
}, ()=>undefined);
}
const ok = res.write(chunk);
if ('flush' in res && typeof res.flush === 'function') {
res.flush();
}
if (!ok) {
readable.pause();
res.once('drain', ()=>{
readable.resume();
});
}
});
readable.on('end', async ()=>{
if (waitUntilForEnd) {
await waitUntilForEnd;
}
if (!res.writableFinished) {
res.end();
}
finished.resolve();
});
readable.on('error', (err)=>{
if (isAbortError(err)) {
finished.resolve();
return;
}
res.destroy(err);
finished.resolve();
});
await finished.promise;
} catch (err) {
if (isAbortError(err)) return;
throw Object.defineProperty(new Error('failed to pipe response', {
cause: err
}), "__NEXT_ERROR_CODE", {
value: "E180",
enumerable: false,
configurable: true
});
}
}
{
"exports": [
["fromResponseCacheEntry"],
["routeKindToIncrementalCacheKind"],
["fromResponseCacheEntry", "toResponseCacheEntry"]
]
}
import { CachedRouteKind, IncrementalCacheKind } from './types';
import RenderResult from '../render-result';
import { RouteKind } from '../route-kind';
import { HTML_CONTENT_TYPE_HEADER } from '../../lib/constants';
export async function fromResponseCacheEntry(cacheEntry) {
var _cacheEntry_value, _cacheEntry_value1;
return {
...cacheEntry,
value: ((_cacheEntry_value = cacheEntry.value) == null ? void 0 : _cacheEntry_value.kind) === CachedRouteKind.PAGES ? {
kind: CachedRouteKind.PAGES,
html: await cacheEntry.value.html.toUnchunkedString(true),
pageData: cacheEntry.value.pageData,
headers: cacheEntry.value.headers,
status: cacheEntry.value.status
} : ((_cacheEntry_value1 = cacheEntry.value) == null ? void 0 : _cacheEntry_value1.kind) === CachedRouteKind.APP_PAGE ? {
kind: CachedRouteKind.APP_PAGE,
html: await cacheEntry.value.html.toUnchunkedString(true),
postponed: cacheEntry.value.postponed,
rscData: cacheEntry.value.rscData,
headers: cacheEntry.value.headers,
status: cacheEntry.value.status,
segmentData: cacheEntry.value.segmentData
} : cacheEntry.value
};
}
export async function toResponseCacheEntry(response) {
var _response_value, _response_value1;
if (!response) return null;
return {
isMiss: response.isMiss,
isStale: response.isStale,
cacheControl: response.cacheControl,
isFallback: response.isFallback,
value: ((_response_value = response.value) == null ? void 0 : _response_value.kind) === CachedRouteKind.PAGES ? {
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(response.value.html, HTML_CONTENT_TYPE_HEADER),
pageData: response.value.pageData,
headers: response.value.headers,
status: response.value.status
} : ((_response_value1 = response.value) == null ? void 0 : _response_value1.kind) === CachedRouteKind.APP_PAGE ? {
kind: CachedRouteKind.APP_PAGE,
html: RenderResult.fromStatic(response.value.html, HTML_CONTENT_TYPE_HEADER),
rscData: response.value.rscData,
headers: response.value.headers,
status: response.value.status,
postponed: response.value.postponed,
segmentData: response.value.segmentData
} : response.value
};
}
export function routeKindToIncrementalCacheKind(routeKind) {
switch(routeKind){
case RouteKind.PAGES:
return IncrementalCacheKind.PAGES;
case RouteKind.APP_PAGE:
return IncrementalCacheKind.APP_PAGE;
case RouteKind.IMAGE:
return IncrementalCacheKind.IMAGE;
case RouteKind.APP_ROUTE:
return IncrementalCacheKind.APP_ROUTE;
case RouteKind.PAGES_API:
// Pages Router API routes are not cached in the incremental cache.
throw Object.defineProperty(new Error(`Unexpected route kind ${routeKind}`), "__NEXT_ERROR_CODE", {
value: "E64",
enumerable: false,
configurable: true
});
default:
return routeKind;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment