Home / File/ sequence.ts — astro Source File

sequence.ts — astro Source File

Architecture documentation for sequence.ts, a typescript file in the astro codebase. 8 imports, 0 dependents.

File typescript CoreAstro CoreMiddleware 8 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  16288903_f811_ef4a_1a6e_6d77b1db7c8b["sequence.ts"]
  135a8084_d596_67c2_9209_cca6693604e6["../types/public/common.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> 135a8084_d596_67c2_9209_cca6693604e6
  28857b9f_4720_3f29_4abb_a7eec34dcca5["../types/public/context.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> 28857b9f_4720_3f29_4abb_a7eec34dcca5
  7216d952_4e4a_2d18_a85b_74b4ace79e2b["../core/constants.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> 7216d952_4e4a_2d18_a85b_74b4ace79e2b
  8df634da_0f30_1e1f_1314_2439b0c9baab["../core/errors/errors-data.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> 8df634da_0f30_1e1f_1314_2439b0c9baab
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  feb254e4_908a_ebac_cf06_c741bffea12a["../core/render/index.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> feb254e4_908a_ebac_cf06_c741bffea12a
  99bef061_4ac1_91d4_947d_810691aafb80["../core/routing/rewrite.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> 99bef061_4ac1_91d4_947d_810691aafb80
  2f137c79_e098_e0b1_ec7d_fe3ae33551ac["./defineMiddleware.js"]
  16288903_f811_ef4a_1a6e_6d77b1db7c8b --> 2f137c79_e098_e0b1_ec7d_fe3ae33551ac
  style 16288903_f811_ef4a_1a6e_6d77b1db7c8b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { MiddlewareHandler, RewritePayload } from '../../types/public/common.js';
import type { APIContext } from '../../types/public/context.js';
import { pipelineSymbol } from '../constants.js';
import { ForbiddenRewrite } from '../errors/errors-data.js';
import { AstroError } from '../errors/index.js';
import { getParams, type Pipeline } from '../render/index.js';
import { setOriginPathname } from '../routing/rewrite.js';
import { defineMiddleware } from './defineMiddleware.js';

// From SvelteKit: https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/hooks/sequence.js
/**
 *
 * It accepts one or more middleware handlers and makes sure that they are run in sequence.
 */
export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler {
	const filtered = handlers.filter((h) => !!h);
	const length = filtered.length;
	if (!length) {
		return defineMiddleware((_context, next) => {
			return next();
		});
	}
	return defineMiddleware((context, next) => {
		/**
		 * This variable is used to carry the rerouting payload across middleware functions.
		 */
		let carriedPayload: RewritePayload | undefined = undefined;
		return applyHandle(0, context);

		function applyHandle(i: number, handleContext: APIContext) {
			const handle = filtered[i];
			// @ts-expect-error
			// SAFETY: Usually `next` always returns something in user land, but in `sequence` we are actually
			// doing a loop over all the `next` functions, and eventually we call the last `next` that returns the `Response`.
			const result = handle(handleContext, async (payload?: RewritePayload) => {
				if (i < length - 1) {
					if (payload) {
						let newRequest;
						if (payload instanceof Request) {
							newRequest = payload;
						} else if (payload instanceof URL) {
							// Cloning the original request ensures that the new Request gets its own copy of the body stream
							// Without this it will throw an error if they both try to consume the stream, which will happen in a rewrite
							newRequest = new Request(payload, handleContext.request.clone());
						} else {
							newRequest = new Request(
								new URL(payload, handleContext.url.origin),
								handleContext.request.clone(),
							);
						}
						const oldPathname = handleContext.url.pathname;
						const pipeline: Pipeline = Reflect.get(handleContext, pipelineSymbol);
						const { routeData, pathname } = await pipeline.tryRewrite(
							payload,
							handleContext.request,
						);

						// This is a case where the user tries to rewrite from a SSR route to a prerendered route (SSG).
						// This case isn't valid because when building for SSR, the prerendered route disappears from the server output because it becomes an HTML file,
						// so Astro can't retrieve it from the emitted manifest.
						if (
							pipeline.manifest.serverLike === true &&
							handleContext.isPrerendered === false &&
							routeData.prerender === true
						) {
							throw new AstroError({
								...ForbiddenRewrite,
								message: ForbiddenRewrite.message(
									handleContext.url.pathname,
									pathname,
									routeData.component,
								),
								hint: ForbiddenRewrite.hint(routeData.component),
							});
						}

						carriedPayload = payload;
						handleContext.request = newRequest;
						handleContext.url = new URL(newRequest.url);
						handleContext.params = getParams(routeData, pathname);
						handleContext.routePattern = routeData.route;
						setOriginPathname(
							handleContext.request,
							oldPathname,
							pipeline.manifest.trailingSlash,
							pipeline.manifest.buildFormat,
						);
					}
					return applyHandle(i + 1, handleContext);
				} else {
					return next(payload ?? carriedPayload);
				}
			});
			return result;
		}
	});
}

Domain

Subdomains

Functions

Dependencies

  • ../core/constants.js
  • ../core/errors/errors-data.js
  • ../core/errors/index.js
  • ../core/render/index.js
  • ../core/routing/rewrite.js
  • ../types/public/common.js
  • ../types/public/context.js
  • ./defineMiddleware.js

Frequently Asked Questions

What does sequence.ts do?
sequence.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in sequence.ts?
sequence.ts defines 1 function(s): sequence.
What does sequence.ts depend on?
sequence.ts imports 8 module(s): ../core/constants.js, ../core/errors/errors-data.js, ../core/errors/index.js, ../core/render/index.js, ../core/routing/rewrite.js, ../types/public/common.js, ../types/public/context.js, ./defineMiddleware.js.
Where is sequence.ts in the architecture?
sequence.ts is located at packages/astro/src/core/middleware/sequence.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/astro/src/core/middleware).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free